@tinchu0/knitt 0.1.0-beta.1 → 0.1.0-beta.2
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/README.md +19 -12
- package/dist/apps/server/src/conversation-server.js +203 -0
- package/dist/apps/server/src/server.js +147 -37
- package/dist/memory-schema.json +1 -0
- package/dist/packages/adapters/src/cli.js +163 -15
- package/dist/packages/adapters/src/command.js +149 -18
- package/dist/packages/adapters/src/discovery.js +128 -0
- package/dist/packages/adapters/src/hooks.js +96 -23
- package/dist/packages/adapters/src/launch.js +10 -3
- package/dist/packages/adapters/src/providers/jsonl.js +105 -0
- package/dist/packages/adapters/src/repository.js +21 -5
- package/dist/packages/adapters/src/status.js +81 -0
- package/dist/packages/adapters/src/transcript.js +70 -15
- package/dist/packages/contracts/src/conversations.js +30 -0
- package/dist/packages/installer/src/cli.js +190 -75
- package/dist/packages/installer/src/connections.js +208 -82
- package/dist/packages/installer/src/runtime.js +94 -37
- package/dist/packages/memory/src/checkpoints.js +68 -0
- package/dist/packages/memory/src/context.js +91 -0
- package/dist/packages/memory/src/conversations.js +148 -0
- package/dist/packages/memory/src/migrations.js +55 -0
- package/dist/packages/memory/src/store.js +201 -23
- package/dist/skills/continue-chat/SKILL.md +1 -1
- package/docs/clients/local-agent-memory.md +50 -0
- package/docs/clients/npm-installation.md +17 -12
- package/package.json +4 -3
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
import { readFileSync, existsSync, mkdirSync, writeFileSync, renameSync, rmSync, realpathSync } from
|
|
2
|
-
import { join, dirname, resolve } from
|
|
3
|
-
import { execFileSync } from
|
|
4
|
-
import { randomUUID } from
|
|
5
|
-
import { parse as parseToml } from
|
|
6
|
-
export const clients = [
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync, renameSync, rmSync, realpathSync, } from "node:fs";
|
|
2
|
+
import { join, dirname, resolve } from "node:path";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { parse as parseToml } from "smol-toml";
|
|
6
|
+
export const clients = [
|
|
7
|
+
"codex",
|
|
8
|
+
"claude-code",
|
|
9
|
+
"antigravity",
|
|
10
|
+
"claude-desktop",
|
|
11
|
+
];
|
|
12
|
+
const begin = "# BEGIN shared-agent-memory", end = "# END shared-agent-memory";
|
|
13
|
+
const statePath = ".memory/npm-connection.json";
|
|
14
|
+
const read = (path) => existsSync(path) ? readFileSync(path, "utf8") : null;
|
|
15
|
+
const dump = (v) => JSON.stringify(v, null, 2) + "\n";
|
|
16
|
+
const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
12
17
|
const equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
13
18
|
export function atomicWrite(path, content) {
|
|
14
19
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
15
|
-
const temporary = path +
|
|
20
|
+
const temporary = path + "." + randomUUID() + ".tmp";
|
|
16
21
|
try {
|
|
17
|
-
writeFileSync(temporary, content, { mode: 0o600, flag:
|
|
22
|
+
writeFileSync(temporary, content, { mode: 0o600, flag: "wx" });
|
|
18
23
|
renameSync(temporary, path);
|
|
19
24
|
}
|
|
20
25
|
finally {
|
|
@@ -26,11 +31,19 @@ export function atomicWrite(path, content) {
|
|
|
26
31
|
function restore(before, after, current) {
|
|
27
32
|
if (equal(current, after))
|
|
28
33
|
return before;
|
|
29
|
-
if (object(after) &&
|
|
34
|
+
if (object(after) &&
|
|
35
|
+
object(current) &&
|
|
36
|
+
typeof after.command === "string" &&
|
|
37
|
+
Array.isArray(after.args))
|
|
30
38
|
return current;
|
|
31
|
-
if (object(after) &&
|
|
39
|
+
if (object(after) &&
|
|
40
|
+
object(current) &&
|
|
41
|
+
(before === undefined || object(before))) {
|
|
32
42
|
const result = { ...current };
|
|
33
|
-
for (const key of new Set([
|
|
43
|
+
for (const key of new Set([
|
|
44
|
+
...Object.keys(before ?? {}),
|
|
45
|
+
...Object.keys(after),
|
|
46
|
+
])) {
|
|
34
47
|
if (equal(before?.[key], after[key]))
|
|
35
48
|
continue;
|
|
36
49
|
const value = restore(before?.[key], after[key], current[key]);
|
|
@@ -39,14 +52,18 @@ function restore(before, after, current) {
|
|
|
39
52
|
else
|
|
40
53
|
result[key] = value;
|
|
41
54
|
}
|
|
42
|
-
return Object.keys(result).length === 0 && before === undefined
|
|
55
|
+
return Object.keys(result).length === 0 && before === undefined
|
|
56
|
+
? undefined
|
|
57
|
+
: result;
|
|
43
58
|
}
|
|
44
|
-
if (Array.isArray(after) &&
|
|
59
|
+
if (Array.isArray(after) &&
|
|
60
|
+
Array.isArray(current) &&
|
|
61
|
+
(before === undefined || Array.isArray(before))) {
|
|
45
62
|
const previous = before ?? [];
|
|
46
|
-
const added = after.filter(v => !previous.some((p) => equal(p, v)));
|
|
47
|
-
const result = current.filter(v => !added.some(a => equal(a, v)));
|
|
63
|
+
const added = after.filter((v) => !previous.some((p) => equal(p, v)));
|
|
64
|
+
const result = current.filter((v) => !added.some((a) => equal(a, v)));
|
|
48
65
|
for (const v of previous)
|
|
49
|
-
if (!after.some(a => equal(a, v)) && !result.some(a => equal(a, v)))
|
|
66
|
+
if (!after.some((a) => equal(a, v)) && !result.some((a) => equal(a, v)))
|
|
50
67
|
result.push(v);
|
|
51
68
|
return result.length === 0 && before === undefined ? undefined : result;
|
|
52
69
|
}
|
|
@@ -58,62 +75,128 @@ export function readConnectionState(workspace) {
|
|
|
58
75
|
return { schema: 1, files: {} };
|
|
59
76
|
const value = JSON.parse(text);
|
|
60
77
|
if (value.schema !== 1 || !object(value.files))
|
|
61
|
-
throw new Error(
|
|
78
|
+
throw new Error("Unsupported connector state");
|
|
62
79
|
for (const [file, snapshot] of Object.entries(value.files)) {
|
|
63
|
-
if (![
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
if (![
|
|
81
|
+
".mcp.json",
|
|
82
|
+
".codex/config.toml",
|
|
83
|
+
".codex/hooks.json",
|
|
84
|
+
".agents/hooks.json",
|
|
85
|
+
".agents/mcp_config.json",
|
|
86
|
+
".agents/skills/continue-chat/SKILL.md",
|
|
87
|
+
".claude/settings.local.json",
|
|
88
|
+
".claude/skills/continue-chat/SKILL.md",
|
|
89
|
+
".memory/connectors/claude-desktop.json",
|
|
90
|
+
].includes(file) ||
|
|
91
|
+
!object(snapshot) ||
|
|
92
|
+
typeof snapshot.after !== "string" ||
|
|
93
|
+
!(snapshot.before === null || typeof snapshot.before === "string"))
|
|
94
|
+
throw new Error("Invalid connector state");
|
|
66
95
|
}
|
|
67
96
|
return value;
|
|
68
97
|
}
|
|
69
98
|
export function planConnection(opts) {
|
|
70
|
-
if (process.platform ===
|
|
71
|
-
throw new Error(
|
|
99
|
+
if (process.platform === "win32")
|
|
100
|
+
throw new Error("Windows hook installation is not yet validated. Use WSL for this preview.");
|
|
72
101
|
const workspace = realpathSync(opts.workspace), app = resolve(opts.appRoot), node = opts.node ?? process.execPath;
|
|
73
|
-
if (!opts.clients.length ||
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
102
|
+
if (!opts.clients.length ||
|
|
103
|
+
opts.clients.some((c) => !clients.includes(c)))
|
|
104
|
+
throw new Error("Choose a supported client");
|
|
105
|
+
const extension = existsSync(join(app, "apps/server/src/main.js"))
|
|
106
|
+
? "js"
|
|
107
|
+
: "ts";
|
|
108
|
+
const server = join(app, "apps/server/src/main." + extension), runner = join(app, "packages/adapters/src/cli." + extension);
|
|
77
109
|
if (!existsSync(server) || !existsSync(runner))
|
|
78
|
-
throw new Error(
|
|
110
|
+
throw new Error("Runtime files missing");
|
|
79
111
|
const state = readConnectionState(workspace), changes = {}, expected = {};
|
|
80
|
-
const get = (path) => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
112
|
+
const get = (path) => {
|
|
113
|
+
const content = read(join(workspace, path));
|
|
114
|
+
expected[path] = content;
|
|
115
|
+
return content;
|
|
116
|
+
};
|
|
117
|
+
const json = (path) => {
|
|
118
|
+
const v = JSON.parse(get(path) ?? "{}");
|
|
119
|
+
if (!object(v))
|
|
120
|
+
throw new Error(path + " must be an object");
|
|
121
|
+
return v;
|
|
122
|
+
};
|
|
123
|
+
const env = {
|
|
124
|
+
MEMORY_DB: resolve(opts.database),
|
|
125
|
+
MEMORY_REPO: opts.repoId,
|
|
126
|
+
MEMORY_WORKSPACE: workspace,
|
|
127
|
+
MEMORY_SESSION_SELECTION: "1",
|
|
128
|
+
...(opts.captureNewSessions
|
|
129
|
+
? { MEMORY_CONVERSATION_MODE: "1", MEMORY_CAPTURE_NEW_SESSIONS: "1" }
|
|
130
|
+
: {}),
|
|
131
|
+
};
|
|
84
132
|
const quote = (s) => "'" + s.replaceAll("'", "'\\''") + "'";
|
|
85
|
-
const handler = (client, action) => ({
|
|
86
|
-
|
|
133
|
+
const handler = (client, action) => ({
|
|
134
|
+
type: "command",
|
|
135
|
+
timeout: 10,
|
|
136
|
+
command: [
|
|
137
|
+
"env",
|
|
138
|
+
...Object.entries({
|
|
139
|
+
...env,
|
|
140
|
+
MEMORY_QUEUE: join(dirname(opts.database), "queue", client),
|
|
141
|
+
MEMORY_MANAGED: "shared-agent-memory",
|
|
142
|
+
}).map(([k, v]) => k + "=" + v),
|
|
143
|
+
node,
|
|
144
|
+
runner,
|
|
145
|
+
action,
|
|
146
|
+
client,
|
|
147
|
+
]
|
|
148
|
+
.map(quote)
|
|
149
|
+
.join(" "),
|
|
150
|
+
});
|
|
151
|
+
const connection = (desktop = false, client = "claude") => {
|
|
152
|
+
const clientEnv = {
|
|
153
|
+
...env,
|
|
154
|
+
...(desktop ? { MEMORY_CHAT_CLIENT: "claude-desktop" } : {}),
|
|
155
|
+
...(!desktop && opts.captureNewSessions ? { MEMORY_CLIENT: client } : {}),
|
|
156
|
+
};
|
|
157
|
+
if (desktop) {
|
|
158
|
+
delete clientEnv.MEMORY_CONVERSATION_MODE;
|
|
159
|
+
delete clientEnv.MEMORY_CAPTURE_NEW_SESSIONS;
|
|
160
|
+
}
|
|
161
|
+
return { command: node, args: [server], env: clientEnv };
|
|
162
|
+
};
|
|
87
163
|
function owned(path, location, current) {
|
|
88
164
|
if (current === undefined)
|
|
89
165
|
return;
|
|
90
166
|
const prior = state.files[path];
|
|
91
|
-
const previous = prior
|
|
167
|
+
const previous = prior
|
|
168
|
+
? location.reduce((v, key) => v?.[key], JSON.parse(prior.after))
|
|
169
|
+
: undefined;
|
|
92
170
|
if (!equal(current, previous))
|
|
93
|
-
throw new Error(
|
|
171
|
+
throw new Error("Unmanaged or modified connector conflict in " + path);
|
|
94
172
|
}
|
|
95
|
-
function mcp(path, desktop = false) {
|
|
173
|
+
function mcp(path, desktop = false, client = "claude") {
|
|
96
174
|
const value = json(path);
|
|
97
175
|
value.mcpServers ??= {};
|
|
98
|
-
owned(path, [
|
|
99
|
-
value.mcpServers[
|
|
176
|
+
owned(path, ["mcpServers", "shared-memory"], value.mcpServers["shared-memory"]);
|
|
177
|
+
value.mcpServers["shared-memory"] = connection(desktop, client);
|
|
100
178
|
changes[path] = dump(value);
|
|
101
179
|
}
|
|
102
180
|
function hooks(path, client) {
|
|
103
181
|
const value = json(path);
|
|
104
182
|
value.hooks ??= {};
|
|
105
|
-
const prior = state.files[path]
|
|
106
|
-
|
|
183
|
+
const prior = state.files[path]
|
|
184
|
+
? JSON.parse(state.files[path].after).hooks
|
|
185
|
+
: {};
|
|
186
|
+
for (const [event, action] of [
|
|
187
|
+
["SessionStart", "recall"],
|
|
188
|
+
["Stop", "capture"],
|
|
189
|
+
]) {
|
|
107
190
|
const old = prior?.[event] ?? [];
|
|
108
191
|
const groups = value.hooks[event] ?? [];
|
|
109
192
|
if (!Array.isArray(groups))
|
|
110
|
-
throw new Error(
|
|
193
|
+
throw new Error("Invalid hooks in " + path);
|
|
111
194
|
// Only remove exact previously generated handlers. Edited handlers remain;
|
|
112
195
|
// reject if they mention our runner to avoid duplicate capture.
|
|
113
|
-
const oldOwned = old.filter((g) => g.hooks?.some((h) => h.command?.includes(
|
|
196
|
+
const oldOwned = old.filter((g) => g.hooks?.some((h) => h.command?.includes("MEMORY_MANAGED=shared-agent-memory")));
|
|
114
197
|
const kept = groups.filter((g) => !oldOwned.some((p) => equal(p, g)));
|
|
115
|
-
if (kept.some((g) => g.hooks?.some((h) => h.command?.includes(
|
|
116
|
-
throw new Error(
|
|
198
|
+
if (kept.some((g) => g.hooks?.some((h) => h.command?.includes("MEMORY_MANAGED=shared-agent-memory"))))
|
|
199
|
+
throw new Error("Modified hook conflict in " + path);
|
|
117
200
|
value.hooks[event] = [...kept, { hooks: [handler(client, action)] }];
|
|
118
201
|
}
|
|
119
202
|
changes[path] = dump(value);
|
|
@@ -121,45 +204,72 @@ export function planConnection(opts) {
|
|
|
121
204
|
function skill(path) {
|
|
122
205
|
const current = get(path);
|
|
123
206
|
if (current && current !== state.files[path]?.after)
|
|
124
|
-
throw new Error(
|
|
125
|
-
changes[path] = readFileSync(join(app,
|
|
207
|
+
throw new Error("Unmanaged or modified skill conflict");
|
|
208
|
+
changes[path] = readFileSync(join(app, "skills/continue-chat/SKILL.md"), "utf8");
|
|
126
209
|
}
|
|
127
210
|
for (const client of opts.clients) {
|
|
128
|
-
if (client ===
|
|
129
|
-
mcp(
|
|
130
|
-
hooks(
|
|
131
|
-
skill(
|
|
211
|
+
if (client === "claude-code") {
|
|
212
|
+
mcp(".mcp.json");
|
|
213
|
+
hooks(".claude/settings.local.json", "claude");
|
|
214
|
+
skill(".claude/skills/continue-chat/SKILL.md");
|
|
132
215
|
}
|
|
133
|
-
if (client ===
|
|
134
|
-
mcp(
|
|
135
|
-
if (client ===
|
|
136
|
-
mcp(
|
|
137
|
-
const path =
|
|
138
|
-
owned(path, [
|
|
139
|
-
value[
|
|
216
|
+
if (client === "claude-desktop")
|
|
217
|
+
mcp(".memory/connectors/claude-desktop.json", true);
|
|
218
|
+
if (client === "antigravity") {
|
|
219
|
+
mcp(".agents/mcp_config.json", false, "antigravity");
|
|
220
|
+
const path = ".agents/hooks.json", value = json(path);
|
|
221
|
+
owned(path, ["shared-memory"], value["shared-memory"]);
|
|
222
|
+
value["shared-memory"] = {
|
|
223
|
+
SessionStart: [handler("antigravity", "recall")],
|
|
224
|
+
Stop: [handler("antigravity", "capture")],
|
|
225
|
+
};
|
|
140
226
|
changes[path] = dump(value);
|
|
141
227
|
}
|
|
142
|
-
if (client ===
|
|
143
|
-
skill(
|
|
144
|
-
hooks(
|
|
145
|
-
const path =
|
|
228
|
+
if (client === "codex") {
|
|
229
|
+
skill(".agents/skills/continue-chat/SKILL.md");
|
|
230
|
+
hooks(".codex/hooks.json", "codex");
|
|
231
|
+
const path = ".codex/config.toml", current = get(path) ?? "";
|
|
146
232
|
const parsed = parseToml(current);
|
|
147
233
|
let base = current;
|
|
148
234
|
if (current.includes(begin)) {
|
|
149
235
|
const previous = state.files[path];
|
|
150
236
|
const block = current.slice(current.indexOf(begin), current.indexOf(end) + end.length);
|
|
151
237
|
if (!previous || !previous.after.includes(block))
|
|
152
|
-
throw new Error(
|
|
153
|
-
base = current.replace(block,
|
|
238
|
+
throw new Error("Modified or unmanaged Codex config conflict");
|
|
239
|
+
base = current.replace(block, "");
|
|
154
240
|
}
|
|
155
|
-
else if (parsed.mcp_servers?.[
|
|
156
|
-
throw new Error(
|
|
157
|
-
changes[path] =
|
|
241
|
+
else if (parsed.mcp_servers?.["shared-memory"])
|
|
242
|
+
throw new Error("Unmanaged Codex server conflict");
|
|
243
|
+
changes[path] =
|
|
244
|
+
base.trimEnd() +
|
|
245
|
+
"\n\n" +
|
|
246
|
+
begin +
|
|
247
|
+
'\n[mcp_servers.shared-memory]\ndefault_tools_approval_mode = "writes"\ncommand = ' +
|
|
248
|
+
JSON.stringify(node) +
|
|
249
|
+
"\nargs = " +
|
|
250
|
+
JSON.stringify([server]) +
|
|
251
|
+
"\ncwd = " +
|
|
252
|
+
JSON.stringify(workspace) +
|
|
253
|
+
"\nenv = { " +
|
|
254
|
+
Object.entries({
|
|
255
|
+
...env,
|
|
256
|
+
...(opts.captureNewSessions ? { MEMORY_CLIENT: "codex" } : {}),
|
|
257
|
+
})
|
|
258
|
+
.map(([k, v]) => k + " = " + JSON.stringify(v))
|
|
259
|
+
.join(", ") +
|
|
260
|
+
' }\ntools.choose_conversation.approval_mode = "approve"\n' +
|
|
261
|
+
end +
|
|
262
|
+
"\n";
|
|
158
263
|
parseToml(changes[path]);
|
|
159
264
|
}
|
|
160
265
|
}
|
|
161
266
|
for (const [path, after] of Object.entries(changes))
|
|
162
|
-
state.files[path] = {
|
|
267
|
+
state.files[path] = {
|
|
268
|
+
before: state.files[path]
|
|
269
|
+
? unapply(path, state.files[path], expected[path] ?? null)
|
|
270
|
+
: (expected[path] ?? null),
|
|
271
|
+
after,
|
|
272
|
+
};
|
|
163
273
|
expected[statePath] = read(join(workspace, statePath));
|
|
164
274
|
changes[statePath] = dump(state);
|
|
165
275
|
return { workspace, changes, expected };
|
|
@@ -168,7 +278,7 @@ export function applyConnectionPlan(plan) {
|
|
|
168
278
|
// Optimistic concurrency prevents applying a preview over later user edits.
|
|
169
279
|
for (const [path, expected] of Object.entries(plan.expected))
|
|
170
280
|
if (read(join(plan.workspace, path)) !== expected)
|
|
171
|
-
throw new Error(
|
|
281
|
+
throw new Error("Configuration changed since preview: " + path);
|
|
172
282
|
const written = [];
|
|
173
283
|
try {
|
|
174
284
|
for (const [path, content] of Object.entries(plan.changes)) {
|
|
@@ -190,25 +300,39 @@ export function applyConnectionPlan(plan) {
|
|
|
190
300
|
throw error;
|
|
191
301
|
}
|
|
192
302
|
try {
|
|
193
|
-
const git = execFileSync(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
303
|
+
const git = execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
|
|
304
|
+
cwd: plan.workspace,
|
|
305
|
+
encoding: "utf8",
|
|
306
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
307
|
+
}).trim();
|
|
308
|
+
const path = join(git, "info/exclude"), current = read(path) ?? "";
|
|
309
|
+
const entries = [
|
|
310
|
+
"/.memory/",
|
|
311
|
+
...Object.keys(plan.changes)
|
|
312
|
+
.filter((p) => !p.startsWith(".memory/"))
|
|
313
|
+
.map((p) => "/" + p),
|
|
314
|
+
];
|
|
315
|
+
atomicWrite(path, current +
|
|
316
|
+
"\n" +
|
|
317
|
+
entries.filter((p) => !current.split("\n").includes(p)).join("\n") +
|
|
318
|
+
"\n");
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
/* Non-Git projects can use the planner; CLI validates Git separately. */
|
|
197
322
|
}
|
|
198
|
-
catch { /* Non-Git projects can use the planner; CLI validates Git separately. */ }
|
|
199
323
|
}
|
|
200
324
|
function unapply(path, snapshot, current) {
|
|
201
325
|
const { before, after } = snapshot;
|
|
202
326
|
if (current === null || current === after)
|
|
203
327
|
return current === null ? null : before;
|
|
204
|
-
if (path.endsWith(
|
|
328
|
+
if (path.endsWith(".json")) {
|
|
205
329
|
const restored = restore(before === null ? undefined : JSON.parse(before), JSON.parse(after), JSON.parse(current));
|
|
206
330
|
return restored === undefined ? null : dump(restored);
|
|
207
331
|
}
|
|
208
|
-
if (path.endsWith(
|
|
332
|
+
if (path.endsWith(".toml")) {
|
|
209
333
|
const block = after.slice(after.indexOf(begin), after.indexOf(end) + end.length);
|
|
210
334
|
if (current.includes(block))
|
|
211
|
-
return current.replace(block,
|
|
335
|
+
return current.replace(block, "").trim() || before;
|
|
212
336
|
}
|
|
213
337
|
return current;
|
|
214
338
|
}
|
|
@@ -223,4 +347,6 @@ export function planUninstall(workspace) {
|
|
|
223
347
|
changes[statePath] = null;
|
|
224
348
|
return { workspace, changes, expected };
|
|
225
349
|
}
|
|
226
|
-
export function uninstallConnections(workspace) {
|
|
350
|
+
export function uninstallConnections(workspace) {
|
|
351
|
+
applyConnectionPlan(planUninstall(workspace));
|
|
352
|
+
}
|
|
@@ -1,40 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, mkdtempSync, writeFileSync, } from "node:fs";
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { atomicWrite } from "./connections.js";
|
|
8
|
+
export const packageName = "@tinchu0/knitt";
|
|
8
9
|
export function defaultHome() {
|
|
9
10
|
if (process.env.SHARED_MEMORY_HOME)
|
|
10
11
|
return resolve(process.env.SHARED_MEMORY_HOME);
|
|
11
|
-
if (process.platform ===
|
|
12
|
-
return join(homedir(),
|
|
13
|
-
if (process.platform ===
|
|
14
|
-
return join(process.env.LOCALAPPDATA ?? join(homedir(),
|
|
15
|
-
return join(process.env.XDG_DATA_HOME ?? join(homedir(),
|
|
12
|
+
if (process.platform === "darwin")
|
|
13
|
+
return join(homedir(), "Library/Application Support/SharedAgentMemory");
|
|
14
|
+
if (process.platform === "win32")
|
|
15
|
+
return join(process.env.LOCALAPPDATA ?? join(homedir(), "AppData/Local"), "SharedAgentMemory");
|
|
16
|
+
return join(process.env.XDG_DATA_HOME ?? join(homedir(), ".local/share"), "shared-agent-memory");
|
|
17
|
+
}
|
|
18
|
+
export function runtimePaths(home) {
|
|
19
|
+
return {
|
|
20
|
+
data: join(home, "data"),
|
|
21
|
+
versions: join(home, "versions"),
|
|
22
|
+
state: join(home, "installation.json"),
|
|
23
|
+
};
|
|
16
24
|
}
|
|
17
|
-
export function runtimePaths(home) { return { data: join(home, 'data'), versions: join(home, 'versions'), state: join(home, 'installation.json') }; }
|
|
18
25
|
export function readInstallation(home) {
|
|
19
26
|
const path = runtimePaths(home).state;
|
|
20
27
|
if (!existsSync(path))
|
|
21
28
|
return null;
|
|
22
|
-
const value = JSON.parse(readFileSync(path,
|
|
23
|
-
if (value.schema !== 1 ||
|
|
24
|
-
|
|
29
|
+
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
30
|
+
if (value.schema !== 1 ||
|
|
31
|
+
!value.current?.appRoot ||
|
|
32
|
+
!Array.isArray(value.projects))
|
|
33
|
+
throw new Error("Invalid installation metadata");
|
|
25
34
|
return value;
|
|
26
35
|
}
|
|
27
36
|
export async function withInstallLock(home, work) {
|
|
28
37
|
mkdirSync(home, { recursive: true, mode: 0o700 });
|
|
29
|
-
const lock = join(home,
|
|
38
|
+
const lock = join(home, "install.lock");
|
|
30
39
|
try {
|
|
31
40
|
mkdirSync(lock, { mode: 0o700 });
|
|
32
41
|
}
|
|
33
42
|
catch {
|
|
34
|
-
throw new Error(
|
|
43
|
+
throw new Error("Another installation is running, or a stale install.lock remains. Check for a running setup process before removing " +
|
|
44
|
+
lock);
|
|
35
45
|
}
|
|
36
46
|
try {
|
|
37
|
-
writeFileSync(join(lock,
|
|
47
|
+
writeFileSync(join(lock, "owner.json"), JSON.stringify({ pid: process.pid, started: new Date().toISOString() }), { mode: 0o600 });
|
|
38
48
|
return await work();
|
|
39
49
|
}
|
|
40
50
|
finally {
|
|
@@ -42,29 +52,48 @@ export async function withInstallLock(home, work) {
|
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
54
|
export function installRuntime(home, spec) {
|
|
45
|
-
if (process.platform ===
|
|
46
|
-
throw new Error(
|
|
47
|
-
if (Number(process.versions.node.split(
|
|
48
|
-
throw new Error(
|
|
55
|
+
if (process.platform === "win32")
|
|
56
|
+
throw new Error("Windows installer validation is pending; use WSL for this preview.");
|
|
57
|
+
if (Number(process.versions.node.split(".")[0]) < 24)
|
|
58
|
+
throw new Error("Node.js 24 or newer is required");
|
|
49
59
|
const paths = runtimePaths(home);
|
|
50
60
|
mkdirSync(paths.versions, { recursive: true, mode: 0o700 });
|
|
51
61
|
mkdirSync(paths.data, { recursive: true, mode: 0o700 });
|
|
52
|
-
const staging = mkdtempSync(join(paths.versions,
|
|
62
|
+
const staging = mkdtempSync(join(paths.versions, ".staging-"));
|
|
53
63
|
try {
|
|
54
|
-
writeFileSync(join(staging,
|
|
64
|
+
writeFileSync(join(staging, "package.json"), JSON.stringify({ name: "shared-memory-runtime", private: true }), { mode: 0o600 });
|
|
55
65
|
// Arguments are passed directly; no shell interpolation or install lifecycle scripts.
|
|
56
|
-
execFileSync(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
execFileSync("npm", [
|
|
67
|
+
"install",
|
|
68
|
+
"--prefix",
|
|
69
|
+
staging,
|
|
70
|
+
"--ignore-scripts",
|
|
71
|
+
"--omit=dev",
|
|
72
|
+
"--no-audit",
|
|
73
|
+
"--no-fund",
|
|
74
|
+
"--save-exact",
|
|
75
|
+
"--",
|
|
76
|
+
spec,
|
|
77
|
+
], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 180_000 });
|
|
78
|
+
const root = join(staging, "node_modules", packageName);
|
|
79
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
80
|
+
if (pkg.name !== packageName ||
|
|
81
|
+
!/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(pkg.version))
|
|
82
|
+
throw new Error("Unexpected runtime package");
|
|
83
|
+
for (const file of [
|
|
84
|
+
"apps/server/src/main.js",
|
|
85
|
+
"packages/installer/src/cli.js",
|
|
86
|
+
])
|
|
87
|
+
if (!existsSync(join(root, "dist", file)))
|
|
88
|
+
throw new Error("Incomplete runtime: " + file);
|
|
89
|
+
execFileSync(process.execPath, [join(root, "dist/packages/installer/src/cli.js"), "--version"], { encoding: "utf8", timeout: 10_000 });
|
|
90
|
+
const destination = join(paths.versions, pkg.version + "-" + randomUUID());
|
|
66
91
|
renameSync(staging, destination);
|
|
67
|
-
return {
|
|
92
|
+
return {
|
|
93
|
+
version: pkg.version,
|
|
94
|
+
node: process.execPath,
|
|
95
|
+
appRoot: join(destination, "node_modules", packageName, "dist"),
|
|
96
|
+
};
|
|
68
97
|
}
|
|
69
98
|
finally {
|
|
70
99
|
rmSync(staging, { recursive: true, force: true });
|
|
@@ -72,5 +101,33 @@ export function installRuntime(home, spec) {
|
|
|
72
101
|
}
|
|
73
102
|
export function activateRuntime(home, current, projects) {
|
|
74
103
|
const previous = readInstallation(home);
|
|
75
|
-
atomicWrite(runtimePaths(home).state, JSON.stringify({
|
|
104
|
+
atomicWrite(runtimePaths(home).state, JSON.stringify({
|
|
105
|
+
schema: 1,
|
|
106
|
+
current,
|
|
107
|
+
previous: previous?.current,
|
|
108
|
+
projects,
|
|
109
|
+
}, null, 2) + "\n");
|
|
110
|
+
}
|
|
111
|
+
export function assertRuntimeCompatibility(runtime, projects) {
|
|
112
|
+
const marker = join(runtime.appRoot, "memory-schema.json");
|
|
113
|
+
const supported = existsSync(marker)
|
|
114
|
+
? JSON.parse(readFileSync(marker, "utf8")).supportedSchema
|
|
115
|
+
: 0;
|
|
116
|
+
if (!Number.isInteger(supported) || supported < 0)
|
|
117
|
+
throw new Error("Invalid runtime schema compatibility metadata");
|
|
118
|
+
for (const database of new Set(projects.map((p) => p.database))) {
|
|
119
|
+
if (!existsSync(database))
|
|
120
|
+
continue;
|
|
121
|
+
const db = new DatabaseSync(database, { readOnly: true });
|
|
122
|
+
try {
|
|
123
|
+
const actual = Number(db.prepare("PRAGMA user_version").get().user_version);
|
|
124
|
+
if (actual > supported)
|
|
125
|
+
throw new Error("Runtime is incompatible with memory schema " +
|
|
126
|
+
actual +
|
|
127
|
+
". Use a compatible runtime or explicitly restore a consistent backup; no data was changed.");
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
db.close();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
76
133
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { checkpointEntrySchema } from "../../contracts/src/conversations.js";
|
|
4
|
+
import { redact } from "../../adapters/src/hooks.js";
|
|
5
|
+
export function latestCheckpoint(db, repoId, conversationId, throughSeq = Number.MAX_SAFE_INTEGER, checkpointSeq = Number.MAX_SAFE_INTEGER) {
|
|
6
|
+
const row = db
|
|
7
|
+
.prepare("SELECT payload FROM checkpoints WHERE repo=? AND conversation=? AND through_seq<=? AND seq<=? ORDER BY seq DESC LIMIT 1")
|
|
8
|
+
.get(repoId, conversationId, throughSeq, checkpointSeq);
|
|
9
|
+
return row ? JSON.parse(String(row.payload)) : null;
|
|
10
|
+
}
|
|
11
|
+
export function saveCheckpoint(db, store, key, input) {
|
|
12
|
+
const entries = z.array(checkpointEntrySchema).min(1).max(100).parse(input);
|
|
13
|
+
const conversation = store.sessionConversation(key);
|
|
14
|
+
db.exec("BEGIN IMMEDIATE");
|
|
15
|
+
try {
|
|
16
|
+
const rows = store.conversationRows(key), permitted = new Set(rows.map((r) => String(JSON.parse(String(r.payload)).eventId)));
|
|
17
|
+
const ids = new Set();
|
|
18
|
+
const previous = latestCheckpoint(db, key.repoId, conversation.conversationId);
|
|
19
|
+
const active = new Map((previous?.entries ?? [])
|
|
20
|
+
.filter((e) => e.evidenceIds.every((id) => permitted.has(id)))
|
|
21
|
+
.map((e) => [e.id, e]));
|
|
22
|
+
for (const original of entries) {
|
|
23
|
+
const entry = { ...original, content: redact(original.content) };
|
|
24
|
+
if (ids.has(entry.id))
|
|
25
|
+
throw new Error("Duplicate checkpoint entry ID");
|
|
26
|
+
ids.add(entry.id);
|
|
27
|
+
if (entry.status === "verified")
|
|
28
|
+
throw new Error("No trusted execution evidence supports verified status; use reported");
|
|
29
|
+
if (!entry.evidenceIds.every((id) => permitted.has(id)))
|
|
30
|
+
throw new Error("Checkpoint evidence is unavailable in this conversation");
|
|
31
|
+
if (db
|
|
32
|
+
.prepare("SELECT 1 FROM retired_checkpoint_entries WHERE repo=? AND conversation=? AND id=?")
|
|
33
|
+
.get(key.repoId, conversation.conversationId, entry.id))
|
|
34
|
+
throw new Error("Checkpoint entry was retired by supersession; use a new ID");
|
|
35
|
+
const old = active.get(entry.id);
|
|
36
|
+
if (old && JSON.stringify(old) !== JSON.stringify(entry))
|
|
37
|
+
throw new Error("Entry ID conflict; supersede the old entry explicitly");
|
|
38
|
+
if (old)
|
|
39
|
+
continue;
|
|
40
|
+
if (entry.supersedes) {
|
|
41
|
+
if (entry.supersedes === entry.id || !active.has(entry.supersedes))
|
|
42
|
+
throw new Error("Unknown superseded checkpoint entry");
|
|
43
|
+
active.delete(entry.supersedes);
|
|
44
|
+
db.prepare("INSERT OR IGNORE INTO retired_checkpoint_entries VALUES(?,?,?)").run(key.repoId, conversation.conversationId, entry.supersedes);
|
|
45
|
+
}
|
|
46
|
+
active.set(entry.id, entry);
|
|
47
|
+
}
|
|
48
|
+
const throughSeq = rows
|
|
49
|
+
.filter((r) => r.conversation === conversation.conversationId)
|
|
50
|
+
.reduce((max, r) => Math.max(max, Number(r.seq)), 0);
|
|
51
|
+
const body = {
|
|
52
|
+
conversationId: conversation.conversationId,
|
|
53
|
+
throughSeq,
|
|
54
|
+
entries: [...active.values()],
|
|
55
|
+
};
|
|
56
|
+
const checkpointId = createHash("sha256")
|
|
57
|
+
.update(JSON.stringify(body))
|
|
58
|
+
.digest("hex");
|
|
59
|
+
const checkpoint = { checkpointId, ...body };
|
|
60
|
+
db.prepare("INSERT OR IGNORE INTO checkpoints(repo,conversation,id,through_seq,payload) VALUES(?,?,?,?,?)").run(key.repoId, conversation.conversationId, checkpointId, throughSeq, JSON.stringify(checkpoint));
|
|
61
|
+
db.exec("COMMIT");
|
|
62
|
+
return checkpoint;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
db.exec("ROLLBACK");
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|