context-mode 2.0.0-next.38 → 2.0.0-next.39

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
@@ -3497,26 +3497,26 @@ function isRecursive(inst, stack2, resolve6) {
3497
3497
  let result = NONE;
3498
3498
  const check2 = (child) => {
3499
3499
  if (result !== PROVEN && child?._zod) {
3500
- const answer2 = isRecursive(child, stack2, resolve6);
3501
- if (answer2 > result)
3502
- result = answer2;
3500
+ const answer3 = isRecursive(child, stack2, resolve6);
3501
+ if (answer3 > result)
3502
+ result = answer3;
3503
3503
  }
3504
3504
  };
3505
3505
  const shape = (sh, spread) => {
3506
- let answer2 = NONE;
3506
+ let answer3 = NONE;
3507
3507
  for (const key of Reflect.ownKeys(sh)) {
3508
3508
  const desc = Object.getOwnPropertyDescriptor(sh, key);
3509
3509
  if (spread && !desc.enumerable)
3510
3510
  continue;
3511
3511
  const child = desc.get ? ASSUMED : desc.value?._zod ? isRecursive(desc.value, stack2, resolve6) : NONE;
3512
- if (child > answer2)
3513
- answer2 = child;
3512
+ if (child > answer3)
3513
+ answer3 = child;
3514
3514
  }
3515
- return answer2;
3515
+ return answer3;
3516
3516
  };
3517
- const merge5 = (answer2) => {
3518
- if (answer2 > result)
3519
- result = answer2;
3517
+ const merge5 = (answer3) => {
3518
+ if (answer3 > result)
3519
+ result = answer3;
3520
3520
  };
3521
3521
  const def = inst._zod.def;
3522
3522
  const kind = def.type;
@@ -3619,10 +3619,10 @@ function isRecursive(inst, stack2, resolve6) {
3619
3619
  stack2.delete(inst);
3620
3620
  return settle(inst, result);
3621
3621
  }
3622
- function settle(inst, answer2) {
3623
- if (answer2 !== ASSUMED)
3624
- recursive.set(inst, answer2 === PROVEN);
3625
- return answer2;
3622
+ function settle(inst, answer3) {
3623
+ if (answer3 !== ASSUMED)
3624
+ recursive.set(inst, answer3 === PROVEN);
3625
+ return answer3;
3626
3626
  }
3627
3627
  function bucketFor(state, inst) {
3628
3628
  let bucket = state.buckets.get(inst);
@@ -11026,26 +11026,26 @@ function isRecursive2(inst, stack2, resolve6) {
11026
11026
  let result = NONE2;
11027
11027
  const check2 = (child) => {
11028
11028
  if (result !== PROVEN2 && child?._zod) {
11029
- const answer2 = isRecursive2(child, stack2, resolve6);
11030
- if (answer2 > result)
11031
- result = answer2;
11029
+ const answer3 = isRecursive2(child, stack2, resolve6);
11030
+ if (answer3 > result)
11031
+ result = answer3;
11032
11032
  }
11033
11033
  };
11034
11034
  const shape = (sh, spread) => {
11035
- let answer2 = NONE2;
11035
+ let answer3 = NONE2;
11036
11036
  for (const key of Reflect.ownKeys(sh)) {
11037
11037
  const desc = Object.getOwnPropertyDescriptor(sh, key);
11038
11038
  if (spread && !desc.enumerable)
11039
11039
  continue;
11040
11040
  const child = desc.get ? ASSUMED2 : desc.value?._zod ? isRecursive2(desc.value, stack2, resolve6) : NONE2;
11041
- if (child > answer2)
11042
- answer2 = child;
11041
+ if (child > answer3)
11042
+ answer3 = child;
11043
11043
  }
11044
- return answer2;
11044
+ return answer3;
11045
11045
  };
11046
- const merge5 = (answer2) => {
11047
- if (answer2 > result)
11048
- result = answer2;
11046
+ const merge5 = (answer3) => {
11047
+ if (answer3 > result)
11048
+ result = answer3;
11049
11049
  };
11050
11050
  const def = inst._zod.def;
11051
11051
  const kind = def.type;
@@ -11148,10 +11148,10 @@ function isRecursive2(inst, stack2, resolve6) {
11148
11148
  stack2.delete(inst);
11149
11149
  return settle2(inst, result);
11150
11150
  }
11151
- function settle2(inst, answer2) {
11152
- if (answer2 !== ASSUMED2)
11153
- recursive2.set(inst, answer2 === PROVEN2);
11154
- return answer2;
11151
+ function settle2(inst, answer3) {
11152
+ if (answer3 !== ASSUMED2)
11153
+ recursive2.set(inst, answer3 === PROVEN2);
11154
+ return answer3;
11155
11155
  }
11156
11156
  function bucketFor2(state, inst) {
11157
11157
  let bucket = state.buckets.get(inst);
@@ -28067,6 +28067,7 @@ var STATE_CLOSE_TAG = "</context_mode_state>";
28067
28067
  var INDEX_WRITE_CHUNK = 1048576;
28068
28068
  var MAX_CHUNK_BYTES = 4096;
28069
28069
  var SEARCH_PAGE_SIZE = 20;
28070
+ var MEMORY_LABEL_PREFIX = "memory ";
28070
28071
  var SEARCH_REPLY_HITS = 5;
28071
28072
  var SNIPPET_MAX_BYTES = 800;
28072
28073
  var SNIPPET_CODE_MAX_BYTES = 2048;
@@ -28405,7 +28406,7 @@ var DECISION_SOURCE = "decision";
28405
28406
  var DECISION_USER_LABEL = "decision (user)";
28406
28407
  var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
28407
28408
  var RELEASE_BRANCH = "release";
28408
- var BUNDLE_SHA = true ? "ede83923546ab97898750ef7cebe34cf82e9534c" : void 0;
28409
+ var BUNDLE_SHA = true ? "1fdd9889d173fa80227ade07d30705291f097df5" : void 0;
28409
28410
  var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
28410
28411
  var DOCTOR_LABEL = Object.freeze({
28411
28412
  D1: "version",
@@ -32088,7 +32089,7 @@ function ftsQueryTimeline(db, scope, ctx, sessionId, limit, after, source, memor
32088
32089
  }
32089
32090
  }
32090
32091
  for (const m of memory) {
32091
- if (m.ts > from && (source === void 0 || m.label === source)) merged.push(m);
32092
+ if (m.ts > from && (source === void 0 || m.label === source || MEMORY_LABEL_PREFIX + m.label === source)) merged.push(m);
32092
32093
  }
32093
32094
  merged.sort(timelineOrder);
32094
32095
  let end = Math.min(limit, merged.length);
@@ -44145,6 +44146,10 @@ function readAutoMemory(project) {
44145
44146
  }
44146
44147
  return rows;
44147
44148
  }
44149
+ function memoryFileNamed(rows, value) {
44150
+ const v = value.split("\\").join("/");
44151
+ return rows.find((r) => r.label === v || MEMORY_LABEL_PREFIX + r.label === v) ?? null;
44152
+ }
44148
44153
  function memoryChunks(rows) {
44149
44154
  const out = [];
44150
44155
  rows.forEach((r, i) => {
@@ -44157,7 +44162,7 @@ function memoryHits(chunks, q, limit) {
44157
44162
  const texts = chunks.map((c) => c.text);
44158
44163
  const any3 = rankTexts(texts, q, "any");
44159
44164
  const all = rankTexts(texts, q, "all");
44160
- return chunks.map((c, i) => ({ c, s: any3[i], full: all[i] > 0 })).filter((e) => e.s > 0).sort((a, b) => Number(b.full) - Number(a.full) || b.s - a.s || a.c.fileNo - b.c.fileNo || a.c.ord - b.c.ord).slice(0, limit).map((e) => ({ sourceId: -e.c.fileNo, label: "memory " + e.c.label, ord: e.c.ord, snippet: highlight(e.c.text, q, SNIPPET_MAX_BYTES), score: e.s, full: e.full }));
44165
+ return chunks.map((c, i) => ({ c, s: any3[i], full: all[i] > 0 })).filter((e) => e.s > 0).sort((a, b) => Number(b.full) - Number(a.full) || b.s - a.s || a.c.fileNo - b.c.fileNo || a.c.ord - b.c.ord).slice(0, limit).map((e) => ({ sourceId: -e.c.fileNo, label: MEMORY_LABEL_PREFIX + e.c.label, ord: e.c.ord, snippet: highlight(e.c.text, q, SNIPPET_MAX_BYTES), score: e.s, full: e.full }));
44161
44166
  }
44162
44167
 
44163
44168
  // src/core/index/shape.ts
@@ -44249,8 +44254,8 @@ function sourceCount(db, scope, ctx, source) {
44249
44254
  });
44250
44255
  return Number(row5.n);
44251
44256
  }
44252
- function zeroHits(db, scope, ctx, source, offset = 0) {
44253
- const n = sourceCount(db, scope, ctx, source);
44257
+ function zeroHits(db, scope, ctx, source, mem, offset = 0) {
44258
+ const n = sourceCount(db, scope, ctx, source) + mem.rows.length;
44254
44259
  if (offset > 0) return reply("No more hits past offset " + offset + ' in scope "' + scope + '" across ' + n + " source(s).", false);
44255
44260
  return reply('No hits in scope "' + scope + '" across ' + n + " source(s).", false);
44256
44261
  }
@@ -44273,7 +44278,7 @@ function sourceCard(db, id, input, scope, arg) {
44273
44278
  }
44274
44279
  function callText(queries, scope, source, cursor) {
44275
44280
  const src = source === void 0 ? "" : ", source:" + JSON.stringify(source);
44276
- return "ctx_search({queries:" + JSON.stringify(queries) + ", scope:" + JSON.stringify(scope) + src + ", " + cursor + "})";
44281
+ return "ctx_search({queries:" + JSON.stringify(queries) + ", scope:" + JSON.stringify(scope) + src + (cursor === "" ? "" : ", " + cursor) + "})";
44277
44282
  }
44278
44283
  function decisionQueries(input) {
44279
44284
  return input.source === DECISION_SOURCE && input.queries.length === 0 ? [""] : input.queries;
@@ -44372,18 +44377,24 @@ function mergeMemory(content, memory) {
44372
44377
  return out;
44373
44378
  }
44374
44379
  var contentIn = (hits, n) => hits.slice(0, n).filter((h) => h.sourceId > 0).length;
44375
- function relevance(db, given, scope, ctx, nm) {
44380
+ function relevance(db, given, scope, ctx, nm, ms, indexed) {
44376
44381
  const input = { ...given, queries: decisionQueries(given) };
44377
44382
  const offset = input.offset ?? 0;
44378
44383
  const size = pageSize(input);
44379
- const mem = offset === 0 && scope !== "session" && input.source === void 0 ? memoryChunks(readAutoMemory(ctx.project)) : [];
44384
+ const mem = ms.named || offset === 0 ? memoryChunks(ms.rows) : [];
44385
+ const count2 = (hits, n) => ms.named ? Math.min(n, hits.length) : contentIn(hits, n);
44380
44386
  const fetched = input.queries.map((q) => {
44387
+ if (ms.named) {
44388
+ const want = offset + size + 1;
44389
+ const content2 = indexed ? ftsQuery(db, q, scope, ctx.project, ctx.sessionId, want, 0, input.source) : [];
44390
+ return mergeMemory(content2, memoryHits(mem, q, want)).slice(offset, want);
44391
+ }
44381
44392
  const content = page(db, q, input, scope, ctx, offset, 1);
44382
44393
  return mem.length === 0 ? content : mergeMemory(content, memoryHits(mem, q, MEMORY_HITS));
44383
44394
  });
44384
44395
  const take = fetched.map((h) => replyHits(h, size));
44385
44396
  const pages = fetched.map((h, i) => h.slice(0, take[i]));
44386
- if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source, offset);
44397
+ if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source, ms, offset);
44387
44398
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, nm.arg, "offset:" + pos);
44388
44399
  let reserve = 0;
44389
44400
  for (const q of input.queries) reserve += bytes2(moreLine3(q, offset + size)) + 1;
@@ -44416,13 +44427,13 @@ function relevance(db, given, scope, ctx, nm) {
44416
44427
  }
44417
44428
  const line = shownLabel(hit.label, hit.sourceId) + " #" + hit.ord + ": " + hit.snippet;
44418
44429
  if (used + bytes2(line) + 1 + reserve > OUTPUT_CAP) {
44419
- cut = offset + contentIn(hits, i);
44430
+ cut = offset + count2(hits, i);
44420
44431
  stopped = true;
44421
44432
  break;
44422
44433
  }
44423
44434
  const room = total.get(hit.sourceId);
44424
44435
  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) {
44425
- cut = offset + contentIn(hits, i);
44436
+ cut = offset + count2(hits, i);
44426
44437
  break;
44427
44438
  }
44428
44439
  lines.push(line);
@@ -44437,7 +44448,7 @@ function relevance(db, given, scope, ctx, nm) {
44437
44448
  used += bytes2(line) + 1;
44438
44449
  }
44439
44450
  if (cut >= 0) more.push({ q, pos: cut });
44440
- else if (contentIn(fetched[qi], fetched[qi].length) > contentIn(fetched[qi], take[qi])) more.push({ q, pos: offset + contentIn(fetched[qi], take[qi]) });
44451
+ else if (count2(fetched[qi], fetched[qi].length) > count2(fetched[qi], take[qi])) more.push({ q, pos: offset + count2(fetched[qi], take[qi]) });
44441
44452
  });
44442
44453
  const moreLines = [];
44443
44454
  const byPos = /* @__PURE__ */ new Map();
@@ -44451,10 +44462,9 @@ function timelineLine(r) {
44451
44462
  const seq = r.seq === null ? "" : " #" + r.seq;
44452
44463
  return "[" + new Date(r.ts).toISOString() + "] " + shownLabel(r.label, r.sourceId) + seq + " " + first;
44453
44464
  }
44454
- function timeline(db, input, scope, ctx, nm) {
44455
- const memory = scope === "session" ? [] : readAutoMemory(ctx.project);
44456
- const page2 = ftsQueryTimeline(db, scope, ctx.project, ctx.sessionId, SEARCH_PAGE_SIZE, input.after ?? null, input.source, memory);
44457
- if (page2.rows.length === 0) return zeroHits(db, scope, ctx, input.source);
44465
+ function timeline(db, input, scope, ctx, nm, ms) {
44466
+ const page2 = ftsQueryTimeline(db, scope, ctx.project, ctx.sessionId, SEARCH_PAGE_SIZE, input.after ?? null, input.source, ms.rows);
44467
+ if (page2.rows.length === 0) return zeroHits(db, scope, ctx, input.source, ms);
44458
44468
  const moreLine3 = (ts) => "more: " + callText(input.queries, scope, nm.arg, 'sort:"timeline", after:' + ts);
44459
44469
  const reserve = bytes2(moreLine3(page2.rows[page2.rows.length - 1].ts)) + 1;
44460
44470
  const lines = nm.note === null ? [] : [nm.note];
@@ -44483,6 +44493,15 @@ function timeline(db, input, scope, ctx, nm) {
44483
44493
  if (more && lastTs !== null) lines.push(moreLine3(lastTs));
44484
44494
  return reply(lines.join("\n"), false);
44485
44495
  }
44496
+ function answer(db, input, scope, ctx, nm, ms, indexed, sort, pick4) {
44497
+ if (sort === "timeline") return timeline(db, input, scope, ctx, nm, ms);
44498
+ if (pick4 !== null && pick4.id !== null && input.queries.length === 0) {
44499
+ const card = sourceCard(db, pick4.id, input, scope, nm.arg);
44500
+ if (card !== null) return card;
44501
+ }
44502
+ const rows = ms.named ? null : jsonRowsReply(db, input, scope, ctx, nm);
44503
+ return rows ?? relevance(db, input, scope, ctx, nm, ms, indexed);
44504
+ }
44486
44505
  async function handler(input, ctx) {
44487
44506
  const sort = input.sort ?? "relevance";
44488
44507
  if (sort === "timeline" && input.offset !== void 0) {
@@ -44498,12 +44517,18 @@ async function handler(input, ctx) {
44498
44517
  const pick4 = given !== void 0 && given !== DECISION_SOURCE ? pickSource(db, given) : null;
44499
44518
  if (pick4 !== null) input = { ...input, source: pick4.label };
44500
44519
  const nm = named(given, pick4);
44501
- if (sort === "timeline") return timeline(db, input, scope, ctx, nm);
44502
- if (pick4 !== null && pick4.id !== null && input.queries.length === 0) {
44503
- const card = sourceCard(db, pick4.id, input, scope, nm.arg);
44504
- if (card !== null) return card;
44505
- }
44506
- return jsonRowsReply(db, input, scope, ctx, nm) ?? relevance(db, input, scope, ctx, nm);
44520
+ const indexed = pick4 !== null && pick4.id !== null;
44521
+ const files = scope !== "session" || given !== void 0 ? readAutoMemory(ctx.project) : [];
44522
+ const file2 = given === void 0 || given === DECISION_SOURCE ? null : memoryFileNamed(files, given);
44523
+ const ms = { rows: scope === "session" ? [] : file2 !== null ? [file2] : given === void 0 ? files : [], named: file2 !== null };
44524
+ if (file2 !== null && indexed && scope !== "session") {
44525
+ const both = "Source " + JSON.stringify(given) + ": searched the indexed source " + shownLabel(pick4.label, pick4.id) + " and the memory file " + file2.label + ".";
44526
+ nm.note = nm.note === null ? both : nm.note + "\n" + both;
44527
+ }
44528
+ const out = answer(db, input, scope, ctx, nm, ms, indexed, sort, pick4);
44529
+ if (file2 === null || scope !== "session") return out;
44530
+ const call = callText(input.queries, "worktree", nm.arg, sort === "timeline" ? 'sort:"timeline"' : "");
44531
+ return reply(out.text + "\nMemory file " + file2.label + ' is in scope "worktree": ' + call + " searches it.", out.isError);
44507
44532
  } finally {
44508
44533
  closeDb(db);
44509
44534
  }
@@ -44639,7 +44664,7 @@ function indexCombined(db, ctx, label2, ran, deadline, budgetBytes) {
44639
44664
  function bytes3(s) {
44640
44665
  return Buffer.byteLength(s, "utf8");
44641
44666
  }
44642
- function answer(db, ctx, ownIds, queries, share, own4) {
44667
+ function answer2(db, ctx, ownIds, queries, share, own4) {
44643
44668
  const seen = /* @__PURE__ */ new Set();
44644
44669
  const lines = [];
44645
44670
  for (const [qi, q] of queries.entries()) {
@@ -44702,7 +44727,7 @@ async function runBatch(input, ctx, boundMs, budgetBytes) {
44702
44727
  notice = cut === null ? stop : cause === null ? cut : cause + " " + cut;
44703
44728
  const fixed = (exitLine === null ? 0 : bytes3(exitLine) + 1) + (notice === null ? 0 : bytes3(notice) + 1) + bytes3(footer);
44704
44729
  const share = Math.floor((OUTPUT_CAP - fixed) / queries.length);
44705
- const answered = answer(db, ctx, idx.ids, queries, share, idx.bodies);
44730
+ const answered = answer2(db, ctx, idx.ids, queries, share, idx.bodies);
44706
44731
  sections = answered.lines;
44707
44732
  const unseen = [...idx.bodies].filter(([k, text]) => text !== "" && !answered.seen.has(k)).map(([k]) => idx.raw.get(k));
44708
44733
  const unseenBytes = unseen.reduce((n, t) => n + bytes3(t) + 1, 0);