@cortexkit/pi-magic-context 0.44.4 → 0.45.0

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.
Files changed (40) hide show
  1. package/dist/embedding-worker.js +47 -0
  2. package/dist/emscripten-module-0g8f45e6.js +602 -0
  3. package/dist/emscripten-module-0pekdjxz.js +1532 -0
  4. package/dist/emscripten-module-e67xwsp3.js +503 -0
  5. package/dist/emscripten-module-pj1vyb44.js +4559 -0
  6. package/dist/ffi-3cx1cxpk.js +78 -0
  7. package/dist/ffi-7bqqe4cd.js +91 -0
  8. package/dist/ffi-7e6gryj5.js +91 -0
  9. package/dist/ffi-bpvkebwr.js +78 -0
  10. package/dist/historian-calibration-extension.js +7 -4
  11. package/dist/{index-5am7amy4.js → index-2bw9k8nx.js} +7848 -23072
  12. package/dist/index-2pbbk3en.js +410 -0
  13. package/dist/index-4d3cba70.js +10 -0
  14. package/dist/{index-59dmj4zs.js → index-7n8gb6j3.js} +165 -3
  15. package/dist/{index-97e5fzy3.js → index-7s5yk3za.js} +1 -1
  16. package/dist/index-901ekbga.js +244 -0
  17. package/dist/index-e16q5sd7.js +240 -0
  18. package/dist/index-g0xfx1c2.js +275 -0
  19. package/dist/{module-asyncify-2EFITU5U-rkqjhjxd.js → index-g20jdh0p.js} +114 -105
  20. package/dist/index-j0mbc5vj.js +359 -0
  21. package/dist/index-k0yd1ph9.js +7774 -0
  22. package/dist/{index-5fw91enm.js → index-n3g5mwa8.js} +1 -1
  23. package/dist/index-q1yc6bs0.js +104 -0
  24. package/dist/index-ttf0cqq8.js +8938 -0
  25. package/dist/{index-npagr1cy.js → index-vxjgsr9m.js} +736 -904
  26. package/dist/{index-n8kg008j.js → index-w76x3af7.js} +28 -18
  27. package/dist/index.js +3983 -1670
  28. package/dist/{message-time-backfill-5yrp95cx.js → message-time-backfill-g83dkh8m.js} +19 -4
  29. package/dist/migration-worker.js +51 -0
  30. package/dist/module-ES6BEMUI-3cet1pyp.js +12 -0
  31. package/dist/module-asyncify-2EFITU5U-c2razzd9.js +6 -0
  32. package/dist/{read-session-chunk-w493f72c.js → read-session-chunk-kmt7a6zj.js} +32 -5
  33. package/dist/rpc-notifications-gm1mdpwh.js +16 -0
  34. package/dist/{safe-notification-target-y3hg2m7p.js → safe-notification-target-6cn3z99t.js} +2 -2
  35. package/dist/subagent-entry.js +15 -6
  36. package/package.json +4 -4
  37. package/dist/index-b3eqj1g6.js +0 -51
  38. package/dist/index-rn7gafjr.js +0 -8
  39. package/dist/index-v6t0tg85.js +0 -13
  40. package/dist/rpc-notifications-jxy30tgz.js +0 -8
@@ -1,8 +1,39 @@
1
+ import {
2
+ OMO_INTERNAL_INITIATOR_MARKER,
3
+ isSystemDirective,
4
+ hasMeaningfulUserText2,
5
+ extractTexts2,
6
+ extractToolResultBodyTokens,
7
+ extractToolCallSummaries,
8
+ estimateTokens,
9
+ normalizeText,
10
+ compactRole,
11
+ formatBlock,
12
+ compactTextForSummary,
13
+ mergeCommitHashes
14
+ } from "./index-j0mbc5vj.js";
1
15
  import {
2
16
  getHarness,
3
17
  harnessOwnsOpenCodeStore,
4
18
  log
5
- } from "./index-59dmj4zs.js";
19
+ } from "./index-7n8gb6j3.js";
20
+ import {
21
+ canonicalModelIdentity,
22
+ renderToolTemplate
23
+ } from "./index-g0xfx1c2.js";
24
+ import {
25
+ stableStringify,
26
+ resolveOpenCodeDbPath,
27
+ assertOpenCodeStoreGeneration,
28
+ openCodeDbPathExists,
29
+ recordOpenCodeDbReadFailure,
30
+ clearOpenCodeDbReadFailure,
31
+ claimOpenCodeDbDiagnosticOnce,
32
+ closeQuietly
33
+ } from "./index-e16q5sd7.js";
34
+ import {
35
+ Database
36
+ } from "./index-2pbbk3en.js";
6
37
 
7
38
  // ../plugin/src/features/magic-context/reclaim-protection.ts
8
39
  var CTX_REDUCE_KEEP = 3;
@@ -10,337 +41,6 @@ function newestCtxReduceTagNumbers(tags) {
10
41
  return new Set(tags.filter((tag) => tag.toolName === "ctx_reduce").sort((left, right) => right.tagNumber - left.tagNumber).slice(0, CTX_REDUCE_KEEP).map((tag) => tag.tagNumber));
11
42
  }
12
43
 
13
- // ../plugin/src/hooks/magic-context/read-session-formatting.ts
14
- import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
15
- import { createRequire } from "node:module";
16
- import { homedir } from "node:os";
17
- import { dirname, join, resolve } from "node:path";
18
- import { pathToFileURL } from "node:url";
19
-
20
- // ../plugin/src/shared/commit-detection.ts
21
- var HASH_HEX = "[0-9a-f]{7,12}";
22
- var COMMIT_HASH_TEST_PATTERN = new RegExp(`\\b${HASH_HEX}\\b`, "i");
23
- var COMMIT_VERB_PATTERN = /\b(?:commit(?:ted|ting|s)?|cherry-?pick(?:ed|ing|s)?|merge[ds]?|merging|rebas(?:e|ed|es|ing))\b/i;
24
- function textMentionsRecentCommit(text) {
25
- return COMMIT_HASH_TEST_PATTERN.test(text) && COMMIT_VERB_PATTERN.test(text);
26
- }
27
- function createCommitHashExtractPattern() {
28
- return new RegExp(`\`?\\b(${HASH_HEX})\\b\`?`, "gi");
29
- }
30
-
31
- // ../plugin/src/shared/internal-initiator-marker.ts
32
- var OMO_INTERNAL_INITIATOR_MARKER = "<!-- OMO_INTERNAL_INITIATOR -->";
33
-
34
- // ../plugin/src/shared/system-directive.ts
35
- var SYSTEM_DIRECTIVE_PREFIX = "[SYSTEM DIRECTIVE: MAGIC-CONTEXT";
36
- function isSystemDirective(text) {
37
- return text.trimStart().startsWith(SYSTEM_DIRECTIVE_PREFIX);
38
- }
39
- function removeSystemReminders(text) {
40
- return text.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/gi, "").trim();
41
- }
42
-
43
- // ../plugin/src/hooks/magic-context/read-session-formatting.ts
44
- var MAX_COMMITS_PER_BLOCK = 5;
45
- function hasMeaningfulUserText2(parts) {
46
- for (const part of parts) {
47
- if (part === null || typeof part !== "object")
48
- continue;
49
- const candidate = part;
50
- if (candidate.type !== "text" || typeof candidate.text !== "string")
51
- continue;
52
- if (candidate.ignored === true)
53
- continue;
54
- const cleaned = removeSystemReminders(candidate.text).replace(OMO_INTERNAL_INITIATOR_MARKER, "").trim();
55
- if (!cleaned)
56
- continue;
57
- if (isSystemDirective(cleaned))
58
- continue;
59
- return true;
60
- }
61
- return false;
62
- }
63
- function extractTexts2(parts) {
64
- const texts = [];
65
- for (const part of parts) {
66
- if (part === null || typeof part !== "object")
67
- continue;
68
- const p = part;
69
- if (p.type === "text" && typeof p.text === "string" && p.text.trim().length > 0) {
70
- texts.push(p.text.trim());
71
- }
72
- }
73
- return texts;
74
- }
75
- function extractToolResultBodyTokens(parts) {
76
- let tokens = 0;
77
- for (const part of parts) {
78
- if (part === null || typeof part !== "object")
79
- continue;
80
- const p = part;
81
- if (p.type !== "tool")
82
- continue;
83
- const state = p.state;
84
- if (!state || typeof state !== "object")
85
- continue;
86
- const body = state.output ?? state.error;
87
- if (body === undefined)
88
- continue;
89
- const text = typeof body === "string" ? body : JSON.stringify(body);
90
- tokens += Math.ceil(text.length / 4);
91
- }
92
- return tokens;
93
- }
94
- function extractToolCallSummaries(parts) {
95
- const summaries = [];
96
- for (const part of parts) {
97
- if (part === null || typeof part !== "object")
98
- continue;
99
- const p = part;
100
- if (p.type !== "tool" || typeof p.tool !== "string")
101
- continue;
102
- const state = p.state;
103
- if (!state || typeof state !== "object")
104
- continue;
105
- const input = state.input;
106
- const metadata = state.metadata;
107
- const description = input && typeof input.description === "string" && input.description || metadata && typeof metadata.description === "string" && metadata.description;
108
- if (description) {
109
- summaries.push(`TC: ${description}`);
110
- continue;
111
- }
112
- const toolName = p.tool;
113
- const keyArg = extractKeyArg(toolName, input);
114
- summaries.push(keyArg ? `TC: ${toolName}(${keyArg})` : `TC: ${toolName}`);
115
- }
116
- return summaries;
117
- }
118
- function extractKeyArg(_toolName, input) {
119
- if (!input)
120
- return null;
121
- if (typeof input.filePath === "string")
122
- return truncateArg(input.filePath);
123
- if (typeof input.path === "string")
124
- return truncateArg(input.path);
125
- if (typeof input.pattern === "string")
126
- return truncateArg(input.pattern);
127
- if (typeof input.query === "string")
128
- return truncateArg(input.query);
129
- if (typeof input.symbol === "string")
130
- return input.symbol;
131
- if (typeof input.module === "string")
132
- return input.module;
133
- if (typeof input.action === "string")
134
- return input.action;
135
- return null;
136
- }
137
- function truncateArg(value, maxLen = 60) {
138
- if (value.length <= maxLen)
139
- return value;
140
- return `${value.slice(0, maxLen)}…`;
141
- }
142
- var TOKENIZER_PACKAGE_DIRS = [
143
- ["@cortexkit", "opencode-magic-context"],
144
- ["@cortexkit", "pi-magic-context"]
145
- ];
146
- var tokenizer;
147
- var tokenizerLoadAttempted = false;
148
- var tokenizerLoadPromise;
149
- var tokenizerWarningSent = false;
150
- var tokenizerEncodingPath;
151
- var tokenizerSerializedTableBytes;
152
- function tokenizerPackageRoots() {
153
- const cwd = process.cwd();
154
- const openCodeCache = join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "opencode");
155
- const roots = [cwd, openCodeCache];
156
- const candidates = [];
157
- for (const root of roots) {
158
- for (const packageDir of TOKENIZER_PACKAGE_DIRS) {
159
- candidates.push(join(root, "node_modules", ...packageDir, "node_modules", "ai-tokenizer"));
160
- }
161
- candidates.push(join(root, "node_modules", "ai-tokenizer"));
162
- }
163
- let ancestor = process.argv[1] ? dirname(resolve(process.argv[1])) : cwd;
164
- while (true) {
165
- candidates.push(join(ancestor, "node_modules", "ai-tokenizer"));
166
- const parent = dirname(ancestor);
167
- if (parent === ancestor)
168
- break;
169
- ancestor = parent;
170
- }
171
- return [...new Set(candidates)];
172
- }
173
- function packageImportTarget(value) {
174
- if (typeof value === "string")
175
- return value;
176
- if (!value || typeof value !== "object")
177
- return;
178
- const conditions = value;
179
- return packageImportTarget(conditions.import) ?? packageImportTarget(conditions.default);
180
- }
181
- function findTokenizerImportPaths() {
182
- for (const packageRoot of tokenizerPackageRoots()) {
183
- const packageJsonPath = join(packageRoot, "package.json");
184
- if (!existsSync(packageJsonPath))
185
- continue;
186
- try {
187
- const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
188
- const tokenizerTarget = packageImportTarget(packageJson.exports?.["."]) ?? (typeof packageJson.module === "string" ? packageJson.module : undefined) ?? (typeof packageJson.main === "string" ? packageJson.main : undefined);
189
- const encodingTarget = packageImportTarget(packageJson.exports?.["./encoding/claude"]);
190
- if (!tokenizerTarget || !encodingTarget)
191
- continue;
192
- return {
193
- tokenizerPath: realpathSync(join(packageRoot, tokenizerTarget)),
194
- encodingPath: realpathSync(join(packageRoot, encodingTarget))
195
- };
196
- } catch {}
197
- }
198
- return;
199
- }
200
- function constructTokenizer(tokenizerModule, claudeEncoding) {
201
- const typedModule = tokenizerModule;
202
- const Tokenizer = typedModule.default ?? typedModule.Tokenizer;
203
- if (!Tokenizer) {
204
- throw new Error("ai-tokenizer does not expose a Tokenizer constructor");
205
- }
206
- return new Tokenizer(claudeEncoding);
207
- }
208
- function loadTokenizer() {
209
- const requireFromThisModule = createRequire(import.meta.url);
210
- const encodingSpecifier = "ai-tokenizer/encoding/" + "claude";
211
- tokenizerEncodingPath = requireFromThisModule.resolve(encodingSpecifier);
212
- tokenizerSerializedTableBytes = undefined;
213
- return constructTokenizer(requireFromThisModule("ai-" + "tokenizer"), requireFromThisModule(encodingSpecifier));
214
- }
215
- async function loadTokenizerFromInstalledPackage() {
216
- const installedPaths = findTokenizerImportPaths();
217
- if (!installedPaths) {
218
- throw new Error("ai-tokenizer was not found under the project, runtime, or OpenCode cache node_modules roots");
219
- }
220
- const [tokenizerModule, claudeEncoding] = await Promise.all([
221
- import(pathToFileURL(installedPaths.tokenizerPath).href),
222
- import(pathToFileURL(installedPaths.encodingPath).href)
223
- ]);
224
- tokenizerEncodingPath = installedPaths.encodingPath;
225
- tokenizerSerializedTableBytes = undefined;
226
- return constructTokenizer(tokenizerModule, claudeEncoding);
227
- }
228
- function warnTokenizerFallback(error) {
229
- if (tokenizerWarningSent)
230
- return;
231
- tokenizerWarningSent = true;
232
- const reason = error instanceof Error ? error.message : String(error);
233
- console.warn("[magic-context] ai-tokenizer is unavailable; using approximate character-based token counts for this process. Token budgets, persisted per-message counts, and protected-tail/compartment boundaries may be less accurate until restart:", reason);
234
- }
235
- async function preloadTokenizer() {
236
- if (tokenizer)
237
- return true;
238
- if (tokenizerLoadAttempted)
239
- return false;
240
- if (tokenizerLoadPromise)
241
- return tokenizerLoadPromise;
242
- tokenizerLoadPromise = (async () => {
243
- try {
244
- try {
245
- tokenizer = loadTokenizer();
246
- } catch {
247
- tokenizer = await loadTokenizerFromInstalledPackage();
248
- }
249
- tokenizerLoadAttempted = true;
250
- return true;
251
- } catch (error) {
252
- tokenizerLoadAttempted = true;
253
- warnTokenizerFallback(error);
254
- return false;
255
- } finally {
256
- tokenizerLoadPromise = undefined;
257
- }
258
- })();
259
- return tokenizerLoadPromise;
260
- }
261
- function getTokenizer() {
262
- if (tokenizer || tokenizerLoadAttempted)
263
- return tokenizer;
264
- tokenizerLoadAttempted = true;
265
- try {
266
- tokenizer = loadTokenizer();
267
- } catch (error) {
268
- warnTokenizerFallback(error);
269
- }
270
- return tokenizer;
271
- }
272
- function estimateTokensHeuristically(text) {
273
- return Math.ceil(text.length / 3.5);
274
- }
275
- function estimateTokens(text) {
276
- if (!text)
277
- return 0;
278
- const activeTokenizer = getTokenizer();
279
- if (!activeTokenizer)
280
- return estimateTokensHeuristically(text);
281
- try {
282
- return activeTokenizer.encode(text, "all").length;
283
- } catch (error) {
284
- tokenizer = undefined;
285
- tokenizerLoadAttempted = true;
286
- warnTokenizerFallback(error);
287
- return estimateTokensHeuristically(text);
288
- }
289
- }
290
- function normalizeText(text) {
291
- return text.replace(/\s+/g, " ").trim();
292
- }
293
- function compactRole(role) {
294
- if (role === "assistant")
295
- return "A";
296
- if (role === "user")
297
- return "U";
298
- return role.slice(0, 1).toUpperCase() || "M";
299
- }
300
- function formatBlock(block) {
301
- const range = block.startOrdinal === block.endOrdinal ? `[${block.startOrdinal}]` : `[${block.startOrdinal}-${block.endOrdinal}]`;
302
- const commitSuffix = block.commitHashes.length > 0 ? ` commits: ${block.commitHashes.join(", ")}` : "";
303
- return `${range} ${block.role}:${commitSuffix} ${block.parts.join(" / ")}`;
304
- }
305
- function extractCommitHashes(text) {
306
- const hashes = [];
307
- const seen = new Set;
308
- for (const match of text.matchAll(createCommitHashExtractPattern())) {
309
- const hash = match[1]?.toLowerCase();
310
- if (!hash || seen.has(hash))
311
- continue;
312
- seen.add(hash);
313
- hashes.push(hash);
314
- if (hashes.length >= MAX_COMMITS_PER_BLOCK)
315
- break;
316
- }
317
- return hashes;
318
- }
319
- function compactTextForSummary(text, role) {
320
- const commitHashes = role === "assistant" ? extractCommitHashes(text) : [];
321
- if (commitHashes.length === 0 || !COMMIT_VERB_PATTERN.test(text)) {
322
- return { text, commitHashes };
323
- }
324
- const withoutHashes = text.replace(createCommitHashExtractPattern(), "").replace(/\(\s*\)/g, "").replace(/\s+,/g, ",").replace(/,\s*,+/g, ", ").replace(/\s{2,}/g, " ").replace(/\s+([,.;:])/g, "$1").trim();
325
- return {
326
- text: withoutHashes.length > 0 ? withoutHashes : text,
327
- commitHashes
328
- };
329
- }
330
- function mergeCommitHashes(existing, next) {
331
- if (next.length === 0)
332
- return existing;
333
- const merged = [...existing];
334
- for (const hash of next) {
335
- if (merged.includes(hash))
336
- continue;
337
- merged.push(hash);
338
- if (merged.length >= MAX_COMMITS_PER_BLOCK)
339
- break;
340
- }
341
- return merged;
342
- }
343
-
344
44
  // ../plugin/src/hooks/magic-context/tag-content-primitives.ts
345
45
  var encoder = new TextEncoder;
346
46
  var TAG_PREFIX_REGEX = /^(?:§\d+§\s*)+/;
@@ -404,26 +104,18 @@ function isRecord(value) {
404
104
  return typeof value === "object" && value !== null && !Array.isArray(value);
405
105
  }
406
106
 
407
- // ../plugin/src/shared/stable-json.ts
408
- function stableStringify(value, seen = new WeakSet) {
409
- if (value === undefined)
410
- return "undefined";
411
- if (value === null || typeof value !== "object")
412
- return JSON.stringify(value) ?? String(value);
413
- if (seen.has(value))
414
- return '"[Circular]"';
415
- seen.add(value);
416
- if (Array.isArray(value)) {
417
- return `[${value.map((item) => stableStringify(item, seen)).join(",")}]`;
418
- }
419
- const entries = Object.entries(value).sort(([a], [b]) => {
420
- if (a < b)
421
- return -1;
422
- if (a > b)
423
- return 1;
424
- return 0;
425
- });
426
- return `{${entries.map(([key, child]) => `${JSON.stringify(key)}:${stableStringify(child, seen)}`).join(",")}}`;
107
+ // ../plugin/src/shared/tag-owner-id.ts
108
+ var TEXT_TAG_IDENTITY_MARKER = ":mc-text-v1:";
109
+ var POSITIONAL_CONTENT_SUFFIX = /:(?:p|file)\d+$/;
110
+ var CONTENT_DERIVED_TEXT_SUFFIX = /:mc-text-v1:[0-9a-f]+:[0-9a-f]+:o\d+$/;
111
+ function contentTagOwnerMessageId(contentId) {
112
+ const positional = POSITIONAL_CONTENT_SUFFIX.exec(contentId);
113
+ if (positional)
114
+ return contentId.slice(0, positional.index);
115
+ const derived = CONTENT_DERIVED_TEXT_SUFFIX.exec(contentId);
116
+ if (derived)
117
+ return contentId.slice(0, derived.index);
118
+ return contentId;
427
119
  }
428
120
 
429
121
  // ../plugin/src/hooks/magic-context/image-token-estimate.ts
@@ -449,7 +141,7 @@ function estimateImageTokensFromDataUrl(url) {
449
141
  if (dims)
450
142
  return clampImageTokens(Math.ceil(dims.w * dims.h / IMAGE_TOKEN_DIVISOR));
451
143
  } else if (header.includes("image/jpeg") || header.includes("image/jpg")) {
452
- const dims = parseJpegDimensions(bytes);
144
+ const dims = parseJpegDimensions(createBase64ByteReader(payload, bytes));
453
145
  if (dims)
454
146
  return clampImageTokens(Math.ceil(dims.w * dims.h / IMAGE_TOKEN_DIVISOR));
455
147
  } else if (header.includes("image/webp")) {
@@ -490,21 +182,46 @@ function parsePngDimensions(b) {
490
182
  return null;
491
183
  return { w, h };
492
184
  }
493
- function parseJpegDimensions(b) {
494
- if (b.length < 4 || b[0] !== 255 || b[1] !== 216)
185
+ var READER_PREVIEW_CHARS = 512;
186
+ var READER_PREVIEW_BYTES = 384;
187
+ var READER_WINDOW_CHARS = 4096;
188
+ var READER_WINDOW_BYTES = 3072;
189
+ function createBase64ByteReader(payload, preview) {
190
+ const windows = new Map;
191
+ return (offset) => {
192
+ if (offset < READER_PREVIEW_BYTES)
193
+ return preview[offset];
194
+ const index = Math.floor((offset - READER_PREVIEW_BYTES) / READER_WINDOW_BYTES);
195
+ let window = windows.get(index);
196
+ if (window === undefined) {
197
+ const start = READER_PREVIEW_CHARS + index * READER_WINDOW_CHARS;
198
+ const chars = payload.slice(start, start + READER_WINDOW_CHARS);
199
+ try {
200
+ window = chars.length > 0 ? base64Decode(chars) : null;
201
+ } catch {
202
+ window = null;
203
+ }
204
+ windows.set(index, window);
205
+ }
206
+ return window?.[offset - READER_PREVIEW_BYTES - index * READER_WINDOW_BYTES];
207
+ };
208
+ }
209
+ var JPEG_SCAN_LIMIT_BYTES = 256 * 1024;
210
+ function parseJpegDimensions(byteAt) {
211
+ if (byteAt(0) !== 255 || byteAt(1) !== 216 || byteAt(3) === undefined)
495
212
  return null;
496
213
  let i = 2;
497
- while (i < b.length - 8) {
498
- if (b[i] !== 255) {
214
+ while (i < JPEG_SCAN_LIMIT_BYTES && byteAt(i + 8) !== undefined) {
215
+ if (byteAt(i) !== 255) {
499
216
  i++;
500
217
  continue;
501
218
  }
502
- const marker = b[i + 1];
219
+ const marker = byteAt(i + 1);
503
220
  if (marker === undefined)
504
221
  break;
505
222
  if (isSofMarker(marker)) {
506
- const h = b[i + 5] << 8 | b[i + 6];
507
- const w = b[i + 7] << 8 | b[i + 8];
223
+ const h = byteAt(i + 5) << 8 | byteAt(i + 6);
224
+ const w = byteAt(i + 7) << 8 | byteAt(i + 8);
508
225
  if (w && h)
509
226
  return { w, h };
510
227
  return null;
@@ -513,7 +230,7 @@ function parseJpegDimensions(b) {
513
230
  i += 2;
514
231
  continue;
515
232
  }
516
- const segLen = b[i + 2] << 8 | b[i + 3];
233
+ const segLen = byteAt(i + 2) << 8 | byteAt(i + 3);
517
234
  if (segLen < 2)
518
235
  return null;
519
236
  i += 2 + segLen;
@@ -1047,6 +764,7 @@ ${body}${hintText}
1047
764
  var TARGET_FRACTION = 0.3;
1048
765
  var TIER_RECENCY_RESERVE = 0.2;
1049
766
  var EMERGENCY_REARM_MIN_TOKENS = 2000;
767
+ var EMERGENCY_MIN_ACHIEVABLE_RECLAIM_TOKENS = EMERGENCY_REARM_MIN_TOKENS;
1050
768
  var T1_TOOLS = new Set(["read", "todowrite", "task", "aft_outline", "aft_zoom"]);
1051
769
  var T2_TOOLS = new Set(["edit", "write", "apply_patch", "grep", "glob", "aft_search"]);
1052
770
  function normalizeToolName(toolName) {
@@ -1108,8 +826,9 @@ function planEmergencyDrop(input) {
1108
826
  const workingSpan = Math.max(ceilingTokens - fixedFloor, 0);
1109
827
  const target = fixedFloor + TARGET_FRACTION * workingSpan;
1110
828
  const reclaimTokens = Math.round(currentTotalInputTokens - target);
829
+ const floorNote = fixedFloor > ceilingTokens ? `; fixed floor ≈${Math.round(fixedFloor)} already exceeds ceiling ${Math.round(ceilingTokens)}: tool drops cannot reach the target` : "";
1111
830
  if (reclaimTokens <= EMERGENCY_REARM_MIN_TOKENS) {
1112
- return noop(`reclaim<=min (${reclaimTokens} <= ${EMERGENCY_REARM_MIN_TOKENS})`);
831
+ return noop(`reclaim<=min (${reclaimTokens} <= ${EMERGENCY_REARM_MIN_TOKENS})${floorNote}`);
1113
832
  }
1114
833
  const cutoff = input.protectedCutoff;
1115
834
  const windowYields = absoluteEmergency;
@@ -1164,13 +883,16 @@ function planEmergencyDrop(input) {
1164
883
  }
1165
884
  }
1166
885
  if (selected.length === 0) {
1167
- return noop("no-candidates");
886
+ return noop(`no-candidates${floorNote}`);
887
+ }
888
+ if (!input.passAlreadyPriced && reclaimed < EMERGENCY_MIN_ACHIEVABLE_RECLAIM_TOKENS) {
889
+ return noop(`achievable reclaim below minimum (${selected.length} tags, reclaim≈${Math.round(reclaimed)} < ${EMERGENCY_MIN_ACHIEVABLE_RECLAIM_TOKENS} against gap ${reclaimTokens})${floorNote}`);
1168
890
  }
1169
891
  return {
1170
892
  shouldDrop: true,
1171
893
  tagNumbers: selected,
1172
894
  reclaimTokens,
1173
- reason: `tiered drop: ${selected.length} tags, reclaim≈${reclaimed}/${reclaimTokens} tokens (floor≈${fixedFloor}, ceiling=${Math.round(ceilingTokens)})`
895
+ reason: `tiered drop: ${selected.length} tags, reclaim≈${reclaimed}/${reclaimTokens} tokens (floor≈${fixedFloor}, ceiling=${Math.round(ceilingTokens)})${floorNote}`
1174
896
  };
1175
897
  }
1176
898
  function measureEmergencyTag(tag, target, calibration, skeleton) {
@@ -1243,14 +965,13 @@ function getUpdateTagInputByteSizeStatement(db) {
1243
965
  function updateTagByteSize(db, sessionId, tagNumber, newByteSize) {
1244
966
  getUpdateTagByteSizeStatement(db).run(newByteSize, sessionId, tagNumber);
1245
967
  }
1246
- var CONTENT_ID_SUFFIX = /:(?:p|file)\d+$/;
1247
968
  var RECENT_OWNER_SCAN_PAGE_SIZE = 128;
1248
969
  var recentTagOwnerStatements = new WeakMap;
1249
970
  function ownerMessageIdForTagRow(row) {
1250
971
  if (row.type === "tool") {
1251
972
  return row.tool_owner_message_id ?? row.message_id;
1252
973
  }
1253
- return row.message_id.replace(CONTENT_ID_SUFFIX, "");
974
+ return contentTagOwnerMessageId(row.message_id);
1254
975
  }
1255
976
  function getRecentTagOwnerStatement(db) {
1256
977
  let statement = recentTagOwnerStatements.get(db);
@@ -1275,7 +996,7 @@ function getRecentTagOwnerMessageIds(db, sessionId, maxOwners) {
1275
996
  for (const row of rows) {
1276
997
  if (typeof row.type !== "string" || typeof row.message_id !== "string")
1277
998
  continue;
1278
- const ownerId = row.type === "tool" ? typeof row.tool_owner_message_id === "string" ? row.tool_owner_message_id : null : row.message_id.replace(CONTENT_ID_SUFFIX, "");
999
+ const ownerId = row.type === "tool" ? typeof row.tool_owner_message_id === "string" ? row.tool_owner_message_id : null : contentTagOwnerMessageId(row.message_id);
1279
1000
  if (!ownerId || recent.has(ownerId))
1280
1001
  continue;
1281
1002
  recent.add(ownerId);
@@ -1521,7 +1242,21 @@ function insertTag(db, sessionId, messageId, type, byteSize, tagNumber, reasonin
1521
1242
  function updateTagStatus(db, sessionId, tagId, status) {
1522
1243
  getUpdateTagStatusStatement(db).run(status, sessionId, tagId);
1523
1244
  }
1524
- function getInertWhitespaceAssistantTags(db, sessionId) {
1245
+ function getInertWhitespaceAssistantTags(db, sessionId, messageIds) {
1246
+ if (messageIds) {
1247
+ const statement = db.prepare(`SELECT tag_number AS tagNumber, entry_fingerprint AS entryFingerprint
1248
+ FROM tags
1249
+ WHERE session_id = ? AND type = 'message' AND status = 'compacted'
1250
+ AND entry_fingerprint >= ? AND entry_fingerprint < ?`);
1251
+ return [...new Set(messageIds)].flatMap((messageId) => {
1252
+ const prefix = `${WHITESPACE_ASSISTANT_INERT_FINGERPRINT_PREFIX}${messageId}:p`;
1253
+ const rows = statement.all(sessionId, prefix, `${prefix.slice(0, -1)}q`);
1254
+ return rows.map((row) => ({
1255
+ tagNumber: row.tagNumber,
1256
+ contentId: row.entryFingerprint.slice(WHITESPACE_ASSISTANT_INERT_FINGERPRINT_PREFIX.length)
1257
+ }));
1258
+ });
1259
+ }
1525
1260
  const rows = db.prepare(`SELECT tag_number AS tagNumber, entry_fingerprint AS entryFingerprint
1526
1261
  FROM tags
1527
1262
  WHERE session_id = ?
@@ -1703,9 +1438,10 @@ function deleteFoldedDuplicateTag(db, sessionId, tagNumber) {
1703
1438
  foldStatement(db, "DELETE FROM tags WHERE session_id = ? AND tag_number = ?").run(sessionId, tagNumber);
1704
1439
  foldStatement(db, "DELETE FROM pending_ops WHERE session_id = ? AND tag_id = ?").run(sessionId, tagNumber);
1705
1440
  }
1706
- function foldDuplicateIntoSurvivor(db, sessionId, survivor, duplicate, duplicateMayHaveQueuedOps = true) {
1441
+ function foldDuplicateIntoSurvivor(db, sessionId, survivor, duplicate, duplicateMayHaveQueuedOps = true, propagateDroppedStatus = true) {
1707
1442
  mergeSizeAndTokenColumnsIntoSurvivor(db, sessionId, survivor, duplicate);
1708
- applyDroppedStatusIfNeeded(db, sessionId, survivor, duplicate);
1443
+ if (propagateDroppedStatus)
1444
+ applyDroppedStatusIfNeeded(db, sessionId, survivor, duplicate);
1709
1445
  if (duplicateMayHaveQueuedOps) {
1710
1446
  retargetPendingOps(db, sessionId, duplicate.tagNumber, survivor.tagNumber);
1711
1447
  }
@@ -1932,6 +1668,13 @@ function getMaxDroppedTagNumber(db, sessionId) {
1932
1668
  const row = getMaxDroppedTagNumberStatement(db).get(sessionId);
1933
1669
  return isMaxTagNumberRow(row) ? row.max_tag_number : 0;
1934
1670
  }
1671
+ function getTagById(db, sessionId, tagId) {
1672
+ const result = db.prepare(`SELECT ${TAG_SELECT_COLUMNS} FROM tags WHERE session_id = ? AND tag_number = ?`).get(sessionId, tagId);
1673
+ if (!isTagRow(result)) {
1674
+ return null;
1675
+ }
1676
+ return toTagEntry(result);
1677
+ }
1935
1678
  var getToolTagNumberByOwnerStatements = new WeakMap;
1936
1679
  var getNullOwnerToolTagStatements = new WeakMap;
1937
1680
  var adoptNullOwnerToolTagStatements = new WeakMap;
@@ -2032,6 +1775,34 @@ function pickNearestPriorOwner(candidates, currentMessageId, times) {
2032
1775
  }
2033
1776
  return best?.id ?? null;
2034
1777
  }
1778
+ // ../plugin/src/shared/historian-tool-defaults.json
1779
+ var historian_tool_defaults_default = {
1780
+ ctx_note: "Note ${input.action} ${input.note_ids}: ${input.content} → ${output.truncate(160)}",
1781
+ ctx_memory: "Memory ${input.action} ${input.category} ${input.ids}: ${input.content} → ${output.truncate(160)}",
1782
+ todowrite: 'Todos (${input.todos.count}): ${input.todos.each("${status}: ${content}")}',
1783
+ question: 'Asked user: ${input.questions[*].question.join(" / ")} → ${output}',
1784
+ task: "Task ${input.subagent_type}: ${input.prompt} → ${output.truncate(200)}",
1785
+ ask: 'Asked: ${input.question.truncate(250)} [options: ${input.options.join(" / ")}] ${input.resolution} → ${output.truncate(300)}',
1786
+ peer_send: "PM to ${input.agent}${input.agent_id}: ${input.message.truncate(400)}",
1787
+ board: 'Board ${input.verb}: ${input.ops.each("${op} ${item}${lane.title} ${state}${lane.status}${props.text} ${lane.items[*].text}")}',
1788
+ room: "Room ${input.action} ${input.room_id}: ${input.text.truncate(400)}",
1789
+ work: "Work ${input.action} ${input.id}: ${input.prompt_file}${input.prompt.truncate(200)}${input.notes.truncate(200)} → ${output.truncate(160)}",
1790
+ knowhow: "Looked up how-to: ${input.query}${input.id}"
1791
+ };
1792
+
1793
+ // ../plugin/src/shared/historian-tool-expansions.ts
1794
+ function expandToolPart(part, overrides) {
1795
+ if (!part || typeof part !== "object")
1796
+ return null;
1797
+ const p = part;
1798
+ if (p.type !== "tool" || typeof p.tool !== "string")
1799
+ return null;
1800
+ const template = overrides && Object.hasOwn(overrides, p.tool) ? overrides[p.tool] : Object.hasOwn(historian_tool_defaults_default, p.tool) ? historian_tool_defaults_default[p.tool] : undefined;
1801
+ if (typeof template !== "string")
1802
+ return null;
1803
+ const state = p.state;
1804
+ return renderToolTemplate(template, state?.input, state?.output ?? state?.error);
1805
+ }
2035
1806
 
2036
1807
  // ../plugin/src/hooks/magic-context/host-served-rows.ts
2037
1808
  var HOST_UNSERVED_ROW = "hostUnservedRow";
@@ -2056,479 +1827,6 @@ function retreatPastHostUnservedRows(messages, exclusiveEnd, floor) {
2056
1827
  return end;
2057
1828
  }
2058
1829
 
2059
- // ../plugin/src/shared/opencode-db-path.ts
2060
- import { existsSync as existsSync2, readdirSync, statSync as statSync2 } from "node:fs";
2061
- import { homedir as homedir2 } from "node:os";
2062
- import { isAbsolute, join as join2, resolve as resolve2 } from "node:path";
2063
- var cachedResolution = null;
2064
- var lastReadFailure = null;
2065
- var claimedDiagnostics = new Set;
2066
- function openCodeDataDir(env = process.env, dataHome) {
2067
- return join2(dataHome ?? env.XDG_DATA_HOME ?? join2(homedir2(), ".local", "share"), "opencode");
2068
- }
2069
- function environmentKey(dataDir, hostGeneration, channel, env) {
2070
- return [
2071
- hostGeneration,
2072
- dataDir,
2073
- env.OPENCODE_DB ?? "",
2074
- env.OPENCODE_DISABLE_CHANNEL_DB ?? "",
2075
- channel ?? env.OPENCODE_CHANNEL ?? ""
2076
- ].join("\x00");
2077
- }
2078
- function channelPath(dataDir, channel) {
2079
- return ["latest", "beta", "prod"].includes(channel) ? join2(dataDir, "opencode.db") : join2(dataDir, `opencode-${channel}.db`);
2080
- }
2081
- function discoveredCandidateNames(dataDir) {
2082
- const names = ["opencode.db", "opencode-local.db", "opencode-dev.db"];
2083
- try {
2084
- const discovered = readdirSync(dataDir, { withFileTypes: true }).filter((entry) => /^opencode-.+\.db$/.test(entry.name) && !names.includes(entry.name)).map((entry) => entry.name).sort();
2085
- names.push(...discovered);
2086
- } catch {}
2087
- return names;
2088
- }
2089
- function discoverOpenCodeDb(dataDir) {
2090
- const candidates = discoveredCandidateNames(dataDir).map((name, order) => {
2091
- const path = join2(dataDir, name);
2092
- try {
2093
- const metadata = statSync2(path);
2094
- return metadata.isFile() ? { path, order, mtimeMs: metadata.mtimeMs } : null;
2095
- } catch {
2096
- return null;
2097
- }
2098
- });
2099
- const existing = candidates.filter((candidate) => candidate !== null).sort((left, right) => right.mtimeMs - left.mtimeMs || left.order - right.order)[0];
2100
- if (!existing) {
2101
- return { path: join2(dataDir, "opencode.db"), source: "default", channel: null };
2102
- }
2103
- const name = existing.path.slice(dataDir.length + 1);
2104
- const channel = name === "opencode.db" ? null : name.slice("opencode-".length, -".db".length) || null;
2105
- return { path: existing.path, source: "discovered", channel };
2106
- }
2107
- function resolveV1Fresh(dataDir, env = process.env) {
2108
- const explicit = env.OPENCODE_DB;
2109
- if (explicit !== undefined && explicit.length > 0) {
2110
- if (explicit === ":memory:") {
2111
- return { path: explicit, source: "OPENCODE_DB", channel: null };
2112
- }
2113
- return {
2114
- path: isAbsolute(explicit) ? explicit : join2(dataDir, explicit),
2115
- source: "OPENCODE_DB",
2116
- channel: null
2117
- };
2118
- }
2119
- const disableChannelDb = env.OPENCODE_DISABLE_CHANNEL_DB;
2120
- if (disableChannelDb === "1" || disableChannelDb === "true") {
2121
- return { path: join2(dataDir, "opencode.db"), source: "default", channel: null };
2122
- }
2123
- const channel = env.OPENCODE_CHANNEL;
2124
- if (channel !== undefined && channel.length > 0) {
2125
- return { path: channelPath(dataDir, channel), source: "channel", channel };
2126
- }
2127
- return discoverOpenCodeDb(dataDir);
2128
- }
2129
- function sourceOpenCodeDatabaseFilename(hostGeneration, channel, env = process.env) {
2130
- if (hostGeneration === "v1") {
2131
- const explicit = env.OPENCODE_DB;
2132
- if (explicit !== undefined && explicit.length > 0)
2133
- return explicit;
2134
- if (env.OPENCODE_DISABLE_CHANNEL_DB === "1" || env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
2135
- return "opencode.db";
2136
- }
2137
- return ["latest", "beta", "prod"].includes(channel) ? "opencode.db" : `opencode-${channel}.db`;
2138
- }
2139
- return env.OPENCODE_DB ?? (["latest", "dev", "beta", "next", "prod"].includes(channel) || env.OPENCODE_DISABLE_CHANNEL_DB === "1" || env.OPENCODE_DISABLE_CHANNEL_DB === "true" ? "opencode.db" : `opencode-${channel.replace(/[^a-zA-Z0-9._-]/g, "")}.db`);
2140
- }
2141
- function resolveV2Fresh(dataDir, channel, env) {
2142
- const filename = sourceOpenCodeDatabaseFilename("v2", channel, env);
2143
- const explicit = env.OPENCODE_DB !== undefined;
2144
- return {
2145
- path: filename === ":memory:" ? filename : resolve2(dataDir, filename),
2146
- source: explicit ? "OPENCODE_DB" : env.OPENCODE_CHANNEL ? "channel" : "default",
2147
- channel: explicit ? null : channel
2148
- };
2149
- }
2150
- function resolveOpenCodeDbPath(hostGeneration = "v1", options = {}) {
2151
- const env = options.env ?? process.env;
2152
- const dataDir = openCodeDataDir(env, options.dataHome);
2153
- const channel = options.channel ?? env.OPENCODE_CHANNEL;
2154
- const key = environmentKey(dataDir, hostGeneration, channel, env);
2155
- if (cachedResolution?.key === key && (!cachedResolution.existed || existsSync2(cachedResolution.resolution.path))) {
2156
- if (cachedResolution.existed)
2157
- return cachedResolution.resolution;
2158
- }
2159
- const resolution = hostGeneration === "v2" ? resolveV2Fresh(dataDir, channel ?? "latest", env) : resolveV1Fresh(dataDir, env);
2160
- cachedResolution = {
2161
- key,
2162
- resolution,
2163
- existed: resolution.path !== ":memory:" && existsSync2(resolution.path)
2164
- };
2165
- return resolution;
2166
- }
2167
- function schemaTableNames(db, schema = "main") {
2168
- const rows = db.prepare(`SELECT name FROM ${schema}.sqlite_master WHERE type = 'table' AND name IN ('message', 'part', 'session', 'project', 'session_message', 'session_v2')`).all();
2169
- return new Set(rows.flatMap((row) => typeof row.name === "string" ? [row.name] : []));
2170
- }
2171
- function hasV1MessageTables(db, schema = "main") {
2172
- const tables = schemaTableNames(db, schema);
2173
- return tables.has("message") && tables.has("part");
2174
- }
2175
- function detectOpenCodeStoreGeneration(db, schema = "main") {
2176
- const tables = schemaTableNames(db, schema);
2177
- const hasV1Messages = tables.has("message") && tables.has("part");
2178
- if (hasV1Messages)
2179
- return "v1";
2180
- if (tables.has("session_message"))
2181
- return "v2";
2182
- if (tables.has("session") || tables.has("project"))
2183
- return "v1";
2184
- return "unknown";
2185
- }
2186
- function hasMigratedV2Schema(tables) {
2187
- return tables.has("session_message") && tables.has("session_v2");
2188
- }
2189
- function isOpenCodeV2Store(db, schema = "main") {
2190
- return detectOpenCodeStoreGeneration(db, schema) === "v2" || hasMigratedV2Schema(schemaTableNames(db, schema));
2191
- }
2192
- function assertOpenCodeStoreGeneration(db, expected, path, schema = "main") {
2193
- const actual = detectOpenCodeStoreGeneration(db, schema);
2194
- if (actual === expected)
2195
- return;
2196
- if (expected === "v2" && isOpenCodeV2Store(db, schema))
2197
- return;
2198
- if (actual === "unknown")
2199
- return;
2200
- throw new Error(`OpenCode store generation mismatch at ${path}: expected ${expected}, found ${actual}; refusing generation-specific database access`);
2201
- }
2202
- function openCodeDbPathExists(resolution = resolveOpenCodeDbPath()) {
2203
- return resolution.path !== ":memory:" && existsSync2(resolution.path);
2204
- }
2205
- function getOpenCodeDbProbeDescriptions(resolution = resolveOpenCodeDbPath()) {
2206
- if (resolution.source === "OPENCODE_DB")
2207
- return [resolution.path];
2208
- if (resolution.source === "channel" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" || process.env.OPENCODE_DISABLE_CHANNEL_DB === "true") {
2209
- return [resolution.path];
2210
- }
2211
- const dataDir = openCodeDataDir();
2212
- return [
2213
- join2(dataDir, "opencode.db"),
2214
- join2(dataDir, "opencode-local.db"),
2215
- join2(dataDir, "opencode-dev.db"),
2216
- join2(dataDir, "opencode-<channel>.db")
2217
- ];
2218
- }
2219
- function recordOpenCodeDbReadFailure(resolution, error) {
2220
- const message = error instanceof Error ? error.message : String(error);
2221
- lastReadFailure = { ...resolution, message };
2222
- return lastReadFailure;
2223
- }
2224
- function clearOpenCodeDbReadFailure(path) {
2225
- if (path === undefined || lastReadFailure?.path === path)
2226
- lastReadFailure = null;
2227
- }
2228
- function claimOpenCodeDbDiagnosticOnce(surface, resolution) {
2229
- const key = `${surface}\x00${resolution.path}\x00${resolution.source}`;
2230
- if (claimedDiagnostics.has(key))
2231
- return false;
2232
- claimedDiagnostics.add(key);
2233
- return true;
2234
- }
2235
-
2236
- // ../plugin/src/shared/sqlite.ts
2237
- import { AsyncLocalStorage } from "node:async_hooks";
2238
- var reportSlowPrivilegedWrite;
2239
- function registerSlowWriteReporter(reporter) {
2240
- reportSlowPrivilegedWrite = reporter;
2241
- }
2242
- function detectSqliteRuntime() {
2243
- const hasBunVersion = typeof process !== "undefined" && typeof process.versions?.bun === "string";
2244
- const hasBunGlobal = typeof globalThis !== "undefined" && typeof globalThis.Bun !== "undefined";
2245
- return hasBunVersion || hasBunGlobal ? "Bun" : "Node.js";
2246
- }
2247
- var bunSpec = "bun:" + "sqlite";
2248
- var nodeSpec = "node:" + "sqlite";
2249
- async function importSqliteModule(specifier) {
2250
- return await import(specifier);
2251
- }
2252
- function isModuleNotFoundError(error, specifier) {
2253
- const candidate = error;
2254
- const code = typeof candidate?.code === "string" ? candidate.code : "";
2255
- const name = typeof candidate?.name === "string" ? candidate.name : "";
2256
- const message = error instanceof Error ? error.message : String(error ?? "");
2257
- const details = `${code} ${name} ${message}`.toLowerCase();
2258
- const mentionsSpecifier = details.includes(specifier.toLowerCase());
2259
- if (!mentionsSpecifier)
2260
- return false;
2261
- return code === "ERR_MODULE_NOT_FOUND" || code === "ERR_UNKNOWN_BUILTIN_MODULE" || code === "MODULE_NOT_FOUND" || name === "ResolveMessage" || details.includes("module not found") || details.includes("cannot find module") || details.includes("cannot find package") || details.includes("no such built-in module");
2262
- }
2263
-
2264
- class SqliteRuntimeUnavailableError extends Error {
2265
- runtime;
2266
- specifier;
2267
- constructor(runtime, specifier, cause) {
2268
- const requirement = specifier === nodeSpec ? "Requires Node.js >= 24, or Bun with bun:sqlite — this Bun build lacks node:sqlite." : "Requires Bun with bun:sqlite, or Node.js >= 24 — this Bun build lacks bun:sqlite.";
2269
- super(`Magic Context detected ${runtime}, but could not load ${specifier}. ${requirement}`, { cause });
2270
- this.name = "SqliteRuntimeUnavailableError";
2271
- this.runtime = runtime;
2272
- this.specifier = specifier;
2273
- }
2274
- }
2275
- async function loadSqliteModule(runtime = detectSqliteRuntime(), importer = importSqliteModule) {
2276
- const specifier = runtime === "Bun" ? bunSpec : nodeSpec;
2277
- try {
2278
- return await importer(specifier);
2279
- } catch (error) {
2280
- if (isModuleNotFoundError(error, specifier)) {
2281
- throw new SqliteRuntimeUnavailableError(runtime, specifier, error);
2282
- }
2283
- throw error;
2284
- }
2285
- }
2286
- var detectedRuntime = detectSqliteRuntime();
2287
- var isBun = detectedRuntime === "Bun";
2288
- var sqliteModule = await loadSqliteModule(detectedRuntime);
2289
- var DatabaseImpl = isBun ? sqliteModule.Database : buildNodeSqliteDatabaseClass(sqliteModule.DatabaseSync);
2290
- var trackedSqliteConnections = new Map;
2291
- var nextSqliteConnectionSequence = 1;
2292
- function installTransactionRouting(db, readonly) {
2293
- const nativeExec = db.exec.bind(db);
2294
- Object.defineProperty(db, "exec", {
2295
- configurable: true,
2296
- writable: true,
2297
- value: (sql) => {
2298
- if (/^\s*BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)(?:\s+TRANSACTION)?\s*;?\s*$/i.test(sql)) {
2299
- if (transformPassScope.getStore()?.active || backgroundWriterScope.getStore())
2300
- acquireShort(db, () => nativeExec(sql), sql.trim());
2301
- else
2302
- nativeExec(sql);
2303
- return db;
2304
- }
2305
- return nativeExec(sql);
2306
- }
2307
- });
2308
- Object.defineProperty(db, "transaction", {
2309
- configurable: true,
2310
- writable: true,
2311
- value: (fn) => {
2312
- const make = (mode) => function(...args) {
2313
- const nested = isInTransaction(db);
2314
- const savepoint = "mc_tx_sp";
2315
- db.exec(nested ? `SAVEPOINT ${savepoint}` : `BEGIN ${mode}`);
2316
- try {
2317
- const result = fn.apply(this, args);
2318
- db.exec(nested ? `RELEASE ${savepoint}` : "COMMIT");
2319
- return result;
2320
- } catch (error) {
2321
- if (isInTransaction(db)) {
2322
- if (nested) {
2323
- db.exec(`ROLLBACK TO ${savepoint}`);
2324
- db.exec(`RELEASE ${savepoint}`);
2325
- } else
2326
- db.exec("ROLLBACK");
2327
- }
2328
- throw error;
2329
- }
2330
- };
2331
- const defaultTransaction = make(readonly ? "DEFERRED" : "IMMEDIATE");
2332
- const variants = {
2333
- default: defaultTransaction,
2334
- deferred: make("DEFERRED"),
2335
- immediate: make("IMMEDIATE"),
2336
- exclusive: make("EXCLUSIVE"),
2337
- database: db
2338
- };
2339
- for (const transaction of [
2340
- variants.default,
2341
- variants.deferred,
2342
- variants.immediate,
2343
- variants.exclusive
2344
- ]) {
2345
- Object.assign(transaction, variants);
2346
- }
2347
- return defaultTransaction;
2348
- }
2349
- });
2350
- }
2351
- function trackSqliteConnection(db, filename, options) {
2352
- const originalClose = db.close.bind(db);
2353
- const sequence = nextSqliteConnectionSequence++;
2354
- const metadata = {
2355
- sequence,
2356
- filename: typeof filename === "string" ? filename : Buffer.isBuffer(filename) ? "<buffer>" : ":memory:",
2357
- readonly: Boolean(options) && typeof options === "object" && (options.readonly === true || options.readOnly === true)
2358
- };
2359
- installTransactionRouting(db, metadata.readonly);
2360
- Object.defineProperty(db, "close", {
2361
- configurable: true,
2362
- value: (...args) => {
2363
- try {
2364
- return originalClose(...args);
2365
- } finally {
2366
- trackedSqliteConnections.delete(sequence);
2367
- }
2368
- }
2369
- });
2370
- trackedSqliteConnections.set(sequence, {
2371
- ...metadata,
2372
- reference: new WeakRef(db)
2373
- });
2374
- return db;
2375
- }
2376
- var TrackedDatabase = new Proxy(DatabaseImpl, {
2377
- construct(target, args) {
2378
- const db = Reflect.construct(target, args, target);
2379
- return trackSqliteConnection(db, args[0], args[1]);
2380
- }
2381
- });
2382
- function buildNodeSqliteDatabaseClass(DatabaseSync) {
2383
-
2384
- class NodeSqliteDatabase extends DatabaseSync {
2385
- constructor(filename, options) {
2386
- const translated = { ...options };
2387
- if (options && "readonly" in options) {
2388
- translated.readOnly = options.readonly;
2389
- delete translated.readonly;
2390
- }
2391
- super(typeof filename === "string" ? filename : ":memory:", translated);
2392
- }
2393
- prepare(sql) {
2394
- const stmt = super.prepare(sql);
2395
- for (const method of ["run", "get", "all"]) {
2396
- const original = stmt[method].bind(stmt);
2397
- stmt[method] = (...args) => args.length === 1 && Array.isArray(args[0]) ? original(...args[0]) : original(...args);
2398
- }
2399
- return stmt;
2400
- }
2401
- }
2402
- return NodeSqliteDatabase;
2403
- }
2404
- var Database = TrackedDatabase;
2405
- function pragmaValue(db, name) {
2406
- const row = db.prepare(`PRAGMA ${name}`).get();
2407
- if (!row)
2408
- return;
2409
- return row[name] ?? Object.values(row)[0];
2410
- }
2411
- function pragmaNumber(db, name) {
2412
- try {
2413
- const value = pragmaValue(db, name);
2414
- return typeof value === "number" && Number.isFinite(value) ? value : null;
2415
- } catch {
2416
- return null;
2417
- }
2418
- }
2419
- var privilegeDepth = new WeakMap;
2420
- var transformPassScope = new AsyncLocalStorage;
2421
- var admissionScope = new AsyncLocalStorage;
2422
- var backgroundWriterScope = new AsyncLocalStorage;
2423
- function withSqliteBackgroundWriter(operation) {
2424
- return backgroundWriterScope.run(true, operation);
2425
- }
2426
- function isTransientSqliteError(error) {
2427
- if (!error || typeof error !== "object")
2428
- return false;
2429
- const value = error;
2430
- return typeof value.code === "string" && /^(SQLITE_BUSY|SQLITE_LOCKED)(_|$)/.test(value.code) || typeof value.errcode === "number" && [5, 6].includes(value.errcode & 255);
2431
- }
2432
-
2433
- class SqliteAcquisitionBusyError extends Error {
2434
- code = "SQLITE_BUSY";
2435
- stage;
2436
- constructor(cause, stage = "BEGIN IMMEDIATE") {
2437
- super("SQLite writer acquisition remained busy", { cause });
2438
- this.name = "SqliteAcquisitionBusyError";
2439
- this.stage = stage;
2440
- }
2441
- }
2442
- var SHORT_BUSY_TIMEOUT_MS = 25;
2443
- var FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS = 250;
2444
- function acquireShort(db, acquire, site) {
2445
- const started = performance.now();
2446
- const previous = pragmaNumber(db, "busy_timeout");
2447
- let acquired = false;
2448
- try {
2449
- const timeout = transformPassScope.getStore()?.active && !admissionScope.getStore() && !backgroundWriterScope.getStore() ? FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS : SHORT_BUSY_TIMEOUT_MS;
2450
- db.exec(`PRAGMA busy_timeout=${timeout}`);
2451
- acquire();
2452
- acquired = true;
2453
- } catch (error) {
2454
- if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
2455
- throw new SqliteAcquisitionBusyError(error, site);
2456
- throw error;
2457
- } finally {
2458
- if (previous !== null)
2459
- db.exec(`PRAGMA busy_timeout=${previous}`);
2460
- const elapsed = performance.now() - started;
2461
- if (elapsed >= 250)
2462
- console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=${site} lane=${transformPassScope.getStore()?.active ? "foreground" : "background"} elapsed=${Math.round(elapsed)}ms attempts=1 outcome=${acquired ? "acquired" : "busy"}`);
2463
- }
2464
- }
2465
- function isInTransaction(db) {
2466
- const candidate = db;
2467
- return candidate.inTransaction === true || candidate.isTransaction === true;
2468
- }
2469
- function withPrivilegedWriter(db, operation) {
2470
- const previousDepth = privilegeDepth.get(db) ?? 0;
2471
- const nested = isInTransaction(db);
2472
- const savepoint = "mc_privilege_scope";
2473
- const transactionStartedAt = nested ? undefined : performance.now();
2474
- if (nested) {
2475
- db.exec(`SAVEPOINT ${savepoint}`);
2476
- } else {
2477
- try {
2478
- db.exec("BEGIN IMMEDIATE");
2479
- } catch (error) {
2480
- if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
2481
- throw new SqliteAcquisitionBusyError(error);
2482
- throw error;
2483
- }
2484
- }
2485
- privilegeDepth.set(db, previousDepth + 1);
2486
- try {
2487
- db.prepare("INSERT INTO context_privilege_state(id, enabled) VALUES (1, 1) ON CONFLICT(id) DO UPDATE SET enabled = 1").run();
2488
- const result = operation();
2489
- if (previousDepth === 0) {
2490
- db.prepare("UPDATE context_privilege_state SET enabled = 0 WHERE id = 1").run();
2491
- }
2492
- if (nested) {
2493
- db.exec(`RELEASE ${savepoint}`);
2494
- } else {
2495
- db.exec("COMMIT");
2496
- if (transactionStartedAt !== undefined) {
2497
- reportSlowPrivilegedWrite?.("privileged_writer", transactionStartedAt);
2498
- }
2499
- }
2500
- if (previousDepth > 0)
2501
- privilegeDepth.set(db, previousDepth);
2502
- else
2503
- privilegeDepth.delete(db);
2504
- return result;
2505
- } catch (error) {
2506
- try {
2507
- if (nested) {
2508
- db.exec(`ROLLBACK TO ${savepoint}`);
2509
- db.exec(`RELEASE ${savepoint}`);
2510
- } else {
2511
- db.exec("ROLLBACK");
2512
- }
2513
- } finally {
2514
- if (previousDepth > 0)
2515
- privilegeDepth.set(db, previousDepth);
2516
- else
2517
- privilegeDepth.delete(db);
2518
- }
2519
- throw error;
2520
- }
2521
- }
2522
-
2523
- // ../plugin/src/shared/sqlite-helpers.ts
2524
- function closeQuietly(db) {
2525
- if (!db)
2526
- return;
2527
- try {
2528
- db.close();
2529
- } catch {}
2530
- }
2531
-
2532
1830
  // ../plugin/src/hooks/magic-context/read-session-db.ts
2533
1831
  function openCodeDbExists() {
2534
1832
  return harnessOwnsOpenCodeStore() && openCodeDbPathExists(resolveOpenCodeDbPath());
@@ -2829,6 +2127,34 @@ function readRawSessionMessageIdOrdinalsFromDb(db, sessionId) {
2829
2127
  }
2830
2128
  return ordinalById;
2831
2129
  }
2130
+ function readRawSessionMessageOrdinalPageFromDb(db, sessionId, after, limit) {
2131
+ const pageSize = Math.max(1, Math.floor(limit));
2132
+ const rows = (after ? db.prepare(`SELECT id, data, time_created
2133
+ FROM message
2134
+ WHERE session_id = ?
2135
+ AND (time_created, id) > (?, ?)
2136
+ ORDER BY time_created ASC, id ASC
2137
+ LIMIT ?`).all(sessionId, after.timeCreated, after.id, pageSize) : db.prepare(`SELECT id, data, time_created
2138
+ FROM message
2139
+ WHERE session_id = ?
2140
+ ORDER BY time_created ASC, id ASC
2141
+ LIMIT ?`).all(sessionId, pageSize)).filter(isRawMessageRow);
2142
+ return rows.flatMap((row) => {
2143
+ if (typeof row.time_created !== "number")
2144
+ return [];
2145
+ const info = parseJsonRecord(row.data);
2146
+ return {
2147
+ id: row.id,
2148
+ timeCreated: row.time_created,
2149
+ contributesOrdinal: !isRawCompactionSummaryInfo(info),
2150
+ hasValidInfo: info !== null
2151
+ };
2152
+ });
2153
+ }
2154
+ function countStoredRawSessionMessagesFromDb(db, sessionId) {
2155
+ const row = db.prepare("SELECT COUNT(*) AS count FROM message WHERE session_id = ?").get(sessionId);
2156
+ return typeof row?.count === "number" ? row.count : 0;
2157
+ }
2832
2158
  function isAnchorRow(row) {
2833
2159
  return row !== null && typeof row === "object" && typeof row.time_created === "number" && typeof row.id === "string";
2834
2160
  }
@@ -2882,6 +2208,24 @@ function readRawSessionTailFromDb(db, sessionId, baseOrdinal, anchorMessageId) {
2882
2208
  }
2883
2209
  return { messages, absoluteMessageCount: Math.max(0, ord - 1) };
2884
2210
  }
2211
+ function readRawSessionMessagePartsByIdFromDb(db, sessionId, messageId, onQuery) {
2212
+ onQuery?.();
2213
+ const row = db.prepare("SELECT id, data, time_created, time_updated FROM message WHERE session_id = ? AND id = ?").get(sessionId, messageId);
2214
+ if (!row || !isRawMessageRow(row) || typeof row.time_created !== "number")
2215
+ return null;
2216
+ const info = parseJsonRecord(row.data);
2217
+ if (!info || isRawCompactionSummaryInfo(info))
2218
+ return null;
2219
+ onQuery?.();
2220
+ const partRows = db.prepare(RAW_MESSAGE_PARTS_BY_ID_SQL).all(sessionId, messageId).filter(isRawPartRow);
2221
+ return {
2222
+ id: row.id,
2223
+ role: typeof info.role === "string" ? info.role : "unknown",
2224
+ parts: partRows.map((part) => attachRawPartVersion(parseJsonUnknown(part.data), part.time_updated)),
2225
+ createdAt: row.time_created,
2226
+ version: row.time_updated ?? null
2227
+ };
2228
+ }
2885
2229
  function readRawSessionMessageOrdinalByIdFromDb(db, sessionId, messageId) {
2886
2230
  const row = db.prepare(`SELECT COUNT(candidate.id) AS ordinal
2887
2231
  FROM message AS target
@@ -2939,6 +2283,44 @@ function readRawSessionMessageByIdFromDb(db, sessionId, messageId) {
2939
2283
  version: row.time_updated ?? null
2940
2284
  };
2941
2285
  }
2286
+ function readRawSeedTailFromDb(db, sessionId, boundaryId) {
2287
+ const rows = db.prepare(`
2288
+ WITH canonical AS (
2289
+ SELECT id, time_created,
2290
+ ROW_NUMBER() OVER (ORDER BY time_created, id) AS ordinal
2291
+ FROM message WHERE session_id = ?
2292
+ AND NOT (CASE WHEN json_valid(data) THEN COALESCE(json_extract(data, '$.summary'), 0) ELSE 0 END = 1
2293
+ AND CASE WHEN json_valid(data) THEN COALESCE(json_extract(data, '$.finish'), '') ELSE '' END = 'stop')
2294
+ )
2295
+ SELECT c.id, m.data, c.time_created, m.time_updated, c.ordinal,
2296
+ p.data AS part_data, p.time_updated AS part_updated
2297
+ FROM canonical c JOIN message m ON m.id = c.id
2298
+ LEFT JOIN part p ON +p.session_id = ? AND likelihood(p.message_id = c.id, 0.000001)
2299
+ WHERE ? IS NULL OR c.ordinal >= (SELECT ordinal FROM canonical WHERE id = ?)
2300
+ ORDER BY c.ordinal, p.time_created, p.id
2301
+ `).all(sessionId, sessionId, boundaryId, boundaryId);
2302
+ const messages = new Map;
2303
+ for (const row of rows) {
2304
+ if (!messages.has(row.id)) {
2305
+ const info = parseJsonRecord(row.data);
2306
+ if (!info)
2307
+ continue;
2308
+ messages.set(row.id, {
2309
+ id: row.id,
2310
+ ordinal: row.ordinal,
2311
+ role: typeof info.role === "string" ? info.role : "unknown",
2312
+ createdAt: row.time_created,
2313
+ version: row.time_updated,
2314
+ parts: []
2315
+ });
2316
+ }
2317
+ if (row.part_data !== null)
2318
+ messages.get(row.id)?.parts.push(attachRawPartVersion(parseJsonUnknown(row.part_data), row.part_updated ?? undefined));
2319
+ }
2320
+ if (boundaryId !== null && !messages.has(boundaryId))
2321
+ throw new Error("state_sync materialized boundary is missing from raw storage");
2322
+ return messages;
2323
+ }
2942
2324
 
2943
2325
  // ../plugin/src/hooks/magic-context/read-session-true-raw-tokens.ts
2944
2326
  function completedToolArcCrossesBoundary(invOrdinal, resOrdinal, boundary) {
@@ -3475,6 +2857,88 @@ function invalidateTrueRawTokenCache(args) {
3475
2857
  args.reason;
3476
2858
  }
3477
2859
 
2860
+ // ../plugin/src/hooks/magic-context/system-injection-stripper.ts
2861
+ var SYSTEM_INJECTION_MARKERS = [
2862
+ "<!-- OMO_INTERNAL_INITIATOR -->",
2863
+ "[SYSTEM DIRECTIVE: MAGIC-CONTEXT",
2864
+ "[SYSTEM DIRECTIVE: OH-MY-OPENCODE",
2865
+ "[Category+Skill Reminder]",
2866
+ "[EDIT ERROR - IMMEDIATE ACTION REQUIRED]",
2867
+ "[task CALL FAILED - IMMEDIATE RETRY REQUIRED]",
2868
+ "[EMERGENCY CONTEXT WINDOW WARNING]",
2869
+ "Unstable background agent appears idle",
2870
+ "**THE SUBAGENT JUST CLAIMED THIS TASK IS DONE."
2871
+ ];
2872
+ var SYSTEM_REMINDER_REGEX = /<system-reminder>[\s\S]*?<\/system-reminder>/gi;
2873
+ var OMO_MARKER_REGEX = /<!-- OMO_INTERNAL_INITIATOR -->/g;
2874
+ var STEERING_WRAPPER_REGEX = /<system-reminder>\nThe user sent the following message:\n[\s\S]*?\n\nPlease address this message and continue with your tasks\.\n<\/system-reminder>/g;
2875
+ function stripSystemInjection(text) {
2876
+ STEERING_WRAPPER_REGEX.lastIndex = 0;
2877
+ if (!STEERING_WRAPPER_REGEX.test(text))
2878
+ return stripInjectedRegions(text);
2879
+ STEERING_WRAPPER_REGEX.lastIndex = 0;
2880
+ let result = "";
2881
+ let changed = false;
2882
+ let cursor = 0;
2883
+ const keepOrStrip = (segment) => {
2884
+ const stripped = stripInjectedRegions(segment);
2885
+ if (stripped === null) {
2886
+ result += segment;
2887
+ } else {
2888
+ result += stripped;
2889
+ changed = true;
2890
+ }
2891
+ };
2892
+ for (const match of text.matchAll(STEERING_WRAPPER_REGEX)) {
2893
+ keepOrStrip(text.slice(cursor, match.index));
2894
+ result += match[0];
2895
+ cursor = match.index + match[0].length;
2896
+ }
2897
+ keepOrStrip(text.slice(cursor));
2898
+ return changed ? result.trim() : null;
2899
+ }
2900
+ function stripOutsideSteeringWrappers(text, strip) {
2901
+ STEERING_WRAPPER_REGEX.lastIndex = 0;
2902
+ let result = "";
2903
+ let cursor = 0;
2904
+ for (const match of text.matchAll(STEERING_WRAPPER_REGEX)) {
2905
+ result += strip(text.slice(cursor, match.index));
2906
+ result += match[0];
2907
+ cursor = match.index + match[0].length;
2908
+ }
2909
+ return result + strip(text.slice(cursor));
2910
+ }
2911
+ function stripInjectedRegions(text) {
2912
+ let hasInjection = false;
2913
+ for (const marker of SYSTEM_INJECTION_MARKERS) {
2914
+ if (text.includes(marker)) {
2915
+ hasInjection = true;
2916
+ break;
2917
+ }
2918
+ }
2919
+ if (SYSTEM_REMINDER_REGEX.test(text))
2920
+ hasInjection = true;
2921
+ SYSTEM_REMINDER_REGEX.lastIndex = 0;
2922
+ if (!hasInjection)
2923
+ return null;
2924
+ let cleaned = text;
2925
+ cleaned = cleaned.replace(SYSTEM_REMINDER_REGEX, "");
2926
+ cleaned = cleaned.replace(OMO_MARKER_REGEX, "");
2927
+ cleaned = cleaned.replace(/\[SYSTEM DIRECTIVE: OH-MY-(?:OPENCODE|CLAUDE)[^\]]*\][\s\S]*?(?=\n\n(?!\s*[-*])|$)/g, "");
2928
+ for (const marker of SYSTEM_INJECTION_MARKERS) {
2929
+ if (marker.startsWith("<!-- ") || marker.startsWith("[SYSTEM DIRECTIVE"))
2930
+ continue;
2931
+ const idx = cleaned.indexOf(marker);
2932
+ if (idx === -1)
2933
+ continue;
2934
+ const blockEnd = cleaned.indexOf(`
2935
+
2936
+ `, idx + marker.length);
2937
+ cleaned = blockEnd !== -1 ? cleaned.slice(0, idx) + cleaned.slice(blockEnd) : cleaned.slice(0, idx);
2938
+ }
2939
+ return cleaned.trim();
2940
+ }
2941
+
3478
2942
  // ../plugin/src/hooks/magic-context/tag-part-guards.ts
3479
2943
  function isTextPart(part) {
3480
2944
  if (part === null || typeof part !== "object")
@@ -3540,12 +3004,20 @@ function createDreamTokenBudget(budget) {
3540
3004
  });
3541
3005
  return {
3542
3006
  snapshot,
3007
+ finalize() {
3008
+ if (finalizeFired)
3009
+ return "continue";
3010
+ finalizeFired = true;
3011
+ return "finalize";
3012
+ },
3543
3013
  charge(input, cacheRead, cacheWrite, completed = false, canFinalize = true) {
3544
3014
  for (const value of [input, cacheRead, cacheWrite]) {
3545
3015
  if (!Number.isSafeInteger(value) || value < 0)
3546
3016
  throw new Error("Invalid prompt usage");
3547
3017
  }
3548
3018
  spent += input + cacheRead + cacheWrite;
3019
+ if (completed)
3020
+ return "continue";
3549
3021
  if (spent >= budget)
3550
3022
  return "stop";
3551
3023
  if (!completed && !finalizeFired && spent >= budget * 0.8) {
@@ -3559,7 +3031,7 @@ function createDreamTokenBudget(budget) {
3559
3031
  return "continue";
3560
3032
  },
3561
3033
  refuseTool() {
3562
- if (!finalizeFired)
3034
+ if (!finalizeFired && !snapshot().hardStopped)
3563
3035
  return null;
3564
3036
  refusedCalls += 1;
3565
3037
  return { message: TOKEN_BUDGET_TOOL_REFUSAL, hardStopped: snapshot().hardStopped };
@@ -3747,37 +3219,6 @@ function applyEditMarkerToInput(input) {
3747
3219
  }
3748
3220
  }
3749
3221
 
3750
- // ../plugin/src/features/magic-context/tool-definition-tokens.ts
3751
- var measurements = new Map;
3752
- var fingerprints = new Map;
3753
- var persistenceDb = null;
3754
- var cachedInsertStmt = null;
3755
- function keyFor(providerID, modelID, agentName) {
3756
- const agent = agentName && agentName.length > 0 ? agentName : "default";
3757
- return `${providerID}/${modelID}/${agent}`;
3758
- }
3759
- function setDatabase(db) {
3760
- persistenceDb = db;
3761
- cachedInsertStmt = null;
3762
- }
3763
- function loadToolDefinitionMeasurements(db) {
3764
- let rows = [];
3765
- try {
3766
- rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count FROM tool_definition_measurements").all();
3767
- } catch {
3768
- return;
3769
- }
3770
- for (const row of rows) {
3771
- const key = keyFor(row.provider_id, row.model_id, row.agent_name);
3772
- let inner = measurements.get(key);
3773
- if (!inner) {
3774
- inner = new Map;
3775
- measurements.set(key, inner);
3776
- }
3777
- inner.set(row.tool_id, row.token_count);
3778
- }
3779
- }
3780
-
3781
3222
  // ../plugin/src/hooks/magic-context/tokenizer-calibration-seeds.json
3782
3223
  var tokenizer_calibration_seeds_default = [
3783
3224
  {
@@ -4287,6 +3728,241 @@ function historyLocalBudget(providerTokens, modelKey) {
4287
3728
  return ratio === 1 ? providerTokens : localBudget(providerTokens, ratio);
4288
3729
  }
4289
3730
 
3731
+ // ../plugin/src/features/magic-context/overflow-detection.ts
3732
+ var OVERFLOW_PATTERNS = [
3733
+ /prompt is too long/i,
3734
+ /input is too long for requested model/i,
3735
+ /exceeds the context window/i,
3736
+ /input token count.*exceeds the maximum/i,
3737
+ /maximum prompt length is \d+/i,
3738
+ /reduce the length of the messages/i,
3739
+ /maximum context length is \d+ tokens/i,
3740
+ /maximum model length is \d+/i,
3741
+ /exceeds the limit of \d+/i,
3742
+ /exceeds the available context size/i,
3743
+ /greater than the context length/i,
3744
+ /context window exceeds limit/i,
3745
+ /exceeded model token limit/i,
3746
+ /context[_ ]length[_ ]exceeded/i,
3747
+ /request entity too large/i,
3748
+ /context length is only \d+ tokens/i,
3749
+ /input length.*exceeds.*context length/i,
3750
+ /prompt too long; exceeded (?:max )?context length/i,
3751
+ /too large for model with \d+ maximum context length/i,
3752
+ /model_context_window_exceeded/i,
3753
+ /context size has been exceeded/i,
3754
+ /prepared prompt exceeds engine max_context/i,
3755
+ /prompt exceeds (?:the )?.{0,32}\bmax_context\b/i
3756
+ ];
3757
+ var LIMIT_EXTRACTION_PATTERNS = [
3758
+ { pattern: /maximum prompt length is (\d+)/i, provenance: "prompt_only" },
3759
+ {
3760
+ pattern: /maximum context length is (\d+) tokens?/i,
3761
+ provenance: "combined"
3762
+ },
3763
+ { pattern: /maximum model length is (\d+)/i, provenance: "combined" },
3764
+ { pattern: /context length is only (\d+) tokens?/i, provenance: "combined" },
3765
+ { pattern: /exceeds the limit of (\d+)/i, provenance: "unknown" },
3766
+ {
3767
+ pattern: /too large for model with (\d+) maximum context length/i,
3768
+ provenance: "combined"
3769
+ },
3770
+ { pattern: /context size[^0-9]{0,40}(\d{4,})\s*tokens?/i, provenance: "combined" },
3771
+ { pattern: /exceeds? the context length of (\d+)/i, provenance: "combined" },
3772
+ {
3773
+ pattern: />\s*(\d+)\s*(?:tokens?\s*)?(?:maximum|max|limit)\b/i,
3774
+ provenance: "prompt_only"
3775
+ },
3776
+ {
3777
+ pattern: /maximum number of tokens allowed\s*\(?\s*(\d+)/i,
3778
+ provenance: "prompt_only"
3779
+ },
3780
+ { pattern: /prepared prompt exceeds engine max_context\s+(\d+)/i, provenance: "unknown" },
3781
+ { pattern: /prompt exceeds (?:the )?.{0,32}\bmax_context\s+(\d+)/i, provenance: "unknown" },
3782
+ { pattern: /max(?:imum)?.*context.*?(\d+)/i, provenance: "unknown" }
3783
+ ];
3784
+ var MIN_PLAUSIBLE_LIMIT = 1024;
3785
+ var MAX_PLAUSIBLE_LIMIT = 1e7;
3786
+ var THINKING_BINDING_MISMATCH_PATTERN = /bound to a different conversation/i;
3787
+ var THINKING_BINDING_FAILING_PATH_PATTERN = /^\s*(messages\.\d+\.content\.\d+):/;
3788
+ var THINKING_BINDING_FIRST_CHANGED_PATTERN = /first at `?(messages\.\d+\.content\.\d+)`?/;
3789
+ function extractErrorMessage(error) {
3790
+ if (!error)
3791
+ return "";
3792
+ if (typeof error === "string")
3793
+ return error;
3794
+ if (typeof error === "object") {
3795
+ const obj = error;
3796
+ const nested = obj.error;
3797
+ if (nested && typeof nested.message === "string" && nested.message.length > 0) {
3798
+ return nested.message;
3799
+ }
3800
+ }
3801
+ if (error instanceof Error)
3802
+ return error.message;
3803
+ if (typeof error === "object") {
3804
+ const obj = error;
3805
+ if (typeof obj.message === "string")
3806
+ return obj.message;
3807
+ if (typeof obj.responseBody === "string")
3808
+ return obj.responseBody;
3809
+ try {
3810
+ return JSON.stringify(error);
3811
+ } catch {
3812
+ return String(error);
3813
+ }
3814
+ }
3815
+ return String(error);
3816
+ }
3817
+ function extractExplicitHttpStatus(error) {
3818
+ if (!error || typeof error !== "object")
3819
+ return;
3820
+ const seen = new Set;
3821
+ const queue = [error];
3822
+ while (queue.length > 0) {
3823
+ const current = queue.shift();
3824
+ if (!current || seen.has(current))
3825
+ continue;
3826
+ seen.add(current);
3827
+ const record = current;
3828
+ for (const key of ["status", "statusCode", "status_code"]) {
3829
+ const value = record[key];
3830
+ if (typeof value === "number")
3831
+ return value;
3832
+ }
3833
+ for (const value of Object.values(record)) {
3834
+ if (value && typeof value === "object")
3835
+ queue.push(value);
3836
+ }
3837
+ }
3838
+ return;
3839
+ }
3840
+ function detectThinkingBindingMismatch(error) {
3841
+ const message = extractErrorMessage(error);
3842
+ const explicitStatus = extractExplicitHttpStatus(error);
3843
+ if (explicitStatus !== undefined && explicitStatus !== 400 || !THINKING_BINDING_MISMATCH_PATTERN.test(message)) {
3844
+ return { isBindingMismatch: false };
3845
+ }
3846
+ const failingBlockPath = THINKING_BINDING_FAILING_PATH_PATTERN.exec(message)?.[1];
3847
+ const firstChangedPath = THINKING_BINDING_FIRST_CHANGED_PATTERN.exec(message)?.[1];
3848
+ return {
3849
+ isBindingMismatch: true,
3850
+ matchedPattern: "bound to a different conversation",
3851
+ ...failingBlockPath ? { failingBlockPath } : {},
3852
+ ...firstChangedPath ? { firstChangedPath } : {}
3853
+ };
3854
+ }
3855
+ var PREFIX_BOUND_THINKING_MODELS = [
3856
+ ["fable", 5, 1],
3857
+ ["opus", 5, 5],
3858
+ ["sonnet", 5, 5]
3859
+ ];
3860
+ var PREFIX_BOUND_THINKING_PATTERN = new RegExp(`(?:^|[-_.:/])(?:${PREFIX_BOUND_THINKING_MODELS.map(([family, major, minor]) => `${family}[-_.]?${major}[-_.]${minor}`).join("|")})(?:$|[-_.:/@])`, "i");
3861
+ function isPrefixBoundThinkingModel(_providerID, modelID) {
3862
+ if (!modelID)
3863
+ return false;
3864
+ return PREFIX_BOUND_THINKING_PATTERN.test(modelID);
3865
+ }
3866
+ function isFable51ThinkingBindingModel(providerID, modelID) {
3867
+ if (providerID?.toLowerCase() !== "anthropic" || !modelID)
3868
+ return false;
3869
+ return /(?:^|[-_.])fable[-_.]?5[-_.]1(?:$|[-_.])/i.test(modelID);
3870
+ }
3871
+ function detectOverflow(error) {
3872
+ const message = extractErrorMessage(error);
3873
+ if (!message) {
3874
+ return { isOverflow: false };
3875
+ }
3876
+ const hasStatus413 = /\b413\b/.test(message) && /(entity|payload|context|prompt)/i.test(message);
3877
+ let matched;
3878
+ for (const pattern of OVERFLOW_PATTERNS) {
3879
+ if (pattern.test(message)) {
3880
+ matched = pattern;
3881
+ break;
3882
+ }
3883
+ }
3884
+ if (!matched && !hasStatus413) {
3885
+ return { isOverflow: false };
3886
+ }
3887
+ const reportedLimit = parseReportedLimit(message);
3888
+ const reportedInputTokens = parseReportedInputTokens(message);
3889
+ return {
3890
+ isOverflow: true,
3891
+ reportedInputTokens,
3892
+ reportedLimit: reportedLimit?.value,
3893
+ reportedLimitProvenance: reportedLimit?.provenance,
3894
+ matchedPattern: matched?.source
3895
+ };
3896
+ }
3897
+ function parseReportedInputTokens(message) {
3898
+ if (!message)
3899
+ return;
3900
+ const patterns = [
3901
+ /prompt is too long:\s*(\d+)/i,
3902
+ /input token count\s*\(?\s*(\d+)/i,
3903
+ /input length\s*(\d+)/i,
3904
+ /prompt was\s*(\d+)/i,
3905
+ /messages resulted in\s*(\d+)\s*tokens?/i
3906
+ ];
3907
+ for (const pattern of patterns) {
3908
+ const raw = message.match(pattern)?.[1];
3909
+ if (!raw)
3910
+ continue;
3911
+ const value = Number.parseInt(raw, 10);
3912
+ if (Number.isFinite(value) && value > 0)
3913
+ return value;
3914
+ }
3915
+ return;
3916
+ }
3917
+ function parseReportedLimit(message) {
3918
+ if (!message)
3919
+ return;
3920
+ for (const { pattern, provenance } of LIMIT_EXTRACTION_PATTERNS) {
3921
+ const match = message.match(pattern);
3922
+ if (!match)
3923
+ continue;
3924
+ const raw = match[1];
3925
+ if (!raw)
3926
+ continue;
3927
+ const value = Number.parseInt(raw, 10);
3928
+ if (!Number.isFinite(value))
3929
+ continue;
3930
+ if (value < MIN_PLAUSIBLE_LIMIT || value > MAX_PLAUSIBLE_LIMIT)
3931
+ continue;
3932
+ return { value, provenance };
3933
+ }
3934
+ return;
3935
+ }
3936
+
3937
+ // ../plugin/src/hooks/magic-context/sentinel.ts
3938
+ function modelAcceptsEmptyContent(providerID) {
3939
+ return providerID === "anthropic";
3940
+ }
3941
+ var REMOVED_REASONING_MARK = Symbol.for("magic-context.removed-reasoning");
3942
+ var NEUTRALIZED_ORIGINALS = new WeakMap;
3943
+ var VARIANT_CACHE_PRESERVING_MODELS = {
3944
+ "anthropic/claude-fable-5-1": "2026-09-02",
3945
+ "openai/gpt-6-astra": "2026-09-05",
3946
+ "anthropic/claude-opus-5-5": "2026-09-23"
3947
+ };
3948
+ function canonicalVariantModelIdentity(providerID, modelID) {
3949
+ const normalizedProviderID = providerID.toLowerCase();
3950
+ const isAnthropicFamily = normalizedProviderID === "anthropic" || normalizedProviderID === "google-vertex-anthropic" || normalizedProviderID.includes("bedrock");
3951
+ if (isAnthropicFamily && isFable51ThinkingBindingModel("anthropic", modelID)) {
3952
+ return "anthropic/claude-fable-5-1";
3953
+ }
3954
+ return canonicalModelIdentity(`${providerID}/${modelID}`).toLowerCase();
3955
+ }
3956
+ function variantChangeBustsProviderCache(providerID, modelID) {
3957
+ if (!providerID || !modelID)
3958
+ return false;
3959
+ const identity = canonicalVariantModelIdentity(providerID, modelID);
3960
+ if (Object.hasOwn(VARIANT_CACHE_PRESERVING_MODELS, identity))
3961
+ return false;
3962
+ const normalizedProviderID = providerID.toLowerCase();
3963
+ return normalizedProviderID === "anthropic" || normalizedProviderID === "google-vertex-anthropic" || normalizedProviderID.includes("bedrock");
3964
+ }
3965
+
4290
3966
  // ../plugin/src/hooks/magic-context/tool-input-size.ts
4291
3967
  var SKELETON_REAL_INPUT_MAX_BYTES = 1024;
4292
3968
  var encoder2 = new TextEncoder;
@@ -4364,19 +4040,41 @@ var sessionProviders = new Map;
4364
4040
  function resolveHostServedBoundaryId2(sessionId, messageId) {
4365
4041
  if (messageId.length === 0)
4366
4042
  return messageId;
4367
- return sessionProviders.get(sessionId)?.readServedBoundaryId?.(messageId) ?? messageId;
4043
+ return sessionProviders.get(sessionId)?.provider.readServedBoundaryId?.(messageId) ?? messageId;
4368
4044
  }
4369
4045
  function hasRawMessageProvider2(sessionId) {
4370
4046
  return sessionProviders.has(sessionId);
4371
4047
  }
4372
4048
  function setRawMessageProvider2(sessionId, provider) {
4373
- sessionProviders.set(sessionId, provider);
4049
+ const current = sessionProviders.get(sessionId);
4050
+ const registration = current?.provider === provider ? current : { provider, scopes: 0 };
4051
+ registration.scopes += 1;
4052
+ sessionProviders.set(sessionId, registration);
4053
+ let active = true;
4374
4054
  return () => {
4375
- const current = sessionProviders.get(sessionId);
4376
- if (current === provider)
4055
+ if (!active)
4056
+ return;
4057
+ active = false;
4058
+ registration.scopes -= 1;
4059
+ if (registration.scopes === 0 && sessionProviders.get(sessionId) === registration) {
4377
4060
  sessionProviders.delete(sessionId);
4061
+ }
4378
4062
  };
4379
4063
  }
4064
+ var boundedProviderWrappers = new WeakMap;
4065
+ function setBoundedRawMessageProvider2(sessionId, provider) {
4066
+ let wrapper = boundedProviderWrappers.get(provider);
4067
+ if (!wrapper) {
4068
+ wrapper = {
4069
+ ...provider,
4070
+ readMessages: () => {
4071
+ throw new Error("Bounded raw-message providers cannot read complete history; full reads are reserved for store-generation conversion");
4072
+ }
4073
+ };
4074
+ boundedProviderWrappers.set(provider, wrapper);
4075
+ }
4076
+ return setRawMessageProvider2(sessionId, wrapper);
4077
+ }
4380
4078
  function withRawMessageProvider2(sessionId, provider, fn) {
4381
4079
  const cleanup = setRawMessageProvider2(sessionId, provider);
4382
4080
  let result;
@@ -4392,8 +4090,27 @@ function withRawMessageProvider2(sessionId, provider, fn) {
4392
4090
  cleanup();
4393
4091
  return result;
4394
4092
  }
4093
+ var SYSTEM_REMINDER_BLOCK_REGEX = /<system-reminder>[\s\S]*?<\/system-reminder>/gi;
4395
4094
  function cleanUserText2(text) {
4396
- return removeSystemReminders(text).replace(OMO_INTERNAL_INITIATOR_MARKER, "").trim();
4095
+ return stripOutsideSteeringWrappers(text, (segment) => segment.replace(SYSTEM_REMINDER_BLOCK_REGEX, "").replace(OMO_INTERNAL_INITIATOR_MARKER, "")).trim();
4096
+ }
4097
+ function hasMeaningfulChunkUserText(parts) {
4098
+ for (const part of parts) {
4099
+ if (part === null || typeof part !== "object")
4100
+ continue;
4101
+ const candidate = part;
4102
+ if (candidate.type !== "text" || typeof candidate.text !== "string")
4103
+ continue;
4104
+ if (candidate.ignored === true)
4105
+ continue;
4106
+ const cleaned = cleanUserText2(candidate.text);
4107
+ if (!cleaned)
4108
+ continue;
4109
+ if (isSystemDirective(cleaned))
4110
+ continue;
4111
+ return true;
4112
+ }
4113
+ return false;
4397
4114
  }
4398
4115
  function withRawSessionMessageCache2(fn) {
4399
4116
  const outerCache = activeRawMessageCache;
@@ -4429,7 +4146,7 @@ function readRawSessionMessages2(sessionId) {
4429
4146
  return readRawSessionMessagesFromSource(sessionId);
4430
4147
  }
4431
4148
  function readRawSessionMessagePage2(sessionId, afterOrdinal, limit, finalWatermark) {
4432
- const provider = sessionProviders.get(sessionId);
4149
+ const provider = sessionProviders.get(sessionId)?.provider;
4433
4150
  if (provider?.readMessagePage) {
4434
4151
  return provider.readMessagePage(afterOrdinal, limit, finalWatermark);
4435
4152
  }
@@ -4441,7 +4158,7 @@ function readRawSessionMessagePage2(sessionId, afterOrdinal, limit, finalWaterma
4441
4158
  return withReadOnlySessionDb((db) => readRawSessionMessagePageFromDb(db, sessionId, afterOrdinal, limit, finalWatermark));
4442
4159
  }
4443
4160
  function getRawSessionMessageOrdinalCount2(sessionId) {
4444
- const provider = sessionProviders.get(sessionId);
4161
+ const provider = sessionProviders.get(sessionId)?.provider;
4445
4162
  if (provider) {
4446
4163
  if (provider.getMessageCount)
4447
4164
  return provider.getMessageCount();
@@ -4454,7 +4171,7 @@ function getRawSessionMessageOrdinalCount2(sessionId) {
4454
4171
  }
4455
4172
  var RAW_MESSAGE_RANGE_PAGE_SIZE = 100;
4456
4173
  function readRawSessionMessageRangeFromSource(sessionId, fromOrdinal, toOrdinal) {
4457
- const provider = sessionProviders.get(sessionId);
4174
+ const provider = sessionProviders.get(sessionId)?.provider;
4458
4175
  if (provider?.iterateMessageRange)
4459
4176
  return [...provider.iterateMessageRange(fromOrdinal, toOrdinal)];
4460
4177
  if (provider && !provider.readMessagePage) {
@@ -4485,13 +4202,13 @@ function readRawSessionMessageRangeFromSource(sessionId, fromOrdinal, toOrdinal)
4485
4202
  }
4486
4203
  return messages;
4487
4204
  }
4488
- var RAW_MESSAGE_VISIT_PAGE_SIZE = 50;
4205
+ var RAW_MESSAGE_VISIT_PAGE_SIZE2 = 50;
4489
4206
  function visitRawSessionMessages2(sessionId, fromOrdinal, toOrdinal, visit, options = {}) {
4490
4207
  const from = Math.max(1, Math.floor(fromOrdinal));
4491
4208
  const to = Math.floor(toOrdinal);
4492
4209
  if (to < from)
4493
4210
  return;
4494
- const provider = sessionProviders.get(sessionId);
4211
+ const provider = sessionProviders.get(sessionId)?.provider;
4495
4212
  if (provider?.iterateMessageRange) {
4496
4213
  for (const message of provider.iterateMessageRange(from, to)) {
4497
4214
  if (!visit(message))
@@ -4510,7 +4227,7 @@ function visitRawSessionMessages2(sessionId, fromOrdinal, toOrdinal, visit, opti
4510
4227
  }
4511
4228
  if (!provider && !openCodeDbExists())
4512
4229
  return;
4513
- const pageSize = Math.max(1, Math.floor(options.pageSize ?? RAW_MESSAGE_VISIT_PAGE_SIZE));
4230
+ const pageSize = Math.max(1, Math.floor(options.pageSize ?? RAW_MESSAGE_VISIT_PAGE_SIZE2));
4514
4231
  let afterOrdinal = from - 1;
4515
4232
  let after;
4516
4233
  while (afterOrdinal < to) {
@@ -4568,7 +4285,7 @@ function primeTailRawMessageCache2(args) {
4568
4285
  return false;
4569
4286
  if (lastCompartmentEnd < 1 || !anchorMessageId)
4570
4287
  return false;
4571
- const provider = sessionProviders.get(sessionId);
4288
+ const provider = sessionProviders.get(sessionId)?.provider;
4572
4289
  if (provider) {
4573
4290
  if (!provider.readMessagePage || !provider.getMessageCount)
4574
4291
  return false;
@@ -4615,26 +4332,70 @@ function primeInMemoryTailRawMessageCache2(args) {
4615
4332
  activeAbsoluteCountCache?.set(sessionId, absoluteMessageCount);
4616
4333
  return true;
4617
4334
  }
4335
+ function readRawSessionMessageOrdinalPage2(sessionId, after, limit) {
4336
+ const provider = sessionProviders.get(sessionId)?.provider;
4337
+ if (provider?.readMessageOrdinalPage)
4338
+ return provider.readMessageOrdinalPage(after, limit);
4339
+ if (provider) {
4340
+ const rows = provider.readMessages().map((message) => ({
4341
+ id: message.id,
4342
+ timeCreated: message.createdAt ?? message.ordinal,
4343
+ contributesOrdinal: true,
4344
+ hasValidInfo: true
4345
+ })).filter((row) => !after || row.timeCreated > after.timeCreated || row.timeCreated === after.timeCreated && row.id > after.id).sort((left, right) => left.timeCreated - right.timeCreated || left.id.localeCompare(right.id));
4346
+ return rows.slice(0, Math.max(1, Math.floor(limit)));
4347
+ }
4348
+ if (!openCodeDbExists())
4349
+ return [];
4350
+ return withReadOnlySessionDb((db) => readRawSessionMessageOrdinalPageFromDb(db, sessionId, after, limit));
4351
+ }
4352
+ function getRawSessionStoredMessageCount2(sessionId) {
4353
+ const provider = sessionProviders.get(sessionId)?.provider;
4354
+ if (provider?.getStoredMessageCount)
4355
+ return provider.getStoredMessageCount();
4356
+ if (provider)
4357
+ return provider.readMessages().length;
4358
+ if (!openCodeDbExists())
4359
+ return 0;
4360
+ return withReadOnlySessionDb((db) => countStoredRawSessionMessagesFromDb(db, sessionId));
4361
+ }
4618
4362
  function readRawSessionMessageIdOrdinalsForRange2(sessionId, fromOrdinal, toOrdinal) {
4619
4363
  const from = Math.max(1, Math.floor(fromOrdinal));
4620
4364
  const to = Math.floor(toOrdinal);
4621
4365
  if (to < from)
4622
4366
  return new Map;
4623
- const provider = sessionProviders.get(sessionId);
4367
+ const provider = sessionProviders.get(sessionId)?.provider;
4624
4368
  if (provider?.readMessageIdOrdinalsForRange) {
4625
4369
  return provider.readMessageIdOrdinalsForRange(from, to);
4626
4370
  }
4627
4371
  const all = provider?.readMessageIdOrdinals ? provider.readMessageIdOrdinals() : provider ? new Map(provider.readMessages().map((message) => [message.id, message.ordinal])) : !openCodeDbExists() ? new Map : withReadOnlySessionDb((db) => readRawSessionMessageIdOrdinalsFromDb(db, sessionId));
4628
4372
  return new Map([...all].filter(([, ordinal]) => ordinal >= from && ordinal <= to));
4629
4373
  }
4374
+ function readRawSessionMessageIdOrdinals2(sessionId) {
4375
+ const count = getRawSessionMessageOrdinalCount2(sessionId);
4376
+ return readRawSessionMessageIdOrdinalsForRange2(sessionId, 1, count);
4377
+ }
4378
+ function readRawSessionMessagePartsById2(sessionId, messageId, onQuery) {
4379
+ const provider = sessionProviders.get(sessionId)?.provider;
4380
+ if (provider?.readMessagePartsById)
4381
+ return provider.readMessagePartsById(messageId);
4382
+ if (provider?.readMessageById)
4383
+ return provider.readMessageById(messageId);
4384
+ if (provider) {
4385
+ return provider.readMessages().find((message) => message.id === messageId) ?? null;
4386
+ }
4387
+ if (!openCodeDbExists())
4388
+ return null;
4389
+ return withReadOnlySessionDb((db) => readRawSessionMessagePartsByIdFromDb(db, sessionId, messageId, onQuery));
4390
+ }
4630
4391
  function hasRawSessionMessageById2(sessionId, messageId) {
4631
- const provider = sessionProviders.get(sessionId);
4392
+ const provider = sessionProviders.get(sessionId)?.provider;
4632
4393
  if (provider?.hasMessageById)
4633
4394
  return provider.hasMessageById(messageId);
4634
- return readRawSessionMessageById(sessionId, messageId) !== null;
4395
+ return readRawSessionMessageById2(sessionId, messageId) !== null;
4635
4396
  }
4636
4397
  function readRawSessionMessageOrdinalById2(sessionId, messageId) {
4637
- const provider = sessionProviders.get(sessionId);
4398
+ const provider = sessionProviders.get(sessionId)?.provider;
4638
4399
  if (provider?.readMessageOrdinalById) {
4639
4400
  return provider.readMessageOrdinalById(messageId);
4640
4401
  }
@@ -4671,7 +4432,7 @@ function readRawSessionMessageOrdinalById2(sessionId, messageId) {
4671
4432
  return withReadOnlySessionDb((db) => readRawSessionMessageOrdinalByIdFromDb(db, sessionId, messageId));
4672
4433
  }
4673
4434
  function compareRawSessionMessageOrder2(sessionId, leftId, rightId) {
4674
- const provider = sessionProviders.get(sessionId);
4435
+ const provider = sessionProviders.get(sessionId)?.provider;
4675
4436
  if (provider) {
4676
4437
  if (!provider.readMessageOrdinalById)
4677
4438
  return null;
@@ -4694,8 +4455,8 @@ function compareRawSessionMessageOrder2(sessionId, leftId, rightId) {
4694
4455
  return left.id < right.id ? -1 : left.id > right.id ? 1 : 0;
4695
4456
  });
4696
4457
  }
4697
- function readRawSessionMessageById(sessionId, messageId) {
4698
- const provider = sessionProviders.get(sessionId);
4458
+ function readRawSessionMessageById2(sessionId, messageId) {
4459
+ const provider = sessionProviders.get(sessionId)?.provider;
4699
4460
  if (provider?.readMessageById) {
4700
4461
  return provider.readMessageById(messageId);
4701
4462
  }
@@ -4707,7 +4468,7 @@ function readRawSessionMessageById(sessionId, messageId) {
4707
4468
  return withReadOnlySessionDb((db) => readRawSessionMessageByIdFromDb(db, sessionId, messageId));
4708
4469
  }
4709
4470
  function readRawSessionMessagesFromSource(sessionId) {
4710
- const provider = sessionProviders.get(sessionId);
4471
+ const provider = sessionProviders.get(sessionId)?.provider;
4711
4472
  if (provider)
4712
4473
  return provider.readMessages();
4713
4474
  if (!openCodeDbExists())
@@ -4715,7 +4476,7 @@ function readRawSessionMessagesFromSource(sessionId) {
4715
4476
  return withReadOnlySessionDb((db) => readRawSessionMessagesFromDb(db, sessionId));
4716
4477
  }
4717
4478
  function getRawSessionMessageCount2(sessionId) {
4718
- const provider = sessionProviders.get(sessionId);
4479
+ const provider = sessionProviders.get(sessionId)?.provider;
4719
4480
  if (provider) {
4720
4481
  if (provider.getMessageCount)
4721
4482
  return provider.getMessageCount();
@@ -4726,7 +4487,7 @@ function getRawSessionMessageCount2(sessionId) {
4726
4487
  return 0;
4727
4488
  return withReadOnlySessionDb((db) => getRawSessionMessageCountFromDb(db, sessionId));
4728
4489
  }
4729
- var RAW_SESSION_TAG_KEY_PAGE_SIZE = 32;
4490
+ var RAW_SESSION_TAG_KEY_PAGE_SIZE2 = 32;
4730
4491
  function yieldRawSessionTagKeyPage() {
4731
4492
  return new Promise((resolve) => {
4732
4493
  const immediate = globalThis.setImmediate;
@@ -4744,7 +4505,7 @@ async function getRawSessionTagKeysThrough2(sessionId, upToMessageIndex, options
4744
4505
  const candidateOwnersByCallId = new Map;
4745
4506
  const messageTimesById = new Map;
4746
4507
  const finalWatermark = Number.isFinite(upToMessageIndex) ? Math.max(0, Math.floor(upToMessageIndex)) : getRawSessionMessageOrdinalCount2(sessionId);
4747
- const pageSize = Number.isFinite(options.pageSize) ? Math.max(1, Math.floor(options.pageSize ?? RAW_SESSION_TAG_KEY_PAGE_SIZE)) : RAW_SESSION_TAG_KEY_PAGE_SIZE;
4508
+ const pageSize = Number.isFinite(options.pageSize) ? Math.max(1, Math.floor(options.pageSize ?? RAW_SESSION_TAG_KEY_PAGE_SIZE2)) : RAW_SESSION_TAG_KEY_PAGE_SIZE2;
4748
4509
  const yieldToEventLoop = options.yieldToEventLoop ?? yieldRawSessionTagKeyPage;
4749
4510
  const nearestPersistedOwner = (callId, currentMessageId) => {
4750
4511
  if (!options.db)
@@ -4842,11 +4603,42 @@ function getLegacyProtectedTailStartOrdinal2(sessionId) {
4842
4603
  }
4843
4604
  return userOrdinals.length < PROTECTED_TAIL_USER_TURNS ? 1 : userOrdinals[PROTECTED_TAIL_USER_TURNS - 1] ?? 1;
4844
4605
  }
4845
- function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdinal) {
4606
+ function getProtectedTailStartOrdinal2(sessionId) {
4607
+ return getLegacyProtectedTailStartOrdinal2(sessionId);
4608
+ }
4609
+ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdinal, options = {}) {
4846
4610
  const totalMessageCount = getCachedAbsoluteMessageCount2(sessionId) ?? getRawSessionMessageOrdinalCount2(sessionId);
4847
4611
  const startOrdinal = Math.max(1, offset);
4848
4612
  const finalOrdinal = eligibleEndOrdinal === undefined ? totalMessageCount : Math.min(totalMessageCount, eligibleEndOrdinal - 1);
4849
4613
  const messages = readRawSessionMessageRange2(sessionId, Math.max(1, startOrdinal - 1), finalOrdinal);
4614
+ const expandedParts = new Map;
4615
+ const expandedResults = new Set;
4616
+ if (options.expand !== false) {
4617
+ const calls = new Map;
4618
+ for (const message of messages)
4619
+ for (const part of message.parts) {
4620
+ if (!part || typeof part !== "object")
4621
+ continue;
4622
+ const p = part;
4623
+ if (p.type !== "tool")
4624
+ continue;
4625
+ const state = p.state;
4626
+ if (state?.input !== undefined && typeof p.callID === "string")
4627
+ calls.set(p.callID, p);
4628
+ expandedParts.set(part, expandToolPart(part, options.expandTools));
4629
+ if (state?.input === undefined && state?.output !== undefined && typeof p.callID === "string") {
4630
+ const call = calls.get(p.callID);
4631
+ if (call) {
4632
+ const expansion = expandToolPart({ ...call, state: { ...call.state, output: state.output } }, options.expandTools);
4633
+ if (expansion !== null) {
4634
+ expandedParts.set(call, expansion);
4635
+ expandedResults.add(part);
4636
+ }
4637
+ calls.delete(p.callID);
4638
+ }
4639
+ }
4640
+ }
4641
+ }
4850
4642
  const completedToolArcs = buildToolArcs(messages).flatMap((arc) => arc.resOrdinal === null ? [] : [{ start: arc.invOrdinal, end: arc.resOrdinal }]);
4851
4643
  const completedToolComponents = [];
4852
4644
  for (const arc of completedToolArcs) {
@@ -4954,8 +4746,13 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
4954
4746
  const meta = { ordinal: msg.ordinal, messageId: msg.id };
4955
4747
  if (isHostUnservedRow(msg))
4956
4748
  markHostUnservedRow(meta);
4957
- if (msg.role === "user" && !hasMeaningfulUserText2(msg.parts)) {
4958
- const tcSummaries = extractToolCallSummaries(msg.parts);
4749
+ if (msg.role === "user" && !hasMeaningfulChunkUserText(msg.parts)) {
4750
+ const tcSummaries = msg.parts.flatMap((part) => {
4751
+ if (expandedResults.has(part))
4752
+ return [];
4753
+ const expansion = expandedParts.get(part) ?? null;
4754
+ return expansion === null ? extractToolCallSummaries([part]) : [`TC: ${expansion}`];
4755
+ });
4959
4756
  if (tcSummaries.length === 0) {
4960
4757
  recordFilteredNoise(meta);
4961
4758
  continue;
@@ -4995,8 +4792,18 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
4995
4792
  }
4996
4793
  const role = compactRole(msg.role);
4997
4794
  const textParts = extractTexts2(msg.parts).map((t) => msg.role === "user" ? cleanUserText2(t) : t).map(normalizeText).filter((value) => value.length > 0);
4998
- const toolSummaries = textParts.length === 0 ? extractToolCallSummaries(msg.parts) : [];
4999
- const allParts = [...textParts, ...toolSummaries];
4795
+ const allParts = options.expand === false ? [
4796
+ ...textParts,
4797
+ ...textParts.length === 0 ? extractToolCallSummaries(msg.parts) : []
4798
+ ] : msg.parts.flatMap((part) => {
4799
+ if (expandedResults.has(part))
4800
+ return [];
4801
+ const expansion = expandedParts.get(part) ?? null;
4802
+ if (expansion !== null)
4803
+ return [`TC: ${expansion}`];
4804
+ const texts = extractTexts2([part]).map((t) => msg.role === "user" ? cleanUserText2(t) : t).map(normalizeText).filter(Boolean);
4805
+ return texts.length ? texts : textParts.length === 0 ? extractToolCallSummaries([part]) : [];
4806
+ });
5000
4807
  const compacted = compactTextForSummary(allParts.join(" / "), msg.role);
5001
4808
  const text = compacted.text;
5002
4809
  if (!text) {
@@ -5072,4 +4879,29 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
5072
4879
  toolResultBoundaries
5073
4880
  };
5074
4881
  }
5075
- export { registerSlowWriteReporter, detectSqliteRuntime, Database, withSqliteBackgroundWriter, withPrivilegedWriter, CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, stableStringify, estimateImageTokensFromDataUrl, TODO_STATUSES, TODO_PRIORITIES, TITLE_DONE_STATUSES, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, compareMeasuredTailPrefix, formatTailHygienePrefixMismatch, freezeTailHygieneMeasurement, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, measureEmergencyTag, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, getInertWhitespaceAssistantTags, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, retreatPastHostUnservedRows, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, closeQuietly, withReadOnlySessionDb, RAW_SUMMARY_TEXT_MAX_CHARS, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, hasUserAnswerMetadata, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, TOKEN_BUDGET_TOOL_REFUSAL, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, setDatabase, loadToolDefinitionMeasurements, resolveDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageIdOrdinalsForRange2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, getRawSessionMessageCount2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, readSessionChunk2 };
4882
+ function getRawSessionMessageIdsThrough2(sessionId, endOrdinal) {
4883
+ if (endOrdinal < 1)
4884
+ return [];
4885
+ return [...readRawSessionMessageIdOrdinalsForRange2(sessionId, 1, endOrdinal).entries()].sort((left, right) => left[1] - right[1]).map(([id]) => id);
4886
+ }
4887
+ function readRawSessionSeedTail2(sessionId, boundaryId, onQuery) {
4888
+ const provider = sessionProviders.get(sessionId)?.provider;
4889
+ if (provider) {
4890
+ const boundaryOrdinal = boundaryId === null ? 1 : readRawSessionMessageOrdinalById2(sessionId, boundaryId);
4891
+ if (boundaryOrdinal === null)
4892
+ throw new Error("state_sync materialized boundary is missing from raw provider");
4893
+ const messages = readRawSessionMessageRange2(sessionId, boundaryOrdinal, getRawSessionMessageOrdinalCount2(sessionId));
4894
+ return new Map(messages.map((message) => [message.id, message]));
4895
+ }
4896
+ if (!openCodeDbExists()) {
4897
+ if (boundaryId !== null)
4898
+ throw new Error("state_sync raw storage is unavailable for materialized boundary");
4899
+ return new Map;
4900
+ }
4901
+ return withReadOnlySessionDb((db) => {
4902
+ onQuery?.();
4903
+ return readRawSeedTailFromDb(db, sessionId, boundaryId);
4904
+ });
4905
+ }
4906
+
4907
+ export { detectThinkingBindingMismatch, isPrefixBoundThinkingModel, detectOverflow, resolveDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, TEXT_TAG_IDENTITY_MARKER, contentTagOwnerMessageId, estimateImageTokensFromDataUrl, TODO_STATUSES, TODO_PRIORITIES, TITLE_DONE_STATUSES, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, compareMeasuredTailPrefix, formatTailHygienePrefixMismatch, freezeTailHygieneMeasurement, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, measureEmergencyTag, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, getInertWhitespaceAssistantTags, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getTagById, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, withReadOnlySessionDb, expandToolPart, retreatPastHostUnservedRows, RAW_SUMMARY_TEXT_MAX_CHARS, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, stripSystemInjection, hasUserAnswerMetadata, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, TOKEN_BUDGET_TOOL_REFUSAL, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, modelAcceptsEmptyContent, variantChangeBustsProviderCache, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, setBoundedRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, RAW_MESSAGE_VISIT_PAGE_SIZE2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageOrdinalPage2, getRawSessionStoredMessageCount2, readRawSessionMessageIdOrdinalsForRange2, readRawSessionMessageIdOrdinals2, readRawSessionMessagePartsById2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, readRawSessionMessageById2, getRawSessionMessageCount2, RAW_SESSION_TAG_KEY_PAGE_SIZE2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, getProtectedTailStartOrdinal2, readSessionChunk2, getRawSessionMessageIdsThrough2, readRawSessionSeedTail2 };