opencode-self-improvement 0.15.18 → 0.15.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/package.json +4 -2
- package/src/capture/candidates.ts +17 -6
- package/src/capture/lag.ts +6 -2
- package/src/capture/prompt.ts +1 -1
- package/src/capture/runtime.ts +85 -21
- package/src/capture/session-model.ts +2 -1
- package/src/capture/transcript.ts +10 -4
- package/src/capture/triggers.ts +48 -14
- package/src/command/ensure.ts +6 -1
- package/src/consolidate/propose.ts +47 -17
- package/src/consolidate/render.ts +3 -1
- package/src/core/cmp-ts.ts +2 -2
- package/src/core/config/budgets.ts +0 -1
- package/src/core/config/paths.ts +2 -3
- package/src/core/hooks.ts +8 -3
- package/src/core/host.ts +8 -3
- package/src/core/logfmt/format.ts +20 -5
- package/src/core/logfmt/publish.ts +21 -4
- package/src/core/lru.ts +20 -20
- package/src/core/permissions.ts +21 -5
- package/src/core/runtime.ts +7 -3
- package/src/core/state.ts +12 -2
- package/src/core/time.ts +0 -1
- package/src/core/tokenize.ts +16 -5
- package/src/core/types.ts +8 -1
- package/src/events.ts +74 -18
- package/src/evidence/record.ts +29 -9
- package/src/gate/gate.ts +208 -64
- package/src/gate/resolver.ts +105 -20
- package/src/hits/ledger.ts +35 -10
- package/src/identity/host-events.ts +35 -11
- package/src/identity/repokey.ts +27 -9
- package/src/identity/session-context.ts +39 -8
- package/src/index.ts +108 -42
- package/src/init/detect.ts +62 -12
- package/src/init/run.ts +50 -11
- package/src/inject/anchor.ts +11 -5
- package/src/inject/attribution.ts +29 -6
- package/src/inject/budget.ts +9 -2
- package/src/inject/digest.ts +16 -8
- package/src/inject/epochs.ts +43 -15
- package/src/inject/pull.ts +11 -5
- package/src/inject/version.ts +10 -5
- package/src/portable/decay.ts +20 -9
- package/src/portable/export.ts +30 -10
- package/src/portable/import.ts +43 -12
- package/src/recall/relevant.ts +11 -5
- package/src/recall/score.ts +5 -3
- package/src/status/format.ts +8 -2
- package/src/status/metrics.ts +22 -6
- package/src/status/payload.ts +7 -2
- package/src/status/report.ts +63 -18
- package/src/status/write.ts +21 -7
- package/src/store/blocklist.ts +16 -4
- package/src/store/containment.ts +27 -8
- package/src/store/files/append.ts +57 -37
- package/src/store/files/deletions.ts +5 -2
- package/src/store/files/load.ts +37 -11
- package/src/store/files/prune.ts +18 -5
- package/src/store/files/snapshots.ts +6 -2
- package/src/store/files/supersede.ts +32 -12
- package/src/store/files/target.ts +4 -1
- package/src/store/lock.ts +10 -2
- package/src/store/model/visibility.ts +26 -6
- package/src/store/prune.ts +1 -1
- package/src/store/quarantine/quarantine.ts +36 -9
- package/src/store/quarantine/release.ts +9 -3
- package/src/store/secrets.ts +4 -2
- package/src/store/tombstones/rejection.ts +21 -5
- package/src/store/tombstones/tombstone.ts +48 -11
- package/src/store/undo.ts +43 -18
- package/src/tools/consolidate.ts +18 -8
- package/src/tools/decay.ts +37 -9
- package/src/tools/deps.ts +10 -2
- package/src/tools/evidence.ts +7 -4
- package/src/tools/export.ts +10 -4
- package/src/tools/forget.ts +15 -5
- package/src/tools/import.ts +16 -5
- package/src/tools/init.ts +13 -4
- package/src/tools/list.ts +12 -6
- package/src/tools/merge-review.ts +52 -20
- package/src/tools/recall.ts +17 -9
- package/src/tools/registry.ts +4 -1
- package/src/tools/remember.ts +21 -7
- package/src/tools/schema.ts +55 -18
- package/src/tools/status-tool.ts +29 -11
- package/src/tools/undo.ts +23 -8
- package/src/tools/update.ts +116 -40
- package/src/tui/panel.tsx +119 -30
- package/src/viewer/mutate.ts +57 -20
- package/src/viewer/rejections.ts +1 -1
- package/src/viewer/server.ts +88 -28
package/src/tools/remember.ts
CHANGED
|
@@ -7,8 +7,16 @@ import type { WriteCandidate } from "../gate/gate.ts";
|
|
|
7
7
|
import { RememberArgs, validated } from "./schema.ts";
|
|
8
8
|
|
|
9
9
|
export const register: ToolRegister = (editor, deps) => {
|
|
10
|
-
const {
|
|
11
|
-
|
|
10
|
+
const {
|
|
11
|
+
toolIds,
|
|
12
|
+
bindSessionIdentity,
|
|
13
|
+
ownerlessWriter,
|
|
14
|
+
refuseOwnerlessWrite,
|
|
15
|
+
gateContext,
|
|
16
|
+
direct,
|
|
17
|
+
} = deps;
|
|
18
|
+
editor.add(
|
|
19
|
+
validated(RememberArgs, {
|
|
12
20
|
name: "memory_remember",
|
|
13
21
|
description:
|
|
14
22
|
"Store a memory after the 4-gate write pipeline validates it (canonical graph, stale refs, dupes). Blocked writes record evidence instead of persisting. Add `kw` — 3-5 aliases, synonyms, verb forms or the literal command a FUTURE session would search with — to make a row findable when the wording differs; keywords are indexed, never injected.",
|
|
@@ -30,15 +38,19 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
30
38
|
// does (#152).
|
|
31
39
|
if (!args.global) {
|
|
32
40
|
const ownerless = ownerlessWriter(sid);
|
|
33
|
-
if (ownerless)
|
|
41
|
+
if (ownerless)
|
|
42
|
+
return await refuseOwnerlessWrite(ownerless, "remember");
|
|
34
43
|
}
|
|
35
44
|
const outcome = await runWriteGate(candidate, gateContext(sid));
|
|
36
45
|
// A durable row whose audit line fell is NOT a clean success
|
|
37
46
|
// (#155 review): the reply says what is missing where it is
|
|
38
47
|
// missing, and the row itself stands.
|
|
39
48
|
if (outcome.action === "promoted")
|
|
40
|
-
return {
|
|
41
|
-
|
|
49
|
+
return {
|
|
50
|
+
content: `Remembered: ${args.type} in ${args.scope}${auditCaveat(outcome)}`,
|
|
51
|
+
};
|
|
52
|
+
if (outcome.action === "collapsed")
|
|
53
|
+
return { content: `Duplicate ignored: ${args.type}/${args.scope}` };
|
|
42
54
|
return {
|
|
43
55
|
content: `Write blocked at GATE (${outcome.reason}). ${
|
|
44
56
|
// #268 F1: the catch-all must name the filter a reader needs.
|
|
@@ -46,9 +58,11 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
46
58
|
// reason — a session that guesses the reason gets "No evidence"
|
|
47
59
|
// while the row is in the log. Keep the `ev=write_blocked`
|
|
48
60
|
// literal in any future hint change.
|
|
49
|
-
outcome.hint ??
|
|
61
|
+
outcome.hint ??
|
|
62
|
+
"Evidence recorded; inspect with memory_evidence ev=write_blocked."
|
|
50
63
|
}`,
|
|
51
64
|
};
|
|
52
65
|
},
|
|
53
|
-
})
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
54
68
|
};
|
package/src/tools/schema.ts
CHANGED
|
@@ -29,9 +29,11 @@ export const isoTs = z.iso.datetime();
|
|
|
29
29
|
// are rejected at every write boundary instead. Lookup-only scopes (recall,
|
|
30
30
|
// forget, export) stay plain strings so a row stored before this check can
|
|
31
31
|
// still be found and forgotten.
|
|
32
|
-
const memoryField = z
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const memoryField = z
|
|
33
|
+
.string()
|
|
34
|
+
.refine((value) => flattenMemoryText(value) === value, {
|
|
35
|
+
message: "must not contain control characters",
|
|
36
|
+
});
|
|
35
37
|
|
|
36
38
|
// Retrieval keywords (#40). The shape cap mirrors MAX_KEYWORDS in memory.ts,
|
|
37
39
|
// which is the authority at write time (it also lowercases, de-dupes and drops
|
|
@@ -68,7 +70,10 @@ export const RememberArgs = z.object({
|
|
|
68
70
|
});
|
|
69
71
|
|
|
70
72
|
export const InitArgs = z.object({
|
|
71
|
-
force: z
|
|
73
|
+
force: z
|
|
74
|
+
.boolean()
|
|
75
|
+
.describe("Re-run even if initialized (refresh after drift)")
|
|
76
|
+
.optional(),
|
|
72
77
|
mode: z
|
|
73
78
|
.enum(["fast", "moderate", "full"])
|
|
74
79
|
.describe("codebase-memory index mode (default moderate)")
|
|
@@ -95,7 +100,9 @@ export const UpdateArgs = z.object({
|
|
|
95
100
|
.optional(),
|
|
96
101
|
status: z
|
|
97
102
|
.enum(STATUSES)
|
|
98
|
-
.describe(
|
|
103
|
+
.describe(
|
|
104
|
+
"Settled or open; omitted keeps the target row's standing (an open frontier row stays open)",
|
|
105
|
+
)
|
|
99
106
|
.optional(),
|
|
100
107
|
});
|
|
101
108
|
|
|
@@ -111,11 +118,15 @@ export const UndoArgs = z.object({
|
|
|
111
118
|
list: z.boolean().describe("List snapshots").optional(),
|
|
112
119
|
snapshot: z
|
|
113
120
|
.string()
|
|
114
|
-
.describe(
|
|
121
|
+
.describe(
|
|
122
|
+
"Store-owned snapshot to restore (bare name from list, or its full path)",
|
|
123
|
+
)
|
|
115
124
|
.optional(),
|
|
116
125
|
merge: z
|
|
117
126
|
.boolean()
|
|
118
|
-
.describe(
|
|
127
|
+
.describe(
|
|
128
|
+
"With snapshot: keep rows written after the snapshot instead of refusing",
|
|
129
|
+
)
|
|
119
130
|
.optional(),
|
|
120
131
|
force: z
|
|
121
132
|
.boolean()
|
|
@@ -126,7 +137,10 @@ export const UndoArgs = z.object({
|
|
|
126
137
|
export const EvidenceArgs = z.object({
|
|
127
138
|
date: z
|
|
128
139
|
.string()
|
|
129
|
-
.regex(
|
|
140
|
+
.regex(
|
|
141
|
+
/^\d{4}-\d{2}-\d{2}$/,
|
|
142
|
+
"must be a YYYY-MM-DD calendar date (paths are refused)",
|
|
143
|
+
)
|
|
130
144
|
.optional(),
|
|
131
145
|
ev: z.string().optional(),
|
|
132
146
|
limit: z.coerce.number().optional(),
|
|
@@ -135,9 +149,15 @@ export const EvidenceArgs = z.object({
|
|
|
135
149
|
export const MergeReviewArgs = z.object({
|
|
136
150
|
loser: z
|
|
137
151
|
.string()
|
|
138
|
-
.describe(
|
|
152
|
+
.describe(
|
|
153
|
+
"Ts id of the stored losing row to supersede (a blocked write was never stored and has no id)",
|
|
154
|
+
),
|
|
139
155
|
survivor: z.string().describe("Ts id of the stored canonical winning row"),
|
|
140
|
-
reason: z
|
|
156
|
+
reason: z
|
|
157
|
+
.string()
|
|
158
|
+
.describe(
|
|
159
|
+
"Why the survivor is canonical over the loser (written to evidence)",
|
|
160
|
+
),
|
|
141
161
|
});
|
|
142
162
|
|
|
143
163
|
export const ConsolidateArgs = z.object({
|
|
@@ -148,11 +168,19 @@ export const ConsolidateArgs = z.object({
|
|
|
148
168
|
.min(0.2)
|
|
149
169
|
.max(0.95)
|
|
150
170
|
.optional(),
|
|
151
|
-
limit: z
|
|
171
|
+
limit: z
|
|
172
|
+
.number()
|
|
173
|
+
.describe("Maximum proposals to report (default 10)")
|
|
174
|
+
.min(1)
|
|
175
|
+
.max(50)
|
|
176
|
+
.optional(),
|
|
152
177
|
});
|
|
153
178
|
|
|
154
179
|
export const ExportArgs = z.object({
|
|
155
|
-
scope: z
|
|
180
|
+
scope: z
|
|
181
|
+
.string()
|
|
182
|
+
.describe("Only export memories in this exact scope")
|
|
183
|
+
.optional(),
|
|
156
184
|
file: z
|
|
157
185
|
.string()
|
|
158
186
|
.describe(
|
|
@@ -162,12 +190,13 @@ export const ExportArgs = z.object({
|
|
|
162
190
|
});
|
|
163
191
|
|
|
164
192
|
export const ImportArgs = z.object({
|
|
165
|
-
file: z
|
|
193
|
+
file: z
|
|
194
|
+
.string()
|
|
195
|
+
.describe("Path to a .jsonl export file, inside the memory root"),
|
|
166
196
|
});
|
|
167
197
|
|
|
168
198
|
export const DecayArgs = z.object({
|
|
169
|
-
olderThanDays: z
|
|
170
|
-
.coerce
|
|
199
|
+
olderThanDays: z.coerce
|
|
171
200
|
.number()
|
|
172
201
|
.optional()
|
|
173
202
|
.meta({ default: 14 })
|
|
@@ -200,11 +229,16 @@ export const EditBody = z.object({
|
|
|
200
229
|
export const DeleteBody = z.object({ ts: isoTs, reason: z.string() });
|
|
201
230
|
export const BlockBody = z.object({ ts: isoTs, reason: z.string() });
|
|
202
231
|
export const UnblockBody = z.object({ ts: isoTs });
|
|
203
|
-
export const ReleaseQuarantineBody = z.object({
|
|
232
|
+
export const ReleaseQuarantineBody = z.object({
|
|
233
|
+
ts: isoTs,
|
|
234
|
+
reason: z.string().trim().min(1),
|
|
235
|
+
});
|
|
204
236
|
// #35: lift a value-keyed rejection. `ch` is the 12-hex content hash the gate's
|
|
205
237
|
// block hint names, and the same identifier a `dedup_rejected` evidence line
|
|
206
238
|
// carries — so a human can act on either without reading the store by hand.
|
|
207
|
-
export const UnrejectBody = z.object({
|
|
239
|
+
export const UnrejectBody = z.object({
|
|
240
|
+
ch: z.string().regex(/^[0-9a-f]{12}$/),
|
|
241
|
+
});
|
|
208
242
|
// Strict fields by intent: String(body.x ?? "") coercion once let unvalidated
|
|
209
243
|
// POSTs append rows with empty type/scope/content.
|
|
210
244
|
// repo/repoPath (#129): the origin stamp of the DELETED row, replayed from the
|
|
@@ -256,7 +290,10 @@ export function validated<S extends ZodTypeAny>(
|
|
|
256
290
|
return {
|
|
257
291
|
...def,
|
|
258
292
|
input: wire(schema),
|
|
259
|
-
async execute(
|
|
293
|
+
async execute(
|
|
294
|
+
raw: unknown,
|
|
295
|
+
context?: unknown,
|
|
296
|
+
): Promise<{ content: string }> {
|
|
260
297
|
const parsed = schema.safeParse(raw ?? {});
|
|
261
298
|
if (!parsed.success) {
|
|
262
299
|
const issues = parsed.error.issues.map(
|
package/src/tools/status-tool.ts
CHANGED
|
@@ -3,18 +3,22 @@
|
|
|
3
3
|
import type { ToolRegister } from "./deps.ts";
|
|
4
4
|
import { readLedger } from "../hits/ledger.ts";
|
|
5
5
|
import { formatMemory } from "../inject/format.ts";
|
|
6
|
-
import { loadAll, malformedCount } from "../store/files/load.ts"
|
|
6
|
+
import { loadAll, malformedCount } from "../store/files/load.ts";
|
|
7
7
|
import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
isQuarantined,
|
|
10
|
+
quarantineReasons,
|
|
11
|
+
} from "../store/quarantine/quarantine.ts";
|
|
9
12
|
import { EmptyArgs, validated } from "./schema.ts";
|
|
10
|
-
import { captureLag, captureLagLines, rowsPerDay } from "../status/metrics.ts"
|
|
11
|
-
import { renderMemoryReport } from "../status/report.ts"
|
|
13
|
+
import { captureLag, captureLagLines, rowsPerDay } from "../status/metrics.ts";
|
|
14
|
+
import { renderMemoryReport } from "../status/report.ts";
|
|
12
15
|
import { blockedRecent } from "../status/write.ts";
|
|
13
16
|
import { summarizeStore } from "../status/store-summary.ts";
|
|
14
17
|
|
|
15
18
|
export const register: ToolRegister = (editor, deps) => {
|
|
16
19
|
const { toolIds, sessions, direct } = deps;
|
|
17
|
-
|
|
20
|
+
editor.add(
|
|
21
|
+
validated(EmptyArgs, {
|
|
18
22
|
name: "memory_status",
|
|
19
23
|
description:
|
|
20
24
|
"Plain-text memory report for chat clients (desktop/web/IDE), same sections as the terminal /memory panel: CONTEXT (this session's injected block, budget use, cumulative ride and abstentions), CACHE (plain-language cache receipts: bust count, and the provider-reported share of prompt tokens served from cache — the proof that memory never mutates your prefix), the block verbatim, recent blocked writes with gate reasons, and store stats. Reads only — no model call.",
|
|
@@ -30,20 +34,34 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
30
34
|
// Use ledger (#42): how much of the store has never been surfaced.
|
|
31
35
|
// Reported, never acted on — retirement is a decision, not a metric.
|
|
32
36
|
const ledger = await readLedger();
|
|
33
|
-
const { active: act, counts: store } = summarizeStore(
|
|
37
|
+
const { active: act, counts: store } = summarizeStore(
|
|
38
|
+
all,
|
|
39
|
+
ledger,
|
|
40
|
+
malformed,
|
|
41
|
+
);
|
|
34
42
|
// Self-metrics (#44): what the store costs, how fast it grows, and how
|
|
35
43
|
// stale a capture is when it fires. Same rule as above — the numbers
|
|
36
44
|
// are the point, not a verdict on them.
|
|
37
|
-
store.bytes = act.reduce(
|
|
38
|
-
|
|
45
|
+
store.bytes = act.reduce(
|
|
46
|
+
(n, m) => n + new TextEncoder().encode(formatMemory(m)).length,
|
|
47
|
+
0,
|
|
48
|
+
);
|
|
49
|
+
store.bytesPerRow = act.length
|
|
50
|
+
? Math.round(store.bytes / act.length)
|
|
51
|
+
: 0;
|
|
39
52
|
store.rowsPerDay = rowsPerDay(act) ?? undefined;
|
|
40
53
|
store.captureLag = captureLag(await captureLagLines());
|
|
41
54
|
return {
|
|
42
|
-
content: renderMemoryReport({
|
|
43
|
-
|
|
55
|
+
content: renderMemoryReport({
|
|
56
|
+
status,
|
|
57
|
+
store,
|
|
58
|
+
blocked: await blockedRecent(6),
|
|
59
|
+
quarantined: act
|
|
60
|
+
.filter((m) => isVisibleTo(m, reportReader) && isQuarantined(m))
|
|
44
61
|
.map((m) => ({ ts: m.ts, patterns: quarantineReasons(m) })),
|
|
45
62
|
}),
|
|
46
63
|
};
|
|
47
64
|
},
|
|
48
|
-
})
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
49
67
|
};
|
package/src/tools/undo.ts
CHANGED
|
@@ -10,7 +10,8 @@ import { basename } from "node:path";
|
|
|
10
10
|
|
|
11
11
|
export const register: ToolRegister = (editor, deps) => {
|
|
12
12
|
const { toolIds, bindSessionIdentity, direct } = deps;
|
|
13
|
-
|
|
13
|
+
editor.add(
|
|
14
|
+
validated(UndoArgs, {
|
|
14
15
|
name: "memory_undo",
|
|
15
16
|
description:
|
|
16
17
|
"List memory snapshots, or restore one (rollback). Only snapshots inside this store's snapshots directory are accepted: pass a bare name from the list or its full path. Non-standard files in snapshots/ are not listed or restorable. SAFE BY DEFAULT: restore refuses if the current file has rows added or edited since the snapshot, and audited human/tool deletions stay deleted. Re-run with merge=true to restore AND keep divergent rows, or force=true for an explicit exact rollback. Blocklisted rows remain inactive in every mode. Rows belonging to other repositories are never restored, rewound, or dropped in any mode — their lines survive verbatim, though a restore may place them later in the shared file. Global rows are visible to every repository, so they are in every repository's rollback scope.",
|
|
@@ -28,7 +29,12 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
28
29
|
// do; the day file is shared (#153) and a wrong reader is a write
|
|
29
30
|
// into another repository's history.
|
|
30
31
|
await bindSessionIdentity(sid);
|
|
31
|
-
const res = await restoreSnapshot(
|
|
32
|
+
const res = await restoreSnapshot(
|
|
33
|
+
args.snapshot,
|
|
34
|
+
memoryRoot(),
|
|
35
|
+
mode,
|
|
36
|
+
readerFromSid(sid),
|
|
37
|
+
);
|
|
32
38
|
if (!res.restored) {
|
|
33
39
|
const parts = [...res.concurrentAdded, ...res.concurrentChanged];
|
|
34
40
|
return {
|
|
@@ -43,21 +49,30 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
43
49
|
: "Rows belonging to other repositories survive in every mode."),
|
|
44
50
|
};
|
|
45
51
|
}
|
|
46
|
-
const kept =
|
|
47
|
-
|
|
52
|
+
const kept =
|
|
53
|
+
res.mode === "merge" &&
|
|
54
|
+
(res.concurrentAdded.length || res.concurrentChanged.length)
|
|
55
|
+
? ` Kept ${res.concurrentAdded.length} added / ${res.concurrentChanged.length} changed row(s) written after the snapshot.`
|
|
56
|
+
: "";
|
|
57
|
+
const safety = res.safety
|
|
58
|
+
? ` Pre-restore file snapshotted -> ${res.safety}.`
|
|
48
59
|
: "";
|
|
49
|
-
const safety = res.safety ? ` Pre-restore file snapshotted -> ${res.safety}.` : "";
|
|
50
60
|
const omitted = res.deletedOmitted.length
|
|
51
61
|
? ` Omitted ${res.deletedOmitted.length} deliberately deleted row(s); use force=true to recover them.`
|
|
52
62
|
: "";
|
|
53
63
|
const foreign = res.foreignPreserved
|
|
54
64
|
? ` Left ${res.foreignPreserved} row(s) belonging to other repositories untouched.`
|
|
55
65
|
: "";
|
|
56
|
-
return {
|
|
66
|
+
return {
|
|
67
|
+
content: `Restored ${basename(res.target)} from ${res.mode} snapshot -> ${res.target}.${kept}${safety}${omitted}${foreign}`,
|
|
68
|
+
};
|
|
57
69
|
}
|
|
58
70
|
const snaps = await listSnapshots();
|
|
59
71
|
if (!snaps.length) return { content: "No snapshots" };
|
|
60
|
-
return {
|
|
72
|
+
return {
|
|
73
|
+
content: snaps.map((s) => `${s.name} (${s.bytes} bytes)`).join("\n"),
|
|
74
|
+
};
|
|
61
75
|
},
|
|
62
|
-
})
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
63
78
|
};
|
package/src/tools/update.ts
CHANGED
|
@@ -6,10 +6,10 @@ import { memoryRoot } from "../core/config/paths.ts";
|
|
|
6
6
|
import { record } from "../evidence/record.ts";
|
|
7
7
|
import { auditCaveat, runWriteGate, sameStrList } from "../gate/gate.ts";
|
|
8
8
|
import { withStoreLock } from "../store/lock.ts";
|
|
9
|
-
import { loadAll } from "../store/files/load.ts"
|
|
10
|
-
import { markSuperseded } from "../store/files/supersede.ts"
|
|
11
|
-
import { resolveMemoryTarget } from "../store/files/target.ts"
|
|
12
|
-
import { isActive } from "../store/model/predicates.ts"
|
|
9
|
+
import { loadAll } from "../store/files/load.ts";
|
|
10
|
+
import { markSuperseded } from "../store/files/supersede.ts";
|
|
11
|
+
import { resolveMemoryTarget } from "../store/files/target.ts";
|
|
12
|
+
import { isActive } from "../store/model/predicates.ts";
|
|
13
13
|
import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
|
|
14
14
|
import type { Memory } from "../store/model/memory.ts";
|
|
15
15
|
import { UpdateArgs, validated } from "./schema.ts";
|
|
@@ -17,8 +17,16 @@ import { redactSecrets } from "../store/secrets.ts";
|
|
|
17
17
|
import { join } from "node:path";
|
|
18
18
|
|
|
19
19
|
export const register: ToolRegister = (editor, deps) => {
|
|
20
|
-
const {
|
|
21
|
-
|
|
20
|
+
const {
|
|
21
|
+
toolIds,
|
|
22
|
+
bindSessionIdentity,
|
|
23
|
+
ownerlessWriter,
|
|
24
|
+
refuseOwnerlessWrite,
|
|
25
|
+
gateContext,
|
|
26
|
+
direct,
|
|
27
|
+
} = deps;
|
|
28
|
+
editor.add(
|
|
29
|
+
validated(UpdateArgs, {
|
|
22
30
|
name: "memory_update",
|
|
23
31
|
description:
|
|
24
32
|
"Update an existing memory (gated like remember; the prior entry is superseded, not erased). `query` selects which row to update: its exact id (ts), a clock fragment (e.g. HH:MM:SS.mmm), or a unique substring of its content. Omitting `query` targets the most recent active row of scope+type. An unmatched or ambiguous `query` fails closed and writes nothing. A prose edit carries over everything the call does not name — tags, issue, keywords, status, and the row's repository. Naming a row that was already updated to exactly this content (metadata included) reports the duplicate; if its live successor holds the prose and only the metadata you name differs, the successor is updated. A failure after the write is reported as PARTIAL: the replacement is durable and memory_undo merge/force restores the old row.",
|
|
@@ -56,7 +64,11 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
56
64
|
join(memoryRoot(), "update-order"),
|
|
57
65
|
async () => {
|
|
58
66
|
const prior = (await loadAll()).filter(
|
|
59
|
-
(m) =>
|
|
67
|
+
(m) =>
|
|
68
|
+
m.scope === args.scope &&
|
|
69
|
+
m.type === args.type &&
|
|
70
|
+
isActive(m) &&
|
|
71
|
+
isVisibleTo(m, reader),
|
|
60
72
|
);
|
|
61
73
|
// Resolve the row BEFORE the gate: an update that cannot identify its
|
|
62
74
|
// target must write nothing, rather than silently superseding whichever
|
|
@@ -77,29 +89,50 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
77
89
|
let chainHead: Memory | undefined;
|
|
78
90
|
if (args.query) {
|
|
79
91
|
const rows = (await loadAll()).filter(
|
|
80
|
-
(m) =>
|
|
92
|
+
(m) =>
|
|
93
|
+
m.scope === args.scope &&
|
|
94
|
+
m.type === args.type &&
|
|
95
|
+
isVisibleTo(m, reader),
|
|
81
96
|
);
|
|
82
97
|
const stale = resolveMemoryTarget(rows, args.query);
|
|
83
|
-
if (
|
|
98
|
+
if (
|
|
99
|
+
stale.status === "ok" &&
|
|
100
|
+
stale.target &&
|
|
101
|
+
!isActive(stale.target)
|
|
102
|
+
) {
|
|
84
103
|
let latest = stale.target;
|
|
85
104
|
const walked = new Set<string>([latest.ts]);
|
|
86
105
|
while (latest.superseded_by) {
|
|
87
|
-
const next = rows.find(
|
|
106
|
+
const next = rows.find(
|
|
107
|
+
(m) => m.ts === latest.superseded_by,
|
|
108
|
+
);
|
|
88
109
|
if (!next || walked.has(next.ts)) break;
|
|
89
110
|
walked.add(next.ts);
|
|
90
111
|
latest = next;
|
|
91
112
|
}
|
|
92
|
-
if (
|
|
113
|
+
if (
|
|
114
|
+
isActive(latest) &&
|
|
115
|
+
latest.content.trim() ===
|
|
116
|
+
redactSecrets(args.content).trim()
|
|
117
|
+
) {
|
|
93
118
|
const restated =
|
|
94
|
-
(args.kw === undefined ||
|
|
95
|
-
|
|
119
|
+
(args.kw === undefined ||
|
|
120
|
+
sameStrList(args.kw, latest.kw)) &&
|
|
121
|
+
(args.status === undefined ||
|
|
122
|
+
args.status === (latest.status ?? "settled"));
|
|
96
123
|
if (restated) {
|
|
97
124
|
// Every other verdict is auditable; this one must
|
|
98
125
|
// be too — the message tells the model to consult
|
|
99
126
|
// memory_evidence.
|
|
100
127
|
await record(
|
|
101
128
|
"dedup_collapse",
|
|
102
|
-
{
|
|
129
|
+
{
|
|
130
|
+
op: "update",
|
|
131
|
+
named: stale.target.ts,
|
|
132
|
+
match: latest.ts,
|
|
133
|
+
action: "exact_successor",
|
|
134
|
+
incoming_ignored: true,
|
|
135
|
+
},
|
|
103
136
|
true,
|
|
104
137
|
sid,
|
|
105
138
|
);
|
|
@@ -114,7 +147,9 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
114
147
|
}
|
|
115
148
|
}
|
|
116
149
|
if (!chainHead) {
|
|
117
|
-
const where = args.query
|
|
150
|
+
const where = args.query
|
|
151
|
+
? ` matches ${JSON.stringify(args.query)}`
|
|
152
|
+
: "";
|
|
118
153
|
return {
|
|
119
154
|
content: `memory_update blocked: no active ${args.type} row in ${args.scope}${where}. Nothing was written.`,
|
|
120
155
|
};
|
|
@@ -150,7 +185,7 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
150
185
|
kw: args.kw ?? target.kw,
|
|
151
186
|
tags: target.tags,
|
|
152
187
|
issue: target.issue,
|
|
153
|
-
status:
|
|
188
|
+
status: args.status ?? target.status ?? "settled",
|
|
154
189
|
repo: target.repo ?? "global",
|
|
155
190
|
};
|
|
156
191
|
const outcome = await runWriteGate(
|
|
@@ -165,7 +200,12 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
165
200
|
repo: requested.repo,
|
|
166
201
|
repoPath: target.repo ? target.repoPath : undefined,
|
|
167
202
|
},
|
|
168
|
-
{
|
|
203
|
+
{
|
|
204
|
+
...updateGate,
|
|
205
|
+
reader,
|
|
206
|
+
supersedesTs: target.ts,
|
|
207
|
+
durable: gateDurable,
|
|
208
|
+
},
|
|
169
209
|
);
|
|
170
210
|
if (outcome.action === "promoted" && outcome.ts && target) {
|
|
171
211
|
mutated = true;
|
|
@@ -206,12 +246,18 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
206
246
|
);
|
|
207
247
|
}
|
|
208
248
|
if (outcome.evidenceError !== undefined) {
|
|
209
|
-
return partial(
|
|
249
|
+
return partial(
|
|
250
|
+
`its write_promoted audit line was not recorded (${outcome.evidenceError})`,
|
|
251
|
+
);
|
|
210
252
|
}
|
|
211
|
-
return {
|
|
253
|
+
return {
|
|
254
|
+
content: `Updated ${args.type} in ${args.scope} (superseded ${retired})`,
|
|
255
|
+
};
|
|
212
256
|
}
|
|
213
257
|
if (outcome.action === "promoted")
|
|
214
|
-
return {
|
|
258
|
+
return {
|
|
259
|
+
content: `Remembered: ${args.type} in ${args.scope}${auditCaveat(outcome)}`,
|
|
260
|
+
};
|
|
215
261
|
if (outcome.action === "collapsed") {
|
|
216
262
|
// Completion (#155 convergence): when a retry collapses
|
|
217
263
|
// because an ACTIVE row already holds exactly this content
|
|
@@ -236,7 +282,10 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
236
282
|
// #175: the gate can observe a session move after this call captured
|
|
237
283
|
// its reader. Recheck before trusting or describing the survivor.
|
|
238
284
|
const survivor = (await loadAll()).find(
|
|
239
|
-
(m) =>
|
|
285
|
+
(m) =>
|
|
286
|
+
m.ts === survivorTs &&
|
|
287
|
+
!m.superseded_by &&
|
|
288
|
+
isVisibleTo(m, reader),
|
|
240
289
|
);
|
|
241
290
|
if (survivor) {
|
|
242
291
|
// #175: private rows are visible by key OR path. Equal keys alone
|
|
@@ -245,7 +294,8 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
245
294
|
// Match raw paths like isVisibleTo; filesystem aliases need not
|
|
246
295
|
// grant the same readers access. This restricts automatic folds;
|
|
247
296
|
// explicit merge_review authorizes both seats under #131 instead.
|
|
248
|
-
const samePathVisibility =
|
|
297
|
+
const samePathVisibility =
|
|
298
|
+
requested.repo === "global" ||
|
|
249
299
|
(target.repoPath ?? "") === (survivor.repoPath ?? "");
|
|
250
300
|
// The predicate is the REQUESTED state vs the survivor
|
|
251
301
|
// (round 3): content equality is already proven by the
|
|
@@ -264,13 +314,17 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
264
314
|
requested.repo === (survivor.repo ?? "global") &&
|
|
265
315
|
samePathVisibility
|
|
266
316
|
) {
|
|
267
|
-
const done = await markSuperseded(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
317
|
+
const done = await markSuperseded(
|
|
318
|
+
target.ts,
|
|
319
|
+
{
|
|
320
|
+
superseded_by: survivorTs,
|
|
321
|
+
reason: "update",
|
|
322
|
+
symbol: target.ts,
|
|
323
|
+
from: target.ts,
|
|
324
|
+
to: survivorTs,
|
|
325
|
+
},
|
|
326
|
+
gateDurable,
|
|
327
|
+
);
|
|
274
328
|
if (done) {
|
|
275
329
|
return {
|
|
276
330
|
content:
|
|
@@ -281,16 +335,27 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
281
335
|
} else {
|
|
282
336
|
const differs: string[] = [];
|
|
283
337
|
if (requested.status !== (survivor.status ?? "settled"))
|
|
284
|
-
differs.push(
|
|
338
|
+
differs.push(
|
|
339
|
+
`status ${requested.status} vs ${survivor.status ?? "settled"}`,
|
|
340
|
+
);
|
|
285
341
|
if (requested.issue !== survivor.issue)
|
|
286
|
-
differs.push(
|
|
342
|
+
differs.push(
|
|
343
|
+
`issue ${requested.issue ?? "-"} vs ${survivor.issue ?? "-"}`,
|
|
344
|
+
);
|
|
287
345
|
if (!sameStrList(requested.kw, survivor.kw))
|
|
288
|
-
differs.push(
|
|
346
|
+
differs.push(
|
|
347
|
+
`kw [${(requested.kw ?? []).join(", ") || "-"}] vs [${(survivor.kw ?? []).join(", ") || "-"}]`,
|
|
348
|
+
);
|
|
289
349
|
if (!sameStrList(requested.tags, survivor.tags))
|
|
290
|
-
differs.push(
|
|
350
|
+
differs.push(
|
|
351
|
+
`tags [${(requested.tags ?? []).join(", ") || "-"}] vs [${(survivor.tags ?? []).join(", ") || "-"}]`,
|
|
352
|
+
);
|
|
291
353
|
if (requested.repo !== (survivor.repo ?? "global"))
|
|
292
|
-
differs.push(
|
|
293
|
-
|
|
354
|
+
differs.push(
|
|
355
|
+
`visibility ${requested.repo} vs ${survivor.repo ?? "global"}`,
|
|
356
|
+
);
|
|
357
|
+
if (!samePathVisibility)
|
|
358
|
+
differs.push("repository path visibility differs");
|
|
294
359
|
return {
|
|
295
360
|
content:
|
|
296
361
|
`Duplicate: ${args.type}/${args.scope} stands active as ${survivorTs}, but ${target.ts} ` +
|
|
@@ -302,14 +367,23 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
302
367
|
}
|
|
303
368
|
}
|
|
304
369
|
}
|
|
305
|
-
return {
|
|
370
|
+
return {
|
|
371
|
+
content: `Duplicate ignored: ${args.type}/${args.scope}`,
|
|
372
|
+
};
|
|
306
373
|
}
|
|
307
|
-
return {
|
|
374
|
+
return {
|
|
375
|
+
content:
|
|
376
|
+
`Write blocked at GATE (${outcome.reason}). ${outcome.hint ?? ""}`.trimEnd(),
|
|
377
|
+
};
|
|
308
378
|
},
|
|
309
379
|
{
|
|
310
380
|
onTimeout: "fail",
|
|
311
381
|
onEvent: (event, waitedMs) => {
|
|
312
|
-
void record("store_lock", {
|
|
382
|
+
void record("store_lock", {
|
|
383
|
+
event,
|
|
384
|
+
waited_ms: waitedMs,
|
|
385
|
+
op: "update",
|
|
386
|
+
});
|
|
313
387
|
},
|
|
314
388
|
},
|
|
315
389
|
);
|
|
@@ -324,7 +398,8 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
324
398
|
// (round 3): the gate may have RETIRED OTHER rows at GATE-2
|
|
325
399
|
// before the fault that got here — those tombstones are on disk,
|
|
326
400
|
// and naming them beats the "Nothing was written" premise.
|
|
327
|
-
const msg =
|
|
401
|
+
const msg =
|
|
402
|
+
err instanceof Error && err.message ? err.message : String(err);
|
|
328
403
|
return {
|
|
329
404
|
content: mutated
|
|
330
405
|
? `memory_update PARTIAL: a step after the durable write failed: ${msg}. ` +
|
|
@@ -338,5 +413,6 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
338
413
|
};
|
|
339
414
|
}
|
|
340
415
|
},
|
|
341
|
-
})
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
342
418
|
};
|