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

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.
@@ -362,7 +362,7 @@ var DECISION_OPTION_KEY = "option";
362
362
  var DECISION_LABEL_JOIN = " | ";
363
363
  var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
364
364
  var RELEASE_BRANCH = "release";
365
- var BUNDLE_SHA = true ? "ea4179fd9a2c4c55eadc9184a295a7f974df90bd" : void 0;
365
+ var BUNDLE_SHA = true ? "fa9bffea6220e2da2a37f2568e1fa96671f5b7c7" : void 0;
366
366
  var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
367
367
  var DOCTOR_LABEL = Object.freeze({
368
368
  D1: "version",
@@ -1427,7 +1427,8 @@ var OPENCODE_EVENTS = {
1427
1427
  "step-finish": "Usage"
1428
1428
  };
1429
1429
  var SHELL_TOOL_NAME = "bash";
1430
- var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", [SHELL_TOOL_NAME]: "Bash" };
1430
+ var SHELL_TOOL_NAMES4 = Object.freeze([SHELL_TOOL_NAME, "shell"]);
1431
+ var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", ...Object.fromEntries(SHELL_TOOL_NAMES4.map((n) => [n, "Bash"])) };
1431
1432
  function toCanonicalTool(name) {
1432
1433
  if (name.startsWith(OWN_WIRE_PREFIX)) return name.slice(OWN_WIRE_PREFIX.length);
1433
1434
  return Object.prototype.hasOwnProperty.call(NATIVE_TO_CANONICAL7, name) ? NATIVE_TO_CANONICAL7[name] : name;
@@ -19964,8 +19965,8 @@ function decisionQueries(input) {
19964
19965
  function pageSize(input) {
19965
19966
  return input.source === DECISION_SOURCE ? SEARCH_PAGE_SIZE : SEARCH_REPLY_HITS;
19966
19967
  }
19967
- function page(db, q, input, scope, ctx, offset) {
19968
- const size = pageSize(input);
19968
+ function page(db, q, input, scope, ctx, offset, extra = 0) {
19969
+ const size = pageSize(input) + extra;
19969
19970
  if (input.source === DECISION_SOURCE && q.trim() === "") {
19970
19971
  return ftsListSource(db, DECISION_SOURCE, scope, ctx.project, ctx.sessionId, size, offset);
19971
19972
  }
@@ -20014,51 +20015,76 @@ function jsonRowsReply(db, input, scope, ctx) {
20014
20015
  }
20015
20016
  return shown2 === 0 && !more ? null : reply(out.join("\n"), false);
20016
20017
  }
20018
+ var SMALL_SOURCE_SHARE = 0.5;
20019
+ var SOURCE_BYTES = "SELECT id, bytes_total FROM sources WHERE id IN (SELECT value FROM json_each(:ids))";
20020
+ function sourceBytes(db, ids) {
20021
+ const out = /* @__PURE__ */ new Map();
20022
+ if (ids.length === 0) return out;
20023
+ for (const r of db.raw.prepare(SOURCE_BYTES).all({ ids: JSON.stringify(ids) })) {
20024
+ out.set(Number(r.id), Number(r.bytes_total));
20025
+ }
20026
+ return out;
20027
+ }
20017
20028
  function relevance(db, given, scope, ctx) {
20018
20029
  const input = { ...given, queries: decisionQueries(given) };
20019
20030
  const offset = input.offset ?? 0;
20020
20031
  const size = pageSize(input);
20021
- const seen = /* @__PURE__ */ new Set();
20022
- const entries = [];
20023
- const full = /* @__PURE__ */ new Set();
20024
- input.queries.forEach((q, qi) => {
20025
- const hits = page(db, q, input, scope, ctx, offset);
20026
- if (hits.length === size) full.add(qi);
20027
- hits.forEach((hit, i) => {
20028
- const key = hit.sourceId + ":" + hit.ord;
20029
- if (seen.has(key)) return;
20030
- seen.add(key);
20031
- entries.push({ qi, pos: offset + i, hit });
20032
- });
20033
- });
20034
- if (entries.length === 0) return zeroHits(db, scope, ctx, input.source);
20032
+ const fetched = input.queries.map((q) => page(db, q, input, scope, ctx, offset, 1));
20033
+ const pages = fetched.map((h) => h.slice(0, size));
20034
+ if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source);
20035
20035
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, input.source, "offset:" + pos);
20036
20036
  let reserve = 0;
20037
20037
  for (const q of input.queries) reserve += bytes2(moreLine3(q, offset + size)) + 1;
20038
+ const total = input.source === DECISION_SOURCE ? /* @__PURE__ */ new Map() : sourceBytes(db, [...new Set(pages.flat().map((h) => h.sourceId))]);
20039
+ const spent = /* @__PURE__ */ new Map();
20038
20040
  const lines = [];
20039
20041
  const more = [];
20042
+ const shown2 = /* @__PURE__ */ new Set();
20040
20043
  let used = 0;
20041
20044
  let stopped = false;
20042
20045
  input.queries.forEach((q, qi) => {
20043
- const mine = entries.filter((e) => e.qi === qi);
20046
+ const hits = pages[qi];
20044
20047
  if (stopped) {
20045
- more.push(moreLine3(q, mine.length > 0 ? mine[0].pos : offset));
20048
+ more.push(moreLine3(q, offset));
20046
20049
  return;
20047
20050
  }
20048
20051
  const head = "Query " + JSON.stringify(q) + ":";
20049
20052
  lines.push(head);
20050
20053
  used += bytes2(head) + 1;
20051
- for (const e of mine) {
20052
- const line = e.hit.label + " #" + e.hit.ord + ": " + e.hit.snippet;
20054
+ const again = /* @__PURE__ */ new Map();
20055
+ const mine = /* @__PURE__ */ new Set();
20056
+ let cut = -1;
20057
+ for (let i = 0; i < hits.length; i++) {
20058
+ const hit = hits[i];
20059
+ const key = hit.sourceId + ":" + hit.ord;
20060
+ if (shown2.has(key)) {
20061
+ again.set(hit.label, [...again.get(hit.label) ?? [], hit.ord]);
20062
+ continue;
20063
+ }
20064
+ const line = hit.label + " #" + hit.ord + ": " + hit.snippet;
20053
20065
  if (used + bytes2(line) + 1 + reserve > OUTPUT_CAP) {
20054
- more.push(moreLine3(q, e.pos));
20066
+ cut = offset + i;
20055
20067
  stopped = true;
20056
20068
  break;
20057
20069
  }
20070
+ const room = total.get(hit.sourceId);
20071
+ if (room !== void 0 && mine.has(hit.sourceId) && (spent.get(hit.sourceId) ?? 0) + bytes2(line) + 1 > room * SMALL_SOURCE_SHARE * (qi + 1) / input.queries.length) {
20072
+ cut = offset + i;
20073
+ break;
20074
+ }
20075
+ lines.push(line);
20076
+ shown2.add(key);
20077
+ used += bytes2(line) + 1;
20078
+ spent.set(hit.sourceId, (spent.get(hit.sourceId) ?? 0) + bytes2(line) + 1);
20079
+ mine.add(hit.sourceId);
20080
+ }
20081
+ if (again.size > 0) {
20082
+ const line = "Same as above: " + [...again].map(([label, ords]) => label + " (chunk " + ords.join(",") + ")").join("; ");
20058
20083
  lines.push(line);
20059
20084
  used += bytes2(line) + 1;
20060
20085
  }
20061
- if (!stopped && full.has(qi)) more.push(moreLine3(q, offset + size));
20086
+ if (cut >= 0) more.push(moreLine3(q, cut));
20087
+ else if (fetched[qi].length > size) more.push(moreLine3(q, offset + size));
20062
20088
  });
20063
20089
  return reply(lines.concat(more).join("\n"), false);
20064
20090
  }
@@ -22826,7 +22852,7 @@ function ownMcpValue(agent, node, server2) {
22826
22852
  return { ...mcp.fields, [MCP_COMMAND_FIELD]: node, [MCP_ARGS_FIELD]: [server2, AGENT_ARG, agent, ...tail] };
22827
22853
  }
22828
22854
  function opencodePluginFile() {
22829
- return join8(dirname4(process.argv[1] ?? ""), ...OPENCODE_PLUGIN_PATH);
22855
+ return dirname4(process.argv[1] ?? "");
22830
22856
  }
22831
22857
  function listTarget(key, own2, v1, alt = []) {
22832
22858
  return {
@@ -23657,7 +23683,8 @@ function checkShell(env) {
23657
23683
  // src/core/ops/doctor-hook.ts
23658
23684
  import { existsSync as existsSync9, readFileSync as readFileSync9 } from "node:fs";
23659
23685
  import path4 from "node:path";
23660
- var D5_SAMPLES = 3;
23686
+ var D5_MIN_PAIRS = 3;
23687
+ var D5_MAX_PAIRS = 9;
23661
23688
  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 };
23662
23689
  function isObject5(v) {
23663
23690
  return typeof v === "object" && v !== null && !Array.isArray(v);
@@ -23701,7 +23728,9 @@ async function checkHookRoundTrip(i) {
23701
23728
  const fixture = loadFixture(i.bundleDir, i.agent);
23702
23729
  if (fixture === null) return row("UNKNOWN", D5_NO_FIXTURE_DETAIL(i.agent));
23703
23730
  const now = i.now ?? (() => performance.now());
23704
- const samples = Math.max(1, i.samples ?? D5_SAMPLES);
23731
+ const fixed = i.samples === void 0 ? null : Math.max(1, i.samples);
23732
+ const maxPairs = fixed ?? D5_MAX_PAIRS;
23733
+ const minPairs = fixed ?? D5_MIN_PAIRS;
23705
23734
  const bareMs = i.bareMs ?? (async () => {
23706
23735
  const t = performance.now();
23707
23736
  await runProcess([process.execPath, "-e", ""], { cwd: i.bundleDir, env: i.env, signal: i.signal, timeoutMs: HOOK_TIMEOUT_MS });
@@ -23709,7 +23738,8 @@ async function checkHookRoundTrip(i) {
23709
23738
  });
23710
23739
  const hookWalls = [];
23711
23740
  const bareWalls = [];
23712
- for (let n = 0; n < samples; n++) {
23741
+ let ms = 0;
23742
+ for (let n = 1; n <= maxPairs; n++) {
23713
23743
  const start = now();
23714
23744
  const r = await runProcess([process.execPath, path4.join(i.bundleDir, HOOK_ENTRY_FILE), AGENT_ARG, i.agent], {
23715
23745
  cwd: i.bundleDir,
@@ -23721,8 +23751,9 @@ async function checkHookRoundTrip(i) {
23721
23751
  hookWalls.push(now() - start);
23722
23752
  if (!isNativeShape(r.stdout.toString("utf8"), r.exitCode, r.killed, fixture.event)) return row("FAIL", D5_SHAPE_DETAIL, D5_FIX);
23723
23753
  bareWalls.push(await bareMs());
23754
+ ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
23755
+ if (n >= minPairs && ms <= HOOK_P95_MS) break;
23724
23756
  }
23725
- const ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
23726
23757
  if (ms > HOOK_P95_MS) return row("FAIL", D5_SLOW_DETAIL(ms), D5_FIX);
23727
23758
  return row("PASS", DOCTOR_PASS_DETAIL);
23728
23759
  }
@@ -29219,7 +29250,7 @@ function checkCall(tool, input) {
29219
29250
  // src/entries/opencode.ts
29220
29251
  var AGENT = "opencode";
29221
29252
  var id = PLUGIN_ID;
29222
- async function setup(app) {
29253
+ async function setupLegacy(app) {
29223
29254
  const cwd = app.directory;
29224
29255
  const env = process.env;
29225
29256
  const turns = /* @__PURE__ */ new Map();
@@ -29301,6 +29332,7 @@ async function setup(app) {
29301
29332
  callID: input.callID,
29302
29333
  args: input.args,
29303
29334
  output: output.output,
29335
+ isError: output.isError,
29304
29336
  turn: turnOf(sessionId)
29305
29337
  });
29306
29338
  },
@@ -29324,9 +29356,61 @@ async function setup(app) {
29324
29356
  }
29325
29357
  };
29326
29358
  }
29359
+ function oc2Text(content) {
29360
+ if (typeof content === "string") return content;
29361
+ return (content ?? []).filter((c) => c.type === "text").map((c) => c.text ?? "").join("\n");
29362
+ }
29363
+ async function setup(ctx) {
29364
+ const tools = /* @__PURE__ */ new Map();
29365
+ const hooks = await setupLegacy({
29366
+ directory: ctx.location.directory,
29367
+ tool(name, def) {
29368
+ tools.set(name, {
29369
+ name,
29370
+ description: def.description,
29371
+ input: external_exports.toJSONSchema(def.args, { target: "draft-2020-12", io: "input" }),
29372
+ options: { codemode: false },
29373
+ // a ctx_* tool is listed to the model by name, not behind the host's code tool
29374
+ async execute(input, context) {
29375
+ return { content: await def.execute(input, { sessionID: context.sessionID, abort: context.signal }) };
29376
+ }
29377
+ });
29378
+ }
29379
+ });
29380
+ await ctx.tool.transform((editor) => {
29381
+ for (const t of tools.values()) editor.add(t);
29382
+ });
29383
+ await ctx.tool.hook("execute.before", async (i) => {
29384
+ await hooks["tool.execute.before"]({ tool: i.tool, sessionID: i.sessionID, callID: i.id }, { args: i.input });
29385
+ });
29386
+ await ctx.tool.hook("execute.after", async (i) => {
29387
+ const failed2 = i.status === "error";
29388
+ const output = failed2 ? i.error?.message ?? "" : oc2Text(i.result?.content);
29389
+ await hooks["tool.execute.after"]({ tool: i.tool, sessionID: i.sessionID, callID: i.id, args: i.input }, { output, isError: failed2 });
29390
+ });
29391
+ await ctx.session.hook("prompt", async (p) => {
29392
+ await hooks["chat.message"]({ sessionID: p.sessionID }, { parts: [{ type: "text", text: p.prompt?.text ?? "" }] });
29393
+ });
29394
+ await ctx.session.hook("context", async (c) => {
29395
+ const system = [];
29396
+ await hooks["experimental.chat.system.transform"]({ sessionID: c.sessionID }, { system });
29397
+ for (const text of system) if (!c.system.some((p) => p.text === text)) c.system.push({ type: "text", text });
29398
+ });
29399
+ await ctx.session.hook("compaction", async (c) => {
29400
+ await hooks["experimental.session.compacting"]({ sessionID: c.sessionID });
29401
+ });
29402
+ const stop = new AbortController();
29403
+ void (async () => {
29404
+ for await (const e of ctx.event.subscribe({ types: ["session.step.ended"], signal: stop.signal })) {
29405
+ await hooks.event({ event: { type: "message.part.updated", properties: { part: { type: "step-finish", sessionID: e.data?.sessionID } } } });
29406
+ }
29407
+ })().catch(() => {
29408
+ });
29409
+ return () => stop.abort();
29410
+ }
29327
29411
  async function server(input, _options) {
29328
29412
  const tool = {};
29329
- const hooks = await setup({
29413
+ const hooks = await setupLegacy({
29330
29414
  directory: input.directory,
29331
29415
  tool(name, def) {
29332
29416
  const shape = def.args.shape;
@@ -29340,5 +29424,6 @@ export {
29340
29424
  opencode_default as default,
29341
29425
  id,
29342
29426
  server,
29343
- setup
29427
+ setup,
29428
+ setupLegacy
29344
29429
  };
package/package.json CHANGED
@@ -9,6 +9,10 @@
9
9
  "engines": {
10
10
  "node": ">=24.15.0"
11
11
  },
12
+ "exports": {
13
+ ".": "./opencode/plugin.js",
14
+ "./server": "./opencode/plugin.js"
15
+ },
12
16
  "files": [
13
17
  ".claude-plugin",
14
18
  ".codex-plugin",
@@ -63,5 +67,5 @@
63
67
  "url": "git+https://github.com/mksglu/context-mode.git"
64
68
  },
65
69
  "type": "module",
66
- "version": "2.0.0-next.18"
70
+ "version": "2.0.0-next.21"
67
71
  }
package/pi/index.js CHANGED
@@ -11508,7 +11508,7 @@ var DECISION_OPTION_KEY = "option";
11508
11508
  var DECISION_LABEL_JOIN = " | ";
11509
11509
  var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
11510
11510
  var RELEASE_BRANCH = "release";
11511
- var BUNDLE_SHA = true ? "ea4179fd9a2c4c55eadc9184a295a7f974df90bd" : void 0;
11511
+ var BUNDLE_SHA = true ? "fa9bffea6220e2da2a37f2568e1fa96671f5b7c7" : void 0;
11512
11512
  var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
11513
11513
  var DOCTOR_LABEL = Object.freeze({
11514
11514
  D1: "version",
@@ -12573,7 +12573,8 @@ var OPENCODE_EVENTS = {
12573
12573
  "step-finish": "Usage"
12574
12574
  };
12575
12575
  var SHELL_TOOL_NAME = "bash";
12576
- var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", [SHELL_TOOL_NAME]: "Bash" };
12576
+ var SHELL_TOOL_NAMES4 = Object.freeze([SHELL_TOOL_NAME, "shell"]);
12577
+ var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", ...Object.fromEntries(SHELL_TOOL_NAMES4.map((n) => [n, "Bash"])) };
12577
12578
  function toCanonicalTool(name) {
12578
12579
  if (name.startsWith(OWN_WIRE_PREFIX)) return name.slice(OWN_WIRE_PREFIX.length);
12579
12580
  return Object.prototype.hasOwnProperty.call(NATIVE_TO_CANONICAL7, name) ? NATIVE_TO_CANONICAL7[name] : name;
@@ -19971,8 +19972,8 @@ function decisionQueries(input) {
19971
19972
  function pageSize(input) {
19972
19973
  return input.source === DECISION_SOURCE ? SEARCH_PAGE_SIZE : SEARCH_REPLY_HITS;
19973
19974
  }
19974
- function page(db, q, input, scope, ctx, offset) {
19975
- const size = pageSize(input);
19975
+ function page(db, q, input, scope, ctx, offset, extra = 0) {
19976
+ const size = pageSize(input) + extra;
19976
19977
  if (input.source === DECISION_SOURCE && q.trim() === "") {
19977
19978
  return ftsListSource(db, DECISION_SOURCE, scope, ctx.project, ctx.sessionId, size, offset);
19978
19979
  }
@@ -20021,51 +20022,76 @@ function jsonRowsReply(db, input, scope, ctx) {
20021
20022
  }
20022
20023
  return shown2 === 0 && !more ? null : reply(out.join("\n"), false);
20023
20024
  }
20025
+ var SMALL_SOURCE_SHARE = 0.5;
20026
+ var SOURCE_BYTES = "SELECT id, bytes_total FROM sources WHERE id IN (SELECT value FROM json_each(:ids))";
20027
+ function sourceBytes(db, ids) {
20028
+ const out = /* @__PURE__ */ new Map();
20029
+ if (ids.length === 0) return out;
20030
+ for (const r of db.raw.prepare(SOURCE_BYTES).all({ ids: JSON.stringify(ids) })) {
20031
+ out.set(Number(r.id), Number(r.bytes_total));
20032
+ }
20033
+ return out;
20034
+ }
20024
20035
  function relevance(db, given, scope, ctx) {
20025
20036
  const input = { ...given, queries: decisionQueries(given) };
20026
20037
  const offset = input.offset ?? 0;
20027
20038
  const size = pageSize(input);
20028
- const seen = /* @__PURE__ */ new Set();
20029
- const entries = [];
20030
- const full = /* @__PURE__ */ new Set();
20031
- input.queries.forEach((q, qi) => {
20032
- const hits = page(db, q, input, scope, ctx, offset);
20033
- if (hits.length === size) full.add(qi);
20034
- hits.forEach((hit, i) => {
20035
- const key = hit.sourceId + ":" + hit.ord;
20036
- if (seen.has(key)) return;
20037
- seen.add(key);
20038
- entries.push({ qi, pos: offset + i, hit });
20039
- });
20040
- });
20041
- if (entries.length === 0) return zeroHits(db, scope, ctx, input.source);
20039
+ const fetched = input.queries.map((q) => page(db, q, input, scope, ctx, offset, 1));
20040
+ const pages = fetched.map((h) => h.slice(0, size));
20041
+ if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source);
20042
20042
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, input.source, "offset:" + pos);
20043
20043
  let reserve = 0;
20044
20044
  for (const q of input.queries) reserve += bytes2(moreLine3(q, offset + size)) + 1;
20045
+ const total = input.source === DECISION_SOURCE ? /* @__PURE__ */ new Map() : sourceBytes(db, [...new Set(pages.flat().map((h) => h.sourceId))]);
20046
+ const spent = /* @__PURE__ */ new Map();
20045
20047
  const lines = [];
20046
20048
  const more = [];
20049
+ const shown2 = /* @__PURE__ */ new Set();
20047
20050
  let used = 0;
20048
20051
  let stopped = false;
20049
20052
  input.queries.forEach((q, qi) => {
20050
- const mine = entries.filter((e) => e.qi === qi);
20053
+ const hits = pages[qi];
20051
20054
  if (stopped) {
20052
- more.push(moreLine3(q, mine.length > 0 ? mine[0].pos : offset));
20055
+ more.push(moreLine3(q, offset));
20053
20056
  return;
20054
20057
  }
20055
20058
  const head = "Query " + JSON.stringify(q) + ":";
20056
20059
  lines.push(head);
20057
20060
  used += bytes2(head) + 1;
20058
- for (const e of mine) {
20059
- const line = e.hit.label + " #" + e.hit.ord + ": " + e.hit.snippet;
20061
+ const again = /* @__PURE__ */ new Map();
20062
+ const mine = /* @__PURE__ */ new Set();
20063
+ let cut = -1;
20064
+ for (let i = 0; i < hits.length; i++) {
20065
+ const hit = hits[i];
20066
+ const key = hit.sourceId + ":" + hit.ord;
20067
+ if (shown2.has(key)) {
20068
+ again.set(hit.label, [...again.get(hit.label) ?? [], hit.ord]);
20069
+ continue;
20070
+ }
20071
+ const line = hit.label + " #" + hit.ord + ": " + hit.snippet;
20060
20072
  if (used + bytes2(line) + 1 + reserve > OUTPUT_CAP) {
20061
- more.push(moreLine3(q, e.pos));
20073
+ cut = offset + i;
20062
20074
  stopped = true;
20063
20075
  break;
20064
20076
  }
20077
+ const room = total.get(hit.sourceId);
20078
+ if (room !== void 0 && mine.has(hit.sourceId) && (spent.get(hit.sourceId) ?? 0) + bytes2(line) + 1 > room * SMALL_SOURCE_SHARE * (qi + 1) / input.queries.length) {
20079
+ cut = offset + i;
20080
+ break;
20081
+ }
20082
+ lines.push(line);
20083
+ shown2.add(key);
20084
+ used += bytes2(line) + 1;
20085
+ spent.set(hit.sourceId, (spent.get(hit.sourceId) ?? 0) + bytes2(line) + 1);
20086
+ mine.add(hit.sourceId);
20087
+ }
20088
+ if (again.size > 0) {
20089
+ const line = "Same as above: " + [...again].map(([label, ords]) => label + " (chunk " + ords.join(",") + ")").join("; ");
20065
20090
  lines.push(line);
20066
20091
  used += bytes2(line) + 1;
20067
20092
  }
20068
- if (!stopped && full.has(qi)) more.push(moreLine3(q, offset + size));
20093
+ if (cut >= 0) more.push(moreLine3(q, cut));
20094
+ else if (fetched[qi].length > size) more.push(moreLine3(q, offset + size));
20069
20095
  });
20070
20096
  return reply(lines.concat(more).join("\n"), false);
20071
20097
  }
@@ -22833,7 +22859,7 @@ function ownMcpValue(agent, node3, server) {
22833
22859
  return { ...mcp.fields, [MCP_COMMAND_FIELD]: node3, [MCP_ARGS_FIELD]: [server, AGENT_ARG, agent, ...tail] };
22834
22860
  }
22835
22861
  function opencodePluginFile() {
22836
- return join8(dirname4(process.argv[1] ?? ""), ...OPENCODE_PLUGIN_PATH);
22862
+ return dirname4(process.argv[1] ?? "");
22837
22863
  }
22838
22864
  function listTarget(key, own4, v1, alt = []) {
22839
22865
  return {
@@ -23664,7 +23690,8 @@ function checkShell(env) {
23664
23690
  // src/core/ops/doctor-hook.ts
23665
23691
  import { existsSync as existsSync9, readFileSync as readFileSync9 } from "node:fs";
23666
23692
  import path4 from "node:path";
23667
- var D5_SAMPLES = 3;
23693
+ var D5_MIN_PAIRS = 3;
23694
+ var D5_MAX_PAIRS = 9;
23668
23695
  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 };
23669
23696
  function isObject5(v) {
23670
23697
  return typeof v === "object" && v !== null && !Array.isArray(v);
@@ -23708,7 +23735,9 @@ async function checkHookRoundTrip(i) {
23708
23735
  const fixture = loadFixture(i.bundleDir, i.agent);
23709
23736
  if (fixture === null) return row("UNKNOWN", D5_NO_FIXTURE_DETAIL(i.agent));
23710
23737
  const now = i.now ?? (() => performance.now());
23711
- const samples = Math.max(1, i.samples ?? D5_SAMPLES);
23738
+ const fixed = i.samples === void 0 ? null : Math.max(1, i.samples);
23739
+ const maxPairs = fixed ?? D5_MAX_PAIRS;
23740
+ const minPairs = fixed ?? D5_MIN_PAIRS;
23712
23741
  const bareMs = i.bareMs ?? (async () => {
23713
23742
  const t = performance.now();
23714
23743
  await runProcess([process.execPath, "-e", ""], { cwd: i.bundleDir, env: i.env, signal: i.signal, timeoutMs: HOOK_TIMEOUT_MS });
@@ -23716,7 +23745,8 @@ async function checkHookRoundTrip(i) {
23716
23745
  });
23717
23746
  const hookWalls = [];
23718
23747
  const bareWalls = [];
23719
- for (let n = 0; n < samples; n++) {
23748
+ let ms = 0;
23749
+ for (let n = 1; n <= maxPairs; n++) {
23720
23750
  const start = now();
23721
23751
  const r = await runProcess([process.execPath, path4.join(i.bundleDir, HOOK_ENTRY_FILE), AGENT_ARG, i.agent], {
23722
23752
  cwd: i.bundleDir,
@@ -23728,8 +23758,9 @@ async function checkHookRoundTrip(i) {
23728
23758
  hookWalls.push(now() - start);
23729
23759
  if (!isNativeShape(r.stdout.toString("utf8"), r.exitCode, r.killed, fixture.event)) return row("FAIL", D5_SHAPE_DETAIL, D5_FIX);
23730
23760
  bareWalls.push(await bareMs());
23761
+ ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
23762
+ if (n >= minPairs && ms <= HOOK_P95_MS) break;
23731
23763
  }
23732
- const ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
23733
23764
  if (ms > HOOK_P95_MS) return row("FAIL", D5_SLOW_DETAIL(ms), D5_FIX);
23734
23765
  return row("PASS", DOCTOR_PASS_DETAIL);
23735
23766
  }
@@ -12,5 +12,5 @@
12
12
  }
13
13
  },
14
14
  "name": "context-mode",
15
- "version": "2.0.0-next.18"
15
+ "version": "2.0.0-next.21"
16
16
  }
package/run.mjs CHANGED
@@ -1040,7 +1040,8 @@ var OPENCODE_EVENTS = {
1040
1040
  "step-finish": "Usage"
1041
1041
  };
1042
1042
  var SHELL_TOOL_NAME = "bash";
1043
- var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", [SHELL_TOOL_NAME]: "Bash" };
1043
+ var SHELL_TOOL_NAMES4 = Object.freeze([SHELL_TOOL_NAME, "shell"]);
1044
+ var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", ...Object.fromEntries(SHELL_TOOL_NAMES4.map((n) => [n, "Bash"])) };
1044
1045
  function toCanonicalTool(name) {
1045
1046
  if (name.startsWith(OWN_WIRE_PREFIX)) return name.slice(OWN_WIRE_PREFIX.length);
1046
1047
  return Object.prototype.hasOwnProperty.call(NATIVE_TO_CANONICAL7, name) ? NATIVE_TO_CANONICAL7[name] : name;
package/search.mjs CHANGED
@@ -1040,7 +1040,8 @@ var OPENCODE_EVENTS = {
1040
1040
  "step-finish": "Usage"
1041
1041
  };
1042
1042
  var SHELL_TOOL_NAME = "bash";
1043
- var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", [SHELL_TOOL_NAME]: "Bash" };
1043
+ var SHELL_TOOL_NAMES4 = Object.freeze([SHELL_TOOL_NAME, "shell"]);
1044
+ var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", ...Object.fromEntries(SHELL_TOOL_NAMES4.map((n) => [n, "Bash"])) };
1044
1045
  function toCanonicalTool(name) {
1045
1046
  if (name.startsWith(OWN_WIRE_PREFIX)) return name.slice(OWN_WIRE_PREFIX.length);
1046
1047
  return Object.prototype.hasOwnProperty.call(NATIVE_TO_CANONICAL7, name) ? NATIVE_TO_CANONICAL7[name] : name;
package/server.mjs CHANGED
@@ -28343,7 +28343,7 @@ var DECISION_SOURCE = "decision";
28343
28343
  var DECISION_USER_LABEL = "decision (user)";
28344
28344
  var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
28345
28345
  var RELEASE_BRANCH = "release";
28346
- var BUNDLE_SHA = true ? "ea4179fd9a2c4c55eadc9184a295a7f974df90bd" : void 0;
28346
+ var BUNDLE_SHA = true ? "fa9bffea6220e2da2a37f2568e1fa96671f5b7c7" : void 0;
28347
28347
  var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
28348
28348
  var DOCTOR_LABEL = Object.freeze({
28349
28349
  D1: "version",
@@ -43914,8 +43914,8 @@ function decisionQueries(input) {
43914
43914
  function pageSize(input) {
43915
43915
  return input.source === DECISION_SOURCE ? SEARCH_PAGE_SIZE : SEARCH_REPLY_HITS;
43916
43916
  }
43917
- function page(db, q, input, scope, ctx, offset) {
43918
- const size = pageSize(input);
43917
+ function page(db, q, input, scope, ctx, offset, extra = 0) {
43918
+ const size = pageSize(input) + extra;
43919
43919
  if (input.source === DECISION_SOURCE && q.trim() === "") {
43920
43920
  return ftsListSource(db, DECISION_SOURCE, scope, ctx.project, ctx.sessionId, size, offset);
43921
43921
  }
@@ -43964,51 +43964,76 @@ function jsonRowsReply(db, input, scope, ctx) {
43964
43964
  }
43965
43965
  return shown2 === 0 && !more ? null : reply(out.join("\n"), false);
43966
43966
  }
43967
+ var SMALL_SOURCE_SHARE = 0.5;
43968
+ var SOURCE_BYTES = "SELECT id, bytes_total FROM sources WHERE id IN (SELECT value FROM json_each(:ids))";
43969
+ function sourceBytes(db, ids) {
43970
+ const out = /* @__PURE__ */ new Map();
43971
+ if (ids.length === 0) return out;
43972
+ for (const r of db.raw.prepare(SOURCE_BYTES).all({ ids: JSON.stringify(ids) })) {
43973
+ out.set(Number(r.id), Number(r.bytes_total));
43974
+ }
43975
+ return out;
43976
+ }
43967
43977
  function relevance(db, given, scope, ctx) {
43968
43978
  const input = { ...given, queries: decisionQueries(given) };
43969
43979
  const offset = input.offset ?? 0;
43970
43980
  const size = pageSize(input);
43971
- const seen = /* @__PURE__ */ new Set();
43972
- const entries = [];
43973
- const full = /* @__PURE__ */ new Set();
43974
- input.queries.forEach((q, qi) => {
43975
- const hits = page(db, q, input, scope, ctx, offset);
43976
- if (hits.length === size) full.add(qi);
43977
- hits.forEach((hit, i) => {
43978
- const key = hit.sourceId + ":" + hit.ord;
43979
- if (seen.has(key)) return;
43980
- seen.add(key);
43981
- entries.push({ qi, pos: offset + i, hit });
43982
- });
43983
- });
43984
- if (entries.length === 0) return zeroHits(db, scope, ctx, input.source);
43981
+ const fetched = input.queries.map((q) => page(db, q, input, scope, ctx, offset, 1));
43982
+ const pages = fetched.map((h) => h.slice(0, size));
43983
+ if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source);
43985
43984
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, input.source, "offset:" + pos);
43986
43985
  let reserve = 0;
43987
43986
  for (const q of input.queries) reserve += bytes2(moreLine3(q, offset + size)) + 1;
43987
+ const total = input.source === DECISION_SOURCE ? /* @__PURE__ */ new Map() : sourceBytes(db, [...new Set(pages.flat().map((h) => h.sourceId))]);
43988
+ const spent = /* @__PURE__ */ new Map();
43988
43989
  const lines = [];
43989
43990
  const more = [];
43991
+ const shown2 = /* @__PURE__ */ new Set();
43990
43992
  let used = 0;
43991
43993
  let stopped = false;
43992
43994
  input.queries.forEach((q, qi) => {
43993
- const mine = entries.filter((e) => e.qi === qi);
43995
+ const hits = pages[qi];
43994
43996
  if (stopped) {
43995
- more.push(moreLine3(q, mine.length > 0 ? mine[0].pos : offset));
43997
+ more.push(moreLine3(q, offset));
43996
43998
  return;
43997
43999
  }
43998
44000
  const head = "Query " + JSON.stringify(q) + ":";
43999
44001
  lines.push(head);
44000
44002
  used += bytes2(head) + 1;
44001
- for (const e of mine) {
44002
- const line = e.hit.label + " #" + e.hit.ord + ": " + e.hit.snippet;
44003
+ const again = /* @__PURE__ */ new Map();
44004
+ const mine = /* @__PURE__ */ new Set();
44005
+ let cut = -1;
44006
+ for (let i = 0; i < hits.length; i++) {
44007
+ const hit = hits[i];
44008
+ const key = hit.sourceId + ":" + hit.ord;
44009
+ if (shown2.has(key)) {
44010
+ again.set(hit.label, [...again.get(hit.label) ?? [], hit.ord]);
44011
+ continue;
44012
+ }
44013
+ const line = hit.label + " #" + hit.ord + ": " + hit.snippet;
44003
44014
  if (used + bytes2(line) + 1 + reserve > OUTPUT_CAP) {
44004
- more.push(moreLine3(q, e.pos));
44015
+ cut = offset + i;
44005
44016
  stopped = true;
44006
44017
  break;
44007
44018
  }
44019
+ const room = total.get(hit.sourceId);
44020
+ if (room !== void 0 && mine.has(hit.sourceId) && (spent.get(hit.sourceId) ?? 0) + bytes2(line) + 1 > room * SMALL_SOURCE_SHARE * (qi + 1) / input.queries.length) {
44021
+ cut = offset + i;
44022
+ break;
44023
+ }
44024
+ lines.push(line);
44025
+ shown2.add(key);
44026
+ used += bytes2(line) + 1;
44027
+ spent.set(hit.sourceId, (spent.get(hit.sourceId) ?? 0) + bytes2(line) + 1);
44028
+ mine.add(hit.sourceId);
44029
+ }
44030
+ if (again.size > 0) {
44031
+ const line = "Same as above: " + [...again].map(([label, ords]) => label + " (chunk " + ords.join(",") + ")").join("; ");
44008
44032
  lines.push(line);
44009
44033
  used += bytes2(line) + 1;
44010
44034
  }
44011
- if (!stopped && full.has(qi)) more.push(moreLine3(q, offset + size));
44035
+ if (cut >= 0) more.push(moreLine3(q, cut));
44036
+ else if (fetched[qi].length > size) more.push(moreLine3(q, offset + size));
44012
44037
  });
44013
44038
  return reply(lines.concat(more).join("\n"), false);
44014
44039
  }
@@ -44917,7 +44942,8 @@ var OPENCODE_EVENTS = {
44917
44942
  "step-finish": "Usage"
44918
44943
  };
44919
44944
  var SHELL_TOOL_NAME = "bash";
44920
- var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", [SHELL_TOOL_NAME]: "Bash" };
44945
+ var SHELL_TOOL_NAMES4 = Object.freeze([SHELL_TOOL_NAME, "shell"]);
44946
+ var NATIVE_TO_CANONICAL7 = { webfetch: "WebFetch", ...Object.fromEntries(SHELL_TOOL_NAMES4.map((n) => [n, "Bash"])) };
44921
44947
  function toCanonicalTool(name) {
44922
44948
  if (name.startsWith(OWN_WIRE_PREFIX)) return name.slice(OWN_WIRE_PREFIX.length);
44923
44949
  return Object.prototype.hasOwnProperty.call(NATIVE_TO_CANONICAL7, name) ? NATIVE_TO_CANONICAL7[name] : name;
@@ -47684,7 +47710,7 @@ function ownMcpValue(agent, node3, server) {
47684
47710
  return { ...mcp.fields, [MCP_COMMAND_FIELD]: node3, [MCP_ARGS_FIELD]: [server, AGENT_ARG, agent, ...tail] };
47685
47711
  }
47686
47712
  function opencodePluginFile() {
47687
- return join6(dirname3(process.argv[1] ?? ""), ...OPENCODE_PLUGIN_PATH);
47713
+ return dirname3(process.argv[1] ?? "");
47688
47714
  }
47689
47715
  function listTarget(key, own4, v1, alt = []) {
47690
47716
  return {
@@ -48515,7 +48541,8 @@ function checkShell(env) {
48515
48541
  // src/core/ops/doctor-hook.ts
48516
48542
  import { existsSync as existsSync10, readFileSync as readFileSync7 } from "node:fs";
48517
48543
  import path4 from "node:path";
48518
- var D5_SAMPLES = 3;
48544
+ var D5_MIN_PAIRS = 3;
48545
+ var D5_MAX_PAIRS = 9;
48519
48546
  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 };
48520
48547
  function isObject5(v) {
48521
48548
  return typeof v === "object" && v !== null && !Array.isArray(v);
@@ -48559,7 +48586,9 @@ async function checkHookRoundTrip(i) {
48559
48586
  const fixture = loadFixture(i.bundleDir, i.agent);
48560
48587
  if (fixture === null) return row("UNKNOWN", D5_NO_FIXTURE_DETAIL(i.agent));
48561
48588
  const now = i.now ?? (() => performance.now());
48562
- const samples = Math.max(1, i.samples ?? D5_SAMPLES);
48589
+ const fixed = i.samples === void 0 ? null : Math.max(1, i.samples);
48590
+ const maxPairs = fixed ?? D5_MAX_PAIRS;
48591
+ const minPairs = fixed ?? D5_MIN_PAIRS;
48563
48592
  const bareMs = i.bareMs ?? (async () => {
48564
48593
  const t = performance.now();
48565
48594
  await runProcess([process.execPath, "-e", ""], { cwd: i.bundleDir, env: i.env, signal: i.signal, timeoutMs: HOOK_TIMEOUT_MS });
@@ -48567,7 +48596,8 @@ async function checkHookRoundTrip(i) {
48567
48596
  });
48568
48597
  const hookWalls = [];
48569
48598
  const bareWalls = [];
48570
- for (let n = 0; n < samples; n++) {
48599
+ let ms = 0;
48600
+ for (let n = 1; n <= maxPairs; n++) {
48571
48601
  const start = now();
48572
48602
  const r = await runProcess([process.execPath, path4.join(i.bundleDir, HOOK_ENTRY_FILE), AGENT_ARG, i.agent], {
48573
48603
  cwd: i.bundleDir,
@@ -48579,8 +48609,9 @@ async function checkHookRoundTrip(i) {
48579
48609
  hookWalls.push(now() - start);
48580
48610
  if (!isNativeShape(r.stdout.toString("utf8"), r.exitCode, r.killed, fixture.event)) return row("FAIL", D5_SHAPE_DETAIL, D5_FIX);
48581
48611
  bareWalls.push(await bareMs());
48612
+ ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
48613
+ if (n >= minPairs && ms <= HOOK_P95_MS) break;
48582
48614
  }
48583
- const ms = Math.max(0, Math.round(Math.min(...hookWalls) - Math.min(...bareWalls)));
48584
48615
  if (ms > HOOK_P95_MS) return row("FAIL", D5_SLOW_DETAIL(ms), D5_FIX);
48585
48616
  return row("PASS", DOCTOR_PASS_DETAIL);
48586
48617
  }