Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/runtime/tui-idle-hook-lane-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const OPENCODE_PERMISSION_DIALOG = [
' Allow once Allow always Reject ctrl+f fullscreen ⇆ select enter confirm',
''
].join('\r\n')
// OpenCode 1.18's question tool, which paints no dialog wording the blocked layer knows.
const OPENCODE_QUESTION = [
' ┃ Which color do you prefer?',
' ┃ 1. Red',
' ┃ 2. Blue',
' ┃ 3. Type your own answer',
' ┃ ↑↓ select enter submit esc dismiss',
''
].join('\r\n')
// Shorter than the 2 s poll, so only the synchronous verdict can settle a wait.
const WAIT_MS = 150

Expand Down Expand Up @@ -194,4 +203,21 @@ describe('tui-idle hook lane through the runtime', () => {
})
).toBe('ready')
})

it('blocks on an open question the hook reports, and settles once it is answered', async () => {
const options = { launchAgent: 'opencode' as const, data: OPENCODE_QUESTION }
expect(await waitOutcome({ ...options, rows: () => [] })).toBe('timeout')
expect(
await waitOutcome({
...options,
rows: () => [row({ agentType: 'opencode', state: 'waiting', receivedAt: Date.now() + 1 })]
})
).toBe('blocked:agent-interactive-prompt')
expect(
await waitOutcome({
...options,
rows: () => [row({ agentType: 'opencode', receivedAt: Date.now() + 1 })]
})
).toBe('ready')
})
})
18 changes: 18 additions & 0 deletions src/main/runtime/tui-idle-hook-lane.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ describe('readTuiIdleHookTurn', () => {
).toEqual(WORKING)
})

it('blocks on a wait the hook reports with no dialog text the arbiter knows (a question)', () => {
const receivedAt = Date.now() - 1000
for (const state of ['waiting', 'blocked'] as const) {
expect(readTuiIdleHookTurn({ ...base, hookRows: [row({ receivedAt, state })] })).toEqual({
state: 'permission',
blockedReason: 'agent-interactive-prompt'
})
}
// Input since the wait opened may have answered it before the next hook arrived.
expect(
readTuiIdleHookTurn({
...base,
hookRows: [row({ receivedAt, state: 'waiting' })],
lastInputAt: receivedAt + 1
})
).toEqual({ state: 'permission', blockedReason: null })
})

it('takes the newest joined row', () => {
const now = Date.now()
expect(
Expand Down
18 changes: 14 additions & 4 deletions src/main/runtime/tui-idle-hook-lane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type HookTurnState = 'done' | 'working' | 'permission'

/**
* The main agent's turn as the hook server's store last saw it for one pane, and the permission
* arbiter's verdict on the pane's blocked text with that turn as its explicit status.
* arbiter's verdict on the pane's blocked text with that turn as its explicit status (or, for a
* wait the hook reports with no text the arbiter knows, a generic interactive prompt).
*/
export type TuiIdleHookTurn = {
state: HookTurnState
Expand Down Expand Up @@ -65,10 +66,19 @@ export function readTuiIdleHookTurn(read: TuiIdleHookTurnRead): TuiIdleHookTurn
return null
}
const state = hookLeadTurnState(row)
if (state === null || (state === 'done' && row.receivedAt < (read.lastInputAt ?? -1))) {
const predatesInput = row.receivedAt < (read.lastInputAt ?? -1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '45,135p' src/main/runtime/tui-idle-hook-lane.ts
sed -n '35,80p' src/shared/agent-status-ipc-payload.ts
rg -n 'stateStartedAt|lastInputAt|receivedAt' src/main/runtime/tui-idle-hook-lane* src/main/runtime/tui-idle* src/main/agent-status* 2>/dev/null | head -100

Repository: stablyai/orca

Length of output: 14230


Compare input with the wait start time.

receivedAt records delivery time, not when the permission wait began. If a permission row has stateStartedAt < lastInputAt < receivedAt, the current check treats the row as fresh and can return agent-interactive-prompt even though the input answered a wait that started earlier.

🐛 Suggested fix
-  const predatesInput = row.receivedAt < (read.lastInputAt ?? -1)
+  const predatesInput = row.stateStartedAt < (read.lastInputAt ?? -1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const predatesInput = row.receivedAt < (read.lastInputAt ?? -1)
const predatesInput = row.stateStartedAt < (read.lastInputAt ?? -1)

if (state === null || (state === 'done' && predatesInput)) {
return null
}
return { state, blockedReason: read.resolveBlockedText(state, row) }
const blockedReason = read.resolveBlockedText(state, row)
// Why the hook alone blocks: a question or custom modal paints no dialog text the arbiter knows,
// and these hooks report its answer. Input since may have answered it before the hook arrived.
return {
state,
blockedReason:
blockedReason ??
(state === 'permission' && !predatesInput ? 'agent-interactive-prompt' : null)
Comment on lines +78 to +80

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Answered question remains blocked If the system clock moves backward, the status store can give a waiting row a timestamp later than the current clock. When the user answers the question, the input gets an earlier timestamp, so this fallback reports agent-interactive-prompt even though the answer was sent. The wait then returns blocked instead of allowing the agent to continue.

}
}

/** The tui-idle verdicts the hook lane can reach (a subset of `TuiIdleVerdict`). */
Expand Down Expand Up @@ -104,7 +114,7 @@ export function evaluateHookTurn(
case 'working':
return { kind: 'working' }
case 'permission':
// Why pending: the arbiter saw no prompt in the tail, so the poll's screen read decides.
// Why pending: input after the wait opened may have answered it; the next hook decides.
return { kind: 'pending', quietForeground: 'closed' }
}
}
Loading