# Commodore watchdog — design

**Status:** draft
**Date:** 2026-08-04
**Author:** orchestrator (brainstorming session with user)

## Problem

The opencode-armada orchestrator ("Commodore") dispatches background subagents (galleon, clipper, corvette, etc.) via the `task` tool. Per its hard rule #1 (`agents/orchestrator/prompt.template.md:71-72`), the orchestrator must keep its turn open while background work is outstanding and wait for subagent results.

When a background subagent process dies (segfault, OOM, hard kill, tmux crash) **without emitting a clean completion event**, the orchestrator is left waiting for a result that will never arrive. The voyage is wedged: the orchestrator holds the turn, the subagent is gone, and no built-in opencode mechanism surfaces the death.

Today:

- `armada-fleet.js` (`src/generator.js:654`) tracks the *lane's* tmux session heartbeat. It does not track individual dispatched subagents.
- `armada-supervision.js` (`src/generator.js:559`) nudges the orchestrator on `session.idle` only when `.opencode/fleet-status.md` exists. It does not check subagent return state.
- The orchestrator has no built-in awareness of "how long has that subagent been running."

## Goal

A thin, opt-in layer that detects when a dispatched subagent has not returned within a configured window, and nudges the orchestrator with one terse message so it can re-dispatch, abort, or ask the user. Minimal implementation, minimal token cost on the happy path.

## Non-goals

- Distinguishing "slow" from "dead" before the threshold elapses.
- Auto-re-dispatch of dead subagents (the orchestrator decides; the watchdog only reports).
- Persisting dispatch history across opencode restarts.
- Catching subagent hangs that DO eventually return (those are not failures).
- A new dashboard view (the existing fleet dashboard is unaffected).

## Design

### Architecture

New opt-in plugin `.opencode/plugins/armada-watchdog.js`, sibling to `armada-fleet.js` and `armada-supervision.js`. Loaded automatically by opencode from the project's plugin directory when `armada.yaml` has `project.supervision.watchdog: true` (or `armada init --watchdog` is passed). Mirrors the existing `plugin` / `fleet` flag pattern in `src/manifest.js:163-165`.

The watchdog is generated by `src/generator.js` (new `renderArmadaWatchdogPlugin()` function), scaffolded by `src/scaffold.js` (new branch alongside the existing `supervision.plugin` / `supervision.fleet` writes at `src/scaffold.js:357-363`), and surfaced in `armada doctor` like the other two (`src/doctor.js:119-136`).

### Components

**1. Plugin (`armada-watchdog.js`)** — auto-loaded, ~50–80 lines, in-memory state only. Listens to:

- `session.created` — record child dispatch.
- `session.completed` / `session.closed` / `session.deleted` — clear child from tracking.
- `session.idle` (on the orchestrator's own session) — check tracked children, nudge if any are past the threshold.

**2. In-memory map.** `Map<parentSessionId, Map<childSessionId, { startedAt }>>`. Cleared on plugin load. No file I/O. If opencode restarts, the watchdog forgets — accepted; a restart also re-dispatches whatever was lost. The orchestrator session is identified as the first `session.created` event seen after plugin load whose `properties.info.title` matches the lane's tmux session name (resolved via `process.env.TMUX` or the parent tmux pane's session name); if no match is found within the first event burst, the watchdog no-ops for that session.

**3. Threshold.** `project.supervision.watchdog.timeoutMs` in `armada.yaml`, default `300_000` (5 min). Single global setting; per-role overrides are out of scope.

**4. Nudge primitive.** `client.session.promptAsync` — same primitive the supervision plugin uses at `src/generator.js:619`. Pushes a one-part text message into the orchestrator's session as the first part of a new turn.

### Data flow

1. Orchestrator session starts. Plugin loads. The first `session.created` event whose `properties.info.title` matches the lane's tmux session name is recorded as the orchestrator session. (Falls back to `properties.sessionID` if the title is missing.) All subsequent child events are correlated to this parent.
2. Orchestrator dispatches a subagent via the `task` tool. opencode creates a child session and emits `session.created` for it. The plugin records `{ childId, startedAt }` under the parent's tracking map.
3. Subagent runs. Orchestrator's session may go idle while it waits (per hard rule #1).
4. On every `session.idle` of the orchestrator, the plugin walks its tracking map. Any child whose `Date.now() - startedAt > timeoutMs` AND that has not received a `session.completed` / `session.closed` / `session.deleted` is considered potentially dead. The plugin emits one consolidated nudge per idle event: `armada-watchdog: <count> dispatched subagent(s) past <timeoutMs>ms without return: <id list>. Likely dead. Re-dispatch or abort.`
5. If the subagent eventually returns, opencode emits `session.completed` (or `closed` / `deleted`). The plugin drops the entry. If the orchestrator was already nudged, the next idle event will be clean (no more past-threshold entries), so no further nudge.
6. The plugin uses the same `skipNextIdle` + `nudgedSessions` recursion guard as the supervision plugin (`src/generator.js:591-592`) so the nudge's own processing does not loop.

### Error handling

- **opencode restart.** In-memory map is lost. New dispatches start fresh. Existing dead subagents will be re-dispatched by opencode itself or remain lost; the watchdog does not try to recover history.
- **Plugin load failure.** If the plugin fails to import, opencode logs the error and the watchdog is simply absent. The rest of the system is unaffected. `armada doctor` checks for the plugin file when the flag is set.
- **Threshold too low.** False positives (nudging normal slow subagents). Mitigated by the 5-min default; tunable per project.
- **Threshold too high.** Real deaths go undetected for longer. The default is a tradeoff; documentation will suggest starting at 5 min and tuning per-role in a future iteration.
- **Mid-turn tool call.** The nudge queues via `promptAsync` until the next `session.idle`. Accepted limitation; the orchestrator is not waiting on the dead subagent during its own tool call anyway.
- **Non-orchestrator session receives the nudge.** Mitigated by matching the parent session id against the lane's tmux session name; the plugin only tracks children of a recognized orchestrator session and only nudges on that orchestrator's `session.idle`.

### Testing

Unit test added to `tests/generator.test.js` (or a new `tests/watchdog.test.js` if the existing file is too crowded). The test:

1. Renders the plugin via `renderArmadaWatchdogPlugin()`.
2. Loads the rendered source via dynamic `import()` into an isolated module context (or executes it under a mock `client`).
3. Fires synthetic `session.created` events for an orchestrator + child, asserts the in-memory tracking map (exposed for test via a debug export) records the child.
4. Fires `session.idle` on the orchestrator before the threshold, asserts no nudge was sent.
5. Advances the clock past the threshold, fires `session.idle`, asserts the nudge payload matches the expected text.
6. Fires `session.completed` for the child, asserts the entry is cleared, and that the next `session.idle` does not nudge.
7. Fires a recursion scenario: idle → nudge → idle (the second idle, guarded by `skipNextIdle`, must not nudge again).

No live opencode server required. Existing test patterns in `tests/generator.test.js:414` (which fires synthetic events into the supervision plugin) are the template.

### Documentation

- One paragraph added to `AGENTS.md` under the existing opt-in supervision section (`AGENTS.md:108-110`).
- One line in the `armada init` help text (`src/cli.js:45-55`).
- A note in `TODO.md` under the "watchdog" heading if not already present.

## Implementation plan

To be produced by the writing-plans skill. Expected phases:

- **Phase 1 — Generator + scaffold + manifest.** Add `renderArmadaWatchdogPlugin()` to `src/generator.js`, extend `src/manifest.js` validation + default for `project.supervision.watchdog`, extend `src/scaffold.js` to write the plugin when the flag is set, extend `src/cli.js` `--watchdog` flag and help text, extend `src/doctor.js` to verify the file. Pure unit tests in `tests/generator.test.js` and round-trip via `tests/scaffold.test.js`.
- **Phase 2 — Plugin unit tests.** Add `tests/watchdog.test.js` covering the six scenarios above. Run full suite.
- **Phase 3 — Docs + PR.** Update `AGENTS.md` and `TODO.md`. Run `node --test 'tests/*.test.js'`. `gh pr create --base master` from the lane.

Phases 1 and 2 write disjoint files; they can run in parallel as background subagents. Phase 3 depends on both.

## Open questions

None at draft time. Threshold default (5 min) is the only knob and is documented as a starting point.
