Skip to content

feat(worker): govern subprocesses across workers and gateways - #124

Merged
nixuuu merged 1 commit into
devfrom
feat/process-governor-1
Nov 5, 2026
Merged

nixuuu merged 1 commit into
devfrom
feat/process-governor-1

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 5, 2026

Copy link
Copy Markdown
Member

Stage 1 of 2. Every subprocess Canopy starts (git, agent CLI probes, utilities) now goes through the shared @canopy/process-governor package. That stops subprocess storms, for example a full worktree scan running hundreds of git processes at once.

  • Budgets: at most 4 git processes and 2 probes at a time; utilities at most 2 per 5 s.
  • Priority: user actions run before background work. Scheduled scans are marked background through an additive proto field.
  • Caching: identical reads run once (single-flight) and are cached for 1 s. Deletions and final checks always read fresh.
  • Timeouts: a timeout kills the whole process group. Queued work that has gone stale is dropped.
  • Agent CLI probes: --version checks are now async and run once per CLI, cached for 60 s, instead of blocking the event loop.
  • Git libraries: reviewed in docs/process-governor.md; Canopy stays on the git CLI for now.

tests/integration/process-governor.test.ts runs 100 status reads over 30 real worktrees. It checks that at most 4 processes run at once and only 30 start in total.

@nixuuu
nixuuu added this pull request to stack #126 November 5, 2026 05:37
@nixuuu
nixuuu force-pushed the feat/process-governor-1 branch from 2327f15 to ec3f7f3 Compare November 5, 2026 05:51
@nixuuu
nixuuu marked this pull request as ready for review November 5, 2026 05:57
@nixuuu
nixuuu merged commit 630ddeb into dev Nov 5, 2026
10 checks passed
@nixuuu
nixuuu deleted the feat/process-governor-1 branch November 5, 2026 05:57
nixuuu added a commit that referenced this pull request Nov 10, 2026
…#482)

Cleanup previews could reuse Git output cached for one second by the
process governor, showing a dirty checkout immediately after external
changes were removed, or a clean checkout immediately after files were
added. `ScanWorktrees` now uses the existing `freshGit` boundary while
preserving process admission, background priority and removal safety
checks. The automation test warms the scan, changes files outside
Canopy, and asserts immediate dirty → clean → dirty → clean results
before cleanup. The worktree documentation describes this freshness
guarantee.

The worktree fixture also waited only for `thread.status === idle`.
`threadReady` can set that before the automation turn or its command has
settled. It now waits for the worktree's actual busy state before
deleting or archiving; preview assertions immediately after
delete/settings changes remain direct assertions, without retries. A
controlled delayed-ack run reproduced the original refusal with clean
Git state, a cancelled turn and a pending `startTurn`. The original CI
log reports only `wouldRemove`, so it cannot distinguish that refusal
from stale scan state.

The desktop/phone CI failure in run 42786085102, attempt 1, rejected a
valid transient `syncing` update after both clients' items and turns
already matched. The test now waits for completion of the specific reply
turn and convergence after recovery, with 100 ms revision probes
exercising resubscriptions. Local baseline runs exposed another fixture
race: after the parent interruption, its queued question turn could
start while `holdTurns` was still enabled and block the final reply
forever. Releasing the hold before ending the parent, then observing the
question turn complete, removes that race. A controlled run reproduced
the stuck interaction turn and queued final message. No production
subscription behavior changes.

Validation:

| Separate sequential file runs | Before | After |
| --- | --- | --- |
| `worktree-automation.test.ts` | 28 passed / 2 failed | 30 passed / 0
failed |
| `thread-multiclient.test.ts` | 29 passed / 1 failed | 30 passed / 0
failed |

Each file ran 30 times before and after, one file at a time. All 30
final phone runs exercised revision resubscription. The local baseline
phone failure was the held-question timeout, distinct from CI's invalid
no-syncing assertion. An intermediate readiness-only revision failed 8/8
worktree runs and exposed the cached Git result; those failures are
excluded from the final counts.

`bun install`, `bun run typecheck`, `bun run lint`, `bun run
format:check`, and `git diff --check` passed. Verification was scoped to
the requested integration files and static checks under the supplied
execution limits; the full suite, generation check and web build were
not run. Scratch logs and fault-injection diagnostics remain outside the
repository and are not committed. No schema, proto, UI copy or UI test
changes; copy audit is not applicable.

Fixes: #124
Related: #473, #478
nixuuu added a commit that referenced this pull request Nov 18, 2026
nixuuu added a commit that referenced this pull request Nov 18, 2026
## What changed

- Report Git timeouts, output-limit failures and nonzero checkout exits
separately. Keep raw command output out of the new checkout errors and
map worker and link failures to fixed messages in `spawn_thread`.
- Mark the new branch and the temporary Git worktree lock with the same
attempt ID. After a failed checkout, remove only a matching worktree and
the unchanged, unused branch created by this attempt. Preserve the
original failure and report when cleanup is incomplete.
- Verify that rollback waits for the process to close and for the
attempt to stop its descendants to finish. Keep the 60 s checkout and 90
s gateway budgets. A gateway deadline or disconnect can still leave the
outcome unconfirmed, so the agent prompts the caller to check before
retrying.

The report involved a Windows 11 checkout of about 58,000 tracked files
with CRLF conversion and Git LFS. Both reported nightly.70 and the
edited report's nightly.74 already include the 60 s checkout limit. The
original error alone cannot distinguish elapsed time from excess output.
The fixtures reproduce those failure paths with real Git on Linux. The
original Windows checkout and its initiating cause remain unverified. A
longer checkout budget would need corresponding worker, gateway and
client deadline changes based on that evidence.

## Verification

- Verified PR head `dc73d853` on `nix-pc` (Linux, Bun 1.4.4, Git
2.56.0). Ran `check:tmp` on the worker worktree tests and five
integration files covering worktrees, creation progress, creation
failures, automation and the process governor. Also ran the worker's
device preview key tests. In all, 51 passed, zero failed, and no
temporary test files were left.
- The tests exercise a checkout filter that times out before completing,
a hook that exceeds output capture, a hook that exits nonzero, a
successful retry with the same branch name, a ref modified during
checkout, preservation of pre-existing worktrees and refs, both gRPC
callers, and an older worker that reports the two limits under one code.
- A generated 58,000-file CRLF checkout completed and unlocked in 2.14 s
on Linux. The fixture used small files and had no LFS filters.
- The prior CI run failed in [Tests
(6/8)](https://github.com/itsoltech/canopy/actions/runs/44361247730/job/135003276782)
because device preview depended on the shared process error text. The
runner now preserves that text and exposes the precise reason to Git.
The existing device preview assertions passed locally.
- Affected worker, gateway and process-governor typechecks, edited-file
lint and formatting, and `git diff --check` passed. Full verification
runs in CI.
- Copy audit (§8). The ambiguous limit message now identifies time or
output, checkout failure names Git settings and filters, and an
incomplete rollback tells the user what to check. New messages use
static sentence-case text without dynamic paths or names. The creation
step and retry were tested through the UI's gRPC API. No browser or
Windows runtime verification.

Fixes: #74
Fixes: #90
Fixes: #124
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