Skip to content

Commit 6b41623

Browse files
sahrizviclaude
andcommitted
fix: [AI-7519] address PR review feedback
Three review findings from CodeRabbit + Kilo + cubic addressed: 1. **Enter busy state before publishing bootstrap phases** (prompt.ts). Previously the TUI renders phase labels only while `status.type === "busy"`, but `SessionStatus.set(..., "busy")` fired at line 506 inside the while-loop — *after* the bootstrap traceSpan calls at lines 411-431. Early bootstrap labels ("Loading session...", "Loading config...", "Preparing telemetry...", "Detecting project shape...") were never visible; only `bootstrap.resolve-tools` at step===1 fired within the busy window. Set busy immediately at the top of `loop()` so all bootstrap phases render. The existing set inside the while-loop is preserved as a busy → busy no-op for entry paths that don't come through session start. 2. **Publish EventV2 Phase alongside legacy** (status.ts). `publishPhase` previously only mirrored to `LegacyEvent.Phase` (the SSE bus the TUI subscribes to). V2 subscribers never saw phase updates. Added `publishPhase` to the Service interface + Layer implementation so `Event.Phase` is published through the EventV2 bridge alongside the legacy bus event. Matches the pattern used by `set()`. 3. **Use tmpdir fixture + assert a mapped phase label** (e2e test). Replaced manual `mkdtempSync` + finally-block cleanup with `await using tmp = await tmpdir()` — matches the codebase convention (scheduler.test.ts and the coding guideline in CONTRIBUTING.md) and handles the case where launchTui rejects before the try block. Added a hard `expect(rendered).toContain("Discovering tools")` assertion — the previous single fallback assertion would pass even if the entire phase-event pipeline was broken (because the fallback renders whenever status is busy). "Discovering tools" is the label for `bootstrap.resolve-tools`; its span duration (MCP tool listing) is reliably longer than the PTY poll interval, so this is safe from flake. Diagnostic dump of the other timing-sensitive labels retained. Local validation - Typecheck clean across `@altimateai/altimate-code`. - Session + fork-guards: 67 pass / 0 fail (18 fork-guards including the AI-7519 pipeline guard). - Rebuilt binary + re-ran the tightened e2e — both hard assertions pass; specific-label hits: "Discovering tools" ✓, others sub-ms. Skipped - Cubic's duplicates of CodeRabbit findings — same items, addressed. Co-Authored-By: Claude Opus 4.7 <[email protected]> Claude-Session: https://claude.ai/code/session_01Q8FGy89Qpr39k8nCSpCcK2
1 parent 3fc64ff commit 6b41623

3 files changed

Lines changed: 74 additions & 31 deletions

File tree

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ export namespace SessionPrompt {
407407
// single "bootstrap" span right before the first processor.process call so
408408
// the pre-first-generation region has a visible parent duration in traces.
409409
const bootstrapStart = Date.now()
410+
// Enter busy state BEFORE the first bootstrap traceSpan fires so the
411+
// phase labels the TUI renders are actually visible during
412+
// session-get / config-get / fingerprint-detect / telemetry-init. The
413+
// TUI's status renderer gates on `status.type === "busy"`; without
414+
// this early set only `bootstrap.resolve-tools` (which fires inside
415+
// the while-loop after the existing busy set at line 506) would show
416+
// a label. The while-loop re-set below is now a no-op busy → busy
417+
// transition, preserved for legacy call sites that may enter the
418+
// loop from elsewhere.
419+
await SessionStatus.set(sessionID, { type: "busy" })
410420
// altimate_change end
411421
const session = await traceSpan(
412422
"bootstrap.session-get",

‎packages/opencode/src/session/status.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export interface Interface {
104104
readonly get: (sessionID: SessionID) => Effect.Effect<Info>
105105
readonly list: () => Effect.Effect<Map<SessionID, Info>>
106106
readonly set: (sessionID: SessionID, status: Info) => Effect.Effect<void>
107+
// altimate_change start (AI-7519) — publish a session.phase event through
108+
// the EventV2 bus so V2 subscribers see phase transitions alongside the
109+
// legacy bus mirror.
110+
readonly publishPhase: (sessionID: SessionID, phase: string, active: boolean) => Effect.Effect<void>
111+
// altimate_change end
107112
}
108113

109114
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionStatus") {}
@@ -137,7 +142,19 @@ export const layer = Layer.effect(
137142
data.set(sessionID, status)
138143
})
139144

140-
return Service.of({ get, list, set })
145+
// altimate_change start (AI-7519) — V2 publish for the session.phase event.
146+
// Complements the LegacyEvent.Phase publish that happens in the imperative
147+
// wrapper below so both V1 (SSE mirror) and V2 subscribers see phases.
148+
const publishPhase = Effect.fn("SessionStatus.publishPhase")(function* (
149+
sessionID: SessionID,
150+
phase: string,
151+
active: boolean,
152+
) {
153+
yield* events.publish(Event.Phase, { sessionID, phase, active })
154+
})
155+
// altimate_change end
156+
157+
return Service.of({ get, list, set, publishPhase })
141158
}),
142159
)
143160

@@ -170,9 +187,15 @@ export async function list() {
170187

171188
// altimate_change start (AI-7519) — publish a session.phase event. Fired by SessionPrompt.traceSpan
172189
// on entry (active=true) and exit (active=false); the TUI subscribes to render an honest label like
173-
// "Discovering warehouse tools..." during the pre-first-visible-response window. Best-effort: any
174-
// failure here must not affect the traced operation itself.
190+
// "Discovering warehouse tools..." during the pre-first-visible-response window. Publishes on both
191+
// the EventV2 bus (for V2 subscribers) and the LegacyEvent.Phase bus (for the SSE mirror the TUI
192+
// consumes). Best-effort: any failure here must not affect the traced operation itself.
175193
export async function publishPhase(sessionID: SessionID, phase: string, active: boolean) {
194+
try {
195+
await runStatus((s) => s.publishPhase(sessionID, phase, active))
196+
} catch {
197+
// never surface phase-publish failures back to the caller
198+
}
176199
try {
177200
await Bus.publish(LegacyEvent.Phase, { sessionID, phase, active })
178201
} catch {

‎packages/opencode/test/cli/tui/phase-label.tui-e2e.test.ts‎

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,36 @@
1010
* exactly that failure mode. This spec drives the real TUI end-to-end so the
1111
* label is verified against the user's actual view.
1212
*
13-
* The label is expected to appear during the brief interval between
14-
* `status.type === "busy"` and the first token from the model. On a bare
15-
* prompt with no auth configured, the busy window closes almost immediately
16-
* with an error banner — but the phase spans still fire (Session.get,
17-
* Config.get, Telemetry.init all run before any LLM call is attempted) and
18-
* the "Thinking..." fallback rendered next to the spinner is a strict
19-
* superset of the specific bootstrap labels. Asserting on "Thinking..." keeps
20-
* the test tolerant to timing variance and provider unreachability.
13+
* Two hard assertions:
14+
* 1. The "Thinking..." fallback renders during any busy window — proves
15+
* the render chain (status → phaseLabel → spinner row) is intact.
16+
* 2. A specific mapped label ("Discovering tools") renders somewhere in
17+
* the run — proves the full phase-event pipeline (publishPhase →
18+
* Bus.publish → sync.tsx handler → store update → render) is intact
19+
* end-to-end. Without this, a regression that entirely broke phase
20+
* publishing would still green here because the fallback renders
21+
* regardless.
22+
*
23+
* "Discovering tools" is the label mapped from `bootstrap.resolve-tools`,
24+
* whose span duration (~30-100ms while listing MCP tools) is comfortably
25+
* larger than the PTY harness poll interval (50ms). The other bootstrap
26+
* sub-spans are sub-millisecond and observed to not survive PTY sampling —
27+
* a diagnostic dump of all specific label hits is retained for future
28+
* timing-driven debugging.
2129
*/
2230

2331
import { describe, expect, test } from "bun:test"
24-
import path from "path"
25-
import { mkdtempSync, rmSync } from "fs"
26-
import { tmpdir } from "os"
2732
import { launchTui } from "../../fixture/pty-tui"
28-
29-
function makeProjectDir(): string {
30-
return mkdtempSync(path.join(tmpdir(), "altimate-tui-phase-"))
31-
}
33+
import { tmpdir } from "../../fixture/fixture"
3234

3335
describe("TUI e2e — AI-7519 phase-label render", () => {
34-
test("phase-label renders next to the busy spinner after a prompt is submitted", async () => {
35-
const project = makeProjectDir()
36+
test("phase-label pipeline renders 'Discovering tools' + 'Thinking...' fallback beside the busy spinner", async () => {
37+
// await using ensures the temp directory is cleaned up even if launchTui
38+
// rejects before the try block — matches the codebase convention in
39+
// scheduler.test.ts and the coding guideline enforced by CI.
40+
await using tmp = await tmpdir()
3641
const tui = await launchTui({
37-
cwd: project,
42+
cwd: tmp.path,
3843
cols: 140,
3944
rows: 50,
4045
waitForReady: "Ask anything",
@@ -53,14 +58,22 @@ describe("TUI e2e — AI-7519 phase-label render", () => {
5358
}
5459
tui.sendKey("Enter")
5560

56-
// The label lookup falls back to "Thinking..." for any active busy
57-
// window without a specific phase name mapped. That fallback is a
58-
// strict superset of every specific bootstrap label, so asserting on
59-
// the fallback is robust to timing races (the specific labels flash
60-
// very briefly during their sub-millisecond spans).
61+
// (1) Fallback assertion — proves the render chain is alive.
62+
// phaseLabel(undefined) renders "Thinking..." during any busy window
63+
// without a specific phase name mapped.
6164
await tui.waitForText(/Thinking\.\.\./, { timeoutMs: 8000 })
6265

63-
// Diagnostic dump for the specific labels — write out which ones caught.
66+
// (2) Mapped-label assertion — proves the full phase pipeline is alive.
67+
// "Discovering tools" is the label for `bootstrap.resolve-tools`, which
68+
// fires on step===1 inside loop(). Its span (MCP tool listing) is
69+
// reliably longer than the PTY poll interval. Regression that breaks
70+
// publishPhase / Bus.publish / sync handler / store update would fail
71+
// this assertion even though the fallback above still succeeds.
72+
await tui.waitForText("Discovering tools", { timeoutMs: 8000 })
73+
74+
// Diagnostic dump of any other mapped labels that landed — helpful for
75+
// future timing-related debugging without gating the test on
76+
// sub-millisecond span durations we can't reliably observe over PTY.
6477
const rendered = tui.text()
6578
const specificLabels = {
6679
"Loading session": rendered.includes("Loading session"),
@@ -71,13 +84,10 @@ describe("TUI e2e — AI-7519 phase-label render", () => {
7184
}
7285
// eslint-disable-next-line no-console
7386
console.log("[AI-7519 e2e] specific label hits:", JSON.stringify(specificLabels))
74-
// The fallback assertion above is the load-bearing check; the specific
75-
// labels are timing-sensitive (sub-millisecond spans + PTY polling
76-
// interval). Not gated on hard equality.
7787
expect(rendered).toContain("Thinking...")
88+
expect(rendered).toContain("Discovering tools")
7889
} finally {
7990
await tui.dispose()
80-
rmSync(project, { recursive: true, force: true })
8191
}
8292
}, 45_000)
8393
})

0 commit comments

Comments
 (0)