dotfiles
bugabingas dorkfiles
owned by bugabinga
fix(pi): ignore stale extension contexts
cd8f98862b2f8cf994d8e55d61cd0f95a8c1dafc
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (2)
pi/agent/extensions/git-status/index.ts
Modified+47 / −36 text lines
101101 return `${status.branch}${statusBlock ? ` ${statusBlock}` : ""}`; return `${status.branch}${statusBlock ? ` ${statusBlock}` : ""}`; 104-function clearSegments(pi: ExtensionAPI) { 105- pi.events.emit("footer:segment", { id: "cwd", text: undefined }); 106- pi.events.emit("footer:segment", { id: "git-branch", text: undefined }); 104+function isStaleContextError(error: unknown): boolean { 105+ return error instanceof Error && error.message.includes("extension ctx is stale"); 109-function emitGit(pi: ExtensionAPI, ctx: ExtensionContext, status: GitStatus) { 108+function ignoreStaleContext(fn: () => void) { 110+ catch (error) { if (!isStaleContextError(error)) throw error; } 113+function clearSegments(pi: ExtensionAPI) { 114+ ignoreStaleContext(() => pi.events.emit("footer:segment", { id: "cwd", text: undefined })); 115+ ignoreStaleContext(() => pi.events.emit("footer:segment", { id: "git-branch", text: undefined })); 118+function cwdLabel(cwd: string): string { 110119 const home = process.env.HOME; const home = process.env.HOME; 111- const path = home && ctx.cwd.startsWith(home) ? `~${ctx.cwd.slice(home.length)}` : ctx.cwd; 120+ return home && cwd.startsWith(home) ? `~${cwd.slice(home.length)}` : cwd; 123+function emitGit(pi: ExtensionAPI, ctx: ExtensionContext, cwd: string, status: GitStatus) { 124+ const path = cwdLabel(cwd); 112125 const branch = gitLabel(status); const branch = gitLabel(status); 114127 if (hasFooter()) { if (hasFooter()) { 115- ctx.ui.setStatus(CWD_STATUS_ID, undefined); 116- ctx.ui.setStatus(GIT_STATUS_ID, undefined); 117- pi.events.emit("footer:segment", { id: "cwd", text: path, icon: "", color: "syntaxOperator", zone: "workspace", order: 0 }); 128+ ignoreStaleContext(() => ctx.ui.setStatus(CWD_STATUS_ID, undefined)); 129+ ignoreStaleContext(() => ctx.ui.setStatus(GIT_STATUS_ID, undefined)); 130+ ignoreStaleContext(() => pi.events.emit("footer:segment", { id: "cwd", text: path, icon: "", color: "syntaxOperator", zone: "workspace", order: 0 })); 118131 if (branch) { if (branch) { 119- pi.events.emit("footer:segment", { 132+ ignoreStaleContext(() => pi.events.emit("footer:segment", { 120133 id: "git-branch", id: "git-branch", 121134 text: branch, text: branch, 122135 icon: ICONS.git, icon: ICONS.git, 123136 color: "syntaxKeyword", color: "syntaxKeyword", 124137 zone: "workspace", zone: "workspace", 125138 order: 1, order: 1, 128- pi.events.emit("footer:segment", { id: "git-branch", text: undefined }); 141+ ignoreStaleContext(() => pi.events.emit("footer:segment", { id: "git-branch", text: undefined })); 133146 clearSegments(pi); clearSegments(pi); 134- ctx.ui.setStatus(CWD_STATUS_ID, path); 135- ctx.ui.setStatus(GIT_STATUS_ID, branch); 147+ ignoreStaleContext(() => ctx.ui.setStatus(CWD_STATUS_ID, path)); 148+ ignoreStaleContext(() => ctx.ui.setStatus(GIT_STATUS_ID, branch)); 138151export default function gitStatusExtension(pi: ExtensionAPI) {export default function gitStatusExtension(pi: ExtensionAPI) { 139- let currentCwd = ""; 140- let currentCtx: ExtensionContext | undefined; 141153 let hasUI = false; let hasUI = false; 142- let refreshInFlight = false; 143- let refreshPending = false; 145- const refresh = async () => { 146- if (!hasUI || !currentCtx || refreshInFlight) { refreshPending = true; return; } 147- const ctx = currentCtx; 148- refreshInFlight = true; 150- const status = await readGitStatus(currentCwd); 151- emitGit(pi, ctx, status); 153- refreshInFlight = false; 154- if (refreshPending) { refreshPending = false; refresh(); } 155+ const refresh = async (ctx: ExtensionContext) => { 156+ const ticket = generation; 158+ try { cwd = ctx.cwd; } 159+ catch (error) { if (isStaleContextError(error)) return; throw error; } 162+ const status = await readGitStatus(cwd); 163+ if (ticket !== generation) return; 164+ emitGit(pi, ctx, cwd, status); 158- pi.on("session_start", async (_e, ctx) => { hasUI = ctx.hasUI; currentCwd = ctx.cwd; currentCtx = ctx; refresh(); }); 159- pi.on("agent_end", async () => { if (hasUI) refresh(); }); 160- pi.on("tool_execution_end", async () => { if (hasUI) refresh(); }); 167+ pi.on("session_start", async (_e, ctx) => { generation++; hasUI = ctx.hasUI; void refresh(ctx); }); 168+ pi.on("agent_end", async (_e, ctx) => { if (hasUI) void refresh(ctx); }); 169+ pi.on("tool_execution_end", async (_e, ctx) => { if (hasUI) void refresh(ctx); }); 161170 pi.on("session_shutdown", async (_e, ctx) => { pi.on("session_shutdown", async (_e, ctx) => { 162172 clearSegments(pi); clearSegments(pi); 164- ctx.ui.setStatus(CWD_STATUS_ID, undefined); 165- ctx.ui.setStatus(GIT_STATUS_ID, undefined); 167- currentCtx = undefined; 173+ ignoreStaleContext(() => { 175+ ctx.ui.setStatus(CWD_STATUS_ID, undefined); 176+ ctx.ui.setStatus(GIT_STATUS_ID, undefined); pi/agent/extensions/runtime/index.ts
Modified+44 / −38 text lines
172172 return undefined; return undefined; 175+function isStaleContextError(error: unknown): boolean { 176+ return error instanceof Error && error.message.includes("extension ctx is stale"); 179+function ignoreStaleContext(fn: () => void) { 181+ catch (error) { if (!isStaleContextError(error)) throw error; } 175184export default function runtimeExtension(pi: ExtensionAPI) {export default function runtimeExtension(pi: ExtensionAPI) { 176- let currentCwd = ""; 177- let currentCtx: ExtensionContext | undefined; 178186 let hasUI = false; let hasUI = false; 179- let refreshInFlight = false; 181188 function clearSegment() { function clearSegment() { 182- pi.events.emit("footer:segment", { id: SEGMENT_ID, text: undefined }); 189+ ignoreStaleContext(() => pi.events.emit("footer:segment", { id: SEGMENT_ID, text: undefined })); 185- async function refresh() { 186- if (!hasUI || !currentCtx || refreshInFlight) return; 187- const ctx = currentCtx; 188- refreshInFlight = true; 192+ async function refresh(ctx: ExtensionContext) { 193+ const ticket = generation; 195+ try { cwd = ctx.cwd; } 196+ catch (error) { if (isStaleContextError(error)) return; throw error; } 191- const rt = await detect(currentCwd); 194- ctx.ui.setStatus(STATUS_ID, undefined); 198- const text = rt.version ?? rt.name; 200- ctx.ui.setStatus(STATUS_ID, undefined); 201- pi.events.emit("footer:segment", { 205- color: RUNTIME_COLORS[rt.name], 207- order: SEGMENT_ORDER, 199+ const rt = await detect(cwd); 200+ if (ticket !== generation) return; 212203 clearSegment(); clearSegment(); 213- ctx.ui.setStatus(STATUS_ID, `${rt.icon} ${text}`); 215- refreshInFlight = false; 204+ ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, undefined)); 208+ const text = rt.version ?? rt.name; 210+ ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, undefined)); 211+ ignoreStaleContext(() => pi.events.emit("footer:segment", { 215+ color: RUNTIME_COLORS[rt.name], 217+ order: SEGMENT_ORDER, 223+ ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, `${rt.icon} ${text}`)); 219226 pi.on("session_start", async (_e, ctx) => { pi.on("session_start", async (_e, ctx) => { 220228 hasUI = ctx.hasUI; hasUI = ctx.hasUI; 221- currentCwd = ctx.cwd; 226- pi.on("agent_end", async () => refresh()); 232+ pi.on("agent_end", async (_e, ctx) => { if (hasUI) void refresh(ctx); }); 227233 pi.on("session_shutdown", async (_e, ctx) => { pi.on("session_shutdown", async (_e, ctx) => { 228235 clearSegment(); clearSegment(); 229- if (ctx.hasUI) ctx.ui.setStatus(STATUS_ID, undefined); 230- currentCtx = undefined; 236+ ignoreStaleContext(() => { if (ctx.hasUI) ctx.ui.setStatus(STATUS_ID, undefined); });