Repository navigation
feat(tools): background Bash tasks that outlive the turn - #1683
Merged
Merged
Conversation
A Bash call belonged to its turn: the default 60-second timeout killed the process group and an abort tore down every local tool controller, so a dev server started from a chat turn died with the turn that spawned it. Long-lived work needs an owner that is not the turn. host-core now owns those processes in a `BackgroundTaskRegistry`: the call returns a `backgroundTaskId` as soon as the shell starts, the task never joins the per-tool-call cancellation registry, output is kept in bounded per-stream rings, and `AppState::shutdown` stops every running task so an application exit never leaves an orphan. `backgroundTasks.list|output|stop` plus a `backgroundTasks.changed` notification expose the registry; the composer renders it as a running-tasks bar above the input, with output and stop controls. A start bumps the change counter, without which the bar would stay empty for exactly as long as the process was alive. The Bash schema advertises `runInBackground` and the tool description explains when to use it. Tools, host RPC, and the E2E plan (E2E-265) are updated in both shipped locales.
Collaborator
|
GOOD , 我有几点需要询问
|
Keep background output draining after process exit and signal shutdown without taking output locks, so busy commands cannot lose their final logs or miss host cleanup. Bound the Composer task list and refresh only the selected preview with stale-response protection and retryable errors. Replace the brittle Bash timeout assertion and cover the user path and ten-task output load.
Include the target repository's current main without rewriting the published background-task commit. This preserves fast-forward delivery to the existing PR branch and satisfies the PR base ancestry gate.
Contributor
Author
|
@Tioit-Wang 核查后,这三点确需修正:
在新的提交中修复了 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A
Bashcall belonged to its turn: the default 60-second timeout killed itsprocess group, and an abort tore down every local tool controller. A dev server
started from a chat turn therefore died with the turn that spawned it. This adds
host-owned background tasks so long-lived commands survive the turn.
host-core
BashacceptsrunInBackground: trueand returnsbackgroundTaskIdas soonas the shell starts. No timeout applies, and the task never joins the
per-tool-call cancellation registry, so
tools.abort/agent.abortcannotreach it.
BackgroundTaskRegistryowns the process tree with the same containmentas the foreground runner (job object on Windows, process group on Unix):
a bounded 256KB output ring per stream, at most 32 concurrent running tasks,
and a bounded history of 16 finished tasks.
AppState::shutdownsignals every running task, so quitting the app neverleaves an orphan process behind.
backgroundTasks.list,backgroundTasks.output,backgroundTasks.stop, plus abackgroundTasks.changednotification driven bya process-wide change counter.
desktop
elapsed time,
Output, andStop.Outputshows the bounded stdout/stderr tails; the elapsed time refreshes onceper second while a task runs.
Rationale
Long-lived work needs an owner that is not the turn. The foreground runner is
correct for commands whose output the turn depends on — its timeout and abort
wiring are the reason a hung command cannot wedge a session — so the fix is a
separate, host-owned registry with explicit stop and shutdown semantics rather
than weakening the foreground path.
Affected specs, ADRs, and E2E scenarios
docs/spec/03-runtime/03-tools-and-permissions.md§5 — therunInBackgroundargument and its lifecycle.
docs/spec/03-runtime/06-host-rpc-protocol.md§4 — the three methods and thebackgroundTasks.changednotification.docs/spec/06-delivery/04-e2e-test-plan.md— new scenario E2E-265 plus itstraceability-matrix row (C / E / F, M6+, Partially automated).
docs/zh-CN/spec/....methods) and alters no existing contract, persisted format, data ownership, or
security boundary. The frozen process model is unchanged — the registry lives
in host-core and the renderer only reads state.
Compatibility, migration, security, and risks
Bashcalls behave exactly as before(
runInBackgrounddefaults tofalse), no stored data changes, and the newRPC methods are new channels in the existing IPC surface.
process.
call goes through the same permission evaluation and approval as a foreground
one. Bound by design: 32 concurrent tasks, 256KB per output stream, and no
orphan processes after app exit.
cancellation, its own approval wait is not interruptible by
tools.aborteither; the task is meant to be stopped from the bar.
apps/desktop's fullnode --testsuite is environment-sensitive and was inconclusive in thesandbox (see Verification), so the PR integration gate should confirm it on CI.
Screenshots
Test plan
runInBackground: true(a local dev server).
backgroundTaskId, and the bar appears above thecomposer with a growing elapsed time.
keeps running past the abort and past the 60-second Bash timeout.
Output, then pressStop— the row settles toStopped.Verification
cargo fmt -p host-core --checkcargo clippy -p host-core --all-targetstranscripts.rswarning)cargo test -p host-core --lockedmain(config_sync ×2, data_relocation ×2, remote_tools ×2)background_tasks::testsapps/desktop/test/background-tasks.test.mjsapps/desktop/test/error-code-registry.test.mjspnpm --filter @pi-desktop/desktop typecheckbiome lint,check-style-tokens@pi-desktop/i18n,@pi-desktop/sharedcheck:locales,check:docs, tests)apps/desktopfullnode --testManual pass: a
node -eloop started withrunInBackgroundsurvived a turnabort and the 60-second Bash timeout, streamed its output into the bar, stopped
from the bar, and left no orphan process after quitting the app.
No linked issue: reported directly by the maintainer.