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
Modified101 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) {
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 }));
119 - pi.events.
emit(
"footer:segment", {
132 + ignoreStaleContext(()
=> pi.events.
emit(
"footer:segment", {
120 133 id:
"git-branch",
123 136 color:
"syntaxKeyword",
124 137 zone:
"workspace",
128 - pi.events.
emit(
"footer:segment", { id:
"git-branch", text:
undefined });
141 + ignoreStaleContext(()
=> pi.events.
emit(
"footer:segment", { id:
"git-branch", text:
undefined }));
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;
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; }
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)
=> {
162 172 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
Modified172 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) {
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;
195 + try { cwd
= ctx.cwd; }
196 + catch (error) {
if (
isStaleContextError(error))
return;
throw error; }
197 + if (
!hasUI)
return;
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],
206 - zone: SEGMENT_ZONE,
207 - order: SEGMENT_ORDER,
199 + const rt
= await detect(cwd);
200 + if (ticket
!== generation)
return;
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],
216 + zone: SEGMENT_ZONE,
217 + order: SEGMENT_ORDER,
223 + ignoreStaleContext(()
=> ctx.ui.
setStatus(STATUS_ID,
`${rt.icon} ${text}`));
219 226 pi.
on(
"session_start",
async (_e, ctx)
=> {
220 228 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); });
227 233 pi.
on(
"session_shutdown",
async (_e, ctx)
=> {
229 - if (ctx.hasUI) ctx.ui.
setStatus(STATUS_ID,
undefined);
230 - currentCtx
= undefined;
236 + ignoreStaleContext(()
=> {
if (ctx.hasUI) ctx.ui.
setStatus(STATUS_ID,
undefined); });