context-mode 2.0.0-next.20 → 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.
@@ -9,7 +9,7 @@
9
9
  "name": "context-mode",
10
10
  "skills": "./claude-code/skills",
11
11
  "source": "./",
12
- "version": "2.0.0-next.20"
12
+ "version": "2.0.0-next.21"
13
13
  }
14
14
  ]
15
15
  }
@@ -13,5 +13,5 @@
13
13
  },
14
14
  "name": "context-mode",
15
15
  "skills": "./claude-code/skills",
16
- "version": "2.0.0-next.20"
16
+ "version": "2.0.0-next.21"
17
17
  }
@@ -8,5 +8,5 @@
8
8
  "mcpServers": "./.codex-plugin/mcp.json",
9
9
  "name": "context-mode",
10
10
  "skills": "./codex/skills/",
11
- "version": "2.0.0-next.20"
11
+ "version": "2.0.0-next.21"
12
12
  }
package/SHA256SUMS CHANGED
@@ -1,18 +1,18 @@
1
- 1891466a144df85c1f933ef2177ceaa5e7d21d0f27ebce77a0276dffd5490180 .claude-plugin/marketplace.json
2
- ce0bd4a4136a2ed741048b9b409ec6d7e2e4a6212f6d274a20300b939861b09b .claude-plugin/plugin.json
1
+ 170d72bc88b1f2568f8fd4c7d67908029522f7ff8bbb190433307cdbd83a2b4a .claude-plugin/marketplace.json
2
+ 0139fba3ed537d7a809009224217d3f92069643a524dd37b753e38c32d21ef25 .claude-plugin/plugin.json
3
3
  187503e1455560d4ab5a72e3b8cd2f274abd4ccc5faa60dd66c37e44bb5f10c2 .codex-plugin/hooks.json
4
4
  4edea851775bc3f0418f8d9cf6ead74f2767c3a9e3b8c05749eacb78ebb3b896 .codex-plugin/mcp.json
5
- 5462e4e6f9cbf7e568ff3b97db119ed872b20fd7a9c4e827925762689b1d9ca2 .codex-plugin/plugin.json
5
+ 75afc997fab549f8449953ae6fccb5499a6d0c0242ceec505d54968e507fecb6 .codex-plugin/plugin.json
6
6
  1773e40a89384dd1af9023ba533da1908f3e2435dbaaa7cc941edf0514148313 claude-code/skills/ctx-doctor/SKILL.md
7
7
  34fffb9c76d69c551f07036230bec0fbaca7a69177a5a47f89ddb9a9990a1fa2 claude-code/skills/ctx-stats/SKILL.md
8
8
  d8f835d2f2b3eb70c5902f8707e7c5857977cec9e20ace76c499378a04e178e0 claude-code/skills/ctx-upgrade/SKILL.md
9
- f8be0a11f3ffb0fff1555e12824652f8e9617fcd8e48f9ced88166f7f65ed006 cli.mjs
9
+ a69c0fac9c978267f9e8676a6fe9c754a72a3e898975be5e78d90698c7142961 cli.mjs
10
10
  83c297020ed0c592fc68c9c482546a6999d41df5d06d8a329498f10edb1bd38e codex/skills/ctx-doctor/SKILL.md
11
11
  adc2c374f699d3698110920bb48411965de09fc6e7232ce59c913f6d58c0d738 codex/skills/ctx-stats/SKILL.md
12
12
  5c12cdd19a5e3ed7d9ba3a47b24d03864a28d36cf7006876284ee07cd4221ec4 codex/skills/ctx-upgrade/SKILL.md
13
- fd06e1e104d35319418e62e3c16b81027e022f7f59666c25aad3b2db4090b133 copilot/plugin.json
13
+ 55b1e5099b525366b8869a400eca087bc61d10fc5444f7f9404e818b44dbc49c copilot/plugin.json
14
14
  e3bc6cae0bd51b806d81631879711bbc48c679fc929b2642ed4634438364167d fixtures/hook-roundtrip.json
15
- fa093979cbd93b0716b9be153f9f581abd74d2c5a4963a10b7aa63dc386aaf4b gemini-extension.json
15
+ 53c5491ef8519eb1527a410009d0db0bed868a4c832f2f5b900a3f85a6aebd56 gemini-extension.json
16
16
  b58303ab9c463e36c85ad20ecf375080c049d82f7dd1e2dd5fbcff4b8830a72c hook.mjs
17
17
  1f8d8650d2b38737522cab6b875878e8baf84a74acf98cd924c7af22852d873f hooks/claude-code.json
18
18
  2524a0eb52683b65c65943c5097db513e9845ba14e84324b39e9a8ea2bb43193 hooks/codex.json
@@ -22,16 +22,16 @@ f336d1db67de49e641687bbd41ba1a9424e31dfe2377b2284bf44358288ebd8b hooks/kiro.jso
22
22
  017c9cc3997dd53231b8548b1a1609fddc51177af53501565a4f334602039860 hooks/qwen.json
23
23
  33f69267645338d0dc392ce551dcd7c69a0b508113469d6ea637d3f34239ef1d hooks/vscode.json
24
24
  085630bccf2073ac9a62be24ef91dfc47d106092dc82c822db70890b7d3ae1c0 hooks/zcode.json
25
- 0587b8de9dcd7c441f8a93c976e72637a6f2a33f5ded4398175df2c2fa3dc20c opencode/plugin.js
26
- 9ac18c9a8856bc75cb7b9a5a67af2650f19204b323cce625dca1ca3d2adfeeab package.json
27
- 8a04b0b071744eb616c24d509a60fe1c734a6f9ca611c4c2c2466cf61e543274 pi/index.js
25
+ 67c46292af5625a4da9ce3b357d26564bcafeac2575238defa55d4f4547e223f opencode/plugin.js
26
+ f70d5caf68c1120422c88db6d94bd988bedc2fb192c2cecaf71c91528efe4d7a package.json
27
+ 0ec19c55e2aaea9d42117fffe9044f0a5e388ccc3b49a04da678cc1d84945fd8 pi/index.js
28
28
  8712881622c9905aa4836af50d903042d34a91506264e459bc7839331f6f0192 pi/skills/ctx-doctor/SKILL.md
29
29
  8d6e8e2a82eb079bddc60727f52d9f9a6e0bf9e15391357fed82ee76ab5fe76c pi/skills/ctx-stats/SKILL.md
30
30
  db6f9c006bc864a7271f1eeffb1e316ebb537a3414f1c22f8b9de0a896e8e3c5 pi/skills/ctx-upgrade/SKILL.md
31
- 4499d3a8d8ffab03a116a047fb69ad9bc0cd3f104d0d1e24c654878b22fa41ef qwen-extension.json
31
+ b5d6f8633de23fab124c0b6bce74656adb25b1b966d32d513fe5543250ed4b0b qwen-extension.json
32
32
  28a957921c1dd24aef587fc0f8c6835de1cbbe49eff18104c5d1dd70c0297aad run.mjs
33
33
  28a957921c1dd24aef587fc0f8c6835de1cbbe49eff18104c5d1dd70c0297aad search.mjs
34
- 62f166931982ed258bdba4d9ec60fde0296ea39e8033171e42b169cda9acca0c server.mjs
34
+ 0a71c81e65e586c7336baa0705848bb98528957064fbf1cbabb429ed10d2c52d server.mjs
35
35
  e5503a22a6238901484d123efc11d2d933aa9a54c6ac2018812cfa65ec4e53e3 skills/ctx-doctor/SKILL.md
36
36
  1044587a1906c69f2467c089a25e19819a290ea5728389bd1f086c6dfc0d0c51 skills/ctx-stats/SKILL.md
37
37
  c1cac75b7506ee0d06089ce611ac6dc9314cf12435da318ce88ad50e2c5debbf skills/ctx-upgrade/SKILL.md
package/cli.mjs CHANGED
@@ -8652,11 +8652,16 @@ function isStaleOwnPlugin(e, entry2) {
8652
8652
  if (n3 >= 3 && seg[n3 - 1] === "plugin.js" && seg[n3 - 2] === "opencode" && seg[n3 - 3] === "context-mode") return true;
8653
8653
  return n3 >= 2 && seg[n3 - 1] === "context-mode" && seg[n3 - 2] === "node_modules";
8654
8654
  }
8655
- function wantedPluginList(list2, entry2) {
8656
- const native = list2.some(isNativeSpec);
8655
+ function wantedPluginList(list2, entry2, nativeElsewhere = false) {
8656
+ const native = nativeElsewhere || list2.some(isNativeSpec);
8657
8657
  const kept = list2.filter((e) => !(native && e === entry2) && !isStaleOwnPlugin(e, entry2));
8658
8658
  return native || kept.includes(entry2) ? kept : [...kept, entry2];
8659
8659
  }
8660
+ var OC2_NATIVE_KEY = "plugins";
8661
+ function nativePluginsKey(doc) {
8662
+ const v = isObj5(doc) ? doc[OC2_NATIVE_KEY] : void 0;
8663
+ return Array.isArray(v) && v.some(isNativeSpec);
8664
+ }
8660
8665
  function pluginListIssue(h2) {
8661
8666
  const text2 = readOr(h2.file);
8662
8667
  if (text2 === void 0) return void 0;
@@ -8668,7 +8673,7 @@ function pluginListIssue(h2) {
8668
8673
  }
8669
8674
  const cur = isObj5(doc) ? doc[h2.key[0]] : void 0;
8670
8675
  const list2 = Array.isArray(cur) ? cur : [];
8671
- if (list2.some(isNativeSpec) && list2.includes(h2.entry)) return `${h2.file}: context-mode loads twice (native entry and ${h2.entry}), every hook fires twice; run: context-mode setup`;
8676
+ if ((list2.some(isNativeSpec) || nativePluginsKey(doc)) && list2.includes(h2.entry)) return `${h2.file}: context-mode loads twice (native entry and ${h2.entry}), every hook fires twice; run: context-mode setup`;
8672
8677
  const stale = list2.find((e) => isStaleOwnPlugin(e, h2.entry));
8673
8678
  if (stale !== void 0) return `${h2.file}: plugin file missing, so OpenCode never loads the hooks (${String(stale)}); run: context-mode setup`;
8674
8679
  return void 0;
@@ -8695,7 +8700,8 @@ function planHookInstall(row, edits, env) {
8695
8700
  planJsonKeys(row, edits, h2.file, fileFormat(row, h2.file), (doc) => {
8696
8701
  const cur = doc[h2.key[0]];
8697
8702
  const list2 = Array.isArray(cur) ? cur : [];
8698
- const want = wantedPluginList(list2, h2.entry);
8703
+ const want = wantedPluginList(list2, h2.entry, nativePluginsKey(doc));
8704
+ if (cur === void 0 && want.length === 0) return [];
8699
8705
  return [[h2.key, want.length === list2.length && want.every((e, i2) => e === list2[i2]) ? list2 : want]];
8700
8706
  });
8701
8707
  }
@@ -8758,12 +8764,14 @@ function statusOf(row, text2) {
8758
8764
  }
8759
8765
  return void 0;
8760
8766
  }
8761
- function opencodeListArgs(bin, env) {
8767
+ function opencodeMajor(bin, env) {
8762
8768
  const r2 = runBin(bin, ["--version"], env);
8763
8769
  const text2 = `${r2.stdout}${r2.stderr}`;
8764
8770
  const digits = text2.split("").filter((c3) => c3 >= "0" && c3 <= "9" || c3 === ".").join("");
8765
- const major = Number(digits.split(".")[0]);
8766
- return major >= 2 ? ["mcp", "list", "--standalone"] : ["mcp", "list", "--pure"];
8771
+ return Number(digits.split(".")[0]);
8772
+ }
8773
+ function opencodeListArgs(bin, env) {
8774
+ return opencodeMajor(bin, env) >= 2 ? ["mcp", "list", "--standalone"] : ["mcp", "list", "--pure"];
8767
8775
  }
8768
8776
  function listState(row, env) {
8769
8777
  const l0 = row.listCommand;
@@ -8950,7 +8958,8 @@ function hookRegisteredCheck(row, env, list2) {
8950
8958
  const trust = h2.kind === "file" ? h2.trust : void 0;
8951
8959
  if (trust && edits.every((e) => e.file === trust && e.changes.every((c3) => isTrust(row, c3)))) return { name: "hook-registered", ok: false, detail: `${h2.file}: the context-mode hooks are not trusted in ${trust}; re-run setup` };
8952
8960
  const issue = h2.kind === "plugin-list" ? pluginListIssue(h2) : h2.kind === "file" && row.id === "kiro" ? kiroDisabledIssue(h2) : void 0;
8953
- return { name: "hook-registered", ok: false, detail: issue ?? `${h2.file} lacks the context-mode hook entry` };
8961
+ const oc2 = issue === void 0 && row.id === "opencode" && h2.kind === "plugin-list" && hostBin("opencode", env).file !== void 0 && opencodeMajor("opencode", env) >= 2;
8962
+ return { name: "hook-registered", ok: false, detail: issue ?? `${h2.file} lacks the context-mode hook entry${oc2 ? "; run: opencode plugin add context-mode@next" : ""}` };
8954
8963
  }
8955
8964
  function hookRunsCheck(row, env) {
8956
8965
  const root = mkdtempSync(path12.join(tmpdir2(), "cm-doctor-runs-"));
@@ -9411,7 +9420,7 @@ async function runDoctor(opts) {
9411
9420
  res.reason = V1_FORM_REASON;
9412
9421
  } else if (res.changes.length && !onlyTrust(row, res.changes) && !hasPluginIssue(row)) {
9413
9422
  res.status = "FAIL";
9414
- res.reason = NOT_SET_UP_REASON;
9423
+ res.reason = notSetUpReason(row, res, ctx.env);
9415
9424
  } else {
9416
9425
  res.checks = await verifyRow(row, ctx);
9417
9426
  const bad = res.checks.filter((c3) => !c3.ok);
@@ -9426,6 +9435,12 @@ async function runDoctor(opts) {
9426
9435
  }
9427
9436
  return { exitCode: agents.some((a2) => a2.status === "FAIL") ? 1 : 0, agents };
9428
9437
  }
9438
+ function notSetUpReason(row, res, env) {
9439
+ const h2 = row.hookInstall;
9440
+ const missing = row.id === "opencode" && h2.kind === "plugin-list" && res.changes.some((c3) => c3.path === h2.key[0]);
9441
+ const oc2 = missing && hostBin("opencode", env).file !== void 0 && opencodeMajor("opencode", env) >= 2;
9442
+ return oc2 ? `${NOT_SET_UP_REASON}, or run: opencode plugin add context-mode@next` : NOT_SET_UP_REASON;
9443
+ }
9429
9444
  function hasPluginIssue(row) {
9430
9445
  const h2 = row.hookInstall;
9431
9446
  if (h2.kind === "file") return row.id === "kiro" && kiroDisabledIssue(h2) !== void 0;
@@ -11,5 +11,5 @@
11
11
  }
12
12
  },
13
13
  "name": "context-mode",
14
- "version": "2.0.0-next.20"
14
+ "version": "2.0.0-next.21"
15
15
  }
@@ -11,5 +11,5 @@
11
11
  }
12
12
  },
13
13
  "name": "context-mode",
14
- "version": "2.0.0-next.20"
14
+ "version": "2.0.0-next.21"
15
15
  }
@@ -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 ? "efe519b1d2f8b002ada6ef542af2df61e2abc20f" : 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",
@@ -19965,8 +19965,8 @@ function decisionQueries(input) {
19965
19965
  function pageSize(input) {
19966
19966
  return input.source === DECISION_SOURCE ? SEARCH_PAGE_SIZE : SEARCH_REPLY_HITS;
19967
19967
  }
19968
- function page(db, q, input, scope, ctx, offset) {
19969
- const size = pageSize(input);
19968
+ function page(db, q, input, scope, ctx, offset, extra = 0) {
19969
+ const size = pageSize(input) + extra;
19970
19970
  if (input.source === DECISION_SOURCE && q.trim() === "") {
19971
19971
  return ftsListSource(db, DECISION_SOURCE, scope, ctx.project, ctx.sessionId, size, offset);
19972
19972
  }
@@ -20015,51 +20015,76 @@ function jsonRowsReply(db, input, scope, ctx) {
20015
20015
  }
20016
20016
  return shown2 === 0 && !more ? null : reply(out.join("\n"), false);
20017
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
+ }
20018
20028
  function relevance(db, given, scope, ctx) {
20019
20029
  const input = { ...given, queries: decisionQueries(given) };
20020
20030
  const offset = input.offset ?? 0;
20021
20031
  const size = pageSize(input);
20022
- const seen = /* @__PURE__ */ new Set();
20023
- const entries = [];
20024
- const full = /* @__PURE__ */ new Set();
20025
- input.queries.forEach((q, qi) => {
20026
- const hits = page(db, q, input, scope, ctx, offset);
20027
- if (hits.length === size) full.add(qi);
20028
- hits.forEach((hit, i) => {
20029
- const key = hit.sourceId + ":" + hit.ord;
20030
- if (seen.has(key)) return;
20031
- seen.add(key);
20032
- entries.push({ qi, pos: offset + i, hit });
20033
- });
20034
- });
20035
- 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);
20036
20035
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, input.source, "offset:" + pos);
20037
20036
  let reserve = 0;
20038
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();
20039
20040
  const lines = [];
20040
20041
  const more = [];
20042
+ const shown2 = /* @__PURE__ */ new Set();
20041
20043
  let used = 0;
20042
20044
  let stopped = false;
20043
20045
  input.queries.forEach((q, qi) => {
20044
- const mine = entries.filter((e) => e.qi === qi);
20046
+ const hits = pages[qi];
20045
20047
  if (stopped) {
20046
- more.push(moreLine3(q, mine.length > 0 ? mine[0].pos : offset));
20048
+ more.push(moreLine3(q, offset));
20047
20049
  return;
20048
20050
  }
20049
20051
  const head = "Query " + JSON.stringify(q) + ":";
20050
20052
  lines.push(head);
20051
20053
  used += bytes2(head) + 1;
20052
- for (const e of mine) {
20053
- 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;
20054
20065
  if (used + bytes2(line) + 1 + reserve > OUTPUT_CAP) {
20055
- more.push(moreLine3(q, e.pos));
20066
+ cut = offset + i;
20056
20067
  stopped = true;
20057
20068
  break;
20058
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("; ");
20059
20083
  lines.push(line);
20060
20084
  used += bytes2(line) + 1;
20061
20085
  }
20062
- 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));
20063
20088
  });
20064
20089
  return reply(lines.concat(more).join("\n"), false);
20065
20090
  }
package/package.json CHANGED
@@ -67,5 +67,5 @@
67
67
  "url": "git+https://github.com/mksglu/context-mode.git"
68
68
  },
69
69
  "type": "module",
70
- "version": "2.0.0-next.20"
70
+ "version": "2.0.0-next.21"
71
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 ? "efe519b1d2f8b002ada6ef542af2df61e2abc20f" : 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",
@@ -19972,8 +19972,8 @@ function decisionQueries(input) {
19972
19972
  function pageSize(input) {
19973
19973
  return input.source === DECISION_SOURCE ? SEARCH_PAGE_SIZE : SEARCH_REPLY_HITS;
19974
19974
  }
19975
- function page(db, q, input, scope, ctx, offset) {
19976
- const size = pageSize(input);
19975
+ function page(db, q, input, scope, ctx, offset, extra = 0) {
19976
+ const size = pageSize(input) + extra;
19977
19977
  if (input.source === DECISION_SOURCE && q.trim() === "") {
19978
19978
  return ftsListSource(db, DECISION_SOURCE, scope, ctx.project, ctx.sessionId, size, offset);
19979
19979
  }
@@ -20022,51 +20022,76 @@ function jsonRowsReply(db, input, scope, ctx) {
20022
20022
  }
20023
20023
  return shown2 === 0 && !more ? null : reply(out.join("\n"), false);
20024
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
+ }
20025
20035
  function relevance(db, given, scope, ctx) {
20026
20036
  const input = { ...given, queries: decisionQueries(given) };
20027
20037
  const offset = input.offset ?? 0;
20028
20038
  const size = pageSize(input);
20029
- const seen = /* @__PURE__ */ new Set();
20030
- const entries = [];
20031
- const full = /* @__PURE__ */ new Set();
20032
- input.queries.forEach((q, qi) => {
20033
- const hits = page(db, q, input, scope, ctx, offset);
20034
- if (hits.length === size) full.add(qi);
20035
- hits.forEach((hit, i) => {
20036
- const key = hit.sourceId + ":" + hit.ord;
20037
- if (seen.has(key)) return;
20038
- seen.add(key);
20039
- entries.push({ qi, pos: offset + i, hit });
20040
- });
20041
- });
20042
- 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);
20043
20042
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, input.source, "offset:" + pos);
20044
20043
  let reserve = 0;
20045
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();
20046
20047
  const lines = [];
20047
20048
  const more = [];
20049
+ const shown2 = /* @__PURE__ */ new Set();
20048
20050
  let used = 0;
20049
20051
  let stopped = false;
20050
20052
  input.queries.forEach((q, qi) => {
20051
- const mine = entries.filter((e) => e.qi === qi);
20053
+ const hits = pages[qi];
20052
20054
  if (stopped) {
20053
- more.push(moreLine3(q, mine.length > 0 ? mine[0].pos : offset));
20055
+ more.push(moreLine3(q, offset));
20054
20056
  return;
20055
20057
  }
20056
20058
  const head = "Query " + JSON.stringify(q) + ":";
20057
20059
  lines.push(head);
20058
20060
  used += bytes2(head) + 1;
20059
- for (const e of mine) {
20060
- 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;
20061
20072
  if (used + bytes2(line) + 1 + reserve > OUTPUT_CAP) {
20062
- more.push(moreLine3(q, e.pos));
20073
+ cut = offset + i;
20063
20074
  stopped = true;
20064
20075
  break;
20065
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("; ");
20066
20090
  lines.push(line);
20067
20091
  used += bytes2(line) + 1;
20068
20092
  }
20069
- 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));
20070
20095
  });
20071
20096
  return reply(lines.concat(more).join("\n"), false);
20072
20097
  }
@@ -12,5 +12,5 @@
12
12
  }
13
13
  },
14
14
  "name": "context-mode",
15
- "version": "2.0.0-next.20"
15
+ "version": "2.0.0-next.21"
16
16
  }
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 ? "efe519b1d2f8b002ada6ef542af2df61e2abc20f" : 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
  }