repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

fix(pi): ignore stale extension contexts

4729ffe99ad66cbfc0007a4335279f653f09de45

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

pi/agent/extensions/git-status/index.ts

Modified
101 101 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) {
109 + try { fn(); }
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 {
110 119 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);
112 125 const branch = gitLabel(status);
114 127 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 }));
118 131 if (branch) {
119 - pi.events.emit("footer:segment", {
132 + ignoreStaleContext(() => pi.events.emit("footer:segment", {
120 133 id: "git-branch",
121 134 text: branch,
122 135 icon: ICONS.git,
123 136 color: "syntaxKeyword",
124 137 zone: "workspace",
125 138 order: 1,
126 - });
139 + }));
127 140 } else {
128 - pi.events.emit("footer:segment", { id: "git-branch", text: undefined });
141 + ignoreStaleContext(() => pi.events.emit("footer:segment", { id: "git-branch", text: undefined }));
130 143 return;
133 146 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));
138 151 export default function gitStatusExtension(pi: ExtensionAPI) {
139 - let currentCwd = "";
140 - let currentCtx: ExtensionContext | undefined;
152 + let generation = 0;
141 153 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;
149 - try {
150 - const status = await readGitStatus(currentCwd);
151 - emitGit(pi, ctx, status);
152 - } finally {
153 - refreshInFlight = false;
154 - if (refreshPending) { refreshPending = false; refresh(); }
155 - }
155 + const refresh = async (ctx: ExtensionContext) => {
156 + const ticket = generation;
157 + let cwd: string;
158 + try { cwd = ctx.cwd; }
159 + catch (error) { if (isStaleContextError(error)) return; throw error; }
160 + if (!hasUI) return;
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); });
161 170 pi.on("session_shutdown", async (_e, ctx) => {
171 + generation++;
162 172 clearSegments(pi);
163 - if (ctx.hasUI) {
164 - ctx.ui.setStatus(CWD_STATUS_ID, undefined);
165 - ctx.ui.setStatus(GIT_STATUS_ID, undefined);
166 - }
167 - currentCtx = undefined;
173 + ignoreStaleContext(() => {
174 + if (ctx.hasUI) {
175 + ctx.ui.setStatus(CWD_STATUS_ID, undefined);
176 + ctx.ui.setStatus(GIT_STATUS_ID, undefined);
178 + });

pi/agent/extensions/runtime/index.ts

Modified
172 172 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) {
180 + try { fn(); }
181 + catch (error) { if (!isStaleContextError(error)) throw error; }
175 184 export default function runtimeExtension(pi: ExtensionAPI) {
176 - let currentCwd = "";
177 - let currentCtx: ExtensionContext | undefined;
185 + let generation = 0;
178 186 let hasUI = false;
179 - let refreshInFlight = false;
181 188 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;
194 + let cwd: string;
195 + try { cwd = ctx.cwd; }
196 + catch (error) { if (isStaleContextError(error)) return; throw error; }
197 + if (!hasUI) return;
190 - try {
191 - const rt = await detect(currentCwd);
192 - if (!rt) {
193 - clearSegment();
194 - ctx.ui.setStatus(STATUS_ID, undefined);
195 - return;
196 - }
198 - const text = rt.version ?? rt.name;
199 - if (hasFooter()) {
200 - ctx.ui.setStatus(STATUS_ID, undefined);
201 - pi.events.emit("footer:segment", {
202 - id: SEGMENT_ID,
203 - icon: rt.icon,
204 - text,
205 - color: RUNTIME_COLORS[rt.name],
206 - zone: SEGMENT_ZONE,
207 - order: SEGMENT_ORDER,
208 - });
209 - return;
210 - }
199 + const rt = await detect(cwd);
200 + if (ticket !== generation) return;
202 + if (!rt) {
212 203 clearSegment();
213 - ctx.ui.setStatus(STATUS_ID, `${rt.icon} ${text}`);
214 - } finally {
215 - refreshInFlight = false;
204 + ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, undefined));
205 + return;
208 + const text = rt.version ?? rt.name;
209 + if (hasFooter()) {
210 + ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, undefined));
211 + ignoreStaleContext(() => pi.events.emit("footer:segment", {
212 + id: SEGMENT_ID,
213 + icon: rt.icon,
214 + text,
215 + color: RUNTIME_COLORS[rt.name],
216 + zone: SEGMENT_ZONE,
217 + order: SEGMENT_ORDER,
218 + }));
219 + return;
222 + clearSegment();
223 + ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, `${rt.icon} ${text}`));
219 226 pi.on("session_start", async (_e, ctx) => {
227 + generation++;
220 228 hasUI = ctx.hasUI;
221 - currentCwd = ctx.cwd;
222 - currentCtx = ctx;
223 - refresh();
229 + void refresh(ctx);
226 - pi.on("agent_end", async () => refresh());
232 + pi.on("agent_end", async (_e, ctx) => { if (hasUI) void refresh(ctx); });
227 233 pi.on("session_shutdown", async (_e, ctx) => {
234 + generation++;
228 235 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); });