Skip to content

agentHost: finish foreground turns independently of background work - #336083

Draft
Justin Chen (justschen) wants to merge 1 commit into
mainfrom
justschen/autopilot-task-completion
Draft

Justin Chen (justschen) wants to merge 1 commit into
mainfrom
justschen/autopilot-task-completion

Conversation

@justschen

@justschen Justin Chen (justschen) commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fix the Copilot parent turn remaining Working after its foreground response has finished while an intentional background shell or agent is still running. The SDK defines session.idle as whole-session quiescence, including background agents and attached shell commands; it is not the foreground request completion boundary.

This is independent of the subagent-rendering work in #335899 and is based directly on main, not on that PR.

Changes

  • Await request-scoped foreground completion for sends and zero-message continuations; use root assistant.idle for SDK-initiated turns without a host request promise.
  • Preserve request identity through steering and reject stale completion after cancellation, failure, disposal, or replacement. Drain pending host-side tool persistence before finalizing.
  • Keep background-child Markdown, reasoning, and tool-input streams intact after parent completion.
  • Protect active background agents and attached/detached shells from automatic session release and deferred client restarts; recheck restart eligibility when session idle eventually arrives.
  • Add native attached-shell regression coverage and extend lifecycle and restart-race tests. No UI, protocol, or permission-setting changes.

Reproduction and validation

Checked against freshly fetched main 3e8601c5f63f5b2358e0fbbfb825fbbfd72cbe2b: a real attached asynchronous shell waits for a test-controlled release file. After the final foreground Markdown arrives, main retains the active parent and InProgress state. Releasing the shell unblocks parent completion. The identical probe passes with this change while the shell remains running.

Local macOS validation:

  • 879 Copilot provider unit tests passed, with 1 existing skipped test.
  • Four persistent real-provider/mock-model integration cases passed: hello, attached shell, detached shell, and idle release/resume.
  • Two strict SDK replays passed: simple message and repeated retained-background-child follow-ups.
  • The independent before/after native-shell probe failed on unmodified main and passed with this patch; temporary probe removed.
  • Client type-check, scoped ESLint, repository formatting, hygiene hooks, and full npm run build-fast passed.

One replay attempt timed out at test-server startup before either scenario began; both passed on a standalone rerun without code or timeout changes. Tests use isolated temporary state and local synthetic/captured model responses, with no real model requests. Cross-platform CI is still to be validated.

The original symptom was observed in Autopilot, but the reproduced host completion-boundary issue is not Autopilot-only. The historical export cannot identify every native-runtime condition in that particular run.

Copilot AI balanced review requested due to automatic review settings September 14, 2026 00:52

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Turn-replacement and session-lifecycle races can strand turns, restarts, or late child edit metadata.

Get a fresh assessment by requesting another Copilot review.

Review tier: Balanced (auto)
Findings: 1 High severity · 2 Medium severity

Note

Copilot is running an experiment and ran this review at Balanced.

Open findings (3)
What changed in this PR

Separates foreground turn completion from session-wide background work while protecting active tasks from release and restart.

Changes:

  • Uses request-scoped foreground completion and root assistant-idle events.
  • Preserves background subagent streams and defers lifecycle cleanup.
  • Adds lifecycle, race, and shell regression coverage.
File Description
providerIntegration/​README.md Documents shell lifecycle coverage.
copilotMockLlm.integrationTest.ts Tests attached and detached shells.
copilotAgentSession.test.ts Covers foreground completion and background work.
copilotAgent.test.ts Tests deferred restart races.
copilotSessionWrapper.ts Exposes assistant-idle events.
copilotAgentSession.ts Implements foreground/background lifecycle separation.
copilotAgent.ts Protects background work from release and restart.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1891 to +1897
private async _completeForegroundRequest(request: symbol, abortToken: CancellationToken): Promise<void> {
const turn = this._currentTurn.value;
if (turn?.foregroundRequest === request) {
turn.markProviderCallResolved();
await this._completeTurnAfterTools(turn, abortToken);
}
}
Comment on lines +1208 to +1211
const sessions = this._allLiveSessions();
if (sessions.length > 0) {
const backgroundWork = await Promise.all(sessions.map(session => session.hasRunningBackgroundWork()));
if (backgroundWork.some(Boolean)) {
this._agentMergeTurn = false;
this._streamingToolCalls.clear();
this._streamingToolDisplaySchedulers.clearAndDisposeAll();
this._clearStreamingToolCalls();

This branch has not been deployed

No deployments
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