opencode-self-improvement 0.22.2 → 0.23.1
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 -1
- package/package.json +1 -1
- package/src/capture/candidates.ts +14 -0
- package/src/capture/checked.ts +128 -0
- package/src/capture/policy.ts +52 -0
- package/src/capture/prompt.ts +39 -0
- package/src/capture/runtime.ts +120 -1
- package/src/capture/triggers.ts +14 -0
- package/src/core/tags.ts +15 -0
- package/src/gate/gate.ts +49 -0
- package/src/gate/value-conflict.ts +300 -0
- package/src/init/format.ts +45 -4
- package/src/init/retag.ts +186 -0
- package/src/init/run.ts +82 -64
- package/src/init/stamp.ts +306 -0
- package/src/inject/format.ts +9 -1
- package/src/store/model/predicates.ts +13 -1
- package/src/store/model/provenance.ts +79 -0
- package/src/tools/init.ts +1 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// Row-vs-row numeric conflict (#390): a correction must retire the row it
|
|
2
|
+
// corrects IN THE DATA, not only in its wording.
|
|
3
|
+
//
|
|
4
|
+
// R3 (`results-stale-x5/`, values 2150 → 1900) showed the store keeping BOTH
|
|
5
|
+
// values, the correction winning only because a language model happened to
|
|
6
|
+
// write "this supersedes the earlier 2150 bps figure" and retrieval happened
|
|
7
|
+
// to prefer it. R3c narrowed the real exposure: with a 30-day gap the newer
|
|
8
|
+
// row wins 10/10 either order; the failure is the same-instant tie (3/10) and
|
|
9
|
+
// any retrieval that surfaces the stale row alone. GATE-3 rivalry never fires
|
|
10
|
+
// here because it only competes `decision`/`preference` rows, and corrections
|
|
11
|
+
// of remembered facts are `context`/`learning`.
|
|
12
|
+
//
|
|
13
|
+
// This retires the older row at write time with an explicit `superseded_by`
|
|
14
|
+
// pointer, so injection cannot carry the stale value alone — superseded rows
|
|
15
|
+
// fail `isInjectable`. It changes no outcome that timestamp resolution had
|
|
16
|
+
// already settled (R3c: newer wins 10/10); it makes the win structural.
|
|
17
|
+
//
|
|
18
|
+
// The guard is threefold, because a retirement destroys nothing but hides the
|
|
19
|
+
// row from every future session:
|
|
20
|
+
// 1. anchor — both rows must name the SAME symbol token (SCREAMING_CASE or
|
|
21
|
+
// a dotted member). "max_retries is 3" and "timeout is 30" in one scope
|
|
22
|
+
// share no anchor and coexist.
|
|
23
|
+
// 2. value — the numbers attached to that anchor must actually disagree
|
|
24
|
+
// (`valuesAgree === false`); "21.5%" and "2150" are one fact restated.
|
|
25
|
+
// 3. sentence — with the numbers stripped, the two rows must say the SAME
|
|
26
|
+
// thing (#400 review round 2, WhollyMindless). Every non-numeric word
|
|
27
|
+
// outside a closed ignore list (prose glue, copulas) must appear in BOTH
|
|
28
|
+
// rows or in a closed EDITORIAL set — words that narrate the act of
|
|
29
|
+
// correcting ("corrected", "actually", "supersedes") and nothing about
|
|
30
|
+
// the fact. One unmatched content word and the rows coexist.
|
|
31
|
+
// This replaces the old jaccard floor (0.71 let "3 in production" retire
|
|
32
|
+
// "5 in staging") AND the environment dictionary (empty qualifier sets
|
|
33
|
+
// let "3 for tenant Alpha" retire "5 for tenant Beta" — a dictionary can
|
|
34
|
+
// never establish same-context, because the qualifier vocabulary is
|
|
35
|
+
// open: tenants, regions, editions, customers). Same-context is now
|
|
36
|
+
// ESTABLISHED, not looked up: a difference the words cannot explain is
|
|
37
|
+
// a difference in the fact. Retirement requires the correction to be
|
|
38
|
+
// the only thing that changed; anything coexisting is the pre-#390
|
|
39
|
+
// state — both rows inject, retrieval prefers the newer — never a
|
|
40
|
+
// hidden fact.
|
|
41
|
+
import { USER_GUIDED_TAG } from "../core/tags.ts";
|
|
42
|
+
import { STOPWORDS, tokens } from "../core/tokenize.ts";
|
|
43
|
+
import type { Memory } from "../store/model/memory.ts";
|
|
44
|
+
import { valuesAgree } from "../recall/conflicts.ts";
|
|
45
|
+
|
|
46
|
+
export interface ValueConflict {
|
|
47
|
+
/** The live row to retire. */
|
|
48
|
+
ts: string;
|
|
49
|
+
symbol: string;
|
|
50
|
+
/** The number the retiring row asserted. */
|
|
51
|
+
from: string;
|
|
52
|
+
/** The number the incoming row asserts. */
|
|
53
|
+
to: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// How far after a symbol a number may sit and still be that symbol's value.
|
|
57
|
+
// Same window as the capture claim check (#393) on purpose: one rule for
|
|
58
|
+
// "this number belongs to this name".
|
|
59
|
+
const WINDOW = 48;
|
|
60
|
+
|
|
61
|
+
// Words carrying no qualifying information: prose glue (the shared GATE-3
|
|
62
|
+
// stopword set) plus copulas/auxiliaries, so "was 3" -> "is now 5" stays one
|
|
63
|
+
// correction. "should" stays OUT: desired-vs-actual is a real distinction.
|
|
64
|
+
const IGNORE: ReadonlySet<string> = new Set([
|
|
65
|
+
...STOPWORDS,
|
|
66
|
+
"was",
|
|
67
|
+
"were",
|
|
68
|
+
"be",
|
|
69
|
+
"been",
|
|
70
|
+
"being",
|
|
71
|
+
"am",
|
|
72
|
+
"do",
|
|
73
|
+
"does",
|
|
74
|
+
"did",
|
|
75
|
+
"has",
|
|
76
|
+
"have",
|
|
77
|
+
"had",
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
// Words that narrate the ACT of correcting without qualifying the fact.
|
|
81
|
+
// Closed and small on purpose: every word here says "this replaced something"
|
|
82
|
+
// and nothing about WHICH fact. A qualifier can never hide in this set
|
|
83
|
+
// because the set names no dimension of the world.
|
|
84
|
+
const EDITORIAL: ReadonlySet<string> = new Set([
|
|
85
|
+
"corrected",
|
|
86
|
+
"correction",
|
|
87
|
+
"actually",
|
|
88
|
+
"now",
|
|
89
|
+
"updated",
|
|
90
|
+
"update",
|
|
91
|
+
"previously",
|
|
92
|
+
"earlier",
|
|
93
|
+
"supersedes",
|
|
94
|
+
"superseded",
|
|
95
|
+
"fixed",
|
|
96
|
+
"typo",
|
|
97
|
+
"wrong",
|
|
98
|
+
"incorrectly",
|
|
99
|
+
"mistake",
|
|
100
|
+
"rather",
|
|
101
|
+
"instead",
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The qualifying content of a row with ONE occurrence removed: the number
|
|
106
|
+
* the given symbol asserts, excised by POSITION before tokenizing. Numbers
|
|
107
|
+
* otherwise STAY (#400 review rounds 3 and 4, WhollyMindless): "tenant 101"
|
|
108
|
+
* and "tenant 202" are distinguished only by digits, and a literal skip of
|
|
109
|
+
* the compared value also skipped a qualifier carrying the same literal —
|
|
110
|
+
* "is 3 for tenant 3" lost its tenant when the 3 was skipped globally.
|
|
111
|
+
* Position is the only distinction between "the value being corrected" and
|
|
112
|
+
* "a qualifier that happens to repeat it": the asserted number is the first
|
|
113
|
+
* within WINDOW after the symbol (falling back to before), exactly what
|
|
114
|
+
* symbolValues reads.
|
|
115
|
+
*/
|
|
116
|
+
function residualTokens(
|
|
117
|
+
content: string,
|
|
118
|
+
excise: readonly [number, number] | undefined,
|
|
119
|
+
): { set: Set<string>; list: string[]; full: string[] } {
|
|
120
|
+
const cut = excise
|
|
121
|
+
? `${content.slice(0, excise[0])} ${content.slice(excise[1])}`
|
|
122
|
+
: content;
|
|
123
|
+
const full: string[] = [];
|
|
124
|
+
const list: string[] = [];
|
|
125
|
+
for (const t of tokens(cut)) {
|
|
126
|
+
const bare = t.replace(/^\.+|\.+$/g, "");
|
|
127
|
+
if (!bare) continue;
|
|
128
|
+
full.push(bare);
|
|
129
|
+
if (!IGNORE.has(bare)) list.push(bare);
|
|
130
|
+
}
|
|
131
|
+
return { set: new Set(list), list, full };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Guard 3: the correction must be the ONLY thing that changed. Every word
|
|
136
|
+
* one row says and the other does not must be editorial narration; a single
|
|
137
|
+
* content word unique to one row ("production", "tenant", "alpha", "v2",
|
|
138
|
+
* "101") means the rows qualify different facts, and nothing retires.
|
|
139
|
+
*
|
|
140
|
+
* A unique NUMBER is additionally excused when it reads as a citation of
|
|
141
|
+
* the other row's value: it equals the other side's asserted value (sign
|
|
142
|
+
* dropped — tokens() strips signs, so a sign flip retires through the value
|
|
143
|
+
* guard, never here) AND its preceding word is not a content word. "was
|
|
144
|
+
* 2150" cites; "tenant 5" qualifies. That keeps R3's correction shape
|
|
145
|
+
* ("...is 1900 basis points, was 2150." retires 2150) while a swapped pair
|
|
146
|
+
* like "is 3 for tenant 5" / "is 5 for tenant 3" — where each extra number
|
|
147
|
+
* is a qualifier, not a citation — coexists.
|
|
148
|
+
*/
|
|
149
|
+
function sameSentenceExceptValue(
|
|
150
|
+
a: { set: Set<string>; list: string[]; full: string[] },
|
|
151
|
+
b: { set: Set<string>; list: string[]; full: string[] },
|
|
152
|
+
aAsserted: string,
|
|
153
|
+
bAsserted: string,
|
|
154
|
+
): boolean {
|
|
155
|
+
const numeric = (t: string) => /^\d+(?:\.\d+)?$/.test(t);
|
|
156
|
+
const cited = (
|
|
157
|
+
side: { set: Set<string>; list: string[]; full: string[] },
|
|
158
|
+
t: string,
|
|
159
|
+
otherAsserted: string,
|
|
160
|
+
) => {
|
|
161
|
+
if (!numeric(t) || t !== otherAsserted.replace(/^[-+]/, "")) return false;
|
|
162
|
+
// Adjacency is read on the RAW token stream: "was 2150" cites even
|
|
163
|
+
// though "was" is prose glue, and "tenant 5" qualifies — the filter
|
|
164
|
+
// that serves the comparison would erase exactly this distinction.
|
|
165
|
+
const i = side.full.indexOf(t);
|
|
166
|
+
if (i === 0) return true;
|
|
167
|
+
const prev = side.full[i - 1] ?? "";
|
|
168
|
+
return IGNORE.has(prev) || EDITORIAL.has(prev);
|
|
169
|
+
};
|
|
170
|
+
for (const t of a.set)
|
|
171
|
+
if (!b.set.has(t) && !EDITORIAL.has(t) && !cited(a, t, bAsserted))
|
|
172
|
+
return false;
|
|
173
|
+
for (const t of b.set)
|
|
174
|
+
if (!a.set.has(t) && !EDITORIAL.has(t) && !cited(b, t, aAsserted))
|
|
175
|
+
return false;
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Escape a symbol for literal use in a RegExp. */
|
|
180
|
+
const reLiteral = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Where the symbol asserts its value: the span of the first number within
|
|
184
|
+
* WINDOW after the symbol (falling back to before), mirroring symbolValues
|
|
185
|
+
* exactly — same search, first symbol match that yields a number.
|
|
186
|
+
*/
|
|
187
|
+
function assertedValueSpan(
|
|
188
|
+
content: string,
|
|
189
|
+
sym: string,
|
|
190
|
+
): [number, number] | undefined {
|
|
191
|
+
const SYMBOL = new RegExp(`\\b${reLiteral(sym)}\\b`, "g");
|
|
192
|
+
let match: RegExpExecArray | null = SYMBOL.exec(content);
|
|
193
|
+
while (match) {
|
|
194
|
+
const afterStart = match.index + match[0].length;
|
|
195
|
+
const after = content.slice(afterStart, afterStart + WINDOW);
|
|
196
|
+
const numAfter = after.match(/[-+]?\d+(?:\.\d+)?/);
|
|
197
|
+
if (numAfter) {
|
|
198
|
+
const at = afterStart + (numAfter.index ?? 0);
|
|
199
|
+
return [at, at + numAfter[0].length];
|
|
200
|
+
}
|
|
201
|
+
const beforeStart = Math.max(0, match.index - WINDOW);
|
|
202
|
+
const before = content.slice(beforeStart, match.index);
|
|
203
|
+
const numBefore = before.match(/[-+]?\d+(?:\.\d+)?/);
|
|
204
|
+
if (numBefore) {
|
|
205
|
+
const at = beforeStart + (numBefore.index ?? 0);
|
|
206
|
+
return [at, at + numBefore[0].length];
|
|
207
|
+
}
|
|
208
|
+
match = SYMBOL.exec(content);
|
|
209
|
+
}
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The value each symbol in a row asserts: the first number within WINDOW
|
|
215
|
+
* after the symbol (falling back to the first before it). Built per call —
|
|
216
|
+
* a module-level `g` regex carries `lastIndex` between calls, which made the
|
|
217
|
+
* #393 check depend on which row was checked last.
|
|
218
|
+
*
|
|
219
|
+
* The sign is part of the value: "-5" and "5" disagree (#399's class — an
|
|
220
|
+
* unsigned extractor grants agreement across a sign flip).
|
|
221
|
+
*/
|
|
222
|
+
export function symbolValues(content: string): Map<string, string> {
|
|
223
|
+
const out = new Map<string, string>();
|
|
224
|
+
const SYMBOL =
|
|
225
|
+
/\b([A-Z][A-Z0-9_]{2,}|[A-Za-z_$][A-Za-z0-9_$]*\.[a-z][A-Za-z0-9_$]*)\b/g;
|
|
226
|
+
let match: RegExpExecArray | null = SYMBOL.exec(content);
|
|
227
|
+
while (match) {
|
|
228
|
+
const sym = match[1] ?? "";
|
|
229
|
+
const after = content.slice(
|
|
230
|
+
match.index + match[0].length,
|
|
231
|
+
match.index + match[0].length + WINDOW,
|
|
232
|
+
);
|
|
233
|
+
const before = content.slice(
|
|
234
|
+
Math.max(0, match.index - WINDOW),
|
|
235
|
+
match.index,
|
|
236
|
+
);
|
|
237
|
+
const num =
|
|
238
|
+
after.match(/[-+]?\d+(?:\.\d+)?/)?.[0] ??
|
|
239
|
+
before.match(/[-+]?\d+(?:\.\d+)?/)?.[0];
|
|
240
|
+
if (num !== undefined && !out.has(sym)) out.set(sym, num);
|
|
241
|
+
match = SYMBOL.exec(content);
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Which live rows the incoming candidate corrects. The caller passes rows
|
|
248
|
+
* already filtered to the candidate's scope, type-compared here.
|
|
249
|
+
*
|
|
250
|
+
* Guided rows are protected from inferred writes: a captured inference must
|
|
251
|
+
* not bury what the user stated — the user is the stronger claim. A guided
|
|
252
|
+
* candidate may retire anything, including a stale guided row.
|
|
253
|
+
*/
|
|
254
|
+
export function valueConflicts(
|
|
255
|
+
candidate: { type: string; content: string; tags?: string[] },
|
|
256
|
+
live: readonly Memory[],
|
|
257
|
+
): ValueConflict[] {
|
|
258
|
+
const guided = candidate.tags?.includes(USER_GUIDED_TAG) === true;
|
|
259
|
+
const mine = symbolValues(candidate.content);
|
|
260
|
+
if (mine.size === 0) return [];
|
|
261
|
+
const out: ValueConflict[] = [];
|
|
262
|
+
for (const m of live) {
|
|
263
|
+
if (m.superseded_by) continue;
|
|
264
|
+
if (m.type !== candidate.type) continue;
|
|
265
|
+
if (!guided && m.tags?.includes(USER_GUIDED_TAG)) continue;
|
|
266
|
+
const theirs = symbolValues(m.content);
|
|
267
|
+
if (theirs.size === 0) continue;
|
|
268
|
+
let hit: { symbol: string; from: string } | undefined;
|
|
269
|
+
for (const [sym, val] of theirs) {
|
|
270
|
+
const said = mine.get(sym);
|
|
271
|
+
if (said === undefined) continue;
|
|
272
|
+
if (valuesAgree(said, val) === false) {
|
|
273
|
+
hit = { symbol: sym, from: val };
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!hit) continue;
|
|
278
|
+
// Excise each side's asserted occurrence of the hit symbol's value,
|
|
279
|
+
// then require the rest of the two sentences to match.
|
|
280
|
+
if (
|
|
281
|
+
!sameSentenceExceptValue(
|
|
282
|
+
residualTokens(
|
|
283
|
+
candidate.content,
|
|
284
|
+
assertedValueSpan(candidate.content, hit.symbol),
|
|
285
|
+
),
|
|
286
|
+
residualTokens(m.content, assertedValueSpan(m.content, hit.symbol)),
|
|
287
|
+
mine.get(hit.symbol) ?? "",
|
|
288
|
+
hit.from,
|
|
289
|
+
)
|
|
290
|
+
)
|
|
291
|
+
continue;
|
|
292
|
+
out.push({
|
|
293
|
+
ts: m.ts,
|
|
294
|
+
symbol: hit.symbol,
|
|
295
|
+
from: hit.from,
|
|
296
|
+
to: mine.get(hit.symbol) ?? "",
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
package/src/init/format.ts
CHANGED
|
@@ -4,15 +4,56 @@
|
|
|
4
4
|
import { type InitSummary } from "./run.ts";
|
|
5
5
|
|
|
6
6
|
export function formatInit(s: InitSummary): string {
|
|
7
|
+
// #376 review: a migration whose file write failed is not a migration. The row
|
|
8
|
+
// was honoured, so this run is still idempotent, but nothing was copied — the
|
|
9
|
+
// reply says so instead of claiming a migration that left no file behind.
|
|
10
|
+
const migrated = s.stampMigrated
|
|
11
|
+
? s.stampError
|
|
12
|
+
? " (stamp read from a store row; the copy to the stamp file failed)"
|
|
13
|
+
: " (stamp migrated from a store row)"
|
|
14
|
+
: "";
|
|
15
|
+
// A retirement that did not land is named, in the same spirit as the re-tag
|
|
16
|
+
// repair's partial count: the migration is durable, the row is still there,
|
|
17
|
+
// and the operator reads which half failed (#376 review).
|
|
18
|
+
const retireFault = s.stampRetireError
|
|
19
|
+
? `Legacy stamp row NOT retired (${s.stampRetireError}); it stays in the store, withheld from recall. Run init again.`
|
|
20
|
+
: "";
|
|
21
|
+
// #375: a repair that did something, or could not finish, is reported. A quiet
|
|
22
|
+
// "Nothing to do." over a store that still holds a live poisoned row is the
|
|
23
|
+
// failure this ticket exists to remove.
|
|
24
|
+
const r = s.retag;
|
|
25
|
+
const repair =
|
|
26
|
+
r && (r.retagged || r.blocked || r.partial || r.remaining || r.unaudited)
|
|
27
|
+
? `\nInjection screen: re-tagged=${r.retagged}, blocked=${r.blocked}, partial=${r.partial}` +
|
|
28
|
+
(r.unaudited ? `, ${r.unaudited} without an audit line` : "") +
|
|
29
|
+
(r.remaining ? `, ${r.remaining} left (run init again)` : "") +
|
|
30
|
+
(r.collateral.length
|
|
31
|
+
? `, ${r.collateral.length} retired by GATE-2 while re-tagging`
|
|
32
|
+
: "")
|
|
33
|
+
: "";
|
|
7
34
|
if (s.skipped) {
|
|
8
|
-
|
|
35
|
+
// The skip path can carry a stamp fault of its own: the migration honoured a
|
|
36
|
+
// legacy row and could not write the file, so the next init re-migrates.
|
|
37
|
+
// Silence here is what let a failed migration report success (#376 review).
|
|
38
|
+
const fault = s.stampError
|
|
39
|
+
? `; stamp NOT recorded (${s.stampError}) — the migration will be retried`
|
|
40
|
+
: "";
|
|
41
|
+
return `Already initialized for ${s.scope} at head ${s.head} (stamp ${s.stampTs}). Nothing to do.${migrated}${fault}${retireFault ? `\n${retireFault}` : ""}${repair}`;
|
|
9
42
|
}
|
|
10
|
-
const scr = s.scripts.map((
|
|
43
|
+
const scr = s.scripts.map((r2) => `- ${r2.source}: "${r2.cmd}"`).join("\n");
|
|
44
|
+
const stamp = s.stampError
|
|
45
|
+
? `; stamp NOT recorded (${s.stampError}) — init will run again`
|
|
46
|
+
: `; stamp=${s.stampTs ?? "-"}`;
|
|
11
47
|
return [
|
|
12
48
|
`Initialized ${s.scope} (root ${s.root}, head ${s.head}).`,
|
|
13
49
|
scr ? `Scripts (${s.scripts.length}):\n${scr}` : "Scripts: none detected",
|
|
14
50
|
`Memories: written=${s.written}, collapsed=${s.collapsed}, blocked=${s.blocked}` +
|
|
15
51
|
(s.unaudited ? `, ${s.unaudited} without an audit line` : "") +
|
|
16
|
-
|
|
17
|
-
|
|
52
|
+
stamp +
|
|
53
|
+
migrated,
|
|
54
|
+
retireFault,
|
|
55
|
+
repair.trimStart(),
|
|
56
|
+
]
|
|
57
|
+
.filter((line) => line !== "")
|
|
58
|
+
.join("\n");
|
|
18
59
|
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// One-time repair for rows stored before #372 screened init's writes (#375).
|
|
2
|
+
//
|
|
3
|
+
// #372 made `/repo-init` run the injection screen over the rows it writes, so a
|
|
4
|
+
// checkout sitting in a directory named `eval (x)` now gets its command memories
|
|
5
|
+
// tagged `quarantine`. That covers writes made after it shipped. A store that
|
|
6
|
+
// already holds an untagged poisoned row keeps it, and re-running init cannot
|
|
7
|
+
// fix it: GATE-3's exact-duplicate check compares type and content, so the new
|
|
8
|
+
// quarantined candidate collapses into the old untagged row. `force: true` does
|
|
9
|
+
// not help either — force skips the stamp check, not the dedup.
|
|
10
|
+
//
|
|
11
|
+
// So init scans what it can read and re-tags the hits. Each re-tag is a normal
|
|
12
|
+
// gated write that replaces the row it names (`supersedesTs`) — the one shape
|
|
13
|
+
// GATE-3 reads as a metadata edit rather than a duplicate. Nothing is deleted:
|
|
14
|
+
// the old row survives superseded, and every replacement emits its own
|
|
15
|
+
// `write_promoted` audit line.
|
|
16
|
+
import { runWriteGate, type GateContext } from "../gate/gate.ts";
|
|
17
|
+
import { markSuperseded } from "../store/files/supersede.ts";
|
|
18
|
+
import { loadAll } from "../store/files/load.ts";
|
|
19
|
+
import type { Memory } from "../store/model/memory.ts";
|
|
20
|
+
import { isActive } from "../store/model/predicates.ts";
|
|
21
|
+
import { isVisibleTo } from "../store/model/visibility.ts";
|
|
22
|
+
import {
|
|
23
|
+
isQuarantined,
|
|
24
|
+
quarantineCandidate,
|
|
25
|
+
} from "../store/quarantine/quarantine.ts";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Writes one init run may make. A pathological store (an injection-shaped
|
|
29
|
+
* directory name plus a thousand rows quoting it) must not turn one `/repo-init`
|
|
30
|
+
* into a thousand full-file publishes. The remainder is reported, never hidden.
|
|
31
|
+
*/
|
|
32
|
+
export const MAX_RETAGS_PER_RUN = 100;
|
|
33
|
+
|
|
34
|
+
export interface RetagResult {
|
|
35
|
+
/** Active rows the caller could read. */
|
|
36
|
+
scanned: number;
|
|
37
|
+
/** Rows replaced by a tagged twin AND retired. */
|
|
38
|
+
retagged: number;
|
|
39
|
+
/** Hits the gate refused; they stay active and untagged. */
|
|
40
|
+
blocked: number;
|
|
41
|
+
/** Promoted, but the retirement did not land (another supersession won). */
|
|
42
|
+
partial: number;
|
|
43
|
+
/** Promoted with a fallen `write_promoted` audit line (#155 class). */
|
|
44
|
+
unaudited: number;
|
|
45
|
+
/** Hits left unprocessed because the per-run bound was reached. */
|
|
46
|
+
remaining: number;
|
|
47
|
+
/** Rows GATE-2 retired as a side effect of these writes. Never hidden. */
|
|
48
|
+
collateral: string[];
|
|
49
|
+
/**
|
|
50
|
+
* Retirement attempts that REJECTED (lock contention) rather than returning
|
|
51
|
+
* false (#378 review). The replacement is durable; the original is not.
|
|
52
|
+
* Counted in `partial`, named here.
|
|
53
|
+
*/
|
|
54
|
+
failures: string[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function emptyRetag(): RetagResult {
|
|
58
|
+
return {
|
|
59
|
+
scanned: 0,
|
|
60
|
+
retagged: 0,
|
|
61
|
+
blocked: 0,
|
|
62
|
+
partial: 0,
|
|
63
|
+
unaudited: 0,
|
|
64
|
+
remaining: 0,
|
|
65
|
+
collateral: [],
|
|
66
|
+
failures: [],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The row as it should be written again: identical prose and metadata, plus the
|
|
72
|
+
* tags the screen asked for.
|
|
73
|
+
*
|
|
74
|
+
* Every field is copied on purpose. A repair that quietly dropped the verbatim
|
|
75
|
+
* quote (`original`) or the contradiction fields would close one boundary and
|
|
76
|
+
* break another, and `repo` is carried so a global row stays global while a
|
|
77
|
+
* repository row stays in its own repository — an edit must never move a row's
|
|
78
|
+
* visibility. The row's own `ts`, `file` and `superseded_by` are deliberately
|
|
79
|
+
* NOT carried: the gate mints a fresh identity for the replacement.
|
|
80
|
+
*/
|
|
81
|
+
function reTagged(row: Memory, tags: string[]) {
|
|
82
|
+
return {
|
|
83
|
+
type: row.type,
|
|
84
|
+
scope: row.scope,
|
|
85
|
+
content: row.content,
|
|
86
|
+
...(row.issue ? { issue: row.issue } : {}),
|
|
87
|
+
...(row.kw ? { kw: row.kw } : {}),
|
|
88
|
+
...(row.ref ? { ref: row.ref } : {}),
|
|
89
|
+
...(row.claim ? { claim: row.claim } : {}),
|
|
90
|
+
...(row.klass ? { klass: row.klass } : {}),
|
|
91
|
+
...(row.original ? { original: row.original } : {}),
|
|
92
|
+
status: row.status ?? "settled",
|
|
93
|
+
repo: row.repo ?? "global",
|
|
94
|
+
repoPath: row.repo ? row.repoPath : undefined,
|
|
95
|
+
tags,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Re-tag rows that match the injection screen but do not carry the tag.
|
|
101
|
+
*
|
|
102
|
+
* Reach is exactly what the caller can read — this repository's rows plus global
|
|
103
|
+
* rows. Another repository's rows are not this caller's to rewrite.
|
|
104
|
+
*/
|
|
105
|
+
export async function retagQuarantinedRows(
|
|
106
|
+
ctx: GateContext,
|
|
107
|
+
): Promise<RetagResult> {
|
|
108
|
+
const out = emptyRetag();
|
|
109
|
+
const rows = (await loadAll()).filter(
|
|
110
|
+
(m) =>
|
|
111
|
+
isActive(m) &&
|
|
112
|
+
!isQuarantined(m) &&
|
|
113
|
+
(ctx.reader ? isVisibleTo(m, ctx.reader) : true),
|
|
114
|
+
);
|
|
115
|
+
out.scanned = rows.length;
|
|
116
|
+
const hits: Memory[] = [];
|
|
117
|
+
for (const row of rows) {
|
|
118
|
+
// quarantineCandidate returns the same object when nothing matches.
|
|
119
|
+
if (quarantineCandidate(row) !== row) hits.push(row);
|
|
120
|
+
}
|
|
121
|
+
if (hits.length > MAX_RETAGS_PER_RUN)
|
|
122
|
+
out.remaining = hits.length - MAX_RETAGS_PER_RUN;
|
|
123
|
+
for (const row of hits.slice(0, MAX_RETAGS_PER_RUN)) {
|
|
124
|
+
const tagged = quarantineCandidate(row);
|
|
125
|
+
const durable: string[] = [];
|
|
126
|
+
const outcome = await runWriteGate(reTagged(row, tagged.tags ?? []), {
|
|
127
|
+
...ctx,
|
|
128
|
+
supersedesTs: row.ts,
|
|
129
|
+
durable,
|
|
130
|
+
});
|
|
131
|
+
if (outcome.action !== "promoted" && outcome.action !== "collapsed") {
|
|
132
|
+
// Blocked: the untagged row stays exactly as it was, and the operator
|
|
133
|
+
// has to hear that rather than read a clean repair.
|
|
134
|
+
out.blocked++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
// #378 review (WhollyMindless): two ways the unsafe original survived.
|
|
138
|
+
// (a) COLLAPSED is not "nothing happened" here — it means a quarantined
|
|
139
|
+
// twin from an earlier interrupted repair is already durable, and the
|
|
140
|
+
// only missing step is retiring the original. Refusing to act on the
|
|
141
|
+
// collapse left the row unquarantined forever (retagged=0, blocked=1
|
|
142
|
+
// on every retry). The collapse survivor IS the twin: the re-tagged
|
|
143
|
+
// candidate differs from the original only by tags, and the
|
|
144
|
+
// supersedesTs carve-out keeps the original from matching as its own
|
|
145
|
+
// duplicate, so an exact-content match is the prior replacement.
|
|
146
|
+
// (b) markSuperseded can REJECT (lock contention), not just return false.
|
|
147
|
+
// The twin is durable either way, so the failure lands in `partial`
|
|
148
|
+
// with its reason instead of escaping as if nothing was written —
|
|
149
|
+
// the #155 lesson, applied to the repair path.
|
|
150
|
+
const successor =
|
|
151
|
+
outcome.action === "collapsed" ? outcome.collapsedWith : outcome.ts;
|
|
152
|
+
if (successor === undefined) {
|
|
153
|
+
out.blocked++;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
let retired = false;
|
|
157
|
+
try {
|
|
158
|
+
retired = await markSuperseded(
|
|
159
|
+
row.ts,
|
|
160
|
+
{
|
|
161
|
+
superseded_by: successor,
|
|
162
|
+
reason: "init",
|
|
163
|
+
symbol: row.ts,
|
|
164
|
+
from: row.ts,
|
|
165
|
+
to: successor,
|
|
166
|
+
},
|
|
167
|
+
durable,
|
|
168
|
+
);
|
|
169
|
+
} catch (err) {
|
|
170
|
+
out.failures.push(
|
|
171
|
+
`${row.ts}: ${err instanceof Error && err.message ? err.message : String(err)}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
for (const ts of durable)
|
|
175
|
+
if (!out.collateral.includes(ts)) out.collateral.push(ts);
|
|
176
|
+
if (!retired) {
|
|
177
|
+
// The replacement IS durable. Name which half did not land, in the shape
|
|
178
|
+
// memory_update uses (#155), instead of counting a clean re-tag.
|
|
179
|
+
out.partial++;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
out.retagged++;
|
|
183
|
+
if (outcome.evidenceError !== undefined) out.unaudited++;
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|