@agentmemory/agentmemory 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +14 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/publish.yml +28 -0
- package/AGENTS.md +113 -0
- package/LICENSE +190 -0
- package/README.md +828 -0
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +73 -0
- package/benchmark/REAL-EMBEDDINGS.md +67 -0
- package/benchmark/SCALE.md +110 -0
- package/benchmark/dataset.ts +293 -0
- package/benchmark/quality-eval.ts +643 -0
- package/benchmark/real-embeddings-eval.ts +405 -0
- package/benchmark/scale-eval.ts +398 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +137 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/docker-compose.yml +14 -0
- package/dist/hooks/notification.d.mts +1 -0
- package/dist/hooks/notification.mjs +45 -0
- package/dist/hooks/notification.mjs.map +1 -0
- package/dist/hooks/post-tool-failure.d.mts +1 -0
- package/dist/hooks/post-tool-failure.mjs +45 -0
- package/dist/hooks/post-tool-failure.mjs.map +1 -0
- package/dist/hooks/post-tool-use.d.mts +1 -0
- package/dist/hooks/post-tool-use.mjs +53 -0
- package/dist/hooks/post-tool-use.mjs.map +1 -0
- package/dist/hooks/pre-compact.d.mts +1 -0
- package/dist/hooks/pre-compact.mjs +50 -0
- package/dist/hooks/pre-compact.mjs.map +1 -0
- package/dist/hooks/pre-tool-use.d.mts +1 -0
- package/dist/hooks/pre-tool-use.mjs +69 -0
- package/dist/hooks/pre-tool-use.mjs.map +1 -0
- package/dist/hooks/prompt-submit.d.mts +1 -0
- package/dist/hooks/prompt-submit.mjs +40 -0
- package/dist/hooks/prompt-submit.mjs.map +1 -0
- package/dist/hooks/session-end.d.mts +1 -0
- package/dist/hooks/session-end.mjs +61 -0
- package/dist/hooks/session-end.mjs.map +1 -0
- package/dist/hooks/session-start.d.mts +1 -0
- package/dist/hooks/session-start.mjs +42 -0
- package/dist/hooks/session-start.mjs.map +1 -0
- package/dist/hooks/stop.d.mts +1 -0
- package/dist/hooks/stop.mjs +33 -0
- package/dist/hooks/stop.mjs.map +1 -0
- package/dist/hooks/subagent-start.d.mts +1 -0
- package/dist/hooks/subagent-start.mjs +43 -0
- package/dist/hooks/subagent-start.mjs.map +1 -0
- package/dist/hooks/subagent-stop.d.mts +1 -0
- package/dist/hooks/subagent-stop.mjs +45 -0
- package/dist/hooks/subagent-stop.mjs.map +1 -0
- package/dist/hooks/task-completed.d.mts +1 -0
- package/dist/hooks/task-completed.mjs +46 -0
- package/dist/hooks/task-completed.mjs.map +1 -0
- package/dist/iii-config.yaml +51 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +13776 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src-QxitMPfJ.mjs +13775 -0
- package/dist/src-QxitMPfJ.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -0
- package/dist/standalone.mjs +1155 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/transformers-BX_tgxdO.mjs +38684 -0
- package/dist/transformers-BX_tgxdO.mjs.map +1 -0
- package/dist/transformers-KMm1i9no.mjs +38683 -0
- package/dist/transformers-KMm1i9no.mjs.map +1 -0
- package/docker-compose.yml +14 -0
- package/iii-config.yaml +51 -0
- package/package.json +59 -0
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/hooks/hooks.json +77 -0
- package/plugin/scripts/diagnostics.mjs +551 -0
- package/plugin/scripts/notification.mjs +45 -0
- package/plugin/scripts/post-tool-failure.mjs +45 -0
- package/plugin/scripts/post-tool-use.mjs +53 -0
- package/plugin/scripts/pre-compact.mjs +50 -0
- package/plugin/scripts/pre-tool-use.mjs +69 -0
- package/plugin/scripts/prompt-submit.mjs +40 -0
- package/plugin/scripts/session-end.mjs +61 -0
- package/plugin/scripts/session-start.mjs +42 -0
- package/plugin/scripts/stop.mjs +33 -0
- package/plugin/scripts/subagent-start.mjs +43 -0
- package/plugin/scripts/subagent-stop.mjs +45 -0
- package/plugin/scripts/task-completed.mjs +46 -0
- package/plugin/skills/forget/SKILL.md +32 -0
- package/plugin/skills/recall/SKILL.md +18 -0
- package/plugin/skills/remember/SKILL.md +25 -0
- package/plugin/skills/session-history/SKILL.md +17 -0
- package/src/auth.ts +12 -0
- package/src/cli.ts +159 -0
- package/src/config.ts +221 -0
- package/src/eval/metrics-store.ts +65 -0
- package/src/eval/quality.ts +51 -0
- package/src/eval/schemas.ts +124 -0
- package/src/eval/self-correct.ts +28 -0
- package/src/eval/validator.ts +31 -0
- package/src/functions/actions.ts +288 -0
- package/src/functions/audit.ts +61 -0
- package/src/functions/auto-forget.ts +169 -0
- package/src/functions/branch-aware.ts +169 -0
- package/src/functions/cascade.ts +80 -0
- package/src/functions/checkpoints.ts +209 -0
- package/src/functions/claude-bridge.ts +161 -0
- package/src/functions/compress.ts +194 -0
- package/src/functions/consolidate.ts +212 -0
- package/src/functions/consolidation-pipeline.ts +258 -0
- package/src/functions/context.ts +169 -0
- package/src/functions/crystallize.ts +293 -0
- package/src/functions/dedup.ts +57 -0
- package/src/functions/diagnostics.ts +785 -0
- package/src/functions/enrich.ts +132 -0
- package/src/functions/evict.ts +163 -0
- package/src/functions/export-import.ts +508 -0
- package/src/functions/facets.ts +248 -0
- package/src/functions/file-index.ts +106 -0
- package/src/functions/flow-compress.ts +214 -0
- package/src/functions/frontier.ts +196 -0
- package/src/functions/governance.ts +131 -0
- package/src/functions/graph-retrieval.ts +277 -0
- package/src/functions/graph.ts +275 -0
- package/src/functions/leases.ts +216 -0
- package/src/functions/lessons.ts +253 -0
- package/src/functions/mesh.ts +434 -0
- package/src/functions/migrate.ts +165 -0
- package/src/functions/observe.ts +144 -0
- package/src/functions/obsidian-export.ts +310 -0
- package/src/functions/patterns.ts +138 -0
- package/src/functions/privacy.ts +39 -0
- package/src/functions/profile.ts +155 -0
- package/src/functions/query-expansion.ts +186 -0
- package/src/functions/relations.ts +237 -0
- package/src/functions/remember.ts +162 -0
- package/src/functions/retention.ts +235 -0
- package/src/functions/routines.ts +289 -0
- package/src/functions/search.ts +80 -0
- package/src/functions/sentinels.ts +417 -0
- package/src/functions/signals.ts +186 -0
- package/src/functions/sketches.ts +274 -0
- package/src/functions/sliding-window.ts +257 -0
- package/src/functions/smart-search.ts +115 -0
- package/src/functions/snapshot.ts +219 -0
- package/src/functions/summarize.ts +155 -0
- package/src/functions/team.ts +147 -0
- package/src/functions/temporal-graph.ts +476 -0
- package/src/functions/timeline.ts +138 -0
- package/src/functions/verify.ts +117 -0
- package/src/health/monitor.ts +110 -0
- package/src/health/thresholds.ts +73 -0
- package/src/hooks/notification.ts +52 -0
- package/src/hooks/post-tool-failure.ts +58 -0
- package/src/hooks/post-tool-use.ts +62 -0
- package/src/hooks/pre-compact.ts +60 -0
- package/src/hooks/pre-tool-use.ts +72 -0
- package/src/hooks/prompt-submit.ts +46 -0
- package/src/hooks/session-end.ts +71 -0
- package/src/hooks/session-start.ts +48 -0
- package/src/hooks/stop.ts +39 -0
- package/src/hooks/subagent-start.ts +49 -0
- package/src/hooks/subagent-stop.ts +54 -0
- package/src/hooks/task-completed.ts +54 -0
- package/src/index.ts +342 -0
- package/src/mcp/in-memory-kv.ts +61 -0
- package/src/mcp/server.ts +1455 -0
- package/src/mcp/standalone.ts +177 -0
- package/src/mcp/tools-registry.ts +769 -0
- package/src/mcp/transport.ts +91 -0
- package/src/prompts/compression.ts +67 -0
- package/src/prompts/consolidation.ts +48 -0
- package/src/prompts/graph-extraction.ts +35 -0
- package/src/prompts/summary.ts +38 -0
- package/src/prompts/xml.ts +26 -0
- package/src/providers/agent-sdk.ts +34 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/circuit-breaker.ts +82 -0
- package/src/providers/embedding/cohere.ts +46 -0
- package/src/providers/embedding/gemini.ts +54 -0
- package/src/providers/embedding/index.ts +39 -0
- package/src/providers/embedding/local.ts +52 -0
- package/src/providers/embedding/openai.ts +45 -0
- package/src/providers/embedding/openrouter.ts +51 -0
- package/src/providers/embedding/voyage.ts +46 -0
- package/src/providers/fallback-chain.ts +31 -0
- package/src/providers/index.ts +84 -0
- package/src/providers/openrouter.ts +71 -0
- package/src/providers/resilient.ts +37 -0
- package/src/state/hybrid-search.ts +295 -0
- package/src/state/index-persistence.ts +63 -0
- package/src/state/keyed-mutex.ts +18 -0
- package/src/state/kv.ts +33 -0
- package/src/state/schema.ts +71 -0
- package/src/state/search-index.ts +245 -0
- package/src/state/stemmer.ts +104 -0
- package/src/state/synonyms.ts +63 -0
- package/src/state/vector-index.ts +130 -0
- package/src/telemetry/setup.ts +116 -0
- package/src/triggers/api.ts +1904 -0
- package/src/triggers/events.ts +71 -0
- package/src/types.ts +769 -0
- package/src/version.ts +1 -0
- package/src/viewer/index.html +2497 -0
- package/src/viewer/server.ts +207 -0
- package/src/xenova.d.ts +3 -0
- package/test/actions.test.ts +490 -0
- package/test/audit.test.ts +108 -0
- package/test/auto-forget.test.ts +188 -0
- package/test/cascade.test.ts +277 -0
- package/test/checkpoints.test.ts +493 -0
- package/test/circuit-breaker.test.ts +107 -0
- package/test/claude-bridge.test.ts +178 -0
- package/test/confidence.test.ts +247 -0
- package/test/consistency.test.ts +61 -0
- package/test/consolidation-pipeline.test.ts +251 -0
- package/test/crystallize.test.ts +521 -0
- package/test/diagnostics.test.ts +638 -0
- package/test/embedding-provider.test.ts +49 -0
- package/test/enrich.test.ts +209 -0
- package/test/eval.test.ts +300 -0
- package/test/export-import.test.ts +251 -0
- package/test/facets.test.ts +448 -0
- package/test/fallback-chain.test.ts +93 -0
- package/test/frontier.test.ts +485 -0
- package/test/governance.test.ts +147 -0
- package/test/graph-retrieval.test.ts +186 -0
- package/test/graph.test.ts +160 -0
- package/test/helpers/mocks.ts +40 -0
- package/test/hybrid-search.test.ts +145 -0
- package/test/index-persistence.test.ts +124 -0
- package/test/integration.test.ts +265 -0
- package/test/leases.test.ts +399 -0
- package/test/mcp-prompts.test.ts +218 -0
- package/test/mcp-resources.test.ts +286 -0
- package/test/mcp-standalone.test.ts +113 -0
- package/test/mesh.test.ts +700 -0
- package/test/privacy.test.ts +87 -0
- package/test/profile.test.ts +161 -0
- package/test/query-expansion.test.ts +154 -0
- package/test/relations.test.ts +198 -0
- package/test/retention.test.ts +245 -0
- package/test/routines.test.ts +497 -0
- package/test/schema-fingerprint.test.ts +81 -0
- package/test/schema.test.ts +42 -0
- package/test/search-index.test.ts +128 -0
- package/test/sentinels.test.ts +626 -0
- package/test/signals.test.ts +410 -0
- package/test/sketches.test.ts +549 -0
- package/test/sliding-window.test.ts +199 -0
- package/test/smart-search.test.ts +169 -0
- package/test/snapshot.test.ts +165 -0
- package/test/team.test.ts +156 -0
- package/test/temporal-graph.test.ts +378 -0
- package/test/timeline.test.ts +148 -0
- package/test/vector-index.test.ts +79 -0
- package/test/verify.test.ts +209 -0
- package/test/xml.test.ts +65 -0
- package/tsconfig.json +22 -0
- package/tsdown.config.ts +62 -0
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import type { StateKV } from "../state/kv.js";
|
|
3
|
+
import { KV } from "../state/schema.js";
|
|
4
|
+
import { withKeyedLock } from "../state/keyed-mutex.js";
|
|
5
|
+
import type {
|
|
6
|
+
Action,
|
|
7
|
+
ActionEdge,
|
|
8
|
+
DiagnosticCheck,
|
|
9
|
+
Lease,
|
|
10
|
+
Checkpoint,
|
|
11
|
+
Signal,
|
|
12
|
+
Sentinel,
|
|
13
|
+
Sketch,
|
|
14
|
+
MeshPeer,
|
|
15
|
+
Session,
|
|
16
|
+
Memory,
|
|
17
|
+
} from "../types.js";
|
|
18
|
+
|
|
19
|
+
const ALL_CATEGORIES = [
|
|
20
|
+
"actions",
|
|
21
|
+
"leases",
|
|
22
|
+
"sentinels",
|
|
23
|
+
"sketches",
|
|
24
|
+
"signals",
|
|
25
|
+
"sessions",
|
|
26
|
+
"memories",
|
|
27
|
+
"mesh",
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const TWENTY_FOUR_HOURS_MS = 24 * 60 * 60 * 1000;
|
|
31
|
+
const ONE_HOUR_MS = 60 * 60 * 1000;
|
|
32
|
+
|
|
33
|
+
export function registerDiagnosticsFunction(sdk: ISdk, kv: StateKV): void {
|
|
34
|
+
sdk.registerFunction(
|
|
35
|
+
{ id: "mem::diagnose" },
|
|
36
|
+
async (data: { categories?: string[] }) => {
|
|
37
|
+
const categories = data.categories && data.categories.length > 0
|
|
38
|
+
? data.categories.filter((c) => ALL_CATEGORIES.includes(c))
|
|
39
|
+
: ALL_CATEGORIES;
|
|
40
|
+
|
|
41
|
+
const checks: DiagnosticCheck[] = [];
|
|
42
|
+
const now = Date.now();
|
|
43
|
+
|
|
44
|
+
if (categories.includes("actions")) {
|
|
45
|
+
const actions = await kv.list<Action>(KV.actions);
|
|
46
|
+
const allEdges = await kv.list<ActionEdge>(KV.actionEdges);
|
|
47
|
+
const leases = await kv.list<Lease>(KV.leases);
|
|
48
|
+
const actionMap = new Map(actions.map((a) => [a.id, a]));
|
|
49
|
+
|
|
50
|
+
for (const action of actions) {
|
|
51
|
+
if (action.status === "active") {
|
|
52
|
+
const hasActiveLease = leases.some(
|
|
53
|
+
(l) =>
|
|
54
|
+
l.actionId === action.id &&
|
|
55
|
+
l.status === "active" &&
|
|
56
|
+
new Date(l.expiresAt).getTime() > now,
|
|
57
|
+
);
|
|
58
|
+
if (!hasActiveLease) {
|
|
59
|
+
checks.push({
|
|
60
|
+
name: `active-no-lease:${action.id}`,
|
|
61
|
+
category: "actions",
|
|
62
|
+
status: "warn",
|
|
63
|
+
message: `Action "${action.title}" is active but has no active lease`,
|
|
64
|
+
fixable: false,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (action.status === "blocked") {
|
|
70
|
+
const deps = allEdges.filter(
|
|
71
|
+
(e) => e.sourceActionId === action.id && e.type === "requires",
|
|
72
|
+
);
|
|
73
|
+
if (deps.length > 0) {
|
|
74
|
+
const allDone = deps.every((d) => {
|
|
75
|
+
const target = actionMap.get(d.targetActionId);
|
|
76
|
+
return target && target.status === "done";
|
|
77
|
+
});
|
|
78
|
+
if (allDone) {
|
|
79
|
+
checks.push({
|
|
80
|
+
name: `blocked-deps-done:${action.id}`,
|
|
81
|
+
category: "actions",
|
|
82
|
+
status: "fail",
|
|
83
|
+
message: `Action "${action.title}" is blocked but all dependencies are done`,
|
|
84
|
+
fixable: true,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (action.status === "pending") {
|
|
91
|
+
const deps = allEdges.filter(
|
|
92
|
+
(e) => e.sourceActionId === action.id && e.type === "requires",
|
|
93
|
+
);
|
|
94
|
+
if (deps.length > 0) {
|
|
95
|
+
const hasUnsatisfied = deps.some((d) => {
|
|
96
|
+
const target = actionMap.get(d.targetActionId);
|
|
97
|
+
return !target || target.status !== "done";
|
|
98
|
+
});
|
|
99
|
+
if (hasUnsatisfied) {
|
|
100
|
+
checks.push({
|
|
101
|
+
name: `pending-unsatisfied-deps:${action.id}`,
|
|
102
|
+
category: "actions",
|
|
103
|
+
status: "fail",
|
|
104
|
+
message: `Action "${action.title}" is pending but has unsatisfied dependencies`,
|
|
105
|
+
fixable: true,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (
|
|
113
|
+
!checks.some((c) => c.category === "actions" && c.status !== "pass")
|
|
114
|
+
) {
|
|
115
|
+
checks.push({
|
|
116
|
+
name: "actions-ok",
|
|
117
|
+
category: "actions",
|
|
118
|
+
status: "pass",
|
|
119
|
+
message: `All ${actions.length} actions are consistent`,
|
|
120
|
+
fixable: false,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (categories.includes("leases")) {
|
|
126
|
+
const leases = await kv.list<Lease>(KV.leases);
|
|
127
|
+
const actions = await kv.list<Action>(KV.actions);
|
|
128
|
+
const actionIds = new Set(actions.map((a) => a.id));
|
|
129
|
+
let leaseIssues = 0;
|
|
130
|
+
|
|
131
|
+
for (const lease of leases) {
|
|
132
|
+
if (
|
|
133
|
+
lease.status === "active" &&
|
|
134
|
+
new Date(lease.expiresAt).getTime() <= now
|
|
135
|
+
) {
|
|
136
|
+
checks.push({
|
|
137
|
+
name: `expired-lease:${lease.id}`,
|
|
138
|
+
category: "leases",
|
|
139
|
+
status: "fail",
|
|
140
|
+
message: `Lease ${lease.id} for action ${lease.actionId} expired at ${lease.expiresAt}`,
|
|
141
|
+
fixable: true,
|
|
142
|
+
});
|
|
143
|
+
leaseIssues++;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!actionIds.has(lease.actionId)) {
|
|
147
|
+
checks.push({
|
|
148
|
+
name: `orphaned-lease:${lease.id}`,
|
|
149
|
+
category: "leases",
|
|
150
|
+
status: "fail",
|
|
151
|
+
message: `Lease ${lease.id} references non-existent action ${lease.actionId}`,
|
|
152
|
+
fixable: true,
|
|
153
|
+
});
|
|
154
|
+
leaseIssues++;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (leaseIssues === 0) {
|
|
159
|
+
checks.push({
|
|
160
|
+
name: "leases-ok",
|
|
161
|
+
category: "leases",
|
|
162
|
+
status: "pass",
|
|
163
|
+
message: `All ${leases.length} leases are healthy`,
|
|
164
|
+
fixable: false,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (categories.includes("sentinels")) {
|
|
170
|
+
const sentinels = await kv.list<Sentinel>(KV.sentinels);
|
|
171
|
+
const actions = await kv.list<Action>(KV.actions);
|
|
172
|
+
const actionIds = new Set(actions.map((a) => a.id));
|
|
173
|
+
let sentinelIssues = 0;
|
|
174
|
+
|
|
175
|
+
for (const sentinel of sentinels) {
|
|
176
|
+
if (
|
|
177
|
+
sentinel.status === "watching" &&
|
|
178
|
+
sentinel.expiresAt &&
|
|
179
|
+
new Date(sentinel.expiresAt).getTime() <= now
|
|
180
|
+
) {
|
|
181
|
+
checks.push({
|
|
182
|
+
name: `expired-sentinel:${sentinel.id}`,
|
|
183
|
+
category: "sentinels",
|
|
184
|
+
status: "fail",
|
|
185
|
+
message: `Sentinel "${sentinel.name}" expired at ${sentinel.expiresAt}`,
|
|
186
|
+
fixable: true,
|
|
187
|
+
});
|
|
188
|
+
sentinelIssues++;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (const actionId of sentinel.linkedActionIds) {
|
|
192
|
+
if (!actionIds.has(actionId)) {
|
|
193
|
+
checks.push({
|
|
194
|
+
name: `sentinel-missing-action:${sentinel.id}:${actionId}`,
|
|
195
|
+
category: "sentinels",
|
|
196
|
+
status: "warn",
|
|
197
|
+
message: `Sentinel "${sentinel.name}" references non-existent action ${actionId}`,
|
|
198
|
+
fixable: false,
|
|
199
|
+
});
|
|
200
|
+
sentinelIssues++;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (sentinelIssues === 0) {
|
|
206
|
+
checks.push({
|
|
207
|
+
name: "sentinels-ok",
|
|
208
|
+
category: "sentinels",
|
|
209
|
+
status: "pass",
|
|
210
|
+
message: `All ${sentinels.length} sentinels are healthy`,
|
|
211
|
+
fixable: false,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (categories.includes("sketches")) {
|
|
217
|
+
const sketches = await kv.list<Sketch>(KV.sketches);
|
|
218
|
+
let sketchIssues = 0;
|
|
219
|
+
|
|
220
|
+
for (const sketch of sketches) {
|
|
221
|
+
if (
|
|
222
|
+
sketch.status === "active" &&
|
|
223
|
+
new Date(sketch.expiresAt).getTime() <= now
|
|
224
|
+
) {
|
|
225
|
+
checks.push({
|
|
226
|
+
name: `expired-sketch:${sketch.id}`,
|
|
227
|
+
category: "sketches",
|
|
228
|
+
status: "fail",
|
|
229
|
+
message: `Sketch "${sketch.title}" expired at ${sketch.expiresAt}`,
|
|
230
|
+
fixable: true,
|
|
231
|
+
});
|
|
232
|
+
sketchIssues++;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (sketchIssues === 0) {
|
|
237
|
+
checks.push({
|
|
238
|
+
name: "sketches-ok",
|
|
239
|
+
category: "sketches",
|
|
240
|
+
status: "pass",
|
|
241
|
+
message: `All ${sketches.length} sketches are healthy`,
|
|
242
|
+
fixable: false,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (categories.includes("signals")) {
|
|
248
|
+
const signals = await kv.list<Signal>(KV.signals);
|
|
249
|
+
let signalIssues = 0;
|
|
250
|
+
|
|
251
|
+
for (const signal of signals) {
|
|
252
|
+
if (
|
|
253
|
+
signal.expiresAt &&
|
|
254
|
+
new Date(signal.expiresAt).getTime() <= now
|
|
255
|
+
) {
|
|
256
|
+
checks.push({
|
|
257
|
+
name: `expired-signal:${signal.id}`,
|
|
258
|
+
category: "signals",
|
|
259
|
+
status: "fail",
|
|
260
|
+
message: `Signal from "${signal.from}" expired at ${signal.expiresAt}`,
|
|
261
|
+
fixable: true,
|
|
262
|
+
});
|
|
263
|
+
signalIssues++;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (signalIssues === 0) {
|
|
268
|
+
checks.push({
|
|
269
|
+
name: "signals-ok",
|
|
270
|
+
category: "signals",
|
|
271
|
+
status: "pass",
|
|
272
|
+
message: `All ${signals.length} signals are healthy`,
|
|
273
|
+
fixable: false,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (categories.includes("sessions")) {
|
|
279
|
+
const sessions = await kv.list<Session>(KV.sessions);
|
|
280
|
+
let sessionIssues = 0;
|
|
281
|
+
|
|
282
|
+
for (const session of sessions) {
|
|
283
|
+
if (
|
|
284
|
+
session.status === "active" &&
|
|
285
|
+
now - new Date(session.startedAt).getTime() > TWENTY_FOUR_HOURS_MS
|
|
286
|
+
) {
|
|
287
|
+
checks.push({
|
|
288
|
+
name: `abandoned-session:${session.id}`,
|
|
289
|
+
category: "sessions",
|
|
290
|
+
status: "warn",
|
|
291
|
+
message: `Session ${session.id} has been active for over 24 hours`,
|
|
292
|
+
fixable: false,
|
|
293
|
+
});
|
|
294
|
+
sessionIssues++;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (sessionIssues === 0) {
|
|
299
|
+
checks.push({
|
|
300
|
+
name: "sessions-ok",
|
|
301
|
+
category: "sessions",
|
|
302
|
+
status: "pass",
|
|
303
|
+
message: `All ${sessions.length} sessions are healthy`,
|
|
304
|
+
fixable: false,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (categories.includes("memories")) {
|
|
310
|
+
const memories = await kv.list<Memory>(KV.memories);
|
|
311
|
+
const memoryIds = new Set(memories.map((m) => m.id));
|
|
312
|
+
const supersededBy = new Map<string, string>();
|
|
313
|
+
let memoryIssues = 0;
|
|
314
|
+
|
|
315
|
+
for (const memory of memories) {
|
|
316
|
+
if (memory.supersedes && memory.supersedes.length > 0) {
|
|
317
|
+
for (const sid of memory.supersedes) {
|
|
318
|
+
if (!memoryIds.has(sid)) {
|
|
319
|
+
checks.push({
|
|
320
|
+
name: `memory-missing-supersedes:${memory.id}:${sid}`,
|
|
321
|
+
category: "memories",
|
|
322
|
+
status: "warn",
|
|
323
|
+
message: `Memory "${memory.title}" supersedes non-existent memory ${sid}`,
|
|
324
|
+
fixable: false,
|
|
325
|
+
});
|
|
326
|
+
memoryIssues++;
|
|
327
|
+
}
|
|
328
|
+
supersededBy.set(sid, memory.id);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
for (const memory of memories) {
|
|
334
|
+
if (memory.isLatest && supersededBy.has(memory.id)) {
|
|
335
|
+
checks.push({
|
|
336
|
+
name: `memory-stale-latest:${memory.id}`,
|
|
337
|
+
category: "memories",
|
|
338
|
+
status: "fail",
|
|
339
|
+
message: `Memory "${memory.title}" has isLatest=true but is superseded by ${supersededBy.get(memory.id)}`,
|
|
340
|
+
fixable: true,
|
|
341
|
+
});
|
|
342
|
+
memoryIssues++;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (memoryIssues === 0) {
|
|
347
|
+
checks.push({
|
|
348
|
+
name: "memories-ok",
|
|
349
|
+
category: "memories",
|
|
350
|
+
status: "pass",
|
|
351
|
+
message: `All ${memories.length} memories are consistent`,
|
|
352
|
+
fixable: false,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (categories.includes("mesh")) {
|
|
358
|
+
const peers = await kv.list<MeshPeer>(KV.mesh);
|
|
359
|
+
let meshIssues = 0;
|
|
360
|
+
|
|
361
|
+
for (const peer of peers) {
|
|
362
|
+
if (
|
|
363
|
+
peer.lastSyncAt &&
|
|
364
|
+
now - new Date(peer.lastSyncAt).getTime() > ONE_HOUR_MS
|
|
365
|
+
) {
|
|
366
|
+
checks.push({
|
|
367
|
+
name: `stale-peer:${peer.id}`,
|
|
368
|
+
category: "mesh",
|
|
369
|
+
status: "warn",
|
|
370
|
+
message: `Peer "${peer.name}" last synced over 1 hour ago`,
|
|
371
|
+
fixable: false,
|
|
372
|
+
});
|
|
373
|
+
meshIssues++;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (peer.status === "error") {
|
|
377
|
+
checks.push({
|
|
378
|
+
name: `error-peer:${peer.id}`,
|
|
379
|
+
category: "mesh",
|
|
380
|
+
status: "warn",
|
|
381
|
+
message: `Peer "${peer.name}" is in error state`,
|
|
382
|
+
fixable: false,
|
|
383
|
+
});
|
|
384
|
+
meshIssues++;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (meshIssues === 0) {
|
|
389
|
+
checks.push({
|
|
390
|
+
name: "mesh-ok",
|
|
391
|
+
category: "mesh",
|
|
392
|
+
status: "pass",
|
|
393
|
+
message: `All ${peers.length} mesh peers are healthy`,
|
|
394
|
+
fixable: false,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const summary = {
|
|
400
|
+
pass: checks.filter((c) => c.status === "pass").length,
|
|
401
|
+
warn: checks.filter((c) => c.status === "warn").length,
|
|
402
|
+
fail: checks.filter((c) => c.status === "fail").length,
|
|
403
|
+
fixable: checks.filter((c) => c.fixable).length,
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
return { success: true, checks, summary };
|
|
407
|
+
},
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
sdk.registerFunction(
|
|
411
|
+
{ id: "mem::heal" },
|
|
412
|
+
async (data: { categories?: string[]; dryRun?: boolean }) => {
|
|
413
|
+
const dryRun = data.dryRun ?? false;
|
|
414
|
+
const categories = data.categories && data.categories.length > 0
|
|
415
|
+
? data.categories.filter((c) => ALL_CATEGORIES.includes(c))
|
|
416
|
+
: ALL_CATEGORIES;
|
|
417
|
+
|
|
418
|
+
let fixed = 0;
|
|
419
|
+
let skipped = 0;
|
|
420
|
+
const details: string[] = [];
|
|
421
|
+
const now = Date.now();
|
|
422
|
+
|
|
423
|
+
if (categories.includes("actions")) {
|
|
424
|
+
const actions = await kv.list<Action>(KV.actions);
|
|
425
|
+
const allEdges = await kv.list<ActionEdge>(KV.actionEdges);
|
|
426
|
+
const actionMap = new Map(actions.map((a) => [a.id, a]));
|
|
427
|
+
|
|
428
|
+
for (const action of actions) {
|
|
429
|
+
if (action.status === "blocked") {
|
|
430
|
+
const deps = allEdges.filter(
|
|
431
|
+
(e) => e.sourceActionId === action.id && e.type === "requires",
|
|
432
|
+
);
|
|
433
|
+
if (deps.length > 0) {
|
|
434
|
+
const allDone = deps.every((d) => {
|
|
435
|
+
const target = actionMap.get(d.targetActionId);
|
|
436
|
+
return target && target.status === "done";
|
|
437
|
+
});
|
|
438
|
+
if (allDone) {
|
|
439
|
+
if (dryRun) {
|
|
440
|
+
details.push(
|
|
441
|
+
`[dry-run] Would unblock action "${action.title}" (${action.id})`,
|
|
442
|
+
);
|
|
443
|
+
fixed++;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
const didFix = await withKeyedLock(
|
|
447
|
+
`mem:action:${action.id}`,
|
|
448
|
+
async () => {
|
|
449
|
+
const fresh = await kv.get<Action>(KV.actions, action.id);
|
|
450
|
+
if (!fresh || fresh.status !== "blocked") return false;
|
|
451
|
+
const freshEdges = await kv.list<ActionEdge>(KV.actionEdges);
|
|
452
|
+
const freshDeps = freshEdges.filter(
|
|
453
|
+
(e) =>
|
|
454
|
+
e.sourceActionId === fresh.id && e.type === "requires",
|
|
455
|
+
);
|
|
456
|
+
const freshActions = await kv.list<Action>(KV.actions);
|
|
457
|
+
const freshMap = new Map(
|
|
458
|
+
freshActions.map((a) => [a.id, a]),
|
|
459
|
+
);
|
|
460
|
+
const stillAllDone = freshDeps.every((d) => {
|
|
461
|
+
const target = freshMap.get(d.targetActionId);
|
|
462
|
+
return target && target.status === "done";
|
|
463
|
+
});
|
|
464
|
+
if (!stillAllDone) return false;
|
|
465
|
+
fresh.status = "pending";
|
|
466
|
+
fresh.updatedAt = new Date().toISOString();
|
|
467
|
+
await kv.set(KV.actions, fresh.id, fresh);
|
|
468
|
+
return true;
|
|
469
|
+
},
|
|
470
|
+
);
|
|
471
|
+
if (didFix) {
|
|
472
|
+
details.push(
|
|
473
|
+
`Unblocked action "${action.title}" (${action.id})`,
|
|
474
|
+
);
|
|
475
|
+
fixed++;
|
|
476
|
+
} else {
|
|
477
|
+
skipped++;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (action.status === "pending") {
|
|
484
|
+
const deps = allEdges.filter(
|
|
485
|
+
(e) => e.sourceActionId === action.id && e.type === "requires",
|
|
486
|
+
);
|
|
487
|
+
if (deps.length > 0) {
|
|
488
|
+
const hasUnsatisfied = deps.some((d) => {
|
|
489
|
+
const target = actionMap.get(d.targetActionId);
|
|
490
|
+
return !target || target.status !== "done";
|
|
491
|
+
});
|
|
492
|
+
if (hasUnsatisfied) {
|
|
493
|
+
if (dryRun) {
|
|
494
|
+
details.push(
|
|
495
|
+
`[dry-run] Would block action "${action.title}" (${action.id})`,
|
|
496
|
+
);
|
|
497
|
+
fixed++;
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const didFix = await withKeyedLock(
|
|
501
|
+
`mem:action:${action.id}`,
|
|
502
|
+
async () => {
|
|
503
|
+
const fresh = await kv.get<Action>(KV.actions, action.id);
|
|
504
|
+
if (!fresh || fresh.status !== "pending") return false;
|
|
505
|
+
const freshEdges = await kv.list<ActionEdge>(KV.actionEdges);
|
|
506
|
+
const freshDeps = freshEdges.filter(
|
|
507
|
+
(e) =>
|
|
508
|
+
e.sourceActionId === fresh.id && e.type === "requires",
|
|
509
|
+
);
|
|
510
|
+
const freshActions = await kv.list<Action>(KV.actions);
|
|
511
|
+
const freshMap = new Map(
|
|
512
|
+
freshActions.map((a) => [a.id, a]),
|
|
513
|
+
);
|
|
514
|
+
const stillUnsatisfied = freshDeps.some((d) => {
|
|
515
|
+
const target = freshMap.get(d.targetActionId);
|
|
516
|
+
return !target || target.status !== "done";
|
|
517
|
+
});
|
|
518
|
+
if (!stillUnsatisfied) return false;
|
|
519
|
+
fresh.status = "blocked";
|
|
520
|
+
fresh.updatedAt = new Date().toISOString();
|
|
521
|
+
await kv.set(KV.actions, fresh.id, fresh);
|
|
522
|
+
return true;
|
|
523
|
+
},
|
|
524
|
+
);
|
|
525
|
+
if (didFix) {
|
|
526
|
+
details.push(
|
|
527
|
+
`Blocked action "${action.title}" (${action.id})`,
|
|
528
|
+
);
|
|
529
|
+
fixed++;
|
|
530
|
+
} else {
|
|
531
|
+
skipped++;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (categories.includes("leases")) {
|
|
540
|
+
const leases = await kv.list<Lease>(KV.leases);
|
|
541
|
+
const actions = await kv.list<Action>(KV.actions);
|
|
542
|
+
const actionIds = new Set(actions.map((a) => a.id));
|
|
543
|
+
|
|
544
|
+
for (const lease of leases) {
|
|
545
|
+
if (
|
|
546
|
+
lease.status === "active" &&
|
|
547
|
+
new Date(lease.expiresAt).getTime() <= now
|
|
548
|
+
) {
|
|
549
|
+
if (dryRun) {
|
|
550
|
+
details.push(
|
|
551
|
+
`[dry-run] Would expire lease ${lease.id} for action ${lease.actionId}`,
|
|
552
|
+
);
|
|
553
|
+
fixed++;
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
const didFix = await withKeyedLock(
|
|
557
|
+
`mem:lease:${lease.actionId}`,
|
|
558
|
+
async () => {
|
|
559
|
+
const fresh = await kv.get<Lease>(KV.leases, lease.id);
|
|
560
|
+
if (
|
|
561
|
+
!fresh ||
|
|
562
|
+
fresh.status !== "active" ||
|
|
563
|
+
new Date(fresh.expiresAt).getTime() > Date.now()
|
|
564
|
+
) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
fresh.status = "expired";
|
|
568
|
+
await kv.set(KV.leases, fresh.id, fresh);
|
|
569
|
+
|
|
570
|
+
const action = await kv.get<Action>(KV.actions, fresh.actionId);
|
|
571
|
+
if (
|
|
572
|
+
action &&
|
|
573
|
+
action.status === "active" &&
|
|
574
|
+
action.assignedTo === fresh.agentId
|
|
575
|
+
) {
|
|
576
|
+
action.status = "pending";
|
|
577
|
+
action.assignedTo = undefined;
|
|
578
|
+
action.updatedAt = new Date().toISOString();
|
|
579
|
+
await kv.set(KV.actions, action.id, action);
|
|
580
|
+
}
|
|
581
|
+
return true;
|
|
582
|
+
},
|
|
583
|
+
);
|
|
584
|
+
if (didFix) {
|
|
585
|
+
details.push(
|
|
586
|
+
`Expired lease ${lease.id} for action ${lease.actionId}`,
|
|
587
|
+
);
|
|
588
|
+
fixed++;
|
|
589
|
+
} else {
|
|
590
|
+
skipped++;
|
|
591
|
+
}
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
if (!actionIds.has(lease.actionId)) {
|
|
596
|
+
if (dryRun) {
|
|
597
|
+
details.push(
|
|
598
|
+
`[dry-run] Would delete orphaned lease ${lease.id}`,
|
|
599
|
+
);
|
|
600
|
+
fixed++;
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
await kv.delete(KV.leases, lease.id);
|
|
604
|
+
details.push(`Deleted orphaned lease ${lease.id}`);
|
|
605
|
+
fixed++;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (categories.includes("sentinels")) {
|
|
611
|
+
const sentinels = await kv.list<Sentinel>(KV.sentinels);
|
|
612
|
+
|
|
613
|
+
for (const sentinel of sentinels) {
|
|
614
|
+
if (
|
|
615
|
+
sentinel.status === "watching" &&
|
|
616
|
+
sentinel.expiresAt &&
|
|
617
|
+
new Date(sentinel.expiresAt).getTime() <= now
|
|
618
|
+
) {
|
|
619
|
+
if (dryRun) {
|
|
620
|
+
details.push(
|
|
621
|
+
`[dry-run] Would expire sentinel "${sentinel.name}" (${sentinel.id})`,
|
|
622
|
+
);
|
|
623
|
+
fixed++;
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
const didFix = await withKeyedLock(
|
|
627
|
+
`mem:sentinel:${sentinel.id}`,
|
|
628
|
+
async () => {
|
|
629
|
+
const fresh = await kv.get<Sentinel>(
|
|
630
|
+
KV.sentinels,
|
|
631
|
+
sentinel.id,
|
|
632
|
+
);
|
|
633
|
+
if (!fresh || fresh.status !== "watching") return false;
|
|
634
|
+
if (
|
|
635
|
+
!fresh.expiresAt ||
|
|
636
|
+
new Date(fresh.expiresAt).getTime() > Date.now()
|
|
637
|
+
) {
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
fresh.status = "expired";
|
|
641
|
+
await kv.set(KV.sentinels, fresh.id, fresh);
|
|
642
|
+
return true;
|
|
643
|
+
},
|
|
644
|
+
);
|
|
645
|
+
if (didFix) {
|
|
646
|
+
details.push(
|
|
647
|
+
`Expired sentinel "${sentinel.name}" (${sentinel.id})`,
|
|
648
|
+
);
|
|
649
|
+
fixed++;
|
|
650
|
+
} else {
|
|
651
|
+
skipped++;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (categories.includes("sketches")) {
|
|
658
|
+
const sketches = await kv.list<Sketch>(KV.sketches);
|
|
659
|
+
|
|
660
|
+
for (const sketch of sketches) {
|
|
661
|
+
if (
|
|
662
|
+
sketch.status === "active" &&
|
|
663
|
+
new Date(sketch.expiresAt).getTime() <= now
|
|
664
|
+
) {
|
|
665
|
+
if (dryRun) {
|
|
666
|
+
details.push(
|
|
667
|
+
`[dry-run] Would discard expired sketch "${sketch.title}" (${sketch.id})`,
|
|
668
|
+
);
|
|
669
|
+
fixed++;
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
const didFix = await withKeyedLock(
|
|
673
|
+
`mem:sketch:${sketch.id}`,
|
|
674
|
+
async () => {
|
|
675
|
+
const fresh = await kv.get<Sketch>(KV.sketches, sketch.id);
|
|
676
|
+
if (
|
|
677
|
+
!fresh ||
|
|
678
|
+
fresh.status !== "active" ||
|
|
679
|
+
new Date(fresh.expiresAt).getTime() > Date.now()
|
|
680
|
+
) {
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const allEdges = await kv.list<ActionEdge>(KV.actionEdges);
|
|
685
|
+
const actionIdSet = new Set(fresh.actionIds);
|
|
686
|
+
for (const edge of allEdges) {
|
|
687
|
+
if (
|
|
688
|
+
actionIdSet.has(edge.sourceActionId) ||
|
|
689
|
+
actionIdSet.has(edge.targetActionId)
|
|
690
|
+
) {
|
|
691
|
+
await kv.delete(KV.actionEdges, edge.id);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
for (const actionId of fresh.actionIds) {
|
|
695
|
+
await kv.delete(KV.actions, actionId);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
fresh.status = "discarded";
|
|
699
|
+
fresh.discardedAt = new Date().toISOString();
|
|
700
|
+
await kv.set(KV.sketches, fresh.id, fresh);
|
|
701
|
+
return true;
|
|
702
|
+
},
|
|
703
|
+
);
|
|
704
|
+
if (didFix) {
|
|
705
|
+
details.push(
|
|
706
|
+
`Discarded expired sketch "${sketch.title}" (${sketch.id})`,
|
|
707
|
+
);
|
|
708
|
+
fixed++;
|
|
709
|
+
} else {
|
|
710
|
+
skipped++;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (categories.includes("signals")) {
|
|
717
|
+
const signals = await kv.list<Signal>(KV.signals);
|
|
718
|
+
|
|
719
|
+
for (const signal of signals) {
|
|
720
|
+
if (
|
|
721
|
+
signal.expiresAt &&
|
|
722
|
+
new Date(signal.expiresAt).getTime() <= now
|
|
723
|
+
) {
|
|
724
|
+
if (dryRun) {
|
|
725
|
+
details.push(
|
|
726
|
+
`[dry-run] Would delete expired signal ${signal.id}`,
|
|
727
|
+
);
|
|
728
|
+
fixed++;
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
await kv.delete(KV.signals, signal.id);
|
|
732
|
+
details.push(`Deleted expired signal ${signal.id}`);
|
|
733
|
+
fixed++;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
if (categories.includes("memories")) {
|
|
739
|
+
const memories = await kv.list<Memory>(KV.memories);
|
|
740
|
+
const supersededBy = new Map<string, string>();
|
|
741
|
+
|
|
742
|
+
for (const memory of memories) {
|
|
743
|
+
if (memory.supersedes && memory.supersedes.length > 0) {
|
|
744
|
+
for (const sid of memory.supersedes) {
|
|
745
|
+
supersededBy.set(sid, memory.id);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
for (const memory of memories) {
|
|
751
|
+
if (memory.isLatest && supersededBy.has(memory.id)) {
|
|
752
|
+
if (dryRun) {
|
|
753
|
+
details.push(
|
|
754
|
+
`[dry-run] Would set isLatest=false on memory "${memory.title}" (${memory.id})`,
|
|
755
|
+
);
|
|
756
|
+
fixed++;
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
const didFix = await withKeyedLock(
|
|
760
|
+
`mem:memory:${memory.id}`,
|
|
761
|
+
async () => {
|
|
762
|
+
const fresh = await kv.get<Memory>(KV.memories, memory.id);
|
|
763
|
+
if (!fresh || !fresh.isLatest) return false;
|
|
764
|
+
fresh.isLatest = false;
|
|
765
|
+
fresh.updatedAt = new Date().toISOString();
|
|
766
|
+
await kv.set(KV.memories, fresh.id, fresh);
|
|
767
|
+
return true;
|
|
768
|
+
},
|
|
769
|
+
);
|
|
770
|
+
if (didFix) {
|
|
771
|
+
details.push(
|
|
772
|
+
`Set isLatest=false on memory "${memory.title}" (${memory.id})`,
|
|
773
|
+
);
|
|
774
|
+
fixed++;
|
|
775
|
+
} else {
|
|
776
|
+
skipped++;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
return { success: true, fixed, skipped, details };
|
|
783
|
+
},
|
|
784
|
+
);
|
|
785
|
+
}
|