Skip to content

feat: detach foreground tasks to background - #821

Merged
kermanx merged 10 commits into
mainfrom
xtr/foreground-task-detach
Jun 20, 2026
Merged

feat: detach foreground tasks to background#821
kermanx merged 10 commits into
mainfrom
xtr/foreground-task-detach

Conversation

@kermanx

@kermanx kermanx commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue. This addresses a maintainer-requested core/API capability for moving foreground shell and subagent tasks into the background.

Problem

Foreground Bash and subagent tasks are managed as blocking tool calls. If a model starts one without run_in_background, there was no core RPC path for a client to detach that already-running foreground task and continue treating it as a background task.

What changed

  • Added a background-task detach path through the agent-core RPC/API surfaces and SDK bindings.
  • Unified foreground and background Bash/subagent execution under BackgroundManager, so tasks can start foreground and later detach without switching lifecycle owners.
  • Moved task-specific output, abort, detach, timeout, and notification behavior into the manager/task boundaries with focused coverage for Bash, subagent, RPC events, persistence, and task tools.
  • Added a patch changeset for the affected packages.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Verification:

  • pnpm --filter @moonshot-ai/agent-core typecheck
  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/tools/background/task-tools.test.ts
  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/tools/bash.test.ts test/tools/shell-quoting.test.ts test/tools/shell-cancel.test.ts
  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/agent/background/agent-timeout.test.ts test/agent/background/manager.test.ts test/agent/background/rpc-events.test.ts test/tools/background/task-tools.test.ts test/tools/agent.test.ts test/tools/builtin-current.test.ts test/session/lifecycle-hooks.test.ts test/agent/bg-idle-notification-repro.test.ts test/agent/background/ids.test.ts test/tools/bash.test.ts test/tools/bash-env.test.ts test/tools/shell-cancel.test.ts test/tools/shell-quoting.test.ts
  • git diff HEAD --check
  • Read-only diff audit found no internal identifier leaks; one Bash foreground failure handling finding was fixed before this PR.

@changeset-bot

changeset-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6b3ea06

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/kimi-code-sdk Patch
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jun 16, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6b3ea06
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6b3ea06

commit: 6b3ea06

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f0c9dd763e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/utils/promise.ts Outdated
@kermanx

kermanx commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b3995e1e90

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/agent/background/process-task.ts Outdated
Comment thread packages/agent-core/src/agent/background/agent-task.ts Outdated
@kermanx

kermanx commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 64ad9611a2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/tools/builtin/shell/bash.ts
kermanx added 3 commits June 17, 2026 12:48
Foreground Bash commands (run_in_background omitted) were registered with
the BackgroundManager as detached:false, which immediately persisted task
metadata and streamed the full stdout/stderr to tasks/<id>/output.log. Those
terminal detached:false tasks are filtered out of every listing, so ordinary
foreground commands left undiscoverable full-output logs in the session
directory — a disk-growth regression for large or frequent commands.

Foreground tasks now keep their output in memory and touch disk only once
they actually detach (or spill past a 1 MiB in-memory bound). On detach the
buffered pre-detach output is flushed first so output.log stays the complete,
in-order record. A foreground task that finishes without detaching or
spilling writes nothing to disk. Spilled/detached records are persisted and
kept as before — not deleted.
…etach

# Conflicts:
#	packages/agent-core/src/rpc/core-impl.ts
@kermanx

kermanx commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e480603aff

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/tools/builtin/shell/bash.ts Outdated
Comment thread packages/protocol/src/events.ts
@kermanx

kermanx commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4291fd8e64

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/foreground-task-detach.md Outdated
Comment thread packages/agent-core/src/tools/builtin/shell/bash.ts Outdated
@kermanx

kermanx commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 904ad0f73c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/agent/background/process-task.ts Outdated
@kermanx

kermanx commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6b3ea06ed1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/foreground-task-detach.md
@kermanx
kermanx merged commit ba64072 into main Jun 20, 2026
8 checks passed
@kermanx
kermanx deleted the xtr/foreground-task-detach branch June 20, 2026 13:24
@github-actions github-actions Bot mentioned this pull request Jun 20, 2026
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.

1 participant