context-mode 2.2.2 → 2.2.3
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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.zcode-plugin/plugin.json +1 -1
- package/README.md +72 -72
- package/SHA256SUMS +16 -16
- package/cli.mjs +35 -16
- package/copilot/plugin.json +1 -1
- package/gemini-extension.json +1 -1
- package/hook.mjs +106 -99
- package/opencode/plugin.js +311 -69
- package/package.json +1 -1
- package/pi/index.js +373 -69
- package/qwen-extension.json +1 -1
- package/run.mjs +14 -12
- package/search.mjs +14 -12
- package/server.mjs +270 -42
package/opencode/plugin.js
CHANGED
|
@@ -23,14 +23,8 @@ var POINTER_MIN = 640;
|
|
|
23
23
|
var STATE_CLOSE_TAG = "</context_mode_state>";
|
|
24
24
|
var INDEX_WRITE_CHUNK = 1048576;
|
|
25
25
|
var SYNC_INDEX_MAX_BYTES = 65536;
|
|
26
|
-
var
|
|
26
|
+
var REPLACE_TRIGGER_BYTES = 4096;
|
|
27
27
|
var REPLACE_TRIGGER_ENV = "CTX_REPLACE_TRIGGER_BYTES";
|
|
28
|
-
function readReplaceTrigger(raw) {
|
|
29
|
-
if (raw === void 0 || raw.trim() === "") return REPLACE_TRIGGER_BYTES_DEFAULT;
|
|
30
|
-
const n = Number(raw);
|
|
31
|
-
return Number.isSafeInteger(n) && n > 0 ? n : REPLACE_TRIGGER_BYTES_DEFAULT;
|
|
32
|
-
}
|
|
33
|
-
var REPLACE_TRIGGER_BYTES = readReplaceTrigger(process.env[REPLACE_TRIGGER_ENV]);
|
|
34
28
|
var RANGE_READ_TOOLS = Object.freeze(["Read", "view"]);
|
|
35
29
|
var READ_RANGE_KEYS = Object.freeze(["offset", "limit", "view_range"]);
|
|
36
30
|
var SKILL_LOAD_TOOLS = Object.freeze(["Skill"]);
|
|
@@ -223,7 +217,7 @@ var HOOK_TIMEOUT_MS = 5e3;
|
|
|
223
217
|
var IN_PROCESS_TIMEOUT_MS = 0;
|
|
224
218
|
var MS_PER_DAY = 864e5;
|
|
225
219
|
var PROJECT_KEY_HEX_LEN = 16;
|
|
226
|
-
var SCHEMA_VERSION =
|
|
220
|
+
var SCHEMA_VERSION = 7;
|
|
227
221
|
var RETENTION_SIZE_CAP_BYTES = 524288e3;
|
|
228
222
|
var PROJECTS_DIR_NAME = "projects";
|
|
229
223
|
var DB_FILE_NAME = "context.db";
|
|
@@ -399,6 +393,8 @@ var RECALL_BUDGET_BYTES = 2048;
|
|
|
399
393
|
var RECALL_MIN_SCORE = 5;
|
|
400
394
|
var RECALL_MAX_ROWS = 340;
|
|
401
395
|
var COMPACTED_META_PREFIX = "compacted:";
|
|
396
|
+
var BET_META_PREFIX = "bet:";
|
|
397
|
+
var LOSTBET_META_PREFIX = "lostbet:";
|
|
402
398
|
var RECALL_UNITS_SEEN_META_PREFIX = "recall_units_seen:";
|
|
403
399
|
var UNITS_ANCESTOR_FACTOR = 8;
|
|
404
400
|
var RECALL_QUERY_MAX_TERMS = 64;
|
|
@@ -495,7 +491,7 @@ var DECISION_ANSWERS_KEY = "answers";
|
|
|
495
491
|
var DECISION_OPTION_KEY = "option";
|
|
496
492
|
var DECISION_LABEL_JOIN = " | ";
|
|
497
493
|
var RELEASE_REPO_URL = "https://github.com/mksglu/context-mode.git";
|
|
498
|
-
var BUNDLE_SHA = true ? "
|
|
494
|
+
var BUNDLE_SHA = true ? "f41c429ae530a3fec5bda3c12a8ecdc2a5f14ada" : void 0;
|
|
499
495
|
var DOCTOR_CHECK_ORDER = Object.freeze(["D1", "D2", "D3", "D4", "D5", "D5b", "D6", "D7", "D8", "D9", "D10", "D11"]);
|
|
500
496
|
var DOCTOR_LABEL = Object.freeze({
|
|
501
497
|
D1: "version",
|
|
@@ -935,6 +931,7 @@ var OMP_CONTENT_INFO = "OMP keeps tool output in content; context-mode writes it
|
|
|
935
931
|
var OMP_PRINT_MODE_INFO = "Print mode sends no prompt event. Use omp or omp --mode rpc to keep prompts.";
|
|
936
932
|
var OMP_SCRIPTS_INFO = "Add < /dev/null in scripts.";
|
|
937
933
|
var OMP_IMPORT_INFO = (claude, mcp) => `OMP also imports context-mode from ${claude}. To run it once, add "context-mode" to disabledServers in ${mcp}.`;
|
|
934
|
+
var REPLACE_TRIGGER_RETIRED_INFO = (where) => `${REPLACE_TRIGGER_ENV} in ${where} is no longer read: context-mode stores every tool output over ${REPLACE_TRIGGER_BYTES} B whole and ctx_search reads it back. You can remove that line from ${where}.`;
|
|
938
935
|
var PI_CURRENT_MAJOR = 2;
|
|
939
936
|
var V1_OPENCODE_PLUGIN_SPEC = "context-mode";
|
|
940
937
|
var V1_TMP_PREFIX = "context-mode-";
|
|
@@ -954,6 +951,10 @@ var OPENCODE_CONFIG_DIR_ENV = "OPENCODE_CONFIG_DIR";
|
|
|
954
951
|
var APPDATA_ENV = "APPDATA";
|
|
955
952
|
var VSCODE_USER_DIR_SEGMENTS = Object.freeze(["Code", "User"]);
|
|
956
953
|
var XDG_CONFIG_HOME_ENV = "XDG_CONFIG_HOME";
|
|
954
|
+
var XDG_DATA_HOME_ENV = "XDG_DATA_HOME";
|
|
955
|
+
var PI_CONFIG_DIR_ENV = "PI_CONFIG_DIR";
|
|
956
|
+
var OMP_APP_DIR = "omp";
|
|
957
|
+
var OMP_PLUGINS_DIR = "plugins";
|
|
957
958
|
var CONFIG_TOML_FILE = "config.toml";
|
|
958
959
|
var CONFIG_JSON_FILE = "config.json";
|
|
959
960
|
var ZCODE_CLI_DIR = "cli";
|
|
@@ -1235,7 +1236,8 @@ var ERROR_TEXT = Object.freeze({
|
|
|
1235
1236
|
NO_RUNTIME: "No <lang> runtime on PATH. Fix: install it or pick another language.",
|
|
1236
1237
|
FETCH_FAILED: "Fetch failed: <code>. Fix: check the URL or proxy.",
|
|
1237
1238
|
SSRF: "Blocked private address <host>. Fix: use a public URL.",
|
|
1238
|
-
OUTSIDE_ROOT: "Path <p> is outside the project. Fix: use a path inside <root
|
|
1239
|
+
OUTSIDE_ROOT: "Path <p> is outside the project and the folders added to this session. Fix: use a path inside <root>, or add its folder to the session (Claude Code /add-dir, Codex --add-dir).",
|
|
1240
|
+
// #1398 D-1398-HOST-ROOTS-1
|
|
1239
1241
|
INVALID_ARGS: "Invalid argument <field>. Fix: <action>.",
|
|
1240
1242
|
ONE_OF_INPUT: "Pass exactly one of <names>. Fix: drop the extra field.",
|
|
1241
1243
|
SPAWN_FAILED: "Failed to start the process: <detail>. Fix: check the command and PATH.",
|
|
@@ -3734,6 +3736,24 @@ function piAgentDirOf(env, home) {
|
|
|
3734
3736
|
const dir = env[PI_AGENT_DIR_ENV];
|
|
3735
3737
|
return dir !== void 0 && dir !== "" ? dir : join(home, ...AGENT_HOME_DIR.pi, PI_AGENT_DIR);
|
|
3736
3738
|
}
|
|
3739
|
+
function ompConfigRootOf(env, home) {
|
|
3740
|
+
const dir = env[PI_CONFIG_DIR_ENV];
|
|
3741
|
+
return dir !== void 0 && dir !== "" ? join(home, dir) : join(home, ...AGENT_HOME_DIR.omp);
|
|
3742
|
+
}
|
|
3743
|
+
function ompAgentDirOf(env, home) {
|
|
3744
|
+
return join(ompConfigRootOf(env, home), PI_AGENT_DIR);
|
|
3745
|
+
}
|
|
3746
|
+
function ompPluginsDirOf(env, home, platform = process.platform) {
|
|
3747
|
+
const root = ompConfigRootOf(env, home);
|
|
3748
|
+
const xdg = env[XDG_DATA_HOME_ENV];
|
|
3749
|
+
const agent = env[PI_AGENT_DIR_ENV];
|
|
3750
|
+
const defaultAgent = agent === void 0 || agent === "" || resolve2(agent) === join(root, PI_AGENT_DIR);
|
|
3751
|
+
if ((platform === "linux" || platform === "darwin") && defaultAgent && xdg !== void 0 && xdg !== "") {
|
|
3752
|
+
const app = join(xdg, OMP_APP_DIR);
|
|
3753
|
+
if (existsSync(app)) return join(app, OMP_PLUGINS_DIR);
|
|
3754
|
+
}
|
|
3755
|
+
return join(root, OMP_PLUGINS_DIR);
|
|
3756
|
+
}
|
|
3737
3757
|
function vscodeUserDirOf(env, home, platform = process.platform) {
|
|
3738
3758
|
const set3 = (v) => v !== void 0 && v !== "";
|
|
3739
3759
|
const p = platform === "win32" ? win32 : posix;
|
|
@@ -4285,7 +4305,7 @@ var TOOL_DESCRIPTIONS = Object.freeze({
|
|
|
4285
4305
|
ctx_execute: 'Example: `ctx_execute({language:"shell",code:"git log --oneline -5"})`. Required: language, code.\nWrite code that prints only the answer, not the raw data. Runs code in a sandboxed subprocess in the project directory (shell, powershell, javascript, typescript, python, or another runtime on PATH); only what the code prints comes back. For a large file, set `file` to its local path: the code gets it in the CTX_FILE environment variable. Set `intent` when output may be large: the matching sections come back and the full output is indexed for ctx_search. `timeoutMs` defaults to and caps at 600000; then the whole process tree is killed.',
|
|
4286
4306
|
ctx_batch: 'Example: `ctx_batch({commands:["git log --oneline -5"],queries:["recent changes"]})`. Required: commands, queries.\nRuns several shell commands, then answers several search queries over their combined output, in one call. Use it to collect facts from more than one command at once, for example test results plus recent commits, when you need specific answers rather than the full text. Returns the matching sections and a one-line footer.',
|
|
4287
4307
|
ctx_index: 'Example: `ctx_index({path:"docs/api.md"})`. Required: one of path, content, url.\nStores content so it can be searched later: a local file or folder (nested .gitignore files are respected), raw text, or a URL, which is fetched and converted to Markdown. Use it for documentation or large files you will query more than once, then use ctx_search instead of reading the content again. Returns the source label and how many chunks were stored.',
|
|
4288
|
-
ctx_search: 'Example: `ctx_search({queries:["auth middleware"]})`. Required: queries.\nSearches content stored with ctx_index and earlier sandbox output in this project. Memory files such as AGENTS.md are searched too. Pass several queries in one call. By default it searches the current worktree; set `scope` to `project` for all worktrees or `session` for this session only. Results from the current session rank first, then by relevance, ranked with measurably better recall than plain keyword search. Set `sort` to `timeline` for oldest-first order across this session, prior sessions and memory, paged with `after`; `offset` is for relevance order only. Set `source` to `decision` to search only rows where the user answered a question. Every hit names its source. When nothing matches, the reply states the scope and how many sources are indexed.',
|
|
4308
|
+
ctx_search: 'Example: `ctx_search({queries:["auth middleware"]})`. Required: queries.\nSearches content stored with ctx_index and earlier sandbox output in this project. Memory files such as AGENTS.md are searched too. Pass several queries in one call. By default it searches the current worktree; set `scope` to `project` for all worktrees or `session` for this session only. Results from the current session rank first, then by relevance, ranked with measurably better recall than plain keyword search. Set `sort` to `timeline` for oldest-first order across this session, prior sessions and memory, paged with `after`; `offset` is for relevance order only. Set `source` to `decision` to search only rows where the user answered a question. Every hit names its source. Set `source` with queries `[""]` to read that source whole, in order, a full page per call; `More:` gives the next `offset`. When nothing matches, the reply states the scope and how many sources are indexed.',
|
|
4289
4309
|
ctx_resume: "Example: `ctx_resume({})`. Required: none.\nReturns a summary of up to 2 KB for this session: the last task and user request, files changed, open errors, and search terms for ctx_search. A large row is shown as a pointer line instead of its full text; call `ctx_resume(seq: N)` with that line's number to read the full row. Call it with no `seq` to get the state block on demand. Use it after the conversation was compacted or resumed, before you repeat earlier work.",
|
|
4290
4310
|
ctx_stats: "Example: `ctx_stats({})`. Required: none.\nReports the bytes context-mode kept out of the chat: a text report (bars, per tool, per agent) and a Markdown table. Show the text block to the user unchanged, in a code block. Use it when the user asks what it saved.",
|
|
4291
4311
|
ctx_doctor: "Example: `ctx_doctor({})`. Required: none.\nChecks this context-mode install and reports a table with one row per check: version, Node and node:sqlite, host config and hooks for this agent, MCP connection, project and worktree, database health, index contents, and hook timing. Each failed row names its cause, the fix and a docs link. Use it when the user asks whether context-mode works, or after a context-mode error. Run each Fix first, then call ctx_doctor again. If a FAIL stays, show the user the report file it names (it holds paths and environment details) and ask before you run the printed gh issue command.\n",
|
|
@@ -4333,6 +4353,10 @@ function isInside(root, p) {
|
|
|
4333
4353
|
}
|
|
4334
4354
|
return true;
|
|
4335
4355
|
}
|
|
4356
|
+
function allowedRootOf(containmentRoot, hostRoots, p) {
|
|
4357
|
+
if (isInside(containmentRoot, p)) return containmentRoot;
|
|
4358
|
+
return hostRoots?.find((r) => isInside(r, p));
|
|
4359
|
+
}
|
|
4336
4360
|
var rootReal = /* @__PURE__ */ new Map();
|
|
4337
4361
|
function realRoot(root) {
|
|
4338
4362
|
const hit = rootReal.get(root);
|
|
@@ -5113,13 +5137,22 @@ CREATE VIRTUAL TABLE line_units USING fts5(
|
|
|
5113
5137
|
var DDL_V6 = `
|
|
5114
5138
|
UPDATE sources SET label = replace(label, char(92), '/') WHERE kind = 'path' AND instr(label, char(92)) > 0;
|
|
5115
5139
|
`;
|
|
5140
|
+
var DDL_V7 = `
|
|
5141
|
+
ALTER TABLE sources ADD COLUMN unit_lo INTEGER;
|
|
5142
|
+
ALTER TABLE sources ADD COLUMN unit_hi INTEGER;
|
|
5143
|
+
CREATE INDEX IF NOT EXISTS idx_sources_session ON sources(session_id, id);
|
|
5144
|
+
UPDATE sources SET unit_lo = g.lo, unit_hi = g.hi
|
|
5145
|
+
FROM (SELECT source_id, MIN(rowid) AS lo, MAX(rowid) AS hi FROM line_units GROUP BY source_id) AS g
|
|
5146
|
+
WHERE g.source_id = sources.id;
|
|
5147
|
+
`;
|
|
5116
5148
|
var MIGRATIONS = [
|
|
5117
5149
|
{ version: 1, sql: DDL },
|
|
5118
5150
|
{ version: 2, sql: DDL_V2 },
|
|
5119
5151
|
{ version: 3, sql: DDL_V3 },
|
|
5120
5152
|
{ version: 4, sql: DDL_V4 },
|
|
5121
5153
|
{ version: 5, sql: DDL_V5 },
|
|
5122
|
-
{ version: 6, sql: DDL_V6 }
|
|
5154
|
+
{ version: 6, sql: DDL_V6 },
|
|
5155
|
+
{ version: 7, sql: DDL_V7 }
|
|
5123
5156
|
];
|
|
5124
5157
|
function applyMigrations(db, fromVersion) {
|
|
5125
5158
|
if (fromVersion === SCHEMA_VERSION2) return;
|
|
@@ -5319,6 +5352,51 @@ function metaTake(db, key) {
|
|
|
5319
5352
|
});
|
|
5320
5353
|
}
|
|
5321
5354
|
|
|
5355
|
+
// src/core/store/lost-bet.ts
|
|
5356
|
+
function lostKey(sessionId, key) {
|
|
5357
|
+
return LOSTBET_META_PREFIX + sessionId + ":" + key;
|
|
5358
|
+
}
|
|
5359
|
+
function isLostBet(db, sessionId, key) {
|
|
5360
|
+
return sessionId !== "" && metaGet(db, lostKey(sessionId, key)) !== null;
|
|
5361
|
+
}
|
|
5362
|
+
function markLostBet(db, sessionId, key, reason, source) {
|
|
5363
|
+
if (sessionId === "" || key === "") return;
|
|
5364
|
+
try {
|
|
5365
|
+
metaSet(db, lostKey(sessionId, key), JSON.stringify({ at: Date.now(), reason, source }));
|
|
5366
|
+
} catch {
|
|
5367
|
+
}
|
|
5368
|
+
}
|
|
5369
|
+
function hasBet(db, sessionId, key) {
|
|
5370
|
+
return sessionId !== "" && metaGet(db, BET_META_PREFIX + sessionId + ":" + key) !== null;
|
|
5371
|
+
}
|
|
5372
|
+
function noteBet(db, sessionId, key, source) {
|
|
5373
|
+
if (sessionId === "" || key === "") return;
|
|
5374
|
+
try {
|
|
5375
|
+
metaSet(db, BET_META_PREFIX + sessionId + ":" + key, source);
|
|
5376
|
+
} catch {
|
|
5377
|
+
}
|
|
5378
|
+
}
|
|
5379
|
+
function clearLostBets(db, sessionId) {
|
|
5380
|
+
if (sessionId === "") return;
|
|
5381
|
+
const lo = LOSTBET_META_PREFIX + sessionId + ":";
|
|
5382
|
+
const hi = LOSTBET_META_PREFIX + sessionId + ";";
|
|
5383
|
+
const run = (h) => {
|
|
5384
|
+
h.raw.prepare("DELETE FROM meta WHERE key >= ? AND key < ?").run(lo, hi);
|
|
5385
|
+
};
|
|
5386
|
+
if (db.raw.isTransaction) run(db);
|
|
5387
|
+
else withWrite(db, run);
|
|
5388
|
+
}
|
|
5389
|
+
function betKeyOfLabel(label2) {
|
|
5390
|
+
if (label2.startsWith(SUBAGENT_SOURCE_PREFIX)) return null;
|
|
5391
|
+
const hash = label2.lastIndexOf("#");
|
|
5392
|
+
if (hash <= 0) return null;
|
|
5393
|
+
const stem = label2.slice(0, hash);
|
|
5394
|
+
const colon = stem.indexOf(":");
|
|
5395
|
+
if (colon <= 0) return null;
|
|
5396
|
+
if (colon + 1 < stem.length || stem.startsWith(MCP_SERVER_TOOL_PREFIX)) return stem;
|
|
5397
|
+
return null;
|
|
5398
|
+
}
|
|
5399
|
+
|
|
5322
5400
|
// src/core/journal/append.ts
|
|
5323
5401
|
var COLUMNS = "events(session_id, worktree, agent, kind, tool, key, path, ok, exit_code, bytes, body, turn, sandbox_bytes, returned_bytes, created_at, accessed_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
5324
5402
|
var INSERT_SQL = "INSERT INTO " + COLUMNS;
|
|
@@ -7462,11 +7540,13 @@ function deleteChunkRows(raw, scope) {
|
|
|
7462
7540
|
if (scope === "all") {
|
|
7463
7541
|
raw.exec("INSERT INTO chunks_tri(chunks_tri) VALUES ('delete-all')");
|
|
7464
7542
|
raw.exec("DELETE FROM line_units");
|
|
7543
|
+
raw.exec("UPDATE sources SET unit_lo = NULL, unit_hi = NULL WHERE unit_lo IS NOT NULL");
|
|
7465
7544
|
return Number(raw.prepare("DELETE FROM chunks").run().changes);
|
|
7466
7545
|
}
|
|
7467
|
-
const [where, arg] = "sourceId" in scope ? ["source_id = ?", scope.sourceId] : ["source_id IN (SELECT id FROM sources WHERE label = ?)", scope.label];
|
|
7546
|
+
const [where, arg, own2] = "sourceId" in scope ? ["source_id = ?", scope.sourceId, "id = ?"] : ["source_id IN (SELECT id FROM sources WHERE label = ?)", scope.label, "label = ?"];
|
|
7468
7547
|
raw.prepare(TRI_DELETE + " WHERE " + where).run(arg);
|
|
7469
7548
|
raw.prepare("DELETE FROM line_units WHERE " + where).run(arg);
|
|
7549
|
+
raw.prepare("UPDATE sources SET unit_lo = NULL, unit_hi = NULL WHERE " + own2).run(arg);
|
|
7470
7550
|
return Number(raw.prepare("DELETE FROM chunks WHERE " + where).run(arg).changes);
|
|
7471
7551
|
}
|
|
7472
7552
|
function toMatch(q) {
|
|
@@ -7983,10 +8063,15 @@ function insertUnits(db, sourceId, sessionId, units) {
|
|
|
7983
8063
|
const from = start;
|
|
7984
8064
|
withWrite(db, (h) => {
|
|
7985
8065
|
const ins = h.raw.prepare("INSERT INTO line_units(body, source_id, session_id, ord) VALUES (?, ?, ?, ?)");
|
|
8066
|
+
let lo = Number.MAX_SAFE_INTEGER;
|
|
8067
|
+
let hi = 0;
|
|
7986
8068
|
for (let i = from; i < end; i++) {
|
|
7987
8069
|
const u = units[i];
|
|
7988
|
-
ins.run(u.body, sourceId, sessionId, u.ord);
|
|
8070
|
+
const rid = Number(ins.run(u.body, sourceId, sessionId, u.ord).lastInsertRowid);
|
|
8071
|
+
if (rid < lo) lo = rid;
|
|
8072
|
+
if (rid > hi) hi = rid;
|
|
7989
8073
|
}
|
|
8074
|
+
h.raw.prepare("UPDATE sources SET unit_lo = min(coalesce(unit_lo, ?), ?), unit_hi = max(coalesce(unit_hi, ?), ?) WHERE id = ?").run(lo, lo, hi, hi, sourceId);
|
|
7990
8075
|
});
|
|
7991
8076
|
start = end;
|
|
7992
8077
|
}
|
|
@@ -8018,9 +8103,15 @@ function readMaxSourceId(db) {
|
|
|
8018
8103
|
function queryUnits(db, sessionId, q, limit, maxSourceId = Number.MAX_SAFE_INTEGER) {
|
|
8019
8104
|
const match = searchTerms(q).join(" OR ");
|
|
8020
8105
|
if (match === "" || sessionId === "") return [];
|
|
8021
|
-
const
|
|
8022
|
-
|
|
8023
|
-
|
|
8106
|
+
const span2 = stmt(
|
|
8107
|
+
db.raw,
|
|
8108
|
+
"SELECT MIN(unit_lo) AS lo, MAX(unit_hi) AS hi FROM sources WHERE session_id = ? AND id <= ? AND unit_lo IS NOT NULL"
|
|
8109
|
+
).get(sessionId, maxSourceId);
|
|
8110
|
+
if (span2 === void 0 || span2.lo === null || span2.hi === null) return [];
|
|
8111
|
+
const rows = stmt(
|
|
8112
|
+
db.raw,
|
|
8113
|
+
"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.rowid BETWEEN ? AND ? AND u.session_id = ? AND s.id <= ? ORDER BY bm25(line_units) LIMIT ?"
|
|
8114
|
+
).all(match, span2.lo, span2.hi, sessionId, maxSourceId, limit);
|
|
8024
8115
|
return rows.map((r) => ({ sourceId: Number(r.source_id), ord: Number(r.ord), label: r.label, body: r.body, score: r.score }));
|
|
8025
8116
|
}
|
|
8026
8117
|
|
|
@@ -8941,6 +9032,7 @@ function buildUnits(text) {
|
|
|
8941
9032
|
|
|
8942
9033
|
// src/core/hooks/spool.ts
|
|
8943
9034
|
var { mkdirSync: mkdirSync3, readdirSync: readdirSync3, readFileSync: readFileSync5, renameSync: renameSync2, statSync: statSync4, unlinkSync, utimesSync, writeFileSync } = process.getBuiltinModule("node:fs");
|
|
9035
|
+
var SEEN_TAG = ".seen";
|
|
8944
9036
|
var OP_SUFFIX = ".json";
|
|
8945
9037
|
var CLAIM_SUFFIX = ".claim";
|
|
8946
9038
|
var TMP_SUFFIX = ".tmp";
|
|
@@ -8969,13 +9061,21 @@ function spool(project, op) {
|
|
|
8969
9061
|
const name = String(Date.now()).padStart(NAME_TIME_DIGITS, "0") + "-" + String(process.pid) + "-" + String(nameSeq++).padStart(NAME_SEQ_DIGITS, "0");
|
|
8970
9062
|
const tmp = join7(dir, name + TMP_SUFFIX);
|
|
8971
9063
|
writeFileSync(tmp, JSON.stringify(op));
|
|
8972
|
-
renameSync2(tmp, join7(dir, name + OP_SUFFIX));
|
|
9064
|
+
renameSync2(tmp, join7(dir, name + (op.t === "seen" ? SEEN_TAG : "") + OP_SUFFIX));
|
|
8973
9065
|
}
|
|
8974
9066
|
function runOp(db, op, chunks, cur) {
|
|
8975
9067
|
if (op.t === "row") {
|
|
8976
9068
|
appendEvent(db, op.row, op.at);
|
|
8977
9069
|
return null;
|
|
8978
9070
|
}
|
|
9071
|
+
if (op.t === "seen") {
|
|
9072
|
+
withWrite(db, (h) => {
|
|
9073
|
+
const have = parseList(metaGet(h, op.key));
|
|
9074
|
+
const add = op.add.filter((v) => !have.includes(v));
|
|
9075
|
+
if (add.length > 0) metaSet(h, op.key, JSON.stringify([...have, ...add]));
|
|
9076
|
+
});
|
|
9077
|
+
return null;
|
|
9078
|
+
}
|
|
8979
9079
|
const c = chunks ?? chunkText(op.text, "text", op.max);
|
|
8980
9080
|
let id2 = op.id;
|
|
8981
9081
|
if (id2 === null) {
|
|
@@ -9004,12 +9104,47 @@ function runOp(db, op, chunks, cur) {
|
|
|
9004
9104
|
}
|
|
9005
9105
|
return id2;
|
|
9006
9106
|
}
|
|
9107
|
+
function parseList(value) {
|
|
9108
|
+
if (value === null) return [];
|
|
9109
|
+
try {
|
|
9110
|
+
const v = JSON.parse(value);
|
|
9111
|
+
return Array.isArray(v) ? v.filter((x) => typeof x === "number" || typeof x === "string") : [];
|
|
9112
|
+
} catch {
|
|
9113
|
+
return [];
|
|
9114
|
+
}
|
|
9115
|
+
}
|
|
9116
|
+
function readSeen(project, db, key) {
|
|
9117
|
+
const out = [];
|
|
9118
|
+
const dir = spoolDir(project);
|
|
9119
|
+
let names = [];
|
|
9120
|
+
try {
|
|
9121
|
+
names = readdirSync3(dir);
|
|
9122
|
+
} catch {
|
|
9123
|
+
}
|
|
9124
|
+
for (const name of names) {
|
|
9125
|
+
const base = name.endsWith(CLAIM_SUFFIX) ? name.slice(0, -CLAIM_SUFFIX.length) : name;
|
|
9126
|
+
if (!base.endsWith(SEEN_TAG + OP_SUFFIX)) continue;
|
|
9127
|
+
let op = null;
|
|
9128
|
+
for (const n of [name, base + CLAIM_SUFFIX, base]) {
|
|
9129
|
+
try {
|
|
9130
|
+
op = JSON.parse(readFileSync5(join7(dir, n), "utf8"));
|
|
9131
|
+
break;
|
|
9132
|
+
} catch {
|
|
9133
|
+
}
|
|
9134
|
+
}
|
|
9135
|
+
if (op !== null && op.t === "seen" && op.key === key) out.push(...op.add);
|
|
9136
|
+
}
|
|
9137
|
+
for (const v of parseList(metaGet(db, key))) if (!out.includes(v)) out.push(v);
|
|
9138
|
+
return out;
|
|
9139
|
+
}
|
|
9140
|
+
var busyUntil = 0;
|
|
9007
9141
|
function writeOrSpool(project, db, op, chunks) {
|
|
9008
9142
|
const cur = { op };
|
|
9009
9143
|
try {
|
|
9010
9144
|
return runOp(db, op, chunks, cur);
|
|
9011
9145
|
} catch (err) {
|
|
9012
9146
|
if (!isBusy(err)) throw err;
|
|
9147
|
+
busyUntil = Date.now() + HOOK_BUSY_MS;
|
|
9013
9148
|
spool(project, cur.op);
|
|
9014
9149
|
return cur.op.t === "src" ? cur.op.id : null;
|
|
9015
9150
|
}
|
|
@@ -9018,11 +9153,18 @@ function writeDetached(project, op, what, chunks) {
|
|
|
9018
9153
|
void (async () => {
|
|
9019
9154
|
try {
|
|
9020
9155
|
const db = await hookDb(project);
|
|
9021
|
-
if (
|
|
9022
|
-
|
|
9156
|
+
if (Date.now() < busyUntil) {
|
|
9157
|
+
spool(project, op);
|
|
9158
|
+
} else if (drain(project, db)) {
|
|
9159
|
+
busyUntil = Date.now() + HOOK_BUSY_MS;
|
|
9160
|
+
spool(project, op);
|
|
9161
|
+
} else {
|
|
9162
|
+
writeOrSpool(project, db, op, chunks);
|
|
9163
|
+
}
|
|
9023
9164
|
} catch (err) {
|
|
9024
9165
|
try {
|
|
9025
9166
|
if (!isBusy(err)) throw err;
|
|
9167
|
+
busyUntil = Date.now() + HOOK_BUSY_MS;
|
|
9026
9168
|
spool(project, op);
|
|
9027
9169
|
} catch (e) {
|
|
9028
9170
|
process.stderr.write("hooks: " + what + " write failed: " + message(e) + "\n");
|
|
@@ -9440,6 +9582,7 @@ function compactText(db, ev, row5) {
|
|
|
9440
9582
|
}
|
|
9441
9583
|
async function sessionStart(ev, row5, project) {
|
|
9442
9584
|
if (ev.source === "compact" && hasRecall(row5)) markCompacted(await hookDb(project), ev.sessionId);
|
|
9585
|
+
if (ev.source === "compact") endLostBets(await hookDb(project), ev.sessionId);
|
|
9443
9586
|
if (ev.source === "compact" && row5.compactDelivery === "sessionstart") {
|
|
9444
9587
|
const db2 = await hookDb(project);
|
|
9445
9588
|
const onceKey = COMPACT_STATE_KEY + ":" + lastSessionSeq(db2, ev.sessionId);
|
|
@@ -9508,24 +9651,6 @@ function compactBoundary(value) {
|
|
|
9508
9651
|
return all;
|
|
9509
9652
|
}
|
|
9510
9653
|
}
|
|
9511
|
-
function seenSeqs(value) {
|
|
9512
|
-
if (value === null) return [];
|
|
9513
|
-
try {
|
|
9514
|
-
const v = JSON.parse(value);
|
|
9515
|
-
return Array.isArray(v) ? v.filter((n) => typeof n === "number") : [];
|
|
9516
|
-
} catch {
|
|
9517
|
-
return [];
|
|
9518
|
-
}
|
|
9519
|
-
}
|
|
9520
|
-
function seenUnitKeys(value) {
|
|
9521
|
-
if (value === null) return [];
|
|
9522
|
-
try {
|
|
9523
|
-
const v = JSON.parse(value);
|
|
9524
|
-
return Array.isArray(v) ? v.filter((k) => typeof k === "string") : [];
|
|
9525
|
-
} catch {
|
|
9526
|
-
return [];
|
|
9527
|
-
}
|
|
9528
|
-
}
|
|
9529
9654
|
async function recall(ev, row5, project, prompt) {
|
|
9530
9655
|
if (!hasRecall(row5) || ev.sessionId === "") return ALLOW2;
|
|
9531
9656
|
const db = await hookDb(project);
|
|
@@ -9533,15 +9658,15 @@ async function recall(ev, row5, project, prompt) {
|
|
|
9533
9658
|
if (compacted === null) return ALLOW2;
|
|
9534
9659
|
const boundary = compactBoundary(compacted);
|
|
9535
9660
|
const seenKey = RECALL_SEEN_META_PREFIX + ev.sessionId;
|
|
9536
|
-
const seen =
|
|
9661
|
+
const seen = readSeen(project, db, seenKey).filter((n) => typeof n === "number");
|
|
9537
9662
|
const unitsKey = RECALL_UNITS_SEEN_META_PREFIX + ev.sessionId;
|
|
9538
|
-
const seenUnits =
|
|
9663
|
+
const seenUnits = readSeen(project, db, unitsKey).filter((k) => typeof k === "string");
|
|
9539
9664
|
const units = queryUnits(db, ev.sessionId, recallQueryText(prompt), RECALL_UNIT_CANDIDATES, boundary.source);
|
|
9540
9665
|
const rows = readRecallRows(db, ev.sessionId, project.worktree, RECALL_MAX_ROWS, boundary.seq);
|
|
9541
9666
|
const picked = pickRecall(rows, prompt, new Set(seen), units, new Set(seenUnits));
|
|
9542
9667
|
if (picked.text === "") return ALLOW2;
|
|
9543
|
-
if (picked.seqs.length > 0)
|
|
9544
|
-
if (picked.units !== void 0)
|
|
9668
|
+
if (picked.seqs.length > 0) writeDetached(project, { t: "seen", key: seenKey, add: picked.seqs }, "recall");
|
|
9669
|
+
if (picked.units !== void 0) writeDetached(project, { t: "seen", key: unitsKey, add: picked.units }, "recall");
|
|
9545
9670
|
return { kind: "hint", text: picked.text, onceKey: RECALL_KEY };
|
|
9546
9671
|
}
|
|
9547
9672
|
function optionsKey(sessionId) {
|
|
@@ -9724,8 +9849,11 @@ function ownRead(ev, tool, env) {
|
|
|
9724
9849
|
return p !== void 0 && isOwnFile(ev.agent, ev.cwd, p, env);
|
|
9725
9850
|
}
|
|
9726
9851
|
async function postReplace(ev, row5, project, env, tool, result, text, persistedFile) {
|
|
9852
|
+
if (boundedRead(tool)) {
|
|
9853
|
+
await rangedAfterBet(ev, row5, project, tool);
|
|
9854
|
+
return ALLOW2;
|
|
9855
|
+
}
|
|
9727
9856
|
if (result.bytes <= REPLACE_TRIGGER_BYTES) return ALLOW2;
|
|
9728
|
-
if (boundedRead(tool)) return ALLOW2;
|
|
9729
9857
|
if (ompRangedRead(ev, tool)) return ALLOW2;
|
|
9730
9858
|
if (ownRead(ev, tool, env)) return ALLOW2;
|
|
9731
9859
|
if (SKILL_LOAD_TOOLS.includes(tool.canonicalName)) return ALLOW2;
|
|
@@ -9752,6 +9880,8 @@ async function postReplace(ev, row5, project, env, tool, result, text, persisted
|
|
|
9752
9880
|
const intent = [tool.input.description, tool.input.command, tool.input.file_path, tool.input.pattern, tool.input.url, tool.name].map(str2).find((v) => v.trim() !== "") ?? tool.canonicalName;
|
|
9753
9881
|
const chunks = chunkText(text, "text", REPLACE_CHUNK_BYTES);
|
|
9754
9882
|
const db = await hookDb(project);
|
|
9883
|
+
const betOn = betKey(ev, tool);
|
|
9884
|
+
if (result.bytes <= OUTPUT_CAP && betOn !== "" && isLostBet(db, ev.sessionId, betOn)) return ALLOW2;
|
|
9755
9885
|
const op = { t: "src", id: null, src: sourceRowOf(project, ev.sessionId, label2), text, max: REPLACE_CHUNK_BYTES, units: true };
|
|
9756
9886
|
let sourceId;
|
|
9757
9887
|
if (result.bytes <= SYNC_INDEX_MAX_BYTES) {
|
|
@@ -9762,6 +9892,7 @@ async function postReplace(ev, row5, project, env, tool, result, text, persisted
|
|
|
9762
9892
|
}
|
|
9763
9893
|
if (pluginMcpCall(tool)) return ALLOW2;
|
|
9764
9894
|
const handle = sourceId === null ? label2 : sourceHandle(sourceId);
|
|
9895
|
+
if (RANGE_READ_TOOLS.includes(tool.canonicalName) && betOn !== "" && !hasBet(db, ev.sessionId, betOn)) noteBet(db, ev.sessionId, betOn, handle);
|
|
9765
9896
|
const prompt = await lastPrompt(project, ev.sessionId);
|
|
9766
9897
|
const rankQuery = prompt.trim() === "" ? void 0 : intent + " " + prompt;
|
|
9767
9898
|
const handleLine = caller.ctx ? REPLACE_HANDLE_LINE(intent, handle, result.bytes, row5.toolNamePrefix, askedWords(intent, prompt, text)) : SUBAGENT_HANDLE_LINE(handle, result.bytes, caller.read);
|
|
@@ -9772,6 +9903,23 @@ async function postReplace(ev, row5, project, env, tool, result, text, persisted
|
|
|
9772
9903
|
const selected = selectChunksInMemory(chunks, intent, budget, label2, rankQuery);
|
|
9773
9904
|
return { kind: "replace", output: selected.more !== null ? replaceOutput(selected.text, pointer) : selected.text };
|
|
9774
9905
|
}
|
|
9906
|
+
function betKey(ev, tool) {
|
|
9907
|
+
if (RANGE_READ_TOOLS.includes(tool.canonicalName)) {
|
|
9908
|
+
const p = READ_PATH_KEYS.map((k) => tool.input[k]).find((v) => typeof v === "string" && v !== "");
|
|
9909
|
+
return p === void 0 ? "" : tool.canonicalName + ":" + resolve8(ev.cwd, p);
|
|
9910
|
+
}
|
|
9911
|
+
return betKeyOfLabel(mainLabel(tool, ev)) ?? "";
|
|
9912
|
+
}
|
|
9913
|
+
async function rangedAfterBet(ev, row5, project, tool) {
|
|
9914
|
+
if (ev.sessionId === "" || !hasReplace(row5)) return;
|
|
9915
|
+
const key = betKey(ev, tool);
|
|
9916
|
+
if (key === "") return;
|
|
9917
|
+
try {
|
|
9918
|
+
const db = await hookDb(project);
|
|
9919
|
+
if (hasBet(db, ev.sessionId, key) && !isLostBet(db, ev.sessionId, key)) markLostBet(db, ev.sessionId, key, "ranged-read", tool.callId ?? "");
|
|
9920
|
+
} catch {
|
|
9921
|
+
}
|
|
9922
|
+
}
|
|
9775
9923
|
function pluginMcpCall(tool) {
|
|
9776
9924
|
return tool.callId !== void 0 && tool.callId.startsWith(PLUGIN_CALL_ID_PREFIX) && tool.name.startsWith(MCP_SERVER_TOOL_PREFIX);
|
|
9777
9925
|
}
|
|
@@ -9889,8 +10037,15 @@ async function preCompact(ev, row5, project) {
|
|
|
9889
10037
|
const db = await hookDb(project);
|
|
9890
10038
|
metaSet(db, PENDING_COMPACT_META_PREFIX + ev.sessionId, PENDING_COMPACT_FLAG);
|
|
9891
10039
|
if (hasRecall(row5)) markCompacted(db, ev.sessionId);
|
|
10040
|
+
endLostBets(db, ev.sessionId);
|
|
9892
10041
|
return ALLOW2;
|
|
9893
10042
|
}
|
|
10043
|
+
function endLostBets(db, sessionId) {
|
|
10044
|
+
try {
|
|
10045
|
+
clearLostBets(db, sessionId);
|
|
10046
|
+
} catch {
|
|
10047
|
+
}
|
|
10048
|
+
}
|
|
9894
10049
|
function usage(ev, project) {
|
|
9895
10050
|
appendDetached(project, ev.sessionId, {
|
|
9896
10051
|
session_id: ev.sessionId,
|
|
@@ -21477,9 +21632,9 @@ function unknownSource(db, scope, ctx, given) {
|
|
|
21477
21632
|
return line + "\nStored sources, newest first: " + names.join(", ") + more + ". " + callText([], scope, void 0, 'sort:"timeline"') + " lists them all.";
|
|
21478
21633
|
}
|
|
21479
21634
|
function named(given, pick2) {
|
|
21480
|
-
if (given === void 0 || pick2 === null) return { arg: given, note: null, pick: null };
|
|
21635
|
+
if (given === void 0 || pick2 === null) return { arg: given, note: null, pick: null, lost: null };
|
|
21481
21636
|
const arg = pick2.id !== null && isLongLabel(given) ? sourceHandle(pick2.id) : given;
|
|
21482
|
-
return { arg, note: collisionLine(given, pick2), pick: pick2 };
|
|
21637
|
+
return { arg, note: collisionLine(given, pick2), pick: pick2, lost: null };
|
|
21483
21638
|
}
|
|
21484
21639
|
function sourceCard(db, id2, input, scope, arg) {
|
|
21485
21640
|
const row5 = sourceCardRow(db, id2);
|
|
@@ -21503,7 +21658,7 @@ function decisionQueries(input) {
|
|
|
21503
21658
|
function pageSize(input) {
|
|
21504
21659
|
return input.source === DECISION_SOURCE ? SEARCH_PAGE_SIZE : SEARCH_REPLY_HITS;
|
|
21505
21660
|
}
|
|
21506
|
-
function page(db, q, input, scope, ctx, offset, extra = 0) {
|
|
21661
|
+
function page(db, q, input, scope, ctx, offset, extra = 0, h9 = { n: 0, list: true }) {
|
|
21507
21662
|
const size = pageSize(input) + extra;
|
|
21508
21663
|
if (input.source === DECISION_SOURCE && q.trim() === "") {
|
|
21509
21664
|
return ftsListSource(db, DECISION_SOURCE, scope, ctx.project, ctx.sessionId, size, offset);
|
|
@@ -21512,6 +21667,8 @@ function page(db, q, input, scope, ctx, offset, extra = 0) {
|
|
|
21512
21667
|
if (hits.length > 0 || input.source === void 0) return hits;
|
|
21513
21668
|
const any2 = offset === 0 ? hits : ftsQuery(db, q, scope, ctx.project, ctx.sessionId, 1, 0, input.source);
|
|
21514
21669
|
if (any2.length > 0) return hits;
|
|
21670
|
+
h9.n++;
|
|
21671
|
+
if (!h9.list) return [];
|
|
21515
21672
|
return ftsListSource(db, input.source, scope, ctx.project, ctx.sessionId, size, offset);
|
|
21516
21673
|
}
|
|
21517
21674
|
var JSON_ROWS_PAGE = 5;
|
|
@@ -21600,15 +21757,27 @@ function relevance(db, given, scope, ctx, nm, ms, indexed) {
|
|
|
21600
21757
|
const size = pageSize(input);
|
|
21601
21758
|
const mem = ms.named || offset === 0 ? memoryChunks(ms.rows) : [];
|
|
21602
21759
|
const count2 = (hits, n) => ms.named ? Math.min(n, hits.length) : contentIn(hits, n);
|
|
21603
|
-
const
|
|
21760
|
+
const h9 = { n: 0, list: false };
|
|
21761
|
+
const listed = [];
|
|
21762
|
+
const fetched = input.queries.map((q, qi) => {
|
|
21604
21763
|
if (ms.named) {
|
|
21605
21764
|
const want = offset + size + 1;
|
|
21606
21765
|
const content2 = indexed ? ftsQuery(db, q, scope, ctx.project, ctx.sessionId, want, 0, input.source) : [];
|
|
21607
21766
|
return mergeMemory(content2, memoryHits(mem, q, want)).slice(offset, want);
|
|
21608
21767
|
}
|
|
21609
|
-
const
|
|
21768
|
+
const before = h9.n;
|
|
21769
|
+
const content = page(db, q, input, scope, ctx, offset, 1, h9);
|
|
21770
|
+
if (h9.n > before) listed.push(qi);
|
|
21610
21771
|
return mem.length === 0 ? content : mergeMemory(content, memoryHits(mem, q, MEMORY_HITS));
|
|
21611
21772
|
});
|
|
21773
|
+
if (listed.length > 0 && listed.length === input.queries.length) {
|
|
21774
|
+
const ordered = orderedRead(db, input, scope, ctx, nm);
|
|
21775
|
+
if (ordered !== null) return ordered;
|
|
21776
|
+
}
|
|
21777
|
+
for (const qi of listed) {
|
|
21778
|
+
const content = ftsListSource(db, input.source, scope, ctx.project, ctx.sessionId, size + 1, offset);
|
|
21779
|
+
fetched[qi] = mem.length === 0 ? content : mergeMemory(content, memoryHits(mem, input.queries[qi], MEMORY_HITS));
|
|
21780
|
+
}
|
|
21612
21781
|
const take = fetched.map((h) => replyHits(h, size));
|
|
21613
21782
|
const pages = fetched.map((h, i) => h.slice(0, take[i]));
|
|
21614
21783
|
if (pages.every((p) => p.length === 0)) return zeroHits(db, scope, ctx, input.source, ms, nm, input.queries, offset);
|
|
@@ -21673,6 +21842,49 @@ function relevance(db, given, scope, ctx, nm, ms, indexed) {
|
|
|
21673
21842
|
for (const [pos, qs] of byPos) moreLines.push("More: " + callText(qs, scope, nm.arg, "offset:" + pos));
|
|
21674
21843
|
return reply(lines.concat(moreLines).join("\n"), false);
|
|
21675
21844
|
}
|
|
21845
|
+
var ORDERED_LINE_BACK = 512;
|
|
21846
|
+
var SOURCE_IN_SCOPE = `
|
|
21847
|
+
SELECT session_id FROM sources WHERE id = :id
|
|
21848
|
+
AND (:scope <> 'session' OR session_id = :session_id)
|
|
21849
|
+
AND (:scope <> 'worktree' OR worktree = :worktree)`;
|
|
21850
|
+
function sourceText(db, id2) {
|
|
21851
|
+
const rows = db.raw.prepare("SELECT body FROM chunks WHERE source_id = ? ORDER BY CAST(ord AS INTEGER)").all(id2);
|
|
21852
|
+
const parts = [];
|
|
21853
|
+
for (let i = 0; i < rows.length; i++) {
|
|
21854
|
+
const body = rows[i].body;
|
|
21855
|
+
parts.push(body);
|
|
21856
|
+
if (i + 1 < rows.length && !body.endsWith("\n") && bytes2(body) < MAX_CHUNK_BYTES - 3) parts.push("\n\n");
|
|
21857
|
+
}
|
|
21858
|
+
return parts.join("");
|
|
21859
|
+
}
|
|
21860
|
+
function orderedRead(db, input, scope, ctx, nm) {
|
|
21861
|
+
const pick2 = nm.pick;
|
|
21862
|
+
if (pick2 === null || pick2.id === null || input.source === void 0 || input.source === DECISION_SOURCE) return null;
|
|
21863
|
+
const where = { id: pick2.id, scope, session_id: ctx.sessionId, worktree: ctx.project.worktree };
|
|
21864
|
+
const row5 = db.raw.prepare(SOURCE_IN_SCOPE).get(where);
|
|
21865
|
+
if (row5 === void 0) return null;
|
|
21866
|
+
const buf = Buffer.from(sourceText(db, pick2.id), "utf8");
|
|
21867
|
+
const total = buf.length;
|
|
21868
|
+
if (total === 0) return null;
|
|
21869
|
+
const name = isLongLabel(pick2.label) ? shownLabel(pick2.label, pick2.id) : JSON.stringify(pick2.label);
|
|
21870
|
+
let start = Math.min(input.offset ?? 0, total);
|
|
21871
|
+
while (start < total && (buf[start] & 192) === 128) start++;
|
|
21872
|
+
if (start >= total) return reply("No more text past offset " + start + " in source " + name + " (" + total + " B).", false);
|
|
21873
|
+
const note = nm.note === null ? "" : " " + nm.note;
|
|
21874
|
+
const headOf2 = (end2) => "Source " + name + " in order, bytes " + start + "-" + end2 + " of " + total + ":" + note;
|
|
21875
|
+
const moreOf = (end2) => "More: " + callText([""], scope, nm.arg, "offset:" + end2);
|
|
21876
|
+
let end = total;
|
|
21877
|
+
if (bytes2(headOf2(total)) + 1 + (total - start) > OUTPUT_CAP) {
|
|
21878
|
+
end = start + OUTPUT_CAP - bytes2(headOf2(total)) - 1 - bytes2(moreOf(total)) - 1;
|
|
21879
|
+
while (end > start && (buf[end] & 192) === 128) end--;
|
|
21880
|
+
const nl = buf.lastIndexOf(10, end - 1);
|
|
21881
|
+
if (nl >= start && nl + 1 > end - ORDERED_LINE_BACK) end = nl + 1;
|
|
21882
|
+
}
|
|
21883
|
+
const text = buf.subarray(start, end).toString("utf8");
|
|
21884
|
+
const key = end * 2 >= total ? betKeyOfLabel(pick2.label) : null;
|
|
21885
|
+
if (key !== null && !isLostBet(db, row5.session_id, key)) nm.lost = { sessionId: row5.session_id, key, source: sourceHandle(pick2.id) };
|
|
21886
|
+
return reply(headOf2(end) + "\n" + text + (end < total ? "\n" + moreOf(end) : ""), false);
|
|
21887
|
+
}
|
|
21676
21888
|
function timelineLine(r) {
|
|
21677
21889
|
const nl = r.text.indexOf("\n");
|
|
21678
21890
|
const first = nl < 0 ? r.text : r.text.slice(0, nl);
|
|
@@ -21733,6 +21945,7 @@ async function handler(input, ctx) {
|
|
|
21733
21945
|
}
|
|
21734
21946
|
const scope = input.scope ?? "worktree";
|
|
21735
21947
|
const db = await openDb(ctx.project, { readOnly: true });
|
|
21948
|
+
let lost = null;
|
|
21736
21949
|
try {
|
|
21737
21950
|
const given = input.source;
|
|
21738
21951
|
const within = { scope, sessionId: ctx.sessionId, worktree: ctx.project.worktree };
|
|
@@ -21748,11 +21961,24 @@ async function handler(input, ctx) {
|
|
|
21748
21961
|
nm.note = nm.note === null ? both : nm.note + "\n" + both;
|
|
21749
21962
|
}
|
|
21750
21963
|
const out = answer(db, input, scope, ctx, nm, ms, indexed, sort, pick2);
|
|
21964
|
+
lost = nm.lost;
|
|
21751
21965
|
if (file2 === null || scope !== "session") return out;
|
|
21752
21966
|
const call = callText(input.queries, "worktree", nm.arg, sort === "timeline" ? 'sort:"timeline"' : "");
|
|
21753
21967
|
return reply(out.text + "\nMemory file " + file2.label + ' is in scope "worktree": ' + call + " searches it.", out.isError);
|
|
21754
21968
|
} finally {
|
|
21755
21969
|
closeDb(db);
|
|
21970
|
+
if (lost !== null) await recordLostBet(ctx, lost);
|
|
21971
|
+
}
|
|
21972
|
+
}
|
|
21973
|
+
async function recordLostBet(ctx, lost) {
|
|
21974
|
+
try {
|
|
21975
|
+
const w = await openDb(ctx.project);
|
|
21976
|
+
try {
|
|
21977
|
+
markLostBet(w, lost.sessionId, lost.key, "ordered-read", lost.source);
|
|
21978
|
+
} finally {
|
|
21979
|
+
closeDb(w);
|
|
21980
|
+
}
|
|
21981
|
+
} catch {
|
|
21756
21982
|
}
|
|
21757
21983
|
}
|
|
21758
21984
|
var ctxSearch = Object.freeze({
|
|
@@ -25327,8 +25553,8 @@ var ENABLED_PROVIDERS = "enabledProviders";
|
|
|
25327
25553
|
var DISABLED_PROVIDERS = "disabledProviders";
|
|
25328
25554
|
var CLAUDE_PROVIDER = "claude";
|
|
25329
25555
|
var ALL_PROVIDERS = ["*", "all"];
|
|
25330
|
-
function ompImportFiles(home) {
|
|
25331
|
-
const agentDir =
|
|
25556
|
+
function ompImportFiles(env, home) {
|
|
25557
|
+
const agentDir = ompAgentDirOf(env, home);
|
|
25332
25558
|
return { config: join16(agentDir, OMP_CONFIG_FILE), mcp: join16(agentDir, OMP_MCP_FILE), claude: join16(home, CLAUDE_USER_CONFIG_FILE) };
|
|
25333
25559
|
}
|
|
25334
25560
|
function readText2(file2) {
|
|
@@ -25711,7 +25937,7 @@ function checkHostConfig(row5, env, worktree) {
|
|
|
25711
25937
|
if ("installs" in choice) return fail3("D3", D3_ROOT_DETAIL(choice.installs.map((r) => r.installPath)), D3_ROOT_FIX(choice.installs));
|
|
25712
25938
|
}
|
|
25713
25939
|
if (row5.id === "pi") return checkPiInstall(env, home, worktree);
|
|
25714
|
-
if (row5.id === "omp") return checkOmpInstall(home);
|
|
25940
|
+
if (row5.id === "omp") return checkOmpInstall(env, home);
|
|
25715
25941
|
const parts = [...mcpWrong(row5, home, env, worktree) ? [PART_MCP] : [], ...hookParts(row5, home, env, worktree)];
|
|
25716
25942
|
if (parts.length === 0) return pass2("D3");
|
|
25717
25943
|
const detail = D3_DETAIL(parts);
|
|
@@ -25723,7 +25949,6 @@ function checkPiInstall(env, home, worktree) {
|
|
|
25723
25949
|
if (found !== void 0) return { ...pass2("D3"), detail: found };
|
|
25724
25950
|
return fail3("D3", D3_DETAIL([PART_MCP]) + D4_REASON_JOIN + join17(piAgentDirOf(env, home), SETTINGS_FILE), NATIVE_INSTALL.pi[0]);
|
|
25725
25951
|
}
|
|
25726
|
-
var OMP_PLUGINS_DIR = "plugins";
|
|
25727
25952
|
var OMP_LOCK_FILE = "omp-plugins.lock.json";
|
|
25728
25953
|
function readJson2(file2) {
|
|
25729
25954
|
try {
|
|
@@ -25737,8 +25962,8 @@ function ownEntry(doc, key) {
|
|
|
25737
25962
|
const map2 = doc?.[key];
|
|
25738
25963
|
return typeof map2 === "object" && map2 !== null ? map2[SERVER_KEY] : void 0;
|
|
25739
25964
|
}
|
|
25740
|
-
function checkOmpInstall(home) {
|
|
25741
|
-
const dir =
|
|
25965
|
+
function checkOmpInstall(env, home) {
|
|
25966
|
+
const dir = ompPluginsDirOf(env, home);
|
|
25742
25967
|
const lock = join17(dir, OMP_LOCK_FILE);
|
|
25743
25968
|
const pkg = join17(dir, PI_PACKAGE_JSON_FILE);
|
|
25744
25969
|
const entry = ownEntry(readJson2(lock), "plugins");
|
|
@@ -25754,12 +25979,26 @@ function ompInfoLines(env, version2) {
|
|
|
25754
25979
|
out.push(OMP_CONTENT_INFO, OMP_PRINT_MODE_INFO, OMP_SCRIPTS_INFO);
|
|
25755
25980
|
const home = homeDirOf(env);
|
|
25756
25981
|
if (home === null) return out;
|
|
25757
|
-
const files = ompImportFiles(home);
|
|
25982
|
+
const files = ompImportFiles(env, home);
|
|
25758
25983
|
if (!ompImportsUs(files, env)) return out;
|
|
25759
25984
|
const listed = readJson2(files.mcp)?.[OMP_DISABLED_SERVERS_KEY];
|
|
25760
25985
|
if (!(Array.isArray(listed) && listed.includes(SERVER_KEY))) out.push(OMP_IMPORT_INFO(files.claude, files.mcp));
|
|
25761
25986
|
return out;
|
|
25762
25987
|
}
|
|
25988
|
+
function replaceTriggerInfoLines(agent, env, project) {
|
|
25989
|
+
const files = [];
|
|
25990
|
+
const home = homeDirOf(env);
|
|
25991
|
+
if (agent === "claude-code") {
|
|
25992
|
+
if (home !== null) files.push(join17(claudeDirOf(env, home), SETTINGS_FILE));
|
|
25993
|
+
if (project !== void 0 && project !== "") files.push(join17(project, HOST_CONFIG_DIR_NAME, SETTINGS_FILE));
|
|
25994
|
+
}
|
|
25995
|
+
const where = [...new Set(files)].filter((f) => {
|
|
25996
|
+
const block = readJson2(f)?.env;
|
|
25997
|
+
return typeof block === "object" && block !== null && REPLACE_TRIGGER_ENV in block;
|
|
25998
|
+
});
|
|
25999
|
+
if (where.length > 0) return [REPLACE_TRIGGER_RETIRED_INFO(where.join(" and "))];
|
|
26000
|
+
return env[REPLACE_TRIGGER_ENV] === void 0 ? [] : [REPLACE_TRIGGER_RETIRED_INFO("the process env")];
|
|
26001
|
+
}
|
|
25763
26002
|
function checkV1Leftovers(agent, home, scope = {}) {
|
|
25764
26003
|
const findings = detectV1(agent, home, scope);
|
|
25765
26004
|
if (findings.length === 0) return pass2("D4");
|
|
@@ -28834,10 +29073,12 @@ async function doctorReply(ctx, host) {
|
|
|
28834
29073
|
const look = rows.find((r) => r.status === "FAIL") ?? rows.find((r) => r.status !== "PASS");
|
|
28835
29074
|
const docs = look === void 0 ? [] : [DOCTOR_DOCS_LABEL + DOCTOR_DOCS_LINK(look.id)];
|
|
28836
29075
|
const info = adapter.id === "omp" ? ompInfoLines(host.env, ctx.clientInfo?.version) : [];
|
|
29076
|
+
const known = projectOrMissing(ctx);
|
|
29077
|
+
info.push(...replaceTriggerInfoLines(adapter.id, host.env, "missing" in known ? host.env.CLAUDE_PROJECT_DIR : known.project.worktree));
|
|
28837
29078
|
const whole2 = [envLine2, table3, ...docs, ...info, ...notes].join("\n");
|
|
28838
29079
|
const bytes5 = Buffer.byteLength(whole2, "utf8");
|
|
28839
29080
|
if (bytes5 <= REPORT_CAP) return { text: whole2, isError: false, sandboxBytes: bytes5 };
|
|
28840
|
-
const p =
|
|
29081
|
+
const p = known;
|
|
28841
29082
|
if ("missing" in p) return { text: whole2, isError: false, sandboxBytes: bytes5 };
|
|
28842
29083
|
const db = await openDb(p.project);
|
|
28843
29084
|
try {
|
|
@@ -28949,11 +29190,11 @@ function schemaLine(issue2) {
|
|
|
28949
29190
|
if (issue2.code === "unrecognized_keys") return new CtxError("UNKNOWN_FIELD", { f: issue2.keys[0] }).toLine();
|
|
28950
29191
|
return issue2.message;
|
|
28951
29192
|
}
|
|
28952
|
-
function fileError(given, abs, root) {
|
|
29193
|
+
function fileError(given, abs, root, hostRoots) {
|
|
28953
29194
|
let dir = abs;
|
|
28954
29195
|
while (!existsSync18(dir) && dirname12(dir) !== dir) dir = dirname12(dir);
|
|
28955
|
-
if (
|
|
28956
|
-
return new CtxError("OUTSIDE_ROOT", { p: given, root }).toLine();
|
|
29196
|
+
if (allowedRootOf(root, hostRoots, dir) !== void 0) return null;
|
|
29197
|
+
return { ...fail5(new CtxError("OUTSIDE_ROOT", { p: given, root }).toLine()), outsideRoot: true };
|
|
28957
29198
|
}
|
|
28958
29199
|
function takesDashC(language, runtime) {
|
|
28959
29200
|
if (language === "shell") return true;
|
|
@@ -29016,8 +29257,8 @@ async function handler3(raw, ctx) {
|
|
|
29016
29257
|
const extra = { ...PLAIN_OUTPUT_ENV2 };
|
|
29017
29258
|
if (input.file !== void 0) {
|
|
29018
29259
|
const abs = resolve11(cwd, input.file);
|
|
29019
|
-
const bad = fileError(input.file, abs, ctx.project.containmentRoot);
|
|
29020
|
-
if (bad !== null) return
|
|
29260
|
+
const bad = fileError(input.file, abs, ctx.project.containmentRoot, ctx.hostRoots);
|
|
29261
|
+
if (bad !== null) return bad;
|
|
29021
29262
|
extra.CTX_FILE = abs;
|
|
29022
29263
|
}
|
|
29023
29264
|
let runtime;
|
|
@@ -33334,18 +33575,19 @@ function readUtf8(absPath) {
|
|
|
33334
33575
|
return void 0;
|
|
33335
33576
|
}
|
|
33336
33577
|
}
|
|
33337
|
-
function missingOrOutside(given, abs, root) {
|
|
33578
|
+
function missingOrOutside(given, abs, root, hostRoots) {
|
|
33338
33579
|
let dir = abs;
|
|
33339
33580
|
while (!existsSync19(dir) && dirname13(dir) !== dir) dir = dirname13(dir);
|
|
33340
|
-
if (dir !== abs &&
|
|
33581
|
+
if (dir !== abs && allowedRootOf(root, hostRoots, dir) !== void 0) return new CtxError("INVALID_ARGS", { field: "path", action: "pass an existing file or folder" });
|
|
33341
33582
|
return new CtxError("OUTSIDE_ROOT", { p: given, root });
|
|
33342
33583
|
}
|
|
33343
|
-
function pathItems(input, project) {
|
|
33584
|
+
function pathItems(input, project, hostRoots) {
|
|
33344
33585
|
const abs = resolve12(project.worktree, input.path);
|
|
33345
|
-
|
|
33586
|
+
const allowed = allowedRootOf(project.containmentRoot, hostRoots, abs);
|
|
33587
|
+
if (allowed === void 0) throw missingOrOutside(input.path, abs, project.containmentRoot, hostRoots);
|
|
33346
33588
|
const base = relative5(project.worktree, abs);
|
|
33347
33589
|
if (statSync10(abs).isDirectory()) {
|
|
33348
|
-
return walkDir(abs,
|
|
33590
|
+
return walkDir(abs, allowed).map((f) => ({
|
|
33349
33591
|
sourceKind: "path",
|
|
33350
33592
|
label: pathLabel(join25(base, f.relPath)),
|
|
33351
33593
|
kind: kindOfPath(f.relPath),
|
|
@@ -33362,7 +33604,7 @@ async function itemsOf(input, ctx) {
|
|
|
33362
33604
|
return { items: [{ sourceKind: "content", label: label3, kind, read: () => text2 }], from: label3 };
|
|
33363
33605
|
}
|
|
33364
33606
|
if (input.path !== void 0) {
|
|
33365
|
-
const items = pathItems({ ...input, path: input.path }, ctx.project);
|
|
33607
|
+
const items = pathItems({ ...input, path: input.path }, ctx.project, ctx.hostRoots);
|
|
33366
33608
|
return { items, from: items.length === 1 ? items[0].label : pathLabel(input.path) };
|
|
33367
33609
|
}
|
|
33368
33610
|
const res = await safeFetch(input.url ?? "", AbortSignal.any([ctx.signal, AbortSignal.timeout(EXEC_TIMEOUT_MS)]));
|
|
@@ -33429,7 +33671,7 @@ async function handler4(input, ctx) {
|
|
|
33429
33671
|
journal: { kind: "index", key: from, path: from, exit_code: null }
|
|
33430
33672
|
};
|
|
33431
33673
|
} catch (err) {
|
|
33432
|
-
if (err instanceof CtxError) return { text: err.toLine(), isError: true, sandboxBytes: 0 };
|
|
33674
|
+
if (err instanceof CtxError) return { text: err.toLine(), isError: true, sandboxBytes: 0, ...err.code === "OUTSIDE_ROOT" ? { outsideRoot: true } : {} };
|
|
33433
33675
|
throw err;
|
|
33434
33676
|
}
|
|
33435
33677
|
}
|
|
@@ -34059,7 +34301,7 @@ function ownEntries2(agent, env) {
|
|
|
34059
34301
|
}
|
|
34060
34302
|
if (agent === "omp") {
|
|
34061
34303
|
return [{
|
|
34062
|
-
file: ompImportFiles(home).mcp,
|
|
34304
|
+
file: ompImportFiles(env, home).mcp,
|
|
34063
34305
|
key: OMP_DISABLED_SERVERS_KEY,
|
|
34064
34306
|
shown: OMP_DISABLED_SERVERS_KEY,
|
|
34065
34307
|
has: (v) => Array.isArray(v) && v.includes(SERVER_KEY),
|