From 0f3496d9cd5baa12b261e4878070553ff920fd0c Mon Sep 17 00:00:00 2001 From: zm2231 Date: Wed, 17 Jun 2026 23:24:44 -0400 Subject: [PATCH] fix: await session_start to prevent stale ctx access Fire-and-forget onSession() could resolve after a session was replaced, then read ctx.cwd / call ctx.ui.notify on the now-stale ctx and trigger ExtensionRunner.assertActive throw. Await the handler so the runner serializes init before any replacement; log via console.error in the catch so the error path doesn't access ctx either. Surfaces on @earendil-works/pi-coding-agent >=0.74. Tested at 0.79.6. --- src/index.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index d3753d0..22fcaa6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -270,10 +270,13 @@ export default function(pi: ExtensionAPI) { } } - pi.on('session_start', (_event: unknown, ctx: ExtensionContext) => { - void onSession(ctx).catch(err => { - ctx.ui.notify(`GitNexus session init failed: ${err.message}`, 'error'); - }); + pi.on('session_start', async (_event: unknown, ctx: ExtensionContext) => { + try { + await onSession(ctx); + } catch (err) { + // Don't touch ctx here — it may be stale if onSession ran past replacement. + console.error('[pi-gitnexus] session init failed:', err instanceof Error ? err.message : String(err)); + } }); pi.on('session_shutdown', () => {