Skip to content

feat(tools): background Bash tasks that outlive the turn - #1683

Merged
Tioit-Wang merged 4 commits into
vastsa:mainfrom
algerkong:feat/background-tasks
Oct 16, 2026
Merged

Tioit-Wang merged 4 commits into
vastsa:mainfrom
algerkong:feat/background-tasks

Conversation

@algerkong

Copy link
Copy Markdown
Contributor

Summary

A Bash call belonged to its turn: the default 60-second timeout killed its
process 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

  • Bash accepts runInBackground: true and returns backgroundTaskId as soon
    as the shell starts. No timeout applies, and the task never joins the
    per-tool-call cancellation registry, so tools.abort / agent.abort cannot
    reach it.
  • A new BackgroundTaskRegistry owns the process tree with the same containment
    as 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::shutdown signals every running task, so quitting the app never
    leaves an orphan process behind.
  • New RPC: backgroundTasks.list, backgroundTasks.output,
    backgroundTasks.stop, plus a backgroundTasks.changed notification driven by
    a process-wide change counter.

desktop

  • The composer renders a running-tasks bar above the input: state, command,
    elapsed time, Output, and Stop.
  • Output shows the bounded stdout/stderr tails; the elapsed time refreshes once
    per 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 — the runInBackground
    argument and its lifecycle.
  • docs/spec/03-runtime/06-host-rpc-protocol.md §4 — the three methods and the
    backgroundTasks.changed notification.
  • docs/spec/06-delivery/04-e2e-test-plan.md — new scenario E2E-265 plus its
    traceability-matrix row (C / E / F, M6+, Partially automated).
  • Chinese mirrors of all three under docs/zh-CN/spec/....
  • No ADR: the change is additive (one new tool argument, three new read/stop
    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

  • Compatibility: additive. Existing Bash calls behave exactly as before
    (runInBackground defaults to false), no stored data changes, and the new
    RPC methods are new channels in the existing IPC surface.
  • Migration: none required; the registry is in-memory and lives with the host
    process.
  • Security: no permission, sandbox, or path check is bypassed — a background
    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.
  • Remaining risk: because a background task does not register for
    cancellation, its own approval wait is not interruptible by tools.abort
    either; the task is meant to be stopped from the bar. apps/desktop's full
    node --test suite is environment-sensitive and was inconclusive in the
    sandbox (see Verification), so the PR integration gate should confirm it on CI.

Screenshots

start log

Test plan

  1. Ask the agent to start a long-lived process with runInBackground: true
    (a local dev server).
  2. The tool result carries backgroundTaskId, and the bar appears above the
    composer with a growing elapsed time.
  3. Abort the turn, then send another prompt in the same session — the process
    keeps running past the abort and past the 60-second Bash timeout.
  4. Expand Output, then press Stop — the row settles to Stopped.
  5. Quit the app — no orphan process remains.

Verification

Check Result
cargo fmt -p host-core --check pass
cargo clippy -p host-core --all-targets pass (only the pre-existing transcripts.rs warning)
cargo test -p host-core --locked 911 passed, 6 failed — all six pre-existing on main (config_sync ×2, data_relocation ×2, remote_tools ×2)
new background_tasks::tests 2/2 — a start must notify listeners; shutdown stops every running task
apps/desktop/test/background-tasks.test.mjs 11/11
apps/desktop/test/error-code-registry.test.mjs 2/2
pnpm --filter @pi-desktop/desktop typecheck 0 errors
biome lint, check-style-tokens pass
@pi-desktop/i18n, @pi-desktop/shared 30/30, 1268/1268
docs (check:locales, check:docs, tests) 85 locale pairs, 574 pages, 13/13
apps/desktop full node --test 1665/2005 in the sandbox; failures are environment-bound (plugin-host init, macOS notarization, network/S3) and representative files pass in isolation — CI must confirm

Manual pass: a node -e loop started with runInBackground survived a turn
abort 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.

algerkong and others added 2 commits October 15, 2026 15:31
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.
@Tioit-Wang

Copy link
Copy Markdown
Collaborator

GOOD , 我有几点需要询问

  1. todo & ask 与这个工具的显示是否有优先级设置?
  2. 是否会存在多个后台任务的情况,10个?布局是怎么样的?
  3. 有没有测试大量输出* 如10个任务都为大量内容输出时,是否会卡顿。 for i in range(100000) ; print("*"*100);time.sleep(0.05)

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.
@algerkong

algerkong commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

@Tioit-Wang 核查后,这三点确需修正:

  1. 显示优先级:无可配置优先级,固定顺序为 Todo → 后台任务 → Plan/Ask → 输入框;面板不互替,后台任务不隐藏 Ask,原有输入约束不变。
  2. 多任务布局:支持多个,最多 32 个运行中。原版会堆高所有行,10 个任务会挤占聊天区;本地修复为列表限 min(200px, 24vh) 内滚动,运行中排前且都可访问,一次只展开一个预览。已验证 1280/700/390px 和第 10 个任务。
  3. 大量输出:不能仅凭缓冲上限判定不卡。已修复展开日志只读一次、切换任务旧响应覆盖新日志,并修复退出未排空输出、关闭宿主时锁竞争漏发停止信号。现在只轮询展开日志,读取结束后每秒刷新,未展开不传 Renderer;预览上限 stdout 8,000、stderr 4,000 字符。

在新的提交中修复了

@Tioit-Wang
Tioit-Wang merged commit 68e2d26 into vastsa:main Oct 16, 2026
6 checks passed
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