Skip to content

fix(orchestration): let a worker brief wait for its launched agent to lead the foreground - #46

Closed
DoozyX wants to merge 1 commit into
mainfrom
DoozyX/retro-issue-18-brief-foreground-settle
Closed

DoozyX wants to merge 1 commit into
mainfrom
DoozyX/retro-issue-18-brief-foreground-settle

Conversation

@DoozyX

@DoozyX DoozyX commented Nov 2, 2026

Copy link
Copy Markdown
Owner

ELI5

When Orca starts a worker, it opens a terminal, launches the agent, and then types the task in. If the agent hadn't quite taken over the terminal yet, Orca gave up on the spot and left a failed start behind. Now Orca checks a few times a second, for up to 10 seconds, and types the task as soon as the agent is in front.

What Changed

Before: orca orchestration worker-start could fail with stage=dispatch_input / lastError=agent_not_in_foreground because the worker brief was only allowed through after a single foreground read. The new terminal was left in residualResources, and the same terminal became ready moments later.

After: the worker-brief write guard resamples the foreground every 250 ms for up to 10 s, never past the start's remaining readiness budget, and writes once the launched agent leads. If a shell prompt returns while waiting, or time runs out, it refuses and the task is never pasted into a shell. The refusal text now names the last foreground sample and the elapsed time.

Mechanism: createLaunchedAgentWriteGuard takes an optional settleDeadlineMs. Only createWorkerBriefWriteGuard sets it, from a new readinessDeadline that deliverWorkerDispatchPreamble and the federation path now pass in. Launch-prompt guards keep their single read and the exact bare agent_not_in_foreground message that external-conversation-send.ts matches. This restores the earlier fix (a23a7be, 9e9df50, 59dbbdf), whose code was lost in later upstream merges. It is adapted to the current guard, with the proven / shellMayHaveReturned / unprovable-host logic unchanged.

Why

The failure was a timing race and the agent became ready shortly afterwards, so waiting inside the existing readiness budget fixes it without new flags, retries or failure semantics. Re-applying the reviewed design is smaller and safer than inventing a new one.

Linked Issue

Fixes #18

Visual Proof

N/A: main-process runtime logic only; no UI change.

Testing

  • Focused tests: 17 files, 205 tests pass (guard, worker-start terminal-target spec, federation brief, parity, structured retry, external-conversation, launch-prompt). New cases cover a first unknown read followed by agent (the Orchestration: dispatch input rejected after worker terminal creation #18 repro, ends ready), shell-return during the wait (refuses, nothing written), an already-passed deadline, disposal during the wait, and launch prompts reading once.
  • pnpm tc, oxlint and oxfmt --check on the changed files, and pnpm check:code-quality:changed: all exit 0. pnpm check:flaky-test-pattern-ratchet exits 0 against the fork base.
  • Not run: the full suite, pnpm build, and any live app or PTY. This is not deployment acceptance.
  • I manually tested these changes locally (no live Orca run; verified by tests only)
  • Automated tests added/updated

Review

Independent read-only review of the whole branch: clean, 0 patch findings, 8 nits and 3 deferred items. The notable nits are plain Error instead of a typed class (matches the file and the exact-string match in external-conversation-send.ts), the overlapping readinessTimeoutMs / readinessDeadline parameters, and a doc note that shell-then-agent recovery usually refuses because TUI startup output looks like a shell prompt marker. settleDeadlineMs holds an absolute timestamp.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Security: the guard still refuses rather than pasting the task into a shell.
  • Cross-platform, remote SSH and folder workspaces: no paths, shells or platform checks; paired-server starts use the same guard through federation.ts.
  • Compatibility: no wire or persisted contract change; lastError is a free string and nothing outside tests matches the worker-start message.
  • Mobile: N/A.
  • Performance: a start can wait up to 10 s longer, inside the existing readiness budget.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (typecheck and scoped lint/tests ran locally; full test and build are left to CI)

… lead the foreground

Refs #18. A fresh worker-start refused its brief at dispatch_input with
agent_not_in_foreground on a single foreground read, although the agent had
just reported ready: one read that caught the launcher still in front, or that
could not tell, failed the start. The fork fix (a23a7be, 9e9df50,
59dbbdf) was lost in later upstream merges; this re-applies it on the
current guard structure.

The worker-brief guard now resamples every 250 ms for up to 10 s
(LAUNCHED_AGENT_FOREGROUND_SETTLE_MS), capped by the remaining readiness
budget, until the agent leads, the host's unprovable-host policy permits the
write, or the window ends; the refusal names the last sample and elapsed time.
A shell-return marker seen during the settle refuses before either success
branch, so the Task is never pasted into a shell. Launch-prompt guards keep
their single read and bare agent_not_in_foreground refusal.
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fc8836d0-f8d4-4b9f-8bcb-eb60692a3843
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@DoozyX

DoozyX commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #63, merged.

@DoozyX DoozyX closed this Nov 15, 2026
@DoozyX
DoozyX deleted the DoozyX/retro-issue-18-brief-foreground-settle branch November 15, 2026 10:55
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.

Orchestration: dispatch input rejected after worker terminal creation

2 participants