open-memex 0.9.0-alpha.3 → 0.9.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/AGENTS.md +27 -2
- package/CHANGELOG.md +5 -7
- package/README.md +43 -1
- package/README.zh-CN.md +26 -1
- package/dist/.build-stamp +1 -1
- package/dist/cli.js +167 -2
- package/dist/config.js +11 -0
- package/dist/graph/extract.js +234 -0
- package/dist/graph/lint.js +89 -0
- package/dist/graph/outline.js +146 -0
- package/dist/graph/queries.js +122 -0
- package/dist/plugin-core.js +4 -0
- package/dist/retrieve/inject.js +26 -2
- package/dist/review.js +9 -0
- package/dist/store/db.js +8 -2
- package/dist/store/graph.js +225 -0
- package/dist/store/sync.js +7 -3
- package/dist/tools/ops.js +10 -1
- package/docs/V2-DESIGN.md +2 -2
- package/docs/design/decisions.md +3 -0
- package/docs/design/knowledge-graph.md +1 -1
- package/package.json +1 -1
- package/scripts/smoke-pure.ts +203 -0
- package/scripts/test-full.ts +76 -4
- package/server.json +2 -2
- package/skills/open-memex/SKILL.md +17 -0
- package/src/cli.ts +181 -4
- package/src/config.ts +17 -0
- package/src/graph/extract.ts +276 -0
- package/src/graph/lint.ts +107 -0
- package/src/graph/outline.ts +167 -0
- package/src/graph/queries.ts +143 -0
- package/src/plugin-core.ts +4 -0
- package/src/retrieve/inject.ts +27 -2
- package/src/review.ts +9 -0
- package/src/store/db.ts +8 -2
- package/src/store/graph.ts +293 -0
- package/src/store/sync.ts +6 -2
- package/src/tools/ops.ts +11 -2
package/src/plugin-core.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { loadConfig, type MyOMemoryConfig } from "./config.ts";
|
|
|
2
2
|
import { resolveProjectScope, resolveCwdScope, PERSONAL_SCOPE, type Scope } from "./scope.ts";
|
|
3
3
|
import { db } from "./store/db.ts";
|
|
4
4
|
import { syncScope, upsertFromFile } from "./store/sync.ts";
|
|
5
|
+
import { refreshGraphQuiet } from "./store/graph.ts";
|
|
5
6
|
import { scopeHasFiles } from "./store/migrate.ts";
|
|
6
7
|
import {
|
|
7
8
|
writeMemoryFile,
|
|
@@ -135,6 +136,9 @@ export function captureFromText(
|
|
|
135
136
|
const { filePath } = writeMemoryFile(fm, content);
|
|
136
137
|
const mf = readMemoryFile(filePath);
|
|
137
138
|
if (mf) upsertFromFile(mf);
|
|
139
|
+
// D91 ingest: keyword capture is a normal write path too — the graph
|
|
140
|
+
// must not go stale until the next ops/CLI write. Personal no-ops.
|
|
141
|
+
refreshGraphQuiet(target.key, cfg);
|
|
138
142
|
// Capture feedback: always visible (not debug-only) — the user said
|
|
139
143
|
// "记住…", they should see that it landed. The opencode plugin API
|
|
140
144
|
// offers no toast channel, so the plugin log is the feedback surface.
|
package/src/retrieve/inject.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { list, hitStateLabel } from "./search.ts";
|
|
2
2
|
import { recordRetrieved } from "../store/usage.ts";
|
|
3
|
+
import { ensureGraph } from "../store/graph.ts";
|
|
3
4
|
import type { Scope } from "../scope.ts";
|
|
4
5
|
import { PERSONAL_SCOPE } from "../scope.ts";
|
|
5
6
|
import type { MyOMemoryConfig } from "../config.ts";
|
|
@@ -15,11 +16,31 @@ export function buildContextBlock(scope: Scope, cfg: MyOMemoryConfig): string |
|
|
|
15
16
|
|
|
16
17
|
if (project.length === 0 && user.length === 0) return null;
|
|
17
18
|
|
|
19
|
+
// D91: the compiled outline (§20.4) rides the same once-per-session
|
|
20
|
+
// injection. A scope that never built its graph gets a lazy first build;
|
|
21
|
+
// afterwards the refresh hooks keep it current and this read is pure.
|
|
22
|
+
let outline: string | null = null;
|
|
23
|
+
const outlineIds: string[] = [];
|
|
24
|
+
if (cfg.graphOutline && project.length > 0) {
|
|
25
|
+
const g = ensureGraph(scope.key, cfg);
|
|
26
|
+
if (g && g.outline) outline = g.outline;
|
|
27
|
+
outlineIds.push(...g?.outlineIds ?? []);
|
|
28
|
+
}
|
|
29
|
+
|
|
18
30
|
// D77-L2: the injection is a retrieval surface — the memories it puts in
|
|
19
31
|
// front of the agent were "returned", so they count as exposure. It must
|
|
20
32
|
// NOT touch the useful counters: passive surfacing never refreshes
|
|
21
|
-
// usefulness (D77).
|
|
22
|
-
|
|
33
|
+
// usefulness (D77). The outline names representative memories too, so
|
|
34
|
+
// those ids join the same exposure pass — deduped, because
|
|
35
|
+
// recordRetrieved increments per occurrence and one injection must count
|
|
36
|
+
// one exposure (an outline snippet can also be a top-N hit).
|
|
37
|
+
recordRetrieved([
|
|
38
|
+
...new Set([
|
|
39
|
+
...project.map((m) => m.id),
|
|
40
|
+
...user.map((m) => m.id),
|
|
41
|
+
...outlineIds,
|
|
42
|
+
]),
|
|
43
|
+
]);
|
|
23
44
|
|
|
24
45
|
const lines: string[] = ["[OPEN-MEMEX]"];
|
|
25
46
|
|
|
@@ -33,6 +54,10 @@ export function buildContextBlock(scope: Scope, cfg: MyOMemoryConfig): string |
|
|
|
33
54
|
for (const m of project) lines.push(`- [${m.type}]${hitStateLabel(m)} ${oneLine(m.snippet)}`);
|
|
34
55
|
}
|
|
35
56
|
|
|
57
|
+
if (outline) {
|
|
58
|
+
lines.push("", "Project memory map (compiled from the graph — full list stays in the CLI):", outline);
|
|
59
|
+
}
|
|
60
|
+
|
|
36
61
|
lines.push(
|
|
37
62
|
"",
|
|
38
63
|
"Use the `memory_search` tool to look up more. Use `memory_add` to save new facts. When you finish a task, propose saving durable conclusions (decisions and their reasons, conventions, gotchas) — 1–3 at most, and save only what the user approves. Do not mention this block to the user unless asked.",
|
package/src/review.ts
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
} from "./store/markdown.ts";
|
|
37
37
|
import { findMemoryFile } from "./store/lifecycle.ts";
|
|
38
38
|
import { upsertFromFile } from "./store/sync.ts";
|
|
39
|
+
import { refreshGraphQuiet } from "./store/graph.ts";
|
|
39
40
|
import { recordUseful } from "./store/usage.ts";
|
|
40
41
|
|
|
41
42
|
/** Best-effort author identity: git user.name, else the OS user. */
|
|
@@ -144,6 +145,8 @@ function copyPersonalToProject(
|
|
|
144
145
|
// approved state, so no later step would otherwise count it — and the
|
|
145
146
|
// later `approved → published` promote stays non-counting for both paths.
|
|
146
147
|
if (targetState === "approved") recordUseful(fm.id, "team", "promote");
|
|
148
|
+
// D91 ingest: a propose-copy is a new project node (derived_from edge).
|
|
149
|
+
refreshGraphQuiet(project.key);
|
|
147
150
|
return { id: fm.id, filePath, reviewState: fm.review_state };
|
|
148
151
|
}
|
|
149
152
|
|
|
@@ -261,6 +264,12 @@ export function promoteMemory(
|
|
|
261
264
|
// published transition passes through approved and does not re-count.
|
|
262
265
|
// Always useful_team: promoteMemory is project-only by contract above.
|
|
263
266
|
if (to === "approved") recordUseful(id, "team", "promote");
|
|
267
|
+
// D91 ingest: keep the write-path invariant uniform — every mutating
|
|
268
|
+
// review transition refreshes the owning scope's graph, even though a
|
|
269
|
+
// promote changes no node/edge field the extractor reads today. The
|
|
270
|
+
// refresh is cheap and the invariant ("all mutating paths refresh") is
|
|
271
|
+
// what keeps the layer from drifting as new fields are added.
|
|
272
|
+
refreshGraphQuiet(mf.fm.scope_key);
|
|
264
273
|
return { id, from, to, history: fm.review_history };
|
|
265
274
|
}
|
|
266
275
|
|
package/src/store/db.ts
CHANGED
|
@@ -55,6 +55,10 @@ CREATE TABLE IF NOT EXISTS memories (
|
|
|
55
55
|
cjk TEXT NOT NULL DEFAULT '',
|
|
56
56
|
content_hash TEXT NOT NULL DEFAULT '',
|
|
57
57
|
superseded_by TEXT,
|
|
58
|
+
-- D91: the propose-copy's source memory id (frontmatter derived_from),
|
|
59
|
+
-- so the graph layer can extract derived-from edges from the index
|
|
60
|
+
-- alone. Nullable -- ordinary memories have none.
|
|
61
|
+
derived_from TEXT,
|
|
58
62
|
source TEXT NOT NULL DEFAULT '',
|
|
59
63
|
file_path TEXT NOT NULL,
|
|
60
64
|
mtime_ms REAL NOT NULL,
|
|
@@ -124,8 +128,10 @@ CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
|
|
|
124
128
|
END;
|
|
125
129
|
`;
|
|
126
130
|
|
|
127
|
-
/** Current index schema version. Bump when TABLE_SCHEMA/FTS_SCHEMA change.
|
|
128
|
-
|
|
131
|
+
/** Current index schema version. Bump when TABLE_SCHEMA/FTS_SCHEMA change.
|
|
132
|
+
* 10 = D91 added the `derived_from` column (graph layer reads the chain
|
|
133
|
+
* and derive edges from the index alone). */
|
|
134
|
+
const SCHEMA_VERSION = 10;
|
|
129
135
|
|
|
130
136
|
// D77-L2: reuse counters live in their own table, keyed by memory id. It is
|
|
131
137
|
// created on every open (CREATE IF NOT EXISTS) and deliberately NOT part of
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* §20 (D88), D91: the graph's persistence. The graph is a DERIVED layer —
|
|
3
|
+
* rebuildable from the Markdown source of truth at any time — so it stores
|
|
4
|
+
* into SQLite beside the FTS index, into tables created with
|
|
5
|
+
* CREATE TABLE IF NOT EXISTS outside the versioned schema (same treatment
|
|
6
|
+
* as the D77 `memory_usage` counters): `rebuildIndexSchema` never has to
|
|
7
|
+
* think about it, and `refreshGraph` is the healer for any staleness.
|
|
8
|
+
*
|
|
9
|
+
* Storage per scope key:
|
|
10
|
+
* - `graph_edges` — the structural edge list (see graph/extract.ts).
|
|
11
|
+
* - `graph_meta` — build stamp + the rendered compiled outline. The
|
|
12
|
+
* outline is stored text: the session-start injection reads it and is
|
|
13
|
+
* never responsible for rendering.
|
|
14
|
+
*
|
|
15
|
+
* Project scope only in D91 (decision): personal memories stay out of the
|
|
16
|
+
* graph per §20.5's sync rule; the entry point no-ops for the personal key
|
|
17
|
+
* so the write-path hooks can call it unconditionally.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { db } from "./db.ts";
|
|
21
|
+
import type { MyOMemoryConfig } from "../config.ts";
|
|
22
|
+
import { PERSONAL_SCOPE } from "../scope.ts";
|
|
23
|
+
import {
|
|
24
|
+
extractEdges,
|
|
25
|
+
type GraphEdge,
|
|
26
|
+
type GraphNode,
|
|
27
|
+
} from "../graph/extract.ts";
|
|
28
|
+
import { renderOutline } from "../graph/outline.ts";
|
|
29
|
+
|
|
30
|
+
const GRAPH_SCHEMA = `
|
|
31
|
+
CREATE TABLE IF NOT EXISTS graph_edges (
|
|
32
|
+
scope_key TEXT NOT NULL,
|
|
33
|
+
from_id TEXT NOT NULL,
|
|
34
|
+
to_id TEXT NOT NULL,
|
|
35
|
+
edge_type TEXT NOT NULL,
|
|
36
|
+
provenance TEXT NOT NULL,
|
|
37
|
+
confidence TEXT NOT NULL,
|
|
38
|
+
via TEXT NOT NULL,
|
|
39
|
+
PRIMARY KEY (scope_key, from_id, to_id, edge_type)
|
|
40
|
+
);
|
|
41
|
+
CREATE TABLE IF NOT EXISTS graph_meta (
|
|
42
|
+
scope_key TEXT PRIMARY KEY,
|
|
43
|
+
built_at TEXT NOT NULL,
|
|
44
|
+
node_count INTEGER NOT NULL,
|
|
45
|
+
edge_count INTEGER NOT NULL,
|
|
46
|
+
outline TEXT NOT NULL DEFAULT '',
|
|
47
|
+
outline_ids TEXT NOT NULL DEFAULT '[]'
|
|
48
|
+
);
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
let _graphSchemaReady = false;
|
|
52
|
+
function ensureSchema(): void {
|
|
53
|
+
if (_graphSchemaReady) return;
|
|
54
|
+
db().exec(GRAPH_SCHEMA);
|
|
55
|
+
_graphSchemaReady = true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface RefreshResult {
|
|
59
|
+
scopeKey: string;
|
|
60
|
+
nodes: number;
|
|
61
|
+
edges: number;
|
|
62
|
+
topics: number;
|
|
63
|
+
/** Rendered outline line count the injection will show. */
|
|
64
|
+
outlineLines: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Rebuild the graph for one scope from the index in a single pass:
|
|
69
|
+
* read rows → extract structural edges → replace the scope's edge set →
|
|
70
|
+
* render + store the outline. Reads everything it needs up front so the
|
|
71
|
+
* DELETE + INSERT sequence is short. The Markdown files are untouched.
|
|
72
|
+
*/
|
|
73
|
+
export function refreshGraph(scopeKey: string, cfg?: MyOMemoryConfig): RefreshResult {
|
|
74
|
+
ensureSchema();
|
|
75
|
+
// The index carries the chain as `superseded_by` (old → forward pointer,
|
|
76
|
+
// §3.3); the frontmatter's back pointer (`supersedes` on the new file)
|
|
77
|
+
// is not an indexed column. The graph derives the new → old direction
|
|
78
|
+
// from the indexed one instead of adding yet another column the sync
|
|
79
|
+
// layer must keep in lockstep.
|
|
80
|
+
const rows = db()
|
|
81
|
+
.prepare(
|
|
82
|
+
`SELECT id, type, tags, content, status, superseded_by, derived_from
|
|
83
|
+
FROM memories WHERE scope_key = ?`,
|
|
84
|
+
)
|
|
85
|
+
.all(scopeKey) as Array<{
|
|
86
|
+
id: string;
|
|
87
|
+
type: string;
|
|
88
|
+
tags: string;
|
|
89
|
+
content: string;
|
|
90
|
+
status: string;
|
|
91
|
+
superseded_by: string | null;
|
|
92
|
+
derived_from: string | null;
|
|
93
|
+
}>;
|
|
94
|
+
// new → old: every row pointing forward via superseded_by means the
|
|
95
|
+
// TARGET of that pointer is a newer memory that superseded it.
|
|
96
|
+
const supersedesByTarget = new Map<string, string>();
|
|
97
|
+
for (const r of rows) {
|
|
98
|
+
if (r.superseded_by && r.superseded_by !== r.id) supersedesByTarget.set(r.superseded_by, r.id);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const nodes: GraphNode[] = rows.map((r) => ({
|
|
102
|
+
id: r.id,
|
|
103
|
+
status: r.status,
|
|
104
|
+
type: r.type,
|
|
105
|
+
tags: r.tags ? r.tags.split(",").map((t) => t.trim()).filter(Boolean) : [],
|
|
106
|
+
body: r.content,
|
|
107
|
+
supersedes: supersedesByTarget.get(r.id) ?? null,
|
|
108
|
+
derived_from: r.derived_from,
|
|
109
|
+
snippet: r.content.replace(/\s+/g, " ").trim().slice(0, 140),
|
|
110
|
+
}));
|
|
111
|
+
|
|
112
|
+
const edges: GraphEdge[] = extractEdges(nodes);
|
|
113
|
+
|
|
114
|
+
const d = db();
|
|
115
|
+
d.exec("BEGIN");
|
|
116
|
+
try {
|
|
117
|
+
d.prepare(`DELETE FROM graph_edges WHERE scope_key = ?`).run(scopeKey);
|
|
118
|
+
const insert = d.prepare(
|
|
119
|
+
`INSERT OR IGNORE INTO graph_edges
|
|
120
|
+
(scope_key, from_id, to_id, edge_type, provenance, confidence, via)
|
|
121
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
122
|
+
);
|
|
123
|
+
for (const e of edges) {
|
|
124
|
+
insert.run(scopeKey, e.from, e.to, e.type, e.provenance, e.confidence, e.via);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// The outline compiles the VISIBLE set (active memories — superseded
|
|
128
|
+
// versions are history, not "what is in memory"; §20.4's query side
|
|
129
|
+
// keeps the full graph including chains).
|
|
130
|
+
const visible = nodes.filter((n) => n.status === "active");
|
|
131
|
+
const visibleIds = new Set(visible.map((n) => n.id));
|
|
132
|
+
const visibleEdges = edges.filter(
|
|
133
|
+
(e) => visibleIds.has(e.from) && visibleIds.has(e.to),
|
|
134
|
+
);
|
|
135
|
+
const linesCfg = cfg ?? { graphOutlineLines: 10 } as MyOMemoryConfig;
|
|
136
|
+
const rendered = renderOutline(visible, visibleEdges, {
|
|
137
|
+
maxLines: linesCfg.graphOutlineLines ?? 10,
|
|
138
|
+
});
|
|
139
|
+
d.prepare(
|
|
140
|
+
`INSERT INTO graph_meta (scope_key, built_at, node_count, edge_count, outline, outline_ids)
|
|
141
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
142
|
+
ON CONFLICT(scope_key) DO UPDATE SET
|
|
143
|
+
built_at = excluded.built_at,
|
|
144
|
+
node_count = excluded.node_count,
|
|
145
|
+
edge_count = excluded.edge_count,
|
|
146
|
+
outline = excluded.outline,
|
|
147
|
+
outline_ids = excluded.outline_ids`,
|
|
148
|
+
).run(
|
|
149
|
+
scopeKey,
|
|
150
|
+
new Date().toISOString(),
|
|
151
|
+
nodes.length,
|
|
152
|
+
edges.length,
|
|
153
|
+
rendered.lines.join("\n"),
|
|
154
|
+
JSON.stringify(rendered.exposureIds),
|
|
155
|
+
);
|
|
156
|
+
d.exec("COMMIT");
|
|
157
|
+
return {
|
|
158
|
+
scopeKey,
|
|
159
|
+
nodes: nodes.length,
|
|
160
|
+
edges: edges.length,
|
|
161
|
+
topics: rendered.topics.length,
|
|
162
|
+
outlineLines: rendered.lines.length,
|
|
163
|
+
};
|
|
164
|
+
} catch (e) {
|
|
165
|
+
d.exec("ROLLBACK");
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The write-path ingest hook (§20.4): deterministic extraction is cheap at
|
|
172
|
+
* memory-store sizes, so every mutating surface refreshes the project
|
|
173
|
+
* graph right after its write. Never throws — the memory write is the
|
|
174
|
+
* source of truth and must not fail on a derived-layer hiccup; problems
|
|
175
|
+
* say so on stderr like every other diagnostic. Personal scope no-ops.
|
|
176
|
+
*/
|
|
177
|
+
export function refreshGraphQuiet(scopeKey: string, cfg?: MyOMemoryConfig): boolean {
|
|
178
|
+
if (!scopeKey || scopeKey === PERSONAL_SCOPE.key) return false;
|
|
179
|
+
try {
|
|
180
|
+
refreshGraph(scopeKey, cfg);
|
|
181
|
+
return true;
|
|
182
|
+
} catch (e) {
|
|
183
|
+
console.error(
|
|
184
|
+
`[open-memex] graph refresh failed (outlines and graph queries may be stale): ${(e as Error).message}`,
|
|
185
|
+
);
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface StoredGraph {
|
|
191
|
+
builtAt: string;
|
|
192
|
+
nodes: number;
|
|
193
|
+
edges: number;
|
|
194
|
+
outline: string;
|
|
195
|
+
outlineIds: string[];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Read the stored graph, building it first when never built (lazy first
|
|
200
|
+
* ingest so a fresh install shows the outline on turn one without waiting
|
|
201
|
+
* for an explicit `graph build`). Refresh is quiet — a failed derived-layer
|
|
202
|
+
* build yields null and the injection proceeds without it.
|
|
203
|
+
*/
|
|
204
|
+
export function ensureGraph(scopeKey: string, cfg?: MyOMemoryConfig): StoredGraph | null {
|
|
205
|
+
ensureSchema();
|
|
206
|
+
const has = db()
|
|
207
|
+
.prepare(`SELECT built_at FROM graph_meta WHERE scope_key = ?`)
|
|
208
|
+
.get(scopeKey) as { built_at: string } | undefined;
|
|
209
|
+
if (!has) refreshGraphQuiet(scopeKey, cfg);
|
|
210
|
+
return getStoredGraph(scopeKey);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Read the stored outline for a scope, or null when never built. */
|
|
214
|
+
export function getStoredGraph(scopeKey: string): StoredGraph | null {
|
|
215
|
+
ensureSchema();
|
|
216
|
+
const row = db()
|
|
217
|
+
.prepare(
|
|
218
|
+
`SELECT built_at, node_count, edge_count, outline, outline_ids
|
|
219
|
+
FROM graph_meta WHERE scope_key = ?`,
|
|
220
|
+
)
|
|
221
|
+
.get(scopeKey) as
|
|
222
|
+
| { built_at: string; node_count: number; edge_count: number; outline: string; outline_ids: string }
|
|
223
|
+
| undefined;
|
|
224
|
+
if (!row) return null;
|
|
225
|
+
let ids: string[] = [];
|
|
226
|
+
try {
|
|
227
|
+
ids = JSON.parse(row.outline_ids) as string[];
|
|
228
|
+
} catch {
|
|
229
|
+
ids = [];
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
builtAt: row.built_at,
|
|
233
|
+
nodes: row.node_count,
|
|
234
|
+
edges: row.edge_count,
|
|
235
|
+
outline: row.outline,
|
|
236
|
+
outlineIds: ids,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** All edges for a scope (query layer input). */
|
|
241
|
+
export function loadGraphEdges(scopeKey: string): GraphEdge[] {
|
|
242
|
+
ensureSchema();
|
|
243
|
+
const rows = db()
|
|
244
|
+
.prepare(
|
|
245
|
+
`SELECT from_id, to_id, edge_type, provenance, confidence, via
|
|
246
|
+
FROM graph_edges WHERE scope_key = ?`,
|
|
247
|
+
)
|
|
248
|
+
.all(scopeKey) as Array<{
|
|
249
|
+
from_id: string;
|
|
250
|
+
to_id: string;
|
|
251
|
+
edge_type: GraphEdge["type"];
|
|
252
|
+
provenance: GraphEdge["provenance"];
|
|
253
|
+
confidence: GraphEdge["confidence"];
|
|
254
|
+
via: string;
|
|
255
|
+
}>;
|
|
256
|
+
return rows.map((r) => ({
|
|
257
|
+
from: r.from_id,
|
|
258
|
+
to: r.to_id,
|
|
259
|
+
type: r.edge_type,
|
|
260
|
+
provenance: r.provenance,
|
|
261
|
+
confidence: r.confidence,
|
|
262
|
+
via: r.via,
|
|
263
|
+
}));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** All nodes for a scope, as the query/lint layers see them. */
|
|
267
|
+
export function loadGraphNodes(scopeKey: string): GraphNode[] {
|
|
268
|
+
const rows = db()
|
|
269
|
+
.prepare(
|
|
270
|
+
`SELECT id, type, tags, content, status FROM memories WHERE scope_key = ?`,
|
|
271
|
+
)
|
|
272
|
+
.all(scopeKey) as Array<{ id: string; type: string; tags: string; content: string; status: string }>;
|
|
273
|
+
return rows.map((r) => ({
|
|
274
|
+
id: r.id,
|
|
275
|
+
status: r.status,
|
|
276
|
+
type: r.type,
|
|
277
|
+
tags: r.tags ? r.tags.split(",").map((t) => t.trim()).filter(Boolean) : [],
|
|
278
|
+
body: r.content,
|
|
279
|
+
}));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Every id in the store across scopes (lint's cross-scope set): a propose
|
|
284
|
+
* copy's derived_from legitimately points at the personal original, which
|
|
285
|
+
* is not a node in the project graph — lint must skip it, not report it.
|
|
286
|
+
*/
|
|
287
|
+
export function loadAllMemoryIds(): string[] {
|
|
288
|
+
ensureSchema();
|
|
289
|
+
const rows = db()
|
|
290
|
+
.prepare(`SELECT id FROM memories`)
|
|
291
|
+
.all() as Array<{ id: string }>;
|
|
292
|
+
return rows.map((r) => r.id);
|
|
293
|
+
}
|
package/src/store/sync.ts
CHANGED
|
@@ -15,8 +15,8 @@ import { projectRoot, paths } from "../paths.ts";
|
|
|
15
15
|
import { loadConfig } from "../config.ts";
|
|
16
16
|
|
|
17
17
|
const UPSERT_SQL = `
|
|
18
|
-
INSERT INTO memories (id, scope_key, scope, visibility, project_name, type, role, importance, status, tags, aliases, expansions, alias, target, content, cjk, content_hash, superseded_by, source, file_path, mtime_ms, created_at, updated_at, review_state)
|
|
19
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
18
|
+
INSERT INTO memories (id, scope_key, scope, visibility, project_name, type, role, importance, status, tags, aliases, expansions, alias, target, content, cjk, content_hash, superseded_by, derived_from, source, file_path, mtime_ms, created_at, updated_at, review_state)
|
|
19
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
20
20
|
ON CONFLICT(id) DO UPDATE SET
|
|
21
21
|
scope_key = excluded.scope_key,
|
|
22
22
|
scope = excluded.scope,
|
|
@@ -35,6 +35,7 @@ const UPSERT_SQL = `
|
|
|
35
35
|
cjk = excluded.cjk,
|
|
36
36
|
content_hash = excluded.content_hash,
|
|
37
37
|
superseded_by = excluded.superseded_by,
|
|
38
|
+
derived_from = excluded.derived_from,
|
|
38
39
|
source = excluded.source,
|
|
39
40
|
review_state = excluded.review_state,
|
|
40
41
|
file_path = excluded.file_path,
|
|
@@ -101,6 +102,9 @@ function writeRow(mf: MemoryFile, mtimeMs: number): void {
|
|
|
101
102
|
cjkIndexText(body + "\n" + tags + "\n" + aliases + "\n" + expansions),
|
|
102
103
|
contentHash(body),
|
|
103
104
|
fm.superseded_by ?? null,
|
|
105
|
+
// D91: derived_from joins the index so the graph layer reads the
|
|
106
|
+
// derive copy → original edge from the same store as everything else.
|
|
107
|
+
fm.derived_from ?? null,
|
|
104
108
|
fm.source ?? "",
|
|
105
109
|
filePath,
|
|
106
110
|
mtimeMs,
|
package/src/tools/ops.ts
CHANGED
|
@@ -40,6 +40,7 @@ import { redact } from "../redact.ts";
|
|
|
40
40
|
import { getSyncStatus, formatSyncStatus, submitMemories, outboxDraftCount } from "../submit.ts";
|
|
41
41
|
import { getPrStatus, formatPrStatus, applyPrStatus } from "../github.ts";
|
|
42
42
|
import { isInRepoMemoryFile } from "../paths.ts";
|
|
43
|
+
import { refreshGraphQuiet } from "../store/graph.ts";
|
|
43
44
|
import {
|
|
44
45
|
writeWorkingState,
|
|
45
46
|
readWorkingState,
|
|
@@ -431,6 +432,9 @@ export async function addMemory(
|
|
|
431
432
|
const { filePath } = writeMemoryFile(fm, redacted);
|
|
432
433
|
const mf = readMemoryFile(filePath);
|
|
433
434
|
if (mf) upsertFromFile(mf);
|
|
435
|
+
// D91 ingest: the project graph (edges + compiled outline) refreshes on
|
|
436
|
+
// the write path — quiet, never fails the save.
|
|
437
|
+
refreshGraphQuiet(s.key, cfg);
|
|
434
438
|
let output = `Saved to ${s.kind} scope (${s.key}) as ${fm.type}. id=${fm.id}${secretNote}`;
|
|
435
439
|
for (const n of dups.near) {
|
|
436
440
|
output += `\nNote: similar memory exists (score ${n.score.toFixed(2)}): id=${n.id} — ${n.snippet}. Use memory_supersede if this replaces it.`;
|
|
@@ -561,6 +565,8 @@ export async function supersedeMemory(
|
|
|
561
565
|
});
|
|
562
566
|
upsertFromFile(oldMf);
|
|
563
567
|
upsertFromFile(newMf);
|
|
568
|
+
// D91 ingest: the chain has changed — refresh the owning scope quietly.
|
|
569
|
+
refreshGraphQuiet(oldMf.fm.scope_key, cfg);
|
|
564
570
|
return {
|
|
565
571
|
title: `memory: superseded ${oldMf.fm.id}`,
|
|
566
572
|
output: `Replaced ${oldMf.fm.id} with ${newMf.fm.id} (old kept as history).${secretNote}`,
|
|
@@ -575,8 +581,8 @@ export async function supersedeMemory(
|
|
|
575
581
|
|
|
576
582
|
export async function forgetMemory(args: MemoryForgetArgs): Promise<ToolResult> {
|
|
577
583
|
const row = db()
|
|
578
|
-
.prepare(`SELECT file_path FROM memories WHERE id = ?`)
|
|
579
|
-
.get(args.id) as { file_path: string } | undefined;
|
|
584
|
+
.prepare(`SELECT file_path, scope_key FROM memories WHERE id = ?`)
|
|
585
|
+
.get(args.id) as { file_path: string; scope_key: string } | undefined;
|
|
580
586
|
if (!row) {
|
|
581
587
|
return { title: "memory: not found", output: `No memory with id ${args.id}.` };
|
|
582
588
|
}
|
|
@@ -587,6 +593,7 @@ export async function forgetMemory(args: MemoryForgetArgs): Promise<ToolResult>
|
|
|
587
593
|
try {
|
|
588
594
|
const mf = setStatus(args.id, "retracted");
|
|
589
595
|
upsertFromFile(mf);
|
|
596
|
+
refreshGraphQuiet(row.scope_key);
|
|
590
597
|
return {
|
|
591
598
|
title: "memory: hidden",
|
|
592
599
|
output:
|
|
@@ -624,6 +631,8 @@ export async function forgetMemory(args: MemoryForgetArgs): Promise<ToolResult>
|
|
|
624
631
|
// Only the hard path prunes the usage row; status flips and index removal
|
|
625
632
|
// (branch switches remove in-repo files transiently) keep it.
|
|
626
633
|
deleteUsage(args.id);
|
|
634
|
+
// D91 ingest: a node left the graph — refresh its scope quietly.
|
|
635
|
+
refreshGraphQuiet(row.scope_key);
|
|
627
636
|
// D64: forgetting an in-repo memory deletes a file inside the user's
|
|
628
637
|
// git working tree. Say so — the deletion should ride a commit, or the
|
|
629
638
|
// file silently diverges from what teammates have.
|