pi-ext
bugabingas pi extensions
owned by bugabinga
Guard Pulse async state persistence
76b89f24ac651a9244d65709ec654ae27f395899
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (4)
4 + "path":
"CHANGELOG.md" 5 +- Restyle autoresearch dashboard with the Nugu diff-review palette, theme selector, cards, side drawer, chart, and live session.jsonl refresh
6 +- Add `.pi/autoresearch/` to `.gitignore`
10 +- Bump package.json from 0.0.2 to 0.0.3
11 +- Fix Pulse stale session crashes by persisting state through the fresh command context
12 +- Guard Pulse timers, config watchers, and async checks with session generation tickets
13 +- Reset Pulse checking state on session start and shutdown
5 17 - Bump package.json from 0.0.1 to 0.0.2
extensions/pulse/__tests__/index.test.ts
Modified 84 + appendCustomEntry(customType: string, data: unknown) {
85 + entries.
push({ type:
"custom", customType, data });
86 + return String(entries.
length);
244 + it(
"persists run state through the fresh command context, not captured pi",
async ()
=> {
245 + const root
= cwd();
246 + write(
root,
"README.md",
"old");
247 + writeConfig(
root, { documents: [{ path:
"README.md" }] });
248 + completionText
= JSON.
stringify({ updates: [] });
249 + const h
= harness(
root);
250 + h.pi.
appendEntry = ()
=> {
251 + throw new Error(
"stale pi appendEntry");
254 + await h.commands.
get(
"pulse").
handler(
"force", h.ctx);
256 + expect(h.entries.
at(
-1)).
toEqual({
258 + customType:
"pulse-state",
261 + lastRunAt: expect.
any(
Number),
262 + lastRunReason:
"manual",
263 + lastResult:
"up to date",
239 268 it(
"rejects model proposals for untracked paths",
async ()
=> {
240 269 const root
= cwd();
241 270 write(
root,
"README.md",
"old");
extensions/pulse/index.ts
Modified 182 182 lastResult: string;
185 +type WritableSessionManager = ExtensionCommandContext[
"sessionManager"]
& {
186 + appendCustomEntry(customType
: string, data
?: unknown)
: string;
185 189 function isPulsePersistedState(value
: unknown)
: value
is PulsePersistedState {
186 190 if (
typeof value
!== "object" || value
=== null)
return false;
187 191 const v
= value
as Partial<
PulsePersistedState>;
613 617 function startTimer(ctx
: ExtensionCommandContext)
: void {
619 + const ticket
= generation;
615 620 state.lastTimerCheck
= Date.
now();
616 621 state.timerHandle
= setInterval(()
=> {
617 - void maybeRunAutoCheck(ctx);
622 + if (ticket
!== generation)
return;
623 + void maybeRunAutoCheck(ctx, ticket);
618 624 }, TIMER_TICK_MS);
639 645 function startConfigWatcher(ctx
: ExtensionCommandContext)
: void {
640 646 stopConfigWatcher();
647 + const ticket
= generation;
641 648 const configPath
= path.
join(state.cwd, PULSE_CONFIG);
642 649 if (
!fs.
existsSync(configPath))
return;
643 650 state.configWatcherCwd
= state.cwd;
647 654 if (watcherDebounce)
clearTimeout(watcherDebounce);
648 655 watcherDebounce
= setTimeout(()
=> {
649 656 watcherDebounce
= null;
657 + if (ticket
!== generation)
return;
650 658 if (state.cwd
!== state.configWatcherCwd)
return;
651 659 const reloaded
= loadConfig(state.cwd);
652 660 if (
!reloaded)
return;
739 747 return state.config.documents;
742 - function persistState()
: void {
743 - pi.
appendEntry?.(PULSE_STATE_ENTRY, {
745 - lastRunAt: state.lastRunAt,
746 - lastRunReason: state.lastRunReason,
747 - lastResult: state.lastResult,
748 - } satisfies PulsePersistedState);
750 + function persistState(ctx
: ExtensionCommandContext)
: void {
751 + (ctx.sessionManager
as WritableSessionManager).
appendCustomEntry(
755 + lastRunAt: state.lastRunAt,
756 + lastRunReason: state.lastRunReason,
757 + lastResult: state.lastResult,
758 + } satisfies PulsePersistedState,
751 762 function restoreState(ctx
: ExtensionCommandContext)
: void {
803 814 ctx: ExtensionCommandContext,
805 816 reason
= manual
? "manual" : "auto",
817 + ticket
= generation,
806 818 ):
Promise<void> {
819 + if (ticket !== generation) return;
807 820 if (state.isChecking) return;
821 + const runCwd
= state.cwd;
822 + const runConfig
= state.config;
808 823 state.isChecking
= true;
809 824 state.lastRunReason
= reason;
810 825 state.lastResult
= "running";
813 - const specs
= getTrackedDocs();
814 - const docs
= expandPulseDocuments(state.cwd, specs);
828 + const specs
= state.isTracking
? runConfig.documents
: [];
829 + const docs
= expandPulseDocuments(runCwd, specs);
815 830 const loadedDocs
= docs
816 - .map((d) =>
loadPulseDoc(state.cwd, d))
831 + .map((d) =>
loadPulseDoc(runCwd, d))
817 832 .
filter((d)
: d
is LoadedPulseDoc => d !==
null);
818 833 if (loadedDocs.length === 0) {
819 834 state.lastResult
= "no files";
824 - const defaultModel
= state.config.model
825 - ? parseModel(state.config.model)
839 + const defaultModel
= runConfig.model
840 + ? parseModel(runConfig.model)
827 842 if (
!defaultModel) {
828 843 state.lastResult
= "no model";
850 865 const auth
= await ctx.modelRegistry.
getApiKeyAndHeaders(
851 866 model
as Parameters<
typeof ctx.modelRegistry.getApiKeyAndHeaders>[
0],
868 + if (ticket
!== generation)
return;
853 869 if (
!auth.ok
|| !auth.apiKey) {
854 870 missingAuthModels.
push(
`${model.provider}/${model.id}`);
884 + if (ticket
!== generation)
return;
868 885 const updates
= await applyModeUpdates(
872 - state.config.apply,
891 + if (ticket
!== generation)
return;
874 892 if (proposed.
length > 0 && updates.
length === 0) skipped
= true;
875 - applied.
push(
...applyDocUpdates(state.cwd, loadedGroup, updates));
893 + applied.
push(
...applyDocUpdates(runCwd, loadedGroup, updates));
878 896 state.lastResult
= e
instanceof Error ? e.message
: String(e);
886 - state.lastGitFingerprint
= gitFingerprint(state.cwd);
904 + state.lastGitFingerprint
= gitFingerprint(runCwd);
887 905 state.lastUpdateTime
= Date.
now();
888 906 state.lastRunAt
= state.lastUpdateTime;
912 930 if (manual) ctx.ui.
notify(
"Pulse: up to date",
"info");
933 + if (ticket
!== generation)
return;
915 934 if (state.lastResult
=== "running") state.lastResult
= "done";
916 935 if (state.lastRunAt
=== 0 || state.lastResult
!== "never")
917 936 state.lastRunAt
= Date.
now();
919 938 state.isChecking
= false;
924 943 async function maybeRunAutoCheck(
925 944 ctx
: ExtensionCommandContext,
945 + ticket
= generation,
926 946 )
: Promise<
void> {
947 + if (ticket
!== generation)
return;
928 949 state.isPaused
|| 929 950 !state.isTracking
|| 954 975 if (intervalReady) state.lastTimerCheck
= now;
955 - await runPulseCheck(ctx,
false, dirtyReady
? "changes" : "periodic");
976 + await runPulseCheck(ctx,
false, dirtyReady
? "changes" : "periodic", ticket);
958 979 function removeDoc(target
: string)
: { removed
: boolean; path
?: string } {
1487 1508 state.lastGitFingerprint
= gitFingerprint(state.cwd);
1489 1510 state.isPaused
= false;
1511 + state.isChecking
= false;
1490 1512 restoreState(ctx
as any);
1492 1514 startConfigWatcher(ctx
as any);
1499 1521 pi.
on(
"session_shutdown",
async (_event, _ctx)
=> {
1523 + state.isChecking
= false;
1501 1524 stopConfigWatcher();