repositories / dotfiles

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)

+91 / −74 text lines

Diff layout

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

Modified

+47 / −36 text lines

101101 return `${status.branch}${statusBlock ? ` ${statusBlock}` : ""}`;
104function clearSegments(pi: ExtensionAPI) {
105 pi.events.emit("footer:segment", { id: "cwd", text: undefined });
106 pi.events.emit("footer:segment", { id: "git-branch", text: undefined });
104function isStaleContextError(error: unknown): boolean {
105 return error instanceof Error && error.message.includes("extension ctx is stale");
109function emitGit(pi: ExtensionAPI, ctx: ExtensionContext, status: GitStatus) {
108function ignoreStaleContext(fn: () => void) {
109 try { fn(); }
110 catch (error) { if (!isStaleContextError(error)) throw error; }
111}
113function 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 }));
116}
118function cwdLabel(cwd: string): string {
110119 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;
121}
123function emitGit(pi: ExtensionAPI, ctx: ExtensionContext, cwd: string, status: GitStatus) {
124 const path = cwdLabel(cwd);
112125 const branch = gitLabel(status);
114127 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) {
119 pi.events.emit("footer:segment", {
132 ignoreStaleContext(() => pi.events.emit("footer:segment", {
120133 id: "git-branch",
121134 text: branch,
122135 icon: ICONS.git,
123136 color: "syntaxKeyword",
124137 zone: "workspace",
125138 order: 1,
126 });
139 }));
127140 } else {
128 pi.events.emit("footer:segment", { id: "git-branch", text: undefined });
141 ignoreStaleContext(() => pi.events.emit("footer:segment", { id: "git-branch", text: undefined }));
129142 }
130143 return;
131144 }
133146 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) {
139 let currentCwd = "";
140 let currentCtx: ExtensionContext | undefined;
152 let generation = 0;
141153 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);
156165 };
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) => {
171 generation++;
162172 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);
177 }
178 });
168179 });

pi/agent/extensions/runtime/index.ts

Modified

+44 / −38 text lines

172172 return undefined;
175function isStaleContextError(error: unknown): boolean {
176 return error instanceof Error && error.message.includes("extension ctx is stale");
177}
179function ignoreStaleContext(fn: () => void) {
180 try { fn(); }
181 catch (error) { if (!isStaleContextError(error)) throw error; }
182}
175184export default function runtimeExtension(pi: ExtensionAPI) {
176 let currentCwd = "";
177 let currentCtx: ExtensionContext | undefined;
185 let generation = 0;
178186 let hasUI = false;
179 let refreshInFlight = false;
181188 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 }));
183190 }
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) {
212203 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;
216206 }
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;
220 }
222 clearSegment();
223 ignoreStaleContext(() => ctx.ui.setStatus(STATUS_ID, `${rt.icon} ${text}`));
217224 }
219226 pi.on("session_start", async (_e, ctx) => {
227 generation++;
220228 hasUI = ctx.hasUI;
221 currentCwd = ctx.cwd;
222 currentCtx = ctx;
223 refresh();
229 void refresh(ctx);
224230 });
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) => {
234 generation++;
228235 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); });
231237 });