Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 113 additions & 7 deletions runtime/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { access, readdir, stat } from "node:fs/promises";
import { access, readdir, readFile, stat } from "node:fs/promises";
import { constants } from "node:fs";
import { delimiter, dirname, isAbsolute, join } from "node:path";
import {
basename,
delimiter,
dirname,
isAbsolute,
join,
normalize,
resolve,
sep,
} from "node:path";
import { homedir } from "node:os";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
Expand Down Expand Up @@ -95,6 +104,89 @@ async function direct(
return null;
}

const WINDOWS_DSH_SHIM =
/^\s*"%~dp0\\?([^"%\r\n]+\.exe)"\s+--expose-internals\s+"%~dp0\\?([^"%\r\n]+\.(?:c|m)?js)"\s+%\*\s*$/i;

async function dshInstallCommand(
executable: string,
script: string,
): Promise<Command | null> {
// Bind the entry to the installation that owns the executable, so a changed
// .exe cannot keep a saved worker approval for the same script identity.
if (basename(executable).toLowerCase() !== "deepseek harness.exe")
return null;
const appAsar = join(dirname(executable), "resources", "app.asar");
if (!script.toLowerCase().startsWith((appAsar + sep).toLowerCase()))
return null;
// A leftover shim from an uninstall must not look "installed", and must not
// block the node_modules fallback in the same directory.
if (!(await exists(executable)) || !(await exists(appAsar))) return null;
return {
command: executable,
prefix: ["--expose-internals", script],
identity: script,
env: { ELECTRON_RUN_AS_NODE: "1" },
};
}

/**
* A saved dsh identity points inside the installer's app.asar, which ordinary
* filesystem access cannot read. Rebuild the command from the owning
* installation before the configured-path existence check rejects it.
*/
async function savedArchiveCommand(
name: string,
configured: string,
): Promise<Command | null> {
if (name !== "dsh" || process.platform !== "win32" || !isAbsolute(configured))
return null;
const normalized = normalize(configured);
// A saved identity must be the exact entry the shim resolved. Reject `..`
// segments that would redirect an approved identity to another installation.
if (normalized.toLowerCase() !== configured.toLowerCase()) return null;
const parts = normalized.split(sep);
const archive = parts.findLastIndex(
(part) => part.toLowerCase() === "app.asar",
);
if (
archive < 2 ||
parts[archive - 1]?.toLowerCase() !== "resources" ||
archive === parts.length - 1 ||
!/\.(?:c|m)?js$/i.test(parts[parts.length - 1])
)
return null;
const install = parts.slice(0, archive - 1).join(sep);
return dshInstallCommand(join(install, "DeepSeek Harness.exe"), normalized);
}

/**
* The launcher installed by the official DeepSeek Harness CLI is a .cmd shim
* that delegates to an Electron executable. Worker sessions spawn with
* `shell: false`, which cannot run a batch file on Windows. Normalize the shim
* into the executable, argument prefix and environment the installer declares,
* so every existing argument-array spawn site works unchanged.
*/
async function windowsShimCommand(path: string): Promise<Command | null> {
let text: string;
try {
text = await readFile(path, "utf8");
} catch {
return null;
}
// Only a line that fully matches the official launcher shape counts. A
// comment or echo that merely mentions the flag cannot shadow the real line.
let match: RegExpMatchArray | null = null;
for (const line of text.split(/\r?\n/)) {
match = line.match(WINDOWS_DSH_SHIM);
if (match) break;
}
if (!match) return null;
const base = dirname(path);
const command = resolve(base, match[1]);
const script = resolve(base, match[2]);
return dshInstallCommand(command, script);
}

async function cursorRuntime(): Promise<Command | null> {
const root = join(homedir(), "AppData", "Local", "cursor-agent", "versions");
try {
Expand Down Expand Up @@ -197,6 +289,8 @@ export async function findCommand(
options: MacCommandOptions & { platform?: NodeJS.Platform } = {},
): Promise<Command | null> {
if (configured) {
const archived = await savedArchiveCommand(name, configured);
if (archived) return archived;
if (!isAbsolute(configured) || !(await exists(configured))) return null;
if (/\.(cjs|mjs|js)$/i.test(configured))
return {
Expand Down Expand Up @@ -227,19 +321,31 @@ export async function findCommand(
.filter(Boolean),
),
];
const extensions =
process.platform === "win32"
? name === "dsh"
? [".exe", ".cmd"]
: [".exe"]
: [""];
for (const dir of dirs) {
const names = name === "cursor" ? ["cursor-agent", "agent"] : [name];
for (const candidate of names) {
const path = join(
dir,
candidate + (process.platform === "win32" ? ".exe" : ""),
);
if ((await exists(path)) && (await stat(path)).isFile())
for (const extension of extensions) {
const path = join(dir, candidate + extension);
if (!((await exists(path)) && (await stat(path)).isFile())) continue;
// A batch shim cannot be passed to spawn with `shell: false`.
if (extension === ".cmd") {
const normalized =
name === "dsh" ? await windowsShimCommand(path) : null;
if (normalized) return normalized;
continue;
}
return {
command: path,
prefix: [],
...(name === "cursor" ? { env: { CURSOR_INVOKED_AS: "agent" } } : {}),
};
}
}
const packageEntry =
name === "memtrace"
Expand Down
39 changes: 39 additions & 0 deletions tests/linux-command-discovery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { findCommand } from "../runtime/commands";

async function linuxPathFixture(t: test.TestContext, names: string[]) {
const root = await mkdtemp(join(tmpdir(), "memcode-linux-discovery-"));
t.after(() => rm(root, { recursive: true, force: true }));
const commands = new Map<string, string>();
for (const name of names) {
const path = join(root, name);
await writeFile(path, "#!/bin/sh\nexit 0\n", { mode: 0o755 });
commands.set(name, path);
}
const beforePath = process.env.PATH;
const beforePlatform = Object.getOwnPropertyDescriptor(process, "platform");
process.env.PATH = root;
// findCommand reads process.platform directly. Stub it so this regression is
// observable on a Windows development machine as well as on Linux CI.
Object.defineProperty(process, "platform", {
value: "linux",
configurable: true,
});
t.after(() => {
if (beforePlatform)
Object.defineProperty(process, "platform", beforePlatform);
if (beforePath === undefined) delete process.env.PATH;
else process.env.PATH = beforePath;
});
return commands;
}

test("extensionless PATH executables are discovered on Linux", async (t) => {
const commands = await linuxPathFixture(t, ["git", "gh", "dsh"]);
for (const [name, path] of commands)
assert.deepEqual(await findCommand(name), { command: path, prefix: [] });
});
Loading