stratagate-dsh 0.3.51 → 0.3.52
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/dist/client.js +2 -2
- package/dist/plugin.js +154 -13
- package/dist/plugin.js.map +1 -1
- package/docs/DSH.md +7 -0
- package/docs/DSH.zh-CN.md +2 -0
- package/package.json +1 -1
package/dist/plugin.js
CHANGED
|
@@ -970,6 +970,7 @@ function normalizeEventMetadata(value) {
|
|
|
970
970
|
if (Number.isSafeInteger(item.extractorVersion) && item.extractorVersion > 0) {
|
|
971
971
|
metadata2.extractorVersion = item.extractorVersion;
|
|
972
972
|
}
|
|
973
|
+
if (item.quotesOrigin === "auto_extracted") metadata2.quotesOrigin = "auto_extracted";
|
|
973
974
|
return metadata2;
|
|
974
975
|
}
|
|
975
976
|
function normalizeEventTemporal(value) {
|
|
@@ -1173,6 +1174,44 @@ async function scanEventDuplicates(events) {
|
|
|
1173
1174
|
return pairs;
|
|
1174
1175
|
}
|
|
1175
1176
|
|
|
1177
|
+
// packages/core/src/event-quotes.ts
|
|
1178
|
+
var AUTO_EXTRACTED_QUOTES_NOTICE = "\u5F15\u7528\u9010\u5B57\u6458\u81EA\u6765\u6E90\u6D88\u606F\uFF0C\u7531\u76F8\u4F3C\u5EA6\u7B97\u6CD5\u9009\u62E9\uFF0C\u672A\u72EC\u7ACB\u9A8C\u8BC1\u5176\u662F\u5426\u5145\u5206\u652F\u6301\u4E8B\u4EF6\u6458\u8981\u3002";
|
|
1179
|
+
function fragments(content) {
|
|
1180
|
+
const trimmed = content.trim();
|
|
1181
|
+
if (!trimmed) return [];
|
|
1182
|
+
const parts = trimmed.split(/(?<=[。!?!?;;\n])/u).flatMap((part) => part.length > 260 ? part.split(/(?<=[,,、])/u) : [part]).map((part) => part.trim()).filter((part) => [...part].length >= 4);
|
|
1183
|
+
if ([...trimmed].length <= 260 && !parts.includes(trimmed)) parts.push(trimmed);
|
|
1184
|
+
return [...new Set(parts)];
|
|
1185
|
+
}
|
|
1186
|
+
function findBestVerbatimQuote(query, messages) {
|
|
1187
|
+
const queryTokens = [...new Set(searchTokens(query))];
|
|
1188
|
+
if (queryTokens.length === 0) return null;
|
|
1189
|
+
const querySet = new Set(queryTokens);
|
|
1190
|
+
const candidates = [];
|
|
1191
|
+
for (const [messageIndex, message] of messages.entries()) {
|
|
1192
|
+
for (const [candidateIndex, text3] of fragments(message.content).entries()) {
|
|
1193
|
+
const normalized = normalizeSearchText(text3);
|
|
1194
|
+
if (!normalized || !normalizeSearchText(message.content).includes(normalized)) continue;
|
|
1195
|
+
let overlap = 0;
|
|
1196
|
+
for (const token of new Set(searchTokens(text3))) if (querySet.has(token)) overlap += 1;
|
|
1197
|
+
const minimumOverlap = queryTokens.length <= 2 ? 1 : Math.max(2, Math.ceil(queryTokens.length * 0.2));
|
|
1198
|
+
if (overlap < minimumOverlap) continue;
|
|
1199
|
+
const coverage = overlap / queryTokens.length;
|
|
1200
|
+
const roleBonus = message.role === "user" ? 0.75 : 0;
|
|
1201
|
+
const lengthPenalty = Math.min(1, Math.max(0, ([...text3].length - 120) / 240));
|
|
1202
|
+
candidates.push({
|
|
1203
|
+
text: text3,
|
|
1204
|
+
messageIndex,
|
|
1205
|
+
candidateIndex,
|
|
1206
|
+
role: message.role,
|
|
1207
|
+
score: overlap * 2 + coverage * 8 + roleBonus - lengthPenalty
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
candidates.sort((left, right) => right.score - left.score || (left.role === "user" ? 0 : 1) - (right.role === "user" ? 0 : 1) || left.messageIndex - right.messageIndex || left.candidateIndex - right.candidateIndex || left.text.localeCompare(right.text));
|
|
1212
|
+
return candidates[0]?.text ?? null;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1176
1215
|
// packages/core/src/external-memory.ts
|
|
1177
1216
|
var EXTERNAL_MEMORY_EXPORT_SCHEMA = "stratagate.external-memory.v2";
|
|
1178
1217
|
var EXTERNAL_MEMORY_DECIDER_PROMPT_ZH_CN = `
|
|
@@ -2198,6 +2237,7 @@ function normalizeSnapshot(value) {
|
|
|
2198
2237
|
const metadata2 = normalizeEventMetadata(event);
|
|
2199
2238
|
delete event.catalogHints;
|
|
2200
2239
|
delete event.extractorVersion;
|
|
2240
|
+
delete event.quotesOrigin;
|
|
2201
2241
|
Object.assign(event, metadata2);
|
|
2202
2242
|
delete event.narrative;
|
|
2203
2243
|
delete event.confidence;
|
|
@@ -4013,7 +4053,7 @@ CREATE TABLE IF NOT EXISTS messages (
|
|
|
4013
4053
|
|
|
4014
4054
|
CREATE INDEX IF NOT EXISTS messages_container_idx ON messages(namespace, block_id, position);
|
|
4015
4055
|
|
|
4016
|
-
-- Optional metadata shared by both Event pools.
|
|
4056
|
+
-- Optional metadata shared by both Event pools.
|
|
4017
4057
|
CREATE TABLE IF NOT EXISTS event_merge_sources (
|
|
4018
4058
|
namespace TEXT NOT NULL,
|
|
4019
4059
|
event_id TEXT NOT NULL,
|
|
@@ -4026,10 +4066,17 @@ CREATE TABLE IF NOT EXISTS event_metadata (
|
|
|
4026
4066
|
event_id TEXT NOT NULL,
|
|
4027
4067
|
catalog_hints_json TEXT,
|
|
4028
4068
|
extractor_version INTEGER,
|
|
4069
|
+
quotes_origin TEXT,
|
|
4029
4070
|
PRIMARY KEY (namespace, event_id),
|
|
4030
4071
|
FOREIGN KEY (namespace) REFERENCES memory_spaces(namespace) ON DELETE CASCADE
|
|
4031
4072
|
) STRICT;
|
|
4032
4073
|
|
|
4074
|
+
CREATE TABLE IF NOT EXISTS event_quote_backfill (
|
|
4075
|
+
namespace TEXT PRIMARY KEY,
|
|
4076
|
+
completed_at TEXT NOT NULL,
|
|
4077
|
+
FOREIGN KEY (namespace) REFERENCES memory_spaces(namespace) ON DELETE CASCADE
|
|
4078
|
+
) STRICT;
|
|
4079
|
+
|
|
4033
4080
|
CREATE TABLE IF NOT EXISTS events (
|
|
4034
4081
|
namespace TEXT NOT NULL,
|
|
4035
4082
|
id TEXT NOT NULL,
|
|
@@ -4039,6 +4086,7 @@ CREATE TABLE IF NOT EXISTS events (
|
|
|
4039
4086
|
narrative TEXT NOT NULL,
|
|
4040
4087
|
tags_json TEXT NOT NULL,
|
|
4041
4088
|
quotes_json TEXT NOT NULL,
|
|
4089
|
+
quotes_origin TEXT,
|
|
4042
4090
|
source_block_id TEXT NOT NULL,
|
|
4043
4091
|
formed_turn INTEGER,
|
|
4044
4092
|
temporal_json TEXT NOT NULL,
|
|
@@ -4082,6 +4130,7 @@ CREATE TABLE IF NOT EXISTS agent_events (
|
|
|
4082
4130
|
narrative TEXT NOT NULL,
|
|
4083
4131
|
tags_json TEXT NOT NULL,
|
|
4084
4132
|
quotes_json TEXT NOT NULL,
|
|
4133
|
+
quotes_origin TEXT,
|
|
4085
4134
|
source_block_id TEXT,
|
|
4086
4135
|
formed_turn INTEGER,
|
|
4087
4136
|
temporal_json TEXT NOT NULL,
|
|
@@ -4332,6 +4381,7 @@ function mapEventRows(rows, sourcesByEvent, table) {
|
|
|
4332
4381
|
summary: row.summary,
|
|
4333
4382
|
tags: parseJson(row.tags_json, `${table}.tags_json`),
|
|
4334
4383
|
quotes: parseJson(row.quotes_json, `${table}.quotes_json`),
|
|
4384
|
+
...row.quotes_origin === "auto_extracted" ? { quotesOrigin: "auto_extracted" } : {},
|
|
4335
4385
|
sourceMessageIds: sourcesByEvent.get(row.id) ?? [],
|
|
4336
4386
|
...row.source_block_id === null ? {} : { sourceBlockId: row.source_block_id },
|
|
4337
4387
|
...row.formed_turn === null ? {} : { formedTurn: row.formed_turn },
|
|
@@ -4404,6 +4454,16 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4404
4454
|
async load(namespace) {
|
|
4405
4455
|
return this.readState(namespace);
|
|
4406
4456
|
}
|
|
4457
|
+
eventQuoteBackfillPending(namespace) {
|
|
4458
|
+
if (!this.database.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'event_quote_backfill'").get()) return false;
|
|
4459
|
+
return !this.database.prepare("SELECT 1 FROM event_quote_backfill WHERE namespace = ?").get(namespace);
|
|
4460
|
+
}
|
|
4461
|
+
markEventQuoteBackfillComplete(namespace) {
|
|
4462
|
+
if (this.readonly) return;
|
|
4463
|
+
this.immediateTransaction(() => {
|
|
4464
|
+
this.database.prepare("INSERT INTO event_quote_backfill (namespace, completed_at) VALUES (?, ?) ON CONFLICT(namespace) DO NOTHING").run(namespace, nowUtc8());
|
|
4465
|
+
});
|
|
4466
|
+
}
|
|
4407
4467
|
readEventModel(namespace) {
|
|
4408
4468
|
this.assertOpen();
|
|
4409
4469
|
const key = nonEmptyNamespace(namespace);
|
|
@@ -4686,17 +4746,32 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4686
4746
|
const agentEvents = mapEventRows(agentEventRows, agentSourcesByEvent, "agent_events");
|
|
4687
4747
|
this.loadMergedSources(key, [...events, ...agentEvents]);
|
|
4688
4748
|
const hasEventMetadata = this.database.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'event_metadata'").get();
|
|
4749
|
+
const metadataHasQuotesOrigin = hasEventMetadata && this.hasColumn("event_metadata", "quotes_origin");
|
|
4689
4750
|
if (hasEventMetadata) {
|
|
4690
|
-
const metadataRows = this.database.prepare(
|
|
4751
|
+
const metadataRows = this.database.prepare(`SELECT event_id, catalog_hints_json, extractor_version${metadataHasQuotesOrigin ? ", quotes_origin" : ""} FROM event_metadata WHERE namespace = ?`).all(key);
|
|
4691
4752
|
const byId = new Map([...events, ...agentEvents].map((event) => [event.id, event]));
|
|
4692
4753
|
for (const row of metadataRows) {
|
|
4693
4754
|
const event = byId.get(row.event_id);
|
|
4694
4755
|
if (event) Object.assign(event, normalizeEventMetadata({
|
|
4695
4756
|
...row.catalog_hints_json === null ? {} : { catalogHints: parseJson(row.catalog_hints_json, "event_metadata.catalog_hints_json") },
|
|
4696
|
-
extractorVersion: row.extractor_version
|
|
4757
|
+
extractorVersion: row.extractor_version,
|
|
4758
|
+
...row.quotes_origin === null || row.quotes_origin === void 0 ? {} : { quotesOrigin: row.quotes_origin }
|
|
4697
4759
|
}));
|
|
4698
4760
|
}
|
|
4699
4761
|
}
|
|
4762
|
+
if (!metadataHasQuotesOrigin) {
|
|
4763
|
+
const messages = new Map([
|
|
4764
|
+
...openTail.map((message) => [message.id, message]),
|
|
4765
|
+
...blocks.flatMap((block) => block.l5Raw.map((message) => [message.id, message]))
|
|
4766
|
+
]);
|
|
4767
|
+
for (const event of [...events, ...agentEvents]) {
|
|
4768
|
+
if (event.quotesOrigin === "auto_extracted" || event.quotes.length === 0) continue;
|
|
4769
|
+
const sourceTexts = event.sourceMessageIds.flatMap((id) => messages.get(id)?.content ?? []);
|
|
4770
|
+
if (event.quotes.some((quote) => sourceTexts.some((content) => content.includes(quote)))) {
|
|
4771
|
+
event.quotesOrigin = "auto_extracted";
|
|
4772
|
+
}
|
|
4773
|
+
}
|
|
4774
|
+
}
|
|
4700
4775
|
const elementSourceRows = this.database.prepare(`
|
|
4701
4776
|
SELECT element_id, event_id, position FROM element_sources
|
|
4702
4777
|
WHERE namespace = ? ORDER BY element_id, position
|
|
@@ -5287,11 +5362,11 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5287
5362
|
});
|
|
5288
5363
|
const insertEvent = this.database.prepare(`
|
|
5289
5364
|
INSERT INTO events (
|
|
5290
|
-
namespace, id, position, title, summary, narrative, tags_json, quotes_json, source_block_id,
|
|
5365
|
+
namespace, id, position, title, summary, narrative, tags_json, quotes_json, quotes_origin, source_block_id,
|
|
5291
5366
|
formed_turn, temporal_json, scope, criticality, confidence, status, superseded_by,
|
|
5292
5367
|
mention_count, last_adopted_turn, last_retrieved_at, pinned, floor_weight, forced_cap,
|
|
5293
5368
|
created_at, updated_at
|
|
5294
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
5369
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
5295
5370
|
ON CONFLICT (namespace, id) DO UPDATE SET
|
|
5296
5371
|
position = excluded.position,
|
|
5297
5372
|
title = excluded.title,
|
|
@@ -5299,6 +5374,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5299
5374
|
narrative = excluded.narrative,
|
|
5300
5375
|
tags_json = excluded.tags_json,
|
|
5301
5376
|
quotes_json = excluded.quotes_json,
|
|
5377
|
+
quotes_origin = excluded.quotes_origin,
|
|
5302
5378
|
source_block_id = excluded.source_block_id,
|
|
5303
5379
|
formed_turn = excluded.formed_turn,
|
|
5304
5380
|
temporal_json = excluded.temporal_json,
|
|
@@ -5333,6 +5409,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5333
5409
|
event.summary,
|
|
5334
5410
|
JSON.stringify(event.tags),
|
|
5335
5411
|
JSON.stringify(event.quotes),
|
|
5412
|
+
event.quotesOrigin ?? null,
|
|
5336
5413
|
event.sourceBlockId,
|
|
5337
5414
|
event.formedTurn ?? null,
|
|
5338
5415
|
JSON.stringify(event.temporal),
|
|
@@ -5363,11 +5440,11 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5363
5440
|
}
|
|
5364
5441
|
const insertAgentEvent = this.database.prepare(`
|
|
5365
5442
|
INSERT INTO agent_events (
|
|
5366
|
-
namespace, id, position, title, summary, narrative, tags_json, quotes_json, source_block_id,
|
|
5443
|
+
namespace, id, position, title, summary, narrative, tags_json, quotes_json, quotes_origin, source_block_id,
|
|
5367
5444
|
formed_turn, temporal_json, scope, criticality, confidence, status, superseded_by,
|
|
5368
5445
|
mention_count, last_adopted_turn, last_retrieved_at, pinned, floor_weight, forced_cap,
|
|
5369
5446
|
created_at, updated_at
|
|
5370
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
5447
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
5371
5448
|
`);
|
|
5372
5449
|
const insertAgentEventSource = this.database.prepare(`
|
|
5373
5450
|
INSERT INTO agent_event_sources (namespace, event_id, message_id, position) VALUES (?, ?, ?, ?)
|
|
@@ -5383,6 +5460,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5383
5460
|
event.summary,
|
|
5384
5461
|
JSON.stringify(event.tags),
|
|
5385
5462
|
JSON.stringify(event.quotes),
|
|
5463
|
+
event.quotesOrigin ?? null,
|
|
5386
5464
|
event.sourceBlockId ?? null,
|
|
5387
5465
|
event.formedTurn ?? null,
|
|
5388
5466
|
JSON.stringify(event.temporal),
|
|
@@ -5424,7 +5502,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5424
5502
|
}
|
|
5425
5503
|
clearEventAuxiliary("event_metadata");
|
|
5426
5504
|
const insertEventMetadata = this.database.prepare(`
|
|
5427
|
-
INSERT INTO event_metadata (namespace, event_id, catalog_hints_json, extractor_version) VALUES (?, ?, ?, ?)
|
|
5505
|
+
INSERT INTO event_metadata (namespace, event_id, catalog_hints_json, extractor_version, quotes_origin) VALUES (?, ?, ?, ?, ?)
|
|
5428
5506
|
`);
|
|
5429
5507
|
for (const event of [...snapshot.events, ...snapshot.agentEvents]) {
|
|
5430
5508
|
if (!changedEvent(event.id)) continue;
|
|
@@ -5434,7 +5512,8 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5434
5512
|
namespace,
|
|
5435
5513
|
event.id,
|
|
5436
5514
|
metadata2.catalogHints === void 0 ? null : JSON.stringify(metadata2.catalogHints),
|
|
5437
|
-
metadata2.extractorVersion ?? null
|
|
5515
|
+
metadata2.extractorVersion ?? null,
|
|
5516
|
+
metadata2.quotesOrigin ?? null
|
|
5438
5517
|
);
|
|
5439
5518
|
}
|
|
5440
5519
|
const insertElement = this.database.prepare(`
|
|
@@ -5984,12 +6063,14 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5984
6063
|
const batch = mapEventRows(rows, sources2, table);
|
|
5985
6064
|
this.loadMergedSources(namespace, batch);
|
|
5986
6065
|
const byId = new Map(batch.map((event) => [event.id, event]));
|
|
5987
|
-
|
|
6066
|
+
const metadataHasQuotesOrigin = this.hasColumn("event_metadata", "quotes_origin");
|
|
6067
|
+
for (const row of this.database.prepare(`SELECT event_id, catalog_hints_json, extractor_version${metadataHasQuotesOrigin ? ", quotes_origin" : ""} FROM event_metadata
|
|
5988
6068
|
WHERE namespace = ? AND event_id IN (${placeholders})`).all(namespace, ...ids2)) {
|
|
5989
6069
|
const event = byId.get(row.event_id);
|
|
5990
6070
|
if (event) Object.assign(event, normalizeEventMetadata({
|
|
5991
6071
|
...row.catalog_hints_json === null ? {} : { catalogHints: parseJson(row.catalog_hints_json, "event_metadata.catalog_hints_json") },
|
|
5992
|
-
extractorVersion: row.extractor_version
|
|
6072
|
+
extractorVersion: row.extractor_version,
|
|
6073
|
+
...row.quotes_origin === null || row.quotes_origin === void 0 ? {} : { quotesOrigin: row.quotes_origin }
|
|
5993
6074
|
}));
|
|
5994
6075
|
}
|
|
5995
6076
|
events.push(...batch);
|
|
@@ -6051,6 +6132,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
6051
6132
|
} else if (version >= 1 && version < STRATAGATE_STORAGE_SCHEMA_VERSION) {
|
|
6052
6133
|
this.immediateTransaction(() => {
|
|
6053
6134
|
this.database.exec(SCHEMA);
|
|
6135
|
+
if (!this.hasColumn("event_metadata", "quotes_origin")) {
|
|
6136
|
+
this.database.exec("ALTER TABLE event_metadata ADD COLUMN quotes_origin TEXT");
|
|
6137
|
+
}
|
|
6054
6138
|
if (version === 1) {
|
|
6055
6139
|
const receiptColumns2 = this.database.prepare("PRAGMA table_info('usage_receipts')").all();
|
|
6056
6140
|
if (!receiptColumns2.some(({ name: name2 }) => name2 === "element_ids_json")) {
|
|
@@ -6100,6 +6184,12 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
6100
6184
|
if (!eventColumns.some(({ name: name2 }) => name2 === "formed_turn")) {
|
|
6101
6185
|
this.database.exec("ALTER TABLE events ADD COLUMN formed_turn INTEGER");
|
|
6102
6186
|
}
|
|
6187
|
+
if (!eventColumns.some(({ name: name2 }) => name2 === "quotes_origin")) {
|
|
6188
|
+
this.database.exec("ALTER TABLE events ADD COLUMN quotes_origin TEXT");
|
|
6189
|
+
}
|
|
6190
|
+
if (!this.hasColumn("agent_events", "quotes_origin")) {
|
|
6191
|
+
this.database.exec("ALTER TABLE agent_events ADD COLUMN quotes_origin TEXT");
|
|
6192
|
+
}
|
|
6103
6193
|
this.database.exec(`
|
|
6104
6194
|
UPDATE events
|
|
6105
6195
|
SET formed_turn = (
|
|
@@ -6157,6 +6247,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
6157
6247
|
} else if (version === STRATAGATE_STORAGE_SCHEMA_VERSION) {
|
|
6158
6248
|
this.immediateTransaction(() => {
|
|
6159
6249
|
this.database.exec(SCHEMA);
|
|
6250
|
+
if (!this.hasColumn("event_metadata", "quotes_origin")) {
|
|
6251
|
+
this.database.exec("ALTER TABLE event_metadata ADD COLUMN quotes_origin TEXT");
|
|
6252
|
+
}
|
|
6160
6253
|
if (!this.hasColumn("memory_spaces", "graph_enabled")) {
|
|
6161
6254
|
this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_enabled INTEGER NOT NULL DEFAULT 0 CHECK (graph_enabled IN (0, 1))");
|
|
6162
6255
|
}
|
|
@@ -6166,6 +6259,8 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
6166
6259
|
this.database.exec("ALTER TABLE blocks ADD COLUMN surface_current_level INTEGER NOT NULL DEFAULT 5");
|
|
6167
6260
|
this.database.exec("UPDATE blocks SET surface_current_level = pointer_current_level");
|
|
6168
6261
|
}
|
|
6262
|
+
if (!this.hasColumn("events", "quotes_origin")) this.database.exec("ALTER TABLE events ADD COLUMN quotes_origin TEXT");
|
|
6263
|
+
if (!this.hasColumn("agent_events", "quotes_origin")) this.database.exec("ALTER TABLE agent_events ADD COLUMN quotes_origin TEXT");
|
|
6169
6264
|
this.database.prepare("INSERT INTO stratagate_graph_policy_migrations (id, completed_at) VALUES (1, ?) ON CONFLICT (id) DO NOTHING").run(nowUtc8());
|
|
6170
6265
|
this.database.exec(THREAD_INDEXES);
|
|
6171
6266
|
this.rebuildLegacyElementSourceForeignKeys();
|
|
@@ -6837,9 +6932,18 @@ var StrataGate = class _StrataGate {
|
|
|
6837
6932
|
memory.revision = loadedRevision;
|
|
6838
6933
|
}
|
|
6839
6934
|
if (options.storage.readonly) return memory;
|
|
6840
|
-
await memory.initializeTopicBootstrap();
|
|
6841
6935
|
if (loaded && loadedSnapshot) {
|
|
6842
6936
|
await memory.recoverExpiredDerivations();
|
|
6937
|
+
if (options.storage.eventQuoteBackfillPending?.(namespace)) {
|
|
6938
|
+
await memory.backfillMissingEventQuotes();
|
|
6939
|
+
options.storage.markEventQuoteBackfillComplete?.(namespace);
|
|
6940
|
+
}
|
|
6941
|
+
}
|
|
6942
|
+
await memory.initializeTopicBootstrap();
|
|
6943
|
+
if (!loaded && options.storage.eventQuoteBackfillPending?.(namespace)) {
|
|
6944
|
+
options.storage.markEventQuoteBackfillComplete?.(namespace);
|
|
6945
|
+
}
|
|
6946
|
+
if (loaded && loadedSnapshot) {
|
|
6843
6947
|
const interruptedProjections = [...memory.elementProjectionJobs.values()].filter((job) => job.status === "running");
|
|
6844
6948
|
if (interruptedProjections.length > 0) {
|
|
6845
6949
|
await memory.commitMutation(() => {
|
|
@@ -7478,6 +7582,32 @@ var StrataGate = class _StrataGate {
|
|
|
7478
7582
|
return event;
|
|
7479
7583
|
});
|
|
7480
7584
|
}
|
|
7585
|
+
/** Fill only empty quote arrays, using the normal mutation/persistence path. */
|
|
7586
|
+
async backfillMissingEventQuotes() {
|
|
7587
|
+
const candidates = this.listAllEvents().filter((event) => event.quotes.length === 0 && event.sourceMessageIds.length > 0);
|
|
7588
|
+
if (candidates.length === 0) return { updated: 0, inspected: 0 };
|
|
7589
|
+
const prepared = candidates.flatMap((event) => {
|
|
7590
|
+
const messages = event.sourceMessageIds.flatMap((id) => {
|
|
7591
|
+
const found = this.rawMessageLookup.get(id)?.message ?? this.openTail.find((message) => message.id === id);
|
|
7592
|
+
return found ? [found] : [];
|
|
7593
|
+
});
|
|
7594
|
+
const quote = findBestVerbatimQuote(`${event.title} ${event.summary}`, messages);
|
|
7595
|
+
return quote ? [{ event, quote }] : [];
|
|
7596
|
+
});
|
|
7597
|
+
if (prepared.length === 0) return { updated: 0, inspected: candidates.length };
|
|
7598
|
+
return this.commitMutation(() => {
|
|
7599
|
+
let updated = 0;
|
|
7600
|
+
const now = toUtc8Iso(this.now());
|
|
7601
|
+
for (const { event, quote } of prepared) {
|
|
7602
|
+
if (event.quotes.length > 0) continue;
|
|
7603
|
+
event.quotes = [quote];
|
|
7604
|
+
event.quotesOrigin = "auto_extracted";
|
|
7605
|
+
event.updatedAt = now;
|
|
7606
|
+
updated += 1;
|
|
7607
|
+
}
|
|
7608
|
+
return { updated, inspected: candidates.length };
|
|
7609
|
+
});
|
|
7610
|
+
}
|
|
7481
7611
|
/**
|
|
7482
7612
|
* Import a memory summary produced by another AI.
|
|
7483
7613
|
*
|
|
@@ -9773,6 +9903,15 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
9773
9903
|
sourceBlockId: target.id,
|
|
9774
9904
|
...event.temporal ? { temporal: restrictExtractionRelations(event.temporal, allowedIds) } : {}
|
|
9775
9905
|
};
|
|
9906
|
+
if ((!candidate.quotes || candidate.quotes.length === 0) && candidate.sourceMessageIds.length > 0) {
|
|
9907
|
+
const sourceIds = new Set(candidate.sourceMessageIds);
|
|
9908
|
+
const sourceMessages2 = target.l5Raw.filter((message) => sourceIds.has(message.id));
|
|
9909
|
+
const quote = findBestVerbatimQuote(`${candidate.title} ${candidate.summary}`, sourceMessages2);
|
|
9910
|
+
if (quote) {
|
|
9911
|
+
candidate.quotes = [quote];
|
|
9912
|
+
candidate.quotesOrigin = "auto_extracted";
|
|
9913
|
+
}
|
|
9914
|
+
}
|
|
9776
9915
|
const candidateKey = JSON.stringify(candidate);
|
|
9777
9916
|
if (seenCandidates.has(candidateKey)) continue;
|
|
9778
9917
|
seenCandidates.add(candidateKey);
|
|
@@ -15036,6 +15175,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15036
15175
|
await this.flush();
|
|
15037
15176
|
const event = (await this.space(session)).listAllEvents().find((candidate) => candidate.id === id);
|
|
15038
15177
|
if (!event) throw new Error(`Unknown event: ${id}`);
|
|
15178
|
+
const expanded = event.quotesOrigin === "auto_extracted" ? { ...event, quotesNotice: AUTO_EXTRACTED_QUOTES_NOTICE } : event;
|
|
15039
15179
|
return this.batch(session, [{
|
|
15040
15180
|
ref: `event:${event.id}`,
|
|
15041
15181
|
target: {
|
|
@@ -15043,7 +15183,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15043
15183
|
elementIds: [],
|
|
15044
15184
|
citation: citation("event", event.id, event.title, `event:${event.id}`, "eventId", { expanded: true })
|
|
15045
15185
|
}
|
|
15046
|
-
}],
|
|
15186
|
+
}], expanded);
|
|
15047
15187
|
}
|
|
15048
15188
|
async assess(session, input, batchId) {
|
|
15049
15189
|
const batch = this.requireBatch(session, batchId, "memory_assess");
|
|
@@ -19277,6 +19417,7 @@ function eventSummary(event, snapshot) {
|
|
|
19277
19417
|
criticality: event.criticality,
|
|
19278
19418
|
...event.catalogHints === void 0 ? {} : { catalogHints: event.catalogHints },
|
|
19279
19419
|
...event.extractorVersion === void 0 ? {} : { extractorVersion: event.extractorVersion },
|
|
19420
|
+
...event.quotesOrigin === "auto_extracted" ? { quotesOrigin: event.quotesOrigin, quotesNotice: AUTO_EXTRACTED_QUOTES_NOTICE } : {},
|
|
19280
19421
|
status: event.status,
|
|
19281
19422
|
supersededBy: event.supersededBy,
|
|
19282
19423
|
weight: event.weight,
|