@staix/agent-mem 0.23.1 → 0.25.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 +28 -12
- package/package.json +1 -1
- package/src/brief-cache.ts +60 -0
- package/src/brief-capture.ts +82 -0
- package/src/brief-curated-native.ts +52 -0
- package/src/brief-curated.ts +118 -0
- package/src/cli.ts +8 -2
- package/src/curated-brief-cache.ts +29 -0
- package/src/hook-contract.ts +22 -2
- package/src/hook-jcode-journal.ts +46 -4
- package/src/hook-record.ts +40 -2
- package/src/hook-spool-replay.ts +8 -1
- package/src/hook-spool.ts +25 -14
- package/src/hook-transcript.ts +10 -2
- package/src/hooks.ts +68 -17
- package/src/index.ts +6 -1
- package/src/ingest-records.ts +113 -0
- package/src/ingest.ts +106 -9
- package/src/mcp-memory.ts +2 -2
- package/src/mcp.ts +1 -1
- package/src/memory/candidate-pending.ts +6 -2
- package/src/memory/candidates.ts +60 -7
- package/src/memory/capture-input.ts +7 -0
- package/src/memory/capture-plan.ts +10 -0
- package/src/memory/capture.ts +98 -8
- package/src/memory/consolidate.ts +35 -1
- package/src/memory/curated-cache-state.ts +36 -0
- package/src/memory/decay-review.ts +3 -1
- package/src/memory/formats.ts +20 -0
- package/src/memory/generation-index.ts +2 -0
- package/src/memory/generation-sync.ts +3 -0
- package/src/memory/index-file.ts +1 -1
- package/src/memory/intent-brief.ts +42 -4
- package/src/memory/metrics.ts +10 -4
- package/src/memory/migrate.ts +7 -1
- package/src/memory/note-add.ts +2 -0
- package/src/memory/ops-contract.ts +1 -1
- package/src/memory/preference-brief.ts +22 -0
- package/src/memory/public-prune-activation.ts +4 -1
- package/src/memory/public-prune-advance.ts +2 -0
- package/src/memory/reader-lease.ts +5 -3
- package/src/memory/recall.ts +4 -1
- package/src/memory/record-candidates.ts +179 -0
- package/src/memory/record-instruction.ts +26 -0
- package/src/memory/record-variant-log.ts +90 -0
- package/src/memory/search-fts.ts +3 -0
- package/src/memory/shard-migration.ts +4 -1
- package/src/memory/shard-sync.ts +3 -0
- package/src/memory/store.ts +184 -4
- package/src/memory/sync-placement-activation.ts +11 -3
- package/src/memory/sync-placement-transport.ts +3 -0
- package/src/memory/sync.ts +3 -2
- package/src/session-brief-worker.ts +13 -2
- package/src/session-brief.ts +150 -70
- package/src/ui/plugin.ts +104 -30
- package/src/ui/snapshot-cache.ts +141 -0
- package/src/ui/snapshot-revision.ts +87 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
type Data = Record<string, unknown>;
|
|
4
|
+
interface Entry { sampledAt: number; generation: number; epoch: number; data: Data; revision: string; cursor: number; until: number; bytes: number }
|
|
5
|
+
export interface SnapshotValue { data: Data; until: number }
|
|
6
|
+
interface Reading extends SnapshotValue { sampledAt: number; generation: number; entry?: Entry }
|
|
7
|
+
|
|
8
|
+
/** Provider-local screened snapshots. No disk cache, timer, watcher or retained store. */
|
|
9
|
+
export class SnapshotCache {
|
|
10
|
+
private entries = new Map<string, Entry>();
|
|
11
|
+
private flights = new Map<string, Promise<Reading>>();
|
|
12
|
+
private active = new Set<Promise<unknown>>();
|
|
13
|
+
private generation = 0;
|
|
14
|
+
private epoch = 0;
|
|
15
|
+
private reuseUnavailable = false;
|
|
16
|
+
// Cached digests occupy the lower 52 bits; fresh replies have distinct safe-integer cursors above them.
|
|
17
|
+
private freshCursor = 2 ** 52;
|
|
18
|
+
private bytes = 0;
|
|
19
|
+
private closed = false;
|
|
20
|
+
constructor(private readonly revision: () => string, private readonly now: () => number = Date.now) {}
|
|
21
|
+
private clearReusable(): void { this.epoch++; this.entries.clear(); this.bytes = 0; this.flights.clear(); }
|
|
22
|
+
invalidate(): void { this.generation++; this.clearReusable(); }
|
|
23
|
+
async close(): Promise<void> { this.closed = true; this.invalidate(); await Promise.allSettled([...this.active]); }
|
|
24
|
+
private disableReuse(): void {
|
|
25
|
+
if (!this.reuseUnavailable) this.clearReusable();
|
|
26
|
+
this.reuseUnavailable = true;
|
|
27
|
+
}
|
|
28
|
+
private sourceRevision(): string | undefined {
|
|
29
|
+
let revision: string;
|
|
30
|
+
try { revision = this.revision(); }
|
|
31
|
+
catch { this.disableReuse(); return undefined; }
|
|
32
|
+
this.reuseUnavailable = false;
|
|
33
|
+
return revision;
|
|
34
|
+
}
|
|
35
|
+
private timely(sampledAt: number, until: number): boolean {
|
|
36
|
+
const now = this.now();
|
|
37
|
+
return now >= sampledAt && now < until;
|
|
38
|
+
}
|
|
39
|
+
private readable(value?: SnapshotValue): void {
|
|
40
|
+
if (this.closed) throw Error("console snapshots are closed");
|
|
41
|
+
if (value?.data.error !== undefined) throw Error("console snapshot unavailable");
|
|
42
|
+
}
|
|
43
|
+
private track<T>(work: Promise<T>): Promise<T> {
|
|
44
|
+
this.active.add(work);
|
|
45
|
+
void work.then(() => this.active.delete(work), () => this.active.delete(work));
|
|
46
|
+
return work;
|
|
47
|
+
}
|
|
48
|
+
private freshReply(data: Data): Data {
|
|
49
|
+
this.readable();
|
|
50
|
+
if (this.freshCursor >= Number.MAX_SAFE_INTEGER) throw Error("console fresh cursor limit");
|
|
51
|
+
const copy = structuredClone(data);
|
|
52
|
+
delete copy.changed;
|
|
53
|
+
return { ...copy, cursor: ++this.freshCursor };
|
|
54
|
+
}
|
|
55
|
+
private readFresh(read: () => Promise<SnapshotValue>, retryAction: boolean): Promise<Data> {
|
|
56
|
+
this.readable();
|
|
57
|
+
if (this.active.size >= 16) throw Error("console snapshot concurrency limit");
|
|
58
|
+
const generation = this.generation;
|
|
59
|
+
return this.track((async () => {
|
|
60
|
+
let value = await read();
|
|
61
|
+
this.readable(value);
|
|
62
|
+
// One post-action reread only. Further changes still yield full uncached data, never a retry loop.
|
|
63
|
+
if (retryAction && generation !== this.generation) { value = await read(); this.readable(value); }
|
|
64
|
+
return this.freshReply(value.data);
|
|
65
|
+
})());
|
|
66
|
+
}
|
|
67
|
+
/** A request with no complete revision computes independently without consulting another revision or retaining data. */
|
|
68
|
+
async fresh(read: () => Promise<SnapshotValue>): Promise<Data> {
|
|
69
|
+
this.readable(); this.disableReuse();
|
|
70
|
+
return this.readFresh(read, true);
|
|
71
|
+
}
|
|
72
|
+
private dropEntry(key: string, entry: Entry | undefined): void {
|
|
73
|
+
if (entry && this.entries.get(key) === entry) { this.entries.delete(key); this.bytes -= entry.bytes; }
|
|
74
|
+
}
|
|
75
|
+
async snapshot(key: string, after: number, read: () => Promise<SnapshotValue>): Promise<Data> {
|
|
76
|
+
this.readable();
|
|
77
|
+
const revision = this.sourceRevision();
|
|
78
|
+
this.readable();
|
|
79
|
+
if (revision === undefined) return this.fresh(read);
|
|
80
|
+
const cached = this.entries.get(key);
|
|
81
|
+
if (cached && cached.revision === revision && this.timely(cached.sampledAt, cached.until)) {
|
|
82
|
+
this.entries.delete(key); this.entries.set(key, cached);
|
|
83
|
+
return this.reply(cached, after);
|
|
84
|
+
}
|
|
85
|
+
// Revision is part of the flight key: a new write never joins an older read.
|
|
86
|
+
const flightKey = JSON.stringify([this.generation, this.epoch, revision, key]);
|
|
87
|
+
let flight = this.flights.get(flightKey);
|
|
88
|
+
if (!flight) {
|
|
89
|
+
if (this.active.size >= 16) throw Error("console snapshot concurrency limit");
|
|
90
|
+
const generation = this.generation, epoch = this.epoch, sampledAt = this.now();
|
|
91
|
+
flight = (async () => {
|
|
92
|
+
const value = await read();
|
|
93
|
+
this.readable(value);
|
|
94
|
+
const observed = this.sourceRevision();
|
|
95
|
+
this.readable(value);
|
|
96
|
+
const data = structuredClone(value.data); delete data.changed;
|
|
97
|
+
const reading: Reading = { data, until: value.until, sampledAt, generation };
|
|
98
|
+
if (observed !== revision || epoch !== this.epoch || generation !== this.generation || !this.timely(sampledAt, value.until)) {
|
|
99
|
+
this.dropEntry(key, cached);
|
|
100
|
+
if (this.flights.get(flightKey) === flight) this.flights.delete(flightKey);
|
|
101
|
+
return reading;
|
|
102
|
+
}
|
|
103
|
+
const serialized = JSON.stringify(data), bytes = Buffer.byteLength(serialized);
|
|
104
|
+
const cursor = Number.parseInt(createHash("sha256").update(JSON.stringify([key, revision, value.until])).update(serialized).digest("hex").slice(0, 13), 16) + 1;
|
|
105
|
+
const entry = { sampledAt, generation, epoch, data, revision, cursor, until: value.until, bytes };
|
|
106
|
+
// ponytail: 16 selections and 4 MiB per provider. Larger views are served once;
|
|
107
|
+
// upgrade with paged panel results rather than retaining an unbounded corpus.
|
|
108
|
+
if (bytes <= 4 * 1024 * 1024) {
|
|
109
|
+
const old = this.entries.get(key); if (old) { this.bytes -= old.bytes; this.entries.delete(key); }
|
|
110
|
+
while (this.entries.size >= 16 || this.bytes + bytes > 4 * 1024 * 1024) {
|
|
111
|
+
const first = this.entries.keys().next().value!; this.bytes -= this.entries.get(first)!.bytes; this.entries.delete(first);
|
|
112
|
+
}
|
|
113
|
+
this.entries.set(key, entry); this.bytes += bytes;
|
|
114
|
+
}
|
|
115
|
+
reading.entry = entry;
|
|
116
|
+
return reading;
|
|
117
|
+
})();
|
|
118
|
+
this.flights.set(flightKey, flight);
|
|
119
|
+
this.track(flight);
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const reading = await flight;
|
|
123
|
+
this.readable(reading);
|
|
124
|
+
if (reading.generation !== this.generation) { this.dropEntry(key, reading.entry); return this.readFresh(read, false); }
|
|
125
|
+
const entry = reading.entry;
|
|
126
|
+
if (!entry) return this.freshReply(reading.data);
|
|
127
|
+
const observed = this.sourceRevision();
|
|
128
|
+
this.readable(reading);
|
|
129
|
+
if (reading.generation !== this.generation) { this.dropEntry(key, entry); return this.readFresh(read, false); }
|
|
130
|
+
if (observed !== entry.revision || entry.epoch !== this.epoch || !this.timely(entry.sampledAt, entry.until)) {
|
|
131
|
+
this.dropEntry(key, entry); return this.freshReply(reading.data);
|
|
132
|
+
}
|
|
133
|
+
return this.reply(entry, after);
|
|
134
|
+
}
|
|
135
|
+
finally { if (this.flights.get(flightKey) === flight) this.flights.delete(flightKey); }
|
|
136
|
+
}
|
|
137
|
+
private reply(entry: Entry, after: number): Data {
|
|
138
|
+
// acon's no-op contract requires exactly these two keys.
|
|
139
|
+
return after === entry.cursor ? { changed: false, cursor: entry.cursor } : { ...structuredClone(entry.data), cursor: entry.cursor };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, opendirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { pendingSummary } from "../memory/candidate-pending.ts";
|
|
5
|
+
import { realPath } from "../memory/platform.ts";
|
|
6
|
+
import { inboxPolicyVersion } from "./inbox.ts";
|
|
7
|
+
|
|
8
|
+
type Scope = "home" | "realm" | "projects" | "generations" | "machines" | "machine" | "work" | "logs" | "evidence";
|
|
9
|
+
type Source = Scope | "file";
|
|
10
|
+
// ponytail: at most 16,384 directory entries and 24 levels per probe, including ignored entries.
|
|
11
|
+
// Refuse larger inventories; upgrade to an authoritative append-revision manifest before increasing these bounds.
|
|
12
|
+
const MAX_ENTRIES = 16_384, MAX_DEPTH = 24;
|
|
13
|
+
const realms = new Set(["user", "legacy", "legacy-main"]);
|
|
14
|
+
const evidence = new Set(["bridge-snapshots", "public-prune-objects", "public-prune-inputs", "public-prune-input-sets", "sync-placement"]);
|
|
15
|
+
const databases = /^(?:capture|candidates)\.db(?:-wal|-journal)?$/;
|
|
16
|
+
const derived = /^(?:index(?:-[a-z0-9]+)*|search(?:-[a-f0-9]+)?|catalog|mutation-lock|reader-admission)\.db(?:-(?:wal|shm|journal))?$/;
|
|
17
|
+
|
|
18
|
+
function source(scope: Scope, name: string): Source | undefined {
|
|
19
|
+
if (name === ".git" || name === "node_modules" || name === "reader-leases" || name === "leases" || name === "readers" || name === "shard-readers"
|
|
20
|
+
|| name.endsWith("-shm") || derived.test(name) || /^brief-(?:capture|curated)\.json$/.test(name)) return;
|
|
21
|
+
if (scope === "logs") return name.endsWith(".jsonl") ? "file" : undefined;
|
|
22
|
+
if (scope === "projects") return /^[a-f0-9]{16}$/.test(name) ? "realm" : undefined;
|
|
23
|
+
if (scope === "generations") return /^[a-f0-9]{32}$/.test(name) ? "realm" : undefined;
|
|
24
|
+
if (scope === "machines") return "machine";
|
|
25
|
+
if (scope === "machine") return databases.test(name) || name === "config.json" ? "file" : undefined;
|
|
26
|
+
if (scope === "work") return name === "log" ? "logs" : undefined;
|
|
27
|
+
if (scope === "evidence") return "evidence";
|
|
28
|
+
if (name === "log" || name === "local") return "logs";
|
|
29
|
+
if (name === "projects") return "projects";
|
|
30
|
+
if (name === "generations") return "generations";
|
|
31
|
+
if (name === "machine") return "machines";
|
|
32
|
+
if (name === "work") return "work";
|
|
33
|
+
if (realms.has(name)) return "realm";
|
|
34
|
+
if (evidence.has(name)) return "evidence";
|
|
35
|
+
if (name === "profile.lock" || databases.test(name) || /\.(?:json|jsonl)$/.test(name)) return "file";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Metadata of authoritative console inputs only. No payloads, read-write stores, index refresh or reader lease. */
|
|
39
|
+
export function snapshotRevision(base: string, now = Date.now()): string {
|
|
40
|
+
if (!Number.isFinite(now)) throw Error("snapshot revision clock is unavailable");
|
|
41
|
+
try { base = realPath(base); }
|
|
42
|
+
catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
|
|
43
|
+
const frames: unknown[] = [];
|
|
44
|
+
let entries = 0;
|
|
45
|
+
const walk = (relative: string, expected: Source, depth: number) => {
|
|
46
|
+
if (depth > MAX_DEPTH) throw Error("snapshot source inventory exceeds its depth bound");
|
|
47
|
+
const file = join(base, relative), stat = lstatSync(file, { bigint: true, throwIfNoEntry: false });
|
|
48
|
+
if (!stat) {
|
|
49
|
+
if (relative) throw Error("snapshot source changed during inventory");
|
|
50
|
+
frames.push(["", "missing"]); return;
|
|
51
|
+
}
|
|
52
|
+
if (stat.isSymbolicLink() || !stat.isDirectory() && !stat.isFile()) throw Error("snapshot source is not a plain file or directory");
|
|
53
|
+
if (stat.isFile()) {
|
|
54
|
+
if (expected !== "file" && expected !== "evidence") throw Error("snapshot source directory is unavailable");
|
|
55
|
+
if (file.endsWith("-wal") && stat.size === 0n) return; // A read-only SQLite open may recreate an empty WAL.
|
|
56
|
+
frames.push([relative, "file", String(stat.dev), String(stat.ino), String(stat.mode), String(stat.size), String(stat.mtimeNs), String(stat.ctimeNs)]);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (expected === "file") throw Error("snapshot source file is unavailable");
|
|
60
|
+
const children: { name: string; kind: Source }[] = [], iterator = opendirSync(file);
|
|
61
|
+
try {
|
|
62
|
+
for (;;) {
|
|
63
|
+
const child = iterator.readSync(); if (!child) break;
|
|
64
|
+
if (++entries > MAX_ENTRIES) throw Error("snapshot source inventory exceeds its entry bound");
|
|
65
|
+
const kind = expected === "machines" && !child.isDirectory() && !child.isSymbolicLink() ? undefined : source(expected, child.name);
|
|
66
|
+
if (kind) children.push({ name: child.name, kind });
|
|
67
|
+
}
|
|
68
|
+
} finally { iterator.closeSync(); }
|
|
69
|
+
children.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
70
|
+
const start = frames.length;
|
|
71
|
+
frames.push(undefined);
|
|
72
|
+
const names: string[] = [];
|
|
73
|
+
for (const child of children) {
|
|
74
|
+
const before = frames.length;
|
|
75
|
+
walk(relative ? `${relative}/${child.name}` : child.name, child.kind, depth + 1);
|
|
76
|
+
if (frames.length > before) names.push(child.name);
|
|
77
|
+
}
|
|
78
|
+
// Readers may create an otherwise empty work/log tree alongside its disposable index.
|
|
79
|
+
if (relative && !names.length) { frames.splice(start, 1); return; }
|
|
80
|
+
// Directory times/size also change for excluded indexes and leases. Namespace plus identity tracks sources without that churn.
|
|
81
|
+
frames[start] = [relative, "directory", String(stat.dev), String(stat.ino), String(stat.mode), names];
|
|
82
|
+
};
|
|
83
|
+
walk("", "home", 0);
|
|
84
|
+
const policy = inboxPolicyVersion(base), pending = pendingSummary(base, undefined, now, true);
|
|
85
|
+
if (pending === null) throw Error("snapshot pending candidates are unavailable");
|
|
86
|
+
return createHash("sha256").update(JSON.stringify({ frames, policy, pending })).digest("hex");
|
|
87
|
+
}
|