opencode-self-improvement 0.15.18 → 0.15.20
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 +244 -94
- 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/gate/gate.ts
CHANGED
|
@@ -2,21 +2,34 @@
|
|
|
2
2
|
// canonical-graph correctness, GATE-2 stale-ref scan + supersession, GATE-3
|
|
3
3
|
// dedup, GATE-4 promotion/block. Every step emits an ordered evidence line.
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
isQuarantined,
|
|
7
|
+
quarantineReasons,
|
|
8
|
+
} from "../store/quarantine/quarantine.ts";
|
|
6
9
|
import { canonicalFile, memoryRoot } from "../core/config/paths.ts";
|
|
7
|
-
import { bracketList, parseLine } from "../core/logfmt/format.ts"
|
|
10
|
+
import { bracketList, parseLine } from "../core/logfmt/format.ts";
|
|
8
11
|
import { readLines } from "../core/logfmt/read.ts";
|
|
9
|
-
import { appendMemory } from "../store/files/append.ts"
|
|
10
|
-
import { loadScope } from "../store/files/load.ts"
|
|
11
|
-
import { snapshotFiles } from "../store/files/snapshots.ts"
|
|
12
|
-
import {
|
|
12
|
+
import { appendMemory } from "../store/files/append.ts";
|
|
13
|
+
import { loadScope } from "../store/files/load.ts";
|
|
14
|
+
import { snapshotFiles } from "../store/files/snapshots.ts";
|
|
15
|
+
import {
|
|
16
|
+
markManySupersededReport,
|
|
17
|
+
type SupersedeFields,
|
|
18
|
+
} from "../store/files/supersede.ts";
|
|
13
19
|
import { MAX_CONTENT_CHARS, type Memory } from "../store/model/memory.ts";
|
|
14
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
captureEvidenceContext,
|
|
22
|
+
type EvidenceContext,
|
|
23
|
+
chainId,
|
|
24
|
+
contentHash,
|
|
25
|
+
record,
|
|
26
|
+
recordWriteBlocked,
|
|
27
|
+
} from "../evidence/record.ts";
|
|
15
28
|
import { getCurrent, noteAuthored } from "../core/state.ts";
|
|
16
29
|
import { MAX_WRITE_EVIDENCE_REFS } from "../capture/triggers.ts";
|
|
17
30
|
import { flattenMemoryText } from "../store/sanitize.ts";
|
|
18
31
|
import { containsSecret, redactSecrets } from "../store/secrets.ts";
|
|
19
|
-
import { UnresolvedWriterError } from "../store/files/append.ts"
|
|
32
|
+
import { UnresolvedWriterError } from "../store/files/append.ts";
|
|
20
33
|
import { readerFromSid, type Reader } from "../store/model/visibility.ts";
|
|
21
34
|
import { writeTarget, type WriteTarget } from "../identity/repokey.ts";
|
|
22
35
|
import {
|
|
@@ -58,7 +71,8 @@ export interface WriteCandidate {
|
|
|
58
71
|
// collapse an edit whose ordering the caller changed deliberately, and the
|
|
59
72
|
// conservative direction for a truth-claim is the one that writes more.
|
|
60
73
|
export const sameStrList = (a?: string[], c?: string[]) =>
|
|
61
|
-
(a?.length ?? 0) === (c?.length ?? 0) &&
|
|
74
|
+
(a?.length ?? 0) === (c?.length ?? 0) &&
|
|
75
|
+
(a ?? []).every((x, i) => (c ?? [])[i] === x);
|
|
62
76
|
|
|
63
77
|
// The sentence every success reply owes the reader when the gate promoted a
|
|
64
78
|
// row but its write_promoted audit line failed to land (#155 review): the
|
|
@@ -148,10 +162,34 @@ const CONFLICT_OVERLAP_FLOOR = 0.3;
|
|
|
148
162
|
// Planning-flavored content (frontier/thread/proposal language). Used only to
|
|
149
163
|
// turn GATE-4's ambiguous block into a hint pointing at the open-thread home.
|
|
150
164
|
const PLANNING_WORDS = new Set([
|
|
151
|
-
"plan",
|
|
152
|
-
"
|
|
153
|
-
"
|
|
154
|
-
"
|
|
165
|
+
"plan",
|
|
166
|
+
"plans",
|
|
167
|
+
"planned",
|
|
168
|
+
"planning",
|
|
169
|
+
"frontier",
|
|
170
|
+
"propos",
|
|
171
|
+
"proposal",
|
|
172
|
+
"open",
|
|
173
|
+
"goal",
|
|
174
|
+
"goals",
|
|
175
|
+
"decide",
|
|
176
|
+
"decides",
|
|
177
|
+
"decided",
|
|
178
|
+
"deciding",
|
|
179
|
+
"reject",
|
|
180
|
+
"rejects",
|
|
181
|
+
"rejected",
|
|
182
|
+
"thread",
|
|
183
|
+
"should",
|
|
184
|
+
"option",
|
|
185
|
+
"options",
|
|
186
|
+
"candidate",
|
|
187
|
+
"candidates",
|
|
188
|
+
"next",
|
|
189
|
+
"roadmap",
|
|
190
|
+
"scale",
|
|
191
|
+
"overhead",
|
|
192
|
+
"bloat",
|
|
155
193
|
]);
|
|
156
194
|
|
|
157
195
|
function looksPlanning(candidate: WriteCandidate): boolean {
|
|
@@ -199,7 +237,8 @@ function canonicalMatch(
|
|
|
199
237
|
if (!aligns) {
|
|
200
238
|
const entitySet = new Set(et);
|
|
201
239
|
const asserted = rawTokens(content).filter(
|
|
202
|
-
(t) =>
|
|
240
|
+
(t) =>
|
|
241
|
+
!STOPWORDS.has(t.toLowerCase()) && !entitySet.has(t.toLowerCase()),
|
|
203
242
|
);
|
|
204
243
|
conflictingValue = asserted[0];
|
|
205
244
|
}
|
|
@@ -215,9 +254,18 @@ interface StaleRef {
|
|
|
215
254
|
to: string;
|
|
216
255
|
}
|
|
217
256
|
|
|
218
|
-
async function staleRefFor(
|
|
257
|
+
async function staleRefFor(
|
|
258
|
+
m: Memory,
|
|
259
|
+
liveRoot: string,
|
|
260
|
+
tree: LazyTree,
|
|
261
|
+
): Promise<StaleRef | null> {
|
|
219
262
|
for (const form of extractCallForms(m.content)) {
|
|
220
|
-
const live = await findLiveCall(
|
|
263
|
+
const live = await findLiveCall(
|
|
264
|
+
form.name,
|
|
265
|
+
form.fileHint,
|
|
266
|
+
liveRoot,
|
|
267
|
+
await tree(),
|
|
268
|
+
);
|
|
221
269
|
if (!live) continue;
|
|
222
270
|
if (!sameArgs(form.argsText, live.argsText)) {
|
|
223
271
|
return {
|
|
@@ -231,7 +279,10 @@ async function staleRefFor(m: Memory, liveRoot: string, tree: LazyTree): Promise
|
|
|
231
279
|
return null;
|
|
232
280
|
}
|
|
233
281
|
|
|
234
|
-
function deltaTokens(
|
|
282
|
+
function deltaTokens(
|
|
283
|
+
candidate: WriteCandidate,
|
|
284
|
+
drift: DriftInfo | null,
|
|
285
|
+
): string[] {
|
|
235
286
|
const set = new Set<string>();
|
|
236
287
|
for (const form of extractCallForms(candidate.content)) {
|
|
237
288
|
if (form.fileHint) set.add(form.fileHint);
|
|
@@ -249,23 +300,54 @@ export async function runWriteGate(
|
|
|
249
300
|
): Promise<GateOutcome> {
|
|
250
301
|
const reader = ctx.reader ?? readerFromSid(ctx.sessionID);
|
|
251
302
|
const writeAt = ctx.writeAt ?? writeTarget(ctx.sessionID ?? getCurrent().sid);
|
|
252
|
-
const evidenceContext =
|
|
303
|
+
const evidenceContext =
|
|
304
|
+
ctx.evidenceContext ?? captureEvidenceContext(ctx.sessionID);
|
|
253
305
|
const chain = chainId();
|
|
254
306
|
// Keep all gate audit lines with entry-time attribution, including refusals.
|
|
255
|
-
const rec = (
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
307
|
+
const rec = (
|
|
308
|
+
ev: string,
|
|
309
|
+
fields: Record<string, unknown> = {},
|
|
310
|
+
includeContext = true,
|
|
311
|
+
) => record(ev, fields, includeContext, ctx.sessionID, evidenceContext);
|
|
312
|
+
const recBlocked = (
|
|
313
|
+
gate: number,
|
|
314
|
+
reason: string,
|
|
315
|
+
flags: string[],
|
|
316
|
+
chain?: string,
|
|
317
|
+
) =>
|
|
318
|
+
recordWriteBlocked(
|
|
319
|
+
gate,
|
|
320
|
+
reason,
|
|
321
|
+
flags,
|
|
322
|
+
chain,
|
|
323
|
+
ctx.sessionID,
|
|
324
|
+
evidenceContext,
|
|
325
|
+
);
|
|
259
326
|
|
|
260
327
|
// Size/type floor BEFORE any scanning: gate analysis regexes
|
|
261
328
|
// over megabyte content backtrack super-linearly and freeze the service
|
|
262
329
|
// event loop (100KB write = 6s, 2MB = never returned).
|
|
263
330
|
if (typeof candidate.content !== "string") {
|
|
264
|
-
await recBlocked(
|
|
265
|
-
|
|
331
|
+
await recBlocked(
|
|
332
|
+
4,
|
|
333
|
+
"invalid_content",
|
|
334
|
+
[`content_type=${typeof candidate.content}`],
|
|
335
|
+
chain,
|
|
336
|
+
);
|
|
337
|
+
return {
|
|
338
|
+
action: "blocked",
|
|
339
|
+
reason: "invalid_content",
|
|
340
|
+
chain,
|
|
341
|
+
hint: "content must be a string.",
|
|
342
|
+
};
|
|
266
343
|
}
|
|
267
344
|
if (candidate.content.length > MAX_CONTENT_CHARS) {
|
|
268
|
-
await recBlocked(
|
|
345
|
+
await recBlocked(
|
|
346
|
+
4,
|
|
347
|
+
"content_too_long",
|
|
348
|
+
[`len=${candidate.content.length}`, `max=${MAX_CONTENT_CHARS}`],
|
|
349
|
+
chain,
|
|
350
|
+
);
|
|
269
351
|
return {
|
|
270
352
|
action: "blocked",
|
|
271
353
|
reason: "content_too_long",
|
|
@@ -274,11 +356,13 @@ export async function runWriteGate(
|
|
|
274
356
|
};
|
|
275
357
|
}
|
|
276
358
|
|
|
277
|
-
const badMetadata: string[] = (
|
|
278
|
-
[
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
359
|
+
const badMetadata: string[] = (
|
|
360
|
+
[
|
|
361
|
+
["type", candidate.type],
|
|
362
|
+
["scope", candidate.scope],
|
|
363
|
+
["issue", candidate.issue],
|
|
364
|
+
] as const
|
|
365
|
+
)
|
|
282
366
|
.filter(
|
|
283
367
|
([, value]) =>
|
|
284
368
|
value !== undefined &&
|
|
@@ -288,19 +372,28 @@ export async function runWriteGate(
|
|
|
288
372
|
if (
|
|
289
373
|
candidate.tags !== undefined &&
|
|
290
374
|
(!Array.isArray(candidate.tags) ||
|
|
291
|
-
candidate.tags.some(
|
|
375
|
+
candidate.tags.some(
|
|
376
|
+
(tag) => typeof tag !== "string" || flattenMemoryText(tag) !== tag,
|
|
377
|
+
))
|
|
292
378
|
) {
|
|
293
379
|
badMetadata.push("tags");
|
|
294
380
|
}
|
|
295
381
|
if (
|
|
296
382
|
candidate.kw !== undefined &&
|
|
297
383
|
(!Array.isArray(candidate.kw) ||
|
|
298
|
-
candidate.kw.some(
|
|
384
|
+
candidate.kw.some(
|
|
385
|
+
(k) => typeof k !== "string" || flattenMemoryText(k) !== k,
|
|
386
|
+
))
|
|
299
387
|
) {
|
|
300
388
|
badMetadata.push("kw");
|
|
301
389
|
}
|
|
302
390
|
if (badMetadata.length) {
|
|
303
|
-
await recBlocked(
|
|
391
|
+
await recBlocked(
|
|
392
|
+
4,
|
|
393
|
+
"invalid_metadata",
|
|
394
|
+
[`fields=${badMetadata.join(",")}`],
|
|
395
|
+
chain,
|
|
396
|
+
);
|
|
304
397
|
return {
|
|
305
398
|
action: "blocked",
|
|
306
399
|
reason: "invalid_metadata",
|
|
@@ -346,14 +439,13 @@ export async function runWriteGate(
|
|
|
346
439
|
type: candidate.type,
|
|
347
440
|
ch,
|
|
348
441
|
quarantined: isQuarantined(candidate) || undefined,
|
|
349
|
-
quarantine_patterns: isQuarantined(candidate)
|
|
442
|
+
quarantine_patterns: isQuarantined(candidate)
|
|
443
|
+
? bracketList(quarantineReasons(candidate))
|
|
444
|
+
: undefined,
|
|
350
445
|
});
|
|
351
446
|
|
|
352
447
|
const scopeMems = candidate.scope
|
|
353
|
-
? await loadScope(
|
|
354
|
-
candidate.scope,
|
|
355
|
-
reader,
|
|
356
|
-
)
|
|
448
|
+
? await loadScope(candidate.scope, reader)
|
|
357
449
|
: [];
|
|
358
450
|
// One tree index per write, shared by gate1 and every gate2 stale scan.
|
|
359
451
|
const tree = memoTree(ctx.liveRoot);
|
|
@@ -410,7 +502,8 @@ export async function runWriteGate(
|
|
|
410
502
|
});
|
|
411
503
|
|
|
412
504
|
let supersededTs: string | undefined;
|
|
413
|
-
const driftPlan: { s: StaleRef; target: string; fields: SupersedeFields }[] =
|
|
505
|
+
const driftPlan: { s: StaleRef; target: string; fields: SupersedeFields }[] =
|
|
506
|
+
[];
|
|
414
507
|
for (const s of stale) {
|
|
415
508
|
await rec("ref_stale", {
|
|
416
509
|
gate: 2,
|
|
@@ -438,27 +531,41 @@ export async function runWriteGate(
|
|
|
438
531
|
driftPlan.push({
|
|
439
532
|
s,
|
|
440
533
|
target,
|
|
441
|
-
fields: {
|
|
534
|
+
fields: {
|
|
535
|
+
superseded_by: target,
|
|
536
|
+
reason: "drift",
|
|
537
|
+
symbol: s.symbol,
|
|
538
|
+
from: s.from,
|
|
539
|
+
to: s.to,
|
|
540
|
+
},
|
|
442
541
|
});
|
|
443
542
|
}
|
|
444
543
|
if (driftPlan.length > 0) {
|
|
445
|
-
const { marked } = await markManySupersededReport(
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
544
|
+
const { marked } = await markManySupersededReport(
|
|
545
|
+
driftPlan.map((p) => ({ ts: p.s.memory.ts, fields: p.fields })),
|
|
546
|
+
ctx.durable,
|
|
547
|
+
);
|
|
548
|
+
// #208: the audit trail names only rows THIS call retired. A row the
|
|
549
|
+
// batch skipped (retired out of band between the scan and the publish)
|
|
550
|
+
// belongs to the other session's chain and reason; recording it here
|
|
551
|
+
// would attribute that retirement to this write — the same rule #158
|
|
552
|
+
// round 3 pins for the durable ledger.
|
|
553
|
+
const retired = new Set(marked);
|
|
554
|
+
// Ledger BEFORE the audit lines: what is on disk now stays on disk no
|
|
555
|
+
// matter what the record loop below does (round-3 probe 3).
|
|
556
|
+
for (const p of driftPlan) {
|
|
557
|
+
if (!retired.has(p.s.memory.ts)) continue;
|
|
558
|
+
supersededTs = p.s.memory.ts;
|
|
559
|
+
await rec("superseded_by", {
|
|
560
|
+
gate: 2,
|
|
561
|
+
mem: p.s.memory.ts,
|
|
562
|
+
target: p.target,
|
|
563
|
+
reason: "drift",
|
|
564
|
+
symbol: p.s.symbol,
|
|
565
|
+
from: p.s.from,
|
|
566
|
+
to: p.s.to,
|
|
567
|
+
chain,
|
|
568
|
+
});
|
|
462
569
|
}
|
|
463
570
|
}
|
|
464
571
|
|
|
@@ -483,7 +590,10 @@ export async function runWriteGate(
|
|
|
483
590
|
if (m.ts === ctx.supersedesTs) continue;
|
|
484
591
|
if (tokens(m.content).some((t) => valueSet.has(t))) continue;
|
|
485
592
|
const from = rawTokens(m.content).find(
|
|
486
|
-
(t) =>
|
|
593
|
+
(t) =>
|
|
594
|
+
!STOPWORDS.has(t.toLowerCase()) &&
|
|
595
|
+
!entitySet.has(t.toLowerCase()) &&
|
|
596
|
+
!valueSet.has(t.toLowerCase()),
|
|
487
597
|
);
|
|
488
598
|
canonPlan.push({ ts: m.ts, from });
|
|
489
599
|
}
|
|
@@ -492,25 +602,32 @@ export async function runWriteGate(
|
|
|
492
602
|
const { marked: canonMarked } = await markManySupersededReport(
|
|
493
603
|
canonPlan.map((p) => ({
|
|
494
604
|
ts: p.ts,
|
|
495
|
-
fields: {
|
|
496
|
-
|
|
497
|
-
ctx.durable,
|
|
498
|
-
);
|
|
499
|
-
if (canonMarked.length > 0) {
|
|
500
|
-
// Same ledger discipline as the drift batch above.
|
|
501
|
-
for (const p of canonPlan) {
|
|
502
|
-
if (!supersededTs) supersededTs = p.ts;
|
|
503
|
-
await rec("superseded_by", {
|
|
504
|
-
gate: 2,
|
|
505
|
-
mem: p.ts,
|
|
506
|
-
target,
|
|
605
|
+
fields: {
|
|
606
|
+
superseded_by: target,
|
|
507
607
|
reason: "canonical",
|
|
508
608
|
entity: canon.entry.entity,
|
|
509
609
|
from: p.from,
|
|
510
610
|
to: canon.entry.value,
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
611
|
+
},
|
|
612
|
+
})),
|
|
613
|
+
ctx.durable,
|
|
614
|
+
);
|
|
615
|
+
// #208: same attribution rule as the drift batch above — evidence and
|
|
616
|
+
// promoted.supersedes name only the rows this call actually retired.
|
|
617
|
+
const canonRetired = new Set(canonMarked);
|
|
618
|
+
for (const p of canonPlan) {
|
|
619
|
+
if (!canonRetired.has(p.ts)) continue;
|
|
620
|
+
if (!supersededTs) supersededTs = p.ts;
|
|
621
|
+
await rec("superseded_by", {
|
|
622
|
+
gate: 2,
|
|
623
|
+
mem: p.ts,
|
|
624
|
+
target,
|
|
625
|
+
reason: "canonical",
|
|
626
|
+
entity: canon.entry.entity,
|
|
627
|
+
from: p.from,
|
|
628
|
+
to: canon.entry.value,
|
|
629
|
+
chain,
|
|
630
|
+
});
|
|
514
631
|
}
|
|
515
632
|
}
|
|
516
633
|
|
|
@@ -528,7 +645,8 @@ export async function runWriteGate(
|
|
|
528
645
|
// (gate_start → gate1 → gate2 → dedup_*) stays intact for every verdict.
|
|
529
646
|
const probe = await probeRejection(candidate.content);
|
|
530
647
|
const rejection = probe.rejection;
|
|
531
|
-
const conflictProne =
|
|
648
|
+
const conflictProne =
|
|
649
|
+
candidate.type === "decision" || candidate.type === "preference";
|
|
532
650
|
const active = scopeMems.filter((m) => !m.superseded_by);
|
|
533
651
|
// Exact duplicates are detected against the full scope (a drift-superseded
|
|
534
652
|
// twin still collapses), but overlap/merge only considers live entries.
|
|
@@ -553,7 +671,9 @@ export async function runWriteGate(
|
|
|
553
671
|
);
|
|
554
672
|
// Same-type live rows that could compete with the incoming statement; the
|
|
555
673
|
// row an update replaces is never a contender.
|
|
556
|
-
const live = active.filter(
|
|
674
|
+
const live = active.filter(
|
|
675
|
+
(m) => m.type === candidate.type && m.ts !== ctx.supersedesTs,
|
|
676
|
+
);
|
|
557
677
|
const divergent = live.filter((m) => m.content !== candidate.content);
|
|
558
678
|
|
|
559
679
|
let collapseMatch: Memory | undefined;
|
|
@@ -670,8 +790,10 @@ export async function runWriteGate(
|
|
|
670
790
|
const planningObject = candidate.status === "open";
|
|
671
791
|
const behaviorGrounded = ctx.hasGateCheck || ctx.hasEngagement === true;
|
|
672
792
|
const ambiguous =
|
|
673
|
-
!ctx.trusted &&
|
|
674
|
-
|
|
793
|
+
!ctx.trusted &&
|
|
794
|
+
!canon &&
|
|
795
|
+
(candidate.scope === "" ||
|
|
796
|
+
(!planningObject && g1.refs === 0 && !behaviorGrounded));
|
|
675
797
|
|
|
676
798
|
let blockReason: string | undefined;
|
|
677
799
|
let blockFlags: string[] = [];
|
|
@@ -683,15 +805,21 @@ export async function runWriteGate(
|
|
|
683
805
|
blockFlags =
|
|
684
806
|
rejection.kind === "exact"
|
|
685
807
|
? [`ch=${rejection.tombstone.ch}`, "exact"]
|
|
686
|
-
: [
|
|
687
|
-
|
|
808
|
+
: [
|
|
809
|
+
`ch=${rejection.tombstone.ch}`,
|
|
810
|
+
`score=${rejection.score}`,
|
|
811
|
+
"near_match",
|
|
812
|
+
];
|
|
813
|
+
const when =
|
|
814
|
+
rejection.tombstone.rejected_at.slice(0, 10) || "an earlier session";
|
|
688
815
|
// Scrubbed here as well as on the way in and on the way out of tombstones():
|
|
689
816
|
// this string goes to the model, and the hint is built from a field a human
|
|
690
817
|
// typed. If the row already came through clean this is a no-op — deleting it
|
|
691
818
|
// changes nothing observable today, which is deliberate: the guarantee lives
|
|
692
819
|
// on the read path (it covers every consumer, the viewer included), and this
|
|
693
820
|
// is the seam where the value becomes a model-facing sentence.
|
|
694
|
-
const why =
|
|
821
|
+
const why =
|
|
822
|
+
redactSecrets(rejection.tombstone.reason) || "no reason recorded";
|
|
695
823
|
const tch = rejection.tombstone.ch;
|
|
696
824
|
// The escape-hatch tail is shared by BOTH kinds. An earlier draft put it
|
|
697
825
|
// only on the near-match branch, which left the COMMON case — a byte-exact
|
|
@@ -706,7 +834,8 @@ export async function runWriteGate(
|
|
|
706
834
|
(rejection.kind === "exact"
|
|
707
835
|
? `This exact content was rejected on ${when} (reason: ${why}; hash ${tch}). Nothing was stored. `
|
|
708
836
|
: `Content ${rejection.score} similar to a value rejected on ${when} (reason: ${why}; ` +
|
|
709
|
-
`hash ${tch}) — the same statement reworded counts as the rejected value. Nothing was stored. `) +
|
|
837
|
+
`hash ${tch}) — the same statement reworded counts as the rejected value. Nothing was stored. `) +
|
|
838
|
+
lift;
|
|
710
839
|
} else if (g1.candidateStale) {
|
|
711
840
|
blockReason = "drift_ref_stale_incoming";
|
|
712
841
|
blockFlags = ["incoming_embeds_pre_drift_ref"];
|
|
@@ -725,7 +854,11 @@ export async function runWriteGate(
|
|
|
725
854
|
];
|
|
726
855
|
} else if (overlapMatch) {
|
|
727
856
|
blockReason = "superseded_overlap";
|
|
728
|
-
blockFlags = [
|
|
857
|
+
blockFlags = [
|
|
858
|
+
`supersedes_check_${overlapMatch.ts}`,
|
|
859
|
+
`score=${overlapScore}`,
|
|
860
|
+
"review_gated",
|
|
861
|
+
];
|
|
729
862
|
hint =
|
|
730
863
|
`A live ${candidate.type} row ${overlapMatch.ts} in ${candidate.scope || "(no scope)"} competes ` +
|
|
731
864
|
`with this write (overlap ${overlapScore}). Nothing was stored. To replace that row, use ` +
|
|
@@ -769,25 +902,37 @@ export async function runWriteGate(
|
|
|
769
902
|
// The write is specifically promoted on engagement (no refs, no gate check,
|
|
770
903
|
// not canonical, not an open object). Emitted so the precedent is auditable.
|
|
771
904
|
const groundedByEngagement =
|
|
772
|
-
!ctx.trusted &&
|
|
773
|
-
|
|
774
|
-
|
|
905
|
+
!ctx.trusted &&
|
|
906
|
+
!canon &&
|
|
907
|
+
!planningObject &&
|
|
908
|
+
g1.refs === 0 &&
|
|
909
|
+
!ctx.hasGateCheck &&
|
|
910
|
+
ctx.hasEngagement === true &&
|
|
911
|
+
candidate.scope !== "";
|
|
775
912
|
if (groundedByEngagement) {
|
|
776
913
|
await rec("gate_injected", { gate: 4, chain });
|
|
777
914
|
}
|
|
778
915
|
|
|
779
|
-
const snapshot = await snapshotFiles([
|
|
916
|
+
const snapshot = await snapshotFiles([
|
|
917
|
+
join(memoryRoot(), `${new Date().toISOString().split("T")[0]}.logfmt`),
|
|
918
|
+
]);
|
|
780
919
|
// The write carries the CALLER's session, not whatever current says after
|
|
781
920
|
// the gate's own awaits - capture's stamp must follow the captured session.
|
|
782
|
-
if (candidate.repo === undefined && !writeAt.ok)
|
|
921
|
+
if (candidate.repo === undefined && !writeAt.ok)
|
|
922
|
+
throw new UnresolvedWriterError(writeAt.sid);
|
|
783
923
|
// A captured unscoped ambient writer deliberately publishes repo=global.
|
|
784
924
|
// Leaving repo undefined would make appendMemory resolve ambient ownership
|
|
785
925
|
// again after awaits. This changes the old unstamped shape: exports now keep
|
|
786
926
|
// this row global on import instead of adopting the importing repository.
|
|
787
|
-
const owner =
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
927
|
+
const owner =
|
|
928
|
+
candidate.repo === undefined && writeAt.ok
|
|
929
|
+
? { repo: writeAt.scope?.key ?? "global", repoPath: writeAt.scope?.path }
|
|
930
|
+
: {};
|
|
931
|
+
const ts = await appendMemory({
|
|
932
|
+
...candidate,
|
|
933
|
+
...owner,
|
|
934
|
+
sessionID: ctx.sessionID,
|
|
935
|
+
});
|
|
791
936
|
// #165: remember who made this row, so the writing session is not billed a
|
|
792
937
|
// prompt-cache bust to be shown its own thought. Only promoted writes are
|
|
793
938
|
// attributed — a blocked candidate created nothing.
|
|
@@ -809,20 +954,25 @@ export async function runWriteGate(
|
|
|
809
954
|
ch,
|
|
810
955
|
supersedes: supersededTs ?? "-",
|
|
811
956
|
canonical: canon ? `canonical.logfmt:${canon.entry.ts}` : "-",
|
|
812
|
-
evidence_refs: bracketList(
|
|
957
|
+
evidence_refs: bracketList(
|
|
958
|
+
ctx.evidenceCalls.slice(-MAX_WRITE_EVIDENCE_REFS),
|
|
959
|
+
),
|
|
813
960
|
evidence_refs_count: ctx.evidenceCallsCount ?? ctx.evidenceCalls.length,
|
|
814
961
|
snapshot: snapshot[0] ?? "-",
|
|
815
962
|
trusted: ctx.trusted ? true : undefined,
|
|
816
963
|
grounded_by: groundedByEngagement ? "engagement" : undefined,
|
|
817
964
|
quarantined: isQuarantined(candidate) || undefined,
|
|
818
|
-
quarantine_patterns: isQuarantined(candidate)
|
|
965
|
+
quarantine_patterns: isQuarantined(candidate)
|
|
966
|
+
? bracketList(quarantineReasons(candidate))
|
|
967
|
+
: undefined,
|
|
819
968
|
chain,
|
|
820
969
|
});
|
|
821
970
|
} catch (err) {
|
|
822
971
|
// NOT `(err as Error).message`: a messageless Error (or a non-Error
|
|
823
972
|
// rejection) would make the field FALSY, and callers test it for truth —
|
|
824
973
|
// the lie this field exists to kill would return as a clean success.
|
|
825
|
-
evidenceError =
|
|
974
|
+
evidenceError =
|
|
975
|
+
err instanceof Error && err.message ? err.message : String(err);
|
|
826
976
|
}
|
|
827
977
|
return { action: "promoted", ts, chain, evidenceError };
|
|
828
978
|
}
|