@staix/agent-mem 0.29.0 → 0.31.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/plugin.json +1 -1
- package/README.md +20 -574
- package/package.json +3 -2
- package/src/cli-agent-reads.ts +17 -2
- package/src/cli.ts +178 -21
- package/src/commands-table.ts +14 -2
- package/src/index.ts +1 -0
- package/src/ingest.ts +7 -5
- package/src/mcp-memory.ts +36 -8
- package/src/memory/bridge.ts +26 -1
- package/src/memory/candidates.ts +17 -1
- package/src/memory/capacity.ts +5 -2
- package/src/memory/catalog.ts +10 -1
- package/src/memory/commands.ts +2 -1
- package/src/memory/config.ts +13 -1
- package/src/memory/decay-review.ts +20 -0
- package/src/memory/doctor.ts +133 -47
- package/src/memory/eval-audit.ts +442 -0
- package/src/memory/eval.ts +66 -0
- package/src/memory/formats.ts +4 -3
- package/src/memory/intent-brief.ts +34 -6
- package/src/memory/metrics.ts +173 -14
- package/src/memory/native-roots.ts +28 -0
- package/src/memory/platform.ts +9 -1
- package/src/memory/review-targets.ts +69 -0
- package/src/memory/shard-bridge.ts +18 -6
- package/src/memory/shard-migration.ts +14 -1
- package/src/memory/shard-sync-format.ts +18 -0
- package/src/memory/shard-sync.ts +52 -4
- package/src/memory/shard.ts +4 -0
- package/src/memory/store.ts +4 -2
- package/src/memory/sync-placement-bound.ts +21 -4
- package/src/memory/sync-placement-cache.ts +16 -1
- package/src/memory/sync-placement-capture.ts +14 -2
- package/src/memory/sync-placement-checkpoint-format.ts +199 -0
- package/src/memory/sync-placement-checkpoint-person.ts +209 -0
- package/src/memory/sync-placement-checkpoint-prospective.ts +141 -0
- package/src/memory/sync-placement-checkpoint-storage.ts +218 -0
- package/src/memory/sync-placement-checkpoint.ts +234 -0
- package/src/memory/sync-placement-dependencies.ts +42 -9
- package/src/memory/sync-placement-effective-writers.ts +12 -6
- package/src/memory/sync-placement-git.ts +17 -7
- package/src/memory/sync-placement-inspection-evidence.ts +1 -1
- package/src/memory/sync-placement-inventory.ts +38 -11
- package/src/memory/sync-placement-journal-format.ts +4 -1
- package/src/memory/sync-placement-journal-storage.ts +129 -26
- package/src/memory/sync-placement-materialize.ts +33 -19
- package/src/memory/sync-placement-projection.ts +7 -1
- package/src/memory/sync-placement-publication-events.ts +42 -0
- package/src/memory/sync-placement-publication-plan.ts +18 -1
- package/src/memory/sync-placement-realm-admission.ts +20 -3
- package/src/memory/sync-placement-realm-discovery.ts +42 -0
- package/src/memory/sync-placement-remote-admission.ts +190 -0
- package/src/memory/sync-placement-remote-adoption-format.ts +54 -0
- package/src/memory/sync-placement-remote-materialization.ts +49 -0
- package/src/memory/sync-placement-remote-refusal.ts +21 -0
- package/src/memory/sync-placement-remote-stage.ts +275 -0
- package/src/memory/sync-placement-transport.ts +207 -53
- package/src/memory/sync-placement-writer-history.ts +75 -17
- package/src/memory/sync-placement-writer-renewal.ts +18 -10
- package/src/memory/sync-placement-writer-revision-format.ts +16 -13
- package/src/memory/sync-placement-yield.ts +14 -0
- package/src/memory/sync-progress.ts +1 -1
- package/src/memory/sync.ts +5 -2
- package/src/memory/use-telemetry.ts +15 -7
- package/src/setup-drift.ts +12 -2
- package/src/setup-owned.ts +42 -0
- package/src/setup.ts +32 -11
- package/src/sync-placement-writer-context.ts +5 -5
- package/src/ui/plugin.ts +26 -11
- package/src/ui/snapshot-cache.ts +24 -11
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/** Private, fenced staging for remote placement adoption; never rewrites an existing curated log. */
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, opendirSync, readSync, readdirSync, renameSync, rmdirSync, unlinkSync, writeSync, type BigIntStats } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { canonicalJSON, fsyncDirectory } from "./generation.ts";
|
|
6
|
+
import { assertMutationHandle } from "./mutation.ts";
|
|
7
|
+
import { realPath } from "./platform.ts";
|
|
8
|
+
import { assertCuratedShardLayout, ensureShardDescriptor, shardDir } from "./shard.ts";
|
|
9
|
+
import type { RealmIntentProof } from "./sync-placement-realm-intent-storage.ts";
|
|
10
|
+
import { REMOTE_ADOPTION_MAX_BYTES, validateRemoteAdoption, type RemoteAdoption } from "./sync-placement-remote-adoption-format.ts";
|
|
11
|
+
import type { RemoteStagedPack } from "./sync-placement-remote-materialization.ts";
|
|
12
|
+
import { RemoteRealmRefusal } from "./sync-placement-remote-refusal.ts";
|
|
13
|
+
|
|
14
|
+
const fail = (): never => { throw new RemoteRealmRefusal("staging"); };
|
|
15
|
+
const sha = (bytes: Uint8Array | string) => createHash("sha256").update(bytes).digest("hex");
|
|
16
|
+
const stamp = (s: BigIntStats) => [s.dev, s.ino, s.uid, s.mode, s.nlink, s.size, s.mtimeNs, s.ctimeNs].map(String).join(":");
|
|
17
|
+
const companion = (home: string, plan: RemoteAdoption) => join(home, `placement-remote-adoption-${plan.keyHash}.json`);
|
|
18
|
+
const staging = (home: string, plan: RemoteAdoption) => join(home, `.placement-remote-adoption-${plan.keyHash}-${plan.seal}.stage`);
|
|
19
|
+
function ownedFile(s: BigIntStats): void {
|
|
20
|
+
if (!s.isFile() || s.isSymbolicLink() || s.nlink !== 1n || s.uid !== BigInt(process.getuid?.() ?? -1) || (s.mode & 0o777n) !== 0o600n) fail();
|
|
21
|
+
}
|
|
22
|
+
function ownedDir(dir: string): BigIntStats {
|
|
23
|
+
const s = lstatSync(dir, { bigint: true });
|
|
24
|
+
if (!s.isDirectory() || s.isSymbolicLink() || s.uid !== BigInt(process.getuid?.() ?? -1) || (s.mode & 0o077n) !== 0n) fail();
|
|
25
|
+
return s;
|
|
26
|
+
}
|
|
27
|
+
/** A not-local scaffold may hold work/capture, but no pre-existing curated state may be replaced. */
|
|
28
|
+
function rejectExistingCurated(dir: string): void {
|
|
29
|
+
const fixed = new Set(["log", "local", "legacy", "legacy-main", "generation.json", "generation-seal.json", "migration-sources.json", "machine.json",
|
|
30
|
+
"prune-translations.jsonl", "prune-plans", "public-prune-objects", "public-prune-inputs", "public-prune-input-sets"]);
|
|
31
|
+
for (const name of readdirSync(dir)) {
|
|
32
|
+
if (fixed.has(name) || /^index(?:-v[0-9]+(?:-i[0-9]+)?)?\.db(?:-wal|-shm|-journal)?$/.test(name)
|
|
33
|
+
|| /^search\.db(?:-wal|-shm|-journal)?$/.test(name))
|
|
34
|
+
throw new RemoteRealmRefusal("existing", name);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function check(home: string, proof: RealmIntentProof): void {
|
|
38
|
+
assertMutationHandle(proof.mutation, home); proof.assertCurrent(); assertMutationHandle(proof.mutation, home);
|
|
39
|
+
}
|
|
40
|
+
function readOwned(file: string, max: number): Buffer | undefined {
|
|
41
|
+
const before = lstatSync(file, { bigint: true, throwIfNoEntry: false }); if (!before) return undefined;
|
|
42
|
+
ownedFile(before); if (before.size > BigInt(max)) return fail();
|
|
43
|
+
const fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
44
|
+
try {
|
|
45
|
+
const opened = fstatSync(fd, { bigint: true }); ownedFile(opened); if (stamp(opened) !== stamp(before)) fail();
|
|
46
|
+
const bytes = Buffer.alloc(Number(opened.size)); let at = 0;
|
|
47
|
+
while (at < bytes.length) { const n = readSync(fd, bytes, at, bytes.length - at, at); if (!n) fail(); at += n; }
|
|
48
|
+
if (stamp(fstatSync(fd, { bigint: true })) !== stamp(opened) || stamp(lstatSync(file, { bigint: true })) !== stamp(opened)) fail();
|
|
49
|
+
return bytes;
|
|
50
|
+
} finally { closeSync(fd); }
|
|
51
|
+
}
|
|
52
|
+
/** Complete only an existing exact prefix or a file created exclusively by this operation. */
|
|
53
|
+
function complete(home: string, file: string, expected: Buffer, proof: RealmIntentProof): void {
|
|
54
|
+
check(home, proof);
|
|
55
|
+
let fd: number;
|
|
56
|
+
try { fd = openSync(file, constants.O_RDWR | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
|
|
57
|
+
catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; fd = openSync(file, constants.O_RDWR | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
|
|
58
|
+
try {
|
|
59
|
+
const before = fstatSync(fd, { bigint: true }); ownedFile(before); if (before.size > BigInt(expected.length)) fail();
|
|
60
|
+
const saved = Buffer.alloc(Number(before.size)); let at = 0;
|
|
61
|
+
while (at < saved.length) { const n = readSync(fd, saved, at, saved.length - at, at); if (!n) fail(); at += n; }
|
|
62
|
+
if (!saved.equals(expected.subarray(0, saved.length)) || stamp(lstatSync(file, { bigint: true })) !== stamp(before)) fail();
|
|
63
|
+
check(home, proof);
|
|
64
|
+
for (let pos = saved.length; pos < expected.length;) { const n = writeSync(fd, expected, pos, expected.length - pos, pos); if (n <= 0) fail(); pos += n; }
|
|
65
|
+
fsyncSync(fd); const after = fstatSync(fd, { bigint: true }); ownedFile(after);
|
|
66
|
+
if (after.size !== BigInt(expected.length) || stamp(lstatSync(file, { bigint: true })) !== stamp(after)) fail();
|
|
67
|
+
} finally { closeSync(fd); }
|
|
68
|
+
check(home, proof);
|
|
69
|
+
if (!readOwned(file, expected.length)?.equals(expected)) fail();
|
|
70
|
+
}
|
|
71
|
+
function ensureDir(home: string, dir: string, parent: string, proof: RealmIntentProof): void {
|
|
72
|
+
check(home, proof); if (!lstatSync(dir, { throwIfNoEntry: false })) { mkdirSync(dir, { mode: 0o700 }); fsyncDirectory(parent); }
|
|
73
|
+
ownedDir(dir); check(home, proof);
|
|
74
|
+
}
|
|
75
|
+
export interface RemoteAdoptionPending { name: string; size: number; hash: string; stamp: string }
|
|
76
|
+
/** Metadata-only inventory; one remote adoption may remain outstanding per home. */
|
|
77
|
+
export function pendingRemoteAdoptions(homeInput: string): { hashes: readonly string[]; unknown: boolean } {
|
|
78
|
+
const home = realPath(homeInput), hashes = new Set<string>(); let unknown = false, count = 0;
|
|
79
|
+
try {
|
|
80
|
+
const dir = opendirSync(home);
|
|
81
|
+
try {
|
|
82
|
+
for (;;) {
|
|
83
|
+
const entry = dir.readSync(); if (!entry) break; if (++count > 20_000) return { hashes: [], unknown: true };
|
|
84
|
+
const hidden = entry.name.startsWith(".placement-remote-adoption-"), visible = entry.name.startsWith("placement-remote-adoption-");
|
|
85
|
+
if (!hidden && !visible) continue;
|
|
86
|
+
const match = hidden ? /^\.placement-remote-adoption-([a-f0-9]{64})-[a-f0-9]{64}\.stage$/.exec(entry.name)
|
|
87
|
+
: /^placement-remote-adoption-([a-f0-9]{64})\.json(?:\.[a-f0-9]{64}\.pending)?$/.exec(entry.name);
|
|
88
|
+
if (!match) { unknown = true; continue; }
|
|
89
|
+
hashes.add(match[1]!); if (hashes.size > 64) return { hashes: [], unknown: true };
|
|
90
|
+
try { if (hidden) ownedDir(join(home, entry.name)); else ownedFile(lstatSync(join(home, entry.name), { bigint: true })); }
|
|
91
|
+
catch { unknown = true; }
|
|
92
|
+
}
|
|
93
|
+
} finally { dir.closeSync(); }
|
|
94
|
+
} catch { return { hashes: [], unknown: true }; }
|
|
95
|
+
return { hashes: [...hashes].sort(), unknown };
|
|
96
|
+
}
|
|
97
|
+
/** A companion never claims another stage directory even when it names the same project. */
|
|
98
|
+
export function assertRemoteAdoptionStageOwnership(homeInput: string, value: RemoteAdoption): void {
|
|
99
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value), prefix = `.placement-remote-adoption-${plan.keyHash}-`, expected = staging(home, plan);
|
|
100
|
+
const dir = opendirSync(home); let count = 0;
|
|
101
|
+
try {
|
|
102
|
+
for (;;) {
|
|
103
|
+
const entry = dir.readSync(); if (!entry) break; if (++count > 20_000) fail();
|
|
104
|
+
if (entry.name.startsWith(prefix) && join(home, entry.name) !== expected) fail();
|
|
105
|
+
}
|
|
106
|
+
} finally { dir.closeSync(); }
|
|
107
|
+
}
|
|
108
|
+
/** A torn companion is private staging, not authority to materialize a realm. */
|
|
109
|
+
export function inspectRemoteAdoptionPending(homeInput: string, keyHash: string): RemoteAdoptionPending[] {
|
|
110
|
+
const home = realPath(homeInput); if (!/^[a-f0-9]{64}$/.test(keyHash)) return fail();
|
|
111
|
+
const prefix = `placement-remote-adoption-${keyHash}.json.`, result: RemoteAdoptionPending[] = []; let count = 0;
|
|
112
|
+
const dir = opendirSync(home);
|
|
113
|
+
try {
|
|
114
|
+
for (;;) {
|
|
115
|
+
const entry = dir.readSync(); if (!entry) break; if (++count > 20_000) return fail();
|
|
116
|
+
if (!entry.name.startsWith(prefix)) continue;
|
|
117
|
+
if (!/^[a-f0-9]{64}\.pending$/.test(entry.name.slice(prefix.length)) || result.length >= 64) return fail();
|
|
118
|
+
const file = join(home, entry.name), before = lstatSync(file, { bigint: true }), bytes = readOwned(file, REMOTE_ADOPTION_MAX_BYTES);
|
|
119
|
+
if (!bytes || stamp(lstatSync(file, { bigint: true })) !== stamp(before)) return fail();
|
|
120
|
+
result.push({ name: entry.name, size: bytes.length, hash: sha(bytes), stamp: stamp(before) });
|
|
121
|
+
}
|
|
122
|
+
} finally { dir.closeSync(); }
|
|
123
|
+
return result.sort((a, b) => a.name.localeCompare(b.name));
|
|
124
|
+
}
|
|
125
|
+
/** Explicitly confirmed cleanup rechecks the exact staged files and never removes an installed companion. */
|
|
126
|
+
export function clearRemoteAdoptionPending(homeInput: string, keyHash: string, expected: readonly RemoteAdoptionPending[], proof: RealmIntentProof): void {
|
|
127
|
+
const home = realPath(homeInput); check(home, proof);
|
|
128
|
+
if (canonicalJSON(inspectRemoteAdoptionPending(home, keyHash)) !== canonicalJSON(expected)) fail();
|
|
129
|
+
for (const stage of expected) {
|
|
130
|
+
const file = join(home, stage.name); check(home, proof);
|
|
131
|
+
const stat = lstatSync(file, { bigint: true }), bytes = readOwned(file, REMOTE_ADOPTION_MAX_BYTES);
|
|
132
|
+
if (!bytes || stamp(stat) !== stage.stamp || bytes.length !== stage.size || sha(bytes) !== stage.hash) fail();
|
|
133
|
+
unlinkSync(file); fsyncDirectory(home);
|
|
134
|
+
}
|
|
135
|
+
check(home, proof); if (inspectRemoteAdoptionPending(home, keyHash).length) fail();
|
|
136
|
+
}
|
|
137
|
+
export function readRemoteAdoption(homeInput: string, keyHash: string): RemoteAdoption | undefined {
|
|
138
|
+
const home = realPath(homeInput);
|
|
139
|
+
if (!/^[a-f0-9]{64}$/.test(keyHash)) return fail();
|
|
140
|
+
const bytes = readOwned(join(home, `placement-remote-adoption-${keyHash}.json`), REMOTE_ADOPTION_MAX_BYTES); if (!bytes) return undefined;
|
|
141
|
+
try {
|
|
142
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes), plan = validateRemoteAdoption(JSON.parse(text));
|
|
143
|
+
if (plan.keyHash !== keyHash || canonicalJSON(plan) + "\n" !== text) return fail(); return plan;
|
|
144
|
+
} catch { return fail(); }
|
|
145
|
+
}
|
|
146
|
+
/** Record reviewed metadata before creating any staged content. */
|
|
147
|
+
export function writeRemoteAdoption(homeInput: string, value: RemoteAdoption, proof: RealmIntentProof): void {
|
|
148
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value), expected = Buffer.from(canonicalJSON(plan) + "\n"), file = companion(home, plan);
|
|
149
|
+
check(home, proof); ownedDir(home);
|
|
150
|
+
const prior = readRemoteAdoption(home, plan.keyHash), pending = `${file}.${plan.seal}.pending`, stages = inspectRemoteAdoptionPending(home, plan.keyHash);
|
|
151
|
+
if (prior) { if (stages.length || canonicalJSON(prior) !== canonicalJSON(plan)) fail(); return; }
|
|
152
|
+
if (stages.some(item => join(home, item.name) !== pending)) fail();
|
|
153
|
+
complete(home, pending, expected, proof); check(home, proof);
|
|
154
|
+
if (lstatSync(file, { throwIfNoEntry: false })) fail();
|
|
155
|
+
renameSync(pending, file); fsyncDirectory(home); check(home, proof);
|
|
156
|
+
if (canonicalJSON(readRemoteAdoption(home, plan.keyHash)) !== canonicalJSON(plan)) fail();
|
|
157
|
+
}
|
|
158
|
+
/** Stage exact content-addressed packs in a private directory no old realm reader opens. */
|
|
159
|
+
export function stageRemoteAdoption(homeInput: string, value: RemoteAdoption, packs: readonly RemoteStagedPack[], proof: RealmIntentProof): void {
|
|
160
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value); check(home, proof);
|
|
161
|
+
if (canonicalJSON(readRemoteAdoption(home, plan.keyHash)) !== canonicalJSON(plan) || packs.length !== plan.packs.length) fail();
|
|
162
|
+
for (let i = 0; i < packs.length; i++) {
|
|
163
|
+
const expected = plan.packs[i], pack = packs[i];
|
|
164
|
+
if (!expected || !pack || expected.name !== pack.name || expected.bytes !== pack.bytes.length || `sync-${sha(pack.bytes)}.jsonl` !== pack.name) fail();
|
|
165
|
+
}
|
|
166
|
+
const root = staging(home, plan), log = join(root, "log"); ensureDir(home, root, home, proof); ensureDir(home, log, root, proof);
|
|
167
|
+
const names = new Set(plan.packs.map(pack => pack.name));
|
|
168
|
+
if (readdirSync(root).some(name => name !== "log") || readdirSync(log).some(name => !names.has(name))) fail();
|
|
169
|
+
for (const pack of packs) complete(home, join(log, pack.name), pack.bytes, proof);
|
|
170
|
+
fsyncDirectory(log); fsyncDirectory(root); check(home, proof);
|
|
171
|
+
}
|
|
172
|
+
/** A missing staged log with an existing destination is a possible completed rename, subject to full verification. */
|
|
173
|
+
export function remoteAdoptionLogMayBeInstalled(homeInput: string, project: string, value: RemoteAdoption): boolean {
|
|
174
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value);
|
|
175
|
+
if (!project || sha(project) !== plan.keyHash) return fail();
|
|
176
|
+
const root = staging(home, plan), stage = lstatSync(root, { throwIfNoEntry: false });
|
|
177
|
+
return !!stage && !lstatSync(join(root, "log"), { throwIfNoEntry: false }) && !!lstatSync(join(shardDir(home, project), "log"), { throwIfNoEntry: false });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Confirmed pre-admission abandon removes only exact unpublished packs this companion installed. */
|
|
181
|
+
export function discardRemoteInstalledLog(homeInput: string, project: string, value: RemoteAdoption, proof: RealmIntentProof): void {
|
|
182
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value);
|
|
183
|
+
if (!project || sha(project) !== plan.keyHash) fail(); check(home, proof);
|
|
184
|
+
if (canonicalJSON(readRemoteAdoption(home, plan.keyHash)) !== canonicalJSON(plan)) fail();
|
|
185
|
+
const root = staging(home, plan), staged = join(root, "log"), projectDir = shardDir(home, project), log = join(projectDir, "log");
|
|
186
|
+
ownedDir(root); if (lstatSync(staged, { throwIfNoEntry: false })) return;
|
|
187
|
+
if (readdirSync(root).length) fail();
|
|
188
|
+
const before = ownedDir(log);
|
|
189
|
+
const names = new Set(readdirSync(log)), allowed = new Set(plan.packs.map(pack => pack.name));
|
|
190
|
+
if ([...names].some(name => !allowed.has(name))) fail();
|
|
191
|
+
// A prior confirmed cleanup may have removed a strict subset before a crash.
|
|
192
|
+
const files = plan.packs.filter(pack => names.has(pack.name)).map(pack => {
|
|
193
|
+
const file = join(log, pack.name), bytes = readOwned(file, pack.bytes), stat = lstatSync(file, { bigint: true });
|
|
194
|
+
if (!bytes || bytes.length !== pack.bytes || `sync-${sha(bytes)}.jsonl` !== pack.name) return fail();
|
|
195
|
+
return { file, bytes, stamp: stamp(stat) };
|
|
196
|
+
});
|
|
197
|
+
for (const item of files) {
|
|
198
|
+
check(home, proof); if (stamp(lstatSync(item.file, { bigint: true })) !== item.stamp || !readOwned(item.file, item.bytes.length)?.equals(item.bytes)) fail();
|
|
199
|
+
unlinkSync(item.file);
|
|
200
|
+
}
|
|
201
|
+
fsyncDirectory(log); check(home, proof);
|
|
202
|
+
const current = ownedDir(log); if (current.dev !== before.dev || current.ino !== before.ino || readdirSync(log).length) fail();
|
|
203
|
+
// A completed #448 scaffold still needs its log directory, even if its extension was abandoned.
|
|
204
|
+
if (!lstatSync(join(projectDir, "generation.json"), { throwIfNoEntry: false })) rmdirSync(log);
|
|
205
|
+
fsyncDirectory(projectDir); check(home, proof);
|
|
206
|
+
}
|
|
207
|
+
/** Install the complete staged log as one same-filesystem rename; work and machine data stay in place. */
|
|
208
|
+
export function installRemoteAdoptionLog(homeInput: string, project: string, value: RemoteAdoption, proof: RealmIntentProof, allowRetainedMetadata = false): void {
|
|
209
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value);
|
|
210
|
+
if (!project || sha(project) !== plan.keyHash) fail();
|
|
211
|
+
check(home, proof); if (canonicalJSON(readRemoteAdoption(home, plan.keyHash)) !== canonicalJSON(plan)) fail();
|
|
212
|
+
const root = staging(home, plan), log = join(root, "log"), projectDir = shardDir(home, project), destination = join(projectDir, "log");
|
|
213
|
+
ownedDir(root);
|
|
214
|
+
const verifyLog = (dir: string) => {
|
|
215
|
+
ownedDir(dir); if (canonicalJSON(readdirSync(dir).sort()) !== canonicalJSON(plan.packs.map(pack => pack.name))) fail();
|
|
216
|
+
for (const pack of plan.packs) { const data = readOwned(join(dir, pack.name), pack.bytes); if (!data || data.length !== pack.bytes || `sync-${sha(data)}.jsonl` !== pack.name) fail(); }
|
|
217
|
+
};
|
|
218
|
+
// A crash after rename leaves the exact destination and an empty owned stage; never rename or truncate it again.
|
|
219
|
+
if (!lstatSync(log, { throwIfNoEntry: false })) {
|
|
220
|
+
if (readdirSync(root).length || !allowRetainedMetadata && lstatSync(join(projectDir, "generation.json"), { throwIfNoEntry: false })) fail();
|
|
221
|
+
assertCuratedShardLayout(home, { project }); ownedDir(projectDir); verifyLog(destination); check(home, proof); return;
|
|
222
|
+
}
|
|
223
|
+
const source = ownedDir(log);
|
|
224
|
+
if (readdirSync(root).some(name => name !== "log")) fail();
|
|
225
|
+
verifyLog(log);
|
|
226
|
+
check(home, proof);
|
|
227
|
+
const projects = join(home, "projects"); ownedDir(projects);
|
|
228
|
+
if (lstatSync(projectDir, { throwIfNoEntry: false })) {
|
|
229
|
+
ownedDir(projectDir); rejectExistingCurated(projectDir);
|
|
230
|
+
}
|
|
231
|
+
assertCuratedShardLayout(home, { project });
|
|
232
|
+
if (!lstatSync(projectDir, { throwIfNoEntry: false })) { mkdirSync(projectDir, { mode: 0o700 }); fsyncDirectory(projects); }
|
|
233
|
+
const target = ownedDir(projectDir);
|
|
234
|
+
rejectExistingCurated(projectDir);
|
|
235
|
+
ensureShardDescriptor(home, project); check(home, proof);
|
|
236
|
+
if (source.dev !== target.dev || lstatSync(destination, { throwIfNoEntry: false })) fail();
|
|
237
|
+
const currentTarget = ownedDir(projectDir);
|
|
238
|
+
if (stamp(ownedDir(log)) !== stamp(source) || currentTarget.dev !== target.dev || currentTarget.ino !== target.ino) fail();
|
|
239
|
+
renameSync(log, destination); fsyncDirectory(root); fsyncDirectory(projectDir); check(home, proof);
|
|
240
|
+
const installed = ownedDir(destination); if (installed.dev !== source.dev || installed.ino !== source.ino) fail();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Remove only this plan's attributable private staging; an installed curated log is never removed. */
|
|
244
|
+
export function clearRemoteAdoption(homeInput: string, value: RemoteAdoption, packs: readonly RemoteStagedPack[], proof: RealmIntentProof): void {
|
|
245
|
+
const home = realPath(homeInput), plan = validateRemoteAdoption(value); check(home, proof);
|
|
246
|
+
assertRemoteAdoptionStageOwnership(home, plan);
|
|
247
|
+
if (canonicalJSON(readRemoteAdoption(home, plan.keyHash)) !== canonicalJSON(plan) || packs.length !== plan.packs.length) fail();
|
|
248
|
+
for (let i = 0; i < packs.length; i++) {
|
|
249
|
+
const expected = plan.packs[i], pack = packs[i];
|
|
250
|
+
if (!expected || !pack || expected.name !== pack.name || expected.bytes !== pack.bytes.length || `sync-${sha(pack.bytes)}.jsonl` !== pack.name) fail();
|
|
251
|
+
}
|
|
252
|
+
const root = staging(home, plan), log = join(root, "log"), stage = lstatSync(root, { bigint: true, throwIfNoEntry: false });
|
|
253
|
+
if (stage) {
|
|
254
|
+
ownedDir(root); if (readdirSync(root).some(name => name !== "log")) fail();
|
|
255
|
+
if (lstatSync(log, { throwIfNoEntry: false })) {
|
|
256
|
+
ownedDir(log); const byName = new Map(packs.map(pack => [pack.name, pack.bytes]));
|
|
257
|
+
const names = readdirSync(log); if (names.some(name => !byName.has(name))) fail();
|
|
258
|
+
// First prove every remaining prefix, then recheck each one immediately before removal.
|
|
259
|
+
const saved = names.map(name => {
|
|
260
|
+
const file = join(log, name), expected = byName.get(name)!, bytes = readOwned(file, expected.length), stat = lstatSync(file, { bigint: true });
|
|
261
|
+
if (!bytes || !bytes.equals(expected.subarray(0, bytes.length))) return fail();
|
|
262
|
+
return { file, expected, bytes, stamp: stamp(stat) };
|
|
263
|
+
});
|
|
264
|
+
for (const item of saved) {
|
|
265
|
+
check(home, proof); if (stamp(lstatSync(item.file, { bigint: true })) !== item.stamp || !readOwned(item.file, item.expected.length)?.equals(item.bytes)) fail();
|
|
266
|
+
unlinkSync(item.file);
|
|
267
|
+
}
|
|
268
|
+
fsyncDirectory(log); check(home, proof); rmdirSync(log); fsyncDirectory(root);
|
|
269
|
+
}
|
|
270
|
+
check(home, proof); if (readdirSync(root).length) fail(); rmdirSync(root); fsyncDirectory(home);
|
|
271
|
+
}
|
|
272
|
+
const file = companion(home, plan), expected = Buffer.from(canonicalJSON(plan) + "\n"), before = lstatSync(file, { bigint: true });
|
|
273
|
+
check(home, proof); if (!readOwned(file, REMOTE_ADOPTION_MAX_BYTES)?.equals(expected) || stamp(lstatSync(file, { bigint: true })) !== stamp(before)) fail();
|
|
274
|
+
unlinkSync(file); fsyncDirectory(home); check(home, proof);
|
|
275
|
+
}
|