repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pulse: persist run status

78f71b23c493eb8330cd3b7bdc4ec09cac33f949

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

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

Modified
41 41 const statuses: Array<{ key: string; value: string | undefined }> = [];
42 42 const confirmations: string[] = [];
43 43 const editors: Array<{ title: string; prefill: string }> = [];
44 + const entries: any[] = [];
44 45 let confirmResult = true;
45 46 let editorResult: string | undefined;
46 47 const ctx: any = {
70 71 sessionManager: {
71 72 getBranch() {
72 - return [];
73 + return entries;
75 76 isIdle() {
84 85 on(name: string, handler: unknown) {
85 86 events.set(name, handler);
88 + appendEntry(customType: string, data: unknown) {
89 + entries.push({ type: "custom", customType, data });
90 + },
88 92 pulse(pi);
89 93 return {
94 98 statuses,
95 99 confirmations,
96 100 editors,
101 + entries,
97 102 setConfirmResult(value: boolean) { confirmResult = value; },
98 103 setEditorResult(value: string | undefined) { editorResult = value; },
260 265 expect(fs.readFileSync(path.join(root, "README.md"), "utf8")).toBe("new");
261 266 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 + });
262 272 await h.events.get("session_shutdown")({}, h.ctx);
263 273 } finally {
264 274 Date.now = realNow;
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 + });

pi/agent/extensions/pulse/index.ts

Modified
26 26 } from "./core.ts";
28 28 const PULSE_CONFIG = ".pi/pulse.json";
29 +const PULSE_STATE_ENTRY = "pulse-state";
29 30 const DEFAULT_INTERVAL_MIN = 10;
30 31 const COOLDOWN_MS = 5 * 60 * 1000;
31 32 const QUIET_WINDOW_MS = 60 * 1000;
131 132 summary: string;
135 +interface PulsePersistedState {
136 + version: 1;
137 + lastRunAt: number;
138 + lastRunReason: string;
139 + lastResult: string;
142 +function 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";
134 148 function sha256(s: string): string {
135 149 return createHash("sha256").update(s).digest("hex");
602 616 return state.config.documents;
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);
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;
638 + state.lastRunAt = 0;
639 + state.lastRunReason = "";
640 + state.lastResult = "never";
605 643 function groupDocsByModel(
606 644 docs: PulseDoc[],
607 645 defaultModel: { provider: string; id: string } | undefined,
705 743 } finally {
706 744 if (state.lastResult === "running") state.lastResult = "done";
707 745 if (state.lastRunAt === 0 || state.lastResult !== "never") state.lastRunAt = Date.now();
746 + persistState();
708 747 state.isChecking = false;
709 748 setStatus(ctx);
1148 1187 state.lastMutationAt = 0;
1149 1188 state.lastGitFingerprint = gitFingerprint(state.cwd);
1150 1189 state.isPaused = false;
1190 + restoreState(ctx);
1152 1192 startConfigWatcher(ctx);
1153 1193 if (state.isTracking && state.config.documents.length > 0) {