repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pulse: persist run status

a9a03dd82a7a5e23c6949dff21293cad0a6d5436

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+68 / −1 text lines

Diff layout

pi/agent/extensions/pulse/__tests__/index.test.ts

Modified

+28 / −1 text lines

4141 const statuses: Array<{ key: string; value: string | undefined }> = [];
4242 const confirmations: string[] = [];
4343 const editors: Array<{ title: string; prefill: string }> = [];
44 const entries: any[] = [];
4445 let confirmResult = true;
4546 let editorResult: string | undefined;
4647 const ctx: any = {
6970 },
7071 sessionManager: {
7172 getBranch() {
72 return [];
73 return entries;
7374 },
7475 },
7576 isIdle() {
8485 on(name: string, handler: unknown) {
8586 events.set(name, handler);
8687 },
88 appendEntry(customType: string, data: unknown) {
89 entries.push({ type: "custom", customType, data });
90 },
8791 };
8892 pulse(pi);
8993 return {
9498 statuses,
9599 confirmations,
96100 editors,
101 entries,
97102 setConfirmResult(value: boolean) { confirmResult = value; },
98103 setEditorResult(value: string | undefined) { editorResult = value; },
99104 };
260265 expect(fs.readFileSync(path.join(root, "README.md"), "utf8")).toBe("new");
261266 expect(h.notifications.some((n) => n.message.includes("updated 1 doc"))).toBe(true);
267 expect(h.entries.at(-1)).toEqual({
268 type: "custom",
269 customType: "pulse-state",
270 data: { version: 1, lastRunAt: now, lastRunReason: "changes", lastResult: "updated 1" },
271 });
262272 await h.events.get("session_shutdown")({}, h.ctx);
263273 } finally {
264274 Date.now = realNow;
265275 }
266276 });
278 it("restores persisted last status on session_start", async () => {
279 const root = cwd();
280 write(root, "README.md", "old");
281 writeConfig(root, { documents: [{ path: "README.md" }] });
282 const h = harness(root);
283 h.entries.push({
284 type: "custom",
285 customType: "pulse-state",
286 data: { version: 1, lastRunAt: 123, lastRunReason: "periodic", lastResult: "up to date" },
287 });
289 await h.events.get("session_start")({}, h.ctx);
290 await h.commands.get("pulse").handler("status", h.ctx);
292 expect(h.notifications.some((n) => n.message.includes("last: up to date (periodic)"))).toBe(true);
293 });
267294});

pi/agent/extensions/pulse/index.ts

Modified

+40 / −0 text lines

2626} from "./core.ts";
2828const PULSE_CONFIG = ".pi/pulse.json";
29const PULSE_STATE_ENTRY = "pulse-state";
2930const DEFAULT_INTERVAL_MIN = 10;
3031const COOLDOWN_MS = 5 * 60 * 1000;
3132const QUIET_WINDOW_MS = 60 * 1000;
131132 summary: string;
135interface PulsePersistedState {
136 version: 1;
137 lastRunAt: number;
138 lastRunReason: string;
139 lastResult: string;
140}
142function isPulsePersistedState(value: unknown): value is PulsePersistedState {
143 if (typeof value !== "object" || value === null) return false;
144 const v = value as Partial<PulsePersistedState>;
145 return v.version === 1 && typeof v.lastRunAt === "number" && typeof v.lastRunReason === "string" && typeof v.lastResult === "string";
146}
134148function sha256(s: string): string {
135149 return createHash("sha256").update(s).digest("hex");
602616 return state.config.documents;
603617 }
619 function persistState(): void {
620 pi.appendEntry?.(PULSE_STATE_ENTRY, {
621 version: 1,
622 lastRunAt: state.lastRunAt,
623 lastRunReason: state.lastRunReason,
624 lastResult: state.lastResult,
625 } satisfies PulsePersistedState);
626 }
628 function restoreState(ctx: ExtensionCommandContext): void {
629 for (const entry of ctx.sessionManager.getBranch().reverse()) {
630 if ((entry as { type?: string }).type !== "custom" || (entry as { customType?: string }).customType !== PULSE_STATE_ENTRY) continue;
631 const data = (entry as { data?: unknown }).data;
632 if (!isPulsePersistedState(data)) continue;
633 state.lastRunAt = data.lastRunAt;
634 state.lastRunReason = data.lastRunReason;
635 state.lastResult = data.lastResult;
636 return;
637 }
638 state.lastRunAt = 0;
639 state.lastRunReason = "";
640 state.lastResult = "never";
641 }
605643 function groupDocsByModel(
606644 docs: PulseDoc[],
607645 defaultModel: { provider: string; id: string } | undefined,
705743 } finally {
706744 if (state.lastResult === "running") state.lastResult = "done";
707745 if (state.lastRunAt === 0 || state.lastResult !== "never") state.lastRunAt = Date.now();
746 persistState();
708747 state.isChecking = false;
709748 setStatus(ctx);
710749 }
11481187 state.lastMutationAt = 0;
11491188 state.lastGitFingerprint = gitFingerprint(state.cwd);
11501189 state.isPaused = false;
1190 restoreState(ctx);
11521192 startConfigWatcher(ctx);
11531193 if (state.isTracking && state.config.documents.length > 0) {