Repository navigation
feat(tui): mark the pause between a thought and the next step #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
61564c1
7013cf7
7ed14ab
99950bf
c1ce792
20c262b
c2fb120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,8 @@ interface ProcessorContext extends Input { | |
| needsCompaction: boolean | ||
| currentText: SessionV1.TextPart | undefined | ||
| reasoningMap: Record<string, SessionV1.ReasoningPart> | ||
| // Time of the last reasoning delta per part, so a thought ends when the model stopped thinking, not when the next chunk arrived. | ||
| reasoningLast: Record<string, number> | ||
| } | ||
|
|
||
| type StreamEvent = LLMEvent | ||
|
|
@@ -114,6 +116,7 @@ const layer = Layer.effect( | |
| needsCompaction: false, | ||
| currentText: undefined, | ||
| reasoningMap: {}, | ||
| reasoningLast: {}, | ||
| } | ||
| let aborted = false | ||
| const clock = createGenerationClock() | ||
|
|
@@ -261,9 +264,13 @@ const layer = Layer.effect( | |
| if (!(reasoningID in ctx.reasoningMap)) return | ||
| // oxlint-disable-next-line no-self-assign -- reactivity trigger | ||
| ctx.reasoningMap[reasoningID].text = ctx.reasoningMap[reasoningID].text | ||
| ctx.reasoningMap[reasoningID].time = { ...ctx.reasoningMap[reasoningID].time, end: Date.now() } | ||
| ctx.reasoningMap[reasoningID].time = { | ||
| ...ctx.reasoningMap[reasoningID].time, | ||
| end: ctx.reasoningLast[reasoningID] ?? Date.now(), | ||
| } | ||
| yield* session.updatePart(ctx.reasoningMap[reasoningID]) | ||
| delete ctx.reasoningMap[reasoningID] | ||
| delete ctx.reasoningLast[reasoningID] | ||
| }) | ||
|
|
||
| const ensureToolCall = Effect.fn("SessionProcessor.ensureToolCall")(function* (input: { | ||
|
|
@@ -342,13 +349,15 @@ const layer = Layer.effect( | |
| time: { start: Date.now() }, | ||
| metadata: value.providerMetadata, | ||
| } | ||
| ctx.reasoningLast[value.id] = Date.now() | ||
| yield* session.updatePart(ctx.reasoningMap[value.id]) | ||
| return | ||
|
|
||
| case "reasoning-delta": | ||
| // Match dev: silently drop orphan deltas (no preceding reasoning-start). | ||
| if (!(value.id in ctx.reasoningMap)) return | ||
| ctx.reasoningMap[value.id].text += value.text | ||
| ctx.reasoningLast[value.id] = Date.now() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: The new end-time semantics are not pinned down by a test This line defines the behaviour the whole feature rests on: the reasoning part's persisted
|
||
| if (value.providerMetadata) ctx.reasoningMap[value.id].metadata = value.providerMetadata | ||
| yield* session.updatePartDelta({ | ||
| sessionID: ctx.reasoningMap[value.id].sessionID, | ||
|
|
@@ -622,14 +631,15 @@ const layer = Layer.effect( | |
| ctx.currentText = undefined | ||
| } | ||
|
|
||
| for (const part of Object.values(ctx.reasoningMap)) { | ||
| const end = Date.now() | ||
| for (const [id, part] of Object.entries(ctx.reasoningMap)) { | ||
| const end = ctx.reasoningLast[id] ?? Date.now() | ||
| yield* session.updatePart({ | ||
| ...part, | ||
| time: { start: part.time.start ?? end, end }, | ||
| }) | ||
| } | ||
| ctx.reasoningMap = {} | ||
| ctx.reasoningLast = {} | ||
|
|
||
| yield* Effect.forEach( | ||
| Object.values(ctx.toolcalls), | ||
|
|
@@ -703,7 +713,10 @@ const layer = Layer.effect( | |
| return yield* Effect.gen(function* () { | ||
| yield* Effect.gen(function* () { | ||
| ctx.currentText = undefined | ||
| // A retry after a stalled or failed stream must not leave the previous attempt's thought open. | ||
| yield* Effect.forEach(Object.keys(ctx.reasoningMap), finishReasoning) | ||
| ctx.reasoningMap = {} | ||
| ctx.reasoningLast = {} | ||
| yield* status.set(ctx.sessionID, { type: "busy" }) | ||
| const stream = llm.stream(streamInput) | ||
| clock.startRequest(Date.now()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type { Part } from "@opencode-ai/sdk/v2" | ||
|
|
||
| // Only a real stall gets a marker: a minute with no token arriving. Normal streaming never gets near this. | ||
| export const PAUSE_MIN_MS = 60_000 | ||
|
|
||
| function startOf(part: Part | undefined): number | undefined { | ||
| if (!part) return undefined | ||
| // A tool part is only timed once its arguments finished streaming, so it cannot tell a silent model from one that is still sending tokens. | ||
| if (part.type === "text" || part.type === "reasoning") return part.time?.start | ||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * Milliseconds with no token between the last thinking token and the next text or | ||
| * thinking part, when long enough to be a stall. | ||
| */ | ||
| export function pauseBefore(parts: readonly Part[], index: number): number | undefined { | ||
| // A retry opens a new step, so look past the step marker to the previous visible part. | ||
| let before = index - 1 | ||
| while (before >= 0 && parts[before]?.type === "step-start") before-- | ||
| const prev = parts[before] | ||
| if (!prev || prev.type !== "reasoning") return undefined | ||
| const end = prev.time.end | ||
| const start = startOf(parts[index]) | ||
| if (end === undefined || start === undefined) return undefined | ||
| const gap = start - end | ||
| // The stream watchdog fires a minute after the last token, so allow a second of timer jitter. | ||
| return gap >= PAUSE_MIN_MS - 1000 ? gap : undefined | ||
| } | ||
|
|
||
| export function pauseLabel(ms: number): string { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION:
Reuse |
||
| const seconds = Math.round(ms / 1000) | ||
| if (seconds < 60) return `${seconds}s` | ||
| return `${Math.floor(seconds / 60)}m ${seconds % 60}s` | ||
| } | ||
|
|
||
| // The marker text. A stall that ends in a new thought is a retried stream. | ||
| export function pauseMarker(parts: readonly Part[], index: number): string | undefined { | ||
| const ms = pauseBefore(parts, index) | ||
| if (ms === undefined) return undefined | ||
| const retried = parts[index]?.type === "reasoning" | ||
| return `Stalled for ${pauseLabel(ms)}${retried ? ", retrying..." : ""}` | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import type { Part } from "@opencode-ai/sdk/v2" | ||
| import { pauseBefore, pauseLabel, pauseMarker } from "../src/util/pause" | ||
|
|
||
| const reasoning = (start: number, end?: number) => ({ type: "reasoning", time: { start, end } }) as unknown as Part | ||
| const text = (start: number) => ({ type: "text", time: { start } }) as unknown as Part | ||
| const tool = (start?: number) => | ||
| ({ type: "tool", state: start === undefined ? { status: "pending" } : { status: "running", time: { start } } }) as unknown as Part | ||
|
|
||
| describe("pauseBefore", () => { | ||
| test("marks a stall of a minute or more before the next text or thought", () => { | ||
| expect(pauseBefore([reasoning(0, 1000), text(61000)], 1)).toBe(60000) | ||
| expect(pauseBefore([reasoning(0, 1000), reasoning(121000, 122000)], 1)).toBe(120000) | ||
| }) | ||
| test("a retry's step marker does not hide the stall", () => { | ||
| const step = { type: "step-start" } as unknown as Part | ||
| expect(pauseBefore([reasoning(0, 1000), step, reasoning(63000, 64000)], 2)).toBe(62000) | ||
| }) | ||
| test("normal streaming latency never gets a marker", () => { | ||
| expect(pauseBefore([reasoning(0, 1000), text(3000)], 1)).toBeUndefined() | ||
| expect(pauseBefore([reasoning(0, 1000), text(30000)], 1)).toBeUndefined() | ||
| expect(pauseBefore([reasoning(0, 1000), text(58000)], 1)).toBeUndefined() | ||
| expect(pauseBefore([reasoning(0, 1000), text(60000)], 1)).toBe(59000) | ||
| }) | ||
| test("tool calls are never marked, since their time includes streamed arguments", () => { | ||
| expect(pauseBefore([reasoning(0, 1000), tool(300000)], 1)).toBeUndefined() | ||
| }) | ||
| test("ignores the first part and parts that do not follow reasoning or lack timing", () => { | ||
| expect(pauseBefore([reasoning(0, 1000)], 0)).toBeUndefined() | ||
| expect(pauseBefore([text(0), text(300000)], 1)).toBeUndefined() | ||
| expect(pauseBefore([reasoning(0), text(300000)], 1)).toBeUndefined() | ||
| }) | ||
| test("labels seconds and minutes", () => { | ||
| expect(pauseLabel(60000)).toBe("1m 0s") | ||
| expect(pauseLabel(75000)).toBe("1m 15s") | ||
| }) | ||
| }) | ||
|
|
||
| describe("pauseMarker", () => { | ||
| test("says retrying when the stall ended in a new thought", () => { | ||
| const step = { type: "step-start" } as unknown as Part | ||
| expect(pauseMarker([reasoning(0, 1000), step, reasoning(63000, 64000)], 2)).toBe("Stalled for 1m 2s, retrying...") | ||
| }) | ||
| test("plain stall before text", () => { | ||
| expect(pauseMarker([reasoning(0, 1000), text(91000)], 1)).toBe("Stalled for 1m 30s") | ||
| expect(pauseMarker([reasoning(0, 1000), text(3000)], 1)).toBeUndefined() | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SUGGESTION:
ctx.reasoningLastis not cleared wherectx.reasoningMapis clearedctx.reasoningMapis reset in two places that leave the newctx.reasoningLastmap populated: the abort cleanup (ctx.reasoningMap = {}, processor.ts:638) and the start of everyprocessiteration (ctx.reasoningMap = {}, processor.ts:712). The two maps are meant to stay parallel:finishReasoningreadsctx.reasoningLast[id]and deletes it only after the early return for an unknown id, so areasoning-endfor an id that is no longer in the map leaves the entry behind.No wrong value is computed today (every
reasoning-startoverwrites the entry), but the drift is a trap for the next change here. Clearctx.reasoningLastin the same two places, and delete the entry infinishReasoningbefore the early return.