stratagate-dsh 0.3.33 → 0.3.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/CONTRIBUTING.md +88 -88
- package/CONTRIBUTING.zh-CN.md +88 -88
- package/LICENSE +21 -21
- package/README.md +487 -487
- package/README.zh-CN.md +491 -491
- package/benchmarks/locomo-conv26-r8-final.json +146 -146
- package/cordis.patch.yml +16 -16
- package/dist/client.d.ts +1 -1
- package/dist/client.js +4 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.js.map +1 -1
- package/dist/plugin.d.ts +3 -0
- package/dist/plugin.js +992 -157
- package/dist/plugin.js.map +1 -1
- package/dist/repair-profile.js.map +1 -1
- package/docs/ARCHITECTURE.md +282 -282
- package/docs/DSH.md +337 -331
- package/docs/DSH.zh-CN.md +315 -309
- package/docs/EVALUATION.md +189 -189
- package/docs/EXTERNAL_MEMORY_IMPORT.zh-CN.md +99 -99
- package/docs/MEMORY_TOPICS.zh-CN.md +51 -51
- package/docs/benchmarks/issue-128-windows-review.json +104 -104
- package/docs/benchmarks/issue-128-windows.json +103 -103
- package/docs/database-access.md +32 -0
- package/docs/dreaming-agent.md +69 -69
- package/docs/event-extractor.md +48 -48
- package/docs/feedback.md +50 -50
- package/docs/issue-128-lease-cache-review-followup.md +55 -55
- package/docs/issue-128-review-followup.md +58 -58
- package/docs/knowledge-graph.md +38 -38
- package/docs/npm-release.md +53 -53
- package/docs/surface-maintenance.md +36 -36
- package/docs/user-operation-priority.md +39 -39
- package/docs/validation/issue119.md +41 -41
- package/docs/validation/workspace-cleanup-20261020.md +36 -36
- package/package.json +255 -255
- package/packages/core/examples/basic.ts +87 -87
- package/packages/core/src/blocks.ts +442 -442
- package/packages/core/src/weights.ts +21 -21
- package/screenshots.json +8 -8
package/dist/plugin.js
CHANGED
|
@@ -18,6 +18,7 @@ function volatileIfSupported(schema) {
|
|
|
18
18
|
var Config = z.object({
|
|
19
19
|
dailyNotesEnabled: z.boolean().default(true),
|
|
20
20
|
dailyNotesTimeZone: z.string(),
|
|
21
|
+
dailyNotesInjectionDays: z.natural().min(1).max(30).default(3),
|
|
21
22
|
dailyNotesContextMaxChars: z.natural().min(1024).default(12e3),
|
|
22
23
|
database: z.string().required(),
|
|
23
24
|
sessionRoot: z.string(),
|
|
@@ -58,7 +59,9 @@ function resolveConfig(config) {
|
|
|
58
59
|
}
|
|
59
60
|
if (config.dailyNotesTimeZone !== void 0) new Intl.DateTimeFormat("en-US", { timeZone: config.dailyNotesTimeZone }).format();
|
|
60
61
|
if (config.dailyNotesContextMaxChars !== void 0 && (!Number.isSafeInteger(config.dailyNotesContextMaxChars) || config.dailyNotesContextMaxChars < 1024)) throw new TypeError("dailyNotesContextMaxChars must be an integer >= 1024");
|
|
62
|
+
if (config.dailyNotesInjectionDays !== void 0 && (!Number.isSafeInteger(config.dailyNotesInjectionDays) || config.dailyNotesInjectionDays < 1 || config.dailyNotesInjectionDays > 30)) throw new TypeError("dailyNotesInjectionDays must be an integer between 1 and 30");
|
|
61
63
|
return {
|
|
64
|
+
dailyNotesInjectionDays: config.dailyNotesInjectionDays ?? 3,
|
|
62
65
|
dailyNotesEnabled: config.dailyNotesEnabled ?? true,
|
|
63
66
|
...config.dailyNotesTimeZone !== void 0 ? { dailyNotesTimeZone: config.dailyNotesTimeZone } : {},
|
|
64
67
|
dailyNotesContextMaxChars: config.dailyNotesContextMaxChars ?? 12e3,
|
|
@@ -619,9 +622,9 @@ function effectiveGraphNodeView(node, edges, events) {
|
|
|
619
622
|
const historicalNodeEventIds = sources2(node.sourceEventIds, /* @__PURE__ */ new Set(["superseded"]));
|
|
620
623
|
if (currentFacts.length === 0 && historicalFacts.length === 0 && currentEdges.length === 0 && historicalEdges.length === 0 && currentNodeEventIds.length === 0 && historicalNodeEventIds.length === 0) return null;
|
|
621
624
|
const visibleNodeSources = unique([...currentNodeEventIds, ...historicalNodeEventIds]);
|
|
622
|
-
const
|
|
623
|
-
const effectiveNode = { ...node, ...
|
|
624
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
625
|
+
const metadata2 = effectiveMetadata(node, eventById);
|
|
626
|
+
const effectiveNode = { ...node, ...metadata2 };
|
|
627
|
+
if (!Object.prototype.hasOwnProperty.call(metadata2, "tags")) delete effectiveNode.tags;
|
|
625
628
|
return {
|
|
626
629
|
node: { ...effectiveNode, currentState: renderCurrentState(currentFacts), facts: currentFacts, sourceEventIds: visibleNodeSources },
|
|
627
630
|
currentFacts,
|
|
@@ -641,7 +644,7 @@ function boundEffectiveGraphNodeView(view, eventIds, options = {}) {
|
|
|
641
644
|
const historicalFacts = view.historicalFacts.flatMap((record3) => boundRecord(record3) ?? []);
|
|
642
645
|
const currentEdges = view.currentEdges.flatMap((record3) => boundRecord(record3) ?? []);
|
|
643
646
|
const historicalEdges = view.historicalEdges.flatMap((record3) => boundRecord(record3) ?? []);
|
|
644
|
-
const
|
|
647
|
+
const metadata2 = view.node.metadataProvenance ? (() => {
|
|
645
648
|
const name2 = (view.node.metadataProvenance?.name ?? []).filter((id) => eventIds.has(id));
|
|
646
649
|
const aliases = (view.node.metadataProvenance?.aliases ?? []).flatMap((entry) => {
|
|
647
650
|
const sourceEventIds = entry.sourceEventIds.filter((id) => eventIds.has(id));
|
|
@@ -664,8 +667,8 @@ function boundEffectiveGraphNodeView(view, eventIds, options = {}) {
|
|
|
664
667
|
})() : view.node.sourceEventIds.length > 0 && view.node.sourceEventIds.every((id) => eventIds.has(id)) || options.preserveLegacyMetadata ? { name: view.node.name, aliases: [...view.node.aliases], ...view.node.tags ? { tags: [...view.node.tags] } : {} } : { name: "", aliases: [] };
|
|
665
668
|
const currentNodeEventIds = view.currentNodeEventIds.filter((id) => eventIds.has(id));
|
|
666
669
|
const historicalNodeEventIds = view.historicalNodeEventIds.filter((id) => eventIds.has(id));
|
|
667
|
-
const boundedNode = { ...view.node, ...
|
|
668
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
670
|
+
const boundedNode = { ...view.node, ...metadata2 };
|
|
671
|
+
if (!Object.prototype.hasOwnProperty.call(metadata2, "tags")) delete boundedNode.tags;
|
|
669
672
|
return {
|
|
670
673
|
node: { ...boundedNode, currentState: renderCurrentState(currentFacts), facts: currentFacts, sourceEventIds: unique([...currentNodeEventIds, ...historicalNodeEventIds]) },
|
|
671
674
|
currentFacts,
|
|
@@ -701,10 +704,10 @@ function chronology(events, ids2, fallback) {
|
|
|
701
704
|
function entityMatchKind(node, proposal, eventById) {
|
|
702
705
|
if (node.type !== proposal.type) return null;
|
|
703
706
|
const key = (value) => normalizeSearchText(value).replace(/[\s_-]+/g, "");
|
|
704
|
-
const
|
|
707
|
+
const metadata2 = effectiveMetadata(node, eventById);
|
|
705
708
|
const proposalNames = new Set([proposal.name, ...proposal.aliases ?? []].map(key));
|
|
706
|
-
if (proposalNames.has(key(
|
|
707
|
-
if (
|
|
709
|
+
if (proposalNames.has(key(metadata2.name))) return "canonical-name";
|
|
710
|
+
if (metadata2.aliases.some((value) => proposalNames.has(key(value)))) return "alias";
|
|
708
711
|
return null;
|
|
709
712
|
}
|
|
710
713
|
function applyGraphProjection(options) {
|
|
@@ -902,14 +905,14 @@ var EVENT_EXTRACTOR_VERSION = 2;
|
|
|
902
905
|
function normalizeEventMetadata(value) {
|
|
903
906
|
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
904
907
|
const item = value;
|
|
905
|
-
const
|
|
908
|
+
const metadata2 = {};
|
|
906
909
|
if (Array.isArray(item.catalogHints)) {
|
|
907
|
-
|
|
910
|
+
metadata2.catalogHints = [...new Set(item.catalogHints.filter((hint) => typeof hint === "string").map((hint) => hint.trim()).filter((hint) => hint.length > 0 && [...hint].length <= 64 && !/^(other|其他|其它)$/i.test(hint) && !/^(chapter|section|topic|chap|sec)[_::-]/i.test(hint) && !/^[\/\\]|[\/\\].*[\/\\]|[>→]/.test(hint)))].slice(0, 2);
|
|
908
911
|
}
|
|
909
912
|
if (Number.isSafeInteger(item.extractorVersion) && item.extractorVersion > 0) {
|
|
910
|
-
|
|
913
|
+
metadata2.extractorVersion = item.extractorVersion;
|
|
911
914
|
}
|
|
912
|
-
return
|
|
915
|
+
return metadata2;
|
|
913
916
|
}
|
|
914
917
|
function normalizeEventTemporal(value) {
|
|
915
918
|
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
@@ -2131,10 +2134,10 @@ function normalizeSnapshot(value) {
|
|
|
2131
2134
|
}
|
|
2132
2135
|
const sourceBlockMap = new Map(snapshot.blocks.map((block) => [block.id, block]));
|
|
2133
2136
|
for (const event of [...snapshot.events, ...snapshot.agentEvents]) {
|
|
2134
|
-
const
|
|
2137
|
+
const metadata2 = normalizeEventMetadata(event);
|
|
2135
2138
|
delete event.catalogHints;
|
|
2136
2139
|
delete event.extractorVersion;
|
|
2137
|
-
Object.assign(event,
|
|
2140
|
+
Object.assign(event, metadata2);
|
|
2138
2141
|
delete event.narrative;
|
|
2139
2142
|
delete event.confidence;
|
|
2140
2143
|
const temporal = normalizeEventTemporal(event.temporal);
|
|
@@ -2337,6 +2340,99 @@ function elementViewAt(element, at) {
|
|
|
2337
2340
|
|
|
2338
2341
|
// packages/core/src/sqlite.ts
|
|
2339
2342
|
import { DatabaseSync } from "node:sqlite";
|
|
2343
|
+
|
|
2344
|
+
// packages/core/src/storage-write-set.ts
|
|
2345
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2346
|
+
var RECORD_COLLECTIONS = [
|
|
2347
|
+
"blocks",
|
|
2348
|
+
"summaryJobs",
|
|
2349
|
+
"events",
|
|
2350
|
+
"agentEvents",
|
|
2351
|
+
"elements",
|
|
2352
|
+
"extractionJobs",
|
|
2353
|
+
"elementProjectionJobs",
|
|
2354
|
+
"usageReceipts",
|
|
2355
|
+
"ingestionReceipts",
|
|
2356
|
+
"externalMemoryImportJobs",
|
|
2357
|
+
"successfulModelResponses"
|
|
2358
|
+
];
|
|
2359
|
+
var recordId = (record3) => record3.id ?? record3.blockId;
|
|
2360
|
+
var ownedToolRoots = /* @__PURE__ */ new WeakSet();
|
|
2361
|
+
function freezeToolValue(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
2362
|
+
if (!value || typeof value !== "object" || Object.isFrozen(value) || seen.has(value)) return;
|
|
2363
|
+
seen.add(value);
|
|
2364
|
+
if (ArrayBuffer.isView(value)) return;
|
|
2365
|
+
for (const child of Object.values(value)) freezeToolValue(child, seen);
|
|
2366
|
+
Object.freeze(value);
|
|
2367
|
+
}
|
|
2368
|
+
function captureRawMessage(message) {
|
|
2369
|
+
if (message.toolCalls && !ownedToolRoots.has(message.toolCalls)) {
|
|
2370
|
+
message.toolCalls = JSON.parse(JSON.stringify(message.toolCalls));
|
|
2371
|
+
freezeToolValue(message.toolCalls);
|
|
2372
|
+
ownedToolRoots.add(message.toolCalls);
|
|
2373
|
+
}
|
|
2374
|
+
return { ...message };
|
|
2375
|
+
}
|
|
2376
|
+
function captureMutationState(state) {
|
|
2377
|
+
const copy = structuredClone({ ...state, openTail: [], blocks: state.blocks.map(({ l5Raw: _raw, ...block }) => ({ ...block, l5Raw: [] })) });
|
|
2378
|
+
copy.openTail = state.openTail.map(captureRawMessage);
|
|
2379
|
+
copy.blocks.forEach((block, index) => {
|
|
2380
|
+
block.l5Raw = state.blocks[index].l5Raw.map(captureRawMessage);
|
|
2381
|
+
});
|
|
2382
|
+
return copy;
|
|
2383
|
+
}
|
|
2384
|
+
function metadata(record3, collection) {
|
|
2385
|
+
if (collection !== "blocks") return record3;
|
|
2386
|
+
const { l5Raw: _raw, ...block } = record3;
|
|
2387
|
+
return block;
|
|
2388
|
+
}
|
|
2389
|
+
function messageLocations(state) {
|
|
2390
|
+
const rows = /* @__PURE__ */ new Map();
|
|
2391
|
+
const add = (messages, blockId) => {
|
|
2392
|
+
messages.forEach((message, position) => rows.set(message.id, { message, blockId, position }));
|
|
2393
|
+
};
|
|
2394
|
+
add(state.openTail, null);
|
|
2395
|
+
for (const block of state.blocks) add(block.l5Raw, block.id);
|
|
2396
|
+
return rows;
|
|
2397
|
+
}
|
|
2398
|
+
function snapshotWriteSet(before, after, rawDelta) {
|
|
2399
|
+
const changed = /* @__PURE__ */ new Map();
|
|
2400
|
+
const removed = /* @__PURE__ */ new Map();
|
|
2401
|
+
for (const collection of RECORD_COLLECTIONS) {
|
|
2402
|
+
const previous2 = new Map((before[collection] ?? []).map((record3, position) => [recordId(record3), { record: record3, position }]));
|
|
2403
|
+
const ids2 = /* @__PURE__ */ new Set();
|
|
2404
|
+
(after[collection] ?? []).forEach((record3, position) => {
|
|
2405
|
+
const id = recordId(record3);
|
|
2406
|
+
const old = previous2.get(id);
|
|
2407
|
+
if (!old || old.position !== position || !isDeepStrictEqual(metadata(old.record, collection), metadata(record3, collection))) ids2.add(id);
|
|
2408
|
+
previous2.delete(id);
|
|
2409
|
+
});
|
|
2410
|
+
changed.set(collection, ids2);
|
|
2411
|
+
removed.set(collection, new Set(previous2.keys()));
|
|
2412
|
+
}
|
|
2413
|
+
const previous = messageLocations(before);
|
|
2414
|
+
const forced = new Set(rawDelta?.upsert.map((message) => message.id));
|
|
2415
|
+
const messages = [];
|
|
2416
|
+
for (const [id, row] of messageLocations(after)) {
|
|
2417
|
+
const old = previous.get(id);
|
|
2418
|
+
if (!old || forced.has(id) || old.blockId !== row.blockId || old.position !== row.position || old.message.content !== row.message.content || old.message.role !== row.message.role || old.message.createdAt !== row.message.createdAt || old.message.threadId !== row.message.threadId || old.message.toolCalls !== row.message.toolCalls && !isDeepStrictEqual(old.message.toolCalls, row.message.toolCalls)) messages.push(row);
|
|
2419
|
+
previous.delete(id);
|
|
2420
|
+
}
|
|
2421
|
+
return {
|
|
2422
|
+
changed,
|
|
2423
|
+
removed,
|
|
2424
|
+
messages,
|
|
2425
|
+
deletedMessages: /* @__PURE__ */ new Set([...previous.keys(), ...rawDelta?.deleteIds ?? []]),
|
|
2426
|
+
graph: !isDeepStrictEqual([before.graphNodes, before.graphEdges, before.graphProjectionJobs], [after.graphNodes, after.graphEdges, after.graphProjectionJobs]),
|
|
2427
|
+
topics: !isDeepStrictEqual(before.memoryTopicState, after.memoryTopicState)
|
|
2428
|
+
};
|
|
2429
|
+
}
|
|
2430
|
+
function hasRecordChanges(before, after) {
|
|
2431
|
+
const changes = snapshotWriteSet(before, after);
|
|
2432
|
+
return changes.graph || [...changes.changed.values(), ...changes.removed.values()].some((ids2) => ids2.size > 0) || changes.messages.length > 0 || changes.deletedMessages.size > 0;
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
// packages/core/src/sqlite.ts
|
|
2340
2436
|
import { resolve } from "node:path";
|
|
2341
2437
|
|
|
2342
2438
|
// packages/core/src/topics.ts
|
|
@@ -2461,8 +2557,8 @@ function safeTopicDiagnostics(value) {
|
|
|
2461
2557
|
}
|
|
2462
2558
|
var TopicProjectionError = class extends Error {
|
|
2463
2559
|
diagnostics;
|
|
2464
|
-
constructor(category, reason,
|
|
2465
|
-
const diagnostics = safeTopicDiagnostics({ ...
|
|
2560
|
+
constructor(category, reason, metadata2 = {}) {
|
|
2561
|
+
const diagnostics = safeTopicDiagnostics({ ...metadata2, category, reason });
|
|
2466
2562
|
super(`${diagnostics.category}: ${diagnostics.reason}`);
|
|
2467
2563
|
this.name = "TopicProjectionError";
|
|
2468
2564
|
this.diagnostics = diagnostics;
|
|
@@ -2470,14 +2566,14 @@ var TopicProjectionError = class extends Error {
|
|
|
2470
2566
|
};
|
|
2471
2567
|
function validateTopicOverviewText(value, fieldPath, category = "validation-failed") {
|
|
2472
2568
|
const count = typeof value === "string" ? Array.from(value).length : void 0;
|
|
2473
|
-
const
|
|
2569
|
+
const metadata2 = {
|
|
2474
2570
|
fieldPath,
|
|
2475
2571
|
minCharacters: 1,
|
|
2476
2572
|
maxCharacters: TOPIC_OVERVIEW_MAX_CHARACTERS,
|
|
2477
2573
|
...count === void 0 ? {} : { actualCharacters: count }
|
|
2478
2574
|
};
|
|
2479
2575
|
const reason = typeof value !== "string" ? "overview.text must be a string" : value.length === 0 ? "overview.text is empty" : !value.trim() ? "overview.text contains only whitespace" : count > TOPIC_OVERVIEW_MAX_CHARACTERS ? "overview.text exceeds the character limit" : null;
|
|
2480
|
-
if (reason) throw new TopicProjectionError(category, reason,
|
|
2576
|
+
if (reason) throw new TopicProjectionError(category, reason, metadata2);
|
|
2481
2577
|
}
|
|
2482
2578
|
|
|
2483
2579
|
// packages/core/src/topic-overview-repair.ts
|
|
@@ -4223,6 +4319,173 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4223
4319
|
}
|
|
4224
4320
|
}
|
|
4225
4321
|
async load(namespace) {
|
|
4322
|
+
return this.readState(namespace);
|
|
4323
|
+
}
|
|
4324
|
+
readEventModel(namespace) {
|
|
4325
|
+
this.assertOpen();
|
|
4326
|
+
const key = nonEmptyNamespace(namespace);
|
|
4327
|
+
this.database.exec("BEGIN");
|
|
4328
|
+
try {
|
|
4329
|
+
const head = this.database.prepare("SELECT schema_version FROM memory_spaces WHERE namespace = ?").get(key);
|
|
4330
|
+
const state = head?.schema_version === STRATAGATE_STORAGE_SCHEMA_VERSION ? this.readState(key, true) : null;
|
|
4331
|
+
this.database.exec("COMMIT");
|
|
4332
|
+
if (!state) return null;
|
|
4333
|
+
const { schemaVersion: storedSchemaVersion, ...model } = state.snapshot;
|
|
4334
|
+
return { ...model, storedSchemaVersion };
|
|
4335
|
+
} catch (error) {
|
|
4336
|
+
try {
|
|
4337
|
+
this.database.exec("ROLLBACK");
|
|
4338
|
+
} catch {
|
|
4339
|
+
}
|
|
4340
|
+
throw error;
|
|
4341
|
+
}
|
|
4342
|
+
}
|
|
4343
|
+
/** Dashboard read model: keep metadata for navigation and hydrate only the
|
|
4344
|
+
* displayed blocks and the provenance explicitly returned by its audit page.
|
|
4345
|
+
*/
|
|
4346
|
+
readDashboardEntries(options, chooseThread) {
|
|
4347
|
+
this.assertOpen();
|
|
4348
|
+
this.database.exec("BEGIN");
|
|
4349
|
+
try {
|
|
4350
|
+
const entries = [];
|
|
4351
|
+
const namespaces = this.listNamespaces();
|
|
4352
|
+
const selected = namespaces.includes(options.namespace ?? "") ? options.namespace : namespaces[0];
|
|
4353
|
+
for (const namespace of namespaces) {
|
|
4354
|
+
const legacy = this.database.prepare(`SELECT 1 FROM blocks WHERE namespace = ? AND (thread_id IS NULL OR trim(thread_id) = '')
|
|
4355
|
+
UNION ALL SELECT 1 FROM messages m LEFT JOIN blocks b ON b.namespace = m.namespace AND b.id = m.block_id
|
|
4356
|
+
WHERE m.namespace = ? AND (m.thread_id IS NULL OR trim(m.thread_id) = '' OR (m.block_id IS NOT NULL AND m.thread_id IS NOT b.thread_id)) LIMIT 1`).get(namespace, namespace);
|
|
4357
|
+
const head = this.database.prepare("SELECT schema_version FROM memory_spaces WHERE namespace = ?").get(namespace);
|
|
4358
|
+
const projected = !legacy && head.schema_version === STRATAGATE_STORAGE_SCHEMA_VERSION;
|
|
4359
|
+
const state = this.readState(namespace, projected);
|
|
4360
|
+
if (!state) continue;
|
|
4361
|
+
const { schemaVersion: storedSchemaVersion, ...data } = state.snapshot;
|
|
4362
|
+
const model = { ...data, storedSchemaVersion };
|
|
4363
|
+
if (namespace === selected && projected) {
|
|
4364
|
+
const threadId = options.threadId?.trim() || chooseThread(model);
|
|
4365
|
+
const ids2 = threadId ? this.database.prepare(`SELECT id FROM blocks WHERE namespace = ? AND thread_id = ?
|
|
4366
|
+
AND (EXISTS (SELECT 1 FROM messages WHERE namespace = blocks.namespace AND block_id = blocks.id)
|
|
4367
|
+
OR EXISTS (SELECT 1 FROM block_summary_jobs j WHERE j.namespace = blocks.namespace AND j.block_id = blocks.id
|
|
4368
|
+
AND j.status = 'failed' AND (j.attempts >= 3 OR j.next_retry_at IS NULL)))
|
|
4369
|
+
ORDER BY sequence LIMIT ? OFFSET ?`).all(namespace, threadId, options.limit, options.offset ?? 0).map((row) => row.id) : [];
|
|
4370
|
+
this.hydrateReadBlocks(namespace, model, ids2);
|
|
4371
|
+
const receipts = [...model.usageReceipts].reverse().slice(options.offset ?? 0, (options.offset ?? 0) + 100);
|
|
4372
|
+
const elementIds = new Set(receipts.flatMap((receipt) => receipt.elementIds));
|
|
4373
|
+
const eventIds = /* @__PURE__ */ new Set([
|
|
4374
|
+
...receipts.flatMap((receipt) => receipt.eventIds),
|
|
4375
|
+
...model.elements.filter((element) => elementIds.has(element.id)).flatMap((element) => element.sourceEventIds)
|
|
4376
|
+
]);
|
|
4377
|
+
const messageIds = model.events.filter((event) => eventIds.has(event.id)).flatMap((event) => event.sourceMessageIds);
|
|
4378
|
+
this.hydrateReadMessages(namespace, model, messageIds);
|
|
4379
|
+
}
|
|
4380
|
+
entries.push({ namespace, revision: state.revision, snapshot: model });
|
|
4381
|
+
}
|
|
4382
|
+
this.database.exec("COMMIT");
|
|
4383
|
+
return entries;
|
|
4384
|
+
} catch (error) {
|
|
4385
|
+
try {
|
|
4386
|
+
this.database.exec("ROLLBACK");
|
|
4387
|
+
} catch {
|
|
4388
|
+
}
|
|
4389
|
+
throw error;
|
|
4390
|
+
}
|
|
4391
|
+
}
|
|
4392
|
+
hydrateReadMessages(namespace, model, ids2) {
|
|
4393
|
+
if (!ids2.length) return;
|
|
4394
|
+
const rows = this.database.prepare(`SELECT id, thread_id, role, content, created_at, tool_calls_json
|
|
4395
|
+
FROM messages WHERE namespace = ? AND id IN (SELECT value FROM json_each(?))`).all(namespace, JSON.stringify([...new Set(ids2)]));
|
|
4396
|
+
const messages = new Map(rows.map((row) => [row.id, {
|
|
4397
|
+
id: row.id,
|
|
4398
|
+
role: row.role,
|
|
4399
|
+
content: row.content,
|
|
4400
|
+
createdAt: row.created_at,
|
|
4401
|
+
...row.thread_id ? { threadId: row.thread_id } : {},
|
|
4402
|
+
...row.tool_calls_json ? { toolCalls: parseJson(row.tool_calls_json, "messages.tool_calls_json") } : {}
|
|
4403
|
+
}]));
|
|
4404
|
+
model.openTail = model.openTail.map((message) => messages.get(message.id) ?? message);
|
|
4405
|
+
for (const block of model.blocks) block.l5Raw = block.l5Raw.map((message) => messages.get(message.id) ?? message);
|
|
4406
|
+
}
|
|
4407
|
+
hydrateReadBlocks(namespace, model, ids2) {
|
|
4408
|
+
if (!ids2.length) return;
|
|
4409
|
+
const byId = new Map(model.blocks.map((block) => [block.id, block]));
|
|
4410
|
+
for (const row of this.database.prepare("SELECT id, l3_condensed, l4_readable FROM blocks WHERE namespace = ? AND id IN (SELECT value FROM json_each(?))").all(namespace, JSON.stringify(ids2))) {
|
|
4411
|
+
const block = byId.get(row.id);
|
|
4412
|
+
block.l3Condensed = row.l3_condensed;
|
|
4413
|
+
block.l4Readable = row.l4_readable;
|
|
4414
|
+
}
|
|
4415
|
+
this.hydrateReadMessages(namespace, model, ids2.flatMap((id) => byId.get(id)?.l5Raw.map((message) => message.id) ?? []));
|
|
4416
|
+
}
|
|
4417
|
+
/** A single read transaction keeps navigation, page and associations at the same revision.
|
|
4418
|
+
* Legacy mixed-thread records use the existing recovery path, never guessed SQL scopes.
|
|
4419
|
+
*/
|
|
4420
|
+
readBlockPage(namespace, options, chooseThread) {
|
|
4421
|
+
this.assertOpen();
|
|
4422
|
+
const key = nonEmptyNamespace(namespace);
|
|
4423
|
+
if (!Number.isSafeInteger(options.offset) || options.offset < 0 || !Number.isSafeInteger(options.limit) || options.limit < 1 || options.limit > 200) throw new TypeError("Invalid block page");
|
|
4424
|
+
this.database.exec("BEGIN");
|
|
4425
|
+
try {
|
|
4426
|
+
const head = this.database.prepare("SELECT schema_version FROM memory_spaces WHERE namespace = ?").get(key);
|
|
4427
|
+
if (!head || head.schema_version !== STRATAGATE_STORAGE_SCHEMA_VERSION) {
|
|
4428
|
+
this.database.exec("COMMIT");
|
|
4429
|
+
return null;
|
|
4430
|
+
}
|
|
4431
|
+
const legacy = this.database.prepare(`SELECT 1 FROM blocks WHERE namespace = ? AND (thread_id IS NULL OR trim(thread_id) = '')
|
|
4432
|
+
UNION ALL SELECT 1 FROM messages m LEFT JOIN blocks b ON b.namespace = m.namespace AND b.id = m.block_id
|
|
4433
|
+
WHERE m.namespace = ? AND (m.thread_id IS NULL OR trim(m.thread_id) = '' OR (m.block_id IS NOT NULL AND m.thread_id IS NOT b.thread_id)) LIMIT 1`).get(key, key);
|
|
4434
|
+
if (legacy) {
|
|
4435
|
+
this.database.exec("COMMIT");
|
|
4436
|
+
return null;
|
|
4437
|
+
}
|
|
4438
|
+
const loaded = this.readState(key, true);
|
|
4439
|
+
if (!loaded) {
|
|
4440
|
+
this.database.exec("COMMIT");
|
|
4441
|
+
return null;
|
|
4442
|
+
}
|
|
4443
|
+
const { schemaVersion: storedSchemaVersion, ...data } = loaded.snapshot;
|
|
4444
|
+
const model = { ...data, storedSchemaVersion };
|
|
4445
|
+
const threadId = options.threadId?.trim() || chooseThread(model);
|
|
4446
|
+
const blockIds = threadId ? this.database.prepare(`SELECT id FROM blocks WHERE namespace = ? AND thread_id = ?
|
|
4447
|
+
AND (EXISTS (SELECT 1 FROM messages WHERE namespace = blocks.namespace AND block_id = blocks.id)
|
|
4448
|
+
OR EXISTS (SELECT 1 FROM block_summary_jobs j WHERE j.namespace = blocks.namespace AND j.block_id = blocks.id
|
|
4449
|
+
AND j.status = 'failed' AND (j.attempts >= ? OR j.next_retry_at IS NULL)))
|
|
4450
|
+
ORDER BY sequence LIMIT ? OFFSET ?`).all(key, threadId, 3, options.limit, options.offset).map((row) => row.id) : [];
|
|
4451
|
+
const failedSummaries = new Set(model.summaryJobs.filter((job) => job.status === "failed" && (job.attempts >= 3 || job.nextRetryAt === null)).map((job) => job.blockId));
|
|
4452
|
+
const total = threadId ? model.blocks.filter((block) => block.threadId === threadId && (block.l5Raw.length || failedSummaries.has(block.id))).length : 0;
|
|
4453
|
+
const byId = new Map(model.blocks.map((block) => [block.id, block]));
|
|
4454
|
+
for (const row of this.database.prepare(`SELECT * FROM blocks WHERE namespace = ? AND id IN (SELECT value FROM json_each(?))`).all(key, JSON.stringify(blockIds))) {
|
|
4455
|
+
const block = byId.get(row.id);
|
|
4456
|
+
if (row.l0_title) {
|
|
4457
|
+
block.shouldExtract = Boolean(row.should_extract);
|
|
4458
|
+
block.l0Title = row.l0_title;
|
|
4459
|
+
block.l0Tags = parseJson(row.l0_tags_json, "blocks.l0_tags_json");
|
|
4460
|
+
block.l1Summary = row.l1_summary;
|
|
4461
|
+
block.l2Keypoints = parseJson(row.l2_keypoints_json, "blocks.l2_keypoints_json");
|
|
4462
|
+
}
|
|
4463
|
+
block.l3Condensed = row.l3_condensed;
|
|
4464
|
+
block.l4Readable = row.l4_readable;
|
|
4465
|
+
block.l5Raw = [];
|
|
4466
|
+
}
|
|
4467
|
+
for (const row of this.database.prepare(`SELECT id, block_id, thread_id, position, role, content, created_at, tool_calls_json
|
|
4468
|
+
FROM messages WHERE namespace = ? AND block_id IN (SELECT value FROM json_each(?)) ORDER BY block_id, position`).all(key, JSON.stringify(blockIds))) {
|
|
4469
|
+
byId.get(row.block_id).l5Raw.push({
|
|
4470
|
+
id: row.id,
|
|
4471
|
+
role: row.role,
|
|
4472
|
+
content: row.content,
|
|
4473
|
+
createdAt: row.created_at,
|
|
4474
|
+
...row.thread_id ? { threadId: row.thread_id } : {},
|
|
4475
|
+
...row.tool_calls_json ? { toolCalls: parseJson(row.tool_calls_json, "messages.tool_calls_json") } : {}
|
|
4476
|
+
});
|
|
4477
|
+
}
|
|
4478
|
+
this.database.exec("COMMIT");
|
|
4479
|
+
return { model, revision: loaded.revision, threadId, blockIds, total };
|
|
4480
|
+
} catch (error) {
|
|
4481
|
+
try {
|
|
4482
|
+
this.database.exec("ROLLBACK");
|
|
4483
|
+
} catch {
|
|
4484
|
+
}
|
|
4485
|
+
throw error;
|
|
4486
|
+
}
|
|
4487
|
+
}
|
|
4488
|
+
readState(namespace, navigation = false) {
|
|
4226
4489
|
this.assertOpen();
|
|
4227
4490
|
const key = nonEmptyNamespace(namespace);
|
|
4228
4491
|
const hasGraphEnabled = this.hasColumn("memory_spaces", "graph_enabled");
|
|
@@ -4234,7 +4497,16 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4234
4497
|
if (space.schema_version !== STRATAGATE_STORAGE_SCHEMA_VERSION && !(this.readonly && [12, 13].includes(space.schema_version))) {
|
|
4235
4498
|
throw new Error(`Unsupported stored StrataGate schema: ${space.schema_version}`);
|
|
4236
4499
|
}
|
|
4237
|
-
const messageRows = this.database.prepare(`
|
|
4500
|
+
const messageRows = navigation ? this.database.prepare(`
|
|
4501
|
+
WITH first_users AS (
|
|
4502
|
+
SELECT m.id, row_number() OVER (PARTITION BY m.thread_id ORDER BY b.sequence IS NULL, b.sequence, m.position) AS rank
|
|
4503
|
+
FROM messages m LEFT JOIN blocks b ON b.namespace = m.namespace AND b.id = m.block_id
|
|
4504
|
+
WHERE m.namespace = ? AND m.role = 'user' AND length(trim(m.content, char(9,10,11,12,13,32,160,5760,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8232,8233,8239,8287,12288,65279))) > 0
|
|
4505
|
+
) SELECT id, block_id, thread_id, position, role, created_at,
|
|
4506
|
+
CASE WHEN id IN (SELECT id FROM first_users WHERE rank = 1) THEN content
|
|
4507
|
+
WHEN length(trim(content, char(9,10,11,12,13,32,160,5760,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8232,8233,8239,8287,12288,65279))) > 0 THEN '\u2026' ELSE '' END AS content,
|
|
4508
|
+
NULL AS tool_calls_json FROM messages WHERE namespace = ? ORDER BY block_id, position
|
|
4509
|
+
`).all(key, key) : this.database.prepare(`
|
|
4238
4510
|
SELECT id, block_id, thread_id, position, role, content, created_at, tool_calls_json
|
|
4239
4511
|
FROM messages WHERE namespace = ? ORDER BY block_id, position
|
|
4240
4512
|
`).all(key);
|
|
@@ -4256,7 +4528,12 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4256
4528
|
messagesByBlock.set(row.block_id, messages);
|
|
4257
4529
|
}
|
|
4258
4530
|
}
|
|
4259
|
-
const blockRows = this.database.prepare(`
|
|
4531
|
+
const blockRows = this.database.prepare(navigation ? `
|
|
4532
|
+
SELECT id, thread_id, sequence, start_turn, end_turn, created_at, processing_status, should_extract,
|
|
4533
|
+
l0_title, l0_tags_json, l1_summary, l2_keypoints_json,
|
|
4534
|
+
'' AS l3_condensed, '' AS l4_readable, pointer_current_level, surface_current_level,
|
|
4535
|
+
pointer_anchor_level, pointer_anchor_block_position, last_lifted_at, last_lifted_by
|
|
4536
|
+
FROM blocks WHERE namespace = ? ORDER BY sequence` : `
|
|
4260
4537
|
SELECT * FROM blocks WHERE namespace = ? ORDER BY sequence
|
|
4261
4538
|
`).all(key);
|
|
4262
4539
|
const blocks = blockRows.map((row) => ({
|
|
@@ -4497,11 +4774,108 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4497
4774
|
successfulModelResponses,
|
|
4498
4775
|
...topicState ? { memoryTopicState: parseJson(topicState.state_json, "memory_topic_state.state_json") } : {}
|
|
4499
4776
|
};
|
|
4777
|
+
if (navigation) {
|
|
4778
|
+
const normalized = normalizeSnapshot({ ...snapshot, openTail: [], blocks: snapshot.blocks.map((block) => ({ ...block, l5Raw: [] })) });
|
|
4779
|
+
normalized.openTail = snapshot.openTail;
|
|
4780
|
+
normalized.blocks.forEach((block, index) => {
|
|
4781
|
+
block.l5Raw = snapshot.blocks[index].l5Raw;
|
|
4782
|
+
});
|
|
4783
|
+
return { snapshot: normalized, revision: space.revision };
|
|
4784
|
+
}
|
|
4500
4785
|
return { snapshot: cloneSnapshot(normalizeSnapshot(snapshot)), revision: space.revision };
|
|
4501
4786
|
}
|
|
4502
|
-
async save(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch) {
|
|
4787
|
+
async save(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch, before) {
|
|
4788
|
+
return this.saveUnit(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch, before);
|
|
4789
|
+
}
|
|
4790
|
+
async saveChanges(namespace, before, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch) {
|
|
4791
|
+
return this.save(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch, before);
|
|
4792
|
+
}
|
|
4793
|
+
async commitRecords(namespace, expectedRevision, mutation) {
|
|
4794
|
+
this.assertOpen();
|
|
4795
|
+
const key = nonEmptyNamespace(namespace);
|
|
4796
|
+
if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) throw new TypeError("Invalid revision");
|
|
4797
|
+
const result = this.immediateTransaction(() => {
|
|
4798
|
+
const space = this.database.prepare("SELECT revision, current_turn FROM memory_spaces WHERE namespace = ?").get(key);
|
|
4799
|
+
if (!space || space.revision !== expectedRevision) throw new StorageConflictError(key, expectedRevision, space?.revision ?? null);
|
|
4800
|
+
if (mutation.kind === "append") {
|
|
4801
|
+
if (mutation.receipt && this.database.prepare("SELECT 1 FROM ingestion_receipts WHERE namespace = ? AND receipt_id = ?").get(key, mutation.receipt.id)) {
|
|
4802
|
+
return { revision: space.revision, changed: false };
|
|
4803
|
+
}
|
|
4804
|
+
const start = this.database.prepare("SELECT COALESCE(MAX(position), -1) + 1 AS position FROM messages WHERE namespace = ? AND block_id IS NULL").get(key);
|
|
4805
|
+
const insert = this.database.prepare("INSERT INTO messages(namespace, id, block_id, thread_id, position, role, content, created_at, tool_calls_json) VALUES (?, ?, NULL, ?, ?, ?, ?, ?, ?)");
|
|
4806
|
+
mutation.messages.forEach((message, index) => insert.run(
|
|
4807
|
+
key,
|
|
4808
|
+
message.id,
|
|
4809
|
+
message.threadId ?? null,
|
|
4810
|
+
start.position + index,
|
|
4811
|
+
message.role,
|
|
4812
|
+
message.content,
|
|
4813
|
+
message.createdAt,
|
|
4814
|
+
message.toolCalls ? JSON.stringify(message.toolCalls) : null
|
|
4815
|
+
));
|
|
4816
|
+
if (mutation.receipt) this.database.prepare("INSERT INTO ingestion_receipts(namespace, receipt_id, created_at) VALUES (?, ?, ?)").run(key, mutation.receipt.id, mutation.receipt.createdAt);
|
|
4817
|
+
this.syncRawSearchIndex(key, { upsert: mutation.messages, deleteIds: [] });
|
|
4818
|
+
} else if (mutation.kind === "block-pointers") {
|
|
4819
|
+
for (const block of mutation.blocks) {
|
|
4820
|
+
const fields = Object.entries(block).filter(([field]) => field !== "id");
|
|
4821
|
+
const columns = {
|
|
4822
|
+
surfaceCurrentLevel: "surface_current_level",
|
|
4823
|
+
pointerCurrentLevel: "pointer_current_level",
|
|
4824
|
+
pointerAnchorLevel: "pointer_anchor_level",
|
|
4825
|
+
pointerAnchorBlockPosition: "pointer_anchor_block_position",
|
|
4826
|
+
lastLiftedAt: "last_lifted_at",
|
|
4827
|
+
lastLiftedBy: "last_lifted_by"
|
|
4828
|
+
};
|
|
4829
|
+
if (!fields.length) continue;
|
|
4830
|
+
for (const [field, value] of fields) {
|
|
4831
|
+
if (!Object.hasOwn(columns, field)) throw new TypeError("Invalid block pointer field");
|
|
4832
|
+
if (field.endsWith("Level") && (!Number.isInteger(value) || Number(value) < 0 || Number(value) > 5)) throw new TypeError("Invalid block level");
|
|
4833
|
+
if (field === "pointerAnchorBlockPosition" && (!Number.isSafeInteger(value) || Number(value) < 1)) throw new TypeError("Invalid block anchor");
|
|
4834
|
+
if (field === "lastLiftedBy" && value !== null && value !== "user" && value !== "agent") throw new TypeError("Invalid block lift source");
|
|
4835
|
+
}
|
|
4836
|
+
const update = this.database.prepare(`UPDATE blocks SET ${fields.map(([field]) => `${columns[field]} = ?`).join(", ")} WHERE namespace = ? AND id = ? AND processing_status = 'ready'`).run(...fields.map(([, value]) => value), key, block.id);
|
|
4837
|
+
if (Number(update.changes) !== 1) throw new Error(`Block ${block.id} is not ready or missing`);
|
|
4838
|
+
}
|
|
4839
|
+
} else if (mutation.kind === "pin-event") {
|
|
4840
|
+
const table = mutation.agent ? "agent_events" : "events";
|
|
4841
|
+
if (Number(this.database.prepare(`UPDATE ${table} SET pinned = ?, updated_at = ? WHERE namespace = ? AND id = ?`).run(Number(mutation.pinned), mutation.updatedAt, key, mutation.id).changes) !== 1) throw new Error(`Unknown event ${mutation.id}`);
|
|
4842
|
+
} else {
|
|
4843
|
+
if (mutation.receipt) {
|
|
4844
|
+
const existing = this.database.prepare("SELECT event_ids_json, element_ids_json, audit_json FROM usage_receipts WHERE namespace = ? AND receipt_id = ?").get(key, mutation.receipt.id);
|
|
4845
|
+
if (existing) {
|
|
4846
|
+
if (existing.event_ids_json !== JSON.stringify(mutation.receipt.eventIds) || existing.element_ids_json !== JSON.stringify(mutation.receipt.elementIds) || existing.audit_json !== JSON.stringify(mutation.receipt.audit ?? {})) throw new Error(`Usage receipt ${mutation.receipt.id} has different content`);
|
|
4847
|
+
return { revision: space.revision, changed: false };
|
|
4848
|
+
}
|
|
4849
|
+
}
|
|
4850
|
+
for (const [table, ids2] of [["events", mutation.eventIds], ["agent_events", mutation.agentEventIds], ["elements", mutation.elementIds]]) {
|
|
4851
|
+
const update = this.database.prepare(`UPDATE ${table} SET mention_count = mention_count + 1, last_adopted_turn = ?, updated_at = ?
|
|
4852
|
+
WHERE namespace = ? AND id = ?${table === "elements" ? "" : " AND status NOT IN ('forgotten', 'archived')"}`);
|
|
4853
|
+
for (const id of new Set(ids2)) update.run(space.current_turn, mutation.updatedAt, key, id);
|
|
4854
|
+
}
|
|
4855
|
+
if (mutation.receipt) this.database.prepare("INSERT INTO usage_receipts(namespace, receipt_id, event_ids_json, element_ids_json, audit_json, created_at) VALUES (?, ?, ?, ?, ?, ?)").run(key, mutation.receipt.id, JSON.stringify(mutation.receipt.eventIds), JSON.stringify(mutation.receipt.elementIds), JSON.stringify(mutation.receipt.audit ?? {}), mutation.receipt.createdAt);
|
|
4856
|
+
}
|
|
4857
|
+
const revision = expectedRevision + 1;
|
|
4858
|
+
this.database.prepare("UPDATE memory_spaces SET revision = ?, current_turn = ?, updated_at = ? WHERE namespace = ?").run(revision, space.current_turn + Number(mutation.kind === "append"), mutation.updatedAt, key);
|
|
4859
|
+
return { revision, changed: true };
|
|
4860
|
+
});
|
|
4861
|
+
if (result.changed) this.notifyWorkChanged();
|
|
4862
|
+
return result;
|
|
4863
|
+
}
|
|
4864
|
+
notifyWorkChanged() {
|
|
4865
|
+
for (const listener of _SqliteStorage.workListeners.get(this.filename) ?? []) {
|
|
4866
|
+
try {
|
|
4867
|
+
listener();
|
|
4868
|
+
} catch {
|
|
4869
|
+
}
|
|
4870
|
+
}
|
|
4871
|
+
}
|
|
4872
|
+
saveUnit(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch, before) {
|
|
4503
4873
|
this.assertOpen();
|
|
4504
|
-
assertValidSnapshot(
|
|
4874
|
+
assertValidSnapshot(before ? {
|
|
4875
|
+
...snapshot,
|
|
4876
|
+
openTail: [],
|
|
4877
|
+
blocks: snapshot.blocks.map(({ l5Raw: _raw, ...block }) => ({ ...block, l5Raw: [] }))
|
|
4878
|
+
} : snapshot);
|
|
4505
4879
|
if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) {
|
|
4506
4880
|
throw new TypeError("expectedRevision must be a non-negative integer");
|
|
4507
4881
|
}
|
|
@@ -4518,14 +4892,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4518
4892
|
const retained = new Set(JSON.parse(row?.jobs_json ?? "[]").map((job) => job.id));
|
|
4519
4893
|
snapshot.graphProjectionJobs = snapshot.graphProjectionJobs.filter((job) => retained.has(job.id));
|
|
4520
4894
|
}
|
|
4521
|
-
return this.persistSnapshot(key, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards);
|
|
4895
|
+
return this.persistSnapshot(key, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, before);
|
|
4522
4896
|
});
|
|
4523
|
-
|
|
4524
|
-
try {
|
|
4525
|
-
listener();
|
|
4526
|
-
} catch {
|
|
4527
|
-
}
|
|
4528
|
-
}
|
|
4897
|
+
this.notifyWorkChanged();
|
|
4529
4898
|
return revision;
|
|
4530
4899
|
}
|
|
4531
4900
|
async close() {
|
|
@@ -4672,7 +5041,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4672
5041
|
return null;
|
|
4673
5042
|
}
|
|
4674
5043
|
}
|
|
4675
|
-
persistSnapshot(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards) {
|
|
5044
|
+
persistSnapshot(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, before) {
|
|
4676
5045
|
const current = this.database.prepare("SELECT revision FROM memory_spaces WHERE namespace = ?").get(namespace);
|
|
4677
5046
|
const actualRevision = current?.revision ?? null;
|
|
4678
5047
|
if ((actualRevision ?? 0) !== expectedRevision || actualRevision === null && expectedRevision !== 0) {
|
|
@@ -4680,6 +5049,8 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4680
5049
|
}
|
|
4681
5050
|
const nextRevision = expectedRevision + 1;
|
|
4682
5051
|
this.mergeDerivationLeases(namespace, snapshot, expectedRevision, leaseGuards);
|
|
5052
|
+
const changes = before && current ? snapshotWriteSet(before, snapshot, rawMessageIndexDelta) : void 0;
|
|
5053
|
+
const includes = (collection, id) => !changes || changes.changed.get(collection).has(id);
|
|
4683
5054
|
const updatedAt = nowUtc8();
|
|
4684
5055
|
if (current) {
|
|
4685
5056
|
this.database.prepare(`
|
|
@@ -4746,6 +5117,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4746
5117
|
processing_status = excluded.processing_status
|
|
4747
5118
|
`);
|
|
4748
5119
|
for (const block of snapshot.blocks) {
|
|
5120
|
+
if (!includes("blocks", block.id)) continue;
|
|
4749
5121
|
insertBlock.run(
|
|
4750
5122
|
namespace,
|
|
4751
5123
|
block.id,
|
|
@@ -4805,16 +5177,31 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4805
5177
|
);
|
|
4806
5178
|
}
|
|
4807
5179
|
};
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
5180
|
+
if (changes) {
|
|
5181
|
+
for (const { message, blockId, position } of changes.messages) {
|
|
5182
|
+
insertMessage.run(
|
|
5183
|
+
namespace,
|
|
5184
|
+
message.id,
|
|
5185
|
+
blockId,
|
|
5186
|
+
message.threadId ?? null,
|
|
5187
|
+
position,
|
|
5188
|
+
message.role,
|
|
5189
|
+
message.content,
|
|
5190
|
+
message.createdAt,
|
|
5191
|
+
message.toolCalls ? JSON.stringify(message.toolCalls) : null
|
|
5192
|
+
);
|
|
5193
|
+
}
|
|
5194
|
+
} else {
|
|
5195
|
+
insertMessages(snapshot.openTail, null);
|
|
5196
|
+
for (const block of snapshot.blocks) insertMessages(block.l5Raw, block.id);
|
|
5197
|
+
}
|
|
5198
|
+
this.syncRawSearchIndex(namespace, changes ? {
|
|
5199
|
+
upsert: changes.messages.map(({ message }) => message),
|
|
5200
|
+
deleteIds: [...changes.deletedMessages]
|
|
5201
|
+
} : rawMessageIndexDelta ?? {
|
|
4811
5202
|
upsert: snapshot.blocks.flatMap(({ l5Raw }) => l5Raw),
|
|
4812
5203
|
deleteIds: []
|
|
4813
5204
|
});
|
|
4814
|
-
if (rawMessageIndexDelta) {
|
|
4815
|
-
const deleteMessage = this.database.prepare("DELETE FROM messages WHERE namespace = ? AND id = ?");
|
|
4816
|
-
for (const messageId of rawMessageIndexDelta.deleteIds) deleteMessage.run(namespace, messageId);
|
|
4817
|
-
}
|
|
4818
5205
|
const insertEvent = this.database.prepare(`
|
|
4819
5206
|
INSERT INTO events (
|
|
4820
5207
|
namespace, id, position, title, summary, narrative, tags_json, quotes_json, source_block_id,
|
|
@@ -4853,6 +5240,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4853
5240
|
ON CONFLICT (namespace, event_id, message_id) DO UPDATE SET position = excluded.position
|
|
4854
5241
|
`);
|
|
4855
5242
|
for (const [eventPosition, event] of snapshot.events.entries()) {
|
|
5243
|
+
if (!includes("events", event.id)) continue;
|
|
4856
5244
|
insertEvent.run(
|
|
4857
5245
|
namespace,
|
|
4858
5246
|
event.id,
|
|
@@ -4884,8 +5272,12 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4884
5272
|
insertEventSource.run(namespace, event.id, messageId, position);
|
|
4885
5273
|
}
|
|
4886
5274
|
}
|
|
4887
|
-
|
|
4888
|
-
|
|
5275
|
+
if (!changes) {
|
|
5276
|
+
this.database.prepare("DELETE FROM agent_event_sources WHERE namespace = ?").run(namespace);
|
|
5277
|
+
this.database.prepare("DELETE FROM agent_events WHERE namespace = ?").run(namespace);
|
|
5278
|
+
} else for (const id of /* @__PURE__ */ new Set([...changes.changed.get("agentEvents"), ...changes.removed.get("agentEvents")])) {
|
|
5279
|
+
this.database.prepare("DELETE FROM agent_events WHERE namespace = ? AND id = ?").run(namespace, id);
|
|
5280
|
+
}
|
|
4889
5281
|
const insertAgentEvent = this.database.prepare(`
|
|
4890
5282
|
INSERT INTO agent_events (
|
|
4891
5283
|
namespace, id, position, title, summary, narrative, tags_json, quotes_json, source_block_id,
|
|
@@ -4898,6 +5290,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4898
5290
|
INSERT INTO agent_event_sources (namespace, event_id, message_id, position) VALUES (?, ?, ?, ?)
|
|
4899
5291
|
`);
|
|
4900
5292
|
for (const [eventPosition, event] of snapshot.agentEvents.entries()) {
|
|
5293
|
+
if (!includes("agentEvents", event.id)) continue;
|
|
4901
5294
|
insertAgentEvent.run(
|
|
4902
5295
|
namespace,
|
|
4903
5296
|
event.id,
|
|
@@ -4928,23 +5321,37 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4928
5321
|
insertAgentEventSource.run(namespace, event.id, messageId, position);
|
|
4929
5322
|
}
|
|
4930
5323
|
}
|
|
4931
|
-
|
|
5324
|
+
const changedEvent = (id) => includes("events", id) || includes("agentEvents", id);
|
|
5325
|
+
const clearEventAuxiliary = (table) => {
|
|
5326
|
+
if (!changes) this.database.prepare(`DELETE FROM ${table} WHERE namespace = ?`).run(namespace);
|
|
5327
|
+
else for (const id of /* @__PURE__ */ new Set([
|
|
5328
|
+
...changes.changed.get("events"),
|
|
5329
|
+
...changes.changed.get("agentEvents"),
|
|
5330
|
+
...changes.removed.get("events"),
|
|
5331
|
+
...changes.removed.get("agentEvents")
|
|
5332
|
+
])) {
|
|
5333
|
+
this.database.prepare(`DELETE FROM ${table} WHERE namespace = ? AND event_id = ?`).run(namespace, id);
|
|
5334
|
+
}
|
|
5335
|
+
};
|
|
5336
|
+
clearEventAuxiliary("event_merge_sources");
|
|
4932
5337
|
const insertMergedSources = this.database.prepare("INSERT INTO event_merge_sources (namespace, event_id, source_ids_json) VALUES (?, ?, ?)");
|
|
4933
5338
|
for (const event of [...snapshot.events, ...snapshot.agentEvents]) {
|
|
5339
|
+
if (!changedEvent(event.id)) continue;
|
|
4934
5340
|
if (event.mergedFromEventIds?.length) insertMergedSources.run(namespace, event.id, JSON.stringify(event.mergedFromEventIds));
|
|
4935
5341
|
}
|
|
4936
|
-
|
|
5342
|
+
clearEventAuxiliary("event_metadata");
|
|
4937
5343
|
const insertEventMetadata = this.database.prepare(`
|
|
4938
5344
|
INSERT INTO event_metadata (namespace, event_id, catalog_hints_json, extractor_version) VALUES (?, ?, ?, ?)
|
|
4939
5345
|
`);
|
|
4940
5346
|
for (const event of [...snapshot.events, ...snapshot.agentEvents]) {
|
|
4941
|
-
|
|
4942
|
-
|
|
5347
|
+
if (!changedEvent(event.id)) continue;
|
|
5348
|
+
const metadata2 = normalizeEventMetadata(event);
|
|
5349
|
+
if (metadata2.catalogHints === void 0 && metadata2.extractorVersion === void 0) continue;
|
|
4943
5350
|
insertEventMetadata.run(
|
|
4944
5351
|
namespace,
|
|
4945
5352
|
event.id,
|
|
4946
|
-
|
|
4947
|
-
|
|
5353
|
+
metadata2.catalogHints === void 0 ? null : JSON.stringify(metadata2.catalogHints),
|
|
5354
|
+
metadata2.extractorVersion ?? null
|
|
4948
5355
|
);
|
|
4949
5356
|
}
|
|
4950
5357
|
const insertElement = this.database.prepare(`
|
|
@@ -4993,6 +5400,11 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4993
5400
|
ON CONFLICT (namespace, fact_id, event_id) DO UPDATE SET position = excluded.position
|
|
4994
5401
|
`);
|
|
4995
5402
|
for (const [elementPosition, element] of snapshot.elements.entries()) {
|
|
5403
|
+
if (!includes("elements", element.id)) continue;
|
|
5404
|
+
if (changes || rawMessageIndexDelta?.deleteIds.length) {
|
|
5405
|
+
this.database.prepare("DELETE FROM element_sources WHERE namespace = ? AND element_id = ?").run(namespace, element.id);
|
|
5406
|
+
this.database.prepare("DELETE FROM element_facts WHERE namespace = ? AND element_id = ?").run(namespace, element.id);
|
|
5407
|
+
}
|
|
4996
5408
|
insertElement.run(
|
|
4997
5409
|
namespace,
|
|
4998
5410
|
element.id,
|
|
@@ -5047,6 +5459,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5047
5459
|
lease_until = excluded.lease_until
|
|
5048
5460
|
`);
|
|
5049
5461
|
for (const job of snapshot.extractionJobs) {
|
|
5462
|
+
if (!includes("extractionJobs", job.blockId)) continue;
|
|
5050
5463
|
insertJob.run(namespace, job.blockId, job.status, job.attempts, job.lastError, job.nextRetryAt, job.updatedAt, job.leaseUntil ?? null);
|
|
5051
5464
|
}
|
|
5052
5465
|
const insertSummaryJob = this.database.prepare(`
|
|
@@ -5062,6 +5475,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5062
5475
|
lease_until = excluded.lease_until
|
|
5063
5476
|
`);
|
|
5064
5477
|
for (const job of snapshot.summaryJobs) {
|
|
5478
|
+
if (!includes("summaryJobs", job.blockId)) continue;
|
|
5065
5479
|
insertSummaryJob.run(namespace, job.blockId, job.status, job.attempts, job.lastError, job.nextRetryAt, job.updatedAt, job.leaseUntil ?? null);
|
|
5066
5480
|
}
|
|
5067
5481
|
const insertElementProjectionJob = this.database.prepare(`
|
|
@@ -5079,6 +5493,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5079
5493
|
updated_at = excluded.updated_at
|
|
5080
5494
|
`);
|
|
5081
5495
|
for (const job of snapshot.elementProjectionJobs) {
|
|
5496
|
+
if (!includes("elementProjectionJobs", job.id)) continue;
|
|
5082
5497
|
insertElementProjectionJob.run(
|
|
5083
5498
|
namespace,
|
|
5084
5499
|
job.id,
|
|
@@ -5092,7 +5507,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5092
5507
|
job.updatedAt
|
|
5093
5508
|
);
|
|
5094
5509
|
}
|
|
5095
|
-
this.database.prepare(`
|
|
5510
|
+
if (!changes || changes.graph) this.database.prepare(`
|
|
5096
5511
|
INSERT INTO graph_state (namespace, nodes_json, edges_json, jobs_json, updated_at)
|
|
5097
5512
|
VALUES (?, ?, ?, ?, ?)
|
|
5098
5513
|
ON CONFLICT (namespace) DO UPDATE SET
|
|
@@ -5107,7 +5522,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5107
5522
|
JSON.stringify(snapshot.graphProjectionJobs),
|
|
5108
5523
|
updatedAt
|
|
5109
5524
|
);
|
|
5110
|
-
this.database.prepare(`
|
|
5525
|
+
if (!changes || changes.topics) this.database.prepare(`
|
|
5111
5526
|
INSERT INTO memory_topic_state (namespace, state_json, updated_at) VALUES (?, ?, ?)
|
|
5112
5527
|
ON CONFLICT (namespace) DO UPDATE SET state_json = excluded.state_json, updated_at = excluded.updated_at
|
|
5113
5528
|
`).run(namespace, JSON.stringify(snapshot.memoryTopicState ?? { topics: [], jobs: [], projectedVersions: {} }), updatedAt);
|
|
@@ -5117,6 +5532,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5117
5532
|
ON CONFLICT (namespace, receipt_id) DO NOTHING
|
|
5118
5533
|
`);
|
|
5119
5534
|
for (const receipt of snapshot.usageReceipts) {
|
|
5535
|
+
if (!includes("usageReceipts", receipt.id)) continue;
|
|
5120
5536
|
insertReceipt.run(
|
|
5121
5537
|
namespace,
|
|
5122
5538
|
receipt.id,
|
|
@@ -5132,27 +5548,61 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5132
5548
|
ON CONFLICT (namespace, receipt_id) DO NOTHING
|
|
5133
5549
|
`);
|
|
5134
5550
|
for (const receipt of snapshot.ingestionReceipts) {
|
|
5551
|
+
if (!includes("ingestionReceipts", receipt.id)) continue;
|
|
5135
5552
|
insertIngestionReceipt.run(namespace, receipt.id, receipt.createdAt);
|
|
5136
5553
|
}
|
|
5137
|
-
this.database.prepare("DELETE FROM external_memory_import_jobs WHERE namespace = ?").run(namespace);
|
|
5554
|
+
if (!changes) this.database.prepare("DELETE FROM external_memory_import_jobs WHERE namespace = ?").run(namespace);
|
|
5555
|
+
else for (const id of /* @__PURE__ */ new Set([...changes.changed.get("externalMemoryImportJobs"), ...changes.removed.get("externalMemoryImportJobs")])) {
|
|
5556
|
+
this.database.prepare("DELETE FROM external_memory_import_jobs WHERE namespace = ? AND id = ?").run(namespace, id);
|
|
5557
|
+
}
|
|
5138
5558
|
const insertExternalMemoryImportJob = this.database.prepare(`
|
|
5139
5559
|
INSERT INTO external_memory_import_jobs (namespace, id, payload_json, created_at, updated_at)
|
|
5140
5560
|
VALUES (?, ?, ?, ?, ?)
|
|
5141
5561
|
`);
|
|
5142
5562
|
for (const job of snapshot.externalMemoryImportJobs) {
|
|
5563
|
+
if (!includes("externalMemoryImportJobs", job.id)) continue;
|
|
5143
5564
|
insertExternalMemoryImportJob.run(namespace, job.id, JSON.stringify(job), job.createdAt, job.updatedAt);
|
|
5144
5565
|
}
|
|
5145
|
-
this.database.prepare("DELETE FROM model_response_history WHERE namespace = ?").run(namespace);
|
|
5566
|
+
if (!changes) this.database.prepare("DELETE FROM model_response_history WHERE namespace = ?").run(namespace);
|
|
5567
|
+
else for (const id of /* @__PURE__ */ new Set([...changes.changed.get("successfulModelResponses"), ...changes.removed.get("successfulModelResponses")])) {
|
|
5568
|
+
this.database.prepare("DELETE FROM model_response_history WHERE namespace = ? AND id = ?").run(namespace, id);
|
|
5569
|
+
}
|
|
5146
5570
|
const insertSuccessfulModelResponse = this.database.prepare(`
|
|
5147
5571
|
INSERT INTO model_response_history (namespace, id, kind, response, created_at)
|
|
5148
5572
|
VALUES (?, ?, ?, ?, ?)
|
|
5149
5573
|
`);
|
|
5150
5574
|
for (const response of snapshot.successfulModelResponses ?? []) {
|
|
5575
|
+
if (!includes("successfulModelResponses", response.id)) continue;
|
|
5151
5576
|
insertSuccessfulModelResponse.run(namespace, response.id, response.kind, response.response, response.createdAt);
|
|
5152
5577
|
}
|
|
5153
5578
|
if (!current && this.rawSearchFtsAvailable) this.database.prepare(
|
|
5154
5579
|
"INSERT INTO raw_message_fts_state (namespace, backfill_complete) VALUES (?, 1) ON CONFLICT (namespace) DO NOTHING"
|
|
5155
5580
|
).run(namespace);
|
|
5581
|
+
if (changes || rawMessageIndexDelta?.deleteIds.length) {
|
|
5582
|
+
const tables = [
|
|
5583
|
+
["elements", "elements", "id"],
|
|
5584
|
+
["elementProjectionJobs", "element_projection_jobs", "id"],
|
|
5585
|
+
["summaryJobs", "block_summary_jobs", "block_id"],
|
|
5586
|
+
["extractionJobs", "extraction_jobs", "block_id"],
|
|
5587
|
+
["events", "events", "id"],
|
|
5588
|
+
["usageReceipts", "usage_receipts", "receipt_id"],
|
|
5589
|
+
["ingestionReceipts", "ingestion_receipts", "receipt_id"],
|
|
5590
|
+
["blocks", "blocks", "id"]
|
|
5591
|
+
];
|
|
5592
|
+
for (const [collection, table, column] of tables) {
|
|
5593
|
+
if (changes) for (const id of changes.removed.get(collection)) {
|
|
5594
|
+
this.database.prepare(`DELETE FROM ${table} WHERE namespace = ? AND ${column} = ?`).run(namespace, id);
|
|
5595
|
+
}
|
|
5596
|
+
else {
|
|
5597
|
+
const ids2 = snapshot[collection].map((record3) => "blockId" in record3 ? record3.blockId : record3.id);
|
|
5598
|
+
this.database.prepare(`DELETE FROM ${table} WHERE namespace = ? AND ${column} NOT IN (SELECT value FROM json_each(?))`).run(namespace, JSON.stringify(ids2));
|
|
5599
|
+
}
|
|
5600
|
+
}
|
|
5601
|
+
}
|
|
5602
|
+
if (changes || rawMessageIndexDelta) {
|
|
5603
|
+
const deleteMessage = this.database.prepare("DELETE FROM messages WHERE namespace = ? AND id = ?");
|
|
5604
|
+
for (const messageId of changes?.deletedMessages ?? rawMessageIndexDelta.deleteIds) deleteMessage.run(namespace, messageId);
|
|
5605
|
+
}
|
|
5156
5606
|
const tails = /* @__PURE__ */ new Map();
|
|
5157
5607
|
for (const message of snapshot.openTail) if (message.role === "user") tails.set(message.threadId, (tails.get(message.threadId) ?? 0) + 1);
|
|
5158
5608
|
this.writeBackgroundWorkTimes(namespace, backgroundWorkTimes({
|
|
@@ -5880,6 +6330,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5880
6330
|
};
|
|
5881
6331
|
|
|
5882
6332
|
// packages/core/src/store.ts
|
|
6333
|
+
import { isDeepStrictEqual as isDeepStrictEqual2 } from "node:util";
|
|
5883
6334
|
function defaultIdFactory(prefix) {
|
|
5884
6335
|
return `${prefix}_${crypto.randomUUID()}`;
|
|
5885
6336
|
}
|
|
@@ -6012,6 +6463,7 @@ var StrataGate = class _StrataGate {
|
|
|
6012
6463
|
storage;
|
|
6013
6464
|
namespace;
|
|
6014
6465
|
revision = 0;
|
|
6466
|
+
committedMetadata;
|
|
6015
6467
|
mutationQueue = Promise.resolve();
|
|
6016
6468
|
constructor(options, token) {
|
|
6017
6469
|
if (token !== STRATAGATE_CONSTRUCTOR_TOKEN) {
|
|
@@ -6122,7 +6574,7 @@ var StrataGate = class _StrataGate {
|
|
|
6122
6574
|
memory.storage = options.storage;
|
|
6123
6575
|
memory.namespace = namespace;
|
|
6124
6576
|
if (loaded && loadedSnapshot) {
|
|
6125
|
-
memory.restoreSnapshot(loadedSnapshot);
|
|
6577
|
+
memory.restoreSnapshot(loadedSnapshot, true);
|
|
6126
6578
|
memory.graphEnabledValue = loadedSnapshot.graphEnabled === true;
|
|
6127
6579
|
memory.revision = loadedRevision;
|
|
6128
6580
|
}
|
|
@@ -6269,7 +6721,7 @@ var StrataGate = class _StrataGate {
|
|
|
6269
6721
|
}
|
|
6270
6722
|
const loaded = await this.storage.load(this.namespace);
|
|
6271
6723
|
if (!loaded || loaded.revision === this.revision && !forceSameRevision) return false;
|
|
6272
|
-
this.restoreSnapshot(loaded.snapshot);
|
|
6724
|
+
this.restoreSnapshot(loaded.snapshot, true);
|
|
6273
6725
|
this.revision = loaded.revision;
|
|
6274
6726
|
return true;
|
|
6275
6727
|
} finally {
|
|
@@ -6527,7 +6979,11 @@ var StrataGate = class _StrataGate {
|
|
|
6527
6979
|
});
|
|
6528
6980
|
}
|
|
6529
6981
|
exportSnapshot() {
|
|
6530
|
-
return cloneSnapshot({
|
|
6982
|
+
return cloneSnapshot({ ...this.snapshotView(), memoryTopicState: this.topicDirectory.snapshot(this.listAllEvents()) });
|
|
6983
|
+
}
|
|
6984
|
+
/** Internal view for the serialized mutation queue; never returned to integrations. */
|
|
6985
|
+
snapshotView(includeTopics = true) {
|
|
6986
|
+
return {
|
|
6531
6987
|
schemaVersion: STRATAGATE_STORAGE_SCHEMA_VERSION,
|
|
6532
6988
|
currentTurn: this.currentTurn,
|
|
6533
6989
|
blockTurnSize: this.blockTurnSize,
|
|
@@ -6550,8 +7006,8 @@ var StrataGate = class _StrataGate {
|
|
|
6550
7006
|
ingestionReceipts: [...this.ingestionReceipts.values()],
|
|
6551
7007
|
externalMemoryImportJobs: [...this.externalMemoryImportJobs.values()],
|
|
6552
7008
|
successfulModelResponses: this.successfulModelResponses,
|
|
6553
|
-
memoryTopicState: this.topicDirectory.snapshot(this.
|
|
6554
|
-
}
|
|
7009
|
+
memoryTopicState: includeTopics ? this.topicDirectory.snapshot() : this.committedMetadata?.memoryTopicState ?? { topics: [], jobs: [], projectedVersions: {} }
|
|
7010
|
+
};
|
|
6555
7011
|
}
|
|
6556
7012
|
hasIngestionReceipt(receiptId) {
|
|
6557
7013
|
return this.ingestionReceipts.has(receiptId.trim());
|
|
@@ -6572,7 +7028,7 @@ var StrataGate = class _StrataGate {
|
|
|
6572
7028
|
content: input.user,
|
|
6573
7029
|
createdAt,
|
|
6574
7030
|
...threadId ? { threadId } : {},
|
|
6575
|
-
...input.userToolCalls ? { toolCalls: input.userToolCalls } : {}
|
|
7031
|
+
...input.userToolCalls ? { toolCalls: structuredClone(input.userToolCalls) } : {}
|
|
6576
7032
|
};
|
|
6577
7033
|
const assistantMessage = {
|
|
6578
7034
|
id: this.idFactory("msg"),
|
|
@@ -6580,15 +7036,37 @@ var StrataGate = class _StrataGate {
|
|
|
6580
7036
|
content: input.assistant,
|
|
6581
7037
|
createdAt,
|
|
6582
7038
|
...threadId ? { threadId } : {},
|
|
6583
|
-
...input.assistantToolCalls ? { toolCalls: input.assistantToolCalls } : {}
|
|
7039
|
+
...input.assistantToolCalls ? { toolCalls: structuredClone(input.assistantToolCalls) } : {}
|
|
6584
7040
|
};
|
|
6585
|
-
const
|
|
7041
|
+
const appendInMemory = () => {
|
|
6586
7042
|
if (receiptId && this.ingestionReceipts.has(receiptId)) return false;
|
|
6587
7043
|
this.currentTurn += 1;
|
|
6588
7044
|
this.openTail.push(userMessage, assistantMessage);
|
|
6589
7045
|
if (receiptId) this.ingestionReceipts.set(receiptId, { id: receiptId, createdAt });
|
|
6590
7046
|
return true;
|
|
6591
|
-
}
|
|
7047
|
+
};
|
|
7048
|
+
const appended = this.storage?.commitRecords ? await this.withMutationQueue(async () => {
|
|
7049
|
+
if (this.hasPendingRecordChanges()) return this.applyMutation(appendInMemory);
|
|
7050
|
+
if (receiptId && this.ingestionReceipts.has(receiptId)) return false;
|
|
7051
|
+
const receipt = receiptId ? { id: receiptId, createdAt } : void 0;
|
|
7052
|
+
const committed = await this.storage.commitRecords(this.namespace, this.revision, {
|
|
7053
|
+
kind: "append",
|
|
7054
|
+
messages: [userMessage, assistantMessage],
|
|
7055
|
+
updatedAt: createdAt,
|
|
7056
|
+
...receipt ? { receipt } : {}
|
|
7057
|
+
});
|
|
7058
|
+
if (committed.changed) {
|
|
7059
|
+
this.currentTurn += 1;
|
|
7060
|
+
this.openTail.push(userMessage, assistantMessage);
|
|
7061
|
+
if (receipt) this.ingestionReceipts.set(receipt.id, receipt);
|
|
7062
|
+
const baseline = this.committedMetadata;
|
|
7063
|
+
baseline.currentTurn = this.currentTurn;
|
|
7064
|
+
baseline.openTail.push(captureRawMessage(userMessage), captureRawMessage(assistantMessage));
|
|
7065
|
+
if (receipt) baseline.ingestionReceipts.push(structuredClone(receipt));
|
|
7066
|
+
}
|
|
7067
|
+
this.revision = committed.revision;
|
|
7068
|
+
return committed.changed;
|
|
7069
|
+
}) : await this.commitMutation(appendInMemory);
|
|
6592
7070
|
if (!appended) return { sealedBlock: null, readyBlocks: [], extractedEvents: [], projectedElements: [] };
|
|
6593
7071
|
if (options.deferProcessing === true) {
|
|
6594
7072
|
return { sealedBlock: null, readyBlocks: [], extractedEvents: [], projectedElements: [] };
|
|
@@ -7471,6 +7949,20 @@ var StrataGate = class _StrataGate {
|
|
|
7471
7949
|
if (block.surfaceCurrentLevel !== level) changed.push({ block, level });
|
|
7472
7950
|
}
|
|
7473
7951
|
if (changed.length === 0) return 0;
|
|
7952
|
+
const pendingPointers = this.storage?.commitRecords ? this.pendingPointerUpdates() : null;
|
|
7953
|
+
if (this.storage?.commitRecords && pendingPointers !== null) {
|
|
7954
|
+
const updates2 = new Map(pendingPointers.map((block) => [block.id, { ...block }]));
|
|
7955
|
+
for (const { block, level } of changed) updates2.set(block.id, { ...updates2.get(block.id), id: block.id, surfaceCurrentLevel: level });
|
|
7956
|
+
const committed = await this.storage.commitRecords(this.namespace, this.revision, {
|
|
7957
|
+
kind: "block-pointers",
|
|
7958
|
+
blocks: [...updates2.values()],
|
|
7959
|
+
updatedAt: toUtc8Iso(this.now())
|
|
7960
|
+
});
|
|
7961
|
+
for (const { block, level } of changed) block.surfaceCurrentLevel = level;
|
|
7962
|
+
for (const update of updates2.values()) Object.assign(this.committedMetadata.blocks.find((item) => item.id === update.id), update);
|
|
7963
|
+
this.revision = committed.revision;
|
|
7964
|
+
return changed.length;
|
|
7965
|
+
}
|
|
7474
7966
|
return this.applyMutation(() => {
|
|
7475
7967
|
for (const { block, level } of changed) block.surfaceCurrentLevel = level;
|
|
7476
7968
|
return changed.length;
|
|
@@ -7496,6 +7988,25 @@ var StrataGate = class _StrataGate {
|
|
|
7496
7988
|
content: renderBlock(block, level)
|
|
7497
7989
|
};
|
|
7498
7990
|
if (source === "agent" && block.lastLiftedBy === "user") return result;
|
|
7991
|
+
if (this.storage?.commitRecords && !this.hasPendingRecordChanges()) {
|
|
7992
|
+
const update = {
|
|
7993
|
+
id,
|
|
7994
|
+
pointerCurrentLevel: level,
|
|
7995
|
+
pointerAnchorLevel: level,
|
|
7996
|
+
pointerAnchorBlockPosition: position.latestBlockPosition,
|
|
7997
|
+
lastLiftedAt: toUtc8Iso(this.now()),
|
|
7998
|
+
lastLiftedBy: source
|
|
7999
|
+
};
|
|
8000
|
+
const committed = await this.storage.commitRecords(this.namespace, this.revision, {
|
|
8001
|
+
kind: "block-pointers",
|
|
8002
|
+
blocks: [update],
|
|
8003
|
+
updatedAt: update.lastLiftedAt
|
|
8004
|
+
});
|
|
8005
|
+
Object.assign(block, update);
|
|
8006
|
+
Object.assign(this.committedMetadata.blocks.find((item) => item.id === id), update);
|
|
8007
|
+
this.revision = committed.revision;
|
|
8008
|
+
return result;
|
|
8009
|
+
}
|
|
7499
8010
|
return this.applyMutation(() => {
|
|
7500
8011
|
block.pointerCurrentLevel = level;
|
|
7501
8012
|
block.pointerAnchorLevel = level;
|
|
@@ -7516,16 +8027,7 @@ var StrataGate = class _StrataGate {
|
|
|
7516
8027
|
const requestedEventIds = [...new Set(normalizedRefs.eventIds ?? [])];
|
|
7517
8028
|
const requestedElementIds = [...new Set(normalizedRefs.elementIds ?? [])];
|
|
7518
8029
|
const audit2 = options.audit === void 0 ? void 0 : structuredClone(options.audit);
|
|
7519
|
-
|
|
7520
|
-
const existing = this.usageReceipts.get(receiptId);
|
|
7521
|
-
if (existing) {
|
|
7522
|
-
if (!sameIds(existing.eventIds, requestedEventIds) || !sameIds(existing.elementIds, requestedElementIds) || JSON.stringify(existing.audit ?? null) !== JSON.stringify(audit2 ?? null)) {
|
|
7523
|
-
throw new Error(`Usage receipt ${receiptId} was already recorded with different memory IDs or audit metadata`);
|
|
7524
|
-
}
|
|
7525
|
-
return;
|
|
7526
|
-
}
|
|
7527
|
-
}
|
|
7528
|
-
await this.commitMutation(() => {
|
|
8030
|
+
const recordUseInMemory = () => {
|
|
7529
8031
|
const now = toUtc8Iso(this.now());
|
|
7530
8032
|
for (const id of requestedEventIds) {
|
|
7531
8033
|
const event = this.findEvent(id);
|
|
@@ -7548,9 +8050,89 @@ var StrataGate = class _StrataGate {
|
|
|
7548
8050
|
...audit2 === void 0 ? {} : { audit: audit2 },
|
|
7549
8051
|
createdAt: now
|
|
7550
8052
|
});
|
|
8053
|
+
};
|
|
8054
|
+
if (receiptId) {
|
|
8055
|
+
const existing = this.usageReceipts.get(receiptId);
|
|
8056
|
+
if (existing) {
|
|
8057
|
+
if (!sameIds(existing.eventIds, requestedEventIds) || !sameIds(existing.elementIds, requestedElementIds) || JSON.stringify(existing.audit ?? null) !== JSON.stringify(audit2 ?? null)) {
|
|
8058
|
+
throw new Error(`Usage receipt ${receiptId} was already recorded with different memory IDs or audit metadata`);
|
|
8059
|
+
}
|
|
8060
|
+
return;
|
|
8061
|
+
}
|
|
8062
|
+
}
|
|
8063
|
+
if (this.storage?.commitRecords) return this.withMutationQueue(async () => {
|
|
8064
|
+
const existing = receiptId ? this.usageReceipts.get(receiptId) : void 0;
|
|
8065
|
+
if (existing) {
|
|
8066
|
+
if (!sameIds(existing.eventIds, requestedEventIds) || !sameIds(existing.elementIds, requestedElementIds) || JSON.stringify(existing.audit ?? null) !== JSON.stringify(audit2 ?? null)) throw new Error(`Usage receipt ${receiptId} was already recorded with different memory IDs or audit metadata`);
|
|
8067
|
+
return;
|
|
8068
|
+
}
|
|
8069
|
+
if (this.hasPendingRecordChanges()) return this.applyMutation(recordUseInMemory);
|
|
8070
|
+
const now = toUtc8Iso(this.now());
|
|
8071
|
+
const events = requestedEventIds.flatMap((id) => {
|
|
8072
|
+
const event = this.findEvent(id);
|
|
8073
|
+
return event && event.status !== "forgotten" && event.status !== "archived" ? [event] : [];
|
|
8074
|
+
});
|
|
8075
|
+
const elements = requestedElementIds.flatMap((id) => this.elements.find((element) => element.id === id) ?? []);
|
|
8076
|
+
const receipt = receiptId ? {
|
|
8077
|
+
id: receiptId,
|
|
8078
|
+
eventIds: requestedEventIds,
|
|
8079
|
+
elementIds: requestedElementIds,
|
|
8080
|
+
...audit2 === void 0 ? {} : { audit: audit2 },
|
|
8081
|
+
createdAt: now
|
|
8082
|
+
} : void 0;
|
|
8083
|
+
const agents = new Set(this.agentEvents.map((event) => event.id));
|
|
8084
|
+
const committed = await this.storage.commitRecords(this.namespace, this.revision, {
|
|
8085
|
+
kind: "memory-use",
|
|
8086
|
+
eventIds: events.filter((event) => !agents.has(event.id)).map((event) => event.id),
|
|
8087
|
+
agentEventIds: events.filter((event) => agents.has(event.id)).map((event) => event.id),
|
|
8088
|
+
elementIds: elements.map((element) => element.id),
|
|
8089
|
+
updatedAt: now,
|
|
8090
|
+
...receipt ? { receipt } : {}
|
|
8091
|
+
});
|
|
8092
|
+
if (committed.changed) {
|
|
8093
|
+
for (const record3 of [...events, ...elements]) {
|
|
8094
|
+
record3.weight.mentionCount += 1;
|
|
8095
|
+
record3.weight.lastAdoptedTurn = this.currentTurn;
|
|
8096
|
+
record3.updatedAt = now;
|
|
8097
|
+
}
|
|
8098
|
+
if (receipt) this.usageReceipts.set(receipt.id, receipt);
|
|
8099
|
+
const eventIds = new Set(events.map((event) => event.id)), elementIds = new Set(elements.map((element) => element.id));
|
|
8100
|
+
for (const [records, ids2] of [
|
|
8101
|
+
[[...this.committedMetadata.events, ...this.committedMetadata.agentEvents], eventIds],
|
|
8102
|
+
[this.committedMetadata.elements, elementIds]
|
|
8103
|
+
]) for (const record3 of records) if (ids2.has(record3.id)) {
|
|
8104
|
+
record3.weight.mentionCount += 1;
|
|
8105
|
+
record3.weight.lastAdoptedTurn = this.currentTurn;
|
|
8106
|
+
record3.updatedAt = now;
|
|
8107
|
+
}
|
|
8108
|
+
if (receipt) this.committedMetadata.usageReceipts.push(structuredClone(receipt));
|
|
8109
|
+
}
|
|
8110
|
+
this.revision = committed.revision;
|
|
7551
8111
|
});
|
|
8112
|
+
await this.commitMutation(recordUseInMemory);
|
|
7552
8113
|
}
|
|
7553
8114
|
async pinEvent(id, pinned = true) {
|
|
8115
|
+
if (this.storage?.commitRecords) return this.withMutationQueue(async () => {
|
|
8116
|
+
const event = this.requireEvent(id);
|
|
8117
|
+
const updatedAt = toUtc8Iso(this.now());
|
|
8118
|
+
if (this.hasPendingRecordChanges()) return this.applyMutation(() => {
|
|
8119
|
+
event.weight.pinned = pinned;
|
|
8120
|
+
event.updatedAt = updatedAt;
|
|
8121
|
+
});
|
|
8122
|
+
const committed = await this.storage.commitRecords(this.namespace, this.revision, {
|
|
8123
|
+
kind: "pin-event",
|
|
8124
|
+
id,
|
|
8125
|
+
pinned,
|
|
8126
|
+
agent: this.agentEvents.includes(event),
|
|
8127
|
+
updatedAt
|
|
8128
|
+
});
|
|
8129
|
+
event.weight.pinned = pinned;
|
|
8130
|
+
event.updatedAt = updatedAt;
|
|
8131
|
+
const baseline = [...this.committedMetadata.events, ...this.committedMetadata.agentEvents].find((item) => item.id === id);
|
|
8132
|
+
baseline.weight.pinned = pinned;
|
|
8133
|
+
baseline.updatedAt = updatedAt;
|
|
8134
|
+
this.revision = committed.revision;
|
|
8135
|
+
});
|
|
7554
8136
|
await this.commitMutation(() => {
|
|
7555
8137
|
const event = this.requireEvent(id);
|
|
7556
8138
|
event.weight.pinned = pinned;
|
|
@@ -8915,6 +9497,29 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8915
9497
|
async commitMutation(mutation, graphEpoch) {
|
|
8916
9498
|
return this.withMutationQueue(() => this.applyMutation(mutation, graphEpoch));
|
|
8917
9499
|
}
|
|
9500
|
+
hasPendingRecordChanges() {
|
|
9501
|
+
return !this.committedMetadata || hasRecordChanges(this.committedMetadata, this.snapshotView(false));
|
|
9502
|
+
}
|
|
9503
|
+
/** getBlockContext historically updates the derived pointer while reading.
|
|
9504
|
+
* Include those pending pointer values in the next surface transaction;
|
|
9505
|
+
* semantic or external record edits still take the general Unit of Work.
|
|
9506
|
+
*/
|
|
9507
|
+
pendingPointerUpdates() {
|
|
9508
|
+
if (!this.committedMetadata) return null;
|
|
9509
|
+
const changes = snapshotWriteSet(this.committedMetadata, this.snapshotView(false));
|
|
9510
|
+
if (changes.graph || changes.messages.length || changes.deletedMessages.size || [...changes.removed.values()].some((ids2) => ids2.size) || [...changes.changed].some(([collection, ids2]) => collection !== "blocks" && ids2.size)) return null;
|
|
9511
|
+
const previous = new Map(this.committedMetadata.blocks.map((block) => [block.id, block]));
|
|
9512
|
+
const updates = [];
|
|
9513
|
+
for (const block of this.blocks) if (changes.changed.get("blocks").has(block.id)) {
|
|
9514
|
+
const old = previous.get(block.id);
|
|
9515
|
+
if (!old) return null;
|
|
9516
|
+
const { l5Raw: _raw, pointerCurrentLevel: _level, ...metadata2 } = block;
|
|
9517
|
+
const { l5Raw: _oldRaw, pointerCurrentLevel: _oldLevel, ...oldMetadata } = old;
|
|
9518
|
+
if (!isDeepStrictEqual2(metadata2, oldMetadata)) return null;
|
|
9519
|
+
updates.push({ id: block.id, pointerCurrentLevel: block.pointerCurrentLevel });
|
|
9520
|
+
}
|
|
9521
|
+
return updates;
|
|
9522
|
+
}
|
|
8918
9523
|
async withMutationQueue(operation) {
|
|
8919
9524
|
if (this.storage?.readonly) throw new Error("Cannot mutate read-only StrataGate storage.");
|
|
8920
9525
|
const previous = this.mutationQueue;
|
|
@@ -8936,7 +9541,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8936
9541
|
this.topicDirectory.synchronize(this.listAllEvents(), toUtc8Iso(this.now()));
|
|
8937
9542
|
return result;
|
|
8938
9543
|
}
|
|
8939
|
-
const before = this.exportSnapshot();
|
|
9544
|
+
const before = this.storage.saveChanges ? captureMutationState(this.snapshotView()) : this.exportSnapshot();
|
|
8940
9545
|
const beforeRevision = this.revision;
|
|
8941
9546
|
const beforeRawUpserts = new Map(this.pendingRawUpserts);
|
|
8942
9547
|
const beforeRawDeletes = new Set(this.pendingRawDeletes);
|
|
@@ -8961,7 +9566,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8961
9566
|
upsert: [...this.pendingRawUpserts.values()],
|
|
8962
9567
|
deleteIds: [...this.pendingRawDeletes]
|
|
8963
9568
|
};
|
|
8964
|
-
const snapshot = this.exportSnapshot();
|
|
9569
|
+
const snapshot = this.storage.saveChanges ? this.snapshotView() : this.exportSnapshot();
|
|
8965
9570
|
const guards = [];
|
|
8966
9571
|
if (before) {
|
|
8967
9572
|
for (const kind of ["summary", "extraction", "graph", "topic"]) {
|
|
@@ -8974,7 +9579,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8974
9579
|
});
|
|
8975
9580
|
}
|
|
8976
9581
|
}
|
|
8977
|
-
this.revision = await this.storage.save(this.namespace, snapshot, this.revision, delta, guards, graphEpoch);
|
|
9582
|
+
this.revision = before && this.storage.saveChanges ? await this.storage.saveChanges(this.namespace, this.committedMetadata ?? before, snapshot, this.revision, delta, guards, graphEpoch) : await this.storage.save(this.namespace, snapshot, this.revision, delta, guards, graphEpoch);
|
|
8978
9583
|
const persistedGraphIds = new Set(snapshot.graphProjectionJobs.map((job) => job.id));
|
|
8979
9584
|
for (const id of this.graphProjectionJobs.keys()) if (!persistedGraphIds.has(id)) this.graphProjectionJobs.delete(id);
|
|
8980
9585
|
for (const [jobs, live2] of [[snapshot.summaryJobs, this.summaryJobs], [snapshot.extractionJobs, this.extractionJobs]]) {
|
|
@@ -8990,8 +9595,9 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8990
9595
|
for (const job of snapshot.memoryTopicState?.jobs ?? []) if (job.leaseUntil) this.topicDirectory.renewLease(job.id, job.attempts, job.updatedAt, job.leaseUntil);
|
|
8991
9596
|
this.pendingRawUpserts.clear();
|
|
8992
9597
|
this.pendingRawDeletes.clear();
|
|
9598
|
+
this.committedMetadata = captureMutationState(this.snapshotView());
|
|
8993
9599
|
}
|
|
8994
|
-
restoreSnapshot(snapshot) {
|
|
9600
|
+
restoreSnapshot(snapshot, committed = false) {
|
|
8995
9601
|
const normalized = normalizeSnapshot(snapshot);
|
|
8996
9602
|
if (normalized.blockTurnSize !== this.blockTurnSize) {
|
|
8997
9603
|
throw new Error(`Snapshot blockTurnSize ${normalized.blockTurnSize} does not match ${this.blockTurnSize}`);
|
|
@@ -9030,6 +9636,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
9030
9636
|
this.topicDirectory.restore(copy.memoryTopicState);
|
|
9031
9637
|
this.topicDirectory.synchronize(this.listAllEvents(), toUtc8Iso(this.now()));
|
|
9032
9638
|
this.validateReferences();
|
|
9639
|
+
if (committed) this.committedMetadata = captureMutationState(this.snapshotView());
|
|
9033
9640
|
}
|
|
9034
9641
|
currentGraphPolicy() {
|
|
9035
9642
|
return this.graphPolicy?.() ?? { enabled: this.graphEnabledValue, paused: false, epoch: this.graphEpoch, backfillEventIds: this.graphApprovedEventIds };
|
|
@@ -9835,6 +10442,65 @@ function parseJsonResponse(value, requiredKeys = []) {
|
|
|
9835
10442
|
throw new ModelJsonResponseError(void 0, { response: value });
|
|
9836
10443
|
}
|
|
9837
10444
|
|
|
10445
|
+
// src/extraction-batches.ts
|
|
10446
|
+
var ExtractionCapacityError = class extends Error {
|
|
10447
|
+
constructor(capacity) {
|
|
10448
|
+
super(`Event extraction ${capacity} capacity exceeded; a smaller source batch is required`);
|
|
10449
|
+
this.capacity = capacity;
|
|
10450
|
+
}
|
|
10451
|
+
capacity;
|
|
10452
|
+
};
|
|
10453
|
+
var CONTEXT_CHARS = 1200;
|
|
10454
|
+
function splitExtractionSlice(slice) {
|
|
10455
|
+
const messages = slice.messages;
|
|
10456
|
+
let left;
|
|
10457
|
+
let right;
|
|
10458
|
+
if (messages.length > 1) {
|
|
10459
|
+
const weights = messages.map((message) => estimateTokens(JSON.stringify(message)));
|
|
10460
|
+
const half = weights.reduce((sum, size2) => sum + size2, 0) / 2;
|
|
10461
|
+
let index = 1;
|
|
10462
|
+
let size = weights[0];
|
|
10463
|
+
while (index < messages.length - 1 && size < half) size += weights[index++];
|
|
10464
|
+
left = messages.slice(0, index);
|
|
10465
|
+
right = messages.slice(index);
|
|
10466
|
+
} else {
|
|
10467
|
+
const message = messages[0];
|
|
10468
|
+
if (!message) throw new Error("Event extraction has no source to split");
|
|
10469
|
+
const content = message.content + (message.toolCalls?.length ? `
|
|
10470
|
+
[Tool evidence, JSON]
|
|
10471
|
+
${JSON.stringify(message.toolCalls)}` : "");
|
|
10472
|
+
if (content.length < 2) throw new Error("Event extraction cannot split this source any further");
|
|
10473
|
+
const middle = Math.floor(content.length / 2);
|
|
10474
|
+
const boundary = content.lastIndexOf("\n", middle);
|
|
10475
|
+
let cut = boundary >= middle / 2 ? boundary + 1 : middle;
|
|
10476
|
+
if (/[\uD800-\uDBFF]/.test(content[cut - 1])) cut += 1;
|
|
10477
|
+
if (cut >= content.length) throw new Error("Event extraction cannot split this source any further");
|
|
10478
|
+
const { toolCalls: _tools, ...base } = message;
|
|
10479
|
+
left = [{ ...base, content: content.slice(0, cut) }];
|
|
10480
|
+
right = [{ ...base, content: content.slice(cut) }];
|
|
10481
|
+
}
|
|
10482
|
+
const excerpt = (items2) => items2.map((message) => JSON.stringify(message)).join("\n");
|
|
10483
|
+
return [
|
|
10484
|
+
{ messages: left, before: slice.before, after: excerpt(right).slice(0, CONTEXT_CHARS), depth: slice.depth + 1 },
|
|
10485
|
+
{ messages: right, before: excerpt(left).slice(-CONTEXT_CHARS), after: slice.after, depth: slice.depth + 1 }
|
|
10486
|
+
];
|
|
10487
|
+
}
|
|
10488
|
+
function mergeExtractionEvents(events, exactKey) {
|
|
10489
|
+
const unique3 = /* @__PURE__ */ new Map();
|
|
10490
|
+
for (const event of events) {
|
|
10491
|
+
const key = exactKey(event);
|
|
10492
|
+
const prior = unique3.get(key);
|
|
10493
|
+
if (!prior) {
|
|
10494
|
+
unique3.set(key, structuredClone(event));
|
|
10495
|
+
continue;
|
|
10496
|
+
}
|
|
10497
|
+
prior.sourceMessageIds = [.../* @__PURE__ */ new Set([...prior.sourceMessageIds, ...event.sourceMessageIds])];
|
|
10498
|
+
prior.tags = [.../* @__PURE__ */ new Set([...prior.tags ?? [], ...event.tags ?? []])].slice(0, 16);
|
|
10499
|
+
prior.quotes = [.../* @__PURE__ */ new Set([...prior.quotes ?? [], ...event.quotes ?? []])].slice(0, 12);
|
|
10500
|
+
}
|
|
10501
|
+
return [...unique3.values()];
|
|
10502
|
+
}
|
|
10503
|
+
|
|
9838
10504
|
// src/llm.ts
|
|
9839
10505
|
import { randomUUID as randomUUID5 } from "node:crypto";
|
|
9840
10506
|
|
|
@@ -10184,27 +10850,38 @@ function l2Neighbor(block) {
|
|
|
10184
10850
|
l2Keypoints: block.l2Keypoints
|
|
10185
10851
|
};
|
|
10186
10852
|
}
|
|
10187
|
-
function extractorPayload(context) {
|
|
10188
|
-
|
|
10853
|
+
function extractorPayload(context, slice) {
|
|
10854
|
+
const payload = {
|
|
10189
10855
|
target: {
|
|
10190
10856
|
blockId: context.target.id,
|
|
10191
10857
|
sequence: context.target.sequence,
|
|
10192
10858
|
startTurn: context.target.startTurn,
|
|
10193
10859
|
endTurn: context.target.endTurn,
|
|
10194
10860
|
createdAt: context.target.createdAt,
|
|
10195
|
-
messages:
|
|
10861
|
+
messages: slice.messages,
|
|
10862
|
+
...slice.depth ? { fragment: { depth: slice.depth, beforeContextOnly: slice.before, afterContextOnly: slice.after } } : {}
|
|
10196
10863
|
},
|
|
10197
10864
|
neighbors: {
|
|
10198
10865
|
previous: l2Neighbor(context.previous),
|
|
10199
10866
|
next: l2Neighbor(context.next)
|
|
10200
10867
|
},
|
|
10201
|
-
allowedSourceMessageIds:
|
|
10202
|
-
timeline: context.timeline
|
|
10868
|
+
allowedSourceMessageIds: [...new Set(slice.messages.map((message) => message.id))],
|
|
10869
|
+
timeline: [...context.timeline]
|
|
10203
10870
|
};
|
|
10871
|
+
if (estimateTokens(JSON.stringify(payload.neighbors)) > 3e3) {
|
|
10872
|
+
payload.neighbors = { previous: null, next: null };
|
|
10873
|
+
}
|
|
10874
|
+
while (payload.timeline.length && estimateTokens(JSON.stringify({ neighbors: payload.neighbors, timeline: payload.timeline })) > 3e3) payload.timeline.pop();
|
|
10875
|
+
return payload;
|
|
10204
10876
|
}
|
|
10205
10877
|
var JSON_RESPONSE_ATTEMPTS = 2;
|
|
10206
10878
|
var JSON_RETRY_INSTRUCTION = "Your previous response did not make one valid call to the requested tool. Do not spend output on analysis or reasoning. Immediately call that tool exactly once with complete arguments. Do not return an answer as text or markdown.";
|
|
10207
10879
|
var DEFAULT_STRUCTURED_TIMEOUT_MS = 12e4;
|
|
10880
|
+
var EXTRACTION_OUTPUT_TOKENS = 8192;
|
|
10881
|
+
var EXTRACTION_INPUT_TOKENS = 12e3;
|
|
10882
|
+
var EXTRACTION_MAX_CALLS = 32;
|
|
10883
|
+
var EXTRACTION_MAX_DEPTH = 8;
|
|
10884
|
+
var EXTRACTION_MAX_RESERVED_TOKENS = 262144;
|
|
10208
10885
|
var STRUCTURED_FIELDS = {
|
|
10209
10886
|
summarizer: ["l0Title", "l0Tags", "l1Summary", "l2Keypoints", "shouldExtract"],
|
|
10210
10887
|
extractor: ["shouldExtract", "reason", "events"],
|
|
@@ -10320,14 +10997,14 @@ function normalizeGraphNameProvenance(value) {
|
|
|
10320
10997
|
if (!Array.isArray(result.nodes)) return value;
|
|
10321
10998
|
for (const candidate of result.nodes) {
|
|
10322
10999
|
const node = object(candidate);
|
|
10323
|
-
const
|
|
10324
|
-
const entries =
|
|
11000
|
+
const metadata2 = object(node.metadataProvenance);
|
|
11001
|
+
const entries = metadata2.name;
|
|
10325
11002
|
if (typeof node.name !== "string" || !Array.isArray(entries) || entries.length === 0) continue;
|
|
10326
11003
|
if (!entries.every((candidateEntry) => {
|
|
10327
11004
|
const entry = object(candidateEntry);
|
|
10328
11005
|
return entry.value === node.name && Object.keys(entry).every((key) => key === "value" || key === "sourceEventIds") && Array.isArray(entry.sourceEventIds) && entry.sourceEventIds.length > 0 && entry.sourceEventIds.every((id) => typeof id === "string");
|
|
10329
11006
|
})) continue;
|
|
10330
|
-
|
|
11007
|
+
metadata2.name = [...new Set(entries.flatMap((entry) => entry.sourceEventIds))];
|
|
10331
11008
|
}
|
|
10332
11009
|
return value;
|
|
10333
11010
|
}
|
|
@@ -10738,6 +11415,7 @@ var DshModelBridge = class {
|
|
|
10738
11415
|
ctx;
|
|
10739
11416
|
config;
|
|
10740
11417
|
liveStructuredReasoningEffort;
|
|
11418
|
+
extractionBudgets = new AsyncLocalStorage();
|
|
10741
11419
|
diagnosticScopes = new AsyncLocalStorage();
|
|
10742
11420
|
diagnostics;
|
|
10743
11421
|
diagnosticNamespace = () => "@shared";
|
|
@@ -10817,7 +11495,40 @@ Choose exactly one action using stratagate_dreaming_step. args is an object. Fin
|
|
|
10817
11495
|
};
|
|
10818
11496
|
};
|
|
10819
11497
|
extractor = async (context) => {
|
|
10820
|
-
const
|
|
11498
|
+
const budget = { calls: 0, reservedTokens: 0, deadline: Date.now() + Math.min(
|
|
11499
|
+
36e4,
|
|
11500
|
+
3 * (this.config.structuredTaskTimeoutMs ?? DEFAULT_STRUCTURED_TIMEOUT_MS)
|
|
11501
|
+
) };
|
|
11502
|
+
return this.extractionBudgets.run(budget, async () => {
|
|
11503
|
+
const exactKeys = /* @__PURE__ */ new WeakMap();
|
|
11504
|
+
const visit = async (slice) => {
|
|
11505
|
+
this.assertExtractionBudget();
|
|
11506
|
+
try {
|
|
11507
|
+
return (await this.extractSlice(context, slice, exactKeys)).events;
|
|
11508
|
+
} catch (error) {
|
|
11509
|
+
if (!(error instanceof ExtractionCapacityError)) throw error;
|
|
11510
|
+
if (slice.depth >= EXTRACTION_MAX_DEPTH) throw new Error("Event extraction split depth exhausted; original Block remains pending", { cause: error });
|
|
11511
|
+
const [left, right] = splitExtractionSlice(slice);
|
|
11512
|
+
return [...await visit(left), ...await visit(right)];
|
|
11513
|
+
}
|
|
11514
|
+
};
|
|
11515
|
+
const events = mergeExtractionEvents(await visit({
|
|
11516
|
+
messages: buildMemoryDerivationMessages(context.target.l5Raw),
|
|
11517
|
+
before: "",
|
|
11518
|
+
after: "",
|
|
11519
|
+
depth: 0
|
|
11520
|
+
}), (event) => exactKeys.get(event));
|
|
11521
|
+
return { shouldExtract: events.length > 0, reason: events.length ? "Durable evidence extracted." : "No durable evidence.", events };
|
|
11522
|
+
});
|
|
11523
|
+
};
|
|
11524
|
+
assertExtractionBudget() {
|
|
11525
|
+
const budget = this.extractionBudgets.getStore();
|
|
11526
|
+
if (budget && (budget.calls >= EXTRACTION_MAX_CALLS || Date.now() >= budget.deadline)) {
|
|
11527
|
+
throw new Error("Event extraction shared call/time budget exhausted; original Block remains pending");
|
|
11528
|
+
}
|
|
11529
|
+
}
|
|
11530
|
+
async extractSlice(context, slice, exactKeys) {
|
|
11531
|
+
const validMessageIds = new Set(slice.messages.map((message) => message.id));
|
|
10821
11532
|
const raw = object(await this.callStructured(
|
|
10822
11533
|
"extractor",
|
|
10823
11534
|
`You are the StrataGate Event Extractor. Make the final extraction decision independently of the Summarizer's high-recall pre-screen. Extract durable, evidence-backed events from target.messages, then call ${STRUCTURED_TOOLS.extractor.name} exactly once. If none qualifies, set shouldExtract=false and events=[]; give a brief reason, not an Event body.
|
|
@@ -10838,10 +11549,21 @@ Preserve source certainty and attribution: distinguish a user statement or decis
|
|
|
10838
11549
|
|
|
10839
11550
|
target.messages is a provenance-preserving derivation view of the target Block: message ids and conversational text are retained, while tool code and oversized tool payloads may be marked compacted. Use only retained tool names, evidence summaries, and excerpts; do not invent omitted details. Only target.messages may supply new facts, exact quotes, and sourceMessageIds. neighbors.previous and neighbors.next are context-only L2 summaries; timeline only helps identify historical relationships. Neither neighbors nor timeline nor an assistant recap of older memory can create a new Event. Require new human input or a new observable task/tool outcome from target.messages. Every sourceMessageIds entry must exactly match allowedSourceMessageIds and directly support the Event.
|
|
10840
11551
|
|
|
11552
|
+
When target.fragment is present, messages are a consecutive source slice, possibly part of one original message. beforeContextOnly and afterContextOnly are adjacent excerpts for resolving references, qualifiers and corrections only, never additional factual evidence or quote sources. Do not infer a completed sentence or tool result from a cut fragment. Preserve corrections, cancellations and uncertainty visible in the source; do not lower admission or merge independent facts to fit the output budget.
|
|
11553
|
+
|
|
10841
11554
|
Use project scope for repository decisions, user scope for stable preferences/identity, and session scope for temporary task state. temporal.eventType must use exactly one stable value: decision, release, task_completed, plan, change, cancellation, incident, meeting, collaboration, migration, or other. temporal.participants contains canonical entity names. Use ISO-8601 timestamps with the explicit +08:00 offset in temporal fields. Keep happened time separate from mentionedAt; when happened time is unknown omit it and set precision/basis to unknown. Do not return the result as text.`,
|
|
10842
|
-
extractorPayload(context)
|
|
11555
|
+
extractorPayload(context, slice),
|
|
11556
|
+
(value) => {
|
|
11557
|
+
const result = object(value);
|
|
11558
|
+
if (result.shouldExtract !== true) return;
|
|
11559
|
+
const events2 = Array.isArray(result.events) ? result.events : [];
|
|
11560
|
+
if (!events2.length || events2.some((candidate) => {
|
|
11561
|
+
const event = object(candidate);
|
|
11562
|
+
const ids2 = strings2(event.sourceMessageIds);
|
|
11563
|
+
return !text2(event.title) || !text2(event.summary) || !ids2.length || ids2.some((id) => !validMessageIds.has(id));
|
|
11564
|
+
})) throw new Error("Extraction requested events but returned empty/invalid evidence or source IDs; correct every event using allowedSourceMessageIds");
|
|
11565
|
+
}
|
|
10843
11566
|
));
|
|
10844
|
-
const exactKeys = /* @__PURE__ */ new WeakMap();
|
|
10845
11567
|
const candidates = (Array.isArray(raw.events) ? raw.events : []).map((candidate) => {
|
|
10846
11568
|
const item = object(candidate);
|
|
10847
11569
|
const sourceMessageIds = strings2(item.sourceMessageIds).filter((id) => validMessageIds.has(id));
|
|
@@ -10883,7 +11605,7 @@ Use project scope for repository decisions, user scope for stable preferences/id
|
|
|
10883
11605
|
reason: text2(raw.reason) || (shouldExtract ? "Durable evidence extracted." : "No durable evidence."),
|
|
10884
11606
|
events: shouldExtract ? events : []
|
|
10885
11607
|
};
|
|
10886
|
-
}
|
|
11608
|
+
}
|
|
10887
11609
|
projector = async (context) => {
|
|
10888
11610
|
const eventIds = new Set(context.events.map((event) => event.id));
|
|
10889
11611
|
const raw = object(await this.callStructured(
|
|
@@ -11102,6 +11824,7 @@ Use project scope for repository decisions, user scope for stable preferences/id
|
|
|
11102
11824
|
});
|
|
11103
11825
|
return result;
|
|
11104
11826
|
} catch (error) {
|
|
11827
|
+
if (error instanceof ExtractionCapacityError && !scope.calls) throw error;
|
|
11105
11828
|
this.diagnostics?.record(scope, {
|
|
11106
11829
|
operations: 1,
|
|
11107
11830
|
operationsSucceeded: 0,
|
|
@@ -11169,7 +11892,7 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
|
|
|
11169
11892
|
type: "function",
|
|
11170
11893
|
function: { name: STRUCTURED_TOOLS[kind].name }
|
|
11171
11894
|
},
|
|
11172
|
-
maxTokens: kind === "dreamingStep" ? TOPIC_OUTPUT_TOKEN_BUDGET : kind === "profileMaintenance" ? Math.max(this.config.maxOutputTokens, Math.min(12e3, 512 + 2 * Array.from(JSON.stringify(payload)).length)) : isTopic ? TOPIC_OUTPUT_TOKEN_BUDGET : isGraph ? GRAPH_OUTPUT_TOKEN_BUDGET : this.config.maxOutputTokens,
|
|
11895
|
+
maxTokens: kind === "dreamingStep" ? TOPIC_OUTPUT_TOKEN_BUDGET : kind === "profileMaintenance" ? Math.max(this.config.maxOutputTokens, Math.min(12e3, 512 + 2 * Array.from(JSON.stringify(payload)).length)) : isTopic ? TOPIC_OUTPUT_TOKEN_BUDGET : isGraph ? GRAPH_OUTPUT_TOKEN_BUDGET : kind === "extractor" ? Math.max(EXTRACTION_OUTPUT_TOKENS, this.config.maxOutputTokens) : this.config.maxOutputTokens,
|
|
11173
11896
|
sessionId: execution.sessionId,
|
|
11174
11897
|
purpose: "compaction"
|
|
11175
11898
|
};
|
|
@@ -11193,6 +11916,15 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
|
|
|
11193
11916
|
outputTokenBudget: request.maxTokens
|
|
11194
11917
|
}) }] }];
|
|
11195
11918
|
}
|
|
11919
|
+
if (kind === "extractor") {
|
|
11920
|
+
const window = this.contextWindows.get(routeKey) ?? 32768;
|
|
11921
|
+
const input = estimateTokens(JSON.stringify({ system: request.system, tools: request.tools, messages: request.messages }));
|
|
11922
|
+
const reserve = Math.min(request.maxTokens, Math.max(256, Math.floor(window / 4)));
|
|
11923
|
+
if (input + estimateTokens(JSON_RETRY_INSTRUCTION) > Math.min(EXTRACTION_INPUT_TOKENS, window - reserve - 1024)) {
|
|
11924
|
+
throw new ExtractionCapacityError("input");
|
|
11925
|
+
}
|
|
11926
|
+
request.maxTokens = Math.min(request.maxTokens, Math.floor(window - input - 1024));
|
|
11927
|
+
}
|
|
11196
11928
|
if (isTopic) {
|
|
11197
11929
|
const estimateInput = () => estimateTokens(JSON.stringify({ system: request.system, tools: request.tools, messages: request.messages }));
|
|
11198
11930
|
let inputTokens = estimateInput();
|
|
@@ -11217,6 +11949,18 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
|
|
|
11217
11949
|
});
|
|
11218
11950
|
}
|
|
11219
11951
|
try {
|
|
11952
|
+
if (kind === "extractor") {
|
|
11953
|
+
this.assertExtractionBudget();
|
|
11954
|
+
const budget = this.extractionBudgets.getStore();
|
|
11955
|
+
if (budget) {
|
|
11956
|
+
const reservation = estimateTokens(JSON.stringify({ system: request.system, tools: request.tools, messages: request.messages })) + request.maxTokens;
|
|
11957
|
+
if (budget.reservedTokens + reservation > EXTRACTION_MAX_RESERVED_TOKENS) {
|
|
11958
|
+
throw new Error("Event extraction shared token budget exhausted; original Block remains pending");
|
|
11959
|
+
}
|
|
11960
|
+
budget.calls += 1;
|
|
11961
|
+
budget.reservedTokens += reservation;
|
|
11962
|
+
}
|
|
11963
|
+
}
|
|
11220
11964
|
await this.consumeStructuredStream(request, assembler);
|
|
11221
11965
|
} catch (error) {
|
|
11222
11966
|
if (!noAdapterRetried && isNoAdapter(error)) {
|
|
@@ -11240,6 +11984,7 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
|
|
|
11240
11984
|
}
|
|
11241
11985
|
assertActive?.();
|
|
11242
11986
|
const finish = assembler.finish;
|
|
11987
|
+
if (kind === "extractor" && finish.kind === "max-tokens") throw new ExtractionCapacityError("output");
|
|
11243
11988
|
if (isTopic) topicDiagnostics.finishReason = finish.kind;
|
|
11244
11989
|
if (finish.kind === "error" || finish.kind === "aborted") {
|
|
11245
11990
|
const failure = new Error(`StrataGate model call failed [${finish.failure.code}]: ${finish.failure.message}`);
|
|
@@ -11431,7 +12176,10 @@ Correct the previous Topic output using these validation errors: ${JSON.stringif
|
|
|
11431
12176
|
this.ctx.logger.warn(`stratagate-memory ${message}`);
|
|
11432
12177
|
}
|
|
11433
12178
|
async consumeStructuredStream(request, assembler) {
|
|
11434
|
-
const timeoutMs =
|
|
12179
|
+
const timeoutMs = Math.max(1, Math.min(
|
|
12180
|
+
this.config.structuredTaskTimeoutMs ?? DEFAULT_STRUCTURED_TIMEOUT_MS,
|
|
12181
|
+
(this.extractionBudgets.getStore()?.deadline ?? Infinity) - Date.now()
|
|
12182
|
+
));
|
|
11435
12183
|
const controller = new AbortController();
|
|
11436
12184
|
const externalSignal = this.sessions.getStore()?.signal;
|
|
11437
12185
|
const aborted = new Promise((_resolve, reject) => {
|
|
@@ -11736,22 +12484,24 @@ function validDate(date) {
|
|
|
11736
12484
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || !Number.isFinite(Date.parse(date)) || new Date(date).toISOString().slice(0, 10) !== date) throw new TypeError("date \u5FC5\u987B\u662F\u6709\u6548\u7684 YYYY-MM-DD");
|
|
11737
12485
|
return date;
|
|
11738
12486
|
}
|
|
11739
|
-
function selectDailyInjectionDates(dates) {
|
|
11740
|
-
return dates.slice(0,
|
|
12487
|
+
function selectDailyInjectionDates(dates, days = 3) {
|
|
12488
|
+
return dates.slice(0, days);
|
|
11741
12489
|
}
|
|
11742
12490
|
function items(text3) {
|
|
11743
12491
|
const matches = [...text3.matchAll(/^## (.+)\r?$/gm)];
|
|
11744
12492
|
return matches.map((match, index) => ({ title: match[1], text: text3.slice(match.index, matches[index + 1]?.index ?? text3.length).trimEnd() }));
|
|
11745
12493
|
}
|
|
11746
12494
|
var DailyNotes = class {
|
|
11747
|
-
constructor(database, timeZone, budget = 12e3) {
|
|
12495
|
+
constructor(database, timeZone, budget = 12e3, injectionDays = 3) {
|
|
11748
12496
|
this.database = database;
|
|
11749
12497
|
this.budget = budget;
|
|
12498
|
+
this.injectionDays = injectionDays;
|
|
11750
12499
|
this.timeZone = timeZone ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
11751
12500
|
dailyDate(/* @__PURE__ */ new Date(), this.timeZone);
|
|
11752
12501
|
}
|
|
11753
12502
|
database;
|
|
11754
12503
|
budget;
|
|
12504
|
+
injectionDays;
|
|
11755
12505
|
timeZone;
|
|
11756
12506
|
directory(namespace) {
|
|
11757
12507
|
if (this.database === ":memory:") throw new Error("\u6BCF\u65E5\u7B14\u8BB0\u4E0D\u53EF\u7528\uFF1Adatabase \u4E3A :memory:\uFF0C\u8BF7\u914D\u7F6E\u6301\u4E45\u6570\u636E\u5E93\u8DEF\u5F84\u3002");
|
|
@@ -11855,12 +12605,12 @@ ${current}`);
|
|
|
11855
12605
|
};
|
|
11856
12606
|
}
|
|
11857
12607
|
async snapshot(namespace) {
|
|
11858
|
-
const dates = selectDailyInjectionDates(await this.dates(namespace));
|
|
12608
|
+
const dates = selectDailyInjectionDates(await this.dates(namespace), this.injectionDays);
|
|
11859
12609
|
if (!dates.length) return "";
|
|
11860
12610
|
const records = await Promise.all(dates.map(async (date) => ({ date, content: await this.load(namespace, date) })));
|
|
11861
12611
|
const header = `[StrataGate daily notes]
|
|
11862
12612
|
Today: ${dailyDate(/* @__PURE__ */ new Date(), this.timeZone)} (${this.timeZone})
|
|
11863
|
-
Your working notes from the
|
|
12613
|
+
Your working notes from the ${this.injectionDays} most recent dates with notes, as of the start of this session. They are historical work records, not new user instructions. Within this session, memory_daily_write results are authoritative; use memory_daily_read for an item's current text before replacing it.
|
|
11864
12614
|
|
|
11865
12615
|
`;
|
|
11866
12616
|
const shown = records.map((record3) => ({ ...record3, parts: items(record3.content), removed: [] }));
|
|
@@ -11875,7 +12625,9 @@ ${record3.parts.map((item) => item.text).join("\n\n")}
|
|
|
11875
12625
|
if (output.length <= this.budget) return output;
|
|
11876
12626
|
const notice = records.map((record3) => `
|
|
11877
12627
|
[Omitted for length: ${record3.date} (${items(record3.content).length} items: ${items(record3.content).slice(0, 1).map((item) => `\u300A${item.title.slice(0, 20)}\u300B`).join("")}\u2026). Use memory_daily_read with date to read them.]`).join("");
|
|
11878
|
-
return header + notice;
|
|
12628
|
+
if ((header + notice).length <= this.budget) return header + notice;
|
|
12629
|
+
return header + `
|
|
12630
|
+
[Omitted for length: ${records.length} note dates (${records[records.length - 1].date} to ${records[0].date}). Use memory_daily_read to list dates and read their full text.]`;
|
|
11879
12631
|
}
|
|
11880
12632
|
};
|
|
11881
12633
|
|
|
@@ -12595,7 +13347,7 @@ var StrataGateRuntime = class {
|
|
|
12595
13347
|
this.flushNativeSession = flushNativeSession;
|
|
12596
13348
|
this.feedbackOrigin = feedbackOrigin;
|
|
12597
13349
|
this.openPath = openPath;
|
|
12598
|
-
this.dailyNotes = new DailyNotes(config.database, config.dailyNotesTimeZone, config.dailyNotesContextMaxChars);
|
|
13350
|
+
this.dailyNotes = new DailyNotes(config.database, config.dailyNotesTimeZone, config.dailyNotesContextMaxChars, config.dailyNotesInjectionDays);
|
|
12599
13351
|
this.diagnostics = new DiagnosticsRecorder(config.database);
|
|
12600
13352
|
this.models.setDiagnostics?.(this.diagnostics, (session, sessionId) => session ? this.namespaceFor(session) : this.diagnosticNamespaceForSession(sessionId ?? "") ?? "@shared");
|
|
12601
13353
|
this.blockTurnSize = config.blockTurnSize;
|
|
@@ -12773,12 +13525,12 @@ var StrataGateRuntime = class {
|
|
|
12773
13525
|
if (!existsSync2(this.config.database)) return;
|
|
12774
13526
|
const storage = this.profileStore();
|
|
12775
13527
|
if (await storage.rebuildBackgroundWorkIndex(8, (error) => this.onIngestError(error))) this.backgroundWorkerWakePending = true;
|
|
12776
|
-
const
|
|
13528
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
12777
13529
|
try {
|
|
12778
13530
|
const now = Date.now();
|
|
12779
|
-
namespaces = storage.dueBackgroundNamespaces(now, typeof this.models.topicProjector === "function",
|
|
13531
|
+
namespaces = storage.dueBackgroundNamespaces(now, typeof this.models.topicProjector === "function", metadata2.topicBootstrapNotBefore(now));
|
|
12780
13532
|
} finally {
|
|
12781
|
-
|
|
13533
|
+
metadata2.close();
|
|
12782
13534
|
}
|
|
12783
13535
|
}
|
|
12784
13536
|
} catch (error) {
|
|
@@ -12796,17 +13548,17 @@ var StrataGateRuntime = class {
|
|
|
12796
13548
|
}
|
|
12797
13549
|
backgroundWorkerDelay() {
|
|
12798
13550
|
if (!this.profileStorage || !this.models.isReady() || this.config.database === ":memory:") return BACKGROUND_WORKER_INTERVAL_MS;
|
|
12799
|
-
let
|
|
13551
|
+
let metadata2;
|
|
12800
13552
|
try {
|
|
12801
|
-
|
|
13553
|
+
metadata2 = new DshMetadataStore(this.config.database);
|
|
12802
13554
|
const now = Date.now();
|
|
12803
|
-
const time = this.profileStorage.nextBackgroundWorkAt(typeof this.models.topicProjector === "function",
|
|
13555
|
+
const time = this.profileStorage.nextBackgroundWorkAt(typeof this.models.topicProjector === "function", metadata2.topicBootstrapNotBefore(now));
|
|
12804
13556
|
return time !== null && time > now ? Math.min(BACKGROUND_WORKER_INTERVAL_MS, time - now) : BACKGROUND_WORKER_INTERVAL_MS;
|
|
12805
13557
|
} catch (error) {
|
|
12806
13558
|
this.onIngestError(error);
|
|
12807
13559
|
return BACKGROUND_WORKER_INTERVAL_MS;
|
|
12808
13560
|
} finally {
|
|
12809
|
-
|
|
13561
|
+
metadata2?.close();
|
|
12810
13562
|
}
|
|
12811
13563
|
}
|
|
12812
13564
|
async runProfileMaintenance(now = Date.now()) {
|
|
@@ -13064,12 +13816,12 @@ var StrataGateRuntime = class {
|
|
|
13064
13816
|
if (canProjectTopics) {
|
|
13065
13817
|
let job = memory.hasPendingTopicWork("incremental") ? await this.retryTopicWrite(namespace, memory, () => memory.claimNextTopicProjection("incremental")) : null;
|
|
13066
13818
|
if (!job && memory.hasPendingTopicWork("bootstrap")) {
|
|
13067
|
-
const
|
|
13819
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
13068
13820
|
let permitted;
|
|
13069
13821
|
try {
|
|
13070
|
-
permitted =
|
|
13822
|
+
permitted = metadata2.reserveTopicBootstrapCall();
|
|
13071
13823
|
} finally {
|
|
13072
|
-
|
|
13824
|
+
metadata2.close();
|
|
13073
13825
|
}
|
|
13074
13826
|
if (permitted) job = await this.retryTopicWrite(namespace, memory, () => memory.claimNextTopicProjection("bootstrap"));
|
|
13075
13827
|
}
|
|
@@ -14382,24 +15134,24 @@ var StrataGateRuntime = class {
|
|
|
14382
15134
|
const cached = this.feedbackDrafts.get(namespace);
|
|
14383
15135
|
if (cached) return structuredClone(cached);
|
|
14384
15136
|
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
14385
|
-
const
|
|
15137
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
14386
15138
|
try {
|
|
14387
|
-
const value =
|
|
15139
|
+
const value = metadata2.feedbackDraft(namespace);
|
|
14388
15140
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
14389
15141
|
const draft = normalizeFeedbackDraft(value, value);
|
|
14390
15142
|
this.feedbackDrafts.set(namespace, draft);
|
|
14391
15143
|
return structuredClone(draft);
|
|
14392
15144
|
} finally {
|
|
14393
|
-
|
|
15145
|
+
metadata2.close();
|
|
14394
15146
|
}
|
|
14395
15147
|
}
|
|
14396
15148
|
saveFeedbackDraft(namespace, draft) {
|
|
14397
15149
|
if (this.config.database !== ":memory:") {
|
|
14398
|
-
const
|
|
15150
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
14399
15151
|
try {
|
|
14400
|
-
|
|
15152
|
+
metadata2.setFeedbackDraft(namespace, draft);
|
|
14401
15153
|
} finally {
|
|
14402
|
-
|
|
15154
|
+
metadata2.close();
|
|
14403
15155
|
}
|
|
14404
15156
|
}
|
|
14405
15157
|
this.feedbackDrafts.set(namespace, structuredClone(draft));
|
|
@@ -14452,13 +15204,13 @@ var StrataGateRuntime = class {
|
|
|
14452
15204
|
}
|
|
14453
15205
|
async syncConfiguredSettings() {
|
|
14454
15206
|
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return;
|
|
14455
|
-
const
|
|
15207
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
14456
15208
|
try {
|
|
14457
|
-
this.blockTurnSize =
|
|
14458
|
-
this.blockDecayLambda =
|
|
14459
|
-
this.agentMemoryRetrievalWeight =
|
|
15209
|
+
this.blockTurnSize = metadata2.blockTurnSize() ?? this.config.blockTurnSize;
|
|
15210
|
+
this.blockDecayLambda = metadata2.blockDecayLambda() ?? this.config.blockDecayLambda;
|
|
15211
|
+
this.agentMemoryRetrievalWeight = metadata2.agentMemoryRetrievalWeight() ?? this.config.agentMemoryRetrievalWeight ?? 1;
|
|
14460
15212
|
} finally {
|
|
14461
|
-
|
|
15213
|
+
metadata2.close();
|
|
14462
15214
|
}
|
|
14463
15215
|
const storage = new SqliteStorage({ filename: this.config.database });
|
|
14464
15216
|
try {
|
|
@@ -14505,6 +15257,42 @@ var StrataGateRuntime = class {
|
|
|
14505
15257
|
await storage.close();
|
|
14506
15258
|
}
|
|
14507
15259
|
}
|
|
15260
|
+
async adminReadBlockPage(namespace, options, chooseThread) {
|
|
15261
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
15262
|
+
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
15263
|
+
try {
|
|
15264
|
+
return storage.readBlockPage(namespace, options, chooseThread);
|
|
15265
|
+
} finally {
|
|
15266
|
+
await storage.close();
|
|
15267
|
+
}
|
|
15268
|
+
}
|
|
15269
|
+
async adminReadEventModel(namespace) {
|
|
15270
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
15271
|
+
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
15272
|
+
try {
|
|
15273
|
+
return storage.readEventModel(namespace);
|
|
15274
|
+
} finally {
|
|
15275
|
+
await storage.close();
|
|
15276
|
+
}
|
|
15277
|
+
}
|
|
15278
|
+
async adminReadNamespaceRevisions() {
|
|
15279
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return [];
|
|
15280
|
+
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
15281
|
+
try {
|
|
15282
|
+
return storage.listNamespaceRevisions();
|
|
15283
|
+
} finally {
|
|
15284
|
+
await storage.close();
|
|
15285
|
+
}
|
|
15286
|
+
}
|
|
15287
|
+
async adminReadDashboardEntries(options, chooseThread) {
|
|
15288
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return [];
|
|
15289
|
+
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
15290
|
+
try {
|
|
15291
|
+
return storage.readDashboardEntries(options, chooseThread);
|
|
15292
|
+
} finally {
|
|
15293
|
+
await storage.close();
|
|
15294
|
+
}
|
|
15295
|
+
}
|
|
14508
15296
|
externalImportView(job) {
|
|
14509
15297
|
return {
|
|
14510
15298
|
jobId: job.id,
|
|
@@ -14972,11 +15760,11 @@ var StrataGateRuntime = class {
|
|
|
14972
15760
|
this.agentMemoryRetrievalWeight = value;
|
|
14973
15761
|
try {
|
|
14974
15762
|
if (this.config.database !== ":memory:") {
|
|
14975
|
-
const
|
|
15763
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
14976
15764
|
try {
|
|
14977
|
-
|
|
15765
|
+
metadata2.setAgentMemoryRetrievalWeight(value);
|
|
14978
15766
|
} finally {
|
|
14979
|
-
|
|
15767
|
+
metadata2.close();
|
|
14980
15768
|
}
|
|
14981
15769
|
}
|
|
14982
15770
|
} catch (error) {
|
|
@@ -15006,7 +15794,8 @@ var StrataGateRuntime = class {
|
|
|
15006
15794
|
enabled: this.dailyNotesEnabled,
|
|
15007
15795
|
timeZone: this.dailyNotes.timeZone,
|
|
15008
15796
|
today: dailyDate(/* @__PURE__ */ new Date(), this.dailyNotes.timeZone),
|
|
15009
|
-
|
|
15797
|
+
injectionDays: this.dailyNotes.injectionDays,
|
|
15798
|
+
injectedDates: selectDailyInjectionDates(dates, this.dailyNotes.injectionDays),
|
|
15010
15799
|
dates
|
|
15011
15800
|
};
|
|
15012
15801
|
}
|
|
@@ -15170,11 +15959,11 @@ var StrataGateRuntime = class {
|
|
|
15170
15959
|
await this.flush();
|
|
15171
15960
|
this.blockDecayLambda = value;
|
|
15172
15961
|
if (this.config.database !== ":memory:") {
|
|
15173
|
-
const
|
|
15962
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
15174
15963
|
try {
|
|
15175
|
-
|
|
15964
|
+
metadata2.setBlockDecayLambda(value);
|
|
15176
15965
|
} finally {
|
|
15177
|
-
|
|
15966
|
+
metadata2.close();
|
|
15178
15967
|
}
|
|
15179
15968
|
}
|
|
15180
15969
|
const openNamespaces = /* @__PURE__ */ new Set();
|
|
@@ -15202,11 +15991,11 @@ var StrataGateRuntime = class {
|
|
|
15202
15991
|
await this.flush();
|
|
15203
15992
|
this.blockTurnSize = value;
|
|
15204
15993
|
if (this.config.database !== ":memory:") {
|
|
15205
|
-
const
|
|
15994
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
15206
15995
|
try {
|
|
15207
|
-
|
|
15996
|
+
metadata2.setBlockTurnSize(value);
|
|
15208
15997
|
} finally {
|
|
15209
|
-
|
|
15998
|
+
metadata2.close();
|
|
15210
15999
|
}
|
|
15211
16000
|
}
|
|
15212
16001
|
const openNamespaces = /* @__PURE__ */ new Set();
|
|
@@ -15450,11 +16239,11 @@ var StrataGateRuntime = class {
|
|
|
15450
16239
|
this.workspaceNames.set(namespace, name2);
|
|
15451
16240
|
if (this.config.database === ":memory:") return;
|
|
15452
16241
|
try {
|
|
15453
|
-
const
|
|
16242
|
+
const metadata2 = new DshMetadataStore(this.config.database);
|
|
15454
16243
|
try {
|
|
15455
|
-
|
|
16244
|
+
metadata2.rememberWorkspace(namespace, name2);
|
|
15456
16245
|
} finally {
|
|
15457
|
-
|
|
16246
|
+
metadata2.close();
|
|
15458
16247
|
}
|
|
15459
16248
|
} catch (error) {
|
|
15460
16249
|
this.onIngestError(error);
|
|
@@ -15465,11 +16254,11 @@ var StrataGateRuntime = class {
|
|
|
15465
16254
|
const responses = this.models.takeSuccessfulResponses();
|
|
15466
16255
|
if (responses.length > 0) await memory.recordSuccessfulModelResponses(responses);
|
|
15467
16256
|
}
|
|
15468
|
-
batch(session, evidence, results,
|
|
16257
|
+
batch(session, evidence, results, metadata2 = {}) {
|
|
15469
16258
|
const sequence = this.batchSequence + 1;
|
|
15470
16259
|
const id = `batch_${sequence}`;
|
|
15471
16260
|
const refs = new Map(evidence.map(({ ref, target }) => [ref, target]));
|
|
15472
|
-
const output = { batchId: id, evidenceRefs: [...refs.keys()], results, ...
|
|
16261
|
+
const output = { batchId: id, evidenceRefs: [...refs.keys()], results, ...metadata2 };
|
|
15473
16262
|
if (validateArgs2({ retrieval: { type: "json", required: true } }, { retrieval: output }).length) {
|
|
15474
16263
|
throw new TypeError("StrataGate retrieval output is not lossless JSON");
|
|
15475
16264
|
}
|
|
@@ -17108,6 +17897,20 @@ function matchesQuery(value, query) {
|
|
|
17108
17897
|
if (!query) return true;
|
|
17109
17898
|
return JSON.stringify(value).toLocaleLowerCase().includes(query.toLocaleLowerCase());
|
|
17110
17899
|
}
|
|
17900
|
+
function associationIndex(values, keys) {
|
|
17901
|
+
const index = /* @__PURE__ */ new Map();
|
|
17902
|
+
for (const value of values) for (const key of new Set(keys(value))) {
|
|
17903
|
+
const group = index.get(key) ?? [];
|
|
17904
|
+
group.push(value);
|
|
17905
|
+
index.set(key, group);
|
|
17906
|
+
}
|
|
17907
|
+
return index;
|
|
17908
|
+
}
|
|
17909
|
+
function indexedAssociations(index, keys, order) {
|
|
17910
|
+
const values = /* @__PURE__ */ new Set();
|
|
17911
|
+
for (const key of keys) for (const value of index.get(key) ?? []) values.add(value);
|
|
17912
|
+
return [...values].sort((left, right) => order.get(left) - order.get(right));
|
|
17913
|
+
}
|
|
17111
17914
|
async function requiredSnapshot(runtime, namespace) {
|
|
17112
17915
|
const snapshot = await runtime.adminSnapshot(namespace);
|
|
17113
17916
|
if (!snapshot) throw new AdminHttpError(404, `Unknown StrataGate namespace: ${namespace}`);
|
|
@@ -17474,7 +18277,7 @@ async function overview(runtime, cachedEntries) {
|
|
|
17474
18277
|
namespace,
|
|
17475
18278
|
duplicateRepair: runtime.adminDuplicateRepairStatus?.(namespace) ?? null,
|
|
17476
18279
|
workspaceName: runtime.adminWorkspaceName(namespace) ?? "\u5F53\u524D\u5DE5\u4F5C\u533A",
|
|
17477
|
-
schemaVersion: snapshot.schemaVersion,
|
|
18280
|
+
schemaVersion: "schemaVersion" in snapshot ? snapshot.schemaVersion : snapshot.storedSchemaVersion,
|
|
17478
18281
|
currentTurn: snapshot.currentTurn,
|
|
17479
18282
|
blockTurnSize: snapshot.blockTurnSize,
|
|
17480
18283
|
blockDecayLambda: snapshot.blockDecayLambda,
|
|
@@ -17727,6 +18530,7 @@ function timestampKey(value) {
|
|
|
17727
18530
|
return Number.isFinite(parsed) ? String(parsed) : value;
|
|
17728
18531
|
}
|
|
17729
18532
|
function recoverSnapshotView(snapshot) {
|
|
18533
|
+
const failedSummaries = new Set(snapshot.summaryJobs.filter((job) => derivationJobState(job, job.status === "succeeded") === "terminal-failed").map((job) => job.blockId));
|
|
17730
18534
|
const receiptThreads = /* @__PURE__ */ new Map();
|
|
17731
18535
|
const receiptActivity = /* @__PURE__ */ new Map();
|
|
17732
18536
|
const receiptTurns = /* @__PURE__ */ new Map();
|
|
@@ -17765,7 +18569,7 @@ function recoverSnapshotView(snapshot) {
|
|
|
17765
18569
|
messages.push(item.message);
|
|
17766
18570
|
groups.set(item.threadId, messages);
|
|
17767
18571
|
}
|
|
17768
|
-
if (groups.size === 0 &&
|
|
18572
|
+
if (groups.size === 0 && failedSummaries.has(source.id)) {
|
|
17769
18573
|
groups.set(source.threadId?.trim() || LEGACY_THREAD_ID, []);
|
|
17770
18574
|
}
|
|
17771
18575
|
const entries = [...groups];
|
|
@@ -17821,16 +18625,18 @@ function virtualBlockLayers(block) {
|
|
|
17821
18625
|
]);
|
|
17822
18626
|
}
|
|
17823
18627
|
function conversationRows(snapshot, view = recoverSnapshotView(snapshot)) {
|
|
18628
|
+
const blocksByThread = associationIndex(view.blocks, (block) => [block.threadId]);
|
|
18629
|
+
const openByThread = associationIndex(view.openMessages, (message) => [message.threadId]);
|
|
17824
18630
|
const ids2 = /* @__PURE__ */ new Set([
|
|
17825
18631
|
...view.blocks.map((block) => block.threadId),
|
|
17826
18632
|
...view.openMessages.map(({ threadId }) => threadId),
|
|
17827
18633
|
...view.receiptThreads.values()
|
|
17828
18634
|
]);
|
|
17829
18635
|
return [...ids2].map((id) => {
|
|
17830
|
-
const blocks =
|
|
18636
|
+
const blocks = blocksByThread.get(id) ?? [];
|
|
17831
18637
|
const messages = [
|
|
17832
18638
|
...blocks.flatMap((block) => block.messages),
|
|
17833
|
-
...
|
|
18639
|
+
...(openByThread.get(id) ?? []).map(({ message }) => message)
|
|
17834
18640
|
];
|
|
17835
18641
|
const firstUser = messages.find(({ role, content }) => role === "user" && content.trim());
|
|
17836
18642
|
const title = firstUser?.content.replace(/\s+/g, " ").trim().slice(0, 28);
|
|
@@ -17850,11 +18656,17 @@ function conversationRows(snapshot, view = recoverSnapshotView(snapshot)) {
|
|
|
17850
18656
|
async function memories(runtime, url) {
|
|
17851
18657
|
const namespace = url.searchParams.get("namespace")?.trim() ?? "";
|
|
17852
18658
|
if (!namespace) throw new AdminHttpError(400, "namespace is required");
|
|
17853
|
-
const snapshot = await requiredSnapshot(runtime, namespace);
|
|
17854
18659
|
const kind = url.searchParams.get("kind") ?? "events";
|
|
17855
18660
|
const query = url.searchParams.get("q")?.trim() ?? "";
|
|
17856
18661
|
const offset = numeric(url.searchParams.get("offset"), 0, 0, Number.MAX_SAFE_INTEGER);
|
|
17857
18662
|
const limit = numeric(url.searchParams.get("limit"), 100, 1, 200);
|
|
18663
|
+
const blockPage = kind === "blocks" && !query && runtime.adminReadBlockPage ? await runtime.adminReadBlockPage(
|
|
18664
|
+
namespace,
|
|
18665
|
+
{ threadId: url.searchParams.get("threadId")?.trim() ?? "", offset, limit },
|
|
18666
|
+
(model) => conversationRows(model)[0]?.id ?? null
|
|
18667
|
+
) : null;
|
|
18668
|
+
const eventModel = kind === "events" && runtime.adminReadEventModel ? await runtime.adminReadEventModel(namespace) : null;
|
|
18669
|
+
const snapshot = blockPage?.model ?? eventModel ?? await requiredSnapshot(runtime, namespace);
|
|
17858
18670
|
let values;
|
|
17859
18671
|
if (kind === "events") {
|
|
17860
18672
|
const timeline = url.searchParams.get("timeline") === "true";
|
|
@@ -17868,7 +18680,10 @@ async function memories(runtime, url) {
|
|
|
17868
18680
|
const happened = event.temporal.happenedStart ?? event.temporal.happenedEnd;
|
|
17869
18681
|
return { value: happened ?? event.temporal.mentionedAt ?? event.createdAt, known: Boolean(happened) };
|
|
17870
18682
|
};
|
|
17871
|
-
|
|
18683
|
+
const nodesById = new Map(snapshot.graphNodes.map((node) => [node.id, node]));
|
|
18684
|
+
const nodeOrder = new Map(snapshot.graphNodes.map((node, index) => [node, index]));
|
|
18685
|
+
const elementsByEvent = associationIndex(snapshot.elements, (element) => element.sourceEventIds);
|
|
18686
|
+
const candidates = [...snapshot.events].sort((left, right) => occurrence(right).value.localeCompare(occurrence(left).value)).filter((event) => !timeline || event.status !== "forgotten" && event.status !== "archived").filter((event) => !eventType || event.temporal.eventType === eventType).filter((event) => !eventStatus || event.temporal.status === eventStatus).filter((event) => !participant || (event.temporal.participantNodeIds ?? []).includes(participant)).filter((event) => {
|
|
17872
18687
|
if (!timeFilter) return true;
|
|
17873
18688
|
const info = occurrence(event);
|
|
17874
18689
|
if (timeFilter === "unknown") return !info.known;
|
|
@@ -17876,11 +18691,13 @@ async function memories(runtime, url) {
|
|
|
17876
18691
|
if (timeFilter === "today") return Number.isFinite(time) && new Date(time).toDateString() === now.toDateString();
|
|
17877
18692
|
if (timeFilter === "week") return Number.isFinite(time) && time >= weekAgo;
|
|
17878
18693
|
return true;
|
|
17879
|
-
})
|
|
18694
|
+
});
|
|
18695
|
+
values = (query ? candidates : candidates.slice(offset, offset + limit)).map((event) => ({
|
|
17880
18696
|
...eventSummary(event, snapshot),
|
|
17881
|
-
relatedNodes:
|
|
17882
|
-
relatedElements:
|
|
18697
|
+
relatedNodes: [...new Set(event.temporal.participantNodeIds ?? [])].flatMap((id) => nodesById.get(id) ?? []).sort((left, right) => nodeOrder.get(left) - nodeOrder.get(right)).map(({ id, name: name2, type, aliases }) => ({ id, name: name2, type, aliases })),
|
|
18698
|
+
relatedElements: (elementsByEvent.get(event.id) ?? []).map(({ id, name: name2 }) => ({ id, name: name2 }))
|
|
17883
18699
|
}));
|
|
18700
|
+
if (!query) return { namespace, kind, total: candidates.length, offset, limit, items: values };
|
|
17884
18701
|
} else if (kind === "graph") {
|
|
17885
18702
|
const eventMap = new Map(snapshot.events.map((event) => [event.id, event]));
|
|
17886
18703
|
return {
|
|
@@ -17900,7 +18717,7 @@ async function memories(runtime, url) {
|
|
|
17900
18717
|
const recovered = recoverSnapshotView(snapshot);
|
|
17901
18718
|
const conversations = conversationRows(snapshot, recovered);
|
|
17902
18719
|
const requestedThreadId = url.searchParams.get("threadId")?.trim() ?? "";
|
|
17903
|
-
const activeThreadId = requestedThreadId || conversations[0]?.id || null;
|
|
18720
|
+
const activeThreadId = blockPage?.threadId ?? (requestedThreadId || conversations[0]?.id || null);
|
|
17904
18721
|
const scopedBlocks = activeThreadId ? recovered.blocks.filter((block) => block.threadId === activeThreadId) : [];
|
|
17905
18722
|
const timeline = buildBlockTimeline(scopedBlocks.map((block) => ({
|
|
17906
18723
|
id: block.id,
|
|
@@ -17908,17 +18725,26 @@ async function memories(runtime, url) {
|
|
|
17908
18725
|
sequence: block.source.sequence
|
|
17909
18726
|
})));
|
|
17910
18727
|
const sourceTimeline = buildBlockTimeline(snapshot.blocks);
|
|
17911
|
-
|
|
18728
|
+
const summaries = new Map(snapshot.summaryJobs.map((job) => [job.blockId, job]));
|
|
18729
|
+
const extractions = new Map(snapshot.extractionJobs.map((job) => [job.blockId, job]));
|
|
18730
|
+
const eventsByBlock = associationIndex(snapshot.events, (event) => event.sourceBlockId ? [event.sourceBlockId] : []);
|
|
18731
|
+
const projectionsByEvent = associationIndex(snapshot.graphProjectionJobs, (job) => job.sourceEventIds);
|
|
18732
|
+
const nodesByEvent = associationIndex(snapshot.graphNodes, (node) => node.sourceEventIds);
|
|
18733
|
+
const projectionOrder = new Map(snapshot.graphProjectionJobs.map((job, index) => [job, index]));
|
|
18734
|
+
const nodeOrder = new Map(snapshot.graphNodes.map((node, index) => [node, index]));
|
|
18735
|
+
const pageIds = blockPage ? new Set(blockPage.blockIds) : null;
|
|
18736
|
+
const pageBlocks = query ? scopedBlocks : pageIds ? scopedBlocks.filter((block) => pageIds.has(block.id)) : scopedBlocks.slice(offset, offset + limit);
|
|
18737
|
+
values = pageBlocks.map((block) => {
|
|
17912
18738
|
const source = block.source;
|
|
17913
18739
|
const layers = virtualBlockLayers(block);
|
|
17914
18740
|
const layerTokens = layers.map(({ level, tokens, percentOfL5 }) => ({ level, tokens, percentOfL5 }));
|
|
17915
|
-
const extraction =
|
|
17916
|
-
const summary =
|
|
18741
|
+
const extraction = extractions.get(source.id);
|
|
18742
|
+
const summary = summaries.get(source.id);
|
|
17917
18743
|
const blockMessageIds = new Set(block.messages.map(({ id }) => id));
|
|
17918
|
-
const relatedEvents =
|
|
18744
|
+
const relatedEvents = (eventsByBlock.get(source.id) ?? []).filter((event) => !block.virtual || event.sourceMessageIds.some((id) => blockMessageIds.has(id)));
|
|
17919
18745
|
const eventIds = new Set(relatedEvents.map(({ id }) => id));
|
|
17920
|
-
const projections =
|
|
17921
|
-
const relatedNodes =
|
|
18746
|
+
const projections = indexedAssociations(projectionsByEvent, eventIds, projectionOrder);
|
|
18747
|
+
const relatedNodes = indexedAssociations(nodesByEvent, eventIds, nodeOrder).map(({ id, name: name2, type }) => ({ id, name: name2, type }));
|
|
17922
18748
|
const failedProjection = projections.find((job) => derivationJobState(job, job.status === "completed") === "terminal-failed");
|
|
17923
18749
|
const pendingProjection = projections.some(graphProjectionIsProcessing);
|
|
17924
18750
|
const needsExtraction = source.shouldExtract === true;
|
|
@@ -17959,7 +18785,7 @@ async function memories(runtime, url) {
|
|
|
17959
18785
|
sourceMessages: block.messages.length,
|
|
17960
18786
|
createdAt: source.createdAt,
|
|
17961
18787
|
virtual: block.virtual,
|
|
17962
|
-
rawAvailable:
|
|
18788
|
+
rawAvailable: block.messages.some(({ content }) => Boolean(content.trim())),
|
|
17963
18789
|
processingStatus: source.processingStatus,
|
|
17964
18790
|
summaryJob: summary ? {
|
|
17965
18791
|
state: derivationJobState(summary, summary.status === "succeeded"),
|
|
@@ -17992,10 +18818,10 @@ async function memories(runtime, url) {
|
|
|
17992
18818
|
return {
|
|
17993
18819
|
namespace,
|
|
17994
18820
|
kind,
|
|
17995
|
-
total: filtered2.length,
|
|
18821
|
+
total: query ? filtered2.length : blockPage?.total ?? scopedBlocks.length,
|
|
17996
18822
|
offset,
|
|
17997
18823
|
limit,
|
|
17998
|
-
items: filtered2.slice(offset, offset + limit),
|
|
18824
|
+
items: query ? filtered2.slice(offset, offset + limit) : filtered2,
|
|
17999
18825
|
openBlock: {
|
|
18000
18826
|
turnRange: openTurns > 0 ? openDshTurns.length > 0 && openDshTurns.every((turn) => turn !== void 0) ? [Math.min(...openDshTurns), Math.max(...openDshTurns)] : [latestSealedTurn + 1, latestSealedTurn + openTurns] : null,
|
|
18001
18827
|
messages: openMessages.length,
|
|
@@ -18205,14 +19031,23 @@ function requestHeader(req, name2) {
|
|
|
18205
19031
|
return Array.isArray(value) ? value.join(", ") : value ?? "";
|
|
18206
19032
|
}
|
|
18207
19033
|
async function dashboard(runtime, url, ifNoneMatch) {
|
|
18208
|
-
const entries = await runtime.adminSnapshotEntries();
|
|
18209
19034
|
const requestedNamespace = url.searchParams.get("namespace")?.trim() ?? "";
|
|
18210
|
-
const selected = entries.find(({ namespace }) => namespace === requestedNamespace) ?? entries[0];
|
|
18211
19035
|
const threadId = url.searchParams.get("threadId")?.trim() ?? "";
|
|
18212
|
-
const
|
|
19036
|
+
const queryKey = JSON.stringify(["offset", "q", "time", "eventType", "eventStatus", "participant", "timeline"].map((key) => url.searchParams.get(key) ?? ""));
|
|
18213
19037
|
const agentMemoryWeight = runtime.adminAgentMemoryRetrievalWeight?.() ?? 1;
|
|
18214
|
-
const
|
|
18215
|
-
|
|
19038
|
+
const etagFor = (entries2) => {
|
|
19039
|
+
const selectedNamespace = entries2.find((entry) => entry.namespace === requestedNamespace)?.namespace ?? entries2[0]?.namespace ?? "";
|
|
19040
|
+
const revisionKey = entries2.map(({ namespace, revision }) => `${namespace}:${revision}`).join("|");
|
|
19041
|
+
const duplicateKey = JSON.stringify([entries2.map(({ namespace }) => runtime.adminDuplicateRepairStatus?.(namespace) ?? null), runtime.adminGraphSettings?.()]);
|
|
19042
|
+
return `"${createHash7("sha256").update(`${STRATAGATE_DSH_VERSION}\0${TOPIC_DIRECTORY_VIEW_VERSION}\0${revisionKey}\0${selectedNamespace}\0${threadId}\0${agentMemoryWeight}\0${duplicateKey}\0${queryKey}`).digest("base64url").slice(0, 24)}"`;
|
|
19043
|
+
};
|
|
19044
|
+
if (ifNoneMatch && typeof runtime.adminReadNamespaceRevisions === "function") {
|
|
19045
|
+
const etag2 = etagFor(await runtime.adminReadNamespaceRevisions());
|
|
19046
|
+
if (ifNoneMatch.split(",").map((value) => value.trim()).includes(etag2)) return { etag: etag2, notModified: true };
|
|
19047
|
+
}
|
|
19048
|
+
const entries = typeof runtime.adminReadDashboardEntries === "function" && !url.searchParams.get("q")?.trim() ? await runtime.adminReadDashboardEntries({ namespace: requestedNamespace, threadId, offset: numeric(url.searchParams.get("offset"), 0, 0, Number.MAX_SAFE_INTEGER), limit: 40 }, (model) => conversationRows(model)[0]?.id ?? null) : await runtime.adminSnapshotEntries();
|
|
19049
|
+
const selected = entries.find(({ namespace }) => namespace === requestedNamespace) ?? entries[0];
|
|
19050
|
+
const etag = etagFor(entries);
|
|
18216
19051
|
if (ifNoneMatch.split(",").map((value) => value.trim()).includes(etag)) return { etag, notModified: true };
|
|
18217
19052
|
const overviewValue = await overview(runtime, entries);
|
|
18218
19053
|
if (!selected) {
|
|
@@ -18892,13 +19727,13 @@ Memory writing:
|
|
|
18892
19727
|
- Stable default location (defaultLocation: reference for weather, nearby services, or local recommendations when no location is specified) and usual city of residence (homeCity) belong preferentially in the always-on Profile. They are independent: do not infer either from the other or from a current/temporary location or a trip. Temporary travel must not automatically overwrite these stable fields. Follow the same Profile consent rule.
|
|
18893
19728
|
- Current city (currentCity) also belongs in the always-on Profile. Travel or business-trip location can be temporary while its saved value persists across sessions until the user updates or clears it; do not expire it automatically. A user request to remember their current trip city belongs here, not in retrieval-dependent memory_remember. Future trip plans, completed trips, and historical locations do not establish current whereabouts. Never copy currentCity into defaultLocation or homeCity. For local tasks use an explicit task location first, otherwise currentCity when set, then defaultLocation. Follow the same Profile consent rule for updates and clearing.
|
|
18894
19729
|
- Use memory_remember for durable project facts, past decisions, corrections, experiences, and context-specific preferences. Record one self-contained, grounded fact per call, with necessary project, time, and scope. Never record speculation, secrets, credentials, or transient task state.`;
|
|
18895
|
-
function memoryProtocol(dailyEnabled) {
|
|
19730
|
+
function memoryProtocol(dailyEnabled, injectionDays = 3) {
|
|
18896
19731
|
if (!dailyEnabled) return MEMORY_PROTOCOL;
|
|
18897
19732
|
return MEMORY_PROTOCOL.replace('- Choose by scope rather than the word "remember": memory_profile_update is for always-on global Profile fields supplied to future conversations without retrieval; memory_remember is for durable information that should surface when relevant; information that matters only to the current turn needs neither. Store the same information in one place by default.', '- Choose by scope rather than the word "remember": memory_profile_update is for always-on global Profile fields supplied to future conversations without retrieval; memory_remember is for durable facts that should surface when relevant; memory_daily_write is for the state of ongoing work (progress, stage results, useful failures, blockers, and next steps) so a later session can continue it. Information that matters only to the current turn needs none of them. Store the same information in one place by default.').replace("Never record speculation, secrets, credentials, or transient task state.", "Never record speculation, secrets, credentials, or the progress of ongoing work; ongoing work belongs in daily notes.") + `
|
|
18898
19733
|
|
|
18899
19734
|
Daily notes:
|
|
18900
19735
|
|
|
18901
|
-
- memory_daily_write keeps a dated working log so later sessions can resume work. Notes from the
|
|
19736
|
+
- memory_daily_write keeps a dated working log so later sessions can resume work. Notes from the ${injectionDays} most recent dates are injected at session start; use memory_daily_read for older dates, keyword search, or an item's current text.
|
|
18902
19737
|
- Write when this turn produced an unrecorded change that affects how work continues: progress, a stage result, a decision, a useful failure, a correction, a blocker, or handoff information. Save at a checkpoint (a stage ends, or before replying, pausing, or handing off) and merge the turn's changes into one write. A new date, rereading old notes, or repeating work without a new conclusion is not a reason to write. Having said something in this conversation does not mean it is saved.
|
|
18903
19738
|
- Lead with the current state, then add only the reasons, verification results, remaining work, and locations that matter. Keep planned, done, and verified distinct; never record a candidate idea as a confirmed decision. Replace stale state, keep failure causes worth knowing, and do not retell the conversation.
|
|
18904
19739
|
- On the same day, edit the existing item instead of adding a duplicate, and mark it done when finished. On a later day, add a new item that names the earlier one instead of copying it.
|
|
@@ -18970,7 +19805,7 @@ async function apply(ctx, config) {
|
|
|
18970
19805
|
settingsCtx.effect(() => settings.configure({ auto: false }, ctx.fiber));
|
|
18971
19806
|
}
|
|
18972
19807
|
});
|
|
18973
|
-
ctx.systemPrompt.section({ name: "tool:stratagate-memory", order: 113, text: () => runtime.graphEnabled ? memoryProtocol(runtime.dailyNotesEnabled) : memoryProtocol(runtime.dailyNotesEnabled).split("\n").map((line) => line.includes("Use memory_search_events for what happened") ? "- Use memory_search_events to retrieve durable facts and changes. Knowledge Graph is disabled." : line.replaceAll("Event/Graph", "Event").replaceAll("Event or Graph", "Event").replaceAll(", Graph node,", ",")).join("\n") });
|
|
19808
|
+
ctx.systemPrompt.section({ name: "tool:stratagate-memory", order: 113, text: () => runtime.graphEnabled ? memoryProtocol(runtime.dailyNotesEnabled, runtime.dailyNotes.injectionDays) : memoryProtocol(runtime.dailyNotesEnabled, runtime.dailyNotes.injectionDays).split("\n").map((line) => line.includes("Use memory_search_events for what happened") ? "- Use memory_search_events to retrieve durable facts and changes. Knowledge Graph is disabled." : line.replaceAll("Event/Graph", "Event").replaceAll("Event or Graph", "Event").replaceAll(", Graph node,", ",")).join("\n") });
|
|
18974
19809
|
if (AGENT_FEEDBACK_ENABLED) ctx.systemPrompt.section({ name: "tool:stratagate-feedback", order: 114, text: FEEDBACK_PROTOCOL });
|
|
18975
19810
|
ctx.on("system-prompt/assemble", async (_assembly, context, next) => {
|
|
18976
19811
|
const assembled = await next();
|