context-mode 2.1.3 → 2.1.4

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/run.mjs CHANGED
@@ -170,6 +170,7 @@ var GIT_COMMONDIR_FILE = "commondir";
170
170
  var GIT_HEAD_FILE = "HEAD";
171
171
  var WORKSPACE_FOLDER_FLAG = "--workspace-folder";
172
172
  var EXEC_LANGUAGES = Object.freeze(["shell", "powershell", "javascript", "typescript", "python"]);
173
+ var SHELL_FAMILY = Object.freeze(["sh", "bash", "zsh", "dash", "ksh", "fish"]);
173
174
  var PATHEXT_DEFAULT = ".COM;.EXE;.BAT;.CMD";
174
175
  var SOURCE_LABEL_MAX_CHARS = 80;
175
176
  var PLAIN_OUTPUT_ENV = Object.freeze({ NO_COLOR: "1", FORCE_COLOR: "0" });
@@ -185,6 +186,7 @@ var RUN_SESSION_PREFIX = "mcp:";
185
186
  var HOST_SESSION_ENV = Object.freeze({ "claude-code": "CLAUDE_CODE_SESSION_ID" });
186
187
  var NODE_MIN = "24.15.0";
187
188
  var DATA_ROOT = "~/.context-mode";
189
+ var DATA_DIR_ENV = "CONTEXT_MODE_DATA_DIR";
188
190
  var REQUIRE_SECURITY_ENV = "CONTEXT_MODE_REQUIRE_SECURITY";
189
191
  var PROJECT_PLUGIN_SCOPES = Object.freeze(["local", "project"]);
190
192
  var PLUGIN_SCOPE_RANK = Object.freeze({ local: 0, project: 1, user: 2, managed: 2 });
@@ -254,7 +256,8 @@ var DUMP_COUNT_FLAGS = Object.freeze(["-n", "-c", "--lines", "--bytes"]);
254
256
  var SED_COMMAND = "sed";
255
257
  var SED_QUIET_FLAGS = Object.freeze(["-n", "--quiet", "--silent"]);
256
258
  var JQ_COMMAND = "jq";
257
- var READ_PATH_KEYS = Object.freeze(["file_path", "absolute_path", "path"]);
259
+ var READ_PATH_KEYS = Object.freeze(["file_path", "absolute_path", "path", "filePath"]);
260
+ var READ_PATH_LIST_KEY = "filePaths";
258
261
  var EDIT_TOOLS = Object.freeze(["Write", "Edit"]);
259
262
  var EDIT_PATH_KEYS = Object.freeze(["file_path", "path"]);
260
263
  var STATS_COLUMNS = Object.freeze(["Scope", "Produced", "Returned", "Saved", "Reduction %"]);
@@ -323,19 +326,18 @@ var AGENT_HOME_DIR = Object.freeze({
323
326
  var UPGRADE_MODES = Object.freeze(["upgrade", "uninstall"]);
324
327
  var GIT_CLONE_ARGS = Object.freeze(["clone", "--depth", "1", "--branch"]);
325
328
  var GIT_HEAD_SHA_ARGS = Object.freeze(["rev-parse", "HEAD"]);
326
- var UPGRADE_GIT_CHECKOUT_AGENTS = Object.freeze(["pi", "opencode"]);
327
329
  var GIT_NO_PROMPT_ENV = Object.freeze({ GIT_TERMINAL_PROMPT: "0" });
328
330
  var GIT_NO_CRLF_ENV = Object.freeze({ GIT_CONFIG_COUNT: "1", GIT_CONFIG_KEY_0: "core.autocrlf", GIT_CONFIG_VALUE_0: "false" });
329
331
  var GIT_BIN = "git";
330
332
  var WIN_SHELL_LANGS = Object.freeze(["shell", "bash", "sh"]);
331
333
  var GIT_BASH_RELATIVE = Object.freeze([["..", "bin", "bash.exe"], ["..", "..", "bin", "bash.exe"]]);
334
+ var PERSISTED_OUTPUT_MAX_BYTES = 16 * 1024 * 1024;
332
335
  var OPS_SPLIT_FILES = Object.freeze([
333
336
  "doctor-env.ts",
334
337
  "doctor-config.ts",
335
338
  "doctor-hook.ts",
336
339
  "doctor-db.ts",
337
340
  "doctor-heal.ts",
338
- "release-fetch.ts",
339
341
  "verify-sums.ts",
340
342
  "plugin-swap.ts",
341
343
  "host-write.ts",
@@ -360,9 +362,15 @@ var CODEX_FEATURES_HOOKS_KEY = Object.freeze(["features", "hooks"]);
360
362
  var DB_SIBLING_SUFFIXES = Object.freeze(["", DB_WAL_SUFFIX, DB_SHM_SUFFIX]);
361
363
  var UPGRADE_HOST_COMMAND = Object.freeze({
362
364
  "claude-code": "claude plugin marketplace update context-mode && claude plugin update context-mode@context-mode",
363
- codex: "rewrite config.toml's [mcp_servers.context-mode*] block to the v2 entry, then restart Codex",
364
- gemini: "gemini extensions install https://github.com/mksglu/context-mode --auto-update",
365
- qwen: "qwen extensions install https://github.com/mksglu/context-mode --auto-update"
365
+ codex: "codex plugin marketplace upgrade context-mode",
366
+ gemini: "gemini extensions update context-mode",
367
+ qwen: "qwen extensions update context-mode",
368
+ pi: "pi update --extensions",
369
+ opencode: "opencode plugin update",
370
+ "copilot-cli": "npm i -g context-mode && context-mode setup --agents copilot-cli",
371
+ vscode: "npm i -g context-mode && context-mode setup --agents vscode",
372
+ kiro: "npm i -g context-mode && context-mode setup --agents kiro",
373
+ zcode: "npm i -g context-mode && context-mode setup --agents zcode"
366
374
  });
367
375
  var V1_SCRIPT_NAMES = Object.freeze([
368
376
  "afteragentresponse.mjs",
@@ -382,12 +390,16 @@ var V1_SCRIPT_NAMES = Object.freeze([
382
390
  ]);
383
391
  var V1_CODEX_HOOKS_DIR = Object.freeze(["hooks", "codex"]);
384
392
  var V1_CACHE_HEAL_EXTS = Object.freeze([".mjs", ".sh"]);
393
+ var V1_PI_PACKAGE = "npm:context-mode";
385
394
  var VSCODE_USER_DIR_SEGMENTS = Object.freeze(["Code", "User"]);
386
395
  var VSCODE_MCP_FILE = "mcp.json";
387
396
  var KIRO_AGENT_FILE = Object.freeze(["agents", "default.json"]);
388
397
  var KIRO_STEERING_FILE = Object.freeze(["steering", "KIRO.md"]);
389
398
  var OPENCODE_CONFIG_FILES = Object.freeze(["opencode.jsonc", "opencode.json"]);
390
399
  var OPENCODE_NPM_SPEC_PREFIX = `${SERVER_KEY}@`;
400
+ var NPM_VERSION_SEP = "@";
401
+ var OPENCODE_NPM_LATEST = `${OPENCODE_NPM_SPEC_PREFIX}latest`;
402
+ var PI_NPM_LATEST = `${V1_PI_PACKAGE}${NPM_VERSION_SEP}latest`;
391
403
  var V1_DATA_ROOT_ENTRIES = Object.freeze(["content", "platform.json"]);
392
404
  var PI_ROLE = "custom";
393
405
  var V1_PATH_SEPS = Object.freeze(["/", "\\"]);
@@ -408,13 +420,17 @@ var V1_REASON = Object.freeze({
408
420
  kiroSteering: "old context-mode Kiro steering doc .kiro/steering/KIRO.md: manual review, report only, never removed",
409
421
  piNpm: "old context-mode Pi package npm:context-mode",
410
422
  opencodePlugin: "old context-mode OpenCode plugin spec context-mode",
423
+ piGit: "old context-mode Pi entry git:github.com/mksglu/context-mode@release: ctx_upgrade replaces it with npm:context-mode@latest",
424
+ opencodeGit: "old context-mode OpenCode entry git+https://github.com/mksglu/context-mode.git#release: ctx_upgrade replaces it with context-mode@latest",
411
425
  zcodeHook: "old context-mode ZCode hook entry: command contains context-mode hook",
412
426
  dataDir: "old context-mode data dir <agent home>/context-mode/",
413
427
  dataDirShared: "old context-mode data dir ~/.gemini/context-mode/: delete only after a prompt (Antigravity shares it)",
414
428
  dataRoot: "old context-mode content under ~/.context-mode/",
415
- tmpFile: "old context-mode temp file context-mode-* under the OS temp dir: report only, safe to keep"
429
+ tmpFile: "old context-mode temp file context-mode-* under the OS temp dir: report only, safe to keep",
430
+ projectLink: "context-mode path in the project reached through a link: report only, upgrade keeps it as is and never follows a link"
416
431
  });
417
- var UPGRADE_REPORT_ONLY_REASONS = Object.freeze([V1_REASON.statusLine, V1_REASON.vscodeMcp, V1_REASON.dataDirShared, V1_REASON.tmpFile, V1_REASON.kiroSteering]);
432
+ var UPGRADE_REPORT_ONLY_REASONS = Object.freeze([V1_REASON.statusLine, V1_REASON.vscodeMcp, V1_REASON.dataDirShared, V1_REASON.tmpFile, V1_REASON.kiroSteering, V1_REASON.piGit, V1_REASON.opencodeGit, V1_REASON.projectLink]);
433
+ var UPGRADE_KEEP_ASIDE_REASONS = Object.freeze([V1_REASON.dataDir, V1_REASON.dataRoot]);
418
434
  var OPENCODE_TOOL_PREFIX = SERVER_KEY + "_";
419
435
  var OPENCODE_PLUGIN_PATH = Object.freeze(["opencode", "plugin.js"]);
420
436
  var ZCODE_MCP_FEATURE_KEY = Object.freeze(["features", "mcp"]);
@@ -500,13 +516,13 @@ var NATIVE_INSTALL = Object.freeze({
500
516
  "copilot-cli": ["npm i -g context-mode", "context-mode setup --agents copilot-cli"],
501
517
  vscode: ["npm i -g context-mode", "context-mode setup --agents vscode"],
502
518
  kiro: ["npm i -g context-mode", "context-mode setup --agents kiro"],
503
- zcode: ["npm i -g context-mode", "context-mode setup --agents zcode"]
519
+ zcode: ["zcode plugins marketplace add mksglu/context-mode", "zcode plugins install context-mode@context-mode"]
504
520
  });
505
521
  var ISSUE_FORM_URL = `https://github.com/${ISSUE_REPO_SLUG}/issues/new?template=${ISSUE_FORM_FILE}`;
506
522
 
507
523
  // src/core/tools/run-wrapper.ts
508
524
  import { randomUUID } from "node:crypto";
509
- import { dirname as dirname4, join as join5 } from "node:path";
525
+ import { dirname as dirname4, join as join6 } from "node:path";
510
526
 
511
527
  // src/adapters/claude-code.ts
512
528
  var TOOL_NAME_PREFIX = "mcp__plugin_context-mode_context-mode__";
@@ -723,6 +739,7 @@ var GEMINI_SOURCES = { startup: "startup", resume: "resume", clear: "clear" };
723
739
  var HINT_EVENTS = ["SessionStart", "UserPromptSubmit", "PostToolUse"];
724
740
  var DENY_EVENTS = ["UserPromptSubmit", "PreToolUse"];
725
741
  var EMPTY = { stdout: "", exitCode: 0 };
742
+ var GEMINI_REPLACE_LEAD = "the tool ran and succeeded; context-mode indexed its full output, and this is the part that answers the call:\n";
726
743
  function own(p, key) {
727
744
  return Object.hasOwn(p, key) ? p[key] : void 0;
728
745
  }
@@ -742,13 +759,67 @@ function canonicalName2(name, mcp) {
742
759
  if (typeof server !== "string" || typeof tool !== "string") return name;
743
760
  return "mcp:" + server + ":" + tool;
744
761
  }
762
+ function partsText(llm) {
763
+ if (!Array.isArray(llm) || llm.length === 0) return void 0;
764
+ const out = [];
765
+ for (const part of llm) {
766
+ if (typeof part === "string") {
767
+ out.push(part);
768
+ continue;
769
+ }
770
+ if (typeof part !== "object" || part === null) return void 0;
771
+ const t = own(part, "text");
772
+ if (typeof t !== "string") return void 0;
773
+ out.push(t);
774
+ }
775
+ return out.join("");
776
+ }
745
777
  function readResult2(resp) {
746
778
  const obj = typeof resp === "object" && resp !== null ? resp : void 0;
747
779
  const llm = obj === void 0 ? void 0 : own(obj, "llmContent");
748
- const text = resp === void 0 ? "" : typeof resp === "string" ? resp : typeof llm === "string" ? llm : JSON.stringify(resp);
780
+ const text = resp === void 0 ? "" : typeof resp === "string" ? resp : typeof llm === "string" ? llm : partsText(llm) ?? JSON.stringify(resp);
749
781
  const err = obj === void 0 ? void 0 : own(obj, "error");
750
782
  return { isError: err !== void 0 && err !== null, bytes: Buffer.byteLength(text), text };
751
783
  }
784
+ var URL_STARTS = ["https://", "http://"];
785
+ var URL_TRAIL = `.,;:!?)]}>'"`;
786
+ var SPACE = " \n\r\f\v";
787
+ function words(text) {
788
+ const out = [];
789
+ let cur = "";
790
+ for (const ch of text) {
791
+ if (SPACE.includes(ch)) {
792
+ if (cur !== "") out.push(cur);
793
+ cur = "";
794
+ } else cur += ch;
795
+ }
796
+ if (cur !== "") out.push(cur);
797
+ return out;
798
+ }
799
+ function firstUrl(prompt) {
800
+ if (typeof prompt !== "string") return void 0;
801
+ for (const word of words(prompt)) {
802
+ const lower = word.toLowerCase();
803
+ const at2 = URL_STARTS.map((s) => lower.indexOf(s)).filter((i) => i >= 0).sort((a, b) => a - b)[0];
804
+ if (at2 === void 0) continue;
805
+ let url = word.slice(at2);
806
+ while (url.length > 0 && URL_TRAIL.includes(url[url.length - 1])) url = url.slice(0, -1);
807
+ return url;
808
+ }
809
+ return void 0;
810
+ }
811
+ function nativeInput(name, input) {
812
+ if (name === "web_fetch" && own(input, "url") === void 0) {
813
+ const url = firstUrl(own(input, "prompt"));
814
+ return url === void 0 ? input : { ...input, url };
815
+ }
816
+ if (name === "read_many_files" && own(input, "file_path") === void 0) {
817
+ const inc = own(input, "include");
818
+ const list2 = Array.isArray(inc) ? inc.filter((v) => typeof v === "string") : [];
819
+ return list2.length === 0 ? input : { ...input, file_path: list2.join(" ") };
820
+ }
821
+ return input;
822
+ }
752
823
  function decode3(raw) {
753
824
  if (typeof raw !== "object" || raw === null || Array.isArray(raw)) throw new Error("gemini: hook payload is not a JSON object");
754
825
  const p = raw;
@@ -766,7 +837,7 @@ function decode3(raw) {
766
837
  ev.tool = {
767
838
  name,
768
839
  canonicalName: canonicalName2(name, own(p, "mcp_context")),
769
- input: typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
840
+ input: nativeInput(name, typeof input === "object" && input !== null && !Array.isArray(input) ? input : {})
770
841
  };
771
842
  }
772
843
  if (event === "PostToolUse") ev.result = readResult2(own(p, "tool_response"));
@@ -784,7 +855,7 @@ function encode3(ev, d, _extra) {
784
855
  return { stdout: JSON.stringify({ decision: "deny", reason: d.rewritten === void 0 ? d.reason : d.rewritten + "\n" + d.reason }), exitCode: 0 };
785
856
  case "replace":
786
857
  if (ev.event !== "PostToolUse") return EMPTY;
787
- return { stdout: JSON.stringify({ decision: "deny", reason: d.output }), exitCode: 0 };
858
+ return { stdout: JSON.stringify({ decision: "deny", reason: GEMINI_REPLACE_LEAD + d.output }), exitCode: 0 };
788
859
  }
789
860
  }
790
861
 
@@ -1117,6 +1188,7 @@ function encode7(ev, d, _extra) {
1117
1188
 
1118
1189
  // src/adapters/zcode.ts
1119
1190
  var TOOL_NAME_PREFIX8 = "mcp__context-mode__";
1191
+ var PLUGIN_TOOL_NAME_PREFIX = "mcp__plugin_context-mode_context-mode__";
1120
1192
  var ZCODE_SOURCES = { startup: "startup", resume: "resume", clear: "clear", compact: "compact" };
1121
1193
  var TWINS = { hook_event_name: "hookEventName", session_id: "sessionId" };
1122
1194
  var FAILURE_EVENT2 = "PostToolUseFailure";
@@ -1126,7 +1198,9 @@ function decode8(raw) {
1126
1198
  for (const [snake, camel] of Object.entries(TWINS)) if (p[snake] === void 0) p[snake] = p[camel];
1127
1199
  const failure = p.hook_event_name === FAILURE_EVENT2;
1128
1200
  if (failure) p.hook_event_name = "PostToolUse";
1129
- const ev = decodeClaudeShape(p, "zcode", TOOL_NAME_PREFIX8, ZCODE_SOURCES);
1201
+ const toolName = typeof p.tool_name === "string" ? p.tool_name : "";
1202
+ const prefix = toolName.startsWith(PLUGIN_TOOL_NAME_PREFIX) ? PLUGIN_TOOL_NAME_PREFIX : TOOL_NAME_PREFIX8;
1203
+ const ev = decodeClaudeShape(p, "zcode", prefix, ZCODE_SOURCES);
1130
1204
  if (failure) {
1131
1205
  const text = typeof p.error === "string" ? p.error : p.error === void 0 ? "" : JSON.stringify(p.error);
1132
1206
  ev.result = { isError: true, bytes: Buffer.byteLength(text), text };
@@ -1207,8 +1281,9 @@ var OPENCODE_EVENTS = {
1207
1281
  "step-finish": "Usage"
1208
1282
  };
1209
1283
  var SHELL_TOOL_NAME = "bash";
1284
+ var READ_TOOL_NAME = "read";
1210
1285
  var SHELL_TOOL_NAMES5 = Object.freeze([SHELL_TOOL_NAME, "shell"]);
1211
- var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", ...Object.fromEntries(SHELL_TOOL_NAMES5.map((n) => [n, "Bash"])) };
1286
+ var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", [READ_TOOL_NAME]: "Read", ...Object.fromEntries(SHELL_TOOL_NAMES5.map((n) => [n, "Bash"])) };
1212
1287
  function toCanonicalTool(name) {
1213
1288
  if (name.startsWith(OWN_WIRE_PREFIX)) return name.slice(OWN_WIRE_PREFIX.length);
1214
1289
  return Object.prototype.hasOwnProperty.call(NATIVE_TO_CANONICAL7, name) ? NATIVE_TO_CANONICAL7[name] : name;
@@ -1436,9 +1511,9 @@ var ADAPTERS = Object.freeze([
1436
1511
  installMode: "in-process",
1437
1512
  projectSource: "spawn-cwd",
1438
1513
  toolNamePrefix: TOOL_NAME_PREFIX10,
1439
- nativeTools: { shell: [SHELL_TOOL_NAME] },
1514
+ nativeTools: { shell: [SHELL_TOOL_NAME], read: [READ_TOOL_NAME] },
1440
1515
  execReadOnlyHint: false,
1441
- // D-HF-OPENCODE-SHELL-1
1516
+ // D-HF-OPENCODE-SHELL-1, D-R44-OC-READ-1
1442
1517
  // Usage: step-finish cost row (DEVIATIONS D-M5-arch-2).
1443
1518
  eventMap: { SessionStart: "context", UserPromptSubmit: "prompt", PreToolUse: "execute.before", PostToolUse: "execute.after", PreCompact: "compaction", Usage: "step-finish" },
1444
1519
  compactDelivery: "opencode-cache",
@@ -1666,6 +1741,15 @@ function applyMigrations(db, fromVersion) {
1666
1741
  }
1667
1742
  }
1668
1743
 
1744
+ // src/core/store/open-read.ts
1745
+ function openReadOnlyHandle(sqlite, path3, opts = {}) {
1746
+ const bun = "bun" in opts ? opts.bun : process.versions.bun;
1747
+ if (bun === void 0) return new sqlite.DatabaseSync(path3, { timeout: DB_TIMEOUT_MS, readOnly: true });
1748
+ const raw = new sqlite.DatabaseSync(path3, { timeout: DB_TIMEOUT_MS });
1749
+ if (opts.queryOnly !== false) raw.exec("PRAGMA query_only = ON");
1750
+ return raw;
1751
+ }
1752
+
1669
1753
  // src/core/store/db.ts
1670
1754
  var { existsSync, linkSync, mkdirSync, rmSync } = process.getBuiltinModule("node:fs");
1671
1755
  function versionParts(v) {
@@ -1731,7 +1815,7 @@ function openWritable(sqlite, path3) {
1731
1815
  }
1732
1816
  }
1733
1817
  function openReadOnly(sqlite, path3) {
1734
- const raw = new sqlite.DatabaseSync(path3, { timeout: DB_TIMEOUT_MS, readOnly: true });
1818
+ const raw = openReadOnlyHandle(sqlite, path3);
1735
1819
  try {
1736
1820
  const found = readUserVersion(raw);
1737
1821
  if (found !== SCHEMA_VERSION) throw new CtxError("SCHEMA_STALE", { found, want: SCHEMA_VERSION });
@@ -3442,18 +3526,18 @@ function insertChunks(db, id, src, chunks) {
3442
3526
  }
3443
3527
  }
3444
3528
  function toMatch(q) {
3445
- const words = [];
3529
+ const words2 = [];
3446
3530
  let cur = "";
3447
3531
  for (const ch of q) {
3448
3532
  if (ch.trim() === "" || ch === "\0") {
3449
- if (cur !== "") words.push(cur);
3533
+ if (cur !== "") words2.push(cur);
3450
3534
  cur = "";
3451
3535
  } else {
3452
3536
  cur += ch === '"' ? '""' : ch;
3453
3537
  }
3454
3538
  }
3455
- if (cur !== "") words.push(cur);
3456
- return words.map((w) => `"${w}"*`).join(" ");
3539
+ if (cur !== "") words2.push(cur);
3540
+ return words2.map((w) => `"${w}"*`).join(" ");
3457
3541
  }
3458
3542
  function toMatchAny(q) {
3459
3543
  const terms = new Set(toMatch(q).split(" ").filter((t) => t !== ""));
@@ -4081,6 +4165,9 @@ var SUBSHELL_CLOSE = ")";
4081
4165
  var CASE_BREAK = ";;";
4082
4166
  var FUNC_PARENS = ["(", ")"];
4083
4167
  var NOT = "!";
4168
+ function append(out, items) {
4169
+ for (const x of items) out.push(x);
4170
+ }
4084
4171
  var KW = Object.freeze({
4085
4172
  group: "{",
4086
4173
  groupEnd: "}",
@@ -4260,12 +4347,12 @@ function ifBody(p) {
4260
4347
  const t = at(p);
4261
4348
  if (isWord(t, KW.then) || isWord(t, KW.else)) {
4262
4349
  p.i++;
4263
- out.push(...list(p, [KW.elif, KW.else, KW.fi]));
4350
+ append(out, list(p, [KW.elif, KW.else, KW.fi]));
4264
4351
  continue;
4265
4352
  }
4266
4353
  if (isWord(t, KW.elif)) {
4267
4354
  p.i++;
4268
- out.push(...list(p, [KW.then]));
4355
+ append(out, list(p, [KW.then]));
4269
4356
  continue;
4270
4357
  }
4271
4358
  if (isWord(t, KW.fi)) p.i++;
@@ -4276,7 +4363,7 @@ function ifBody(p) {
4276
4363
  function loopBody(p) {
4277
4364
  p.i++;
4278
4365
  const out = list(p, [KW.do]);
4279
- out.push(...doBlock(p));
4366
+ append(out, doBlock(p));
4280
4367
  return out;
4281
4368
  }
4282
4369
  function forBody(p) {
@@ -4290,10 +4377,10 @@ function forBody(p) {
4290
4377
  p.i++;
4291
4378
  const from = p.i;
4292
4379
  while ((at(p)?.kind === "word" || at(p)?.kind === "unhandled") && !isWord(at(p), KW.do)) p.i++;
4293
- out.push(...carrierOf(p, from, p.i));
4380
+ append(out, carrierOf(p, from, p.i));
4294
4381
  }
4295
4382
  }
4296
- out.push(...doBlock(p));
4383
+ append(out, doBlock(p));
4297
4384
  return out;
4298
4385
  }
4299
4386
  function caseBody(p) {
@@ -4302,7 +4389,7 @@ function caseBody(p) {
4302
4389
  const subject = at(p);
4303
4390
  if (subject !== void 0 && subject.kind !== "op" && subject.kind !== "io") {
4304
4391
  p.i++;
4305
- out.push(...carrierOf(p, p.i - 1, p.i));
4392
+ append(out, carrierOf(p, p.i - 1, p.i));
4306
4393
  }
4307
4394
  if (isWord(at(p), KW.in)) p.i++;
4308
4395
  while (p.i < p.toks.length && p.err === null) {
@@ -4325,7 +4412,7 @@ function caseBody(p) {
4325
4412
  p.i++;
4326
4413
  const body = list(p, [KW.esac]);
4327
4414
  tag(body, KW.case);
4328
- out.push(...body);
4415
+ append(out, body);
4329
4416
  }
4330
4417
  return out;
4331
4418
  }
@@ -4340,7 +4427,7 @@ function hasFuncParens(p, from) {
4340
4427
  }
4341
4428
  function simple(p) {
4342
4429
  const first = p.i;
4343
- const words = [];
4430
+ const words2 = [];
4344
4431
  let toFile = false;
4345
4432
  while (p.i < p.toks.length) {
4346
4433
  const t = p.toks[p.i];
@@ -4349,7 +4436,7 @@ function simple(p) {
4349
4436
  toFile = ioRedirect(p) || toFile;
4350
4437
  continue;
4351
4438
  }
4352
- if (words.length === 0 && t.kind === "word" && isAssignment(t.text)) {
4439
+ if (words2.length === 0 && t.kind === "word" && isAssignment(t.text)) {
4353
4440
  p.i++;
4354
4441
  const open = at(p);
4355
4442
  if (t.text.endsWith(OPT_VALUE) && open !== void 0 && isOp(open, SUBSHELL_OPEN) && open.start === t.end) {
@@ -4363,15 +4450,15 @@ function simple(p) {
4363
4450
  }
4364
4451
  continue;
4365
4452
  }
4366
- words.push(t);
4453
+ words2.push(t);
4367
4454
  p.i++;
4368
4455
  }
4369
4456
  if (p.i === first) return EMPTY3();
4370
4457
  const raw = p.src.slice(p.toks[first].start, p.toks[p.i - 1].end);
4371
- if (words.length === 0) return { cmds: carrier(raw), simple: null };
4458
+ if (words2.length === 0) return { cmds: carrier(raw), simple: null };
4372
4459
  const text = (t) => t.kind === "word" ? dequote(t.text) : t.text;
4373
- const argv0 = baseName(text(words[0]));
4374
- const args = words.slice(1).map(text);
4460
+ const argv0 = baseName(text(words2[0]));
4461
+ const args = words2.slice(1).map(text);
4375
4462
  const cmd = { argv0, args, redirectsToFile: toFile || writesFile(argv0, args), endsInBoundedSink: false, raw };
4376
4463
  return { cmds: [cmd], simple: cmd };
4377
4464
  }
@@ -4442,7 +4529,7 @@ function pipeline(p) {
4442
4529
  let last = EMPTY3();
4443
4530
  while (p.err === null) {
4444
4531
  last = element(p);
4445
- all.push(...last.cmds);
4532
+ append(all, last.cmds);
4446
4533
  const t = at(p);
4447
4534
  if (t !== void 0 && t.kind === "op" && PIPES.includes(t.text)) {
4448
4535
  for (const c of last.cmds) c.pipesOut = true;
@@ -4471,7 +4558,7 @@ function list(p, stop) {
4471
4558
  }
4472
4559
  if (t.kind === "word" && stop.includes(t.text)) return out;
4473
4560
  const before = p.i;
4474
- out.push(...pipeline(p));
4561
+ append(out, pipeline(p));
4475
4562
  if (p.i === before && p.err === null) p.i++;
4476
4563
  }
4477
4564
  return out;
@@ -4484,7 +4571,7 @@ function parseShell(cmd) {
4484
4571
  const p = { toks: s.tokens, src: cmd, bodies: new Set(s.heredocBodies), i: 0, depth: 0, err: null };
4485
4572
  const commands = [];
4486
4573
  while (p.i < p.toks.length && p.err === null) {
4487
- commands.push(...list(p, NO_STOP));
4574
+ append(commands, list(p, NO_STOP));
4488
4575
  if (p.i < p.toks.length) p.i++;
4489
4576
  }
4490
4577
  if (p.err !== null) return { ok: false, construct: p.err };
@@ -4739,57 +4826,57 @@ function param(word, names) {
4739
4826
  const hits = names.filter((p) => p.startsWith(name));
4740
4827
  return { full: hits.length === 1 ? hits[0] : void 0, inline };
4741
4828
  }
4742
- function psDump(words) {
4743
- if (words.length === 0) return null;
4744
- const name = cmdName(words[0]);
4829
+ function psDump(words2) {
4830
+ if (words2.length === 0) return null;
4831
+ const name = cmdName(words2[0]);
4745
4832
  if (!READ_COMMANDS.includes(name)) {
4746
- const lower = words[0].toLowerCase();
4833
+ const lower = words2[0].toLowerCase();
4747
4834
  const cls = FILE_CLASSES.find((c) => lower.startsWith(c));
4748
- if (cls === void 0 || !FILE_READS.includes(lower.slice(cls.length)) || words.length < MIN_STATIC_CALL) return null;
4749
- return { file: words[1], bounded: false };
4835
+ if (cls === void 0 || !FILE_READS.includes(lower.slice(cls.length)) || words2.length < MIN_STATIC_CALL) return null;
4836
+ return { file: words2[1], bounded: false };
4750
4837
  }
4751
4838
  const files = [];
4752
4839
  let bounded = false;
4753
- for (let k = 1; k < words.length; k++) {
4754
- const a = words[k];
4840
+ for (let k = 1; k < words2.length; k++) {
4841
+ const a = words2[k];
4755
4842
  if (!a.startsWith(DASH) || a.length === DASH.length) {
4756
4843
  files.push(a);
4757
4844
  continue;
4758
4845
  }
4759
4846
  const p = param(a, ALL_PARAMS);
4760
4847
  if (p.full === void 0 || !VALUE_PARAMS.includes(p.full)) continue;
4761
- const value = p.inline ?? words[++k];
4848
+ const value = p.inline ?? words2[++k];
4762
4849
  if (value === void 0) return null;
4763
4850
  if (PATH_PARAMS.includes(p.full)) files.push(value);
4764
4851
  if (BOUND_PARAMS.includes(p.full)) bounded = true;
4765
4852
  }
4766
4853
  return files.length === 1 ? { file: files[0], bounded } : null;
4767
4854
  }
4768
- function nestedScript(name, words) {
4855
+ function nestedScript(name, words2) {
4769
4856
  if (POWERSHELLS.includes(name)) {
4770
- for (let k = 1; k < words.length; k++) {
4771
- const w = words[k];
4857
+ for (let k = 1; k < words2.length; k++) {
4858
+ const w = words2[k];
4772
4859
  if (!w.startsWith(DASH) || w.length === DASH.length) continue;
4773
4860
  const p = param(w, [COMMAND_PARAM]);
4774
4861
  if (p.full === void 0) continue;
4775
- return [...p.inline === void 0 ? [] : [p.inline], ...words.slice(k + 1)].join(" ");
4862
+ return [...p.inline === void 0 ? [] : [p.inline], ...words2.slice(k + 1)].join(" ");
4776
4863
  }
4777
4864
  return null;
4778
4865
  }
4779
4866
  if (CMDS.includes(name)) {
4780
- for (let k = 1; k < words.length; k++) {
4781
- if (CMD_RUN_FLAGS.includes(words[k].toLowerCase())) return words.slice(k + 1).join(" ");
4867
+ for (let k = 1; k < words2.length; k++) {
4868
+ if (CMD_RUN_FLAGS.includes(words2[k].toLowerCase())) return words2.slice(k + 1).join(" ");
4782
4869
  }
4783
4870
  }
4784
4871
  return null;
4785
4872
  }
4786
- function cdTarget(words) {
4787
- for (let k = 1; k < words.length; k++) {
4788
- const w = words[k];
4873
+ function cdTarget(words2) {
4874
+ for (let k = 1; k < words2.length; k++) {
4875
+ const w = words2[k];
4789
4876
  if (w.toLowerCase() === "/d") continue;
4790
4877
  if (!w.startsWith(DASH) || w.length === DASH.length) return w;
4791
4878
  const p = param(w, PATH_PARAMS);
4792
- if (p.full !== void 0) return p.inline ?? words[k + 1] ?? null;
4879
+ if (p.full !== void 0) return p.inline ?? words2[k + 1] ?? null;
4793
4880
  }
4794
4881
  return null;
4795
4882
  }
@@ -4849,8 +4936,13 @@ function requireHomeDir(env, platform = process.platform) {
4849
4936
  if (home !== null) return home;
4850
4937
  throw new CtxError("NO_HOME", { name: platform === "win32" ? "USERPROFILE" : "HOME" });
4851
4938
  }
4939
+ function dataRootOf(env, home) {
4940
+ const dir = env[DATA_DIR_ENV];
4941
+ return dir !== void 0 && dir !== "" ? resolve2(dir) : join(home, DATA_ROOT.slice(1));
4942
+ }
4852
4943
  function resolveConfigDir(env) {
4853
- const dir = join(requireHomeDir(env), DATA_ROOT.slice(1));
4944
+ const set = env[DATA_DIR_ENV];
4945
+ const dir = dataRootOf(env, set !== void 0 && set !== "" ? "" : requireHomeDir(env));
4854
4946
  mkdirSync2(dir, { recursive: true });
4855
4947
  return realpathSync.native(dir);
4856
4948
  }
@@ -5017,14 +5109,13 @@ function tokenize(glob, path3) {
5017
5109
  }
5018
5110
  return toks;
5019
5111
  }
5020
- function globMatch(glob, text, path3) {
5021
- const toks = tokenize(glob, path3);
5112
+ function matchStarts(toks, text, path3, ends) {
5022
5113
  const n = text.length;
5023
- let next = new Uint8Array(n + 1);
5024
- next[n] = 1;
5114
+ let next = ends;
5115
+ let cur = new Uint8Array(n + 1);
5025
5116
  for (let i = toks.length - 1; i >= 0; i--) {
5026
5117
  const tok = toks[i];
5027
- const cur = new Uint8Array(n + 1);
5118
+ if (next === ends) next = Uint8Array.from(ends);
5028
5119
  let acc = 0;
5029
5120
  for (let j = n; j >= 0; j--) {
5030
5121
  const has2 = j < n;
@@ -5048,9 +5139,19 @@ function globMatch(glob, text, path3) {
5048
5139
  break;
5049
5140
  }
5050
5141
  }
5051
- next = cur;
5142
+ const done = cur;
5143
+ cur = next;
5144
+ next = done;
5052
5145
  }
5053
- return next[0] === 1;
5146
+ return next;
5147
+ }
5148
+ function globMatch(glob, text, path3) {
5149
+ const toks = tokenize(glob, path3);
5150
+ const first = toks[0];
5151
+ if (first !== void 0 && first.t === "lit" && text[0] !== first.c) return false;
5152
+ const ends = new Uint8Array(text.length + 1);
5153
+ ends[text.length] = 1;
5154
+ return matchStarts(toks, text, path3, ends)[0] === 1;
5054
5155
  }
5055
5156
  function parsePattern(pattern) {
5056
5157
  const open = pattern.indexOf("(");
@@ -5058,14 +5159,25 @@ function parsePattern(pattern) {
5058
5159
  const glob = pattern.slice(open + 1, pattern.length - ")".length);
5059
5160
  return glob === "" ? null : { tool: pattern.slice(0, open), glob };
5060
5161
  }
5162
+ function commandStarts(glob, text) {
5163
+ const n = text.length;
5164
+ const ends = new Uint8Array(n + 1);
5165
+ ends[n] = 1;
5166
+ if (!glob.endsWith(PREFIX_RULE)) return matchStarts(tokenize(glob, false), text, false, ends);
5167
+ for (let k = 0; k < n; k++) if (WHITESPACE.includes(text[k])) ends[k] = 1;
5168
+ return matchStarts(tokenize(glob.slice(0, glob.length - PREFIX_RULE.length), false), text, false, ends);
5169
+ }
5061
5170
  function matchCommand(glob, text) {
5062
- if (!glob.endsWith(PREFIX_RULE)) return globMatch(glob, text, false);
5063
- const head = glob.slice(0, glob.length - PREFIX_RULE.length);
5064
- if (globMatch(head, text, false)) return true;
5065
- for (let k = 0; k < text.length; k++) {
5066
- if (WHITESPACE.includes(text[k]) && globMatch(head, text.slice(0, k), false)) return true;
5171
+ return commandStarts(glob, text)[0] === 1;
5172
+ }
5173
+ function denyPatternFromAny(text, patterns, from) {
5174
+ for (const pattern of patterns) {
5175
+ const p = parsePattern(pattern);
5176
+ if (p === null || p.tool !== BASH_TOOL) continue;
5177
+ const starts = commandStarts(p.glob, text);
5178
+ for (let j = 0; j <= text.length; j++) if (from[j] === 1 && starts[j] === 1) return pattern;
5067
5179
  }
5068
- return false;
5180
+ return null;
5069
5181
  }
5070
5182
  var DRIVE_PREFIX = /^[A-Za-z]:(?=\/)/;
5071
5183
  function toSlash(p) {
@@ -5136,6 +5248,7 @@ var HOST_SEP = ".";
5136
5248
  var SED_RANGE_SEP = ",";
5137
5249
  var SED_PRINT = "p";
5138
5250
  var SED_SCRIPT_FLAGS = ["-e", "--expression"];
5251
+ var EXE_SUFFIX = ".exe";
5139
5252
  var COMMAND_STARTS = ";&|(`\n\r";
5140
5253
  var ALLOW = Object.freeze({ kind: "allow" });
5141
5254
  var deny = (reason) => ({ kind: "deny", reason });
@@ -5173,6 +5286,11 @@ function readPath(input) {
5173
5286
  }
5174
5287
  return void 0;
5175
5288
  }
5289
+ function readPaths(input) {
5290
+ const one = readPath(input);
5291
+ const many = Array.isArray(input[READ_PATH_LIST_KEY]) ? input[READ_PATH_LIST_KEY].filter((p) => typeof p === "string" && p !== "") : [];
5292
+ return one === void 0 ? many : [one, ...many];
5293
+ }
5176
5294
  function userDenyHit(canonical, input, cwd, rules) {
5177
5295
  if (rules.length === 0) return null;
5178
5296
  const readRule = (p) => matchesAnyDenyPattern(resolve3(cwd, p), rules, { tool: READ, projectDir: cwd });
@@ -5181,35 +5299,65 @@ function userDenyHit(canonical, input, cwd, rules) {
5181
5299
  return cmd === void 0 ? null : matchesAnyDenyPattern(cmd, rules);
5182
5300
  }
5183
5301
  if (canonical === READ) {
5184
- const p = readPath(input);
5185
- return p === void 0 ? null : readRule(p);
5302
+ for (const p of readPaths(input)) {
5303
+ const h = readRule(p);
5304
+ if (h !== null) return h;
5305
+ }
5306
+ return null;
5186
5307
  }
5187
5308
  if (canonical === CTX_EXECUTE) {
5188
5309
  const file = str(input.file);
5189
5310
  const byFile = file === void 0 || file === "" ? null : readRule(file);
5190
5311
  if (byFile !== null) return byFile;
5191
5312
  const code = str(input.code);
5192
- return input.language === SHELL_LANGUAGE && code !== void 0 ? matchesAnyDenyPattern(code, rules) : null;
5313
+ return isShellLanguage(input.language) && code !== void 0 ? shellHit(code, rules) : null;
5193
5314
  }
5194
5315
  if (canonical === CTX_BATCH && Array.isArray(input.commands)) {
5195
5316
  for (const c of input.commands) {
5196
- const h = typeof c === "string" ? matchesAnyDenyPattern(c, rules) : null;
5317
+ const h = typeof c === "string" ? shellHit(c, rules) : null;
5197
5318
  if (h !== null) return h;
5198
5319
  }
5199
5320
  }
5200
5321
  return null;
5201
5322
  }
5323
+ function isShellLanguage(language) {
5324
+ if (typeof language !== "string") return false;
5325
+ let name = language.toLowerCase();
5326
+ const slash = Math.max(name.lastIndexOf("/"), name.lastIndexOf("\\"));
5327
+ if (slash >= 0) name = name.slice(slash + 1);
5328
+ if (name.endsWith(EXE_SUFFIX)) name = name.slice(0, name.length - EXE_SUFFIX.length);
5329
+ return name === SHELL_LANGUAGE || has(SHELL_FAMILY, name);
5330
+ }
5331
+ function shellHit(cmd, rules) {
5332
+ const whole = matchesAnyDenyPattern(cmd, rules);
5333
+ if (whole !== null) return whole;
5334
+ const parsed = parseShell(cmd);
5335
+ if (!parsed.ok) return couldMatchRule(cmd, rules);
5336
+ for (const c of unwrap(parsed.commands)) {
5337
+ const h = segmentHit(c, rules);
5338
+ if (h !== null) return h;
5339
+ }
5340
+ return null;
5341
+ }
5202
5342
  function segmentHit(c, rules) {
5203
5343
  if (rules.length === 0) return null;
5204
5344
  return matchesAnyDenyPattern([c.argv0, ...c.args].join(" "), rules) ?? matchesAnyDenyPattern(c.raw, rules);
5205
5345
  }
5206
- function couldMatch(text, rules) {
5207
- if (rules.length === 0) return false;
5346
+ function couldMatchRule(text, rules) {
5347
+ if (rules.length === 0) return null;
5348
+ const from = new Uint8Array(text.length + 1);
5349
+ let pending = false;
5208
5350
  for (let k = 0; k <= text.length; k++) {
5209
- if (k > 0 && !COMMAND_STARTS.includes(text.charAt(k - 1))) continue;
5210
- if (matchesAnyDenyPattern(text.slice(k).trimStart(), rules) !== null) return true;
5351
+ if (k === 0 || COMMAND_STARTS.includes(text.charAt(k - 1))) pending = true;
5352
+ if (pending && (k === text.length || text.charAt(k).trim() !== "")) {
5353
+ from[k] = 1;
5354
+ pending = false;
5355
+ }
5211
5356
  }
5212
- return false;
5357
+ return denyPatternFromAny(text, rules, from);
5358
+ }
5359
+ function couldMatch(text, rules) {
5360
+ return couldMatchRule(text, rules) !== null;
5213
5361
  }
5214
5362
  function neverDenied(url) {
5215
5363
  let host;
@@ -5284,8 +5432,7 @@ function fileSize(abs) {
5284
5432
  }
5285
5433
  function read(ev, row, input) {
5286
5434
  if (!present(input.offset) && !present(input.limit)) {
5287
- const p = readPath(input);
5288
- if (p !== void 0) {
5435
+ for (const p of readPaths(input)) {
5289
5436
  const abs = resolve3(ev.cwd, p);
5290
5437
  const size = fileSize(abs);
5291
5438
  if (size > OUTPUT_CAP) return deny(READ_DENY_MESSAGE(abs, size, row.toolNamePrefix));
@@ -5379,11 +5526,11 @@ function dumpTarget(name, args) {
5379
5526
  }
5380
5527
 
5381
5528
  // src/core/project/resolve.ts
5382
- import { basename as basename2, dirname as dirname3, join as join4, resolve as resolve4 } from "node:path";
5529
+ import { basename as basename2, dirname as dirname3, join as join5, resolve as resolve4 } from "node:path";
5383
5530
 
5384
5531
  // src/core/project/contain.ts
5385
- import { dirname as dirname2, sep as sep2 } from "node:path";
5386
- var { realpathSync: realpathSync2 } = process.getBuiltinModule("node:fs");
5532
+ import { dirname as dirname2, join as join4, relative as relative2, sep as sep2 } from "node:path";
5533
+ var { lstatSync, realpathSync: realpathSync2 } = process.getBuiltinModule("node:fs");
5387
5534
  function real(p) {
5388
5535
  try {
5389
5536
  return realpathSync2.native(p);
@@ -5571,9 +5718,9 @@ function gitDirs(dir) {
5571
5718
  }
5572
5719
  const start = cur;
5573
5720
  for (; ; ) {
5574
- const gitDir = gitDirAt(join4(cur, GIT_DIR_NAME));
5721
+ const gitDir = gitDirAt(join5(cur, GIT_DIR_NAME));
5575
5722
  if (gitDir !== null) {
5576
- const common = firstLine(join4(gitDir, GIT_COMMONDIR_FILE));
5723
+ const common = firstLine(join5(gitDir, GIT_COMMONDIR_FILE));
5577
5724
  return { commonDir: realpathSync3.native(common === null ? gitDir : resolve4(gitDir, common)), worktree: cur, start };
5578
5725
  }
5579
5726
  const up = dirname3(cur);
@@ -5594,7 +5741,7 @@ function gitDirAt(dotGit) {
5594
5741
  } catch {
5595
5742
  return null;
5596
5743
  }
5597
- return existsSync2(join4(gitDir, GIT_HEAD_FILE)) ? gitDir : null;
5744
+ return existsSync2(join5(gitDir, GIT_HEAD_FILE)) ? gitDir : null;
5598
5745
  }
5599
5746
  function firstLine(file) {
5600
5747
  let text;
@@ -5635,11 +5782,11 @@ function resolveProject(agent, src) {
5635
5782
  return key;
5636
5783
  },
5637
5784
  get projectDir() {
5638
- pdir ??= join4(resolveConfigDir(process.env), PROJECTS_DIR_NAME, this.projectKey);
5785
+ pdir ??= join5(resolveConfigDir(process.env), PROJECTS_DIR_NAME, this.projectKey);
5639
5786
  return pdir;
5640
5787
  },
5641
5788
  get dbPath() {
5642
- return join4(this.projectDir, DB_FILE_NAME);
5789
+ return join5(this.projectDir, DB_FILE_NAME);
5643
5790
  },
5644
5791
  worktree,
5645
5792
  get containmentRoot() {
@@ -6131,7 +6278,7 @@ function cutPoint(src, start, end, scratch) {
6131
6278
  const r = encoder.encodeInto(src.slice(start, limit), scratch);
6132
6279
  return { end: start + r.read, written: r.written };
6133
6280
  }
6134
- function merge(src, blocks, join6, cap, lineCut) {
6281
+ function merge(src, blocks, join7, cap, lineCut) {
6135
6282
  const out = [];
6136
6283
  const scratch = new Uint8Array(cap);
6137
6284
  let wide;
@@ -6141,7 +6288,7 @@ function merge(src, blocks, join6, cap, lineCut) {
6141
6288
  out.push({ ord: out.length, text: src.slice(start, end), bytes: bytes2 });
6142
6289
  };
6143
6290
  for (const b of blocks) {
6144
- if (cur !== void 0 && !join6) {
6291
+ if (cur !== void 0 && !join7) {
6145
6292
  emit(cur.start, cur.end, curBytes);
6146
6293
  cur = void 0;
6147
6294
  }
@@ -6249,7 +6396,7 @@ function sourceLabel(command) {
6249
6396
  return Array.from(RUN_LABEL_PREFIX + first).slice(0, SOURCE_LABEL_MAX_CHARS).join("");
6250
6397
  }
6251
6398
  function moreLine2(bundleDir, sourceId, terms) {
6252
- return RUN_MORE_PREFIX + JSON.stringify(join5(bundleDir, SEARCH_WRAPPER_FILE)) + " " + sourceId + " " + JSON.stringify(terms);
6399
+ return RUN_MORE_PREFIX + JSON.stringify(join6(bundleDir, SEARCH_WRAPPER_FILE)) + " " + sourceId + " " + JSON.stringify(terms);
6253
6400
  }
6254
6401
  function bytes(s) {
6255
6402
  return Buffer.byteLength(s, "utf8");