context-mode 2.1.6 → 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.zcode-plugin/plugin.json +1 -1
- package/README.md +72 -72
- package/SHA256SUMS +16 -16
- package/cli.mjs +1005 -634
- package/copilot/plugin.json +1 -1
- package/gemini-extension.json +1 -1
- package/hook.mjs +96 -96
- package/opencode/plugin.js +689 -287
- package/package.json +1 -1
- package/pi/index.js +554 -190
- package/qwen-extension.json +1 -1
- package/run.mjs +195 -32
- package/search.mjs +195 -32
- package/server.mjs +479 -162
package/qwen-extension.json
CHANGED
package/run.mjs
CHANGED
|
@@ -158,6 +158,8 @@ var DECAY_X1000 = Object.freeze([
|
|
|
158
158
|
]);
|
|
159
159
|
var EXEC_TIMEOUT_MS = 6e5;
|
|
160
160
|
var KILL_GRACE_MS = 1e3;
|
|
161
|
+
var PIPE_EOF_GRACE_MS = 250;
|
|
162
|
+
var PIPE_DRAIN_MAX_MS = 2e3;
|
|
161
163
|
var DB_TIMEOUT_MS = 5e3;
|
|
162
164
|
var DB_WAL_SUFFIX = "-wal";
|
|
163
165
|
var DB_SHM_SUFFIX = "-shm";
|
|
@@ -379,6 +381,89 @@ var OPS_SPLIT_FILES = Object.freeze([
|
|
|
379
381
|
"v1-remove.ts",
|
|
380
382
|
"uninstall.ts"
|
|
381
383
|
]);
|
|
384
|
+
var unsupported = (h) => Object.freeze(h);
|
|
385
|
+
var UNSUPPORTED_HOSTS = Object.freeze([
|
|
386
|
+
unsupported({
|
|
387
|
+
id: "antigravity",
|
|
388
|
+
name: "Antigravity",
|
|
389
|
+
clientNames: ["antigravity", "antigravity-client", "antigravity-cli", "agy"],
|
|
390
|
+
envVars: ["ANTIGRAVITY_CLI_ALIAS"],
|
|
391
|
+
pluginPaths: [[".gemini", "config", "plugins"], [".gemini", "antigravity", "plugins"], [".gemini", "antigravity-cli", "plugins"], [".gemini", "antigravity-ide", "plugins"]],
|
|
392
|
+
configDirs: [".gemini/antigravity", ".gemini/antigravity-cli", ".gemini/config"],
|
|
393
|
+
binaries: ["agy"],
|
|
394
|
+
source: "v1.0.169:src/adapters/client-map.ts:15-17; detect.ts:156,481-483; #1340"
|
|
395
|
+
}),
|
|
396
|
+
unsupported({
|
|
397
|
+
id: "cursor",
|
|
398
|
+
name: "Cursor",
|
|
399
|
+
clientNames: ["cursor-vscode"],
|
|
400
|
+
envVars: ["CURSOR_TRACE_ID", "CURSOR_CLI", "CURSOR_CWD"],
|
|
401
|
+
pluginPaths: [],
|
|
402
|
+
configDirs: [".cursor"],
|
|
403
|
+
binaries: [],
|
|
404
|
+
source: "v1.0.169:src/adapters/client-map.ts:18; detect.ts:163-165,584"
|
|
405
|
+
}),
|
|
406
|
+
unsupported({
|
|
407
|
+
id: "omp",
|
|
408
|
+
name: "OMP (oh-my-pi)",
|
|
409
|
+
clientNames: ["omp-coding-agent"],
|
|
410
|
+
envVars: [],
|
|
411
|
+
pluginPaths: [],
|
|
412
|
+
configDirs: [".omp"],
|
|
413
|
+
binaries: ["omp"],
|
|
414
|
+
source: "v1.0.169:src/adapters/client-map.ts:37; detect.ts:543 (PI_CODING_AGENT_DIR is not a signal: supported Pi reads it too)"
|
|
415
|
+
}),
|
|
416
|
+
unsupported({
|
|
417
|
+
id: "openclaw",
|
|
418
|
+
name: "OpenClaw",
|
|
419
|
+
clientNames: [],
|
|
420
|
+
envVars: [],
|
|
421
|
+
pluginPaths: [],
|
|
422
|
+
configDirs: [".openclaw"],
|
|
423
|
+
binaries: [],
|
|
424
|
+
source: "v1.0.169:src/adapters/detect.ts:252,575 (the runtime sets no env var)"
|
|
425
|
+
}),
|
|
426
|
+
unsupported({
|
|
427
|
+
id: "kimi",
|
|
428
|
+
name: "Kimi Code",
|
|
429
|
+
clientNames: ["kimi-code", "kimi"],
|
|
430
|
+
envVars: [],
|
|
431
|
+
pluginPaths: [],
|
|
432
|
+
configDirs: [".kimi-code"],
|
|
433
|
+
binaries: ["kimi"],
|
|
434
|
+
source: "v1.0.169:src/adapters/client-map.ts:42-44; detect.ts:567; adapters/kimi/index.ts:127"
|
|
435
|
+
}),
|
|
436
|
+
unsupported({
|
|
437
|
+
id: "kilo",
|
|
438
|
+
name: "Kilo Code",
|
|
439
|
+
clientNames: ["kilo code"],
|
|
440
|
+
envVars: ["KILO", "KILO_PID"],
|
|
441
|
+
pluginPaths: [],
|
|
442
|
+
configDirs: [".config/kilo"],
|
|
443
|
+
binaries: [],
|
|
444
|
+
source: "v1.0.169:src/adapters/client-map.ts:28; detect.ts:170-171,592"
|
|
445
|
+
}),
|
|
446
|
+
unsupported({
|
|
447
|
+
id: "zed",
|
|
448
|
+
name: "Zed",
|
|
449
|
+
clientNames: ["zed"],
|
|
450
|
+
envVars: ["ZED_SESSION_ID", "ZED_TERM"],
|
|
451
|
+
pluginPaths: [],
|
|
452
|
+
configDirs: [".config/zed"],
|
|
453
|
+
binaries: [],
|
|
454
|
+
source: "v1.0.169:src/adapters/client-map.ts:38-39; detect.ts:189-190,616"
|
|
455
|
+
}),
|
|
456
|
+
unsupported({
|
|
457
|
+
id: "jetbrains-copilot",
|
|
458
|
+
name: "JetBrains Copilot",
|
|
459
|
+
clientNames: ["jetbrains client", "intellij idea", "pycharm"],
|
|
460
|
+
envVars: ["IDEA_INITIAL_DIRECTORY"],
|
|
461
|
+
pluginPaths: [],
|
|
462
|
+
configDirs: [".config/JetBrains"],
|
|
463
|
+
binaries: [],
|
|
464
|
+
source: "v1.0.169:src/adapters/client-map.ts:23-25; detect.ts:213,600"
|
|
465
|
+
})
|
|
466
|
+
]);
|
|
382
467
|
var HOOK_HOST_FILE = Object.freeze({
|
|
383
468
|
"claude-code": { root: "plugin", file: ["hooks", "claude-code.json"], key: ["hooks"], shape: "nested", timeoutKey: "timeout", matcher: true },
|
|
384
469
|
codex: { root: "home", file: [".codex", "hooks.json"], key: ["hooks"], shape: "nested", timeoutKey: "timeout", matcher: true },
|
|
@@ -424,6 +509,14 @@ var V1_SCRIPT_NAMES = Object.freeze([
|
|
|
424
509
|
"userpromptsubmit.mjs"
|
|
425
510
|
]);
|
|
426
511
|
var V1_CODEX_HOOKS_DIR = Object.freeze(["hooks", "codex"]);
|
|
512
|
+
var V1_CODEX_SCRIPT_NAMES = Object.freeze([
|
|
513
|
+
"pretooluse.mjs",
|
|
514
|
+
"posttooluse.mjs",
|
|
515
|
+
"sessionstart.mjs",
|
|
516
|
+
"precompact.mjs",
|
|
517
|
+
"userpromptsubmit.mjs",
|
|
518
|
+
"stop.mjs"
|
|
519
|
+
]);
|
|
427
520
|
var V1_CACHE_HEAL_EXTS = Object.freeze([".mjs", ".sh"]);
|
|
428
521
|
var V1_PI_PACKAGE = "npm:context-mode";
|
|
429
522
|
var CLAUDE_CONFIG_DIR_ENV = "CLAUDE_CONFIG_DIR";
|
|
@@ -456,7 +549,7 @@ var V1_REASON = Object.freeze({
|
|
|
456
549
|
cacheHealFile: "old context-mode cache-heal script file",
|
|
457
550
|
statusLine: "old context-mode statusLine (context-mode statusline): report only, never removed",
|
|
458
551
|
mcpEntry: "old context-mode MCP entry mcpServers.context-mode",
|
|
459
|
-
codexHook: "old context-mode Codex hook entry: context-mode hook codex or hooks/codex
|
|
552
|
+
codexHook: "old context-mode Codex hook entry: context-mode hook codex or hooks/codex/<event>.mjs",
|
|
460
553
|
codexToml: "old context-mode config.toml [mcp_servers.context-mode*] section",
|
|
461
554
|
hookFile: "old context-mode hook file context-mode.json (runs context-mode hook)",
|
|
462
555
|
vscodeMcp: "old context-mode MCP entry servers.context-mode in .vscode/mcp.json: report only, never removed",
|
|
@@ -1286,6 +1379,7 @@ function readResult6(p) {
|
|
|
1286
1379
|
const text = p.content === void 0 ? "" : typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
1287
1380
|
const info = { isError: p.isError === true, bytes: Buffer.byteLength(text), text };
|
|
1288
1381
|
if (typeof p.exitCode === "number") info.exitCode = p.exitCode;
|
|
1382
|
+
if (p.keep === true) info.keep = true;
|
|
1289
1383
|
return info;
|
|
1290
1384
|
}
|
|
1291
1385
|
function decode9(raw) {
|
|
@@ -1362,6 +1456,11 @@ function decode10(raw) {
|
|
|
1362
1456
|
const event = Object.prototype.hasOwnProperty.call(OPENCODE_EVENTS, native) ? OPENCODE_EVENTS[native] : void 0;
|
|
1363
1457
|
if (event === void 0) throw new Error("opencode: unknown hook event " + String(p.type));
|
|
1364
1458
|
const ev = { event, agent: "opencode", sessionId: String(p.sessionID ?? ""), cwd: String(p.directory ?? "") };
|
|
1459
|
+
if (typeof p.agentID === "string" && p.agentID !== "") {
|
|
1460
|
+
ev.agentId = p.agentID;
|
|
1461
|
+
ev.agentType = p.agentID;
|
|
1462
|
+
}
|
|
1463
|
+
if (Array.isArray(p.agentTools)) ev.agentTools = p.agentTools.filter((t) => typeof t === "string");
|
|
1365
1464
|
if (event === "UserPromptSubmit" && typeof p.message === "string") ev.prompt = p.message;
|
|
1366
1465
|
if (event === "PreToolUse" || event === "PostToolUse") {
|
|
1367
1466
|
const name = String(p.tool ?? "");
|
|
@@ -1382,7 +1481,7 @@ function encode10(ev, d, _extra) {
|
|
|
1382
1481
|
if (ev.event !== "PreToolUse") return { stdout: "", exitCode: 0 };
|
|
1383
1482
|
return { stdout: d.rewritten === void 0 ? d.reason : d.rewritten + "\n" + d.reason, exitCode: 2 };
|
|
1384
1483
|
case "replace":
|
|
1385
|
-
return { stdout: "", exitCode: 0 };
|
|
1484
|
+
return { stdout: ev.event === "PostToolUse" ? d.output : "", exitCode: 0 };
|
|
1386
1485
|
}
|
|
1387
1486
|
}
|
|
1388
1487
|
|
|
@@ -1587,7 +1686,7 @@ var ADAPTERS = Object.freeze([
|
|
|
1587
1686
|
}
|
|
1588
1687
|
]);
|
|
1589
1688
|
var BY_ID = new Map(ADAPTERS.map((r) => [r.id, r]));
|
|
1590
|
-
var REPLACE_ROWS = /* @__PURE__ */ new Set(["claude-code", "gemini", "copilot-cli", "pi"]);
|
|
1689
|
+
var REPLACE_ROWS = /* @__PURE__ */ new Set(["claude-code", "gemini", "copilot-cli", "pi", "opencode"]);
|
|
1591
1690
|
function hasReplace(row) {
|
|
1592
1691
|
return REPLACE_ROWS.has(row.id);
|
|
1593
1692
|
}
|
|
@@ -3693,7 +3792,7 @@ function moreLine(intent, label) {
|
|
|
3693
3792
|
}
|
|
3694
3793
|
|
|
3695
3794
|
// src/core/policy/decide.ts
|
|
3696
|
-
import { resolve as
|
|
3795
|
+
import { resolve as resolve5 } from "node:path";
|
|
3697
3796
|
|
|
3698
3797
|
// src/core/policy/lexer.ts
|
|
3699
3798
|
var OPS = ["&&", "||", ";;", "|&", ";", "|", "&", "(", ")"];
|
|
@@ -5357,10 +5456,10 @@ function READ_DENY_RANGE_MESSAGE(path3, size) {
|
|
|
5357
5456
|
}
|
|
5358
5457
|
|
|
5359
5458
|
// src/core/policy/own-file.ts
|
|
5360
|
-
import { extname, join as join5 } from "node:path";
|
|
5459
|
+
import { extname, join as join5, resolve as resolve4 } from "node:path";
|
|
5361
5460
|
|
|
5362
5461
|
// src/core/project/contain.ts
|
|
5363
|
-
import { dirname as dirname2, join as join4, relative as relative2, sep as sep2 } from "node:path";
|
|
5462
|
+
import { dirname as dirname2, isAbsolute as isAbsolute2, join as join4, relative as relative2, resolve as resolve3, sep as sep2 } from "node:path";
|
|
5364
5463
|
var { lstatSync, realpathSync: realpathSync2 } = process.getBuiltinModule("node:fs");
|
|
5365
5464
|
function real(p) {
|
|
5366
5465
|
try {
|
|
@@ -5392,6 +5491,29 @@ function isInsideAny(roots, p) {
|
|
|
5392
5491
|
return qc.length >= rc.length && rc.every((c, i) => c === qc[i]);
|
|
5393
5492
|
});
|
|
5394
5493
|
}
|
|
5494
|
+
function hasParentStep(cwd, p) {
|
|
5495
|
+
const parts = isAbsolute2(p) ? components(p) : [...components(cwd), ...components(p)];
|
|
5496
|
+
return parts.includes("..");
|
|
5497
|
+
}
|
|
5498
|
+
var rootParts = /* @__PURE__ */ new Map();
|
|
5499
|
+
function lexicalRoot(root) {
|
|
5500
|
+
let rc = rootParts.get(root);
|
|
5501
|
+
if (rc === void 0) {
|
|
5502
|
+
rc = components(resolve3(root));
|
|
5503
|
+
rootParts.set(root, rc);
|
|
5504
|
+
}
|
|
5505
|
+
return rc;
|
|
5506
|
+
}
|
|
5507
|
+
function isLexicallyInsideAny(roots, p) {
|
|
5508
|
+
if (roots.length === 0) return false;
|
|
5509
|
+
const raw = components(p);
|
|
5510
|
+
if (raw.includes("..")) return false;
|
|
5511
|
+
const qc = sep2 === "/" && isAbsolute2(p) ? raw.filter((c) => c !== ".") : components(resolve3(p));
|
|
5512
|
+
return roots.some((root) => {
|
|
5513
|
+
const rc = lexicalRoot(root);
|
|
5514
|
+
return qc.length >= rc.length && rc.every((c, i) => c === qc[i]);
|
|
5515
|
+
});
|
|
5516
|
+
}
|
|
5395
5517
|
function commonRoot(paths) {
|
|
5396
5518
|
const reals = paths.map(real).filter((r) => r !== void 0);
|
|
5397
5519
|
if (reals.length === 0) throw new RangeError("commonRoot needs at least one existing path");
|
|
@@ -5417,9 +5539,11 @@ function ownDirsOf(agent, env) {
|
|
|
5417
5539
|
const own2 = agent === "claude-code" ? [claudeDirOf(env, home)] : agent === "codex" ? [codexDirOf(env, home)] : agent === "gemini" ? [geminiDirOf(env, home)] : agent === "copilot-cli" ? [copilotDirOf(env, home)] : agent === "kiro" ? [kiroDirOf(env, home)] : agent === "opencode" ? [opencodeDirOf(env, home)] : agent === "pi" ? [piAgentDirOf(env, home)] : agent === "vscode" ? [join5(home, ...AGENT_HOME_DIR.vscode), vscodeUserDirOf(env, home)] : [join5(home, ...AGENT_HOME_DIR[agent])];
|
|
5418
5540
|
return [...own2, shared];
|
|
5419
5541
|
}
|
|
5420
|
-
function isOwnFile(agent,
|
|
5421
|
-
if (extname(
|
|
5422
|
-
|
|
5542
|
+
function isOwnFile(agent, cwd, p, env = process.env) {
|
|
5543
|
+
if (extname(p).toLowerCase() !== AGENT_FILE_EXT) return false;
|
|
5544
|
+
const dirs = ownDirsOf(agent, env);
|
|
5545
|
+
const abs = resolve4(cwd, p);
|
|
5546
|
+
return !hasParentStep(cwd, p) && isLexicallyInsideAny(dirs, abs) || isInsideAny(dirs, abs);
|
|
5423
5547
|
}
|
|
5424
5548
|
|
|
5425
5549
|
// src/core/policy/read-media.ts
|
|
@@ -5510,7 +5634,7 @@ function readPaths(input) {
|
|
|
5510
5634
|
}
|
|
5511
5635
|
function userDenyHit(canonical, input, cwd, rules) {
|
|
5512
5636
|
if (rules.length === 0) return null;
|
|
5513
|
-
const readRule = (p) => matchesAnyDenyPattern(
|
|
5637
|
+
const readRule = (p) => matchesAnyDenyPattern(resolve5(cwd, p), rules, { tool: READ, projectDir: cwd });
|
|
5514
5638
|
if (canonical === BASH) {
|
|
5515
5639
|
const cmd = str(input.command);
|
|
5516
5640
|
return cmd === void 0 ? null : matchesAnyDenyPattern(cmd, rules);
|
|
@@ -5647,8 +5771,9 @@ function fileSize(abs) {
|
|
|
5647
5771
|
return -1;
|
|
5648
5772
|
}
|
|
5649
5773
|
}
|
|
5650
|
-
function bigDeny(ev, row,
|
|
5651
|
-
|
|
5774
|
+
function bigDeny(ev, row, cwd, p, size, root) {
|
|
5775
|
+
const abs = resolve5(cwd, p);
|
|
5776
|
+
if (size <= OUTPUT_CAP || isOwnFile(ev.agent, cwd, p)) return null;
|
|
5652
5777
|
return deny(executeCanOpen(root, abs) ? READ_DENY_MESSAGE(abs, size, row.toolNamePrefix) : READ_DENY_RANGE_MESSAGE(abs, size));
|
|
5653
5778
|
}
|
|
5654
5779
|
function executeCanOpen(root, abs) {
|
|
@@ -5663,10 +5788,10 @@ function executeCanOpen(root, abs) {
|
|
|
5663
5788
|
function read(ev, row, input, root) {
|
|
5664
5789
|
if (!present(input.offset) && !present(input.limit)) {
|
|
5665
5790
|
for (const p of readPaths(input)) {
|
|
5666
|
-
const abs =
|
|
5791
|
+
const abs = resolve5(ev.cwd, p);
|
|
5667
5792
|
const size = fileSize(abs);
|
|
5668
5793
|
if (size > OUTPUT_CAP && !isMediaRead(ev.agent, abs)) {
|
|
5669
|
-
const d = bigDeny(ev, row,
|
|
5794
|
+
const d = bigDeny(ev, row, ev.cwd, p, size, root);
|
|
5670
5795
|
if (d !== null) return d;
|
|
5671
5796
|
}
|
|
5672
5797
|
}
|
|
@@ -5679,7 +5804,7 @@ function shellDump(ev, cmds, row, root) {
|
|
|
5679
5804
|
let verdict = null;
|
|
5680
5805
|
for (const c of cmds) {
|
|
5681
5806
|
const name = c.argv0.toLowerCase();
|
|
5682
|
-
if (name === CD_COMMAND && c.args.length === 1 && c.insideConstruct === void 0) dir =
|
|
5807
|
+
if (name === CD_COMMAND && c.args.length === 1 && c.insideConstruct === void 0) dir = resolve5(dir, c.args[0]);
|
|
5683
5808
|
if (c.redirectsToFile || c.endsInBoundedSink || c.pipesOut === true || c.insideConstruct !== void 0) continue;
|
|
5684
5809
|
if (!has(SHELL_DUMP_COMMANDS, name)) continue;
|
|
5685
5810
|
const d = dumpTarget(name, c.args);
|
|
@@ -5688,8 +5813,8 @@ function shellDump(ev, cmds, row, root) {
|
|
|
5688
5813
|
verdict = ALLOW;
|
|
5689
5814
|
continue;
|
|
5690
5815
|
}
|
|
5691
|
-
const abs =
|
|
5692
|
-
const big = bigDeny(ev, row,
|
|
5816
|
+
const abs = resolve5(dir, d.file);
|
|
5817
|
+
const big = bigDeny(ev, row, dir, d.file, fileSize(abs), root);
|
|
5693
5818
|
if (big !== null) return big;
|
|
5694
5819
|
}
|
|
5695
5820
|
return verdict;
|
|
@@ -5697,7 +5822,7 @@ function shellDump(ev, cmds, row, root) {
|
|
|
5697
5822
|
function psShellDump(ev, cmd, row, root) {
|
|
5698
5823
|
if (row.nativeTools.read !== void 0 && row.nativeTools.read.length > 0 && dumpRunsThenReplaces(row)) return null;
|
|
5699
5824
|
for (const abs of psDumpFiles(cmd, ev.cwd)) {
|
|
5700
|
-
const d = bigDeny(ev, row, abs, fileSize(abs), root);
|
|
5825
|
+
const d = bigDeny(ev, row, ev.cwd, abs, fileSize(abs), root);
|
|
5701
5826
|
if (d !== null) return d;
|
|
5702
5827
|
}
|
|
5703
5828
|
return null;
|
|
@@ -5759,7 +5884,7 @@ function dumpTarget(name, args) {
|
|
|
5759
5884
|
}
|
|
5760
5885
|
|
|
5761
5886
|
// src/core/project/resolve.ts
|
|
5762
|
-
import { basename as basename2, dirname as dirname3, join as join6, resolve as
|
|
5887
|
+
import { basename as basename2, dirname as dirname3, join as join6, resolve as resolve6 } from "node:path";
|
|
5763
5888
|
|
|
5764
5889
|
// src/core/project/sha256.ts
|
|
5765
5890
|
var K = new Uint32Array([
|
|
@@ -5924,7 +6049,7 @@ function gitDirs(dir) {
|
|
|
5924
6049
|
const gitDir = gitDirAt(join6(cur, GIT_DIR_NAME));
|
|
5925
6050
|
if (gitDir !== null) {
|
|
5926
6051
|
const common = firstLine(join6(gitDir, GIT_COMMONDIR_FILE));
|
|
5927
|
-
return { commonDir: realpathSync3.native(common === null ? gitDir :
|
|
6052
|
+
return { commonDir: realpathSync3.native(common === null ? gitDir : resolve6(gitDir, common)), worktree: cur, start };
|
|
5928
6053
|
}
|
|
5929
6054
|
const up = dirname3(cur);
|
|
5930
6055
|
if (up === cur) return { commonDir: null, start };
|
|
@@ -5939,7 +6064,7 @@ function gitDirAt(dotGit) {
|
|
|
5939
6064
|
} else {
|
|
5940
6065
|
const line = firstLine(dotGit);
|
|
5941
6066
|
if (line === null || !line.startsWith(GITDIR_FILE_PREFIX)) return null;
|
|
5942
|
-
gitDir =
|
|
6067
|
+
gitDir = resolve6(dirname3(dotGit), line.slice(GITDIR_FILE_PREFIX.length));
|
|
5943
6068
|
}
|
|
5944
6069
|
} catch {
|
|
5945
6070
|
return null;
|
|
@@ -6121,11 +6246,11 @@ function noRuntimeError(lang) {
|
|
|
6121
6246
|
}
|
|
6122
6247
|
|
|
6123
6248
|
// src/core/sandbox/spawn.ts
|
|
6124
|
-
import { spawn } from "node:child_process";
|
|
6249
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
6125
6250
|
import path2 from "node:path";
|
|
6126
6251
|
|
|
6127
6252
|
// src/core/sandbox/kill.ts
|
|
6128
|
-
import {
|
|
6253
|
+
import { spawn } from "node:child_process";
|
|
6129
6254
|
function signalGroup(pid, signal) {
|
|
6130
6255
|
try {
|
|
6131
6256
|
process.kill(-pid, signal);
|
|
@@ -6139,7 +6264,10 @@ function killTree(pid) {
|
|
|
6139
6264
|
return;
|
|
6140
6265
|
}
|
|
6141
6266
|
try {
|
|
6142
|
-
|
|
6267
|
+
const t = spawn("taskkill", ["/F", "/T", "/PID", String(pid)], { stdio: "ignore", windowsHide: true });
|
|
6268
|
+
t.on("error", () => {
|
|
6269
|
+
});
|
|
6270
|
+
t.unref();
|
|
6143
6271
|
} catch {
|
|
6144
6272
|
}
|
|
6145
6273
|
}
|
|
@@ -6152,13 +6280,13 @@ function startArgv(argv) {
|
|
|
6152
6280
|
return sh === null ? null : [sh, ...argv];
|
|
6153
6281
|
}
|
|
6154
6282
|
function runProcess(argv, o) {
|
|
6155
|
-
return new Promise((
|
|
6283
|
+
return new Promise((resolve7) => {
|
|
6156
6284
|
const isWin2 = process.platform === "win32";
|
|
6157
6285
|
const start = startArgv(argv);
|
|
6158
|
-
if (start === null) return
|
|
6286
|
+
if (start === null) return resolve7(NOT_STARTED);
|
|
6159
6287
|
let child;
|
|
6160
6288
|
try {
|
|
6161
|
-
child =
|
|
6289
|
+
child = spawn2(start[0], start.slice(1), {
|
|
6162
6290
|
cwd: o.cwd,
|
|
6163
6291
|
env: o.env,
|
|
6164
6292
|
stdio: ["pipe", "pipe", "pipe"],
|
|
@@ -6169,14 +6297,20 @@ function runProcess(argv, o) {
|
|
|
6169
6297
|
windowsHide: true
|
|
6170
6298
|
});
|
|
6171
6299
|
} catch {
|
|
6172
|
-
return
|
|
6300
|
+
return resolve7(NOT_STARTED);
|
|
6173
6301
|
}
|
|
6174
6302
|
const stdoutChunks = [];
|
|
6175
6303
|
const stderrChunks = [];
|
|
6176
6304
|
let killedBy = null;
|
|
6177
6305
|
let settled = false;
|
|
6306
|
+
let held = false;
|
|
6307
|
+
let exited = false;
|
|
6308
|
+
let graceTimer;
|
|
6309
|
+
let drainTimer;
|
|
6310
|
+
let stopTimer;
|
|
6178
6311
|
const kill = () => {
|
|
6179
6312
|
if (child.pid !== void 0) killTree(child.pid);
|
|
6313
|
+
if (stopTimer === void 0) stopTimer = setTimeout(() => finishHeld(), KILL_GRACE_MS);
|
|
6180
6314
|
};
|
|
6181
6315
|
const onAbort = () => {
|
|
6182
6316
|
if (killedBy !== null || settled) return;
|
|
@@ -6193,17 +6327,46 @@ function runProcess(argv, o) {
|
|
|
6193
6327
|
if (settled) return;
|
|
6194
6328
|
settled = true;
|
|
6195
6329
|
clearTimeout(timer);
|
|
6330
|
+
clearTimeout(graceTimer);
|
|
6331
|
+
clearTimeout(drainTimer);
|
|
6332
|
+
clearTimeout(stopTimer);
|
|
6196
6333
|
o.signal.removeEventListener("abort", onAbort);
|
|
6197
|
-
|
|
6334
|
+
resolve7({
|
|
6198
6335
|
stdout: Buffer.concat(stdoutChunks),
|
|
6199
6336
|
stderr: Buffer.concat(stderrChunks),
|
|
6200
6337
|
exitCode,
|
|
6201
|
-
killed: killedBy
|
|
6338
|
+
killed: killedBy,
|
|
6339
|
+
...held ? { held: true } : {}
|
|
6202
6340
|
});
|
|
6203
6341
|
};
|
|
6204
|
-
|
|
6205
|
-
|
|
6342
|
+
const finishHeld = () => {
|
|
6343
|
+
if (settled) return;
|
|
6344
|
+
held = exited && killedBy === null;
|
|
6345
|
+
child.stdout.destroy();
|
|
6346
|
+
child.stderr.destroy();
|
|
6347
|
+
if (child.pid !== void 0) killTree(child.pid);
|
|
6348
|
+
settle(child.exitCode);
|
|
6349
|
+
};
|
|
6350
|
+
const bump = () => {
|
|
6351
|
+
if (!exited || settled) return;
|
|
6352
|
+
clearTimeout(graceTimer);
|
|
6353
|
+
graceTimer = setTimeout(finishHeld, PIPE_EOF_GRACE_MS);
|
|
6354
|
+
};
|
|
6355
|
+
child.stdout.on("data", (chunk) => {
|
|
6356
|
+
stdoutChunks.push(chunk);
|
|
6357
|
+
bump();
|
|
6358
|
+
});
|
|
6359
|
+
child.stderr.on("data", (chunk) => {
|
|
6360
|
+
stderrChunks.push(chunk);
|
|
6361
|
+
bump();
|
|
6362
|
+
});
|
|
6206
6363
|
child.on("close", (code) => settle(code));
|
|
6364
|
+
child.on("exit", () => {
|
|
6365
|
+
if (settled) return;
|
|
6366
|
+
exited = true;
|
|
6367
|
+
graceTimer = setTimeout(finishHeld, PIPE_EOF_GRACE_MS);
|
|
6368
|
+
drainTimer = setTimeout(finishHeld, PIPE_DRAIN_MAX_MS);
|
|
6369
|
+
});
|
|
6207
6370
|
child.on("error", () => settle(null));
|
|
6208
6371
|
child.stdin.on("error", () => {
|
|
6209
6372
|
});
|