opencode-self-improvement 0.19.0 → 0.21.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 +19 -1
- package/package.json +2 -1
- package/src/capture/candidates.ts +37 -1
- package/src/capture/guided-rules.ts +198 -0
- package/src/capture/lag.ts +11 -1
- package/src/capture/prompt.ts +23 -0
- package/src/capture/runtime.ts +244 -23
- package/src/capture/transcript.ts +24 -0
- package/src/core/hooks.ts +18 -0
- package/src/gate/gate.ts +5 -0
- package/src/index.ts +8 -0
- package/src/inject/budget.ts +8 -2
- package/src/inject/epochs.ts +74 -5
- package/src/inject/format.ts +8 -1
- package/src/recall/conflicts.ts +247 -0
- package/src/recall/relevant.ts +47 -4
- package/src/store/blocklist.ts +1 -1
- package/src/store/files/append.ts +96 -55
- package/src/store/files/deletions.ts +1 -1
- package/src/store/files/load.ts +99 -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 +62 -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/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
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// memory — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
|
|
3
|
+
import { flattenMemoryText } from "../sanitize.ts";
|
|
4
|
+
|
|
3
5
|
export type MemoryStatus = "open" | "settled";
|
|
4
6
|
|
|
5
7
|
// Hard ceiling on stored content. No entry point may persist a line longer
|
|
@@ -41,6 +43,52 @@ export function normalizeKeywords(kw: unknown): string[] | undefined {
|
|
|
41
43
|
return out.length ? out : undefined;
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
/** A conflict ref names a code identifier, not a path or expression. */
|
|
47
|
+
const CONFLICT_REF_RE = /^[A-Za-z_][A-Za-z0-9_]{1,63}$/;
|
|
48
|
+
|
|
49
|
+
export const MAX_CLAIM_CHARS = 32;
|
|
50
|
+
|
|
51
|
+
/** Cap on the verbatim user utterance a guided row may carry (bench #347 follow-up). */
|
|
52
|
+
export const MAX_ORIGINAL_CHARS = 300;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Normalise the verbatim user utterance stored beside a guided row. TOTAL on
|
|
56
|
+
* purpose, like the conflict fields: a junk or over-long quote drops the
|
|
57
|
+
* field, never the row. The value must already be whitespace-flattened; the
|
|
58
|
+
* capture runtime verifies it is a true substring of what the model saw, so
|
|
59
|
+
* a stored quote is the user's own words, not the capture model's paraphrase
|
|
60
|
+
* (FINDINGS mystery 2, candidate 4: a paraphrased hint made the bench's
|
|
61
|
+
* exact-text needle miss a row that actually rode).
|
|
62
|
+
*/
|
|
63
|
+
export function normalizeOriginal(original: unknown): string | undefined {
|
|
64
|
+
if (typeof original !== "string") return undefined;
|
|
65
|
+
const o = original.trim();
|
|
66
|
+
if (!o || o.length > MAX_ORIGINAL_CHARS) return undefined;
|
|
67
|
+
if (flattenMemoryText(o) !== o) return undefined;
|
|
68
|
+
return o;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Normalise the contradiction-surface fields of a guided candidate. TOTAL on
|
|
73
|
+
* purpose, like keywords: a junk ref/claim/klass drops the fields, never the
|
|
74
|
+
* row — the prose still deserved storage, it just cannot be code-checked.
|
|
75
|
+
*/
|
|
76
|
+
export function normalizeConflictFields(o: {
|
|
77
|
+
ref?: unknown;
|
|
78
|
+
claim?: unknown;
|
|
79
|
+
klass?: unknown;
|
|
80
|
+
}): { ref?: string; claim?: string; klass?: "spec" | "behavior" } {
|
|
81
|
+
const out: { ref?: string; claim?: string; klass?: "spec" | "behavior" } = {};
|
|
82
|
+
if (typeof o.ref === "string" && CONFLICT_REF_RE.test(o.ref)) out.ref = o.ref;
|
|
83
|
+
if (typeof o.claim === "string") {
|
|
84
|
+
const c = o.claim.trim();
|
|
85
|
+
if (c && c.length <= MAX_CLAIM_CHARS && flattenMemoryText(c) === c)
|
|
86
|
+
out.claim = c;
|
|
87
|
+
}
|
|
88
|
+
if (o.klass === "spec" || o.klass === "behavior") out.klass = o.klass;
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
|
|
44
92
|
export interface Memory {
|
|
45
93
|
ts: string;
|
|
46
94
|
type: string;
|
|
@@ -64,6 +112,20 @@ export interface Memory {
|
|
|
64
112
|
// in isVisibleHere().
|
|
65
113
|
repo?: string;
|
|
66
114
|
repoPath?: string;
|
|
115
|
+
// Contradiction surface (POC, bench FINDINGS §4): a user-guided row may
|
|
116
|
+
// name the code symbol (ref) its value (claim) is about and who holds
|
|
117
|
+
// authority (klass). Injection reads the code at render time — deterministically,
|
|
118
|
+
// no model call — and a collision renders one CONFLICT line whose Action is
|
|
119
|
+
// fixed by the class: behavior class trusts the code, spec class fixes it.
|
|
120
|
+
// #345 measured the alternative: a wrong guided row rewrote correct code.
|
|
121
|
+
ref?: string;
|
|
122
|
+
claim?: string;
|
|
123
|
+
klass?: "spec" | "behavior";
|
|
124
|
+
// Verbatim user utterance behind a guided row, written by capture after a
|
|
125
|
+
// substring check against the transcript the capture model saw. Injection
|
|
126
|
+
// renders it, so the user's exact words reach the next session's wire bytes
|
|
127
|
+
// even when the distilled content paraphrases them.
|
|
128
|
+
original?: string;
|
|
67
129
|
file: string;
|
|
68
130
|
index: number;
|
|
69
131
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// predicates — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
import { isQuarantined } from "../quarantine/quarantine.ts";
|
|
3
|
-
import {
|
|
3
|
+
import type { Memory } from "./memory.ts";
|
|
4
4
|
|
|
5
5
|
export function isActive(m: Memory): boolean {
|
|
6
6
|
return !m.superseded_by;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// Repository visibility, shared by memory rows, restore lines, and evidence.
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
+
import { parseLine } from "../../core/logfmt/format.ts";
|
|
4
|
+
import { getCurrent } from "../../core/state.ts";
|
|
3
5
|
import {
|
|
4
6
|
normalizeDirPath,
|
|
7
|
+
type RepoScope,
|
|
5
8
|
repoScopeFor,
|
|
6
9
|
sessionScope,
|
|
7
|
-
type RepoScope,
|
|
8
10
|
} from "../../identity/repokey.ts";
|
|
9
|
-
import { getCurrent } from "../../core/state.ts";
|
|
10
|
-
import { parseLine } from "../../core/logfmt/format.ts";
|
|
11
11
|
import { prune } from "../files/prune.ts";
|
|
12
|
-
import {
|
|
12
|
+
import type { Memory } from "./memory.ts";
|
|
13
13
|
|
|
14
14
|
// --- repo visibility ---------------------------------------------------------
|
|
15
15
|
// A READER is an immutable caller identity, resolved once at the entry of a
|
package/src/store/prune.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// reversible (snapshot) and never touches a repo. The `prune` evidence line is
|
|
3
3
|
// emitted after prune() returns so S5 can assert the reason and snapshot.
|
|
4
4
|
import { deletionsFile } from "../core/config/paths.ts";
|
|
5
|
-
import { prune, type PruneResult } from "./files/prune.ts";
|
|
6
|
-
import { readerFromSid } from "./model/visibility.ts";
|
|
7
5
|
import { record } from "../evidence/record.ts";
|
|
6
|
+
import { type PruneResult, prune } from "./files/prune.ts";
|
|
7
|
+
import { readerFromSid } from "./model/visibility.ts";
|
|
8
8
|
|
|
9
9
|
export async function pruneMemory(
|
|
10
10
|
scope: string,
|
|
@@ -41,15 +41,24 @@ export function quarantineReasons(row: { tags?: string[] }): string[] {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export function quarantineCandidate<
|
|
44
|
-
T extends {
|
|
44
|
+
T extends {
|
|
45
|
+
content: string;
|
|
46
|
+
scope?: string;
|
|
47
|
+
original?: string;
|
|
48
|
+
tags?: string[];
|
|
49
|
+
},
|
|
45
50
|
>(candidate: T): T {
|
|
46
51
|
// #205: scope renders model-facing (inject/format.ts prints it on every
|
|
47
|
-
// injected line), so the scanner covers it as well as content.
|
|
48
|
-
//
|
|
52
|
+
// injected line), so the scanner covers it as well as content. #364 review:
|
|
53
|
+
// so does `original`, the verbatim quote the same line prints as
|
|
54
|
+
// `(user said: "...")` — a benign summary carrying an instruction-shaped
|
|
55
|
+
// quote reached the prompt untagged. Hits are merged and deduplicated;
|
|
56
|
+
// provenance tags survive untouched.
|
|
49
57
|
const hits = [
|
|
50
58
|
...new Set([
|
|
51
59
|
...quarantinePatterns(candidate.content),
|
|
52
60
|
...(candidate.scope ? quarantinePatterns(candidate.scope) : []),
|
|
61
|
+
...(candidate.original ? quarantinePatterns(candidate.original) : []),
|
|
53
62
|
]),
|
|
54
63
|
];
|
|
55
64
|
if (!hits.length) return candidate;
|
|
@@ -1,29 +1,46 @@
|
|
|
1
1
|
// release — split from src/memory.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { QUARANTINE_TAG, quarantineReasons } from "./quarantine.ts";
|
|
5
|
-
import { memoryFiles } from "../files/load.ts";
|
|
2
|
+
import { type Fields, formatLine } from "../../core/logfmt/format.ts";
|
|
3
|
+
import { filesHoldingTs, memoryFiles } from "../files/load.ts";
|
|
6
4
|
import { stampRow } from "../files/snapshots.ts";
|
|
5
|
+
import { QUARANTINE_TAG, quarantineReasons } from "./quarantine.ts";
|
|
7
6
|
|
|
8
7
|
// Human viewer action only. Preserve identity, origin, and all non-quarantine
|
|
9
8
|
// metadata; stampRow snapshots and atomically publishes under the store lock.
|
|
10
9
|
export async function releaseQuarantine(
|
|
11
10
|
ts: string,
|
|
12
11
|
): Promise<"released" | "not_found" | "not_quarantined"> {
|
|
12
|
+
const release = (fields: Fields) => {
|
|
13
|
+
const tags = typeof fields.tags === "string" ? fields.tags.split(",") : [];
|
|
14
|
+
if (!tags.includes(QUARANTINE_TAG)) return null;
|
|
15
|
+
const scanTags = new Set([
|
|
16
|
+
QUARANTINE_TAG,
|
|
17
|
+
...quarantineReasons({ tags }).map((id) => `quarantine:${id}`),
|
|
18
|
+
]);
|
|
19
|
+
const kept = tags.filter((tag) => !scanTags.has(tag));
|
|
20
|
+
if (kept.length) fields.tags = kept.join(",");
|
|
21
|
+
else delete fields.tags;
|
|
22
|
+
return formatLine(fields);
|
|
23
|
+
};
|
|
24
|
+
// #215 + #373 review: release every copy. The day file the ts names is the
|
|
25
|
+
// common case; the previous day's file joins ONLY when a scan finds the same
|
|
26
|
+
// identity in it, which is what an older midnight-boundary append could write.
|
|
27
|
+
// Leaving a duplicate quarantined beside a released copy would keep the row
|
|
28
|
+
// hidden from the operator who just released it.
|
|
29
|
+
const candidates = await filesHoldingTs(ts);
|
|
30
|
+
let found = false;
|
|
31
|
+
let released = false;
|
|
32
|
+
for (const file of candidates) {
|
|
33
|
+
const result = await stampRow(file, ts, release);
|
|
34
|
+
if (result === null) continue;
|
|
35
|
+
found = true;
|
|
36
|
+
released = released || result;
|
|
37
|
+
}
|
|
38
|
+
if (found) return released ? "released" : "not_quarantined";
|
|
39
|
+
// Never seen where it should be: the sweep this replaced, unchanged.
|
|
40
|
+
const tried = new Set(candidates);
|
|
13
41
|
for (const file of await memoryFiles()) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
typeof fields.tags === "string" ? fields.tags.split(",") : [];
|
|
17
|
-
if (!tags.includes(QUARANTINE_TAG)) return null;
|
|
18
|
-
const scanTags = new Set([
|
|
19
|
-
QUARANTINE_TAG,
|
|
20
|
-
...quarantineReasons({ tags }).map((id) => `quarantine:${id}`),
|
|
21
|
-
]);
|
|
22
|
-
const kept = tags.filter((tag) => !scanTags.has(tag));
|
|
23
|
-
if (kept.length) fields.tags = kept.join(",");
|
|
24
|
-
else delete fields.tags;
|
|
25
|
-
return formatLine(fields);
|
|
26
|
-
});
|
|
42
|
+
if (tried.has(file)) continue;
|
|
43
|
+
const result = await stampRow(file, ts, release);
|
|
27
44
|
if (result !== null) return result ? "released" : "not_quarantined";
|
|
28
45
|
}
|
|
29
46
|
return "not_found";
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// rejection — split from src/tombstones.ts (#168 domain restructure). Declaration bodies are unchanged.
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { contentHash } from "../../evidence/record.ts";
|
|
4
3
|
import { memoryRoot } from "../../core/config/paths.ts";
|
|
4
|
+
import { contentHash } from "../../evidence/record.ts";
|
|
5
5
|
import {
|
|
6
|
+
differsIn,
|
|
6
7
|
NEAR_FLOOR,
|
|
7
8
|
NEAR_MIN_SHARED,
|
|
8
|
-
type Tombstone,
|
|
9
|
-
differsIn,
|
|
10
9
|
sketchOf,
|
|
10
|
+
type Tombstone,
|
|
11
11
|
tombstone,
|
|
12
12
|
tombstones,
|
|
13
13
|
valueSigOf,
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
// Not snapshotted. snapshotFiles() walks memoryFiles(), which excludes every
|
|
43
43
|
// RESERVED name, so `memory_undo` cannot resurrect a tombstone any more than
|
|
44
44
|
// it can resurrect a blocklist entry — and conversely cannot erase one.
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
import { createHash } from "node:crypto";
|
|
47
|
+
import { readFile } from "node:fs/promises";
|
|
47
48
|
import { join } from "node:path";
|
|
48
|
-
import { contentHash } from "../../evidence/record.ts";
|
|
49
49
|
import { formatLine, parseLine } from "../../core/logfmt/format.ts";
|
|
50
50
|
import {
|
|
51
51
|
appendLog,
|
|
@@ -53,19 +53,20 @@ import {
|
|
|
53
53
|
writeAtomic,
|
|
54
54
|
} from "../../core/logfmt/publish.ts";
|
|
55
55
|
import { readLines } from "../../core/logfmt/read.ts";
|
|
56
|
-
import { withStoreLock, LOCK_WRITE } from "../lock.ts";
|
|
57
|
-
import { redactSecrets } from "../secrets.ts";
|
|
58
56
|
import {
|
|
59
|
-
STOPWORDS,
|
|
60
|
-
tokens,
|
|
61
57
|
numericTokens,
|
|
62
58
|
polaritySigns,
|
|
59
|
+
STOPWORDS,
|
|
60
|
+
tokens,
|
|
63
61
|
} from "../../core/tokenize.ts";
|
|
62
|
+
import { contentHash } from "../../evidence/record.ts";
|
|
63
|
+
import { LOCK_WRITE, withStoreLock } from "../lock.ts";
|
|
64
|
+
import { redactSecrets } from "../secrets.ts";
|
|
64
65
|
import {
|
|
65
|
-
type Rejection,
|
|
66
|
-
type ValueDifference,
|
|
67
66
|
findRejection,
|
|
67
|
+
type Rejection,
|
|
68
68
|
rejectedFile,
|
|
69
|
+
type ValueDifference,
|
|
69
70
|
} from "./rejection.ts";
|
|
70
71
|
|
|
71
72
|
/** How many lowest token-hashes the sketch keeps. */
|
package/src/store/undo.ts
CHANGED
|
@@ -29,8 +29,6 @@
|
|
|
29
29
|
import { lstat, readFile, realpath, stat } from "node:fs/promises";
|
|
30
30
|
import { basename, isAbsolute, join, relative, resolve } from "node:path";
|
|
31
31
|
import { deletionsFile, snapshotDir } from "../core/config/paths.ts";
|
|
32
|
-
import { contentHash } from "../evidence/record.ts";
|
|
33
|
-
import { isWithin } from "./containment.ts";
|
|
34
32
|
import { parseLine } from "../core/logfmt/format.ts";
|
|
35
33
|
import {
|
|
36
34
|
ensureDir,
|
|
@@ -38,14 +36,16 @@ import {
|
|
|
38
36
|
writeAtomic,
|
|
39
37
|
} from "../core/logfmt/publish.ts";
|
|
40
38
|
import { readLines } from "../core/logfmt/read.ts";
|
|
41
|
-
import {
|
|
39
|
+
import { contentHash } from "../evidence/record.ts";
|
|
40
|
+
import { isWithin } from "./containment.ts";
|
|
42
41
|
import { snapshotFiles } from "./files/snapshots.ts";
|
|
43
|
-
import {
|
|
42
|
+
import { LOCK_RESTORE, withStoreLock } from "./lock.ts";
|
|
44
43
|
import {
|
|
45
44
|
ADMIN_READER,
|
|
46
45
|
lineVisibleTo,
|
|
47
46
|
type Reader,
|
|
48
47
|
} from "./model/visibility.ts";
|
|
48
|
+
import { tombstones } from "./tombstones/tombstone.ts";
|
|
49
49
|
|
|
50
50
|
const SNAPSHOT_NAME = /^(\d+)-(\d{4}-\d{2}-\d{2}\.logfmt)$/;
|
|
51
51
|
|