diff --git a/.changeset/activity-events-log.md b/.changeset/activity-events-log.md deleted file mode 100644 index 6fa912606..000000000 --- a/.changeset/activity-events-log.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-cli": patch ---- - -Add SQLite-backed activity event logging for session and lifecycle diagnostics, plus `ao events` commands for listing, searching, and inspecting event log stats. diff --git a/.changeset/agent-plugin-safety-costs.md b/.changeset/agent-plugin-safety-costs.md deleted file mode 100644 index 21cb52d38..000000000 --- a/.changeset/agent-plugin-safety-costs.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -"@aoagents/ao-plugin-agent-codex": patch -"@aoagents/ao-plugin-agent-claude-code": patch -"@aoagents/ao-web": patch ---- - -Improve Claude Code and Codex session cost estimates to account for cached-token spend, make Codex restore commands fall back to approval prompts for worker sessions instead of blindly reusing dangerous bypass flags, and register the Codex plugin in the web dashboard so native activity detection works there. diff --git a/.changeset/auto-cleanup-on-merge.md b/.changeset/auto-cleanup-on-merge.md deleted file mode 100644 index 3cc639bc1..000000000 --- a/.changeset/auto-cleanup-on-merge.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -"@aoagents/ao-core": minor ---- - -Sessions whose PRs are detected as merged now auto-terminate (tmux kill + worktree remove + metadata archive) instead of lingering in the active `sessions/` directory with a `merged` status. `ao status` and `ao session ls` stay clean without an external watchdog. - -Enabled by default. Guarded by an idleness check so in-flight agents are not killed mid-task; deferred cleanups retry on each lifecycle poll until the agent idles or a 5-minute grace window elapses. - -Opt out or tune via the new top-level `lifecycle` config in `agent-orchestrator.yaml`: - -```yaml -lifecycle: - autoCleanupOnMerge: false # preserve merged worktrees for inspection - mergeCleanupIdleGraceMs: 300000 # grace window before forcing cleanup -``` - -`sessionManager.kill()` now takes an optional `reason` (`"manually_killed" | "pr_merged" | "auto_cleanup"`) and returns `KillResult` (`{ cleaned, alreadyTerminated }`) instead of `void`. All existing call sites ignore the return value so this is backward-compatible in practice. - -Closes #1309. Part of #536. diff --git a/.changeset/cli-session-ls-terminated.md b/.changeset/cli-session-ls-terminated.md deleted file mode 100644 index 792882282..000000000 --- a/.changeset/cli-session-ls-terminated.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-cli": patch ---- - -`ao session ls` hides terminal sessions in text output by default; use `--include-terminated` for the full text list. diff --git a/.changeset/collapsible-agent-reports.md b/.changeset/collapsible-agent-reports.md deleted file mode 100644 index d8e31abd1..000000000 --- a/.changeset/collapsible-agent-reports.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-web": patch ---- - -Make session detail agent reports collapsible and add explicit audit attribution for the session, actor, and report source command. diff --git a/.changeset/filter-terminated-sessions-from-ls.md b/.changeset/filter-terminated-sessions-from-ls.md deleted file mode 100644 index 744433762..000000000 --- a/.changeset/filter-terminated-sessions-from-ls.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -"@aoagents/ao-cli": minor -"@aoagents/ao-core": patch ---- - -`ao session ls` and `ao status` now hide terminated sessions (`killed`, `terminated`, `done`, `merged`, `errored`, `cleanup`) by default. A dim footer reports how many were hidden and how to surface them. Pass `--include-terminated` to restore the previous unfiltered output. - -Core change: `parseCanonicalLifecycle()` now preserves `pr.state="merged"` when reconstructing legacy metadata with `status=merged` but no `pr=` URL (previously collapsed to `pr.state="none"`, which made `isTerminalSession()` return false for those sessions). Also exports `sessionFromMetadata` so consumers can round-trip flat metadata through the canonical lifecycle. - -**Breaking — JSON output shape:** `ao session ls --json` and `ao status --json` now emit `{ data: [...], meta: { hiddenTerminatedCount: number } }` instead of a bare array. Scripts consuming the JSON must read `.data` for the session list. `--include-terminated` restores full data and reports `hiddenTerminatedCount: 0`. - -The existing `-a, --all` flag still only governs orchestrator visibility on `ao session ls` — it does **not** re-enable terminated sessions. Combine with `--include-terminated` when you want both. diff --git a/.changeset/fix-activity-unavailable-stuck.md b/.changeset/fix-activity-unavailable-stuck.md deleted file mode 100644 index 1ad7e562d..000000000 --- a/.changeset/fix-activity-unavailable-stuck.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-core": patch ---- - -Stop carrying forward `stuck` / `probe_failure` session truth when the runtime is still confirmed alive and activity is merely unavailable, and degrade that combination to `detecting` until stronger evidence arrives. diff --git a/.changeset/fix-bugbot-detail-dispatch.md b/.changeset/fix-bugbot-detail-dispatch.md deleted file mode 100644 index 1298f0b5d..000000000 --- a/.changeset/fix-bugbot-detail-dispatch.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -"@aoagents/ao-core": minor ---- - -Enrich lifecycle events with PR/issue context for webhook consumers. All events now carry `data.context` with `pr` (url, title, number, branch), `issueId`, `issueTitle`, `summary`, and `branch` when available, plus `data.schemaVersion: 2`. - -Additional changes: - -- Persist `issueTitle` in session metadata during spawn so it survives across restarts and is available for event enrichment. -- Refactor `executeReaction()` to accept a `Session` object instead of separate `sessionId`/`projectId` arguments. -- Add `maybeDispatchCIFailureDetails()` — when a session enters `ci_failed`, the agent receives a follow-up message with the failed check names and URLs (deduped via fingerprint so subsequent polls don't re-send the same failure set). -- `bugbot-comments` reaction dispatches an enriched message listing every automated comment inline, so the agent doesn't need to re-fetch via `gh api`. diff --git a/.changeset/fix-start-restore-and-codex-resume.md b/.changeset/fix-start-restore-and-codex-resume.md deleted file mode 100644 index 84e465c47..000000000 --- a/.changeset/fix-start-restore-and-codex-resume.md +++ /dev/null @@ -1,10 +0,0 @@ -"@aoagents/ao-cli": patch -"@aoagents/ao-web": patch -"@aoagents/ao-plugin-agent-codex": patch ---- - -Fix restore behavior across AO session recovery flows. - -- restore the latest dead-but-restorable orchestrator on `ao start` instead of silently spawning a new orchestrator when tmux is gone -- make worker session orchestrator navigation prefer the most recently active live orchestrator for the project -- make permissionless Codex restores preserve dangerous bypass semantics so resumed workers behave like fresh permissionless launches diff --git a/.changeset/fix-web-lifecycle-restore-affordances.md b/.changeset/fix-web-lifecycle-restore-affordances.md deleted file mode 100644 index d3257c31a..000000000 --- a/.changeset/fix-web-lifecycle-restore-affordances.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-web": patch ---- - -Keep closed-unmerged sessions actionable in the dashboard by removing them from the done lane unless the runtime actually ended, and hide restore controls for merged sessions that are intentionally non-restorable. diff --git a/.changeset/fix-web-tmux-resolver-wrapped-storagekey.md b/.changeset/fix-web-tmux-resolver-wrapped-storagekey.md deleted file mode 100644 index d986967c4..000000000 --- a/.changeset/fix-web-tmux-resolver-wrapped-storagekey.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -"@aoagents/ao-web": patch ---- - -Fix DirectTerminal "can't find session" when the project uses a wrapped storageKey. `ao-core` names tmux sessions as `{storageKey}-{sessionId}`, where `storageKey` can be either a bare 12-char hash or the legacy wrapped form `{hash}-{projectName}` (e.g. `361287ebbad1-smx-foundation`). The web resolver only handled the bare-hash form, so lookups for sessions like `sf-orchestrator-1` against the tmux name `361287ebbad1-smx-foundation-sf-orchestrator-1` always returned `null` and the terminal never attached (#1486). - -The resolver now looks up the owning storageKey on disk (from the session record at `~/.agent-orchestrator/{storageKey}/sessions/{sessionId}`) and asks tmux for the exact `{storageKey}-{sessionId}` name. The on-disk record is the authoritative disambiguator, so sessions whose IDs happen to be suffixes of other session IDs (e.g. looking up `app-1` while `my-app-1` exists in the same project) cannot be falsely matched. If the on-disk record is missing, the resolver still recovers bare-hash sessions via the tmux session listing as a fallback. diff --git a/.changeset/fixed-orchestrator-identity.md b/.changeset/fixed-orchestrator-identity.md deleted file mode 100644 index 578718126..000000000 --- a/.changeset/fixed-orchestrator-identity.md +++ /dev/null @@ -1,10 +0,0 @@ -"@aoagents/ao-cli": patch -"@aoagents/ao-core": patch -"@aoagents/ao-web": patch ---- - -Make project orchestrators deterministic and idempotent. - -- ensure each project uses the canonical `{prefix}-orchestrator` session instead of creating numbered main orchestrators -- make `ao start`, the dashboard, and the orchestrator API reuse or restore the canonical session -- keep legacy numbered orchestrators visible as stale sessions without treating them as the main orchestrator diff --git a/.changeset/harden-worker-branch-refresh.md b/.changeset/harden-worker-branch-refresh.md deleted file mode 100644 index f65c22467..000000000 --- a/.changeset/harden-worker-branch-refresh.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-core": patch ---- - -Harden worker branch refresh during lifecycle polling by preserving branch metadata through transient detached Git states, skipping orchestrators and active open PRs, and preventing duplicate branch adoption within a single poll cycle. diff --git a/.changeset/isolate-lifecycle-terminal-logs.md b/.changeset/isolate-lifecycle-terminal-logs.md deleted file mode 100644 index 29befe0ae..000000000 --- a/.changeset/isolate-lifecycle-terminal-logs.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-core": patch ---- - -Keep lifecycle observability and batch diagnostic logs out of user-visible terminal stderr by routing them into AO's observability audit files instead, while preserving structured traces for debugging and regression coverage. diff --git a/.changeset/late-lions-share.md b/.changeset/late-lions-share.md deleted file mode 100644 index 26f584514..000000000 --- a/.changeset/late-lions-share.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -"@aoagents/ao-core": minor -"@aoagents/ao-cli": minor ---- - -Allow workers to report non-terminal PR workflow events like `pr-created`, `draft-pr-created`, and `ready-for-review` with optional PR URL/number metadata, while keeping merged and closed PR state SCM-owned. - -**Migration:** `Session` now carries canonical lifecycle truth in `session.lifecycle` -and explicit activity-evidence metadata in `session.activitySignal`. Third-party -callers that construct `Session` objects directly must populate those fields or -route through the core session helpers that synthesize them. diff --git a/.changeset/lifecycle-activity-signal-contract.md b/.changeset/lifecycle-activity-signal-contract.md deleted file mode 100644 index 05d75dc7d..000000000 --- a/.changeset/lifecycle-activity-signal-contract.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-web": patch ---- - -Model activity evidence explicitly across lifecycle inference and dashboard rendering so missing or failed probes cannot spuriously produce idle or stuck interpretations. This also stabilizes repeated polls by preserving stronger prior lifecycle states when the only new evidence is weak or unavailable. diff --git a/.changeset/lifecycle-transitions-report-watcher.md b/.changeset/lifecycle-transitions-report-watcher.md deleted file mode 100644 index 6253f9750..000000000 --- a/.changeset/lifecycle-transitions-report-watcher.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -"@aoagents/ao-core": minor ---- - -Add centralized lifecycle transitions and report watcher for agent monitoring. - -- **Lifecycle transitions (#137)**: Centralize all lifecycle state mutations through `applyLifecycleDecision()` for consistent timestamp handling, atomic metadata persistence, and observability. -- **Detecting bounds (#138)**: Add time-based (5 min) and attempt-based (3 attempts) bounds to detecting state with evidence hashing to prevent counter reset on unchanged probe results. -- **Report watcher (#140)**: Background trigger system that audits agent reports for anomalies (no_acknowledge, stale_report, agent_needs_input) and integrates with the reaction engine. - -New exports: -- `applyLifecycleDecision`, `applyDecisionToLifecycle`, `buildTransitionMetadataPatch`, `createStateTransitionDecision` -- `DETECTING_MAX_ATTEMPTS`, `DETECTING_MAX_DURATION_MS`, `hashEvidence`, `isDetectingTimedOut` -- `auditAgentReports`, `checkAcknowledgeTimeout`, `checkStaleReport`, `checkBlockedAgent`, `shouldAuditSession`, `getReactionKeyForTrigger`, `DEFAULT_REPORT_WATCHER_CONFIG`, `REPORT_WATCHER_METADATA_KEYS` diff --git a/.changeset/native-windows-support.md b/.changeset/native-windows-support.md new file mode 100644 index 000000000..f40349b08 --- /dev/null +++ b/.changeset/native-windows-support.md @@ -0,0 +1,54 @@ +--- +"@aoagents/ao-core": minor +"@aoagents/ao-cli": minor +"@aoagents/ao": minor +"@aoagents/ao-plugin-runtime-process": minor +"@aoagents/ao-plugin-runtime-tmux": minor +"@aoagents/ao-plugin-agent-claude-code": minor +"@aoagents/ao-plugin-agent-codex": minor +"@aoagents/ao-plugin-agent-aider": minor +"@aoagents/ao-plugin-agent-opencode": minor +"@aoagents/ao-plugin-workspace-worktree": minor +"@aoagents/ao-plugin-workspace-clone": minor +"@aoagents/ao-plugin-tracker-github": minor +"@aoagents/ao-plugin-tracker-linear": minor +"@aoagents/ao-plugin-scm-github": minor +"@aoagents/ao-plugin-notifier-desktop": minor +"@aoagents/ao-plugin-notifier-slack": minor +"@aoagents/ao-plugin-notifier-webhook": minor +"@aoagents/ao-plugin-notifier-composio": minor +"@aoagents/ao-plugin-terminal-iterm2": minor +"@aoagents/ao-plugin-terminal-web": minor +"@aoagents/ao-web": minor +--- + +feat: native Windows support + +AO now runs natively on Windows. The default runtime on Windows is `process` +(ConPTY via `node-pty` + named pipes — no tmux, no WSL); the dashboard, +agents (claude-code, codex, kimicode, aider, opencode, cursor), `ao doctor`, +and `ao update` all work out of the box. Each session gets a small detached +pty-host helper that wraps a ConPTY behind `\\.\pipe\ao-pty-`, +registered so `ao stop` can reach it. + +A new cross-platform abstraction layer (`packages/core/src/platform.ts`) +centralises every platform branch behind helpers like `isWindows()`, +`getDefaultRuntime()`, `getShell()`, `killProcessTree()`, `findPidByPort()`, +and `getEnvDefaults()`. Path comparison uses `pathsEqual` / +`canonicalCompareKey` to handle NTFS case-insensitivity. PATH wrappers for +agent plugins (`gh`, `git`) ship as `.cjs` + `.cmd` shims on Windows; +`script-runner` runs `.ps1` siblings of `.sh` scripts via PowerShell. New +`ao-doctor.ps1` / `ao-update.ps1` shipped. + +`ao open` is now cross-platform: it sources sessions from `sm.list()` +instead of `tmux list-sessions` (so `runtime-process` sessions on Windows +appear), and the open action branches per OS — `open-iterm-tab` stays the +macOS path, native handling on Windows and Linux. + +Behaviour on macOS and Linux is unchanged. Every Windows path is gated +behind `isWindows()`; `runtime-tmux` and the bash hook flows are untouched. + +See `docs/CROSS_PLATFORM.md` for the developer reference (helper inventory, +EPERM-vs-ESRCH gotcha, PowerShell-vs-bash differences, pre-merge checklist). +The Windows runtime architecture (pty-host, pipe protocol, registry, sweep, +mux WS Windows branch) is documented in `docs/ARCHITECTURE.md`. diff --git a/.changeset/opencode-tmpdir-and-shared-cache.md b/.changeset/opencode-tmpdir-and-shared-cache.md deleted file mode 100644 index fe5b4f38e..000000000 --- a/.changeset/opencode-tmpdir-and-shared-cache.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-cli": patch -"@aoagents/ao-plugin-agent-opencode": patch ---- - -opencode: bound /tmp blast radius and consolidate session-list cache - -Addresses review feedback on PR #1478: - -- **TMPDIR isolation.** Every `opencode` child we spawn now points at - `~/.agent-orchestrator/.bun-tmp/` via `TMPDIR`/`TMP`/`TEMP`. Bun's - embedded shared-library extraction lands there instead of the system - `/tmp`, so the cli janitor only ever sweeps AO-owned files. Other - users' or other applications' Bun artifacts on a shared host can no - longer be touched by the regex. -- **Single shared session-list cache.** Core and the agent-opencode - plugin previously kept independent caches; per poll cycle the system - spawned at least two `opencode session list` processes instead of - one. Both consumers now use the shared cache exported from - `@aoagents/ao-core` (`getCachedOpenCodeSessionList`). -- **TTL no longer covers the send-confirmation loop.** The cache TTL - dropped from 3s to 500ms so the - `updatedAt > baselineUpdatedAt` delivery signal in - `sendWithConfirmation` actually fires. Concurrent callers still - share the in-flight promise. -- **Delete invalidates the cache.** `deleteOpenCodeSession` now calls - `invalidateOpenCodeSessionListCache()` on success so reuse, remap, - and restore code paths cannot observe a deleted session id within - the TTL window. -- **Janitor reliability.** `sweepOnce` now filters synchronously - before allocating per-file promises (matters on hosts with thousands - of `/tmp` entries), and `stopBunTmpJanitor()` is now async and awaits - any in-flight sweep so SIGTERM cannot exit while `unlink` is mid-flight. -- **Janitor observability.** The sweep callback in `ao start` now logs - successful reclaims, not just errors, so operators can confirm the - janitor is doing useful work. diff --git a/.changeset/optional-repo-field.md b/.changeset/optional-repo-field.md deleted file mode 100644 index 299c6114b..000000000 --- a/.changeset/optional-repo-field.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -"@aoagents/ao-core": minor -"@aoagents/ao-cli": patch -"@aoagents/ao-web": patch -"@aoagents/ao-plugin-scm-github": patch -"@aoagents/ao-plugin-scm-gitlab": patch -"@aoagents/ao-plugin-tracker-github": patch -"@aoagents/ao-plugin-tracker-gitlab": patch ---- - -Make `ProjectConfig.repo` optional to support projects without a configured remote. - -**Migration:** `ProjectConfig.repo` is now `string | undefined` instead of `string`. -External plugins that access `project.repo` directly (e.g. `project.repo.split("/")`) must -add a null check first. Use a guard like `if (!project.repo) return null;` or a helper that -throws with a descriptive error. diff --git a/.changeset/pr1300-review-followups.md b/.changeset/pr1300-review-followups.md deleted file mode 100644 index 39ac3922e..000000000 --- a/.changeset/pr1300-review-followups.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-cli": patch -"@aoagents/ao-web": patch ---- - -Tighten the session lifecycle review follow-ups by debouncing report-watcher reactions, restoring the shared Geist/JetBrains font setup, wiring recovery validation to real agent activity probes, adding direct coverage for `ao report`, activity-signal classification, and dashboard lifecycle audit panels, fixing the remaining lifecycle-state regressions around legacy merged-session rehydration and malformed canonical payload parsing, making agent-report metadata writes atomic, persisting canonical payloads for legacy sessions on read, stabilizing detecting evidence hashes, and removing the remaining inline-style cleanup debt from the session detail view. Follow-on fixes also split the Session Detail view into smaller components, harden PR URL parsing and wrapper capture for GitHub Enterprise and GitLab-style hosts, redact sensitive observability payload fields, bound on-disk audit logs, and align cleanup wording with the current merged-session lifecycle policy. diff --git a/.changeset/refactor-session-detail.md b/.changeset/refactor-session-detail.md deleted file mode 100644 index d7d839581..000000000 --- a/.changeset/refactor-session-detail.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-web": patch ---- - -Refactor SessionDetail.tsx by extracting the topbar header, PR card, and unresolved comment thread into dedicated components. The previously-orphaned SessionDetailPRCard, session-detail-utils, and session-detail-agent-actions modules are now wired in. All files are under the 400-line component limit. diff --git a/.changeset/scm-github-ci-merge-pending-detectpr-cache.md b/.changeset/scm-github-ci-merge-pending-detectpr-cache.md deleted file mode 100644 index 87f324160..000000000 --- a/.changeset/scm-github-ci-merge-pending-detectpr-cache.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -"@aoagents/ao-plugin-scm-github": patch ---- - -scm-github: cache 4 more hot-path reads (CI, mergeability, pending comments, detectPR) - -Completes the bulk of the AO-side caching work alongside the prior PR view -cache. Per-method TTLs match the approved policy: 5s max for -decision-influencing fields. - -- `getCIChecks` (`gh pr checks`): 5s TTL -- `getMergeability` (composite `pr view` + CI + state): 5s TTL on the composite result -- `getPendingComments` (`gh api graphql` review threads): 5s TTL — ETag doesn't help on GraphQL per Experiment 2 -- `detectPR` (`gh pr list --head BRANCH`): 5s TTL, **positive-only** — `[]` results are never cached so a freshly created PR surfaces on the next poll. Branch-keyed entry is invalidated by `mergePR`/`closePR` alongside the number-keyed entries. - -Combined with the prior PR view cache, this covers the top 6 AO-side gh -operation categories that accounted for ~85% of calls in tier-5 bench traces. - -Tests: 85 existing + 9 new cache tests, all 162 passing. diff --git a/.changeset/scm-github-pr-cache.md b/.changeset/scm-github-pr-cache.md deleted file mode 100644 index 159bb32bf..000000000 --- a/.changeset/scm-github-pr-cache.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -"@aoagents/ao-plugin-scm-github": patch ---- - -scm-github: cache 5 `gh pr view` callsites with per-method TTLs - -The lifecycle worker repeatedly polls each PR for state, summary, reviews, -and review decision. Trace data showed `gh pr view` was the single largest -AO-side endpoint at 1,280 calls per 5-session tier-5 run with >97% duplicate -rate (e.g. PR #184 polled 86× for `--json state` alone in 11.5 minutes). - -Adds an in-process per-instance cache inside `createGitHubSCM()`, keyed by -`${owner}/${repo}#${prKey}:${method}` so different field-sets stay isolated. -Per-method TTLs balance reduction against staleness on decision-influencing -fields: - -- `resolvePR`: 60s (identity metadata only — number, url, title, branch refs, isDraft) -- `getPRState`: 5s -- `getPRSummary`: 5s -- `getReviews`: 5s -- `getReviewDecision`: 5s - -`assignPRToCurrentUser`, `mergePR`, and `closePR` each invalidate the entire -PR cache for that PR after the mutation, so AO never sees stale state from -its own writes. Failures are not cached. - -`getCIChecksFromStatusRollup` and `getMergeability` are intentionally NOT -cached here — those need ETag-based revalidation, not blind TTL, and will -land in a follow-up change. - -Expected reduction: ~1,165 of ~1,280 `gh pr view` calls per tier-5 run. - -Tests: 73 existing + 12 new cache tests, all passing. diff --git a/.changeset/session-ui-audit-oscillation.md b/.changeset/session-ui-audit-oscillation.md deleted file mode 100644 index 0bcf242ab..000000000 --- a/.changeset/session-ui-audit-oscillation.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-cli": patch -"@aoagents/ao-web": patch ---- - -Split orchestrator-only detail views from worker detail views, add an auditable history for `ao acknowledge` / `ao report`, and preserve canonical `needs_input` / `stuck` lifecycle states when polling only has weak or unchanged evidence. diff --git a/.changeset/stage2-evidence-recovery.md b/.changeset/stage2-evidence-recovery.md deleted file mode 100644 index 536f76d26..000000000 --- a/.changeset/stage2-evidence-recovery.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-core": minor ---- - -Improve lifecycle detection to use bounded `detecting` retries when runtime, process, and activity evidence disagree, and make recovery validation escalate probe uncertainty for human review instead of treating it as cleanup-safe death. diff --git a/.changeset/stage4-pr-policy.md b/.changeset/stage4-pr-policy.md deleted file mode 100644 index 3887a4016..000000000 --- a/.changeset/stage4-pr-policy.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-web": patch ---- - -Decouple canonical session state from PR state so workers stay idle while waiting on reviews or merged/closed PR decisions, stop cleanup from auto-killing merged PR sessions, and make the dashboard/rendered labels follow canonical PR truth instead of inferring it from legacy lifecycle aliases. diff --git a/.changeset/stage5-ui-api-rollout.md b/.changeset/stage5-ui-api-rollout.md deleted file mode 100644 index 0dc2948fc..000000000 --- a/.changeset/stage5-ui-api-rollout.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@aoagents/ao-core": patch -"@aoagents/ao-web": patch ---- - -Expose split session, PR, and runtime lifecycle truth in dashboard API payloads, render that truth directly in session cards and detail views, and extend lifecycle observability with structured transition evidence, reasons, and recovery context while preserving legacy metadata compatibility. diff --git a/.changeset/tracker-github-issue-cache.md b/.changeset/tracker-github-issue-cache.md deleted file mode 100644 index 05cc6544f..000000000 --- a/.changeset/tracker-github-issue-cache.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -"@aoagents/ao-plugin-tracker-github": patch ---- - -tracker-github: cache `gh issue view` responses in-process (5 min TTL, bounded LRU) - -The lifecycle worker polls `getIssue` and `isCompleted` repeatedly for the same -issue across a session. In a 5-session tier-5 bench run (10 min), trace data -showed the same `(repo, issue)` pair fetched 64+ times with >97% duplicate rate. - -This change caches the full `Issue` object per `(repo, identifier)` for 5 -minutes inside each `createGitHubTracker()` instance. `isCompleted` now routes -through `getIssue` to share the cache. `updateIssue` invalidates the cache -entry on any mutation. Failures are not cached. - -Expected reduction: ~744 `gh issue view` calls per tier-5 run → ~15 calls. diff --git a/.changeset/web-copy-debug-bundle.md b/.changeset/web-copy-debug-bundle.md deleted file mode 100644 index fae72c6e3..000000000 --- a/.changeset/web-copy-debug-bundle.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@composio/ao-web": patch ---- - -Add a dashboard control to copy observability diagnostics and page context to the clipboard for support and issue reports. diff --git a/.changeset/web-merge-conflict-actions.md b/.changeset/web-merge-conflict-actions.md deleted file mode 100644 index 364715347..000000000 --- a/.changeset/web-merge-conflict-actions.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-web": patch ---- - -Show GitHub compare and copy-branch actions on session PR detail when the PR has merge conflicts. diff --git a/.changeset/worker-system-prompt-split.md b/.changeset/worker-system-prompt-split.md deleted file mode 100644 index 034c20359..000000000 --- a/.changeset/worker-system-prompt-split.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@aoagents/ao-core": patch ---- - -Split worker session prompts into persistent system instructions and task-only input, materialize OpenCode worker/orchestrator instructions into session-scoped `AGENTS.md`, and keep restore behavior aligned with the updated AO prompt markers. diff --git a/.cursor/BUGBOT.md b/.cursor/BUGBOT.md index 36c155b43..279d57cca 100644 --- a/.cursor/BUGBOT.md +++ b/.cursor/BUGBOT.md @@ -15,7 +15,7 @@ Agent Orchestrator is a TypeScript monorepo for managing parallel AI coding agen ## Review Focus -- **Security**: Watch for command injection (especially in shell/tmux/git commands), AppleScript injection, GraphQL injection, unsanitized user input in API routes +- **Security**: Watch for command injection (especially in shell/tmux/git/PowerShell commands and Windows named-pipe session IDs — `validateSessionId()` should guard those), AppleScript injection, GraphQL injection, unsanitized user input in API routes - **Shell execution**: Prefer `execFile` over `exec` to avoid shell injection. Flag any use of `exec` or string concatenation in shell commands - **Plugin pattern**: Plugins must export `{ manifest, create } satisfies PluginModule` with types from `@aoagents/ao-core` - **Type safety**: Flag `as unknown as T` casts, unguarded `JSON.parse`, and type re-declarations that should import from core diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index a57ad382f..cdd9facea 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -97,7 +97,7 @@ These are the areas where Copilot review adds the most value: issues CI cannot c - Core utilities exported from `@aoagents/ao-core` **7. Resource cleanup.** Check that: -- File handles, subprocesses, and tmux sessions are cleaned up on all exit paths: success, error, and early return +- File handles, subprocesses, and runtime sessions (tmux on Unix, ConPTY pty-host processes on Windows) are cleaned up on all exit paths: success, error, and early return - `destroy()` methods exist and use best-effort semantics - There are no resource leaks in error paths @@ -124,7 +124,7 @@ These files have a wide blast radius and deserve extra scrutiny: | `packages/core/src/lifecycle-manager.ts` | State machine and polling loop with subtle state dependencies. | | `packages/core/src/session-manager.ts` | Session CRUD + stale runtime reconciliation. `list()` persists `runtime_lost` to disk when enrichment detects dead runtimes. Invariant violations can cause phantom `killed` or `exited` sessions. | | `packages/core/src/lifecycle-state.ts` | Canonical lifecycle → legacy status mapping. New terminal reasons (e.g. `runtime_lost`) must be added to `deriveLegacyStatus()`. | -| `packages/cli/src/commands/start.ts` | ao start/stop + Ctrl+C shutdown. Cross-project scoping logic is subtle — `ao stop ` must not kill parent process. | +| `packages/cli/src/commands/start.ts` | ao start/stop + Ctrl+C shutdown. Cross-project scoping logic is subtle — `ao stop ` must not kill parent process. On Windows, also calls `sweepWindowsPtyHosts()` to gracefully tear down detached ConPTY pty-host processes that `taskkill /T` cannot reach. | | `packages/core/src/config.ts` | Zod validation schema. Changes affect every `ao` command. | | `packages/core/src/index.ts` | Stable public API. Do not break it without deprecation. | | `packages/web/src/app/globals.css` | Design tokens used by 50+ components. Renaming tokens breaks the UI. | diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6a977156f..67c7f4af4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,11 @@ concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true +# Minimal token scope — all jobs only checkout, install, build, and test. +# None of them push code, comment on PRs, or call mutating GitHub APIs. +permissions: + contents: read + jobs: lint: name: Lint @@ -25,8 +30,12 @@ jobs: - run: pnpm lint typecheck: - name: Typecheck - runs-on: ubuntu-latest + name: Typecheck (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 @@ -36,15 +45,19 @@ jobs: cache: pnpm - run: pnpm install --frozen-lockfile # Build all non-web packages - - run: pnpm -r --filter '!@aoagents/ao-web' build + - run: pnpm -r --filter "!@aoagents/ao-web" build # Typecheck all non-web packages - - run: pnpm -r --filter '!@aoagents/ao-web' typecheck + - run: pnpm -r --filter "!@aoagents/ao-web" typecheck # Build web (Next.js build includes its own typecheck) - run: pnpm --filter @aoagents/ao-web build test: - name: Test - runs-on: ubuntu-latest + name: Test (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 @@ -53,12 +66,23 @@ jobs: node-version: 20 cache: pnpm - run: pnpm install --frozen-lockfile - - run: pnpm -r --filter '!@aoagents/ao-web' build + - run: pnpm -r --filter "!@aoagents/ao-web" build + # Verify node-pty's Windows prebuild loads cleanly before any test that + # depends on it. A broken prebuild fails this step in seconds with a + # clear "node-pty" stack rather than a buried integration-test failure. + - name: Verify node-pty prebuild (Windows) + if: runner.os == 'Windows' + working-directory: packages/plugins/runtime-process + run: node -e "const p=require('node-pty');const t=p.spawn('cmd.exe',['/c','exit'],{cols:80,rows:24});t.onExit(({exitCode})=>process.exit(exitCode));setTimeout(()=>process.exit(2),5000)" - run: pnpm test test-web: - name: Test (Web) - runs-on: ubuntu-latest + name: Test Web (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 @@ -66,11 +90,18 @@ jobs: with: node-version: 20 cache: pnpm + # tmux is the Linux/macOS terminal runtime backing direct-terminal-ws + # integration tests. Windows uses runtime-process + named pipes (covered + # by mux-websocket-windows.test.ts) — those tmux tests self-skip. - name: Install tmux + if: runner.os == 'Linux' run: sudo apt-get update && sudo apt-get install -y tmux - name: Start tmux server + if: runner.os == 'Linux' run: tmux start-server - run: pnpm install --frozen-lockfile - - run: pnpm -r --filter '!@aoagents/ao-web' build - - name: Run web server tests (unit + integration) - run: pnpm --filter @aoagents/ao-web exec vitest run server/__tests__/ + - run: pnpm -r --filter "!@aoagents/ao-web" build + # Full web suite — components, hooks, libs, app routes, and server tests. + # Previously this job was scoped to server/__tests__/ only; broadening it + # closes a long-standing coverage gap on both Linux and Windows. + - run: pnpm --filter @aoagents/ao-web test diff --git a/.gitignore b/.gitignore index 369cd8e00..e23087ce1 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ agent-orchestrator.yaml .DS_Store Thumbs.db .gstack/ +package-lock.json diff --git a/AGENTS.md b/AGENTS.md index 3d5bb2321..58c556242 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -47,3 +47,13 @@ Full guidelines with AO-specific context: see "Working Principles" in CLAUDE.md. - Ctrl+C on `ao start` performs full graceful shutdown (same as `ao stop`) - `LastStopState` includes `otherProjects` for cross-project session restore on next `ao start` - Dashboard sidebar always shows ALL projects' sessions regardless of active project view + +## Cross-Platform (Windows) Compatibility + +AO ships on macOS, Linux, **and Windows**. All three are first-class. + +**Golden Rule:** Never write `process.platform === "win32"` in new code. Use `isWindows()` from `@aoagents/ao-core`. If you need branching the helpers don't cover, add it to `packages/core/src/platform.ts` — never inline at the call site. Inline checks bypass the central platform-mock test pattern and become silent regressions. + +**Read `docs/CROSS_PLATFORM.md` before merging any change that touches:** process spawning/killing/signalling, file paths, shell commands, network binding, POSIX shell-outs (`tmux`, `lsof`, etc.), runtime/agent/workspace plugins, agent-plugin internals (`setupPathWrapperWorkspace`, `getActivityState`, `formatLaunchCommand`, `isProcessRunning`, `detect()`), the Windows pty-host pipe protocol or registry, or any new `process.platform === "win32"` check. + +That doc has the **full helper inventory** (every import path), the EPERM-vs-ESRCH gotcha when probing processes, path case-insensitivity rules, PowerShell-vs-bash differences (`& ` call-operator, `$env:VAR`, no `/dev/null`, no `$(cat …)`, `.cmd` shim resolution via `shell: isWindows()`), IPv6 `localhost` stalls on Windows, agent-plugin Windows specifics, the test pattern for mocking `process.platform`, and a 10-point pre-merge checklist. CLAUDE.md has the quick-reference helper table; CROSS_PLATFORM.md has the depth. diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 0c15a9fc9..e46d014d7 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -97,7 +97,7 @@ ao-1, ao-2 (agent-orchestrator) ss-1, ss-2 (safe-split) ``` -### Tmux Session Names (Globally Unique) +### Runtime Session Names (Globally Unique) ``` {hash}-{sessionPrefix}-{num} @@ -107,6 +107,8 @@ a3b4c5d6e7f8-ao-1 f1e2d3c4b5a6-int-1 (different checkout, no collision!) ``` +On Unix this is the tmux session name. On Windows (where the default runtime is `process`, not `tmux`) the same string identifies the named pipe path `\\.\pipe\ao-pty-{sessionId}` and is recorded in `~/.agent-orchestrator/windows-pty-hosts.json`. + ### Prefix Generation (Clean Heuristic) ```typescript @@ -159,7 +161,7 @@ project=integrator issue=INT-100 branch=feat/INT-100 status=working -tmuxName=a3b4c5d6e7f8-int-1 +tmuxName=a3b4c5d6e7f8-int-1 # Unix; on Windows the runtime handle is `pipePath=\\.\pipe\ao-pty-` plus `ptyHostPid` worktree=/Users/alice/.agent-orchestrator/a3b4c5d6e7f8-integrator/worktrees/int-1 createdAt=2026-02-17T10:30:00Z pr=https://github.com/ComposioHQ/integrator/pull/123 @@ -187,7 +189,7 @@ ao list integrator # Spawn new session ao spawn integrator INT-100 -# Attach to session (orchestrator finds tmux name) +# Attach to session (orchestrator finds the runtime handle: tmux name on Unix, named pipe on Windows) ao attach int-1 # Kill session diff --git a/CLAUDE.md b/CLAUDE.md index 2cccabaaf..3c5738d9b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -228,6 +228,61 @@ Strong success criteria let you loop independently. Weak criteria ("make it work - `deriveLegacyStatus()` maps canonical lifecycle to legacy status — new terminal reasons must be added here - Tab completions merge local config + global config to show all projects +## Cross-Platform (Windows) Compatibility + +AO ships on macOS, Linux, **and Windows**. All three are first-class. + +### The Golden Rule + +> **Never write `process.platform === "win32"` in new code. Use `isWindows()` from `@aoagents/ao-core`. If you need branching the helpers don't cover, add it to `packages/core/src/platform.ts` (or one of the targeted helper modules below) — never inline at the call site.** + +The codebase has a deliberate set of cross-platform abstractions. Every platform helper is centrally tested by mocking `process.platform`; inline checks bypass those tests and become silent regressions. Whenever you'd type `process.platform`, stop and check the helper inventory in `docs/CROSS_PLATFORM.md` first. + +### Read `docs/CROSS_PLATFORM.md` before merging if you touch any of: + +- Process spawning, killing, signalling, or process-tree teardown (`child_process`, `process.kill`, runtime plugins) +- File paths — comparison, joining, walking, anything OS-specific +- Shell commands (`exec`, `execFile`, command strings, redirections, PowerShell-vs-bash) +- Network binding, sockets, anything that says `localhost` +- Shell-outs to POSIX tools (`tmux`, `lsof`, `pkill`, `which`, coreutils) +- Adding any new `if (process.platform === "win32")` check (it should go into `platform.ts` instead — see the Golden Rule) +- Runtime / agent / workspace plugin code that runs on both `runtime-tmux` and `runtime-process` +- Agent-plugin internals: `setupPathWrapperWorkspace`, `getActivityState`, `formatLaunchCommand`, `isProcessRunning`, `detect()` +- The Windows pty-host pipe protocol or registry (`pty-client.ts`, `windows-pty-registry.ts`, `sweepWindowsPtyHosts`) + +### Quick reference: helpers to use instead of raw platform checks + +All importable from `@aoagents/ao-core` unless noted: + +| Need | Use | +|------|-----| +| OS check | `isWindows()` | +| Pick runtime | `getDefaultRuntime()` | +| Resolve shell (PowerShell vs `/bin/sh`) | `getShell()` | +| Kill process + descendants | `killProcessTree(pid, signal?)` | +| Find PID listening on a port | `findPidByPort(port)` | +| Default env (HOME / TMPDIR / SHELL / PATH / USER) | `getEnvDefaults()` | +| Compare paths (case-insensitive on NTFS/APFS) | `pathsEqual()` / `canonicalCompareKey()` from `cli/src/lib/path-equality.ts` | +| Escape shell args | `shellEscape()` | +| Install agent PATH wrappers (`gh`/`git`) | `setupPathWrapperWorkspace(workspacePath)` | +| Build env PATH with `~/.ao/bin` prepended | `buildAgentPath(basePath?)` | +| Tail JSONL | `readLastJsonlEntry` / `readLastActivityEntry` | +| Activity-state contract helpers | `checkActivityLogState`, `getActivityFallbackState`, `classifyTerminalActivity`, `recordTerminalActivity`, `appendActivityEntry` | +| Windows pty-host registry (used by `ao stop`) | `registerWindowsPtyHost`, `getWindowsPtyHosts`, `unregisterWindowsPtyHost`, `clearWindowsPtyHostRegistry` | +| Reap orphan pty-hosts on `ao stop` | `sweepWindowsPtyHosts()` from `@aoagents/ao-plugin-runtime-process` | +| Talk to a Windows pty-host over its named pipe | `getPipePath`, `connectPtyHost`, `ptyHostSendMessage`, `ptyHostGetOutput`, `ptyHostIsAlive`, `ptyHostKill` from `@aoagents/ao-plugin-runtime-process` | +| Validate user-supplied session ID before pipe/shell use | `validateSessionId()` from `@/server/tmux-utils` | +| Resolve a session's Windows pipe path | `resolvePipePath()` from `@/server/tmux-utils` | +| POSIX-only Ctrl+C signal forwarding | `forwardSignalsToChild()` from `cli/src/lib/shell.ts` (guard with `!isWindows()`) | +| Defensive PowerShell sweep of orphan pty-hosts | `stopStaleWindowsPtyHosts(projectDir)` from `web/src/lib/windows-pty-cleanup.ts` | + +`docs/CROSS_PLATFORM.md` has the full helper reference with import paths, the EPERM-vs-ESRCH gotcha when probing processes (with a copyable code snippet), path case-insensitivity rules, PowerShell-vs-bash differences (`& ` call-operator, `$env:VAR`, no `/dev/null`, no `$(cat …)`, `.cmd`/`.bat`/`.exe` shim resolution via `shell: isWindows()`), the IPv6 `localhost` stall on Windows, agent-plugin Windows specifics, the test pattern for mocking `process.platform`, and a 10-point pre-merge checklist. **Run through that checklist for any non-trivial change.** + +### Environment variables to know about + +- `AO_SHELL` — overrides `getShell()` resolution (escape hatch for Git Bash users on Windows). Args inferred from basename: `cmd` → `/c`, `bash`/`sh`/`zsh` → `-c`, anything else → `-Command`. +- `AO_BASH_PATH` — used by `script-runner.ts` on Windows to locate bash before falling back to Git Bash auto-detection. WSL bash is excluded (it sees Linux paths from a Windows cwd, breaking script semantics). + ## Conventions ### Code Style diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f0ace2aac..e4fb28ee3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -27,7 +27,10 @@ Include: ## Development Setup -**Prerequisites**: Node.js 20+, pnpm 9.15+, Git 2.25+, tmux, gh CLI +**Prerequisites**: Node.js 20+, pnpm 9.15+, Git 2.25+, gh CLI + +- **Unix (macOS/Linux)**: also install `tmux` — it is the default runtime. +- **Windows**: tmux is **not** required. The default runtime on Windows is `process` (ConPTY via `node-pty`), and PowerShell is the default shell. See [docs/CROSS_PLATFORM.md](docs/CROSS_PLATFORM.md) for what's different on Windows when contributing. ```bash git clone https://github.com/ComposioHQ/agent-orchestrator.git diff --git a/README.md b/README.md index 58463b9a7..db4c11d26 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Spawn parallel AI coding agents, each in its own git worktree. Agents autonomous Agent Orchestrator manages fleets of AI coding agents working in parallel on your codebase. Each agent gets its own git worktree, its own branch, and its own PR. When CI fails, the agent fixes it. When reviewers leave comments, the agent addresses them. You only get pulled in when human judgment is needed. -**Agent-agnostic** (Claude Code, Codex, Aider) · **Runtime-agnostic** (tmux, Docker) · **Tracker-agnostic** (GitHub, Linear) +**Agent-agnostic** (Claude Code, Codex, Aider) · **Runtime-agnostic** (tmux, ConPTY/process, Docker) · **Tracker-agnostic** (GitHub, Linear)
@@ -45,7 +45,9 @@ Agent Orchestrator manages fleets of AI coding agents working in parallel on you ## Quick Start -> **Prerequisites:** [Node.js 20+](https://nodejs.org), [Git 2.25+](https://git-scm.com), [tmux](https://github.com/tmux/tmux/wiki/Installing), [`gh` CLI](https://cli.github.com). Install tmux via `brew install tmux` (macOS) or `sudo apt install tmux` (Linux). +> **Prerequisites:** [Node.js 20+](https://nodejs.org), [Git 2.25+](https://git-scm.com), [`gh` CLI](https://cli.github.com), and: +> - **macOS / Linux:** [tmux](https://github.com/tmux/tmux/wiki/Installing) — install via `brew install tmux` or `sudo apt install tmux`. +> - **Windows:** PowerShell 7+ recommended. tmux is **not** required — AO uses native ConPTY via the `runtime-process` plugin (the default on Windows). Set `AO_SHELL=bash` if you have Git Bash and prefer it. ### Install @@ -135,7 +137,7 @@ $schema: https://raw.githubusercontent.com/ComposioHQ/agent-orchestrator/main/sc port: 3000 defaults: - runtime: tmux + runtime: tmux # default on macOS / Linux; on Windows the default is `process` (ConPTY) agent: claude-code workspace: worktree notifiers: [desktop] @@ -177,20 +179,22 @@ AO keeps your Mac awake while running, so you can access the dashboard remotely # agent-orchestrator.yaml $schema: https://raw.githubusercontent.com/ComposioHQ/agent-orchestrator/main/schema/config.schema.json power: - preventIdleSleep: true # Default on macOS, no-op on Linux + preventIdleSleep: true # Default on macOS; no-op on Linux and Windows ``` Set to `false` if you want to allow idle sleep while AO runs. **Lid-close limitation:** macOS enforces lid-close sleep at the hardware level — no userspace assertion can override it. If you need remote access while traveling with the lid closed, use [clamshell mode](https://support.apple.com/en-us/102505) (external power + display + input device). +**Linux / Windows:** AO does not currently hold a wake assertion on these platforms. On Linux, idle-sleep behaviour is governed by your desktop environment / `systemd-logind`; configure that directly. On Windows, set the OS power plan if remote access matters while idle. + ## Plugin Architecture Seven plugin slots. Lifecycle stays in core. | Slot | Default | Alternatives | | --------- | ----------- | ------------------------ | -| Runtime | tmux | process | +| Runtime | tmux (macOS/Linux) / process (Windows) | process, docker | | Agent | claude-code | codex, aider, cursor, opencode, kimicode | | Workspace | worktree | clone | | Tracker | github | linear, gitlab | diff --git a/SETUP.md b/SETUP.md index faeabe61a..81782df10 100644 --- a/SETUP.md +++ b/SETUP.md @@ -18,7 +18,9 @@ Comprehensive guide to installing, configuring, and troubleshooting Agent Orches git --version ``` -- **tmux** (for tmux runtime) - Terminal multiplexer for session management +- **Terminal runtime** — varies by OS: + + **On macOS / Linux:** `tmux` is required (it's the default runtime). ```bash tmux -V @@ -33,6 +35,8 @@ Comprehensive guide to installing, configuring, and troubleshooting Agent Orches sudo dnf install tmux ``` + **On Windows:** tmux is **not** required. AO uses native ConPTY via the `runtime-process` plugin (the default on Windows). PowerShell 7+ is recommended; if you have Git Bash and prefer bash semantics for shell-out commands, set `AO_SHELL=bash` in your environment. WSL is not required. + - **GitHub CLI** (for GitHub integration) - Required for PR creation, issue management ```bash @@ -147,7 +151,7 @@ If a config already exists, the new project is appended. If not, one is created - **Project type** — language, framework, test runner, package manager - **Agent runtime** — which AI agents are installed (Claude Code, Codex, Aider, OpenCode) - **Free port** — if configured port is busy, auto-finds the next available -- **tmux** — warns if not installed +- **tmux** — warns if not installed (skipped on Windows; AO uses ConPTY there and tmux is not required) - **GitHub CLI** — checks `gh auth status` ### Manual Configuration @@ -192,7 +196,7 @@ Agent Orchestrator has 8 plugin slots. All are swappable: | Slot | Purpose | Default | Alternatives | | ------------- | -------------------- | ------------- | ----------------------------------------------- | -| **Runtime** | How sessions run | `tmux` | `process`, `docker`, `kubernetes`, `ssh`, `e2b` | +| **Runtime** | How sessions run | `tmux` (macOS/Linux) / `process` (Windows; ConPTY via node-pty) | `process`, `docker`, `kubernetes`, `ssh`, `e2b` | | **Agent** | AI coding assistant | `claude-code` | `codex`, `aider`, `goose`, custom | | **Workspace** | Workspace isolation | `worktree` | `clone`, `copy` | | **Tracker** | Issue tracking | `github` | `linear`, `jira`, custom | @@ -288,7 +292,7 @@ Override defaults per project: ```yaml projects: frontend: - runtime: tmux + runtime: tmux # default on macOS/Linux; on Windows use `process` agent: claude-code workspace: worktree @@ -403,7 +407,7 @@ ao doctor ao doctor --fix ``` -`ao doctor` reports deterministic PASS/WARN/FAIL checks for PATH and launcher resolution, required binaries, tmux and GitHub CLI health, stale AO temp files, config support directories, and core build/runtime sanity. `--fix` only applies safe fixes such as creating missing AO support directories, refreshing the local launcher link, and removing stale AO temp files. +`ao doctor` reports deterministic PASS/WARN/FAIL checks for PATH and launcher resolution, required binaries, terminal-runtime health (tmux on Unix; PowerShell / `runtime-process` on Windows), GitHub CLI health, stale AO temp files, config support directories, and core build/runtime sanity. It runs and is supported on Windows. `--fix` only applies safe fixes such as creating missing AO support directories, refreshing the local launcher link, and removing stale AO temp files. ### Run `ao update` @@ -414,7 +418,7 @@ git switch main ao update ``` -`ao update` is intentionally conservative: it requires a clean working tree on `main`, fast-forwards from `origin/main`, reinstalls dependencies, clean-rebuilds the critical core/CLI/web packages, refreshes the launcher with `npm link`, and runs CLI smoke tests. Use `ao update --skip-smoke` to stop after rebuild, or `ao update --smoke-only` to rerun just the smoke checks. +`ao update` is intentionally conservative: it requires a clean working tree on `main`, fast-forwards from `origin/main`, reinstalls dependencies, clean-rebuilds the critical core/CLI/web packages, refreshes the launcher with `npm link`, and runs CLI smoke tests. Works on macOS, Linux, and Windows (Windows uses the bundled `ao-update.ps1` script automatically). Use `ao update --skip-smoke` to stop after rebuild, or `ao update --smoke-only` to rerun just the smoke checks. ### "No agent-orchestrator.yaml found" @@ -432,7 +436,7 @@ cp examples/simple-github.yaml agent-orchestrator.yaml ### "tmux not found" -**Problem:** tmux is not installed (required for tmux runtime). +**Problem:** tmux is not installed (required for the tmux runtime — the default on macOS and Linux). **Solution:** @@ -447,6 +451,8 @@ sudo apt install tmux sudo dnf install tmux ``` +**On Windows:** this error should not appear in normal use. If it does, your config has `runtime: tmux` set explicitly. Switch to `runtime: process` (or remove the override — `process` is the Windows default), and AO will use ConPTY natively without tmux. + ### "gh auth failed" **Problem:** GitHub CLI is not authenticated. @@ -682,7 +688,7 @@ notifiers: A session is an isolated workspace where an agent works on a single issue. Each session has: - Its own git worktree or clone -- Its own tmux session (or Docker container, etc.) +- Its own runtime session — a tmux session on macOS/Linux, a ConPTY pty-host process on Windows (or a Docker container, etc.) - Its own metadata (branch, PR, status) - Its own event log diff --git a/agent-orchestrator.yaml.example b/agent-orchestrator.yaml.example index 04d5f476c..eecf3dd49 100644 --- a/agent-orchestrator.yaml.example +++ b/agent-orchestrator.yaml.example @@ -31,8 +31,9 @@ port: 3000 # # that is still active at merge time. Default 5 min. # Default plugins (these are the defaults — you can omit this section) +# runtime defaults to 'tmux' on Linux/macOS, 'process' on Windows defaults: - runtime: tmux # tmux | process + # runtime: tmux # tmux (Linux/macOS default) | process (Windows default) agent: claude-code # claude-code | codex | aider | opencode | cursor | kimicode # orchestrator: # agent: claude-code @@ -79,6 +80,12 @@ projects: # deliveryHeader: x-github-delivery # maxBodyBytes: 1048576 + # Per-project environment variables forwarded into worker session runtimes. + # Useful for scoping per-project tokens (e.g. pinning gh auth via GH_TOKEN). + # AO-internal vars (AO_SESSION, AO_PROJECT_ID, etc.) always take precedence. + # env: + # GH_TOKEN: ghp_xxx + # Files to symlink into workspaces # symlinks: [.env, .claude] diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 04eb6fa6a..1b3099b72 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -23,11 +23,11 @@ graph TB subgraph MuxServer["② WebSocket Server — :14801 (separate Node process)"] MuxWS["ws://host:14801/mux\nMultiplexed — two sub-channels\nover one connection"] - TermMgr["TerminalManager\n(node-pty → tmux PTY)"] + TermMgr["TerminalManager (Unix)\n(node-pty → tmux PTY)\n— or —\nNamed-pipe relay (Windows)\nhandleWindowsPipeMessage →\n\\\\.\\pipe\\ao-pty-{id}"] Broadcaster["SessionBroadcaster\n(setInterval every 3s →\nGET /api/sessions/patches)"] end - subgraph Agents["AI Agents (one tmux window each)"] + subgraph Agents["AI Agents (one tmux window per session on Unix; one ConPTY pty-host per session on Windows)"] ClaudeCode["Claude Code"] Codex["Codex"] Aider["Aider"] @@ -57,7 +57,7 @@ graph TB MuxWS -- "session patches\n→ useSessionEvents()\n→ useMuxSessionActivity()" --> UI %% Mux auto-recovery calls back to Next.js - TermMgr -- "① HTTP POST /api/sessions/:id/restore\n(auto-recovery when tmux dies)" --> Sessions + TermMgr -- "① HTTP POST /api/sessions/:id/restore\n(auto-recovery when the runtime dies:\ntmux daemon on Unix, pty-host on Windows)" --> Sessions %% External Sessions -- "REST calls" --> GitHub @@ -110,14 +110,14 @@ sequenceDiagram participant XTerm as xterm.js participant MuxClient as MuxProvider (browser) participant MuxWS as WS Server :14801/mux - participant PTY as node-pty (tmux) + participant PTY as PTY (Unix: node-pty → tmux; Windows: named pipe → ConPTY pty-host) participant Next as Next.js :3000 MuxClient->>MuxWS: connect ws://localhost:14801/mux Note over MuxClient,MuxWS: Open a terminal MuxClient->>MuxWS: {ch:"terminal", id:"sess-1", type:"open"} - MuxWS->>PTY: attach tmux PTY + MuxWS->>PTY: attach (Unix: tmux PTY; Windows: connect named pipe) MuxWS-->>MuxClient: {ch:"terminal", id:"sess-1", type:"opened"} Note over MuxClient,MuxWS: Terminal I/O @@ -137,7 +137,7 @@ sequenceDiagram Note over MuxWS,Next: Auto-recovery (session dead) MuxWS->>Next: POST /api/sessions/sess-1/restore Next-->>MuxWS: 200 OK - MuxWS->>PTY: reattach to new tmux session + MuxWS->>PTY: reattach (Unix: new tmux session; Windows: reopen named pipe) ``` **Message types:** @@ -182,7 +182,7 @@ graph LR The CLI (`ao start`) forks two long-running processes: - **Next.js** on `:3000` — serves the dashboard and all REST routes -- **Terminal WS server** on `:14801` — handles multiplexed WebSocket + PTY management + session patch polling +- **Terminal WS server** on `:14801` — handles multiplexed WebSocket + PTY management + session patch polling. PTY transport is platform-specific: tmux via `node-pty` on Unix, named-pipe relay (`handleWindowsPipeMessage` → `\\.\pipe\ao-pty-{sessionId}`) on Windows. Both paths use the same outer mux protocol. Both processes share no in-memory state; coordination happens through flat files in `~/.agent-orchestrator/` and HTTP calls from the WS server to Next.js. @@ -202,3 +202,132 @@ Both processes share no in-memory state; coordination happens through flat files | WS server restores session | HTTP POST | `:14801` → `:3000/api/sessions/:id/restore` | | GitHub notifies of CI / PR | HTTP POST | GitHub → `:3000/api/webhooks/github` | | CLI queries sessions | HTTP GET | `ao` CLI → `:3000/api/sessions` | + +--- + +## Windows Runtime Architecture + +On Windows the high-level component map (HTTP API, mux WS server, dashboard, flat-file storage) is identical, but the **PTY transport layer is different** because tmux is not available natively. This section describes only what's different. + +> For the developer-facing rules of "how do I write code that works on both," see [`docs/CROSS_PLATFORM.md`](CROSS_PLATFORM.md). The section below is the architectural reference for *what was built*. + +### Default runtime + +`getDefaultRuntime()` from `@aoagents/ao-core` returns `"process"` on Windows and `"tmux"` everywhere else. A fresh Windows install therefore loads the `runtime-process` plugin without requiring YAML edits. Users on Unix who want the process runtime opt in via `runtime: process` in `agent-orchestrator.yaml`. + +### The pty-host helper process + +Because `node-pty` ConPTY sessions are tied to the lifetime of the host Node process, the orchestrator can't simply spawn ConPTY directly inside Next.js or the mux WS server: those processes restart, get killed by `taskkill /T`, etc. Instead, each AO session on Windows owns a small dedicated helper process — the **pty-host**. + +```mermaid +graph LR + subgraph SessionWindows["AO Session (Windows)"] + AOStart["ao start / spawn"] + PtyHost["pty-host.cjs
(detached Node child)"] + Pipe["Named pipe
\\.\pipe\ao-pty-{sessionId}"] + ConPty["ConPTY
(node-pty)"] + Agent["Agent process
(claude-code, codex, …)"] + end + + AOStart -- "spawn detached" --> PtyHost + PtyHost -- "open server" --> Pipe + PtyHost -- "spawn" --> ConPty + ConPty -- "PTY I/O" --> Agent + + MuxWS["Mux WS server\nhandleWindowsPipeMessage"] -- "connect (net.Socket)" --> Pipe + Browser["Browser xterm.js"] -- "WS frames" --> MuxWS +``` + +Implemented in `packages/plugins/runtime-process/src/pty-host.ts` (also runnable as a `.cjs` script). Key properties: + +- Spawned `detached: true, windowsHide: true` by `runtime-process` and `unref`'d so it survives parent exit (mirrors tmux daemon behaviour). +- Signals readiness by printing `READY:` to stdout; the spawner waits for that line (10 s timeout) before considering the session up. +- Maintains a 1000-line rolling output buffer, ANSI-faithful, replayed to every new client connection (this is the "scrollback on attach" equivalent of `tmux attach`). +- Intercepts `SIGTERM`/`SIGINT`/`SIGHUP`/`SIGBREAK`/`beforeExit`/`uncaughtException`/`exit` and always calls `pty.kill()` before exiting. Without this, ConPTY's `conpty_console_list_agent.exe` orphans and triggers a Windows Error Reporting dialog (`0x800700e8`). + +### Pipe protocol + +The pty-host exposes a small binary protocol over `\\.\pipe\ao-pty-{sessionId}`. Messages share a 5-byte header — `[1-byte type][4-byte big-endian length]` — followed by the payload. + +| Type | Direction | Meaning | +|------|-----------|---------| +| `0x01` `MSG_TERMINAL_DATA` | host → client | Raw PTY output bytes | +| `0x02` `MSG_TERMINAL_INPUT` | client → host | User keystrokes (chunked into ≤512 chars with 15 ms gaps to avoid ConPTY input-buffer truncation) | +| `0x03` `MSG_RESIZE` | client → host | JSON `{cols, rows}` | +| `0x04` / `0x05` `MSG_GET_OUTPUT_REQ` / `_RES` | client ↔ host | Request and return scrollback buffer | +| `0x06` / `0x07` `MSG_STATUS_REQ` / `_RES` | client ↔ host | Liveness check (`{alive, pid, exitCode?}`) | +| `0x08` `MSG_KILL_REQ` | client → host | Cooperative shutdown (host disposes ConPTY then exits) | + +Client helpers in `packages/plugins/runtime-process/src/pty-client.ts`: +- `connectPtyHost`, `ptyHostSendMessage`, `ptyHostGetOutput`, `ptyHostIsAlive`, `ptyHostKill`, plus `getPipePath(sessionId)` → `\\.\pipe\ao-pty-{sessionId}`. +- `MessageParser` skips interleaved data frames so request/response pairs work over a busy pipe. + +### Mux WS server: tmux vs Windows pipe relay + +`packages/web/server/mux-websocket.ts` branches by platform: + +- **Unix**: instantiates `TerminalManager` (node-pty → tmux PTY) and dispatches all `terminal` channel messages to it. +- **Windows**: skips `TerminalManager` entirely and routes through `handleWindowsPipeMessage(msg, ws, winPipes, winPipeBuffers, deps)`, which maps each `(projectId, sessionId)` to a `net.Socket` connected to its pipe. `open` opens the socket, `data` writes a `0x02` framed message, `resize` writes `0x03`, `close` ends the socket. Inbound `0x01` frames are forwarded back as WebSocket `{ch:"terminal", type:"data"}` payloads; `0x07` with `alive:false` becomes `exited`. +- The pipe path is resolved by `resolvePipePath(sessionId, projectId?)` in `packages/web/server/tmux-utils.ts`, which reads the session's metadata file (V2 layout `~/.agent-orchestrator/projects/{projectId}/sessions/{sessionId}.json`, V1 fallback) and returns the `pipePath` field that `runtime-process` wrote at spawn time. +- `findTmux()` returns `null` on Windows; `direct-terminal-ws.ts` logs `Windows mode — using named pipe relay to PTY hosts` and starts the same WS server with no tmux dependency. + +### Pty-host registry — `~/.agent-orchestrator/windows-pty-hosts.json` + +Because pty-hosts run detached, `taskkill /T` on the parent ao-start process cannot reach them. To allow `ao stop` to find and clean them up, every spawned pty-host is recorded in a small JSON registry. + +`packages/core/src/windows-pty-registry.ts`: +- `registerWindowsPtyHost(entry)` — write/replace the entry on spawn. +- `getWindowsPtyHosts()` — read all entries; auto-prune any whose PID is gone (probed via `process.kill(pid, 0)`, treating `EPERM` as alive). +- `unregisterWindowsPtyHost(sessionId)` — remove on session destroy. +- `clearWindowsPtyHostRegistry()` — wipe (for tests / recovery). + +`sweepWindowsPtyHosts()` (in `runtime-process`) iterates the registry: for each live entry it sends a graceful `MSG_KILL_REQ` over the pipe, polls up to 500 ms for the process to exit (treating `EPERM` as still alive), then `killProcessTree(ptyHostPid, "SIGKILL")` for stragglers. It is called by `ao stop` and `ao stop --all` before tearing down the parent process. + +### Process map (Windows variant) + +```mermaid +graph LR + subgraph Host + CLI["ao CLI"] + Next["Next.js :3000"] + MuxSrv["Terminal WS :14801"] + Sweep["sweepWindowsPtyHosts()
(called by ao stop)"] + end + + subgraph Sessions["Per-session pty-hosts (detached)"] + PH1["pty-host #1
\\.\pipe\ao-pty-id1"] + PH2["pty-host #2
\\.\pipe\ao-pty-id2"] + end + + subgraph Storage["Flat files"] + Reg["~/.agent-orchestrator/
windows-pty-hosts.json"] + Meta["~/.agent-orchestrator/
projects/{id}/sessions/*"] + end + + CLI -- "spawn detached" --> PH1 + CLI -- "spawn detached" --> PH2 + PH1 -- "register" --> Reg + PH2 -- "register" --> Reg + MuxSrv -- "resolvePipePath()
reads metadata" --> Meta + MuxSrv -- "net.Socket connect" --> PH1 + MuxSrv -- "net.Socket connect" --> PH2 + Sweep -- "MSG_KILL_REQ → killProcessTree" --> PH1 + Sweep -- "MSG_KILL_REQ → killProcessTree" --> PH2 + Sweep -- "read entries" --> Reg +``` + +### Shell resolution + +`getShell()` in `packages/core/src/platform.ts` is platform-aware and cached: + +- **Unix**: `/bin/sh -c` (always; never `$SHELL` — non-interactive launches must not depend on the user's login shell). +- **Windows** (`resolveWindowsShell`): in priority order — `AO_SHELL` env override → `pwsh` on PATH → `%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe` (absolute path, robust to degraded PATH) → `powershell` on PATH → `%ComSpec%` (`cmd.exe`, last resort). + +Args are inferred from the basename: `cmd` → `/c`, `bash`/`sh`/`zsh` → `-c`, anything PowerShell-shaped → `-Command`. `AO_SHELL` is the supported escape hatch (e.g. for Git Bash users). + +### Other Windows-specific touch points + +- **CLI** — `ao start` no longer detaches its dashboard child on Windows (so Ctrl+C reaches the whole console group); `forwardSignalsToChild` is Unix-only. `ao stop` calls `sweepWindowsPtyHosts()` before terminating the parent. `script-runner.ts` runs `.ps1` siblings of `.sh` scripts directly on Windows; otherwise it tries `AO_BASH_PATH` then auto-detects Git Bash (WSL bash is excluded — it sees Linux paths from a Windows cwd). +- **Agent plugins** — `setupPathWrapperWorkspace()` generates `.cjs` + `.cmd` wrapper pairs (instead of bash scripts) for `gh`/`git` interception. `formatLaunchCommand` for codex / kimicode prepends `& ` so PowerShell parses the quoted binary path as a call expression. `agent-claude-code` ships a Node.js metadata-updater (`.cjs`) hook in place of the bash version; system-prompt files are inlined rather than `$(cat …)`-substituted. +- **Path-equality** — `packages/cli/src/lib/path-equality.ts` (`pathsEqual`, `canonicalCompareKey`) handles NTFS case-insensitivity and drive-letter case differences when comparing project paths in `ao start`. +- **`stopStaleWindowsPtyHosts(projectDir)`** in `packages/web/src/lib/windows-pty-cleanup.ts` is a defensive sweeper used by the dashboard to clean up orphan pty-hosts found via a PowerShell `Get-CimInstance Win32_Process` query. diff --git a/docs/CLI.md b/docs/CLI.md index 26d4e7894..ab4bae5c7 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -17,7 +17,7 @@ ao completion zsh # Print the zsh completion script ## Commands the orchestrator agent uses -These are primarily invoked by the orchestrator agent running inside a tmux session. You can use them manually if needed, but the orchestrator handles this automatically. +These are primarily invoked by the orchestrator agent running inside a runtime session (a tmux window on macOS/Linux; a ConPTY pty-host on Windows). You can use them manually if needed, but the orchestrator handles this automatically. ```bash ao spawn [issue] # Spawn an agent (project auto-detected from cwd) @@ -64,9 +64,9 @@ compinit With Oh My Zsh, write the generated file to `${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/ao/_ao` and add `ao` to the `plugins=(...)` list in `~/.zshrc`. -`ao doctor` checks PATH and launcher resolution, required binaries, configured plugin resolution, tmux and GitHub CLI health, config support directories, stale AO temp files, and core build/runtime sanity. +`ao doctor` checks PATH and launcher resolution, required binaries, configured plugin resolution, terminal-runtime health (tmux on Unix; PowerShell / `runtime-process` on Windows), GitHub CLI health, config support directories, stale AO temp files, and core build/runtime sanity. Runs and is supported on macOS, Linux, and Windows. -`ao update` fast-forwards the local install on `main`, reinstalls dependencies, clean-rebuilds core packages, refreshes the launcher, and runs smoke tests. Use `ao update --skip-smoke` to stop after rebuild, or `ao update --smoke-only` to rerun just the smoke checks. +`ao update` fast-forwards the local install on `main`, reinstalls dependencies, clean-rebuilds core packages, refreshes the launcher, and runs smoke tests. Works on macOS, Linux, and Windows (Windows uses the bundled `ao-update.ps1` script automatically). Use `ao update --skip-smoke` to stop after rebuild, or `ao update --smoke-only` to rerun just the smoke checks. ## Multi-Project Rollout diff --git a/docs/CROSS_PLATFORM.md b/docs/CROSS_PLATFORM.md new file mode 100644 index 000000000..5435ffcd7 --- /dev/null +++ b/docs/CROSS_PLATFORM.md @@ -0,0 +1,389 @@ +# Cross-Platform Compatibility + +> **Read this before merging any change that touches process spawning, path handling, shell commands, network binding, file I/O, runtime/agent/workspace plugins, or anything that does platform-specific work.** +> +> AO ships on macOS, Linux, **and Windows**. All three are first-class — every change must keep all three working. + +--- + +## The Golden Rule + +> **Never write `process.platform === "win32"` in new code. Use `isWindows()` from `@aoagents/ao-core`. If you need branching the helper doesn't cover, add it to `packages/core/src/platform.ts` (or one of the targeted helpers in [the inventory](#helper-inventory)) — never inline at the call site.** + +This isn't stylistic. The branching in `platform.ts` is centrally tested with `Object.defineProperty(process, "platform", …)` so both Windows and POSIX paths are exercised on every CI runner. Inline `process.platform` checks are invisible to that test pattern, drift out of sync, and produce the bugs that took weeks to track down on the way to shipping the Windows port. + +If you find yourself typing `process.platform`: + +1. Stop. Look at the [helper inventory below](#helper-inventory) — almost certainly the helper you need already exists. +2. If it doesn't, ask: "Could a future feature also need this branch?" Almost always yes. Add a function to `platform.ts` (or the closest existing helper module) and test both branches. +3. Only if the branch is genuinely a one-off (e.g. a single test guarding a Linux-only assertion) is an inline check acceptable, and even then prefer `isWindows()` for readability. + +--- + +## When to read this file + +If your change does **any** of the following, you must read the relevant section below: + +| If you're touching… | …read | +|---------------------|-------| +| `process.spawn`, `child_process`, runtime plugins | [The two runtimes](#the-two-runtimes), [Process management](#process-management-gotchas) | +| `process.kill`, signals, process-tree teardown | [Process management](#process-management-gotchas) | +| Anything with file paths (compare, join, walk) | [Paths](#paths) | +| Shell commands (`exec`, command strings) | [Shell](#shell) | +| `server.listen`, sockets, `localhost` | [Networking](#networking) | +| tmux / lsof / pkill / which / coreutils shell-outs | [POSIX-only tools](#posix-only-tools) | +| Adding a new `if (process.platform === "win32")` | [The Golden Rule](#the-golden-rule), [Helper inventory](#helper-inventory) | +| Agent plugins (PATH wrappers, hooks, launch commands) | [Agent plugin helpers](#agent-plugin-helpers) | +| Activity detection / JSONL processing | [Activity-state helpers](#activity-state-helpers) | +| Tests for any of the above | [Testing for cross-platform behaviour](#testing-for-cross-platform-behaviour) | +| Anything else? | At minimum, the [pre-merge checklist](#pre-merge-checklist) | + +--- + +## Helper inventory + +Every helper you need to write Windows-safe code. **Memorise the imports — these are the building blocks.** + +### Platform check + defaults — `packages/core/src/platform.ts` + +```ts +import { + isWindows, + getDefaultRuntime, + getShell, + killProcessTree, + findPidByPort, + getEnvDefaults, +} from "@aoagents/ao-core"; +``` + +| Symbol | Purpose | Notes | +|--------|---------|-------| +| `isWindows(): boolean` | The canonical OS check. **Always use this** instead of `process.platform === "win32"`. | Constant-time. Trivially mockable in tests. | +| `getDefaultRuntime(): "tmux" \| "process"` | Returns `"process"` on Windows, `"tmux"` elsewhere. Used by `ao start` / startup-preflight to default runtime selection. | Don't hardcode `"tmux"`. | +| `getShell(): { cmd, args(command) }` | Resolves the shell for non-interactive command execution. POSIX → `/bin/sh -c`. Windows → priority order: `AO_SHELL` env override → `pwsh` → `powershell.exe` (absolute path, robust to degraded PATH) → `powershell` → `cmd.exe`. Cached. | Use this whenever you need to run *any* shellish string. Don't assume bash. | +| `killProcessTree(pid, signal?)` | Kills a process and its descendants. Windows → `taskkill /T /F /PID `. POSIX → `process.kill(-pid, signal)` with direct-PID fallback. Guards `pid > 0`. | **Never write `process.kill(-pid, …)` directly.** Negative PIDs are POSIX-only. | +| `findPidByPort(port): Promise` | Finds the LISTENING PID on a port. Windows → parses `netstat -ano`. POSIX → `lsof -ti :PORT -sTCP:LISTEN`. | Use this; don't shell-out yourself. | +| `getEnvDefaults(): { HOME, SHELL, TMPDIR, PATH, USER }` | Returns platform-correct env defaults: Windows reads `USERPROFILE`/`TEMP`/`USERNAME`, POSIX reads `HOME`/`SHELL`/`TMPDIR`/`USER`. | Use instead of hardcoding `/tmp`, `~`, `$HOME`. | +| `_resetShellCache()` | Test-only — clears the cached shell resolution. | `@internal`. | + +### Path equality — `packages/cli/src/lib/path-equality.ts` + +```ts +import { pathsEqual, canonicalCompareKey } from "../../src/lib/path-equality.js"; +``` + +| Symbol | Purpose | +|--------|---------| +| `pathsEqual(a, b): boolean` | "Same filesystem entry" comparison. Resolves both via `realpathSync` (falls back to literal on error), then lowercases on Windows so `D:\Foo` == `d:\foo`. | +| `canonicalCompareKey(input): string` | Stable Map/Set key for a path. Expands `~`, resolves to absolute, calls `realpathSync`, lowercases on Windows. | + +**Rule:** never compare paths with `===`. Always go through these. + +### Windows pty-host registry — `packages/core/src/windows-pty-registry.ts` + +Only used by Windows runtime code, but exported from `@aoagents/ao-core` so the CLI's `ao stop` can find detached pty-hosts that `taskkill /T` cannot reach. + +```ts +import { + registerWindowsPtyHost, + unregisterWindowsPtyHost, + getWindowsPtyHosts, + clearWindowsPtyHostRegistry, +} from "@aoagents/ao-core"; +``` + +| Symbol | Purpose | +|--------|---------| +| `registerWindowsPtyHost(entry)` | Add/replace a `{sessionId, ptyHostPid, pipePath}` entry in `~/.agent-orchestrator/windows-pty-hosts.json`. Called when `runtime-process` spawns a pty-host. | +| `unregisterWindowsPtyHost(sessionId)` | Remove on session destroy. | +| `getWindowsPtyHosts(): WindowsPtyHostEntry[]` | Return all entries whose PID is still alive (probes via `process.kill(pid, 0)` treating `EPERM` as alive). Auto-prunes dead ones. | +| `clearWindowsPtyHostRegistry()` | Wipe the file (recovery / tests). | + +### Pty-host client (Windows pipe protocol) — `packages/plugins/runtime-process/src/pty-client.ts` + +Use these whenever you need to talk to a Windows pty-host over its named pipe. The mux WS server, `runtime-process`, and `sweepWindowsPtyHosts` all go through this module — never write to a `\\.\pipe\…` directly. + +```ts +import { + getPipePath, + connectPtyHost, + ptyHostSendMessage, + ptyHostGetOutput, + ptyHostIsAlive, + ptyHostKill, + MessageParser, + encodeMessage, +} from "@aoagents/ao-plugin-runtime-process"; +``` + +| Symbol | Purpose | +|--------|---------| +| `getPipePath(sessionId)` | Returns `\\.\pipe\ao-pty-`. Don't construct the path manually. | +| `connectPtyHost(pipePath, timeoutMs?)` | Open a `net.Socket` to the named pipe with timeout. | +| `ptyHostSendMessage(pipePath, message)` | Send keystrokes; chunks into ≤512-char pieces with 15 ms gaps to dodge ConPTY input-buffer truncation. | +| `ptyHostGetOutput(pipePath, lines?)` | Request scrollback buffer. Returns `""` on timeout. | +| `ptyHostIsAlive(pipePath)` | Liveness probe; `true` ≡ pipe reachable. | +| `ptyHostKill(pipePath)` | Cooperative shutdown (host disposes ConPTY then exits). Silently succeeds if pipe is unreachable. | +| `MessageParser`, `encodeMessage` | Frame-protocol primitives if you're writing new pty-host integrations. | + +### Pty-host sweep — `packages/plugins/runtime-process/src/index.ts` + +```ts +import { sweepWindowsPtyHosts } from "@aoagents/ao-plugin-runtime-process"; +``` + +`sweepWindowsPtyHosts(): Promise<{ attempted, gracefullyExited, forceKilled, failed }>` — iterates the registry, sends graceful `MSG_KILL_REQ`, polls up to 500 ms, then `killProcessTree` for stragglers. Called by `ao stop`. **No-op on non-Windows.** + +The exit-poll inside this function is the canonical EPERM/ESRCH pattern — copy it whenever you probe a Windows process for liveness: + +```ts +while (Date.now() < deadline) { + try { + process.kill(entry.ptyHostPid, 0); + } catch (err: unknown) { + // EPERM = alive but unsignalable (cross-context on Windows) → fall through to force-kill. + // ESRCH (or anything else) = process is gone → mark exited. + if ((err as { code?: string }).code !== "EPERM") { + exited = true; + } + break; + } + await new Promise((r) => setTimeout(r, 25)); +} +``` + +### Web-side helpers + +```ts +// packages/web/server/tmux-utils.ts +import { validateSessionId, resolvePipePath } from "@/server/tmux-utils"; + +// packages/web/src/lib/windows-pty-cleanup.ts +import { stopStaleWindowsPtyHosts } from "@/lib/windows-pty-cleanup"; +``` + +| Symbol | Purpose | +|--------|---------| +| `validateSessionId(id): boolean` | Charset/length guard. **Always validate any session ID before using it in a tmux command, named-pipe path, or shell argument** — these are user-controllable inputs. | +| `resolvePipePath(sessionId, projectId?)` | Reads the session metadata file and returns the `pipePath` field stored by `runtime-process`. Returns `null` on non-Windows. Used by the mux WS server when relaying pipe traffic. | +| `stopStaleWindowsPtyHosts(projectDir)` | Defensive sweeper. Uses a PowerShell `Get-CimInstance Win32_Process` query to find pty-hosts whose command line contains a project dir, then `taskkill`'s them. No-op on non-Windows. Use as a recovery escape hatch, not in the hot path. | + +### Agent plugin helpers — `packages/core/src/agent-workspace-hooks.ts` + +```ts +import { setupPathWrapperWorkspace, buildAgentPath } from "@aoagents/ao-core"; +``` + +| Symbol | Purpose | +|--------|---------| +| `setupPathWrapperWorkspace(workspacePath)` | Installs `~/.ao/bin` PATH wrappers for `gh` / `git` so AO can intercept agent commands. **Cross-platform.** On Windows it generates `.cjs` + `.cmd` wrapper pairs (skipping bash); on Unix it generates the bash equivalents. Every agent plugin that uses PATH-wrapper interception (codex, kimicode, aider, opencode) must call this — never reimplement. | +| `buildAgentPath(basePath?)` | Prepends `~/.ao/bin` to PATH using the right separator (`;` on Windows, `:` on Unix). Use when constructing the agent's env. | + +### Activity-state helpers — `packages/core/src/activity-log.ts` and `utils.ts` + +```ts +import { + appendActivityEntry, + readLastActivityEntry, + checkActivityLogState, + getActivityFallbackState, + classifyTerminalActivity, + recordTerminalActivity, + readLastJsonlEntry, +} from "@aoagents/ao-core"; +``` + +`getActivityFallbackState` is **mandatory** for new agent plugins. See [the agent-plugin section in the root CLAUDE.md](../CLAUDE.md#agent-plugin-implementation-standards) for the full contract — but the relevant cross-platform note is: AO activity JSONL works the same on all platforms, so write your activity-detection logic against it, not against tmux capture-pane / ps output. + +### Shell escaping — `packages/core/src/utils.ts` + +```ts +import { shellEscape } from "@aoagents/ao-core"; +``` + +`shellEscape(arg)` produces a safely-quoted argument. Always use it when interpolating any value into a shell command line, even on Windows. Windows quoting rules are messier than POSIX and the helper handles them. + +### CLI signal forwarding — `packages/cli/src/lib/shell.ts` + +```ts +import { forwardSignalsToChild } from "../lib/shell.js"; +``` + +`forwardSignalsToChild(pid, child)` — call **only on POSIX** (`if (!isWindows() && pid)`). On Windows, Ctrl+C reaches the entire console group natively; explicit forwarding is harmful (double-signals). + +### Environment variables to know + +| Variable | Effect | +|----------|--------| +| `AO_SHELL` | Override `getShell()` resolution. Set to an absolute path or shell name (`pwsh`, `cmd`, `bash`, …). Args are inferred from basename. The supported escape hatch for Git Bash users on Windows. | +| `AO_BASH_PATH` | Used by `script-runner.ts` on Windows to locate bash before falling back to Git Bash auto-detection. WSL bash is intentionally excluded. | + +--- + +## The two runtimes + +| Platform | Default runtime | How PTYs work | +|----------|----------------|---------------| +| macOS / Linux | `tmux` | Real tmux server, POSIX signals, Unix sockets | +| Windows | `process` | `node-pty` + ConPTY, named pipes (`\\.\pipe\ao-pty-…`), pty-host helper process | + +Pick the runtime via `getDefaultRuntime()`, never hardcode. Plugin code that runs across runtimes must handle both — for Windows that means no `tmux` shell-outs, no SIGTERM/SIGKILL group kills, no POSIX-only tools. + +For the architectural detail of how the Windows pty-host, named-pipe protocol, and mux WS Windows branch fit together, see the **"Windows Runtime Architecture"** section at the bottom of [`docs/ARCHITECTURE.md`](ARCHITECTURE.md). + +--- + +## Process management gotchas + +- **`process.kill(pid, 0)` distinguishes liveness on POSIX, but on Windows it can throw `EPERM`** when the target exists in a different security context. Treat `EPERM` as *alive but unsignalable* (fall through to force-kill); only `ESRCH` (or any other code) means the process is gone. The pattern is shown in the [`sweepWindowsPtyHosts` snippet above](#pty-host-sweep--packagespluginsruntime-processsrcindexts) — copy it, don't bare-`catch`. The same pattern lives in `runtime-process` `destroy()` (around line 290) and was the bug fix that prompted this section. +- **Never `process.kill(-pid, …)`** to kill a process group. Negative PIDs are POSIX-only and become a no-op or worse on Windows. Use `killProcessTree()`. +- **Graceful shutdown before SIGKILL on Windows**: SIGKILL'ing the pty-host while ConPTY is mid-spawn orphans `conpty_console_list_agent.exe` and triggers a Windows Error Reporting dialog (`0x800700e8`). Send the cooperative kill (`ptyHostKill`) first, poll for exit ~500 ms, **then** `killProcessTree`. +- **`pid <= 0` guard**: `process.kill(0, …)` signals the *current process group* on Unix. Always guard `pid > 0` before signalling. +- **Detached children**: on Windows `ao start` does NOT detach its dashboard child (so Ctrl+C reaches the whole console group natively); on POSIX it does. Use `detached: !isWindows()` rather than always-`true` or always-`false`. + +## Paths + +- **Filesystem case-insensitive on Windows (NTFS) and macOS (default APFS)**, case-sensitive on Linux. `D:\Foo` and `d:\foo` are the same directory; `/foo` and `/Foo` are not. Compare paths via `pathsEqual()`, never `===`. +- **Always use `path.join()` / `path.sep`**. Never hardcode `/` or `\` separators. Never split paths on `/` to walk segments. +- **Drive letters and UNC paths exist.** A path can start with `C:\`, `\\?\C:\`, `\\server\share\`, or `D:`. Don't assume paths begin with `/`. +- **Paths can contain spaces** (`C:\Program Files\…`, `C:\Users\Some Name\…`). Always quote when interpolating into shell commands; prefer `execFile` over `exec`. +- **HOME / tmp paths differ**: use `getEnvDefaults()` rather than hardcoding `/tmp`, `~`, or `$HOME`. +- **Drive-letter slugs**: when encoding a path as a filename slug (used by Claude Code's session-JSONL lookup), `C:\Users\dev\project` → `C--Users-dev-project`. Preserve the leading drive-letter dash; don't strip the colon-replacement. + +## Shell + +- **Default shell on Windows is PowerShell**, not bash. Bash syntax (`&&` chains, `$VAR`, `2>/dev/null`, here-docs) won't work in `cmd.exe` and is only partially supported by PowerShell. When you need to run *anything* shellish from Node, prefer `execFile` with explicit args; if you must use a shell, route through `getShell()`. +- **PowerShell call operator**: a launch command that begins with a quoted absolute path needs `& ` prepended on Windows (e.g. `& "C:\path\to\bin.exe" arg1`) or PowerShell parses the quoted path as a string expression. The `agent-codex` and `agent-kimicode` plugins do this in `formatLaunchCommand`. +- **No `/dev/null`** on Windows — use `NUL`, or just discard the stream in Node. +- **Env vars in PowerShell**: `$env:NAME`, not `$NAME`. Line continuation is backtick (`` ` ``), not backslash. +- **`.cmd` / `.bat` / `.exe` shims**: spawning npm-installed CLIs (e.g. `codex`, `where`) needs `shell: true` on Windows so `PATHEXT` is consulted; otherwise Node only finds extensionless executables. Pattern: `spawn(cmd, args, { shell: isWindows(), windowsHide: true })`. +- **`windowsHide: true`** on every `spawn`/`execFile` you don't want flashing a console window. +- **Always `shellEscape()`** any value that ends up in a shell command line, even on Windows. Windows quoting rules are tricky and the helper handles them. +- **Avoid pipes / redirection in shell strings** — they don't behave consistently across cmd.exe / PowerShell / bash. Build the pipeline in Node with stream APIs instead. +- **`$(cat …)` substitution** doesn't exist in PowerShell or cmd.exe. If you're inlining a file's contents into a command line, read it in Node and pass the contents as an argument (e.g. `--append-system-prompt `). + +## Networking + +- **Bind to `127.0.0.1` explicitly, not `localhost`**, when starting local servers. On Windows `localhost` resolves to `::1` first; if the server only listens on IPv4 the client stalls ~21 s before the kernel falls back. The same problem reverses if you bind IPv6-only. +- **Named pipes** are the Windows IPC primitive (`\\.\pipe\…`); the relay code already handles them in `mux-websocket.ts` via `handleWindowsPipeMessage`. Don't introduce Unix-socket assumptions in new code paths. +- **Firewall prompts**: any `0.0.0.0` bind on Windows can pop a Windows Defender Firewall prompt the first time it runs. Stick to loopback unless there's a real reason. +- **Pipe path injection**: a pipe path is constructed from a session ID; always validate that ID with `validateSessionId()` before passing to `getPipePath()` or interpolating into any system call. + +## POSIX-only tools + +`tmux`, `screen`, `lsof`, `pkill`, `which`, most coreutils — gone on Windows. If you need their function, either branch through `platform.ts` or use a Node API instead. + +Examples already in `platform.ts`: +- `findPidByPort` uses `netstat -ano` on Windows vs `lsof` elsewhere +- `killProcessTree` uses `taskkill /T /F` vs POSIX signal-based kill +- `getShell` resolves PowerShell on Windows vs `/bin/sh` on POSIX + +If you find yourself reaching for a POSIX-only binary in new code, **add the Windows alternative to `platform.ts`** rather than gating the feature. + +## Agent plugin specifics (Windows) + +When writing or modifying an agent plugin (`packages/plugins/agent-*`), these are the patterns to follow: + +- **Use `setupPathWrapperWorkspace`** for PATH-wrapper interception (gh / git). It auto-handles bash vs `.cmd`+`.cjs` wrappers per platform. +- **`isProcessRunning`** must short-circuit on Windows when it would have used tmux or `ps -eo`: `if (isWindows()) return false` (or implement a real Windows check via tasklist / signal-0 with EPERM handling — never assume tmux exists). +- **`detect()`** spawn options should be `{ shell: isWindows(), windowsHide: true }` so `.cmd` shims resolve via `PATHEXT` and no console window flashes. +- **Stderr suppression** — the cursor plugin's `detect()` previously bled stderr to the user's console on Windows; it now uses `stdio: ['ignore', 'pipe', 'ignore']` for the probe. Match that pattern. +- **`getCachedProcessList()`** (Claude Code) should return `""` on Windows — `ps -eo` doesn't exist. +- **`formatLaunchCommand`**: when the binary is at a quoted absolute path, prepend `& ` on Windows so PowerShell parses it as a call. +- **`systemPromptFile`**: instead of `$(cat )` shell substitution, read the file in Node and inline as `--append-system-prompt `. +- **Codex binary resolution**: prefer `.cmd` shims (npm) over `.exe` (Cargo) on Windows; use `where.exe` (not `which`). + +## Activity-state helpers + +The activity-detection contract in CLAUDE.md is platform-agnostic — same JSONL on all platforms — but the inputs (terminal output) come from different runtimes. Use `recordTerminalActivity` from core (which delegates to `classifyTerminalActivity` → `appendActivityEntry`) so you don't have to think about platform. + +The mandatory `getActivityFallbackState` step (see CLAUDE.md "Activity detection architecture") is what keeps the dashboard alive when a native agent API is unavailable — which on Windows happens more often than on Unix because more things shell-out and fail silently. Skipping it has historically broken stuck-detection on Windows. + +--- + +## Testing for cross-platform behaviour + +CI runs on Linux, macOS, and Windows. To make platform-specific code reviewable in a single host environment and to catch regressions even when one runner is unavailable: + +- Any new function in `platform.ts` (or platform-branching elsewhere) must have **both** an `it.skipIf(process.platform !== "win32")` test and a POSIX test. See `packages/cli/__tests__/lib/path-equality.test.ts` for the pattern (it mocks `process.platform` via `Object.defineProperty` to exercise both branches on a single CI host). +- For process-kill / EPERM-handling code, add a unit test that simulates `process.kill` throwing `{ code: "EPERM" }` and asserts force-kill is still attempted. The `runtime-process` test suite has examples (look for "win32 destroy when graceful shutdown times out"). +- Plugin tests that hit a tmux runtime must `skipIf(isWindows())`. Plugin tests that hit `runtime-process` should run on all platforms. +- For path code, test mixed-case inputs and inputs with spaces. + +Pattern for mocking platform on Linux CI: + +```ts +let originalPlatform: PropertyDescriptor | undefined; +beforeEach(() => { + originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); +}); +afterEach(() => { + if (originalPlatform) Object.defineProperty(process, "platform", originalPlatform); +}); +function setPlatform(p: NodeJS.Platform) { + Object.defineProperty(process, "platform", { value: p, configurable: true }); +} +``` + +--- + +## Pre-merge checklist + +Before saying "done" on any feature, verify each of these (or mark N/A with reasoning): + +1. **No raw `process.platform` checks** — used `isWindows()` from `@aoagents/ao-core`? +2. **Process spawning** — used `runtime-process` (Windows) or `runtime-tmux` (POSIX) abstractions? Shell-out used `shellEscape` + `getShell` or `execFile`? `windowsHide: true` and `shell: isWindows()` for `.cmd`/`.bat` resolution? +3. **Process killing** — distinguished `EPERM` from `ESRCH`? No negative PIDs? Used `killProcessTree`? Guarded `pid > 0`? Cooperative kill before force-kill on Windows? +4. **Paths** — used `pathsEqual` for comparison? `path.join` for construction? No `===`, no hardcoded `/` or `\`? +5. **Shell** — no bash-isms (`&&` chains, `$(cat)`, `$VAR`, `/dev/null`)? `& ` prefix for quoted-path PowerShell calls? Routed through `getShell()` or used `execFile`? +6. **Networking** — explicit `127.0.0.1` instead of `localhost`? Validated session IDs before constructing pipe paths? +7. **Runtimes** — both `runtime-tmux` and `runtime-process` paths covered? `isProcessRunning` works for tmux TTY *and* PID signal-0 *with EPERM handling*? +8. **Agent plugins** — `setupPathWrapperWorkspace` instead of bash hooks? `getActivityFallbackState` fallback in `getActivityState`? +9. **New platform branching** — went into `platform.ts` (or another shared helper), not inline at call sites? +10. **Tests** — both Windows and POSIX branches covered (mock `process.platform` if you can't run on both)? + +If you can't say "yes" or "N/A" to all ten, your change probably breaks Windows. + +--- + +## Quick reference: "where do I import X from?" + +```ts +// Platform check, runtime/shell/env defaults, process kill, port lookup +import { + isWindows, getDefaultRuntime, getShell, + killProcessTree, findPidByPort, getEnvDefaults, + shellEscape, + setupPathWrapperWorkspace, buildAgentPath, + registerWindowsPtyHost, unregisterWindowsPtyHost, + getWindowsPtyHosts, clearWindowsPtyHostRegistry, + appendActivityEntry, readLastActivityEntry, + checkActivityLogState, getActivityFallbackState, + classifyTerminalActivity, recordTerminalActivity, + readLastJsonlEntry, +} from "@aoagents/ao-core"; + +// Path comparison (CLI package) +import { pathsEqual, canonicalCompareKey } + from "../../src/lib/path-equality.js"; + +// Windows pty-host pipe protocol + sweep +import { + getPipePath, connectPtyHost, ptyHostSendMessage, + ptyHostGetOutput, ptyHostIsAlive, ptyHostKill, + MessageParser, encodeMessage, + sweepWindowsPtyHosts, +} from "@aoagents/ao-plugin-runtime-process"; + +// Web-side helpers +import { validateSessionId, resolvePipePath } + from "@/server/tmux-utils"; +import { stopStaleWindowsPtyHosts } + from "@/lib/windows-pty-cleanup"; + +// CLI-only signal forwarding (POSIX only — guard with !isWindows()) +import { forwardSignalsToChild } from "../lib/shell.js"; +``` + +If a helper you need isn't in this list, that's a strong signal you should add it to `platform.ts` (or the closest existing module) rather than write platform-branching at the call site. diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index f81ff1165..1b34d136f 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -22,7 +22,7 @@ Every abstraction is a swappable plugin. All interfaces are defined in [`package | Slot | Interface | Default | Alternatives | | --------- | ----------- | ------------- | ---------------------------------------- | -| Runtime | `Runtime` | `tmux` | `process`, `docker`, `k8s`, `ssh`, `e2b` | +| Runtime | `Runtime` | `tmux` (Unix) / `process` (Windows; ConPTY via node-pty) | `process`, `docker`, `k8s`, `ssh`, `e2b` | | Agent | `Agent` | `claude-code` | `codex`, `aider`, `cursor`, `kimicode`, `opencode` | | Workspace | `Workspace` | `worktree` | `clone` | | Tracker | `Tracker` | `github` | `linear` | @@ -44,7 +44,7 @@ const dataDir = `~/.agent-orchestrator/${instanceId}`; This means: - Multiple orchestrator checkouts on the same machine never collide -- Session names are globally unique in tmux: `{hash}-{prefix}-{num}` +- Runtime handles are globally unique: `{hash}-{prefix}-{num}` (tmux session name on Unix; suffix of the named pipe `\\.\pipe\ao-pty-{sessionId}` on Windows) - User-facing names stay clean: `ao-1`, `myapp-2` ### Session Lifecycle @@ -388,8 +388,11 @@ cat ~/.agent-orchestrator/{hash}-{project}/sessions/{session-id} # Check API state curl http://localhost:3000/api/sessions/{session-id} -# Attach to tmux session directly +# Attach to the runtime session directly +# Unix: tmux attach -t {hash}-{prefix}-{num} +# Windows: there's no tmux. Use the AO command, which connects to \\.\pipe\ao-pty-: +ao session attach # Enable verbose logging AO_LOG_LEVEL=debug ao start @@ -469,10 +472,10 @@ Debuggability: `cat ~/.agent-orchestrator/a3b4-myapp/sessions/ao-1` shows full s Simpler local setup (no ngrok), survives orchestrator restarts, works offline. CI/review state is fetched, not pushed. **Why plugin slots?** -Swappability: use tmux locally, Docker in CI, Kubernetes in prod — without changing application code. Testability: mock any plugin in unit tests. Extensibility: users add company-specific plugins without forking. +Swappability: use `process` (ConPTY) on Windows, tmux on Linux/macOS, Docker in CI, Kubernetes in prod — without changing application code. The `Runtime` interface is the layer that lets the same agent/workspace/tracker stack run across all of them. Testability: mock any plugin in unit tests. Extensibility: users add company-specific plugins without forking. **Why hash-based namespacing?** -Multiple orchestrator checkouts on the same machine don't collide in tmux or on disk. Different checkouts get different hashes; projects within the same config share a hash. +Multiple orchestrator checkouts on the same machine don't collide at the runtime layer (tmux session names on Unix, named-pipe paths on Windows) or on disk. Different checkouts get different hashes; projects within the same config share a hash. **Why ESM with `.js` extensions?** Node.js ESM requires explicit extensions on local imports. All packages use `"type": "module"`. Missing extensions cause runtime errors. diff --git a/docs/openclaw-plugin-setup.md b/docs/openclaw-plugin-setup.md index 08010712a..223da0103 100644 --- a/docs/openclaw-plugin-setup.md +++ b/docs/openclaw-plugin-setup.md @@ -5,7 +5,7 @@ How to set up the Agent Orchestrator (AO) plugin for OpenClaw so the AI bot dele ## Prerequisites - [OpenClaw](https://openclaw.ai) installed and running -- [Agent Orchestrator](https://github.com/ComposioHQ/agent-orchestrator) installed with `ao init` completed in your repo +- [Agent Orchestrator](https://github.com/ComposioHQ/agent-orchestrator) installed with `ao start` completed in your repo - `ao`, `gh`, `tmux`, and `node` available in PATH - GitHub CLI (`gh`) authenticated diff --git a/packages/ao/CHANGELOG.md b/packages/ao/CHANGELOG.md index 2149ce5d2..6da307509 100644 --- a/packages/ao/CHANGELOG.md +++ b/packages/ao/CHANGELOG.md @@ -1,4 +1,34 @@ -# @composio/ao +# @aoagents/ao + +## 0.6.0 + +### Patch Changes + +- Updated dependencies [0f539a3] + - @aoagents/ao-cli@0.6.0 + +## 0.5.0 + +### Patch Changes + +- Updated dependencies [3a69722] + - @aoagents/ao-cli@0.5.0 + +## 0.4.0 + +### Patch Changes + +- Updated dependencies [2306078] +- Updated dependencies [f09cc72] +- Updated dependencies [f330a1e] +- Updated dependencies [e1bb51f] +- Updated dependencies [f674422] +- Updated dependencies [e7ad928] +- Updated dependencies [4701122] +- Updated dependencies [c8af50f] +- Updated dependencies [bcdda4b] +- Updated dependencies [1cbf657] + - @aoagents/ao-cli@0.4.0 ## 0.2.2 diff --git a/packages/ao/package.json b/packages/ao/package.json index ee31812f9..7279505f4 100644 --- a/packages/ao/package.json +++ b/packages/ao/package.json @@ -1,6 +1,6 @@ { "name": "@aoagents/ao", - "version": "0.3.0", + "version": "0.6.0", "description": "Orchestrate parallel AI coding agents — global CLI wrapper", "license": "MIT", "type": "module", diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 3ebefb021..a110d80f4 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -1,4 +1,204 @@ -# @composio/ao-cli +# @aoagents/ao-cli + +## 0.6.0 + +### Patch Changes + +- 0f539a3: Fix dashboard 404 after adding a project from the "AO is already running" menu. The CLI now notifies the running daemon to reload its cached config so the new project's page is reachable immediately. +- Updated dependencies +- Updated dependencies +- Updated dependencies [40aeb78] +- Updated dependencies +- Updated dependencies +- Updated dependencies +- Updated dependencies + - @aoagents/ao-core@0.6.0 + - @aoagents/ao-web@0.6.0 + - @aoagents/ao-plugin-runtime-tmux@0.6.0 + - @aoagents/ao-plugin-agent-aider@0.6.0 + - @aoagents/ao-plugin-agent-claude-code@0.6.0 + - @aoagents/ao-plugin-agent-codex@0.6.0 + - @aoagents/ao-plugin-agent-cursor@0.1.4 + - @aoagents/ao-plugin-agent-kimicode@0.1.3 + - @aoagents/ao-plugin-agent-opencode@0.6.0 + - @aoagents/ao-plugin-notifier-composio@0.6.0 + - @aoagents/ao-plugin-notifier-desktop@0.6.0 + - @aoagents/ao-plugin-notifier-discord@0.2.9 + - @aoagents/ao-plugin-notifier-openclaw@0.2.9 + - @aoagents/ao-plugin-notifier-slack@0.6.0 + - @aoagents/ao-plugin-notifier-webhook@0.6.0 + - @aoagents/ao-plugin-runtime-process@0.6.0 + - @aoagents/ao-plugin-scm-github@0.6.0 + - @aoagents/ao-plugin-terminal-iterm2@0.6.0 + - @aoagents/ao-plugin-terminal-web@0.6.0 + - @aoagents/ao-plugin-tracker-github@0.6.0 + - @aoagents/ao-plugin-tracker-linear@0.6.0 + - @aoagents/ao-plugin-workspace-clone@0.6.0 + - @aoagents/ao-plugin-workspace-worktree@0.6.0 + +## 0.5.0 + +### Minor Changes + +- 3a69722: Remove the deprecated `ao init` command. Use `ao start` instead — it auto-creates the config on first run in an unconfigured repo. + +### Patch Changes + +- Updated dependencies [dd07b6b] +- Updated dependencies [dd07b6b] +- Updated dependencies [dd07b6b] + - @aoagents/ao-core@0.5.0 + - @aoagents/ao-web@0.5.0 + - @aoagents/ao-plugin-agent-aider@0.5.0 + - @aoagents/ao-plugin-agent-claude-code@0.5.0 + - @aoagents/ao-plugin-agent-codex@0.5.0 + - @aoagents/ao-plugin-agent-cursor@0.1.3 + - @aoagents/ao-plugin-agent-kimicode@0.1.2 + - @aoagents/ao-plugin-agent-opencode@0.5.0 + - @aoagents/ao-plugin-notifier-composio@0.5.0 + - @aoagents/ao-plugin-notifier-desktop@0.5.0 + - @aoagents/ao-plugin-notifier-discord@0.2.8 + - @aoagents/ao-plugin-notifier-openclaw@0.2.8 + - @aoagents/ao-plugin-notifier-slack@0.5.0 + - @aoagents/ao-plugin-notifier-webhook@0.5.0 + - @aoagents/ao-plugin-runtime-process@0.5.0 + - @aoagents/ao-plugin-runtime-tmux@0.5.0 + - @aoagents/ao-plugin-scm-github@0.5.0 + - @aoagents/ao-plugin-terminal-iterm2@0.5.0 + - @aoagents/ao-plugin-terminal-web@0.5.0 + - @aoagents/ao-plugin-tracker-github@0.5.0 + - @aoagents/ao-plugin-tracker-linear@0.5.0 + - @aoagents/ao-plugin-workspace-clone@0.5.0 + - @aoagents/ao-plugin-workspace-worktree@0.5.0 + +## 0.4.0 + +### Minor Changes + +- f330a1e: `ao session ls` and `ao status` now hide terminated sessions (`killed`, `terminated`, `done`, `merged`, `errored`, `cleanup`) by default. A dim footer reports how many were hidden and how to surface them. Pass `--include-terminated` to restore the previous unfiltered output. + + Core change: `parseCanonicalLifecycle()` now preserves `pr.state="merged"` when reconstructing legacy metadata with `status=merged` but no `pr=` URL (previously collapsed to `pr.state="none"`, which made `isTerminalSession()` return false for those sessions). Also exports `sessionFromMetadata` so consumers can round-trip flat metadata through the canonical lifecycle. + + **Breaking — JSON output shape:** `ao session ls --json` and `ao status --json` now emit `{ data: [...], meta: { hiddenTerminatedCount: number } }` instead of a bare array. Scripts consuming the JSON must read `.data` for the session list. `--include-terminated` restores full data and reports `hiddenTerminatedCount: 0`. + + The existing `-a, --all` flag still only governs orchestrator visibility on `ao session ls` — it does **not** re-enable terminated sessions. Combine with `--include-terminated` when you want both. + +- e7ad928: Allow workers to report non-terminal PR workflow events like `pr-created`, `draft-pr-created`, and `ready-for-review` with optional PR URL/number metadata, while keeping merged and closed PR state SCM-owned. + + **Migration:** `Session` now carries canonical lifecycle truth in `session.lifecycle` + and explicit activity-evidence metadata in `session.activitySignal`. Third-party + callers that construct `Session` objects directly must populate those fields or + route through the core session helpers that synthesize them. + +### Patch Changes + +- 2306078: Add SQLite-backed activity event logging for session and lifecycle diagnostics, plus `ao events` commands for listing, searching, and inspecting event log stats. +- f09cc72: `ao session ls` hides terminal sessions in text output by default; use `--include-terminated` for the full text list. +- e1bb51f: Fix restore behavior across AO session recovery flows. + - restore the latest dead-but-restorable orchestrator on `ao start` instead of silently spawning a new orchestrator when tmux is gone + - make worker session orchestrator navigation prefer the most recently active live orchestrator for the project + - make permissionless Codex restores preserve dangerous bypass semantics so resumed workers behave like fresh permissionless launches + +- f674422: Make project orchestrators deterministic and idempotent. + - ensure each project uses the canonical `{prefix}-orchestrator` session instead of creating numbered main orchestrators + - make `ao start`, the dashboard, and the orchestrator API reuse or restore the canonical session + - keep legacy numbered orchestrators visible as stale sessions without treating them as the main orchestrator + +- 4701122: opencode: bound /tmp blast radius and consolidate session-list cache + + Addresses review feedback on PR #1478: + - **TMPDIR isolation.** Every `opencode` child we spawn now points at + `~/.agent-orchestrator/.bun-tmp/` via `TMPDIR`/`TMP`/`TEMP`. Bun's + embedded shared-library extraction lands there instead of the system + `/tmp`, so the cli janitor only ever sweeps AO-owned files. Other + users' or other applications' Bun artifacts on a shared host can no + longer be touched by the regex. + - **Single shared session-list cache.** Core and the agent-opencode + plugin previously kept independent caches; per poll cycle the system + spawned at least two `opencode session list` processes instead of + one. Both consumers now use the shared cache exported from + `@aoagents/ao-core` (`getCachedOpenCodeSessionList`). + - **TTL no longer covers the send-confirmation loop.** The cache TTL + dropped from 3s to 500ms so the + `updatedAt > baselineUpdatedAt` delivery signal in + `sendWithConfirmation` actually fires. Concurrent callers still + share the in-flight promise. + - **Delete invalidates the cache.** `deleteOpenCodeSession` now calls + `invalidateOpenCodeSessionListCache()` on success so reuse, remap, + and restore code paths cannot observe a deleted session id within + the TTL window. + - **Janitor reliability.** `sweepOnce` now filters synchronously + before allocating per-file promises (matters on hosts with thousands + of `/tmp` entries), and `stopBunTmpJanitor()` is now async and awaits + any in-flight sweep so SIGTERM cannot exit while `unlink` is mid-flight. + - **Janitor observability.** The sweep callback in `ao start` now logs + successful reclaims, not just errors, so operators can confirm the + janitor is doing useful work. + +- c8af50f: Make `ProjectConfig.repo` optional to support projects without a configured remote. + + **Migration:** `ProjectConfig.repo` is now `string | undefined` instead of `string`. + External plugins that access `project.repo` directly (e.g. `project.repo.split("/")`) must + add a null check first. Use a guard like `if (!project.repo) return null;` or a helper that + throws with a descriptive error. + +- bcdda4b: Tighten the session lifecycle review follow-ups by debouncing report-watcher reactions, restoring the shared Geist/JetBrains font setup, wiring recovery validation to real agent activity probes, adding direct coverage for `ao report`, activity-signal classification, and dashboard lifecycle audit panels, fixing the remaining lifecycle-state regressions around legacy merged-session rehydration and malformed canonical payload parsing, making agent-report metadata writes atomic, persisting canonical payloads for legacy sessions on read, stabilizing detecting evidence hashes, and removing the remaining inline-style cleanup debt from the session detail view. Follow-on fixes also split the Session Detail view into smaller components, harden PR URL parsing and wrapper capture for GitHub Enterprise and GitLab-style hosts, redact sensitive observability payload fields, bound on-disk audit logs, and align cleanup wording with the current merged-session lifecycle policy. +- 1cbf657: Split orchestrator-only detail views from worker detail views, add an auditable history for `ao acknowledge` / `ao report`, and preserve canonical `needs_input` / `stuck` lifecycle states when polling only has weak or unchanged evidence. +- Updated dependencies [2306078] +- Updated dependencies [b0d0994] +- Updated dependencies [faaddb1] +- Updated dependencies [0cf0190] +- Updated dependencies [f330a1e] +- Updated dependencies [a862327] +- Updated dependencies [331f1ce] +- Updated dependencies [e465a47] +- Updated dependencies [703d584] +- Updated dependencies [e1bb51f] +- Updated dependencies [08667c8] +- Updated dependencies [eca3001] +- Updated dependencies [f674422] +- Updated dependencies [62353eb] +- Updated dependencies [bd36c7b] +- Updated dependencies [e7ad928] +- Updated dependencies [ca8c4cc] +- Updated dependencies [7b82374] +- Updated dependencies [4701122] +- Updated dependencies [c8af50f] +- Updated dependencies [bcdda4b] +- Updated dependencies [eb7314b] +- Updated dependencies [a8bc746] +- Updated dependencies [a8bc746] +- Updated dependencies [1cbf657] +- Updated dependencies [c447c7c] +- Updated dependencies [a45eb32] +- Updated dependencies [7072143] +- Updated dependencies [a8bc746] +- Updated dependencies [e518562] +- Updated dependencies [fed25d5] +- Updated dependencies [ed2dcea] + - @aoagents/ao-core@0.4.0 + - @aoagents/ao-plugin-agent-codex@0.4.0 + - @aoagents/ao-plugin-agent-claude-code@0.4.0 + - @aoagents/ao-web@0.4.0 + - @aoagents/ao-plugin-agent-opencode@0.4.0 + - @aoagents/ao-plugin-scm-github@0.4.0 + - @aoagents/ao-plugin-tracker-github@0.4.0 + - @aoagents/ao-plugin-agent-aider@0.4.0 + - @aoagents/ao-plugin-agent-cursor@0.1.2 + - @aoagents/ao-plugin-agent-kimicode@0.1.1 + - @aoagents/ao-plugin-notifier-composio@0.4.0 + - @aoagents/ao-plugin-notifier-desktop@0.4.0 + - @aoagents/ao-plugin-notifier-discord@0.2.7 + - @aoagents/ao-plugin-notifier-openclaw@0.2.7 + - @aoagents/ao-plugin-notifier-slack@0.4.0 + - @aoagents/ao-plugin-notifier-webhook@0.4.0 + - @aoagents/ao-plugin-runtime-process@0.4.0 + - @aoagents/ao-plugin-runtime-tmux@0.4.0 + - @aoagents/ao-plugin-terminal-iterm2@0.4.0 + - @aoagents/ao-plugin-terminal-web@0.4.0 + - @aoagents/ao-plugin-tracker-linear@0.4.0 + - @aoagents/ao-plugin-workspace-clone@0.4.0 + - @aoagents/ao-plugin-workspace-worktree@0.4.0 ## 0.2.2 diff --git a/packages/cli/__tests__/commands/dashboard.test.ts b/packages/cli/__tests__/commands/dashboard.test.ts index 6151ff1ce..8b7c029f5 100644 --- a/packages/cli/__tests__/commands/dashboard.test.ts +++ b/packages/cli/__tests__/commands/dashboard.test.ts @@ -3,9 +3,10 @@ import { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync } from "node: import { join } from "node:path"; import { tmpdir } from "node:os"; -const { mockExec, mockExecSilent } = vi.hoisted(() => ({ +const { mockExec, mockExecSilent, mockFindPidByPort } = vi.hoisted(() => ({ mockExec: vi.fn(), mockExecSilent: vi.fn(), + mockFindPidByPort: vi.fn(), })); vi.mock("../../src/lib/shell.js", () => ({ @@ -13,6 +14,15 @@ vi.mock("../../src/lib/shell.js", () => ({ execSilent: mockExecSilent, })); +vi.mock("@aoagents/ao-core", async (importOriginal) => { + // eslint-disable-next-line @typescript-eslint/consistent-type-imports + const actual = await importOriginal(); + return { + ...actual, + findPidByPort: mockFindPidByPort, + }; +}); + vi.mock("ora", () => ({ default: () => ({ start: vi.fn().mockReturnThis(), @@ -29,6 +39,7 @@ beforeEach(() => { tmpDir = mkdtempSync(join(tmpdir(), "ao-dashboard-test-")); mockExec.mockReset(); mockExecSilent.mockReset(); + mockFindPidByPort.mockReset(); mockExec.mockResolvedValue({ stdout: "", stderr: "" }); }); @@ -68,27 +79,6 @@ describe("cleanNextCache", () => { }); }); -describe("findRunningDashboardPid", () => { - it("returns PID when a process is listening", async () => { - mockExecSilent.mockResolvedValue("12345"); - - const { findRunningDashboardPid } = await import("../../src/lib/dashboard-rebuild.js"); - - const pid = await findRunningDashboardPid(3000); - expect(pid).toBe("12345"); - expect(mockExecSilent).toHaveBeenCalledWith("lsof", ["-ti", ":3000", "-sTCP:LISTEN"]); - }); - - it("returns null when no process is listening", async () => { - mockExecSilent.mockResolvedValue(null); - - const { findRunningDashboardPid } = await import("../../src/lib/dashboard-rebuild.js"); - - const pid = await findRunningDashboardPid(3000); - expect(pid).toBeNull(); - }); -}); - describe("isInstalledUnderNodeModules", () => { it("returns true for a Unix node_modules path segment", async () => { const { isInstalledUnderNodeModules } = await import("../../src/lib/dashboard-rebuild.js"); @@ -285,7 +275,9 @@ describe("looksLikeStaleBuild pattern matching", () => { }); describe("findRunningDashboardPidsForWebDir", () => { - it("returns only listeners whose cwd matches the web directory", async () => { + // Unix-only: Windows code path skips lsof and uses findPidByPort (no cwd check), + // by design — see findRunningDashboardPidsForWebDir in dashboard-rebuild.ts. + it.skipIf(process.platform === "win32")("returns only listeners whose cwd matches the web directory", async () => { const webDir = join(tmpDir, "packages", "web"); mkdirSync(webDir, { recursive: true }); @@ -302,7 +294,7 @@ describe("findRunningDashboardPidsForWebDir", () => { expect(mockExecSilent).toHaveBeenCalledWith("lsof", ["-a", "-p", "111", "-d", "cwd", "-Fn"]); }); - it("deduplicates dashboard pids found on multiple ports", async () => { + it.skipIf(process.platform === "win32")("deduplicates dashboard pids found on multiple ports", async () => { const webDir = join(tmpDir, "packages", "web"); mkdirSync(webDir, { recursive: true }); @@ -317,4 +309,47 @@ describe("findRunningDashboardPidsForWebDir", () => { await expect(findRunningDashboardPidsForWebDir(webDir, [3000, 3001])).resolves.toEqual(["111"]); }); + + // Windows-runif parallels: on Windows, the function intentionally skips the + // lsof + cwd verification (lsof doesn't exist) and trusts findPidByPort. The + // tests above assert lsof behavior; these assert the Windows path runs the + // findPidByPort branch and produces correct dedup semantics. + it.runIf(process.platform === "win32")( + "returns all pids on the listed ports via findPidByPort on Windows", + async () => { + const webDir = join(tmpDir, "packages", "web"); + mkdirSync(webDir, { recursive: true }); + + mockFindPidByPort.mockImplementation(async (port: number) => + port === 3000 ? "111" : port === 3001 ? "222" : null, + ); + + const { findRunningDashboardPidsForWebDir } = + await import("../../src/lib/dashboard-rebuild.js"); + + const pids = await findRunningDashboardPidsForWebDir(webDir, [3000, 3001, 3002]); + expect(pids.sort()).toEqual(["111", "222"]); + // lsof must NOT be invoked on Windows. + expect(mockExecSilent).not.toHaveBeenCalled(); + }, + ); + + it.runIf(process.platform === "win32")( + "deduplicates dashboard pids found on multiple ports on Windows", + async () => { + const webDir = join(tmpDir, "packages", "web"); + mkdirSync(webDir, { recursive: true }); + + // Same pid claimed on two ports (e.g. parent + child Next.js workers + // sharing the listener) — must collapse to one entry. + mockFindPidByPort.mockResolvedValue("111"); + + const { findRunningDashboardPidsForWebDir } = + await import("../../src/lib/dashboard-rebuild.js"); + + await expect(findRunningDashboardPidsForWebDir(webDir, [3000, 3001])).resolves.toEqual([ + "111", + ]); + }, + ); }); diff --git a/packages/cli/__tests__/commands/init.test.ts b/packages/cli/__tests__/commands/init.test.ts deleted file mode 100644 index ce724a1f5..000000000 --- a/packages/cli/__tests__/commands/init.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { describe, it, expect } from "vitest"; - -import { Command } from "commander"; -import { registerInit } from "../../src/commands/init.js"; - -describe("init command", () => { - it("registers as a deprecated command", () => { - const program = new Command(); - registerInit(program); - - const initCmd = program.commands.find((c) => c.name() === "init"); - expect(initCmd).toBeDefined(); - expect(initCmd!.description()).toContain("deprecated"); - }); - - it("has no --output, --auto, or --smart flags", () => { - const program = new Command(); - registerInit(program); - - const initCmd = program.commands.find((c) => c.name() === "init"); - expect(initCmd).toBeDefined(); - - const optionNames = initCmd!.options.map((o) => o.long); - expect(optionNames).not.toContain("--output"); - expect(optionNames).not.toContain("--auto"); - expect(optionNames).not.toContain("--smart"); - }); -}); diff --git a/packages/cli/__tests__/commands/open.test.ts b/packages/cli/__tests__/commands/open.test.ts index 83bef6e18..94a69a4ca 100644 --- a/packages/cli/__tests__/commands/open.test.ts +++ b/packages/cli/__tests__/commands/open.test.ts @@ -1,35 +1,109 @@ +import type * as ChildProcess from "node:child_process"; import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; -const { mockExec, mockConfigRef, mockTmux } = vi.hoisted(() => ({ +const { + mockExec, + mockSpawn, + mockConfigRef, + mockListRef, + mockOpenUrl, + mockIsMacRef, + mockIsWindowsRef, + mockRunningRef, +} = vi.hoisted(() => ({ mockExec: vi.fn(), - mockTmux: vi.fn(), + mockSpawn: vi.fn(), mockConfigRef: { current: null as Record | null }, + mockListRef: { current: [] as Array<{ id: string; projectId: string; lifecycle: { session: { state: string } } }> }, + mockOpenUrl: vi.fn(), + mockIsMacRef: { current: true }, + mockIsWindowsRef: { current: false }, + mockRunningRef: { current: { pid: 1, port: 3000, projects: [] } as { pid: number; port: number; projects: string[] } | null }, })); +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, spawn: mockSpawn }; +}); + vi.mock("../../src/lib/shell.js", () => ({ exec: mockExec, execSilent: vi.fn(), - tmux: mockTmux, + tmux: vi.fn(), git: vi.fn(), gh: vi.fn(), - getTmuxSessions: async () => { - const output = await mockTmux("list-sessions", "-F", "#{session_name}"); - if (!output) return []; - return output.split("\n").filter(Boolean); - }, + getTmuxSessions: vi.fn(), getTmuxActivity: vi.fn().mockResolvedValue(null), })); +vi.mock("../../src/lib/create-session-manager.js", () => ({ + getSessionManager: async () => ({ + list: async () => mockListRef.current, + }), +})); + +vi.mock("../../src/lib/web-dir.js", () => ({ + openUrl: mockOpenUrl, +})); + +vi.mock("../../src/lib/running-state.js", () => ({ + getRunning: async () => mockRunningRef.current, +})); + vi.mock("@aoagents/ao-core", () => ({ loadConfig: () => mockConfigRef.current, + isMac: () => mockIsMacRef.current, + isWindows: () => mockIsWindowsRef.current, + isTerminalSession: (s: { lifecycle?: { session?: { state?: string } } }) => + s.lifecycle?.session?.state === "terminated" || s.lifecycle?.session?.state === "done", })); import { Command } from "commander"; import { registerOpen } from "../../src/commands/open.js"; +// Fictional fixture path used only inside the in-memory mock config below. +// Not anyone's real filesystem path — assertions reference this constant so +// the test verifies "config.projects[id].path flows through to wt's -d flag", +// independent of the literal value. +const TEST_REPO_PATH = "/fixtures/test-repo"; + let program: Command; let consoleSpy: ReturnType; +function makeSession(id: string, projectId: string, state = "working") { + const sessionState = + state === "terminated" + ? { + state, + reason: "runtime_lost", + terminatedAt: "2026-05-04T19:51:10.488Z", + } + : { state, reason: "task_in_progress", terminatedAt: null }; + const runtimeState = + state === "terminated" + ? { state: "missing", reason: "process_missing" } + : { state: "alive", reason: "process_running" }; + return { + id, + projectId, + lifecycle: { + session: sessionState, + runtime: runtimeState, + }, + }; +} + +function makeSpawnChild() { + const handlers: Record void> = {}; + return { + on: vi.fn((event: string, cb: () => void) => { + handlers[event] = cb; + return undefined; + }), + unref: vi.fn(), + }; +} + beforeEach(() => { mockConfigRef.current = { dataDir: "/tmp/ao", @@ -55,6 +129,12 @@ beforeEach(() => { path: "/home/user/backend", defaultBranch: "main", }, + "test-repo": { + name: "Test Repo", + repo: "org/test-repo", + path: TEST_REPO_PATH, + defaultBranch: "main", + }, }, notifiers: {}, notificationRouting: {}, @@ -71,20 +151,27 @@ beforeEach(() => { }); mockExec.mockReset(); - mockTmux.mockReset(); + mockSpawn.mockReset(); + mockOpenUrl.mockReset(); + mockListRef.current = []; + mockIsMacRef.current = true; + mockIsWindowsRef.current = false; + mockRunningRef.current = { pid: 1, port: 3000, projects: [] }; mockExec.mockResolvedValue({ stdout: "", stderr: "" }); + mockSpawn.mockReturnValue(makeSpawnChild()); }); afterEach(() => { vi.restoreAllMocks(); }); -describe("open command", () => { +describe("open command (macOS)", () => { it("opens all sessions when target is 'all'", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1\napp-2\nbackend-1"; - return null; - }); + mockListRef.current = [ + makeSession("app-1", "my-app"), + makeSession("app-2", "my-app"), + makeSession("backend-1", "backend"), + ]; await program.parseAsync(["node", "test", "open", "all"]); @@ -96,10 +183,7 @@ describe("open command", () => { }); it("opens all sessions when no target given", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1"; - return null; - }); + mockListRef.current = [makeSession("app-1", "my-app")]; await program.parseAsync(["node", "test", "open"]); @@ -108,10 +192,11 @@ describe("open command", () => { }); it("opens sessions for a specific project", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1\napp-2\nbackend-1"; - return null; - }); + mockListRef.current = [ + makeSession("app-1", "my-app"), + makeSession("app-2", "my-app"), + makeSession("backend-1", "backend"), + ]; await program.parseAsync(["node", "test", "open", "my-app"]); @@ -122,25 +207,8 @@ describe("open command", () => { expect(output).not.toContain("backend-1"); }); - it("matches hashed tmux worker session names", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "1686e4aaaeaa-app-1\nbackend-1"; - return null; - }); - - await program.parseAsync(["node", "test", "open", "my-app"]); - - const output = consoleSpy.mock.calls.map((c) => String(c[0])).join("\n"); - expect(output).toContain("Opening 1 session"); - expect(output).toContain("1686e4aaaeaa-app-1"); - expect(output).not.toContain("backend-1"); - }); - it("opens a single session by name", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1\napp-2"; - return null; - }); + mockListRef.current = [makeSession("app-1", "my-app"), makeSession("app-2", "my-app")]; await program.parseAsync(["node", "test", "open", "app-1"]); @@ -150,10 +218,7 @@ describe("open command", () => { }); it("rejects unknown target", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1"; - return null; - }); + mockListRef.current = [makeSession("app-1", "my-app")]; await expect(program.parseAsync(["node", "test", "open", "nonexistent"])).rejects.toThrow( "process.exit(1)", @@ -161,10 +226,7 @@ describe("open command", () => { }); it("passes --new-window flag to open-iterm-tab", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1"; - return null; - }); + mockListRef.current = [makeSession("app-1", "my-app")]; await program.parseAsync(["node", "test", "open", "-w", "app-1"]); @@ -172,33 +234,82 @@ describe("open command", () => { }); it("falls back gracefully when open-iterm-tab fails", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-1"; - return null; - }); + mockListRef.current = [makeSession("app-1", "my-app")]; mockExec.mockRejectedValue(new Error("command not found")); await program.parseAsync(["node", "test", "open", "app-1"]); - const output = consoleSpy.mock.calls.map((c) => String(c[0])).join("\n"); - expect(output).toContain("http://localhost:3000/projects/my-app/sessions/app-1"); + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/my-app/sessions/app-1", + ); }); - it("falls back to the owning project for orchestrator sessions", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "list-sessions") return "app-orchestrator"; - return null; - }); - mockExec.mockRejectedValue(new Error("command not found")); + it("excludes terminated sessions from aggregate targets", async () => { + mockListRef.current = [ + makeSession("app-1", "my-app"), + makeSession("app-dead", "my-app", "terminated"), + ]; - await program.parseAsync(["node", "test", "open", "app-orchestrator"]); + await program.parseAsync(["node", "test", "open", "all"]); const output = consoleSpy.mock.calls.map((c) => String(c[0])).join("\n"); - expect(output).toContain("http://localhost:3000/projects/my-app/sessions/app-orchestrator"); + expect(output).toContain("Opening 1 session"); + expect(output).toContain("app-1"); + expect(output).not.toContain("app-dead"); + }); + + it("includes a terminated session when looked up by name (opens dashboard with death reason)", async () => { + mockListRef.current = [makeSession("app-dead", "my-app", "terminated")]; + + await program.parseAsync(["node", "test", "open", "app-dead"]); + + expect(mockExec).not.toHaveBeenCalled(); + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/my-app/sessions/app-dead", + ); + const output = consoleSpy.mock.calls.map((c) => String(c[0])).join("\n"); + expect(output).toContain("(terminated)"); + expect(output).toContain("session=runtime_lost"); + expect(output).toContain("runtime=process_missing"); + expect(output).toContain("ao session restore app-dead"); + }); + + it("--browser forces dashboard URL even on macOS", async () => { + mockListRef.current = [makeSession("app-1", "my-app")]; + + await program.parseAsync(["node", "test", "open", "-b", "app-1"]); + + expect(mockExec).not.toHaveBeenCalled(); + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/my-app/sessions/app-1", + ); + }); + + it("uses the live daemon's port from running-state, not config", async () => { + mockListRef.current = [makeSession("app-1", "my-app")]; + mockExec.mockRejectedValue(new Error("no iterm")); + mockRunningRef.current = { pid: 42, port: 4173, projects: ["my-app"] }; + + await program.parseAsync(["node", "test", "open", "app-1"]); + + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:4173/projects/my-app/sessions/app-1", + ); + }); + + it("warns when daemon is not running (URL fallback may not load)", async () => { + mockListRef.current = [makeSession("app-1", "my-app")]; + mockExec.mockRejectedValue(new Error("no iterm")); + mockRunningRef.current = null; + + await program.parseAsync(["node", "test", "open", "app-1"]); + + const output = consoleSpy.mock.calls.map((c) => String(c[0])).join("\n"); + expect(output).toContain("daemon does not appear to be running"); }); it("shows 'No sessions to open' when none exist", async () => { - mockTmux.mockResolvedValue(null); + mockListRef.current = []; await program.parseAsync(["node", "test", "open", "my-app"]); @@ -206,3 +317,101 @@ describe("open command", () => { expect(output).toContain("No sessions to open"); }); }); + +describe("open command (Windows)", () => { + beforeEach(() => { + mockIsMacRef.current = false; + mockIsWindowsRef.current = true; + }); + + it("spawns Windows Terminal running `ao session attach `", async () => { + mockListRef.current = [makeSession("tr-orchestrator", "test-repo")]; + + await program.parseAsync(["node", "test", "open", "tr-orchestrator"]); + + expect(mockSpawn).toHaveBeenCalledTimes(1); + const [cmd, args] = mockSpawn.mock.calls[0]; + expect(cmd).toBe("wt.exe"); + expect(args).toEqual([ + "-w", "0", "new-tab", + "--title", "ao:tr-orchestrator", + "-d", TEST_REPO_PATH, + "cmd.exe", "/k", "ao", "session", "attach", "tr-orchestrator", + ]); + expect(mockOpenUrl).not.toHaveBeenCalled(); + }); + + it("falls back to `cmd /k` when wt.exe is unavailable", async () => { + mockListRef.current = [makeSession("tr-orchestrator", "test-repo")]; + mockSpawn.mockImplementationOnce(() => { + throw new Error("ENOENT: wt.exe not found"); + }); + mockSpawn.mockImplementationOnce(() => makeSpawnChild()); + + await program.parseAsync(["node", "test", "open", "tr-orchestrator"]); + + expect(mockSpawn).toHaveBeenCalledTimes(2); + expect(mockSpawn.mock.calls[1][0]).toBe("cmd.exe"); + expect(mockSpawn.mock.calls[1][1]).toEqual([ + "/c", "start", "ao:tr-orchestrator", + "/d", TEST_REPO_PATH, + "cmd.exe", "/k", "ao", "session", "attach", "tr-orchestrator", + ]); + }); + + it("falls back to dashboard URL when both terminal launchers fail", async () => { + mockListRef.current = [makeSession("tr-orchestrator", "test-repo")]; + mockSpawn.mockImplementation(() => { + throw new Error("ENOENT"); + }); + + await program.parseAsync(["node", "test", "open", "tr-orchestrator"]); + + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/test-repo/sessions/tr-orchestrator", + ); + }); + + it("--browser skips terminal spawn and opens URL directly", async () => { + mockListRef.current = [makeSession("tr-orchestrator", "test-repo")]; + + await program.parseAsync(["node", "test", "open", "-b", "tr-orchestrator"]); + + expect(mockSpawn).not.toHaveBeenCalled(); + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/test-repo/sessions/tr-orchestrator", + ); + }); + + it("opens dashboard URL for terminated sessions instead of attempting attach", async () => { + mockListRef.current = [makeSession("tr-orchestrator", "test-repo", "terminated")]; + + await program.parseAsync(["node", "test", "open", "tr-orchestrator"]); + + expect(mockSpawn).not.toHaveBeenCalled(); + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/test-repo/sessions/tr-orchestrator", + ); + const output = consoleSpy.mock.calls.map((c) => String(c[0])).join("\n"); + expect(output).toContain("(terminated)"); + }); +}); + +describe("open command (Linux)", () => { + beforeEach(() => { + mockIsMacRef.current = false; + mockIsWindowsRef.current = false; + }); + + it("opens the dashboard URL (no terminal-spawn helper exists)", async () => { + mockListRef.current = [makeSession("app-1", "my-app")]; + + await program.parseAsync(["node", "test", "open", "app-1"]); + + expect(mockSpawn).not.toHaveBeenCalled(); + expect(mockExec).not.toHaveBeenCalled(); + expect(mockOpenUrl).toHaveBeenCalledWith( + "http://localhost:3000/projects/my-app/sessions/app-1", + ); + }); +}); diff --git a/packages/cli/__tests__/commands/send.test.ts b/packages/cli/__tests__/commands/send.test.ts index ea0cba48a..7955966f6 100644 --- a/packages/cli/__tests__/commands/send.test.ts +++ b/packages/cli/__tests__/commands/send.test.ts @@ -144,20 +144,22 @@ describe("send command", () => { ); }); - it("detects busy session and waits via agent plugin", async () => { - mockTmux.mockImplementation(async (...args: string[]) => { - if (args[0] === "has-session") return ""; - if (args[0] === "capture-pane") return "some output"; - return ""; - }); + it( + "detects busy session and waits via agent plugin", + async () => { + mockTmux.mockImplementation(async (...args: string[]) => { + if (args[0] === "has-session") return ""; + if (args[0] === "capture-pane") return "some output"; + return ""; + }); - // First call: active (busy), second call: idle, third call: active (verification) - mockDetectActivity - .mockReturnValueOnce("active") // busy - .mockReturnValueOnce("idle") // now idle - .mockReturnValueOnce("active"); // verification: processing + // First call: active (busy), second call: idle, third call: active (verification) + mockDetectActivity + .mockReturnValueOnce("active") // busy + .mockReturnValueOnce("idle") // now idle + .mockReturnValueOnce("active"); // verification: processing - await program.parseAsync(["node", "test", "send", "my-session", "fix", "the", "bug"]); + await program.parseAsync(["node", "test", "send", "my-session", "fix", "the", "bug"]); // Should have eventually sent the message expect(mockExec).toHaveBeenCalledWith("tmux", [ @@ -167,7 +169,7 @@ describe("send command", () => { "-l", "fix the bug", ]); - }, 15000); + }, 30_000); it("skips busy detection with --no-wait", async () => { mockTmux.mockImplementation(async (...args: string[]) => { diff --git a/packages/cli/__tests__/commands/session.test.ts b/packages/cli/__tests__/commands/session.test.ts index 76bc50e94..c2427836d 100644 --- a/packages/cli/__tests__/commands/session.test.ts +++ b/packages/cli/__tests__/commands/session.test.ts @@ -30,6 +30,7 @@ const { mockGh, mockExec, mockSpawn, + mockIsWindows, mockConfigRef, mockSessionManager, sessionsDirRef, @@ -39,6 +40,7 @@ const { mockGh: vi.fn(), mockExec: vi.fn(), mockSpawn: vi.fn(), + mockIsWindows: vi.fn().mockReturnValue(false), mockConfigRef: { current: null as Record | null }, mockSessionManager: { list: vi.fn(), @@ -71,6 +73,16 @@ vi.mock("node:child_process", async (importOriginal) => { }; }); +const mockNetConnect = vi.fn(); +vi.mock("node:net", async (importOriginal) => { + // eslint-disable-next-line @typescript-eslint/consistent-type-imports + const actual = await importOriginal(); + return { + ...actual, + connect: (...args: unknown[]) => mockNetConnect(...args), + }; +}); + vi.mock("../../src/lib/shell.js", () => ({ tmux: mockTmux, exec: mockExec, @@ -96,6 +108,8 @@ vi.mock("@aoagents/ao-core", async (importOriginal) => { return { ...actual, loadConfig: () => mockConfigRef.current, + isWindows: () => mockIsWindows(), + generateConfigHash: () => "abcdef123456", }; }); @@ -735,6 +749,7 @@ describe("session attach", () => { }); it("fails when tmux session does not exist", async () => { + mockIsWindows.mockReturnValue(false); mockSessionManager.get.mockResolvedValue(null); mockTmux.mockResolvedValue(null); @@ -742,6 +757,207 @@ describe("session attach", () => { program.parseAsync(["node", "test", "session", "attach", "unknown-1"]), ).rejects.toThrow("process.exit(1)"); }); + + it("connects to named pipe on Windows", async () => { + mockIsWindows.mockReturnValue(true); + mockSessionManager.get.mockResolvedValue({ + id: "app-1", + projectId: "my-app", + status: "working", + activity: null, + branch: null, + issueId: null, + pr: null, + workspacePath: null, + runtimeHandle: { id: "hash-app-1", runtimeName: "process", data: { pipePath: "\\\\.\\pipe\\ao-pty-hash-app-1" } }, + agentInfo: null, + createdAt: new Date(), + lastActivityAt: new Date(), + metadata: {}, + } satisfies Session); + + const mockSocket = new EventEmitter(); + Object.assign(mockSocket, { destroy: vi.fn(), write: vi.fn() }); + mockNetConnect.mockReturnValue(mockSocket); + + // Fire the command — it awaits an infinite promise, so don't await it. + // The process.exit mock throws, which surfaces synchronously through emit(). + void program.parseAsync(["node", "test", "session", "attach", "app-1"]); + + await new Promise((r) => setTimeout(r, 10)); + mockSocket.emit("connect"); + await new Promise((r) => setTimeout(r, 10)); + + // Exercise binary protocol: send terminal data (0x01) + const termData = Buffer.from("hello"); + const dataFrame = Buffer.alloc(5 + termData.length); + dataFrame.writeUInt8(0x01, 0); + dataFrame.writeUInt32BE(termData.length, 1); + termData.copy(dataFrame, 5); + const writeSpy = vi.spyOn(process.stdout, "write").mockImplementation(() => true); + mockSocket.emit("data", dataFrame); + expect(writeSpy).toHaveBeenCalledWith(termData); + writeSpy.mockRestore(); + + // Exercise stdin relay: send input data (becomes MSG_TERMINAL_INPUT = 0x02) + const inputData = Buffer.from("ls\r"); + process.stdin.emit("data", inputData); + expect((mockSocket as { write: ReturnType }).write).toHaveBeenCalled(); + const written = (mockSocket as { write: ReturnType }).write.mock.calls.at(-1)![0] as Buffer; + expect(written.readUInt8(0)).toBe(0x02); // MSG_TERMINAL_INPUT + expect(written.subarray(5).toString()).toBe("ls\r"); + + // close handler calls process.exit(0) which throws synchronously through emit + expect(() => mockSocket.emit("close")).toThrow("process.exit(0)"); + expect(mockNetConnect).toHaveBeenCalledWith("\\\\.\\pipe\\ao-pty-hash-app-1"); + // Remove stdin listeners to prevent cross-test contamination + process.stdin.removeAllListeners("data"); + mockIsWindows.mockReturnValue(false); + }); + + it("handles PTY exit status on Windows", async () => { + mockIsWindows.mockReturnValue(true); + mockSessionManager.get.mockResolvedValue({ + id: "app-1", + projectId: "my-app", + status: "working", + activity: null, + branch: null, + issueId: null, + pr: null, + workspacePath: null, + runtimeHandle: { id: "hash-app-1", runtimeName: "process", data: {} }, + agentInfo: null, + createdAt: new Date(), + lastActivityAt: new Date(), + metadata: {}, + } satisfies Session); + + const mockSocket = new EventEmitter(); + Object.assign(mockSocket, { destroy: vi.fn(), write: vi.fn() }); + mockNetConnect.mockReturnValue(mockSocket); + + void program.parseAsync(["node", "test", "session", "attach", "app-1"]); + + await new Promise((r) => setTimeout(r, 10)); + mockSocket.emit("connect"); + await new Promise((r) => setTimeout(r, 10)); + + // Exercise PTY exit status (MSG_STATUS_RES = 0x07, alive=false) + // process.exit is inside try/catch in the data handler, so the mock throw + // gets swallowed. Verify via side effects instead. + const statusPayload = Buffer.from(JSON.stringify({ alive: false, exitCode: 42 })); + const statusFrame = Buffer.alloc(5 + statusPayload.length); + statusFrame.writeUInt8(0x07, 0); + statusFrame.writeUInt32BE(statusPayload.length, 1); + statusPayload.copy(statusFrame, 5); + mockSocket.emit("data", statusFrame); + + // cleanup() was called (socket destroyed) + expect((mockSocket as { destroy: ReturnType }).destroy).toHaveBeenCalled(); + // process.exit was called with the exit code from the status message + expect(process.exit).toHaveBeenCalledWith(42); + + mockIsWindows.mockReturnValue(false); + }); + + it("detaches on Ctrl+backslash on Windows", async () => { + mockIsWindows.mockReturnValue(true); + mockSessionManager.get.mockResolvedValue({ + id: "app-1", + projectId: "my-app", + status: "working", + activity: null, + branch: null, + issueId: null, + pr: null, + workspacePath: null, + runtimeHandle: { id: "hash-app-1", runtimeName: "process", data: {} }, + agentInfo: null, + createdAt: new Date(), + lastActivityAt: new Date(), + metadata: {}, + } satisfies Session); + + const mockSocket = new EventEmitter(); + Object.assign(mockSocket, { destroy: vi.fn(), write: vi.fn() }); + mockNetConnect.mockReturnValue(mockSocket); + + // Temporarily replace process.exit with a non-throwing spy so it doesn't + // propagate through EventEmitter and prevent subsequent listener calls. + // The global beforeEach spy throws, which breaks emit() propagation for + // listeners registered on process.stdin (a shared singleton). + const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => undefined as never); + + void program.parseAsync(["node", "test", "session", "attach", "app-1"]); + + await new Promise((r) => setTimeout(r, 10)); + mockSocket.emit("connect"); + await new Promise((r) => setTimeout(r, 10)); + + // Ctrl+\ (0x1c) triggers detach + process.stdin.emit("data", Buffer.from([0x1c])); + + expect((mockSocket as { destroy: ReturnType }).destroy).toHaveBeenCalled(); + expect(exitSpy).toHaveBeenCalledWith(0); + exitSpy.mockRestore(); + + // Remove the stdin listener we attached to prevent cross-test contamination + process.stdin.removeAllListeners("data"); + mockIsWindows.mockReturnValue(false); + }); + + it("falls back to config hash when runtimeHandle is missing on Windows", async () => { + mockIsWindows.mockReturnValue(true); + mockSessionManager.get.mockResolvedValue(null); + + const mockSocket = new EventEmitter(); + Object.assign(mockSocket, { destroy: vi.fn() }); + mockNetConnect.mockReturnValue(mockSocket); + + void program.parseAsync(["node", "test", "session", "attach", "app-1"]); + + await new Promise((r) => setTimeout(r, 10)); + // Should use config hash fallback for pipe path + expect(mockNetConnect).toHaveBeenCalled(); + const pipePath = mockNetConnect.mock.calls[0][0] as string; + expect(pipePath).toMatch(/\\\\\.\\pipe\\ao-pty-/); + + // Clean up: trigger error to exit + expect(() => mockSocket.emit("error", new Error("ENOENT"))).toThrow("process.exit(1)"); + mockIsWindows.mockReturnValue(false); + }); + + it("shows error when pipe not available on Windows", async () => { + mockIsWindows.mockReturnValue(true); + mockSessionManager.get.mockResolvedValue({ + id: "app-1", + projectId: "my-app", + status: "working", + activity: null, + branch: null, + issueId: null, + pr: null, + workspacePath: null, + runtimeHandle: { id: "hash-app-1", runtimeName: "process", data: { pipePath: "\\\\.\\pipe\\ao-pty-hash-app-1" } }, + agentInfo: null, + createdAt: new Date(), + lastActivityAt: new Date(), + metadata: {}, + } satisfies Session); + + const mockSocket = new EventEmitter(); + Object.assign(mockSocket, { destroy: vi.fn() }); + mockNetConnect.mockReturnValue(mockSocket); + + // Fire the command — it awaits an infinite promise, so don't await it. + void program.parseAsync(["node", "test", "session", "attach", "app-1"]); + + await new Promise((r) => setTimeout(r, 10)); + // error handler calls process.exit(1) which throws synchronously through emit + expect(() => mockSocket.emit("error", new Error("connect ENOENT"))).toThrow("process.exit(1)"); + mockIsWindows.mockReturnValue(false); + }); }); describe("session claim-pr", () => { diff --git a/packages/cli/__tests__/commands/spawn.test.ts b/packages/cli/__tests__/commands/spawn.test.ts index c5ed05393..60ffeb6c7 100644 --- a/packages/cli/__tests__/commands/spawn.test.ts +++ b/packages/cli/__tests__/commands/spawn.test.ts @@ -52,8 +52,18 @@ vi.mock("@aoagents/ao-core", async (importOriginal) => { }; }); +// Default registry returns no plugins → preflight loop is a no-op. Tests that +// need a specific plugin's preflight to fire override mockRegistryGet. +const mockRegistryGet = vi.fn().mockReturnValue(null); vi.mock("../../src/lib/create-session-manager.js", () => ({ getSessionManager: async (): Promise => mockSessionManager as SessionManager, + getPluginRegistry: async () => ({ + register: vi.fn(), + get: mockRegistryGet, + list: vi.fn().mockReturnValue([]), + loadBuiltins: vi.fn(), + loadFromConfig: vi.fn(), + }), })); vi.mock("../../src/lib/running-state.js", () => ({ @@ -124,6 +134,7 @@ beforeEach(() => { mockSessionManager.claimPR.mockReset(); mockExec.mockReset(); mockGetRunning.mockReset(); + mockRegistryGet.mockReset().mockReturnValue(null); mockGetRunning.mockResolvedValue({ pid: 1234, port: 3000, startedAt: "", projects: ["my-app"] }); }); @@ -219,6 +230,13 @@ describe("spawn command", () => { mkdirSync(backendSubdir, { recursive: true }); cwdSpy = vi.spyOn(process, "cwd").mockReturnValue(backendSubdir); + mockGetRunning.mockResolvedValue({ + pid: 1234, + port: 3000, + startedAt: "", + projects: ["backend", "frontend"], + }); + const fakeSession: Session = { id: "be-1", projectId: "backend", @@ -273,7 +291,7 @@ describe("spawn command", () => { pid: 1234, port: 3000, startedAt: "", - projects: ["agent-orchestrator"], + projects: ["agent-orchestrator", "x402-identity"], }); const fakeSession: Session = { @@ -325,7 +343,7 @@ describe("spawn command", () => { pid: 1234, port: 3000, startedAt: "", - projects: ["agent-orchestrator"], + projects: ["agent-orchestrator", "x402-identity"], }); const fakeSession: Session = { @@ -673,23 +691,14 @@ describe("spawn command", () => { }); describe("spawn pre-flight checks", () => { - it("fails with clear error when tmux is not installed (default runtime)", async () => { - mockExec.mockRejectedValue(new Error("ENOENT")); + // The spawn CLI now iterates the configured plugins and calls each one's + // optional preflight(). Plugin-internal checks (e.g. checkTmux, gh auth + // status) live in the plugin packages — see runtime-tmux / tracker-github / + // scm-github tests for that coverage. These tests verify the orchestration: + // the right plugins are iterated, and the intent context is forwarded. - await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow( - "process.exit(1)", - ); - - const errors = vi - .mocked(console.error) - .mock.calls.map((c) => String(c[0])) - .join("\n"); - expect(errors).toContain("tmux"); - expect(mockSessionManager.spawn).not.toHaveBeenCalled(); - }); - - it("skips tmux check when runtime is not tmux", async () => { - const fakeSession: Session = { + function makeFakeSession(overrides: Partial = {}): Session { + return { id: "app-1", projectId: "my-app", status: "spawning", @@ -698,98 +707,77 @@ describe("spawn pre-flight checks", () => { issueId: null, pr: null, workspacePath: "/tmp/wt", - runtimeHandle: { id: "proc-1", runtimeName: "process", data: {} }, + runtimeHandle: { id: "hash-1", runtimeName: "tmux", data: {} }, agentInfo: null, createdAt: new Date(), lastActivityAt: new Date(), metadata: {}, + ...overrides, }; - mockSessionManager.spawn.mockResolvedValue(fakeSession); + } - // Set runtime to "process" - (mockConfigRef.current as Record).defaults = { - runtime: "process", - agent: "claude-code", - workspace: "worktree", - notifiers: ["desktop"], - }; + it("surfaces a plugin's preflight error and aborts before sm.spawn", async () => { + mockRegistryGet.mockImplementation((slot: string) => { + if (slot === "runtime") { + return { + name: "tmux", + preflight: vi + .fn() + .mockRejectedValue(new Error("tmux is not installed. Install it: brew install tmux")), + }; + } + return null; + }); - // exec would fail for tmux but should never be called - mockExec.mockRejectedValue(new Error("ENOENT")); + await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow("process.exit(1)"); - await program.parseAsync(["node", "test", "spawn"]); - - expect(mockSessionManager.spawn).toHaveBeenCalled(); + const errors = vi + .mocked(console.error) + .mock.calls.map((c) => String(c[0])) + .join("\n"); + expect(errors).toContain("tmux is not installed"); + expect(mockSessionManager.spawn).not.toHaveBeenCalled(); }); - it("checks gh auth when tracker is github", async () => { + it("skips scm.preflight when --claim-pr is not provided", async () => { + const trackerPreflight = vi.fn().mockResolvedValue(undefined); + const scmPreflight = vi.fn().mockResolvedValue(undefined); + mockRegistryGet.mockImplementation((slot: string) => { + if (slot === "tracker") return { name: "github", preflight: trackerPreflight }; + if (slot === "scm") return { name: "github", preflight: scmPreflight }; + return null; + }); + const projects = (mockConfigRef.current as Record).projects as Record< string, Record >; projects["my-app"].tracker = { plugin: "github" }; + projects["my-app"].scm = { plugin: "github" }; - // tmux check passes, gh --version passes, gh auth status fails - mockExec - .mockResolvedValueOnce({ stdout: "tmux 3.3a", stderr: "" }) // tmux -V - .mockResolvedValueOnce({ stdout: "gh version 2.40", stderr: "" }) // gh --version - .mockRejectedValueOnce(new Error("not logged in")); // gh auth status + mockSessionManager.spawn.mockResolvedValue(makeFakeSession()); - await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow( - "process.exit(1)", - ); + await program.parseAsync(["node", "test", "spawn"]); - const errors = vi - .mocked(console.error) - .mock.calls.map((c) => String(c[0])) - .join("\n"); - expect(errors).toContain("not authenticated"); - expect(mockSessionManager.spawn).not.toHaveBeenCalled(); + expect(trackerPreflight).toHaveBeenCalled(); + expect(scmPreflight).not.toHaveBeenCalled(); + expect(mockSessionManager.spawn).toHaveBeenCalled(); }); - it("checks gh auth when --claim-pr targets a github SCM project", async () => { + it("calls scm.preflight with willClaimExistingPR=true when --claim-pr is provided", async () => { + const scmPreflight = vi.fn().mockResolvedValue(undefined); + mockRegistryGet.mockImplementation((slot: string) => { + if (slot === "scm") return { name: "github", preflight: scmPreflight }; + return null; + }); + const projects = (mockConfigRef.current as Record).projects as Record< string, Record >; - projects["my-app"].tracker = { plugin: "linear" }; projects["my-app"].scm = { plugin: "github" }; - mockExec - .mockResolvedValueOnce({ stdout: "tmux 3.3a", stderr: "" }) - .mockResolvedValueOnce({ stdout: "gh version 2.40", stderr: "" }) - .mockRejectedValueOnce(new Error("not logged in")); - - await expect( - program.parseAsync(["node", "test", "spawn", "--claim-pr", "123"]), - ).rejects.toThrow("process.exit(1)"); - - const errors = vi - .mocked(console.error) - .mock.calls.map((c) => String(c[0])) - .join("\n"); - expect(errors).toContain("not authenticated"); - expect(mockSessionManager.spawn).not.toHaveBeenCalled(); - }); - - it("handles tracker+scm github preflight when claiming during spawn", async () => { - const fakeSession: Session = { - id: "app-1", - projectId: "my-app", - status: "spawning", - activity: null, - branch: null, - issueId: null, - pr: null, - workspacePath: "/tmp/wt", - runtimeHandle: { id: "hash-app-1", runtimeName: "tmux", data: {} }, - agentInfo: null, - createdAt: new Date(), - lastActivityAt: new Date(), - metadata: {}, - }; - - mockSessionManager.spawn.mockResolvedValue(fakeSession); + mockSessionManager.spawn.mockResolvedValue(makeFakeSession()); mockSessionManager.claimPR.mockResolvedValue({ sessionId: "app-1", projectId: "my-app", @@ -808,86 +796,59 @@ describe("spawn pre-flight checks", () => { takenOverFrom: [], }); - const projects = (mockConfigRef.current as Record).projects as Record< - string, - Record - >; - projects["my-app"].tracker = { plugin: "github" }; - projects["my-app"].scm = { plugin: "github" }; - - mockExec - .mockResolvedValueOnce({ stdout: "tmux 3.3a", stderr: "" }) - .mockResolvedValueOnce({ stdout: "gh version 2.40", stderr: "" }) - .mockResolvedValueOnce({ stdout: "Logged in", stderr: "" }); - await program.parseAsync(["node", "test", "spawn", "--claim-pr", "123"]); - expect(mockExec).toHaveBeenCalledWith("tmux", ["-V"]); - const ghCalls = mockExec.mock.calls.filter(([command]) => command === "gh"); - expect(ghCalls).toHaveLength(2); - expect(mockSessionManager.spawn).toHaveBeenCalled(); - expect(mockSessionManager.claimPR).toHaveBeenCalledWith("app-1", "123", { - assignOnGithub: undefined, - }); + expect(scmPreflight).toHaveBeenCalledTimes(1); + const ctx = scmPreflight.mock.calls[0]?.[0] as { intent: { willClaimExistingPR: boolean } }; + expect(ctx.intent.willClaimExistingPR).toBe(true); }); - it("skips gh auth check when tracker is not github", async () => { - const fakeSession: Session = { - id: "app-1", - projectId: "my-app", - status: "spawning", - activity: null, - branch: null, - issueId: null, - pr: null, - workspacePath: "/tmp/wt", - runtimeHandle: { id: "hash-1", runtimeName: "tmux", data: {} }, - agentInfo: null, - createdAt: new Date(), - lastActivityAt: new Date(), - metadata: {}, - }; - mockSessionManager.spawn.mockResolvedValue(fakeSession); + it("does not iterate the tracker slot when no tracker is configured", async () => { + const trackerPreflight = vi.fn().mockResolvedValue(undefined); + mockRegistryGet.mockImplementation((slot: string) => { + if (slot === "tracker") return { name: "github", preflight: trackerPreflight }; + return null; + }); - const projects = (mockConfigRef.current as Record).projects as Record< - string, - Record - >; - projects["my-app"].tracker = { plugin: "linear" }; - - // tmux check passes — gh should never be called - mockExec.mockResolvedValue({ stdout: "tmux 3.3a", stderr: "" }); + // Project intentionally has no tracker configured. + mockSessionManager.spawn.mockResolvedValue(makeFakeSession()); await program.parseAsync(["node", "test", "spawn"]); - // Should only call tmux -V, not gh - expect(mockExec).toHaveBeenCalledWith("tmux", ["-V"]); - expect(mockExec).not.toHaveBeenCalledWith("gh", expect.anything()); - expect(mockSessionManager.spawn).toHaveBeenCalled(); + expect(trackerPreflight).not.toHaveBeenCalled(); }); - it("distinguishes gh not installed from gh not authenticated", async () => { + it("collects every plugin's preflight failure into one combined error", async () => { + const runtimePreflight = vi.fn().mockRejectedValue(new Error("tmux is not installed")); + const trackerPreflight = vi + .fn() + .mockRejectedValue(new Error("GitHub CLI is not authenticated. Run: gh auth login")); + mockRegistryGet.mockImplementation((slot: string) => { + if (slot === "runtime") return { name: "tmux", preflight: runtimePreflight }; + if (slot === "tracker") return { name: "github", preflight: trackerPreflight }; + return null; + }); + const projects = (mockConfigRef.current as Record).projects as Record< string, Record >; projects["my-app"].tracker = { plugin: "github" }; - // tmux passes, gh --version fails (not installed) - mockExec - .mockResolvedValueOnce({ stdout: "tmux 3.3a", stderr: "" }) // tmux -V - .mockRejectedValueOnce(new Error("ENOENT")); // gh --version fails + await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow("process.exit(1)"); - await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow( - "process.exit(1)", - ); + // Both preflights ran (collect-all, not fail-fast). + expect(runtimePreflight).toHaveBeenCalled(); + expect(trackerPreflight).toHaveBeenCalled(); const errors = vi .mocked(console.error) .mock.calls.map((c) => String(c[0])) .join("\n"); - expect(errors).toContain("not installed"); - expect(errors).not.toContain("not authenticated"); + expect(errors).toContain("2 preflight checks failed"); + expect(errors).toContain("tmux is not installed"); + expect(errors).toContain("gh auth login"); + expect(mockSessionManager.spawn).not.toHaveBeenCalled(); }); }); @@ -993,6 +954,12 @@ describe("batch-spawn command", () => { }; mkdirSync(join(tmpDir, "agent-orchestrator"), { recursive: true }); mkdirSync(join(tmpDir, "x402-identity"), { recursive: true }); + mockGetRunning.mockResolvedValue({ + pid: 1234, + port: 3000, + startedAt: "", + projects: ["agent-orchestrator", "x402-identity"], + }); // Pre-existing active session in x402-identity for issue 20 mockSessionManager.list.mockImplementation(async (pid: string) => { @@ -1029,3 +996,91 @@ describe("batch-spawn command", () => { }); }); }); + +describe("spawn daemon-polling enforcement", () => { + it("refuses to spawn when no AO daemon is running", async () => { + mockGetRunning.mockResolvedValue(null); + + await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow( + "process.exit(1)", + ); + + const errors = vi + .mocked(console.error) + .mock.calls.map((c) => String(c[0])) + .join("\n"); + expect(errors).toContain("AO is not running"); + expect(errors).toContain("ao start"); + expect(mockSessionManager.spawn).not.toHaveBeenCalled(); + }); + + it("refuses to spawn when the running daemon is not polling the project", async () => { + mockGetRunning.mockResolvedValue({ + pid: 99999, + port: 3000, + startedAt: "", + projects: ["other-project"], + }); + + await expect(program.parseAsync(["node", "test", "spawn"])).rejects.toThrow( + "process.exit(1)", + ); + + const errors = vi + .mocked(console.error) + .mock.calls.map((c) => String(c[0])) + .join("\n"); + expect(errors).toContain("not polling project"); + expect(errors).toContain("my-app"); + expect(errors).toContain("ao start my-app"); + expect(mockSessionManager.spawn).not.toHaveBeenCalled(); + }); +}); + +describe("batch-spawn daemon-polling enforcement", () => { + let batchProgram: Command; + + beforeEach(() => { + batchProgram = new Command(); + batchProgram.exitOverride(); + registerBatchSpawn(batchProgram); + }); + + it("refuses to batch-spawn when no AO daemon is running", async () => { + mockGetRunning.mockResolvedValue(null); + + await expect( + batchProgram.parseAsync(["node", "test", "batch-spawn", "INT-1", "INT-2"]), + ).rejects.toThrow("process.exit(1)"); + + const errors = vi + .mocked(console.error) + .mock.calls.map((c) => String(c[0])) + .join("\n"); + expect(errors).toContain("AO is not running"); + expect(errors).toContain("ao start"); + expect(mockSessionManager.spawn).not.toHaveBeenCalled(); + }); + + it("refuses to batch-spawn when the running daemon is not polling the project", async () => { + mockGetRunning.mockResolvedValue({ + pid: 99999, + port: 3000, + startedAt: "", + projects: ["other-project"], + }); + + await expect( + batchProgram.parseAsync(["node", "test", "batch-spawn", "INT-1", "INT-2"]), + ).rejects.toThrow("process.exit(1)"); + + const errors = vi + .mocked(console.error) + .mock.calls.map((c) => String(c[0])) + .join("\n"); + expect(errors).toContain("not polling project"); + expect(errors).toContain("my-app"); + expect(errors).toContain("ao start my-app"); + expect(mockSessionManager.spawn).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/cli/__tests__/commands/start.test.ts b/packages/cli/__tests__/commands/start.test.ts index 6eefaee4d..06041c2f3 100644 --- a/packages/cli/__tests__/commands/start.test.ts +++ b/packages/cli/__tests__/commands/start.test.ts @@ -32,6 +32,8 @@ const { mockSessionManager, mockWaitForPortAndOpen, mockSpawn, + mockFindPidByPort, + mockKillProcessTree, mockStartProjectSupervisor, } = vi.hoisted(() => ({ mockExec: vi.fn(), @@ -52,6 +54,8 @@ const { }, mockWaitForPortAndOpen: vi.fn().mockResolvedValue(undefined), mockSpawn: vi.fn(), + mockFindPidByPort: vi.fn(), + mockKillProcessTree: vi.fn(), mockStartProjectSupervisor: vi.fn(), })); @@ -138,6 +142,8 @@ vi.mock("@aoagents/ao-core", async (importOriginal) => { if (path) return actual.loadConfig(path); return mockConfigRef.current; }, + findPidByPort: mockFindPidByPort, + killProcessTree: mockKillProcessTree, }; }); @@ -175,7 +181,6 @@ vi.mock("../../src/lib/preflight.js", () => ({ preflight: { checkPort: vi.fn(), checkBuilt: vi.fn(), - checkTmux: vi.fn().mockResolvedValue(undefined), }, })); @@ -261,7 +266,7 @@ vi.mock("node:process", async (importOriginal) => { // --------------------------------------------------------------------------- import { Command } from "commander"; -import { registerStart, registerStop, createConfigOnly } from "../../src/commands/start.js"; +import { registerStart, registerStop, autoCreateConfig } from "../../src/commands/start.js"; let tmpDir: string; let program: Command; @@ -328,7 +333,11 @@ beforeEach(async () => { vi.mocked(webDir.findFreePort).mockResolvedValue(3000); vi.mocked(webDir.buildDashboardEnv).mockResolvedValue({}); const projectDetection = await import("../../src/lib/project-detection.js"); - vi.mocked(projectDetection.detectProjectType).mockReturnValue({ languages: [], frameworks: [], tools: [] }); + vi.mocked(projectDetection.detectProjectType).mockReturnValue({ + languages: [], + frameworks: [], + tools: [], + }); vi.mocked(projectDetection.generateRulesFromTemplates).mockReturnValue(null); vi.mocked(projectDetection.formatProjectTypeForDisplay).mockReturnValue(""); @@ -374,6 +383,10 @@ beforeEach(async () => { }); mockWaitForPortAndOpen.mockReset(); mockWaitForPortAndOpen.mockResolvedValue(undefined); + mockFindPidByPort.mockReset(); + mockFindPidByPort.mockResolvedValue(null); + mockKillProcessTree.mockReset(); + mockKillProcessTree.mockResolvedValue(undefined); mockStartProjectSupervisor.mockReset(); mockStartProjectSupervisor.mockResolvedValue({ stop: vi.fn(), reconcileNow: vi.fn() }); mockDetectOpenClawInstallation.mockReset(); @@ -425,7 +438,10 @@ function makeConfig(projects: Record>): Record { mockExecSilent.mockResolvedValue("Logged in"); mockSpawn.mockImplementation( - ( - cmd: string, - args: string[], - _opts?: { cwd?: string; env?: NodeJS.ProcessEnv }, - ) => { - if (cmd === "gh" && args[0] === "repo" && args[1] === "clone") { - createFakeRepo(repoDir, "https://github.com/owner/my-app.git", { - "Cargo.toml": "", - }); - } - return createSpawnChild({ closeCode: 0 }); + (cmd: string, args: string[], _opts?: { cwd?: string; env?: NodeJS.ProcessEnv }) => { + if (cmd === "gh" && args[0] === "repo" && args[1] === "clone") { + createFakeRepo(repoDir, "https://github.com/owner/my-app.git", { + "Cargo.toml": "", + }); + } + return createSpawnChild({ closeCode: 0 }); }, ); @@ -696,25 +708,21 @@ describe("start command — URL argument", () => { }); mockSpawn.mockImplementation( - ( - cmd: string, - args: string[], - _opts?: { cwd?: string; env?: NodeJS.ProcessEnv }, - ) => { - if (cmd === "git" && args[0] === "clone") { - const url = String(args[3] ?? ""); - // SSH attempt fails (simulate non-zero exit) - if (url.startsWith("git@")) { - return createSpawnChild({ closeCode: 1 }); + (cmd: string, args: string[], _opts?: { cwd?: string; env?: NodeJS.ProcessEnv }) => { + if (cmd === "git" && args[0] === "clone") { + const url = String(args[3] ?? ""); + // SSH attempt fails (simulate non-zero exit) + if (url.startsWith("git@")) { + return createSpawnChild({ closeCode: 1 }); + } + + // HTTPS fallback succeeds + createFakeRepo(repoDir, "https://github.com/owner/my-app.git", { + "Cargo.toml": "", + }); } - // HTTPS fallback succeeds - createFakeRepo(repoDir, "https://github.com/owner/my-app.git", { - "Cargo.toml": "", - }); - } - - return createSpawnChild({ closeCode: 0 }); + return createSpawnChild({ closeCode: 0 }); }, ); @@ -756,7 +764,7 @@ describe("start command — URL argument", () => { [ "port: 4000", "defaults:", - " runtime: tmux", + " runtime: process", " agent: claude-code", " workspace: worktree", " notifiers: [desktop]", @@ -797,7 +805,7 @@ describe("start command — URL argument", () => { [ "port: 4000", "defaults:", - " runtime: tmux", + " runtime: process", " agent: claude-code", " workspace: worktree", " notifiers: [desktop]", @@ -873,7 +881,20 @@ describe("start command — non-interactive install safety", () => { it("does not auto-install tmux when missing in non-interactive mode", async () => { mockIsHumanCaller.mockReturnValue(false); - mockConfigRef.current = makeConfig({ "my-app": makeProject() }); + // This test exercises the tmux preflight path, so the config must + // explicitly select runtime: tmux (makeConfig defaults to process). + // Pin the platform to linux so the Windows branch (which exits before + // calling execSilent) doesn't short-circuit the tmux -V check we're + // asserting on. + const tmuxConfig = makeConfig({ "my-app": makeProject() }) as { + defaults: Record; + }; + tmuxConfig.defaults.runtime = "tmux"; + mockConfigRef.current = tmuxConfig; + + const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); + Object.defineProperty(process, "platform", { value: "linux", configurable: true }); + mockExecSilent.mockImplementation(async (cmd: string, args: string[] = []) => { if (cmd === "git" && args[0] === "--version") return "git version 2.43.0"; if (cmd === "tmux" && args[0] === "-V") return null; @@ -882,9 +903,15 @@ describe("start command — non-interactive install safety", () => { return null; }); - await expect( - program.parseAsync(["node", "test", "start", "--no-dashboard", "--no-orchestrator"]), - ).rejects.toThrow("process.exit(1)"); + try { + await expect( + program.parseAsync(["node", "test", "start", "--no-dashboard", "--no-orchestrator"]), + ).rejects.toThrow("process.exit(1)"); + } finally { + if (originalPlatform) { + Object.defineProperty(process, "platform", originalPlatform); + } + } expect(hasPrivilegedInstallAttempt()).toBe(false); expect(mockExec.mock.calls.some((call) => String(call[0]) === "tmux")).toBe(false); @@ -1229,10 +1256,10 @@ describe("start command — orchestrator session strategy display", () => { await program.parseAsync(["node", "test", "start", "--rebuild", "--no-orchestrator"]); - expect(dashboardRebuild.rebuildDashboardProductionArtifacts).toHaveBeenCalledWith(tmpDir, [ - 3000, - 3001, - ]); + expect(dashboardRebuild.rebuildDashboardProductionArtifacts).toHaveBeenCalledWith( + tmpDir, + [3000, 3001], + ); }); it("opens the most recent orchestrator session page when multiple existing orchestrators found with dashboard enabled and reuse is explicit", async () => { @@ -1744,87 +1771,172 @@ describe("stop command", () => { }); }); - it("finds orphaned dashboard on a reassigned port via port scan", async () => { + it("calls killProcessTree with numeric PID when findPidByPort returns a PID", async () => { mockConfigRef.current = makeConfig({ "my-app": makeProject() }); - mockSessionManager.get.mockResolvedValue({ id: "app-orchestrator", status: "running" }); - mockSessionManager.kill.mockResolvedValue({ cleaned: true, alreadyTerminated: false }); - // Port 3000 has nothing, but port 3001 has the orphaned dashboard - mockDashboardOnPort(3001, "99999"); - - await program.parseAsync(["node", "test", "stop"]); - - const output = vi - .mocked(console.log) - .mock.calls.map((c) => c.join(" ")) - .join("\n"); - expect(output).toContain("was on port 3001"); - }); - - it("skips non-dashboard processes during port scan", async () => { - mockConfigRef.current = makeConfig({ "my-app": makeProject() }); - mockSessionManager.get.mockResolvedValue({ id: "app-orchestrator", status: "running" }); - mockSessionManager.kill.mockResolvedValue({ cleaned: true, alreadyTerminated: false }); - // Port 3000 has nothing, port 3001 has an unrelated process, - // port 3002 has the actual dashboard - mockExec.mockImplementation(async (cmd: string, args: string[] = []) => { - if (cmd === "kill") return { stdout: "", stderr: "" }; - if (cmd === "ps") { - const pid = args[1]; - if (pid === "11111") return { stdout: "python -m http.server 3001", stderr: "" }; - if (pid === "22222") - return { stdout: "node /fake/web/dist-server/start-all.js", stderr: "" }; - return { stdout: "", stderr: "" }; - } - if (cmd === "lsof") { - const portArg = args.find((a) => a.startsWith(":")); - if (portArg === ":3001") return { stdout: "11111", stderr: "" }; - if (portArg === ":3002") return { stdout: "22222", stderr: "" }; - } + mockSessionManager.list.mockResolvedValue([]); + mockFindPidByPort.mockResolvedValue("1234"); + // killDashboardOnPort verifies the PID is an AO dashboard via `ps` on Unix + // before killing. Stub it to return a matching cmdline so we reach the kill. + mockExec.mockImplementation(async (cmd: string) => { + if (cmd === "ps") return { stdout: "node /fake/web/dist-server/start-all.js", stderr: "" }; throw new Error("no process"); }); await program.parseAsync(["node", "test", "stop"]); - const output = vi - .mocked(console.log) - .mock.calls.map((c) => c.join(" ")) - .join("\n"); - // Should skip port 3001 (python) and find the dashboard on 3002 - expect(output).toContain("was on port 3002"); + expect(mockFindPidByPort).toHaveBeenCalledWith(3000); + expect(mockKillProcessTree).toHaveBeenCalledWith(1234); }); - it("only kills dashboard PIDs when port has mixed processes", async () => { + it("does not call killProcessTree when findPidByPort returns null", async () => { mockConfigRef.current = makeConfig({ "my-app": makeProject() }); - mockSessionManager.get.mockResolvedValue({ id: "app-orchestrator", status: "running" }); - mockSessionManager.kill.mockResolvedValue({ cleaned: true, alreadyTerminated: false }); - // Port 3000 has two processes: a dashboard and an unrelated sidecar - mockExec.mockImplementation(async (cmd: string, args: string[] = []) => { - if (cmd === "kill") { - // Only the dashboard PID should be killed, not the sidecar - expect(args).toEqual(["11111"]); - return { stdout: "", stderr: "" }; - } - if (cmd === "ps") { - const pid = args[1]; - if (pid === "11111") - return { stdout: "node /fake/web/dist-server/start-all.js", stderr: "" }; - if (pid === "22222") return { stdout: "nginx: worker process", stderr: "" }; - return { stdout: "", stderr: "" }; - } - if (cmd === "lsof") { - const portArg = args.find((a) => a.startsWith(":")); - if (portArg === ":3000") return { stdout: "11111\n22222", stderr: "" }; - } - throw new Error("no process"); - }); + mockSessionManager.list.mockResolvedValue([]); + mockFindPidByPort.mockResolvedValue(null); await program.parseAsync(["node", "test", "stop"]); - const output = vi - .mocked(console.log) - .mock.calls.map((c) => c.join(" ")) - .join("\n"); - expect(output).toContain("Dashboard stopped"); + expect(mockFindPidByPort).toHaveBeenCalledWith(3000); + expect(mockKillProcessTree).not.toHaveBeenCalled(); + }); + + // Recovers from issue #645: when the configured port was busy at start, the + // dashboard auto-reassigned to port+N and `ao stop` couldn't find it. The + // port-scan fallback in stopDashboard walks port+1..port+MAX_PORT_SCAN. + // Skip on Windows: killDashboardOnPort skips the `ps` cmdline verification + // there (uses netstat trust), so the assertions on `ps` output don't apply. + it.skipIf(process.platform === "win32")( + "finds orphaned dashboard on a reassigned port via port scan", + async () => { + mockConfigRef.current = makeConfig({ "my-app": makeProject() }); + mockSessionManager.list.mockResolvedValue([]); + // Port 3000 has nothing; port 3001 has the orphaned dashboard + mockFindPidByPort.mockImplementation(async (port: number) => + port === 3001 ? "99999" : null, + ); + // ps cmdline check inside killDashboardOnPort must pass for the kill to fire + mockExec.mockImplementation(async (cmd: string) => { + if (cmd === "ps") return { stdout: "node /fake/web/dist-server/start-all.js", stderr: "" }; + throw new Error("no process"); + }); + + await program.parseAsync(["node", "test", "stop"]); + + expect(mockKillProcessTree).toHaveBeenCalledWith(99999); + const output = vi + .mocked(console.log) + .mock.calls.map((c) => c.join(" ")) + .join("\n"); + expect(output).toContain("was on port 3001"); + }, + ); + + // Windows parallel: the port-scan fallback must still find the orphaned + // dashboard, but killDashboardOnPort intentionally skips the `ps` cmdline + // check (no `ps` on Windows; we trust netstat output via findPidByPort). + // Ensures a developer who breaks the Windows port-scan path is caught. + it.runIf(process.platform === "win32")( + "finds orphaned dashboard on a reassigned port via port scan (Windows)", + async () => { + mockConfigRef.current = makeConfig({ "my-app": makeProject() }); + mockSessionManager.list.mockResolvedValue([]); + mockFindPidByPort.mockImplementation(async (port: number) => + port === 3001 ? "99999" : null, + ); + + await program.parseAsync(["node", "test", "stop"]); + + expect(mockKillProcessTree).toHaveBeenCalledWith(99999); + // `ps` must NOT be invoked on Windows — the cmdline verification is + // skipped by design in killDashboardOnPort. + const psCalls = mockExec.mock.calls.filter((c) => c[0] === "ps"); + expect(psCalls).toHaveLength(0); + const output = vi + .mocked(console.log) + .mock.calls.map((c) => c.join(" ")) + .join("\n"); + expect(output).toContain("was on port 3001"); + }, + ); +}); + +// --------------------------------------------------------------------------- +// runtime fallback — platform-aware default (B01/B02/B21) +// --------------------------------------------------------------------------- + +describe("start command — platform-aware runtime fallback", () => { + it("does not call ensureTmux when config has no runtime and platform is win32", async () => { + // Config with no defaults.runtime — the fallback kicks in. + const configWithoutRuntime: Record = { + configPath: join(tmpDir, "agent-orchestrator.yaml"), + port: 3000, + defaults: { + // runtime intentionally absent + agent: "claude-code", + workspace: "worktree", + notifiers: [], + }, + projects: { "my-app": makeProject() }, + notifiers: {}, + notificationRouting: {}, + reactions: {}, + }; + mockConfigRef.current = configWithoutRuntime; + + // Simulate Windows — getDefaultRuntime() will return "process". + const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); + Object.defineProperty(process, "platform", { value: "win32", configurable: true }); + + try { + await program.parseAsync(["node", "test", "start", "--no-dashboard", "--no-orchestrator"]); + } finally { + if (originalPlatform) { + Object.defineProperty(process, "platform", originalPlatform); + } + } + + // ensureTmux() calls execSilent("tmux", ["-V"]) — it must NOT have been called. + const tmuxChecks = mockExecSilent.mock.calls.filter( + (call) => + String(call[0]) === "tmux" && Array.isArray(call[1]) && (call[1] as string[])[0] === "-V", + ); + expect(tmuxChecks).toHaveLength(0); + }); + + it("calls ensureTmux when config has no runtime and platform is linux", async () => { + // Same config without runtime, but on a non-Windows platform. + const configWithoutRuntime: Record = { + configPath: join(tmpDir, "agent-orchestrator.yaml"), + port: 3000, + defaults: { + agent: "claude-code", + workspace: "worktree", + notifiers: [], + }, + projects: { "my-app": makeProject() }, + notifiers: {}, + notificationRouting: {}, + reactions: {}, + }; + mockConfigRef.current = configWithoutRuntime; + + // Simulate Linux — getDefaultRuntime() returns "tmux", ensureTmux() must fire. + const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); + Object.defineProperty(process, "platform", { value: "linux", configurable: true }); + + try { + await program.parseAsync(["node", "test", "start", "--no-dashboard", "--no-orchestrator"]); + } finally { + if (originalPlatform) { + Object.defineProperty(process, "platform", originalPlatform); + } + } + + // ensureTmux() must have checked for tmux availability. + const tmuxChecks = mockExecSilent.mock.calls.filter( + (call) => + String(call[0]) === "tmux" && Array.isArray(call[1]) && (call[1] as string[])[0] === "-V", + ); + expect(tmuxChecks.length).toBeGreaterThan(0); }); it("targeted stop does NOT kill parent process or dashboard", async () => { @@ -1981,15 +2093,14 @@ describe("stop command", () => { mockSessionManager.list.mockResolvedValue([]); mockExec.mockRejectedValue(new Error("no process")); - const killSpy = vi.spyOn(process, "kill").mockImplementation(() => true); - await program.parseAsync(["node", "test", "stop"]); - expect(killSpy).toHaveBeenCalledWith(99999, "SIGTERM"); + // Stop now goes through killProcessTree (which is module-mocked above), + // not a direct process.kill — that's how it gets `taskkill /T /F` on + // Windows and process-group kill on Unix. Assert on the mock. + expect(mockKillProcessTree).toHaveBeenCalledWith(99999, "SIGTERM"); expect(mockUnregister).toHaveBeenCalled(); expect(mockRemoveProjectFromRunning).not.toHaveBeenCalled(); - - killSpy.mockRestore(); }); it("targeted stop records last-stop with correct project scope", async () => { @@ -2134,7 +2245,7 @@ describe("start command — autoCreateConfig", () => { const callerContext = await import("../../src/lib/caller-context.js"); vi.spyOn(callerContext, "isHumanCaller").mockReturnValue(false); - await createConfigOnly(); + await autoCreateConfig(tmpDir); const configPath = join(tmpDir, "agent-orchestrator.yaml"); expect(existsSync(configPath)).toBe(true); @@ -2268,7 +2379,7 @@ describe("start command — already-running detection", () => { globalConfigPath, yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { "my-app": { name: "My App", @@ -2313,8 +2424,7 @@ describe("start command — already-running detection", () => { ) { return "https://github.com/org/new-repo.git"; } - if (args[0] === "symbolic-ref" && workingDir === repoDir) - return "refs/remotes/origin/main"; + if (args[0] === "symbolic-ref" && workingDir === repoDir) return "refs/remotes/origin/main"; if (args[0] === "rev-parse" && args[1] === "--verify" && workingDir === repoDir) return "abc"; return null; @@ -2417,8 +2527,7 @@ describe("start command — already-running detection", () => { }); mockWaitForExit.mockResolvedValue(true); - - const killSpy = vi.spyOn(process, "kill").mockImplementation(() => true); + mockKillProcessTree.mockResolvedValue(undefined); mockPromptSelect.mockResolvedValue("restart"); @@ -2432,7 +2541,10 @@ describe("start command — already-running detection", () => { // Startup after restart may throw — that's OK for this test } - expect(killSpy).toHaveBeenCalledWith(9999, "SIGTERM"); + // killExistingDaemon delegates to killProcessTree (taskkill /T /F on Windows, + // process group signalling on Unix) instead of raw process.kill, so dead + // grandchildren of the daemon don't leak. + expect(mockKillProcessTree).toHaveBeenCalledWith(9999, "SIGTERM"); expect(mockUnregister).toHaveBeenCalled(); const output = vi @@ -2440,8 +2552,6 @@ describe("start command — already-running detection", () => { .mock.calls.map((c) => c.join(" ")) .join("\n"); expect(output).toContain("Stopped existing instance"); - - killSpy.mockRestore(); }); it("creates new orchestrator entry when human caller selects 'new'", async () => { @@ -2461,7 +2571,7 @@ describe("start command — already-running detection", () => { configPath, yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { "my-app": { name: "My App", @@ -2515,7 +2625,7 @@ describe("start command — already-running detection", () => { const { stringify: yamlStringify } = await import("yaml"); const originalYaml = yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { "my-app": { name: "My App", @@ -2568,7 +2678,7 @@ describe("start command — path-based deduplication in addProjectToConfig", () configPath, yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { "my-app": { name: "My App", @@ -2621,7 +2731,7 @@ describe("start command — path-based deduplication in addProjectToConfig", () configPath, yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { "old-name": { name: "Old Name", @@ -2681,7 +2791,7 @@ describe("start command — global registry mutations", () => { globalConfigPath, yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { current: { projectId: "current", @@ -2782,7 +2892,7 @@ describe("start command — global registry mutations", () => { globalConfigPath, yamlStringify( { - defaults: { runtime: "tmux", agent: "claude-code", workspace: "worktree", notifiers: [] }, + defaults: { runtime: "process", agent: "claude-code", workspace: "worktree", notifiers: [] }, projects: { current: { projectId: "current", diff --git a/packages/cli/__tests__/lib/bun-tmp-janitor.test.ts b/packages/cli/__tests__/lib/bun-tmp-janitor.test.ts index 66742f5db..d02406136 100644 --- a/packages/cli/__tests__/lib/bun-tmp-janitor.test.ts +++ b/packages/cli/__tests__/lib/bun-tmp-janitor.test.ts @@ -30,7 +30,10 @@ function setMtime(path: string, ageMs: number): void { utimesSync(path, t, t); } -describe("bun-tmp-janitor", () => { +// Skipped on Windows: startBunTmpJanitor() is a no-op on win32 (opencode ships +// no Windows binary, and the kernel disallows unlinking mapped files), so the +// behavioural tests below have no work to assert against. +describe.skipIf(process.platform === "win32")("bun-tmp-janitor", () => { beforeEach(() => { mockedDir = mkdtempSync(join(tmpdir(), "ao-bun-janitor-test-")); }); diff --git a/packages/cli/__tests__/lib/daemon.test.ts b/packages/cli/__tests__/lib/daemon.test.ts new file mode 100644 index 000000000..c50e1edd1 --- /dev/null +++ b/packages/cli/__tests__/lib/daemon.test.ts @@ -0,0 +1,131 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; +import type * as AoCore from "@aoagents/ao-core"; + +const { mockUnregister, mockWaitForExit, mockKillProcessTree } = vi.hoisted(() => ({ + mockUnregister: vi.fn(), + mockWaitForExit: vi.fn(), + mockKillProcessTree: vi.fn(), +})); + +vi.mock("../../src/lib/running-state.js", () => ({ + unregister: mockUnregister, + waitForExit: mockWaitForExit, +})); + +vi.mock("@aoagents/ao-core", async () => { + const actual = await vi.importActual("@aoagents/ao-core"); + return { + ...actual, + killProcessTree: mockKillProcessTree, + }; +}); + +import { attachToDaemon, killExistingDaemon } from "../../src/lib/daemon.js"; +import type { RunningState } from "../../src/lib/running-state.js"; + +const fakeRunning: RunningState = { + pid: 12345, + configPath: "/fake/config.yaml", + port: 3000, + startedAt: "2026-05-04T00:00:00Z", + projects: ["my-app"], +}; + +beforeEach(() => { + mockUnregister.mockReset(); + mockUnregister.mockResolvedValue(undefined); + mockWaitForExit.mockReset(); + mockKillProcessTree.mockReset(); + mockKillProcessTree.mockResolvedValue(undefined); +}); + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe("attachToDaemon", () => { + it("returns an AttachedDaemon with the running state's port and pid", () => { + const daemon = attachToDaemon(fakeRunning); + expect(daemon.outcome).toBe("attached"); + expect(daemon.port).toBe(3000); + expect(daemon.pid).toBe(12345); + }); + + it("notifyProjectChange POSTs /api/projects/reload and returns ok on 2xx", async () => { + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue(new Response(null, { status: 200 })); + const daemon = attachToDaemon(fakeRunning); + const result = await daemon.notifyProjectChange(); + expect(result).toEqual({ ok: true }); + expect(fetchSpy).toHaveBeenCalledWith("http://localhost:3000/api/projects/reload", { + method: "POST", + }); + fetchSpy.mockRestore(); + }); + + it("notifyProjectChange returns a reasoned failure on non-2xx", async () => { + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue(new Response(null, { status: 503 })); + const daemon = attachToDaemon(fakeRunning); + const result = await daemon.notifyProjectChange(); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.reason).toContain("503"); + } + fetchSpy.mockRestore(); + }); + + it("notifyProjectChange returns a reasoned failure when fetch throws", async () => { + const fetchSpy = vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("ECONNREFUSED")); + const daemon = attachToDaemon(fakeRunning); + const result = await daemon.notifyProjectChange(); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.reason).toContain("ECONNREFUSED"); + } + fetchSpy.mockRestore(); + }); +}); + +describe("killExistingDaemon", () => { + it("uses killProcessTree(SIGTERM), awaits exit, and unregisters on the happy path", async () => { + mockWaitForExit.mockResolvedValueOnce(true); + await killExistingDaemon(fakeRunning); + expect(mockKillProcessTree).toHaveBeenCalledWith(12345, "SIGTERM"); + expect(mockKillProcessTree).toHaveBeenCalledTimes(1); + expect(mockWaitForExit).toHaveBeenCalledWith(12345, 5000); + expect(mockUnregister).toHaveBeenCalled(); + }); + + it("escalates to SIGKILL via killProcessTree when SIGTERM does not exit", async () => { + mockWaitForExit.mockResolvedValueOnce(false); + mockWaitForExit.mockResolvedValueOnce(true); + await killExistingDaemon(fakeRunning); + expect(mockKillProcessTree).toHaveBeenNthCalledWith(1, 12345, "SIGTERM"); + expect(mockKillProcessTree).toHaveBeenNthCalledWith(2, 12345, "SIGKILL"); + expect(mockUnregister).toHaveBeenCalled(); + }); + + it("throws when SIGKILL also fails to exit, and does not unregister", async () => { + mockWaitForExit.mockResolvedValueOnce(false); + mockWaitForExit.mockResolvedValueOnce(false); + await expect(killExistingDaemon(fakeRunning)).rejects.toThrow( + /Failed to stop AO process \(PID 12345\)/, + ); + expect(mockUnregister).not.toHaveBeenCalled(); + }); + + it("treats killProcessTree errors as best-effort and still unregisters when process is gone", async () => { + // killProcessTree itself swallows errors internally, but defend against + // a future regression by ensuring an unexpected throw does not crash + // unregister() when the process has actually exited. + mockKillProcessTree.mockRejectedValueOnce(new Error("transient")); + mockWaitForExit.mockResolvedValueOnce(true); + await expect(killExistingDaemon(fakeRunning)).rejects.toThrow("transient"); + // unregister should NOT have been called in this rejection path — + // we only want to unregister after a clean exit. + expect(mockUnregister).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/cli/__tests__/lib/openclaw-probe.test.ts b/packages/cli/__tests__/lib/openclaw-probe.test.ts index 85b9fad92..d55de3ee1 100644 --- a/packages/cli/__tests__/lib/openclaw-probe.test.ts +++ b/packages/cli/__tests__/lib/openclaw-probe.test.ts @@ -138,7 +138,8 @@ describe("openclaw-probe", () => { const result = await detectOpenClawInstallation(); expect(result.state).toBe("running"); - expect(result.configPath).toContain(".openclaw/openclaw.json"); + expect(result.configPath).toContain(".openclaw"); + expect(result.configPath).toContain("openclaw.json"); }); }); diff --git a/packages/cli/__tests__/lib/path-equality.test.ts b/packages/cli/__tests__/lib/path-equality.test.ts new file mode 100644 index 000000000..dd3c86bcb --- /dev/null +++ b/packages/cli/__tests__/lib/path-equality.test.ts @@ -0,0 +1,98 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; +import { mkdtempSync, rmSync, mkdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { pathsEqual, canonicalCompareKey } from "../../src/lib/path-equality.js"; + +let tmpDir: string; +let originalPlatform: PropertyDescriptor | undefined; + +function setPlatform(p: NodeJS.Platform): void { + Object.defineProperty(process, "platform", { value: p, configurable: true }); +} + +beforeEach(() => { + tmpDir = mkdtempSync(join(tmpdir(), "ao-pathseq-")); + originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); +}); + +afterEach(() => { + if (originalPlatform) { + Object.defineProperty(process, "platform", originalPlatform); + } + rmSync(tmpDir, { recursive: true, force: true }); + vi.restoreAllMocks(); +}); + +describe("pathsEqual", () => { + it("returns true for the same path", () => { + const dir = join(tmpDir, "same"); + mkdirSync(dir); + expect(pathsEqual(dir, dir)).toBe(true); + }); + + it("returns false for clearly different paths", () => { + const a = join(tmpDir, "a"); + const b = join(tmpDir, "b"); + mkdirSync(a); + mkdirSync(b); + expect(pathsEqual(a, b)).toBe(false); + }); + + it.skipIf(process.platform !== "win32")("treats drive-letter case as equal on Windows", () => { + // Real filesystem path so realpathSync resolves; only the input case differs. + const dir = join(tmpDir, "case-test"); + mkdirSync(dir); + const lowerDrive = dir.replace(/^([A-Z]):/, (_, c: string) => `${c.toLowerCase()}:`); + const upperDrive = dir.replace(/^([a-z]):/, (_, c: string) => `${c.toUpperCase()}:`); + expect(pathsEqual(lowerDrive, upperDrive)).toBe(true); + }); + + it.skipIf(process.platform !== "win32")( + "treats arbitrary path-segment case as equal on Windows", + () => { + const dir = join(tmpDir, "MixedCaseSegment"); + mkdirSync(dir); + const lower = dir.toLowerCase(); + // realpathSync should resolve both to the same on-disk canonical form; + // pathsEqual then lowercases for comparison on Windows. + expect(pathsEqual(dir, lower)).toBe(true); + }, + ); + + it.skipIf(process.platform === "win32")("is case-sensitive on POSIX", () => { + // Don't actually mkdir — we just want to verify the comparison logic. + // Use a non-existent path so realpathSync falls back to the literal. + setPlatform("linux"); + const a = "/tmp/Case-Sensitive-Test-NoExist"; + const b = "/tmp/case-sensitive-test-noexist"; + expect(pathsEqual(a, b)).toBe(false); + }); + + it("falls back to literal comparison when realpathSync fails (path doesn't exist)", () => { + const a = join(tmpDir, "nonexistent"); + expect(pathsEqual(a, a)).toBe(true); + }); +}); + +describe("canonicalCompareKey", () => { + it("expands ~ to HOME", () => { + const originalHome = process.env["HOME"]; + process.env["HOME"] = tmpDir; + try { + const key = canonicalCompareKey("~"); + // On Windows the result is lowercased; on POSIX it's case-preserved. + expect(key.toLowerCase()).toBe(tmpDir.toLowerCase()); + } finally { + if (originalHome === undefined) delete process.env["HOME"]; + else process.env["HOME"] = originalHome; + } + }); + + it("returns the same key for equivalent inputs", () => { + const dir = join(tmpDir, "equiv"); + mkdirSync(dir); + expect(canonicalCompareKey(dir)).toBe(canonicalCompareKey(dir)); + }); +}); diff --git a/packages/cli/__tests__/lib/preflight.test.ts b/packages/cli/__tests__/lib/preflight.test.ts index 92c41a31a..3593629fa 100644 --- a/packages/cli/__tests__/lib/preflight.test.ts +++ b/packages/cli/__tests__/lib/preflight.test.ts @@ -1,15 +1,10 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; -const { mockExec, mockIsPortAvailable, mockExistsSync } = vi.hoisted(() => ({ - mockExec: vi.fn(), +const { mockIsPortAvailable, mockExistsSync } = vi.hoisted(() => ({ mockIsPortAvailable: vi.fn(), mockExistsSync: vi.fn(), })); -vi.mock("../../src/lib/shell.js", () => ({ - exec: mockExec, -})); - vi.mock("../../src/lib/web-dir.js", () => ({ isPortAvailable: mockIsPortAvailable, })); @@ -26,7 +21,6 @@ vi.mock("../../src/lib/dashboard-rebuild.js", () => ({ import { preflight } from "../../src/lib/preflight.js"; beforeEach(() => { - mockExec.mockReset(); mockIsPortAvailable.mockReset(); mockExistsSync.mockReset(); }); @@ -130,65 +124,5 @@ describe("preflight.checkBuilt", () => { }); }); -describe("preflight.checkTmux", () => { - it("passes when tmux is already installed", async () => { - mockExec.mockResolvedValue({ stdout: "tmux 3.3a", stderr: "" }); - await expect(preflight.checkTmux()).resolves.toBeUndefined(); - expect(mockExec).toHaveBeenCalledWith("tmux", ["-V"]); - }); - - it("throws with install instructions when tmux is missing", async () => { - mockExec.mockRejectedValue(new Error("ENOENT")); - const err = await preflight.checkTmux().catch((e: Error) => e); - expect(err).toBeInstanceOf(Error); - expect(err.message).toContain("tmux is not installed"); - expect(err.message).toContain("Install it:"); - expect(mockExec).toHaveBeenCalledTimes(1); - expect(mockExec).toHaveBeenCalledWith("tmux", ["-V"]); - }); -}); - -describe("preflight.checkGhAuth", () => { - it("passes when gh is installed and authenticated", async () => { - mockExec.mockResolvedValue({ stdout: "ok", stderr: "" }); - await expect(preflight.checkGhAuth()).resolves.toBeUndefined(); - expect(mockExec).toHaveBeenCalledWith("gh", ["--version"]); - expect(mockExec).toHaveBeenCalledWith("gh", ["auth", "status"]); - }); - - it("throws 'not installed' when gh is missing (ENOENT)", async () => { - mockExec.mockRejectedValue(new Error("ENOENT")); - await expect(preflight.checkGhAuth()).rejects.toThrow( - "GitHub CLI (gh) is not installed", - ); - // Should only call --version, not auth status - expect(mockExec).toHaveBeenCalledTimes(1); - expect(mockExec).toHaveBeenCalledWith("gh", ["--version"]); - }); - - it("throws 'not authenticated' when gh exists but auth fails", async () => { - mockExec - .mockResolvedValueOnce({ stdout: "gh version 2.40", stderr: "" }) // --version succeeds - .mockRejectedValueOnce(new Error("not logged in")); // auth status fails - await expect(preflight.checkGhAuth()).rejects.toThrow( - "GitHub CLI is not authenticated", - ); - expect(mockExec).toHaveBeenCalledTimes(2); - }); - - it("includes correct fix instructions for each failure", async () => { - // Not installed → install link - mockExec.mockRejectedValue(new Error("ENOENT")); - await expect(preflight.checkGhAuth()).rejects.toThrow( - "https://cli.github.com/", - ); - - mockExec.mockReset(); - - // Not authenticated → auth login - mockExec - .mockResolvedValueOnce({ stdout: "gh version 2.40", stderr: "" }) - .mockRejectedValueOnce(new Error("not logged in")); - await expect(preflight.checkGhAuth()).rejects.toThrow("gh auth login"); - }); -}); +// checkTmux + checkGhAuth moved into the runtime-tmux / tracker-github / scm-github +// plugins as their own preflight() methods. See those plugins' tests for coverage. diff --git a/packages/cli/__tests__/lib/script-runner.test.ts b/packages/cli/__tests__/lib/script-runner.test.ts index 56910a16f..532a1b40e 100644 --- a/packages/cli/__tests__/lib/script-runner.test.ts +++ b/packages/cli/__tests__/lib/script-runner.test.ts @@ -58,17 +58,24 @@ describe("script-runner", () => { } }); - it("uses the package root for packaged installs inside node_modules", () => { - const modulePath = - "/usr/local/lib/node_modules/@aoagents/ao-cli/dist/lib/script-runner.js"; + // POSIX-style fixture paths in these tests reach `path.resolve()` on + // Windows, which prepends the current drive letter and converts to + // backslashes. Skip on Windows; the same code paths are exercised by the + // other tests using `mkdtempSync` (which produces native paths). + it.skipIf(process.platform === "win32")( + "uses the package root for packaged installs inside node_modules", + () => { + const modulePath = + "/usr/local/lib/node_modules/@aoagents/ao-cli/dist/lib/script-runner.js"; - expect(resolveScriptLayoutFromPath(modulePath)).toBe("package-install"); - expect(resolveDefaultRepoRootFromPath(modulePath)).toBe( - "/usr/local/lib/node_modules/@aoagents/ao-cli", - ); - }); + expect(resolveScriptLayoutFromPath(modulePath)).toBe("package-install"); + expect(resolveDefaultRepoRootFromPath(modulePath)).toBe( + "/usr/local/lib/node_modules/@aoagents/ao-cli", + ); + }, + ); - it("uses the repository root for source checkouts", () => { + it.skipIf(process.platform === "win32")("uses the repository root for source checkouts", () => { const modulePath = "/Users/test/agent-orchestrator/packages/cli/src/lib/script-runner.ts"; @@ -84,9 +91,12 @@ describe("script-runner", () => { "../../src/assets/scripts", ); + // Escape every regex metachar (including '\' on Windows paths) for the + // scripts-directory portion so the assertion is path-separator-agnostic. + const escapedDir = expectedScriptsDir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); expect(() => resolveScriptPath("does-not-exist.sh")).toThrowError( new RegExp( - `Script not found: does-not-exist\\.sh\\. Expected at: .*does-not-exist\\.sh \\(scripts directory: ${expectedScriptsDir.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\$&")}\\)`, + `Script not found: does-not-exist\\.sh\\. Expected at: .*does-not-exist\\.sh \\(scripts directory: ${escapedDir}\\)`, ), ); }); @@ -125,7 +135,88 @@ describe("script-runner", () => { expect(resolveScriptLayout()).toBe("package-install"); }); - it("pins script execution cwd to the resolved install root", async () => { + // ----------------------------------------------------------------------- + // Windows PowerShell branch — runRepoScript prefers