Skip to content

Fix terminal worktree cleanup and token reporting#58

Merged
nickbeau merged 1 commit into
mainfrom
feature/worktree-cleanup-token-reporting
Apr 26, 2026
Merged

Fix terminal worktree cleanup and token reporting#58
nickbeau merged 1 commit into
mainfrom
feature/worktree-cleanup-token-reporting

Conversation

@nickbeau
Copy link
Copy Markdown
Contributor

Spec References

  • SPEC.md Section 8.5: terminal tracker state stops running workers and cleans the workspace.
  • SPEC.md Section 8.6: startup and terminal workspace cleanup semantics.
  • SPEC.md Section 13.5: token accounting must prefer absolute totals, ignore delta/generic payloads, and track deltas from last reported totals.
  • SPEC.md Section 15.1/15.2: workspace containment and subprocess safety remain mandatory.
  • SPEC.md Section 17.2, 17.4, 17.5, and 17.6: conformance coverage for workspace cleanup, reconciliation, protocol usage extraction, and token/rate-limit observability.

Summary

This change tightens terminal cleanup, token accounting, and Git worktree preparation reliability for Symphony's v1 orchestrator.

Terminal Worktree Cleanup

  • Persists terminal stop intent before canceling live issue workers, so the runner can reliably observe RunStopReasons.Terminal and the CleanupWorkspaceOnStop flag.
  • Cleans worktrees for tracked retrying issues when GitHub reports the issue has moved to a configured terminal state such as Closed.
  • Releases retry queue entries and active dispatch claims for retrying issues that become terminal.
  • Records terminal cleanup metadata on workspace records when cleanup succeeds or the workspace is already absent.

Token Reporting

  • Stops treating ordinary per-event usage maps as cumulative token totals.
  • Continues accepting absolute usage payloads such as thread/tokenUsage/updated, total_token_usage, totalTokenUsage, token_usage, and tokenUsage.
  • Still accepts a nested usage map when the event itself is an absolute token usage update.
  • Resets LastReported*Tokens when a retry run starts a fresh agent attempt so new absolute session totals are counted correctly without losing the aggregate run totals.

Git Worktree Config Lock Fix

  • Creates new per-issue worktree branches with git worktree add --no-track -b ... so Git does not attempt to write upstream branch configuration.
  • Serializes shared-clone Git mutations inside the host process to reduce .git/config and worktree metadata contention when multiple issues dispatch concurrently.
  • Avoids unnecessary git remote set-url origin ... calls when the shared clone already points at the configured remote.
  • Flushes Git redirected output before checking command results.

Documentation

  • Updates README.md to describe retrying terminal workspace cleanup and absolute-only token totals.
  • Updates docs/UserGuide.md to document terminal retry cleanup, token accounting behavior, and upstream-free worktree branch creation.

Tests

  • dotnet test tests/Symphony.Integration.Tests/Symphony.Integration.Tests.csproj --filter "WorkspaceManagerTests|OrchestrationTickServiceTests|CodexAgentRunnerTests"
  • dotnet test

Full suite result: 13 core tests passed, 119 integration tests passed, 1 real GitHub integration test skipped because it is gated by opt-in credentials.

Notes

  • The pre-existing untracked .claude/ directory was intentionally not staged.
  • No migrations were required.

Persist terminal stop intent before canceling live runs, clean retry worktrees when tracked issues close, avoid counting generic usage maps as absolute token totals, and reduce shared clone git config writes during worktree creation.
Copilot AI review requested due to automatic review settings April 26, 2026 04:20
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR tightens Symphony’s v1 orchestrator behavior around terminal workspace cleanup, absolute-only token usage accounting, and safer/more reliable Git worktree preparation under concurrent dispatch.

Changes:

  • Persist terminal stop intent before canceling live runs; extend terminal cleanup to tracked retrying issues (including releasing retry/claim state and recording cleanup metadata).
  • Token accounting now ignores generic per-event usage maps except for explicit absolute token-usage update events, and resets “last reported” totals when a retry starts a fresh attempt.
  • Git worktree creation now uses upstream-free branches (--no-track) and serializes shared-clone Git mutations to reduce .git/config contention.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/Symphony.Integration.Tests/WorkspaceManagerTests.cs Adds coverage ensuring worktree branches don’t configure upstream tracking; updates git helper to assert nonzero exit codes.
tests/Symphony.Integration.Tests/OrchestrationTickServiceTests.cs Adds integration tests for persisting terminal stop state pre-cancel, retry workspace cleanup on terminal transition, and token “last reported” reset on retry attempt restart.
tests/Symphony.Integration.Tests/CodexAgentRunnerTests.cs Adds tests verifying generic usage is ignored on ordinary events but accepted for absolute token usage update events.
src/Symphony.Infrastructure.Workspaces/GitWorktreeWorkspaceManager.cs Adds per-shared-clone locking; switches new worktree branch creation to --no-track; avoids redundant remote set-url calls; improves process output flushing.
src/Symphony.Infrastructure.Agent.Codex/CodexAgentRunner.cs Restricts usage parsing to absolute token usage update contexts; continues accepting known absolute total token payload shapes.
src/Symphony.Host/Services/OrchestrationTickService.cs Passes instanceId into tracked-issue state refresh to support terminal retry cleanup/claim release.
src/Symphony.Host/Services/OrchestrationTickService.Reconciliation.cs Persists stop intent before attempting to stop live execution, enabling runners to observe terminal stop semantics reliably.
src/Symphony.Host/Services/OrchestrationTickService.Persistence.cs Adds tracked terminal cleanup for retrying issues during cache refresh; releases retry queue/claim state; records cleanup metadata.
src/Symphony.Host/Services/OrchestrationTickService.Dispatch.cs Resets SessionId and last-reported token totals when re-dispatching a retrying run as a fresh attempt.
docs/UserGuide.md Documents absolute-only token accounting and terminal cleanup behavior for retrying issues; notes upstream-free worktree branches and serialized Git mutations.
README.md Updates high-level runtime behavior summary for retrying terminal cleanup and absolute-only token totals.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nickbeau nickbeau merged commit de7e675 into main Apr 26, 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