Skip to content

Commit 3875d00

Browse files
sahrizviclaude
andcommitted
fix(onboarding): repair regressions the first round of bot fixes introduced
The close-handler fix in `fe6ac08` inverted the metric it was meant to repair, and two others had side effects. Found by `cubic` on the pushed head and by a class-scoped `codex` sweep. - `dialog-scan-gate.tsx` / `app.tsx`: `run()` calls `dialog.clear()` before `props.onChoose(arg)`, and `dialog.clear()` invokes the close handler synchronously — so the latched `dismissed` recorder consumed EVERY Yes/No and the funnel reported a dismissal for every real choice. New `onOutcome` seam, invoked before the clear on both the choice and dismiss paths; `onChoose` keeps its position after the clear so the prompt-submit ordering is unchanged. - `app.tsx`: dropped `markFirstRunActive()` from the pre-completed-setup branch. Its only clear is `markSetupComplete()`, which has already run there, so the flag latched true for the session and every later `/model` pick would have emitted funnel events. - `telemetry/index.ts`: publishing `initPromise = reinitPromise` made `doShutdown()`'s reset guard miss, leaving `initDone` true while `enabled` was already false — `track()` then silently DROPPED events in exactly the window the reinit path exists to protect. Reset `initDone` when publishing. - `telemetry/index.ts`: the shutdown budget was still not hard. One absolute deadline now spans the drain and the final flush, and a drain that expires aborts the in-flight request rather than letting the final flush chain behind the very request it gave up on — which is what made the timeout buy nothing. Retry write-back is suppressed during shutdown, since re-inserting into a buffer about to be cleared only ships those events under the next launch id. Tests. The existing scan-gate tests all passed against wiring that does not exist: `mountGate()` renders the gate as a plain child of `DialogProvider`, while production mounts it through `dialog.replace(fn, onClose)`. Added `mountGateAsApp()`, which reproduces the real wiring, plus three tests over it (scan, skip, escape). Added a shutdown-budget test with a hanging request. All four new tests were mutation-checked: they fail with their fix reverted, and the budget test hangs for the full 10s default without the abort. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_018SLUQF3xgZHsGZHSjxe7vb
1 parent a949fdc commit 3875d00

5 files changed

Lines changed: 257 additions & 9 deletions

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,13 @@ export namespace Telemetry {
14361436
let droppedEvents = 0
14371437
let initPromise: Promise<void> | undefined
14381438
let initDone = false
1439+
// altimate_change — shutdown needs to cancel a flush already in flight, not just stop waiting
1440+
// for it: `flush()` chains through inFlightFlush, so a timed-out drain still had its final
1441+
// flush queue behind the very request it gave up on. `shuttingDown` additionally suppresses
1442+
// the retry write-back, which would otherwise re-insert events into a buffer about to be
1443+
// cleared and ship them under the NEXT lifecycle.
1444+
let activeFlushAbort: AbortController | undefined
1445+
let shuttingDown = false
14391446
// altimate_change — in-flight shutdown, declared with the rest of the module state so init()
14401447
// above can consult it. See shutdown() for why concurrent shutdowns must be serialized.
14411448
let shutdownPromise: Promise<void> | undefined
@@ -1588,6 +1595,13 @@ export namespace Telemetry {
15881595
// settled — but before this chained doInit() finished — saw an empty initPromise and
15891596
// started a SECOND one, replacing the flush timer and racing over the buffer.
15901597
initPromise = reinitPromise
1598+
// Publishing it also makes the outgoing doShutdown()'s `initPromise === initPromiseAtShutdown`
1599+
// reset guard miss, which used to leave initDone stuck at `true` from the dying generation
1600+
// while shutdown had already set `enabled = false`. track()'s "initialized and disabled →
1601+
// drop" rule then silently discarded every event emitted during the reinit — the exact
1602+
// window this path exists to keep. Hand the new generation a pre-init state so those
1603+
// events buffer and flush once doInit() re-enables.
1604+
initDone = false
15911605
}
15921606
return reinitPromise
15931607
}
@@ -1736,6 +1750,7 @@ export namespace Telemetry {
17361750
}
17371751

17381752
const controller = new AbortController()
1753+
activeFlushAbort = controller
17391754
const timeout = setTimeout(() => controller.abort(), timeoutMs)
17401755
try {
17411756
const response = await fetch(appInsights.endpoint, {
@@ -1748,6 +1763,10 @@ export namespace Telemetry {
17481763
log.debug("telemetry flush failed", { status: response.status })
17491764
}
17501765
} catch {
1766+
// altimate_change — no write-back during shutdown. The buffer is cleared a few lines later
1767+
// regardless, so re-inserting here does not save these events; it only leaves them to be
1768+
// shipped by whatever lifecycle comes next, under a different launch id.
1769+
if (shuttingDown) return
17511770
// Re-add events that haven't been retried yet to avoid data loss
17521771
const retriable = events.filter((e) => !(e as any)._retried)
17531772
for (const e of retriable) {
@@ -1757,6 +1776,7 @@ export namespace Telemetry {
17571776
buffer.unshift(...retriable.slice(0, space))
17581777
} finally {
17591778
clearTimeout(timeout)
1779+
if (activeFlushAbort === controller) activeFlushAbort = undefined
17601780
}
17611781
}
17621782

@@ -1830,21 +1850,41 @@ export namespace Telemetry {
18301850
}
18311851
// altimate_change — drain any flush already running before the final one, so a timer batch
18321852
// in mid-request is not abandoned and cannot write back into a buffer we are about to clear.
1853+
const deadline = Date.now() + (timeoutMs ?? REQUEST_TIMEOUT_MS)
1854+
shuttingDown = true
18331855
if (inFlightFlush) {
18341856
// Bounded: a flush already in flight uses the DEFAULT request timeout, so awaiting it
18351857
// unbounded could burn 10s before the bounded flush below even starts — well past the 5s
18361858
// the TUI allows the whole shutdown RPC.
1859+
//
1860+
// One ABSOLUTE deadline covers the drain and the final flush together. Giving each the full
1861+
// `timeoutMs` let a slow in-flight request spend the whole budget and then hand the final
1862+
// flush a fresh copy of it, so shutdown could still take twice what the caller allowed and
1863+
// overrun the deadline that keeps the worker's buffer from being discarded.
18371864
let drainTimer: ReturnType<typeof setTimeout> | undefined
1865+
let drained = false
18381866
await Promise.race([
1839-
inFlightFlush.catch(() => {}),
1867+
inFlightFlush.catch(() => {}).then(() => {
1868+
drained = true
1869+
}),
18401870
new Promise<void>((resolve) => {
1841-
drainTimer = setTimeout(resolve, timeoutMs ?? REQUEST_TIMEOUT_MS)
1871+
drainTimer = setTimeout(resolve, Math.max(0, deadline - Date.now()))
18421872
}),
18431873
])
18441874
if (drainTimer) clearTimeout(drainTimer)
1875+
if (!drained) {
1876+
// Giving up on the wait is not enough: flush() chains onto inFlightFlush, so the final
1877+
// flush below would queue behind the very request the drain just abandoned and inherit
1878+
// its remaining runtime — up to the full default 10s on top of the budget. Abort it.
1879+
activeFlushAbort?.abort()
1880+
await inFlightFlush.catch(() => {})
1881+
}
18451882
}
1846-
await flush(timeoutMs)
1883+
// Whatever the drain left of the shared deadline, never below a floor that lets the request
1884+
// actually be made.
1885+
await flush(Math.max(250, deadline - Date.now()))
18471886
inFlightFlush = undefined
1887+
shuttingDown = false
18481888
enabled = false
18491889
appInsights = undefined
18501890
buffer = []

‎packages/opencode/test/altimate/telemetry/onboarding.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,64 @@ describe("cross-package event parity", () => {
455455
expect(true).toBe(true)
456456
})
457457
})
458+
459+
// ---------------------------------------------------------------------------
460+
// Shutdown budget
461+
// ---------------------------------------------------------------------------
462+
//
463+
// The TUI exit path passes a small budget precisely so quitting does not hang the shell. Before
464+
// this was enforced, a periodic flush already in flight ran on the DEFAULT 10s request timeout,
465+
// and shutdown's own flush chained onto it — so the budget bought nothing and exit could stall
466+
// for the full default instead.
467+
describe("shutdown budget", () => {
468+
afterEach(async () => {
469+
await Telemetry.shutdown()
470+
mock.restore()
471+
})
472+
473+
test("a slow in-flight flush cannot make shutdown exceed the caller's budget", async () => {
474+
const origDisabled = process.env.ALTIMATE_TELEMETRY_DISABLED
475+
const origCs = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
476+
const origHome = process.env.HOME
477+
const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "altimate-shutdown-test-"))
478+
process.env.HOME = tmpHome
479+
480+
// Never resolves on its own; only the AbortController can end it. This is the shape of a
481+
// blackholed network, which is what the budget exists for.
482+
const fetchMock = spyOn(global, "fetch").mockImplementation((async (_i: any, init: any) => {
483+
return new Promise((_resolve, reject) => {
484+
init?.signal?.addEventListener("abort", () => reject(new Error("aborted")))
485+
})
486+
}) as unknown as typeof fetch)
487+
488+
try {
489+
delete process.env.ALTIMATE_TELEMETRY_DISABLED
490+
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING =
491+
"InstrumentationKey=k;IngestionEndpoint=https://example.invalid"
492+
await Telemetry.init()
493+
494+
Telemetry.track({ type: "onboarding_started", timestamp: 1, session_id: "" })
495+
// Start a flush on the DEFAULT budget and leave it hanging, exactly as the 5s interval does.
496+
const hanging = Telemetry.flush()
497+
498+
Telemetry.track({ type: "scan_gate_shown", timestamp: 2, session_id: "" })
499+
const startedAt = Date.now()
500+
await Telemetry.shutdown({ timeoutMs: 300 })
501+
const elapsed = Date.now() - startedAt
502+
503+
// Generous ceiling: the point is that it is bounded by the 300ms budget (plus the 250ms
504+
// floor the final request gets) rather than by the 10s default the hanging request holds.
505+
expect(elapsed).toBeLessThan(3000)
506+
await hanging.catch(() => {})
507+
} finally {
508+
process.env.ALTIMATE_TELEMETRY_DISABLED = origDisabled ?? undefined
509+
if (origDisabled === undefined) delete process.env.ALTIMATE_TELEMETRY_DISABLED
510+
if (origCs !== undefined) process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = origCs
511+
else delete process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
512+
if (origHome !== undefined) process.env.HOME = origHome
513+
else delete process.env.HOME
514+
fs.rmSync(tmpHome, { recursive: true, force: true })
515+
fetchMock.mockRestore()
516+
}
517+
})
518+
})

‎packages/tui/src/app.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,11 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi
605605
// every launch and is only set by a setup the user completed during THIS one, so a genuine
606606
// returning user never trips this branch.
607607
if (setupComplete()) {
608-
markFirstRunActive()
608+
// Deliberately NOT markFirstRunActive(): its only clear is markSetupComplete(), which has
609+
// already run on this branch and will not run again, so setting it here would latch the
610+
// flag true for the rest of the session and make every later /model switch emit funnel
611+
// events. The three events below are emitted directly and do not consult it. The prompt
612+
// gate arms it instead, at the point the picker actually opens.
609613
scanGateShown = true
610614
trackOnboarding({ name: "onboarding_started" })
611615
trackOnboarding({ name: "onboarding_completed" })
@@ -672,12 +676,12 @@ function App(props: { onSnapshot?: () => Promise<string[]>; pluginHost: TuiPlugi
672676
dialog.replace(
673677
() => (
674678
<DialogScanGate
675-
onDismiss={() => recordScanChoice("dismissed")}
679+
onOutcome={(outcome) => recordScanChoice(outcome)}
676680
onChoose={(arg) => {
677681
// altimate_change — funnel: emitted here rather than inside the gate because the
678682
// dialog overlay renders outside the provider tree; `onChoose` is already the
679683
// established way to hand the gate something it cannot reach itself.
680-
recordScanChoice(arg)
684+
// Recorded by onOutcome above, which the gate calls before dialog.clear().
681685
// Yes → /onboard-connect scan; No → /onboard-connect skip.
682686
// Both branches now have a real follow-up: `scan` runs
683687
// project_scan and branches into the discovery UX; `skip`

‎packages/tui/src/component/dialog-scan-gate.tsx‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ import { useDialog } from "../ui/dialog"
1313
// `onChoose` is injected by App (which lives inside PromptRefProvider); the dialog
1414
// overlay is mounted above that provider, so the gate cannot resolve the prompt
1515
// ref itself — it must be handed in.
16-
export function DialogScanGate(props: { onChoose: (arg: "scan" | "skip") => void; onDismiss?: () => void }) {
16+
export function DialogScanGate(props: {
17+
onChoose: (arg: "scan" | "skip") => void
18+
/** altimate_change — funnel: the telemetry seam, deliberately separate from `onChoose`.
19+
*
20+
* It MUST be invoked before `dialog.clear()`. App registers a close handler on the dialog that
21+
* records a dismissal, and `dialog.clear()` runs that handler synchronously — so recording
22+
* after the clear (as `onChoose` is, to preserve the existing clear-then-submit ordering) loses
23+
* every real choice to the dismissal latch. */
24+
onOutcome?: (outcome: "scan" | "skip" | "dismissed") => void
25+
}) {
1726
const { theme } = useTheme()
1827
const dialog = useDialog()
1928
const [selected, setSelected] = createSignal(0) // 0 = Yes (default, per spec ❯)
@@ -31,13 +40,16 @@ export function DialogScanGate(props: { onChoose: (arg: "scan" | "skip") => void
3140
function dismiss() {
3241
if (chosen) return
3342
chosen = true
34-
props.onDismiss?.()
43+
props.onOutcome?.("dismissed")
3544
dialog.clear()
3645
}
3746

3847
function run(arg: "scan" | "skip") {
3948
if (chosen) return
4049
chosen = true
50+
// Before dialog.clear() — see onOutcome's note. onChoose stays after it so the prompt
51+
// submission still happens with the overlay already torn down, as it always has.
52+
props.onOutcome?.(arg)
4153
dialog.clear()
4254
props.onChoose(arg)
4355
}

‎packages/tui/test/cli/tui/dialog-scan-gate.test.tsx‎

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { testRender, useRenderer } from "@opentui/solid"
99
import { expect, test } from "bun:test"
1010
import { mkdir } from "node:fs/promises"
1111
import path from "node:path"
12-
import { onCleanup } from "solid-js"
12+
import { onCleanup, onMount } from "solid-js"
1313
import { tmpdir } from "../../fixture/fixture"
1414
import { createTuiResolvedConfig } from "../../fixture/tui-runtime"
1515
import { TestTuiContexts } from "../../fixture/tui-environment"
@@ -167,3 +167,134 @@ test("scan gate: y then n keeps the first choice", async () => {
167167
await gate.cleanup()
168168
}
169169
})
170+
171+
// altimate_change start — funnel wiring, mounted the way app.tsx actually mounts it.
172+
//
173+
// mountGate() above renders the gate as a plain child of DialogProvider, which is why every test
174+
// there passed while the recorded outcome was wrong in the product: app.tsx mounts it through
175+
// `dialog.replace(fn, onClose)` and registers a latched dismissal recorder as that onClose. The
176+
// gate's run() calls dialog.clear() — which fires onClose synchronously — so an outcome recorded
177+
// after the clear loses to the dismissal latch. This harness reproduces that wiring exactly.
178+
async function mountGateAsApp(root: string) {
179+
const state = path.join(root, "state")
180+
await mkdir(state, { recursive: true })
181+
await Bun.write(path.join(state, "kv.json"), "{}")
182+
183+
const [
184+
{ DialogProvider, useDialog },
185+
{ DialogScanGate },
186+
{ KVProvider },
187+
{ ThemeProvider },
188+
{ TuiConfigProvider },
189+
{ ToastProvider },
190+
{ OpencodeKeymapProvider, registerOpencodeKeymap },
191+
] = await Promise.all([
192+
import("../../../src/ui/dialog"),
193+
import("../../../src/component/dialog-scan-gate"),
194+
import("../../../src/context/kv"),
195+
import("../../../src/context/theme"),
196+
import("../../../src/config"),
197+
import("../../../src/ui/toast"),
198+
import("../../../src/keymap"),
199+
])
200+
201+
const chosen: Array<"scan" | "skip"> = []
202+
const recorded: Array<"scan" | "skip" | "dismissed"> = []
203+
204+
// Same latch as app.tsx: the close handler must not overwrite a real choice.
205+
let latched = false
206+
function record(outcome: "scan" | "skip" | "dismissed") {
207+
if (latched) return
208+
latched = true
209+
recorded.push(outcome)
210+
}
211+
212+
function Opener() {
213+
const dialog = useDialog()
214+
onMount(() => {
215+
dialog.replace(
216+
() => <DialogScanGate onOutcome={record} onChoose={(arg) => chosen.push(arg)} />,
217+
() => record("dismissed"),
218+
)
219+
})
220+
return null
221+
}
222+
223+
function Harness() {
224+
const renderer = useRenderer()
225+
const keymap = createDefaultOpenTuiKeymap(renderer)
226+
const resolvedConfig = createTuiResolvedConfig({ leader_timeout: 1000 })
227+
const off = registerOpencodeKeymap(keymap, renderer, resolvedConfig)
228+
onCleanup(off)
229+
230+
return (
231+
<TestTuiContexts directory={root} paths={{ home: root, state, worktree: root }}>
232+
<OpencodeKeymapProvider keymap={keymap}>
233+
<TuiConfigProvider config={resolvedConfig}>
234+
<KVProvider>
235+
<ThemeProvider mode="dark">
236+
<ToastProvider>
237+
<DialogProvider>
238+
<Opener />
239+
</DialogProvider>
240+
</ToastProvider>
241+
</ThemeProvider>
242+
</KVProvider>
243+
</TuiConfigProvider>
244+
</OpencodeKeymapProvider>
245+
</TestTuiContexts>
246+
)
247+
}
248+
249+
const app = await testRender(() => <Harness />, { kittyKeyboard: true })
250+
await app.renderOnce()
251+
await Bun.sleep(25)
252+
await app.renderOnce()
253+
return { app, chosen, recorded, async cleanup() { app.renderer.destroy() } }
254+
}
255+
256+
test("scan gate wired as app.tsx does: a real choice is recorded as the choice, not a dismissal", async () => {
257+
await using tmp = await tmpdir()
258+
const gate = await mountGateAsApp(tmp.path)
259+
try {
260+
gate.app.mockInput.pressKey("y")
261+
await wait(() => gate.chosen.length > 0)
262+
await Bun.sleep(50)
263+
expect(gate.chosen).toEqual(["scan"])
264+
// The regression: dialog.clear() inside run() fires the close handler, so recording the
265+
// outcome after the clear reported "dismissed" for every Yes and every No.
266+
expect(gate.recorded).toEqual(["scan"])
267+
} finally {
268+
await gate.cleanup()
269+
}
270+
})
271+
272+
test("scan gate wired as app.tsx does: skip is recorded as skip", async () => {
273+
await using tmp = await tmpdir()
274+
const gate = await mountGateAsApp(tmp.path)
275+
try {
276+
gate.app.mockInput.pressKey("n")
277+
await wait(() => gate.chosen.length > 0)
278+
await Bun.sleep(50)
279+
expect(gate.recorded).toEqual(["skip"])
280+
} finally {
281+
await gate.cleanup()
282+
}
283+
})
284+
285+
test("scan gate wired as app.tsx does: escape records exactly one dismissal", async () => {
286+
await using tmp = await tmpdir()
287+
const gate = await mountGateAsApp(tmp.path)
288+
try {
289+
// Escape is handled by DialogProvider, never by the gate's own handlers — the close handler
290+
// is the only thing that can see it.
291+
gate.app.mockInput.pressKey("ESCAPE")
292+
await wait(() => gate.recorded.length > 0)
293+
await Bun.sleep(50)
294+
expect(gate.recorded).toEqual(["dismissed"])
295+
expect(gate.chosen).toEqual([])
296+
} finally {
297+
await gate.cleanup()
298+
}
299+
})
300+
// altimate_change end

0 commit comments

Comments
 (0)