context-mode 2.0.0-next.17 → 2.0.0-next.18

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/server.mjs CHANGED
@@ -7,8 +7,8 @@ var __export = (target, all) => {
7
7
 
8
8
  // src/entries/server.ts
9
9
  import { randomUUID } from "node:crypto";
10
- import { existsSync as existsSync20, rmSync as rmSync11 } from "node:fs";
11
- import { join as join21 } from "node:path";
10
+ import { existsSync as existsSync21, realpathSync as realpathSync9, rmSync as rmSync11 } from "node:fs";
11
+ import { dirname as dirname10, join as join21 } from "node:path";
12
12
  import { fileURLToPath as fileURLToPath2 } from "node:url";
13
13
 
14
14
  // node_modules/@modelcontextprotocol/server/dist/chunk-Br0eD_fh.mjs
@@ -28291,6 +28291,9 @@ var HOOK_FAIL_REDIRECT = Object.freeze({
28291
28291
  function D5_LAST_HOOK_ERROR(ts, stage, label) {
28292
28292
  return `; last hook error ${ts} at ${stage} (${label}), stack in ${HOOK_ERROR_LOG_FILE}`;
28293
28293
  }
28294
+ function D5_PAST_HOOK_ERROR(ts, stage, label) {
28295
+ return `; past hook error ${ts} at ${stage} (${label}), before this run, stack in ${HOOK_ERROR_LOG_FILE}`;
28296
+ }
28294
28297
  var BOUNDED_COMMANDS = Object.freeze(["pwd", "whoami", "uname", "id", "realpath", "ln"]);
28295
28298
  var BOUNDED_GIT_SUBCOMMANDS = Object.freeze(["status"]);
28296
28299
  var VERSION_PROBE_FLAGS = Object.freeze(["--version", "-v"]);
@@ -28340,7 +28343,7 @@ var DECISION_SOURCE = "decision";
28340
28343
  var DECISION_USER_LABEL = "decision (user)";
28341
28344
  var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
28342
28345
  var RELEASE_BRANCH = "release";
28343
- var BUNDLE_SHA = true ? "d526f1e54835a809b09257c9587a22172141d4c3" : void 0;
28346
+ var BUNDLE_SHA = true ? "ea4179fd9a2c4c55eadc9184a295a7f974df90bd" : void 0;
28344
28347
  var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
28345
28348
  var DOCTOR_LABEL = Object.freeze({
28346
28349
  D1: "version",
@@ -28529,7 +28532,7 @@ var REGEX_ESCAPE = "\\";
28529
28532
  var CODEX_FEATURES_HOOKS_KEY = Object.freeze(["features", "hooks"]);
28530
28533
  var D5_SHAPE_DETAIL = "hook output is not the agent's native shape";
28531
28534
  function D5_SLOW_DETAIL(ms) {
28532
- return `hook round trip took ${ms} ms, over ${HOOK_P95_MS} ms`;
28535
+ return `hook round trip took ${ms} ms over bare node, over ${HOOK_P95_MS} ms`;
28533
28536
  }
28534
28537
  var D5_FIX = "run ctx_upgrade";
28535
28538
  var PRAGMA_INTEGRITY_CHECK = "PRAGMA integrity_check";
@@ -44278,7 +44281,7 @@ var ctxBatch = Object.freeze({
44278
44281
  });
44279
44282
 
44280
44283
  // src/core/tools/doctor.ts
44281
- import { readdirSync as readdirSync5, readFileSync as readFileSync8, existsSync as existsSync11 } from "node:fs";
44284
+ import { readdirSync as readdirSync6, readFileSync as readFileSync8, existsSync as existsSync12 } from "node:fs";
44282
44285
  import { basename as basename3, dirname as dirname5, join as join11 } from "node:path";
44283
44286
 
44284
44287
  // src/adapters/claude-code.ts
@@ -44557,7 +44560,8 @@ var RESULT_SUCCESS = "success";
44557
44560
  function isRecord(v) {
44558
44561
  return typeof v === "object" && v !== null && !Array.isArray(v);
44559
44562
  }
44560
- var NATIVE_TO_CANONICAL3 = { bash: "Bash" };
44563
+ var SHELL_TOOL_NAMES = Object.freeze(["bash", "powershell"]);
44564
+ var NATIVE_TO_CANONICAL3 = Object.fromEntries(SHELL_TOOL_NAMES.map((n) => [n, "Bash"]));
44561
44565
  function canonicalName3(name) {
44562
44566
  if (name.startsWith(TOOL_NAME_PREFIX5)) return name.slice(TOOL_NAME_PREFIX5.length);
44563
44567
  if (Object.hasOwn(NATIVE_TO_CANONICAL3, name)) return NATIVE_TO_CANONICAL3[name];
@@ -44638,11 +44642,11 @@ var VSCODE_EVENTS = ["SessionStart", "UserPromptSubmit", "PreToolUse", "PostTool
44638
44642
  var MCP_PREFIX2 = "mcp_";
44639
44643
  var MCP_SEP2 = "_";
44640
44644
  var SESSION_START_NEW = "new";
44641
- var SHELL_TOOL_NAMES = Object.freeze(["run_in_terminal", "runTerminalCommand", "bash", "powershell"]);
44645
+ var SHELL_TOOL_NAMES2 = Object.freeze(["run_in_terminal", "runTerminalCommand", "bash", "powershell"]);
44642
44646
  var READ_TOOL_NAMES = Object.freeze(["read_file", "view"]);
44643
44647
  var WEBFETCH_TOOL_NAMES = Object.freeze(["fetch_webpage", "web_fetch"]);
44644
44648
  var NATIVE_TO_CANONICAL4 = Object.fromEntries([
44645
- ...SHELL_TOOL_NAMES.map((n) => [n, "Bash"]),
44649
+ ...SHELL_TOOL_NAMES2.map((n) => [n, "Bash"]),
44646
44650
  ...READ_TOOL_NAMES.map((n) => [n, "Read"]),
44647
44651
  ...WEBFETCH_TOOL_NAMES.map((n) => [n, "WebFetch"])
44648
44652
  ]);
@@ -44770,8 +44774,8 @@ var KIRO_EVENTS = {
44770
44774
  };
44771
44775
  var PROMPT_ENV = "USER_PROMPT";
44772
44776
  var MCP_PREFIX3 = "mcp_";
44773
- var SHELL_TOOL_NAMES2 = Object.freeze(["execute_bash", "execute_pwsh", "shell"]);
44774
- var NATIVE_TO_CANONICAL5 = Object.fromEntries(SHELL_TOOL_NAMES2.map((n) => [n, "Bash"]));
44777
+ var SHELL_TOOL_NAMES3 = Object.freeze(["execute_bash", "execute_pwsh", "shell"]);
44778
+ var NATIVE_TO_CANONICAL5 = Object.fromEntries(SHELL_TOOL_NAMES3.map((n) => [n, "Bash"]));
44775
44779
  var MCP_SEP3 = "_";
44776
44780
  var KIRO_SOURCES = { startup: "startup", resume: "resume", clear: "clear", compact: "compact" };
44777
44781
  function canonicalName5(name) {
@@ -45043,8 +45047,9 @@ var ADAPTERS = Object.freeze([
45043
45047
  installMode: "plugin",
45044
45048
  projectSource: "arg:--agent+cwd",
45045
45049
  toolNamePrefix: TOOL_NAME_PREFIX5,
45046
- nativeTools: { shell: ["bash"] },
45050
+ nativeTools: { shell: [...SHELL_TOOL_NAMES] },
45047
45051
  execReadOnlyHint: false,
45052
+ // D-PWSH-DUMP-1
45048
45053
  eventMap: { SessionStart: "sessionStart", UserPromptSubmit: "userPromptSubmitted", PreToolUse: "preToolUse", PostToolUse: "postToolUse" },
45049
45054
  compactDelivery: "pending-flag",
45050
45055
  compactSummary: "none",
@@ -45062,7 +45067,7 @@ var ADAPTERS = Object.freeze([
45062
45067
  projectSource: "arg:workspaceFolder",
45063
45068
  toolNamePrefix: TOOL_NAME_PREFIX6,
45064
45069
  // PreCompact sets the pending flag (ADR-0009 §6, DEVIATIONS D-M3-A4-1).
45065
- nativeTools: { shell: [...SHELL_TOOL_NAMES], read: [...READ_TOOL_NAMES], webfetch: [...WEBFETCH_TOOL_NAMES] },
45070
+ nativeTools: { shell: [...SHELL_TOOL_NAMES2], read: [...READ_TOOL_NAMES], webfetch: [...WEBFETCH_TOOL_NAMES] },
45066
45071
  // D-HF-VSCODE-PROJECT-1, D-N7-VSCODE-TOOLS-1
45067
45072
  execReadOnlyHint: false,
45068
45073
  eventMap: { ...CLAUDE_EVENTS, PreCompact: "PreCompact" },
@@ -45081,7 +45086,7 @@ var ADAPTERS = Object.freeze([
45081
45086
  installMode: "mcp-entry",
45082
45087
  projectSource: "arg:--agent+cwd",
45083
45088
  toolNamePrefix: TOOL_NAME_PREFIX7,
45084
- nativeTools: { shell: [...SHELL_TOOL_NAMES2] },
45089
+ nativeTools: { shell: [...SHELL_TOOL_NAMES3] },
45085
45090
  execReadOnlyHint: false,
45086
45091
  // D-HF-KIRO-SHELL-1, D-N7-KIRO-1
45087
45092
  eventMap: { SessionStart: "SessionStart", UserPromptSubmit: "UserPromptSubmit", PreToolUse: "PreToolUse", PostToolUse: "PostToolUse" },
@@ -45165,6 +45170,7 @@ function getAdapter(id) {
45165
45170
  }
45166
45171
 
45167
45172
  // src/core/ops/doctor-config.ts
45173
+ import { existsSync as existsSync7, readdirSync as readdirSync5 } from "node:fs";
45168
45174
  import { join as join9 } from "node:path";
45169
45175
 
45170
45176
  // src/core/ops/detect-v1.ts
@@ -48222,7 +48228,47 @@ function mcpFile(row5, home, env) {
48222
48228
  }
48223
48229
  return null;
48224
48230
  }
48231
+ var CODEX_PLUGIN_KEY = `${SERVER_KEY}@${SERVER_KEY}`;
48232
+ var CODEX_PLUGIN_SUBDIR = ".codex-plugin";
48233
+ var CODEX_PLUGIN_MCP_FILE = "mcp.json";
48234
+ var CODEX_PLUGIN_HOOKS_FILE = "hooks.json";
48235
+ var MCP_SERVERS_FIELD = "mcpServers";
48236
+ function codexNativeDir(env, home) {
48237
+ const codexDir = codexDirOf(env, home);
48238
+ let enabled;
48239
+ try {
48240
+ enabled = hostConfigValueAt(readParsed("codex", join9(codexDir, CONFIG_TOML_FILE)).value, ["plugins", CODEX_PLUGIN_KEY, "enabled"]);
48241
+ } catch {
48242
+ return null;
48243
+ }
48244
+ if (enabled !== true) return null;
48245
+ const cache = join9(codexDir, "plugins", PLUGIN_CACHE_DIR_NAME);
48246
+ if (!existsSync7(cache)) return null;
48247
+ const found = [];
48248
+ for (const market of readdirSync5(cache)) {
48249
+ const plugin = join9(cache, market, SERVER_KEY);
48250
+ if (!existsSync7(plugin)) continue;
48251
+ for (const version4 of readdirSync5(plugin).sort()) {
48252
+ const dir = join9(plugin, version4, CODEX_PLUGIN_SUBDIR);
48253
+ if (existsSync7(join9(dir, CODEX_PLUGIN_MCP_FILE))) found.push(dir);
48254
+ }
48255
+ }
48256
+ return found.length === 0 ? null : found[found.length - 1];
48257
+ }
48258
+ function nativeMcpWrong(dir, row5) {
48259
+ try {
48260
+ const entry = hostConfigValueAt(readParsed(row5.id, join9(dir, CODEX_PLUGIN_MCP_FILE)).value, [MCP_SERVERS_FIELD, SERVER_KEY]);
48261
+ if (!isObject4(entry)) return true;
48262
+ const args = list(entry[MCP_ARGS_FIELD]);
48263
+ const script = args[0];
48264
+ return entry.command !== HOST_NODE_COMMAND || typeof script !== "string" || !script.endsWith(SERVER_ENTRY_FILE) || args[1] !== AGENT_ARG || args[2] !== row5.id || args.length !== 3;
48265
+ } catch {
48266
+ return true;
48267
+ }
48268
+ }
48225
48269
  function mcpWrong(row5, home, env) {
48270
+ const native = row5.id === "codex" ? codexNativeDir(env, home) : null;
48271
+ if (native !== null) return nativeMcpWrong(native, row5);
48226
48272
  const file2 = mcpFile(row5, home, env);
48227
48273
  try {
48228
48274
  if (file2 === null) {
@@ -48241,7 +48287,8 @@ function hookParts(row5, home, env, worktree) {
48241
48287
  let block;
48242
48288
  try {
48243
48289
  const root = spec.root === "plugin" ? resolvePluginRoot(row5, env) : spec.root === "home" ? home : worktree;
48244
- const file2 = row5.id === "codex" ? join9(codexDirOf(env, home), HOOKS_JSON_FILE) : row5.id === "copilot-cli" ? join9(copilotDirOf(env, home), ...spec.file.slice(1)) : row5.id === "kiro" ? join9(kiroDirOf(env, home), ...spec.file.slice(1)) : join9(root, ...spec.file);
48290
+ const native = row5.id === "codex" ? codexNativeDir(env, home) : null;
48291
+ const file2 = native !== null ? join9(native, CODEX_PLUGIN_HOOKS_FILE) : row5.id === "codex" ? join9(codexDirOf(env, home), HOOKS_JSON_FILE) : row5.id === "copilot-cli" ? join9(copilotDirOf(env, home), ...spec.file.slice(1)) : row5.id === "kiro" ? join9(kiroDirOf(env, home), ...spec.file.slice(1)) : join9(root, ...spec.file);
48245
48292
  block = hostConfigValueAt(readParsed(row5.id, file2).value, spec.key);
48246
48293
  if (row5.id === "codex") {
48247
48294
  const config4 = readParsed(row5.id, join9(codexDirOf(env, home), CONFIG_TOML_FILE)).value;
@@ -48304,7 +48351,7 @@ function checkDoubleFire(agent, worktree) {
48304
48351
  }
48305
48352
 
48306
48353
  // src/core/ops/doctor-db.ts
48307
- import { existsSync as existsSync7 } from "node:fs";
48354
+ import { existsSync as existsSync8 } from "node:fs";
48308
48355
  var unknown4 = (id, detail) => ({ id, label: DOCTOR_LABEL[id], status: "UNKNOWN", detail });
48309
48356
  var pass2 = (id, detail) => ({ id, label: DOCTOR_LABEL[id], status: "PASS", detail });
48310
48357
  var fail2 = (detail, fixCommand) => ({ id: "D8", label: DOCTOR_LABEL.D8, status: "FAIL", detail, fixCommand });
@@ -48344,7 +48391,7 @@ function failsOnCorrupt(step) {
48344
48391
  }
48345
48392
  async function checkDb(project) {
48346
48393
  const clean = { schemaStale: false, integrityFailed: false };
48347
- if (!existsSync7(project.dbPath)) return { row: pass2("D8", DOCTOR_PASS_DETAIL), ...clean };
48394
+ if (!existsSync8(project.dbPath)) return { row: pass2("D8", DOCTOR_PASS_DETAIL), ...clean };
48348
48395
  const sqlite = await loadSqlite2();
48349
48396
  if (sqlite === null) return { row: unknown4("D8", D2_SQLITE_DETAIL), ...clean };
48350
48397
  let raw = null;
@@ -48370,7 +48417,7 @@ async function checkDb(project) {
48370
48417
  }
48371
48418
  }
48372
48419
  async function checkIndex(project) {
48373
- if (!existsSync7(project.dbPath)) return pass2("D9", D9_INDEX_DETAIL(0, 0));
48420
+ if (!existsSync8(project.dbPath)) return pass2("D9", D9_INDEX_DETAIL(0, 0));
48374
48421
  const sqlite = await loadSqlite2();
48375
48422
  if (sqlite === null) return unknown4("D9", D2_SQLITE_DETAIL);
48376
48423
  let raw = null;
@@ -48387,7 +48434,7 @@ async function checkIndex(project) {
48387
48434
  }
48388
48435
 
48389
48436
  // src/core/ops/doctor-env.ts
48390
- import { existsSync as existsSync8, realpathSync as realpathSync5 } from "node:fs";
48437
+ import { existsSync as existsSync9, realpathSync as realpathSync5 } from "node:fs";
48391
48438
  import { isAbsolute as isAbsolute2, relative } from "node:path";
48392
48439
  function pass3(id, detail = DOCTOR_PASS_DETAIL) {
48393
48440
  return { id, label: DOCTOR_LABEL[id], status: "PASS", detail };
@@ -48445,7 +48492,7 @@ function checkMcp(ctx) {
48445
48492
  return named || versioned ? pass3("D6") : unknown5("D6", D6_UNKNOWN_DETAIL);
48446
48493
  }
48447
48494
  function piCwdInside(worktree, piCwd) {
48448
- if (piCwd === void 0 || !existsSync8(piCwd)) return false;
48495
+ if (piCwd === void 0 || !existsSync9(piCwd)) return false;
48449
48496
  const rel = relative(worktree, realpathSync5.native(piCwd));
48450
48497
  return rel === "" || !rel.startsWith("..") && !isAbsolute2(rel);
48451
48498
  }
@@ -48466,15 +48513,16 @@ function checkShell(env) {
48466
48513
  }
48467
48514
 
48468
48515
  // src/core/ops/doctor-hook.ts
48469
- import { existsSync as existsSync9, readFileSync as readFileSync7 } from "node:fs";
48516
+ import { existsSync as existsSync10, readFileSync as readFileSync7 } from "node:fs";
48470
48517
  import path4 from "node:path";
48518
+ var D5_SAMPLES = 3;
48471
48519
  var row = (status, detail, fixCommand) => fixCommand === void 0 ? { id: "D5", label: DOCTOR_LABEL.D5, status, detail } : { id: "D5", label: DOCTOR_LABEL.D5, status, detail, fixCommand };
48472
48520
  function isObject5(v) {
48473
48521
  return typeof v === "object" && v !== null && !Array.isArray(v);
48474
48522
  }
48475
48523
  function loadFixture(bundleDir2, agent) {
48476
48524
  const file2 = path4.join(bundleDir2, HOOK_FIXTURE_DIR, HOOK_FIXTURE_FILE);
48477
- if (!existsSync9(file2)) return null;
48525
+ if (!existsSync10(file2)) return null;
48478
48526
  let all;
48479
48527
  try {
48480
48528
  all = JSON.parse(hostText(readFileSync7(file2, "utf8")));
@@ -48486,6 +48534,9 @@ function loadFixture(bundleDir2, agent) {
48486
48534
  if (!isObject5(e) || typeof e.event !== "string" || !Object.hasOwn(e, "payload")) return null;
48487
48535
  return { event: e.event, payload: e.payload };
48488
48536
  }
48537
+ function payloadFor(payload, worktree) {
48538
+ return worktree !== void 0 && isObject5(payload) ? { ...payload, cwd: worktree } : payload;
48539
+ }
48489
48540
  function isNativeShape(stdout, exitCode, killed, event) {
48490
48541
  if (killed !== null || exitCode !== 0 && exitCode !== 2) return false;
48491
48542
  if (stdout.length === 0) return true;
@@ -48502,22 +48553,34 @@ function codexHookJournaled(db) {
48502
48553
  return db.raw.prepare("SELECT 1 FROM events WHERE agent = ? LIMIT 1").get("codex") !== void 0;
48503
48554
  }
48504
48555
  async function checkHookRoundTrip(i) {
48505
- if (i.agent === "codex" && existsSync9(path4.join(i.codexConfigDir, HOOKS_JSON_FILE)) && !codexHookJournaled(i.db)) {
48556
+ if (i.agent === "codex" && existsSync10(path4.join(i.codexConfigDir, HOOKS_JSON_FILE)) && !codexHookJournaled(i.db)) {
48506
48557
  return row("UNKNOWN", D5_CODEX_UNTRUSTED_DETAIL);
48507
48558
  }
48508
48559
  const fixture = loadFixture(i.bundleDir, i.agent);
48509
48560
  if (fixture === null) return row("UNKNOWN", D5_NO_FIXTURE_DETAIL(i.agent));
48510
48561
  const now = i.now ?? (() => performance.now());
48511
- const start = now();
48512
- const r = await runProcess([process.execPath, path4.join(i.bundleDir, HOOK_ENTRY_FILE), AGENT_ARG, i.agent], {
48513
- cwd: i.bundleDir,
48514
- env: i.env,
48515
- signal: i.signal,
48516
- timeoutMs: HOOK_TIMEOUT_MS,
48517
- stdin: JSON.stringify(fixture.payload)
48518
- });
48519
- const ms = Math.round(now() - start);
48520
- if (!isNativeShape(r.stdout.toString("utf8"), r.exitCode, r.killed, fixture.event)) return row("FAIL", D5_SHAPE_DETAIL, D5_FIX);
48562
+ const samples = Math.max(1, i.samples ?? D5_SAMPLES);
48563
+ const bareMs = i.bareMs ?? (async () => {
48564
+ const t = performance.now();
48565
+ await runProcess([process.execPath, "-e", ""], { cwd: i.bundleDir, env: i.env, signal: i.signal, timeoutMs: HOOK_TIMEOUT_MS });
48566
+ return performance.now() - t;
48567
+ });
48568
+ const hookWalls = [];
48569
+ const bareWalls = [];
48570
+ for (let n = 0; n < samples; n++) {
48571
+ const start = now();
48572
+ const r = await runProcess([process.execPath, path4.join(i.bundleDir, HOOK_ENTRY_FILE), AGENT_ARG, i.agent], {
48573
+ cwd: i.bundleDir,
48574
+ env: i.env,
48575
+ signal: i.signal,
48576
+ timeoutMs: HOOK_TIMEOUT_MS,
48577
+ stdin: JSON.stringify(payloadFor(fixture.payload, i.worktree))
48578
+ });
48579
+ hookWalls.push(now() - start);
48580
+ if (!isNativeShape(r.stdout.toString("utf8"), r.exitCode, r.killed, fixture.event)) return row("FAIL", D5_SHAPE_DETAIL, D5_FIX);
48581
+ bareWalls.push(await bareMs());
48582
+ }
48583
+ const ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
48521
48584
  if (ms > HOOK_P95_MS) return row("FAIL", D5_SLOW_DETAIL(ms), D5_FIX);
48522
48585
  return row("PASS", DOCTOR_PASS_DETAIL);
48523
48586
  }
@@ -48560,7 +48623,7 @@ function lastHookError(env) {
48560
48623
  }
48561
48624
 
48562
48625
  // src/core/ops/db-heal.ts
48563
- import { existsSync as existsSync10, rmSync as rmSync4 } from "node:fs";
48626
+ import { existsSync as existsSync11, rmSync as rmSync4 } from "node:fs";
48564
48627
  function integrityOk2(rows) {
48565
48628
  return rows.length === 1 && rows[0].integrity_check === INTEGRITY_OK;
48566
48629
  }
@@ -48594,7 +48657,7 @@ function renameAside(dbPath) {
48594
48657
  const ts = String(Date.now());
48595
48658
  for (const suffix of DB_SIBLING_SUFFIXES) {
48596
48659
  const file2 = dbPath + suffix;
48597
- if (existsSync10(file2)) renameWithRetry(file2, file2 + HEAL_TS_SEP + ts + HEAL_BACKUP_SUFFIX);
48660
+ if (existsSync11(file2)) renameWithRetry(file2, file2 + HEAL_TS_SEP + ts + HEAL_BACKUP_SUFFIX);
48598
48661
  }
48599
48662
  return ts;
48600
48663
  }
@@ -48694,7 +48757,7 @@ async function guard(id, run) {
48694
48757
  }
48695
48758
  }
48696
48759
  async function codexDb(ctx, adapter) {
48697
- if (adapter.id !== "codex" || !existsSync11(ctx.project.dbPath)) return null;
48760
+ if (adapter.id !== "codex" || !existsSync12(ctx.project.dbPath)) return null;
48698
48761
  try {
48699
48762
  return await openDb(ctx.project, { readOnly: true });
48700
48763
  } catch {
@@ -48703,8 +48766,10 @@ async function codexDb(ctx, adapter) {
48703
48766
  }
48704
48767
  async function hookRow(ctx, adapter, host) {
48705
48768
  const db = await codexDb(ctx, adapter);
48769
+ const runStart = Date.now();
48706
48770
  try {
48707
48771
  const row5 = await checkHookRoundTrip({
48772
+ worktree: ctx.project.worktree,
48708
48773
  agent: adapter.id,
48709
48774
  bundleDir: host.bundleDir,
48710
48775
  env: buildChildEnv(host.env),
@@ -48714,15 +48779,17 @@ async function hookRow(ctx, adapter, host) {
48714
48779
  db
48715
48780
  });
48716
48781
  const last2 = lastHookError(host.env);
48717
- return last2 === null ? row5 : { ...row5, detail: row5.detail + D5_LAST_HOOK_ERROR(last2.ts, last2.stage, last2.label) };
48782
+ if (last2 === null) return row5;
48783
+ const text = Date.parse(last2.ts) >= runStart ? D5_LAST_HOOK_ERROR : D5_PAST_HOOK_ERROR;
48784
+ return { ...row5, detail: row5.detail + text(last2.ts, last2.stage, last2.label) };
48718
48785
  } finally {
48719
48786
  if (db !== null) closeDb(db);
48720
48787
  }
48721
48788
  }
48722
48789
  function ownServerId(configDir, projectKey) {
48723
48790
  const dir = join11(configDir, PROJECTS_DIR_NAME, projectKey, OWNERS_DIR_NAME);
48724
- if (!existsSync11(dir)) return "";
48725
- for (const name of readdirSync5(dir)) {
48791
+ if (!existsSync12(dir)) return "";
48792
+ for (const name of readdirSync6(dir)) {
48726
48793
  if (!name.endsWith(OWNER_PID_EXT)) continue;
48727
48794
  if (readFileSync8(join11(dir, name), "utf8").trim() === String(process.pid)) return basename3(name, OWNER_PID_EXT);
48728
48795
  }
@@ -48796,7 +48863,7 @@ var ctxDoctor = Object.freeze({
48796
48863
  });
48797
48864
 
48798
48865
  // src/core/tools/execute.ts
48799
- import { existsSync as existsSync12, mkdtempSync, rmSync as rmSync5, writeFileSync as writeFileSync3 } from "node:fs";
48866
+ import { existsSync as existsSync13, mkdtempSync, rmSync as rmSync5, writeFileSync as writeFileSync3 } from "node:fs";
48800
48867
  import { tmpdir as tmpdir2 } from "node:os";
48801
48868
  import { dirname as dirname6, join as join12, parse as parse5, resolve as resolve3 } from "node:path";
48802
48869
 
@@ -48921,7 +48988,7 @@ function schemaLine(issue4) {
48921
48988
  }
48922
48989
  function fileError(given, abs, root) {
48923
48990
  let dir = abs;
48924
- while (!existsSync12(dir) && dirname6(dir) !== dir) dir = dirname6(dir);
48991
+ while (!existsSync13(dir) && dirname6(dir) !== dir) dir = dirname6(dir);
48925
48992
  if (isInside(root, dir)) return null;
48926
48993
  return new CtxError("OUTSIDE_ROOT", { p: given, root }).toLine();
48927
48994
  }
@@ -49036,11 +49103,11 @@ var ctxExecute = {
49036
49103
  };
49037
49104
 
49038
49105
  // src/core/tools/index.ts
49039
- import { existsSync as existsSync13, readFileSync as readFileSync10, statSync as statSync5 } from "node:fs";
49106
+ import { existsSync as existsSync14, readFileSync as readFileSync10, statSync as statSync5 } from "node:fs";
49040
49107
  import { dirname as dirname7, extname, join as join14, relative as relative2, resolve as resolve4 } from "node:path";
49041
49108
 
49042
49109
  // src/core/index/walk.ts
49043
- import { readdirSync as readdirSync6, readFileSync as readFileSync9, realpathSync as realpathSync6, statSync as statSync4 } from "node:fs";
49110
+ import { readdirSync as readdirSync7, readFileSync as readFileSync9, realpathSync as realpathSync6, statSync as statSync4 } from "node:fs";
49044
49111
  import { join as join13 } from "node:path";
49045
49112
  var GITIGNORE = ".gitignore";
49046
49113
  var GIT_DIR = ".git";
@@ -49055,7 +49122,7 @@ function walkDir(root, containmentRoot) {
49055
49122
  function visit(abs, rel, parent, contain, seen, found) {
49056
49123
  let entries;
49057
49124
  try {
49058
- entries = readdirSync6(abs, { withFileTypes: true });
49125
+ entries = readdirSync7(abs, { withFileTypes: true });
49059
49126
  } catch {
49060
49127
  return;
49061
49128
  }
@@ -53192,7 +53259,7 @@ function readUtf8(absPath) {
53192
53259
  }
53193
53260
  function missingOrOutside(given, abs, root) {
53194
53261
  let dir = abs;
53195
- while (!existsSync13(dir) && dirname7(dir) !== dir) dir = dirname7(dir);
53262
+ while (!existsSync14(dir) && dirname7(dir) !== dir) dir = dirname7(dir);
53196
53263
  if (dir !== abs && isInside(root, dir)) return new CtxError("INVALID_ARGS", { field: "path", action: "pass an existing file or folder" });
53197
53264
  return new CtxError("OUTSIDE_ROOT", { p: given, root });
53198
53265
  }
@@ -53724,11 +53791,11 @@ var ctxStats = Object.freeze({
53724
53791
  });
53725
53792
 
53726
53793
  // src/core/tools/upgrade.ts
53727
- import { existsSync as existsSync19, realpathSync as realpathSync7, rmSync as rmSync10 } from "node:fs";
53794
+ import { existsSync as existsSync20, realpathSync as realpathSync7, rmSync as rmSync10 } from "node:fs";
53728
53795
  import { basename as basename5, dirname as dirname9, join as join20 } from "node:path";
53729
53796
 
53730
53797
  // src/core/ops/host-write.ts
53731
- import { copyFileSync, existsSync as existsSync14, mkdirSync as mkdirSync5, writeFileSync as writeFileSync4 } from "node:fs";
53798
+ import { copyFileSync, existsSync as existsSync15, mkdirSync as mkdirSync5, writeFileSync as writeFileSync4 } from "node:fs";
53732
53799
  import { dirname as dirname8, join as join15 } from "node:path";
53733
53800
  function defaultWrite(file2, text) {
53734
53801
  writeFileSync4(file2, text, "utf8");
@@ -53757,7 +53824,7 @@ function toRow(file2, edit) {
53757
53824
  function writeWithRollback(file2, text, write, atomic) {
53758
53825
  const trash = file2 + UPGRADE_TRASH_SUFFIX;
53759
53826
  const tmp = `${file2}${HOST_CONFIG_TMP_SUFFIX}.${process.pid}`;
53760
- const had = existsSync14(file2);
53827
+ const had = existsSync15(file2);
53761
53828
  mkdirSync5(dirname8(file2), { recursive: true });
53762
53829
  if (had) copyFileSync(file2, trash);
53763
53830
  try {
@@ -53787,7 +53854,7 @@ function regenerateHostConfig(row5, home, opts = {}) {
53787
53854
  if (requested === null) return [];
53788
53855
  const current = readHostConfig(row5.id, requested);
53789
53856
  const result = mergeOwnEntries(row5.id, current.text, current.format, row5);
53790
- const exists = existsSync14(current.path);
53857
+ const exists = existsSync15(current.path);
53791
53858
  if (exists && result.text === current.text) return [];
53792
53859
  const text = result.text === "" && current.format !== "toml" ? JSON.stringify({}) : result.text;
53793
53860
  writeWithRollback(current.path, current.layout === void 0 ? text : withLayout(text, current.layout), opts.write ?? defaultWrite, opts.atomic ?? {});
@@ -53795,7 +53862,7 @@ function regenerateHostConfig(row5, home, opts = {}) {
53795
53862
  }
53796
53863
 
53797
53864
  // src/core/ops/plugin-swap.ts
53798
- import { existsSync as existsSync15, lstatSync, readdirSync as readdirSync7, readlinkSync, rmSync as rmSync6, symlinkSync, unlinkSync as unlinkSync2 } from "node:fs";
53865
+ import { existsSync as existsSync16, lstatSync, readdirSync as readdirSync8, readlinkSync, rmSync as rmSync6, symlinkSync, unlinkSync as unlinkSync2 } from "node:fs";
53799
53866
  import { basename as basename4, join as join16, win32 as win322 } from "node:path";
53800
53867
  function linkTarget(path5, win) {
53801
53868
  if (!isLink(path5)) return null;
@@ -53806,7 +53873,7 @@ function isLink(path5) {
53806
53873
  return lstatSync(path5, { throwIfNoEntry: false })?.isSymbolicLink() === true;
53807
53874
  }
53808
53875
  function generations(agentDir) {
53809
- return readdirSync7(agentDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name).filter((name) => !name.startsWith(UPGRADE_FETCH_DIR_PREFIX) && !name.endsWith(UPGRADE_TRASH_SUFFIX)).sort();
53876
+ return readdirSync8(agentDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name).filter((name) => !name.startsWith(UPGRADE_FETCH_DIR_PREFIX) && !name.endsWith(UPGRADE_TRASH_SUFFIX)).sort();
53810
53877
  }
53811
53878
  function settleLinks(link, tmp, old, opts) {
53812
53879
  if (isLink(tmp)) unlinkSync2(tmp);
@@ -53816,7 +53883,7 @@ function settleLinks(link, tmp, old, opts) {
53816
53883
  }
53817
53884
  function swapPluginCurrent(agentDir, sha, opts = {}) {
53818
53885
  const next = join16(agentDir, sha);
53819
- if (!existsSync15(next) || !lstatSync(next).isDirectory()) {
53886
+ if (!existsSync16(next) || !lstatSync(next).isDirectory()) {
53820
53887
  throw new CtxError("PLUGIN_ROOT_MISSING", { agent: basename4(agentDir), path: next });
53821
53888
  }
53822
53889
  const link = join16(agentDir, PLUGIN_CURRENT_LINK);
@@ -53836,12 +53903,12 @@ function swapPluginCurrent(agentDir, sha, opts = {}) {
53836
53903
  if (before !== null) keep.add(before);
53837
53904
  const removed = generations(agentDir).filter((name) => !keep.has(name)).map((name) => join16(agentDir, name));
53838
53905
  for (const dir of removed) rmSync6(dir, { recursive: true, force: true });
53839
- const previous = before !== null && existsSync15(join16(agentDir, before)) ? join16(agentDir, before) : null;
53906
+ const previous = before !== null && existsSync16(join16(agentDir, before)) ? join16(agentDir, before) : null;
53840
53907
  return { current: next, previous, removed };
53841
53908
  }
53842
53909
 
53843
53910
  // src/core/ops/release-fetch.ts
53844
- import { existsSync as existsSync16, mkdirSync as mkdirSync6, rmSync as rmSync7 } from "node:fs";
53911
+ import { existsSync as existsSync17, mkdirSync as mkdirSync6, rmSync as rmSync7 } from "node:fs";
53845
53912
  import { join as join17 } from "node:path";
53846
53913
  function failed(r) {
53847
53914
  if (r.exitCode === null && r.killed === null) return new CtxError("FETCH_FAILED", { code: GIT_NOT_STARTED });
@@ -53865,7 +53932,7 @@ async function fetchRelease(agent, url4, env, signal, opts = {}) {
53865
53932
  await git2(gitBin, [...GIT_CLONE_ARGS, RELEASE_BRANCH, url4, fetchDir], agentDir, childEnv, signal);
53866
53933
  const sha = await git2(gitBin, GIT_HEAD_SHA_ARGS, fetchDir, childEnv, signal);
53867
53934
  const dir = join17(agentDir, sha);
53868
- if (existsSync16(dir)) rmSync7(fetchDir, { recursive: true, force: true });
53935
+ if (existsSync17(dir)) rmSync7(fetchDir, { recursive: true, force: true });
53869
53936
  else renameWithRetry(fetchDir, dir, opts);
53870
53937
  return { sha, dir };
53871
53938
  } catch (e) {
@@ -53875,7 +53942,7 @@ async function fetchRelease(agent, url4, env, signal, opts = {}) {
53875
53942
  }
53876
53943
 
53877
53944
  // src/core/ops/uninstall.ts
53878
- import { existsSync as existsSync17, readdirSync as readdirSync8, rmSync as rmSync8, writeFileSync as writeFileSync5 } from "node:fs";
53945
+ import { existsSync as existsSync18, readdirSync as readdirSync9, rmSync as rmSync8, writeFileSync as writeFileSync5 } from "node:fs";
53879
53946
  import { join as join18 } from "node:path";
53880
53947
  function row2(area, before, after) {
53881
53948
  return { area, before, after };
@@ -53905,7 +53972,7 @@ function removeCache(adapter, env, configDir) {
53905
53972
  }
53906
53973
  case "plugin": {
53907
53974
  const agentDir = join18(configDir, DATA_PLUGINS_DIR_NAME, adapter.id);
53908
- if (!existsSync17(agentDir)) return row2(UPGRADE_CACHE_AREA, "", UPGRADE_NOT_FOUND);
53975
+ if (!existsSync18(agentDir)) return row2(UPGRADE_CACHE_AREA, "", UPGRADE_NOT_FOUND);
53909
53976
  rmSync8(agentDir, { recursive: true, force: true });
53910
53977
  return row2(UPGRADE_CACHE_AREA, agentDir, UPGRADE_REMOVED);
53911
53978
  }
@@ -53985,9 +54052,9 @@ function removeOwnEntries(agent, env) {
53985
54052
  function removeProjectData(configDir, projectKey) {
53986
54053
  const projects = join18(configDir, PROJECTS_DIR_NAME);
53987
54054
  const dir = join18(projects, projectKey);
53988
- if (!existsSync17(dir)) return row2(UPGRADE_DATA_AREA, "", UPGRADE_NOT_FOUND);
54055
+ if (!existsSync18(dir)) return row2(UPGRADE_DATA_AREA, "", UPGRADE_NOT_FOUND);
53989
54056
  if (dir === projects || !isInside(projects, dir)) throw new CtxError("OUTSIDE_ROOT", { p: dir, root: projects });
53990
- for (const name of readdirSync8(dir)) {
54057
+ for (const name of readdirSync9(dir)) {
53991
54058
  if (name !== OWNERS_DIR_NAME) rmSync8(join18(dir, name), { recursive: true, force: true });
53992
54059
  }
53993
54060
  return row2(UPGRADE_DATA_AREA, dir, UPGRADE_DATA_DELETED);
@@ -54001,7 +54068,7 @@ function runUninstall(input) {
54001
54068
  }
54002
54069
 
54003
54070
  // src/core/ops/v1-remove.ts
54004
- import { copyFileSync as copyFileSync2, existsSync as existsSync18, readFileSync as readFileSync11, rmSync as rmSync9 } from "node:fs";
54071
+ import { copyFileSync as copyFileSync2, existsSync as existsSync19, readFileSync as readFileSync11, rmSync as rmSync9 } from "node:fs";
54005
54072
  var isObj3 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
54006
54073
  var areaOf = (f) => f.path === "" ? f.file : f.file + UPGRADE_AREA_KEY_SEP + f.path;
54007
54074
  var row3 = (f, after) => ({ area: areaOf(f), before: f.reason, after });
@@ -54082,14 +54149,14 @@ function removeV1(agent, findings) {
54082
54149
  for (const f of findings) {
54083
54150
  if (UPGRADE_REPORT_ONLY_REASONS.includes(f.reason)) done.set(f, UPGRADE_REPORT_ONLY);
54084
54151
  else if (f.path === "") {
54085
- if (existsSync18(f.file)) {
54152
+ if (existsSync19(f.file)) {
54086
54153
  moveAside(f.file, undo);
54087
54154
  done.set(f, UPGRADE_REMOVED);
54088
54155
  } else done.set(f, UPGRADE_NOT_FOUND);
54089
54156
  } else entries.set(f.file, [...entries.get(f.file) ?? [], f]);
54090
54157
  }
54091
54158
  for (const [file2, list3] of entries) {
54092
- if (!existsSync18(file2)) {
54159
+ if (!existsSync19(file2)) {
54093
54160
  for (const f of list3) done.set(f, UPGRADE_NOT_FOUND);
54094
54161
  continue;
54095
54162
  }
@@ -54122,7 +54189,7 @@ function removeV1(agent, findings) {
54122
54189
  },
54123
54190
  rollback() {
54124
54191
  for (const u of undo.splice(0).reverse()) {
54125
- if (!existsSync18(u.trash)) continue;
54192
+ if (!existsSync19(u.trash)) continue;
54126
54193
  rmSync9(u.target, { recursive: true, force: true });
54127
54194
  renameWithRetry(u.trash, u.target);
54128
54195
  }
@@ -54132,7 +54199,7 @@ function removeV1(agent, findings) {
54132
54199
 
54133
54200
  // src/core/ops/verify-sums.ts
54134
54201
  import { createHash } from "node:crypto";
54135
- import { lstatSync as lstatSync2, readdirSync as readdirSync9, readFileSync as readFileSync12 } from "node:fs";
54202
+ import { lstatSync as lstatSync2, readdirSync as readdirSync10, readFileSync as readFileSync12 } from "node:fs";
54136
54203
  import { join as join19 } from "node:path";
54137
54204
  function mismatch() {
54138
54205
  return new CtxError("BUNDLE_HASH_MISMATCH");
@@ -54151,7 +54218,7 @@ function parseSums(text) {
54151
54218
  return sums;
54152
54219
  }
54153
54220
  function walk2(dir, prefix, out) {
54154
- for (const entry of readdirSync9(dir, { withFileTypes: true })) {
54221
+ for (const entry of readdirSync10(dir, { withFileTypes: true })) {
54155
54222
  const rel = prefix === "" ? entry.name : prefix + "/" + entry.name;
54156
54223
  if (prefix === "" && (entry.name === GIT_DIR_NAME || entry.name === RELEASE_SUMS_FILE)) continue;
54157
54224
  const abs = join19(dir, entry.name);
@@ -54200,7 +54267,7 @@ function pluginRootRow(adapter, env) {
54200
54267
  }
54201
54268
  function isCurrent(agentDir, dir) {
54202
54269
  const link = join20(agentDir, PLUGIN_CURRENT_LINK);
54203
- return existsSync19(link) && realpathSync7.native(link) === realpathSync7.native(dir);
54270
+ return existsSync20(link) && realpathSync7.native(link) === realpathSync7.native(dir);
54204
54271
  }
54205
54272
  async function checkout(adapter, deps) {
54206
54273
  const fetched = await fetchRelease(adapter.id, deps.releaseUrl, deps.env, deps.signal);
@@ -54385,6 +54452,32 @@ function sessionIdOf(meta3, agent, env, serverId) {
54385
54452
  if (typeof fromHost === "string" && fromHost !== "") return fromHost;
54386
54453
  return RUN_SESSION_PREFIX + serverId;
54387
54454
  }
54455
+ var CODEX_SANDBOX_STATE_META = "codex/sandbox-state-meta";
54456
+ var FILE_URL_PREFIX = "file:";
54457
+ function workspaceFromMeta(meta3) {
54458
+ const state = meta3?.[CODEX_SANDBOX_STATE_META];
54459
+ const cwd = typeof state === "object" && state !== null ? state.sandboxCwd : void 0;
54460
+ if (typeof cwd !== "string" || cwd === "") return void 0;
54461
+ if (!cwd.startsWith(FILE_URL_PREFIX)) return cwd;
54462
+ try {
54463
+ return fileURLToPath2(cwd);
54464
+ } catch {
54465
+ return void 0;
54466
+ }
54467
+ }
54468
+ function realOrSame(p) {
54469
+ try {
54470
+ return realpathSync9.native(p);
54471
+ } catch {
54472
+ return p;
54473
+ }
54474
+ }
54475
+ function workspaceCwdOf(meta3, spawnCwd, script) {
54476
+ const fromHost = workspaceFromMeta(meta3);
54477
+ if (fromHost !== void 0) return fromHost;
54478
+ if (script === void 0 || script === "") return spawnCwd;
54479
+ return realOrSame(spawnCwd) === realOrSame(dirname10(script)) ? "" : spawnCwd;
54480
+ }
54388
54481
  function errorReply(err) {
54389
54482
  if (err instanceof CtxError) return { text: err.toLine(), isError: true, sandboxBytes: 0 };
54390
54483
  console.error(err);
@@ -54422,7 +54515,7 @@ async function startServer(argv, env) {
54422
54515
  opening.catch(() => pending.delete(project.dbPath));
54423
54516
  }
54424
54517
  const db = await opening;
54425
- if (!existsSync20(db.path)) throw new CtxError("DB_GONE");
54518
+ if (!existsSync21(db.path)) throw new CtxError("DB_GONE");
54426
54519
  return db;
54427
54520
  }
54428
54521
  function stopMarker(configDir, projectKey) {
@@ -54430,7 +54523,7 @@ async function startServer(argv, env) {
54430
54523
  }
54431
54524
  function stopRequested() {
54432
54525
  const configDir = resolveConfigDir(process.env);
54433
- return held.some((db) => existsSync20(stopMarker(configDir, db.projectKey)));
54526
+ return held.some((db) => existsSync21(stopMarker(configDir, db.projectKey)));
54434
54527
  }
54435
54528
  async function journal(project, ctx, name, reply4) {
54436
54529
  const db = await ownDb(project);
@@ -54443,7 +54536,7 @@ async function startServer(argv, env) {
54443
54536
  let project;
54444
54537
  const projectOf = () => {
54445
54538
  if (agent === void 0) throw new CtxError("NO_PROJECT", { kind: "--agent" });
54446
- project ??= resolveProject(agent, { env, argv, cwd: process.cwd(), piCwd: void 0 });
54539
+ project ??= resolveProject(agent, { env, argv, cwd: workspaceCwdOf(meta3, process.cwd(), process.argv[1]), piCwd: void 0 });
54447
54540
  return project;
54448
54541
  };
54449
54542
  const ctx = {
@@ -54495,6 +54588,7 @@ async function startServer(argv, env) {
54495
54588
  // DEVIATIONS: no version constant in contracts yet
54496
54589
  {
54497
54590
  instructions: INSTRUCTIONS,
54591
+ capabilities: { experimental: { [CODEX_SANDBOX_STATE_META]: {} } },
54498
54592
  cacheHints: {
54499
54593
  "tools/list": { ttlMs: TOOLS_TTL_MS, cacheScope: "public" },
54500
54594
  "server/discover": { ttlMs: TOOLS_TTL_MS, cacheScope: "public" }
@@ -54545,7 +54639,8 @@ if (isEntry(import.meta.main, process.argv[1], fileURLToPath2(import.meta.url)))
54545
54639
  export {
54546
54640
  INSTRUCTIONS,
54547
54641
  sessionIdOf,
54548
- startServer
54642
+ startServer,
54643
+ workspaceCwdOf
54549
54644
  };
54550
54645
  /*! Bundled license information:
54551
54646