Skip to content

feat(agent-manager): add delete action to agent_manager tool - #13056

Open
romanstetsenko wants to merge 2 commits into
Kilo-Org:mainfrom
romanstetsenko:feat/agent-manager-delete-tool
Open

feat(agent-manager): add delete action to agent_manager tool#13056
romanstetsenko wants to merge 2 commits into
Kilo-Org:mainfrom
romanstetsenko:feat/agent-manager-delete-tool

Conversation

@romanstetsenko

Copy link
Copy Markdown

Summary

Adds a delete action to the agent_manager tool so driven agents and orchestration flows can tear down their own Agent Manager worktrees. delete removes the worktree card, its .kilo/worktrees/<name> directory, and its local branch — mirroring the UI "Delete worktree" button — given a worktreeID from action=list.

This is the programmatic equivalent of the UI button that today only exists behind the agentManager.deleteWorktree message; stop only ends a session and leaves the worktree, directory, and branch behind.

Why

A core Agent Manager use case is fan-out (one worktree per task/approach). Creation is scriptable; teardown is not. Driven agents and orchestration flows cannot clean up their own worktrees without dropping to manual git/fs commands.

What changed

  • packages/opencode/src/kilocode/agent-manager/protocol.ts: DeleteRequest (operation: "delete", worktreeID) + DeleteResult; added to the Request/Result unions; matches now matches delete by operation.
  • packages/opencode/src/kilocode/tool/agent-manager.ts: DeleteParams, wired into Params/WireParams (action enum + worktreeID), and an execute branch that asks the agent_manager/delete permission then calls host.request({ operation: "delete", ... }).
  • packages/opencode/src/kilocode/permission/agent-manager.ts: delete added to the hardened side-effect patterns (a broad agent_manager: allow no longer silently grants deletion).
  • packages/opencode/src/kilocode/agent-manager/service.ts: matches update for delete.
  • packages/kilo-vscode/.../orchestration-bridge.ts (+ orchestration-setup.ts, AgentManagerProvider.ts): handles the delete operation through deleteLifecycleWorktree, resolving the project context by directory like the existing close path. closeSession``/deleteWorktree` extracted into helpers to stay under the lint complexity limit.
  • packages/sdk/js/src/v2/gen/types.gen.ts + packages/sdk/openapi.json: hand-mirrored to the generator output (CI regenerates these).
  • Tests: tool-level delete test, bridge-level delete test (real WorktreeStateManager), and updated schema/permission tests.

Notes

  • The optional removeStale action (for already-gone worktrees) from the issue was intentionally left out to keep scope to the core ask.
  • Verified locally with bun (typecheck, lint, knip, unit tests all pass) after installing bun in a local temp dir and running bun install --ignore-scripts (the repo postinstall writes .git/config, which was permission-denied here and is non-essential).

🤖 Generated with Kilo CLI

if (!state.getWorktree(worktreeID)) {
throw new OrchestrationError("unknown_session", "The worktree is not managed by this Agent Manager workspace")
}
await this.options.delete(worktreeID, origin.directory)

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.

WARNING: delete reports success even when the deletion was aborted

options.delete resolves identically for success and failure because onDeleteWorktree/deleteLifecycleWorktree return null on every path. When clearRun fails to stop a Run/Setup script terminal, deleteLifecycleWorktree aborts the deletion (posting an error only to the webview) and the worktree card, .kilo/worktrees/<name> directory, and branch all remain — yet the caller still receives { operation: "delete", deleted: true } and the tool prints "Deleted Agent Manager worktree …". The UI path has a human who sees the error toast; a driven agent or orchestration flow will act on a false success. The same applies if the worktree disappears between the pre-check above and the actual delete (lifecycle just logs and returns).

Consider failing the request when the worktree is still present after the await — state here is the same WorktreeStateManager instance the lifecycle mutates, and removeWorktree runs synchronously on the success path before the async disk removal:

await this.options.delete(worktreeID, origin.directory)
if (state.getWorktree(worktreeID)) {
  throw new OrchestrationError("host_error", "The worktree could not be deleted (a Run/Setup script may still be running)")
}

Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 0500cda: deleteWorktree now throws host_error if the worktree is still present in state after the teardown await, so an aborted deletion (e.g. Run/Setup script terminal could not be stopped) is no longer reported as success. Added a bridge test covering the aborted case.

*/
export function harden(permission: string, pattern: string, rule: Rule): Rule {
if (permission !== "agent_manager" || !["prompt", "stop", "move"].includes(pattern) || rule.action !== "allow") return rule
if (permission !== "agent_manager" || !["prompt", "stop", "move", "delete"].includes(pattern) || rule.action !== "allow") return rule

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.

SUGGESTION: The doc comment above harden is now stale

It still reads "Prompting, stopping, or moving an existing Agent Manager session has an external side effect." — worth mentioning deleting so the enumeration matches the pattern list updated on this line.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 0500cda: the harden doc comment now enumerates prompting, stopping, moving, and deleting.

@kilo-code-bot

kilo-code-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Incremental review of 0500cdaf..c21c64f. The delta contains no PR-authored changes: the branch was rebased onto the v7.4.21 release commit from main, so the only new content is release artifacts (consumed changesets, version bumps, CHANGELOGs, bun.lock, packages/extensions/zed/extension.toml). All PR source files are byte-identical to the previously reviewed head, and both prior findings remain resolved at the new HEAD (deleteWorktree still throws host_error on aborted teardown; the harden doc comment still enumerates deleting).

Files Reviewed (incremental delta + re-verified files)
  • packages/kilo-vscode/src/agent-manager/orchestration-bridge.ts (re-verified at HEAD, unchanged)
  • packages/opencode/src/kilocode/permission/agent-manager.ts (re-verified at HEAD, unchanged)
  • Release artifacts from main (.changeset/*, package.json versions, CHANGELOG.md files, bun.lock, packages/extensions/zed/extension.toml) — not PR-authored
Previous Review Summaries (2 snapshots, latest commit 0500cda)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 0500cda)

Status: No Issues Found | Recommendation: Merge

Incremental review of e9ed0a38..0500cdaf. Both previous findings are resolved: the bridge now rejects a delete whose teardown was aborted (worktree still present in state after the lifecycle call) instead of reporting a false success, and the harden doc comment now enumerates deleting. The state-instance identity assumption behind the new check was verified end-to-end (ready()/getState() and deleteLifecycleWorktree resolve the same cached WorktreeStateManager, and removeWorktree runs synchronously on the success path before the delete promise resolves).

Files Reviewed (3 files)
  • packages/kilo-vscode/src/agent-manager/orchestration-bridge.ts
  • packages/kilo-vscode/tests/unit/agent-manager-orchestration-bridge.test.ts
  • packages/opencode/src/kilocode/permission/agent-manager.ts

Previous review (commit e9ed0a3)

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 1
Issue Details (click to expand)

WARNING

File Line Issue
packages/kilo-vscode/src/agent-manager/orchestration-bridge.ts 335 delete replies deleted: true even when deleteLifecycleWorktree aborts (e.g. Run/Setup script terminal fails to stop) — false success reported to the calling agent

SUGGESTION

File Line Issue
packages/opencode/src/kilocode/permission/agent-manager.ts 9 harden doc comment still says "Prompting, stopping, or moving" — not updated for delete
Files Reviewed (14 files)
  • .changeset/agent-manager-delete-worktree-tool.md
  • packages/kilo-vscode/src/agent-manager/AgentManagerProvider.ts
  • packages/kilo-vscode/src/agent-manager/orchestration-bridge.ts - 1 issue
  • packages/kilo-vscode/src/agent-manager/orchestration-setup.ts
  • packages/kilo-vscode/tests/unit/agent-manager-orchestration-bridge.test.ts
  • packages/opencode/src/kilocode/agent-manager/protocol.ts
  • packages/opencode/src/kilocode/agent-manager/service.ts
  • packages/opencode/src/kilocode/permission/agent-manager.ts - 1 issue
  • packages/opencode/src/kilocode/tool/agent-manager.ts
  • packages/opencode/src/kilocode/tool/agent-manager.txt
  • packages/opencode/test/kilocode/agent-manager-tool.test.ts
  • packages/opencode/test/kilocode/permission/agent-manager-prompt.test.ts
  • packages/sdk/js/src/v2/gen/types.gen.ts
  • packages/sdk/openapi.json

Fix these issues in Kilo Cloud


Reviewed by kimi-k3 · Input: 32.5K · Output: 4.5K · Cached: 177.4K

Review guidance: REVIEW.md from base branch main

Your Name added 2 commits August 11, 2026 10:28
Add a delete action to the agent_manager tool so driven agents and orchestration flows can tear down their own Agent Manager worktrees. delete removes the worktree card, its .kilo/worktrees/<name> directory, and its local branch, mirroring the UI 'Delete worktree' button, given a worktreeID from action=list.

- protocol/tool: DeleteRequest + DeleteResult, wired through host.request
- bridge: handle delete via deleteLifecycleWorktree (extracted closeSession/deleteWorktree helpers to stay under lint complexity)
- SDK types.gen.ts + openapi.json hand-mirrored to generator output (CI regenerates)
…mission doc

Address review comments on the delete action.
@romanstetsenko
romanstetsenko force-pushed the feat/agent-manager-delete-tool branch from 0500cda to c21c64f Compare August 11, 2026 08:28
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