opencode-self-improvement 0.20.0 → 0.22.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/README.md +3 -0
- package/package.json +1 -1
- package/src/capture/candidates.ts +13 -0
- package/src/capture/guided-rules.ts +198 -0
- package/src/capture/lag.ts +11 -1
- package/src/capture/policy.ts +254 -0
- package/src/capture/prompt.ts +15 -0
- package/src/capture/runtime.ts +331 -24
- package/src/capture/transcript.ts +42 -0
- package/src/core/hooks.ts +18 -0
- package/src/index.ts +6 -0
- package/src/inject/epochs.ts +47 -1
- package/src/inject/format.ts +8 -1
- package/src/store/blocklist.ts +1 -1
- package/src/store/files/append.ts +86 -57
- package/src/store/files/deletions.ts +1 -1
- package/src/store/files/load.ts +90 -2
- package/src/store/files/prune.ts +14 -7
- package/src/store/files/snapshots.ts +3 -3
- package/src/store/files/supersede.ts +125 -74
- package/src/store/lock.ts +17 -1
- package/src/store/model/memory.ts +25 -0
- package/src/store/model/predicates.ts +1 -1
- package/src/store/model/visibility.ts +4 -4
- package/src/store/prune.ts +2 -2
- package/src/store/quarantine/quarantine.ts +12 -3
- package/src/store/quarantine/release.ts +34 -17
- package/src/store/tombstones/rejection.ts +3 -3
- package/src/store/tombstones/tombstone.ts +9 -8
- package/src/store/undo.ts +4 -4
- package/src/tools/update.ts +19 -7
package/src/store/blocklist.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
writeAtomic,
|
|
14
14
|
} from "../core/logfmt/publish.ts";
|
|
15
15
|
import { readLines } from "../core/logfmt/read.ts";
|
|
16
|
-
import {
|
|
16
|
+
import { LOCK_WRITE, withStoreLock } from "./lock.ts";
|
|
17
17
|
import { redactSecrets } from "./secrets.ts";
|
|
18
18
|
|
|
19
19
|
const BLOCKLIST_FILENAME = "blocklist.logfmt";
|
|
@@ -2,20 +2,21 @@
|
|
|
2
2
|
import { readFile } from "node:fs/promises";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { memoryRoot } from "../../core/config/paths.ts";
|
|
5
|
-
import { dateStr } from "../../core/time.ts";
|
|
6
|
-
import { writeTarget } from "../../identity/repokey.ts";
|
|
7
|
-
import { getCurrent } from "../../core/state.ts";
|
|
8
5
|
import { formatLine, parseLine } from "../../core/logfmt/format.ts";
|
|
9
6
|
import { fileIdentity, writeAtomic } from "../../core/logfmt/publish.ts";
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
7
|
+
import { getCurrent } from "../../core/state.ts";
|
|
8
|
+
import { dateStr } from "../../core/time.ts";
|
|
9
|
+
import { writeTarget } from "../../identity/repokey.ts";
|
|
10
|
+
import { LOCK_WRITE, withStoreLock } from "../lock.ts";
|
|
13
11
|
import {
|
|
14
12
|
MAX_CONTENT_CHARS,
|
|
15
13
|
type MemoryStatus,
|
|
16
14
|
normalizeConflictFields,
|
|
17
15
|
normalizeKeywords,
|
|
16
|
+
normalizeOriginal,
|
|
18
17
|
} from "../model/memory.ts";
|
|
18
|
+
import { flattenMemoryText } from "../sanitize.ts";
|
|
19
|
+
import { containsSecret, redactSecrets } from "../secrets.ts";
|
|
19
20
|
|
|
20
21
|
export interface AppendMemoryInput {
|
|
21
22
|
type: string;
|
|
@@ -33,6 +34,11 @@ export interface AppendMemoryInput {
|
|
|
33
34
|
ref?: string;
|
|
34
35
|
claim?: string;
|
|
35
36
|
klass?: "spec" | "behavior";
|
|
37
|
+
/**
|
|
38
|
+
* Verbatim user utterance behind a guided row (see model/memory.ts).
|
|
39
|
+
* Normalised by appendMemory; junk drops the field only.
|
|
40
|
+
*/
|
|
41
|
+
original?: string;
|
|
36
42
|
// "global" for explicit cross-project writes; imports may carry a validated
|
|
37
43
|
// same-repo origin stamp (see portable.ts). Any missing value is resolved by
|
|
38
44
|
// appendMemory against the live repo scope.
|
|
@@ -154,55 +160,78 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
154
160
|
repoPath = target.scope.path;
|
|
155
161
|
}
|
|
156
162
|
}
|
|
157
|
-
//
|
|
158
|
-
//
|
|
159
|
-
//
|
|
160
|
-
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
163
|
+
// The day file is chosen before the lock because it IS the lock key. A
|
|
164
|
+
// colliding ts is bumped inside that lock, and the bump can cross UTC midnight
|
|
165
|
+
// — which #215 made more than cosmetic: a supersede addresses a row by the day
|
|
166
|
+
// its own ts names, so a row filed under another day is a second, unreachable
|
|
167
|
+
// address for the same identity (two files can then hold the same ts). So when
|
|
168
|
+
// the bumped ts leaves the day, hand the row to the file it names and take that
|
|
169
|
+
// file's lock instead. The loop releases before it re-acquires — never two
|
|
170
|
+
// locks at once — and is bounded only so a pathological store cannot spin.
|
|
171
|
+
let day = dateStr();
|
|
172
|
+
let carryTs: string | undefined;
|
|
173
|
+
for (let attempt = 0; ; attempt++) {
|
|
174
|
+
const file = join(memoryRoot(), `${day}.logfmt`);
|
|
175
|
+
// Locked end to end: seeding ts from a scan another process can race yields
|
|
176
|
+
// two rows with the same identity, and an append that lands inside someone
|
|
177
|
+
// else's whole-file rewrite is simply not there afterwards.
|
|
178
|
+
const outcome = await withStoreLock(
|
|
179
|
+
file,
|
|
180
|
+
async (): Promise<{
|
|
181
|
+
moved?: string;
|
|
182
|
+
bumped?: string;
|
|
183
|
+
ts?: string;
|
|
184
|
+
}> => {
|
|
185
|
+
const since = await fileIdentity(file);
|
|
186
|
+
let prior = "";
|
|
187
|
+
try {
|
|
188
|
+
prior = await readFile(file, "utf8");
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err;
|
|
191
|
+
}
|
|
192
|
+
const taken = existingTs(prior);
|
|
193
|
+
let ts = carryTs ?? new Date().toISOString();
|
|
194
|
+
let guard = 0;
|
|
195
|
+
while (taken.has(ts) && guard++ < 1000) {
|
|
196
|
+
ts = new Date(new Date(ts).getTime() + 1).toISOString();
|
|
197
|
+
}
|
|
198
|
+
const ownDay = ts.slice(0, 10);
|
|
199
|
+
// Carry the bumped identity with the move: re-reading the clock in the
|
|
200
|
+
// new file would hand back the PREVIOUS day's instant and file it under
|
|
201
|
+
// the next day — the same lie in the other direction.
|
|
202
|
+
if (ownDay !== day && attempt < 3) return { moved: ownDay, bumped: ts };
|
|
203
|
+
const kw = normalizeKeywords(input.kw);
|
|
204
|
+
const conflict = normalizeConflictFields(input);
|
|
205
|
+
const original = normalizeOriginal(input.original);
|
|
206
|
+
const line = formatLine({
|
|
207
|
+
ts,
|
|
208
|
+
type: input.type,
|
|
209
|
+
scope: input.scope,
|
|
210
|
+
content,
|
|
211
|
+
issue: input.issue,
|
|
212
|
+
tags: input.tags?.length ? input.tags.join(",") : undefined,
|
|
213
|
+
kw: kw?.join(","),
|
|
214
|
+
status: input.status,
|
|
215
|
+
...conflict,
|
|
216
|
+
...(original ? { original } : {}),
|
|
217
|
+
repo,
|
|
218
|
+
repoPath,
|
|
219
|
+
});
|
|
220
|
+
// A lock excludes writers, not readers: an O_APPEND read can end mid-row.
|
|
221
|
+
// Publish the complete day file using the read already needed for IDs.
|
|
222
|
+
// This costs a whole-file write and can refuse on Windows reader handles;
|
|
223
|
+
// the old destination remains intact if publication fails (#198).
|
|
224
|
+
await writeAtomic(
|
|
225
|
+
file,
|
|
226
|
+
prior + (prior && !prior.endsWith("\n") ? "\n" : "") + line + "\n",
|
|
227
|
+
since,
|
|
228
|
+
);
|
|
229
|
+
return { ts };
|
|
230
|
+
},
|
|
231
|
+
LOCK_WRITE,
|
|
232
|
+
);
|
|
233
|
+
if (outcome.ts !== undefined) return outcome.ts;
|
|
234
|
+
day = outcome.moved as string;
|
|
235
|
+
carryTs = outcome.bumped;
|
|
236
|
+
}
|
|
208
237
|
}
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
import { deletionsFile } from "../../core/config/paths.ts";
|
|
3
3
|
import { formatLine } from "../../core/logfmt/format.ts";
|
|
4
4
|
import { appendLog } from "../../core/logfmt/publish.ts";
|
|
5
|
+
import type { Memory } from "../model/memory.ts";
|
|
5
6
|
import { redactSecrets } from "../secrets.ts";
|
|
6
|
-
import { type Memory } from "../model/memory.ts";
|
|
7
7
|
|
|
8
8
|
export interface DeletedMemory {
|
|
9
9
|
memory: Memory;
|
package/src/store/files/load.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// needs: supersession markers, snapshots and audited deletion.
|
|
6
6
|
import { existsSync } from "node:fs";
|
|
7
7
|
import { open } from "node:fs/promises";
|
|
8
|
-
import { join } from "node:path";
|
|
8
|
+
import { basename, join } from "node:path";
|
|
9
9
|
import { HITS_FILENAME, memoryRoot } from "../../core/config/paths.ts";
|
|
10
10
|
import { parseLine } from "../../core/logfmt/format.ts";
|
|
11
11
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
@@ -16,8 +16,8 @@ import {
|
|
|
16
16
|
malformedWarnedFiles,
|
|
17
17
|
} from "../model/memory.ts";
|
|
18
18
|
import {
|
|
19
|
-
type Reader,
|
|
20
19
|
isVisibleTo,
|
|
20
|
+
type Reader,
|
|
21
21
|
readerForAmbient,
|
|
22
22
|
} from "../model/visibility.ts";
|
|
23
23
|
|
|
@@ -88,6 +88,10 @@ export function toMemory(
|
|
|
88
88
|
fields.klass === "spec" || fields.klass === "behavior"
|
|
89
89
|
? fields.klass
|
|
90
90
|
: undefined,
|
|
91
|
+
original:
|
|
92
|
+
typeof fields.original === "string" && fields.original
|
|
93
|
+
? fields.original
|
|
94
|
+
: undefined,
|
|
91
95
|
reason: typeof fields.reason === "string" ? fields.reason : undefined,
|
|
92
96
|
status:
|
|
93
97
|
fields.status === "open" || fields.status === "settled"
|
|
@@ -114,6 +118,90 @@ export async function memoryFiles(): Promise<string[]> {
|
|
|
114
118
|
return files.sort();
|
|
115
119
|
}
|
|
116
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The day file a row's own ts names, as a `YYYY-MM-DD.logfmt` basename, or null
|
|
123
|
+
* when the string is not an ISO instant. `appendMemory` mints ts at write time
|
|
124
|
+
* and appends to that same day file, so for every row the plugin wrote the ts
|
|
125
|
+
* date IS the file date (#215).
|
|
126
|
+
*/
|
|
127
|
+
export function dayFileName(ts: string): string | null {
|
|
128
|
+
const m = /^(\d{4}-\d{2}-\d{2})T/.exec(ts);
|
|
129
|
+
return m ? `${m[1]}.logfmt` : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Day files ordered so the ones these ts values name come first (#215). A
|
|
134
|
+
* single-row mutation that stops as soon as it has SEEN the row then locks one
|
|
135
|
+
* file instead of one per day of store age — the cost that made supersing a row
|
|
136
|
+
* 25x a full read on a 30-day store.
|
|
137
|
+
*
|
|
138
|
+
* The mapping is a hint, never a proof: a store edited outside the plugin can
|
|
139
|
+
* hold a row in a file its ts does not name. Callers must keep walking the rest
|
|
140
|
+
* of the list until they find the row, which is the behaviour this replaced.
|
|
141
|
+
*/
|
|
142
|
+
export async function filesOrderedFor(tsValues: string[]): Promise<string[]> {
|
|
143
|
+
const all = await memoryFiles();
|
|
144
|
+
const names = new Set<string>();
|
|
145
|
+
for (const ts of tsValues) {
|
|
146
|
+
const name = dayFileName(ts);
|
|
147
|
+
if (name) names.add(name);
|
|
148
|
+
}
|
|
149
|
+
if (!names.size) return all;
|
|
150
|
+
const near: string[] = [];
|
|
151
|
+
const rest: string[] = [];
|
|
152
|
+
for (const file of all) {
|
|
153
|
+
(names.has(basename(file)) ? near : rest).push(file);
|
|
154
|
+
}
|
|
155
|
+
return [...near, ...rest];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The day BEFORE `YYYY-MM-DD`, UTC. Month and year boundaries included: the
|
|
160
|
+
* only caller uses it to name a file that may hold a row appended a millisecond
|
|
161
|
+
* before midnight.
|
|
162
|
+
*/
|
|
163
|
+
export function previousDayName(day: string): string {
|
|
164
|
+
return new Date(new Date(`${day}T12:00:00.000Z`).getTime() - 86_400_000)
|
|
165
|
+
.toISOString()
|
|
166
|
+
.slice(0, 10);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The day files that could hold a copy of `ts` — the addresses a single-row
|
|
171
|
+
* mutation must try before it may report success (#373 review).
|
|
172
|
+
*
|
|
173
|
+
* Two files, at most. The one the ts names, plus the previous day's file ONLY
|
|
174
|
+
* when a text scan finds the identity in it: `appendMemory` used to pick the
|
|
175
|
+
* file from the clock and bump a colliding ts inside the lock, so a row whose ts
|
|
176
|
+
* is one millisecond past midnight could be filed under the day before it. That
|
|
177
|
+
* is the only way the plugin can write a row outside its own day, so the
|
|
178
|
+
* neighbour scan is complete, and scanning (not locking) keeps the ordinary
|
|
179
|
+
* single-row mutation at ONE lock.
|
|
180
|
+
*
|
|
181
|
+
* Reading without a lock is sound because this only decides what to lock next:
|
|
182
|
+
* every mutation re-reads under the lock it takes.
|
|
183
|
+
*
|
|
184
|
+
* Empty for a ts that is not an ISO instant — the caller then sweeps, as before.
|
|
185
|
+
*/
|
|
186
|
+
export async function filesHoldingTs(
|
|
187
|
+
ts: string,
|
|
188
|
+
present?: Set<string>,
|
|
189
|
+
): Promise<string[]> {
|
|
190
|
+
const name = dayFileName(ts);
|
|
191
|
+
if (!name) return [];
|
|
192
|
+
const have =
|
|
193
|
+
present ?? new Set((await memoryFiles()).map((f) => basename(f)));
|
|
194
|
+
const out: string[] = [];
|
|
195
|
+
if (have.has(name)) out.push(join(memoryRoot(), name));
|
|
196
|
+
const prev = `${previousDayName(name.slice(0, 10))}.logfmt`;
|
|
197
|
+
if (have.has(prev)) {
|
|
198
|
+
const file = join(memoryRoot(), prev);
|
|
199
|
+
const text = await Bun.file(file).text();
|
|
200
|
+
if (text.includes(`ts=${ts}`)) out.push(file);
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
|
|
117
205
|
export async function loadAll(): Promise<Memory[]> {
|
|
118
206
|
malformedRows = 0; // per-load telemetry: the most recent read wins
|
|
119
207
|
const blocked = await blockedTs();
|
package/src/store/files/prune.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
// prune — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
|
-
import {
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
3
|
import { parseLine } from "../../core/logfmt/format.ts";
|
|
4
4
|
import { fileIdentity, writeAtomic } from "../../core/logfmt/publish.ts";
|
|
5
5
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
6
|
-
import {
|
|
7
|
-
import { tombstoneMany } from "../tombstones/tombstone.ts";
|
|
8
|
-
import { type DeletedMemory, logDeletion } from "./deletions.ts";
|
|
9
|
-
import { memoryFiles, toMemory } from "./load.ts";
|
|
10
|
-
import { snapshotFiles } from "./snapshots.ts";
|
|
6
|
+
import { LOCK_WRITE, withStoreLock } from "../lock.ts";
|
|
11
7
|
import {
|
|
12
|
-
type Reader,
|
|
13
8
|
isVisibleTo,
|
|
9
|
+
type Reader,
|
|
14
10
|
readerForAmbient,
|
|
15
11
|
} from "../model/visibility.ts";
|
|
12
|
+
import { tombstoneMany } from "../tombstones/tombstone.ts";
|
|
13
|
+
import { type DeletedMemory, logDeletion } from "./deletions.ts";
|
|
14
|
+
import { memoryFiles, toMemory } from "./load.ts";
|
|
15
|
+
import { snapshotFiles } from "./snapshots.ts";
|
|
16
16
|
|
|
17
17
|
export interface PruneResult {
|
|
18
18
|
deleted: DeletedMemory[];
|
|
@@ -45,6 +45,13 @@ export async function prune(
|
|
|
45
45
|
bookkeepingError = bookkeepingError ? `${bookkeepingError}; ${msg}` : msg;
|
|
46
46
|
};
|
|
47
47
|
try {
|
|
48
|
+
// #215 narrowed the ts-addressed write paths (supersede, clearSuperseded,
|
|
49
|
+
// releaseQuarantine) to the day file the row's own ts names. This one keeps
|
|
50
|
+
// the full sweep on purpose: a forget is addressed by scope/type, not by ts,
|
|
51
|
+
// so the only way to know which files match is to look — and a pre-pass that
|
|
52
|
+
// skipped a file would let a row appended during the forget survive it. The
|
|
53
|
+
// per-file body re-reads under the lock precisely so "deleted" means deleted;
|
|
54
|
+
// a forget is a user action, not a per-write path, so it pays the sweep.
|
|
48
55
|
for (const file of await memoryFiles()) {
|
|
49
56
|
const fileDeleted: DeletedMemory[] = [];
|
|
50
57
|
await withStoreLock(
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// snapshots — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
import { open, readFile } from "node:fs/promises";
|
|
3
|
-
import {
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
4
|
import { snapshotDir } from "../../core/config/paths.ts";
|
|
5
|
-
import {
|
|
5
|
+
import { type Fields, parseLine } from "../../core/logfmt/format.ts";
|
|
6
6
|
import {
|
|
7
7
|
ensureDir,
|
|
8
8
|
fileIdentity,
|
|
9
9
|
writeAtomic,
|
|
10
10
|
} from "../../core/logfmt/publish.ts";
|
|
11
11
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
12
|
-
import {
|
|
12
|
+
import { LOCK_WRITE, withStoreLock } from "../lock.ts";
|
|
13
13
|
|
|
14
14
|
// Copy `file` into snapshots/ before a mutation.
|
|
15
15
|
export async function snapshotBefore(file: string): Promise<void> {
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
// supersede — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
type Fields,
|
|
6
|
+
formatLine,
|
|
7
|
+
parseLine,
|
|
8
|
+
} from "../../core/logfmt/format.ts";
|
|
4
9
|
import { fileIdentity, writeAtomic } from "../../core/logfmt/publish.ts";
|
|
5
10
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
6
|
-
import {
|
|
11
|
+
import { LOCK_WRITE, withStoreLock } from "../lock.ts";
|
|
7
12
|
import { redactSecrets } from "../secrets.ts";
|
|
8
13
|
import { appendMemory } from "./append.ts";
|
|
9
|
-
import { memoryFiles } from "./load.ts";
|
|
14
|
+
import { filesHoldingTs, filesOrderedFor, memoryFiles } from "./load.ts";
|
|
10
15
|
import { snapshotBefore, stampRow } from "./snapshots.ts";
|
|
11
16
|
|
|
12
17
|
export interface SupersedeFields {
|
|
@@ -63,55 +68,79 @@ export async function markManySupersededReport(
|
|
|
63
68
|
const wanted = new Map<string, SupersedeFields>();
|
|
64
69
|
for (const e of entries) if (!wanted.has(e.ts)) wanted.set(e.ts, e.fields);
|
|
65
70
|
const marked: string[] = [];
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
71
|
+
const seen = new Set<string>();
|
|
72
|
+
const markFile = (file: string) =>
|
|
73
|
+
withStoreLock(
|
|
74
|
+
file,
|
|
75
|
+
async () => {
|
|
76
|
+
// #106: identity BEFORE the read, like every other whole-file
|
|
77
|
+
// read-modify-write (#89's contract) - the publish below may retry a
|
|
78
|
+
// Windows handover only while the destination still matches this.
|
|
79
|
+
const since = await fileIdentity(file);
|
|
80
|
+
const lines = await readLines(file);
|
|
81
|
+
const ids: string[] = [];
|
|
82
|
+
for (let i = 0; i < lines.length; i++) {
|
|
83
|
+
const parsed = parseLine(lines[i]);
|
|
84
|
+
if (!parsed) continue;
|
|
85
|
+
if (typeof parsed.ts === "string" && wanted.has(parsed.ts))
|
|
86
|
+
seen.add(parsed.ts);
|
|
87
|
+
if (typeof parsed.superseded_by === "string") continue;
|
|
88
|
+
const fields =
|
|
89
|
+
typeof parsed.ts === "string" ? wanted.get(parsed.ts) : undefined;
|
|
90
|
+
if (!fields) continue;
|
|
91
|
+
// Whole line rewritten, not extended: an old-format line comes out in
|
|
92
|
+
// the current format with its values unchanged (as markSuperseded did).
|
|
93
|
+
// The one exception is SECRET SCRUB (#128, review probe 4): the
|
|
94
|
+
// fields a merge/decay/update copies into the loser's row carry
|
|
95
|
+
// caller-authored prose (the rotation note "key ghp_... rotated"),
|
|
96
|
+
// and content already scrubs at this same boundary (appendMemory
|
|
97
|
+
// redacts content in src/store/files/append.ts). The
|
|
98
|
+
// durable row must not be the one place a credential survives when
|
|
99
|
+
// the evidence line beside it was cleaned.
|
|
100
|
+
const clean: SupersedeFields = { ...fields };
|
|
101
|
+
for (const k of Object.keys(clean)) {
|
|
102
|
+
const v = (clean as unknown as Record<string, unknown>)[k];
|
|
103
|
+
if (typeof v === "string")
|
|
104
|
+
(clean as unknown as Record<string, unknown>)[k] =
|
|
105
|
+
redactSecrets(v);
|
|
101
106
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
lines[i] = formatLine({ ...parsed, ...clean });
|
|
108
|
+
ids.push(parsed.ts as string);
|
|
109
|
+
}
|
|
110
|
+
if (ids.length === 0) return [];
|
|
111
|
+
await snapshotBefore(file);
|
|
112
|
+
await writeAtomic(file, lines.join("\n") + "\n", since);
|
|
113
|
+
durable?.push(...ids);
|
|
114
|
+
return ids;
|
|
115
|
+
},
|
|
116
|
+
LOCK_WRITE,
|
|
110
117
|
);
|
|
118
|
+
|
|
119
|
+
// #215 + #373 review: retire every address a row can have, and pay for it.
|
|
120
|
+
// Pass 1 walks the day files the batch's own ts values name, plus the previous
|
|
121
|
+
// day's file ONLY where a cheap scan proves a second copy lives there — the
|
|
122
|
+
// one shape an older `appendMemory` could write at UTC midnight. A one-row
|
|
123
|
+
// supersede therefore still costs one lock, and a success can never leave a
|
|
124
|
+
// live copy behind under the same identity.
|
|
125
|
+
const present = new Set((await memoryFiles()).map((f) => basename(f)));
|
|
126
|
+
const addressed = new Set<string>();
|
|
127
|
+
for (const ts of wanted.keys())
|
|
128
|
+
for (const f of await filesHoldingTs(ts, present)) addressed.add(f);
|
|
129
|
+
for (const file of addressed) marked.push(...(await markFile(file)));
|
|
130
|
+
// Pass 2: a target neither address held — a store edited outside the plugin,
|
|
131
|
+
// or a day file that was renamed — keeps the full sweep this replaced.
|
|
132
|
+
if (seen.size < wanted.size) {
|
|
133
|
+
for (const file of await filesOrderedFor([...wanted.keys()])) {
|
|
134
|
+
if (addressed.has(file)) continue;
|
|
135
|
+
marked.push(...(await markFile(file)));
|
|
136
|
+
if (seen.size === wanted.size) break;
|
|
137
|
+
}
|
|
111
138
|
}
|
|
112
|
-
// A ts that appears in
|
|
113
|
-
//
|
|
114
|
-
//
|
|
139
|
+
// A ts that appears in more than one file retires in EVERY file that holds it
|
|
140
|
+
// (#373 review): reporting a retirement while a live copy stands would be the
|
|
141
|
+
// worse bug, so the neighbour-day scan above exists. `appendMemory` now also
|
|
142
|
+
// refuses to create the collision in the first place; the scan is what keeps a
|
|
143
|
+
// store written before that fix honest.
|
|
115
144
|
return { marked };
|
|
116
145
|
}
|
|
117
146
|
|
|
@@ -137,31 +166,53 @@ export async function clearSuperseded(
|
|
|
137
166
|
ts: string,
|
|
138
167
|
provenance?: ClearProvenance,
|
|
139
168
|
): Promise<boolean> {
|
|
169
|
+
const clear = (parsed: Fields) => {
|
|
170
|
+
if (typeof parsed.superseded_by !== "string") return null;
|
|
171
|
+
if (
|
|
172
|
+
provenance &&
|
|
173
|
+
parsed.superseded_by !== provenance.supersededBy &&
|
|
174
|
+
parsed.reason !== provenance.reason
|
|
175
|
+
)
|
|
176
|
+
return null;
|
|
177
|
+
// These six keys are written only by markSuperseded (drift, canonical,
|
|
178
|
+
// update and merge_review), never by appendMemory, so they are all part of
|
|
179
|
+
// the marker and go with it. Field order round-trips because parseLine
|
|
180
|
+
// inserts keys in source order.
|
|
181
|
+
for (const key of [
|
|
182
|
+
"superseded_by",
|
|
183
|
+
"reason",
|
|
184
|
+
"symbol",
|
|
185
|
+
"from",
|
|
186
|
+
"to",
|
|
187
|
+
"entity",
|
|
188
|
+
]) {
|
|
189
|
+
delete parsed[key];
|
|
190
|
+
}
|
|
191
|
+
return formatLine(parsed);
|
|
192
|
+
};
|
|
193
|
+
// #215 + #373 review: revive every copy a row has. The common case is the one
|
|
194
|
+
// day file the ts names; the previous day's file joins ONLY when a scan finds
|
|
195
|
+
// the same identity in it, which is what an older midnight-boundary append
|
|
196
|
+
// could write. Reviving one copy while a duplicate stayed retired would put
|
|
197
|
+
// the row back in recall with a stale sibling still on disk.
|
|
198
|
+
const present = new Set((await memoryFiles()).map((f) => basename(f)));
|
|
199
|
+
const candidates = await filesHoldingTs(ts, present);
|
|
200
|
+
let found = false;
|
|
201
|
+
let cleared = false;
|
|
202
|
+
for (const file of candidates) {
|
|
203
|
+
const outcome = await stampRow(file, ts, clear);
|
|
204
|
+
if (outcome === null) continue;
|
|
205
|
+
found = true;
|
|
206
|
+
cleared = cleared || outcome;
|
|
207
|
+
}
|
|
208
|
+
if (found) return cleared;
|
|
209
|
+
// Never seen where it should be: the sweep this replaced, unchanged. A store
|
|
210
|
+
// edited outside the plugin can hold the row under any name, so this one reads
|
|
211
|
+
// every file rather than trusting the day mapping.
|
|
212
|
+
const tried = new Set(candidates);
|
|
140
213
|
for (const file of await memoryFiles()) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
provenance &&
|
|
145
|
-
parsed.superseded_by !== provenance.supersededBy &&
|
|
146
|
-
parsed.reason !== provenance.reason
|
|
147
|
-
)
|
|
148
|
-
return null;
|
|
149
|
-
// These six keys are written only by markSuperseded (drift, canonical,
|
|
150
|
-
// update and merge_review), never by appendMemory, so they are all part of
|
|
151
|
-
// the marker and go with it. Field order round-trips because parseLine
|
|
152
|
-
// inserts keys in source order.
|
|
153
|
-
for (const key of [
|
|
154
|
-
"superseded_by",
|
|
155
|
-
"reason",
|
|
156
|
-
"symbol",
|
|
157
|
-
"from",
|
|
158
|
-
"to",
|
|
159
|
-
"entity",
|
|
160
|
-
]) {
|
|
161
|
-
delete parsed[key];
|
|
162
|
-
}
|
|
163
|
-
return formatLine(parsed);
|
|
164
|
-
});
|
|
214
|
+
if (tried.has(file)) continue;
|
|
215
|
+
const outcome = await stampRow(file, ts, clear);
|
|
165
216
|
if (outcome !== null) return outcome;
|
|
166
217
|
}
|
|
167
218
|
return false;
|
package/src/store/lock.ts
CHANGED
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
// (writeAtomic in logfmt.ts), so a reader always sees a complete store, never
|
|
23
23
|
// the middle of a rewrite.
|
|
24
24
|
import { open, stat, unlink } from "node:fs/promises";
|
|
25
|
-
import { ensureDir } from "../core/logfmt/publish.ts";
|
|
26
25
|
import { basename, dirname } from "node:path";
|
|
26
|
+
import { ensureDir } from "../core/logfmt/publish.ts";
|
|
27
27
|
import { record } from "../evidence/record.ts";
|
|
28
28
|
|
|
29
29
|
const LOCK_SUFFIX = ".lock";
|
|
@@ -147,6 +147,21 @@ async function stealIfStale(lockPath: string, now: number): Promise<boolean> {
|
|
|
147
147
|
* tests that must land the budget deadline inside a specific probe iteration;
|
|
148
148
|
* production callers never pass it and the real clock is used.
|
|
149
149
|
*/
|
|
150
|
+
/**
|
|
151
|
+
* Test seam (#215): observe how many store files a write path locks.
|
|
152
|
+
*
|
|
153
|
+
* The narrowing this ticket asked for is invisible to a functional test — a
|
|
154
|
+
* supersede that sweeps every day file and one that touches a single file both
|
|
155
|
+
* leave the same store behind — so the assertion has to be about lock
|
|
156
|
+
* acquisitions. Production never installs a hook and pays one undefined check
|
|
157
|
+
* per acquisition; a test sets one, reads the counts, and clears it.
|
|
158
|
+
*/
|
|
159
|
+
let acquiredHook: ((file: string) => void) | undefined;
|
|
160
|
+
|
|
161
|
+
export function setLockAcquiredHook(hook?: (file: string) => void): void {
|
|
162
|
+
acquiredHook = hook;
|
|
163
|
+
}
|
|
164
|
+
|
|
150
165
|
export async function holdFileLock<T>(
|
|
151
166
|
lockPath: string,
|
|
152
167
|
file: string,
|
|
@@ -177,6 +192,7 @@ export async function holdFileLock<T>(
|
|
|
177
192
|
} finally {
|
|
178
193
|
await handle.close();
|
|
179
194
|
}
|
|
195
|
+
acquiredHook?.(file);
|
|
180
196
|
break;
|
|
181
197
|
} catch (err) {
|
|
182
198
|
// Any creation failure means: wait for the current holder, whatever
|