context-mode 2.2.0 → 2.2.1

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
@@ -28227,6 +28227,10 @@ var DB_TIMEOUT_MS = 5e3;
28227
28227
  var DB_WAL_SUFFIX = "-wal";
28228
28228
  var DB_SHM_SUFFIX = "-shm";
28229
28229
  var DB_TMP_SUFFIX = ".tmp";
28230
+ var DB_LINK_DENIED_CODES = ["EACCES", "EPERM", "ENOSYS", "ENOTSUP", "EOPNOTSUPP"];
28231
+ var DB_PUBLISH_CLAIM_SUFFIX = ".publish";
28232
+ var DB_CLAIM_BUSY_CODES = ["EEXIST", "EPERM"];
28233
+ var DB_PUBLISH_POLL_MS = 5;
28230
28234
  var SQLITE_PRIMARY_CODE_MASK = 255;
28231
28235
  var SQLITE_CORRUPT_CODE = 11;
28232
28236
  var SQLITE_NOTADB_CODE = 26;
@@ -28447,7 +28451,7 @@ function PURGE_SUMMARY(rows, scope) {
28447
28451
  var DECISION_SOURCE = "decision";
28448
28452
  var DECISION_USER_LABEL = "decision (user)";
28449
28453
  var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
28450
- var BUNDLE_SHA = true ? "9cb2aaa5ebb63491960b3f3e9655a5821383eae4" : void 0;
28454
+ var BUNDLE_SHA = true ? "1fce537096e8e2b7d1924c2b5ff2b83287f157ee" : void 0;
28451
28455
  var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
28452
28456
  var DOCTOR_LABEL = Object.freeze({
28453
28457
  D1: "version",
@@ -29314,7 +29318,7 @@ function openReadOnlyHandle(sqlite, path8, opts = {}) {
29314
29318
  }
29315
29319
 
29316
29320
  // src/core/store/db.ts
29317
- var { existsSync, linkSync, mkdirSync, rmSync } = process.getBuiltinModule("node:fs");
29321
+ var { existsSync, linkSync, mkdirSync, renameSync, rmSync, statSync } = process.getBuiltinModule("node:fs");
29318
29322
  function versionParts(v) {
29319
29323
  const bare2 = v.startsWith("v") ? v.slice(1) : v;
29320
29324
  return bare2.split(".").map((p) => Number.parseInt(p, 10));
@@ -29346,6 +29350,34 @@ function readUserVersion(raw) {
29346
29350
  const row5 = raw.prepare("PRAGMA user_version").get();
29347
29351
  return row5.user_version;
29348
29352
  }
29353
+ function errCode(err) {
29354
+ return err.code ?? "";
29355
+ }
29356
+ function publishWithoutLink(tmp, path8) {
29357
+ const claim3 = path8 + DB_PUBLISH_CLAIM_SUFFIX;
29358
+ const start = Date.now();
29359
+ for (; ; ) {
29360
+ if (existsSync(path8)) return;
29361
+ try {
29362
+ mkdirSync(claim3);
29363
+ } catch (err) {
29364
+ if (!DB_CLAIM_BUSY_CODES.includes(errCode(err))) throw err;
29365
+ if (Date.now() - start < DB_TIMEOUT_MS) {
29366
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, DB_PUBLISH_POLL_MS);
29367
+ continue;
29368
+ }
29369
+ const held = statSync(claim3, { throwIfNoEntry: false });
29370
+ if (held !== void 0 && held.mtimeMs < start) rmSync(claim3, { recursive: true, force: true });
29371
+ throw new Error(`context.db was not published within ${DB_TIMEOUT_MS} ms: ${claim3} was held; open again`);
29372
+ }
29373
+ try {
29374
+ if (!existsSync(path8)) renameSync(tmp, path8);
29375
+ } finally {
29376
+ rmSync(claim3, { recursive: true, force: true });
29377
+ }
29378
+ return;
29379
+ }
29380
+ }
29349
29381
  function createAtomically(sqlite, path8) {
29350
29382
  const { randomUUID: randomUUID2 } = process.getBuiltinModule("node:crypto");
29351
29383
  const tmp = path8 + "." + randomUUID2() + DB_TMP_SUFFIX;
@@ -29359,7 +29391,9 @@ function createAtomically(sqlite, path8) {
29359
29391
  try {
29360
29392
  linkSync(tmp, path8);
29361
29393
  } catch (err) {
29362
- if (err.code !== "EEXIST") throw err;
29394
+ const code = errCode(err);
29395
+ if (DB_LINK_DENIED_CODES.includes(code)) publishWithoutLink(tmp, path8);
29396
+ else if (code !== "EEXIST") throw err;
29363
29397
  }
29364
29398
  } finally {
29365
29399
  for (const suffix of ["", DB_WAL_SUFFIX, DB_SHM_SUFFIX]) rmSync(tmp + suffix, { force: true });
@@ -29855,7 +29889,7 @@ function sha256Hex(text) {
29855
29889
  }
29856
29890
 
29857
29891
  // src/core/project/resolve.ts
29858
- var { existsSync: existsSync3, readFileSync: readFileSync2, realpathSync: realpathSync3, statSync } = process.getBuiltinModule("node:fs");
29892
+ var { existsSync: existsSync3, readFileSync: readFileSync2, realpathSync: realpathSync3, statSync: statSync2 } = process.getBuiltinModule("node:fs");
29859
29893
  function sourceOf(agent) {
29860
29894
  switch (agent) {
29861
29895
  case "claude-code":
@@ -29916,7 +29950,7 @@ function gitDirs(dir) {
29916
29950
  function gitDirAt(dotGit) {
29917
29951
  let gitDir;
29918
29952
  try {
29919
- if (statSync(dotGit).isDirectory()) {
29953
+ if (statSync2(dotGit).isDirectory()) {
29920
29954
  gitDir = dotGit;
29921
29955
  } else {
29922
29956
  const line = firstLine(dotGit);
@@ -32531,12 +32565,12 @@ function recallQueryText(prompt) {
32531
32565
  if (cur !== "" && kept.size < RECALL_QUERY_MAX_TERMS) add(cur);
32532
32566
  return [...kept.size > 0 ? kept : all].join(" ");
32533
32567
  }
32534
- function queryUnits(db, sessionId, q, limit) {
32568
+ function queryUnits(db, sessionId, q, limit, maxSourceId = Number.MAX_SAFE_INTEGER) {
32535
32569
  const match = searchTerms(q).join(" OR ");
32536
32570
  if (match === "" || sessionId === "") return [];
32537
32571
  const rows = db.raw.prepare(
32538
- "SELECT u.source_id AS source_id, u.ord AS ord, u.body AS body, -bm25(line_units) AS score, s.label AS label FROM line_units u JOIN sources s ON s.id = u.source_id WHERE line_units MATCH ? AND u.session_id = ? ORDER BY bm25(line_units) LIMIT ?"
32539
- ).all(match, sessionId, limit);
32572
+ "SELECT u.source_id AS source_id, u.ord AS ord, u.body AS body, -bm25(line_units) AS score, s.label AS label FROM line_units u JOIN sources s ON s.id = u.source_id WHERE line_units MATCH ? AND u.session_id = ? AND s.id <= ? ORDER BY bm25(line_units) LIMIT ?"
32573
+ ).all(match, sessionId, maxSourceId, limit);
32540
32574
  return rows.map((r) => ({ sourceId: Number(r.source_id), ord: Number(r.ord), label: r.label, body: r.body, score: r.score }));
32541
32575
  }
32542
32576
 
@@ -32549,7 +32583,7 @@ function ownersDir(configDir, projectKey) {
32549
32583
  function ownerFile(configDir, projectKey, serverId) {
32550
32584
  return join4(ownersDir(configDir, projectKey), serverId + OWNER_PID_EXT);
32551
32585
  }
32552
- function errCode(err) {
32586
+ function errCode2(err) {
32553
32587
  return err.code;
32554
32588
  }
32555
32589
  function writeOwnerPid(configDir, projectKey, serverId) {
@@ -32565,8 +32599,8 @@ function isLive(pid) {
32565
32599
  process.kill(pid, 0);
32566
32600
  return true;
32567
32601
  } catch (err) {
32568
- if (errCode(err) === "ESRCH") return false;
32569
- if (errCode(err) === "EPERM") return true;
32602
+ if (errCode2(err) === "ESRCH") return false;
32603
+ if (errCode2(err) === "EPERM") return true;
32570
32604
  throw err;
32571
32605
  }
32572
32606
  }
@@ -32576,7 +32610,7 @@ function listLiveOwners(configDir, projectKey) {
32576
32610
  try {
32577
32611
  names = readdirSync2(dir);
32578
32612
  } catch (err) {
32579
- if (errCode(err) === "ENOENT") return [];
32613
+ if (errCode2(err) === "ENOENT") return [];
32580
32614
  throw err;
32581
32615
  }
32582
32616
  const live = [];
@@ -32587,7 +32621,7 @@ function listLiveOwners(configDir, projectKey) {
32587
32621
  try {
32588
32622
  text = readFileSync3(file2, "utf8");
32589
32623
  } catch (err) {
32590
- if (errCode(err) === "ENOENT") continue;
32624
+ if (errCode2(err) === "ENOENT") continue;
32591
32625
  throw err;
32592
32626
  }
32593
32627
  const serverId = basename3(name, OWNER_PID_EXT);
@@ -44220,7 +44254,7 @@ function buildChildEnv(parent, extra) {
44220
44254
  }
44221
44255
 
44222
44256
  // src/core/sandbox/runtimes.ts
44223
- import { accessSync, constants as fsConstants, statSync as statSync2 } from "node:fs";
44257
+ import { accessSync, constants as fsConstants, statSync as statSync3 } from "node:fs";
44224
44258
  import path from "node:path";
44225
44259
 
44226
44260
  // src/core/sandbox/path-entries.ts
@@ -44265,7 +44299,7 @@ function fileNames(name, pathExt) {
44265
44299
  function probe(full) {
44266
44300
  let st;
44267
44301
  try {
44268
- st = statSync2(full, { throwIfNoEntry: false });
44302
+ st = statSync3(full, { throwIfNoEntry: false });
44269
44303
  } catch {
44270
44304
  return "unreadable";
44271
44305
  }
@@ -44460,7 +44494,7 @@ function runProcess(argv, o) {
44460
44494
  }
44461
44495
 
44462
44496
  // src/core/index/auto-memory.ts
44463
- import { readdirSync as readdirSync4, readFileSync as readFileSync4, statSync as statSync3 } from "node:fs";
44497
+ import { readdirSync as readdirSync4, readFileSync as readFileSync4, statSync as statSync4 } from "node:fs";
44464
44498
  import { join as join6 } from "node:path";
44465
44499
  var MEMORY_FILES = Object.freeze([
44466
44500
  Object.freeze(["CLAUDE.md"]),
@@ -44486,7 +44520,7 @@ function readAutoMemory(project) {
44486
44520
  const path8 = join6(project.worktree, ...segs);
44487
44521
  if (!isInside(project.worktree, path8)) continue;
44488
44522
  try {
44489
- const st = statSync3(path8);
44523
+ const st = statSync4(path8);
44490
44524
  if (!st.isFile()) continue;
44491
44525
  rows.push({
44492
44526
  origin: "memory",
@@ -44610,15 +44644,42 @@ function sourceCount(db, scope, ctx, source) {
44610
44644
  });
44611
44645
  return Number(row5.n);
44612
44646
  }
44613
- function zeroHits(db, scope, ctx, source, mem, offset = 0) {
44614
- const n = sourceCount(db, scope, ctx, source) + mem.rows.length;
44647
+ function zeroHits(db, scope, ctx, source, mem, nm, queries, offset = 0) {
44648
+ const inScope = sourceCount(db, scope, ctx, source);
44649
+ const n = inScope + mem.rows.length;
44615
44650
  if (offset > 0) return reply("No more hits past offset " + offset + ' in scope "' + scope + '" across ' + n + " source(s).", false);
44616
- return reply('No hits in scope "' + scope + '" across ' + n + " source(s).", false);
44651
+ const head = 'No hits in scope "' + scope + '" across ' + n + " source(s).";
44652
+ const pick4 = nm.pick;
44653
+ if (pick4 === null || mem.named || inScope > 0) return reply(head, false);
44654
+ if (pick4.id !== null) {
44655
+ const wider = scope === "session" && sourceCount(db, "worktree", ctx, source) > 0 ? "worktree" : "project";
44656
+ return reply(head + "\nSource " + JSON.stringify(nm.arg) + ' is stored in scope "' + wider + '": ' + callText(queries, wider, nm.arg, "") + " searches it.", false);
44657
+ }
44658
+ return reply(head + "\n" + unknownSource(db, scope, ctx, nm.arg), true);
44659
+ }
44660
+ var STORED_SOURCES = `
44661
+ SELECT MAX(id) AS id, label FROM sources
44662
+ WHERE (:scope <> 'session' OR session_id = :session_id)
44663
+ AND (:scope <> 'worktree' OR worktree = :worktree)
44664
+ GROUP BY label ORDER BY MAX(id) DESC LIMIT :limit`;
44665
+ var STORED_SOURCES_SHOWN = 5;
44666
+ function unknownSource(db, scope, ctx, given) {
44667
+ const line = "Source " + JSON.stringify(given) + ' is not a stored source in scope "' + scope + '".';
44668
+ const rows = db.raw.prepare(STORED_SOURCES).all({
44669
+ scope,
44670
+ session_id: ctx.sessionId,
44671
+ worktree: ctx.project.worktree,
44672
+ limit: STORED_SOURCES_SHOWN + 1
44673
+ });
44674
+ if (rows.length === 0) return line + '\nctx_index stores one: ctx_index({content:"...", source:' + JSON.stringify(given) + "}).";
44675
+ const names = rows.slice(0, STORED_SOURCES_SHOWN).map((r) => isLongLabel(r.label) ? sourceHandle(Number(r.id)) : JSON.stringify(r.label));
44676
+ const more = rows.length > STORED_SOURCES_SHOWN ? ", ..." : "";
44677
+ return line + "\nStored sources, newest first: " + names.join(", ") + more + ". " + callText([], scope, void 0, 'sort:"timeline"') + " lists them all.";
44617
44678
  }
44618
44679
  function named(given, pick4) {
44619
- if (given === void 0 || pick4 === null) return { arg: given, note: null };
44680
+ if (given === void 0 || pick4 === null) return { arg: given, note: null, pick: null };
44620
44681
  const arg = pick4.id !== null && isLongLabel(given) ? sourceHandle(pick4.id) : given;
44621
- return { arg, note: collisionLine(given, pick4) };
44682
+ return { arg, note: collisionLine(given, pick4), pick: pick4 };
44622
44683
  }
44623
44684
  function sourceCard(db, id, input, scope, arg) {
44624
44685
  const row5 = sourceCardRow(db, id);
@@ -44750,7 +44811,7 @@ function relevance(db, given, scope, ctx, nm, ms, indexed) {
44750
44811
  });
44751
44812
  const take = fetched.map((h) => replyHits(h, size));
44752
44813
  const pages = fetched.map((h, i) => h.slice(0, take[i]));
44753
- if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source, ms, offset);
44814
+ if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source, ms, nm, input.queries, offset);
44754
44815
  const moreLine3 = (q, pos) => "More: " + callText([q], scope, nm.arg, "offset:" + pos);
44755
44816
  let reserve = 0;
44756
44817
  for (const q of input.queries) reserve += bytes2(moreLine3(q, offset + size)) + 1;
@@ -44820,7 +44881,7 @@ function timelineLine(r) {
44820
44881
  }
44821
44882
  function timeline(db, input, scope, ctx, nm, ms) {
44822
44883
  const page2 = ftsQueryTimeline(db, scope, ctx.project, ctx.sessionId, SEARCH_PAGE_SIZE, input.after ?? null, input.source, ms.rows);
44823
- if (page2.rows.length === 0) return zeroHits(db, scope, ctx, input.source, ms);
44884
+ if (page2.rows.length === 0) return zeroHits(db, scope, ctx, input.source, ms, nm, input.queries);
44824
44885
  const moreLine3 = (ts) => "more: " + callText(input.queries, scope, nm.arg, 'sort:"timeline", after:' + ts);
44825
44886
  const reserve = bytes2(moreLine3(page2.rows[page2.rows.length - 1].ts)) + 1;
44826
44887
  const lines = nm.note === null ? [] : [nm.note];
@@ -48285,16 +48346,16 @@ function parseTOML(code, options) {
48285
48346
  }
48286
48347
 
48287
48348
  // src/core/ops/atomic-file.ts
48288
- import { mkdirSync as mkdirSync4, renameSync, unlinkSync, writeFileSync as writeFileSync2 } from "node:fs";
48349
+ import { mkdirSync as mkdirSync4, renameSync as renameSync2, unlinkSync, writeFileSync as writeFileSync2 } from "node:fs";
48289
48350
  import path3 from "node:path";
48290
48351
  function sleepSync(ms) {
48291
48352
  Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
48292
48353
  }
48293
- function errCode2(e) {
48354
+ function errCode3(e) {
48294
48355
  return e.code;
48295
48356
  }
48296
48357
  function renameWithRetry(from, to, opts = {}) {
48297
- const rename = opts.rename ?? renameSync;
48358
+ const rename = opts.rename ?? renameSync2;
48298
48359
  const sleep2 = opts.sleep ?? sleepSync;
48299
48360
  const win = (opts.platform ?? process.platform) === "win32";
48300
48361
  for (let attempt = 0; ; attempt += 1) {
@@ -48302,7 +48363,7 @@ function renameWithRetry(from, to, opts = {}) {
48302
48363
  rename(from, to);
48303
48364
  return;
48304
48365
  } catch (e) {
48305
- const busy = win && ATOMIC_RENAME_BUSY_CODES.includes(errCode2(e) ?? "");
48366
+ const busy = win && ATOMIC_RENAME_BUSY_CODES.includes(errCode3(e) ?? "");
48306
48367
  if (!busy || attempt >= ATOMIC_RENAME_RETRIES) throw e;
48307
48368
  sleep2(ATOMIC_RENAME_DELAY_MS);
48308
48369
  }
@@ -48317,15 +48378,15 @@ function unlinkWithRetry(file2, opts = {}) {
48317
48378
  unlink(file2);
48318
48379
  return;
48319
48380
  } catch (e) {
48320
- if (errCode2(e) === "ENOENT") return;
48321
- const busy = win && ATOMIC_RENAME_BUSY_CODES.includes(errCode2(e) ?? "");
48381
+ if (errCode3(e) === "ENOENT") return;
48382
+ const busy = win && ATOMIC_RENAME_BUSY_CODES.includes(errCode3(e) ?? "");
48322
48383
  if (!busy || attempt >= ATOMIC_RENAME_RETRIES) throw replaceFailure(file2, e);
48323
48384
  sleep2(ATOMIC_RENAME_DELAY_MS);
48324
48385
  }
48325
48386
  }
48326
48387
  }
48327
48388
  function replaceFailure(file2, e) {
48328
- const busy = ATOMIC_RENAME_BUSY_CODES.includes(errCode2(e) ?? "");
48389
+ const busy = ATOMIC_RENAME_BUSY_CODES.includes(errCode3(e) ?? "");
48329
48390
  const cause = e.message;
48330
48391
  return new Error(busy ? `Could not replace ${file2}: ${cause}. Fix: close the program using ${file2}, then re-run` : `Could not write ${file2}: ${cause}. Fix: check the path and its permissions, then re-run`, { cause: e });
48331
48392
  }
@@ -50015,7 +50076,7 @@ function configPaths(row5, env, worktree) {
50015
50076
  }
50016
50077
 
50017
50078
  // src/core/ops/doctor-db.ts
50018
- import { existsSync as existsSync13, statSync as statSync4 } from "node:fs";
50079
+ import { existsSync as existsSync13, statSync as statSync5 } from "node:fs";
50019
50080
  var unknown5 = (id, detail) => ({ id, label: DOCTOR_LABEL[id], status: "UNKNOWN", detail });
50020
50081
  var pass3 = (id, detail) => ({ id, label: DOCTOR_LABEL[id], status: "PASS", detail });
50021
50082
  var fail3 = (detail, fixCommand) => ({ id: "D8", label: DOCTOR_LABEL.D8, status: "FAIL", detail, fixCommand });
@@ -50057,7 +50118,7 @@ function orphanFiles(dbPath) {
50057
50118
  const files = [];
50058
50119
  for (const suffix of [DB_WAL_SUFFIX, DB_SHM_SUFFIX]) {
50059
50120
  const path8 = dbPath + suffix;
50060
- if (existsSync13(path8)) files.push({ path: path8, bytes: statSync4(path8).size });
50121
+ if (existsSync13(path8)) files.push({ path: path8, bytes: statSync5(path8).size });
50061
50122
  }
50062
50123
  return files.length > 0 && !existsSync13(dbPath) ? files : [];
50063
50124
  }
@@ -51942,7 +52003,7 @@ import { spawn as spawn3, spawnSync as spawnSync2 } from "node:child_process";
51942
52003
 
51943
52004
  // src/setup/spawn-bin.ts
51944
52005
  import { spawnSync } from "node:child_process";
51945
- import { accessSync as accessSync2, constants as fsc, statSync as statSync5 } from "node:fs";
52006
+ import { accessSync as accessSync2, constants as fsc, statSync as statSync6 } from "node:fs";
51946
52007
  import path5 from "node:path";
51947
52008
  var WIN_EXT = ".COM;.EXE;.BAT;.CMD";
51948
52009
  var WIN_SHIM_EXTS = Object.freeze([".cmd", ".bat"]);
@@ -51960,7 +52021,7 @@ function whichBin(bin, env, platform = process.platform) {
51960
52021
  const f = path5.join(dir, n);
51961
52022
  try {
51962
52023
  accessSync2(f, fsc.X_OK);
51963
- if (statSync5(f).isFile()) return f;
52024
+ if (statSync6(f).isFile()) return f;
51964
52025
  } catch {
51965
52026
  }
51966
52027
  }
@@ -53287,11 +53348,11 @@ var ctxExecute = {
53287
53348
  };
53288
53349
 
53289
53350
  // src/core/tools/index.ts
53290
- import { existsSync as existsSync20, readFileSync as readFileSync17, statSync as statSync7 } from "node:fs";
53351
+ import { existsSync as existsSync20, readFileSync as readFileSync17, statSync as statSync8 } from "node:fs";
53291
53352
  import { dirname as dirname12, extname as extname3, join as join22, relative as relative3, resolve as resolve9 } from "node:path";
53292
53353
 
53293
53354
  // src/core/index/walk.ts
53294
- import { readdirSync as readdirSync9, readFileSync as readFileSync15, realpathSync as realpathSync10, statSync as statSync6 } from "node:fs";
53355
+ import { readdirSync as readdirSync9, readFileSync as readFileSync15, realpathSync as realpathSync10, statSync as statSync7 } from "node:fs";
53295
53356
  import { join as join20 } from "node:path";
53296
53357
  var GITIGNORE = ".gitignore";
53297
53358
  var GIT_DIR = ".git";
@@ -53325,7 +53386,7 @@ function visit(abs, rel, parent, contain, seen, found) {
53325
53386
  continue;
53326
53387
  }
53327
53388
  if (!isInside(contain, resolved)) continue;
53328
- const st = statSync6(resolved);
53389
+ const st = statSync7(resolved);
53329
53390
  isDir = st.isDirectory();
53330
53391
  isFile = st.isFile();
53331
53392
  target = resolved;
@@ -57513,7 +57574,7 @@ function pathItems(input, project) {
57513
57574
  const abs = resolve9(project.worktree, input.path);
57514
57575
  if (!isInside(project.containmentRoot, abs)) throw missingOrOutside(input.path, abs, project.containmentRoot);
57515
57576
  const base = relative3(project.worktree, abs);
57516
- if (statSync7(abs).isDirectory()) {
57577
+ if (statSync8(abs).isDirectory()) {
57517
57578
  return walkDir(abs, project.containmentRoot).map((f) => ({
57518
57579
  sourceKind: "path",
57519
57580
  label: pathLabel(join22(base, f.relPath)),