Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/functions/cascade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IIIClient } from "iii-sdk";
import type { StateKV } from "../state/kv.js";
import { KV } from "../state/schema.js";
import { withKeyedLock } from "../state/keyed-mutex.js";
import type { Memory, GraphNode, GraphEdge } from "../types.js";
import { recordAudit } from "./audit.js";

Expand Down Expand Up @@ -28,10 +29,14 @@ export function registerCascadeFunction(sdk: IIIClient, kv: StateKV): void {
for (const node of nodes) {
if (node.stale) continue;
const overlap = (node.sourceObservationIds ?? []).some((id) => obsIds.has(id));
if (overlap) {
node.stale = true;
node.updatedAt = now;
await kv.set(KV.graphNodes, node.id, node);
if (!overlap) continue;
const flagged = await withKeyedLock("graph:persist", async () => {
const latest = (await kv.get<GraphNode>(KV.graphNodes, node.id)) ?? node;
if (latest.stale) return false;
await kv.set(KV.graphNodes, node.id, { ...latest, stale: true, updatedAt: now });
return true;
});
if (flagged) {
await recordAudit(kv, "consolidate", "mem::cascade-update", [node.id], {
resourceType: "GraphNode",
change: "marked stale from superseded memory",
Expand All @@ -45,9 +50,14 @@ export function registerCascadeFunction(sdk: IIIClient, kv: StateKV): void {
for (const edge of edges) {
if (edge.stale) continue;
const overlap = (edge.sourceObservationIds ?? []).some((id) => obsIds.has(id));
if (overlap) {
edge.stale = true;
await kv.set(KV.graphEdges, edge.id, edge);
if (!overlap) continue;
const flagged = await withKeyedLock("graph:persist", async () => {
const latest = (await kv.get<GraphEdge>(KV.graphEdges, edge.id)) ?? edge;
if (latest.stale) return false;
await kv.set(KV.graphEdges, edge.id, { ...latest, stale: true });
return true;
});
if (flagged) {
await recordAudit(kv, "consolidate", "mem::cascade-update", [edge.id], {
resourceType: "GraphEdge",
change: "marked stale from superseded memory",
Expand Down
71 changes: 41 additions & 30 deletions src/functions/export-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { normalizeAccessLog } from "./access-tracker.js";
import { KV } from "../state/schema.js";
import { checkPayloadFrameSize } from "../state/frame-guard.js";
import { StateKV } from "../state/kv.js";
import { withKeyedLock } from "../state/keyed-mutex.js";
import { VERSION } from "../version.js";
import { recordAudit } from "./audit.js";
import { indexRecords } from "./search.js";
Expand Down Expand Up @@ -372,13 +373,17 @@ export function registerExportImportFunction(sdk: IIIClient, kv: StateKV): void
await kv.list<Insight>(KV.insights).catch(() => []),
(i) => kv.delete(KV.insights, i.id),
);
await runChunked(
await kv.list<{ id: string }>(KV.graphNodes).catch(() => []),
(n) => kv.delete(KV.graphNodes, n.id),
await withKeyedLock("graph:persist", async () =>
runChunked(
await kv.list<{ id: string }>(KV.graphNodes).catch(() => []),
(n) => kv.delete(KV.graphNodes, n.id),
),
);
await runChunked(
await kv.list<{ id: string }>(KV.graphEdges).catch(() => []),
(e) => kv.delete(KV.graphEdges, e.id),
await withKeyedLock("graph:persist", async () =>
runChunked(
await kv.list<{ id: string }>(KV.graphEdges).catch(() => []),
(e) => kv.delete(KV.graphEdges, e.id),
),
);
await runChunked(
await kv.list<{ id: string }>(KV.semantic).catch(() => []),
Expand Down Expand Up @@ -407,17 +412,19 @@ export function registerExportImportFunction(sdk: IIIClient, kv: StateKV): void
const indexMems: Memory[] = [];

await runChunked(importData.sessions, async (session) => {
if (strategy === "skip") {
const existing = await kv
.get<Session>(KV.sessions, session.id)
.catch(() => null);
if (existing) {
stats.skipped++;
return;
await withKeyedLock(`obs:${session.id}`, async () => {
if (strategy === "skip") {
const existing = await kv
.get<Session>(KV.sessions, session.id)
.catch(() => null);
if (existing) {
stats.skipped++;
return;
}
}
}
await kv.set(KV.sessions, session.id, session);
stats.sessions++;
await kv.set(KV.sessions, session.id, session);
stats.sessions++;
});
});

for (const [sessionId, obs] of Object.entries(importData.observations)) {
Expand Down Expand Up @@ -473,22 +480,26 @@ export function registerExportImportFunction(sdk: IIIClient, kv: StateKV): void
});

if (importData.graphNodes) {
await runChunked(importData.graphNodes, async (node) => {
if (strategy === "skip") {
const existing = await kv.get(KV.graphNodes, node.id).catch(() => null);
if (existing) { stats.skipped++; return; }
}
await kv.set(KV.graphNodes, node.id, node);
});
await withKeyedLock("graph:persist", () =>
runChunked(importData.graphNodes!, async (node) => {
if (strategy === "skip") {
const existing = await kv.get(KV.graphNodes, node.id).catch(() => null);
if (existing) { stats.skipped++; return; }
}
await kv.set(KV.graphNodes, node.id, node);
}),
);
}
if (importData.graphEdges) {
await runChunked(importData.graphEdges, async (edge) => {
if (strategy === "skip") {
const existing = await kv.get(KV.graphEdges, edge.id).catch(() => null);
if (existing) { stats.skipped++; return; }
}
await kv.set(KV.graphEdges, edge.id, edge);
});
await withKeyedLock("graph:persist", () =>
runChunked(importData.graphEdges!, async (edge) => {
if (strategy === "skip") {
const existing = await kv.get(KV.graphEdges, edge.id).catch(() => null);
if (existing) { stats.skipped++; return; }
}
await kv.set(KV.graphEdges, edge.id, edge);
}),
);
}
if (importData.semanticMemories) {
await runChunked(importData.semanticMemories, async (sem) => {
Expand Down
Loading
Loading