@retaw106/mmc-pi 2.5.0 → 2.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4187,6 +4187,70 @@ function expandToolPart(part, overrides, legacyPreview = false) {
|
|
|
4187
4187
|
return renderToolTemplate(template, input, state?.output ?? state?.error, legacyPreview);
|
|
4188
4188
|
}
|
|
4189
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
|
+
|
|
4190
4254
|
// ../plugin/src/hooks/magic-context/host-served-rows.ts
|
|
4191
4255
|
var HOST_UNSERVED_ROW = "hostUnservedRow";
|
|
4192
4256
|
function markHostUnservedRow(target) {
|
|
@@ -4209,10 +4273,6 @@ function retreatPastHostUnservedRows(messages, exclusiveEnd, floor) {
|
|
|
4209
4273
|
end -= 1;
|
|
4210
4274
|
return end;
|
|
4211
4275
|
}
|
|
4212
|
-
var COVERED_INSTRUCTION_ROW = "coveredInstructionRow";
|
|
4213
|
-
function isCoveredInstructionRow(value) {
|
|
4214
|
-
return typeof value === "object" && value !== null && value[COVERED_INSTRUCTION_ROW] === true;
|
|
4215
|
-
}
|
|
4216
4276
|
|
|
4217
4277
|
// ../plugin/src/shared/opencode-db-path.ts
|
|
4218
4278
|
import { existsSync as existsSync2, readdirSync, statSync as statSync2 } from "node:fs";
|
|
@@ -4777,6 +4837,9 @@ var ordinalWatermarks = new WeakMap;
|
|
|
4777
4837
|
var ordinalWatermarkEpochs = new Map;
|
|
4778
4838
|
var ordinalRowsVisited = 0;
|
|
4779
4839
|
var STABLE_STORE_ATTEMPTS = 3;
|
|
4840
|
+
function forgetRawSessionOrdinalWatermark(sessionId) {
|
|
4841
|
+
ordinalWatermarkEpochs.set(sessionId, (ordinalWatermarkEpochs.get(sessionId) ?? 0) + 1);
|
|
4842
|
+
}
|
|
4780
4843
|
function compareOrdinalKeys(left, right) {
|
|
4781
4844
|
if (left.timeCreated !== right.timeCreated)
|
|
4782
4845
|
return left.timeCreated < right.timeCreated ? -1 : 1;
|
|
@@ -4803,6 +4866,11 @@ var writtenSummaryIds = new Map;
|
|
|
4803
4866
|
var indexedCountUnsupported = new WeakSet;
|
|
4804
4867
|
var ordinalJsonRowsRead = 0;
|
|
4805
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
|
+
}
|
|
4806
4874
|
var ANCHOR_ROWS_SQL = `SELECT rowid AS rid, id, 1 AS kind FROM message
|
|
4807
4875
|
WHERE rowid > (SELECT MAX(rowid) FROM message) - ${SUMMARY_ANCHOR_SPAN}`;
|
|
4808
4876
|
var SUMMARY_FULL_SCAN_SQL = `${ANCHOR_ROWS_SQL}
|
|
@@ -4823,6 +4891,20 @@ function survivingAnchor(db, set) {
|
|
|
4823
4891
|
}
|
|
4824
4892
|
var ordinalCheckpoints = new WeakMap;
|
|
4825
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
|
+
}
|
|
4826
4908
|
var ORDINAL_LOOKUP_LIMIT = 512;
|
|
4827
4909
|
function currentOrdinalCheckpoints(db, sessionId) {
|
|
4828
4910
|
const cached = ordinalCheckpoints.get(db)?.get(sessionId);
|
|
@@ -4853,10 +4935,16 @@ function extendOrdinalCheckpoints(db, sessionId, scan, limit) {
|
|
|
4853
4935
|
scan.sessionRows = points.at(-1)?.storedCount ?? 0;
|
|
4854
4936
|
}
|
|
4855
4937
|
const tail = points.at(-1);
|
|
4856
|
-
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
|
|
4857
4939
|
WHERE session_id=?${tail ? " AND (time_created,id) > (?,?)" : ""}
|
|
4858
4940
|
ORDER BY time_created,id${limit === undefined ? "" : ` LIMIT ${limit + 1}`}`).all(sessionId, ...tail ? [tail.timeCreated, tail.id] : []);
|
|
4859
4941
|
ordinalJsonRowsRead += rows.length;
|
|
4942
|
+
if (hasLateCommitBehindCheckpoint(db, sessionId, scan)) {
|
|
4943
|
+
discardRetainedOrdinalRanks(db, sessionId);
|
|
4944
|
+
if (isMainThread)
|
|
4945
|
+
forgetRawSessionSummaryRows(sessionId);
|
|
4946
|
+
return false;
|
|
4947
|
+
}
|
|
4860
4948
|
if (limit !== undefined && rows.length > limit)
|
|
4861
4949
|
return false;
|
|
4862
4950
|
let total = scan.total ?? 0;
|
|
@@ -4873,6 +4961,13 @@ function extendOrdinalCheckpoints(db, sessionId, scan, limit) {
|
|
|
4873
4961
|
scan.checkpoints = points;
|
|
4874
4962
|
scan.sessionRows += rows.length;
|
|
4875
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
|
+
}
|
|
4876
4971
|
return true;
|
|
4877
4972
|
}
|
|
4878
4973
|
function warmRawSessionOrdinalsForDb(db, sessionId) {
|
|
@@ -4948,8 +5043,7 @@ function boundedOrdinalPrefixBounded(db, sessionId, through) {
|
|
|
4948
5043
|
}
|
|
4949
5044
|
function scanRawSessionSummaryRows(db, sessionId, previous) {
|
|
4950
5045
|
return withReadSnapshot(db, () => {
|
|
4951
|
-
if (previous?.checkpoints) {
|
|
4952
|
-
extendOrdinalCheckpoints(db, sessionId, previous);
|
|
5046
|
+
if (previous?.checkpoints && extendOrdinalCheckpoints(db, sessionId, previous)) {
|
|
4953
5047
|
return previous;
|
|
4954
5048
|
}
|
|
4955
5049
|
if (previous) {
|
|
@@ -4981,12 +5075,15 @@ function scanRawSessionSummaryRows(db, sessionId, previous) {
|
|
|
4981
5075
|
}
|
|
4982
5076
|
anchors.sort((left, right) => right.rowid - left.rowid);
|
|
4983
5077
|
anchors.length = Math.min(anchors.length, SUMMARY_ANCHOR_LIMIT);
|
|
4984
|
-
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 = ?
|
|
4985
5079
|
ORDER BY time_created, id`).all(sessionId);
|
|
4986
5080
|
const checkpoints = [];
|
|
4987
5081
|
let total = 0;
|
|
5082
|
+
let maxRowid;
|
|
4988
5083
|
for (let index = 0;index < keys.length; index++) {
|
|
4989
5084
|
const key = keys[index];
|
|
5085
|
+
if (typeof key.rowid === "number")
|
|
5086
|
+
maxRowid = maxRowid === undefined ? key.rowid : Math.max(maxRowid, key.rowid);
|
|
4990
5087
|
if (key.excluded !== 1)
|
|
4991
5088
|
total++;
|
|
4992
5089
|
if (index % 256 === 255 || index === keys.length - 1)
|
|
@@ -4997,7 +5094,7 @@ function scanRawSessionSummaryRows(db, sessionId, previous) {
|
|
|
4997
5094
|
storedCount: index + 1
|
|
4998
5095
|
});
|
|
4999
5096
|
}
|
|
5000
|
-
return { anchors, ids, sessionRows, checkpoints, total };
|
|
5097
|
+
return { anchors, ids, sessionRows, checkpoints, total, maxRowid };
|
|
5001
5098
|
});
|
|
5002
5099
|
}
|
|
5003
5100
|
function getRawSessionSummaryEpoch(sessionId) {
|
|
@@ -5250,48 +5347,53 @@ function hasRawSessionMessageByIdFromDb(db, sessionId, messageId) {
|
|
|
5250
5347
|
var boundaryEndpointCaches = new WeakMap;
|
|
5251
5348
|
|
|
5252
5349
|
// ../plugin/src/hooks/magic-context/read-session-db.ts
|
|
5253
|
-
function openCodeDbExists() {
|
|
5254
|
-
return harnessOwnsOpenCodeStore() && openCodeDbPathExists(resolveOpenCodeDbPath());
|
|
5350
|
+
function openCodeDbExists(generation = "v1") {
|
|
5351
|
+
return harnessOwnsOpenCodeStore() && openCodeDbPathExists(resolveOpenCodeDbPath(generation));
|
|
5255
5352
|
}
|
|
5256
|
-
var
|
|
5257
|
-
|
|
5258
|
-
|
|
5353
|
+
var cachedReadOnlyDbs = {
|
|
5354
|
+
v1: null,
|
|
5355
|
+
v2: null
|
|
5356
|
+
};
|
|
5357
|
+
function closeCachedReadOnlyDb(generation) {
|
|
5358
|
+
const cached = cachedReadOnlyDbs[generation];
|
|
5359
|
+
if (!cached) {
|
|
5259
5360
|
return;
|
|
5260
5361
|
}
|
|
5261
5362
|
try {
|
|
5262
|
-
closeQuietly(
|
|
5363
|
+
closeQuietly(cached.db);
|
|
5263
5364
|
} catch (error) {
|
|
5264
5365
|
log("[magic-context] failed to close cached OpenCode read-only DB:", error);
|
|
5265
5366
|
} finally {
|
|
5266
|
-
|
|
5367
|
+
cachedReadOnlyDbs[generation] = null;
|
|
5267
5368
|
}
|
|
5268
5369
|
}
|
|
5269
|
-
function getReadOnlySessionDb() {
|
|
5370
|
+
function getReadOnlySessionDb(generation = "v1") {
|
|
5270
5371
|
if (!harnessOwnsOpenCodeStore()) {
|
|
5271
5372
|
throw new Error("OpenCode session database is not readable from a Pi-compatible process; its history lives in Pi sessions");
|
|
5272
5373
|
}
|
|
5273
|
-
const resolution = resolveOpenCodeDbPath();
|
|
5374
|
+
const resolution = resolveOpenCodeDbPath(generation);
|
|
5274
5375
|
const dbPath = resolution.path;
|
|
5275
5376
|
if (!openCodeDbPathExists(resolution)) {
|
|
5276
5377
|
throw new Error(`OpenCode session database is unavailable at ${dbPath} (source=${resolution.source})`);
|
|
5277
5378
|
}
|
|
5278
|
-
|
|
5279
|
-
|
|
5379
|
+
const cached = cachedReadOnlyDbs[generation];
|
|
5380
|
+
if (cached?.path === dbPath) {
|
|
5381
|
+
return cached.db;
|
|
5280
5382
|
}
|
|
5281
|
-
closeCachedReadOnlyDb();
|
|
5383
|
+
closeCachedReadOnlyDb(generation);
|
|
5282
5384
|
const db = new Database(dbPath, { readonly: true });
|
|
5283
5385
|
try {
|
|
5284
|
-
assertOpenCodeStoreGeneration(db,
|
|
5386
|
+
assertOpenCodeStoreGeneration(db, generation, dbPath);
|
|
5285
5387
|
} catch (error) {
|
|
5286
5388
|
closeQuietly(db);
|
|
5287
5389
|
throw error;
|
|
5288
5390
|
}
|
|
5289
|
-
|
|
5391
|
+
cachedReadOnlyDbs[generation] = { path: dbPath, db };
|
|
5290
5392
|
clearOpenCodeDbReadFailure();
|
|
5291
5393
|
return db;
|
|
5292
5394
|
}
|
|
5293
|
-
function withReadOnlySessionDb(fn) {
|
|
5294
|
-
return fn(getReadOnlySessionDb());
|
|
5395
|
+
function withReadOnlySessionDb(fn, generation = "v1") {
|
|
5396
|
+
return fn(getReadOnlySessionDb(generation));
|
|
5295
5397
|
}
|
|
5296
5398
|
function getRawSessionMessageCountFromDb(db, sessionId) {
|
|
5297
5399
|
const row = db.prepare(`SELECT COUNT(*) as count FROM message WHERE session_id = ?
|
|
@@ -5315,15 +5417,14 @@ function getMessageTimesFromOpenCodeDb(sessionId, messageIds) {
|
|
|
5315
5417
|
if (messageIds.length === 0 || !harnessOwnsOpenCodeStore())
|
|
5316
5418
|
return result;
|
|
5317
5419
|
try {
|
|
5318
|
-
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5324
|
-
}
|
|
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);
|
|
5325
5426
|
}
|
|
5326
|
-
}
|
|
5427
|
+
}
|
|
5327
5428
|
} catch (error) {
|
|
5328
5429
|
logProbeFailureOnce(resolveOpenCodeDbPath(), error);
|
|
5329
5430
|
}
|
|
@@ -6160,34 +6261,170 @@ function applyEditMarkerToInput(input) {
|
|
|
6160
6261
|
}
|
|
6161
6262
|
}
|
|
6162
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
|
+
|
|
6163
6309
|
// ../plugin/src/features/magic-context/tool-definition-tokens.ts
|
|
6164
|
-
var
|
|
6165
|
-
var
|
|
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);
|
|
6166
6315
|
var persistenceDb = null;
|
|
6316
|
+
var windowedSince;
|
|
6317
|
+
var boundAt = 0;
|
|
6167
6318
|
var cachedInsertStmt = null;
|
|
6319
|
+
var cachedDeleteStmt = null;
|
|
6320
|
+
function agentKey(agentName) {
|
|
6321
|
+
return agentName && agentName.length > 0 ? agentName : "default";
|
|
6322
|
+
}
|
|
6168
6323
|
function keyFor(providerID, modelID, agentName) {
|
|
6169
|
-
|
|
6170
|
-
|
|
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 {}
|
|
6171
6371
|
}
|
|
6172
6372
|
function setDatabase(db) {
|
|
6373
|
+
const rebound = persistenceDb !== null && persistenceDb !== db;
|
|
6173
6374
|
persistenceDb = db;
|
|
6375
|
+
boundAt = Date.now();
|
|
6174
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 {}
|
|
6175
6396
|
}
|
|
6176
6397
|
function loadToolDefinitionMeasurements(db) {
|
|
6177
6398
|
let rows = [];
|
|
6178
6399
|
try {
|
|
6179
|
-
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();
|
|
6180
6401
|
} catch {
|
|
6181
6402
|
return;
|
|
6182
6403
|
}
|
|
6404
|
+
const since = readWindowedSince(db);
|
|
6405
|
+
const loadedAt = new Map;
|
|
6183
6406
|
for (const row of rows) {
|
|
6184
|
-
const
|
|
6185
|
-
|
|
6186
|
-
|
|
6187
|
-
|
|
6188
|
-
|
|
6189
|
-
|
|
6190
|
-
|
|
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);
|
|
6191
6428
|
}
|
|
6192
6429
|
}
|
|
6193
6430
|
|
|
@@ -6628,6 +6865,9 @@ function withRawMessageProvider2(sessionId, provider, fn) {
|
|
|
6628
6865
|
function cleanUserText2(text) {
|
|
6629
6866
|
return stripOutsideSteeringWrappers(text, (segment) => removeSystemReminders(segment).replace(OMO_INTERNAL_INITIATOR_MARKER, "")).trim();
|
|
6630
6867
|
}
|
|
6868
|
+
function answerLineParts(rawText) {
|
|
6869
|
+
return askAnswerLines(rawText).map(normalizeText).filter((value) => value.length > 0);
|
|
6870
|
+
}
|
|
6631
6871
|
function hasMeaningfulChunkUserText(parts) {
|
|
6632
6872
|
for (const part of parts) {
|
|
6633
6873
|
if (part === null || typeof part !== "object")
|
|
@@ -6644,7 +6884,7 @@ function hasMeaningfulChunkUserText(parts) {
|
|
|
6644
6884
|
continue;
|
|
6645
6885
|
return true;
|
|
6646
6886
|
}
|
|
6647
|
-
return
|
|
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);
|
|
6648
6888
|
}
|
|
6649
6889
|
function withRawSessionMessageCache2(fn) {
|
|
6650
6890
|
const outerCache = activeRawMessageCache;
|
|
@@ -7250,6 +7490,10 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
|
|
|
7250
7490
|
const meta = { ordinal: msg.ordinal, messageId: msg.id };
|
|
7251
7491
|
if (isHostUnservedRow(msg))
|
|
7252
7492
|
markHostUnservedRow(meta);
|
|
7493
|
+
if (msg.role === "system") {
|
|
7494
|
+
recordFilteredNoise(meta);
|
|
7495
|
+
continue;
|
|
7496
|
+
}
|
|
7253
7497
|
if (msg.role === "user" && !hasMeaningfulChunkUserText(msg.parts)) {
|
|
7254
7498
|
const tcSummaries = msg.parts.flatMap((part) => {
|
|
7255
7499
|
if (expandedResults.has(part))
|
|
@@ -7296,8 +7540,10 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
|
|
|
7296
7540
|
}
|
|
7297
7541
|
const role = compactRole(msg.role);
|
|
7298
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;
|
|
7299
7545
|
const allParts = options.expand === false ? [
|
|
7300
|
-
...
|
|
7546
|
+
...userParts,
|
|
7301
7547
|
...textParts.length === 0 ? extractToolCallSummaries(msg.parts) : []
|
|
7302
7548
|
] : msg.parts.flatMap((part) => {
|
|
7303
7549
|
if (expandedResults.has(part))
|
|
@@ -7306,6 +7552,9 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
|
|
|
7306
7552
|
if (expansion !== null)
|
|
7307
7553
|
return [`TC: ${expansion}`];
|
|
7308
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
|
+
}
|
|
7309
7558
|
return texts.length ? texts : textParts.length === 0 ? extractToolCallSummaries([part]) : [];
|
|
7310
7559
|
});
|
|
7311
7560
|
const compacted = compactTextForSummary(allParts.join(" / "), msg.role);
|
|
@@ -7314,7 +7563,7 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
|
|
|
7314
7563
|
recordFilteredNoise(meta);
|
|
7315
7564
|
continue;
|
|
7316
7565
|
}
|
|
7317
|
-
const msgHasNarrative =
|
|
7566
|
+
const msgHasNarrative = userParts.length > 0;
|
|
7318
7567
|
if (currentBlock && currentBlock.role === role) {
|
|
7319
7568
|
currentBlock.endOrdinal = msg.ordinal;
|
|
7320
7569
|
currentBlock.parts.push(text);
|
|
@@ -7396,4 +7645,4 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
|
|
|
7396
7645
|
toolResultBoundaries
|
|
7397
7646
|
};
|
|
7398
7647
|
}
|
|
7399
|
-
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, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, withReadOnlySessionDb, expandToolPart, retreatPastHostUnservedRows,
|
|
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 };
|