opencode-self-improvement 0.8.3 → 0.8.4
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/package.json +1 -1
- package/src/index.ts +5 -2
- package/src/schema.ts +1 -1
- package/src/undo.ts +123 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-self-improvement",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"description": "Durable memory for opencode: remember facts, decisions, and commands across sessions, plus a one-command per-repo setup (/repo-init).",
|
|
5
5
|
"main": "index.ts",
|
|
6
6
|
"exports": {
|
package/src/index.ts
CHANGED
|
@@ -399,7 +399,7 @@ const impl = {
|
|
|
399
399
|
editor.add(validated(UndoArgs, {
|
|
400
400
|
name: "memory_undo",
|
|
401
401
|
description:
|
|
402
|
-
"List memory snapshots, or restore one (rollback).
|
|
402
|
+
"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. 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.",
|
|
403
403
|
options: direct,
|
|
404
404
|
async execute(args): Promise<{ content: string }> {
|
|
405
405
|
if (args.snapshot) {
|
|
@@ -420,7 +420,10 @@ const impl = {
|
|
|
420
420
|
? ` Kept ${res.concurrentAdded.length} added / ${res.concurrentChanged.length} changed row(s) written after the snapshot.`
|
|
421
421
|
: "";
|
|
422
422
|
const safety = res.safety ? ` Pre-restore file snapshotted -> ${res.safety}.` : "";
|
|
423
|
-
|
|
423
|
+
const omitted = res.deletedOmitted.length
|
|
424
|
+
? ` Omitted ${res.deletedOmitted.length} deliberately deleted row(s); use force=true to recover them.`
|
|
425
|
+
: "";
|
|
426
|
+
return { content: `Restored ${basename(res.target)} from ${res.mode} snapshot -> ${res.target}.${kept}${safety}${omitted}` };
|
|
424
427
|
}
|
|
425
428
|
const snaps = await listSnapshots();
|
|
426
429
|
if (!snaps.length) return { content: "No snapshots" };
|
package/src/schema.ts
CHANGED
|
@@ -94,7 +94,7 @@ export const UndoArgs = z.object({
|
|
|
94
94
|
list: z.boolean().describe("List snapshots").optional(),
|
|
95
95
|
snapshot: z
|
|
96
96
|
.string()
|
|
97
|
-
.describe("
|
|
97
|
+
.describe("Store-owned snapshot to restore (bare name from list, or its full path)")
|
|
98
98
|
.optional(),
|
|
99
99
|
merge: z
|
|
100
100
|
.boolean()
|
package/src/undo.ts
CHANGED
|
@@ -10,12 +10,15 @@
|
|
|
10
10
|
// MERGE replays them on top of the snapshot; FORCE is the old exact rollback,
|
|
11
11
|
// and additionally snapshots the file it replaces, so the state it drops stays
|
|
12
12
|
// undoable (result.safety).
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
13
|
+
import { lstat, realpath, stat } from "node:fs/promises";
|
|
14
|
+
import { basename, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
15
|
+
import { deletionsFile, snapshotDir } from "./config.ts";
|
|
16
|
+
import { ensureDir, parseLine, readLines, writeAtomic } from "./logfmt.ts";
|
|
16
17
|
import { withStoreLock, LOCK_RESTORE } from "./lock.ts";
|
|
17
18
|
import { snapshotFiles } from "./memory.ts";
|
|
18
19
|
|
|
20
|
+
const SNAPSHOT_NAME = /^(\d+)-(\d{4}-\d{2}-\d{2}\.logfmt)$/;
|
|
21
|
+
|
|
19
22
|
export interface SnapshotInfo {
|
|
20
23
|
file: string;
|
|
21
24
|
name: string;
|
|
@@ -28,6 +31,7 @@ export async function listSnapshots(): Promise<SnapshotInfo[]> {
|
|
|
28
31
|
const out: SnapshotInfo[] = [];
|
|
29
32
|
try {
|
|
30
33
|
for await (const f of glob.scan(snapshotDir())) {
|
|
34
|
+
if (!SNAPSHOT_NAME.test(f)) continue;
|
|
31
35
|
const info = Bun.file(join(snapshotDir(), f));
|
|
32
36
|
out.push({
|
|
33
37
|
file: join(snapshotDir(), f),
|
|
@@ -55,6 +59,9 @@ export interface RestoreResult {
|
|
|
55
59
|
// (added after it — or in-place edits, listed under their original ts).
|
|
56
60
|
concurrentAdded: string[];
|
|
57
61
|
concurrentChanged: string[];
|
|
62
|
+
// ts of rows intentionally deleted after this snapshot. CHECK and MERGE
|
|
63
|
+
// leave them deleted; FORCE is the explicit recovery path.
|
|
64
|
+
deletedOmitted: string[];
|
|
58
65
|
}
|
|
59
66
|
|
|
60
67
|
// Rows are keyed by their ts= field; unparseable lines key on the whole line.
|
|
@@ -66,33 +73,113 @@ function rows(text: string): string[] {
|
|
|
66
73
|
return text.split("\n").filter((l) => l.trim());
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
function isWithin(parent: string, child: string): boolean {
|
|
77
|
+
const rel = relative(parent, child);
|
|
78
|
+
return rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function resolveSnapshot(
|
|
82
|
+
snapshot: string,
|
|
83
|
+
memoryRootDir: string,
|
|
84
|
+
): Promise<{ path: string; original: string }> {
|
|
85
|
+
const name = basename(snapshot);
|
|
86
|
+
const match = SNAPSHOT_NAME.exec(name);
|
|
87
|
+
const original = match?.[2];
|
|
88
|
+
const stamp = match ? Number(match[1]) : Number.NaN;
|
|
89
|
+
if (!original || !Number.isSafeInteger(stamp)) {
|
|
90
|
+
throw new Error(`invalid snapshot name: ${name}`);
|
|
91
|
+
}
|
|
92
|
+
if (!isAbsolute(snapshot) && snapshot !== name) {
|
|
93
|
+
throw new Error("snapshot must be a bare name from memory_undo list or a full path");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const rootResolved = resolve(memoryRootDir);
|
|
97
|
+
const snapshotsResolved = resolve(rootResolved, "snapshots");
|
|
98
|
+
const requestedResolved = isAbsolute(snapshot)
|
|
99
|
+
? resolve(snapshot)
|
|
100
|
+
: resolve(snapshotsResolved, name);
|
|
101
|
+
if (!isWithin(snapshotsResolved, requestedResolved)) {
|
|
102
|
+
throw new Error(`snapshot is outside the snapshots directory: ${snapshot}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let rootReal: string;
|
|
106
|
+
let snapshotsReal: string;
|
|
107
|
+
let snapshotReal: string;
|
|
108
|
+
try {
|
|
109
|
+
[rootReal, snapshotsReal, snapshotReal] = await Promise.all([
|
|
110
|
+
realpath(rootResolved),
|
|
111
|
+
realpath(snapshotsResolved),
|
|
112
|
+
realpath(requestedResolved),
|
|
113
|
+
]);
|
|
114
|
+
} catch {
|
|
115
|
+
throw new Error(`snapshot not found: ${snapshot}`);
|
|
116
|
+
}
|
|
117
|
+
if (!isWithin(rootReal, snapshotsReal)) {
|
|
118
|
+
throw new Error("snapshots directory escapes the memory root");
|
|
119
|
+
}
|
|
120
|
+
if (!isWithin(snapshotsReal, snapshotReal)) {
|
|
121
|
+
throw new Error(`snapshot is outside the snapshots directory: ${snapshot}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const [linkInfo, fileInfo] = await Promise.all([
|
|
125
|
+
lstat(requestedResolved),
|
|
126
|
+
stat(snapshotReal),
|
|
127
|
+
]);
|
|
128
|
+
if (linkInfo.isSymbolicLink() || !fileInfo.isFile()) {
|
|
129
|
+
throw new Error(`snapshot is not a regular file: ${snapshot}`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { path: snapshotReal, original };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ts of every row memory_forget or a viewer delete has removed. Ordering
|
|
136
|
+
// against the snapshot is not needed: a snapshot row with a deletion audit
|
|
137
|
+
// that is absent from the live file was removed on purpose.
|
|
138
|
+
async function auditedDeletions(): Promise<Set<string>> {
|
|
139
|
+
const deleted = new Set<string>();
|
|
140
|
+
for (const line of await readLines(deletionsFile())) {
|
|
141
|
+
const fields = parseLine(line);
|
|
142
|
+
if (fields?.action !== "deleted" || typeof fields.original_ts !== "string") continue;
|
|
143
|
+
deleted.add(fields.original_ts);
|
|
144
|
+
}
|
|
145
|
+
return deleted;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Restore `snapshot` over its original file. Only `<stamp>-<YYYY-MM-DD>.logfmt`
|
|
149
|
+
// files inside <memory-root>/snapshots/ are accepted (the viewer restore's
|
|
150
|
+
// grammar); the stamp is stripped to recover the target day file. A bare name
|
|
151
|
+
// (as printed by the list) resolves against <memory-root>/snapshots/.
|
|
72
152
|
export async function restoreSnapshot(
|
|
73
153
|
snapshot: string,
|
|
74
154
|
memoryRootDir: string,
|
|
75
155
|
mode: RestoreMode = "check",
|
|
76
156
|
): Promise<RestoreResult> {
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
157
|
+
const snap = await resolveSnapshot(snapshot, memoryRootDir);
|
|
158
|
+
const rootReal = await realpath(resolve(memoryRootDir));
|
|
159
|
+
const realTarget = resolve(rootReal, snap.original);
|
|
160
|
+
if (!isWithin(rootReal, realTarget)) {
|
|
161
|
+
throw new Error(`snapshot target escapes the memory root: ${snap.original}`);
|
|
82
162
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// carries no separators — but a restore must never write outside the memory
|
|
89
|
-
// root no matter what a future caller passes.
|
|
90
|
-
const rootAbs = resolve(memoryRootDir);
|
|
91
|
-
const target = resolve(rootAbs, original);
|
|
92
|
-
if (!target.startsWith(rootAbs + sep)) {
|
|
93
|
-
throw new Error(`snapshot target escapes the memory root: ${original}`);
|
|
163
|
+
let targetInfo: Awaited<ReturnType<typeof lstat>> | undefined;
|
|
164
|
+
try {
|
|
165
|
+
targetInfo = await lstat(realTarget);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
if (!(err instanceof Error && "code" in err && err.code === "ENOENT")) throw err;
|
|
94
168
|
}
|
|
95
|
-
|
|
169
|
+
if (targetInfo) {
|
|
170
|
+
if (targetInfo.isSymbolicLink()) {
|
|
171
|
+
throw new Error(`snapshot target must not be a symbolic link: ${snap.original}`);
|
|
172
|
+
}
|
|
173
|
+
const targetReal = await realpath(realTarget);
|
|
174
|
+
if (relative(realTarget, targetReal) !== "") {
|
|
175
|
+
throw new Error(`snapshot target must not be a symbolic link: ${snap.original}`);
|
|
176
|
+
}
|
|
177
|
+
if (!targetInfo.isFile()) throw new Error(`snapshot target is not a file: ${snap.original}`);
|
|
178
|
+
}
|
|
179
|
+
// Checks above ran on real paths; lock and write through the same lexical
|
|
180
|
+
// path every other writer uses, so the in-process lock queue is shared.
|
|
181
|
+
const target = resolve(memoryRootDir, snap.original);
|
|
182
|
+
const rawSnapLines = rows(await Bun.file(snap.path).text());
|
|
96
183
|
|
|
97
184
|
// Comparing and publishing happen under the same lock: a divergence check on
|
|
98
185
|
// a stale read would wave through a row that landed after it. A restore waits
|
|
@@ -104,6 +191,17 @@ export async function restoreSnapshot(
|
|
|
104
191
|
async () => {
|
|
105
192
|
const cur = Bun.file(target);
|
|
106
193
|
const curLines = (await cur.exists()) ? rows(await cur.text()) : [];
|
|
194
|
+
// Deliberate deletions are read inside the lock too, so a forget that
|
|
195
|
+
// finished just before this restore is honoured.
|
|
196
|
+
const audited = mode !== "force" ? await auditedDeletions() : new Set<string>();
|
|
197
|
+
const live = new Set(curLines.map(rowKey));
|
|
198
|
+
const deletedOmitted: string[] = [];
|
|
199
|
+
const snapLines = rawSnapLines.filter((line) => {
|
|
200
|
+
const key = rowKey(line);
|
|
201
|
+
if (!audited.has(key) || live.has(key)) return true;
|
|
202
|
+
deletedOmitted.push(key);
|
|
203
|
+
return false;
|
|
204
|
+
});
|
|
107
205
|
|
|
108
206
|
const snapByKey = new Map(snapLines.map((l) => [rowKey(l), l]));
|
|
109
207
|
const concurrentAdded: string[] = [];
|
|
@@ -118,7 +216,7 @@ export async function restoreSnapshot(
|
|
|
118
216
|
}
|
|
119
217
|
|
|
120
218
|
if (mode === "check" && (concurrentAdded.length || concurrentChanged.length)) {
|
|
121
|
-
return { target, restored: false, mode, concurrentAdded, concurrentChanged };
|
|
219
|
+
return { target, restored: false, mode, concurrentAdded, concurrentChanged, deletedOmitted };
|
|
122
220
|
}
|
|
123
221
|
|
|
124
222
|
// FORCE is the mode that drops rows, so the file it replaces is
|
|
@@ -140,7 +238,7 @@ export async function restoreSnapshot(
|
|
|
140
238
|
}
|
|
141
239
|
await ensureDir(memoryRootDir);
|
|
142
240
|
await writeAtomic(target, out.length ? out.join("\n") + "\n" : "");
|
|
143
|
-
return { target, restored: true, mode, safety, concurrentAdded, concurrentChanged };
|
|
241
|
+
return { target, restored: true, mode, safety, concurrentAdded, concurrentChanged, deletedOmitted };
|
|
144
242
|
},
|
|
145
243
|
LOCK_RESTORE,
|
|
146
244
|
);
|