@retaw106/mmc-pi 2.4.0 → 2.6.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.
@@ -2,7 +2,7 @@ import {
2
2
  getTagIdentityWriteGeneration,
3
3
  Database,
4
4
  getSqliteDatabasePath
5
- } from "./index-cr4hzd76.js";
5
+ } from "./index-vhs0y3s0.js";
6
6
  import {
7
7
  getHarness,
8
8
  harnessOwnsOpenCodeStore,
@@ -592,7 +592,27 @@ function isSystemDirective(text) {
592
592
  return text.trimStart().startsWith(SYSTEM_DIRECTIVE_PREFIX);
593
593
  }
594
594
  function removeSystemReminders(text) {
595
- return text.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/gi, "").trim();
595
+ return removeNestedBlocks(text, ["system-reminder"]).trim();
596
+ }
597
+ function removeNestedBlocks(text, names) {
598
+ const tags = new RegExp(`<(/?)(${names.join("|")})(?:\\s[^<>]*?)?\\s*(/?)>`, "gi");
599
+ const stack = [];
600
+ let end = 0;
601
+ let result = "";
602
+ for (const match of text.matchAll(tags)) {
603
+ if (stack.length === 0)
604
+ result += text.slice(end, match.index);
605
+ const name = match[2].toLowerCase();
606
+ if (match[1]) {
607
+ if (stack[stack.length - 1] === name)
608
+ stack.pop();
609
+ } else if (!match[3])
610
+ stack.push(name);
611
+ end = match.index + match[0].length;
612
+ }
613
+ if (stack.length === 0)
614
+ result += text.slice(end);
615
+ return result;
596
616
  }
597
617
 
598
618
  // ../plugin/src/hooks/magic-context/token-count-exact.ts
@@ -1255,13 +1275,16 @@ function stripDanglingTagNotationGlobally(value) {
1255
1275
  function stripTagSectionCharacters(value) {
1256
1276
  return value.replace(STRAY_SECTION_CHAR_REGEX, "");
1257
1277
  }
1258
- function stripPersistedAssistantText(value) {
1278
+ function stripPersistedAssistantText(value, acceptsEmpty = true) {
1279
+ if (!acceptsEmpty && !value.includes("§") && !value.trim())
1280
+ return value;
1259
1281
  let text = stripWellFormedLeadingTagPrefix(value);
1260
1282
  text = stripCompleteTagPairsGlobally(text);
1261
1283
  text = stripMalformedTagNotationGlobally(text);
1262
1284
  text = stripDanglingTagNotationGlobally(text);
1263
1285
  text = stripTagSectionCharacters(text);
1264
- return text.trim();
1286
+ const stripped = text.trim();
1287
+ return !acceptsEmpty && value.trim().length > 0 && stripped.length === 0 ? value : stripped;
1265
1288
  }
1266
1289
  function byteSize(value) {
1267
1290
  return encoder.encode(value).length;
@@ -2382,9 +2405,11 @@ function planEmergencyDrop(input) {
2382
2405
  const workingSpan = Math.max(ceilingTokens - fixedFloor, 0);
2383
2406
  const target = fixedFloor + TARGET_FRACTION * workingSpan;
2384
2407
  const reclaimTokens = Math.round(currentTotalInputTokens - target);
2385
- const floorNote = fixedFloor > ceilingTokens ? `; fixed floor ≈${Math.round(fixedFloor)} already exceeds ceiling ${Math.round(ceilingTokens)}: tool drops cannot reach the target` : "";
2408
+ if (fixedFloor > ceilingTokens) {
2409
+ return noop(`fixed floor ≈${Math.round(fixedFloor)} already exceeds ceiling ${Math.round(ceilingTokens)}: tool drops cannot reach the target`);
2410
+ }
2386
2411
  if (reclaimTokens <= EMERGENCY_REARM_MIN_TOKENS) {
2387
- return noop(`reclaim<=min (${reclaimTokens} <= ${EMERGENCY_REARM_MIN_TOKENS})${floorNote}`);
2412
+ return noop(`reclaim<=min (${reclaimTokens} <= ${EMERGENCY_REARM_MIN_TOKENS})`);
2388
2413
  }
2389
2414
  const cutoff = input.protectedCutoff;
2390
2415
  const windowYields = absoluteEmergency;
@@ -2439,16 +2464,16 @@ function planEmergencyDrop(input) {
2439
2464
  }
2440
2465
  }
2441
2466
  if (selected.length === 0) {
2442
- return noop(`no-candidates${floorNote}`);
2467
+ return noop("no-candidates");
2443
2468
  }
2444
2469
  if (!input.passAlreadyPriced && reclaimed < EMERGENCY_MIN_ACHIEVABLE_RECLAIM_TOKENS) {
2445
- return noop(`achievable reclaim below minimum (${selected.length} tags, reclaim≈${Math.round(reclaimed)} < ${EMERGENCY_MIN_ACHIEVABLE_RECLAIM_TOKENS} against gap ${reclaimTokens})${floorNote}`);
2470
+ return noop(`achievable reclaim below minimum (${selected.length} tags, reclaim≈${Math.round(reclaimed)} < ${EMERGENCY_MIN_ACHIEVABLE_RECLAIM_TOKENS} against gap ${reclaimTokens})`);
2446
2471
  }
2447
2472
  return {
2448
2473
  shouldDrop: true,
2449
2474
  tagNumbers: selected,
2450
2475
  reclaimTokens,
2451
- reason: `tiered drop: ${selected.length} tags, reclaim≈${reclaimed}/${reclaimTokens} tokens (floor≈${fixedFloor}, ceiling=${Math.round(ceilingTokens)})${floorNote}`
2476
+ reason: `tiered drop: ${selected.length} tags, reclaim≈${reclaimed}/${reclaimTokens} tokens (floor≈${fixedFloor}, ceiling=${Math.round(ceilingTokens)})`
2452
2477
  };
2453
2478
  }
2454
2479
  function measureEmergencyTag(tag, target, calibration, skeleton) {
@@ -4162,6 +4187,70 @@ function expandToolPart(part, overrides, legacyPreview = false) {
4162
4187
  return renderToolTemplate(template, input, state?.output ?? state?.error, legacyPreview);
4163
4188
  }
4164
4189
 
4190
+ // ../plugin/src/hooks/magic-context/ask-answer-notice.ts
4191
+ var ANSWER_HEADER_PREFIX = /^Inbox [^\n]* notice from user via user_answer \(user_answer\)/;
4192
+ var ANSWER_TRAILER = /\n([\s\S]*?)\nThis ask is now closed; no dismiss is needed\. Continue accordingly\.$/;
4193
+ var ANSWER_ANSWER_LINE = /^The user answered your parked question (\S+)\.\nQuestion: ([^\n]+)\nAnswer: /;
4194
+ var ANSWER_QUESTION_LINE = /^Question: ([^\n]+)\nAnswer: /;
4195
+ var reminderEnvelopePattern = /<system-reminder>([\s\S]*?)<\/system-reminder>/gi;
4196
+ function reminderBodies(text) {
4197
+ reminderEnvelopePattern.lastIndex = 0;
4198
+ const bodies = [];
4199
+ for (const match of text.matchAll(reminderEnvelopePattern)) {
4200
+ const body = match[1];
4201
+ if (body !== undefined)
4202
+ bodies.push(body);
4203
+ }
4204
+ return bodies;
4205
+ }
4206
+ function parseAnswerBody(body) {
4207
+ const lines = body.replace(/\r\n/g, `
4208
+ `).replace(/^\n+|\n+$/g, "");
4209
+ if (!ANSWER_HEADER_PREFIX.test(lines))
4210
+ return null;
4211
+ if (lines.includes("The user dismissed this question"))
4212
+ return null;
4213
+ const framed = ANSWER_TRAILER.exec(lines);
4214
+ if (framed === null)
4215
+ return null;
4216
+ const content = framed[1] ?? "";
4217
+ const narrated = ANSWER_ANSWER_LINE.exec(content);
4218
+ if (narrated !== null) {
4219
+ const question = narrated[2] ?? "";
4220
+ const answer = content.slice(narrated[0].length);
4221
+ return closedAnswer(question, answer);
4222
+ }
4223
+ const bare = ANSWER_QUESTION_LINE.exec(content);
4224
+ if (bare !== null) {
4225
+ const question = bare[1] ?? "";
4226
+ const answer = content.slice(bare[0].length);
4227
+ return closedAnswer(question, answer);
4228
+ }
4229
+ return null;
4230
+ }
4231
+ function closedAnswer(question, answer) {
4232
+ if (question.trim().length === 0)
4233
+ return null;
4234
+ if (answer.trim().length === 0)
4235
+ return null;
4236
+ return { question, answer };
4237
+ }
4238
+ function extractAskAnswerNotices(text) {
4239
+ const notices = [];
4240
+ for (const body of reminderBodies(text)) {
4241
+ const notice = parseAnswerBody(body);
4242
+ if (notice !== null)
4243
+ notices.push(notice);
4244
+ }
4245
+ return notices;
4246
+ }
4247
+ function formatAskAnswerNotice(notice) {
4248
+ return `U (answered "${notice.question}"): ${notice.answer}`;
4249
+ }
4250
+ function askAnswerLines(text) {
4251
+ return extractAskAnswerNotices(text).map(formatAskAnswerNotice);
4252
+ }
4253
+
4165
4254
  // ../plugin/src/hooks/magic-context/host-served-rows.ts
4166
4255
  var HOST_UNSERVED_ROW = "hostUnservedRow";
4167
4256
  function markHostUnservedRow(target) {
@@ -4748,6 +4837,9 @@ var ordinalWatermarks = new WeakMap;
4748
4837
  var ordinalWatermarkEpochs = new Map;
4749
4838
  var ordinalRowsVisited = 0;
4750
4839
  var STABLE_STORE_ATTEMPTS = 3;
4840
+ function forgetRawSessionOrdinalWatermark(sessionId) {
4841
+ ordinalWatermarkEpochs.set(sessionId, (ordinalWatermarkEpochs.get(sessionId) ?? 0) + 1);
4842
+ }
4751
4843
  function compareOrdinalKeys(left, right) {
4752
4844
  if (left.timeCreated !== right.timeCreated)
4753
4845
  return left.timeCreated < right.timeCreated ? -1 : 1;
@@ -4774,6 +4866,11 @@ var writtenSummaryIds = new Map;
4774
4866
  var indexedCountUnsupported = new WeakSet;
4775
4867
  var ordinalJsonRowsRead = 0;
4776
4868
  var summaryFullScans = 0;
4869
+ function forgetRawSessionSummaryRows(sessionId) {
4870
+ summaryCandidateEpochs.set(sessionId, (summaryCandidateEpochs.get(sessionId) ?? 0) + 1);
4871
+ writtenSummaryIds.delete(sessionId);
4872
+ forgetRawSessionOrdinalWatermark(sessionId);
4873
+ }
4777
4874
  var ANCHOR_ROWS_SQL = `SELECT rowid AS rid, id, 1 AS kind FROM message
4778
4875
  WHERE rowid > (SELECT MAX(rowid) FROM message) - ${SUMMARY_ANCHOR_SPAN}`;
4779
4876
  var SUMMARY_FULL_SCAN_SQL = `${ANCHOR_ROWS_SQL}
@@ -4794,6 +4891,20 @@ function survivingAnchor(db, set) {
4794
4891
  }
4795
4892
  var ordinalCheckpoints = new WeakMap;
4796
4893
  var ordinalSummarySeeds = new WeakMap;
4894
+ function hasLateCommitBehindCheckpoint(db, sessionId, scan) {
4895
+ const tail = scan.checkpoints?.at(-1);
4896
+ if (!tail || scan.maxRowid === undefined)
4897
+ return false;
4898
+ const row = db.prepare(`SELECT COUNT(*) AS count FROM message
4899
+ WHERE rowid > ? AND +session_id = ?
4900
+ AND (time_created, id) <= (?, ?)
4901
+ AND NOT (${EXCLUDED_ROW_SQL})`).get(scan.maxRowid, sessionId, tail.timeCreated, tail.id);
4902
+ return typeof row?.count === "number" && row.count > 0;
4903
+ }
4904
+ function discardRetainedOrdinalRanks(db, sessionId) {
4905
+ ordinalCheckpoints.get(db)?.delete(sessionId);
4906
+ ordinalSummarySeeds.get(db)?.delete(sessionId);
4907
+ }
4797
4908
  var ORDINAL_LOOKUP_LIMIT = 512;
4798
4909
  function currentOrdinalCheckpoints(db, sessionId) {
4799
4910
  const cached = ordinalCheckpoints.get(db)?.get(sessionId);
@@ -4824,10 +4935,16 @@ function extendOrdinalCheckpoints(db, sessionId, scan, limit) {
4824
4935
  scan.sessionRows = points.at(-1)?.storedCount ?? 0;
4825
4936
  }
4826
4937
  const tail = points.at(-1);
4827
- const rows = db.prepare(`SELECT id, time_created AS timeCreated, ${EXCLUDED_ROW_SQL} AS excluded FROM message
4938
+ const rows = db.prepare(`SELECT id, time_created AS timeCreated, rowid AS rowid, ${EXCLUDED_ROW_SQL} AS excluded FROM message
4828
4939
  WHERE session_id=?${tail ? " AND (time_created,id) > (?,?)" : ""}
4829
4940
  ORDER BY time_created,id${limit === undefined ? "" : ` LIMIT ${limit + 1}`}`).all(sessionId, ...tail ? [tail.timeCreated, tail.id] : []);
4830
4941
  ordinalJsonRowsRead += rows.length;
4942
+ if (hasLateCommitBehindCheckpoint(db, sessionId, scan)) {
4943
+ discardRetainedOrdinalRanks(db, sessionId);
4944
+ if (isMainThread)
4945
+ forgetRawSessionSummaryRows(sessionId);
4946
+ return false;
4947
+ }
4831
4948
  if (limit !== undefined && rows.length > limit)
4832
4949
  return false;
4833
4950
  let total = scan.total ?? 0;
@@ -4844,6 +4961,13 @@ function extendOrdinalCheckpoints(db, sessionId, scan, limit) {
4844
4961
  scan.checkpoints = points;
4845
4962
  scan.sessionRows += rows.length;
4846
4963
  scan.total = total;
4964
+ if (rows.length > 0) {
4965
+ let maxRowid = scan.maxRowid ?? -1;
4966
+ for (const row of rows)
4967
+ if (row.rowid > maxRowid)
4968
+ maxRowid = row.rowid;
4969
+ scan.maxRowid = maxRowid;
4970
+ }
4847
4971
  return true;
4848
4972
  }
4849
4973
  function warmRawSessionOrdinalsForDb(db, sessionId) {
@@ -4919,8 +5043,7 @@ function boundedOrdinalPrefixBounded(db, sessionId, through) {
4919
5043
  }
4920
5044
  function scanRawSessionSummaryRows(db, sessionId, previous) {
4921
5045
  return withReadSnapshot(db, () => {
4922
- if (previous?.checkpoints) {
4923
- extendOrdinalCheckpoints(db, sessionId, previous);
5046
+ if (previous?.checkpoints && extendOrdinalCheckpoints(db, sessionId, previous)) {
4924
5047
  return previous;
4925
5048
  }
4926
5049
  if (previous) {
@@ -4952,12 +5075,15 @@ function scanRawSessionSummaryRows(db, sessionId, previous) {
4952
5075
  }
4953
5076
  anchors.sort((left, right) => right.rowid - left.rowid);
4954
5077
  anchors.length = Math.min(anchors.length, SUMMARY_ANCHOR_LIMIT);
4955
- const keys = db.prepare(`SELECT id, time_created AS timeCreated, ${EXCLUDED_ROW_SQL} AS excluded FROM message WHERE session_id = ?
5078
+ const keys = db.prepare(`SELECT id, time_created AS timeCreated, rowid AS rowid, ${EXCLUDED_ROW_SQL} AS excluded FROM message WHERE session_id = ?
4956
5079
  ORDER BY time_created, id`).all(sessionId);
4957
5080
  const checkpoints = [];
4958
5081
  let total = 0;
5082
+ let maxRowid;
4959
5083
  for (let index = 0;index < keys.length; index++) {
4960
5084
  const key = keys[index];
5085
+ if (typeof key.rowid === "number")
5086
+ maxRowid = maxRowid === undefined ? key.rowid : Math.max(maxRowid, key.rowid);
4961
5087
  if (key.excluded !== 1)
4962
5088
  total++;
4963
5089
  if (index % 256 === 255 || index === keys.length - 1)
@@ -4968,7 +5094,7 @@ function scanRawSessionSummaryRows(db, sessionId, previous) {
4968
5094
  storedCount: index + 1
4969
5095
  });
4970
5096
  }
4971
- return { anchors, ids, sessionRows, checkpoints, total };
5097
+ return { anchors, ids, sessionRows, checkpoints, total, maxRowid };
4972
5098
  });
4973
5099
  }
4974
5100
  function getRawSessionSummaryEpoch(sessionId) {
@@ -5221,48 +5347,53 @@ function hasRawSessionMessageByIdFromDb(db, sessionId, messageId) {
5221
5347
  var boundaryEndpointCaches = new WeakMap;
5222
5348
 
5223
5349
  // ../plugin/src/hooks/magic-context/read-session-db.ts
5224
- function openCodeDbExists() {
5225
- return harnessOwnsOpenCodeStore() && openCodeDbPathExists(resolveOpenCodeDbPath());
5350
+ function openCodeDbExists(generation = "v1") {
5351
+ return harnessOwnsOpenCodeStore() && openCodeDbPathExists(resolveOpenCodeDbPath(generation));
5226
5352
  }
5227
- var cachedReadOnlyDb = null;
5228
- function closeCachedReadOnlyDb() {
5229
- if (!cachedReadOnlyDb) {
5353
+ var cachedReadOnlyDbs = {
5354
+ v1: null,
5355
+ v2: null
5356
+ };
5357
+ function closeCachedReadOnlyDb(generation) {
5358
+ const cached = cachedReadOnlyDbs[generation];
5359
+ if (!cached) {
5230
5360
  return;
5231
5361
  }
5232
5362
  try {
5233
- closeQuietly(cachedReadOnlyDb.db);
5363
+ closeQuietly(cached.db);
5234
5364
  } catch (error) {
5235
5365
  log("[magic-context] failed to close cached OpenCode read-only DB:", error);
5236
5366
  } finally {
5237
- cachedReadOnlyDb = null;
5367
+ cachedReadOnlyDbs[generation] = null;
5238
5368
  }
5239
5369
  }
5240
- function getReadOnlySessionDb() {
5370
+ function getReadOnlySessionDb(generation = "v1") {
5241
5371
  if (!harnessOwnsOpenCodeStore()) {
5242
5372
  throw new Error("OpenCode session database is not readable from a Pi-compatible process; its history lives in Pi sessions");
5243
5373
  }
5244
- const resolution = resolveOpenCodeDbPath();
5374
+ const resolution = resolveOpenCodeDbPath(generation);
5245
5375
  const dbPath = resolution.path;
5246
5376
  if (!openCodeDbPathExists(resolution)) {
5247
5377
  throw new Error(`OpenCode session database is unavailable at ${dbPath} (source=${resolution.source})`);
5248
5378
  }
5249
- if (cachedReadOnlyDb?.path === dbPath) {
5250
- return cachedReadOnlyDb.db;
5379
+ const cached = cachedReadOnlyDbs[generation];
5380
+ if (cached?.path === dbPath) {
5381
+ return cached.db;
5251
5382
  }
5252
- closeCachedReadOnlyDb();
5383
+ closeCachedReadOnlyDb(generation);
5253
5384
  const db = new Database(dbPath, { readonly: true });
5254
5385
  try {
5255
- assertOpenCodeStoreGeneration(db, "v1", dbPath);
5386
+ assertOpenCodeStoreGeneration(db, generation, dbPath);
5256
5387
  } catch (error) {
5257
5388
  closeQuietly(db);
5258
5389
  throw error;
5259
5390
  }
5260
- cachedReadOnlyDb = { path: dbPath, db };
5391
+ cachedReadOnlyDbs[generation] = { path: dbPath, db };
5261
5392
  clearOpenCodeDbReadFailure();
5262
5393
  return db;
5263
5394
  }
5264
- function withReadOnlySessionDb(fn) {
5265
- return fn(getReadOnlySessionDb());
5395
+ function withReadOnlySessionDb(fn, generation = "v1") {
5396
+ return fn(getReadOnlySessionDb(generation));
5266
5397
  }
5267
5398
  function getRawSessionMessageCountFromDb(db, sessionId) {
5268
5399
  const row = db.prepare(`SELECT COUNT(*) as count FROM message WHERE session_id = ?
@@ -5286,15 +5417,14 @@ function getMessageTimesFromOpenCodeDb(sessionId, messageIds) {
5286
5417
  if (messageIds.length === 0 || !harnessOwnsOpenCodeStore())
5287
5418
  return result;
5288
5419
  try {
5289
- withReadOnlySessionDb((db) => {
5290
- const placeholders = messageIds.map(() => "?").join(",");
5291
- const rows = db.prepare(`SELECT id, time_created FROM message WHERE +session_id = ? AND id IN (${placeholders})`).all(sessionId, ...messageIds);
5292
- for (const row of rows) {
5293
- if (typeof row.id === "string" && typeof row.time_created === "number") {
5294
- result.set(row.id, row.time_created);
5295
- }
5420
+ const placeholders = messageIds.map(() => "?").join(",");
5421
+ const rows = getHarness() === "opencode2" ? getReadOnlySessionDb("v2").prepare(`SELECT id, COALESCE(time_created, json_extract(data, '$.time.created')) AS time_created
5422
+ FROM session_message WHERE +session_id = ? AND id IN (${placeholders})`).all(sessionId, ...messageIds) : withReadOnlySessionDb((db) => db.prepare(`SELECT id, time_created FROM message WHERE +session_id = ? AND id IN (${placeholders})`).all(sessionId, ...messageIds));
5423
+ for (const row of rows) {
5424
+ if (typeof row.id === "string" && typeof row.time_created === "number") {
5425
+ result.set(row.id, row.time_created);
5296
5426
  }
5297
- });
5427
+ }
5298
5428
  } catch (error) {
5299
5429
  logProbeFailureOnce(resolveOpenCodeDbPath(), error);
5300
5430
  }
@@ -6131,34 +6261,170 @@ function applyEditMarkerToInput(input) {
6131
6261
  }
6132
6262
  }
6133
6263
 
6264
+ // ../plugin/src/shared/bounded-session-map.ts
6265
+ class BoundedSessionMap {
6266
+ maxEntries;
6267
+ store = new Map;
6268
+ constructor(maxEntries) {
6269
+ if (!Number.isFinite(maxEntries) || maxEntries < 1) {
6270
+ throw new Error(`BoundedSessionMap: maxEntries must be >= 1, got ${maxEntries}`);
6271
+ }
6272
+ this.maxEntries = maxEntries;
6273
+ }
6274
+ get(sessionId) {
6275
+ const value = this.store.get(sessionId);
6276
+ if (value === undefined)
6277
+ return;
6278
+ this.store.delete(sessionId);
6279
+ this.store.set(sessionId, value);
6280
+ return value;
6281
+ }
6282
+ peek(sessionId) {
6283
+ return this.store.get(sessionId);
6284
+ }
6285
+ has(sessionId) {
6286
+ return this.store.has(sessionId);
6287
+ }
6288
+ set(sessionId, value) {
6289
+ if (this.store.has(sessionId)) {
6290
+ this.store.delete(sessionId);
6291
+ } else if (this.store.size >= this.maxEntries) {
6292
+ const oldest = this.store.keys().next().value;
6293
+ if (oldest !== undefined)
6294
+ this.store.delete(oldest);
6295
+ }
6296
+ this.store.set(sessionId, value);
6297
+ }
6298
+ delete(sessionId) {
6299
+ return this.store.delete(sessionId);
6300
+ }
6301
+ clear() {
6302
+ this.store.clear();
6303
+ }
6304
+ get size() {
6305
+ return this.store.size;
6306
+ }
6307
+ }
6308
+
6134
6309
  // ../plugin/src/features/magic-context/tool-definition-tokens.ts
6135
- var measurements = new Map;
6136
- var fingerprints = new Map;
6310
+ var ACTIVITY_WINDOW_MS = 10 * 60000;
6311
+ var MAX_ACTIVITY_GAP_MS = 60000;
6312
+ var WINDOWED_SINCE_KEY = "tool_definition_measurements:windowed_since";
6313
+ var routes = new Map;
6314
+ var sessionFlights = new BoundedSessionMap(1000);
6137
6315
  var persistenceDb = null;
6316
+ var windowedSince;
6317
+ var boundAt = 0;
6138
6318
  var cachedInsertStmt = null;
6319
+ var cachedDeleteStmt = null;
6320
+ function agentKey(agentName) {
6321
+ return agentName && agentName.length > 0 ? agentName : "default";
6322
+ }
6139
6323
  function keyFor(providerID, modelID, agentName) {
6140
- const agent = agentName && agentName.length > 0 ? agentName : "default";
6141
- return `${providerID}/${modelID}/${agent}`;
6324
+ return `${providerID}/${modelID}/${agentKey(agentName)}`;
6325
+ }
6326
+ function routeFor(providerID, modelID, agentName) {
6327
+ const key = keyFor(providerID, modelID, agentName);
6328
+ let route = routes.get(key);
6329
+ if (!route) {
6330
+ route = {
6331
+ providerID,
6332
+ modelID,
6333
+ agentName: agentKey(agentName),
6334
+ tools: new Map,
6335
+ live: false,
6336
+ clock: 0,
6337
+ lastWallAt: Date.now(),
6338
+ oldestFireAt: Number.POSITIVE_INFINITY,
6339
+ retired: new Map,
6340
+ legacyRows: new Set
6341
+ };
6342
+ routes.set(key, route);
6343
+ }
6344
+ return { key, route };
6345
+ }
6346
+ function advanceClock(route) {
6347
+ const now = Date.now();
6348
+ const gap = Math.min(Math.max(now - route.lastWallAt, 0), MAX_ACTIVITY_GAP_MS);
6349
+ route.clock += gap;
6350
+ route.lastWallAt = Math.max(route.lastWallAt, now);
6351
+ return route.clock;
6352
+ }
6353
+ function readWindowedSince(db) {
6354
+ try {
6355
+ const row = db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(WINDOWED_SINCE_KEY);
6356
+ if (!row)
6357
+ return;
6358
+ const value = Number(row.value);
6359
+ return Number.isFinite(value) ? value : undefined;
6360
+ } catch {
6361
+ return;
6362
+ }
6363
+ }
6364
+ function markWindowed() {
6365
+ if (windowedSince !== undefined || !persistenceDb)
6366
+ return;
6367
+ try {
6368
+ persistenceDb.prepare("INSERT OR IGNORE INTO schema_migrations_meta (key, value) VALUES (?, ?)").run(WINDOWED_SINCE_KEY, String(boundAt));
6369
+ windowedSince = readWindowedSince(persistenceDb);
6370
+ } catch {}
6142
6371
  }
6143
6372
  function setDatabase(db) {
6373
+ const rebound = persistenceDb !== null && persistenceDb !== db;
6144
6374
  persistenceDb = db;
6375
+ boundAt = Date.now();
6145
6376
  cachedInsertStmt = null;
6377
+ cachedDeleteStmt = null;
6378
+ if (rebound) {
6379
+ for (const route of routes.values()) {
6380
+ for (const slot of route.tools.values()) {
6381
+ slot.persisted = false;
6382
+ slot.writeFailedAt = undefined;
6383
+ }
6384
+ for (const retired of route.retired.values())
6385
+ retired.rowKept = false;
6386
+ route.legacyRows.clear();
6387
+ }
6388
+ }
6389
+ windowedSince = readWindowedSince(db);
6390
+ if (windowedSince !== undefined)
6391
+ return;
6392
+ try {
6393
+ if (!db.prepare("SELECT 1 FROM tool_definition_measurements LIMIT 1").get())
6394
+ markWindowed();
6395
+ } catch {}
6146
6396
  }
6147
6397
  function loadToolDefinitionMeasurements(db) {
6148
6398
  let rows = [];
6149
6399
  try {
6150
- rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count FROM tool_definition_measurements").all();
6400
+ rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count, recorded_at FROM tool_definition_measurements").all();
6151
6401
  } catch {
6152
6402
  return;
6153
6403
  }
6404
+ const since = readWindowedSince(db);
6405
+ const loadedAt = new Map;
6154
6406
  for (const row of rows) {
6155
- const key = keyFor(row.provider_id, row.model_id, row.agent_name);
6156
- let inner = measurements.get(key);
6157
- if (!inner) {
6158
- inner = new Map;
6159
- measurements.set(key, inner);
6160
- }
6161
- inner.set(row.tool_id, row.token_count);
6407
+ const { route } = routeFor(row.provider_id, row.model_id, row.agent_name);
6408
+ if (route.live)
6409
+ continue;
6410
+ let now = loadedAt.get(route);
6411
+ if (now === undefined) {
6412
+ now = advanceClock(route);
6413
+ loadedAt.set(route, now);
6414
+ }
6415
+ const legacy = since === undefined || !(Number(row.recorded_at) >= since);
6416
+ route.tools.set(row.tool_id, {
6417
+ tokens: row.token_count,
6418
+ fingerprint: undefined,
6419
+ lastFireAt: now,
6420
+ legacy,
6421
+ persisted: !legacy
6422
+ });
6423
+ route.oldestFireAt = Math.min(route.oldestFireAt, now);
6424
+ if (legacy)
6425
+ route.legacyRows.add(row.tool_id);
6426
+ else
6427
+ route.legacyRows.delete(row.tool_id);
6162
6428
  }
6163
6429
  }
6164
6430
 
@@ -6596,9 +6862,11 @@ function withRawMessageProvider2(sessionId, provider, fn) {
6596
6862
  cleanup();
6597
6863
  return result;
6598
6864
  }
6599
- var SYSTEM_REMINDER_BLOCK_REGEX = /<system-reminder>[\s\S]*?<\/system-reminder>/gi;
6600
6865
  function cleanUserText2(text) {
6601
- return stripOutsideSteeringWrappers(text, (segment) => segment.replace(SYSTEM_REMINDER_BLOCK_REGEX, "").replace(OMO_INTERNAL_INITIATOR_MARKER, "")).trim();
6866
+ return stripOutsideSteeringWrappers(text, (segment) => removeSystemReminders(segment).replace(OMO_INTERNAL_INITIATOR_MARKER, "")).trim();
6867
+ }
6868
+ function answerLineParts(rawText) {
6869
+ return askAnswerLines(rawText).map(normalizeText).filter((value) => value.length > 0);
6602
6870
  }
6603
6871
  function hasMeaningfulChunkUserText(parts) {
6604
6872
  for (const part of parts) {
@@ -6616,7 +6884,7 @@ function hasMeaningfulChunkUserText(parts) {
6616
6884
  continue;
6617
6885
  return true;
6618
6886
  }
6619
- return false;
6887
+ return parts.some((part) => part !== null && typeof part === "object" && part.type === "text" && typeof part.text === "string" && part.ignored !== true && askAnswerLines(part.text).length > 0);
6620
6888
  }
6621
6889
  function withRawSessionMessageCache2(fn) {
6622
6890
  const outerCache = activeRawMessageCache;
@@ -7222,6 +7490,10 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
7222
7490
  const meta = { ordinal: msg.ordinal, messageId: msg.id };
7223
7491
  if (isHostUnservedRow(msg))
7224
7492
  markHostUnservedRow(meta);
7493
+ if (msg.role === "system") {
7494
+ recordFilteredNoise(meta);
7495
+ continue;
7496
+ }
7225
7497
  if (msg.role === "user" && !hasMeaningfulChunkUserText(msg.parts)) {
7226
7498
  const tcSummaries = msg.parts.flatMap((part) => {
7227
7499
  if (expandedResults.has(part))
@@ -7268,8 +7540,10 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
7268
7540
  }
7269
7541
  const role = compactRole(msg.role);
7270
7542
  const textParts = extractTexts2(msg.parts).map((t) => msg.role === "user" ? cleanUserText2(t) : t).map(normalizeText).filter((value) => value.length > 0);
7543
+ const answerParts = msg.role === "user" ? extractTexts2(msg.parts).flatMap((text) => answerLineParts(text)) : [];
7544
+ const userParts = answerParts.length > 0 ? [...textParts, ...answerParts] : textParts;
7271
7545
  const allParts = options.expand === false ? [
7272
- ...textParts,
7546
+ ...userParts,
7273
7547
  ...textParts.length === 0 ? extractToolCallSummaries(msg.parts) : []
7274
7548
  ] : msg.parts.flatMap((part) => {
7275
7549
  if (expandedResults.has(part))
@@ -7278,6 +7552,9 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
7278
7552
  if (expansion !== null)
7279
7553
  return [`TC: ${expansion}`];
7280
7554
  const texts = extractTexts2([part]).map((t) => msg.role === "user" ? cleanUserText2(t) : t).map(normalizeText).filter(Boolean);
7555
+ if (msg.role === "user") {
7556
+ texts.push(...extractTexts2([part]).flatMap((text) => answerLineParts(text)));
7557
+ }
7281
7558
  return texts.length ? texts : textParts.length === 0 ? extractToolCallSummaries([part]) : [];
7282
7559
  });
7283
7560
  const compacted = compactTextForSummary(allParts.join(" / "), msg.role);
@@ -7286,7 +7563,7 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
7286
7563
  recordFilteredNoise(meta);
7287
7564
  continue;
7288
7565
  }
7289
- const msgHasNarrative = textParts.length > 0;
7566
+ const msgHasNarrative = userParts.length > 0;
7290
7567
  if (currentBlock && currentBlock.role === role) {
7291
7568
  currentBlock.endOrdinal = msg.ordinal;
7292
7569
  currentBlock.parts.push(text);
@@ -7368,4 +7645,4 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
7368
7645
  toolResultBoundaries
7369
7646
  };
7370
7647
  }
7371
- export { canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, toolTemplateError, DEFAULT_PROTECTED_TOOLS, normalizeProtectedToolName, mergeProtectedTools, detectThinkingBindingMismatch, detectLatestTurnThinkingMismatch, isPrefixBoundThinkingModel, detectOverflow, stableStringify, UNKNOWN_FIT_RATIO, resolveDecisionCalibration, hasMeasuredDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, adoptedProtectedToolsPolicy, protectedToolTagNumbers, protectedToolTokenCount, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, preloadTokenizer, hasTokenizerForFit, tokenCountUsesByteBound, estimateTokens, estimateFixedPromptTokens, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, rememberCurrentToolOutputCount, isRecord, TEXT_TAG_IDENTITY_MARKER, PI_FALLBACK_PREFIXES_JSON, contentTagOwnerMessageId, TailHygieneJsonMemo, piJsonHash, estimateImageTokensFromDataUrl, providerVisibleMessage, 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, closeQuietly, isKnownAutocommit, installV3PerfSchema, queuePendingOp, queuePendingDropForUnchangedTag, getPendingOps, getPendingOpsCount, clearPendingOps, removePendingOp, tagRevisionIncarnationSql, tagRevisionCountSql, tagRevisionKey, tagIdentityRevisionKey, TAG_INSERT_CONFLICT_ROWS, installTagIdentityRevisionTrigger, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, getReasoningTokenEstimatesByMessage, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, markTagDropped, getInertWhitespaceAssistantTagsByNumbers, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, PiTagIdentityConflictError, getPiMessageTagCandidates, findPiTagIdentityConflict, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getNewestToolTagNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getTagById, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, withReadOnlySessionDb, expandToolPart, retreatPastHostUnservedRows, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, stripSystemInjection, hasUserAnswerMetadata, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, setDatabase, loadToolDefinitionMeasurements, estimatePartImageTokens, modelAcceptsEmptyContent, isAnthropicFamilyRoute, variantChangeBustsProviderCache, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageIdOrdinalsForRange2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, readRawSessionMessageById2, getRawSessionMessageCount2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, readSessionChunk2 };
7648
+ export { canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, toolTemplateError, DEFAULT_PROTECTED_TOOLS, normalizeProtectedToolName, mergeProtectedTools, detectThinkingBindingMismatch, detectLatestTurnThinkingMismatch, isPrefixBoundThinkingModel, detectOverflow, stableStringify, UNKNOWN_FIT_RATIO, resolveDecisionCalibration, hasMeasuredDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, adoptedProtectedToolsPolicy, protectedToolTagNumbers, protectedToolTokenCount, removeSystemReminders, removeNestedBlocks, hasMeaningfulUserText2, extractTexts2, preloadTokenizer, hasTokenizerForFit, tokenCountUsesByteBound, estimateTokens, estimateFixedPromptTokens, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, rememberCurrentToolOutputCount, isRecord, TEXT_TAG_IDENTITY_MARKER, PI_FALLBACK_PREFIXES_JSON, contentTagOwnerMessageId, TailHygieneJsonMemo, piJsonHash, estimateImageTokensFromDataUrl, providerVisibleMessage, 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, closeQuietly, isKnownAutocommit, installV3PerfSchema, queuePendingOp, queuePendingDropForUnchangedTag, getPendingOps, getPendingOpsCount, clearPendingOps, removePendingOp, tagRevisionIncarnationSql, tagRevisionCountSql, tagRevisionKey, tagIdentityRevisionKey, TAG_INSERT_CONFLICT_ROWS, installTagIdentityRevisionTrigger, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, getReasoningTokenEstimatesByMessage, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, markTagDropped, getInertWhitespaceAssistantTagsByNumbers, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, PiTagIdentityConflictError, getPiMessageTagCandidates, findPiTagIdentityConflict, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getNewestToolTagNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getTagById, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, BoundedSessionMap, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, withReadOnlySessionDb, expandToolPart, retreatPastHostUnservedRows, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, stripSystemInjection, isFilePart, hasUserAnswerMetadata, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, setDatabase, loadToolDefinitionMeasurements, estimatePartImageTokens, modelAcceptsEmptyContent, isAnthropicFamilyRoute, variantChangeBustsProviderCache, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageIdOrdinalsForRange2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, readRawSessionMessageById2, getRawSessionMessageCount2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, readSessionChunk2 };