stratagate-dsh 0.3.26 → 0.3.28
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 +679 -735
- package/LICENSE +21 -21
- package/dist/client.js +5 -6
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +623 -164
- package/dist/plugin.js.map +1 -1
- package/docs/ARCHITECTURE.md +4 -0
- package/docs/DSH.md +7 -3
- package/docs/DSH.zh-CN.md +7 -3
- package/docs/benchmarks/issue-128-windows-review.json +104 -104
- package/docs/benchmarks/issue-128-windows.json +103 -103
- 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 -0
- package/docs/npm-release.md +52 -52
- package/package.json +13 -13
- package/screenshots.json +8 -8
- package/docs/validation/frontend-feedback-20261021/background-refresh-failed.png +0 -0
- package/docs/validation/frontend-feedback-20261021/browser-results.json +0 -334
- package/docs/validation/frontend-feedback-20261021/first-read-graph.png +0 -0
- package/docs/validation/frontend-feedback-20261021/first-read-profile.png +0 -0
- package/docs/validation/frontend-feedback-20261021/first-read-short.png +0 -0
- package/docs/validation/frontend-feedback-20261021/profile-conflict.png +0 -0
- package/docs/validation/frontend-feedback-20261021/profile-save-regression.json +0 -466
- package/docs/validation/frontend-feedback-20261021/report.json +0 -99
- package/docs/validation/frontend-feedback-20261021/settings-saved-read-failed.png +0 -0
- package/docs/validation/frontend-feedback-20261021/settings-unknown.png +0 -0
- package/docs/validation/frontend-feedback-20261021/unit-results.json +0 -1
- package/docs/validation/issue119.md +0 -41
- package/docs/validation/workspace-cleanup-20261020.md +0 -36
package/dist/plugin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { mkdir as mkdir2 } from "node:fs/promises";
|
|
3
|
-
import { existsSync as
|
|
3
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
4
4
|
import { dirname as dirname3 } from "node:path";
|
|
5
5
|
|
|
6
6
|
// src/config.ts
|
|
@@ -1881,6 +1881,12 @@ var SYNTHETIC_SOURCE_THREAD_PREFIXES = ["external-import:", "agent-memory:"];
|
|
|
1881
1881
|
function isSyntheticSourceThreadId(threadId) {
|
|
1882
1882
|
return !!threadId && SYNTHETIC_SOURCE_THREAD_PREFIXES.some((prefix) => threadId.startsWith(prefix));
|
|
1883
1883
|
}
|
|
1884
|
+
function graphProjectionSourceIds(job, events, epoch, approved) {
|
|
1885
|
+
return job.sourceEventIds.filter((id) => {
|
|
1886
|
+
const event = events.get(id);
|
|
1887
|
+
return (event?.status === "active" || event?.status === "superseded") && (job.createdEpoch === epoch || approved.has(id));
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1884
1890
|
function graphProjectionNeedsRecovery(job, events) {
|
|
1885
1891
|
const signature = /^StrataGate model did not produce a valid stratagate_project_knowledge_graph call after 2 attempts: StrataGate stratagate_project_knowledge_graph arguments were invalid: "nodes\[\d+\]\.metadataProvenance\.name\[\d+\]" must be a string(?:; "nodes\[\d+\]\.metadataProvenance\.name\[\d+\]" must be a string)*$/;
|
|
1886
1892
|
return job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION && job.nameProvenanceRecoveryVersion === void 0 && job.status === "failed" && !(job.attempts < DERIVATION_MAX_ATTEMPTS && job.nextRetryAt !== null) && signature.test((job.lastError ?? "").split("\n")[0].trim()) && job.sourceEventIds.length > 0 && job.sourceEventIds.every((id) => events.some((event) => event.id === id && (event.status === "active" || event.status === "superseded")));
|
|
@@ -2045,6 +2051,12 @@ function normalizeSnapshot(value) {
|
|
|
2045
2051
|
if (!Number.isFinite(snapshot.blockDecayLambda) || snapshot.blockDecayLambda < 0) {
|
|
2046
2052
|
throw new TypeError("Invalid StrataGate snapshot: blockDecayLambda must be a non-negative finite number");
|
|
2047
2053
|
}
|
|
2054
|
+
snapshot.graphEnabled = snapshot.graphEnabled === true;
|
|
2055
|
+
snapshot.graphEpoch = Number.isSafeInteger(snapshot.graphEpoch) && snapshot.graphEpoch >= 0 ? snapshot.graphEpoch : 0;
|
|
2056
|
+
if (snapshot.graphBackfillEventIds === void 0) snapshot.graphBackfillEventIds = [];
|
|
2057
|
+
if (!Array.isArray(snapshot.graphBackfillEventIds) || !snapshot.graphBackfillEventIds.every((id) => typeof id === "string")) {
|
|
2058
|
+
throw new TypeError("Invalid StrataGate snapshot: graphBackfillEventIds must be a string array");
|
|
2059
|
+
}
|
|
2048
2060
|
if (!Array.isArray(snapshot.externalMemoryImportJobs)) snapshot.externalMemoryImportJobs = [];
|
|
2049
2061
|
for (const key of ["openTail", "blocks", "summaryJobs", "events", "agentEvents", "graphNodes", "graphEdges", "graphProjectionJobs", "elements", "extractionJobs", "elementProjectionJobs", "usageReceipts", "ingestionReceipts", "externalMemoryImportJobs"]) {
|
|
2050
2062
|
if (!Array.isArray(snapshot[key])) throw new TypeError(`Invalid StrataGate snapshot: ${key} must be an array`);
|
|
@@ -2095,7 +2107,7 @@ function assertValidSnapshot(value) {
|
|
|
2095
2107
|
}
|
|
2096
2108
|
|
|
2097
2109
|
// packages/core/src/store.ts
|
|
2098
|
-
import { createHash as createHash4 } from "node:crypto";
|
|
2110
|
+
import { createHash as createHash4, randomUUID as randomUUID3 } from "node:crypto";
|
|
2099
2111
|
|
|
2100
2112
|
// packages/core/src/weights.ts
|
|
2101
2113
|
var BASE_DECAY = 0.15;
|
|
@@ -3593,7 +3605,7 @@ var MemoryTopicDirectory = class _MemoryTopicDirectory {
|
|
|
3593
3605
|
|
|
3594
3606
|
// packages/core/src/background-work.ts
|
|
3595
3607
|
var BACKGROUND_WORK_INDEX_VERSION = JSON.stringify([
|
|
3596
|
-
|
|
3608
|
+
3,
|
|
3597
3609
|
KNOWLEDGE_GRAPH_PROJECTOR_VERSION,
|
|
3598
3610
|
MEMORY_TOPIC_PROJECTOR_VERSION,
|
|
3599
3611
|
TOPIC_RECOVERY_VERSION
|
|
@@ -3624,9 +3636,19 @@ function backgroundWorkTimes(input, now = Date.now()) {
|
|
|
3624
3636
|
}
|
|
3625
3637
|
}
|
|
3626
3638
|
const events = [...input.events, ...input.agentEvents];
|
|
3639
|
+
const eventById = new Map(events.map((event) => [event.id, event]));
|
|
3640
|
+
const approved = new Set(input.graphBackfillEventIds ?? []);
|
|
3641
|
+
const graphJobCanRun = (job) => graphProjectionSourceIds(job, eventById, input.graphEpoch ?? 0, approved).length > 0;
|
|
3627
3642
|
const covered = new Set(input.graphProjectionJobs.filter((job) => job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION).flatMap((job) => job.sourceEventIds));
|
|
3628
|
-
if (events.some((event) => event.status !== "forgotten" && event.status !== "archived" && !covered.has(event.id))) due.push(0);
|
|
3629
|
-
for (const job of input.graphProjectionJobs) {
|
|
3643
|
+
if (input.graphEnabled && events.some((event) => input.graphBackfillEventIds?.includes(event.id) && event.status !== "forgotten" && event.status !== "archived" && !covered.has(event.id))) due.push(0);
|
|
3644
|
+
for (const job of input.graphEnabled ? input.graphProjectionJobs : []) {
|
|
3645
|
+
if (job.attempts >= DERIVATION_MAX_ATTEMPTS && (job.status === "pending" || job.status === "running" && job.claimEpoch !== void 0 && job.claimEpoch !== (input.graphEpoch ?? 0))) {
|
|
3646
|
+
due.push(0);
|
|
3647
|
+
continue;
|
|
3648
|
+
}
|
|
3649
|
+
if (!graphJobCanRun(job)) continue;
|
|
3650
|
+
if (job.lastError === "Graph claim revoked") continue;
|
|
3651
|
+
if (input.graphEpoch !== void 0 && job.claimEpoch !== void 0 && job.claimEpoch !== input.graphEpoch && job.status !== "completed" && job.attempts < DERIVATION_MAX_ATTEMPTS) due.push(0);
|
|
3630
3652
|
const time = jobWake(job);
|
|
3631
3653
|
if (time !== null) due.push(time);
|
|
3632
3654
|
if (graphProjectionNeedsRecovery(job, events)) due.push(0);
|
|
@@ -3694,6 +3716,9 @@ CREATE TABLE IF NOT EXISTS memory_spaces (
|
|
|
3694
3716
|
current_turn INTEGER NOT NULL,
|
|
3695
3717
|
block_turn_size INTEGER NOT NULL,
|
|
3696
3718
|
block_decay_lambda REAL NOT NULL,
|
|
3719
|
+
graph_enabled INTEGER NOT NULL DEFAULT 0 CHECK (graph_enabled IN (0, 1)),
|
|
3720
|
+
graph_epoch INTEGER NOT NULL DEFAULT 0,
|
|
3721
|
+
graph_approved_event_ids_json TEXT NOT NULL DEFAULT '[]',
|
|
3697
3722
|
created_at TEXT NOT NULL,
|
|
3698
3723
|
updated_at TEXT NOT NULL
|
|
3699
3724
|
) STRICT;
|
|
@@ -4008,6 +4033,11 @@ CREATE TABLE IF NOT EXISTS raw_message_fts_state (
|
|
|
4008
4033
|
backfill_complete INTEGER NOT NULL DEFAULT 0,
|
|
4009
4034
|
FOREIGN KEY (namespace) REFERENCES memory_spaces(namespace) ON DELETE CASCADE
|
|
4010
4035
|
) STRICT;
|
|
4036
|
+
|
|
4037
|
+
CREATE TABLE IF NOT EXISTS stratagate_graph_policy_migrations (
|
|
4038
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
4039
|
+
completed_at TEXT NOT NULL
|
|
4040
|
+
) STRICT;
|
|
4011
4041
|
`;
|
|
4012
4042
|
var RAW_MESSAGE_FTS_SCHEMA = `
|
|
4013
4043
|
CREATE VIRTUAL TABLE IF NOT EXISTS raw_message_fts USING fts5(
|
|
@@ -4070,6 +4100,7 @@ function encodedRawSearchTokens(content) {
|
|
|
4070
4100
|
return searchTokens(content).map(encodeRawSearchToken).join(" ");
|
|
4071
4101
|
}
|
|
4072
4102
|
var SqliteStorage = class _SqliteStorage {
|
|
4103
|
+
graphPolicy;
|
|
4073
4104
|
static workListeners = /* @__PURE__ */ new Map();
|
|
4074
4105
|
filename;
|
|
4075
4106
|
readonly;
|
|
@@ -4077,6 +4108,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4077
4108
|
rawSearchFtsAvailable = false;
|
|
4078
4109
|
closed = false;
|
|
4079
4110
|
constructor(options) {
|
|
4111
|
+
this.graphPolicy = options.graphPolicy;
|
|
4080
4112
|
if (!options.filename.trim()) throw new TypeError("SQLite filename must not be empty");
|
|
4081
4113
|
this.readonly = options.readonly ?? false;
|
|
4082
4114
|
this.filename = options.filename === ":memory:" ? ":memory:" : resolve(options.filename);
|
|
@@ -4103,8 +4135,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4103
4135
|
async load(namespace) {
|
|
4104
4136
|
this.assertOpen();
|
|
4105
4137
|
const key = nonEmptyNamespace(namespace);
|
|
4138
|
+
const hasGraphEnabled = this.hasColumn("memory_spaces", "graph_enabled");
|
|
4106
4139
|
const space = this.database.prepare(`
|
|
4107
|
-
SELECT schema_version, revision, current_turn, block_turn_size, block_decay_lambda
|
|
4140
|
+
SELECT schema_version, revision, current_turn, block_turn_size, block_decay_lambda${hasGraphEnabled ? ", graph_enabled, graph_epoch" : ""}${this.hasColumn("memory_spaces", "graph_approved_event_ids_json") ? ", graph_approved_event_ids_json" : ""}
|
|
4108
4141
|
FROM memory_spaces WHERE namespace = ?
|
|
4109
4142
|
`).get(key);
|
|
4110
4143
|
if (!space) return null;
|
|
@@ -4353,6 +4386,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4353
4386
|
currentTurn: space.current_turn,
|
|
4354
4387
|
blockTurnSize: space.block_turn_size,
|
|
4355
4388
|
blockDecayLambda: space.block_decay_lambda,
|
|
4389
|
+
graphEnabled: Boolean(space.graph_enabled),
|
|
4390
|
+
graphEpoch: space.graph_epoch ?? 0,
|
|
4391
|
+
graphBackfillEventIds: parseJson(space.graph_approved_event_ids_json ?? "[]", "memory_spaces.graph_approved_event_ids_json"),
|
|
4356
4392
|
openTail,
|
|
4357
4393
|
blocks,
|
|
4358
4394
|
summaryJobs,
|
|
@@ -4372,14 +4408,27 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4372
4408
|
};
|
|
4373
4409
|
return { snapshot: cloneSnapshot(normalizeSnapshot(snapshot)), revision: space.revision };
|
|
4374
4410
|
}
|
|
4375
|
-
async save(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards) {
|
|
4411
|
+
async save(namespace, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards, graphEpoch) {
|
|
4376
4412
|
this.assertOpen();
|
|
4377
4413
|
assertValidSnapshot(snapshot);
|
|
4378
4414
|
if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) {
|
|
4379
4415
|
throw new TypeError("expectedRevision must be a non-negative integer");
|
|
4380
4416
|
}
|
|
4381
4417
|
const key = nonEmptyNamespace(namespace);
|
|
4382
|
-
const revision = this.immediateTransaction(() =>
|
|
4418
|
+
const revision = this.immediateTransaction(() => {
|
|
4419
|
+
const storedPolicy = this.database.prepare("SELECT graph_enabled, graph_epoch FROM memory_spaces WHERE namespace = ?").get(key);
|
|
4420
|
+
const policy = this.graphPolicy?.(key) ?? { enabled: storedPolicy ? Boolean(storedPolicy.graph_enabled) : snapshot.graphEnabled === true, paused: false, epoch: storedPolicy?.graph_epoch ?? snapshot.graphEpoch ?? 0 };
|
|
4421
|
+
if (graphEpoch !== void 0) {
|
|
4422
|
+
if (!policy.enabled || policy.paused || policy.epoch !== graphEpoch) {
|
|
4423
|
+
throw new StorageConflictError(key, expectedRevision, expectedRevision);
|
|
4424
|
+
}
|
|
4425
|
+
} else if ((!policy.enabled || policy.paused) && snapshot.graphProjectionJobs.length && (this.graphPolicy || snapshot.graphEpoch === storedPolicy?.graph_epoch)) {
|
|
4426
|
+
const row = this.database.prepare("SELECT jobs_json FROM graph_state WHERE namespace = ?").get(key);
|
|
4427
|
+
const retained = new Set(JSON.parse(row?.jobs_json ?? "[]").map((job) => job.id));
|
|
4428
|
+
snapshot.graphProjectionJobs = snapshot.graphProjectionJobs.filter((job) => retained.has(job.id));
|
|
4429
|
+
}
|
|
4430
|
+
return this.persistSnapshot(key, snapshot, expectedRevision, rawMessageIndexDelta, leaseGuards);
|
|
4431
|
+
});
|
|
4383
4432
|
for (const listener of _SqliteStorage.workListeners.get(this.filename) ?? []) {
|
|
4384
4433
|
try {
|
|
4385
4434
|
listener();
|
|
@@ -4544,7 +4593,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4544
4593
|
if (current) {
|
|
4545
4594
|
this.database.prepare(`
|
|
4546
4595
|
UPDATE memory_spaces
|
|
4547
|
-
SET schema_version = ?, revision = ?, current_turn = ?, block_turn_size = ?, block_decay_lambda = ?, updated_at = ?
|
|
4596
|
+
SET schema_version = ?, revision = ?, current_turn = ?, block_turn_size = ?, block_decay_lambda = ?, graph_enabled = ?, graph_epoch = ?, graph_approved_event_ids_json = ?, updated_at = ?
|
|
4548
4597
|
WHERE namespace = ?
|
|
4549
4598
|
`).run(
|
|
4550
4599
|
snapshot.schemaVersion,
|
|
@@ -4552,14 +4601,17 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4552
4601
|
snapshot.currentTurn,
|
|
4553
4602
|
snapshot.blockTurnSize,
|
|
4554
4603
|
snapshot.blockDecayLambda,
|
|
4604
|
+
snapshot.graphEnabled === true ? 1 : 0,
|
|
4605
|
+
snapshot.graphEpoch ?? 0,
|
|
4606
|
+
JSON.stringify(snapshot.graphBackfillEventIds ?? []),
|
|
4555
4607
|
updatedAt,
|
|
4556
4608
|
namespace
|
|
4557
4609
|
);
|
|
4558
4610
|
} else {
|
|
4559
4611
|
this.database.prepare(`
|
|
4560
4612
|
INSERT INTO memory_spaces (
|
|
4561
|
-
namespace, schema_version, revision, current_turn, block_turn_size, block_decay_lambda, created_at, updated_at
|
|
4562
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
4613
|
+
namespace, schema_version, revision, current_turn, block_turn_size, block_decay_lambda, graph_enabled, graph_epoch, graph_approved_event_ids_json, created_at, updated_at
|
|
4614
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
4563
4615
|
`).run(
|
|
4564
4616
|
namespace,
|
|
4565
4617
|
snapshot.schemaVersion,
|
|
@@ -4567,6 +4619,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
4567
4619
|
snapshot.currentTurn,
|
|
4568
4620
|
snapshot.blockTurnSize,
|
|
4569
4621
|
snapshot.blockDecayLambda,
|
|
4622
|
+
snapshot.graphEnabled === true ? 1 : 0,
|
|
4623
|
+
snapshot.graphEpoch ?? 0,
|
|
4624
|
+
JSON.stringify(snapshot.graphBackfillEventIds ?? []),
|
|
4570
4625
|
updatedAt,
|
|
4571
4626
|
updatedAt
|
|
4572
4627
|
);
|
|
@@ -5009,7 +5064,8 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5009
5064
|
for (const message of snapshot.openTail) if (message.role === "user") tails.set(message.threadId, (tails.get(message.threadId) ?? 0) + 1);
|
|
5010
5065
|
this.writeBackgroundWorkTimes(namespace, backgroundWorkTimes({
|
|
5011
5066
|
...snapshot,
|
|
5012
|
-
sealableTail: [...tails.values()].some((count) => count >= snapshot.blockTurnSize)
|
|
5067
|
+
sealableTail: [...tails.values()].some((count) => count >= snapshot.blockTurnSize),
|
|
5068
|
+
...this.graphSchedulingPolicy(namespace)
|
|
5013
5069
|
}));
|
|
5014
5070
|
return nextRevision;
|
|
5015
5071
|
}
|
|
@@ -5105,6 +5161,11 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5105
5161
|
const jobs = row?.jobs_json ? parseJson(row.jobs_json, `${kind}.jobs`) : [];
|
|
5106
5162
|
const job = jobs.find((job2) => job2.id === id);
|
|
5107
5163
|
if (!job || job.status !== "running" || job.attempts !== attempts || job.updatedAt !== claimedAt || !(Date.parse(job.leaseUntil ?? "") > now)) return null;
|
|
5164
|
+
if (kind === "graph") {
|
|
5165
|
+
const row2 = this.database.prepare("SELECT graph_enabled, graph_epoch FROM memory_spaces WHERE namespace = ?").get(namespace);
|
|
5166
|
+
const policy = this.graphPolicy?.(namespace) ?? { enabled: Boolean(row2?.graph_enabled), paused: false, epoch: row2?.graph_epoch ?? 0 };
|
|
5167
|
+
if (!policy.enabled || policy.paused || "claimEpoch" in job && job.claimEpoch !== void 0 && job.claimEpoch !== policy.epoch) return null;
|
|
5168
|
+
}
|
|
5108
5169
|
job.leaseUntil = until;
|
|
5109
5170
|
const update = kind === "graph" ? "UPDATE graph_state SET jobs_json = ? WHERE namespace = ?" : "UPDATE memory_topic_state SET state_json = json_set(state_json, '$.jobs', json(?)) WHERE namespace = ?";
|
|
5110
5171
|
this.database.prepare(update).run(JSON.stringify(jobs), namespace);
|
|
@@ -5235,6 +5296,7 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5235
5296
|
const agentEvents = await this.loadSchedulingEvents(namespace, "agent_events");
|
|
5236
5297
|
if (this.closed) return false;
|
|
5237
5298
|
const times = backgroundWorkTimes({
|
|
5299
|
+
...this.graphSchedulingPolicy(namespace),
|
|
5238
5300
|
blocks: blocks.map((block) => ({
|
|
5239
5301
|
id: block.id,
|
|
5240
5302
|
processingStatus: block.processing_status,
|
|
@@ -5262,6 +5324,20 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5262
5324
|
}
|
|
5263
5325
|
return Boolean(this.database.prepare("SELECT 1 FROM background_work_dirty WHERE rebuild_after <= ? LIMIT 1").get(Date.now()));
|
|
5264
5326
|
}
|
|
5327
|
+
graphSchedulingPolicy(namespace) {
|
|
5328
|
+
const policy = this.graphPolicy?.(namespace);
|
|
5329
|
+
if (policy) return {
|
|
5330
|
+
graphEnabled: policy.enabled && !policy.paused,
|
|
5331
|
+
graphEpoch: policy.epoch,
|
|
5332
|
+
...policy.backfillEventIds ? { graphBackfillEventIds: policy.backfillEventIds } : {}
|
|
5333
|
+
};
|
|
5334
|
+
const row = this.database.prepare("SELECT graph_enabled, graph_epoch, graph_approved_event_ids_json FROM memory_spaces WHERE namespace = ?").get(namespace);
|
|
5335
|
+
return {
|
|
5336
|
+
graphEnabled: Boolean(row?.graph_enabled),
|
|
5337
|
+
graphEpoch: row?.graph_epoch ?? 0,
|
|
5338
|
+
graphBackfillEventIds: parseJson(row?.graph_approved_event_ids_json ?? "[]", "memory_spaces.graph_approved_event_ids_json")
|
|
5339
|
+
};
|
|
5340
|
+
}
|
|
5265
5341
|
async loadSchedulingEvents(namespace, table) {
|
|
5266
5342
|
const sourceTable = table === "events" ? "event_sources" : "agent_event_sources";
|
|
5267
5343
|
const events = [];
|
|
@@ -5363,6 +5439,11 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5363
5439
|
if (!spaceColumns.some(({ name: name2 }) => name2 === "block_decay_lambda")) {
|
|
5364
5440
|
this.database.exec("ALTER TABLE memory_spaces ADD COLUMN block_decay_lambda REAL NOT NULL DEFAULT 0.3");
|
|
5365
5441
|
}
|
|
5442
|
+
if (!spaceColumns.some(({ name: name2 }) => name2 === "graph_enabled")) {
|
|
5443
|
+
this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_enabled INTEGER NOT NULL DEFAULT 0 CHECK (graph_enabled IN (0, 1))");
|
|
5444
|
+
}
|
|
5445
|
+
if (!spaceColumns.some(({ name: name2 }) => name2 === "graph_epoch")) this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_epoch INTEGER NOT NULL DEFAULT 0");
|
|
5446
|
+
if (!spaceColumns.some(({ name: name2 }) => name2 === "graph_approved_event_ids_json")) this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_approved_event_ids_json TEXT NOT NULL DEFAULT '[]'");
|
|
5366
5447
|
const blockColumns = this.database.prepare("PRAGMA table_info('blocks')").all();
|
|
5367
5448
|
if (!blockColumns.some(({ name: name2 }) => name2 === "thread_id")) {
|
|
5368
5449
|
this.database.exec("ALTER TABLE blocks ADD COLUMN thread_id TEXT");
|
|
@@ -5416,12 +5497,19 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5416
5497
|
this.database.exec(THREAD_INDEXES);
|
|
5417
5498
|
this.rebuildLegacyElementSourceForeignKeys();
|
|
5418
5499
|
this.enableRawSearchFts();
|
|
5500
|
+
this.database.prepare("INSERT INTO stratagate_graph_policy_migrations (id, completed_at) VALUES (1, ?) ON CONFLICT (id) DO NOTHING").run(nowUtc8());
|
|
5419
5501
|
this.database.prepare("UPDATE memory_spaces SET schema_version = ? WHERE schema_version < ?").run(STRATAGATE_STORAGE_SCHEMA_VERSION, STRATAGATE_STORAGE_SCHEMA_VERSION);
|
|
5420
5502
|
this.database.exec(`PRAGMA user_version = ${STRATAGATE_STORAGE_SCHEMA_VERSION}`);
|
|
5421
5503
|
});
|
|
5422
5504
|
} else if (version === STRATAGATE_STORAGE_SCHEMA_VERSION) {
|
|
5423
5505
|
this.immediateTransaction(() => {
|
|
5424
5506
|
this.database.exec(SCHEMA);
|
|
5507
|
+
if (!this.hasColumn("memory_spaces", "graph_enabled")) {
|
|
5508
|
+
this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_enabled INTEGER NOT NULL DEFAULT 0 CHECK (graph_enabled IN (0, 1))");
|
|
5509
|
+
}
|
|
5510
|
+
if (!this.hasColumn("memory_spaces", "graph_epoch")) this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_epoch INTEGER NOT NULL DEFAULT 0");
|
|
5511
|
+
if (!this.hasColumn("memory_spaces", "graph_approved_event_ids_json")) this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_approved_event_ids_json TEXT NOT NULL DEFAULT '[]'");
|
|
5512
|
+
this.database.prepare("INSERT INTO stratagate_graph_policy_migrations (id, completed_at) VALUES (1, ?) ON CONFLICT (id) DO NOTHING").run(nowUtc8());
|
|
5425
5513
|
this.database.exec(THREAD_INDEXES);
|
|
5426
5514
|
this.rebuildLegacyElementSourceForeignKeys();
|
|
5427
5515
|
this.enableRawSearchFts();
|
|
@@ -5442,6 +5530,9 @@ var SqliteStorage = class _SqliteStorage {
|
|
|
5442
5530
|
});
|
|
5443
5531
|
this.assertSchemaVersion();
|
|
5444
5532
|
}
|
|
5533
|
+
hasColumn(table, column) {
|
|
5534
|
+
return this.database.prepare(`PRAGMA table_info('${table}')`).all().some((row) => row.name === column);
|
|
5535
|
+
}
|
|
5445
5536
|
/**
|
|
5446
5537
|
* v12 widened element provenance to the agent_events pool, but SQLite cannot
|
|
5447
5538
|
* retarget an existing FOREIGN KEY. Tables still carrying the legacy
|
|
@@ -5761,6 +5852,10 @@ function duplicateUndoDerived(snapshot, ids2) {
|
|
|
5761
5852
|
var StrataGate = class _StrataGate {
|
|
5762
5853
|
blockTurnSizeValue;
|
|
5763
5854
|
blockDecayLambdaValue;
|
|
5855
|
+
graphEnabledValue;
|
|
5856
|
+
graphEpoch = 0;
|
|
5857
|
+
graphApprovedEventIds = [];
|
|
5858
|
+
graphPolicy;
|
|
5764
5859
|
summarizer;
|
|
5765
5860
|
extractor;
|
|
5766
5861
|
eventDuplicateDecider;
|
|
@@ -5806,6 +5901,8 @@ var StrataGate = class _StrataGate {
|
|
|
5806
5901
|
throw new TypeError("blockDecayLambda must be a non-negative finite number");
|
|
5807
5902
|
}
|
|
5808
5903
|
this.blockDecayLambdaValue = blockDecayLambda;
|
|
5904
|
+
this.graphEnabledValue = options.graphEnabled === true;
|
|
5905
|
+
this.graphPolicy = options.graphPolicy;
|
|
5809
5906
|
this.summarizer = options.summarizer;
|
|
5810
5907
|
this.extractor = options.extractor;
|
|
5811
5908
|
this.eventDuplicateDecider = options.eventDuplicateDecider;
|
|
@@ -5827,6 +5924,7 @@ var StrataGate = class _StrataGate {
|
|
|
5827
5924
|
if (!database) throw new TypeError("SQLite database path must not be empty");
|
|
5828
5925
|
const storage = new SqliteStorage({
|
|
5829
5926
|
filename: database,
|
|
5927
|
+
...options.graphPolicy ? { graphPolicy: options.graphPolicy } : {},
|
|
5830
5928
|
...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {}
|
|
5831
5929
|
});
|
|
5832
5930
|
try {
|
|
@@ -5835,6 +5933,8 @@ var StrataGate = class _StrataGate {
|
|
|
5835
5933
|
namespace: options.namespace,
|
|
5836
5934
|
...options.blockTurnSize !== void 0 ? { blockTurnSize: options.blockTurnSize } : {},
|
|
5837
5935
|
...options.blockDecayLambda !== void 0 ? { blockDecayLambda: options.blockDecayLambda } : {},
|
|
5936
|
+
...options.graphEnabled !== void 0 ? { graphEnabled: options.graphEnabled } : {},
|
|
5937
|
+
...options.graphPolicy ? { graphPolicy: options.graphPolicy } : {},
|
|
5838
5938
|
...options.summarizer ? { summarizer: options.summarizer } : {},
|
|
5839
5939
|
...options.extractor ? { extractor: options.extractor } : {},
|
|
5840
5940
|
...options.eventDuplicateDecider ? { eventDuplicateDecider: options.eventDuplicateDecider } : {},
|
|
@@ -5879,10 +5979,13 @@ var StrataGate = class _StrataGate {
|
|
|
5879
5979
|
if (settingsChanged) loadedRevision = await options.storage.save(namespace, loadedSnapshot, loadedRevision);
|
|
5880
5980
|
}
|
|
5881
5981
|
const memoryOptions = {};
|
|
5982
|
+
if (options.graphPolicy) memoryOptions.graphPolicy = options.graphPolicy;
|
|
5882
5983
|
if (loadedSnapshot) memoryOptions.blockTurnSize = loadedSnapshot.blockTurnSize;
|
|
5883
5984
|
else if (options.blockTurnSize !== void 0) memoryOptions.blockTurnSize = options.blockTurnSize;
|
|
5884
5985
|
if (loadedSnapshot) memoryOptions.blockDecayLambda = loadedSnapshot.blockDecayLambda;
|
|
5885
5986
|
else if (options.blockDecayLambda !== void 0) memoryOptions.blockDecayLambda = options.blockDecayLambda;
|
|
5987
|
+
if (loadedSnapshot) memoryOptions.graphEnabled = loadedSnapshot.graphEnabled === true;
|
|
5988
|
+
else if (options.graphEnabled !== void 0) memoryOptions.graphEnabled = options.graphEnabled;
|
|
5886
5989
|
if (options.summarizer) memoryOptions.summarizer = options.summarizer;
|
|
5887
5990
|
if (options.extractor) memoryOptions.extractor = options.extractor;
|
|
5888
5991
|
if (options.eventDuplicateDecider) memoryOptions.eventDuplicateDecider = options.eventDuplicateDecider;
|
|
@@ -5898,6 +6001,7 @@ var StrataGate = class _StrataGate {
|
|
|
5898
6001
|
memory.namespace = namespace;
|
|
5899
6002
|
if (loaded && loadedSnapshot) {
|
|
5900
6003
|
memory.restoreSnapshot(loadedSnapshot);
|
|
6004
|
+
memory.graphEnabledValue = loadedSnapshot.graphEnabled === true;
|
|
5901
6005
|
memory.revision = loadedRevision;
|
|
5902
6006
|
}
|
|
5903
6007
|
if (options.storage.readonly) return memory;
|
|
@@ -5919,10 +6023,14 @@ var StrataGate = class _StrataGate {
|
|
|
5919
6023
|
});
|
|
5920
6024
|
}
|
|
5921
6025
|
const covered = new Set(memory.listGraphProjectionJobs().filter((job) => job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION).flatMap((job) => job.sourceEventIds));
|
|
5922
|
-
|
|
6026
|
+
const graphPolicy = memory.currentGraphPolicy();
|
|
6027
|
+
const eventById = new Map(memory.listAllEvents().map((event) => [event.id, event]));
|
|
6028
|
+
const approved = new Set(graphPolicy.backfillEventIds ?? []);
|
|
6029
|
+
if (memory.graphCanProcess() && memory.graphProjector && (memory.listGraphProjectionJobs().some((job) => graphProjectionNeedsRecovery(job, memory.listAllEvents()) && graphProjectionSourceIds(job, eventById, graphPolicy.epoch, approved).length > 0) || graphPolicy.backfillEventIds?.some((id) => !covered.has(id)))) await memory.commitMutation(() => {
|
|
6030
|
+
memory.assertGraphEpoch(graphPolicy.epoch);
|
|
5923
6031
|
memory.recoverGraphNameProvenanceFailures();
|
|
5924
6032
|
memory.queueMissingGraphProjections();
|
|
5925
|
-
});
|
|
6033
|
+
}, graphPolicy.epoch);
|
|
5926
6034
|
}
|
|
5927
6035
|
return memory;
|
|
5928
6036
|
}
|
|
@@ -5959,9 +6067,11 @@ var StrataGate = class _StrataGate {
|
|
|
5959
6067
|
}
|
|
5960
6068
|
});
|
|
5961
6069
|
}
|
|
5962
|
-
const interruptedGraphProjections = [...this.graphProjectionJobs.values()].filter(expired);
|
|
6070
|
+
const interruptedGraphProjections = this.graphCanProcess() ? [...this.graphProjectionJobs.values()].filter(expired) : [];
|
|
5963
6071
|
if (interruptedGraphProjections.length > 0) {
|
|
6072
|
+
const epoch = this.currentGraphPolicy().epoch;
|
|
5964
6073
|
await this.commitMutation(() => {
|
|
6074
|
+
this.assertGraphEpoch(epoch);
|
|
5965
6075
|
const now = toUtc8Iso(this.now());
|
|
5966
6076
|
for (const job of interruptedGraphProjections) {
|
|
5967
6077
|
this.graphProjectionJobs.set(job.id, {
|
|
@@ -5972,7 +6082,7 @@ var StrataGate = class _StrataGate {
|
|
|
5972
6082
|
updatedAt: now
|
|
5973
6083
|
});
|
|
5974
6084
|
}
|
|
5975
|
-
});
|
|
6085
|
+
}, epoch);
|
|
5976
6086
|
}
|
|
5977
6087
|
}
|
|
5978
6088
|
async initializeTopicBootstrap() {
|
|
@@ -6167,25 +6277,52 @@ var StrataGate = class _StrataGate {
|
|
|
6167
6277
|
}
|
|
6168
6278
|
/** Retry exactly one failed Graph projection batch. */
|
|
6169
6279
|
async retryGraphProjection(id) {
|
|
6280
|
+
if (!this.graphCanProcess()) throw new Error("Knowledge Graph processing is disabled or paused");
|
|
6170
6281
|
const jobId = id.trim();
|
|
6171
6282
|
if (!jobId) throw new TypeError("Graph projection id must not be empty");
|
|
6172
6283
|
return this.runManualRetry(`graph:${jobId}`, async () => {
|
|
6173
6284
|
if (!this.graphProjector) throw new Error("Graph projector is not configured");
|
|
6174
|
-
|
|
6175
|
-
|
|
6285
|
+
const epoch = this.currentGraphPolicy().epoch;
|
|
6286
|
+
const retryId = await this.commitMutation(() => {
|
|
6287
|
+
this.assertGraphEpoch(epoch);
|
|
6288
|
+
let job = this.requireGraphProjectionJob(jobId);
|
|
6176
6289
|
if (job.status !== "failed") throw new Error(`Graph projection ${jobId} is ${job.status}, not failed`);
|
|
6177
|
-
job.
|
|
6178
|
-
|
|
6179
|
-
|
|
6290
|
+
const ids2 = job.sourceEventIds.filter((id2) => {
|
|
6291
|
+
const event = this.findEvent(id2);
|
|
6292
|
+
return event?.status === "active" || event?.status === "superseded";
|
|
6293
|
+
});
|
|
6294
|
+
if (!ids2.length) throw new Error("Graph projection has no available source events");
|
|
6295
|
+
const replacement = {
|
|
6296
|
+
...job,
|
|
6297
|
+
id: this.graphIdFactory("gproj"),
|
|
6298
|
+
sourceEventIds: ids2,
|
|
6299
|
+
createdEpoch: epoch,
|
|
6300
|
+
status: "pending",
|
|
6301
|
+
attempts: 0,
|
|
6302
|
+
lastError: null,
|
|
6303
|
+
nextRetryAt: null,
|
|
6304
|
+
leaseUntil: null,
|
|
6305
|
+
updatedAt: toUtc8Iso(this.now())
|
|
6306
|
+
};
|
|
6307
|
+
delete replacement.claimEpoch;
|
|
6308
|
+
delete replacement.claimToken;
|
|
6309
|
+
job.status = "failed";
|
|
6310
|
+
job.lastError = "Graph projection manually retried.";
|
|
6180
6311
|
job.nextRetryAt = null;
|
|
6312
|
+
job.leaseUntil = null;
|
|
6181
6313
|
job.updatedAt = toUtc8Iso(this.now());
|
|
6182
|
-
|
|
6183
|
-
|
|
6314
|
+
this.graphProjectionJobs.set(replacement.id, replacement);
|
|
6315
|
+
return replacement.id;
|
|
6316
|
+
}, epoch);
|
|
6317
|
+
const batch = await this.claimGraphProjection(retryId);
|
|
6184
6318
|
if (!batch) throw new Error(`Graph projection ${jobId} could not be claimed`);
|
|
6185
6319
|
try {
|
|
6186
|
-
return await this.completeGraphProjection(jobId, await this.withDerivationLease("graph", jobId, () =>
|
|
6320
|
+
return await this.completeGraphProjection(batch.jobId, await this.withDerivationLease("graph", batch.jobId, () => {
|
|
6321
|
+
batch.assertActive?.();
|
|
6322
|
+
return this.graphProjector(batch);
|
|
6323
|
+
}), batch.claimToken);
|
|
6187
6324
|
} catch (error) {
|
|
6188
|
-
await this.failGraphProjection(jobId, error);
|
|
6325
|
+
await this.failGraphProjection(batch.jobId, error, batch.claimToken);
|
|
6189
6326
|
throw error;
|
|
6190
6327
|
}
|
|
6191
6328
|
});
|
|
@@ -6273,6 +6410,9 @@ var StrataGate = class _StrataGate {
|
|
|
6273
6410
|
currentTurn: this.currentTurn,
|
|
6274
6411
|
blockTurnSize: this.blockTurnSize,
|
|
6275
6412
|
blockDecayLambda: this.blockDecayLambda,
|
|
6413
|
+
graphEnabled: this.graphEnabledValue,
|
|
6414
|
+
graphEpoch: this.graphEpoch,
|
|
6415
|
+
graphBackfillEventIds: this.graphApprovedEventIds,
|
|
6276
6416
|
openTail: this.openTail,
|
|
6277
6417
|
blocks: this.blocks,
|
|
6278
6418
|
summaryJobs: [...this.summaryJobs.values()],
|
|
@@ -6360,6 +6500,7 @@ var StrataGate = class _StrataGate {
|
|
|
6360
6500
|
if (options.deferDerivation === true) {
|
|
6361
6501
|
return { sealedBlocks, readyBlocks, extractedEvents, projectedElements };
|
|
6362
6502
|
}
|
|
6503
|
+
await this.prepareGraphWork();
|
|
6363
6504
|
await this.recoverExpiredDerivations();
|
|
6364
6505
|
for (const block of this.blocks) {
|
|
6365
6506
|
if (options.threadId !== void 0 && block.threadId !== options.threadId) continue;
|
|
@@ -6368,7 +6509,6 @@ var StrataGate = class _StrataGate {
|
|
|
6368
6509
|
extractedEvents.push(...extracted);
|
|
6369
6510
|
if (this.blocks.find((candidate) => candidate.id === block.id)?.processingStatus === "ready") readyBlocks.push(block);
|
|
6370
6511
|
projectedElements.push(...await this.projectEligibleElements() ?? []);
|
|
6371
|
-
await this.projectEligibleGraph();
|
|
6372
6512
|
}
|
|
6373
6513
|
while (true) {
|
|
6374
6514
|
const projected = await this.projectEligibleElements();
|
|
@@ -6591,6 +6731,18 @@ var StrataGate = class _StrataGate {
|
|
|
6591
6731
|
return structuredClone(job);
|
|
6592
6732
|
});
|
|
6593
6733
|
}
|
|
6734
|
+
async abandonExternalMemoryImportJob(jobId) {
|
|
6735
|
+
return this.commitMutation(() => {
|
|
6736
|
+
const job = this.requireExternalMemoryImportJob(jobId);
|
|
6737
|
+
if (job.status === "abandoned") return structuredClone(job);
|
|
6738
|
+
if (!["failed", "ready", "awaiting_confirmation"].includes(job.status)) {
|
|
6739
|
+
throw new Error("\u53EA\u80FD\u653E\u5F03\u5DF2\u505C\u6B62\u4E14\u5C1A\u672A\u5BFC\u5165\u7684\u4EFB\u52A1");
|
|
6740
|
+
}
|
|
6741
|
+
job.status = "abandoned";
|
|
6742
|
+
job.updatedAt = toUtc8Iso(this.now());
|
|
6743
|
+
return structuredClone(job);
|
|
6744
|
+
});
|
|
6745
|
+
}
|
|
6594
6746
|
async completeExternalMemoryImportJob(jobId, result) {
|
|
6595
6747
|
return this.commitMutation(() => {
|
|
6596
6748
|
const job = this.requireExternalMemoryImportJob(jobId);
|
|
@@ -6927,12 +7079,26 @@ var StrataGate = class _StrataGate {
|
|
|
6927
7079
|
});
|
|
6928
7080
|
}
|
|
6929
7081
|
async claimGraphProjection(jobId) {
|
|
6930
|
-
|
|
7082
|
+
if (!this.graphCanProcess()) return null;
|
|
7083
|
+
await this.prepareGraphWork();
|
|
7084
|
+
const policy = this.currentGraphPolicy();
|
|
7085
|
+
const epoch = policy.epoch;
|
|
7086
|
+
const approved = new Set(policy.backfillEventIds ?? []);
|
|
7087
|
+
const eventById = new Map(this.listAllEvents().map((event) => [event.id, event]));
|
|
7088
|
+
const sources2 = (job) => graphProjectionSourceIds(job, eventById, epoch, approved);
|
|
7089
|
+
const eligible = (candidate) => (jobId === void 0 || candidate.id === jobId) && candidate.attempts < DERIVATION_MAX_ATTEMPTS && sources2(candidate).length > 0 && (candidate.status === "pending" || candidate.status === "failed" && candidate.nextRetryAt !== null && Date.parse(candidate.nextRetryAt) <= this.now().getTime());
|
|
6931
7090
|
if (![...this.graphProjectionJobs.values()].some(eligible)) return null;
|
|
6932
7091
|
return this.commitMutation(() => {
|
|
7092
|
+
this.assertGraphEpoch(epoch);
|
|
6933
7093
|
const job = [...this.graphProjectionJobs.values()].filter(eligible).sort((left, right) => Number(left.status !== "pending") - Number(right.status !== "pending") || right.priority - left.priority || left.createdAt.localeCompare(right.createdAt))[0];
|
|
6934
7094
|
if (!job) return null;
|
|
6935
|
-
const sourceEventIds = job
|
|
7095
|
+
const sourceEventIds = sources2(job);
|
|
7096
|
+
const allowed = new Set(sourceEventIds);
|
|
7097
|
+
const deferred = job.sourceEventIds.filter((id) => !allowed.has(id));
|
|
7098
|
+
if (deferred.length) {
|
|
7099
|
+
const id = this.graphIdFactory("gproj");
|
|
7100
|
+
this.graphProjectionJobs.set(id, { ...job, id, sourceEventIds: deferred, nodeIds: [], edgeIds: [] });
|
|
7101
|
+
}
|
|
6936
7102
|
for (let index = GRAPH_PROJECTION_BATCH_SIZE; index < sourceEventIds.length; index += GRAPH_PROJECTION_BATCH_SIZE) {
|
|
6937
7103
|
const id = this.graphIdFactory("gproj");
|
|
6938
7104
|
this.graphProjectionJobs.set(id, {
|
|
@@ -6948,8 +7114,10 @@ var StrataGate = class _StrataGate {
|
|
|
6948
7114
|
const event = this.findEvent(id);
|
|
6949
7115
|
return event ? [event] : [];
|
|
6950
7116
|
});
|
|
6951
|
-
if (events.length
|
|
7117
|
+
if (events.length !== job.sourceEventIds.length) throw new Error(`Graph projection ${job.id} has unavailable source events`);
|
|
6952
7118
|
job.nameProvenanceRecoveryVersion = 1;
|
|
7119
|
+
job.claimEpoch = epoch;
|
|
7120
|
+
job.claimToken = randomUUID3();
|
|
6953
7121
|
job.status = "running";
|
|
6954
7122
|
job.leaseUntil = new Date(this.now().getTime() + DERIVATION_LEASE_MS).toISOString();
|
|
6955
7123
|
job.attempts += 1;
|
|
@@ -6972,19 +7140,32 @@ var StrataGate = class _StrataGate {
|
|
|
6972
7140
|
]).map(({ id }) => id));
|
|
6973
7141
|
return {
|
|
6974
7142
|
jobId: job.id,
|
|
7143
|
+
claimToken: job.claimToken,
|
|
6975
7144
|
projectorVersion: job.projectorVersion,
|
|
7145
|
+
assertActive: () => {
|
|
7146
|
+
this.assertGraphEpoch(epoch);
|
|
7147
|
+
const current = this.graphProjectionJobs.get(job.id);
|
|
7148
|
+
if (!current || current.status !== "running" || current.claimToken !== job.claimToken) {
|
|
7149
|
+
throw new Error("Knowledge Graph claim superseded; late result discarded");
|
|
7150
|
+
}
|
|
7151
|
+
},
|
|
6976
7152
|
events: structuredClone(events),
|
|
6977
7153
|
existingNodes: structuredClone(existingNodes),
|
|
6978
7154
|
existingEdges: structuredClone(this.graphEdges.filter(({ id, fromNodeId, toNodeId }) => visibleEdgeIds.has(id) && nodeIds.has(fromNodeId) && nodeIds.has(toNodeId)).slice(-60))
|
|
6979
7155
|
};
|
|
6980
|
-
});
|
|
7156
|
+
}, epoch);
|
|
6981
7157
|
}
|
|
6982
7158
|
async claimNextGraphProjection() {
|
|
6983
7159
|
return this.claimGraphProjection();
|
|
6984
7160
|
}
|
|
6985
|
-
async completeGraphProjection(jobId, result) {
|
|
7161
|
+
async completeGraphProjection(jobId, result, claimToken) {
|
|
7162
|
+
const epoch = this.requireGraphProjectionJob(jobId).claimEpoch ?? this.currentGraphPolicy().epoch;
|
|
6986
7163
|
return this.commitMutation(() => {
|
|
7164
|
+
this.assertGraphEpoch(epoch);
|
|
6987
7165
|
const job = this.requireGraphProjectionJob(jobId);
|
|
7166
|
+
if (claimToken !== void 0 && job.claimToken !== claimToken) {
|
|
7167
|
+
throw new Error("Knowledge Graph claim superseded; late result discarded");
|
|
7168
|
+
}
|
|
6988
7169
|
if (job.status === "completed") return { nodeIds: job.nodeIds, edgeIds: job.edgeIds };
|
|
6989
7170
|
if (job.status !== "running") throw new Error(`Graph projection ${job.id} is ${job.status}, not running`);
|
|
6990
7171
|
const touched = applyGraphProjection({
|
|
@@ -7006,17 +7187,22 @@ var StrataGate = class _StrataGate {
|
|
|
7006
7187
|
job.nextRetryAt = null;
|
|
7007
7188
|
job.updatedAt = toUtc8Iso(this.now());
|
|
7008
7189
|
return { nodeIds: touched.nodeIds, edgeIds: touched.edgeIds };
|
|
7009
|
-
});
|
|
7190
|
+
}, epoch);
|
|
7010
7191
|
}
|
|
7011
|
-
async failGraphProjection(jobId, error) {
|
|
7192
|
+
async failGraphProjection(jobId, error, claimToken) {
|
|
7193
|
+
if (!this.graphCanProcess()) return;
|
|
7194
|
+
const epoch = this.requireGraphProjectionJob(jobId).claimEpoch ?? this.currentGraphPolicy().epoch;
|
|
7195
|
+
if (epoch !== this.currentGraphPolicy().epoch) return;
|
|
7012
7196
|
await this.commitMutation(() => {
|
|
7197
|
+
this.assertGraphEpoch(epoch);
|
|
7013
7198
|
const job = this.requireGraphProjectionJob(jobId);
|
|
7199
|
+
if (claimToken !== void 0 && job.claimToken !== claimToken) return;
|
|
7014
7200
|
if (job.status === "completed") return;
|
|
7015
7201
|
job.status = "failed";
|
|
7016
7202
|
job.lastError = errorMessage(error);
|
|
7017
7203
|
job.nextRetryAt = this.retryAt(job.attempts);
|
|
7018
7204
|
job.updatedAt = toUtc8Iso(this.now());
|
|
7019
|
-
});
|
|
7205
|
+
}, epoch);
|
|
7020
7206
|
}
|
|
7021
7207
|
async searchElements(query, options = {}) {
|
|
7022
7208
|
const normalizedName = normalizeSearchText(options.name ?? "");
|
|
@@ -7831,10 +8017,12 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
7831
8017
|
}
|
|
7832
8018
|
/** Use the same Event-authoritative visibility boundary as Graph search. */
|
|
7833
8019
|
hasSearchableGraphNodes() {
|
|
8020
|
+
if (!this.graphEnabled) return false;
|
|
7834
8021
|
const events = this.listAllEvents();
|
|
7835
8022
|
return this.graphNodes.some((node) => effectiveGraphNodeView(node, this.graphEdges, events) !== null);
|
|
7836
8023
|
}
|
|
7837
8024
|
async searchGraphNodes(query, limit = 8) {
|
|
8025
|
+
if (!this.graphEnabled) return [];
|
|
7838
8026
|
const queryTokens = [...new Set(searchTokens(query))];
|
|
7839
8027
|
if (queryTokens.length === 0) return [];
|
|
7840
8028
|
const views = this.graphNodes.flatMap((node) => effectiveGraphNodeView(node, this.graphEdges, this.listAllEvents()) ?? []);
|
|
@@ -8017,10 +8205,10 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8017
8205
|
return job;
|
|
8018
8206
|
}
|
|
8019
8207
|
queueGraphProjection(sourceEventIds, priority) {
|
|
8020
|
-
if (!this.graphProjector) return;
|
|
8208
|
+
if (!this.graphProjector || !this.graphCanProcess()) return;
|
|
8021
8209
|
const completed = new Set([...this.graphProjectionJobs.values()].filter((job) => job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION && job.status === "completed").flatMap((job) => job.sourceEventIds));
|
|
8022
8210
|
const queued = new Set([...this.graphProjectionJobs.values()].filter((job) => job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION && job.status !== "completed").flatMap((job) => job.sourceEventIds));
|
|
8023
|
-
const ids2 = [...new Set(sourceEventIds.filter((id) => this.findEvent(id) && !completed.has(id) && !queued.has(id)))];
|
|
8211
|
+
const ids2 = [...new Set(sourceEventIds.filter((id) => this.findEvent(id) && this.findEvent(id).status !== "forgotten" && this.findEvent(id).status !== "archived" && !completed.has(id) && !queued.has(id)))];
|
|
8024
8212
|
const now = toUtc8Iso(this.now());
|
|
8025
8213
|
for (let index = 0; index < ids2.length; index += GRAPH_PROJECTION_BATCH_SIZE) {
|
|
8026
8214
|
const job = {
|
|
@@ -8032,6 +8220,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8032
8220
|
priority,
|
|
8033
8221
|
nodeIds: [],
|
|
8034
8222
|
edgeIds: [],
|
|
8223
|
+
createdEpoch: this.currentGraphPolicy().epoch,
|
|
8035
8224
|
nameProvenanceRecoveryVersion: 1,
|
|
8036
8225
|
reason: null,
|
|
8037
8226
|
lastError: null,
|
|
@@ -8042,9 +8231,53 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8042
8231
|
this.graphProjectionJobs.set(job.id, job);
|
|
8043
8232
|
}
|
|
8044
8233
|
}
|
|
8234
|
+
async prepareGraphWork() {
|
|
8235
|
+
if (!this.graphCanProcess() || !this.graphProjector) return;
|
|
8236
|
+
const policy = this.currentGraphPolicy();
|
|
8237
|
+
const eventById = new Map(this.listAllEvents().map((event) => [event.id, event]));
|
|
8238
|
+
const approved = new Set(policy.backfillEventIds ?? []);
|
|
8239
|
+
const revoked = [...this.graphProjectionJobs.values()].filter((job) => job.status === "pending" && job.attempts >= DERIVATION_MAX_ATTEMPTS || job.claimEpoch !== void 0 && job.claimEpoch !== policy.epoch && job.status !== "completed" && job.lastError !== "Graph claim revoked" && (job.attempts < DERIVATION_MAX_ATTEMPTS || job.status !== "failed" || job.leaseUntil != null || job.nextRetryAt !== null) && (job.attempts >= DERIVATION_MAX_ATTEMPTS || graphProjectionSourceIds(job, eventById, policy.epoch, approved).length > 0));
|
|
8240
|
+
const covered = new Set([...this.graphProjectionJobs.values()].filter((job) => job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION).flatMap((job) => job.sourceEventIds));
|
|
8241
|
+
const missing = policy.backfillEventIds?.some((id) => !covered.has(id) && this.findEvent(id));
|
|
8242
|
+
if (!revoked.length && !missing) return;
|
|
8243
|
+
await this.commitMutation(() => {
|
|
8244
|
+
this.assertGraphEpoch(policy.epoch);
|
|
8245
|
+
for (const job of revoked) {
|
|
8246
|
+
if (job.attempts >= DERIVATION_MAX_ATTEMPTS) {
|
|
8247
|
+
job.status = "failed";
|
|
8248
|
+
job.lastError ??= "Graph projection retry budget exhausted.";
|
|
8249
|
+
job.nextRetryAt = null;
|
|
8250
|
+
job.leaseUntil = null;
|
|
8251
|
+
job.updatedAt = toUtc8Iso(this.now());
|
|
8252
|
+
continue;
|
|
8253
|
+
}
|
|
8254
|
+
const sourceEventIds = graphProjectionSourceIds(job, eventById, policy.epoch, approved);
|
|
8255
|
+
if (!sourceEventIds.length) continue;
|
|
8256
|
+
const replacement = {
|
|
8257
|
+
...job,
|
|
8258
|
+
id: this.graphIdFactory("gproj"),
|
|
8259
|
+
status: job.nextRetryAt ? "failed" : "pending",
|
|
8260
|
+
leaseUntil: null,
|
|
8261
|
+
updatedAt: toUtc8Iso(this.now())
|
|
8262
|
+
};
|
|
8263
|
+
delete replacement.claimEpoch;
|
|
8264
|
+
job.status = "failed";
|
|
8265
|
+
job.lastError = "Graph claim revoked";
|
|
8266
|
+
job.nextRetryAt = null;
|
|
8267
|
+
job.leaseUntil = null;
|
|
8268
|
+
this.graphProjectionJobs.set(replacement.id, replacement);
|
|
8269
|
+
}
|
|
8270
|
+
this.queueMissingGraphProjections();
|
|
8271
|
+
}, policy.epoch);
|
|
8272
|
+
}
|
|
8045
8273
|
recoverGraphNameProvenanceFailures() {
|
|
8274
|
+
if (!this.graphCanProcess()) return;
|
|
8275
|
+
const policy = this.currentGraphPolicy();
|
|
8276
|
+
const eventById = new Map(this.listAllEvents().map((event) => [event.id, event]));
|
|
8277
|
+
const approved = new Set(policy.backfillEventIds ?? []);
|
|
8046
8278
|
for (const job of this.graphProjectionJobs.values()) {
|
|
8047
8279
|
if (!graphProjectionNeedsRecovery(job, this.listAllEvents())) continue;
|
|
8280
|
+
if (!graphProjectionSourceIds(job, eventById, policy.epoch, approved).length) continue;
|
|
8048
8281
|
job.nameProvenanceRecoveryVersion = 1;
|
|
8049
8282
|
job.status = "pending";
|
|
8050
8283
|
job.attempts = 0;
|
|
@@ -8054,7 +8287,9 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8054
8287
|
}
|
|
8055
8288
|
}
|
|
8056
8289
|
queueMissingGraphProjections() {
|
|
8057
|
-
|
|
8290
|
+
if (!this.graphCanProcess()) return;
|
|
8291
|
+
const approved = this.currentGraphPolicy().backfillEventIds;
|
|
8292
|
+
const candidates = this.listAllEvents().filter((event) => approved?.includes(event.id)).filter((event) => event.status !== "forgotten" && event.status !== "archived").sort((left, right) => {
|
|
8058
8293
|
const score = (event) => (event.status === "active" ? 1e4 : 0) + event.weight.mentionCount * 100 + (event.scope === "project" ? 500 : 0) + (Date.parse(event.temporal.happenedStart ?? event.temporal.mentionedAt ?? event.updatedAt) || 0) / 1e12;
|
|
8059
8294
|
return score(right) - score(left);
|
|
8060
8295
|
});
|
|
@@ -8490,9 +8725,12 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8490
8725
|
const batch = await this.claimNextGraphProjection();
|
|
8491
8726
|
if (!batch) return null;
|
|
8492
8727
|
try {
|
|
8493
|
-
return await this.completeGraphProjection(batch.jobId, await this.withDerivationLease("graph", batch.jobId, () =>
|
|
8728
|
+
return await this.completeGraphProjection(batch.jobId, await this.withDerivationLease("graph", batch.jobId, () => {
|
|
8729
|
+
batch.assertActive?.();
|
|
8730
|
+
return this.graphProjector(batch);
|
|
8731
|
+
}), batch.claimToken);
|
|
8494
8732
|
} catch (error) {
|
|
8495
|
-
await this.failGraphProjection(batch.jobId, error);
|
|
8733
|
+
await this.failGraphProjection(batch.jobId, error, batch.claimToken);
|
|
8496
8734
|
return null;
|
|
8497
8735
|
}
|
|
8498
8736
|
}
|
|
@@ -8536,7 +8774,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8536
8774
|
await pending;
|
|
8537
8775
|
}
|
|
8538
8776
|
}
|
|
8539
|
-
async commitMutation(mutation) {
|
|
8777
|
+
async commitMutation(mutation, graphEpoch) {
|
|
8540
8778
|
if (this.storage?.readonly) throw new Error("Cannot mutate read-only StrataGate storage.");
|
|
8541
8779
|
const previous = this.mutationQueue;
|
|
8542
8780
|
let release;
|
|
@@ -8560,7 +8798,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8560
8798
|
try {
|
|
8561
8799
|
const result = await mutation();
|
|
8562
8800
|
this.topicDirectory.synchronize(this.listAllEvents(), toUtc8Iso(this.now()));
|
|
8563
|
-
await this.persist(before);
|
|
8801
|
+
await this.persist(before, graphEpoch);
|
|
8564
8802
|
return result;
|
|
8565
8803
|
} catch (error) {
|
|
8566
8804
|
this.restoreSnapshot(before);
|
|
@@ -8574,7 +8812,7 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8574
8812
|
release();
|
|
8575
8813
|
}
|
|
8576
8814
|
}
|
|
8577
|
-
async persist(before) {
|
|
8815
|
+
async persist(before, graphEpoch) {
|
|
8578
8816
|
if (!this.storage || !this.namespace) return;
|
|
8579
8817
|
const delta = {
|
|
8580
8818
|
upsert: [...this.pendingRawUpserts.values()],
|
|
@@ -8593,7 +8831,9 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8593
8831
|
});
|
|
8594
8832
|
}
|
|
8595
8833
|
}
|
|
8596
|
-
this.revision = await this.storage.save(this.namespace, snapshot, this.revision, delta, guards);
|
|
8834
|
+
this.revision = await this.storage.save(this.namespace, snapshot, this.revision, delta, guards, graphEpoch);
|
|
8835
|
+
const persistedGraphIds = new Set(snapshot.graphProjectionJobs.map((job) => job.id));
|
|
8836
|
+
for (const id of this.graphProjectionJobs.keys()) if (!persistedGraphIds.has(id)) this.graphProjectionJobs.delete(id);
|
|
8597
8837
|
for (const [jobs, live2] of [[snapshot.summaryJobs, this.summaryJobs], [snapshot.extractionJobs, this.extractionJobs]]) {
|
|
8598
8838
|
for (const job of jobs) {
|
|
8599
8839
|
const current = live2.get(job.blockId);
|
|
@@ -8615,6 +8855,9 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8615
8855
|
}
|
|
8616
8856
|
const copy = cloneSnapshot(normalized);
|
|
8617
8857
|
this.blockDecayLambdaValue = copy.blockDecayLambda;
|
|
8858
|
+
this.graphEnabledValue = copy.graphEnabled === true;
|
|
8859
|
+
this.graphEpoch = copy.graphEpoch ?? 0;
|
|
8860
|
+
this.graphApprovedEventIds = copy.graphBackfillEventIds ?? [];
|
|
8618
8861
|
this.currentTurn = copy.currentTurn;
|
|
8619
8862
|
this.openTail.splice(0, this.openTail.length, ...copy.openTail);
|
|
8620
8863
|
this.blocks.splice(0, this.blocks.length, ...copy.blocks);
|
|
@@ -8645,6 +8888,56 @@ ${duplicate.summary}` : duplicate.summary;
|
|
|
8645
8888
|
this.topicDirectory.synchronize(this.listAllEvents(), toUtc8Iso(this.now()));
|
|
8646
8889
|
this.validateReferences();
|
|
8647
8890
|
}
|
|
8891
|
+
currentGraphPolicy() {
|
|
8892
|
+
return this.graphPolicy?.() ?? { enabled: this.graphEnabledValue, paused: false, epoch: this.graphEpoch, backfillEventIds: this.graphApprovedEventIds };
|
|
8893
|
+
}
|
|
8894
|
+
get graphEnabled() {
|
|
8895
|
+
return this.currentGraphPolicy().enabled;
|
|
8896
|
+
}
|
|
8897
|
+
graphCanProcess() {
|
|
8898
|
+
const policy = this.currentGraphPolicy();
|
|
8899
|
+
return policy.enabled && !policy.paused;
|
|
8900
|
+
}
|
|
8901
|
+
assertGraphEpoch(epoch) {
|
|
8902
|
+
if (!this.graphCanProcess() || this.currentGraphPolicy().epoch !== epoch) throw new Error("Knowledge Graph claim revoked; late result discarded");
|
|
8903
|
+
}
|
|
8904
|
+
/** Count only events that are not already covered by a current-version job. */
|
|
8905
|
+
graphBackfillStatus() {
|
|
8906
|
+
const events = this.listAllEvents().filter((event) => event.status !== "forgotten" && event.status !== "archived");
|
|
8907
|
+
const covered = new Set([...this.graphProjectionJobs.values()].filter((job) => job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION && job.status === "completed").flatMap((job) => job.sourceEventIds));
|
|
8908
|
+
const pending = events.filter((event) => !covered.has(event.id) && ![...this.graphProjectionJobs.values()].some((job) => job.status !== "completed" && job.sourceEventIds.includes(event.id))).length;
|
|
8909
|
+
return {
|
|
8910
|
+
enabled: this.graphEnabledValue,
|
|
8911
|
+
pending,
|
|
8912
|
+
running: [...this.graphProjectionJobs.values()].filter((job) => job.status === "running").length,
|
|
8913
|
+
failed: [...this.graphProjectionJobs.values()].filter((job) => job.status === "failed").length,
|
|
8914
|
+
total: events.length
|
|
8915
|
+
};
|
|
8916
|
+
}
|
|
8917
|
+
async setGraphEnabled(enabled, approvedEventIds) {
|
|
8918
|
+
return this.commitMutation(() => {
|
|
8919
|
+
if (enabled === this.graphEnabledValue) return this.graphBackfillStatus();
|
|
8920
|
+
this.graphEnabledValue = enabled;
|
|
8921
|
+
this.graphEpoch += 1;
|
|
8922
|
+
this.graphApprovedEventIds = enabled ? [...new Set(approvedEventIds ?? [])] : [];
|
|
8923
|
+
if (!enabled) {
|
|
8924
|
+
for (const job of this.graphProjectionJobs.values()) if (job.status === "running") {
|
|
8925
|
+
job.status = "failed";
|
|
8926
|
+
job.lastError = "Knowledge Graph disabled before completion.";
|
|
8927
|
+
job.nextRetryAt = null;
|
|
8928
|
+
job.leaseUntil = null;
|
|
8929
|
+
job.updatedAt = toUtc8Iso(this.now());
|
|
8930
|
+
}
|
|
8931
|
+
} else if (approvedEventIds) {
|
|
8932
|
+
this.queueGraphProjection(approvedEventIds, 1);
|
|
8933
|
+
}
|
|
8934
|
+
return this.graphBackfillStatus();
|
|
8935
|
+
});
|
|
8936
|
+
}
|
|
8937
|
+
graphBackfillEventIds() {
|
|
8938
|
+
const covered = new Set([...this.graphProjectionJobs.values()].filter((job) => job.status === "completed").flatMap((job) => job.sourceEventIds));
|
|
8939
|
+
return this.listAllEvents().filter((event) => event.status !== "forgotten" && event.status !== "archived" && !covered.has(event.id)).map((event) => event.id);
|
|
8940
|
+
}
|
|
8648
8941
|
validateReferences() {
|
|
8649
8942
|
const blockIds = /* @__PURE__ */ new Set();
|
|
8650
8943
|
const messageBlockIds = /* @__PURE__ */ new Map();
|
|
@@ -9400,10 +9693,10 @@ function parseJsonResponse(value, requiredKeys = []) {
|
|
|
9400
9693
|
}
|
|
9401
9694
|
|
|
9402
9695
|
// src/llm.ts
|
|
9403
|
-
import { randomUUID as
|
|
9696
|
+
import { randomUUID as randomUUID5 } from "node:crypto";
|
|
9404
9697
|
|
|
9405
9698
|
// src/diagnostics.ts
|
|
9406
|
-
import { randomUUID as
|
|
9699
|
+
import { randomUUID as randomUUID4 } from "node:crypto";
|
|
9407
9700
|
import { createRequire as createRequire2 } from "node:module";
|
|
9408
9701
|
import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
|
|
9409
9702
|
var DAY = 864e5;
|
|
@@ -9462,7 +9755,7 @@ var DiagnosticsRecorder = class {
|
|
|
9462
9755
|
if (this.closed) return;
|
|
9463
9756
|
const bounded2 = Object.fromEntries(Object.entries(counters).filter(([, n]) => Number.isFinite(n) && n >= 0));
|
|
9464
9757
|
this.pending.push({
|
|
9465
|
-
id: `diag_${
|
|
9758
|
+
id: `diag_${randomUUID4()}`,
|
|
9466
9759
|
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
9467
9760
|
namespace: scope.namespace,
|
|
9468
9761
|
stage: scope.stage,
|
|
@@ -10482,7 +10775,10 @@ Use project scope for repository decisions, user scope for stable preferences/id
|
|
|
10482
10775
|
const raw = object(await this.callStructured(
|
|
10483
10776
|
"graphProjector",
|
|
10484
10777
|
`Project the supplied Events into the current Knowledge Graph, then call ${STRUCTURED_TOOLS.graphProjector.name} exactly once. Events are the sole source of truth; never use legacy Element data. Return only nodes and edges touched by the supplied Events; never echo unchanged historical graph records. Return at most 24 nodes and 32 edges. Use stable entity nodes for people, projects, organizations, tools, and places. Use aliases to merge spelling/case/separator variants. Give every returned node 1-6 concise semantic role tags such as benchmark, evaluation, memory-plugin, parser, or development-tool; tags describe the node's specific role and never replace its person/project/organization/tool/place type. Reuse stable tag wording when possible. For every node name, alias, and tag, include metadataProvenance with the exact supplied Event ids that support that individual value; never use an unrelated active Event as a substitute. The shapes are different: metadataProvenance.name is an Event ID string array, e.g. ["evt_x"], supporting node.name; it is never an array of objects. Only metadataProvenance.aliases and metadataProvenance.tags are arrays of { value, sourceEventIds: ["evt_x"] } objects for each alias/tag. Example for node.name="StrataGate": metadataProvenance={"name":["evt_x"],"aliases":[{"value":"strata_gate","sourceEventIds":["evt_x"]}],"tags":[{"value":"memory-plugin","sourceEventIds":["evt_x"]}]}. Put attributes in node facts and every relationship in a directed edge using fromRef/toRef\u2014never encode a relationship as a fact string. Prefer concise canonical Chinese relation labels such as \u4F7F\u7528\u3001\u5C5E\u4E8E\u3001\u521B\u5EFA\u3001\u53C2\u4E0E\u3001\u8D21\u732E\u3001\u4F9D\u8D56\u3001\u4F4D\u4E8E\u3001\u76F8\u5173. Every node, fact, edge, and metadata provenance id must cite only supplied Event ids. Do not return text.`,
|
|
10485
|
-
compactGraphProjectionContext(context)
|
|
10778
|
+
compactGraphProjectionContext(context),
|
|
10779
|
+
void 0,
|
|
10780
|
+
void 0,
|
|
10781
|
+
context.assertActive
|
|
10486
10782
|
));
|
|
10487
10783
|
const nodes = (Array.isArray(raw.nodes) ? raw.nodes : []).flatMap((candidate) => {
|
|
10488
10784
|
const item = object(candidate);
|
|
@@ -10636,18 +10932,18 @@ Use project scope for repository decisions, user scope for stable preferences/id
|
|
|
10636
10932
|
}
|
|
10637
10933
|
return proposed;
|
|
10638
10934
|
}
|
|
10639
|
-
async callStructured(kind, system, payload, validate, topicMetadata) {
|
|
10935
|
+
async callStructured(kind, system, payload, validate, topicMetadata, assertActive) {
|
|
10640
10936
|
const execution = this.sessions.getStore();
|
|
10641
10937
|
const scope = {
|
|
10642
10938
|
namespace: execution?.namespace ?? this.diagnosticNamespace(execution?.session, String(execution?.sessionId ?? "")),
|
|
10643
10939
|
stage: kind,
|
|
10644
|
-
taskId: `task_${
|
|
10940
|
+
taskId: `task_${randomUUID5()}`,
|
|
10645
10941
|
calls: 0
|
|
10646
10942
|
};
|
|
10647
10943
|
const start = Date.now();
|
|
10648
10944
|
return this.diagnosticScopes.run(scope, async () => {
|
|
10649
10945
|
try {
|
|
10650
|
-
const result = await this.callStructuredInner(kind, system, payload, validate, topicMetadata);
|
|
10946
|
+
const result = await this.callStructuredInner(kind, system, payload, validate, topicMetadata, assertActive);
|
|
10651
10947
|
this.diagnostics?.record(scope, {
|
|
10652
10948
|
operations: 1,
|
|
10653
10949
|
operationsSucceeded: 1,
|
|
@@ -10670,11 +10966,12 @@ Use project scope for repository decisions, user scope for stable preferences/id
|
|
|
10670
10966
|
}
|
|
10671
10967
|
});
|
|
10672
10968
|
}
|
|
10673
|
-
async callStructuredInner(kind, system, payload, validate, topicMetadata) {
|
|
10969
|
+
async callStructuredInner(kind, system, payload, validate, topicMetadata, assertActive) {
|
|
10674
10970
|
const execution = this.sessions.getStore();
|
|
10675
10971
|
if (!execution) throw new Error("StrataGate model callback ran without an execution context");
|
|
10676
10972
|
const baseRoute = this.resolveRoute(execution.session);
|
|
10677
10973
|
const routeKey = `${baseRoute.provider}\0${baseRoute.model}`;
|
|
10974
|
+
assertActive?.();
|
|
10678
10975
|
let useOff = await this.shouldUseOff(baseRoute);
|
|
10679
10976
|
const isTopic = kind === "topicProjector";
|
|
10680
10977
|
const isGraph = kind === "graphProjector";
|
|
@@ -10692,6 +10989,7 @@ Use project scope for repository decisions, user scope for stable preferences/id
|
|
|
10692
10989
|
let topicRetryFeedback = "";
|
|
10693
10990
|
let offFallback = false;
|
|
10694
10991
|
for (let attempt = 1; attempt <= JSON_RESPONSE_ATTEMPTS; attempt += 1) {
|
|
10992
|
+
assertActive?.();
|
|
10695
10993
|
attemptsUsed = attempt;
|
|
10696
10994
|
if (kind === "dreamingStep") {
|
|
10697
10995
|
payload = structuredClone(payload);
|
|
@@ -10793,6 +11091,7 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
|
|
|
10793
11091
|
}
|
|
10794
11092
|
throw error;
|
|
10795
11093
|
}
|
|
11094
|
+
assertActive?.();
|
|
10796
11095
|
const finish = assembler.finish;
|
|
10797
11096
|
if (isTopic) topicDiagnostics.finishReason = finish.kind;
|
|
10798
11097
|
if (finish.kind === "error" || finish.kind === "aborted") {
|
|
@@ -11053,6 +11352,21 @@ function isOffRejection(error) {
|
|
|
11053
11352
|
|
|
11054
11353
|
// src/metadata.ts
|
|
11055
11354
|
import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
|
|
11355
|
+
import { existsSync } from "node:fs";
|
|
11356
|
+
function readGraphSettings(filename) {
|
|
11357
|
+
const defaults = { enabled: false, paused: false, epoch: 0, legacy: false, noticeSeen: false, everEnabled: false, approved: {} };
|
|
11358
|
+
if (filename === ":memory:" || !existsSync(filename)) return defaults;
|
|
11359
|
+
const db = new DatabaseSync3(filename, { readOnly: true, timeout: 5e3 });
|
|
11360
|
+
try {
|
|
11361
|
+
const tables = new Set(db.prepare("SELECT name FROM sqlite_master WHERE type = 'table'").all().map((row) => row.name));
|
|
11362
|
+
defaults.legacy = tables.has("memory_spaces") && Boolean(db.prepare("SELECT 1 FROM memory_spaces LIMIT 1").get());
|
|
11363
|
+
if (!tables.has("stratagate_dsh_settings")) return defaults;
|
|
11364
|
+
const settings = new Map(db.prepare("SELECT key, value FROM stratagate_dsh_settings WHERE key IN ('graphEnabled', 'graphControl')").all().map((row) => [row.key, row.value]));
|
|
11365
|
+
return { ...defaults, ...JSON.parse(settings.get("graphControl") ?? "{}"), enabled: settings.get("graphEnabled") === "true" };
|
|
11366
|
+
} finally {
|
|
11367
|
+
db.close();
|
|
11368
|
+
}
|
|
11369
|
+
}
|
|
11056
11370
|
var TOPIC_BOOTSTRAP_WINDOW_MS = 10 * 6e4;
|
|
11057
11371
|
var TOPIC_BOOTSTRAP_WINDOW_CALLS = 2;
|
|
11058
11372
|
var METADATA_SCHEMA = `
|
|
@@ -11088,8 +11402,47 @@ function dropLegacyAgentMemoriesTable(filename) {
|
|
|
11088
11402
|
var DshMetadataStore = class {
|
|
11089
11403
|
database;
|
|
11090
11404
|
constructor(filename) {
|
|
11091
|
-
this.database = new DatabaseSync3(filename);
|
|
11405
|
+
this.database = new DatabaseSync3(filename, { timeout: 5e3 });
|
|
11406
|
+
this.database.exec("PRAGMA busy_timeout = 5000");
|
|
11092
11407
|
this.database.exec(METADATA_SCHEMA);
|
|
11408
|
+
this.database.exec("BEGIN IMMEDIATE");
|
|
11409
|
+
try {
|
|
11410
|
+
if (!this.settingValue("graphPolicyMigration")) {
|
|
11411
|
+
const exists2 = this.database.prepare("SELECT 1 FROM sqlite_master WHERE name = 'memory_spaces'").get();
|
|
11412
|
+
const legacy = Boolean(exists2 && this.database.prepare("SELECT 1 FROM memory_spaces LIMIT 1").get());
|
|
11413
|
+
const enabled = this.settingValue("graphEnabled") === "true";
|
|
11414
|
+
if (this.settingValue("graphEnabled") === null) this.setSettingValue("graphEnabled", "false");
|
|
11415
|
+
if (this.settingValue("graphControl") === null) this.setSettingValue("graphControl", JSON.stringify({ paused: false, epoch: 0, legacy, noticeSeen: false, everEnabled: enabled, approved: {} }));
|
|
11416
|
+
this.setSettingValue("graphPolicyMigration", "1");
|
|
11417
|
+
}
|
|
11418
|
+
this.database.exec("COMMIT");
|
|
11419
|
+
} catch (error) {
|
|
11420
|
+
this.database.exec("ROLLBACK");
|
|
11421
|
+
throw error;
|
|
11422
|
+
}
|
|
11423
|
+
}
|
|
11424
|
+
settingValue(key) {
|
|
11425
|
+
return this.database.prepare("SELECT value FROM stratagate_dsh_settings WHERE key = ?").get(key)?.value ?? null;
|
|
11426
|
+
}
|
|
11427
|
+
graphSettings() {
|
|
11428
|
+
const control = JSON.parse(this.settingValue("graphControl") ?? "{}");
|
|
11429
|
+
return { ...control, enabled: this.settingValue("graphEnabled") === "true" };
|
|
11430
|
+
}
|
|
11431
|
+
updateGraphSettings(expectedEpoch, update) {
|
|
11432
|
+
this.database.exec("BEGIN IMMEDIATE");
|
|
11433
|
+
try {
|
|
11434
|
+
const current = this.graphSettings();
|
|
11435
|
+
if (current.epoch !== expectedEpoch) throw new Error("Graph settings changed; refresh and confirm again");
|
|
11436
|
+
const next = { ...current, ...update };
|
|
11437
|
+
this.setSettingValue("graphEnabled", String(next.enabled));
|
|
11438
|
+
const { enabled: _enabled, ...control } = next;
|
|
11439
|
+
this.setSettingValue("graphControl", JSON.stringify(control));
|
|
11440
|
+
this.database.exec("COMMIT");
|
|
11441
|
+
return next;
|
|
11442
|
+
} catch (error) {
|
|
11443
|
+
this.database.exec("ROLLBACK");
|
|
11444
|
+
throw error;
|
|
11445
|
+
}
|
|
11093
11446
|
}
|
|
11094
11447
|
/** Database-wide, durable reservation before any historical model attempt.
|
|
11095
11448
|
* Crashes or claim conflicts can waste a slot, but never refund a paid call.
|
|
@@ -11218,8 +11571,8 @@ var DshMetadataStore = class {
|
|
|
11218
11571
|
};
|
|
11219
11572
|
|
|
11220
11573
|
// src/runtime.ts
|
|
11221
|
-
import { createHash as createHash5, randomUUID as
|
|
11222
|
-
import { existsSync } from "node:fs";
|
|
11574
|
+
import { createHash as createHash5, randomUUID as randomUUID7 } from "node:crypto";
|
|
11575
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
11223
11576
|
import { dirname, resolve as resolve2 } from "node:path";
|
|
11224
11577
|
import { DatabaseSync as DatabaseSync5 } from "node:sqlite";
|
|
11225
11578
|
import { setTimeout as delay } from "node:timers/promises";
|
|
@@ -11347,7 +11700,7 @@ var TurnFolder = class {
|
|
|
11347
11700
|
};
|
|
11348
11701
|
|
|
11349
11702
|
// src/event-duplicate-maintenance.ts
|
|
11350
|
-
import { randomUUID as
|
|
11703
|
+
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
11351
11704
|
import { DatabaseSync as DatabaseSync4 } from "node:sqlite";
|
|
11352
11705
|
var LEASE_MS = 18e4;
|
|
11353
11706
|
function repairSummary(state) {
|
|
@@ -11496,7 +11849,7 @@ var EventDuplicateMaintenance = class {
|
|
|
11496
11849
|
if (keepId && keepId !== pair.leftId && keepId !== pair.rightId) throw new Error("\u4FDD\u7559\u5361\u4E0D\u5C5E\u4E8E\u5F53\u524D\u5019\u9009");
|
|
11497
11850
|
if (!keepId) {
|
|
11498
11851
|
const { undo: _undo, manualResolution: _intent, ...original } = structuredClone(pair);
|
|
11499
|
-
pair.undo = { id:
|
|
11852
|
+
pair.undo = { id: randomUUID6(), previous: original };
|
|
11500
11853
|
} else {
|
|
11501
11854
|
const previous = structuredClone(state);
|
|
11502
11855
|
pair.manualResolution = { mode, keepId };
|
|
@@ -11504,7 +11857,7 @@ var EventDuplicateMaintenance = class {
|
|
|
11504
11857
|
this.save(namespace, owner, state);
|
|
11505
11858
|
if (!await archive(expected, keepId, (data) => {
|
|
11506
11859
|
const { undo: _undo, manualResolution: _intent, ...original } = previous.pairs.find((value) => value.leftId === pair.leftId && value.rightId === pair.rightId);
|
|
11507
|
-
pair.undo = { id:
|
|
11860
|
+
pair.undo = { id: randomUUID6(), data, previous: original };
|
|
11508
11861
|
this.save(namespace, owner, state);
|
|
11509
11862
|
})) {
|
|
11510
11863
|
this.save(namespace, owner, previous);
|
|
@@ -11593,7 +11946,7 @@ var EventDuplicateMaintenance = class {
|
|
|
11593
11946
|
}
|
|
11594
11947
|
}
|
|
11595
11948
|
claim(namespace) {
|
|
11596
|
-
const owner =
|
|
11949
|
+
const owner = randomUUID6();
|
|
11597
11950
|
return this.database.prepare(`UPDATE stratagate_dsh_event_duplicate_repair SET lease_owner = ?, lease_until = ?
|
|
11598
11951
|
WHERE namespace = ? AND lease_until <= ?`).run(owner, Date.now() + LEASE_MS, namespace, Date.now()).changes ? owner : null;
|
|
11599
11952
|
}
|
|
@@ -11687,7 +12040,7 @@ var EventDuplicateMaintenance = class {
|
|
|
11687
12040
|
VALUES (?, ?, ?) ON CONFLICT(namespace) DO UPDATE SET version = excluded.version, state_json = excluded.state_json,
|
|
11688
12041
|
lease_owner = NULL, lease_until = 0 WHERE stratagate_dsh_event_duplicate_repair.version < excluded.version`).run(namespace, state.version, JSON.stringify(state));
|
|
11689
12042
|
}
|
|
11690
|
-
const owner =
|
|
12043
|
+
const owner = randomUUID6();
|
|
11691
12044
|
const claim = this.database.prepare(`UPDATE stratagate_dsh_event_duplicate_repair SET lease_owner = ?, lease_until = ?
|
|
11692
12045
|
WHERE namespace = ? AND version = ? AND lease_until <= ?`).run(owner, Date.now() + LEASE_MS, namespace, EVENT_DUPLICATE_REPAIR_VERSION, Date.now());
|
|
11693
12046
|
if (!claim.changes) return false;
|
|
@@ -11768,7 +12121,7 @@ var EventDuplicateMaintenance = class {
|
|
|
11768
12121
|
pair.manualResolution = { mode: "merge", keepId, actor: "ai" };
|
|
11769
12122
|
this.save(namespace, owner, state);
|
|
11770
12123
|
pair.outcome = await operations.archive(pair, keepId, pair.draft, (data) => {
|
|
11771
|
-
pair.undo = { id:
|
|
12124
|
+
pair.undo = { id: randomUUID6(), data, previous };
|
|
11772
12125
|
this.save(namespace, owner, state);
|
|
11773
12126
|
}) ? "merged" : "kept";
|
|
11774
12127
|
delete pair.manualResolution;
|
|
@@ -11967,7 +12320,7 @@ var StrataGateRuntime = class {
|
|
|
11967
12320
|
externalImportRuns = /* @__PURE__ */ new Map();
|
|
11968
12321
|
dreamingRuns = /* @__PURE__ */ new Map();
|
|
11969
12322
|
dreamingControllers = /* @__PURE__ */ new Map();
|
|
11970
|
-
dreamingOwner =
|
|
12323
|
+
dreamingOwner = randomUUID7();
|
|
11971
12324
|
dreamingHost;
|
|
11972
12325
|
adminJobRetryRuns = /* @__PURE__ */ new Map();
|
|
11973
12326
|
feedbackDrafts = /* @__PURE__ */ new Map();
|
|
@@ -11990,6 +12343,67 @@ var StrataGateRuntime = class {
|
|
|
11990
12343
|
blockTurnSize;
|
|
11991
12344
|
blockDecayLambda;
|
|
11992
12345
|
agentMemoryRetrievalWeight;
|
|
12346
|
+
graphMetadata;
|
|
12347
|
+
graphPreviews = /* @__PURE__ */ new Map();
|
|
12348
|
+
graphStore() {
|
|
12349
|
+
if (!this.graphMetadata) {
|
|
12350
|
+
this.profileStore();
|
|
12351
|
+
this.graphMetadata = new DshMetadataStore(this.config.database);
|
|
12352
|
+
}
|
|
12353
|
+
return this.graphMetadata;
|
|
12354
|
+
}
|
|
12355
|
+
graphSettings() {
|
|
12356
|
+
return this.graphMetadata?.graphSettings() ?? readGraphSettings(this.config.database);
|
|
12357
|
+
}
|
|
12358
|
+
get graphEnabled() {
|
|
12359
|
+
return this.graphSettings().enabled;
|
|
12360
|
+
}
|
|
12361
|
+
onGraphAvailabilityChanged(listener) {
|
|
12362
|
+
this.graphListeners.add(listener);
|
|
12363
|
+
return () => this.graphListeners.delete(listener);
|
|
12364
|
+
}
|
|
12365
|
+
graphListeners = /* @__PURE__ */ new Set();
|
|
12366
|
+
graphPolicyFor(namespace) {
|
|
12367
|
+
const state = this.graphSettings();
|
|
12368
|
+
return { enabled: state.enabled, paused: state.paused, epoch: state.epoch, backfillEventIds: state.approved[namespace] ?? [] };
|
|
12369
|
+
}
|
|
12370
|
+
adminGraphSettings() {
|
|
12371
|
+
const { approved: _approved, ...state } = this.graphSettings();
|
|
12372
|
+
return state;
|
|
12373
|
+
}
|
|
12374
|
+
async adminGraphControl(action, token) {
|
|
12375
|
+
const store = this.graphStore();
|
|
12376
|
+
const state = store.graphSettings();
|
|
12377
|
+
if (action === "preview") {
|
|
12378
|
+
const approved = {};
|
|
12379
|
+
let count = 0;
|
|
12380
|
+
for (const { namespace, snapshot } of await this.adminSnapshotEntries()) {
|
|
12381
|
+
const completed = new Set(snapshot.graphProjectionJobs.filter((job) => job.status === "completed" && job.projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION).flatMap((job) => job.sourceEventIds));
|
|
12382
|
+
approved[namespace] = [...snapshot.events, ...snapshot.agentEvents].filter((event) => event.status !== "forgotten" && event.status !== "archived" && !completed.has(event.id)).map((event) => event.id);
|
|
12383
|
+
count += approved[namespace].length;
|
|
12384
|
+
}
|
|
12385
|
+
const id = randomUUID7();
|
|
12386
|
+
this.graphPreviews.clear();
|
|
12387
|
+
this.graphPreviews.set(id, { epoch: state.epoch, approved, count });
|
|
12388
|
+
return { token: id, pending: count, batches: Object.values(approved).reduce((sum, ids2) => sum + Math.ceil(ids2.length / 8), 0), epoch: state.epoch };
|
|
12389
|
+
}
|
|
12390
|
+
if (action === "notice") {
|
|
12391
|
+
store.updateGraphSettings(state.epoch, { noticeSeen: true });
|
|
12392
|
+
} else if (action === "enable" || action === "resume") {
|
|
12393
|
+
const preview = token ? this.graphPreviews.get(token) : void 0;
|
|
12394
|
+
if (!preview || preview.epoch !== state.epoch) throw new Error("\u8BF7\u5148\u7EDF\u8BA1\u5F85\u5904\u7406\u6570\u91CF\u5E76\u786E\u8BA4\u5F00\u542F");
|
|
12395
|
+
store.updateGraphSettings(state.epoch, { enabled: true, paused: false, epoch: state.epoch + 1, everEnabled: true, approved: preview.approved });
|
|
12396
|
+
this.graphPreviews.delete(token);
|
|
12397
|
+
} else if (action === "disable" || action === "pause") {
|
|
12398
|
+
store.updateGraphSettings(state.epoch, { enabled: action === "pause" && state.enabled, paused: action === "pause", epoch: state.epoch + 1 });
|
|
12399
|
+
for (const timer of this.migrationTimers.values()) clearTimeout(timer);
|
|
12400
|
+
this.migrationTimers.clear();
|
|
12401
|
+
} else throw new Error("Invalid Graph control action");
|
|
12402
|
+
this.profileStore().rebuildBackgroundWorkSchedule();
|
|
12403
|
+
for (const listener of this.graphListeners) listener();
|
|
12404
|
+
this.wakeBackgroundWorker();
|
|
12405
|
+
return this.adminGraphSettings();
|
|
12406
|
+
}
|
|
11993
12407
|
get agentMemoryEnabled() {
|
|
11994
12408
|
return this.config.agentMemoryEnabled !== false;
|
|
11995
12409
|
}
|
|
@@ -12019,7 +12433,7 @@ var StrataGateRuntime = class {
|
|
|
12019
12433
|
if (this.config.database === ":memory:") {
|
|
12020
12434
|
namespaces = [...this.spaces.keys()];
|
|
12021
12435
|
} else {
|
|
12022
|
-
if (!
|
|
12436
|
+
if (!existsSync2(this.config.database)) return;
|
|
12023
12437
|
const storage = this.profileStore();
|
|
12024
12438
|
if (await storage.rebuildBackgroundWorkIndex(8, (error) => this.onIngestError(error))) this.backgroundWorkerWakePending = true;
|
|
12025
12439
|
const metadata = new DshMetadataStore(this.config.database);
|
|
@@ -12083,7 +12497,7 @@ var StrataGateRuntime = class {
|
|
|
12083
12497
|
/** Upgrade repair: one durable scan per namespace; bounded independent background work. */
|
|
12084
12498
|
async runEventDuplicateMaintenance() {
|
|
12085
12499
|
if (this.duplicateMaintenanceRun) return this.duplicateMaintenanceRun;
|
|
12086
|
-
if (this.closed || this.config.database === ":memory:" || !
|
|
12500
|
+
if (this.closed || this.config.database === ":memory:" || !existsSync2(this.config.database)) return false;
|
|
12087
12501
|
const canDecide = this.models.isReady() && typeof this.models.eventDuplicateDecider === "function";
|
|
12088
12502
|
const run = (async () => {
|
|
12089
12503
|
for (const namespace of canDecide ? await this.adminNamespaces() : EventDuplicateMaintenance.pendingManualNamespaces(this.config.database)) {
|
|
@@ -12137,7 +12551,7 @@ var StrataGateRuntime = class {
|
|
|
12137
12551
|
}
|
|
12138
12552
|
adminDuplicateRepairStatus(namespace) {
|
|
12139
12553
|
if (this.duplicateMaintenance) return this.duplicateMaintenance.summary(namespace);
|
|
12140
|
-
if (this.config.database === ":memory:" || !
|
|
12554
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
12141
12555
|
return EventDuplicateMaintenance.readSummary(this.config.database, namespace);
|
|
12142
12556
|
}
|
|
12143
12557
|
async adminDuplicateRepairPairs(namespace, offset = 0, limit = 20, queue = "all", focus) {
|
|
@@ -12276,7 +12690,7 @@ var StrataGateRuntime = class {
|
|
|
12276
12690
|
return result;
|
|
12277
12691
|
}
|
|
12278
12692
|
profileStore() {
|
|
12279
|
-
return this.profileStorage ??= new SqliteStorage({ filename: this.config.database });
|
|
12693
|
+
return this.profileStorage ??= new SqliteStorage({ filename: this.config.database, graphPolicy: (namespace) => this.graphPolicyFor(namespace) });
|
|
12280
12694
|
}
|
|
12281
12695
|
updatePersistentProfileFromTool(field, value) {
|
|
12282
12696
|
return this.updatePersistentProfile(field, value, "agent_tool");
|
|
@@ -12292,10 +12706,11 @@ var StrataGateRuntime = class {
|
|
|
12292
12706
|
try {
|
|
12293
12707
|
await this.refreshExternalImportMemory(namespace, memory);
|
|
12294
12708
|
const canProjectTopics = typeof this.models.topicProjector === "function";
|
|
12709
|
+
const graphPolicy = this.graphPolicyFor(namespace);
|
|
12295
12710
|
const runnable = memory.hasSealableTail() || [
|
|
12296
12711
|
...memory.listSummaryJobs(),
|
|
12297
12712
|
...memory.listExtractionJobs()
|
|
12298
|
-
].some((job) => job.status === "running" && (!job.leaseUntil || Date.parse(job.leaseUntil) <= Date.now()) || job.status === "pending" || job.status === "failed" && job.nextRetryAt !== null && Date.parse(job.nextRetryAt) <= Date.now()) || memory.listGraphProjectionJobs().some((job) => graphProjectionCanRun(job)) || memory.listGraphProjectionJobs().some((job) => job.status === "running" && (!job.leaseUntil || Date.parse(job.leaseUntil) <= Date.now())) || memory.listBlocks().some((block) => block.processingStatus === "pending" && memory.listSummaryJobs().some((job) => job.blockId === block.id && job.status === "succeeded") && !memory.listExtractionJobs().some((job) => job.blockId === block.id)) || canProjectTopics && memory.hasPendingTopicWork();
|
|
12713
|
+
].some((job) => job.status === "running" && (!job.leaseUntil || Date.parse(job.leaseUntil) <= Date.now()) || job.status === "pending" || job.status === "failed" && job.nextRetryAt !== null && Date.parse(job.nextRetryAt) <= Date.now()) || graphPolicy.enabled && !graphPolicy.paused && (memory.listGraphProjectionJobs().some((job) => graphProjectionCanRun(job) || job.claimEpoch !== void 0 && job.claimEpoch !== graphPolicy.epoch && job.status !== "completed" && job.lastError !== "Graph claim revoked" && job.attempts < DERIVATION_MAX_ATTEMPTS) || memory.graphBackfillEventIds().some((id) => graphPolicy.backfillEventIds?.includes(id)) || memory.listGraphProjectionJobs().some((job) => job.status === "running" && (!job.leaseUntil || Date.parse(job.leaseUntil) <= Date.now()))) || memory.listBlocks().some((block) => block.processingStatus === "pending" && memory.listSummaryJobs().some((job) => job.blockId === block.id && job.status === "succeeded") && !memory.listExtractionJobs().some((job) => job.blockId === block.id)) || canProjectTopics && memory.hasPendingTopicWork();
|
|
12299
12714
|
if (!runnable) return;
|
|
12300
12715
|
const hasActiveSessionWork = [
|
|
12301
12716
|
...this.derivationTimers.keys(),
|
|
@@ -12565,7 +12980,7 @@ var StrataGateRuntime = class {
|
|
|
12565
12980
|
await this.refreshExternalImportMemory(namespace, memory);
|
|
12566
12981
|
return renderMemoryDirectory(this.visibleTopics(memory), memory.listAllEvents());
|
|
12567
12982
|
}
|
|
12568
|
-
if (this.config.database === ":memory:" || !
|
|
12983
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return "";
|
|
12569
12984
|
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
12570
12985
|
try {
|
|
12571
12986
|
const memory = await StrataGate.openWithStorage({ storage, namespace });
|
|
@@ -12692,8 +13107,12 @@ var StrataGateRuntime = class {
|
|
|
12692
13107
|
}], result);
|
|
12693
13108
|
}
|
|
12694
13109
|
async expandElement(session, id, at) {
|
|
13110
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
12695
13111
|
await this.flush();
|
|
12696
|
-
|
|
13112
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
13113
|
+
const memory = await this.space(session);
|
|
13114
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
13115
|
+
const result = memory.expandElement(id, at);
|
|
12697
13116
|
return this.batch(session, [{
|
|
12698
13117
|
ref: `element:${result.id}`,
|
|
12699
13118
|
target: {
|
|
@@ -12885,9 +13304,9 @@ var StrataGateRuntime = class {
|
|
|
12885
13304
|
agentMemoryWeight: this.agentMemoryRetrievalWeight
|
|
12886
13305
|
}) : [];
|
|
12887
13306
|
let graphResults = [];
|
|
12888
|
-
if (activationQuery && typeof memory.searchGraphNodes === "function") {
|
|
13307
|
+
if (this.graphEnabled && activationQuery && typeof memory.searchGraphNodes === "function") {
|
|
12889
13308
|
graphResults = await memory.searchGraphNodes(activationQuery, 12);
|
|
12890
|
-
} else if (activationQuery) {
|
|
13309
|
+
} else if (this.graphEnabled && activationQuery) {
|
|
12891
13310
|
const legacy = activatedElements(memory, await memory.searchElements(activationQuery, { limit: 12 }));
|
|
12892
13311
|
graphResults = legacy.map((item) => ({
|
|
12893
13312
|
node: {
|
|
@@ -12913,7 +13332,7 @@ var StrataGateRuntime = class {
|
|
|
12913
13332
|
const currentEventIds = new Set(memory.listAllEvents().filter((event) => event.sourceBlockId !== void 0 && currentBlockIds.has(event.sourceBlockId)).map(({ id }) => id));
|
|
12914
13333
|
const longTermEvents = events.filter((event) => !currentEventIds.has(event.id)).slice(0, AUTO_EVENT_LIMIT);
|
|
12915
13334
|
graphResults = graphResults.filter((result) => !(result.provenanceEventIds ?? result.node.sourceEventIds).some((id) => currentEventIds.has(id))).slice(0, AUTO_ELEMENT_LIMIT);
|
|
12916
|
-
return renderActivatedMemory(longTermEvents, graphResults);
|
|
13335
|
+
return renderActivatedMemory(longTermEvents, this.graphEnabled ? graphResults : []);
|
|
12917
13336
|
}
|
|
12918
13337
|
/**
|
|
12919
13338
|
* Replace the just-sealed DSH turns with one native surface message. The raw
|
|
@@ -13054,6 +13473,7 @@ var StrataGateRuntime = class {
|
|
|
13054
13473
|
this.diagnostics.close();
|
|
13055
13474
|
await Promise.all(settled.flatMap((result) => result.status === "fulfilled" ? [result.value.close()] : []));
|
|
13056
13475
|
if (this.profileStorage) await this.profileStorage.close();
|
|
13476
|
+
this.graphMetadata?.close();
|
|
13057
13477
|
this.sessionsById.clear();
|
|
13058
13478
|
if (flushError !== void 0) throw flushError;
|
|
13059
13479
|
}
|
|
@@ -13256,7 +13676,7 @@ var StrataGateRuntime = class {
|
|
|
13256
13676
|
loadFeedbackDraft(namespace) {
|
|
13257
13677
|
const cached = this.feedbackDrafts.get(namespace);
|
|
13258
13678
|
if (cached) return structuredClone(cached);
|
|
13259
|
-
if (this.config.database === ":memory:" || !
|
|
13679
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
13260
13680
|
const metadata = new DshMetadataStore(this.config.database);
|
|
13261
13681
|
try {
|
|
13262
13682
|
const value = metadata.feedbackDraft(namespace);
|
|
@@ -13280,7 +13700,7 @@ var StrataGateRuntime = class {
|
|
|
13280
13700
|
this.feedbackDrafts.set(namespace, structuredClone(draft));
|
|
13281
13701
|
}
|
|
13282
13702
|
async adminNamespaces() {
|
|
13283
|
-
if (this.config.database === ":memory:" || !
|
|
13703
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return [];
|
|
13284
13704
|
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
13285
13705
|
try {
|
|
13286
13706
|
return storage.listNamespaces();
|
|
@@ -13289,7 +13709,7 @@ var StrataGateRuntime = class {
|
|
|
13289
13709
|
}
|
|
13290
13710
|
}
|
|
13291
13711
|
async adminSnapshotEntries() {
|
|
13292
|
-
if (this.config.database === ":memory:" || !
|
|
13712
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return [];
|
|
13293
13713
|
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
13294
13714
|
try {
|
|
13295
13715
|
const entries = [];
|
|
@@ -13326,7 +13746,7 @@ var StrataGateRuntime = class {
|
|
|
13326
13746
|
}
|
|
13327
13747
|
}
|
|
13328
13748
|
async syncConfiguredSettings() {
|
|
13329
|
-
if (this.config.database === ":memory:" || !
|
|
13749
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return;
|
|
13330
13750
|
const metadata = new DshMetadataStore(this.config.database);
|
|
13331
13751
|
try {
|
|
13332
13752
|
this.blockTurnSize = metadata.blockTurnSize() ?? this.config.blockTurnSize;
|
|
@@ -13353,7 +13773,7 @@ var StrataGateRuntime = class {
|
|
|
13353
13773
|
async adminSnapshot(namespace) {
|
|
13354
13774
|
const key = namespace.trim();
|
|
13355
13775
|
if (!key) throw new TypeError("StrataGate admin namespace must not be empty");
|
|
13356
|
-
if (this.config.database === ":memory:" || !
|
|
13776
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
13357
13777
|
const opening = this.spaces.get(key);
|
|
13358
13778
|
if (opening) {
|
|
13359
13779
|
const memory = await opening;
|
|
@@ -13394,12 +13814,12 @@ var StrataGateRuntime = class {
|
|
|
13394
13814
|
importedCount: job.importedCount,
|
|
13395
13815
|
createdAt: job.createdAt,
|
|
13396
13816
|
updatedAt: job.updatedAt,
|
|
13397
|
-
decisions: job.decisions
|
|
13817
|
+
decisions: job.decisions,
|
|
13398
13818
|
requiresConfirmationCount: job.decisions.filter(({ requiresConfirmation }) => requiresConfirmation).length
|
|
13399
13819
|
};
|
|
13400
13820
|
}
|
|
13401
13821
|
async refreshExternalImportMemory(namespace, memory) {
|
|
13402
|
-
if (this.config.database === ":memory:" || !
|
|
13822
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return;
|
|
13403
13823
|
const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
|
|
13404
13824
|
try {
|
|
13405
13825
|
const revision = storage.namespaceRevision(namespace);
|
|
@@ -13746,6 +14166,12 @@ var StrataGateRuntime = class {
|
|
|
13746
14166
|
}
|
|
13747
14167
|
throw lastConflict;
|
|
13748
14168
|
}
|
|
14169
|
+
async adminAbandonExternalMemory(namespace, jobId) {
|
|
14170
|
+
const key = namespace.trim();
|
|
14171
|
+
if (!key) throw new TypeError("StrataGate admin namespace must not be empty");
|
|
14172
|
+
const job = await this.retryExternalImportWrite(key, (memory) => memory.abandonExternalMemoryImportJob(jobId));
|
|
14173
|
+
return this.externalImportView(job);
|
|
14174
|
+
}
|
|
13749
14175
|
async adminCommitExternalMemory(namespace, jobId, choices) {
|
|
13750
14176
|
const key = namespace.trim();
|
|
13751
14177
|
const { memory, owned } = await this.openAdminMemory(key);
|
|
@@ -13806,7 +14232,7 @@ var StrataGateRuntime = class {
|
|
|
13806
14232
|
adminWorkspaceName(namespace) {
|
|
13807
14233
|
const remembered = this.workspaceNames.get(namespace);
|
|
13808
14234
|
if (remembered) return remembered;
|
|
13809
|
-
if (this.config.database === ":memory:" || !
|
|
14235
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return null;
|
|
13810
14236
|
const database = new DatabaseSync5(this.config.database, { readOnly: true });
|
|
13811
14237
|
try {
|
|
13812
14238
|
if (!database.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'stratagate_dsh_workspaces'").get()) {
|
|
@@ -13818,10 +14244,6 @@ var StrataGateRuntime = class {
|
|
|
13818
14244
|
database.close();
|
|
13819
14245
|
}
|
|
13820
14246
|
}
|
|
13821
|
-
async adminSettings() {
|
|
13822
|
-
await this.settingsTail;
|
|
13823
|
-
return { blockTurnSize: this.blockTurnSize, blockDecayLambda: this.blockDecayLambda, agentMemoryRetrievalWeight: this.agentMemoryRetrievalWeight };
|
|
13824
|
-
}
|
|
13825
14247
|
async adminSetBlockDecayLambda(value) {
|
|
13826
14248
|
if (!Number.isFinite(value) || value < 0) {
|
|
13827
14249
|
throw new TypeError("blockDecayLambda must be a non-negative finite number");
|
|
@@ -13880,6 +14302,7 @@ var StrataGateRuntime = class {
|
|
|
13880
14302
|
return { opened: true, path };
|
|
13881
14303
|
}
|
|
13882
14304
|
async adminExpandBlock(namespace, id, target) {
|
|
14305
|
+
this.graphStore();
|
|
13883
14306
|
const key = namespace.trim();
|
|
13884
14307
|
if (!key) throw new TypeError("StrataGate admin namespace must not be empty");
|
|
13885
14308
|
const update = this.settingsTail.catch(() => {
|
|
@@ -13887,12 +14310,13 @@ var StrataGateRuntime = class {
|
|
|
13887
14310
|
await this.flush();
|
|
13888
14311
|
const active = this.spaces.get(key);
|
|
13889
14312
|
if (active) return (await active).expandBlock(id, target, "user");
|
|
13890
|
-
if (this.config.database === ":memory:" || !
|
|
14313
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) {
|
|
13891
14314
|
throw new Error(`Unknown StrataGate namespace: ${key}`);
|
|
13892
14315
|
}
|
|
13893
14316
|
const memory = await StrataGate.open({
|
|
13894
14317
|
database: this.config.database,
|
|
13895
14318
|
namespace: key,
|
|
14319
|
+
graphPolicy: () => this.graphPolicyFor(key),
|
|
13896
14320
|
blockTurnSize: this.blockTurnSize,
|
|
13897
14321
|
blockDecayLambda: this.blockDecayLambda,
|
|
13898
14322
|
summarizer: this.models.summarizer,
|
|
@@ -13913,6 +14337,7 @@ var StrataGateRuntime = class {
|
|
|
13913
14337
|
return update;
|
|
13914
14338
|
}
|
|
13915
14339
|
async adminRetryJob(namespace, kind, id) {
|
|
14340
|
+
if (kind === "graph-projection" && (!this.graphEnabled || this.graphStore().graphSettings().paused)) throw new Error("Knowledge Graph processing is disabled or paused");
|
|
13916
14341
|
const key = namespace.trim();
|
|
13917
14342
|
const jobId = id.trim();
|
|
13918
14343
|
if (!key) throw new TypeError("StrataGate admin namespace must not be empty");
|
|
@@ -13928,6 +14353,7 @@ var StrataGateRuntime = class {
|
|
|
13928
14353
|
try {
|
|
13929
14354
|
await this.refreshExternalImportMemory(key, memory);
|
|
13930
14355
|
const graphJob = kind === "graph-projection" ? memory.listGraphProjectionJobs().find((candidate) => candidate.id === jobId) : void 0;
|
|
14356
|
+
const graphJobIdsBeforeRetry = kind === "graph-projection" ? new Set(memory.listGraphProjectionJobs().map((candidate) => candidate.id)) : void 0;
|
|
13931
14357
|
const blockId = kind === "graph-projection" ? graphJob?.sourceEventIds.flatMap((eventId) => memory.listAllEvents().find(({ id: eventCandidateId }) => eventCandidateId === eventId)?.sourceBlockId ?? [])[0] : jobId;
|
|
13932
14358
|
const block = blockId ? memory.listBlocks().find((candidate) => candidate.id === blockId) : void 0;
|
|
13933
14359
|
if (kind !== "graph-projection" && !block) throw new Error(`Unknown block: ${jobId}`);
|
|
@@ -13949,12 +14375,13 @@ var StrataGateRuntime = class {
|
|
|
13949
14375
|
this.pendingSurfaceSync.set(`${key}\0${currentBlock.id}`, { namespace: key, blockId: currentBlock.id });
|
|
13950
14376
|
if (session && this.namespaceFor(session) === key) await this.syncPendingRetrySurface(session, memory);
|
|
13951
14377
|
}
|
|
13952
|
-
const job = kind === "block-summary" ? memory.listSummaryJobs().find(({ blockId: candidateId }) => candidateId === jobId) : kind === "event-extraction" ? memory.listExtractionJobs().find(({ blockId: candidateId }) => candidateId === jobId) : memory.listGraphProjectionJobs().find(({ id: candidateId }) => candidateId === jobId);
|
|
14378
|
+
const job = kind === "block-summary" ? memory.listSummaryJobs().find(({ blockId: candidateId }) => candidateId === jobId) : kind === "event-extraction" ? memory.listExtractionJobs().find(({ blockId: candidateId }) => candidateId === jobId) : memory.listGraphProjectionJobs().find((candidate) => graphJobIdsBeforeRetry !== void 0 && !graphJobIdsBeforeRetry.has(candidate.id)) ?? memory.listGraphProjectionJobs().find(({ id: candidateId }) => candidateId === jobId);
|
|
14379
|
+
const effectiveJobId = kind === "graph-projection" && job && "id" in job ? job.id : jobId;
|
|
13953
14380
|
const succeeded = kind === "graph-projection" ? job?.status === "completed" : job?.status === "succeeded" || job?.status === "skipped";
|
|
13954
14381
|
if (!succeeded) throw new Error(job?.lastError || `${kind} retry did not complete`);
|
|
13955
14382
|
return {
|
|
13956
14383
|
kind,
|
|
13957
|
-
jobId,
|
|
14384
|
+
jobId: effectiveJobId,
|
|
13958
14385
|
blockId,
|
|
13959
14386
|
status: job?.status,
|
|
13960
14387
|
job: job ?? null,
|
|
@@ -14017,7 +14444,7 @@ var StrataGateRuntime = class {
|
|
|
14017
14444
|
await memory.setBlockDecayLambda(value);
|
|
14018
14445
|
openNamespaces.add(namespace);
|
|
14019
14446
|
}
|
|
14020
|
-
if (this.config.database !== ":memory:" &&
|
|
14447
|
+
if (this.config.database !== ":memory:" && existsSync2(this.config.database)) {
|
|
14021
14448
|
const storage = new SqliteStorage({ filename: this.config.database });
|
|
14022
14449
|
try {
|
|
14023
14450
|
for (const namespace of storage.listNamespaces()) {
|
|
@@ -14048,7 +14475,7 @@ var StrataGateRuntime = class {
|
|
|
14048
14475
|
await memory.setBlockTurnSize(value);
|
|
14049
14476
|
openNamespaces.add(namespace);
|
|
14050
14477
|
}
|
|
14051
|
-
if (this.config.database !== ":memory:" &&
|
|
14478
|
+
if (this.config.database !== ":memory:" && existsSync2(this.config.database)) {
|
|
14052
14479
|
const storage = new SqliteStorage({ filename: this.config.database });
|
|
14053
14480
|
try {
|
|
14054
14481
|
for (const namespace of storage.listNamespaces()) {
|
|
@@ -14063,6 +14490,7 @@ var StrataGateRuntime = class {
|
|
|
14063
14490
|
}
|
|
14064
14491
|
}
|
|
14065
14492
|
async space(session) {
|
|
14493
|
+
this.graphStore();
|
|
14066
14494
|
const namespace = this.namespaceFor(session);
|
|
14067
14495
|
this.knownSessions.set(String(session.id), new WeakRef(session));
|
|
14068
14496
|
this.rememberWorkspace(namespace, session.header.cwd);
|
|
@@ -14071,6 +14499,7 @@ var StrataGateRuntime = class {
|
|
|
14071
14499
|
opening = StrataGate.open({
|
|
14072
14500
|
database: this.config.database,
|
|
14073
14501
|
namespace,
|
|
14502
|
+
graphPolicy: () => this.graphPolicyFor(namespace),
|
|
14074
14503
|
blockTurnSize: this.blockTurnSize,
|
|
14075
14504
|
blockDecayLambda: this.blockDecayLambda,
|
|
14076
14505
|
summarizer: this.models.summarizer,
|
|
@@ -14108,9 +14537,11 @@ var StrataGateRuntime = class {
|
|
|
14108
14537
|
return memory;
|
|
14109
14538
|
}
|
|
14110
14539
|
async searchGraph(session, query, limit = 8) {
|
|
14540
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
14111
14541
|
await this.flush();
|
|
14112
14542
|
const memory = await this.space(session);
|
|
14113
14543
|
const results = await memory.searchGraphNodes(query, limit);
|
|
14544
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
14114
14545
|
return this.batch(session, results.map(({ node, provenanceEventIds }) => ({
|
|
14115
14546
|
ref: `graph-node:${node.id}`,
|
|
14116
14547
|
target: {
|
|
@@ -14121,8 +14552,10 @@ var StrataGateRuntime = class {
|
|
|
14121
14552
|
})), results.map(compactGraphNode), searchStatus(results, () => memory.hasSearchableGraphNodes?.()));
|
|
14122
14553
|
}
|
|
14123
14554
|
async expandGraphNode(session, id) {
|
|
14555
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
14124
14556
|
await this.flush();
|
|
14125
14557
|
const memory = await this.space(session);
|
|
14558
|
+
if (!this.graphEnabled) throw new Error("Knowledge Graph is disabled");
|
|
14126
14559
|
const node = memory.listGraphNodes().find((candidate) => candidate.id === id);
|
|
14127
14560
|
if (!node) throw new Error(`Unknown graph node: ${id}`);
|
|
14128
14561
|
const view = effectiveGraphNodeView(node, memory.listGraphEdges(), memory.listAllEvents());
|
|
@@ -14221,6 +14654,7 @@ var StrataGateRuntime = class {
|
|
|
14221
14654
|
this.derivationTimers.set(key, timer);
|
|
14222
14655
|
}
|
|
14223
14656
|
scheduleGraphMigration(session, memory) {
|
|
14657
|
+
if (!this.graphEnabled || this.graphStore().graphSettings().paused) return;
|
|
14224
14658
|
const namespace = this.namespaceFor(session);
|
|
14225
14659
|
if (this.closed || !this.models.isReady(session) || this.migrationTimers.has(namespace) || this.migrationRuns.has(namespace)) return;
|
|
14226
14660
|
if (typeof memory.listGraphProjectionJobs !== "function") return;
|
|
@@ -14261,15 +14695,17 @@ var StrataGateRuntime = class {
|
|
|
14261
14695
|
if ([...this.failedCoreJobs(memory)].some((failure) => !before.has(failure))) this.notePluginError(session);
|
|
14262
14696
|
}
|
|
14263
14697
|
async openAdminMemory(namespace, options = {}) {
|
|
14698
|
+
this.graphStore();
|
|
14264
14699
|
const active = this.spaces.get(namespace);
|
|
14265
14700
|
if (active) return { memory: await active, owned: false };
|
|
14266
|
-
if (this.config.database === ":memory:" || !
|
|
14701
|
+
if (this.config.database === ":memory:" || !existsSync2(this.config.database)) {
|
|
14267
14702
|
throw new Error(`Unknown StrataGate namespace: ${namespace}`);
|
|
14268
14703
|
}
|
|
14269
14704
|
return {
|
|
14270
14705
|
memory: await StrataGate.open({
|
|
14271
14706
|
database: this.config.database,
|
|
14272
14707
|
namespace,
|
|
14708
|
+
graphPolicy: () => this.graphPolicyFor(namespace),
|
|
14273
14709
|
blockTurnSize: this.blockTurnSize,
|
|
14274
14710
|
blockDecayLambda: this.blockDecayLambda,
|
|
14275
14711
|
...options.derivation ? {
|
|
@@ -15267,7 +15703,7 @@ function registerMemoryTools(ctx, runtime) {
|
|
|
15267
15703
|
...args.participants ? { participants: args.participants } : {}
|
|
15268
15704
|
})
|
|
15269
15705
|
}));
|
|
15270
|
-
|
|
15706
|
+
const graphDefinitions = [defineTool2({
|
|
15271
15707
|
name: "memory_search_graph",
|
|
15272
15708
|
description: "This tool is provided by the StrataGate plugin. Search the Event-backed Knowledge Graph for current state and query-relevant history. Results explicitly label current, historical, or both; historical matches never imply current truth, disputed records remain marked, and Event evidence is bounded to the match. Endpoint names and aliases can match relations, while relation text alone is ranking context. rankScore is ranking-only, never confidence or factual accuracy." + SEARCH_RESULT_GUIDANCE,
|
|
15273
15709
|
parameters: {
|
|
@@ -15276,14 +15712,38 @@ function registerMemoryTools(ctx, runtime) {
|
|
|
15276
15712
|
},
|
|
15277
15713
|
output: jsonOutput,
|
|
15278
15714
|
execute: async (args, exec) => runtime.searchGraph(sessionOf(exec), args.query, args.limit ?? 8)
|
|
15279
|
-
})
|
|
15280
|
-
ctx.tools.register(defineTool2({
|
|
15715
|
+
}), defineTool2({
|
|
15281
15716
|
name: "memory_expand_graph_node",
|
|
15282
15717
|
description: "This tool is provided by the StrataGate plugin. Expand one Knowledge Graph node through the same Event-authoritative view as search: dynamic current state, marked disputed records, retrievable history, and bounded supporting Event evidence. Forgotten or archived Event provenance is never exposed.",
|
|
15283
15718
|
parameters: { id: { type: "string", required: true } },
|
|
15284
15719
|
output: jsonOutput,
|
|
15285
15720
|
execute: async (args, exec) => runtime.expandGraphNode(sessionOf(exec), args.id)
|
|
15286
|
-
})
|
|
15721
|
+
}), defineTool2({
|
|
15722
|
+
name: "memory_expand_element",
|
|
15723
|
+
description: "This tool is provided by the StrataGate plugin. Expand an Element card, optionally as it was at an ISO date. The result is a new evidence batch and must be assessed.",
|
|
15724
|
+
parameters: {
|
|
15725
|
+
id: { type: "string", required: true },
|
|
15726
|
+
at: { type: "string" }
|
|
15727
|
+
},
|
|
15728
|
+
output: jsonOutput,
|
|
15729
|
+
execute: async (args, exec) => runtime.expandElement(sessionOf(exec), args.id, args.at)
|
|
15730
|
+
})];
|
|
15731
|
+
let graphDisposers = [];
|
|
15732
|
+
let syncAssessmentTool = () => {
|
|
15733
|
+
};
|
|
15734
|
+
const syncGraphTools = () => {
|
|
15735
|
+
if (runtime.graphEnabled === false) {
|
|
15736
|
+
for (const dispose of graphDisposers) dispose?.();
|
|
15737
|
+
graphDisposers = [];
|
|
15738
|
+
} else if (!graphDisposers.length) graphDisposers = graphDefinitions.map((definition) => ctx.tools.register(definition));
|
|
15739
|
+
syncAssessmentTool();
|
|
15740
|
+
};
|
|
15741
|
+
syncGraphTools();
|
|
15742
|
+
runtime.onGraphAvailabilityChanged?.(syncGraphTools);
|
|
15743
|
+
ctx.on?.("system-prompt/assemble", async (_assembly, _context, next) => {
|
|
15744
|
+
syncGraphTools();
|
|
15745
|
+
return next();
|
|
15746
|
+
});
|
|
15287
15747
|
ctx.tools.register(defineTool2({
|
|
15288
15748
|
name: "memory_search_raw",
|
|
15289
15749
|
description: "This tool is provided by the StrataGate plugin. Search archived messages when summarized memories are insufficient. Returns compact raw hits (message id, blockId, excerpt, role, and time); use memory_expand_block with blockId for complete source details. By default searches the whole current namespace; use scope=session for the active thread. Returns evidence refs and batchId for assessment." + SEARCH_RESULT_GUIDANCE,
|
|
@@ -15325,34 +15785,33 @@ function registerMemoryTools(ctx, runtime) {
|
|
|
15325
15785
|
output: jsonOutput,
|
|
15326
15786
|
execute: async (args, exec) => runtime.expandEvent(sessionOf(exec), args.id)
|
|
15327
15787
|
}));
|
|
15328
|
-
|
|
15329
|
-
|
|
15330
|
-
|
|
15331
|
-
|
|
15332
|
-
|
|
15333
|
-
|
|
15334
|
-
|
|
15335
|
-
|
|
15336
|
-
|
|
15337
|
-
|
|
15338
|
-
|
|
15339
|
-
|
|
15340
|
-
|
|
15341
|
-
|
|
15342
|
-
|
|
15343
|
-
|
|
15344
|
-
|
|
15345
|
-
|
|
15346
|
-
|
|
15347
|
-
|
|
15348
|
-
|
|
15349
|
-
|
|
15350
|
-
|
|
15351
|
-
|
|
15352
|
-
|
|
15353
|
-
|
|
15354
|
-
|
|
15355
|
-
}));
|
|
15788
|
+
let assessmentDisposer;
|
|
15789
|
+
let assessmentGraphEnabled;
|
|
15790
|
+
syncAssessmentTool = () => {
|
|
15791
|
+
const enabled = runtime.graphEnabled !== false;
|
|
15792
|
+
if (assessmentGraphEnabled === enabled) return;
|
|
15793
|
+
assessmentDisposer?.();
|
|
15794
|
+
assessmentDisposer = ctx.tools.register(defineTool2({
|
|
15795
|
+
name: "memory_assess",
|
|
15796
|
+
description: "This tool is provided by the StrataGate plugin. Apply StrataGate Evidence Gate to a retrieval batch BEFORE the final user answer. Assess evidence, close all batches with memory_record_use, then output the user answer. Pass batch_id from the retrieval result; omitting it remains compatible with sequential flows and selects the latest batch. The response reports every input ref that was not adopted and why.",
|
|
15797
|
+
parameters: {
|
|
15798
|
+
batch_id: { type: "string", description: "The batchId returned by the retrieval to assess. Omit only in a strictly sequential flow." },
|
|
15799
|
+
verdict: { type: "string", enum: ["sufficient", "partial", "wrong"], required: true },
|
|
15800
|
+
evidence_refs: { type: "array", items: { type: "string" }, required: true },
|
|
15801
|
+
fit: { type: "string", required: true },
|
|
15802
|
+
missing: { type: "string", required: true },
|
|
15803
|
+
next_strategy: {
|
|
15804
|
+
type: "string",
|
|
15805
|
+
enum: enabled ? ["answer", "search_events", "expand_event", "search_graph", "expand_graph_node", "expand_element", "search_raw_memory", "expand_block"] : ["answer", "search_events", "expand_event", "search_raw_memory", "expand_block"],
|
|
15806
|
+
required: true
|
|
15807
|
+
}
|
|
15808
|
+
},
|
|
15809
|
+
output: jsonOutput,
|
|
15810
|
+
execute: async (args, exec) => runtime.assess(sessionOf(exec), args, args.batch_id)
|
|
15811
|
+
}));
|
|
15812
|
+
assessmentGraphEnabled = enabled;
|
|
15813
|
+
};
|
|
15814
|
+
syncAssessmentTool();
|
|
15356
15815
|
ctx.tools.register(defineTool2({
|
|
15357
15816
|
name: "memory_record_use",
|
|
15358
15817
|
description: "This tool is provided by the StrataGate plugin. Close one StrataGate retrieval batch BEFORE outputting the final user answer. Complete memory_assess / memory_record_use as pre-answer evidence processing for ALL batches, then generate the final user answer. Pass its batch_id and exactly the evidenceRefs from that batch actually adopted for the answer, or [] when none are used. Non-empty refs require that batch's sufficient assessment. Never write the answer first and record usage afterwards. Do not turn this tool's internal receipt or batch status into a user-facing answer. The host renders successful selections as answer-tail citations, so do not write a manual citation list. Omitting batch_id selects the latest batch for sequential compatibility.",
|
|
@@ -15990,9 +16449,9 @@ async function topicEvents(runtime, url) {
|
|
|
15990
16449
|
items: page2.map(({ id, title, status, createdAt }) => ({ id, title, status, createdAt }))
|
|
15991
16450
|
};
|
|
15992
16451
|
}
|
|
15993
|
-
function liveGraphProjection(snapshot) {
|
|
16452
|
+
function liveGraphProjection(snapshot, processing = true) {
|
|
15994
16453
|
const eventIds = new Set([...snapshot.events, ...snapshot.agentEvents].filter(({ status: status2 }) => status2 !== "forgotten" && status2 !== "archived").map(({ id }) => id));
|
|
15995
|
-
const jobs = snapshot.graphProjectionJobs.filter((
|
|
16454
|
+
const jobs = snapshot.graphProjectionJobs.filter((job) => job.lastError !== "Graph claim revoked" && (processing || job.status === "completed") && job.sourceEventIds.some((id) => eventIds.has(id)));
|
|
15996
16455
|
const status = summarizeDerivationJobs(jobs, (job) => job.status === "completed");
|
|
15997
16456
|
const projected = new Set(jobs.filter(({ status: status2, projectorVersion }) => status2 === "completed" && projectorVersion === KNOWLEDGE_GRAPH_PROJECTOR_VERSION).flatMap(({ sourceEventIds }) => sourceEventIds.filter((id) => eventIds.has(id)))).size;
|
|
15998
16457
|
const total = eventIds.size;
|
|
@@ -16121,7 +16580,8 @@ async function overview(runtime, cachedEntries) {
|
|
|
16121
16580
|
const topicStatus = summarizeDerivationJobs(topicJobs, (job) => job.status === "completed");
|
|
16122
16581
|
const summaryStatus = summarizeDerivationJobs(snapshot.summaryJobs, (job) => job.status === "succeeded");
|
|
16123
16582
|
const extractionStatus = summarizeDerivationJobs(snapshot.extractionJobs, (job) => job.status === "succeeded" || job.status === "skipped");
|
|
16124
|
-
const
|
|
16583
|
+
const policy = runtime.adminGraphSettings?.();
|
|
16584
|
+
const graphWork = liveGraphProjection(snapshot, policy ? policy.enabled && !policy.paused : true);
|
|
16125
16585
|
const graphStatus = graphWork.status;
|
|
16126
16586
|
const failedJobs = summaryStatus.terminalFailed + extractionStatus.terminalFailed + graphStatus.terminalFailed + topicStatus.terminalFailed;
|
|
16127
16587
|
const processingJobs = summaryStatus.processing + extractionStatus.processing + graphStatus.processing + topicStatus.processing;
|
|
@@ -16191,6 +16651,7 @@ async function overview(runtime, cachedEntries) {
|
|
|
16191
16651
|
return {
|
|
16192
16652
|
readonly: true,
|
|
16193
16653
|
settingsWritable: true,
|
|
16654
|
+
graphSettings: runtime.adminGraphSettings?.() ?? { enabled: false, paused: false, legacy: false, noticeSeen: true, everEnabled: false },
|
|
16194
16655
|
dataDirectory: runtime.adminDataDirectory?.() ?? null,
|
|
16195
16656
|
pluginVersion: STRATAGATE_DSH_VERSION,
|
|
16196
16657
|
harnessVersion: installedPackageVersion(["@deepseek-ai/dsh", "@deepseek-ai/dsh-session"]),
|
|
@@ -16361,6 +16822,11 @@ async function importExternalMemory(runtime, req) {
|
|
|
16361
16822
|
if (!jobId) throw new AdminHttpError(400, "jobId is required");
|
|
16362
16823
|
return runtime.adminRetryExternalMemory(namespace, jobId);
|
|
16363
16824
|
}
|
|
16825
|
+
if (operation === "abandon") {
|
|
16826
|
+
const jobId = typeof body.jobId === "string" ? body.jobId.trim() : "";
|
|
16827
|
+
if (!jobId) throw new AdminHttpError(400, "jobId is required");
|
|
16828
|
+
return runtime.adminAbandonExternalMemory(namespace, jobId);
|
|
16829
|
+
}
|
|
16364
16830
|
if (operation === "commit") {
|
|
16365
16831
|
const jobId = typeof body.jobId === "string" ? body.jobId.trim() : "";
|
|
16366
16832
|
if (!jobId) throw new AdminHttpError(400, "jobId is required");
|
|
@@ -16379,7 +16845,7 @@ async function importExternalMemory(runtime, req) {
|
|
|
16379
16845
|
if (!sourceBlockId) throw new AdminHttpError(400, "sourceBlockId is required");
|
|
16380
16846
|
return runtime.adminUndoExternalMemory(namespace, sourceBlockId);
|
|
16381
16847
|
}
|
|
16382
|
-
throw new AdminHttpError(400, "operation must be preview, status, retry, commit, or undo");
|
|
16848
|
+
throw new AdminHttpError(400, "operation must be preview, status, retry, abandon, commit, or undo");
|
|
16383
16849
|
}
|
|
16384
16850
|
function externalMemoryPrompt() {
|
|
16385
16851
|
return { prompt: EXTERNAL_MEMORY_EXPORT_PROMPT_ZH_CN, schemaVersion: "stratagate.external-memory.v2" };
|
|
@@ -16869,12 +17335,11 @@ async function dashboard(runtime, url, ifNoneMatch) {
|
|
|
16869
17335
|
const requestedNamespace = url.searchParams.get("namespace")?.trim() ?? "";
|
|
16870
17336
|
const selected = entries.find(({ namespace }) => namespace === requestedNamespace) ?? entries[0];
|
|
16871
17337
|
const threadId = url.searchParams.get("threadId")?.trim() ?? "";
|
|
16872
|
-
const region = url.searchParams.get("region") || "";
|
|
16873
17338
|
const revisionKey = entries.map(({ namespace, revision }) => `${namespace}:${revision}`).join("|");
|
|
16874
17339
|
const agentMemoryWeight = runtime.adminAgentMemoryRetrievalWeight?.() ?? 1;
|
|
16875
|
-
const duplicateKey = JSON.stringify(entries.map(({ namespace }) => runtime.adminDuplicateRepairStatus?.(namespace) ?? null));
|
|
17340
|
+
const duplicateKey = JSON.stringify([entries.map(({ namespace }) => runtime.adminDuplicateRepairStatus?.(namespace) ?? null), runtime.adminGraphSettings?.()]);
|
|
16876
17341
|
const etag = `"${createHash6("sha256").update(`${STRATAGATE_DSH_VERSION}\0${TOPIC_DIRECTORY_VIEW_VERSION}\0${revisionKey}\0${selected?.namespace ?? ""}\0${threadId}\0${agentMemoryWeight}\0${duplicateKey}`).digest("base64url").slice(0, 24)}"`;
|
|
16877
|
-
if (
|
|
17342
|
+
if (ifNoneMatch.split(",").map((value) => value.trim()).includes(etag)) return { etag, notModified: true };
|
|
16878
17343
|
const overviewValue = await overview(runtime, entries);
|
|
16879
17344
|
if (!selected) {
|
|
16880
17345
|
return { etag, notModified: false, body: { namespace: null, overview: overviewValue, data: null, processing: false } };
|
|
@@ -16889,50 +17354,42 @@ async function dashboard(runtime, url, ifNoneMatch) {
|
|
|
16889
17354
|
if (limit) target.searchParams.set("limit", limit);
|
|
16890
17355
|
return target;
|
|
16891
17356
|
};
|
|
16892
|
-
const
|
|
16893
|
-
|
|
16894
|
-
|
|
16895
|
-
|
|
16896
|
-
audit
|
|
16897
|
-
|
|
16898
|
-
};
|
|
16899
|
-
if (region && !Object.hasOwn(readers, region)) throw new AdminHttpError(400, "Unknown dashboard region");
|
|
16900
|
-
const keys = region ? [region] : Object.keys(readers);
|
|
16901
|
-
const results = await Promise.allSettled(keys.map((key) => Promise.resolve().then(readers[key])));
|
|
16902
|
-
const values = {}, readErrors = {};
|
|
16903
|
-
results.forEach((result, index) => {
|
|
16904
|
-
if (result.status === "fulfilled") values[keys[index]] = result.value;
|
|
16905
|
-
else readErrors[keys[index]] = "Content could not be read";
|
|
16906
|
-
});
|
|
16907
|
-
const { events: eventResult, graph: graphResult, blocks: blockResult, audit: auditResult, topics: topicDirectoryData } = values;
|
|
17357
|
+
const [eventResult, graphResult, blockResult, auditResult] = await Promise.all([
|
|
17358
|
+
memories(snapshotRuntime, memoryUrl("events", "40")),
|
|
17359
|
+
memories(snapshotRuntime, memoryUrl("graph")),
|
|
17360
|
+
memories(snapshotRuntime, memoryUrl("blocks", "40")),
|
|
17361
|
+
audit(snapshotRuntime, memoryUrl("audit", "100"))
|
|
17362
|
+
]);
|
|
16908
17363
|
const selectedOverview = overviewValue.namespaces?.find(({ namespace }) => namespace === selected.namespace);
|
|
17364
|
+
const topicDirectoryData = topicDirectoryProjection(selected.snapshot, agentMemoryWeight).data;
|
|
16909
17365
|
return {
|
|
16910
|
-
|
|
16911
|
-
etag: region || Object.keys(readErrors).length ? "" : etag,
|
|
17366
|
+
etag,
|
|
16912
17367
|
notModified: false,
|
|
16913
17368
|
body: {
|
|
16914
17369
|
namespace: selected.namespace,
|
|
16915
17370
|
revision: selected.revision,
|
|
16916
17371
|
overview: overviewValue,
|
|
16917
|
-
|
|
16918
|
-
processing: Number(selectedOverview?.processingJobs ?? 0) > 0 || selectedOverview?.duplicateRepair?.status === "pending" || topicDirectoryData?.bootstrap != null && topicDirectoryData.bootstrap.total > 0 && (topicDirectoryData.bootstrap.status === "pending" || topicDirectoryData.bootstrap.status === "running"),
|
|
17372
|
+
processing: Number(selectedOverview?.processingJobs ?? 0) > 0 || selectedOverview?.duplicateRepair?.status === "pending" || topicDirectoryData.bootstrap !== null && topicDirectoryData.bootstrap.total > 0 && (topicDirectoryData.bootstrap.status === "pending" || topicDirectoryData.bootstrap.status === "running"),
|
|
16919
17373
|
data: {
|
|
16920
|
-
|
|
16921
|
-
|
|
16922
|
-
|
|
16923
|
-
|
|
16924
|
-
|
|
17374
|
+
events: eventResult.items ?? [],
|
|
17375
|
+
graph: graphResult,
|
|
17376
|
+
blocks: blockResult.items ?? [],
|
|
17377
|
+
openBlock: blockResult.openBlock ?? null,
|
|
17378
|
+
conversations: blockResult.conversations ?? [],
|
|
17379
|
+
activeThreadId: blockResult.activeThreadId ?? null,
|
|
17380
|
+
audit: auditResult.items ?? [],
|
|
17381
|
+
topicDirectory: topicDirectoryData,
|
|
16925
17382
|
pagination: {
|
|
16926
|
-
|
|
16927
|
-
|
|
16928
|
-
|
|
17383
|
+
events: { total: eventResult.total ?? 0, offset: eventResult.offset ?? 0, limit: eventResult.limit ?? 40 },
|
|
17384
|
+
blocks: { total: blockResult.total ?? 0, offset: blockResult.offset ?? 0, limit: blockResult.limit ?? 40 },
|
|
17385
|
+
audit: { total: auditResult.total ?? 0, offset: auditResult.offset ?? 0, limit: auditResult.limit ?? 100 }
|
|
16929
17386
|
}
|
|
16930
17387
|
}
|
|
16931
17388
|
}
|
|
16932
17389
|
};
|
|
16933
17390
|
}
|
|
16934
17391
|
function sendDashboard(res, result) {
|
|
16935
|
-
|
|
17392
|
+
res.setHeader("ETag", result.etag);
|
|
16936
17393
|
res.setHeader("Cache-Control", "private, no-cache");
|
|
16937
17394
|
if (result.notModified) {
|
|
16938
17395
|
res.statusCode = 304;
|
|
@@ -17016,10 +17473,12 @@ async function handleAdminRequest(runtime, req, res) {
|
|
|
17016
17473
|
sendJson(res, 200, await runtime.diagnosticReport(namespace, { ...id ? { id, details: true } : {}, offset: numeric(url.searchParams.get("offset"), 0, 0, 2e3) }));
|
|
17017
17474
|
} else if (path === "/api/stratagate/profile") {
|
|
17018
17475
|
sendJson(res, 200, await persistentProfile(runtime, req), true);
|
|
17476
|
+
} else if (path === "/api/stratagate/graph/control") {
|
|
17477
|
+
if (req.method !== "POST") throw new AdminHttpError(405, "Graph control requires POST");
|
|
17478
|
+
sendJson(res, 200, await runtime.adminGraphControl(url.searchParams.get("action") ?? "", url.searchParams.get("token") ?? void 0));
|
|
17019
17479
|
} else if (path === "/api/stratagate/settings") {
|
|
17020
|
-
if (req.method
|
|
17021
|
-
|
|
17022
|
-
else throw new AdminHttpError(405, "StrataGate settings require GET or PATCH");
|
|
17480
|
+
if (req.method !== "PATCH") throw new AdminHttpError(405, "StrataGate settings require PATCH");
|
|
17481
|
+
sendJson(res, 200, await updateSettings(runtime, url));
|
|
17023
17482
|
} else if (path === "/api/stratagate/storage/open-directory") {
|
|
17024
17483
|
if (req.method !== "POST") throw new AdminHttpError(405, "StrataGate data directory open requires POST");
|
|
17025
17484
|
sendJson(res, 200, await runtime.adminOpenDataDirectory(AbortSignal.timeout(15e3)));
|
|
@@ -17181,7 +17640,7 @@ import {
|
|
|
17181
17640
|
writeFile
|
|
17182
17641
|
} from "node:fs/promises";
|
|
17183
17642
|
import { basename, dirname as dirname2, join } from "node:path";
|
|
17184
|
-
import { createHash as createHash7, randomUUID as
|
|
17643
|
+
import { createHash as createHash7, randomUUID as randomUUID8 } from "node:crypto";
|
|
17185
17644
|
import { encodeSeqRanges, KNOWN_SESSION_EVENT_TYPES } from "@deepseek-ai/dsh-session";
|
|
17186
17645
|
var LEGACY_EVENT_TYPE = "stratagate/memory-citations";
|
|
17187
17646
|
var COMPAT_EVENT_TYPE = "feedback/record";
|
|
@@ -17410,7 +17869,7 @@ async function exists(path) {
|
|
|
17410
17869
|
}
|
|
17411
17870
|
async function publishExclusive(path, bytes) {
|
|
17412
17871
|
await mkdir(dirname2(path), { recursive: true });
|
|
17413
|
-
const temporary = join(dirname2(path), `.${basename(path)}.stratagate-${
|
|
17872
|
+
const temporary = join(dirname2(path), `.${basename(path)}.stratagate-${randomUUID8()}.tmp`);
|
|
17414
17873
|
const handle = await open(temporary, "wx", 384);
|
|
17415
17874
|
try {
|
|
17416
17875
|
await handle.writeFile(bytes);
|
|
@@ -17558,7 +18017,7 @@ async function apply(ctx, config) {
|
|
|
17558
18017
|
ctx.logger.info(`stratagate-memory prepared ${legacyMigration.migrated} legacy Session generation(s) for DSH ${compatibility.cliVersion}`);
|
|
17559
18018
|
}
|
|
17560
18019
|
await mkdir2(dirname3(resolved.database), { recursive: true });
|
|
17561
|
-
if (resolved.database !== ":memory:" &&
|
|
18020
|
+
if (resolved.database !== ":memory:" && existsSync3(resolved.database)) {
|
|
17562
18021
|
try {
|
|
17563
18022
|
dropLegacyAgentMemoriesTable(resolved.database);
|
|
17564
18023
|
} catch (error) {
|
|
@@ -17598,7 +18057,7 @@ async function apply(ctx, config) {
|
|
|
17598
18057
|
settingsCtx.effect(() => settings.configure({ auto: false }, ctx.fiber));
|
|
17599
18058
|
}
|
|
17600
18059
|
});
|
|
17601
|
-
ctx.systemPrompt.section({ name: "tool:stratagate-memory", order: 113, text: MEMORY_PROTOCOL });
|
|
18060
|
+
ctx.systemPrompt.section({ name: "tool:stratagate-memory", order: 113, text: () => runtime.graphEnabled ? MEMORY_PROTOCOL : MEMORY_PROTOCOL.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") });
|
|
17602
18061
|
if (AGENT_FEEDBACK_ENABLED) ctx.systemPrompt.section({ name: "tool:stratagate-feedback", order: 114, text: FEEDBACK_PROTOCOL });
|
|
17603
18062
|
ctx.on("system-prompt/assemble", async (_assembly, context, next) => {
|
|
17604
18063
|
const assembled = await next();
|