opencode-self-improvement 0.15.18 → 0.15.19
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 +4 -0
- package/package.json +4 -2
- package/src/capture/candidates.ts +17 -6
- package/src/capture/lag.ts +6 -2
- package/src/capture/prompt.ts +1 -1
- package/src/capture/runtime.ts +85 -21
- package/src/capture/session-model.ts +2 -1
- package/src/capture/transcript.ts +10 -4
- package/src/capture/triggers.ts +48 -14
- package/src/command/ensure.ts +6 -1
- package/src/consolidate/propose.ts +47 -17
- package/src/consolidate/render.ts +3 -1
- package/src/core/cmp-ts.ts +2 -2
- package/src/core/config/budgets.ts +0 -1
- package/src/core/config/paths.ts +2 -3
- package/src/core/hooks.ts +8 -3
- package/src/core/host.ts +8 -3
- package/src/core/logfmt/format.ts +20 -5
- package/src/core/logfmt/publish.ts +21 -4
- package/src/core/lru.ts +20 -20
- package/src/core/permissions.ts +21 -5
- package/src/core/runtime.ts +7 -3
- package/src/core/state.ts +12 -2
- package/src/core/time.ts +0 -1
- package/src/core/tokenize.ts +16 -5
- package/src/core/types.ts +8 -1
- package/src/events.ts +74 -18
- package/src/evidence/record.ts +29 -9
- package/src/gate/gate.ts +208 -64
- package/src/gate/resolver.ts +105 -20
- package/src/hits/ledger.ts +35 -10
- package/src/identity/host-events.ts +35 -11
- package/src/identity/repokey.ts +27 -9
- package/src/identity/session-context.ts +39 -8
- package/src/index.ts +108 -42
- package/src/init/detect.ts +62 -12
- package/src/init/run.ts +50 -11
- package/src/inject/anchor.ts +11 -5
- package/src/inject/attribution.ts +29 -6
- package/src/inject/budget.ts +9 -2
- package/src/inject/digest.ts +16 -8
- package/src/inject/epochs.ts +43 -15
- package/src/inject/pull.ts +11 -5
- package/src/inject/version.ts +10 -5
- package/src/portable/decay.ts +20 -9
- package/src/portable/export.ts +30 -10
- package/src/portable/import.ts +43 -12
- package/src/recall/relevant.ts +11 -5
- package/src/recall/score.ts +5 -3
- package/src/status/format.ts +8 -2
- package/src/status/metrics.ts +22 -6
- package/src/status/payload.ts +7 -2
- package/src/status/report.ts +63 -18
- package/src/status/write.ts +21 -7
- package/src/store/blocklist.ts +16 -4
- package/src/store/containment.ts +27 -8
- package/src/store/files/append.ts +57 -37
- package/src/store/files/deletions.ts +5 -2
- package/src/store/files/load.ts +37 -11
- package/src/store/files/prune.ts +18 -5
- package/src/store/files/snapshots.ts +6 -2
- package/src/store/files/supersede.ts +32 -12
- package/src/store/files/target.ts +4 -1
- package/src/store/lock.ts +10 -2
- package/src/store/model/visibility.ts +26 -6
- package/src/store/prune.ts +1 -1
- package/src/store/quarantine/quarantine.ts +36 -9
- package/src/store/quarantine/release.ts +9 -3
- package/src/store/secrets.ts +4 -2
- package/src/store/tombstones/rejection.ts +21 -5
- package/src/store/tombstones/tombstone.ts +48 -11
- package/src/store/undo.ts +43 -18
- package/src/tools/consolidate.ts +18 -8
- package/src/tools/decay.ts +37 -9
- package/src/tools/deps.ts +10 -2
- package/src/tools/evidence.ts +7 -4
- package/src/tools/export.ts +10 -4
- package/src/tools/forget.ts +15 -5
- package/src/tools/import.ts +16 -5
- package/src/tools/init.ts +13 -4
- package/src/tools/list.ts +12 -6
- package/src/tools/merge-review.ts +52 -20
- package/src/tools/recall.ts +17 -9
- package/src/tools/registry.ts +4 -1
- package/src/tools/remember.ts +21 -7
- package/src/tools/schema.ts +55 -18
- package/src/tools/status-tool.ts +29 -11
- package/src/tools/undo.ts +23 -8
- package/src/tools/update.ts +116 -40
- package/src/tui/panel.tsx +119 -30
- package/src/viewer/mutate.ts +57 -20
- package/src/viewer/rejections.ts +1 -1
- package/src/viewer/server.ts +88 -28
package/src/status/report.ts
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { cacheRate, estTok, fmtBytes, fmtCount, fmtMs } from "./format.ts";
|
|
4
4
|
import { captureLag, ledgerIsYoung, rowsPerDay } from "./metrics.ts";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
type BlockedRow,
|
|
7
|
+
type StatusPayload,
|
|
8
|
+
type StoreCounts,
|
|
9
|
+
emptyStats,
|
|
10
|
+
} from "./payload.ts";
|
|
6
11
|
|
|
7
12
|
function ago(iso: string): string {
|
|
8
13
|
const ms = Date.now() - new Date(iso).getTime();
|
|
@@ -44,7 +49,9 @@ export function cacheLines(p: StatusPayload): string[] {
|
|
|
44
49
|
]
|
|
45
50
|
: [
|
|
46
51
|
` cache busts ${st.invalidations} this session · last ${
|
|
47
|
-
st.lastInvalidation
|
|
52
|
+
st.lastInvalidation
|
|
53
|
+
? `${st.lastInvalidation.at.slice(11, 19)} (${st.lastInvalidation.reason})`
|
|
54
|
+
: "-"
|
|
48
55
|
}`,
|
|
49
56
|
" a memory YOU saved in this session never costs one — you",
|
|
50
57
|
" already said it (#165). A correction, a removal, or a row",
|
|
@@ -88,14 +95,18 @@ export function windowLines(p: StatusPayload): string[] {
|
|
|
88
95
|
if (used > 0) {
|
|
89
96
|
const share = Math.round((memTok / used) * 1000) / 10;
|
|
90
97
|
const rate = cacheRate(m);
|
|
91
|
-
out.push(
|
|
98
|
+
out.push(
|
|
99
|
+
` of what you sent ≈ ${fmtCount(used)} prompt tok · memory is ${share}% of it`,
|
|
100
|
+
);
|
|
92
101
|
out.push(
|
|
93
102
|
rate
|
|
94
103
|
? ` cache served ${rate.pct}% of that prompt from cache → memory rides free after turn 1`
|
|
95
104
|
: " cache served provider has not reported a split yet",
|
|
96
105
|
);
|
|
97
106
|
} else {
|
|
98
|
-
out.push(
|
|
107
|
+
out.push(
|
|
108
|
+
" of what you sent not measured yet — receipts appear after the first assistant turn",
|
|
109
|
+
);
|
|
99
110
|
}
|
|
100
111
|
const busts = p.stats?.invalidations ?? 0;
|
|
101
112
|
out.push(
|
|
@@ -114,7 +125,9 @@ export function attributionLines(p: StatusPayload): string[] {
|
|
|
114
125
|
const a = p.attribution;
|
|
115
126
|
if (!a) return [" not computed — no memory was injected this session"];
|
|
116
127
|
if (a.pathsCited === 0 && a.commandsMatched === 0 && a.readsTotal === 0) {
|
|
117
|
-
return [
|
|
128
|
+
return [
|
|
129
|
+
" the injected memory cited no file paths or commands yet — nothing to attribute",
|
|
130
|
+
];
|
|
118
131
|
}
|
|
119
132
|
return [
|
|
120
133
|
` Tier A · commands ${a.commandSaves} lookup(s) avoided [measured] · ran a remembered command with no prior file lookup`,
|
|
@@ -136,9 +149,14 @@ export function renderMemoryReport(input: {
|
|
|
136
149
|
const { status, store, blocked } = input;
|
|
137
150
|
const out: string[] = ["MEMORY STATUS", ""];
|
|
138
151
|
if (input.quarantined?.length) {
|
|
139
|
-
out.push(
|
|
152
|
+
out.push(
|
|
153
|
+
"QUARANTINE",
|
|
154
|
+
` ${input.quarantined.length} rows withheld from recall and injection; review/release in the memory viewer.`,
|
|
155
|
+
);
|
|
140
156
|
for (const row of input.quarantined.slice(0, 20)) {
|
|
141
|
-
out.push(
|
|
157
|
+
out.push(
|
|
158
|
+
` ${row.ts} ${row.patterns.join(", ") || "marked for review"}`,
|
|
159
|
+
);
|
|
142
160
|
}
|
|
143
161
|
out.push("");
|
|
144
162
|
}
|
|
@@ -150,15 +168,28 @@ export function renderMemoryReport(input: {
|
|
|
150
168
|
);
|
|
151
169
|
} else {
|
|
152
170
|
out.push("CONTEXT", ...contextLines(status));
|
|
153
|
-
out.push(
|
|
154
|
-
|
|
171
|
+
out.push(
|
|
172
|
+
"",
|
|
173
|
+
"CACHE — the receipts on your prompt-cache cost",
|
|
174
|
+
...cacheLines(status),
|
|
175
|
+
);
|
|
176
|
+
out.push(
|
|
177
|
+
"",
|
|
178
|
+
"CONTEXT WINDOW — is memory bloating your context?",
|
|
179
|
+
...windowLines(status),
|
|
180
|
+
);
|
|
155
181
|
out.push("", "INJECTED BLOCK (verbatim)");
|
|
156
|
-
if (status.block)
|
|
182
|
+
if (status.block)
|
|
183
|
+
out.push(...status.block.split("\n").map((l) => ` ${l}`));
|
|
157
184
|
else
|
|
158
185
|
out.push(
|
|
159
186
|
" abstained — your prompt shares no content vocabulary with any memory, so nothing was injected (by design, no noise).",
|
|
160
187
|
);
|
|
161
|
-
out.push(
|
|
188
|
+
out.push(
|
|
189
|
+
"",
|
|
190
|
+
"MEMORY ATTRIBUTION — answered from memory vs re-reading files",
|
|
191
|
+
...attributionLines(status),
|
|
192
|
+
);
|
|
162
193
|
}
|
|
163
194
|
|
|
164
195
|
out.push("", "RECENT BLOCKED WRITES");
|
|
@@ -166,17 +197,23 @@ export function renderMemoryReport(input: {
|
|
|
166
197
|
for (const b of blocked) {
|
|
167
198
|
out.push(` ${b.ts} session ${b.sid} ${b.reason}`);
|
|
168
199
|
}
|
|
169
|
-
out.push(
|
|
200
|
+
out.push(
|
|
201
|
+
" (reasons are logged via memory_evidence; the gate never deletes — it asks for a rephrase)",
|
|
202
|
+
);
|
|
170
203
|
|
|
171
204
|
out.push(
|
|
172
205
|
"",
|
|
173
206
|
"STORE",
|
|
174
207
|
` ${store.active} active · ${store.superseded} superseded · ${store.open} open · ${store.scopes} scopes` +
|
|
175
|
-
(store.malformed
|
|
208
|
+
(store.malformed
|
|
209
|
+
? ` · ${store.malformed} malformed (skipped; fix the ts= field or undo)`
|
|
210
|
+
: ""),
|
|
176
211
|
);
|
|
177
212
|
if (store.neverRetrieved !== undefined) {
|
|
178
213
|
const used = store.active - store.neverRetrieved;
|
|
179
|
-
const share = store.active
|
|
214
|
+
const share = store.active
|
|
215
|
+
? Math.round((store.neverRetrieved / store.active) * 100)
|
|
216
|
+
: 0;
|
|
180
217
|
// A ledger younger than the window cannot honestly call anything "never"
|
|
181
218
|
// — it has not been watching long enough. Say what it covers instead of
|
|
182
219
|
// letting day-one readers see 100% dead weight.
|
|
@@ -190,13 +227,21 @@ export function renderMemoryReport(input: {
|
|
|
190
227
|
}
|
|
191
228
|
const sizing: string[] = [];
|
|
192
229
|
if (store.bytes !== undefined) {
|
|
193
|
-
sizing.push(
|
|
230
|
+
sizing.push(
|
|
231
|
+
`${fmtBytes(store.bytes)} total · ${fmtBytes(store.bytesPerRow ?? 0)} per row`,
|
|
232
|
+
);
|
|
194
233
|
}
|
|
195
|
-
if (store.rowsPerDay !== undefined)
|
|
234
|
+
if (store.rowsPerDay !== undefined)
|
|
235
|
+
sizing.push(`${store.rowsPerDay} rows/day (30d)`);
|
|
196
236
|
if (store.captureLag && store.captureLag.n) {
|
|
197
|
-
sizing.push(
|
|
237
|
+
sizing.push(
|
|
238
|
+
`capture lag p50 ${fmtMs(store.captureLag.p50)} / p95 ${fmtMs(store.captureLag.p95)}`,
|
|
239
|
+
);
|
|
198
240
|
}
|
|
199
241
|
if (sizing.length) out.push(` ${sizing.join(" · ")}`);
|
|
200
|
-
out.push(
|
|
242
|
+
out.push(
|
|
243
|
+
"",
|
|
244
|
+
"Terminal (CLI/TUI) users: /memory opens this as a live panel; the footer shows a one-line badge.",
|
|
245
|
+
);
|
|
201
246
|
return out.join("\n");
|
|
202
247
|
}
|
package/src/status/write.ts
CHANGED
|
@@ -15,11 +15,17 @@
|
|
|
15
15
|
// sees no status files (it says so) — the RPC surface is the planned follow-up.
|
|
16
16
|
import { readdir, rm, stat } from "node:fs/promises";
|
|
17
17
|
import { join } from "node:path";
|
|
18
|
-
import { evidenceDir, memoryRoot } from "../core/config/paths.ts"
|
|
18
|
+
import { evidenceDir, memoryRoot } from "../core/config/paths.ts";
|
|
19
19
|
import { dateStr } from "../core/time.ts";
|
|
20
|
-
import { parseLine } from "../core/logfmt/format.ts"
|
|
20
|
+
import { parseLine } from "../core/logfmt/format.ts";
|
|
21
21
|
import { readLines } from "../core/logfmt/read.ts";
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
type BlockedRow,
|
|
24
|
+
SESSION_FILE_CAP,
|
|
25
|
+
type StatusPayload,
|
|
26
|
+
readJson,
|
|
27
|
+
writeJson,
|
|
28
|
+
} from "./payload.ts";
|
|
23
29
|
|
|
24
30
|
export function statusFile(): string {
|
|
25
31
|
return join(memoryRoot(), "status.json");
|
|
@@ -41,7 +47,9 @@ export function sessionStatusFile(sessionID: string): string {
|
|
|
41
47
|
|
|
42
48
|
async function pruneStatusDir(): Promise<void> {
|
|
43
49
|
try {
|
|
44
|
-
const names = (await readdir(statusDir())).filter((n) =>
|
|
50
|
+
const names = (await readdir(statusDir())).filter((n) =>
|
|
51
|
+
n.endsWith(".json"),
|
|
52
|
+
);
|
|
45
53
|
if (names.length <= SESSION_FILE_CAP) return;
|
|
46
54
|
const withMtime = await Promise.all(
|
|
47
55
|
names.map(async (n) => {
|
|
@@ -80,7 +88,9 @@ export async function writeStatus(p: StatusPayload): Promise<void> {
|
|
|
80
88
|
* a missing file returns null ("mem -") rather than another session's epoch.
|
|
81
89
|
* Without one: the global latest file.
|
|
82
90
|
*/
|
|
83
|
-
export async function readStatus(
|
|
91
|
+
export async function readStatus(
|
|
92
|
+
sessionID?: string,
|
|
93
|
+
): Promise<StatusPayload | null> {
|
|
84
94
|
try {
|
|
85
95
|
if (sessionID) return await readJson(sessionStatusFile(sessionID));
|
|
86
96
|
return await readJson(statusFile());
|
|
@@ -94,10 +104,14 @@ export async function readStatus(sessionID?: string): Promise<StatusPayload | nu
|
|
|
94
104
|
export async function blockedRecent(limit = 6): Promise<BlockedRow[]> {
|
|
95
105
|
try {
|
|
96
106
|
const today = dateStr();
|
|
97
|
-
const yesterday = new Date(Date.now() - 86_400_000)
|
|
107
|
+
const yesterday = new Date(Date.now() - 86_400_000)
|
|
108
|
+
.toISOString()
|
|
109
|
+
.split("T")[0]!;
|
|
98
110
|
const rows: BlockedRow[] = [];
|
|
99
111
|
for (const day of [yesterday, today]) {
|
|
100
|
-
for (const line of await readLines(
|
|
112
|
+
for (const line of await readLines(
|
|
113
|
+
join(evidenceDir(), `${day}.logfmt`),
|
|
114
|
+
)) {
|
|
101
115
|
const f = parseLine(line);
|
|
102
116
|
if (!f || f.ev !== "write_blocked") continue;
|
|
103
117
|
rows.push({
|
package/src/store/blocklist.ts
CHANGED
|
@@ -6,8 +6,12 @@
|
|
|
6
6
|
import { readFile } from "node:fs/promises";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { memoryRoot } from "../core/config/paths.ts";
|
|
9
|
-
import { formatLine, parseLine } from "../core/logfmt/format.ts"
|
|
10
|
-
import {
|
|
9
|
+
import { formatLine, parseLine } from "../core/logfmt/format.ts";
|
|
10
|
+
import {
|
|
11
|
+
appendLog,
|
|
12
|
+
fileIdentity,
|
|
13
|
+
writeAtomic,
|
|
14
|
+
} from "../core/logfmt/publish.ts";
|
|
11
15
|
import { readLines } from "../core/logfmt/read.ts";
|
|
12
16
|
import { withStoreLock, LOCK_WRITE } from "./lock.ts";
|
|
13
17
|
|
|
@@ -47,7 +51,11 @@ export async function addBlock(ts: string, reason: string): Promise<string> {
|
|
|
47
51
|
const file = blocklistFile();
|
|
48
52
|
await withStoreLock(
|
|
49
53
|
file,
|
|
50
|
-
() =>
|
|
54
|
+
() =>
|
|
55
|
+
appendLog(
|
|
56
|
+
file,
|
|
57
|
+
formatLine({ ts, reason, blocked_at: new Date().toISOString() }),
|
|
58
|
+
),
|
|
51
59
|
LOCK_WRITE,
|
|
52
60
|
);
|
|
53
61
|
return ts;
|
|
@@ -72,7 +80,11 @@ export async function removeBlock(ts: string): Promise<boolean> {
|
|
|
72
80
|
const row = parseLine(l);
|
|
73
81
|
return !(row && row.ts === ts);
|
|
74
82
|
});
|
|
75
|
-
await writeAtomic(
|
|
83
|
+
await writeAtomic(
|
|
84
|
+
file,
|
|
85
|
+
kept.join("\n") + (kept.length ? "\n" : ""),
|
|
86
|
+
since,
|
|
87
|
+
);
|
|
76
88
|
return true;
|
|
77
89
|
},
|
|
78
90
|
LOCK_WRITE,
|
package/src/store/containment.ts
CHANGED
|
@@ -24,7 +24,14 @@
|
|
|
24
24
|
// device policy, not a path check.
|
|
25
25
|
import { lstat, realpath } from "node:fs/promises";
|
|
26
26
|
import { homedir } from "node:os";
|
|
27
|
-
import {
|
|
27
|
+
import {
|
|
28
|
+
basename,
|
|
29
|
+
dirname,
|
|
30
|
+
isAbsolute,
|
|
31
|
+
relative,
|
|
32
|
+
resolve,
|
|
33
|
+
sep,
|
|
34
|
+
} from "node:path";
|
|
28
35
|
|
|
29
36
|
// True when `child` is `parent` itself or lives beneath it. Both must already
|
|
30
37
|
// be absolute and lexically normalised (resolve()).
|
|
@@ -64,7 +71,8 @@ function normalize(requested: string, reject: (why: string) => string): string {
|
|
|
64
71
|
// actually truncate or confuse a path survive it and are still caught here.
|
|
65
72
|
const trimmed = requested.trim();
|
|
66
73
|
if (!trimmed) throw new Error(reject("must not be empty"));
|
|
67
|
-
if (CONTROL_CHARS.test(trimmed))
|
|
74
|
+
if (CONTROL_CHARS.test(trimmed))
|
|
75
|
+
throw new Error(reject("must not contain control characters"));
|
|
68
76
|
const expanded = expandHome(trimmed);
|
|
69
77
|
if (expanded === trimmed && expanded.split(/[/\\]/)[0]?.startsWith("~")) {
|
|
70
78
|
throw new Error(reject("must not use a `~` home path"));
|
|
@@ -81,7 +89,10 @@ function normalize(requested: string, reject: (why: string) => string): string {
|
|
|
81
89
|
// becomes the same neutral rejection. Letting them through verbatim is both an
|
|
82
90
|
// existence oracle — ENOTDIR on `<root>/status.json/x` proves status.json is
|
|
83
91
|
// there — and a raw Bun error in a model-facing tool result.
|
|
84
|
-
async function realAncestor(
|
|
92
|
+
async function realAncestor(
|
|
93
|
+
path: string,
|
|
94
|
+
reject: (why: string) => string,
|
|
95
|
+
): Promise<string> {
|
|
85
96
|
const missing: string[] = [];
|
|
86
97
|
let cursor = path;
|
|
87
98
|
for (;;) {
|
|
@@ -147,7 +158,8 @@ export async function resolveContained(
|
|
|
147
158
|
opts: ContainOptions,
|
|
148
159
|
): Promise<string> {
|
|
149
160
|
const { label } = opts;
|
|
150
|
-
const reject = (why: string) =>
|
|
161
|
+
const reject = (why: string) =>
|
|
162
|
+
`${label} ${why}${opts.hint ? ` — ${opts.hint}` : ""}`;
|
|
151
163
|
const clean = normalize(requested, reject);
|
|
152
164
|
// Escape hatch: still no control characters, no `~` smuggling, but the caller
|
|
153
165
|
// owns the destination and a relative path means what it always meant here —
|
|
@@ -157,7 +169,9 @@ export async function resolveContained(
|
|
|
157
169
|
const rootAbs = resolve(rootDir);
|
|
158
170
|
const reqAbs = resolve(rootAbs, clean);
|
|
159
171
|
if (relative(rootAbs, reqAbs) === "") {
|
|
160
|
-
throw new Error(
|
|
172
|
+
throw new Error(
|
|
173
|
+
reject("must name a file inside the root, not the root itself"),
|
|
174
|
+
);
|
|
161
175
|
}
|
|
162
176
|
if (!isWithin(rootAbs, reqAbs)) {
|
|
163
177
|
throw new Error(reject(`must stay inside ${rootAbs} (got "${requested}")`));
|
|
@@ -182,12 +196,16 @@ export async function resolveContained(
|
|
|
182
196
|
|
|
183
197
|
const realReq = await realAncestor(reqAbs, reject);
|
|
184
198
|
if (!isWithin(realRoot, realReq)) {
|
|
185
|
-
throw new Error(
|
|
199
|
+
throw new Error(
|
|
200
|
+
reject(`escapes ${rootAbs} through a symbolic link (got "${requested}")`),
|
|
201
|
+
);
|
|
186
202
|
}
|
|
187
203
|
if (opts.outerRoot) {
|
|
188
204
|
const realOuter = await realAncestor(resolve(opts.outerRoot), reject);
|
|
189
205
|
if (!isWithin(realOuter, realReq)) {
|
|
190
|
-
throw new Error(
|
|
206
|
+
throw new Error(
|
|
207
|
+
reject(`escapes the store root ${realOuter} (got "${requested}")`),
|
|
208
|
+
);
|
|
191
209
|
}
|
|
192
210
|
}
|
|
193
211
|
|
|
@@ -201,7 +219,8 @@ export async function resolveContained(
|
|
|
201
219
|
if (!isNotFound(err)) throw new Error(reject("could not be resolved"));
|
|
202
220
|
}
|
|
203
221
|
if (finalInfo) {
|
|
204
|
-
if (finalInfo.isSymbolicLink())
|
|
222
|
+
if (finalInfo.isSymbolicLink())
|
|
223
|
+
throw new Error(reject("must not be a symbolic link"));
|
|
205
224
|
if (!finalInfo.isFile()) throw new Error(reject("is not a regular file"));
|
|
206
225
|
}
|
|
207
226
|
return reqAbs;
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
// append — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
import { readFile } from "node:fs/promises";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
-
import { memoryRoot } from "../../core/config/paths.ts"
|
|
4
|
+
import { memoryRoot } from "../../core/config/paths.ts";
|
|
5
5
|
import { dateStr } from "../../core/time.ts";
|
|
6
6
|
import { writeTarget } from "../../identity/repokey.ts";
|
|
7
7
|
import { getCurrent } from "../../core/state.ts";
|
|
8
|
-
import { formatLine, parseLine } from "../../core/logfmt/format.ts"
|
|
8
|
+
import { formatLine, parseLine } from "../../core/logfmt/format.ts";
|
|
9
9
|
import { fileIdentity, writeAtomic } from "../../core/logfmt/publish.ts";
|
|
10
10
|
import { withStoreLock, LOCK_WRITE } from "../lock.ts";
|
|
11
11
|
import { containsSecret, redactSecrets } from "../secrets.ts";
|
|
12
12
|
import { flattenMemoryText } from "../sanitize.ts";
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
MAX_CONTENT_CHARS,
|
|
15
|
+
type MemoryStatus,
|
|
16
|
+
normalizeKeywords,
|
|
17
|
+
} from "../model/memory.ts";
|
|
14
18
|
|
|
15
19
|
export interface AppendMemoryInput {
|
|
16
20
|
type: string;
|
|
@@ -74,9 +78,12 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
74
78
|
// Absolute floor: every write SHOULD arrive through the gate,
|
|
75
79
|
// which rejects invalid input politely; viewer writes do not, so enforce the
|
|
76
80
|
// same storage rules here before anything reaches disk.
|
|
77
|
-
if (typeof input.content !== "string")
|
|
81
|
+
if (typeof input.content !== "string")
|
|
82
|
+
throw new Error("memory content must be a string");
|
|
78
83
|
if (input.content.length > MAX_CONTENT_CHARS) {
|
|
79
|
-
throw new Error(
|
|
84
|
+
throw new Error(
|
|
85
|
+
`memory content exceeds ${MAX_CONTENT_CHARS} chars (${input.content.length})`,
|
|
86
|
+
);
|
|
80
87
|
}
|
|
81
88
|
for (const [field, value] of [
|
|
82
89
|
["type", input.type],
|
|
@@ -92,7 +99,9 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
92
99
|
}
|
|
93
100
|
// Keywords are indexed, never rendered, but they still live on the row's
|
|
94
101
|
// single line, so the same control-character floor applies.
|
|
95
|
-
if (
|
|
102
|
+
if (
|
|
103
|
+
input.kw?.some((k) => typeof k !== "string" || flattenMemoryText(k) !== k)
|
|
104
|
+
) {
|
|
96
105
|
throw new Error("memory keywords must not contain control characters");
|
|
97
106
|
}
|
|
98
107
|
const credentialMetadata = [
|
|
@@ -142,35 +151,46 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
142
151
|
// Locked end to end: seeding ts from a scan another process can race yields
|
|
143
152
|
// two rows with the same identity, and an append that lands inside someone
|
|
144
153
|
// else's whole-file rewrite is simply not there afterwards.
|
|
145
|
-
return withStoreLock(
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
ts
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
154
|
+
return withStoreLock(
|
|
155
|
+
file,
|
|
156
|
+
async () => {
|
|
157
|
+
const since = await fileIdentity(file);
|
|
158
|
+
let prior = "";
|
|
159
|
+
try {
|
|
160
|
+
prior = await readFile(file, "utf8");
|
|
161
|
+
} catch (err) {
|
|
162
|
+
if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err;
|
|
163
|
+
}
|
|
164
|
+
const taken = existingTs(prior);
|
|
165
|
+
let ts = new Date().toISOString();
|
|
166
|
+
let guard = 0;
|
|
167
|
+
while (taken.has(ts) && guard++ < 1000) {
|
|
168
|
+
ts = new Date(new Date(ts).getTime() + 1).toISOString();
|
|
169
|
+
}
|
|
170
|
+
const kw = normalizeKeywords(input.kw);
|
|
171
|
+
const line = formatLine({
|
|
172
|
+
ts,
|
|
173
|
+
type: input.type,
|
|
174
|
+
scope: input.scope,
|
|
175
|
+
content,
|
|
176
|
+
issue: input.issue,
|
|
177
|
+
tags: input.tags?.length ? input.tags.join(",") : undefined,
|
|
178
|
+
kw: kw?.join(","),
|
|
179
|
+
status: input.status,
|
|
180
|
+
repo,
|
|
181
|
+
repoPath,
|
|
182
|
+
});
|
|
183
|
+
// A lock excludes writers, not readers: an O_APPEND read can end mid-row.
|
|
184
|
+
// Publish the complete day file using the read already needed for IDs.
|
|
185
|
+
// This costs a whole-file write and can refuse on Windows reader handles;
|
|
186
|
+
// the old destination remains intact if publication fails (#198).
|
|
187
|
+
await writeAtomic(
|
|
188
|
+
file,
|
|
189
|
+
prior + (prior && !prior.endsWith("\n") ? "\n" : "") + line + "\n",
|
|
190
|
+
since,
|
|
191
|
+
);
|
|
192
|
+
return ts;
|
|
193
|
+
},
|
|
194
|
+
LOCK_WRITE,
|
|
195
|
+
);
|
|
176
196
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// deletions — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
import { deletionsFile } from "../../core/config/paths.ts";
|
|
3
|
-
import { formatLine } from "../../core/logfmt/format.ts"
|
|
3
|
+
import { formatLine } from "../../core/logfmt/format.ts";
|
|
4
4
|
import { appendLog } from "../../core/logfmt/publish.ts";
|
|
5
5
|
import { redactSecrets } from "../secrets.ts";
|
|
6
6
|
import { type Memory } from "../model/memory.ts";
|
|
@@ -10,7 +10,10 @@ export interface DeletedMemory {
|
|
|
10
10
|
reason: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export async function logDeletion(
|
|
13
|
+
export async function logDeletion(
|
|
14
|
+
memory: Memory,
|
|
15
|
+
reason: string,
|
|
16
|
+
): Promise<void> {
|
|
14
17
|
await appendLog(
|
|
15
18
|
deletionsFile(),
|
|
16
19
|
formatLine({
|
package/src/store/files/load.ts
CHANGED
|
@@ -7,11 +7,19 @@ import { existsSync } from "node:fs";
|
|
|
7
7
|
import { open } from "node:fs/promises";
|
|
8
8
|
import { join } from "node:path";
|
|
9
9
|
import { HITS_FILENAME, memoryRoot } from "../../core/config/paths.ts";
|
|
10
|
-
import { parseLine } from "../../core/logfmt/format.ts"
|
|
10
|
+
import { parseLine } from "../../core/logfmt/format.ts";
|
|
11
11
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
12
12
|
import { blockedTs } from "../blocklist.ts";
|
|
13
|
-
import {
|
|
14
|
-
|
|
13
|
+
import {
|
|
14
|
+
ISO_TS_RE,
|
|
15
|
+
type Memory,
|
|
16
|
+
malformedWarnedFiles,
|
|
17
|
+
} from "../model/memory.ts";
|
|
18
|
+
import {
|
|
19
|
+
type Reader,
|
|
20
|
+
isVisibleTo,
|
|
21
|
+
readerForAmbient,
|
|
22
|
+
} from "../model/visibility.ts";
|
|
15
23
|
|
|
16
24
|
const RESERVED = new Set([
|
|
17
25
|
"deletions.logfmt",
|
|
@@ -33,7 +41,11 @@ export function malformedCount(): number {
|
|
|
33
41
|
return malformedRows;
|
|
34
42
|
}
|
|
35
43
|
|
|
36
|
-
export function toMemory(
|
|
44
|
+
export function toMemory(
|
|
45
|
+
fields: Record<string, unknown>,
|
|
46
|
+
file: string,
|
|
47
|
+
index: number,
|
|
48
|
+
): Memory | null {
|
|
37
49
|
const ts = fields.ts;
|
|
38
50
|
const type = fields.type;
|
|
39
51
|
const scope = fields.scope;
|
|
@@ -56,13 +68,22 @@ export function toMemory(fields: Record<string, unknown>, file: string, index: n
|
|
|
56
68
|
content: typeof fields.content === "string" ? fields.content : "",
|
|
57
69
|
issue: typeof fields.issue === "string" ? fields.issue : undefined,
|
|
58
70
|
tags: typeof fields.tags === "string" ? fields.tags.split(",") : undefined,
|
|
59
|
-
kw:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
kw:
|
|
72
|
+
typeof fields.kw === "string" && fields.kw
|
|
73
|
+
? fields.kw
|
|
74
|
+
.split(",")
|
|
75
|
+
.map((k) => k.trim())
|
|
76
|
+
.filter((k) => k.length > 0)
|
|
77
|
+
: undefined,
|
|
78
|
+
superseded_by:
|
|
79
|
+
typeof fields.superseded_by === "string"
|
|
80
|
+
? fields.superseded_by
|
|
81
|
+
: undefined,
|
|
63
82
|
reason: typeof fields.reason === "string" ? fields.reason : undefined,
|
|
64
83
|
status:
|
|
65
|
-
fields.status === "open" || fields.status === "settled"
|
|
84
|
+
fields.status === "open" || fields.status === "settled"
|
|
85
|
+
? fields.status
|
|
86
|
+
: undefined,
|
|
66
87
|
repo: typeof fields.repo === "string" ? fields.repo : undefined,
|
|
67
88
|
repoPath: typeof fields.repoPath === "string" ? fields.repoPath : undefined,
|
|
68
89
|
file,
|
|
@@ -98,9 +119,14 @@ export async function loadAll(): Promise<Memory[]> {
|
|
|
98
119
|
return out;
|
|
99
120
|
}
|
|
100
121
|
|
|
101
|
-
export async function loadScope(
|
|
122
|
+
export async function loadScope(
|
|
123
|
+
scope: string,
|
|
124
|
+
reader: Reader = readerForAmbient(),
|
|
125
|
+
): Promise<Memory[]> {
|
|
102
126
|
// Gate dedup/stale reads are repo-scoped: a row from another repo must not
|
|
103
127
|
// collapse or supersede a write here. The reader is the CALLER's identity,
|
|
104
128
|
// resolved at flow entry — not whatever current says after this await.
|
|
105
|
-
return (await loadAll()).filter(
|
|
129
|
+
return (await loadAll()).filter(
|
|
130
|
+
(m) => m.scope === scope && isVisibleTo(m, reader),
|
|
131
|
+
);
|
|
106
132
|
}
|
package/src/store/files/prune.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
// prune — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
import { join, basename } from "node:path";
|
|
3
|
-
import { parseLine } from "../../core/logfmt/format.ts"
|
|
4
|
-
import { fileIdentity, writeAtomic } from "../../core/logfmt/publish.ts"
|
|
3
|
+
import { parseLine } from "../../core/logfmt/format.ts";
|
|
4
|
+
import { fileIdentity, writeAtomic } from "../../core/logfmt/publish.ts";
|
|
5
5
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
6
6
|
import { withStoreLock, LOCK_WRITE } from "../lock.ts";
|
|
7
7
|
import { tombstoneMany } from "../tombstones/tombstone.ts";
|
|
8
8
|
import { type DeletedMemory, logDeletion } from "./deletions.ts";
|
|
9
9
|
import { memoryFiles, toMemory } from "./load.ts";
|
|
10
10
|
import { snapshotFiles } from "./snapshots.ts";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
type Reader,
|
|
13
|
+
isVisibleTo,
|
|
14
|
+
readerForAmbient,
|
|
15
|
+
} from "../model/visibility.ts";
|
|
12
16
|
|
|
13
17
|
export interface PruneResult {
|
|
14
18
|
deleted: DeletedMemory[];
|
|
@@ -43,7 +47,12 @@ export async function prune(
|
|
|
43
47
|
// Repo floor: forgetting scope X here must never delete
|
|
44
48
|
// another repo's row for scope X — same basename collisions are real
|
|
45
49
|
// (a fork next to its upstream).
|
|
46
|
-
if (
|
|
50
|
+
if (
|
|
51
|
+
m &&
|
|
52
|
+
m.scope === scope &&
|
|
53
|
+
m.type === type &&
|
|
54
|
+
isVisibleTo(m, reader)
|
|
55
|
+
) {
|
|
47
56
|
fileDeleted.push({ memory: m, reason });
|
|
48
57
|
await logDeletion(m, reason);
|
|
49
58
|
changed = true;
|
|
@@ -53,7 +62,11 @@ export async function prune(
|
|
|
53
62
|
}
|
|
54
63
|
if (!changed) return;
|
|
55
64
|
snapshot.push(...(await snapshotFiles([file])));
|
|
56
|
-
await writeAtomic(
|
|
65
|
+
await writeAtomic(
|
|
66
|
+
file,
|
|
67
|
+
kept.join("\n") + (kept.length ? "\n" : ""),
|
|
68
|
+
since,
|
|
69
|
+
);
|
|
57
70
|
deleted.push(...fileDeleted);
|
|
58
71
|
},
|
|
59
72
|
LOCK_WRITE,
|
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
import { open, readFile } from "node:fs/promises";
|
|
3
3
|
import { join, basename } from "node:path";
|
|
4
4
|
import { snapshotDir } from "../../core/config/paths.ts";
|
|
5
|
-
import { parseLine, type Fields } from "../../core/logfmt/format.ts"
|
|
6
|
-
import {
|
|
5
|
+
import { parseLine, type Fields } from "../../core/logfmt/format.ts";
|
|
6
|
+
import {
|
|
7
|
+
ensureDir,
|
|
8
|
+
fileIdentity,
|
|
9
|
+
writeAtomic,
|
|
10
|
+
} from "../../core/logfmt/publish.ts";
|
|
7
11
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
8
12
|
import { withStoreLock, LOCK_WRITE } from "../lock.ts";
|
|
9
13
|
|