@andares/pi-toolkits 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +47 -6
- package/README.zh-CN.md +41 -6
- package/package.json +7 -6
- package/src/features/autostart/autostart.ts +1 -1
- package/src/features/autostart/index.ts +3 -2
- package/src/features/compact/index.ts +1 -1
- package/src/features/compact/prompt.ts +3 -3
- package/src/features/favorites/patch-selector.ts +5 -1
- package/src/features/keep/constants.ts +44 -0
- package/src/features/keep/format.ts +77 -0
- package/src/features/keep/index.ts +409 -0
- package/src/features/keep/keep.test.ts +955 -0
- package/src/features/keep/ledger.ts +340 -0
- package/src/features/keep/picker.ts +257 -0
- package/src/features/keep/store.ts +271 -0
- package/src/features/permission/panel.ts +3 -1
- package/src/features/quit-guard/index.ts +4 -3
- package/src/features/spec/index.test.ts +121 -0
- package/src/features/spec/index.ts +81 -22
- package/src/features/spec/server.test.ts +49 -0
- package/src/features/spec/server.ts +18 -2
- package/src/features/uat/ledger.ts +13 -62
- package/src/index.ts +3 -0
- package/src/lib/event-ledger.ts +112 -0
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* keep feature — ledger folding, notes, rebuild, commands and picker.
|
|
3
|
+
*
|
|
4
|
+
* Everything is deterministic: a temp ledger, an injected clock/host/pid, and
|
|
5
|
+
* liveness/file probes supplied per test. The command cases encode the product
|
|
6
|
+
* rules that are easy to break: loading never removes a keep, a dead owner is
|
|
7
|
+
* still listed (dimmed) and still loadable, and a cancelled switch must not
|
|
8
|
+
* leave a `load` behind.
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
mkdirSync,
|
|
12
|
+
mkdtempSync,
|
|
13
|
+
readFileSync,
|
|
14
|
+
rmSync,
|
|
15
|
+
symlinkSync,
|
|
16
|
+
writeFileSync,
|
|
17
|
+
} from "node:fs";
|
|
18
|
+
import { tmpdir } from "node:os";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
21
|
+
import type {
|
|
22
|
+
ExtensionAPI,
|
|
23
|
+
ExtensionCommandContext,
|
|
24
|
+
ExtensionContext,
|
|
25
|
+
Theme,
|
|
26
|
+
} from "@earendil-works/pi-coding-agent";
|
|
27
|
+
import { KEEP_ENTRY, KEEP_NOTICE_ENTRY } from "./constants.js";
|
|
28
|
+
import {
|
|
29
|
+
formatAge,
|
|
30
|
+
formatStamp,
|
|
31
|
+
noticeText,
|
|
32
|
+
pickerTitle,
|
|
33
|
+
resumeText,
|
|
34
|
+
rowText,
|
|
35
|
+
} from "./format.js";
|
|
36
|
+
import {
|
|
37
|
+
appendDrop,
|
|
38
|
+
appendKeep,
|
|
39
|
+
appendLoad,
|
|
40
|
+
deriveKeepState,
|
|
41
|
+
type KeepEntryView,
|
|
42
|
+
type KeepLedgerEvent,
|
|
43
|
+
processIsAlive,
|
|
44
|
+
readKeepState,
|
|
45
|
+
reapStaleOwners,
|
|
46
|
+
} from "./ledger.js";
|
|
47
|
+
import { findTarget, parseLoadArgs, registerKeep, samePath, type KeepOptions } from "./index.js";
|
|
48
|
+
import { KeepPicker } from "./picker.js";
|
|
49
|
+
import {
|
|
50
|
+
canonicalSessionFile,
|
|
51
|
+
currentSessionInfo,
|
|
52
|
+
defaultNote,
|
|
53
|
+
rebuildKeepLedger,
|
|
54
|
+
truncateNote,
|
|
55
|
+
} from "./store.js";
|
|
56
|
+
|
|
57
|
+
const dirs: string[] = [];
|
|
58
|
+
|
|
59
|
+
function tempDir(): string {
|
|
60
|
+
const dir = mkdtempSync(join(tmpdir(), "pi-keep-"));
|
|
61
|
+
dirs.push(dir);
|
|
62
|
+
return dir;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
afterEach(() => {
|
|
66
|
+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const NOW = new Date("2026-11-25T10:00:00.000Z");
|
|
70
|
+
|
|
71
|
+
function event(partial: Partial<KeepLedgerEvent> & Pick<KeepLedgerEvent, "type" | "session">): KeepLedgerEvent {
|
|
72
|
+
return {
|
|
73
|
+
v: 1,
|
|
74
|
+
id: `e-${Math.random().toString(36).slice(2, 10)}`,
|
|
75
|
+
ts: "2026-11-25T09:00:00.000Z",
|
|
76
|
+
...(partial as object),
|
|
77
|
+
} as KeepLedgerEvent;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function keepEvent(session: string, overrides: Partial<{ ts: string; note: string; cwd: string; file: string }> = {}): KeepLedgerEvent {
|
|
81
|
+
return {
|
|
82
|
+
v: 1,
|
|
83
|
+
id: `keep-${session}-${Math.random().toString(36).slice(2, 8)}`,
|
|
84
|
+
ts: overrides.ts ?? "2026-11-25T09:00:00.000Z",
|
|
85
|
+
type: "keep",
|
|
86
|
+
session,
|
|
87
|
+
file: overrides.file ?? `/sessions/${session}.jsonl`,
|
|
88
|
+
cwd: overrides.cwd ?? "/tmp/proj",
|
|
89
|
+
note: overrides.note ?? `note ${session}`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function entry(overrides: Partial<KeepEntryView> = {}): KeepEntryView {
|
|
94
|
+
return {
|
|
95
|
+
session: "s1",
|
|
96
|
+
file: "/sessions/s1.jsonl",
|
|
97
|
+
cwd: "/tmp/proj",
|
|
98
|
+
note: "order migration",
|
|
99
|
+
keptAt: "2026-11-25T09:00:00.000Z",
|
|
100
|
+
loaded: false,
|
|
101
|
+
missing: false,
|
|
102
|
+
...overrides,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const probes = {
|
|
107
|
+
host: "host-a",
|
|
108
|
+
isAlive: () => true,
|
|
109
|
+
fileExists: () => true,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
describe("deriveKeepState", () => {
|
|
113
|
+
it("keeps the last keep/drop decision, not the first", () => {
|
|
114
|
+
const dropped = deriveKeepState(
|
|
115
|
+
[keepEvent("s1"), event({ type: "drop", session: "s1" })],
|
|
116
|
+
probes,
|
|
117
|
+
);
|
|
118
|
+
expect(dropped.entries).toHaveLength(0);
|
|
119
|
+
|
|
120
|
+
const rekept = deriveKeepState(
|
|
121
|
+
[
|
|
122
|
+
keepEvent("s1"),
|
|
123
|
+
event({ type: "drop", session: "s1" }),
|
|
124
|
+
keepEvent("s1", { ts: "2026-11-25T09:30:00.000Z" }),
|
|
125
|
+
],
|
|
126
|
+
probes,
|
|
127
|
+
);
|
|
128
|
+
expect(rekept.entries.map((item) => item.session)).toEqual(["s1"]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("stays loaded while any owner is alive", () => {
|
|
132
|
+
const owners = (
|
|
133
|
+
[
|
|
134
|
+
{ type: "load", session: "s1", owner: { pid: 1, host: "host-a", since: "t" } },
|
|
135
|
+
{ type: "load", session: "s1", owner: { pid: 2, host: "host-a", since: "t" } },
|
|
136
|
+
] as KeepLedgerEvent[]
|
|
137
|
+
).map((partial) => event(partial as never));
|
|
138
|
+
const state = deriveKeepState([keepEvent("s1"), ...owners], probes);
|
|
139
|
+
expect(state.entries[0].loaded).toBe(true);
|
|
140
|
+
expect(state.loaded).toHaveLength(1);
|
|
141
|
+
|
|
142
|
+
const afterOne = deriveKeepState(
|
|
143
|
+
[keepEvent("s1"), ...owners, event({ type: "unload", session: "s1", pid: 1 } as never)],
|
|
144
|
+
probes,
|
|
145
|
+
);
|
|
146
|
+
expect(afterOne.entries[0].loaded).toBe(true);
|
|
147
|
+
|
|
148
|
+
const afterBoth = deriveKeepState(
|
|
149
|
+
[
|
|
150
|
+
keepEvent("s1"),
|
|
151
|
+
...owners,
|
|
152
|
+
event({ type: "unload", session: "s1", pid: 1 } as never),
|
|
153
|
+
event({ type: "unload", session: "s1", pid: 2 } as never),
|
|
154
|
+
],
|
|
155
|
+
probes,
|
|
156
|
+
);
|
|
157
|
+
expect(afterBoth.entries[0].loaded).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("treats a dead owner on this host as gone and reports it for reaping", () => {
|
|
161
|
+
const state = deriveKeepState(
|
|
162
|
+
[keepEvent("s1"), event({ type: "load", session: "s1", owner: { pid: 4242, host: "host-a", since: "t" } } as never)],
|
|
163
|
+
{ ...probes, isAlive: () => false },
|
|
164
|
+
);
|
|
165
|
+
expect(state.entries[0].loaded).toBe(false);
|
|
166
|
+
expect(state.staleOwners).toEqual([{ session: "s1", pid: 4242 }]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("assumes an owner on another host is alive (nothing to probe)", () => {
|
|
170
|
+
const state = deriveKeepState(
|
|
171
|
+
[keepEvent("s1"), event({ type: "load", session: "s1", owner: { pid: 4242, host: "host-b", since: "t" } } as never)],
|
|
172
|
+
{ ...probes, isAlive: () => false },
|
|
173
|
+
);
|
|
174
|
+
expect(state.entries[0].loaded).toBe(true);
|
|
175
|
+
expect(state.staleOwners).toHaveLength(0);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("sorts not-loaded first and newest keep first inside each group", () => {
|
|
179
|
+
const state = deriveKeepState(
|
|
180
|
+
[
|
|
181
|
+
keepEvent("old", { ts: "2026-11-20T09:00:00.000Z" }),
|
|
182
|
+
keepEvent("new", { ts: "2026-11-24T09:00:00.000Z" }),
|
|
183
|
+
keepEvent("open", { ts: "2026-11-23T09:00:00.000Z" }),
|
|
184
|
+
event({ type: "load", session: "open", owner: { pid: 1, host: "host-a", since: "t" } } as never),
|
|
185
|
+
],
|
|
186
|
+
probes,
|
|
187
|
+
);
|
|
188
|
+
expect(state.entries.map((item) => item.session)).toEqual(["new", "old", "open"]);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("flags a missing session file", () => {
|
|
192
|
+
const state = deriveKeepState([keepEvent("s1")], { ...probes, fileExists: () => false });
|
|
193
|
+
expect(state.entries[0].missing).toBe(true);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("reaping a dead owner converges: the second read has nothing to repair", () => {
|
|
197
|
+
const ledger = join(tempDir(), "keep.jsonl");
|
|
198
|
+
appendKeep(ledger, { session: "s1", file: "/sessions/s1.jsonl", cwd: "/tmp/proj", note: "n" });
|
|
199
|
+
appendLoad(ledger, "s1", { pid: 4242, host: "host-a", since: NOW.toISOString() });
|
|
200
|
+
const dead = { ...probes, isAlive: () => false };
|
|
201
|
+
|
|
202
|
+
const first = readKeepState(ledger, dead);
|
|
203
|
+
expect(first.state.staleOwners).toEqual([{ session: "s1", pid: 4242 }]);
|
|
204
|
+
reapStaleOwners(ledger, first.state.staleOwners);
|
|
205
|
+
|
|
206
|
+
const second = readKeepState(ledger, dead);
|
|
207
|
+
expect(second.state.staleOwners).toHaveLength(0);
|
|
208
|
+
expect(second.state.entries[0].loaded).toBe(false);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("ledger file", () => {
|
|
213
|
+
it("skips malformed lines and duplicate ids", () => {
|
|
214
|
+
const dir = tempDir();
|
|
215
|
+
const file = join(dir, "keep.jsonl");
|
|
216
|
+
const once = keepEvent("s1");
|
|
217
|
+
writeFileSync(
|
|
218
|
+
file,
|
|
219
|
+
[
|
|
220
|
+
JSON.stringify(once),
|
|
221
|
+
JSON.stringify(once),
|
|
222
|
+
"{not json",
|
|
223
|
+
JSON.stringify({ v: 1, id: "x", ts: "t", type: "keep" }),
|
|
224
|
+
"",
|
|
225
|
+
].join("\n"),
|
|
226
|
+
"utf8",
|
|
227
|
+
);
|
|
228
|
+
const { skipped } = readKeepState(file, probes);
|
|
229
|
+
expect(skipped).toBe(2);
|
|
230
|
+
expect(readKeepState(file, probes).state.entries).toHaveLength(1);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("repairs a half-written last line instead of gluing two records", () => {
|
|
234
|
+
const dir = tempDir();
|
|
235
|
+
const file = join(dir, "keep.jsonl");
|
|
236
|
+
writeFileSync(file, JSON.stringify(keepEvent("s1")).slice(0, 20), "utf8");
|
|
237
|
+
appendDrop(file, "s1", NOW);
|
|
238
|
+
const lines = readFileSync(file, "utf8").split("\n").filter((line) => line.length > 0);
|
|
239
|
+
expect(lines).toHaveLength(2);
|
|
240
|
+
expect(lines[1]).toContain('"type":"drop"');
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("format", () => {
|
|
245
|
+
it("rounds ages coarsely", () => {
|
|
246
|
+
expect(formatAge("2026-11-25T09:59:40.000Z", NOW)).toBe("now");
|
|
247
|
+
expect(formatAge("2026-11-25T09:48:00.000Z", NOW)).toBe("12m");
|
|
248
|
+
expect(formatAge("2026-11-25T07:00:00.000Z", NOW)).toBe("3h");
|
|
249
|
+
expect(formatAge("2026-11-23T10:00:00.000Z", NOW)).toBe("2d");
|
|
250
|
+
expect(formatAge("2026-11-10T10:00:00.000Z", NOW)).toBe("2w");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("renders a row: glyph, age, note, optional project and missing", () => {
|
|
254
|
+
expect(rowText(entry(), { now: NOW, showProject: false })).toBe("○ 1h order migration");
|
|
255
|
+
expect(rowText(entry({ loaded: true }), { now: NOW, showProject: false })).toBe(
|
|
256
|
+
"● 1h order migration",
|
|
257
|
+
);
|
|
258
|
+
expect(rowText(entry({ loaded: true, cwd: "/tmp/other-project" }), { now: NOW, showProject: true })).toBe(
|
|
259
|
+
"● 1h order migration · other-project",
|
|
260
|
+
);
|
|
261
|
+
expect(rowText(entry({ missing: true }), { now: NOW, showProject: false })).toBe(
|
|
262
|
+
"○ 1h order migration · missing",
|
|
263
|
+
);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("titles and notices stay one line", () => {
|
|
267
|
+
expect(pickerTitle(2, 1)).toBe("Keep backlog — 2 not loaded, 1 loaded");
|
|
268
|
+
expect(pickerTitle(2, 0)).toBe("Keep backlog — 2 not loaded");
|
|
269
|
+
expect(noticeText(2)).toBe("2 not loaded · /keep-load");
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("resume text carries the local stamp and the note", () => {
|
|
273
|
+
expect(formatStamp("2026-11-25T09:00:00.000Z")).toBe("2026-11-25 09:00");
|
|
274
|
+
expect(resumeText(entry())).toBe(
|
|
275
|
+
'[keep] Resumed from the backlog — note left 2026-11-25 09:00: "order migration"',
|
|
276
|
+
);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe("args", () => {
|
|
281
|
+
it("parses flags in any order", () => {
|
|
282
|
+
expect(parseLoadArgs("")).toEqual({ all: false, rebuild: false, target: undefined });
|
|
283
|
+
expect(parseLoadArgs("--all")).toEqual({ all: true, rebuild: false, target: undefined });
|
|
284
|
+
expect(parseLoadArgs("rebuild")).toEqual({ all: false, rebuild: true, target: undefined });
|
|
285
|
+
expect(parseLoadArgs("2 --all")).toEqual({ all: true, rebuild: false, target: "2" });
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("resolves a 1-based index or a unique id prefix", () => {
|
|
289
|
+
const list = [entry({ session: "aaa-1" }), entry({ session: "bbb-2" })];
|
|
290
|
+
expect(findTarget(list, "1")?.session).toBe("aaa-1");
|
|
291
|
+
expect(findTarget(list, "2")?.session).toBe("bbb-2");
|
|
292
|
+
expect(findTarget(list, "3")).toBeUndefined();
|
|
293
|
+
expect(findTarget(list, "bbb")?.session).toBe("bbb-2");
|
|
294
|
+
expect(findTarget([entry({ session: "aaa-1" }), entry({ session: "aaa-2" })], "aaa")).toBeUndefined();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("compares directories tolerantly", () => {
|
|
298
|
+
expect(samePath("/a/b/", "/a/b")).toBe(true);
|
|
299
|
+
expect(samePath("/a/b", "/a/c")).toBe(false);
|
|
300
|
+
expect(samePath("", "/a/b")).toBe(false);
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe("notes", () => {
|
|
305
|
+
const info = {
|
|
306
|
+
session: "s1",
|
|
307
|
+
file: "/s.jsonl",
|
|
308
|
+
cwd: "/tmp/proj",
|
|
309
|
+
hasConversation: true,
|
|
310
|
+
firstUserText: "please migrate the order table",
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
it("falls back explicit → previous → session name → first message", () => {
|
|
314
|
+
expect(defaultNote({ explicit: " short note ", info, previous: "older" })).toBe("short note");
|
|
315
|
+
expect(defaultNote({ info, previous: "older" })).toBe("older");
|
|
316
|
+
expect(defaultNote({ info: { ...info, name: "Named" } })).toBe("Named");
|
|
317
|
+
expect(defaultNote({ info })).toBe("please migrate the order table");
|
|
318
|
+
expect(defaultNote({ info: { ...info, firstUserText: undefined } })).toBe("(no note)");
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("collapses whitespace and caps long notes", () => {
|
|
322
|
+
expect(truncateNote(" a\n\n b ")).toBe("a b");
|
|
323
|
+
expect(truncateNote("x".repeat(80), 10)).toHaveLength(10);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("removes terminal control characters from notes", () => {
|
|
327
|
+
expect(truncateNote("before\u001b[31mafter")).toBe("before[31mafter");
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
describe("currentSessionInfo", () => {
|
|
332
|
+
it("canonicalizes a symlinked session file", () => {
|
|
333
|
+
const dir = tempDir();
|
|
334
|
+
const real = join(dir, "real.jsonl");
|
|
335
|
+
const link = join(dir, "link.jsonl");
|
|
336
|
+
writeFileSync(real, "", "utf8");
|
|
337
|
+
symlinkSync(real, link);
|
|
338
|
+
expect(canonicalSessionFile(link)).toBe(real);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
function ctxWith(manager: Record<string, unknown>): ExtensionContext {
|
|
342
|
+
return { sessionManager: manager } as unknown as ExtensionContext;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
it("returns undefined for an in-memory session", () => {
|
|
346
|
+
expect(
|
|
347
|
+
currentSessionInfo(
|
|
348
|
+
ctxWith({ getSessionFile: () => undefined, getSessionId: () => "s1" }),
|
|
349
|
+
),
|
|
350
|
+
).toBeUndefined();
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("reads the first user text out of content blocks", () => {
|
|
354
|
+
const info = currentSessionInfo(
|
|
355
|
+
ctxWith({
|
|
356
|
+
getSessionFile: () => "/s.jsonl",
|
|
357
|
+
getSessionId: () => "s1",
|
|
358
|
+
getCwd: () => "/tmp/proj",
|
|
359
|
+
getSessionName: () => "named",
|
|
360
|
+
buildContextEntries: () => [
|
|
361
|
+
{ type: "message", message: { role: "user", content: [{ type: "text", text: "hello there" }] } },
|
|
362
|
+
{ type: "message", message: { role: "assistant", content: "hi" } },
|
|
363
|
+
],
|
|
364
|
+
}),
|
|
365
|
+
);
|
|
366
|
+
expect(info).toMatchObject({
|
|
367
|
+
session: "s1",
|
|
368
|
+
file: "/s.jsonl",
|
|
369
|
+
cwd: "/tmp/proj",
|
|
370
|
+
name: "named",
|
|
371
|
+
hasConversation: true,
|
|
372
|
+
firstUserText: "hello there",
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("reports an empty session as no conversation", () => {
|
|
377
|
+
const info = currentSessionInfo(
|
|
378
|
+
ctxWith({
|
|
379
|
+
getSessionFile: () => "/s.jsonl",
|
|
380
|
+
getSessionId: () => "s1",
|
|
381
|
+
getCwd: () => "/tmp/proj",
|
|
382
|
+
getSessionName: () => undefined,
|
|
383
|
+
buildContextEntries: () => [],
|
|
384
|
+
}),
|
|
385
|
+
);
|
|
386
|
+
expect(info?.hasConversation).toBe(false);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
describe("rebuildKeepLedger", () => {
|
|
391
|
+
it("re-adds a keep from the session breadcrumb and leaves the rest alone", () => {
|
|
392
|
+
const dir = tempDir();
|
|
393
|
+
const sessions = join(dir, "sessions");
|
|
394
|
+
mkdirSync(sessions, { recursive: true });
|
|
395
|
+
const ledger = join(dir, "keep.jsonl");
|
|
396
|
+
|
|
397
|
+
const withBreadcrumb = join(sessions, "with.jsonl");
|
|
398
|
+
writeFileSync(
|
|
399
|
+
withBreadcrumb,
|
|
400
|
+
[
|
|
401
|
+
JSON.stringify({ type: "session", version: 3, id: "abc", cwd: "/tmp/proj" }),
|
|
402
|
+
JSON.stringify({ type: "message", message: { role: "user", content: "x" } }),
|
|
403
|
+
JSON.stringify({ type: "custom", id: "c1", parentId: null, timestamp: "2026-11-24T08:00:00.000Z", customType: KEEP_ENTRY, data: { note: "half done", at: 1790000000000 } }),
|
|
404
|
+
].join("\n") + "\n",
|
|
405
|
+
"utf8",
|
|
406
|
+
);
|
|
407
|
+
const plain = join(sessions, "plain.jsonl");
|
|
408
|
+
writeFileSync(plain, JSON.stringify({ type: "session", id: "def", cwd: "/tmp/proj" }) + "\n", "utf8");
|
|
409
|
+
|
|
410
|
+
appendKeep(ledger, { session: "known", file: "/sessions/known.jsonl", cwd: "/tmp/proj", note: "already" });
|
|
411
|
+
const state = readKeepState(ledger, probes).state;
|
|
412
|
+
|
|
413
|
+
const result = rebuildKeepLedger({ dir: sessions, ledger, state, now: NOW });
|
|
414
|
+
expect(result).toEqual({ scanned: 2, restored: 1 });
|
|
415
|
+
|
|
416
|
+
const after = readKeepState(ledger, probes).state;
|
|
417
|
+
expect(after.entries.map((item) => item.session).sort()).toEqual(["abc", "known"]);
|
|
418
|
+
expect(after.entries.find((item) => item.session === "abc")?.note).toBe("half done");
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("never resurrects a session that was explicitly dropped", () => {
|
|
422
|
+
const dir = tempDir();
|
|
423
|
+
const sessions = join(dir, "sessions");
|
|
424
|
+
mkdirSync(sessions, { recursive: true });
|
|
425
|
+
const ledger = join(dir, "keep.jsonl");
|
|
426
|
+
const file = join(sessions, "dropped.jsonl");
|
|
427
|
+
writeFileSync(
|
|
428
|
+
file,
|
|
429
|
+
[
|
|
430
|
+
JSON.stringify({ type: "session", id: "dropped", cwd: "/tmp/proj" }),
|
|
431
|
+
JSON.stringify({ type: "custom", customType: KEEP_ENTRY, data: { note: "finished", at: 1790000000000 } }),
|
|
432
|
+
].join("\n") + "\n",
|
|
433
|
+
"utf8",
|
|
434
|
+
);
|
|
435
|
+
appendKeep(ledger, { session: "dropped", file, cwd: "/tmp/proj", note: "finished" });
|
|
436
|
+
appendDrop(ledger, "dropped", NOW);
|
|
437
|
+
|
|
438
|
+
const state = readKeepState(ledger, probes).state;
|
|
439
|
+
expect(state.entries).toHaveLength(0);
|
|
440
|
+
expect(rebuildKeepLedger({ dir: sessions, ledger, state, now: NOW })).toEqual({
|
|
441
|
+
scanned: 1,
|
|
442
|
+
restored: 0,
|
|
443
|
+
});
|
|
444
|
+
expect(readKeepState(ledger, probes).state.entries).toHaveLength(0);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it("never removes anything", () => {
|
|
448
|
+
const dir = tempDir();
|
|
449
|
+
const sessions = join(dir, "sessions");
|
|
450
|
+
mkdirSync(sessions, { recursive: true });
|
|
451
|
+
const ledger = join(dir, "keep.jsonl");
|
|
452
|
+
appendKeep(ledger, { session: "gone", file: "/x.jsonl", cwd: "/tmp/proj", note: "n" });
|
|
453
|
+
|
|
454
|
+
const state = readKeepState(ledger, { ...probes, fileExists: () => false }).state;
|
|
455
|
+
expect(state.entries).toHaveLength(1);
|
|
456
|
+
rebuildKeepLedger({ dir: sessions, ledger, state, now: NOW });
|
|
457
|
+
expect(readKeepState(ledger, probes).state.entries).toHaveLength(1);
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
interface Harness {
|
|
462
|
+
readonly notes: Array<{ message: string; kind?: string }>;
|
|
463
|
+
readonly entries: Array<{ type: string; data: unknown }>;
|
|
464
|
+
readonly switched: string[];
|
|
465
|
+
readonly ready: boolean;
|
|
466
|
+
shutdowns: number;
|
|
467
|
+
sessionName?: string;
|
|
468
|
+
run(command: string, args: string, ctx: ExtensionCommandContext): Promise<void>;
|
|
469
|
+
start(reason: string, ctx: ExtensionContext): void;
|
|
470
|
+
shutdown(reason: string): void;
|
|
471
|
+
ctx(overrides?: Record<string, unknown>): ExtensionCommandContext;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function harness(options: KeepOptions & { ledgerFile: string }): Harness {
|
|
475
|
+
const commands = new Map<string, (args: string, ctx: ExtensionCommandContext) => Promise<void>>();
|
|
476
|
+
const handlers: Record<string, (event: unknown, ctx: ExtensionContext) => unknown> = {};
|
|
477
|
+
const notes: Harness["notes"] = [];
|
|
478
|
+
const entries: Harness["entries"] = [];
|
|
479
|
+
const switched: string[] = [];
|
|
480
|
+
const state = { shutdowns: 0, sessionName: undefined as string | undefined, ready: true };
|
|
481
|
+
|
|
482
|
+
const pi = {
|
|
483
|
+
registerCommand(name: string, definition: { handler: (args: string, ctx: ExtensionCommandContext) => Promise<void> }) {
|
|
484
|
+
commands.set(name, definition.handler);
|
|
485
|
+
},
|
|
486
|
+
registerEntryRenderer() {},
|
|
487
|
+
appendEntry(type: string, data: unknown) {
|
|
488
|
+
entries.push({ type, data });
|
|
489
|
+
},
|
|
490
|
+
setSessionName(name: string) {
|
|
491
|
+
state.sessionName = name;
|
|
492
|
+
},
|
|
493
|
+
on(event: string, handler: (event: unknown, ctx: ExtensionContext) => unknown) {
|
|
494
|
+
handlers[event] = handler;
|
|
495
|
+
},
|
|
496
|
+
} as unknown as ExtensionAPI;
|
|
497
|
+
|
|
498
|
+
registerKeep(pi, { fileExists: () => true, ...options });
|
|
499
|
+
|
|
500
|
+
function makeCtx(overrides: Record<string, unknown> = {}): ExtensionCommandContext {
|
|
501
|
+
const base = {
|
|
502
|
+
mode: "tui",
|
|
503
|
+
hasUI: true,
|
|
504
|
+
cwd: "/tmp/proj",
|
|
505
|
+
isIdle: () => true,
|
|
506
|
+
ui: {
|
|
507
|
+
notify: (message: string, kind?: string) => notes.push({ message, kind }),
|
|
508
|
+
},
|
|
509
|
+
sessionManager: {
|
|
510
|
+
getSessionFile: () => "/sessions/current.jsonl",
|
|
511
|
+
getSessionId: () => "current",
|
|
512
|
+
getCwd: () => "/tmp/proj",
|
|
513
|
+
getSessionName: () => state.sessionName,
|
|
514
|
+
getSessionDir: () => "/sessions",
|
|
515
|
+
buildContextEntries: () => [
|
|
516
|
+
{ type: "message", message: { role: "user", content: "work in progress" } },
|
|
517
|
+
],
|
|
518
|
+
},
|
|
519
|
+
shutdown: () => {
|
|
520
|
+
state.shutdowns += 1;
|
|
521
|
+
},
|
|
522
|
+
switchSession: async (path: string, opts: { withSession?: (ctx: ExtensionContext) => Promise<void> }) => {
|
|
523
|
+
switched.push(path);
|
|
524
|
+
await opts.withSession?.({
|
|
525
|
+
ui: { notify: () => {} },
|
|
526
|
+
sendMessage: () => Promise.resolve(),
|
|
527
|
+
} as unknown as ExtensionContext);
|
|
528
|
+
return { cancelled: false };
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
return { ...base, ...overrides } as unknown as ExtensionCommandContext;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return {
|
|
535
|
+
notes,
|
|
536
|
+
entries,
|
|
537
|
+
switched,
|
|
538
|
+
get ready() {
|
|
539
|
+
return state.ready;
|
|
540
|
+
},
|
|
541
|
+
get shutdowns() {
|
|
542
|
+
return state.shutdowns;
|
|
543
|
+
},
|
|
544
|
+
get sessionName() {
|
|
545
|
+
return state.sessionName;
|
|
546
|
+
},
|
|
547
|
+
run: (command, args, ctx) => commands.get(command)?.(args, ctx) ?? Promise.resolve(),
|
|
548
|
+
start: (reason, ctx) => {
|
|
549
|
+
handlers.session_start?.({ type: "session_start", reason }, ctx);
|
|
550
|
+
},
|
|
551
|
+
shutdown: (reason) => {
|
|
552
|
+
handlers.session_shutdown?.({ type: "session_shutdown", reason }, {} as ExtensionContext);
|
|
553
|
+
},
|
|
554
|
+
ctx: makeCtx,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
describe("/keep-quit", () => {
|
|
559
|
+
function setup(overrides: Partial<KeepOptions> = {}) {
|
|
560
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
561
|
+
return {
|
|
562
|
+
ledgerFile,
|
|
563
|
+
harness: harness({
|
|
564
|
+
ledgerFile,
|
|
565
|
+
now: () => NOW,
|
|
566
|
+
host: "host-a",
|
|
567
|
+
pid: 100,
|
|
568
|
+
isAlive: () => true,
|
|
569
|
+
fileExists: () => true,
|
|
570
|
+
...overrides,
|
|
571
|
+
}),
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
it("refuses outside the TUI, while busy, when unpersisted, and when empty", async () => {
|
|
576
|
+
const { harness: h } = setup();
|
|
577
|
+
await h.run("keep-quit", "", h.ctx({ mode: "print" }));
|
|
578
|
+
await h.run("keep-quit", "", h.ctx({ isIdle: () => false }));
|
|
579
|
+
await h.run(
|
|
580
|
+
"keep-quit",
|
|
581
|
+
"",
|
|
582
|
+
h.ctx({ sessionManager: { getSessionFile: () => undefined } }),
|
|
583
|
+
);
|
|
584
|
+
await h.run(
|
|
585
|
+
"keep-quit",
|
|
586
|
+
"",
|
|
587
|
+
h.ctx({
|
|
588
|
+
sessionManager: {
|
|
589
|
+
getSessionFile: () => "/sessions/current.jsonl",
|
|
590
|
+
getSessionId: () => "current",
|
|
591
|
+
getCwd: () => "/tmp/proj",
|
|
592
|
+
getSessionName: () => undefined,
|
|
593
|
+
buildContextEntries: () => [],
|
|
594
|
+
},
|
|
595
|
+
}),
|
|
596
|
+
);
|
|
597
|
+
expect(h.notes).toHaveLength(4);
|
|
598
|
+
expect(h.entries).toHaveLength(0);
|
|
599
|
+
expect(h.shutdowns).toBe(0);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it("refuses when the session file was deleted", async () => {
|
|
603
|
+
const { harness: h } = setup({ fileExists: () => false });
|
|
604
|
+
await h.run("keep-quit", "must not record", h.ctx());
|
|
605
|
+
expect(h.notes.at(-1)?.message).toBe("This session is not saved on disk — there is nothing to keep");
|
|
606
|
+
expect(h.entries).toHaveLength(0);
|
|
607
|
+
expect(h.shutdowns).toBe(0);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("writes the breadcrumb, the keep event, the session name, then quits", async () => {
|
|
611
|
+
const { harness: h, ledgerFile } = setup();
|
|
612
|
+
await h.run("keep-quit", " order migration ", h.ctx());
|
|
613
|
+
|
|
614
|
+
expect(h.entries).toEqual([
|
|
615
|
+
{ type: KEEP_ENTRY, data: { note: "order migration", at: NOW.getTime() } },
|
|
616
|
+
]);
|
|
617
|
+
expect(h.sessionName).toBe("order migration");
|
|
618
|
+
expect(h.shutdowns).toBe(1);
|
|
619
|
+
|
|
620
|
+
const state = readKeepState(ledgerFile, probes).state;
|
|
621
|
+
expect(state.entries).toHaveLength(1);
|
|
622
|
+
expect(state.entries[0]).toMatchObject({
|
|
623
|
+
session: "current",
|
|
624
|
+
file: "/sessions/current.jsonl",
|
|
625
|
+
cwd: "/tmp/proj",
|
|
626
|
+
note: "order migration",
|
|
627
|
+
loaded: false,
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
describe("/keep-drop", () => {
|
|
633
|
+
it("drops a kept session and refuses one that is not kept", async () => {
|
|
634
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
635
|
+
appendKeep(ledgerFile, {
|
|
636
|
+
session: "current",
|
|
637
|
+
file: "/sessions/current.jsonl",
|
|
638
|
+
cwd: "/tmp/proj",
|
|
639
|
+
note: "n",
|
|
640
|
+
});
|
|
641
|
+
const h = harness({ ledgerFile, now: () => NOW, host: "host-a", pid: 100, isAlive: () => true });
|
|
642
|
+
|
|
643
|
+
await h.run("keep-drop", "", h.ctx());
|
|
644
|
+
expect(readKeepState(ledgerFile, probes).state.entries).toHaveLength(0);
|
|
645
|
+
expect(h.notes.at(-1)?.message).toContain("Dropped");
|
|
646
|
+
|
|
647
|
+
await h.run("keep-drop", "", h.ctx());
|
|
648
|
+
expect(h.notes.at(-1)?.message).toBe("This session is not in the keep list");
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
describe("/keep-load", () => {
|
|
653
|
+
function setup(options: { veto?: boolean; missing?: boolean } = {}) {
|
|
654
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
655
|
+
appendKeep(ledgerFile, {
|
|
656
|
+
session: "target",
|
|
657
|
+
file: "/sessions/target.jsonl",
|
|
658
|
+
cwd: "/tmp/proj",
|
|
659
|
+
note: "older task",
|
|
660
|
+
});
|
|
661
|
+
const h = harness({
|
|
662
|
+
ledgerFile,
|
|
663
|
+
now: () => NOW,
|
|
664
|
+
host: "host-a",
|
|
665
|
+
pid: 100,
|
|
666
|
+
isAlive: () => true,
|
|
667
|
+
fileExists: () => !options.missing,
|
|
668
|
+
ask: () => "load",
|
|
669
|
+
});
|
|
670
|
+
const ctx = h.ctx({
|
|
671
|
+
switchSession: async (
|
|
672
|
+
path: string,
|
|
673
|
+
opts: { withSession?: (ctx: ExtensionContext) => Promise<void> },
|
|
674
|
+
) => {
|
|
675
|
+
if (options.veto) return { cancelled: true };
|
|
676
|
+
h.switched.push(path);
|
|
677
|
+
await opts.withSession?.({
|
|
678
|
+
ui: { notify: () => {} },
|
|
679
|
+
sendMessage: (message: { customType: string }) => {
|
|
680
|
+
h.entries.push({ type: message.customType, data: null });
|
|
681
|
+
},
|
|
682
|
+
} as unknown as ExtensionContext);
|
|
683
|
+
return { cancelled: false };
|
|
684
|
+
},
|
|
685
|
+
});
|
|
686
|
+
return { h, ctx, ledgerFile };
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
it("loads a target by index and injects the resume note", async () => {
|
|
690
|
+
const { h, ctx, ledgerFile } = setup();
|
|
691
|
+
await h.run("keep-load", "1", ctx);
|
|
692
|
+
|
|
693
|
+
expect(h.switched).toEqual(["/sessions/target.jsonl"]);
|
|
694
|
+
expect(h.entries.map((item) => item.type)).toEqual(["keep:resume"]);
|
|
695
|
+
|
|
696
|
+
const state = readKeepState(ledgerFile, probes).state;
|
|
697
|
+
expect(state.entries).toHaveLength(1);
|
|
698
|
+
expect(state.entries[0].loaded).toBe(true);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it("releases the load when the switch is cancelled", async () => {
|
|
702
|
+
const { h, ctx, ledgerFile } = setup({ veto: true });
|
|
703
|
+
await h.run("keep-load", "1", ctx);
|
|
704
|
+
|
|
705
|
+
expect(h.switched).toHaveLength(0);
|
|
706
|
+
expect(h.notes.at(-1)?.message).toBe("Load cancelled");
|
|
707
|
+
const state = readKeepState(ledgerFile, probes).state;
|
|
708
|
+
expect(state.entries[0].loaded).toBe(false);
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it("keeps the switched session loaded when saving the resume note fails", async () => {
|
|
712
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
713
|
+
appendKeep(ledgerFile, {
|
|
714
|
+
session: "target",
|
|
715
|
+
file: "/sessions/target.jsonl",
|
|
716
|
+
cwd: "/tmp/proj",
|
|
717
|
+
note: "older",
|
|
718
|
+
});
|
|
719
|
+
const h = harness({
|
|
720
|
+
ledgerFile,
|
|
721
|
+
now: () => NOW,
|
|
722
|
+
host: "host-a",
|
|
723
|
+
pid: 100,
|
|
724
|
+
isAlive: () => true,
|
|
725
|
+
fileExists: () => true,
|
|
726
|
+
ask: () => "load",
|
|
727
|
+
});
|
|
728
|
+
const ctx = h.ctx({
|
|
729
|
+
switchSession: async (
|
|
730
|
+
path: string,
|
|
731
|
+
opts: { withSession?: (ctx: ExtensionContext) => Promise<void> },
|
|
732
|
+
) => {
|
|
733
|
+
h.switched.push(path);
|
|
734
|
+
await opts.withSession?.({
|
|
735
|
+
ui: { notify: (message: string, kind?: string) => h.notes.push({ message, kind }) },
|
|
736
|
+
sendMessage: () => Promise.reject(new Error("disk full")),
|
|
737
|
+
} as unknown as ExtensionContext);
|
|
738
|
+
return { cancelled: false };
|
|
739
|
+
},
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
await h.run("keep-load", "1", ctx);
|
|
743
|
+
expect(h.switched).toEqual(["/sessions/target.jsonl"]);
|
|
744
|
+
expect(h.notes.at(-1)?.message).toContain("resume note could not be saved: disk full");
|
|
745
|
+
expect(readKeepState(ledgerFile, probes).state.entries[0].loaded).toBe(true);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it("refuses a missing session file without switching", async () => {
|
|
749
|
+
const { h, ctx } = setup({ missing: true });
|
|
750
|
+
await h.run("keep-load", "1", ctx);
|
|
751
|
+
expect(h.switched).toHaveLength(0);
|
|
752
|
+
expect(h.notes.at(-1)?.message).toContain("Session file is gone");
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it("hides the current session and only lists this project without --all", async () => {
|
|
756
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
757
|
+
appendKeep(ledgerFile, { session: "target", file: "/t.jsonl", cwd: "/tmp/proj", note: "mine" });
|
|
758
|
+
appendKeep(ledgerFile, { session: "current", file: "/sessions/current.jsonl", cwd: "/tmp/proj", note: "me" });
|
|
759
|
+
appendKeep(ledgerFile, { session: "elsewhere", file: "/e.jsonl", cwd: "/tmp/other", note: "theirs" });
|
|
760
|
+
|
|
761
|
+
const seen: string[][] = [];
|
|
762
|
+
const h = harness({
|
|
763
|
+
ledgerFile,
|
|
764
|
+
now: () => NOW,
|
|
765
|
+
host: "host-a",
|
|
766
|
+
pid: 100,
|
|
767
|
+
isAlive: () => true,
|
|
768
|
+
ask: () => "load",
|
|
769
|
+
pick: (_ctx, entries) => {
|
|
770
|
+
seen.push(entries.map((item) => item.session));
|
|
771
|
+
return { action: "cancel" };
|
|
772
|
+
},
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
await h.run("keep-load", "", h.ctx());
|
|
776
|
+
await h.run("keep-load", "--all", h.ctx());
|
|
777
|
+
expect(seen[0]).toEqual(["target"]);
|
|
778
|
+
// Both keeps can share a millisecond; only membership matters here.
|
|
779
|
+
expect([...seen[1]].sort()).toEqual(["elsewhere", "target"]);
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
it("keeps the current session first when the dialog says so", async () => {
|
|
783
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
784
|
+
appendKeep(ledgerFile, {
|
|
785
|
+
session: "target",
|
|
786
|
+
file: "/sessions/target.jsonl",
|
|
787
|
+
cwd: "/tmp/proj",
|
|
788
|
+
note: "older",
|
|
789
|
+
});
|
|
790
|
+
const h = harness({
|
|
791
|
+
ledgerFile,
|
|
792
|
+
now: () => NOW,
|
|
793
|
+
host: "host-a",
|
|
794
|
+
pid: 100,
|
|
795
|
+
isAlive: () => true,
|
|
796
|
+
fileExists: () => true,
|
|
797
|
+
ask: () => "keep",
|
|
798
|
+
});
|
|
799
|
+
await h.run("keep-load", "1", h.ctx());
|
|
800
|
+
expect(h.switched).toEqual(["/sessions/target.jsonl"]);
|
|
801
|
+
const state = readKeepState(ledgerFile, probes).state;
|
|
802
|
+
expect(state.entries.map((item) => item.session).sort()).toEqual(["current", "target"]);
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
describe("startup notice", () => {
|
|
807
|
+
function setup() {
|
|
808
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
809
|
+
appendKeep(ledgerFile, { session: "a", file: "/a.jsonl", cwd: "/tmp/proj", note: "one" });
|
|
810
|
+
appendKeep(ledgerFile, { session: "b", file: "/b.jsonl", cwd: "/tmp/proj", note: "two" });
|
|
811
|
+
appendKeep(ledgerFile, { session: "c", file: "/c.jsonl", cwd: "/tmp/other", note: "elsewhere" });
|
|
812
|
+
const h = harness({
|
|
813
|
+
ledgerFile,
|
|
814
|
+
now: () => NOW,
|
|
815
|
+
host: "host-a",
|
|
816
|
+
pid: 100,
|
|
817
|
+
isAlive: () => true,
|
|
818
|
+
fileExists: () => true,
|
|
819
|
+
});
|
|
820
|
+
const ctx = (sessionId: string) =>
|
|
821
|
+
({
|
|
822
|
+
mode: "tui",
|
|
823
|
+
cwd: "/tmp/proj",
|
|
824
|
+
ui: { notify: () => {} },
|
|
825
|
+
sessionManager: { getSessionId: () => sessionId },
|
|
826
|
+
}) as unknown as ExtensionContext;
|
|
827
|
+
return { h, ctx };
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
it("counts this project only, excluding the current session, once per session", () => {
|
|
831
|
+
const { h, ctx } = setup();
|
|
832
|
+
h.start("startup", ctx("current"));
|
|
833
|
+
expect(h.entries).toEqual([{ type: KEEP_NOTICE_ENTRY, data: { count: 2 } }]);
|
|
834
|
+
|
|
835
|
+
h.start("startup", ctx("current"));
|
|
836
|
+
h.start("startup", ctx("a"));
|
|
837
|
+
expect(h.entries).toEqual([
|
|
838
|
+
{ type: KEEP_NOTICE_ENTRY, data: { count: 2 } },
|
|
839
|
+
{ type: KEEP_NOTICE_ENTRY, data: { count: 1 } },
|
|
840
|
+
]);
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
it("ignores non-startup reasons and non-TUI modes", () => {
|
|
844
|
+
const { h, ctx } = setup();
|
|
845
|
+
h.start("resume", ctx("current"));
|
|
846
|
+
h.start("startup", { ...(ctx("current") as object), mode: "print" } as ExtensionContext);
|
|
847
|
+
expect(h.entries).toHaveLength(0);
|
|
848
|
+
});
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
describe("session_shutdown", () => {
|
|
852
|
+
it("releases the loaded session once, except on reload", async () => {
|
|
853
|
+
const ledgerFile = join(tempDir(), "keep.jsonl");
|
|
854
|
+
appendKeep(ledgerFile, { session: "target", file: "/t.jsonl", cwd: "/tmp/proj", note: "n" });
|
|
855
|
+
const h = harness({
|
|
856
|
+
ledgerFile,
|
|
857
|
+
now: () => NOW,
|
|
858
|
+
host: "host-a",
|
|
859
|
+
pid: 100,
|
|
860
|
+
isAlive: () => true,
|
|
861
|
+
fileExists: () => true,
|
|
862
|
+
ask: () => "load",
|
|
863
|
+
});
|
|
864
|
+
await h.run("keep-load", "1", h.ctx());
|
|
865
|
+
expect(readKeepState(ledgerFile, probes).state.entries[0].loaded).toBe(true);
|
|
866
|
+
|
|
867
|
+
h.shutdown("reload");
|
|
868
|
+
expect(readKeepState(ledgerFile, probes).state.entries[0].loaded).toBe(true);
|
|
869
|
+
|
|
870
|
+
h.shutdown("quit");
|
|
871
|
+
expect(readKeepState(ledgerFile, probes).state.entries[0].loaded).toBe(false);
|
|
872
|
+
|
|
873
|
+
h.shutdown("quit");
|
|
874
|
+
const unloads = readFileSync(ledgerFile, "utf8")
|
|
875
|
+
.split("\n")
|
|
876
|
+
.filter((line) => line.includes('"type":"unload"'));
|
|
877
|
+
expect(unloads).toHaveLength(1);
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
describe("KeepPicker", () => {
|
|
882
|
+
const theme = { bold: (text: string) => text, fg: (_color: string, text: string) => text } as unknown as Theme;
|
|
883
|
+
|
|
884
|
+
function mount(entries: KeepEntryView[]) {
|
|
885
|
+
const drops: string[] = [];
|
|
886
|
+
let result: unknown;
|
|
887
|
+
const picker = new KeepPicker(
|
|
888
|
+
entries,
|
|
889
|
+
{ now: NOW, showProject: false, drop: (item) => drops.push(item.session) },
|
|
890
|
+
theme,
|
|
891
|
+
(value) => {
|
|
892
|
+
result = value;
|
|
893
|
+
},
|
|
894
|
+
() => {},
|
|
895
|
+
);
|
|
896
|
+
return { picker, drops, result: () => result };
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
it("renders one row per entry with the state glyph", () => {
|
|
900
|
+
const { picker } = mount([entry(), entry({ session: "s2", loaded: true, note: "second" })]);
|
|
901
|
+
const lines = picker.render(80);
|
|
902
|
+
expect(lines[0]).toBe("Keep backlog — 1 not loaded, 1 loaded");
|
|
903
|
+
expect(lines[3]).toBe("→ ○ 1h order migration");
|
|
904
|
+
expect(lines[4]).toBe(" ● 1h second");
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
it("drops the highlighted row with ctrl+r without loading", () => {
|
|
908
|
+
const { picker, drops, result } = mount([entry(), entry({ session: "s2", note: "second" })]);
|
|
909
|
+
picker.handleInput("\x12");
|
|
910
|
+
expect(drops).toEqual(["s1"]);
|
|
911
|
+
expect(result()).toBeUndefined();
|
|
912
|
+
expect(picker.render(80)[3]).toBe("→ ○ 1h second");
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
it("moves with the arrow keys and loads with enter", () => {
|
|
916
|
+
const { picker, result } = mount([entry(), entry({ session: "s2", note: "second" })]);
|
|
917
|
+
picker.handleInput("\x1b[B");
|
|
918
|
+
picker.handleInput("\r");
|
|
919
|
+
expect(result()).toEqual({ action: "load", entry: expect.objectContaining({ session: "s2" }) });
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
it("cancels on escape", () => {
|
|
923
|
+
const { picker, result } = mount([entry()]);
|
|
924
|
+
picker.handleInput("\x1b");
|
|
925
|
+
expect(result()).toEqual({ action: "cancel" });
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
it("cancels when the last row is dropped", () => {
|
|
929
|
+
const { picker, result } = mount([entry()]);
|
|
930
|
+
picker.handleInput("\x12");
|
|
931
|
+
expect(result()).toEqual({ action: "cancel" });
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
it("scrolls instead of overflowing", () => {
|
|
935
|
+
const many = Array.from({ length: 20 }, (_unused, index) =>
|
|
936
|
+
entry({ session: `s${index}`, note: `task ${index}` }),
|
|
937
|
+
);
|
|
938
|
+
const picker = new KeepPicker(
|
|
939
|
+
many,
|
|
940
|
+
{ now: NOW, showProject: false, maxRows: 3, drop: () => {} },
|
|
941
|
+
theme,
|
|
942
|
+
() => {},
|
|
943
|
+
() => {},
|
|
944
|
+
);
|
|
945
|
+
const lines = picker.render(80);
|
|
946
|
+
expect(lines).toHaveLength(3 + 3 + 1);
|
|
947
|
+
expect(lines.at(-1)).toBe(" 1/20");
|
|
948
|
+
});
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
describe("probes", () => {
|
|
952
|
+
it("treats the current process as alive", () => {
|
|
953
|
+
expect(processIsAlive(process.pid)).toBe(true);
|
|
954
|
+
});
|
|
955
|
+
});
|