Skip to content

fix(tui): queue concurrent permission requests - #885

Merged
Astro-Han merged 1 commit into
apache:mainfrom
M4n5ter:fix/856-tui-permission-fifo
Jul 14, 2026
Merged

Astro-Han merged 1 commit into
apache:mainfrom
M4n5ter:fix/856-tui-permission-fifo

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Jul 13, 2026 •

Copy link
Copy Markdown
Member
English

Follow-up to #856.

Summary

  • Harden the TUI's shared pending-interaction FIFO so concurrent permission requests are retained and duplicate transport events are ignored.
  • Keep only the queue head actionable and visible, while preserving the existing shared queue and overlay behavior for AskUserQuestion.
  • Let permission_decision_ack, a matching terminal tool_result, or turn termination advance permission state; completing respondToPermission() alone does not.

Why

Runtime may park more than one tool call at the permission boundary. The shared interaction queue provides the right common structure for permission requests and user questions, but permission lifecycle edges still need precise identity and terminal cleanup so overlapping or replayed events cannot lose or duplicate an actionable request.

The response API has a second timing boundary: its Promise confirms that a response was submitted, not that Runtime has committed the decision. Advancing locally at that point can expose a stale sibling request that Runtime already resolved through turn memory. The event stream is the authoritative lifecycle source for both cases.

Key decisions

Decision Rationale
Keep permission requests in the existing generic interaction FIFO and deduplicate by requestId. Every distinct request remains actionable, replayed transport frames do not create duplicate prompts, and AskUserQuestion does not need a parallel queue.
Render and accept input for the head only. The existing interaction stays simple and deterministic; this is a correctness fix rather than a queue-management UI.
Remove acknowledgements by requestId, including out of order. Runtime events identify the request whose lifecycle changed; local head assumptions cannot override that fact.
Remove terminal tool results by toolUseId. Timeout or expiry paths can finish without a separate permission acknowledgement.
Keep a submitted response in flight until an authoritative event removes that request. Repeated keys cannot submit twice or answer the next request before Runtime has advanced. A rejected submission leaves the head visible for retry.
Clear the queue on error, abort, complete, thrown stream failure, or session replacement. Requests from a finished or replaced event stream are no longer actionable.

Scope

Included:

  • Generic TUI interaction state as it applies to permission handling
  • Interleaving permission requests with the existing AskUserQuestion interaction
  • Duplicate, overlapping, and out-of-order permission events
  • Missing-ack terminal cleanup and abnormal stream cleanup
  • Deterministic TUI coverage with response completion separated from acknowledgement

Not included:

  • Desktop queue changes
  • A visible pending-request list or queue controls
  • Permission policy, turn-memory capability, or request-schema changes
  • WriteStdin-specific behavior, inspection UI, or runtime contracts

Verification

  • Root npm run typecheck, npm test, and npm run build passed.
  • GitHub CI passed typecheck, test, and e2e on the rebased head.
  • git diff --check upstream/main...HEAD passed.

No visual artifact is included because the existing prompt layout is unchanged; the user-visible difference occurs only when permission events overlap, and that lifecycle is covered through the real TUI runner surface.

Impact

Concurrent TUI permission requests can no longer disappear, duplicate, or be answered before Runtime advances them. Ordinary single-request prompts retain the same keys and presentation, and user-question interactions continue through the same generic queue.

There is no schema migration, dependency change, compatibility layer, documentation requirement, or release-note impact.

Reviewer notes

The highest-value review boundaries are:

  1. pi-transcript.ts: shared interaction identity, permission-specific terminal cleanup, and duplicate suppression.
  2. pi-tui-runner.ts: response in-flight state versus authoritative event-driven dequeue.
  3. The parallel-permission runner test: a resolved response Promise is deliberately separated from its delayed acknowledgement.

Ready for review

  • Verification is complete and the results above are current
  • Impact, migration, documentation, and release-note needs are addressed
  • Visual evidence is not applicable and the reason is stated
简体中文

#856 的后续改进。

摘要

  • 加固 TUI 既有的共享 pending-interaction FIFO,确保并发 permission request 都会保留,并忽略重复 transport event。
  • 仍然只让队首可操作、可见,同时保留 AskUserQuestion 既有的共享队列与 overlay 行为。
  • 只有 permission_decision_ack、匹配的 terminal tool_result 或 turn 终止才能推进 permission 状态;respondToPermission() 自身完成不能推进。

原因

Runtime 可能在权限边界同时 park 多个工具调用。共享 interaction queue 是 permission request 与 user question 的正确通用结构,但 permission 生命周期边界仍需精确的身份与 terminal cleanup,避免重叠或重放事件丢失请求或制造重复的可操作项。

Response API 还有第二个时序边界:它的 Promise 只确认响应已经提交,并不代表 Runtime 已经提交决策。如果此时由本地提前推进,TUI 可能展示一条已被 Runtime 通过 turn memory 自动处理的 stale sibling request。两种情况下都应以 event stream 作为权威生命周期来源。

关键决策

决策 理由
将 permission request 保留在既有通用 interaction FIFO 中,并按 requestId 去重。 每个不同请求都保持可操作,重放的 transport frame 不会产生重复提示,AskUserQuestion 也不需要另一套队列。
只渲染队首,也只接受针对队首的输入。 保持现有交互简单、确定;本次是正确性修复,不是 queue-management UI。
按 requestId 删除 ack,包括乱序 ack。 Runtime event 已明确指出哪个请求的生命周期发生变化,本地不能用队首假设覆盖该事实。
按 toolUseId 删除 terminal tool result。 Timeout 或 expiry 路径可能结束而不再产生单独的 permission ack。
已提交响应保持 in-flight,直到权威事件删除该请求。 连续按键不能重复提交,也不能在 Runtime 推进前回答下一项;提交被拒绝时队首仍可见并允许重试。
Error、abort、complete、stream 直接抛错或 session replacement 时清空队列。 已结束或被替换的 event stream 中的请求不再可操作。

范围

包含:

  • 通用 TUI interaction state 中与 permission handling 相关的部分
  • Permission request 与既有 AskUserQuestion interaction 的交错
  • 重复、重叠和乱序 permission event
  • 缺少 ack 时的 terminal cleanup 与异常 stream cleanup
  • 将 response 完成与 acknowledgement 分离的确定性 TUI 覆盖

不包含:

  • Desktop queue 变更
  • 可见 pending-request 列表或队列控制
  • Permission policy、turn-memory capability 或 request schema 变更
  • WriteStdin 专用行为、检查 UI 或 runtime contract

验证

  • 根级 npm run typecheck、npm test 与 npm run build 通过。
  • Rebase 后 head 的 GitHub CI typecheck、test 与 e2e 均通过。
  • git diff --check upstream/main...HEAD 通过。

现有 permission prompt 布局没有变化,因此不附视觉 artifact;用户可见差异只发生在 permission event 重叠时,该生命周期已通过真实 TUI runner surface 覆盖。

影响

并发 TUI permission request 不再丢失、重复,也不能在 Runtime 推进前被提前回答。普通单请求 prompt 的按键和展示保持不变,user-question interaction 继续使用同一通用队列。

本次没有 schema migration、依赖变更、兼容层、文档要求或 release-note 影响。

审查重点

最值得审查的边界是:

  1. pi-transcript.ts:共享 interaction 身份、permission 专用 terminal cleanup 与重复抑制。
  2. pi-tui-runner.ts:response in-flight 状态与由权威事件驱动的出队。
  3. 并行 permission runner 测试:已完成的 response Promise 会与延迟 ack 被刻意分开。

可供审查

  • 验证已经完成,以上结果均为当前结果
  • 影响、迁移、文档与 release-note 需求均已说明
  • 已说明不适用视觉证据的原因

@Astro-Han Astro-Han left a comment

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.

No blocking code findings on the current head.

GitHub currently reports merge conflicts around the newer generic interaction queue and permission handling. Please preserve those main-branch behaviors during the rebase; the resolved diff may need another look.

@M4n5ter
M4n5ter force-pushed the fix/856-tui-permission-fifo branch from f5be57e to 92a9377 Compare July 14, 2026 03:35
@M4n5ter

M4n5ter commented Jul 14, 2026

Copy link
Copy Markdown
Member Author
English

@Astro-Han The conflict resolution for this PR has been pushed.

The resolved diff stays on top of the newer generic interaction queue rather than restoring a permission-only slot or parallel queue. It adds permission-specific identity and lifecycle handling within that shared structure: request-ID deduplication, authoritative dequeue by acknowledgement or terminal event, and in-flight response fencing until Runtime advances the request. The existing AskUserQuestion queue and overlay behavior remain intact.

The rebased head is mergeable, and typecheck, test, and e2e CI are green. I also updated the PR description to reflect the post-conflict design.

简体中文

@Astro-Han 该 PR 的冲突解决已经推送。

解决后的 diff 继续建立在较新的通用 interaction queue 上,没有恢复 permission-only 槽或引入平行队列。它只在共享结构内补充 permission 所需的身份与生命周期处理:按 request ID 去重、由 acknowledgement 或 terminal event 权威出队,以及在 Runtime 推进请求前保持 response in-flight fence。既有 AskUserQuestion queue 与 overlay 行为保持不变。

Rebase 后的 head 可合并,typecheck、test 与 e2e CI 均已通过。我也更新了 PR 描述,使其反映冲突解决后的设计。

@Astro-Han
Astro-Han merged commit 96e6ae4 into apache:main Jul 14, 2026
3 checks passed
@M4n5ter
M4n5ter deleted the fix/856-tui-permission-fifo branch August 5, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants