diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000000..3f81354a6d4 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +# Every cargo test/run in this repo, any profile and any crate, opts out of +# product telemetry. Cargo's [env] never overrides a variable that is +# already set, so a test that needs it can still set its own value. +[env] +DO_NOT_TRACK = "1" diff --git a/.changes/acp-daemon-initialize-validation.md b/.changes/acp-daemon-initialize-validation.md new file mode 100644 index 00000000000..21100365c33 --- /dev/null +++ b/.changes/acp-daemon-initialize-validation.md @@ -0,0 +1 @@ +- ACP mode over the daemon now rejects an `initialize` request with invalid params (for example a string `protocolVersion`), as the TypeScript version does. diff --git a/.changes/acp-delete-in-process-fallback.md b/.changes/acp-delete-in-process-fallback.md new file mode 100644 index 00000000000..ca58f4dacdb --- /dev/null +++ b/.changes/acp-delete-in-process-fallback.md @@ -0,0 +1 @@ +- `--mode acp` no longer falls back to a separate in-process engine when the background service cannot start: it now requires the daemon, and a startup failure prints `Error:` and exits 1, like the TypeScript version. A `session/cancel` sent as a request instead of a notification now answers "method not found", like the TypeScript version. diff --git a/.changes/acp-heartbeat-bash-updates.md b/.changes/acp-heartbeat-bash-updates.md new file mode 100644 index 00000000000..9f408776ef3 --- /dev/null +++ b/.changes/acp-heartbeat-bash-updates.md @@ -0,0 +1 @@ +- ACP mode now reports heartbeat schedule changes and shell commands that another client runs in the session (as tool calls), and leaves out empty tool output, like the TypeScript version. diff --git a/.changes/acp-mcp-replace-cleanup.md b/.changes/acp-mcp-replace-cleanup.md new file mode 100644 index 00000000000..ecaee2816b9 --- /dev/null +++ b/.changes/acp-mcp-replace-cleanup.md @@ -0,0 +1 @@ +- ACP `session/new` now clears the connection's MCP servers when admitting them fails or times out, so servers the worker applied before a lost acknowledgement no longer stay admitted, matching the TypeScript behavior. diff --git a/.changes/acp-rlm-quiescence.md b/.changes/acp-rlm-quiescence.md new file mode 100644 index 00000000000..9bdbf62dcca --- /dev/null +++ b/.changes/acp-rlm-quiescence.md @@ -0,0 +1 @@ +- ACP prompts now finish only once the subagents they started have finished: the completion update reports how many were still running, the final update reports zero, and cancelling or closing the session cancels them. Closing stdin no longer waits for running work to finish. diff --git a/.changes/acp-session-flags.md b/.changes/acp-session-flags.md new file mode 100644 index 00000000000..28c1950fd5c --- /dev/null +++ b/.changes/acp-session-flags.md @@ -0,0 +1 @@ +- ACP mode over the daemon now honors the session flags: without `--no-session` the conversation is saved like any other session and `--resume`, `--continue`, `--fork` and `--session-dir` open or place it; only `--no-session` sessions belong to the editor's connection and end when it disconnects, and `session/close` stops the running work without ending the session. Startup failures (an unknown or busy session) now exit with an error instead of starting, and `DO_NOT_TRACK` / the telemetry setting now also disable the daemon session's telemetry. diff --git a/.changes/agent-message-wakes-passivated-child.md b/.changes/agent-message-wakes-passivated-child.md new file mode 100644 index 00000000000..d5b7a0f857a --- /dev/null +++ b/.changes/agent-message-wakes-passivated-child.md @@ -0,0 +1 @@ +- Messaging a subagent whose worker was idle-passivated now wakes it and delivers the message, instead of failing with "Unknown active session". diff --git a/.changes/csi-modifier-non-ascii-panic.md b/.changes/csi-modifier-non-ascii-panic.md new file mode 100644 index 00000000000..0a7716c64de --- /dev/null +++ b/.changes/csi-modifier-non-ascii-panic.md @@ -0,0 +1 @@ +- A malformed escape sequence containing a non-ASCII character (for example, Option-key input mixed with Cyrillic or accented text) is now passed through as typed text instead of crashing the terminal UI. diff --git a/.changes/feat-machine-library.md b/.changes/feat-machine-library.md new file mode 100644 index 00000000000..8e5db780c03 --- /dev/null +++ b/.changes/feat-machine-library.md @@ -0,0 +1,2 @@ +- New `prime-agent factory list | import | export` commands manage the machine library of shareable `MACHINE.md` factory templates; the bundled seeds (`builder`, `pr-manager`, `review-sweep`) ship inside the runtime so every install sees them, personal machines live under the agent dir, and an invalid machine never persists. +- `await rlm.factory.run('')` runs a library machine directly without creating a harness entry; a machine that exists but is broken names its exact errors, and an existing export target is refused instead of silently overwritten. diff --git a/.changes/feat-rust-semantic-edges.md b/.changes/feat-rust-semantic-edges.md new file mode 100644 index 00000000000..1067e66c409 --- /dev/null +++ b/.changes/feat-rust-semantic-edges.md @@ -0,0 +1 @@ +- Each model request now carries a request id header, and each session writes a semantic-edge ledger (request, subagent and compaction events) matching the TS agent. diff --git a/.changes/fix-acp-turn-ownership.md b/.changes/fix-acp-turn-ownership.md new file mode 100644 index 00000000000..accfa4a2bee --- /dev/null +++ b/.changes/fix-acp-turn-ownership.md @@ -0,0 +1 @@ +- ACP sessions run one prompt turn at a time: a prompt sent while a turn runs, or while a cancel is stopping it, is refused instead of overlapping it; a cancel now stops only its own turn, and a failed turn no longer reports itself as finished. diff --git a/.changes/fix-adopted-worker-exit-wait.md b/.changes/fix-adopted-worker-exit-wait.md new file mode 100644 index 00000000000..88ca2cb8d13 --- /dev/null +++ b/.changes/fix-adopted-worker-exit-wait.md @@ -0,0 +1 @@ +- The daemon no longer wakes up every second for each session it keeps running across a restart or update: it now waits on the operating system's process-exit notification (kqueue on macOS, pidfd on Linux), which cuts idle battery drain. On macOS this also stops a `ps` process being spawned every second for each such session. diff --git a/.changes/fix-cancel-owned-prompt-admission.md b/.changes/fix-cancel-owned-prompt-admission.md new file mode 100644 index 00000000000..09900c3bf03 --- /dev/null +++ b/.changes/fix-cancel-owned-prompt-admission.md @@ -0,0 +1 @@ +- Cancelling a running prompt by its admission id now reports it as `owned` instead of `cancelled`, and with `cancelOwned` the cancel now stops the turn. diff --git a/.changes/fix-click-expand-custom-rows.md b/.changes/fix-click-expand-custom-rows.md new file mode 100644 index 00000000000..de215d9564c --- /dev/null +++ b/.changes/fix-click-expand-custom-rows.md @@ -0,0 +1 @@ +- Clicking a compaction summary, a harness refinement notice, a skill invocation, or an injected prompt (heartbeat, goal, subagent status) in the transcript expands or collapses it again. diff --git a/.changes/fix-code-fence-wrap.md b/.changes/fix-code-fence-wrap.md new file mode 100644 index 00000000000..87c51a6b3df --- /dev/null +++ b/.changes/fix-code-fence-wrap.md @@ -0,0 +1 @@ +- Long lines inside fenced code blocks now wrap onto the next row instead of being cut off at the edge of the terminal. diff --git a/.changes/fix-daemon-client-partial-line.md b/.changes/fix-daemon-client-partial-line.md new file mode 100644 index 00000000000..045544fc4ee --- /dev/null +++ b/.changes/fix-daemon-client-partial-line.md @@ -0,0 +1 @@ +- `prime-agent list` and other daemon commands no longer hang until they time out when a busy daemon sends a large response slowly. diff --git a/.changes/fix-kernel-wedge-snapshot-fd.md b/.changes/fix-kernel-wedge-snapshot-fd.md new file mode 100644 index 00000000000..33c47ed2d2e --- /dev/null +++ b/.changes/fix-kernel-wedge-snapshot-fd.md @@ -0,0 +1 @@ +- Session resume no longer wedges when the saved state held a `bash()` handle: snapshots skip live process handles, whose restore reopened a stale fd number and killed the restored kernel's event loop. Restoring also refuses to reopen any pickled raw fd number (legacy snapshots, user pipe objects), failing just that saved name instead of the whole kernel. A kernel that drops a protocol frame now logs it to kernel-stderr.log instead of failing silently. A timed-out request whose code was still being sent to a stalled kernel no longer leaves the kernel permanently busy; the kernel restarts instead. diff --git a/.changes/fix-mid-stream-websocket-retry.md b/.changes/fix-mid-stream-websocket-retry.md new file mode 100644 index 00000000000..5dad15d0c76 --- /dev/null +++ b/.changes/fix-mid-stream-websocket-retry.md @@ -0,0 +1 @@ +- Retry interrupted WebSocket responses from committed context with exactly 2s, 4s and 8s waits, at most three retries. Discard failed sockets and partial replies, preserve completed tools, and replace failed partial content in the live and reloaded transcript. Direct print/json uses the same session retry driver. diff --git a/.changes/fix-models-json-max-tokens.md b/.changes/fix-models-json-max-tokens.md new file mode 100644 index 00000000000..024973e480c --- /dev/null +++ b/.changes/fix-models-json-max-tokens.md @@ -0,0 +1 @@ +- A `maxTokens` configured in models.json (or a per-model override) now reaches the provider unchanged instead of being silently clamped to the 32000 default output ceiling; catalog models keep the default ceiling ([#755](https://github.com/PrimeIntellect-ai/prime-agent/issues/755)). diff --git a/.changes/fix-nosession-client-owned.md b/.changes/fix-nosession-client-owned.md new file mode 100644 index 00000000000..c9ad8727e4f --- /dev/null +++ b/.changes/fix-nosession-client-owned.md @@ -0,0 +1 @@ +- ACP sessions and TUI `--no-session` sessions are now owned by the client that created them: they are never idle-passivated, and their agents-view row is removed when they stop instead of lingering as a passive row that cannot be resumed. diff --git a/.changes/fix-owned-worker-disconnect-cleanup.md b/.changes/fix-owned-worker-disconnect-cleanup.md new file mode 100644 index 00000000000..6bf42bb78d3 --- /dev/null +++ b/.changes/fix-owned-worker-disconnect-cleanup.md @@ -0,0 +1 @@ +- Client-owned sessions (`--no-session` ACP and TUI sessions) now stop 30 seconds after their client disconnects without closing them, or 30 seconds after a daemon restart if the client does not reconnect, instead of running on hidden until the daemon stops. diff --git a/.changes/fix-rlm-child-failure-notice.md b/.changes/fix-rlm-child-failure-notice.md new file mode 100644 index 00000000000..295fd54bdb2 --- /dev/null +++ b/.changes/fix-rlm-child-failure-notice.md @@ -0,0 +1 @@ +- A subagent that fails because its task cannot be delivered, or whose worker becomes unreachable, now sends a failure notice with the error to its parent, and a goal waiting on it resumes. diff --git a/.changes/fix-tools-flag.md b/.changes/fix-tools-flag.md new file mode 100644 index 00000000000..abbb735e5bb --- /dev/null +++ b/.changes/fix-tools-flag.md @@ -0,0 +1 @@ +- Removed the `-t/--tools`, `-nt/--no-tools` and `-nbt/--no-builtin-tools` flags. They were silently ignored; they now fail as unknown options. diff --git a/.changes/git-state-off-session-lock.md b/.changes/git-state-off-session-lock.md new file mode 100644 index 00000000000..2ea68dfe8ce --- /dev/null +++ b/.changes/git-state-off-session-lock.md @@ -0,0 +1 @@ +- Agent runs no longer stall the session while recording git state: the git checks at the start and end of each run now run in parallel in the background instead of blocking other work on the session. diff --git a/.changes/installer-force-scope.md b/.changes/installer-force-scope.md new file mode 100644 index 00000000000..937d11cf5f5 --- /dev/null +++ b/.changes/installer-force-scope.md @@ -0,0 +1 @@ +- The installer's `--force` no longer stops the daemon the install runs under, waits for the normal drain before signalling a stuck daemon, and no longer leaves a temp directory behind when it exits early. diff --git a/.changes/ipython-bash-dominated-cells.md b/.changes/ipython-bash-dominated-cells.md new file mode 100644 index 00000000000..8ad9ad52034 --- /dev/null +++ b/.changes/ipython-bash-dominated-cells.md @@ -0,0 +1 @@ +- An ipython cell that mostly runs shell commands now shows as `bash` in its collapsed card, with its first `bash()` command and how many more it ran, including commands run through helper functions. diff --git a/.changes/linux-dead-pid-skips-ps.md b/.changes/linux-dead-pid-skips-ps.md new file mode 100644 index 00000000000..9672a82f2ca --- /dev/null +++ b/.changes/linux-dead-pid-skips-ps.md @@ -0,0 +1 @@ +- On Linux the daemon no longer spawns a `ps` process each time it checks the identity of a process that has already exited (stale session leases, session reuse, update checks). diff --git a/.changes/long-turn-routes.md b/.changes/long-turn-routes.md new file mode 100644 index 00000000000..a37ed6fcf80 --- /dev/null +++ b/.changes/long-turn-routes.md @@ -0,0 +1 @@ +- ACP prompts and other long daemon requests (waiting for a turn, idle or headless completion, compaction, tree navigation, session start) now wait up to 24 hours like the TypeScript daemon instead of failing with "Session worker timed out" after 10 minutes, and a session worker that dies mid-request now fails it right away instead of after its restart. diff --git a/.changes/nightly-update-direct.md b/.changes/nightly-update-direct.md new file mode 100644 index 00000000000..b6265a56440 --- /dev/null +++ b/.changes/nightly-update-direct.md @@ -0,0 +1 @@ +- Nightly updates download the nightly installer directly instead of going through the stable installer first. diff --git a/.changes/rlm-children-survive-daemon-restart.md b/.changes/rlm-children-survive-daemon-restart.md new file mode 100644 index 00000000000..3c496e933e4 --- /dev/null +++ b/.changes/rlm-children-survive-daemon-restart.md @@ -0,0 +1 @@ +- After a daemon restart or update, a parent session lists its subagents again, and messaging one wakes it as the parent's subagent, instead of rlm.list_subagents() coming back empty. diff --git a/.changes/rlm-rename.md b/.changes/rlm-rename.md new file mode 100644 index 00000000000..c7d7c948c57 --- /dev/null +++ b/.changes/rlm-rename.md @@ -0,0 +1,2 @@ +- Added the `rlm.rename(new_name, session_id=...)` kernel function: renames the calling session (no `session_id`) or a direct child selected by spawn handle, `list_subagents()` row, or session id — never by child name — reusing spawn-name validation and daemon-owned sibling uniqueness; the renamed session gets a `Session renamed `old` -> `new`` transcript notice (suffixed ` by parent` when a parent renamed it), and the kernel readiness check now requires a runtime with `rlm.rename`. +- Agents view: the rename affordance (ctrl+r / `/name`) now also applies to subagent rows, which carry the same renameable session identity as top-level agent rows. diff --git a/.changes/session-id-path-validation.md b/.changes/session-id-path-validation.md new file mode 100644 index 00000000000..1203f469225 --- /dev/null +++ b/.changes/session-id-path-validation.md @@ -0,0 +1 @@ +- A session file whose header id is not a plain file name (for example `../x` or an absolute path) is now treated as an invalid session, so its artifacts can no longer be written outside the session-artifacts folder. diff --git a/.changes/sse-done-marker-ends-stream.md b/.changes/sse-done-marker-ends-stream.md new file mode 100644 index 00000000000..50d081fd470 --- /dev/null +++ b/.changes/sse-done-marker-ends-stream.md @@ -0,0 +1 @@ +- openai-completions streams no longer hang forever when a server keeps the response body open past the `data: [DONE]` marker (a persistent SSE channel with no content length, chunked terminator, or half-close): the stream now ends at the marker like the TypeScript SDK, so the turn runs its tool call instead of waiting silently for an EOF that never comes. diff --git a/.changes/supervisor-event-order.md b/.changes/supervisor-event-order.md new file mode 100644 index 00000000000..3682f6fa834 --- /dev/null +++ b/.changes/supervisor-event-order.md @@ -0,0 +1 @@ +- The daemon now always delivers a turn's events before the reply that ends the turn, and ACP mode no longer waits up to 30 seconds after some turns before answering. diff --git a/.changes/unstash-on-send.md b/.changes/unstash-on-send.md new file mode 100644 index 00000000000..bedfdf0960e --- /dev/null +++ b/.changes/unstash-on-send.md @@ -0,0 +1 @@ +- A prompt stashed with ctrl+s now returns to the empty prompt box after you send another prompt, as it did in the TypeScript version. diff --git a/.changes/update-installer-no-terminal.md b/.changes/update-installer-no-terminal.md new file mode 100644 index 00000000000..e5023b01c7d --- /dev/null +++ b/.changes/update-installer-no-terminal.md @@ -0,0 +1 @@ +- `/update` in the TUI no longer hangs on an installer prompt or swallows keystrokes: the installer now runs detached from the terminal. diff --git a/.changes/windows-install-entry-point-publish.md b/.changes/windows-install-entry-point-publish.md new file mode 100644 index 00000000000..e1a2c56bf51 --- /dev/null +++ b/.changes/windows-install-entry-point-publish.md @@ -0,0 +1 @@ +- The Windows one-liner is real again: the release pipeline publishes `install.ps1` (the stable-channel render, the channel's own base + fallback to beta when stable carries no Windows build) on every beta cut instead of waiting for a first Rust stable that has not been cut, and the README's PowerShell command points at the copy that actually serves (the merged repo file, the same route the Windows battery installs from) instead of the domain prefix that 404s. diff --git a/.changes/worker-socket-closed-error-text.md b/.changes/worker-socket-closed-error-text.md new file mode 100644 index 00000000000..5f1a6cd5d66 --- /dev/null +++ b/.changes/worker-socket-closed-error-text.md @@ -0,0 +1 @@ +- A request that a crashed session worker never answered now fails with "Daemon worker socket closed", the same error the TypeScript daemon reports. diff --git a/.cursor/BUGBOT.md b/.cursor/BUGBOT.md index 7ab99488d5d..06b4ea4bdbf 100644 --- a/.cursor/BUGBOT.md +++ b/.cursor/BUGBOT.md @@ -3,6 +3,17 @@ These rules mirror the repo standards in AGENTS.md - update both whenever either changes. +## Repository identity + +- Repository commits use only the ambient Git identity. Never configure or override author/committer names or emails for repository history. Missing identity is an error to report, not repair with configuration. +- Never add `Co-authored-by` or other assistant attribution trailers. + +## Performance and the critical path + +- First paint and user-visible frames must never await network, disk sync, telemetry delivery, or background bookkeeping. Hand work to its owning background worker and return. +- Telemetry delivery is fire-and-forget from startup, session creation, tab opening, and render paths. Timeout, retry, and ordering belong to the delivery worker. +- A changed fire-and-forget boundary needs a red-first test with a hanging sink. Offline or mocked delivery alone cannot prove paint stays unblocked. Keep `the_startup_flush_never_blocks_the_first_frame` as the launch-path guard. + ## No legacy or migration scaffolding - When a migration or refactor completes within a PR, its staging scaffolding must be deleted, not kept: no type aliases where two names point at one type, no re-exports "for compatibility" without a live external consumer, no migration-era vocabulary ("staged", "legacy", "new-style") surviving in names, comments, or test titles. @@ -54,6 +65,11 @@ either changes. - Every inline `#[allow(...)]` or lint exception states its reason in a comment next to it; flag unexplained or broad exceptions. Advisory ignores in `deny.toml` need a reason and a review date. - New lint families land whole-site: enabling a family requires fixing every current site in the same change. Flag partial enablements. +## Branding and wire compatibility + +- Brand user-visible docs, CLI help, errors, onboarding, TUI labels and quoted user-facing comments as Prime Agent, not pi/pi-mono/pi-ai or Prime Intellect. +- Preserve byte-compatible TS wire identifiers (`PI_PACKAGE_DIR`, settings/env keys, provider IDs such as `prime-inference`, and `ai.primeintellect.prime-agent` namespaces). Parity beats branding on the wire; require the PR to list preserved identifiers. + ## Generated files - The model catalog is hand-maintained data: `crates/pa-ai/src/models_generated.rs` mirrors the TS `packages/ai/src/models.generated.ts` object literal (no generator, no generated JSON — edit it by hand exactly like its TS source), and the parity fixture `crates/pa-models/tests/fixtures/catalog.v1.json` refreshes with `scripts/generate-catalog-fixture.py` from the real `PrimeIntellect-ai/prime-agent-catalog` snapshot. Flag edits that would reintroduce a generator or hand-tweak the fixture rows. diff --git a/.github/VOUCHED.td b/.github/VOUCHED.td new file mode 100644 index 00000000000..564172c2b42 --- /dev/null +++ b/.github/VOUCHED.td @@ -0,0 +1,5 @@ +# Trusted external contributors, one GitHub username per line without @. +# Maintainers, bots, and collaborators with write access are allowed automatically. +Arian-Urdu +DariusMatin +sirouk diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 413250177ad..a5cc05f05ed 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,3 +1,9 @@ + + ## Ownership compliance diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67905a9ac2f..8ce8ab58b80 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,14 @@ # CI gates for the prime-agent Rust port: the tip-compiles-tests guarantee # plus PR gating, in one workflow (the shape of the TS product's ci.yml). -# +# `.github/workflows/` is the one home for workflow files; the +# windows-cross/windows/deny jobs run live in this file alongside the unix +# gates. # Two triggers, one # gate set mirroring `make check` exactly (fmt --all --check, clippy --workspace # --all-targets -- -D warnings, cargo test --workspace) on the pinned # toolchain (1.98.1) and runner image the continuous/release builds use: # -# push to main/rust — every merge is a push. continuous.yml proves the +# push to main — every merge is a push. continuous.yml proves the # release binaries link; THIS workflow proves the tip's test/bench/example # targets compile (`cargo check --workspace --all-targets`) and the test # suites pass (the test suite sharded below; the count is the SHARD_COUNT @@ -57,9 +59,15 @@ # assigned unit executed — and merges the failure reports into one table # naming the failing binaries; the gate is green only when all shards are. # -# Strictly read-only: contents: read, persist-credentials: false, no secrets. -# Windows coverage runs via the on-demand windows-runtime-triage.yml battery -# (any ref); there is no windows or deny job in this workflow. +# Strictly read-only: contents: read, persist-credentials: false, no secrets +# (the trust job's vouch step reads the GitHub API with the ambient +# GITHUB_TOKEN). The contributor-trust gate (the `trust` job, ported from +# main's ci.yml — the vouch mechanism) precedes every job: push events are +# always allowed; PR authors must be a bot, a collaborator with write +# access, or a vouched contributor (mitchellh/vouch; the vouched list +# resolves from the repo's default branch — `.github/VOUCHED.td` on main, +# nothing local on this branch), so unvouched PR authors never bill a +# runner minute. The auto-close enforcement lives in contribution-gate.yml. # name: ci @@ -110,9 +118,10 @@ jobs: # feeds test shards' --crates; empty means "the full union". changes: name: touched crates (PR) / full selection (push + queue) + needs: trust runs-on: ubuntu-latest timeout-minutes: 10 - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name != 'pull_request' || !github.event.pull_request.draft) outputs: crates: ${{ steps.select.outputs.crates }} scope: ${{ steps.select.outputs.scope }} @@ -153,35 +162,61 @@ jobs: echo "crates under test: ${{ steps.select.outputs.crates }}" fi + # The contributor-trust gate (this PR's port from main's TS-era ci.yml): + # the `allowed` output every job below keys on — push events are always + # allowed, PR authors must be a bot, a collaborator with write access, + # or vouched. allow-fail keeps this job green: the gate is the output, + # not the step. + trust: + name: contributor trust + runs-on: ubuntu-24.04 + timeout-minutes: 10 + outputs: + # merge_group auto-allows: the queued candidate already passed the + # vouch-gated pull_request wave (the queue only takes vouched PRs); + # the merge-group run is the full-selection validation of the + # already-vouched candidate. + allowed: ${{ github.event_name == 'push' || github.event_name == 'merge_group' || steps.vouch.outputs.vouched == 'true' }} + steps: + - name: Check pull request author + id: vouch + if: github.event_name == 'pull_request' + uses: mitchellh/vouch/action/check-user@d66fa29a64600490892131ad87597c30c91fcac4 # v1 + with: + user: ${{ github.event.pull_request.user.login }} + allow-fail: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + fmt: name: fmt + needs: trust runs-on: ubuntu-24.04 timeout-minutes: 30 # Draft PRs do not gate (no billing on work-in-progress pushes); the # ready_for_review trigger runs the gates once a draft is marked ready. - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name != 'pull_request' || !github.event.pull_request.draft) steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; toolchain pinned below + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; the SHA selects the toolchain with: - toolchain: "1.98.1" components: rustfmt - run: cargo fmt --all --check clippy-test: name: check + clippy (workspace) + needs: trust runs-on: ubuntu-24.04 timeout-minutes: 45 - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name != 'pull_request' || !github.event.pull_request.draft) steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; toolchain pinned below + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; the SHA selects the toolchain with: - toolchain: "1.98.1" components: clippy - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: @@ -209,13 +244,13 @@ jobs: # The shard legs need the wave's selection (the changes job's crates # output) before they can decide what to run — and whether the # sibling-binary build is needed at all. - needs: [changes] + needs: [changes, trust] # strategy.job-total (the matrix leg count) auto-derives the displayed # total; the env context is not reachable from a job name. name: test shard ${{ matrix.shard }}/${{ strategy.job-total }} runs-on: ubuntu-24.04 timeout-minutes: 45 - if: github.event_name != 'pull_request' || !github.event.pull_request.draft + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name != 'pull_request' || !github.event.pull_request.draft) strategy: # One red shard must not cancel the others: their own conclusions are # the per-shard signal the summary merges. @@ -229,17 +264,25 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; toolchain pinned below - with: - toolchain: "1.98.1" + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; the SHA selects the toolchain - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: cache-on-failure: true - # Per-shard caches (the default key carries the job id, which is - # identical for all matrix legs); parallel legs would otherwise - # race onto one cache entry and only the first save would stick. - shared-key: shard-${{ matrix.shard }} - - name: Install uv (the kernel tests bootstrap the Python kernel venv with it) + # One shared dependency cache for all legs: per-shard keys held + # 8x ~500 MB per ref and evicted the release-build caches (the + # repo sits at the 10 GB Actions-cache quota; the shards were 83% + # of it). The shards' dev-dependencies differ, and whichever leg + # saves first defines the shared entry, so other legs get a + # partial hit and compile the rest. Slower hits, no correctness + # issue. + shared-key: test-shards + # Only the push wave writes the entry: PR and merge-queue legs + # restore (the restore key falls back to the push wave's entry) + # and save nothing - a PR's save would land under the PR's own + # ref, and PR-ref entries are the eviction pressure on the + # release-build caches. + save-if: ${{ github.event_name == 'push' }} + - name: Install uv (the kernel venv bootstrap below uses it) run: | python3 -m pip install --user uv echo "$HOME/.local/bin" >> "$GITHUB_PATH" @@ -289,8 +332,8 @@ jobs: name: test summary runs-on: ubuntu-24.04 timeout-minutes: 10 - needs: [test-shard] - if: always() && (github.event_name != 'pull_request' || !github.event.pull_request.draft) + needs: [trust, test-shard] + if: always() && needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name != 'pull_request' || !github.event.pull_request.draft) steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: @@ -303,5 +346,90 @@ jobs: - name: Audit the shards and merge their failure reports run: python3 scripts/ci_test_shard_summary.py --total ${{ env.SHARD_COUNT }} --dir manifests - # Windows coverage: the on-demand windows-runtime-triage.yml battery (any - # ref); no windows job runs in this workflow. + # The Windows job set + the supply-chain deny gate: + # + # windows-cross — the cfg-hygiene cross-target gate, the CI mirror of + # `make windows-cross`. The Sep-23..25 + # unix-only test regressions that broke it were fixed by #2833, so it + # type-checks every crate (including the cfg(windows) implementations + # and the Windows-only tests) for x86_64-pc-windows-gnu; ring's C needs + # the mingw cross compiler even for check builds. + # + # windows-latest — NOT in this promotion set. The full MSVC battery + # surfaced tip-side windows product debt the trunk has carried invisibly + # (the on-demand windows-runtime-triage.yml battery never ran on main; + # its first dispatches read deep red across crates, and the ACP + # daemon-attach e2e deadlocks on the named-pipe transport). The + # windows-latest promotion lands with the windows-readiness lane's + # product fixes; windows-cross below stays this PR's windows authority + # (the compile + clippy proof for the whole workspace incl. the + # cfg(windows) implementations). + # + # deny — cargo-deny advisories + licenses, the CI mirror of `make deny`, + # the same check continuous.yml's deny job runs on every push. + # Promoted on the operator's 2026-09-28 policy decision: the advisory + # DB drifts daily, so a drifted advisory can gate PRs — that is the + # accepted trade. + windows-cross: + name: windows cross-check + clippy (x86_64-pc-windows-gnu) + needs: trust + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name == 'push' || !github.event.pull_request.draft) + runs-on: ubuntu-24.04 + timeout-minutes: 60 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # the ref IS the 1.98.1 toolchain; the action ignores the toolchain input + with: + targets: x86_64-pc-windows-gnu + components: clippy + - name: Install the mingw-w64 cross toolchain (ring C for the target) + run: sudo apt-get update && sudo apt-get install -y gcc-mingw-w64-x86-64 + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 + with: + key: windows-cross + - run: cargo check --locked --workspace --target x86_64-pc-windows-gnu --all-targets + - run: cargo clippy --locked --workspace --target x86_64-pc-windows-gnu --all-targets -- -D warnings + + deny: + name: deny (cargo-deny advisories + licenses) + needs: trust + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' && (github.event_name == 'push' || !github.event.pull_request.draft) + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + - name: Dependency audit (cargo-deny advisories + licenses) + uses: EmbarkStudios/cargo-deny-action@3c6349835b2b7b196a839186cb8b78e02f7b5f25 # v2.1.1 + with: + command: check + arguments: --all-features --workspace + command-arguments: advisories licenses + + # The trust gate's own failure made visible: a vouch-step error (the + # action's setup, the runner) would otherwise leave every dependent job + # skipped, and skipped required checks read as passing to branch + # protection - a green-looking wave with no gates run. This audit runs + # unconditionally (always()) and fails the required-check set when the + # trust job itself did not conclude success. The deliberate not-vouched + # path is unaffected: there the trust job SUCCEEDS with allowed=false, + # the dependents skip by design, and the contribution-gate auto-close + # enforces. + trust-audit: + name: trust gate audit + needs: [trust, changes, fmt, clippy-test, test-shard, test-summary, windows-cross, deny] + if: always() + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Audit the contributor-trust gate's own outcome + run: | + set -euo pipefail + if [ "${{ needs.trust.result }}" != "success" ]; then + echo "::error::the contributor-trust gate did not conclude cleanly (result: ${{ needs.trust.result }}) - the wave is NOT trustworthy: every gate below it skipped" + exit 1 + fi + echo "trust gate concluded cleanly (the allowed verdict: ${{ needs.trust.outputs.allowed }})" diff --git a/.github/workflows/codebase-health.yml b/.github/workflows/codebase-health.yml index cbd3823db04..4e0b907d434 100644 --- a/.github/workflows/codebase-health.yml +++ b/.github/workflows/codebase-health.yml @@ -5,7 +5,31 @@ on: permissions: contents: read jobs: + # The contributor-trust gate (ported from main's ci.yml — the vouch + # mechanism): unvouched PR authors never bill a runner minute, not even + # for guidance-only jobs. allow-fail keeps the job green; the gate is the + # `allowed` output the loc job keys on. + trust: + name: contributor trust + runs-on: ubuntu-latest + outputs: + # merge_group auto-allows: the queued candidate already passed the + # vouch-gated pull_request wave; the merge-group run is the + # full-selection validation of the already-vouched candidate. + allowed: ${{ github.event_name == 'push' || github.event_name == 'merge_group' || steps.vouch.outputs.vouched == 'true' }} + steps: + - name: Check pull request author + id: vouch + if: github.event_name == 'pull_request' + uses: mitchellh/vouch/action/check-user@d66fa29a64600490892131ad87597c30c91fcac4 # v1 + with: + user: ${{ github.event.pull_request.user.login }} + allow-fail: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} loc: + needs: trust + if: needs.trust.result == 'success' && needs.trust.outputs.allowed == 'true' runs-on: ubuntu-latest steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 @@ -68,3 +92,23 @@ jobs: print(f' {before} -> {after} lines: {path}') print(f'{len(findings)} changed Rust file(s) grew beyond 2,000 lines (guidance only).') PY + + # The trust gate's own failure made visible (see ci.yml's trust-audit + # for the full rationale): always() runs even when the loc job skips or + # the trust job itself fails, and this audit fails the required-check + # set when the trust gate did not conclude cleanly. + trust-audit: + name: trust gate audit + needs: [trust, loc] + if: always() + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Audit the contributor-trust gate's own outcome + run: | + set -euo pipefail + if [ "${{ needs.trust.result }}" != "success" ]; then + echo "::error::the contributor-trust gate did not conclude cleanly (result: ${{ needs.trust.result }}) - the wave is NOT trustworthy: the loc gates skipped" + exit 1 + fi + echo "trust gate concluded cleanly (the allowed verdict: ${{ needs.trust.outputs.allowed }})" diff --git a/.github/workflows/continuous.yml b/.github/workflows/continuous.yml index dabe1976221..e3d6296222b 100644 --- a/.github/workflows/continuous.yml +++ b/.github/workflows/continuous.yml @@ -1,6 +1,6 @@ # Continuous build pipeline for Prime Agent (Rust). # -# Builds the 4-target release matrix on every push to `main` or `rust` and +# Builds the 4-target release matrix on every push to `main` and # uploads the tarballs as workflow artifacts (downloadable from the run page). # Does NOT create tags or releases — the org repo's release history stays # owned by the merged product's release workflow (release.yml). @@ -13,7 +13,6 @@ on: push: branches: - main - - main permissions: {} @@ -27,7 +26,7 @@ env: jobs: # Superseded-push gate (TS #2567): with `cancel-in-progress: false`, every - # push to main/rust queues a full 4-target build, so a burst of merges + # push to main queues a full 4-target build, so a burst of merges # leaves older runs building artifacts for a commit that is no longer the # tip. The gate checks the live branch tip first: when the workflow commit # is superseded, the expensive build jobs skip (neutral exit) and the @@ -138,9 +137,11 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@e081816240890017053eacbb1bdf337761dc5582 # 1.95.0 action pin; toolchain pinned below + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e with: - toolchain: "1.98.1" targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: @@ -151,8 +152,11 @@ jobs: # the new key builds cold; the marker moves only with reviewed # baseline changes (same protocol as the image digest below). key: ${{ matrix.target }}-glibc235 + # Only the shipped binary links here: the job packages `prime-agent` + # alone, so pa-daemon's [[bin]] would be a second thin-LTO link with + # no consumer (~26% of the build step, measured). - name: Build (release, locked) - run: cargo build --release --locked --target ${{ matrix.target }} -j 4 + run: cargo build --release --locked --target ${{ matrix.target }} -j 4 -p pa-cli --bin prime-agent - name: Split the shipped binary + split-debug decoder (Option C posture) run: | set -euo pipefail @@ -234,8 +238,17 @@ jobs: include: - target: aarch64-apple-darwin runs-on: macos-14 + # The x86_64 leg cross-compiles on the Apple-silicon pool: rustc + # and the Xcode linker build either darwin arch from an M-series + # host, and the M-pool links this binary ~2.2x faster than the + # macos-15-intel pool with far tighter variance (484 s vs 1046 s + # same-commit link tail, 2026-09-30). The livecheck below runs + # the assembled x64 artifact under Rosetta 2 — the macos-15 + # arm64 image installs it, and install-rust.sh ships the x64 + # build to an Apple-Silicon Mac's Rosetta shell, so the gate + # still boots the binary on its delivery runtime. - target: x86_64-apple-darwin - runs-on: macos-15-intel # macos-13 is retired; this is the Intel runner now + runs-on: macos-15 env: # AppleDouble resource forks must not leak into the archive; the # assembler additionally enforces plain-file payloads. @@ -244,15 +257,20 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@e081816240890017053eacbb1bdf337761dc5582 # 1.95.0 action pin; toolchain pinned below + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e with: - toolchain: "1.98.1" targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: key: ${{ matrix.target }} + # Only the shipped binary links here: the job packages `prime-agent` + # alone, so pa-daemon's [[bin]] would be a second thin-LTO link with + # no consumer (~26% of the build step, measured). - name: Build (release, locked) - run: cargo build --release --locked --target ${{ matrix.target }} -j 4 + run: cargo build --release --locked --target ${{ matrix.target }} -j 4 -p pa-cli --bin prime-agent - name: Generate bundled catalog assets (fixture, offline-safe) run: | set -euo pipefail @@ -272,7 +290,13 @@ jobs: --catalog-assets "target/${{ matrix.target }}/catalog-assets" \ --out-dir "target/${{ matrix.target }}/dist" cat "target/${{ matrix.target }}/dist/manifest.json" - - name: Livecheck the assembled continuous artifact on its native runner + # The livecheck extracts the tarball and runs the payload binary's + # `--version` from a scratch cwd (verify_release.py). The aarch64 leg + # executes it natively; the x86_64 leg, cross-compiled on the + # Apple-silicon pool, executes it under Rosetta 2 — the runtime the + # x64 payload runs on for an Apple-Silicon Mac customer, so the gate + # still boots the artifact on its delivery runtime. + - name: Livecheck the assembled continuous artifact on its build runner run: | set -euo pipefail version="$(sed -n 's/^version *= *"\([^"]*\)".*/\1/p' Cargo.toml | head -1)" diff --git a/.github/workflows/contribution-gate.yml b/.github/workflows/contribution-gate.yml new file mode 100644 index 00000000000..0661b446c93 --- /dev/null +++ b/.github/workflows/contribution-gate.yml @@ -0,0 +1,40 @@ +name: Contribution gate + +on: + issues: + types: [opened, reopened] + pull_request_target: + types: [opened, reopened, ready_for_review] + +permissions: {} + +jobs: + issue: + name: Check issue author + if: github.event_name == 'issues' + runs-on: ubuntu-latest + permissions: + contents: read + issues: write + steps: + - uses: mitchellh/vouch/action/check-issue@d66fa29a64600490892131ad87597c30c91fcac4 # v1 + with: + issue-number: ${{ github.event.issue.number }} + auto-close: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + pull-request: + name: Check pull request author + if: github.event_name == 'pull_request_target' + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + steps: + - uses: mitchellh/vouch/action/check-pr@d66fa29a64600490892131ad87597c30c91fcac4 # v1 + with: + pr-number: ${{ github.event.pull_request.number }} + auto-close: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 669840d2de2..e3eca687a45 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -7,14 +7,14 @@ # # CADENCE (the operator's ruling: the beta channel follows EVERY green # tip - 'nightly' meant the beta release, and the beta updates on every -# commit, not once a night): every push to `rust` fires a cut for that +# commit, not once a night): every push to `main` fires a cut for that # commit. The cut WAITS for the pushed commit's `continuous` run to # conclude green (the proven-built evidence: the tip CI must have greened # on that commit) before tagging. The SUPERSEDE cost-guard keeps busy days # cheap: this job's concurrency group cancels the in-flight WAIT when a # newer push lands (nothing has been tagged during the wait), and # release.yml's beta publish refuses to move the channel pointers when a -# newer commit exists on rust - so only the LATEST green commit ever +# newer commit exists on main - so only the LATEST green commit ever # advances the channel, and 20 merges cost at most a handful of actual # beta cuts, each the freshest green. # @@ -45,8 +45,9 @@ # event, so release.yml would never run. The App's ref creation is the # push event that triggers release.yml's -beta* channel: build -> # promote (beta.json, a PRERELEASE - never takes GitHub's Latest -# pointer; the release environment approval is the publish gate) -> -# nightly-refresh rolls the rolling `nightly` release. +# pointer; beta tags publish automatically from the release-beta +# environment, no approval) -> nightly-refresh rolls the rolling +# `nightly` release. # # Idempotency/counter safety: if the computed tag already exists, the cut # skips loudly. That covers both "nothing new to ship" and "the previous @@ -57,12 +58,12 @@ # # Not built (deliberate, per the proposal): a prune job for accumulated # -beta* releases (deferred until asset count matters), and any content -# change to the branch (the tag-only commit never lands on rust). +# change to the branch (the tag-only commit never lands on main). name: nightly on: - # The rolling beta (the operator's ruling): every push to `rust` fires a + # The rolling beta (the operator's ruling): every push to `main` fires a # cut for that commit; the cut waits out the pushed commit's continuous # CI before tagging (the proven-built evidence). push: @@ -177,7 +178,7 @@ jobs: fi echo "rolling beta parent: ${target_sha} (continuous run ${run_id}: all ${total} build jobs green)" else - # The dispatch walk: the newest continuous run on rust whose + # The dispatch walk: the newest continuous run on main whose # BUILD actually ran green, walking the successful runs newest # first and accepting the first whose build-* jobs all # completed green. @@ -191,6 +192,8 @@ jobs: exit 1 fi while IFS="$(printf '\t')" read -r run_id candidate_sha; do + # the continue is the intended else arm + # shellcheck disable=SC2015 [ -n "${run_id}" ] && [ -n "${candidate_sha}" ] || continue jobs_json="$(gh api "repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}/jobs" 2>/dev/null || echo '{"jobs":[]}')" read -r built total < <(printf '%s\n' "${jobs_json}" | jq -r \ @@ -363,7 +366,7 @@ jobs: (out / "commit.json").write_text(json.dumps({ "message": f"nightly: {tag} (tag-only: the branch is never touched)\n\n" f"The parent {target_sha} is the newest successful continuous build " - f"on rust; this tree differs from it only by the workspace version " + f"on main; this tree differs from it only by the workspace version " f"rows (Cargo.toml + Cargo.lock). Cut by the nightly workflow; no " f"branch head references this commit.", "tree": "", @@ -373,7 +376,7 @@ jobs: "tag": tag, "message": f"Nightly build {tag}\n\n" f"Built from the continuous train at parent {target_sha} " - f"(the newest successful continuous build on rust) via the nightly " + f"(the newest successful continuous build on main) via the nightly " f"workflow's tag-only commit. The -beta* tag publishes the nightly " f"channel (beta.json) through the release.yml promote gate.", "object": "", @@ -442,4 +445,4 @@ jobs: tag="$(jq -r .tag "${out}/meta.json")" gh api -X POST "${repo}/git/refs" --input "${out}/ref.json" echo "annotated tag ${tag} cut at the tag-only commit ${commit_sha} (parent $(jq -r .parent "${out}/meta.json"))" - echo "release.yml (tag-triggered, -beta* channel) takes over: build -> promote (beta.json + prerelease; the release environment approval is the publish gate) -> nightly-refresh rolls the nightly release" + echo "release.yml (tag-triggered, -beta* channel) takes over: build -> promote (beta.json + prerelease; publishes automatically, no approval) -> nightly-refresh rolls the nightly release" diff --git a/.github/workflows/release-prepare.yml b/.github/workflows/release-prepare.yml index de2ea7f3d05..3b424f619f5 100644 --- a/.github/workflows/release-prepare.yml +++ b/.github/workflows/release-prepare.yml @@ -35,12 +35,9 @@ # prepare act fails with a config pointer - and the tag act is unreachable # by construction (no release PR can be opened without the App). # -# Ref-agnostic by construction: workflow_dispatch registers only from the -# default branch (verified on this repo: the rust-only -# windows-runtime-triage.yml dispatch 404s), so the operator mirrors this -# file to `main` as one config act; both acts resolve the rust line -# explicitly (the prepare job checks out the `rust` branch, the tag job reads -# the merge commit) and never read the ref the workflow ran on. +# Ref-agnostic by construction: both acts resolve `main` explicitly (the +# prepare job checks out `main`, the tag job reads the merge commit) and +# never read the ref the workflow ran on. name: release-prepare @@ -94,18 +91,19 @@ jobs: # Setting any permission input narrows the token to exactly these. permission-contents: write permission-pull-requests: write - - name: "Check out the rust line (full history: the fold sorts fragments by add-date)" + - name: "Check out main (full history: the fold sorts fragments by add-date)" uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: ref: main - # The rust branch clones at ~94MB (measured 2026-09-29); a release - # is rare, and the fold's add-date ordering reads fragment - # add-commits anywhere in that history. + # A full-history clone is large (the pre-merge rust branch measured + # ~94MB on 2026-09-29); a release is rare, and the fold's add-date + # ordering reads fragment add-commits anywhere in that history. fetch-depth: 0 persist-credentials: false - - uses: dtolnay/rust-toolchain@e081816240890017053eacbb1bdf337761dc5582 # 1.95.0 action pin; toolchain pinned below - with: - toolchain: "1.98.1" + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e - name: Guard the lineage (refuse an existing tag or a below-tip version) env: INPUT_VERSION: ${{ inputs.version }} @@ -158,7 +156,7 @@ jobs: | sort > "$RUNNER_TEMP/release-fragments.txt" || true python3 scripts/release/fold_changelog.py --version "${INPUT_VERSION}" git add -A CHANGELOG.md Cargo.toml Cargo.lock - - name: Commit the release branch (release/vX.Y.Z off the rust tip) + - name: Commit the release branch (release/vX.Y.Z off the main tip) env: INPUT_VERSION: ${{ inputs.version }} run: | @@ -167,7 +165,7 @@ jobs: git config user.email "41898282+github-actions[bot]@users.noreply.github.com" base_sha="$(git rev-parse HEAD)" git checkout -q -b "release/v${INPUT_VERSION}" - git commit -q -m "release: v${INPUT_VERSION}" -m "Version bump + the .changes/ fragment fold, cut by the release-prepare workflow off the rust tip ${base_sha}. Merging this PR ships v${INPUT_VERSION}: the tag act creates the annotated tag at this PR's squash-merge commit, and the tag-triggered release.yml runs the promote gate (the protected release environment)." + git commit -q -m "release: v${INPUT_VERSION}" -m "Version bump + the .changes/ fragment fold, cut by the release-prepare workflow off the main tip ${base_sha}. Merging this PR ships v${INPUT_VERSION}: the tag act creates the annotated tag at this PR's squash-merge commit, and the tag-triggered release.yml runs the promote gate (the protected release environment)." echo "release/v${INPUT_VERSION} at $(git rev-parse HEAD) (base ${base_sha})" - name: Push the release branch (the App token, one-shot credential helper) env: @@ -235,8 +233,9 @@ jobs: PR's squash-merge commit via the GitHub API (the release-bot App; no user PAT, no local tags). 2. `release.yml` (tag-triggered, unchanged) validates tag == Cargo.toml, - builds the 4-target matrix, and `promote` waits for the protected - `release` environment approval - **the approval is the publish**. + builds the 5-target matrix (the windows MSVC build included), and + `promote` waits for the protected `release` environment approval - + **the approval is the publish**. 3. On approval: assets + SHA256SUMS + SBOMs + SLSA provenance + `latest.json` land on the `{tag}` GitHub release; the in-app updater (`latest.json`) picks it up, and the curl install lands with the @@ -251,9 +250,6 @@ jobs: and named humans may create/move tags; manual/local tags are refused. - [ ] The `release` environment approvers (Settings - Environments - release): the promotion authority for the promote gate. - - [ ] The `main` mirror for `release-prepare.yml` + `nightly.yml` (the - default branch registers schedule/dispatch triggers; both workflows - resolve the rust line explicitly, so the same bytes are safe there). _No tag is cut by opening or reviewing this PR; the tag act runs only after the merge, and the publish itself is gated behind the environment approval. diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 86bf9a05532..5582d195267 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,7 +1,7 @@ # Release pipeline for Prime Agent (Rust). # # THE TWO BUILD ROUTES (one promote gate): -# - a plain vX.Y.Z tag (stable) BUILDS its own 4-target matrix: the stable +# - a plain vX.Y.Z tag (stable) BUILDS its own 5-target matrix: the stable # cadence is rare, and its cut must not depend on a continuous run the # staleness gate may have skipped (a superseded push builds nothing) or # artifact retention may have evicted; @@ -19,6 +19,8 @@ # green gates on that commit). Both routes upload the same # artifacts- set into this run, so the promote gate consumes # one shape from either route. +# - the Windows target (build-windows) BUILDS on both routes: continuous +# builds no Windows target, so a beta has no win32 artifact to consume. # name: release @@ -57,9 +59,10 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@e081816240890017053eacbb1bdf337761dc5582 # 1.95.0 action pin; toolchain pinned below - with: - toolchain: "1.98.1" + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e - name: Validate tag matches workspace version run: | set -euo pipefail @@ -107,14 +110,16 @@ jobs: strategy: fail-fast: false matrix: + # Windows cannot ride this matrix: the job-level `container:` pins the + # linux builds to the reviewed ubuntu:22.04 image (the digest-pinned + # glibc baseline), and a Windows runner cannot host that container. + # The MSVC target builds in its own `build-windows` job below (the + # build-darwin pattern). include: - target: x86_64-unknown-linux-gnu runs-on: ubuntu-24.04 - target: aarch64-unknown-linux-gnu runs-on: ubuntu-24.04-arm - # Windows ships when the platform trait impls land: - # - target: x86_64-pc-windows-msvc - # runs-on: windows-2022 env: # AppleDouble resource forks must not leak into the archive; the # assembler additionally enforces plain-file payloads. @@ -132,9 +137,11 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@e081816240890017053eacbb1bdf337761dc5582 # 1.95.0 action pin; toolchain pinned below + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e with: - toolchain: "1.98.1" targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: @@ -145,8 +152,11 @@ jobs: # the new key builds cold; the marker moves only with reviewed # baseline changes (same protocol as the image digest below). key: ${{ matrix.target }}-glibc235 + # Only the shipped binary links here: the job packages `prime-agent` + # alone, so pa-daemon's [[bin]] would be a second thin-LTO link with + # no consumer (~26% of the build step, measured). - name: Build (release, locked) - run: cargo build --release --locked --target ${{ matrix.target }} -j 4 + run: cargo build --release --locked --target ${{ matrix.target }} -j 4 -p pa-cli --bin prime-agent - name: Split the shipped binary + split-debug decoder (Option C posture) # Linux only: the build emits line tables and does not strip at # link; this step produces the SHIPPED binary (objcopy @@ -234,8 +244,17 @@ jobs: include: - target: aarch64-apple-darwin runs-on: macos-14 + # The x86_64 leg cross-compiles on the Apple-silicon pool: rustc + # and the Xcode linker build either darwin arch from an M-series + # host, and the M-pool links this binary ~2.2x faster than the + # macos-15-intel pool with far tighter variance (484 s vs 1046 s + # same-commit link tail, 2026-09-30). The livecheck below runs + # the assembled x64 artifact under Rosetta 2 — the macos-15 + # arm64 image installs it, and install-rust.sh ships the x64 + # build to an Apple-Silicon Mac's Rosetta shell, so the gate + # still boots the binary on its delivery runtime. - target: x86_64-apple-darwin - runs-on: macos-15-intel # macos-13 is retired; this is the Intel runner now + runs-on: macos-15 env: # AppleDouble resource forks must not leak into the archive; the # assembler additionally enforces plain-file payloads. @@ -244,15 +263,24 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@e081816240890017053eacbb1bdf337761dc5582 # 1.95.0 action pin; toolchain pinned below + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e with: - toolchain: "1.98.1" targets: ${{ matrix.target }} - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: key: ${{ matrix.target }} + # Only the shipped binary links here: the job packages `prime-agent` + # alone, so pa-daemon's [[bin]] would be a second thin-LTO link with + # no consumer (~26% of the build step, measured). - name: Build (release, locked) - run: cargo build --release --locked --target ${{ matrix.target }} -j 4 + run: cargo build --release --locked --target ${{ matrix.target }} -j 4 -p pa-cli --bin prime-agent + # The aarch64 leg livechecks natively; the x86_64 leg, + # cross-compiled on the Apple-silicon pool, livechecks under + # Rosetta 2 — the runtime install-rust.sh gives the x64 payload on + # an Apple-Silicon Mac. - name: "Livecheck gate: --version prints the tag version" run: | set -euo pipefail @@ -285,6 +313,89 @@ jobs: target/${{ matrix.target }}/dist/manifest.json if-no-files-found: error + # The Windows build: its own job (the build-darwin pattern), because the + # GNU builds pin their glibc baseline inside the digest-reviewed ubuntu:22.04 + # container - a runner-level image a windows runner cannot host. The runner + # label is pinned to windows-2022 (never the mutable windows-latest) for the + # same reviewed-baseline reason as the container digest: a label that + # drifts under promote would drift the release baseline; it moves only + # through reviewed changes. + # + # BOTH ROUTES build it: the continuous workflow builds no Windows target, + # so a -beta* tag has no continuous win32 artifact to consume - this job + # builds the tag commit itself on either route and uploads the same + # artifacts- shape into this run. + build-windows: + needs: tag-check + name: build-${{ matrix.target }} + runs-on: ${{ matrix.runs-on }} + permissions: + contents: read + timeout-minutes: 90 + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-pc-windows-msvc + runs-on: windows-2022 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + # The pinned SHA is the toolchain pin: this action version has no + # `toolchain` input, and its parse step hard-codes 1.98.1. Change the + # compiler by changing the SHA; a `toolchain:` input is ignored. + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e + with: + targets: ${{ matrix.target }} + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 + with: + key: ${{ matrix.target }} + # The Windows runner images ship `python` (python3 is not a stable alias + # there); the release scripts run under whichever spelling exists. + - name: Resolve the Python interpreter + shell: bash + run: echo "PY=$(command -v python3 || command -v python)" >> "$GITHUB_ENV" + # Only the shipped binary links here (the build-gnu/build-darwin rule). + - name: Build (release, locked) + run: cargo build --release --locked --target ${{ matrix.target }} -j 4 -p pa-cli --bin prime-agent + - name: "Livecheck gate: --version prints the tag version" + shell: bash + run: | + set -euo pipefail + # Cargo's MSVC linker emits prime-agent.exe; the payload name the + # assembler stages and the installer extracts. + bin="target/${{ matrix.target }}/release/prime-agent.exe" + out="$("${bin}" --version)" + [[ "${out}" == "${RELEASE_VERSION#v}" ]] || { echo "binary reports '${out}', expected '${RELEASE_VERSION#v}'"; exit 1; } + - name: Generate bundled catalog assets (fixture, offline-safe) + shell: bash + run: | + set -euo pipefail + "${PY}" scripts/release/bundle_catalog.py generate --fixture \ + --out "target/${{ matrix.target }}/catalog-assets" + - name: Assemble tarball + checksums + manifest + shell: bash + run: | + set -euo pipefail + "${PY}" scripts/release/assemble_artifacts.py \ + --repo-root . \ + --version "${RELEASE_VERSION#v}" \ + --target "${{ matrix.target }}" \ + --binary "target/${{ matrix.target }}/release/prime-agent.exe" \ + --catalog-assets "target/${{ matrix.target }}/catalog-assets" \ + --out-dir "target/${{ matrix.target }}/dist" + cat "target/${{ matrix.target }}/dist/manifest.json" + - name: Upload artifacts + uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 + with: + name: artifacts-${{ matrix.target }} + path: | + target/${{ matrix.target }}/dist/prime-agent-* + target/${{ matrix.target }}/dist/SHA256SUMS + target/${{ matrix.target }}/dist/manifest.json + if-no-files-found: error + # THE BETA CHANNEL'S BUILD (the consume route): the continuous workflow # already built the exact commit this beta names - nightly.yml waits out # the pushed commit's continuous run (its whole build matrix green) before @@ -407,17 +518,23 @@ jobs: # promote on BOTH routes - each route deliberately skips the other two # legs. The expression admits exactly the two healthy routes and nothing # else: (stable) both build jobs succeeded, or (beta) the consume route + # succeeded - and on both, build-windows (it runs on either route) # succeeded; a failed leg, a cancelled run, or a failed tag-check leaves # promote skipped, matching the pre-consume failure semantics. The # artifact completeness assertion inside the gate is the belt behind the # routing: an incomplete set fails the promotion loudly either way. - if: ${{ !cancelled() && ( + if: ${{ !cancelled() && needs.build-windows.result == 'success' && ( (needs.build-gnu.result == 'success' && needs.build-darwin.result == 'success') || needs.reuse-continuous.result == 'success') }} - needs: [build-gnu, build-darwin, reuse-continuous] + needs: [build-gnu, build-darwin, build-windows, reuse-continuous] name: promotion gate -> release attach runs-on: ubuntu-latest - environment: release + # The channel gates differ: the stable channel publishes from the protected + # `release` environment (the required-reviewer approval is the ship gate); + # the nightly/beta channel publishes from `release-beta` (no required + # reviewers - every green merge rolls the beta automatically, the rolling + # channel by design; the stable cut stays the human-approved act). + environment: ${{ contains(github.ref_name, '-beta') && 'release-beta' || 'release' }} permissions: contents: write id-token: write @@ -485,6 +602,31 @@ jobs: sys.exit("hash continuity failed:\n" + "\n".join(failures)) print(f"hash continuity verified for all archives") PY + # Prime Agent 0.9.8 (the TypeScript release) updates itself with its + # bundled installer, which installs any archive that carries install.sh, + # theme/prime.json, export-html/template.html and photon_rs_bg.wasm. + # The Rust archive lacks all four, and that is the only thing stopping a + # TS client from installing a Rust release into the TS layout (which + # breaks it). TS needs all four, so refusing a root-level install.sh + # keeps every Rust archive uninstallable by the TS updater. + - name: Refuse archives the TypeScript updater would install + run: | + set -euo pipefail + python3 - <<'PY' + import pathlib, sys, tarfile + incoming = pathlib.Path("incoming") + failures = [] + for entry_dir in sorted(p for p in incoming.iterdir() if p.is_dir()): + for archive in sorted(entry_dir.glob("prime-agent-*.tar.gz")): + with tarfile.open(archive) as tar: + names = {pathlib.PurePosixPath(m.name).parts for m in tar.getmembers()} + if ("install.sh",) in names: + failures.append(f"{archive.name}: contains a root-level install.sh") + if failures: + sys.exit("a TypeScript Prime Agent updater could install these archives:\n" + + "\n".join(failures)) + print("no archive carries the TypeScript installer layout") + PY - name: Verify Linux decoder integrity against installed tar ELF run: | set -euo pipefail @@ -616,6 +758,21 @@ jobs: rows.append( {"platform": platform, "file": file_name, "sha256": sha} ) + # COMPLETENESS: the channel's platform list must match the built set. + # The reader serves every known platform; a manifest that silently + # dropped one (a skipped matrix leg, a build-job regression) would + # strand that platform's installs on the old channel version, so a + # missing row fails the release here instead of publishing a + # shrunken channel. `promote` needs every build job, so the merged + # manifest carries every known platform by construction; this gate + # turns that invariant into an enforced contract. + missing = known - seen + if missing: + sys.exit( + f"the release is missing artifact rows for {sorted(missing)}: " + "the channel manifest must carry a row for every known " + "platform (one build job per platform)" + ) document = {"version": f"v{version}"} v1_rows = [row for row in rows if row["platform"] in v1_platforms] if v1_rows: @@ -627,35 +784,41 @@ jobs: print(f"channel manifest {out.name}: {document['version']}, {len(rows)} artifacts") PY # THE R2 PUBLISH PHASE (the operator's distribution-model ruling: the - # user path is curl /install.sh and everything it reads comes - # from the same R2-backed base — nobody fetches a GitHub URL). + # channel — pointers, manifests, payloads, checksums — lives on the + # R2-backed base alone. The sh one-liner's route carries no GitHub + # URL; the Windows one-liner's entry script comes from the repo's raw + # copy, and that fetch is the only GitHub touch on any user path — + # everything either installer reads is R2-backed). # # THE SECURITY MODEL, in one block for every future reader: # - SEPARATION: the build jobs (build-gnu/build-darwin) produce and - # attest the artifacts and hold NO R2 credentials; this promote job - # — the protected 'release' environment, gated by the operator's - # approval click — is the ONLY job that ever holds the R2 secrets. - # The build is not the release: the approval gate IS the release - # decision, and every s3 cp below runs only after it. + # attest the artifacts and never reference the R2 secrets; this + # promote job is the only job that does. The R2 secrets are + # repo-level (neither environment holds any), so the environment + # is not a credential boundary. Stable tags run in the 'release' + # environment, whose required-reviewer approval is a process gate: + # a stable s3 cp runs only after a human approves. Beta tags run in + # 'release-beta' (no reviewers) and publish automatically once the + # builds pass. # - CHECKSUM FIRST: sha256sum --check SHA256SUMS passes before any # upload — a corrupt or mismatched artifact never reaches the # bucket. # - HEADER FORM (TS parity): the version-prefixed artifacts # (releases/v/) are immutable (1y) with their content # types; the root pointers (latest.json, stable, beta, beta.json, - # install.sh, install-beta.sh) are no-cache — the user path is - # always fresh. + # install.sh, install-beta.sh, install.ps1, install-beta.ps1) are + # no-cache — the user path is always fresh. # - NO PATS, NO LEAKS: the GitHub side uses the workflow's own - # identity (github.token); the R2 keys are environment secrets - # scoped to this job; nothing is echoed. + # identity (github.token); the R2 keys are repo-level secrets + # read only by this job; nothing is echoed. # - BETA SUPERSEDE: only the LATEST green commit gets a beta cut; - # a newer rust push landing while this beta built means a newer + # a newer main push landing while this beta built means a newer # cut is coming — this publish keeps its versioned artifacts but # skips the beta channel pointers (beta.json/beta/install-beta.sh), # so the channel only ever advances forward. # - AUDIT SURFACE: the GitHub release below still attaches every # artifact (the TS order: the R2 publish first, GitHub second). - - name: Publish the R2 channel (the user path never touches GitHub) + - name: Publish the R2 channel (the channel serves no GitHub URL) env: AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} @@ -688,12 +851,14 @@ jobs: --content-type text/plain \ --cache-control 'public, max-age=31536000, immutable' - # The rendered installer pair: the repo file's defaults ARE the + # The rendered installer pairs: the repo files' defaults ARE the # official domain, so the publish render only stamps the build's - # R2 base + channel default into the served copies. Both renders + # R2 base + channel default into the served copies. All renders # publish on every cut (TS parity: a changed installer ships with # whichever channel publishes, so the served scripts never go - # stale between releases). + # stale between releases) — the sh pair (install.sh / + # install-beta.sh) and the Windows PowerShell pair (install.ps1 / + # install-beta.ps1). render_installer() { channel="$1" sed -e "s|^DOWNLOAD_BASE_URL_DEFAULT=.*|DOWNLOAD_BASE_URL_DEFAULT=\"${INSTALL_BASE_URL}\"|" \ @@ -703,6 +868,20 @@ jobs: grep -q "^RELEASE_CHANNEL_DEFAULT=${channel}$" "/tmp/install-${channel}.sh" } + # The Windows-native render of the same installer (install.ps1): + # the same base + channel stamps, the same publish-every-cut rule. + # The Windows-idiomatic entry is + # irm /install.ps1 | iex + # while Git-Bash users run the install-rust.sh form above. + render_installer_ps1() { + channel="$1" + sed -e "s|^\$DownloadBaseUrlDefault = '.*'|\$DownloadBaseUrlDefault = '${INSTALL_BASE_URL}'|" \ + -e "s|^\$ReleaseChannelDefault = '.*'|\$ReleaseChannelDefault = '${channel}'|" \ + "verification-source/install.ps1" > "/tmp/install-${channel}.ps1" + grep -qF "\$DownloadBaseUrlDefault = '${INSTALL_BASE_URL}'" "/tmp/install-${channel}.ps1" + grep -qF "\$ReleaseChannelDefault = '${channel}'" "/tmp/install-${channel}.ps1" + } + # THE ANNOTATED-TAG PEEL: the nightly cuts create ANNOTATED tags, # so GITHUB_SHA is the tag OBJECT, not the commit — resolve the # commit (and its parent) through the ref chain instead of @@ -799,7 +978,7 @@ jobs: if [[ "$RELEASE_VERSION" == *-beta* ]]; then # THE BETA CHANNEL: the rolling per-push cut. The supersede # rule — only the newest proven commit advances the channel: a - # newer rust push landing while this beta built means a newer + # newer main push landing while this beta built means a newer # cut is already coming; this one keeps its versioned # artifacts (immutable, still served to pinned installs) but # leaves the channel pointers alone. @@ -817,10 +996,20 @@ jobs: --endpoint-url "$R2_ENDPOINT_URL" \ --content-type text/x-shellscript \ --cache-control no-cache - # The stable installer (install.sh) moves ONLY on a stable - # release — a beta cut never touches the stable channel's - # surface (the TS version serves installs until the first - # Rust stable release replaces it). + render_installer_ps1 beta + aws s3 cp /tmp/install-beta.ps1 "s3://${R2_BUCKET}/install-beta.ps1" \ + --endpoint-url "$R2_ENDPOINT_URL" \ + --content-type text/plain \ + --cache-control no-cache + # The beta route also uploads the stable PowerShell render + # (install.ps1, the Windows entry point — the installer's + # Windows channel fallback rides the stable channel default). + # install.sh stays stable-only; a beta cut never overwrites it. + render_installer_ps1 stable + aws s3 cp /tmp/install-stable.ps1 "s3://${R2_BUCKET}/install.ps1" \ + --endpoint-url "$R2_ENDPOINT_URL" \ + --content-type text/plain \ + --cache-control no-cache echo "beta channel advanced to ${RELEASE_VERSION} at ${INSTALL_BASE_URL}" fi fi @@ -838,11 +1027,21 @@ jobs: --endpoint-url "$R2_ENDPOINT_URL" \ --content-type text/x-shellscript \ --cache-control no-cache + render_installer_ps1 stable + aws s3 cp /tmp/install-stable.ps1 "s3://${R2_BUCKET}/install.ps1" \ + --endpoint-url "$R2_ENDPOINT_URL" \ + --content-type text/plain \ + --cache-control no-cache render_installer beta aws s3 cp /tmp/install-beta.sh "s3://${R2_BUCKET}/install-beta.sh" \ --endpoint-url "$R2_ENDPOINT_URL" \ --content-type text/x-shellscript \ --cache-control no-cache + render_installer_ps1 beta + aws s3 cp /tmp/install-beta.ps1 "s3://${R2_BUCKET}/install-beta.ps1" \ + --endpoint-url "$R2_ENDPOINT_URL" \ + --content-type text/plain \ + --cache-control no-cache echo "stable channel advanced to ${RELEASE_VERSION} at ${INSTALL_BASE_URL}" fi fi @@ -904,7 +1103,21 @@ jobs: # refuses to clobber a newer rolling beta.json (re-runs of older tags). nightly-refresh: needs: promote - if: contains(github.ref_name, '-') + # THE GATE IS EXPLICIT (the promote precedent: an `if` without a status + # function is auto-prefixed with success(), and GitHub's default needs + # gate skips every job downstream of a skipped job in the dependency + # chain). On a -beta* tag the two stable-route build jobs skip themselves + # by their own gates; they are promote's needs and therefore this job's + # TRANSITIVE needs, so the bare `if: contains(github.ref_name, '-')` that + # lived here skipped the refresh on every + # green beta promote since the flow went live - the rolling `nightly` + # release never materialized (releases/tags/nightly 404s) while the + # per-tag prereleases and the R2 beta.json published and the payload + # uploaded inside the same green promote. The explicit form admits + # exactly the one healthy state: a green promote - the only producer of + # the nightly-refresh-payload artifact - on a -beta* tag. A failed, + # skipped, or cancelled promote leaves the refresh skipped. + if: ${{ !cancelled() && needs.promote.result == 'success' && contains(github.ref_name, '-') }} name: rolling nightly release refresh (the nightly channel's discoverable base) concurrency: group: rolling-nightly-refresh @@ -921,6 +1134,13 @@ jobs: - name: Refresh the rolling nightly release env: GH_TOKEN: ${{ github.token }} + # gh resolves the repo from the git remote - absent: this job never + # checks out - or GH_REPO, and never from GITHUB_REPOSITORY, so + # without this row every `gh release ...` call below dies with + # "failed to run git: not a git repository" (probe-pinned: the gate + # fix alone let the job run and its first gh call failed exactly + # so). + GH_REPO: ${{ github.repository }} run: | set -euo pipefail # The rolling update channel distributes tarballs only. Per-tag diff --git a/.github/workflows/windows-runtime-triage.yml b/.github/workflows/windows-runtime-triage.yml index 5d8fabac7eb..56ec0b5e3f1 100644 --- a/.github/workflows/windows-runtime-triage.yml +++ b/.github/workflows/windows-runtime-triage.yml @@ -31,15 +31,17 @@ jobs: windows-battery: name: windows battery (on-demand) runs-on: windows-latest - timeout-minutes: 120 + # GitHub-hosted jobs stop at six hours whatever this says, so 360 is + # the real maximum. The full pa-cli e2e set alone runs past it on this + # runner (measured on four dispatches), which is why the install e2e + # runs before the pa-cli battery. + timeout-minutes: 360 steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false ref: ${{ inputs.ref || github.ref }} - - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; toolchain pinned below - with: - toolchain: "1.98.1" + - uses: dtolnay/rust-toolchain@ce678459e9fc7500d337468f904b95f1b5c10b5e # 1.98.1 action pin; the SHA selects the toolchain - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: key: windows-triage @@ -82,6 +84,71 @@ jobs: ForEach-Object { Remove-Item -Path ("Env:" + $_.Name) -ErrorAction SilentlyContinue } cargo test --locked -p pa-tui 2>&1 | Tee-Object -FilePath pa-tui_win.log exit $LASTEXITCODE + # The Windows install gates: both entry points (install.ps1 and + # install-rust.sh under Git Bash) install the REAL assembled + # win32-x64 artifact from a locally served channel and answer + # --version through their launchers (scripts/release/ + # test_windows_install.ps1). This is the shipped-product proof: the + # build's artifact + the installers the channel serves. THE ORDER: + # BEFORE the pa-cli battery - the full pa-cli e2e set runs past + # three hours on this runner (measured twice), so the install proof + # must not sit behind it. + - name: windows install e2e (ps1 + sh routes) + id: install_e2e + continue-on-error: true + shell: pwsh + run: | + pwsh -NoProfile -File scripts/release/test_windows_install.ps1 2>&1 | Tee-Object -FilePath win-install-e2e.log + exit $LASTEXITCODE + # The Windows channel-fallback regression (the local-channel unit test + # for the stable->beta fallback the plain one-liner rides): a stable + # channel without a win32-x64 row falls back to beta with the notice, + # and an explicitly requested channel refuses with the beta route. + - name: windows channel-fallback unit (install.ps1 stable->beta) + id: channel_fallback + continue-on-error: true + shell: pwsh + run: | + pwsh -NoProfile -File scripts/release/test_windows_channel_fallback.ps1 2>&1 | Tee-Object -FilePath win-channel-fallback.log + exit $LASTEXITCODE + # THE RAW ONE-LINER on the real channel, end to end: install.ps1 + # itself from raw.githubusercontent (the ref under test - on main it + # is the README's shipped command), then the pointer, the manifest, + # the win32-x64 tarball, and its sums from the real bucket, the real + # default prefix ($HOME\.local), the real stable->beta Windows + # fallback, and the launcher answering --version. The transcript + # (win-raw-one-liner.log) is the evidence: the install transcript, + # the --version output, and the exit codes. + - name: the raw one-liner on the real channel (irm | iex) + id: raw_one_liner + continue-on-error: true + shell: pwsh + env: + RAW_REF: ${{ inputs.ref || github.ref }} + run: | + Get-ChildItem Env: | Where-Object { $_.Name -like 'PRIME_AGENT_*' } | + ForEach-Object { Remove-Item -Path ("Env:" + $_.Name) -ErrorAction SilentlyContinue } + Start-Transcript -Path win-raw-one-liner.log + $installExit = 0 + try { + irm "https://raw.githubusercontent.com/PrimeIntellect-ai/prime-agent/$env:RAW_REF/install.ps1" | iex + } catch { + $installExit = 1 + Write-Output "RAW_ONE_LINER: the one-liner threw: $($_.Exception.Message)" + } + $launcherExit = 0 + try { + & (Join-Path $HOME '.local\bin\prime-agent.cmd') --version + $launcherExit = $LASTEXITCODE + Write-Output "RAW_ONE_LINER: launcher --version exit $launcherExit" + if ($launcherExit -ne 0) { $installExit = 1 } + } catch { + $installExit = 1 + Write-Output "RAW_ONE_LINER: the launcher did not answer --version: $($_.Exception.Message)" + } + Write-Output "RAW_ONE_LINER: install exit $installExit" + Stop-Transcript + exit $installExit - name: pa-cli battery (unit + the e2e set) id: pa_cli continue-on-error: true @@ -94,10 +161,10 @@ jobs: - name: Summary + the overall verdict shell: pwsh run: | - $summary = "WIN_BATTERY pa-types=${{ steps.pa_types.outcome }} pa-core=${{ steps.pa_core.outcome }} pa-daemon=${{ steps.pa_daemon.outcome }} pa-tui=${{ steps.pa_tui.outcome }} pa-cli=${{ steps.pa_cli.outcome }}" + $summary = "WIN_BATTERY pa-types=${{ steps.pa_types.outcome }} pa-core=${{ steps.pa_core.outcome }} pa-daemon=${{ steps.pa_daemon.outcome }} pa-tui=${{ steps.pa_tui.outcome }} pa-cli=${{ steps.pa_cli.outcome }} install-e2e=${{ steps.install_e2e.outcome }} channel-fallback=${{ steps.channel_fallback.outcome }} raw-one-liner=${{ steps.raw_one_liner.outcome }}" Add-Content -Path win_battery_summary.log -Value $summary Write-Output $summary - if (@("${{ steps.pa_types.outcome }}", "${{ steps.pa_core.outcome }}", "${{ steps.pa_daemon.outcome }}", "${{ steps.pa_tui.outcome }}", "${{ steps.pa_cli.outcome }}") -contains 'failure') { exit 1 } + if (@("${{ steps.pa_types.outcome }}", "${{ steps.pa_core.outcome }}", "${{ steps.pa_daemon.outcome }}", "${{ steps.pa_tui.outcome }}", "${{ steps.pa_cli.outcome }}", "${{ steps.install_e2e.outcome }}", "${{ steps.channel_fallback.outcome }}", "${{ steps.raw_one_liner.outcome }}") -contains 'failure') { exit 1 } - name: Upload the per-crate logs if: always() uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 @@ -109,5 +176,8 @@ jobs: pa-daemon_win.log pa-tui_win.log pa-cli_win.log + win-install-e2e.log + win-channel-fallback.log + win-raw-one-liner.log win_battery_summary.log if-no-files-found: warn diff --git a/AGENTS.md b/AGENTS.md index d6d2b7fe82a..528d5ee61e7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,6 +5,17 @@ Every contributor (human or agent) must read this before working on this repo. ## Repository +- **Never set git identity for commits to this repo's history.** Agents must not set + `user.name`/`user.email` (config or `-c`) or `GIT_AUTHOR_NAME`/`GIT_AUTHOR_EMAIL`/ + `GIT_COMMITTER_NAME`/`GIT_COMMITTER_EMAIL` for commits that land in this repository. + Those commits carry the identity of the ambient git config. If such a commit fails for a + missing identity, report it instead of adding config. Allowed: the `*_DATE` variables + (the golden corpus pins them for reproducibility), throwaway temp repos created by + tests, and the release automation's bot identity. +- **Never add `Co-authored-by` trailers** — not in commit messages, not in squash + suggestions, not as attribution for work merged from branches. A PR's commits carry + one identity: the ambient git config's. Fabricated or hand-written trailers (including + noreply forms) misattribute commits to unrelated GitHub accounts. - The repo is PrimeIntellect-ai/prime-agent; the Rust implementation lives on the `main` branch. - PRs go to the org repo with base `main`: `gh pr create --repo PrimeIntellect-ai/prime-agent --base main`. @@ -72,6 +83,43 @@ Every contributor (human or agent) must read this before working on this repo. - Explain lint exceptions next to the `#[allow(...)]` or workspace configuration; keep exceptions narrow. Advisory ignores in `deny.toml` need a reason and review date. +## Performance and the critical path + +The UI critical path is paint-latency-critical. First paint, and every +frame the user waits on, must never block on network, disk sync, or +background bookkeeping. The launch benchmarks measure this surface +(cold/warm ready, first paint); a merge that regresses them undoes the +product's core claim. + +Rules: + +- Nothing user-visible awaits the network. Any HTTP/socket call on a + render path (telemetry, catalog fetch, update check) runs on a + background worker; the paint path hands the work off and returns + immediately. Delivery semantics (timeout, retry, ordering) live with + the worker, not the caller. +- Telemetry is fire-and-forget from user-facing code. `pa-telemetry`'s + background worker owns delivery end to end; no startup, session + create, tab open, or first-render path may hold a flush or await a + sink. +- Fire-and-forget boundaries need a red-first test: a hanging sink + (one that never replies) must not delay paint. The test fails if any + await on the critical path reaches the worker's delivery. +- Tests that mock a sink or run offline cannot prove the above (the + benchmark's mock hides the POST). The regression test with a hanging + sink is the required verifier. + +Example (the #3288 regression this rule exists to prevent): the +telemetry startup flush was awaited on the interactive launch path, so +the analytics POST — a real HTTPS round-trip, ~157 ms — ran before the +first frame. Every CI check passed because tests set DO_NOT_TRACK=1 and +the benchmark mocked the provider offline; only real-network launches +paid the cost. The fix moved the flush to the background worker and +added `the_startup_flush_never_blocks_the_first_frame` (a hanging sink +must not delay paint) as the permanent guard. Any change touching the +launch path must keep that test red-first. + + ## Merge gates - `cargo fmt --all --check`, `cargo clippy --workspace --all-targets -- -D warnings`, @@ -104,7 +152,8 @@ Every contributor (human or agent) must read this before working on this repo. Every user-visible feature ships its adoption telemetry event in the same PR as the feature: the event name + properties join the versioned schema (`pa-telemetry`), and a seam emits it from day one. Telemetry properties never carry prompt, session, or file content -(primitives only; see `pa-telemetry`). +(primitives only; see `pa-telemetry`); delivery is fire-and-forget from user-facing code +(see Performance and the critical path). ## Branding diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000000..871d6a9ef03 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,43 @@ +# Contributing to Prime Agent + +Thanks for your interest in contributing to Prime Agent! Prime Agent is developed in public, and we welcome bug reports, feature requests, questions, and other feedback. To keep the project maintainable, public contributions begin in [GitHub Discussions](https://github.com/PrimeIntellect-ai/prime-agent/discussions). + +With the influx of agent-generated contributions, we do not review unsolicited pull requests or use public Issues as the initial intake queue. While we are open to contributions by agents, you are responsible for your code and must understand how it interacts with the entire project. + +## Start with a Discussion + +Choose the category that best matches what you want to share: + +- [General discussion or question](https://github.com/PrimeIntellect-ai/prime-agent/discussions/categories/general) +- [Bug report](https://github.com/PrimeIntellect-ai/prime-agent/discussions/categories/bug-reports) +- [Feature request](https://github.com/PrimeIntellect-ai/prime-agent/discussions/categories/feature-requests) + +Search existing Discussions before creating a new one. Include enough detail for someone else to understand and reproduce the problem, but do not share API keys, tokens, private prompts, or other sensitive information. + +For security vulnerabilities, report them privately to the maintainers instead of posting publicly. + +## Issues + +GitHub Issues track work that maintainers have accepted and intend to investigate or implement. A maintainer may create an Issue from a Discussion when the scope is clear and the work fits the roadmap. An existing Issue does not automatically mean that an external pull request is wanted. Wait for a maintainer to invite implementation before starting substantial work. + +Issues opened by unapproved contributors are automatically closed and redirected to Discussions. To contribute, share interest in Discussions or corresponding issues, and maintainers can invite implementation for requested work. + +## Pull Requests and Trusted Contributors + +Prime Agent runs on user machines and can execute code with the user's permissions. We therefore limit pull requests to maintainers and trusted contributors who have been explicitly vouched for. Maintainers may vouch for someone after they have consistently demonstrated a useful understanding of the project through Discussions, issue investigation, testing, documentation, or other collaboration. There is no separate application process and no guarantee that participation will result in approval. + +Pull requests from unvouched contributors are automatically closed. If you are interested in contributing code, begin with a Discussion and work with the maintainers on the problem first. + +## Preparing an Approved Pull Request + +If a maintainer has invited a pull request: + +1. Keep the change focused on the accepted Issue or Discussion. +2. Follow the repository's development rules and existing conventions. +3. Add or update tests for behavioral changes. +4. Run the relevant checks locally and describe the validation in the pull request. +5. Avoid unrelated refactors or dependency changes. + +Development rules, setup, and commands for this branch are documented in [AGENTS.md](AGENTS.md) and the Makefile gate targets (`make check`, `make deny`, `make windows-cross`). + +Maintainers may close a pull request that changes scope, cannot be validated safely, or no longer fits the project roadmap. diff --git a/Cargo.lock b/Cargo.lock index 2940a0a989c..5ec6ea6641b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1336,6 +1336,7 @@ dependencies = [ "pa-models", "pa-telemetry", "pa-types", + "proptest", "serde", "serde_json", "sha2", @@ -1371,6 +1372,7 @@ version = "0.9.8" dependencies = [ "anyhow", "reqwest", + "rustix 1.1.4", "serde", "serde_json", "tempfile", diff --git a/Makefile b/Makefile index 0b87aa7276f..3af0bda89c2 100644 --- a/Makefile +++ b/Makefile @@ -15,18 +15,33 @@ deny: cargo deny --all-features --workspace check advisories licenses # Windows cfg-hygiene gate: cross-target check + -# clippy at -D warnings for every crate and test. Fails loudly when the +# clippy at -D warnings for every crate and test, the local mirror of the +# ci.yml windows-cross job (.github/workflows/ci.yml). Fails loudly when the # target is missing instead of silently skipping the gate. windows-cross: @rustup target list --installed | grep -q x86_64-pc-windows-gnu || { echo "x86_64-pc-windows-gnu target not installed (rustup target add x86_64-pc-windows-gnu)"; exit 1; } cargo check --workspace --target x86_64-pc-windows-gnu --all-targets cargo clippy --workspace --target x86_64-pc-windows-gnu --all-targets -- -D warnings -# Lints the live workflow files (.github/workflows/). +# Windows MSVC cross-check: the release target's own triple (release.yml's +# build-windows job builds x86_64-pc-windows-msvc natively on windows-2022; +# this gate type-checks the same target from a linux host). The check form +# needs MSVC-side C tooling for the native build scripts (ring): cargo-xwin +# wraps the build with the MSVC CRT + clang-cl/llvm-lib, so the gate fails +# loudly when cargo-xwin (or the llvm tools) is missing instead of silently +# skipping. The gnu gate above stays the cheap cfg-hygiene mirror; the +# authoritative msvc build runs on the windows runner. +windows-msvc-cross: + @rustup target list --installed | grep -q x86_64-pc-windows-msvc || { echo "x86_64-pc-windows-msvc target not installed (rustup target add x86_64-pc-windows-msvc)"; exit 1; } + @command -v cargo-xwin >/dev/null 2>&1 || { echo "cargo-xwin not installed (cargo install cargo-xwin --locked; it provisions the MSVC CRT + expects clang-cl/llvm-lib on PATH)"; exit 1; } + cargo xwin check --workspace --target x86_64-pc-windows-msvc --all-targets + +# Lints every workflow file (.github/workflows/ is the one home for +# workflow files). actionlint: @command -v actionlint >/dev/null 2>&1 || { echo "actionlint not installed (see rhysd/actionlint releases)"; exit 1; } - actionlint .github/workflows/ci.yml .github/workflows/continuous.yml \ - .github/workflows/release.yml .github/workflows/windows-runtime-triage.yml \ + actionlint .github/workflows/ci.yml .github/workflows/codebase-health.yml .github/workflows/contribution-gate.yml \ + .github/workflows/continuous.yml .github/workflows/release.yml .github/workflows/windows-runtime-triage.yml \ .github/workflows/release-prepare.yml .github/workflows/nightly.yml # GLIBC baseline gate (the continuous.yml/release.yml build-gnu jobs): a @@ -51,14 +66,6 @@ glibc-gate: fi \ ;; esac -# Perf wave + regression gate (benchmark.yml job, the local mirror): runs the -# TS binary and a fresh release build side by side in a fresh Prime sandbox -# (both sides on one quiet machine, the methodology BENCHMARKS.md requires) -# and gates the rust medians against scripts/battery/perf-baseline.json. -# PA_BENCH_NO_SANDBOX=1 runs it on the bare runner instead. -perf-wave: - scripts/battery/ci_perf_wave.sh - # Local mirror of the release build-job gates: # release build against the committed lockfile, deterministic tarball assembly, # then end-to-end verification of the host-target artifact. The vendored @@ -180,4 +187,4 @@ shard-gates: python3 scripts/test_ci_test_shard.py python3 scripts/test_ci_pr_crates.py -.PHONY: check deny windows-cross actionlint perf-wave glibc-gate release-dry-run continuous-dry-run audit-build package catalog-assets catalog-assets-fixture catalog-assets-gates fold-gates restamp-gates shard-gates +.PHONY: check deny windows-cross actionlint glibc-gate release-dry-run continuous-dry-run audit-build package catalog-assets catalog-assets-fixture catalog-assets-gates fold-gates restamp-gates shard-gates diff --git a/README.md b/README.md index 5add0aef23a..34a2d46718b 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,19 @@ Prime Agent is an open-source coding and research agent for general and long-run ## Install -Install the latest build with the one-command installer (every push to the `rust` branch publishes a fresh rolling beta; the stable channel ships on release): +Install the latest stable version with the one-command installer: ```bash curl -fsSL https://app.primeintellect.ai/prime-agent/install.sh | sh ``` -The served installer and everything it downloads — the version pointers, the platform tarballs, the checksums — come from the same domain: no GitHub URL is on the user path. +On Windows, install from PowerShell: + +```powershell +irm https://raw.githubusercontent.com/PrimeIntellect-ai/prime-agent/main/install.ps1 | iex +``` + +Windows builds ship on the beta channel for now; when the stable channel has no Windows build, the installer falls back to the beta channel and says so. Only this copy of `install.ps1` comes from GitHub — the version pointers, platform tarballs, and checksums it downloads come from the release channel's own base. ## Why Prime Agent @@ -60,6 +66,12 @@ Prime Agent combines a persistent Python control environment with durable harnes - **Agents communicate directly:** running agents can exchange messages and orchestrate one another without routing everything through the user. - **Long tasks keep moving:** automatic compaction, persistent goals, heartbeats, schedules, autonomous mode, and retained subagents preserve progress across turns and terminal sessions. +## Clipboard over SSH and tmux + +Prime Agent uses the local clipboard tool (such as `pbcopy`) when available. In tmux it asks the attached tmux client to forward a clipboard buffer if that client advertises the `Ms` capability. This works with tmux's default `set-clipboard external`, unlike application-origin OSC 52. Without tmux, remote copies request delivery using OSC 52. Terminal requests cannot confirm that your local clipboard changed, so Prime Agent reports them as unconfirmed. + +Nested tmux servers and terminals may still block delivery. An outer tmux with `set-clipboard external` can reject the inner server's OSC 52. In that case, run `tmux set -s set-clipboard on` on the outer server, or use native terminal selection (Shift-drag in most terminals; Option-drag in iTerm2) followed by Cmd-C. macOS Terminal.app does not support OSC 52; iTerm2 requires its “Applications in terminal may access clipboard” setting. + ## Getting Started Start Prime Agent from the repository or directory you want it to work in: diff --git a/RUST-BASE.md b/RUST-BASE.md index e14fd4075d6..2974589a9ac 100644 --- a/RUST-BASE.md +++ b/RUST-BASE.md @@ -16,6 +16,10 @@ The Oneiron fork of prime-agent on the Rust product. Upstream replaced the TypeS into the TS branches. - Toolchain: Rust 1.98.1, the version upstream CI pins (`.github/workflows/ci.yml`). An older stable trips clippy lints that 1.98 does not raise. +- Upstream sync 2026-10-06: merged pinned `main` `672085e883c25a8a5f10c77ad8e5d3fb38dd6584` + (89 upstream-only commits) into fork tip `925ee93ab0fa50eb47067285aade7fcc3722ed59`. + #3197 is now upstream-merged; the still-open #3201 local patch remains. The historical + base above is unchanged; this is a merge, not a rebase or integration-branch force-update. ## Carried upstream patches @@ -41,6 +45,7 @@ when the diff is identical, otherwise drop it by hand. | `--daemon-hosted` | `crates/pa-cli/src/hosted_print.rs`, `crates/pa-daemon/src/headless_client.rs` | The TS fork's resident print/json session (attachable while it runs; detach-only exit). `--offline` and `--no-skills` ride the create as the fork's session policy (schema revision 31, capability `session_policy`, `crates/pa-daemon/src/session_policy.rs`): the offline session's worker starts with `PI_OFFLINE=1` in its own launch env (TS meaning: no startup network operations; not a firewall, and the kernel's environment provisioning is not guarded), `noSkills` turns off skill discovery, an RLM child inherits its parent session's policy, a passivated session wakes under its own (a per-session record beside the worker descriptors), an online-policy worker drops a `PI_OFFLINE` it would inherit from an offline supervisor, and a live session is reused only under the same policy and a tool selection that resolves the same (the refusal names which differs). A daemon without the capability refuses the flags. Still refused: `--no-prompt-templates`, `--no-context-files`, `--goal`, `--fork`, and the factory's `W7_CARGO_*` overlay, the remaining blocker for build-routed daemon seats (the routing also needs the factory's cargo wrapper on the seat's `PATH`, which a shared worker does not take from one client). Wire divergence from TS print: each `prompt_and_wait` carries an `admissionId` (when the daemon advertises `prompt_admission_cancellation`) and a timed-out one is followed by `cancel_prompt_admission`; TS print sends neither because its route never times out. | | A timed-out prompt route keeps its admission | `crates/pa-daemon/src/prompt_admission.rs` | Upstream's 10-minute `prompt_and_wait` route budget is shorter than TS's 24-hour worker forward and deleted the admission with the route, so `cancel_prompt_admission` answered `unknown` for a turn still running. The record now stays until one read has answered for it (or the client's connection closes); `--daemon-hosted` relies on it to tell its own running turn from a queued or lost prompt. No wire field changes. | | A cancelled admission never runs | `crates/pa-daemon/src/worker/turn.rs`, `crates/pa-daemon/src/prompt_admission.rs` | Upstream committed a prompt's admission only inside its turn, after the pickup, so a cancel landing in between (or before the enqueue) answered `cancelled` while the prompt ran. The pickup now commits under the registry lock and drops a cancelled prompt, as TS never runs one; withdrawn admissions are cleared. | +| Mid-stream WebSocket retry | `crates/pa-ai/src/providers/openai_codex_responses/`, `crates/pa-core/src/session_engine/{auto_retry,provider_retry,provider_failover}.rs`, `crates/pa-cli/src/print_retry.rs`, `crates/pa-tui/src/{snapshot,session_ui}/` | A Codex event channel disappearing before completion is a typed transport failure, never a committed partial. Session transport retries use exactly 2000/4000/8000 ms with three retries total, independent of configured provider jitter/quota/failover schedules (the enabled toggle still applies). Failed sockets and continuation anchors are discarded; retry continues committed history/tool results without executing cut calls. Direct print/json composes the same retry driver, and live/reloaded transcripts supersede failed partial text and uncommitted tool cards. Existing retry wire events and outcome rows are reused; no protocol/schema change. | | Headless selection skips `provider_retry_outcome` | `crates/pa-core/src/session_engine/headless.rs` | Upstream's one-row retry disclosure (a sanctioned TS divergence) is persisted after the episode's last assistant and hid it from text-mode selection; TS, which keeps only the per-attempt rows, prints the recovered answer. | | Effective `--no-skills`/`--no-prompt-templates`/`--no-context-files` | `crates/pa-core/src/resources/mod.rs` (`ResourceLoadingPolicy`) | Upstream parses the flags but always discovered every resource; the `sol` wrapper and factory rely on `--no-skills` for prompt size. | | Headless exit follows the answer | `crates/pa-cli/src/headless_exit.rs`, `crates/pa-core/src/kernel/bootstrap/venv/subprocess.rs`, `KernelPrewarm` in `crates/pa-core/src/session_engine/engine.rs` | Upstream's print runtime started the kernel bootstrap in the background and dropped its Tokio runtime implicitly, which waits without bound for the bootstrap's blocking `uv`/interpreter children: a fresh home's first one-shot exited 43 s after its answer. Print/json now set the kernel environment up before the first turn (`KernelPrewarm::BeforeFirstTurn`), run every setup child as an owned, cancellable tokio child, abandon the kernel boot after the last output and shut the runtime down within 300 ms; the hosted client's detach waits the same budget. `--verbose` traces the exit phases. Resident sessions keep `KernelPrewarm::Background`. Either mode runs only when `ipython` is active after the launch tool selection (`--tools`/`--no-tools`/`--no-builtin-tools`). | diff --git a/crates/pa-agent/src/agent.rs b/crates/pa-agent/src/agent.rs index cfde94943c2..a4a3376842d 100644 --- a/crates/pa-agent/src/agent.rs +++ b/crates/pa-agent/src/agent.rs @@ -1075,11 +1075,6 @@ impl Agent { self.inner.follow_up_queue.lock().unwrap().clear(); } - pub fn clear_all_queues(&self) { - self.clear_steering_queue(); - self.clear_follow_up_queue(); - } - /// Previews of the queued steering batches (TS /// `getSteeringMessagePreviews`): one text preview per queued batch, /// in queue order. @@ -1435,6 +1430,7 @@ mod tests { cost: crate::types::UsageCost::default(), context_window: 200_000, max_tokens: 8_192, + max_tokens_explicit: false, } } @@ -1544,6 +1540,7 @@ mod tests { cost: crate::types::UsageCost::default(), context_window: 200_000, max_tokens: 8_192, + max_tokens_explicit: false, } } @@ -1604,6 +1601,7 @@ mod tests { cost: crate::types::UsageCost::default(), context_window: 200_000, max_tokens: 8_192, + max_tokens_explicit: false, } } diff --git a/crates/pa-agent/src/agent_loop/response.rs b/crates/pa-agent/src/agent_loop/response.rs index dd748121a69..1509ae1ecfe 100644 --- a/crates/pa-agent/src/agent_loop/response.rs +++ b/crates/pa-agent/src/agent_loop/response.rs @@ -151,6 +151,7 @@ async fn stream_assistant_response_inner( // composes them per request at the `StreamFn` boundary instead. on_payload: None, on_response: None, + headers: None, }; let mut response = race_with_abort( diff --git a/crates/pa-agent/src/proxy.rs b/crates/pa-agent/src/proxy.rs index 49268a02a39..9e244fd2580 100644 --- a/crates/pa-agent/src/proxy.rs +++ b/crates/pa-agent/src/proxy.rs @@ -230,6 +230,8 @@ struct ProxyRequestOptions { session_id: Option, #[serde(skip_serializing_if = "Option::is_none")] service_tier: Option, + #[serde(skip_serializing_if = "Option::is_none")] + headers: Option>, } #[derive(serde::Serialize)] @@ -267,6 +269,7 @@ async fn proxy_run( }, session_id: options.session_id.clone(), service_tier: options.service_tier, + headers: options.headers.clone(), }; let body = serde_json::json!({ "model": model, @@ -1016,6 +1019,7 @@ mod tests { signal: AbortSignal::never(), on_payload: None, on_response: None, + headers: None, } } @@ -1099,6 +1103,7 @@ mod tests { cost: crate::types::UsageCost::default(), context_window: 128_000, max_tokens: 8_192, + max_tokens_explicit: false, } } @@ -1247,6 +1252,7 @@ mod tests { reasoning: ThinkingLevelWire::High, session_id: Some("session".to_string()), service_tier: Some(ServiceTier::Flex), + headers: Some([("X-ACP-Model-Request-ID".to_string(), "id".to_string())].into()), }; let value = serde_json::to_value(&options).expect("serialize"); let keys: std::collections::BTreeSet = @@ -1254,6 +1260,7 @@ mod tests { assert_eq!( keys, [ + "headers", "maxTokens", "reasoning", "serviceTier", diff --git a/crates/pa-agent/src/stream.rs b/crates/pa-agent/src/stream.rs index 4f50a71abad..17f2a039de0 100644 --- a/crates/pa-agent/src/stream.rs +++ b/crates/pa-agent/src/stream.rs @@ -154,10 +154,10 @@ pub type OnResponseHook = std::sync::Arc, @@ -177,6 +177,9 @@ pub struct StreamRequestOptions { /// Response-headers hook (TS `SimpleStreamOptions.onResponse`). Invoked /// once per request after the response headers arrive. pub on_response: Option, + /// Extra request headers (TS `SimpleStreamOptions.headers`), merged + /// over the provider's auth-resolved headers at the adapter seam. + pub headers: Option>, } impl std::fmt::Debug for StreamRequestOptions { @@ -191,6 +194,7 @@ impl std::fmt::Debug for StreamRequestOptions { .field("signal", &self.signal) .field("on_payload", &self.on_payload.is_some()) .field("on_response", &self.on_response.is_some()) + .field("headers", &self.headers) .finish() } } @@ -207,6 +211,7 @@ impl Default for StreamRequestOptions { signal: crate::abort::AbortSignal::never(), on_payload: None, on_response: None, + headers: None, } } } diff --git a/crates/pa-agent/src/types.rs b/crates/pa-agent/src/types.rs index 50bc6e9bc69..a1c0bd1191d 100644 --- a/crates/pa-agent/src/types.rs +++ b/crates/pa-agent/src/types.rs @@ -446,6 +446,22 @@ pub struct Model { pub context_window: u64, #[serde(rename = "maxTokens", default)] pub max_tokens: u64, + /// Set when `maxTokens` came from explicit configuration: rides the + /// wire-shape round-trips with the pa-ai model so the flag survives + /// the crate boundary and the persisted session state. + #[serde( + rename = "maxTokensExplicit", + default, + skip_serializing_if = "is_false" + )] + pub max_tokens_explicit: bool, +} + +/// `skip_serializing_if` predicate for [`Model::max_tokens_explicit`]: the +/// persisted/proxied JSON stays byte-identical when the flag is unset. +#[allow(clippy::trivially_copy_pass_by_ref)] // serde's skip predicate ABI takes the field by reference +fn is_false(value: &bool) -> bool { + !*value } impl Model { @@ -461,6 +477,7 @@ impl Model { cost: UsageCost::default(), context_window: 0, max_tokens: 0, + max_tokens_explicit: false, } } } diff --git a/crates/pa-agent/tests/agent_loop_scripted.rs b/crates/pa-agent/tests/agent_loop_scripted.rs index f7949ffb38b..cf528a08341 100644 --- a/crates/pa-agent/tests/agent_loop_scripted.rs +++ b/crates/pa-agent/tests/agent_loop_scripted.rs @@ -32,6 +32,7 @@ fn test_model() -> Model { cost: pa_agent::types::UsageCost::default(), context_window: 100_000, max_tokens: 4_096, + max_tokens_explicit: false, } } diff --git a/crates/pa-ai/README.md b/crates/pa-ai/README.md index fad6f27054f..6a04c219301 100644 --- a/crates/pa-ai/README.md +++ b/crates/pa-ai/README.md @@ -16,6 +16,19 @@ owns the session and tells pa-ai when to release it. `cleanup_session_resources(session_id)` (release a session's WebSocket connections and cancel its in-flight requests as disposed), and behind the `test-support` feature `test_support` (the scripted loopback Responses server, for crates that drive the WebSocket transport end to end in their tests). +`utils::stream_failure::ProviderWsTransportError` carries transport classification separately +from diagnostic identity: `sdk_name` preserves an optional provider runtime name, and +`error_name()` borrows it as `&str`. Codex retains its numeric close code; generic Responses +keeps its existing transport diagnostic shape. These are Rust API fields, not new wire fields. +`StreamFailureKind::ConnectionLimit` is a public enum addition, serialized as +`connection_limit`. The generic Responses parser recognizes only the exact structured +`websocket_connection_limit_reached` code, retaining the separate code, supplied status, +and redacted diagnostic. WebSocket failures emit the normal Start/Error lifecycle and +return immediately; pa-ai never replays a cap in the same invocation or falls back to SSE +for that verdict. The session engine owns the bounded refresh and partial rollback. +Generic cached idle sockets rotate on acquisition at 55 minutes; busy responses are never +closed by this age check. Generic pre-event network fallback and Codex SSE behavior stay +unchanged. No helper API or dependency is added. Per-provider internals are `pub(crate)`. ## Depends on diff --git a/crates/pa-ai/src/models.rs b/crates/pa-ai/src/models.rs index 828511e2dc9..413dd94f3fd 100644 --- a/crates/pa-ai/src/models.rs +++ b/crates/pa-ai/src/models.rs @@ -79,6 +79,7 @@ mod tests { }, context_window: 128_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/models_generated.rs b/crates/pa-ai/src/models_generated.rs index d2fdafecc97..9f7b308a4b6 100644 --- a/crates/pa-ai/src/models_generated.rs +++ b/crates/pa-ai/src/models_generated.rs @@ -4436,6 +4436,7 @@ fn model_from_raw(raw: &RawModel) -> Model { }, context_window: raw.context_window, max_tokens: raw.max_tokens, + max_tokens_explicit: false, featured: raw.featured, headers, compat: raw.compat.as_ref().map(compat_from_raw), diff --git a/crates/pa-ai/src/oauth/anthropic.rs b/crates/pa-ai/src/oauth/anthropic.rs index 7a2399d236d..cde1c6dd1be 100644 --- a/crates/pa-ai/src/oauth/anthropic.rs +++ b/crates/pa-ai/src/oauth/anthropic.rs @@ -92,17 +92,29 @@ pub async fn login_anthropic( } let (verifier, challenge) = generate_pkce(); let server = AnthropicCallbackServer::start(&verifier)?; + login_with_server(http, ui, &server, &verifier, &challenge).await +} + +/// The flow after the bind: present the URL, race the callback against +/// the paste, exchange the code. `server` listens with `verifier` as +/// its state. +async fn login_with_server( + http: &dyn ProviderHttp, + ui: &dyn OAuthLoginUi, + server: &AnthropicCallbackServer, + verifier: &str, + challenge: &str, +) -> Result { ui.on_auth( - &authorization_url(&challenge, &verifier), + &authorization_url(challenge, verifier), Some(AUTH_INSTRUCTIONS), ); - - let code = wait_for_code(&server, ui, &verifier).await?; + let code = wait_for_code(server, ui, verifier).await?; if ui.is_cancelled() { return Err(LOGIN_CANCELLED.to_string()); } ui.on_progress("Exchanging authorization code for tokens..."); - exchange_authorization_code(http, &code, &verifier).await + exchange_authorization_code(http, &code, verifier).await } /// Refresh an expired credential (TS `refreshAnthropicToken`). @@ -441,7 +453,6 @@ async fn post_json( #[cfg(test)] mod tests { use super::*; - use crate::oauth::anthropic_callback::CALLBACK_PORT; use std::collections::HashMap; use std::future::Future; use std::pin::Pin; @@ -449,8 +460,6 @@ mod tests { use std::sync::{Arc, Mutex}; use tokio::io::AsyncWriteExt as _; - use super::super::anthropic_callback::{registered_port_stages, CALLBACK_PORT_LOCK}; - /// A scripted transport: url -> response, recording every posted /// body. Unknown urls fail the request (the TS suite throws on /// unexpected fetches). @@ -629,6 +638,18 @@ mod tests { )]) } + /// The flow on a free loopback port (the registered port is `start`'s + /// production bind; `a_bound_port_fails_the_start_clearly` pins it). + async fn login_on_a_free_port( + http: &ScriptedHttp, + ui: &ScriptedUi, + ) -> Result { + let (verifier, challenge) = generate_pkce(); + let server = AnthropicCallbackServer::bind("127.0.0.1", 0, &verifier) + .expect("a free loopback port binds"); + login_with_server(http, ui, &server, &verifier, &challenge).await + } + #[tokio::test] async fn the_authorization_url_carries_the_ts_parameters() { let url = authorization_url("the-challenge", "the-verifier"); @@ -655,13 +676,9 @@ mod tests { #[tokio::test] async fn the_exchange_body_matches_the_ts_grant() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = token_http(); let ui = ScriptedUi::new(Some(ScriptedAnswer::value("the-code")), None); - let credentials = login_anthropic(&http, &ui).await.unwrap(); + let credentials = login_on_a_free_port(&http, &ui).await.unwrap(); assert_eq!(credentials.access, "the-access"); assert_eq!(credentials.refresh, "the-refresh"); // TS: expires = now + expires_in * 1000 - 5 minutes. @@ -693,13 +710,9 @@ mod tests { #[tokio::test] async fn a_failed_exchange_surfaces_the_ts_message() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = ScriptedHttp::new(vec![(TOKEN_URL, 400, "no grant")]); let ui = ScriptedUi::new(Some(ScriptedAnswer::value("the-code")), None); - let error = login_anthropic(&http, &ui).await.unwrap_err(); + let error = login_on_a_free_port(&http, &ui).await.unwrap_err(); assert_eq!( error, format!( @@ -710,13 +723,9 @@ mod tests { #[tokio::test] async fn a_missing_field_exchange_names_the_response() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = ScriptedHttp::new(vec![(TOKEN_URL, 200, r#"{"access_token":"a"}"#)]); let ui = ScriptedUi::new(Some(ScriptedAnswer::value("the-code")), None); - let error = login_anthropic(&http, &ui).await.unwrap_err(); + let error = login_on_a_free_port(&http, &ui).await.unwrap_err(); assert!( error.starts_with("Token exchange response missing fields"), "{error}" @@ -748,10 +757,6 @@ mod tests { #[tokio::test] async fn a_state_mismatch_fails_the_paste() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = token_http(); let ui = ScriptedUi::new( Some(ScriptedAnswer::value( @@ -759,23 +764,19 @@ mod tests { )), None, ); - let error = login_anthropic(&http, &ui).await.unwrap_err(); + let error = login_on_a_free_port(&http, &ui).await.unwrap_err(); assert_eq!(error, "OAuth state mismatch"); assert!(http.requests.lock().unwrap().is_empty()); } #[tokio::test] async fn a_paste_without_a_code_falls_back_to_the_prompt() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = token_http(); let ui = ScriptedUi::new( Some(ScriptedAnswer::value(" ")), Some(ScriptedAnswer::value("the-prompted-code")), ); - login_anthropic(&http, &ui).await.unwrap(); + login_on_a_free_port(&http, &ui).await.unwrap(); let body = http.first_body(TOKEN_URL); let json: serde_json::Value = serde_json::from_str(&body).unwrap(); assert_eq!(json["code"], "the-prompted-code"); @@ -783,43 +784,31 @@ mod tests { #[tokio::test] async fn a_cancelled_paste_ends_the_login() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = token_http(); let ui = ScriptedUi::new(Some(ScriptedAnswer::ready()), None); - let error = login_anthropic(&http, &ui).await.unwrap_err(); + let error = login_on_a_free_port(&http, &ui).await.unwrap_err(); assert_eq!(error, LOGIN_CANCELLED); } #[tokio::test] async fn a_cancelled_prompt_ends_the_login() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = token_http(); let ui = ScriptedUi::new( Some(ScriptedAnswer::value(" ")), Some(ScriptedAnswer::ready()), ); - let error = login_anthropic(&http, &ui).await.unwrap_err(); + let error = login_on_a_free_port(&http, &ui).await.unwrap_err(); assert_eq!(error, LOGIN_CANCELLED); } #[tokio::test] async fn a_cancelled_surface_ends_the_login_between_polls() { - let _port = CALLBACK_PORT_LOCK.lock().await; - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } // The flag flips when the url lands: the race loop's first // poll-step check ends the flow before any code arrives. let http = token_http(); let mut ui = ScriptedUi::new(Some(ScriptedAnswer::Pending), None); ui.cancel_on_auth = true; - let error = login_anthropic(&http, &ui).await.unwrap_err(); + let error = login_on_a_free_port(&http, &ui).await.unwrap_err(); assert_eq!(error, LOGIN_CANCELLED); // No token request ever posted. assert!(http.requests.lock().unwrap().is_empty()); @@ -827,20 +816,25 @@ mod tests { #[tokio::test] async fn the_browser_callback_wins_the_race() { - let _port = CALLBACK_PORT_LOCK.lock().await; - // The real registered port: the flow binds its callback server - // and the browser redirect settles the code. Skip when another - // process holds the port — the bind-failure path is its own - // invariant. - if !registered_port_stages() { - return; // the registered port is busy: this run cannot stage it. - } let http = Arc::new(token_http()); let ui = Arc::new(ScriptedUi::new(Some(ScriptedAnswer::Pending), None)); + let (verifier, challenge) = generate_pkce(); + let server = AnthropicCallbackServer::bind("127.0.0.1", 0, &verifier) + .expect("a free loopback port binds"); + let port = server.port(); let flow_ui = Arc::clone(&ui); let flow = { let flow_http = Arc::clone(&http); - tokio::spawn(async move { login_anthropic(flow_http.as_ref(), flow_ui.as_ref()).await }) + tokio::spawn(async move { + login_with_server( + flow_http.as_ref(), + flow_ui.as_ref(), + &server, + &verifier, + &challenge, + ) + .await + }) }; let url = ui.captured_url().await; let state = url::Url::parse(&url) @@ -849,7 +843,7 @@ mod tests { .find(|(key, _)| key == "state") .map(|(_, value)| value.to_string()) .expect("the authorization url carries the state"); - let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", CALLBACK_PORT)) + let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port)) .await .expect("the flow's callback server accepts the redirect"); stream diff --git a/crates/pa-ai/src/oauth/anthropic_callback.rs b/crates/pa-ai/src/oauth/anthropic_callback.rs index 6e3949ebccc..029fc076f95 100644 --- a/crates/pa-ai/src/oauth/anthropic_callback.rs +++ b/crates/pa-ai/src/oauth/anthropic_callback.rs @@ -55,26 +55,6 @@ impl CallbackShared { } } -/// The tests that bind the registered callback port (`53_692`) -/// serialize on this lock across the oauth test modules: parallel -/// test threads never collide on the one port (the OS answers the -/// bind with `address already in use` otherwise). -#[cfg(test)] -pub(crate) static CALLBACK_PORT_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); - -/// The registered port's staging probe: `true` when this run can bind -/// it. The lock above serializes the port's binders inside one test -/// binary; a holder outside it — an ephemeral-range collision on the -/// runner (`53692` sits inside the kernel's 32768-60999 allocation -/// range) — is invisible to the lock, so a test that stages the real -/// flow probes first and skips when the port is busy: the -/// bind-failure path is its own invariant -/// (`tests::a_bound_port_fails_the_start_clearly`). -#[cfg(test)] -pub(crate) fn registered_port_stages() -> bool { - std::net::TcpListener::bind(("127.0.0.1", CALLBACK_PORT)).is_ok() -} - /// A running callback server. /// /// Dropping the server aborts its accept loop, releasing the @@ -495,7 +475,6 @@ mod tests { #[tokio::test] async fn a_bound_port_fails_the_start_clearly() { - let _port = CALLBACK_PORT_LOCK.lock().await; // The registered port is occupied, so the login fails (TS // rejects the server promise with the bind error). Skip when // the port was already busy before this test — the blocker diff --git a/crates/pa-ai/src/prime_inference_differential_test.rs b/crates/pa-ai/src/prime_inference_differential_test.rs index fae01499447..feb7d8bd83d 100644 --- a/crates/pa-ai/src/prime_inference_differential_test.rs +++ b/crates/pa-ai/src/prime_inference_differential_test.rs @@ -47,6 +47,7 @@ fn glm_53_flash(base_url: &str) -> Model { }, context_window: 1_310_720, max_tokens: 131_072, + max_tokens_explicit: false, featured: None, headers: None, compat: Some(crate::types::ModelCompat::from_kind( diff --git a/crates/pa-ai/src/providers/anthropic/headers.rs b/crates/pa-ai/src/providers/anthropic/headers.rs index 914691ec5a7..74f90009653 100644 --- a/crates/pa-ai/src/providers/anthropic/headers.rs +++ b/crates/pa-ai/src/providers/anthropic/headers.rs @@ -210,6 +210,7 @@ mod tests { cost: zero_model_cost(), context_window: 200_000, max_tokens: 32_000, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/anthropic/session_affinity_tests.rs b/crates/pa-ai/src/providers/anthropic/session_affinity_tests.rs index 48ef650eb36..8d922883b35 100644 --- a/crates/pa-ai/src/providers/anthropic/session_affinity_tests.rs +++ b/crates/pa-ai/src/providers/anthropic/session_affinity_tests.rs @@ -327,7 +327,7 @@ const TS_ONLY_COPILOT_HEADERS: [&str; 3] = /// overrides of the client's own version and auth headers, and for the /// conversation-marker shapes (a final image, two final text blocks, /// grouped tool results, a final assistant turn). The transport's own -/// framing (`host`, `content-length`, `accept-encoding`) is not compared. +/// framing and SDK `content-type` are excluded by the TS capture driver. #[tokio::test] async fn ts_capture_parity() { let cases: Vec = serde_json::from_str(TS_CAPTURE).unwrap(); @@ -343,7 +343,10 @@ async fn ts_capture_parity() { .headers .into_iter() .filter(|(name, _)| { - !matches!(name.as_str(), "host" | "content-length" | "accept-encoding") + !matches!( + name.as_str(), + "host" | "content-length" | "accept-encoding" | "content-type" + ) }) .collect(); rust_headers.sort(); diff --git a/crates/pa-ai/src/providers/anthropic/stream_tests.rs b/crates/pa-ai/src/providers/anthropic/stream_tests.rs index 8ecffc02810..87f4fedf429 100644 --- a/crates/pa-ai/src/providers/anthropic/stream_tests.rs +++ b/crates/pa-ai/src/providers/anthropic/stream_tests.rs @@ -233,3 +233,142 @@ async fn message_start_without_a_usable_model_records_none() { ); } } + +/// Serve one SSE script for the provider's messages POST and hand the +/// captured request head back to the caller (the one-request mock-server +/// harness above, plus the capture). +async fn serve_sse_capturing_head( + sse: String, +) -> (std::net::SocketAddr, tokio::sync::oneshot::Receiver) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let (head_tx, head_rx) = tokio::sync::oneshot::channel::(); + tokio::spawn(async move { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let (mut socket, _) = listener.accept().await.unwrap(); + let mut captured = String::new(); + let mut buffer = [0u8; 4096]; + loop { + let read = socket.read(&mut buffer).await.unwrap(); + if read == 0 { + break; + } + captured.push_str(&String::from_utf8_lossy(&buffer[..read])); + if captured.contains("\r\n\r\n") { + break; + } + } + let _ = head_tx.send(captured); + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\n\r\n{sse}", + sse.len() + ); + socket.write_all(response.as_bytes()).await.unwrap(); + }); + (addr, head_rx) +} + +/// The values of one wire header, case-insensitive on the name (HTTP/1.1 +/// headers are case-insensitive; a duplicated header shows up twice). +fn wire_header_values(head: &str, name: &str) -> Vec { + head.lines() + .skip(1) // the request line: POST /v1/messages HTTP/1.1 + .filter_map(|line| line.split_once(':')) + .filter(|(key, _)| key.trim().eq_ignore_ascii_case(name)) + .map(|(_, value)| value.trim().to_string()) + .collect() +} + +/// Stream the provider against the mock to completion and return the +/// request head it put on the wire. +async fn stream_capture_head( + options_headers: Option>, +) -> String { + // The known-green SSE script from the snapshot pin above: a thinking + // block, a tool call, the delta and stop events. + let mut sse = [ + r#"event: message_start +data: {"type":"message_start","message":{"id":"msg_1","usage":{"input_tokens":1,"output_tokens":2}}}"#, + r#"event: content_block_start +data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}"#, + r#"event: content_block_delta +data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"think"}}"#, + r#"event: content_block_delta +data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"sig"}}"#, + r#"event: content_block_stop +data: {"type":"content_block_stop","index":0}"#, + r#"event: content_block_start +data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"t1","name":"lookup","input":{}}}"#, + r#"event: content_block_delta +data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"a\":1}"}}"#, + r#"event: content_block_stop +data: {"type":"content_block_stop","index":1}"#, + r#"event: message_delta +data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":5}}"#, + r#"event: message_stop +data: {"type":"message_stop"}"#, + ] + .join("\n\n"); + sse.push_str("\n\n"); + + let (addr, head_rx) = serve_sse_capturing_head(sse).await; + let model: Model = serde_json::from_value(json!({ + "id": "claude-test", "name": "Claude Test", "api": "anthropic-messages", + "provider": "anthropic", "baseUrl": format!("http://{addr}"), "reasoning": false, + "input": ["text"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 128_000, "maxTokens": 8192 + })) + .unwrap(); + let options = AnthropicOptions::from_base(StreamOptions { + api_key: Some("sk-ant-api-test".into()), + headers: options_headers, + ..Default::default() + }); + let mut reader = stream_anthropic( + &model, + &Context { + system_prompt: None, + messages: vec![], + tools: None, + }, + Some(&options), + ); + while (reader.next_event().await).is_some() {} + + let captured = head_rx.await.unwrap(); + captured + .split("\r\n\r\n") + .next() + .unwrap_or(&captured) + .to_string() +} + +/// The messages POST carries exactly one `content-type: application/json` +/// label; a strict anthropic-compatible frontend 400s without it. +#[tokio::test] +async fn wire_carries_one_content_type_application_json() { + let head = stream_capture_head(None).await; + assert_eq!( + wire_header_values(&head, "content-type"), + vec!["application/json"], + "the wire head: {head}" + ); +} + +/// A caller-configured content-type (stream-options headers) still wins: +/// the transport stamps its label only when the caller did not provide +/// one, and it never duplicates the header. +#[tokio::test] +async fn wire_caller_content_type_override_wins() { + let headers = std::collections::HashMap::from([( + "Content-Type".to_string(), + "application/vnd.anthropic+json".to_string(), + )]); + let head = stream_capture_head(Some(headers)).await; + assert_eq!( + wire_header_values(&head, "content-type"), + vec!["application/vnd.anthropic+json"], + "the wire head: {head}" + ); +} diff --git a/crates/pa-ai/src/providers/bedrock/events.rs b/crates/pa-ai/src/providers/bedrock/events.rs index 06a377e4216..b491a13dd59 100644 --- a/crates/pa-ai/src/providers/bedrock/events.rs +++ b/crates/pa-ai/src/providers/bedrock/events.rs @@ -444,6 +444,7 @@ mod tests { cost: zero_model_cost(), context_window: 200_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/bedrock/eventstream.rs b/crates/pa-ai/src/providers/bedrock/eventstream.rs index 3d17e273b7e..2725d5d00bc 100644 --- a/crates/pa-ai/src/providers/bedrock/eventstream.rs +++ b/crates/pa-ai/src/providers/bedrock/eventstream.rs @@ -99,6 +99,9 @@ fn parse_headers(bytes: &[u8]) -> Vec<(String, Option)> { } let len = read_u16(&bytes[offset..]) as usize; offset += 2; + if offset + len > bytes.len() { + break; + } let value = String::from_utf8_lossy(&bytes[offset..offset + len]).to_string(); offset += len; Some(value) @@ -159,6 +162,11 @@ impl EventStreamDecoder { self.buffer.drain(..total_len); continue; } + if headers_len > total_len - 16 { + // Header section overruns the frame; skip past it. + self.buffer.drain(..total_len); + continue; + } let headers = parse_headers(&message[12..12 + headers_len]); let payload = message[12 + headers_len..total_len - 4].to_vec(); let header = |name: &str| { @@ -199,16 +207,22 @@ mod tests { push_header(":message-type", "event"); push_header(":content-type", "application/json"); push_header(":event-type", event_type); + let headers_len = headers.len(); + headers.extend_from_slice(payload); + raw_frame(headers_len, &headers) + } + + /// Frame `body` (header section + payload) with a prelude declaring `headers_len`. + fn raw_frame(headers_len: usize, body: &[u8]) -> Vec { let mut message = Vec::new(); - let total_len = 12 + headers.len() + payload.len() + 4; + let total_len = 12 + body.len() + 4; // Test frames are tiny; the event-stream wire lengths are u32. #[allow(clippy::cast_possible_truncation)] message.extend_from_slice(&(total_len as u32).to_be_bytes()); #[allow(clippy::cast_possible_truncation)] - message.extend_from_slice(&(headers.len() as u32).to_be_bytes()); + message.extend_from_slice(&(headers_len as u32).to_be_bytes()); message.extend_from_slice(&crc32(&message[..8]).to_be_bytes()); - message.extend_from_slice(&headers); - message.extend_from_slice(payload); + message.extend_from_slice(body); message.extend_from_slice(&crc32(&message).to_be_bytes()); message } @@ -236,6 +250,31 @@ mod tests { assert_eq!(all[1].event_type.as_deref(), Some("metadata")); } + #[test] + fn string_header_overrunning_the_section_stops_header_parsing() { + // Header section: name_len 0, type 7, string len 0xFF7E past the section end. + let mut stream = raw_frame(4, &[0, 7, 255, 126]); + stream.extend(frame("metadata", b"{}")); + let messages = EventStreamDecoder::new().push(&stream); + let decoded: Vec<_> = messages + .iter() + .map(|m| (m.event_type.as_deref(), m.payload.as_slice())) + .collect(); + assert_eq!(decoded, [(None, &b""[..]), (Some("metadata"), &b"{}"[..])]); + } + + #[test] + fn frame_whose_headers_len_overruns_it_is_skipped() { + let mut stream = raw_frame(0xFF7E, &[]); + stream.extend(frame("metadata", b"{}")); + let messages = EventStreamDecoder::new().push(&stream); + let decoded: Vec<_> = messages + .iter() + .map(|m| (m.event_type.as_deref(), m.payload.as_slice())) + .collect(); + assert_eq!(decoded, [(Some("metadata"), &b"{}"[..])]); + } + #[test] fn crc32_matches_reference_value() { // CRC-32 of "123456789" (standard IEEE check value). diff --git a/crates/pa-ai/src/providers/bedrock/mod.rs b/crates/pa-ai/src/providers/bedrock/mod.rs index b2eaa8f7c8b..e66d6242870 100644 --- a/crates/pa-ai/src/providers/bedrock/mod.rs +++ b/crates/pa-ai/src/providers/bedrock/mod.rs @@ -1040,6 +1040,7 @@ mod tests { cost: crate::types::zero_model_cost(), context_window: 200_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/faux/mod.rs b/crates/pa-ai/src/providers/faux/mod.rs index 4cb15d3c699..18af511862b 100644 --- a/crates/pa-ai/src/providers/faux/mod.rs +++ b/crates/pa-ai/src/providers/faux/mod.rs @@ -967,6 +967,7 @@ pub fn register_faux_provider(options: RegisterFauxProviderOptions) -> FauxProvi cost: definition.cost.unwrap_or_else(zero_model_cost), context_window: definition.context_window.unwrap_or(128_000), max_tokens: definition.max_tokens.unwrap_or(16_384), + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/mistral/mod.rs b/crates/pa-ai/src/providers/mistral/mod.rs index 6b54205edd6..77521babb59 100644 --- a/crates/pa-ai/src/providers/mistral/mod.rs +++ b/crates/pa-ai/src/providers/mistral/mod.rs @@ -277,6 +277,7 @@ mod tests { cost: crate::types::zero_model_cost(), context_window: 128_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/mistral/stream.rs b/crates/pa-ai/src/providers/mistral/stream.rs index a703643a61f..e857ebb5dc1 100644 --- a/crates/pa-ai/src/providers/mistral/stream.rs +++ b/crates/pa-ai/src/providers/mistral/stream.rs @@ -861,6 +861,7 @@ mod tests { cost: crate::types::zero_model_cost(), context_window: 128_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/openai_codex_responses/errors.rs b/crates/pa-ai/src/providers/openai_codex_responses/errors.rs index 0f2036b11b1..3b82d442409 100644 --- a/crates/pa-ai/src/providers/openai_codex_responses/errors.rs +++ b/crates/pa-ai/src/providers/openai_codex_responses/errors.rs @@ -10,7 +10,9 @@ use crate::types::{AssistantMessage, Usage}; use crate::utils::stream_failure::parse_retry_after_ms; use crate::utils_inner::diagnostics::now_ms; use crate::utils_inner::http::HttpResponse; -use crate::utils_inner::stream_failure::ProviderError; +use crate::utils_inner::stream_failure::{ + ProviderError, StreamTransportFailureCause, StreamTransportFailureDetail, +}; /// Codex API error (`CodexApiError` in the TS reference). Carries the wire /// error code, HTTP status, and retry delay parsed from headers/body. @@ -100,7 +102,11 @@ impl CodexProtocolError { pub enum WebSocketTransportError { /// A close-event failure (`WebSocketCloseError` in the TS): the composed /// close message plus the numeric close code. - Close { message: String, code: u16 }, + Close { + message: String, + code: u16, + reason: String, + }, /// A connect- or send-phase runtime failure (plain `Error` in the TS). Runtime { message: String }, } @@ -136,7 +142,11 @@ impl WebSocketTransportError { let message = format!("WebSocket closed {code}{reason_text}") .trim() .to_string(); - Self::Close { message, code } + Self::Close { + message, + code, + reason: reason.to_string(), + } } /// A plain runtime failure (`Error` in the TS) with the given text. @@ -213,9 +223,23 @@ impl CodexStreamError { crate::utils_inner::stream_failure::ProviderWsTransportError { message: error.message().to_string(), close_code: error.close_code(), - // The codex transport keeps its unclassified runtime - // surface (the provider error type is the class name). - transport: None, + sdk_name: Some(error.error_name().to_string()), + transport: Some(match &error { + WebSocketTransportError::Close { code, reason, .. } => { + StreamTransportFailureDetail { + close_code: Some(*code), + close_reason: (!reason.is_empty()).then(|| reason.clone()), + ..StreamTransportFailureDetail::websocket( + StreamTransportFailureCause::Closed, + ) + } + } + WebSocketTransportError::Runtime { .. } => { + StreamTransportFailureDetail::websocket( + StreamTransportFailureCause::Connect, + ) + } + }), }, ), CodexStreamError::Aborted => ProviderError::Aborted, @@ -956,11 +980,77 @@ mod tests { "code": 1011 }, "details": { - "kind": "unknown", - "providerErrorType": "WebSocketCloseError" + "kind": "transport", + "providerErrorType": "websocket_closed", + "transport": { + "protocol": "websocket", "cause": "closed", + "closeCode": 1011, "closeReason": "mock server reason" + } } } ]) ); } + + #[test] + fn codex_transport_classification_preserves_runtime_diagnostics_and_unknown_cleanliness() { + for runtime in [ + WebSocketTransportError::close(1006, "Connection ended"), + WebSocketTransportError::close(1011, "protocol read failure"), + WebSocketTransportError::close(1000, "normal but incomplete"), + WebSocketTransportError::runtime("WebSocket send failed"), + ] { + let mut expected_error = json!({ + "name": runtime.error_name(), "message": runtime.message(), + }); + let mut transport = json!({"protocol": "websocket"}); + match &runtime { + WebSocketTransportError::Close { code, reason, .. } => { + expected_error["code"] = json!(code); + transport["cause"] = json!("closed"); + transport["closeCode"] = json!(code); + transport["closeReason"] = json!(reason); + } + WebSocketTransportError::Runtime { .. } => { + transport["cause"] = json!("connect"); + } + } + let error = CodexStreamError::Transport(runtime).into_provider_error(); + let mut output = AssistantMessage { + content: Vec::new(), + api: API_OPENAI_CODEX_RESPONSES.to_string(), + provider: "openai-codex".to_string(), + model: "gpt-5-codex".to_string(), + response_model: None, + response_model_source: None, + response_id: None, + diagnostics: None, + usage: crate::types::Usage::default(), + stop_reason: crate::types::StopReason::Error, + stop_reason_raw: None, + error_message: Some(error.to_string()), + timestamp: 0, + rest: Map::default(), + }; + crate::utils_inner::stream_failure::record_stream_failure( + ("openai-codex", "gpt-5-codex", API_OPENAI_CODEX_RESPONSES), + &mut output, + &error, + ); + let diagnostics = serde_json::to_value(output.diagnostics.as_deref()).unwrap(); + assert_eq!( + diagnostics, + json!([{ + "type": "provider_stream_failure", + "timestamp": diagnostics[0]["timestamp"], + "error": expected_error, + "details": { + "kind": "transport", + "providerErrorType": format!("websocket_{}", transport["cause"].as_str().unwrap()), + "transport": transport, + }, + }]) + ); + } + } } diff --git a/crates/pa-ai/src/providers/openai_codex_responses/mod.rs b/crates/pa-ai/src/providers/openai_codex_responses/mod.rs index 3a300c5c5bc..b8630d03ece 100644 --- a/crates/pa-ai/src/providers/openai_codex_responses/mod.rs +++ b/crates/pa-ai/src/providers/openai_codex_responses/mod.rs @@ -452,62 +452,26 @@ async fn run_websocket_attempt( ); } - let mut events = connection + let mut events = match connection .send_request(&request_body, options.base.signal.clone()) - .await?; + .await + { + Ok(events) => events, + Err(error) => { + release_connection(connection, false, None).await; + return Err(error); + } + }; let mut keep_connection = true; - let start_partial = output.clone(); - let process = async { - let model_id = model.id.clone(); - let hooks = ResponsesStreamHooks { - request_service_tier: options.service_tier, - resolve_service_tier: Some(Box::new(resolve_codex_service_tier)), - apply_service_tier_pricing: Some(Box::new(move |usage, service_tier| { - apply_codex_service_tier_pricing(usage, service_tier.as_deref(), &model_id); - })), - }; - let mut start_emitted = false; - let mut processor = ResponsesStreamProcessor::new(model, output, writer, hooks); - // The TS catch settles partial tool calls before the error event - // carries the message (TS PR #2783). - let streamed: Result<(), CodexStreamError> = async { - while let Some(event) = events.recv().await { - match event { - websocket::WorkerEvent::Event(event) => { - // Codex can emit lifecycle, telemetry, or vendor - // metadata before rejecting a stale continuation. - // Keep the attempt retryable until an event can - // produce output consumed by the shared processor - // (TS #2374's visible-event gate). - if !start_emitted && is_codex_visible_response_event(&event) { - start_emitted = true; - *websocket_started = true; - writer.push(AssistantMessageEvent::Start { - partial: start_partial.clone(), - }); - } - let mapped = map_codex_event(event)?; - processor.handle_event(&mapped.event)?; - if mapped.done { - break; - } - } - websocket::WorkerEvent::End(end) => { - websocket::codex_socket_end(end)?; - break; - } - } - } - processor.finish()?; - Ok(()) - } - .await; - if streamed.is_err() { - processor.settle_partial_tool_calls(); - } - streamed - } + let process = process_websocket_events( + model, + options, + output, + writer, + websocket_started, + &mut events, + ) .await; match process { @@ -559,6 +523,80 @@ async fn run_websocket_attempt( } } +/// Collect one Codex response. A missing end event is a transport failure, +/// never a successful partial response. +async fn process_websocket_events( + model: &Model, + options: &OpenAICodexResponsesOptions, + output: &mut AssistantMessage, + writer: &AssistantMessageEventWriter, + websocket_started: &mut bool, + events: &mut tokio::sync::mpsc::Receiver, +) -> Result<(), CodexStreamError> { + let start_partial = output.clone(); + let model_id = model.id.clone(); + let hooks = ResponsesStreamHooks { + request_service_tier: options.service_tier, + resolve_service_tier: Some(Box::new(resolve_codex_service_tier)), + apply_service_tier_pricing: Some(Box::new(move |usage, service_tier| { + apply_codex_service_tier_pricing(usage, service_tier.as_deref(), &model_id); + })), + }; + let mut start_emitted = false; + let mut processor = ResponsesStreamProcessor::new(model, output, writer, hooks); + // The TS catch settles partial tool calls before the error event + // carries the message (TS PR #2783). + let streamed: Result<(), CodexStreamError> = async { + loop { + let Some(event) = events.recv().await else { + if options + .base + .signal + .as_ref() + .is_some_and(tokio_util::sync::CancellationToken::is_cancelled) + { + return Err(CodexStreamError::Aborted); + } + return websocket::codex_socket_end( + crate::providers::responses_websocket::connection::SocketEnd::Eof, + ); + }; + match event { + websocket::WorkerEvent::Event(event) => { + // Codex can emit lifecycle, telemetry, or vendor + // metadata before rejecting a stale continuation. + // Keep the attempt retryable until an event can + // produce output consumed by the shared processor + // (TS #2374's visible-event gate). + if !start_emitted && is_codex_visible_response_event(&event) { + start_emitted = true; + *websocket_started = true; + writer.push(AssistantMessageEvent::Start { + partial: start_partial.clone(), + }); + } + let mapped = map_codex_event(event)?; + processor.handle_event(&mapped.event)?; + if mapped.done { + break; + } + } + websocket::WorkerEvent::End(end) => { + websocket::codex_socket_end(end)?; + break; + } + } + } + processor.finish()?; + Ok(()) + } + .await; + if streamed.is_err() { + processor.settle_partial_tool_calls(); + } + streamed +} + /// Rebuild an `AssistantMessage` as a `Message` for continuation-item /// conversion (the TS passes `{ messages: [output] }` directly). fn assistant_as_message(output: &AssistantMessage) -> crate::types::Message { @@ -910,6 +948,7 @@ mod tests { cost: crate::types::zero_model_cost(), context_window: 400_000, max_tokens: 128_000, + max_tokens_explicit: false, featured: None, headers: None, compat: None, @@ -1594,4 +1633,44 @@ mod tests { "the retry sends the full context: {bodies:?}" ); } + #[tokio::test] + async fn a_lost_websocket_event_sender_never_commits_partial_output() { + let model = codex_wire_model(); + let options = OpenAICodexResponsesOptions::default(); + let (sender, mut events) = tokio::sync::mpsc::channel(16); + for event in codex_response_events("resp_cut", "msg_cut", "uncommitted") + .into_iter() + .take(4) + { + sender + .send(websocket::WorkerEvent::Event(event)) + .await + .unwrap(); + } + drop(sender); + let mut output = crate::providers::faux::faux_assistant_text_message( + "", + crate::providers::faux::FauxAssistantMessageOptions::default(), + ); + output.content.clear(); + let (writer, _reader) = create_assistant_message_event_stream(); + let mut started = false; + let error = process_websocket_events( + &model, + &options, + &mut output, + &writer, + &mut started, + &mut events, + ) + .await + .unwrap_err() + .into_provider_error(); + assert!(started); + assert_eq!(codex_message_text(&output).as_deref(), Some("uncommitted")); + assert_eq!( + crate::utils_inner::stream_failure::extract_stream_failure_info(&error).kind, + crate::utils_inner::stream_failure::StreamFailureKind::Transport, + ); + } } diff --git a/crates/pa-ai/src/providers/openai_completions/convert.rs b/crates/pa-ai/src/providers/openai_completions/convert.rs index 894a37dba72..22bd60a3758 100644 --- a/crates/pa-ai/src/providers/openai_completions/convert.rs +++ b/crates/pa-ai/src/providers/openai_completions/convert.rs @@ -553,6 +553,7 @@ mod tests { }, context_window: 128_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/openai_completions/stream.rs b/crates/pa-ai/src/providers/openai_completions/stream.rs index 7d1c7e52a62..cf393b48860 100644 --- a/crates/pa-ai/src/providers/openai_completions/stream.rs +++ b/crates/pa-ai/src/providers/openai_completions/stream.rs @@ -659,19 +659,26 @@ async fn run_stream( let events = decoder.push_text(&chunk); for event in &events { if mark_done_marker(event, &mut state) { - continue; + // A held-open body past [DONE] never EOFs; the marker + // ends the stream. + break; } if let Some(chunk) = parse_sse_event_data(event) { handle_chunk(&chunk, model, cache_write_cost, &mut state, writer); } } - } - for event in decoder.finish() { - if mark_done_marker(&event, &mut state) { - continue; + if state.saw_done_marker { + break; } - if let Some(chunk) = parse_sse_event_data(&event) { - handle_chunk(&chunk, model, cache_write_cost, &mut state, writer); + } + if !state.saw_done_marker { + for event in decoder.finish() { + if mark_done_marker(&event, &mut state) { + continue; + } + if let Some(chunk) = parse_sse_event_data(&event) { + handle_chunk(&chunk, model, cache_write_cost, &mut state, writer); + } } } // The multiplier table is OpenAI's own; gateways price tiers per endpoint @@ -774,6 +781,15 @@ mod tests { #[path = "stream_pins.rs"] mod stream_pins; + // The #755 output-budget wire pins (the captured request bodies). + #[path = "stream_max_tokens.rs"] + mod stream_max_tokens; + + // The wire-level pins (the request head on the wire) live in their + // own child module with this file's test harness. + #[path = "stream_wire.rs"] + mod stream_wire; + /// Serve one SSE response body for the provider's POST and return the /// bound address. async fn serve_sse(body: String) -> SocketAddr { diff --git a/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_max_tokens.rs b/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_max_tokens.rs new file mode 100644 index 00000000000..449ab26b838 --- /dev/null +++ b/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_max_tokens.rs @@ -0,0 +1,167 @@ +//! The #755 output-budget wire pins: the request body's output budget +//! carries an explicitly configured `maxTokens` unchanged, a catalog +//! value stays capped at the default 32000 ceiling, and a model with no +//! declared max output sends no budget field at all (issue #755's +//! captured bodies; the TS fix PR's `max-tokens.test.ts` behavior, pinned +//! at the provider wire instead of the option assembly). + +use super::*; +use std::sync::{Arc, Mutex}; + +/// The reporter's model (issue #755: a vLLM-served GLM configured in +/// models.json with `"maxTokens": 131072` and the OpenAI-completions +/// compat the captured bodies show). The `maxTokensExplicit` flag is what +/// the models.json parse attaches to a configured value. +fn glm_model(max_tokens: u64, explicit: bool) -> Value { + json!({ + "id": "glm-5.2", "name": "GLM 5.2", "api": "openai-completions", + "provider": "glm-h200", "reasoning": true, "input": ["text"], + "cost": { "input": 0.0, "output": 0.0, "cacheRead": 0.0, "cacheWrite": 0.0 }, + "contextWindow": 393_216, "maxTokens": max_tokens, + "compat": { + "supportsDeveloperRole": false, "supportsReasoningEffort": false, + "maxTokensField": "max_tokens", "thinkingFormat": "qwen-chat-template" + }, + "maxTokensExplicit": explicit, + }) +} + +/// Serve one SSE response body for the provider's POST, capturing the raw +/// request bytes it sent (the issue's logging-proxy capture, in-process). +async fn serve_sse_capturing(body: String) -> (SocketAddr, Arc>>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let captured: Arc>> = Arc::new(Mutex::new(Vec::new())); + let sink = Arc::clone(&captured); + tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut request = Vec::new(); + let mut chunk = [0u8; 4096]; + let mut head_end = None; + let mut content_length = 0usize; + loop { + let read = socket.read(&mut chunk).await.unwrap(); + if read == 0 { + break; + } + request.extend_from_slice(&chunk[..read]); + if head_end.is_none() { + if let Some(pos) = request.windows(4).position(|w| w == b"\r\n\r\n") { + head_end = Some(pos + 4); + let head = String::from_utf8_lossy(&request[..pos]).to_lowercase(); + content_length = head + .lines() + .find_map(|line| { + line.strip_prefix("content-length:") + .and_then(|value| value.trim().parse().ok()) + }) + .unwrap_or(0); + } + } + if let Some(end) = head_end { + if request.len() >= end + content_length { + break; + } + } + } + *sink.lock().unwrap() = request; + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + socket.write_all(response.as_bytes()).await.unwrap(); + }); + (addr, captured) +} + +/// One SSE body ending in the terminal marker, over `chunks`. +fn sse_body(chunks: &[Value]) -> String { + use std::fmt::Write as _; + let mut body = String::new(); + for chunk in chunks { + let _ = write!(body, "data: {chunk}\n\n"); + } + let _ = write!(body, "data: [DONE]\n\n"); + body +} + +/// The captured POST body as JSON (the request head stripped). +fn request_body_json(request: &[u8]) -> Value { + let text = String::from_utf8_lossy(request); + let (_, body) = text + .split_once("\r\n\r\n") + .expect("the captured request carries its head"); + serde_json::from_str(body).expect("the captured request body is JSON") +} + +/// Stream one minimal completion against `model` and return the JSON body +/// the provider put on the wire. +async fn wire_body_for(model: Value) -> Value { + let body = sse_body(&[json!({ + "id": "m", "object": "chat.completion.chunk", "model": model["id"], + "choices": [{ "index": 0, "delta": {}, "finish_reason": "stop" }], + "usage": { "prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2 } + })]); + let (addr, captured) = serve_sse_capturing(body).await; + let mut model = model; + model["baseUrl"] = json!(format!("http://{addr}")); + let model: Model = serde_json::from_value(model).unwrap(); + // The product path: the daemon's stream call goes through the SIMPLE + // provider entry (`build_base_options` fills the per-model default + // output budget there), so this is the request shape a real turn sends. + let simple = crate::types::SimpleStreamOptions { + base: crate::types::StreamOptions { + api_key: Some("test".into()), + ..Default::default() + }, + ..Default::default() + }; + let mut reader = crate::providers::openai_completions::stream_simple_openai_completions( + &model, + &Context { + system_prompt: None, + messages: vec![], + tools: None, + }, + Some(&simple), + ); + loop { + match reader.next_event().await.unwrap() { + AssistantMessageEvent::Done { .. } => break, + AssistantMessageEvent::Error { error, .. } => { + panic!("stream failed: {:?}", error.error_message) + } + _ => {} + } + } + let request = captured.lock().unwrap().clone(); + request_body_json(&request) +} + +#[tokio::test] +async fn an_explicitly_configured_max_tokens_reaches_the_wire_unchanged() { + // Issue #755's exact configuration: a models.json entry asking for + // `"maxTokens": 131072`. The configured cap reaches the provider + // unchanged — the captured body must not show the silent 32000. + let body = wire_body_for(glm_model(131_072, true)).await; + assert_eq!(body["max_tokens"], json!(131_072)); +} + +#[tokio::test] +async fn a_catalog_model_stays_capped_at_the_default_output_ceiling() { + // The untouched default: a catalog model's declared maxTokens is a + // ceiling the request clamps to 32000 (TS `DEFAULT_MAX_OUTPUT_TOKENS`) + // — most catalog models declare far more than a turn needs, so their + // request size must not change. + let body = wire_body_for(glm_model(131_072, false)).await; + assert_eq!(body["max_tokens"], json!(32_000)); +} + +#[tokio::test] +async fn a_model_without_max_tokens_sends_no_output_budget() { + // A model that declares no max output (0) sends neither budget field: + // the provider defaults server-side. + let body = wire_body_for(glm_model(0, false)).await; + assert!(body.get("max_tokens").is_none()); + assert!(body.get("max_completion_tokens").is_none()); +} diff --git a/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_wire.rs b/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_wire.rs new file mode 100644 index 00000000000..b778214848a --- /dev/null +++ b/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_wire.rs @@ -0,0 +1,135 @@ +//! The wire-level pins: the request head the shared transport puts on +//! the wire for a chat/completions POST. The JSON body carries exactly +//! one `content-type: application/json` label, and a caller-configured +//! content-type (`model.headers` or stream-options headers) still wins. + +use super::*; + +use std::net::SocketAddr; + +/// The known-green SSE script from the tier pins (a content chunk, a +/// usage chunk, `[DONE]`): the stream completes so the head capture and +/// the response both land in one test. +fn tier_sse() -> String { + format!("{TIERED_CONTENT_CHUNK}{USAGE_CHUNK}{DONE}") +} + +/// Serve one SSE response for the provider's POST and hand the captured +/// request head back to the caller (the same one-request mock-server +/// harness as [`serve_sse`], plus the capture). +async fn serve_sse_capturing_head( + body: String, +) -> (SocketAddr, tokio::sync::oneshot::Receiver) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let (head_tx, head_rx) = tokio::sync::oneshot::channel::(); + tokio::spawn(async move { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let (mut socket, _) = listener.accept().await.unwrap(); + let mut captured = String::new(); + let mut buffer = [0u8; 4096]; + loop { + let read = socket.read(&mut buffer).await.unwrap(); + if read == 0 { + break; + } + captured.push_str(&String::from_utf8_lossy(&buffer[..read])); + if captured.contains("\r\n\r\n") { + break; + } + } + let _ = head_tx.send(captured); + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + socket.write_all(response.as_bytes()).await.unwrap(); + }); + (addr, head_rx) +} + +/// Stream the provider against the mock to completion and return the +/// request head it put on the wire. +async fn stream_capture_head( + model: Value, + headers: Option>, +) -> String { + let (addr, head_rx) = serve_sse_capturing_head(tier_sse()).await; + let mut model = model; + model["baseUrl"] = json!(format!("http://{addr}")); + let model: Model = serde_json::from_value(model).unwrap(); + let options = OpenAICompletionsOptions::from_base(crate::types::StreamOptions { + api_key: Some("test".into()), + headers, + ..Default::default() + }); + let mut reader = stream_openai_completions( + &model, + &Context { + system_prompt: None, + messages: vec![], + tools: None, + }, + Some(&options), + ); + loop { + let event = reader.next_event().await.unwrap(); + if let AssistantMessageEvent::Done { .. } = event { + break; + } + if let AssistantMessageEvent::Error { error, .. } = event { + panic!("stream failed: {:?}", error.error_message); + } + } + let captured = head_rx.await.unwrap(); + captured + .split("\r\n\r\n") + .next() + .unwrap_or(&captured) + .to_string() +} + +/// The values of one wire header, case-insensitive on the name (HTTP/1.1 +/// headers are case-insensitive; a duplicated header shows up twice). +fn header_values(head: &str, name: &str) -> Vec { + head.lines() + .skip(1) // the request line: POST /chat/completions HTTP/1.1 + .filter_map(|line| line.split_once(':')) + .filter(|(key, _)| key.trim().eq_ignore_ascii_case(name)) + .map(|(_, value)| value.trim().to_string()) + .collect() +} + +/// The chat/completions POST carries exactly one +/// `content-type: application/json` label; a strict OpenAI-compatible +/// frontend 400s without it. +#[tokio::test] +async fn wire_carries_one_content_type_application_json() { + let head = stream_capture_head(completions_model("gpt-5.5", "openai", 1.0, 1.0), None).await; + assert_eq!( + header_values(&head, "content-type"), + vec!["application/json"], + "the wire head: {head}" + ); +} + +/// A caller-configured content-type (stream-options headers) still wins: +/// the transport stamps its label only when the caller did not provide +/// one, and it never duplicates the header. +#[tokio::test] +async fn wire_caller_content_type_override_wins() { + let headers = std::collections::HashMap::from([( + "Content-Type".to_string(), + "application/vnd.api+json".to_string(), + )]); + let head = stream_capture_head( + completions_model("gpt-5.5", "openai", 1.0, 1.0), + Some(headers), + ) + .await; + assert_eq!( + header_values(&head, "content-type"), + vec!["application/vnd.api+json"], + "the wire head: {head}" + ); +} diff --git a/crates/pa-ai/src/providers/openai_completions/stream_drop.rs b/crates/pa-ai/src/providers/openai_completions/stream_drop.rs index 18474253492..c876a69a160 100644 --- a/crates/pa-ai/src/providers/openai_completions/stream_drop.rs +++ b/crates/pa-ai/src/providers/openai_completions/stream_drop.rs @@ -210,3 +210,86 @@ async fn a_done_marker_without_finish_reason_completes() { assert_eq!(message.stop_reason, StopReason::Stop); assert_eq!(message.error_message, None); } + +/// Serve the SSE body and hold the connection open: no `Content-Length`, +/// no terminator, no close — body EOF never arrives. +async fn serve_sse_hold_open(body: String) -> SocketAddr { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { + let (socket, _) = listener.accept().await.unwrap(); + let mut socket = socket; + let mut request = vec![0u8; 8192]; + let _ = socket.read(&mut request).await.unwrap(); + let response = format!("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\n\r\n{body}"); + socket.write_all(response.as_bytes()).await.unwrap(); + socket.flush().await.unwrap(); + // The held-open tail: the body never EOFs while the stream reads. + tokio::time::sleep(std::time::Duration::from_secs(30)).await; + }); + addr +} + +/// A tool-call chunk (`finish_reason` still null — the stream is +/// mid-block). +const TOOLCALL_DELTA: &str = r#"data: {"id":"c1","object":"chat.completion.chunk","model":"glm-test","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"ipython","arguments":"{\"code\": \"print(17*23)\"}"}}]},"finish_reason":null}]} + +"#; + +/// The tool-calls stop signal: the final chunk carries +/// `finish_reason: "tool_calls"`. +const TOOLCALLS_FINISH: &str = "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"model\":\"glm-test\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n"; + +/// A full tool-call turn whose connection stays open after the `[DONE]` +/// marker: the stream must complete at the marker, never wait for body +/// EOF. The hang this pins leaves the turn silent mid-stream — the model +/// already streamed its tool call, the worker never executes it, and the +/// prompt waits forever. +#[tokio::test] +async fn the_done_marker_ends_a_stream_whose_body_stays_open() { + let body = format!("{TEXT_DELTA}{TOOLCALL_DELTA}{TOOLCALLS_FINISH}{DONE}"); + let addr = serve_sse_hold_open(body).await; + let model: Model = serde_json::from_value(json!({ + "id": "glm-test", "name": "GLM test", "api": "openai-completions", + "provider": "prime-inference", "baseUrl": format!("http://{addr}"), + "reasoning": true, "input": ["text"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 131_072, "maxTokens": 8192, + })) + .expect("test model"); + let options = OpenAICompletionsOptions::from_base(crate::types::StreamOptions { + api_key: Some("test".into()), + ..Default::default() + }); + let mut reader = stream_openai_completions( + &model, + &Context { + system_prompt: None, + messages: vec![], + tools: None, + }, + Some(&options), + ); + let terminal = tokio::time::timeout(std::time::Duration::from_secs(5), async { + loop { + let event = reader + .next_event() + .await + .expect("stream events while the body is held open"); + if event.is_terminal() { + return event; + } + } + }) + .await + .expect("the [DONE] marker ends the stream; body EOF never arrives"); + let AssistantMessageEvent::Done { message, .. } = terminal else { + panic!("a marker-terminated stream must complete, not error"); + }; + assert_eq!(message.stop_reason, StopReason::ToolUse); + assert_eq!(message.error_message, None); + assert!(message + .content + .iter() + .any(|block| matches!(block, AssistantContent::ToolCall(call) if call.name == "ipython"))); +} diff --git a/crates/pa-ai/src/providers/openai_responses.rs b/crates/pa-ai/src/providers/openai_responses.rs index f4f34acfb49..326bfc770c7 100644 --- a/crates/pa-ai/src/providers/openai_responses.rs +++ b/crates/pa-ai/src/providers/openai_responses.rs @@ -5,7 +5,8 @@ //! A model that opts in (`compat.supportsWebSocket`) or an explicit //! `websocket`/`websocket-cached` transport streams over the Responses //! WebSocket transport first ([`crate::providers::responses_websocket`]); -//! SSE takes over only when the socket failed before its first event. +//! SSE takes over only when the socket failed before its first event, +//! while structured connection-cap failures return to the session retry driver. use serde_json::{json, Map, Value}; @@ -391,14 +392,6 @@ async fn run_stream( }; if websocket_enabled { let mut started = false; - let model_id = model.id.clone(); - let hooks = ResponsesStreamHooks { - request_service_tier: options.service_tier, - resolve_service_tier: None, - apply_service_tier_pricing: Some(Box::new(move |usage, service_tier| { - apply_service_tier_pricing(usage, service_tier.as_deref(), &model_id); - })), - }; // WebSocket APIs expose no portable upgrade headers: the hook sees // the successful 101 handshake (TS `onOpen`). let on_open = || { @@ -412,6 +405,14 @@ async fn run_stream( ); } }; + let model_id = model.id.clone(); + let hooks = ResponsesStreamHooks { + request_service_tier: options.service_tier, + resolve_service_tier: None, + apply_service_tier_pricing: Some(Box::new(move |usage, service_tier| { + apply_service_tier_pricing(usage, service_tier.as_deref(), &model_id); + })), + }; let attempt = match resolve_responses_websocket_url(&model.base_url) { Ok(url) => { process_generation( diff --git a/crates/pa-ai/src/providers/openai_responses_errors.rs b/crates/pa-ai/src/providers/openai_responses_errors.rs index 05c72d57d87..62aa991550c 100644 --- a/crates/pa-ai/src/providers/openai_responses_errors.rs +++ b/crates/pa-ai/src/providers/openai_responses_errors.rs @@ -15,6 +15,7 @@ use serde_json::{Map, Value}; use crate::utils_inner::stream_failure::{ classify_stream_failure, stream_failure_message, StreamFailureError, StreamFailureInfo, + StreamFailureKind, }; /// Bound on the user-facing message (TS `redactErrorText` default). @@ -117,11 +118,17 @@ fn parse_responses_error_frame(event: &Value) -> ResponsesErrorFrame { /// The classified failure a Responses `error` event raises (the TS /// `processResponsesStream` error arm): provider type, separate code, and -/// status classify the kind; the redacted message is the detail. +/// status classify the kind; the exact connection-cap code has its own +/// session-refresh kind. The redacted message is the detail. pub(crate) fn responses_error_event_failure(event: &Value) -> StreamFailureError { let frame = parse_responses_error_frame(event); let info = StreamFailureInfo { - kind: classify_stream_failure(frame.provider_error_type.as_deref(), frame.status), + kind: if frame.provider_error_code.as_deref() == Some("websocket_connection_limit_reached") + { + StreamFailureKind::ConnectionLimit + } else { + classify_stream_failure(frame.provider_error_type.as_deref(), frame.status) + }, provider_error_type: frame.provider_error_type, provider_error_code: frame.provider_error_code, status: frame.status, @@ -143,7 +150,6 @@ pub(crate) fn responses_error_event_failure(event: &Value) -> StreamFailureError #[cfg(test)] mod tests { use super::*; - use crate::utils_inner::stream_failure::StreamFailureKind; use serde_json::json; /// The flat `OpenAI` frame: the code is both the provider type and code. diff --git a/crates/pa-ai/src/providers/openai_responses_shared.rs b/crates/pa-ai/src/providers/openai_responses_shared.rs index 10813863920..becab074606 100644 --- a/crates/pa-ai/src/providers/openai_responses_shared.rs +++ b/crates/pa-ai/src/providers/openai_responses_shared.rs @@ -412,6 +412,7 @@ mod tests { cost: crate::types::zero_model_cost(), context_window: 400_000, max_tokens: 128_000, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/providers/responses_websocket/limit_tests.rs b/crates/pa-ai/src/providers/responses_websocket/limit_tests.rs new file mode 100644 index 00000000000..2820832d2e3 --- /dev/null +++ b/crates/pa-ai/src/providers/responses_websocket/limit_tests.rs @@ -0,0 +1,221 @@ +//! Structured cap verdicts and acquisition-only socket age rotation. +use super::*; +use crate::event_stream::AssistantMessageEvent; + +fn cap_400() -> Value { + json!({ + "type": "error", "status": 400, + "error": { "type": "invalid_request_error", "code": "websocket_connection_limit_reached", + "message": "connection reached its age limit" }, + }) +} + +fn flat_cap() -> Value { + json!({ + "type": "error", "code": "websocket_connection_limit_reached", + "message": "connection reached its age limit", + }) +} + +#[tokio::test] +async fn exact_caps_return_a_structured_failure_with_start_and_no_inline_request_or_sse() { + for (session, rejection, response_id, status) in [ + ("cap-400", vec![cap_400()], None, Some(400)), + ("cap-flat", vec![flat_cap()], None, None), + ( + "cap-metadata", + vec![ + json!({ "type": "response.created", "response": { "id": "resp_rejected" } }), + flat_cap(), + ], + Some("resp_rejected"), + None, + ), + ] { + let mut server = mock_server::spawn( + vec![Upgrade::Accept(vec![Turn::Events(rejection)])], + vec![SseReply::Events(response("resp_sse", "never"))], + ) + .await; + let events = stream_openai_responses( + &ws_model(&server), + &context(vec![user("one")]), + Some(&options(Some(session), None)), + ) + .collect() + .await; + assert_eq!( + events + .iter() + .filter(|event| matches!(event, AssistantMessageEvent::Start { .. })) + .count(), + 1, + ); + let Some(AssistantMessageEvent::Error { error, .. }) = events.last() else { + panic!("the session, not the provider, owns cap recovery: {events:?}"); + }; + assert_eq!( + ( + error.stop_reason, + error.response_id.as_deref(), + error.content.len() + ), + (StopReason::Error, response_id, 0), + ); + let details = diagnostic(error, "provider_stream_failure").unwrap()["details"].clone(); + assert_eq!(details["kind"], "connection_limit"); + assert_eq!( + details["providerErrorCode"], + "websocket_connection_limit_reached" + ); + assert_eq!(details.get("status").and_then(Value::as_u64), status); + assert_eq!( + seen(server.drain()), + vec![ + Seen::Upgrade(1), + Seen::Ws { + connection: 1, + previous: None, + inputs: 1 + } + ], + ); + crate::cleanup_session_resources(Some(session)); + } +} + +#[tokio::test] +async fn an_unrelated_400_with_cap_text_does_not_reset_the_socket() { + let mut server = mock_server::spawn( + vec![Upgrade::Accept(vec![Turn::Events(vec![json!({ + "type": "error", "status": 400, + "error": { "type": "invalid_request_error", "code": "bad_input", + "message": "not websocket_connection_limit_reached" }, + })])])], + vec![SseReply::Events(response("resp_sse", "never"))], + ) + .await; + let message = run( + &ws_model(&server), + &context(vec![user("one")]), + &options(Some("cap-unrelated"), None), + ) + .await; + assert_eq!(message.stop_reason, StopReason::Error); + assert_eq!( + diagnostic(&message, "provider_stream_failure").unwrap()["details"]["kind"], + "invalid_request" + ); + assert_eq!( + seen(server.drain()), + vec![ + Seen::Upgrade(1), + Seen::Ws { + connection: 1, + previous: None, + inputs: 1 + }, + ] + ); +} + +#[tokio::test] +async fn a_cap_preserves_failed_partial_content_for_session_rollback() { + let mut partial = + vec![json!({ "type": "response.created", "response": { "id": "resp_partial" } })]; + partial.extend(text_item("resp_partial", "partial").into_iter().take(3)); + partial.push(flat_cap()); + let mut server = mock_server::spawn( + vec![Upgrade::Accept(vec![Turn::Events(partial)])], + vec![SseReply::Events(response("resp_sse", "never"))], + ) + .await; + let message = run( + &ws_model(&server), + &context(vec![user("one")]), + &options(Some("cap-after-content"), None), + ) + .await; + assert_eq!( + (message.stop_reason, text_of(&message)), + (StopReason::Error, "partial".to_string()) + ); + assert_eq!( + diagnostic(&message, "provider_stream_failure").unwrap()["details"]["kind"], + "connection_limit" + ); + assert_eq!( + seen(server.drain()), + vec![ + Seen::Upgrade(1), + Seen::Ws { + connection: 1, + previous: None, + inputs: 1 + }, + ] + ); +} + +#[tokio::test] +async fn frequently_active_cached_sockets_rotate_at_55_minutes_with_full_history() { + let mut server = mock_server::spawn( + vec![ + Upgrade::Accept( + (0..16) + .map(|index| Turn::Events(response(&format!("resp_{index}"), "ok"))) + .collect(), + ), + Upgrade::Accept(vec![Turn::Events(response("resp_fresh", "fresh"))]), + ], + Vec::new(), + ) + .await; + let ws = ws_model(&server); + let request = options(Some("max-age-active"), Some(Transport::WebsocketCached)); + let mut messages = vec![user("one")]; + let mut last = run(&ws, &context(messages.clone()), &request).await; + let connection = super::super::session::cached_connection_id("max-age-active").unwrap(); + for (index, minutes) in [4; 13].into_iter().chain([2, 1]).enumerate() { + messages.extend([Message::Assistant(last), user("next")]); + // Advance only between requests; activity stays below the idle TTL. + tokio::time::pause(); + tokio::time::advance(std::time::Duration::from_mins(minutes)).await; + tokio::time::resume(); + last = run(&ws, &context(messages.clone()), &request).await; + if index < 14 { + assert_eq!( + super::super::session::cached_connection_id("max-age-active"), + Some(connection) + ); + } + } + assert_eq!(text_of(&last), "fresh"); + assert_ne!( + super::super::session::cached_connection_id("max-age-active"), + Some(connection) + ); + let mut expected = vec![ + Seen::Upgrade(1), + Seen::Ws { + connection: 1, + previous: None, + inputs: 1, + }, + ]; + expected.extend((1..15).map(|index| Seen::Ws { + connection: 1, + previous: Some(format!("resp_{}", index - 1)), + inputs: 1, + })); + expected.extend([ + Seen::Upgrade(2), + Seen::Ws { + connection: 2, + previous: None, + inputs: 31, + }, + ]); + assert_eq!(seen(server.drain()), expected); + crate::cleanup_session_resources(Some("max-age-active")); +} diff --git a/crates/pa-ai/src/providers/responses_websocket/mod.rs b/crates/pa-ai/src/providers/responses_websocket/mod.rs index 5bc3e8f782a..6097c4e7db7 100644 --- a/crates/pa-ai/src/providers/responses_websocket/mod.rs +++ b/crates/pa-ai/src/providers/responses_websocket/mod.rs @@ -164,6 +164,7 @@ fn transport_error( ResponsesWsError::Transport(ProviderWsTransportError { message: message.into(), close_code: detail.close_code, + sdk_name: None, transport: Some(detail), }) } @@ -318,9 +319,9 @@ fn response_items(model: &Model, output: &AssistantMessage) -> Vec { /// Port of `processOpenAIResponsesWebSocket`: acquire the session's /// connection, send the (possibly delta) request, and feed its events /// through the shared Responses processor. `started` turns true at the -/// first event of any kind (`response.created` included), when the -/// `start` event is pushed; the caller's SSE fallback is allowed only -/// before that. +/// first event (`response.created` and structured errors included), when +/// the `start` event is pushed. The caller's SSE fallback is allowed only +/// before that; provider verdicts return to the session retry driver. /// /// # Errors /// diff --git a/crates/pa-ai/src/providers/responses_websocket/session.rs b/crates/pa-ai/src/providers/responses_websocket/session.rs index d233131ad40..ded4cf52dd8 100644 --- a/crates/pa-ai/src/providers/responses_websocket/session.rs +++ b/crates/pa-ai/src/providers/responses_websocket/session.rs @@ -3,7 +3,7 @@ //! //! This is provider connection state, not session-engine state: one //! reusable connection per session id, reused only while idle, open, and -//! under the same connection identity (URL plus normalized handshake +//! under the same connection identity (URL plus normalized connection //! headers, credentials included — process-local, never logged or //! persisted). Concurrent acquisitions take generation claims so a later //! claim owns the cache slot even when an earlier connection opens later; @@ -18,6 +18,7 @@ use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, OnceLock}; use std::time::Duration; +use tokio::time::Instant; use tokio_util::sync::CancellationToken; use super::connection::{connect, CloseReason, ConnectFailure, EventDialect, WorkerHandle}; @@ -25,10 +26,13 @@ use super::continuation::ContinuationAnchor; /// Idle connections stay cached this long (TS `CACHE_TTL_MS`). pub(crate) const CONNECTION_IDLE_TTL: Duration = Duration::from_mins(5); +/// Rotate idle sockets on acquisition before the provider's one-hour cap. +const CONNECTION_MAX_AGE: Duration = Duration::from_mins(55); /// The process-local reuse key: URL plus lowercased, sorted handshake -/// headers (a later duplicate replaces an earlier one). It carries the -/// credentials, so `Debug` never prints it. +/// headers except the per-call correlation/idempotency pair (a later +/// duplicate replaces an earlier one). It carries the credentials, so +/// `Debug` never prints it. #[derive(Clone, PartialEq, Eq)] pub(crate) struct ConnectionIdentity(String); @@ -39,9 +43,12 @@ impl std::fmt::Debug for ConnectionIdentity { } pub(crate) fn connection_identity(url: &str, headers: &[(String, String)]) -> ConnectionIdentity { + // These IDs describe a model call, not the connection. They still go + // on fresh handshakes; a reused socket cannot resend updated headers. let normalized: BTreeMap = headers .iter() .map(|(name, value)| (name.to_ascii_lowercase(), value.as_str())) + .filter(|(name, _)| !matches!(name.as_str(), "x-acp-model-request-id" | "idempotency-key")) .collect(); ConnectionIdentity(serde_json::json!([url, normalized]).to_string()) } @@ -49,6 +56,7 @@ pub(crate) fn connection_identity(url: &str, headers: &[(String, String)]) -> Co struct Entry { worker: WorkerHandle, identity: ConnectionIdentity, + opened_at: Instant, busy: bool, /// Bumped on every acquire and release: a pending idle timer fires /// only for the generation it was scheduled under. @@ -251,6 +259,11 @@ pub(crate) async fn acquire( // request ends with the close. old.worker.close(CloseReason::IdentityChanged); retire_old = true; + } else if !old.busy && old.opened_at.elapsed() >= CONNECTION_MAX_AGE { + // Acquisition-only rotation never interrupts a busy request. + // Removing the entry also removes its continuation anchor. + old.worker.close(CloseReason::Done); + retire_old = true; } else if !old.busy && old.worker.is_open() { old.busy = true; reused = Some((old.worker.clone(), old.continuation.clone())); @@ -314,6 +327,7 @@ pub(crate) async fn acquire( Entry { worker: worker.clone(), identity, + opened_at: Instant::now(), busy: true, expiry_generation: 0, continuation: None, @@ -492,7 +506,24 @@ mod tests { }; let base = connection_identity( "ws://h/v1/responses", - &headers(&[("Authorization", "Bearer a"), ("X-Team", "t")]), + &headers(&[ + ("Authorization", "Bearer a"), + ("X-Team", "t"), + ("X-ACP-Model-Request-ID", "edge-1"), + ("Idempotency-Key", "edge-1"), + ]), + ); + assert_eq!( + base, + connection_identity( + "ws://h/v1/responses", + &headers(&[ + ("Authorization", "Bearer a"), + ("X-Team", "t"), + ("x-AcP-MoDeL-ReQuEsT-iD", "edge-2"), + ("IDEMPOTENCY-KEY", "edge-2"), + ]), + ) ); assert_eq!( base, @@ -528,6 +559,13 @@ mod tests { &headers(&[("Authorization", "Bearer a"), ("X-Team", "t")]), ) ); + assert_ne!( + base, + connection_identity( + "ws://h/v1/responses", + &headers(&[("Authorization", "Bearer a"), ("X-Team", "other")]), + ) + ); // The credential-bearing identity never prints. assert_eq!(format!("{base:?}"), "ConnectionIdentity()"); } diff --git a/crates/pa-ai/src/providers/responses_websocket/tests.rs b/crates/pa-ai/src/providers/responses_websocket/tests.rs index 2492d204f92..7934a21a3ba 100644 --- a/crates/pa-ai/src/providers/responses_websocket/tests.rs +++ b/crates/pa-ai/src/providers/responses_websocket/tests.rs @@ -4,6 +4,9 @@ //! continuation, fallback rules, terminal handling, ownership, and the //! transport half of the drop/retry/continuation/fallback scenario. +#[path = "limit_tests.rs"] +mod limit_tests; + use serde_json::{json, Map, Value}; use tokio_util::sync::CancellationToken; @@ -283,7 +286,13 @@ fn upgrade_headers(records: &[Record]) -> Vec<(String, String)> { .filter(|(name, _)| { matches!( name.as_str(), - "x-team" | "x-model-only" | "session_id" | "x-client-request-id" | "authorization" + "x-team" + | "x-model-only" + | "session_id" + | "x-client-request-id" + | "authorization" + | "x-acp-model-request-id" + | "idempotency-key" ) }) .cloned() @@ -511,6 +520,96 @@ async fn full_and_cached_modes_share_the_socket_but_not_the_delta() { } } +/// Per-call IDs change without replacing the cached socket or losing the +/// committed tool response's continuation anchor. Only the first call's +/// IDs travel in the handshake; reused sockets do not refresh headers. +#[tokio::test] +async fn per_call_ids_keep_the_cached_socket_and_tool_delta() { + let mut server = mock_server::spawn( + vec![ + Upgrade::Accept(vec![ + Turn::Events(probe_call("resp_1", "call_1", "once")), + Turn::Events(response("resp_2", "after tool")), + ]), + Upgrade::Accept(vec![Turn::Events(response("resp_2", "after tool"))]), + ], + Vec::new(), + ) + .await; + let ws = ws_model(&server); + let mut request = options(Some("per-call-ids"), Some(Transport::WebsocketCached)); + request.base.headers = Some( + [ + pair("X-ACP-Model-Request-ID", "edge-1"), + pair("Idempotency-Key", "edge-1"), + ] + .into_iter() + .collect(), + ); + let first = run(&ws, &context(vec![user("one")]), &request).await; + let AssistantContent::ToolCall(call) = &first.content[0] else { + panic!("expected the probe call: {first:?}"); + }; + let tool_result = serde_json::from_value(json!({ + "role": "toolResult", "toolCallId": call.id, "toolName": "probe", + "content": [{ "type": "text", "text": "probe ran once" }], + "isError": false, "timestamp": 1, + })) + .expect("tool result message"); + request.base.headers = Some( + [ + pair("x-AcP-MoDeL-ReQuEsT-iD", "edge-2"), + pair("IDEMPOTENCY-KEY", "edge-2"), + ] + .into_iter() + .collect(), + ); + let second = run( + &ws, + &context(vec![user("one"), Message::Assistant(first), tool_result]), + &request, + ) + .await; + assert_eq!(text_of(&second), "after tool"); + let records = server.drain(); + assert_eq!( + upgrade_headers(&records), + vec![ + pair("authorization", "Bearer test-key"), + pair("idempotency-key", "edge-1"), + pair("session_id", "per-call-ids"), + pair("x-acp-model-request-id", "edge-1"), + pair("x-client-request-id", "per-call-ids"), + ] + ); + assert_eq!( + seen(records.clone()), + vec![ + Seen::Upgrade(1), + Seen::Ws { + connection: 1, + previous: None, + inputs: 1 + }, + Seen::Ws { + connection: 1, + previous: Some("resp_1".to_string()), + inputs: 1 + }, + ] + ); + let Record::WsRequest { body, .. } = &records[2] else { + panic!("expected the continued request: {records:?}"); + }; + assert_eq!( + body["input"], + json!([{ + "type": "function_call_output", "call_id": "call_1", "output": "probe ran once", + }]) + ); + crate::cleanup_session_resources(Some("per-call-ids")); +} + /// A body change (here the model) or a rewritten history invalidates the /// anchor: the request goes out in full on the same socket. #[tokio::test] @@ -1534,7 +1633,7 @@ async fn an_identity_change_closes_the_busy_socket_under_its_request() { ) .await; let ws = ws_model(&server); - let stalled = stream_openai_responses( + let mut stalled = stream_openai_responses( &ws, &context(vec![user("one")]), Some(&options(Some("rotate-busy"), None)), @@ -1547,6 +1646,10 @@ async fn an_identity_change_closes_the_busy_socket_under_its_request() { server.next_request().await, Record::WsRequest { connection: 1, .. } )); + assert!(matches!( + stalled.next_event().await, + Some(crate::types::AssistantMessageEvent::Start { .. }) + )); let mut rotated = options(Some("rotate-busy"), None); rotated.base.api_key = Some("rotated-key".to_string()); let fresh = run(&ws, &context(vec![user("two")]), &rotated).await; diff --git a/crates/pa-ai/src/providers/responses_websocket/wire_tests.rs b/crates/pa-ai/src/providers/responses_websocket/wire_tests.rs index dea20bab049..b64f49c8b81 100644 --- a/crates/pa-ai/src/providers/responses_websocket/wire_tests.rs +++ b/crates/pa-ai/src/providers/responses_websocket/wire_tests.rs @@ -181,6 +181,7 @@ fn socket_ends_map_to_structured_causes() { ProviderWsTransportError { message: "WebSocket closed before response.completed".to_string(), close_code: Some(1006), + sdk_name: None, transport: Some(StreamTransportFailureDetail { close_code: Some(1006), was_clean: Some(false), @@ -205,6 +206,7 @@ fn socket_ends_map_to_structured_causes() { ProviderWsTransportError { message: "WebSocket stream closed before response.completed".to_string(), close_code: None, + sdk_name: None, transport: Some(StreamTransportFailureDetail::websocket( StreamTransportFailureCause::Eof )), diff --git a/crates/pa-ai/src/providers/simple_options.rs b/crates/pa-ai/src/providers/simple_options.rs index 0b1fc78318f..08a4cd50dfd 100644 --- a/crates/pa-ai/src/providers/simple_options.rs +++ b/crates/pa-ai/src/providers/simple_options.rs @@ -5,20 +5,29 @@ use crate::types::{ Model, ModelThinkingLevel, SimpleStreamOptions, StreamOptions, ThinkingBudgets, }; -/// The per-request output cap when the caller sets no `max_tokens` (TS -/// `buildBaseOptions`: `Math.min(model.maxTokens, 32000)`). +/// The default ceiling on requested output tokens (TS +/// `DEFAULT_MAX_OUTPUT_TOKENS`): most catalog models advertise a far larger +/// `maxTokens` than a turn needs, so requests are capped unless the value +/// was configured explicitly. pub const REQUEST_MAX_TOKENS_CAP: u64 = 32_000; /// The smallest output budget a request may keep after clamping (TS /// `adjustMaxTokensForThinking`: `minOutputTokens`). pub const MIN_OUTPUT_TOKENS: u64 = 1_024; -/// The default per-request output budget for a model (TS `buildBaseOptions`): -/// the model's max output capped at [`REQUEST_MAX_TOKENS_CAP`], or `None` when -/// the model declares no max output (providers that default server-side). +/// The default per-request output budget for a model (TS `resolveMaxTokens`): +/// an explicitly configured `maxTokens` passes through unchanged; a catalog +/// value is capped at [`REQUEST_MAX_TOKENS_CAP`]; `None` when the model +/// declares no max output (providers that default server-side). #[must_use] pub fn default_request_max_tokens(model: &Model) -> Option { - (model.max_tokens > 0).then(|| model.max_tokens.min(REQUEST_MAX_TOKENS_CAP)) + if model.max_tokens == 0 { + return None; + } + if model.max_tokens_explicit { + return Some(model.max_tokens); + } + Some(model.max_tokens.min(REQUEST_MAX_TOKENS_CAP)) } pub fn build_base_options( @@ -102,7 +111,12 @@ pub fn adjust_max_tokens_for_thinking( } .unwrap_or(min_thinking_tokens); let mut thinking_budget = level_budget.max(min_thinking_tokens); - let max_tokens = (base_max_tokens + thinking_budget).min(model_max_tokens); + // Saturating: an explicitly configured `maxTokens` may be any nonzero + // u64, so the base + budget sum can reach the integer ceiling before + // the model-max clamp ever runs. + let max_tokens = base_max_tokens + .saturating_add(thinking_budget) + .min(model_max_tokens); if max_tokens <= min_thinking_tokens { return Err( "Budget-based thinking requires at least 1024 thinking tokens plus room for the response" @@ -167,6 +181,108 @@ mod tests { .expect("test model") } + fn explicit_model(api: &str, id: &str, max_tokens: u64) -> Model { + serde_json::from_value(json!({ + "id": id, "name": id, "api": api, "provider": "p", + "baseUrl": "http://localhost", "reasoning": true, "input": ["text"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 200_000, "maxTokens": max_tokens, + "maxTokensExplicit": true, + })) + .expect("test model") + } + + #[test] + fn a_catalog_model_is_capped_at_the_default_output_ceiling() { + // TS resolveMaxTokens: a catalog value above the ceiling clamps to + // 32000 — most catalog models advertise far more than a turn needs. + let catalog = model("openai-completions", "gpt-x", 131_072); + assert_eq!(default_request_max_tokens(&catalog), Some(32_000)); + assert_eq!( + build_base_options(&catalog, None, None).max_tokens, + Some(32_000) + ); + } + + #[test] + fn an_explicitly_configured_max_tokens_passes_through_unchanged() { + // The #755 fix: a configured value bypasses the ceiling — the + // reporter's models.json entry asking for 131072 arrives whole. + let explicit = explicit_model("openai-completions", "glm-5.2", 131_072); + assert_eq!(default_request_max_tokens(&explicit), Some(131_072)); + assert_eq!( + build_base_options(&explicit, None, None).max_tokens, + Some(131_072) + ); + } + + #[test] + fn an_explicit_model_below_the_ceiling_keeps_its_value() { + // min semantics: an explicit 8000 stays 8000. + let small = explicit_model("openai-completions", "glm-5.2", 8_000); + assert_eq!( + build_base_options(&small, None, None).max_tokens, + Some(8_000) + ); + } + + #[test] + fn a_model_without_max_tokens_has_no_output_budget_even_when_flagged() { + // maxTokens <= 0 sends no cap field (providers default server-side). + let bare = model("openai-completions", "gpt-x", 0); + assert_eq!(default_request_max_tokens(&bare), None); + assert_eq!(build_base_options(&bare, None, None).max_tokens, None); + let flagged = explicit_model("openai-completions", "glm-5.2", 0); + assert_eq!(build_base_options(&flagged, None, None).max_tokens, None); + } + + #[test] + fn the_effective_budget_reserves_an_explicit_output_ceiling_too() { + // The compaction threshold consumer (`effective_request_max_tokens`) + // must reserve the LARGER explicit budget, or a request can claim + // input + 131072 > contextWindow while the trigger says "not due". + // The thinking fold still caps at the model's declared max output. + let explicit = explicit_model("anthropic", "claude-sonnet-4-5", 131_072); + assert_eq!( + effective_request_max_tokens(&explicit, ModelThinkingLevel::Off), + 131_072 + ); + assert_eq!( + effective_request_max_tokens(&explicit, ModelThinkingLevel::High), + 131_072 + ); + // The fold itself still caps at the model's declared max output: + // an explicit base IS the model's max (the flag rides the same + // value), so the thinking fold cannot exceed it. + let roomy = explicit_model("anthropic", "claude-sonnet-4-5", 96_000); + assert_eq!( + effective_request_max_tokens(&roomy, ModelThinkingLevel::Medium), + 96_000 + ); + } + + #[test] + fn an_explicit_budget_at_the_integer_ceiling_never_overflows_the_fold() { + // An explicitly configured maxTokens may be any nonzero u64, so the + // thinking fold's base + budget addition must saturate before the + // model-max clamp. + let huge = explicit_model("anthropic", "claude-sonnet-4-5", u64::MAX); + assert_eq!( + effective_request_max_tokens(&huge, ModelThinkingLevel::High), + u64::MAX + ); + // The fold itself stays bounded at the model's declared max: the + // wrapped sum (a few thousand) must never stand in for a huge one. + let (max_tokens, _) = adjust_max_tokens_for_thinking( + u64::MAX - 100, + u64::MAX - 100, + ModelThinkingLevel::High, + None, + ) + .unwrap(); + assert_eq!(max_tokens, u64::MAX - 100); + } + #[test] fn budget_folding_providers_add_the_thinking_budget_on_top() { // A non-adaptive Anthropic model: `high` folds 16_384 thinking @@ -213,6 +329,26 @@ mod tests { ); } + #[test] + fn a_per_request_max_tokens_wins_over_the_model_value_and_the_ceiling() { + // TS `buildBaseOptions`: a per-request `options.maxTokens` keeps + // precedence over both the model's declared value and the default + // ceiling (the bypass #755's reporter observed, unchanged by the + // explicit-flag fix). + let openai = model("openai-completions", "gpt-x", 65_536); + let options = SimpleStreamOptions { + base: crate::types::StreamOptions { + max_tokens: Some(64_000), + ..Default::default() + }, + ..Default::default() + }; + assert_eq!( + build_base_options(&openai, Some(&options), None).max_tokens, + Some(64_000) + ); + } + #[test] fn a_model_without_a_declared_max_output_has_no_budget_to_fold() { let bare = model("anthropic", "claude-sonnet-4-5", 0); diff --git a/crates/pa-ai/src/providers/transform_messages.rs b/crates/pa-ai/src/providers/transform_messages.rs index 37b6a56251d..b12f84d68bc 100644 --- a/crates/pa-ai/src/providers/transform_messages.rs +++ b/crates/pa-ai/src/providers/transform_messages.rs @@ -308,6 +308,7 @@ mod tests { cost: crate::types::zero_model_cost(), context_window: 128_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-ai/src/stream.rs b/crates/pa-ai/src/stream.rs index c00779d66bf..5433ab27894 100644 --- a/crates/pa-ai/src/stream.rs +++ b/crates/pa-ai/src/stream.rs @@ -168,9 +168,10 @@ pub fn stream_simple( let provider = resolve_provider(&model.api)?; let mut options = options; // A `None` caller still gets the default output budget downstream - // (`build_base_options` fills `min(model.maxTokens, 32000)`), so - // materialize the options to clamp that default too. The materialized - // default builds the same request otherwise. + // (`build_base_options` fills the per-model default: an explicit + // `maxTokens` unchanged, a catalog value `min(model.maxTokens, 32000)`), + // so materialize the options to clamp that default too. The + // materialized default builds the same request otherwise. let simple = options.get_or_insert_with(SimpleStreamOptions::default); clamp_output_budget(model, context, Some(&mut simple.base)); Ok(provider.stream_simple(model, context, options.as_ref())) diff --git a/crates/pa-ai/src/utils_inner/http.rs b/crates/pa-ai/src/utils_inner/http.rs index f895860a9b9..5dae3e0bd93 100644 --- a/crates/pa-ai/src/utils_inner/http.rs +++ b/crates/pa-ai/src/utils_inner/http.rs @@ -208,6 +208,19 @@ pub async fn send(request: RequestOptions) -> Result`. +/// A WebSocket transport failure with independent classification and +/// diagnostic identity. Codex preserves its runtime class and numeric +/// code; generic Responses uses the structured transport class. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProviderWsTransportError { pub message: String, pub close_code: Option, - /// The structured transport classification, when the transport - /// records one (the generic Responses WebSocket path). + /// Explicit runtime class, when the provider preserves one. + pub sdk_name: Option, pub transport: Option, } impl ProviderWsTransportError { - /// The TS runtime/WS error class name recorded in the diagnostic - /// (`error.name`): the structured class for the generic transport, - /// otherwise the close-code presence decides it. + /// The diagnostic class, independent of the retry classification. #[must_use] - pub fn error_name(&self) -> &'static str { + pub fn error_name(&self) -> &str { + if let Some(name) = self.sdk_name.as_deref() { + return name; + } match (&self.transport, self.close_code) { (Some(_), _) => "WebSocketTransportError", (None, Some(_)) => "WebSocketCloseError", @@ -527,14 +527,13 @@ impl ProviderWsTransportError { } } - /// The TS diagnostic's `error.code`: the codex close error carries the - /// numeric close code; the structured transport error has no `code` - /// property (its close code lives in the classification detail). + /// Codex keeps its numeric runtime code even with transport detail. #[must_use] pub fn diagnostic_code(&self) -> Option { - match self.transport { - Some(_) => None, - None => self.close_code, + if self.sdk_name.is_some() || self.transport.is_none() { + self.close_code + } else { + None } } } @@ -627,48 +626,25 @@ impl From for ProviderError { } } -const KIND_MESSAGES: &[(StreamFailureKind, &str)] = &[ - (StreamFailureKind::Refusal, "Model refused to respond"), - ( - StreamFailureKind::Safety, - "Response blocked by provider safety filters", - ), - (StreamFailureKind::Overloaded, "Provider overloaded"), - (StreamFailureKind::RateLimit, "Provider rate limit exceeded"), - (StreamFailureKind::ServerError, "Provider server error"), - (StreamFailureKind::Auth, "Provider authentication failed"), - ( - StreamFailureKind::Permission, - "Provider denied access to the requested resource", - ), - ( - StreamFailureKind::InvalidRequest, - "Provider rejected the request", - ), - ( - StreamFailureKind::MalformedResponse, - "Provider returned a malformed response", - ), - ( - StreamFailureKind::PaymentRequired, - "Provider requires payment", - ), - ( - StreamFailureKind::StreamDrop, - "Provider dropped the response stream", - ), - ( - StreamFailureKind::Transport, - "Connection to the provider was lost before the response completed", - ), - (StreamFailureKind::Unknown, "Provider stream failed"), -]; - fn kind_message(kind: StreamFailureKind) -> &'static str { - KIND_MESSAGES - .iter() - .find(|(candidate, _)| *candidate == kind) - .map_or("Provider stream failed", |(_, message)| *message) + match kind { + StreamFailureKind::Refusal => "Model refused to respond", + StreamFailureKind::Safety => "Response blocked by provider safety filters", + StreamFailureKind::PaymentRequired => "Provider requires payment", + StreamFailureKind::Overloaded => "Provider overloaded", + StreamFailureKind::RateLimit => "Provider rate limit exceeded", + StreamFailureKind::ServerError => "Provider server error", + StreamFailureKind::Auth => "Provider authentication failed", + StreamFailureKind::Permission => "Provider denied access to the requested resource", + StreamFailureKind::InvalidRequest => "Provider rejected the request", + StreamFailureKind::ConnectionLimit => "Provider connection reached its limit", + StreamFailureKind::MalformedResponse => "Provider returned a malformed response", + StreamFailureKind::StreamDrop => "Provider dropped the response stream", + StreamFailureKind::Transport => { + "Connection to the provider was lost before the response completed" + } + StreamFailureKind::Unknown => "Provider stream failed", + } } /// Build a user-facing message like "Provider overloaded (`overloaded_error`, 529) [`request_id`: `req_abc`]". diff --git a/crates/pa-ai/src/utils_inner/stream_failure/tests.rs b/crates/pa-ai/src/utils_inner/stream_failure/tests.rs index 98a85013822..53c2a0107dd 100644 --- a/crates/pa-ai/src/utils_inner/stream_failure/tests.rs +++ b/crates/pa-ai/src/utils_inner/stream_failure/tests.rs @@ -527,6 +527,7 @@ fn structured_transport_failures_classify_as_transport() { let error = ProviderError::Transport(ProviderWsTransportError { message: "WebSocket closed before response.completed".to_string(), close_code: Some(1006), + sdk_name: None, transport: Some(StreamTransportFailureDetail { close_code: Some(1006), was_clean: Some(false), diff --git a/crates/pa-cli/README.md b/crates/pa-cli/README.md index 6a216c0422d..ca9d58cdfc8 100644 --- a/crates/pa-cli/README.md +++ b/crates/pa-cli/README.md @@ -50,6 +50,15 @@ prompt wait (the raw error to stderr, exit 1, no later prompts). Client commands (`/model`, `/tree`, ...) stay model prompts — TS `_normalizeSubmission` classifies only the four session commands before admission, so they never cross the turn boundary. +Direct print retry composition (`print_retry.rs`) supplies an invocation-owned +cancellation signal (Ctrl+C aborts without killing the process) and serializes +pa-core's `RetryUpdate` onto the existing retry/message event shapes. The CLI +does not own retry episodes, semantic parking, or outcome persistence; those +live in `SessionEngine::run_prompt_with_retry`. Hosted print keeps the daemon +driver and does not stack another retry loop. A connection-cap refresh projects as +`auto_retry_start` with the existing optional `reason: "connection_reset"`, attempt 1, +max 1, and delay 0. Its End/outcome row uses the unchanged ordinary retry count (zero +when only the connection refreshed). No CLI-owned retry policy or new wire fields are added. The json stream emits the TS session-event surface byte-for-shape: the session header row (version 3), the `message_update` streaming deltas with the slim `assistantMessageEvent` (the daemon wire drops the nested diff --git a/crates/pa-cli/src/client_settings.rs b/crates/pa-cli/src/client_settings.rs index ea75bf2c417..df8f3e62ee9 100644 --- a/crates/pa-cli/src/client_settings.rs +++ b/crates/pa-cli/src/client_settings.rs @@ -181,6 +181,13 @@ impl ClientSettings for CliClientSettings { get_chat_detail, set_chat_detail ); + setting!( + factory_enabled, + set_factory_enabled, + get_factory_enabled, + set_factory_enabled, + bool + ); setting!( warnings_anthropic_extra_usage, set_warnings_anthropic_extra_usage, @@ -227,6 +234,33 @@ impl ClientSettings for CliClientSettings { .wire_name() .to_string() } + + fn telemetry_status(&self) -> String { + pa_core::session_engine::telemetry::telemetry_status_text(&self.manager(), &self.agent_dir) + } + + fn set_telemetry_enabled(&self, enabled: bool) -> Result { + pa_core::session_engine::telemetry::set_telemetry_enabled_text( + &mut self.manager(), + &self.agent_dir, + enabled, + ) + } + + fn telemetry_notice_due(&self) -> bool { + // TS agent-session-services: telemetry enabled, onboarding + // already shown (a first interactive launch belongs to the + // onboarding screen; the notice surfaces on the next launch), + // and the notice not yet shown. + let manager = self.manager(); + pa_core::session_engine::telemetry::telemetry_switch(&manager).enabled() + && manager.get_onboarding_shown() + && !manager.get_telemetry_notice_shown() + } + + fn set_telemetry_notice_shown(&self) -> Result<()> { + self.manager().set_telemetry_notice_shown(true) + } } #[cfg(test)] @@ -282,5 +316,27 @@ mod tests { // ...and the /nightly on path's. settings.set_update_channel("nightly").expect("channel"); assert_eq!(settings.update_channel().as_deref(), Some("nightly")); + + // The factory's opt-in gate (`/factory on|off|status`): unset reads + // as disabled (the default off), and the write persists the exact + // nested-camelCase key the kernel's gate and the daemon's lane + // advertisement read -- over the same document, leaving the other + // keys alone. + assert!(!settings.factory_enabled()); + settings.set_factory_enabled(true).expect("factory enabled"); + assert!(settings.factory_enabled()); + let content = + std::fs::read_to_string(agent_dir.join("settings.json")).expect("settings file"); + let value: serde_json::Value = serde_json::from_str(&content).expect("parse"); + assert_eq!(value["factory"]["enabled"], true); + assert_eq!(value["theme"], "dark", "the write leaves the other keys"); + settings + .set_factory_enabled(false) + .expect("factory disabled"); + assert!(!settings.factory_enabled()); + let content = + std::fs::read_to_string(agent_dir.join("settings.json")).expect("settings file"); + let value: serde_json::Value = serde_json::from_str(&content).expect("parse"); + assert_eq!(value["factory"]["enabled"], false); } } diff --git a/crates/pa-cli/src/codex_subscription_login.rs b/crates/pa-cli/src/codex_subscription_login.rs index 33d59bbf67a..9bda9fe61a8 100644 --- a/crates/pa-cli/src/codex_subscription_login.rs +++ b/crates/pa-cli/src/codex_subscription_login.rs @@ -133,7 +133,6 @@ mod tests { use super::*; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; - use std::time::Duration; use pa_ai::oauth::CodexHttpResponse; @@ -387,72 +386,4 @@ mod tests { Some(account_jwt("acct-2")) ); } - - #[tokio::test] - async fn the_panel_surface_mounts_the_ts_prompts() { - // The panel adapter's seam: on_auth renders the url block, the - // manual paste mounts with the TS prompt, and the fallback - // prompt passes the flow's message. The paste/prompt futures - // are LAZY (the request sends on first poll), so the test - // spawns them and reads the requests off the channel's - // receiving side, bounded by a recv timeout that fails the - // test — never a green-on-timeout retry. - let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); - let panel = AuthPanelHandle::new(tx); - let ui = PanelCodexLoginUi::new(panel); - ui.on_auth("https://auth.example/authorize", "Open your browser."); - // The url block sends immediately (a plain request, not a - // future). - let url_request = rx.recv().await.expect("the url block sends"); - let pa_tui::auth_panel::AuthPanelRequest::AuthUrl { url, instructions } = url_request - else { - panic!("expected the url block request") - }; - assert_eq!(url, "https://auth.example/authorize"); - assert_eq!(instructions.as_deref(), Some("Open your browser.")); - - // The manual paste: spawned (the boxed future sends its - // PastePrompt on first poll, then pends on the answer this test - // never gives). - let manual = ui - .on_manual_code_input() - .expect("the panel supplies the paste"); - let manual_task = tokio::spawn(async move { - let _ = manual.await; - }); - let paste_request = tokio::time::timeout(Duration::from_secs(2), rx.recv()) - .await - .expect("the paste request sends once polled") - .expect("the channel stays open"); - let pa_tui::auth_panel::AuthPanelRequest::PastePrompt { prompt, .. } = paste_request else { - panic!("expected the paste request") - }; - assert_eq!( - prompt, - "Paste redirect URL below, or complete login in browser:" - ); - - // The fallback prompt carries the flow's own message. - let prompt = ui.on_prompt("Paste the authorization code (or full redirect URL):"); - let prompt_task = tokio::spawn(async move { - let _ = prompt.await; - }); - let prompt_request = tokio::time::timeout(Duration::from_secs(2), rx.recv()) - .await - .expect("the prompt request sends once polled") - .expect("the channel stays open"); - let pa_tui::auth_panel::AuthPanelRequest::PastePrompt { prompt, .. } = prompt_request - else { - panic!("expected the prompt request") - }; - assert_eq!( - prompt, - "Paste the authorization code (or full redirect URL):" - ); - - // The pending paste/prompt tasks are dropped: no answer is - // expected (the test never mounts the panel). - manual_task.abort(); - prompt_task.abort(); - } } diff --git a/crates/pa-cli/src/command_registry.rs b/crates/pa-cli/src/command_registry.rs index d4fd2d2e74e..5b085cbc6c7 100644 --- a/crates/pa-cli/src/command_registry.rs +++ b/crates/pa-cli/src/command_registry.rs @@ -116,6 +116,14 @@ pub const COMMAND_SPECS: &[CommandSpec] = &[ "status [--json]", "Show background service status", ), + CommandSpec::new( + &["telemetry"], + "telemetry [status|on|off]", + "Show or change usage telemetry", + ) + .description( + "Prime Agent sends pseudonymous usage and performance metrics, never prompts, responses, tool content, file paths, or repository data. status (the default) shows whether telemetry is on and why, where it sends, and the installation id; on and off save the choice in settings.", + ), CommandSpec::new( &["doctor"], "doctor [--fix] [--json]", @@ -204,22 +212,22 @@ pub const COMMAND_SPECS: &[CommandSpec] = &[ .options(&[ "--check Print the update channel's latest release vs the running version, without installing", "--force Reinstall the latest build of the channel", - "--rollback Restore the previous compiled release (the managed-install flow)", + "--rollback Restore the previous version the last update kept", "--nightly Switch updates to the nightly channel (the latest main build)", "--stable Return updates to the stable channel", - "--archive Install a local release payload (the managed-install flow)", - "--source The https:// origin recorded as the release's install source (required with --archive)", + "--archive Install a local release archive", + "--source The https:// origin recorded as the release's install source (managed installs; required there with --archive)", ]) .description( "Move from the TypeScript version to the Rust port in one step: `prime-agent update` \ - fetches the installer from the official domain endpoint \ - (https://app.primeintellect.ai/prime-agent/install.sh — never a GitHub raw or workflow \ - URL) and runs it, which uninstalls the TypeScript version and installs the latest Rust \ - build; your sessions and configuration (~/.prime/agent) are never touched. Restart \ - prime-agent after the update to run the new build. This command exists only in the \ - Rust binary — the TypeScript version does not have it; the move happens when you run \ - the installer's curl|sh URL (the README's Install section) or `prime-agent update` \ - (after the Rust install exists).", + fetches the update channel's installer (stable: \ + https://app.primeintellect.ai/prime-agent/install.sh; nightly: install-beta.sh from the \ + release download base — never a GitHub raw or workflow URL) and runs it, which \ + uninstalls the TypeScript version and installs the latest Rust build; your sessions and \ + configuration (~/.prime/agent) are never touched. Restart prime-agent after the update \ + to run the new build. This command exists only in the Rust binary — the TypeScript \ + version does not have it; the move happens when you run the installer's curl|sh URL (the \ + README's Install section) or `prime-agent update` (after the Rust install exists).", ), CommandSpec::new(&["model"], "model list [search]", "Inspect available models"), CommandSpec::new(&["model", "list"], "model list [search]", "List available models"), @@ -234,6 +242,51 @@ pub const COMMAND_SPECS: &[CommandSpec] = &[ "Export a saved session to HTML", ), CommandSpec::new(&["config"], "config", "Configure package resources"), + CommandSpec::new( + &["factory"], + "factory ", + "Share and run factory machines from the machine library", + ) + .description( + "Machines are MACHINE.md templates (frontmatter plus a fenced machine-spec block) \ +resolved from the bundled library shipped inside the runtime, then from the personal \ +library under the agent dir. Run one with `await rlm.factory.run(\"\")` from a session.", + ), + CommandSpec::new( + &["factory", "list"], + "factory list [--json]", + "List the machine library", + ) + .description( + "Lists the bundled machines and the personal machines with their descriptions; \ +files that fail the parser skip with a warning.", + ) + .options(&["--json Print JSON"]), + CommandSpec::new( + &["factory", "import"], + "factory import [--json]", + "Validate and import a MACHINE.md into the personal library", + ) + .description( + "The machine's spec passes the kernel's write-time validator; an invalid spec \ +never persists and the exact errors print verbatim.", + ) + .options(&["--json Print JSON"]), + CommandSpec::new( + &["factory", "export"], + "factory export --out [--json]", + "Export a machine to a MACHINE.md file", + ) + .description( + "Resolves the library machine named (bundled first, then the personal \ +library) and copies its MACHINE.md to the output path; an existing target is \ +refused, never overwritten.", + ) + .options(&[ + "--out Destination MACHINE.md path", + "--json Print JSON", + ]) + .examples(&["factory export review-sweep --out shared-review-sweep.MACHINE.md"]), CommandSpec::new( &["prompt"], "prompt [--model ] [--cwd ] [--tools ] [--no-tools] [--no-builtin-tools] [--json]", @@ -335,7 +388,7 @@ const TOP_LEVEL_OPTION_GROUPS: &[OptionGroup] = &[ ], }, OptionGroup { - heading: "Tool and resource options", + heading: "Resource options", options: &[ ( "-t, --tools ", diff --git a/crates/pa-cli/src/daemon_client.rs b/crates/pa-cli/src/daemon_client.rs index d67fbf221cf..c8796bd33cb 100644 --- a/crates/pa-cli/src/daemon_client.rs +++ b/crates/pa-cli/src/daemon_client.rs @@ -37,6 +37,9 @@ const READ_POLL: Duration = Duration::from_millis(50); pub(crate) struct DaemonClient { socket_path: PathBuf, reader: BufReader>, + /// Bytes of a line whose newline has not arrived yet, kept across poll + /// timeouts and reads. + partial_line: Vec, writer: Box, hello: Option, daemon_closing_reason: Option, @@ -65,10 +68,14 @@ impl DaemonClient { fn connect_raw(socket_path: &Path) -> std::io::Result { let stream = connect_blocking(socket_path)?; + // Darwin rejects socket options after peer close, even with a reply + // still queued. Configure polling before any request can be sent. + stream.set_read_timeout(READ_POLL)?; let writer = stream.try_clone_box()?; Ok(DaemonClient { socket_path: socket_path.to_path_buf(), reader: BufReader::new(stream), + partial_line: Vec::new(), writer, hello: None, daemon_closing_reason: None, @@ -179,12 +186,7 @@ impl DaemonClient { operation: Operation<'_>, ) -> Result { loop { - self.reader - .get_mut() - .set_read_timeout(READ_POLL) - .map_err(|error| anyhow!("daemon socket error: {error}"))?; - let mut line = String::new(); - let read = self.reader.read_line(&mut line); + let read = self.reader.read_until(b'\n', &mut self.partial_line); match read { Ok(0) => { let reason = self @@ -198,6 +200,9 @@ impl DaemonClient { )); } Ok(_) => { + let line = String::from_utf8(std::mem::take(&mut self.partial_line)).map_err( + |_| anyhow!("daemon socket error: stream did not contain valid UTF-8"), + )?; if line.trim().is_empty() { continue; } @@ -317,8 +322,72 @@ mod tests { }); let mut client = DaemonClient::connect(&socket).unwrap(); let response = client.request(list_command("daemon_1")).unwrap(); - assert!(response.success); - assert!(server.join().is_ok()); + assert_eq!( + response, + DaemonResponse { + id: Some("daemon_1".to_string()), + command: "list".to_string(), + success: true, + data: Some(json!({ "sessions": [] })), + error: None, + error_info: None, + } + ); + server.join().unwrap(); + } + + /// The peer has closed before either read starts. Drain the replies, + /// including any bytes read ahead into the reader's buffer. + #[test] + fn read_line_drains_replies_after_peer_close() { + let dir = tempfile::TempDir::new().unwrap(); + let socket = dir.path().join("daemon.sock"); + let listener = UnixListener::bind(&socket).unwrap(); + let server = std::thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut writer = stream.try_clone().unwrap(); + let mut reader = BufReader::new(stream); + writer + .write_all(b"{\"type\":\"daemon_hello\",\"protocol\":{\"name\":\"prime-agent.daemon\",\"version\":7}}\n") + .unwrap(); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + let request: serde_json::Value = serde_json::from_str(line.trim()).unwrap(); + assert_eq!(request["command"]["id"], request["id"]); + let id = request["id"].as_str().unwrap(); + let replies = [ + "{\"id\":\"daemon_other\",\"type\":\"response\",\"command\":\"list\",\"success\":true,\"data\":{\"sessions\":[]}}\n" + .to_string(), + format!( + "{{\"id\":\"{id}\",\"type\":\"response\",\"command\":\"list\",\"success\":true,\"data\":{{\"sessions\":[]}}}}\n" + ), + ]; + writer.write_all(replies.concat().as_bytes()).unwrap(); + replies + }); + let mut client = DaemonClient::connect(&socket).unwrap(); + let hello = client.wait_for_hello(HELLO_TIMEOUT_MS).unwrap(); + let envelope = json!({ + "type": "command", + "id": "daemon_1", + "protocol": daemon_protocol(&hello).unwrap(), + "clientId": client.protocol_client_id, + "command": list_command("daemon_1"), + }); + writeln!(client.writer, "{envelope}").unwrap(); + client.writer.flush().unwrap(); + // Joining observes both peer handles being dropped, without a sleep. + let replies = server.join().unwrap(); + for expected in replies { + let line = client + .read_line( + Instant::now() + Duration::from_millis(REQUEST_TIMEOUT_MS), + REQUEST_TIMEOUT_MS, + Operation::Command("list"), + ) + .unwrap(); + assert_eq!(line, expected); + } } #[test] @@ -367,4 +436,71 @@ mod tests { assert!(text.contains("Socket: "), "{text}"); server.join().unwrap(); } + + /// Replays scripted reads; `None` is a poll timeout. + #[derive(Debug)] + struct ScriptedReads(std::collections::VecDeque>); + + impl std::io::Read for ScriptedReads { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self.0.pop_front() { + Some(Some(bytes)) => { + buf[..bytes.len()].copy_from_slice(bytes); + Ok(bytes.len()) + } + Some(None) => Err(std::io::ErrorKind::WouldBlock.into()), + None => Ok(0), + } + } + } + + impl std::io::Write for ScriptedReads { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + + impl BlockingTransportStream for ScriptedReads { + fn try_clone_box(&self) -> std::io::Result> { + Err(std::io::ErrorKind::Unsupported.into()) + } + + fn set_read_timeout(&self, _: Duration) -> std::io::Result<()> { + Ok(()) + } + } + + /// A line split by poll timeouts, one of them inside a multi-byte + /// character, comes back whole. + #[test] + fn read_line_keeps_bytes_across_poll_timeouts() { + let dir = tempfile::TempDir::new().unwrap(); + let socket = dir.path().join("daemon.sock"); + let _listener = UnixListener::bind(&socket).unwrap(); + let mut client = DaemonClient::connect(&socket).unwrap(); + let line: &'static [u8] = "{\"type\":\"daemon_hello\",\"name\":\"é\"}\n".as_bytes(); + let split = line.len() - 4; // between the two bytes of "é" + client.reader = BufReader::new(Box::new(ScriptedReads( + [ + Some(&line[..10]), + None, + Some(&line[10..split]), + None, + Some(&line[split..]), + ] + .into(), + ))); + let read = client + .read_line( + Instant::now() + Duration::from_secs(10), + 10_000, + Operation::Handshake, + ) + .unwrap(); + assert_eq!(read.as_bytes(), line); + } } diff --git a/crates/pa-cli/src/daemon_command.rs b/crates/pa-cli/src/daemon_command.rs index 04334d25611..9f7426c3f90 100644 --- a/crates/pa-cli/src/daemon_command.rs +++ b/crates/pa-cli/src/daemon_command.rs @@ -341,6 +341,7 @@ fn run_rename(client: &mut DaemonClient, args: &[String], json: bool) -> Result< id: None, active_session_id, name: name.clone(), + renamed_by: None, rest: serde_json::Map::new(), })?; let data = require_success(response)?.unwrap_or(Value::Null); @@ -692,6 +693,9 @@ mod tests { list.iter().map(std::string::ToString::to_string).collect() } + // The socket-path/separator shapes are unix paths (the windows transport + // is named pipes; the tilde/socket spellings do not exist there). + #[cfg(unix)] #[test] fn parse_consumes_socket_json_and_separator() { let parsed = diff --git a/crates/pa-cli/src/daemon_discovery/mod.rs b/crates/pa-cli/src/daemon_discovery/mod.rs index d5796197925..661bcacc003 100644 --- a/crates/pa-cli/src/daemon_discovery/mod.rs +++ b/crates/pa-cli/src/daemon_discovery/mod.rs @@ -254,7 +254,8 @@ fn scan_socket_dir(socket_dir: &Path) -> Vec { /// Windows daemon endpoints are named pipes: there is no socket directory to /// sweep, and discovery comes from tracked worker descriptors and the -/// default pipe (TS `scanSocketDir` returns [] on win32). +/// default pipe (see [`discover_daemons_with`]; TS `scanSocketDir` returns +/// [] on win32). #[cfg(not(unix))] fn scan_socket_dir(_socket_dir: &Path) -> Vec { Vec::new() @@ -470,6 +471,17 @@ pub(crate) fn verify_hello_supervisor_pid( /// current root, tests pass their own fixture dirs. A daemon outside the /// root is never discovered, probed, or stopped. pub(crate) fn discover_daemons(root: &DaemonStateRoot) -> Vec { + discover_daemons_with(root, cfg!(windows)) +} + +/// [`discover_daemons`] with the endpoint model explicit. With +/// `named_pipes` (Windows) there is no listener census and no socket dir to +/// sweep, so the fixed daemon pipe is always probed: an idle daemon with no +/// tracked workers is otherwise invisible and `shutdown` would report +/// success without stopping it. A pipe exists only while its server holds +/// it, so an unanswered one with no listener or tracked worker is simply +/// absent - there is no orphan file to report or remove. +fn discover_daemons_with(root: &DaemonStateRoot, named_pipes: bool) -> Vec { let mut process_by_socket = std::collections::HashMap::new(); for daemon in scan_listening_daemons(root) { if is_worker_socket_path(&daemon.socket_path, &root.socket_dir) { @@ -491,12 +503,13 @@ pub(crate) fn discover_daemons(root: &DaemonStateRoot) -> Vec { .filter(|path| !is_worker_socket_path(path, &root.socket_dir)), ) .chain(worker_sockets.iter().cloned()) + .chain(named_pipes.then(|| root.default_socket_path.clone())) .collect(); sockets.retain(|path| state_root_matches(root, path)); let mut infos: Vec = sockets .into_iter() - .map(|socket_path| { + .filter_map(|socket_path| { let proc = process_by_socket.get(&socket_path); let probe = probe_daemon(&socket_path); let pid = proc.map(|daemon| daemon.pid).or_else(|| { @@ -510,10 +523,12 @@ pub(crate) fn discover_daemons(root: &DaemonStateRoot) -> Vec { classify_reachable(&probe) } else if proc.is_some() || has_tracked_workers { DaemonStatus::Unreachable + } else if named_pipes { + return None; } else { DaemonStatus::OrphanFile }; - DaemonInfo { + Some(DaemonInfo { pid_source: pid.map(|_| { if proc.is_some() { PidSource::Listener @@ -533,7 +548,7 @@ pub(crate) fn discover_daemons(root: &DaemonStateRoot) -> Vec { session_count: probe.session_count, status, has_tracked_workers: has_tracked_workers.then_some(true), - } + }) }) .collect(); sort_daemons(&mut infos); @@ -704,6 +719,41 @@ mod tests { assert!(!infos[0].is_default); } + #[cfg(unix)] + #[test] + fn named_pipe_discovery_probes_the_default_endpoint_without_tracked_workers() { + let tmp = tempfile::tempdir().expect("tempdir"); + let mut root = fixture_root(tmp.path()); + // Outside the socket dir, so the socket-dir sweep cannot find it: + // only the named-pipe arm's fixed endpoint does, like the Windows + // daemon pipe. + root.default_socket_path = root.agent_dir.join("daemon-pipe.sock"); + std::fs::create_dir_all(&root.socket_dir).expect("socket dir"); + + // No daemon on the pipe: nothing is reported, not a fake orphan. + assert!(discover_daemons_with(&root, true).is_empty()); + + // An idle daemon with no tracked workers. Accepted connections are + // dropped at once, so the probe is reachable without the hello wait. + let listener = + std::os::unix::net::UnixListener::bind(&root.default_socket_path).expect("bind"); + std::thread::spawn(move || { + for stream in listener.incoming() { + drop(stream); + } + }); + assert!( + discover_daemons_with(&root, false).is_empty(), + "the unix arm only sees socket-dir files and listeners" + ); + let infos = discover_daemons_with(&root, true); + assert_eq!(infos.len(), 1); + assert_eq!(infos[0].socket_path, root.default_socket_path); + assert!(infos[0].is_default); + assert_eq!(infos[0].has_tracked_workers, None); + assert_eq!(infos[0].status, DaemonStatus::Stale); + } + #[test] fn quiet_period_completes_after_the_threshold() { assert!(evaluate_shutdown_quiet_period(1_500, Some(500))); diff --git a/crates/pa-cli/src/factory_command.rs b/crates/pa-cli/src/factory_command.rs new file mode 100644 index 00000000000..be48cb9bc29 --- /dev/null +++ b/crates/pa-cli/src/factory_command.rs @@ -0,0 +1,533 @@ +//! `prime-agent factory`: the machine library commands. +//! +//! Every subcommand — list included — is one `rlm.factory.cli_dispatch` +//! payload through the kernel Python. The kernel owns the whole library +//! contract: the bundled seeds ship as wheel package data inside the +//! runtime, the personal library lives under the agent dir, the strict +//! MACHINE.md parser gates what lists, and the write-time validator gates +//! what persists (an invalid spec never persists; the exact error +//! sentences reach this command's output verbatim). The CLI never +//! re-implements resolution or parsing, so the two sides cannot drift. + +use std::io::Write as _; +use std::path::PathBuf; +use std::process::{Command, Stdio}; + +use serde_json::{json, Value}; + +/// One machine as the kernel lists it (`cli_dispatch`'s `list` op). +#[derive(Debug, Clone, PartialEq, serde::Deserialize)] +struct MachineListing { + name: String, + description: String, + #[serde(default)] + version: String, + #[serde(default)] + author: String, + /// The library level: `repo` (the bundled library) or `user`. + source: String, + path: String, +} + +/// The thin runner executed by the kernel Python: one JSON payload in on +/// stdin, one JSON result out on stdout (`rlm.factory.cli_dispatch`). +const CLI_DISPATCH_RUNNER: &str = concat!( + "import json, sys\n", + "from rlm.factory import cli_dispatch\n", + "payload = json.loads(sys.stdin.read() or \"{}\")\n", + "print(json.dumps(cli_dispatch(payload)))\n", +); + +/// The parsed `prime-agent factory` invocation. +#[derive(Debug, PartialEq, Eq)] +enum FactoryCommand { + List { + json: bool, + }, + Import { + path: String, + json: bool, + }, + Export { + name: String, + out: String, + json: bool, + }, +} + +/// Parse the subcommand and its flags; `Err` carries a usage hint. +fn parse_factory_command(args: &[String]) -> Result { + let children: Vec<&str> = crate::command_registry::get_child_command_specs(&["factory"]) + .into_iter() + .map(|spec| spec.path[spec.path.len() - 1]) + .collect(); + let Some(subcommand) = args.first() else { + return Err(String::new()); + }; + if !children.contains(&subcommand.as_str()) { + let suggestion = crate::command_registry::find_command_suggestion(subcommand, &children); + return Err(format!( + "Unknown factory command: {subcommand}{}", + suggestion.map_or_else(String::new, |s| format!(" (did you mean \"{s}\"?)")) + )); + } + let mut json = false; + let mut operands: Vec = Vec::new(); + let mut out: Option = None; + let mut rest = args[1..].iter(); + while let Some(arg) = rest.next() { + match arg.as_str() { + "--json" => json = true, + "--out" => { + let Some(value) = rest.next() else { + return Err("--out requires a file path".to_string()); + }; + out = Some(value.clone()); + } + other if other.starts_with('-') => { + return Err(format!("unknown option {other:?}")); + } + other => operands.push(other.to_string()), + } + } + match subcommand.as_str() { + "list" => { + if !operands.is_empty() { + return Err("Usage: prime-agent factory list [--json]".to_string()); + } + Ok(FactoryCommand::List { json }) + } + "import" => { + if operands.len() != 1 { + return Err("Usage: prime-agent factory import [--json]".to_string()); + } + Ok(FactoryCommand::Import { + path: operands.remove(0), + json, + }) + } + "export" => { + if operands.len() != 1 { + return Err( + "Usage: prime-agent factory export --out [--json]".to_string(), + ); + } + let Some(out) = out else { + return Err( + "Usage: prime-agent factory export --out [--json]".to_string(), + ); + }; + Ok(FactoryCommand::Export { + name: operands.remove(0), + out, + json, + }) + } + _ => Err(String::new()), + } +} + +/// Run the `factory` command; returns the process exit code. +pub fn run_factory_command(args: &[String]) -> i32 { + let command = match parse_factory_command(args) { + Ok(command) => command, + Err(empty_hint) if empty_hint.is_empty() => { + eprintln!("Missing factory command."); + eprintln!("Run \"prime-agent help factory\" for usage."); + return 1; + } + Err(error) => { + eprintln!("Error: {error}"); + eprintln!("Run `prime-agent help factory` for usage."); + return 1; + } + }; + match command { + FactoryCommand::List { json } => run_list(json), + FactoryCommand::Import { path, json } => run_import(&path, json), + FactoryCommand::Export { name, out, json } => run_export(&name, &out, json), + } +} + +/// The `list` payload: the op alone — the kernel resolves every library +/// directory itself. +fn list_payload() -> Value { + json!({"op": "list"}) +} + +/// The `import` payload: the machine file the user pointed at. +fn import_payload(path: &std::path::Path) -> Value { + json!({ + "op": "import", + "path": path.display().to_string(), + }) +} + +/// The `export` payload: the name and the out target the user chose. +fn export_payload(name: &str, out: &std::path::Path) -> Value { + json!({ + "op": "export", + "name": name, + "out": out.display().to_string(), + }) +} + +/// `factory list`: library contents with descriptions. +fn run_list(json: bool) -> i32 { + let payload = list_payload(); + match resolve_kernel_python().and_then(|python| dispatch_via_kernel(&python, &payload)) { + Ok(result) => render_list(&result, json), + Err(error) => { + eprintln!("Error: {error}"); + 1 + } + } +} + +/// Render one `list` dispatch result: errors verbatim, warnings as +/// warnings, machines as rows (or the whole payload for `--json`). +fn render_list(result: &Value, json: bool) -> i32 { + if let Some(code) = print_dispatch_errors(result) { + return code; + } + if json { + println!( + "{}", + serde_json::to_string_pretty(result).unwrap_or_default() + ); + return 0; + } + let machines: Vec = result + .get("machines") + .cloned() + .map(|value| serde_json::from_value(value).unwrap_or_default()) + .unwrap_or_default(); + for warning in result + .get("warnings") + .and_then(Value::as_array) + .map(|warnings| { + warnings + .iter() + .filter_map(Value::as_str) + .collect::>() + }) + .unwrap_or_default() + { + eprintln!("Warning: {warning}"); + } + if machines.is_empty() { + println!("No machines in the library."); + return 0; + } + let name_width = machines + .iter() + .map(|machine| machine.name.chars().count()) + .max() + .unwrap_or(0); + for machine in &machines { + println!("{}", listing_row(machine, name_width)); + } + 0 +} + +fn pad_end(text: &str, width: usize) -> String { + let length = text.chars().count(); + if length >= width { + return text.to_string(); + } + let mut padded = String::from(text); + padded.extend(std::iter::repeat_n(' ', width - length)); + padded +} + +/// Format one listing row (exposed for the unit tests). +#[must_use] +fn listing_row(machine: &MachineListing, name_width: usize) -> String { + format!( + "{} {} {}", + pad_end(&machine.name, name_width), + machine.source.as_str(), + machine.description + ) +} + +/// Resolve the kernel Python (bootstrapping the venv on first use), the +/// same interpreter the factory gate lives in. +fn resolve_kernel_python() -> Result { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(|error| format!("failed to start the CLI runtime: {error}"))?; + runtime + .block_on(pa_core::kernel::ensure_kernel_python( + pa_core::kernel::EnsureKernelPythonOptions::default(), + )) + .map_err(|error| format!("kernel python unavailable: {error:#}")) +} + +/// Drive one `rlm.factory.cli_dispatch` payload through the kernel Python. +fn dispatch_via_kernel(python: &std::path::Path, payload: &Value) -> Result { + let mut child = Command::new(python); + child + .args(["-c", CLI_DISPATCH_RUNNER]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + // Hidden window on Windows, matching the kernel probes. + pa_core::platform::process::set_no_window(&mut child); + let mut child = child + .spawn() + .map_err(|error| format!("failed to run the kernel python: {error}"))?; + { + let stdin = child.stdin.as_mut().expect("stdin was piped on this child"); + stdin + .write_all(payload.to_string().as_bytes()) + .map_err(|error| format!("failed to send the payload: {error}"))?; + } + let output = child + .wait_with_output() + .map_err(|error| format!("failed to wait for the kernel python: {error}"))?; + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + let tail: Vec<&str> = stderr.lines().rev().take(3).collect::>(); + let mut lines = tail; + lines.reverse(); + return Err(format!( + "the kernel python failed (exit {}): {}", + output.status, + lines.join(" | ") + )); + } + let stdout = String::from_utf8_lossy(&output.stdout); + serde_json::from_str(stdout.trim_end_matches(['\n', '\r'])) + .map_err(|error| format!("unreadable kernel result: {error}")) +} + +/// `factory import `: validate through the kernel gate and persist. +fn run_import(path: &str, json: bool) -> i32 { + let source = crate::config::expand_tilde_path(path); + if !source.is_file() { + eprintln!("Error: machine file not found: {}", source.display()); + return 1; + } + let payload = import_payload(&source); + match resolve_kernel_python().and_then(|python| dispatch_via_kernel(&python, &payload)) { + Ok(result) => print_dispatch_result(&result, json, "imported"), + Err(error) => { + eprintln!("Error: {error}"); + 1 + } + } +} + +/// `factory export --out `: serialize a machine to MACHINE.md. +fn run_export(name: &str, out: &str, json: bool) -> i32 { + let payload = export_payload(name, &crate::config::expand_tilde_path(out)); + match resolve_kernel_python().and_then(|python| dispatch_via_kernel(&python, &payload)) { + Ok(result) => print_dispatch_result(&result, json, "exported"), + Err(error) => { + eprintln!("Error: {error}"); + 1 + } + } +} + +/// The failed dispatch's error sentences (the validator's exact text, +/// unwrapped from the JSON envelope — printing the `Value` would show the +/// JSON quoting around every sentence). +fn dispatch_error_sentences(result: &Value) -> Vec<&str> { + result + .get("errors") + .and_then(Value::as_array) + .map(|errors| errors.iter().filter_map(Value::as_str).collect()) + .unwrap_or_default() +} + +/// Print a failed dispatch's errors verbatim (the validator's exact +/// sentences); `Some(exit code)` when the result is not an ok-payload. +fn print_dispatch_errors(result: &Value) -> Option { + if result.get("ok").and_then(Value::as_bool).unwrap_or(false) { + return None; + } + for error in dispatch_error_sentences(result) { + eprintln!("Error: {error}"); + } + Some(1) +} + +/// Print one dispatch result: errors verbatim (the validator's exact +/// sentences), or the ok-payload as text or JSON. `verb` is the done-word +/// of the subcommand that ran ("imported" or "exported"). +fn print_dispatch_result(result: &Value, json: bool, verb: &str) -> i32 { + if let Some(code) = print_dispatch_errors(result) { + return code; + } + if json { + println!( + "{}", + serde_json::to_string_pretty(result).unwrap_or_default() + ); + return 0; + } + let name = result + .get("name") + .and_then(Value::as_str) + .unwrap_or_default(); + let path = result + .get("path") + .and_then(Value::as_str) + .unwrap_or_default(); + let source = result.get("source").and_then(Value::as_str); + if verb == "exported" { + match source { + Some(source) => println!("Exported {name} to {path} (from {source})."), + None => println!("Exported {name} to {path}."), + } + } else { + println!("Imported {name} into {path}."); + } + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + fn arg(words: &[&str]) -> Vec { + words.iter().map(ToString::to_string).collect() + } + + #[test] + fn parses_the_three_subcommands() { + assert_eq!( + parse_factory_command(&arg(&["list"])), + Ok(FactoryCommand::List { json: false }) + ); + assert_eq!( + parse_factory_command(&arg(&["list", "--json"])), + Ok(FactoryCommand::List { json: true }) + ); + assert_eq!( + parse_factory_command(&arg(&["import", "/tmp/m.MACHINE.md"])), + Ok(FactoryCommand::Import { + path: "/tmp/m.MACHINE.md".to_string(), + json: false + }) + ); + assert_eq!( + parse_factory_command(&arg(&[ + "export", + "sweep", + "--out", + "/tmp/s.MACHINE.md", + "--json" + ])), + Ok(FactoryCommand::Export { + name: "sweep".to_string(), + out: "/tmp/s.MACHINE.md".to_string(), + json: true + }) + ); + } + + #[test] + fn rejects_missing_and_unknown_subcommands() { + assert_eq!(parse_factory_command(&arg(&[])), Err(String::new())); + let unknown = parse_factory_command(&arg(&["run"])); + assert!(unknown.is_err()); + assert!(unknown + .unwrap_err() + .contains("Unknown factory command: run")); + let missing_out = parse_factory_command(&arg(&["export", "sweep"])); + assert!(missing_out.is_err()); + assert!(missing_out + .unwrap_err() + .starts_with("Usage: prime-agent factory export")); + let dangling_out = parse_factory_command(&arg(&["export", "sweep", "--out"])); + assert!(dangling_out.is_err()); + assert!(dangling_out + .unwrap_err() + .starts_with("--out requires a file path")); + let extra = parse_factory_command(&arg(&["list", "extra"])); + assert!(extra.is_err()); + } + + #[test] + fn dispatch_results_print_errors_verbatim() { + let failure: Value = serde_json::from_str( + r#"{"ok": false, "errors": ["run max_parallel must be an integer between 1 and 64"]}"#, + ) + .expect("fixture"); + // Errors go to stderr inside print_dispatch_result; the exit code is the pin. + assert_eq!(print_dispatch_result(&failure, false, "imported"), 1); + assert_eq!(render_list(&failure, false), 1); + // The sentence prints verbatim: unwrapped from the JSON envelope, + // never with JSON quoting. + assert_eq!( + dispatch_error_sentences(&failure), + ["run max_parallel must be an integer between 1 and 64"] + ); + } + + #[test] + fn dispatch_runner_contract_is_the_json_facade() { + assert!(CLI_DISPATCH_RUNNER.contains("from rlm.factory import cli_dispatch")); + assert!(CLI_DISPATCH_RUNNER.contains("print(json.dumps(cli_dispatch(payload)))")); + } + + #[test] + fn payloads_carry_only_what_the_user_typed() { + // The kernel resolves every library directory itself, so the + // payloads carry no directory fields to drift out of sync. + assert_eq!(list_payload(), serde_json::json!({"op": "list"})); + assert_eq!( + import_payload(std::path::Path::new("/tmp/m.MACHINE.md")), + serde_json::json!({"op": "import", "path": "/tmp/m.MACHINE.md"}) + ); + assert_eq!( + export_payload("sweep", std::path::Path::new("/tmp/s.MACHINE.md")), + serde_json::json!({ + "op": "export", + "name": "sweep", + "out": "/tmp/s.MACHINE.md", + }) + ); + } + + #[test] + fn listing_row_renders_name_source_description() { + let listing = MachineListing { + name: "sweep".to_string(), + description: "A machine that sweeps.".to_string(), + version: "1".to_string(), + author: "Tester".to_string(), + source: "repo".to_string(), + path: "/machines/sweep/MACHINE.md".to_string(), + }; + assert_eq!( + listing_row(&listing, 5), + "sweep repo A machine that sweeps." + ); + } + + #[test] + fn render_list_parses_the_kernel_listing() { + let result: Value = serde_json::from_str( + r#"{"ok": true, "machines": [ + {"name": "review-sweep", "description": "Sweep a branch.", "version": "1", "author": "Prime Agent", "source": "repo", "path": "/rlm/machines/review-sweep/MACHINE.md"}, + {"name": "mine", "description": "Personal.", "version": "", "author": "", "source": "user", "path": "/agent/machines/mine/MACHINE.md"} + ], "warnings": []}"#, + ) + .expect("fixture"); + // The rows print to stdout; the exit code pins the ok-path. + assert_eq!(render_list(&result, false), 0); + assert_eq!(render_list(&result, true), 0); + } + + #[test] + fn render_list_exits_cleanly_with_no_machines() { + let result: Value = serde_json::from_str(r#"{"ok": true, "machines": [], "warnings": []}"#) + .expect("fixture"); + assert_eq!(render_list(&result, false), 0); + } +} diff --git a/crates/pa-cli/src/global_flags.rs b/crates/pa-cli/src/global_flags.rs index 5fecdad3810..c09773f7a57 100644 --- a/crates/pa-cli/src/global_flags.rs +++ b/crates/pa-cli/src/global_flags.rs @@ -19,11 +19,11 @@ pub const GLOBAL_VALUE_FLAGS: [&str; 25] = [ "--session-dir", "--models", "--tools", - "-t", "--thinking", "--skill", "--prompt-template", "--theme", + "--goal-token-budget", "--autonomous-gate", "--autonomous-gate-retries", "--autonomous-gate-timeout-ms", diff --git a/crates/pa-cli/src/installer_update.rs b/crates/pa-cli/src/installer_update.rs index 13cb7c0f1b7..7fe485e0629 100644 --- a/crates/pa-cli/src/installer_update.rs +++ b/crates/pa-cli/src/installer_update.rs @@ -1,7 +1,8 @@ //! The `prime-agent update` body: the TS->Rust migration path. One step — -//! the update fetches the installer from the OFFICIAL DOMAIN endpoint -//! (`https://app.primeintellect.ai/prime-agent/install.sh`, never a -//! GitHub raw or workflow URL) and runs it; the script uninstalls the +//! the update fetches the update channel's installer (stable: the official +//! domain's `https://app.primeintellect.ai/prime-agent/install.sh`; +//! nightly: the download base's `install-beta.sh`; never a GitHub raw or +//! workflow URL) and runs it; the script uninstalls the //! TypeScript version, installs the latest Rust build of the update //! channel, and never touches `~/.prime/agent` (the sessions and //! configuration). The TUI's `/update` runs the same core out-of-band @@ -93,11 +94,11 @@ pub fn run(options: &UpdateOptions) -> i32 { // pin (a test or a pinned install) changes where the funnel actually // fetches from, and the banner must not claim a source the run will // not use. + let channel = requested_installer_channel(options.channel); println!( "Updating to the latest Rust build — fetching the installer from {}:", - installer::installer_script_url() + installer::installer_script_url(Some(channel)) ); - let channel = requested_installer_channel(options.channel); match runtime.block_on(installer::run_installer( Some(channel), InstallerOutput::Inherit, @@ -125,6 +126,65 @@ pub fn run(options: &UpdateOptions) -> i32 { } } +/// The local operation on an installer install: `--rollback` restores the +/// previous version the last install kept, `--archive` installs a local +/// release archive. Both run the installer script bundled into this build +/// against the prefix the running binary was installed under. Returns the +/// process exit code. +pub fn run_local(prefix: &std::path::Path, archive: Option<&std::path::Path>) -> i32 { + let archive = match archive.map(std::path::absolute).transpose() { + Ok(archive) => archive, + Err(error) => { + eprintln!("Error: could not resolve the archive path: {error}"); + return 1; + } + }; + let args: Vec<&std::ffi::OsStr> = match &archive { + Some(path) => vec!["--archive".as_ref(), path.as_os_str()], + None => vec!["--rollback".as_ref()], + }; + let Ok(runtime) = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + else { + eprintln!("Error: could not start the update runtime."); + return 1; + }; + #[cfg(windows)] + let handed_off = installer::caller_owns_payload(prefix); + match runtime.block_on(installer::run_bundled_installer(prefix, &args)) { + Ok(installed) => { + #[cfg(windows)] + if handed_off { + // The handed-off child prints its own outcome to the + // inherited terminal after this process exits; this + // process never saw the landed version, so the "rolled + // back to" line would lie. The announcement rides here — + // the child is spawned by now, so a refused pre-flight + // (which returns Err below) never announced a background + // run that does not exist. + println!( + "the {} continues in the background after this command exits — Windows only releases the payload once this process does", + if archive.is_some() { "archive install" } else { "rollback" } + ); + return 0; + } + let done = if archive.is_some() { + "installed" + } else { + "rolled back to" + }; + let version = installed.version.as_deref().unwrap_or("the previous build"); + println!("{done} {version} — restart prime-agent to run it"); + 0 + } + Err(failure) => { + eprintln!("Error: {}", failure.message); + 1 + } + } +} + /// Persist an explicit channel switch (`--nightly` / `--stable`) after a /// completed update. fn save_channel(channel: UpdateChannel) { diff --git a/crates/pa-cli/src/interactive_mode.rs b/crates/pa-cli/src/interactive_mode.rs index 3fd605deca7..0a6871589d3 100644 --- a/crates/pa-cli/src/interactive_mode.rs +++ b/crates/pa-cli/src/interactive_mode.rs @@ -65,27 +65,11 @@ pub fn run_interactive_mode(options: &RunOptions) -> Result { )), )?; let configuration_load_ms = configuration_load_started.elapsed().as_millis() as u64; - // Telemetry disclosure (TS agent-session-services): once per - // installation, only after onboarding marked itself shown (a first - // interactive run belongs to the onboarding screen; the notice surfaces - // on the next launch). Divergence from TS: the TS product renders it as - // (a session diagnostic in the TUI; the Rust build prints it to stderr - // before the TUI starts, which keeps the same text visible without a - // daemon-side diagnostics round-trip). - if !tui_options.telemetry_disabled.unwrap_or(false) { - let mut settings = pa_core::settings::SettingsManager::create( - &options.config.cwd, - &options.config.agent_dir, - ); - if settings.get_onboarding_shown() && !settings.get_telemetry_notice_shown() { - eprintln!( - "Prime Agent sends pseudonymous usage and performance metrics without prompts, responses, tool content, file paths, or repository data. Disable this with telemetry.enabled=false, PRIME_AGENT_TELEMETRY=0, DO_NOT_TRACK=1, or offline mode." - ); - if let Err(error) = settings.set_telemetry_notice_shown(true) { - eprintln!("Warning: could not persist the telemetry notice: {error}"); - } - } - } + // The telemetry disclosure renders inside the TUI (TS + // agent-session-services' session diagnostic): the interactive + // attach pushes the info row once per installation, deferred behind + // onboarding — a pre-TUI stderr print would be hidden by the alt + // screen. let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .build() @@ -106,7 +90,7 @@ pub fn run_interactive_mode(options: &RunOptions) -> Result { } else { "cold" }; - let startup_telemetry = (!options.config.telemetry_disabled).then(|| { + let mut startup_telemetry = (!options.config.telemetry_disabled).then(|| { let agent_dir = options.config.agent_dir.clone(); let settings = pa_core::settings::SettingsManager::create(&options.config.cwd, &agent_dir); @@ -153,9 +137,10 @@ pub fn run_interactive_mode(options: &RunOptions) -> Result { } // `startup` (schema v1): process entry to a ready interactive // session environment (daemon listening). Emitted through a - // one-shot client that flushes immediately; the session's own - // telemetry rides the daemon worker. - if let Some(client) = startup_telemetry.as_ref() { + // one-shot client; the session's own telemetry rides the daemon + // worker. The flush handle rides to the end of the run — the + // quick-exit join below bounds delivery on a fast quit. + let startup_flush = startup_telemetry.take().map(|client| { let daemon_ready_ms = startup_started.elapsed().as_millis() as u64; let mut properties = pa_telemetry::base_properties("interactive"); properties.set("duration_ms", serde_json::Value::from(daemon_ready_ms)); @@ -170,9 +155,9 @@ pub fn run_interactive_mode(options: &RunOptions) -> Result { startup_kind: Some(startup_kind), timing_scope: Some("system_work"), } - .track(client); - let _ = client.shutdown().await; - } + .track(&client); + flush_startup_telemetry(client) + }); // `prime-agent agents` and bare `--resume` open the agents view // (TS `agentsViewRequested`); the view then opens sessions, and a // session exits back into the view until the user exits it. TS gates @@ -188,29 +173,52 @@ pub fn run_interactive_mode(options: &RunOptions) -> Result { tui_options.onboarding.is_some(), continue_view.is_some(), ); - if agents_view { - let (anchor, notice) = continue_view.map_or((None, None), |view| { - (Some(view.session_id), Some(view.notice)) - }); - run_agents_view_flow(tui_options, anchor, notice).await - } else { - let outcome = - pa_tui::interactive::run_interactive(tui_options.clone(), UiMode::Terminal).await?; - // TS `main.ts`: a direct session run closes into the agents view - // when the exit came through agents-back or `/resume` - // (`launchAgentsView` anchored on the session just left); every - // other exit (ctrl+c/ctrl+d, `/quit`) ends the process. - if outcome.return_to_agents_view { - // A startup attach that fell back to the view has no session - // identity to anchor on; its notice seeds the view's status - // line instead. - let anchor = (!outcome.session_id.is_empty()).then(|| outcome.session_id.clone()); - run_agents_view_flow(tui_options, anchor, outcome.agents_view_notice).await + // The interactive dispatch, one future so EVERY exit — the view + // loop's, the session run's, a failed open's error return — + // passes the quick-exit join below. + let run = async { + if agents_view { + let (anchor, notice) = continue_view.map_or((None, None), |view| { + (Some(view.session_id), Some(view.notice)) + }); + run_agents_view_flow(tui_options, anchor, notice).await } else { - print_resume_hint(outcome.resume_hint.as_deref()); - Ok(()) + let outcome = + pa_tui::interactive::run_interactive(tui_options.clone(), UiMode::Terminal) + .await?; + // TS `main.ts`: a direct session run closes into the agents view + // when the exit came through agents-back or `/resume` + // (`launchAgentsView` anchored on the session just left); every + // other exit (ctrl+c/ctrl+d, `/quit`) ends the process. + if outcome.return_to_agents_view { + // A startup attach that fell back to the view has no session + // identity to anchor on; its notice seeds the view's status + // line instead. + let anchor = + (!outcome.session_id.is_empty()).then(|| outcome.session_id.clone()); + run_agents_view_flow(tui_options, anchor, outcome.agents_view_notice).await + } else { + print_resume_hint(outcome.resume_hint.as_deref()); + Ok(()) + } } + }; + let result = run.await; + // The quick-exit join: the runtime that owns the handed-off drain + // dies with this block, so a run that ends inside the delivery + // window (a fast quit — the sink allows up to 1.5s) would lose + // the startup events to the teardown. The same shared exit bound + // `tui exit` and the agents-view exit report already join under + // applies here: the drain delivers (typical ~150ms) or drops, + // bounded — never cut mid-POST. + if let Some(flush) = startup_flush { + let _ = tokio::time::timeout( + Duration::from_millis(pa_tui::interactive::TELEMETRY_EXIT_TIMEOUT_MS), + flush, + ) + .await; } + result })?; // tmux (verified on 3.2a) can drop the pane's final output when the // process dies immediately after writing it: the just-printed resume @@ -224,6 +232,26 @@ pub fn run_interactive_mode(options: &RunOptions) -> Result { Ok(0) } +/// The one-shot startup client's final flush, handed to the runtime: +/// fire-and-forget from the paint path's perspective — the tracked +/// `startup` events' delivery belongs to the background worker (the +/// sink's bounded request timeout; a re-sent batch keeps its event ids, +/// so the backend dedupes), never to the first frame. The returned +/// handle is the quick-exit seam: the composition root joins it under +/// the shared exit bound when the run ends inside the delivery window, +/// so a fast quit delivers (or drops, bounded) instead of the runtime +/// teardown cutting the worker mid-POST. +/// `the_startup_flush_never_blocks_the_first_frame` guards the boundary +/// with a hanging sink: the flush hand-off must complete while delivery +/// is still blocked. +pub(super) fn flush_startup_telemetry( + client: pa_telemetry::TelemetryClient, +) -> tokio::task::JoinHandle<()> { + tokio::spawn(async move { + let _ = client.shutdown().await; + }) +} + /// TS `shutdown` prints the dim resume hint (`formatResumeHint`) to stdout /// after the TUI is restored; agents-view returns suppress it. Dim is the /// TS `chalk.dim` styling (`ESC[2m` ... `ESC[22m`). The print is the last @@ -564,8 +592,7 @@ fn build_tui_options( // gates the completion marker, and the auth handle serves the // not-ready branch's sign-in steps. onboarding, - // Only Some(true) rides the wire (TS `telemetryDisabled`). - telemetry_disabled: config.telemetry_disabled.then_some(true), + telemetry_disabled: crate::mode::create_telemetry_disabled(config), // `/mcp login` / `/mcp logout`: the client-side auth flows run in // this process (the TS interactive client's placement) and persist // through the shared auth store the daemon's sessions read. @@ -589,10 +616,10 @@ fn build_tui_options( // `/login` + `/logout`: the provider auth flows (the API-key store, // the MCP device flow, the provider catalog). provider_auth: Some(provider_auth), - telemetry: Some(std::sync::Arc::new(CliInteractionTelemetry { - cwd: config.cwd.clone(), - agent_dir: config.agent_dir.clone(), - })), + telemetry: Some(std::sync::Arc::new(CliInteractionTelemetry::new( + config.cwd.clone(), + config.agent_dir.clone(), + ))), keybindings, // The process-wide prompt stash store (TS `ClientPromptStashStore` // lives in `main.ts`'s invocation scope): one store per process, so @@ -732,9 +759,6 @@ fn continue_recent_view( let cwd = options.config.cwd.clone(); let path = pa_core::session::discovery::find_most_recent_session_for_cwd(&session_dir, &cwd)?; let header = pa_core::session::manager::read_session_header(&path)?; - if header.id.is_empty() { - return None; - } Some(ContinueRecentView { session_id: header.id.clone(), notice: format!( diff --git a/crates/pa-cli/src/interactive_mode/daemon.rs b/crates/pa-cli/src/interactive_mode/daemon.rs index 578c9441306..0bfb1727b5a 100644 --- a/crates/pa-cli/src/interactive_mode/daemon.rs +++ b/crates/pa-cli/src/interactive_mode/daemon.rs @@ -9,12 +9,16 @@ use super::{anyhow, Command, Duration, Instant, Path, Result, Stdio}; const DAEMON_STARTUP_TIMEOUT_MS: u64 = 30_000; const DAEMON_SHUTDOWN_WAIT_MS: u64 = 5_000; /// Pause between daemon-startup probes (TS `ensureDaemonRunning` polls at -/// 25ms). This port tightens the poll to 5ms: a cold supervisor binds its -/// socket ~39ms after the spawn and the 25ms grid quantized every cold -/// launch by 0-25ms (mean ~12.5ms) of pure wait (boot-floor lane record -/// 20260926-194800 at 7064d039a). Timing-only: the probe itself, the -/// 30s startup budget, and the timeout error are unchanged. -const DAEMON_PROBE_INTERVAL_MS: u64 = 5; +/// 25ms). A cold supervisor binds its socket ~39ms after the spawn, so the +/// poll interval quantizes every cold launch: the bind lands 0-interval +/// before the next probe, a pure wait on a floor of microseconds. The 1ms +/// interval caps that overshoot at <=1ms for the cost of ~40 failed +/// connects per cold launch (each ~us; bounded by the same 30s startup +/// budget, so a hung boot adds at most ~1k connects/s of syscall work, +/// not a spin). Overshoot tables: probe-grid record 20261001-034500 on the +/// bench repo. Timing-only: the probe itself, the 30s startup budget, and +/// the timeout error are unchanged. +const DAEMON_PROBE_INTERVAL_MS: u64 = 1; /// The daemon probe outcome (TS `DaemonVersionProbe`). enum DaemonProbe { @@ -197,7 +201,12 @@ fn spawn_supervisor_detached(socket_path: &Path, spawn_cwd: &Path, exe: &Path) - // lease this daemon's workers write — TS `daemon-launch.ts` // deletes the same var before spawning the supervisor. .env_remove(pa_daemon::lease::SESSION_LEASE_OWNER_ID_ENV); - // Detached: own process group, reaped by init, survives this CLI. + // A daemon must not share the launching TUI's terminal session: a + // session-wide terminal cleanup could hang it up after the TUI exits. + // On Unix, setsid also creates its own process group. + #[cfg(unix)] + pa_core::platform::process::set_new_session(&mut command); + #[cfg(not(unix))] pa_core::platform::process::set_new_process_group(&mut command); command .spawn() diff --git a/crates/pa-cli/src/interactive_mode/onboarding.rs b/crates/pa-cli/src/interactive_mode/onboarding.rs index be92c6c359b..5f4725c9833 100644 --- a/crates/pa-cli/src/interactive_mode/onboarding.rs +++ b/crates/pa-cli/src/interactive_mode/onboarding.rs @@ -145,6 +145,54 @@ pub(super) struct SettingsOnboardingSink { /// The startup-model probe (the completion telemetry's category /// columns: the resolved startup model and its auth source). pub(super) probe: StartupModelProbe, + /// The success outcome was reported (the completion marker ran). + pub(super) completion_reported: std::sync::atomic::AtomicBool, +} + +impl SettingsOnboardingSink { + /// The `onboarding completed` properties (TS + /// `captureOnboardingCompleted`): the auth and provider categories read + /// the resolved startup model (`getCurrentModel` + its auth status). + fn completed_properties(&self, outcome: &str, duration_ms: u64) -> pa_telemetry::Properties { + let mut properties = pa_telemetry::base_properties("interactive"); + properties.set("duration_ms", serde_json::Value::from(duration_ms)); + properties.set("outcome", serde_json::Value::from(outcome)); + let (auth_category, provider_category) = self.probe.telemetry_categories(); + properties.set("auth_category", serde_json::Value::from(auth_category)); + properties.set( + "provider_category", + serde_json::Value::from(provider_category), + ); + properties.set( + "onboarding_id", + serde_json::Value::from(self.onboarding_id.as_str()), + ); + properties + } + + /// Track `onboarding completed` with an unfinished `outcome` on a fresh + /// client and return it, or `None` when nothing is reported. + pub(super) fn track_incomplete(&self, outcome: &str) -> Option { + // A completed flow whose marker failed to persist already reported + // its success. + if self + .completion_reported + .load(std::sync::atomic::Ordering::SeqCst) + { + return None; + } + let settings = pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir); + if crate::mode::telemetry_disabled(&settings) { + return None; + } + let client = pa_core::session_engine::telemetry::build_client(&settings, &self.agent_dir); + let duration_ms = self.created_at.elapsed().as_millis() as u64; + client.track( + "onboarding completed", + self.completed_properties(outcome, duration_ms), + ); + Some(client) + } } impl pa_tui::interactive::OnboardingSink for SettingsOnboardingSink { @@ -166,29 +214,19 @@ impl pa_tui::interactive::OnboardingSink for SettingsOnboardingSink { fn mark_onboarding_complete(&self) -> Result<()> { let mut settings = pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir); settings.set_onboarding_shown(true)?; - // `onboarding completed` (schema v1): the marker writes only on a - // completed flow, so the outcome is always success; the auth and - // provider categories read the resolved startup model (TS - // `captureOnboardingCompleted`'s `getCurrentModel` + auth status - // columns). Best-effort like all telemetry. + // `onboarding completed` (schema v1) with the success outcome: the + // marker writes only on a completed flow. Best-effort like all + // telemetry. + self.completion_reported + .store(true, std::sync::atomic::Ordering::SeqCst); if !crate::mode::telemetry_disabled(&settings) { let client = pa_core::session_engine::telemetry::build_client(&settings, &self.agent_dir); let duration_ms = self.created_at.elapsed().as_millis() as u64; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("duration_ms", serde_json::Value::from(duration_ms)); - properties.set("outcome", serde_json::Value::from("success")); - let (auth_category, provider_category) = self.probe.telemetry_categories(); - properties.set("auth_category", serde_json::Value::from(auth_category)); - properties.set( - "provider_category", - serde_json::Value::from(provider_category), - ); - properties.set( - "onboarding_id", - serde_json::Value::from(self.onboarding_id.as_str()), + client.track( + "onboarding completed", + self.completed_properties("success", duration_ms), ); - client.track("onboarding completed", properties); // The `exit` stage (#2117 `onboarding stage`): the completion // marker's own stage event, paired by `onboarding_id`. The // final flush rides the client's drop (the worker drains once @@ -226,6 +264,21 @@ impl pa_tui::interactive::OnboardingSink for SettingsOnboardingSink { } Ok(()) } + + fn onboarding_incomplete( + &self, + outcome: &'static str, + ) -> std::pin::Pin + Send + '_>> { + let client = self.track_incomplete(outcome); + Box::pin(async move { + // The exit keys quit the app right after: wait out the final + // flush (bounded by the sink's request timeout) so the runtime + // teardown cannot abort the worker before it delivers. + if let Some(client) = client { + let _ = client.shutdown().await; + } + }) + } } /// TS `shouldRunOnboarding`: first launch is defined by the settings flag @@ -307,6 +360,7 @@ pub(super) fn onboarding_task( created_at: std::time::Instant::now(), onboarding_id, ready_emitted: std::sync::atomic::AtomicBool::new(mount_ready), + completion_reported: std::sync::atomic::AtomicBool::new(false), probe, }), model_ready: std::sync::Arc::new(move || readiness_probe.resolve().1), diff --git a/crates/pa-cli/src/interactive_mode/telemetry.rs b/crates/pa-cli/src/interactive_mode/telemetry.rs index e2d26d7f1fa..e1bd9082227 100644 --- a/crates/pa-cli/src/interactive_mode/telemetry.rs +++ b/crates/pa-cli/src/interactive_mode/telemetry.rs @@ -1,22 +1,51 @@ -//! The interaction-telemetry concern (moved with its concern): the -//! one-shot client wrapper and the adoption events the pa-tui -//! interactive loop reports through the `InteractionTelemetry` trait. +//! The interaction-telemetry concern: the pa-tui interactive loop reports +//! through the `InteractionTelemetry` trait. Interactions only count into +//! the session run's counters, which ride its one `tui exit` event. Two +//! standalone events remain their own (TS parity, plus one upstream +//! addition): `agent command used`, and `tui ipython bash rendered` — +//! the settled-shell-cell metric upstream #3307 tracks per render, kept +//! intact from main. + +use std::collections::BTreeMap; +use std::sync::Mutex; use super::{Future, PathBuf, Pin}; -/// `tui scroll used` / `tui exit` adoption telemetry: a one-shot client per -/// event, tracked and flushed at the emission point (the `startup`-event -/// pattern). Telemetry must never fail the session: opt-out or a broken -/// install id drops the event. +/// The interactive client's telemetry: per-session-run adoption counters +/// flushed with `tui exit`, plus the two standalone events — the TS +/// `agent command used`, and the upstream #3307 `tui ipython bash +/// rendered` — each on a one-shot client. Telemetry must never fail the +/// session: opt-out or a broken install id drops the events. pub(super) struct CliInteractionTelemetry { pub(super) cwd: PathBuf, pub(super) agent_dir: PathBuf, + pub(super) counters: Mutex, +} + +/// The session run's adoption counters (`tui_*_count`, `feature_*_count`, +/// `input_*`) and its terminal capability flags. +#[derive(Default)] +pub(super) struct TuiCounters { + counts: BTreeMap, + flags: BTreeMap<&'static str, bool>, } impl CliInteractionTelemetry { + pub(super) fn new(cwd: PathBuf, agent_dir: PathBuf) -> Self { + Self { + cwd, + agent_dir, + counters: Mutex::default(), + } + } + + fn settings(&self) -> pa_core::settings::SettingsManager { + pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir) + } + /// A one-shot client, or `None` when telemetry is opted out. fn client(&self) -> Option { - let settings = pa_core::settings::SettingsManager::create(&self.cwd, &self.agent_dir); + let settings = self.settings(); if crate::mode::telemetry_disabled(&settings) { return None; } @@ -25,6 +54,24 @@ impl CliInteractionTelemetry { &self.agent_dir, )) } + + /// Count only while telemetry is on, so turning it on later never + /// sends what happened while it was off. + fn with(&self, update: impl FnOnce(&mut TuiCounters)) { + if crate::mode::telemetry_disabled(&self.settings()) { + return; + } + update( + &mut self + .counters + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner), + ); + } + + fn count(&self, key: &'static str) { + self.with(|counters| *counters.counts.entry(key.to_string()).or_default() += 1); + } } impl pa_tui::interactive::InteractionTelemetry for CliInteractionTelemetry { @@ -32,291 +79,182 @@ impl pa_tui::interactive::InteractionTelemetry for CliInteractionTelemetry { &self, feature: &'static str, outcome: &'static str, - duration_ms: Option, + _duration_ms: Option, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - pa_telemetry::AgentFeatureOutcome { - feature_id: uuid::Uuid::new_v4().to_string(), - feature_name: feature, - outcome, - duration_ms, - configuration_choice: None, - } - .track(&client); - }) + if let Some(key) = pa_telemetry::feature_outcome_key(feature, outcome) { + self.with(|counters| *counters.counts.entry(key).or_default() += 1); + } + Box::pin(std::future::ready(())) } fn input_stage( &self, - input_id: String, + _input_id: String, stage: &'static str, - outcome: &'static str, + _outcome: &'static str, duration_ms: u64, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - pa_telemetry::AgentInputStage { - input_id, - stage, - outcome, - duration_ms: Some(duration_ms), - timing_origin: Some("ui_input"), - } - .track(&client); - }) + if let Some(prefix) = pa_telemetry::input_stage_key(stage) { + self.with(|counters| { + *counters + .counts + .entry(format!("{prefix}_count")) + .or_default() += 1; + let max = counters + .counts + .entry(format!("{prefix}_max_ms")) + .or_default(); + *max = (*max).max(duration_ms); + }); + } + Box::pin(std::future::ready(())) } fn bash_shortcut_used( &self, - excluded: bool, - side_conversation: bool, + _excluded: bool, + _side_conversation: bool, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("excluded", serde_json::Value::from(excluded)); - properties.set( - "side_conversation", - serde_json::Value::from(side_conversation), - ); - client.track("tui bash shortcut used", properties); - let _ = client.shutdown().await; - }) + self.count("tui_bash_shortcut_count"); + Box::pin(std::future::ready(())) } fn bash_bang_executed( &self, - duration_bucket: &'static str, - exit_class: &'static str, + _duration_bucket: &'static str, + _exit_class: &'static str, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("duration_bucket", serde_json::Value::from(duration_bucket)); - properties.set("exit_class", serde_json::Value::from(exit_class)); - client.track("tui bash bang executed", properties); - let _ = client.shutdown().await; - }) + self.count("tui_bash_bang_count"); + Box::pin(std::future::ready(())) } fn prompt_stash( &self, - action: &'static str, - had_images: bool, + _action: &'static str, + _had_images: bool, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("action", serde_json::Value::from(action)); - properties.set("had_images", serde_json::Value::from(had_images)); - client.track("tui prompt stash", properties); - let _ = client.shutdown().await; - }) + self.count("tui_prompt_stash_count"); + Box::pin(std::future::ready(())) } fn external_editor_used( &self, - outcome: &'static str, + _outcome: &'static str, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("outcome", serde_json::Value::from(outcome)); - client.track("tui external editor used", properties); - let _ = client.shutdown().await; - }) + self.count("tui_external_editor_count"); + Box::pin(std::future::ready(())) } fn scoped_models_used( &self, - action: &'static str, - scoped: bool, + _action: &'static str, + _scoped: bool, + ) -> Pin + Send + '_>> { + self.count("tui_scoped_models_count"); + Box::pin(std::future::ready(())) + } + + fn ipython_bash_rendered( + &self, + bash_lines: usize, + cell_lines: usize, + count: usize, ) -> Pin + Send + '_>> { Box::pin(async move { let Some(client) = self.client() else { return; }; let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("action", serde_json::Value::from(action)); - properties.set("scoped", serde_json::Value::from(scoped)); - client.track("tui scoped models used", properties); + properties.set("bash_lines", serde_json::Value::from(bash_lines)); + properties.set("cell_lines", serde_json::Value::from(cell_lines)); + properties.set("count", serde_json::Value::from(count)); + client.track("tui ipython bash rendered", properties); let _ = client.shutdown().await; }) } fn scroll_used( &self, - action: &'static str, - resumed_following: bool, + _action: &'static str, + _resumed_following: bool, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("action", serde_json::Value::from(action)); - properties.set( - "resumed_following", - serde_json::Value::from(resumed_following), - ); - client.track("tui scroll used", properties); - let _ = client.shutdown().await; - }) + self.count("tui_scroll_count"); + Box::pin(std::future::ready(())) } - fn selection_used(&self, lines: usize) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("lines", serde_json::Value::from(lines as u64)); - client.track("tui selection used", properties); - let _ = client.shutdown().await; - }) + fn selection_used(&self, _lines: usize) -> Pin + Send + '_>> { + self.count("tui_selection_count"); + Box::pin(std::future::ready(())) } - fn click_used(&self, surface: &'static str) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("surface", serde_json::Value::from(surface)); - client.track("tui click used", properties); - let _ = client.shutdown().await; - }) + + fn click_used(&self, _surface: &'static str) -> Pin + Send + '_>> { + self.count("tui_click_count"); + Box::pin(std::future::ready(())) } - fn client_exit( + + fn activity_opened( &self, - reason: &'static str, - turn_active: bool, + _kind: &'static str, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("exit_reason", serde_json::Value::from(reason)); - properties.set("turn_active", serde_json::Value::from(turn_active)); - client.track("tui exit", properties); - let _ = client.shutdown().await; - }) - } - - fn activity_opened(&self, kind: &'static str) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("kind", serde_json::Value::from(kind)); - client.track("tui activity opened", properties); - let _ = client.shutdown().await; - }) + self.count("tui_activity_open_count"); + Box::pin(std::future::ready(())) } fn menu_opened( &self, - menu: &'static str, - source: &'static str, + _menu: &'static str, + _source: &'static str, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("menu", serde_json::Value::from(menu)); - properties.set("source", serde_json::Value::from(source)); - client.track("tui menu opened", properties); - let _ = client.shutdown().await; - }) + self.count("tui_menu_open_count"); + Box::pin(std::future::ready(())) } fn subagents_view_opened( &self, - children_total: u64, + _children_total: u64, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("children_total", serde_json::Value::from(children_total)); - client.track("tui subagents open", properties); - let _ = client.shutdown().await; - }) + self.count("tui_subagents_open_count"); + Box::pin(std::future::ready(())) } - fn command_used(&self, command: &'static str) -> Pin + Send + '_>> { - // `agent command used` (TS `captureAgentCommandUsed`): builtin - // client commands report from the client; session commands report - // through the session telemetry, so the two seams never double-emit. - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("command_name", serde_json::Value::from(command)); - client.track("agent command used", properties); - let _ = client.shutdown().await; - }) + fn scoped_agent_created(&self, _depth: u32) -> Pin + Send + '_>> { + self.count("tui_scoped_agent_count"); + Box::pin(std::future::ready(())) } - fn image_pasted(&self, mime_type: &str) -> Pin + Send + '_>> { - // The returned future borrows only `self`, so the mime type rides - // inside it by value. - let mime_type = mime_type.to_string(); - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("mime_type", serde_json::Value::from(mime_type)); - client.track("tui image pasted", properties); - let _ = client.shutdown().await; - }) + fn image_pasted(&self, _mime_type: &str) -> Pin + Send + '_>> { + self.count("tui_image_paste_count"); + Box::pin(std::future::ready(())) } fn queued_input( &self, - lane: &'static str, - steering_mode: String, + _lane: &'static str, + _steering_mode: String, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("lane", serde_json::Value::from(lane)); - properties.set("steering_mode", serde_json::Value::from(steering_mode)); - client.track("tui input queued", properties); - let _ = client.shutdown().await; - }) + self.count("tui_input_queued_count"); + Box::pin(std::future::ready(())) } - fn queue_edited(&self, action: &'static str) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("action", serde_json::Value::from(action)); - client.track("tui queue edited", properties); - let _ = client.shutdown().await; - }) + fn queue_edited(&self, _action: &'static str) -> Pin + Send + '_>> { + self.count("tui_queue_edit_count"); + Box::pin(std::future::ready(())) + } + + fn suspend_used( + &self, + _outcome: &'static str, + ) -> Pin + Send + '_>> { + self.count("tui_suspend_count"); + Box::pin(std::future::ready(())) + } + + fn agents_view_action( + &self, + _action: &'static str, + ) -> Pin + Send + '_>> { + self.count("tui_agents_action_count"); + Box::pin(std::future::ready(())) } fn enhanced_keys( @@ -324,57 +262,142 @@ impl pa_tui::interactive::InteractionTelemetry for CliInteractionTelemetry { kitty: bool, modify_other_keys: bool, ) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("kitty", serde_json::Value::from(kitty)); - properties.set( - "modify_other_keys", - serde_json::Value::from(modify_other_keys), - ); - client.track("tui enhanced keys", properties); - let _ = client.shutdown().await; - }) + self.with(|counters| { + counters.flags.insert("tui_enhanced_keys_kitty", kitty); + counters + .flags + .insert("tui_enhanced_keys_modify_other_keys", modify_other_keys); + }); + Box::pin(std::future::ready(())) } fn hyperlinks_active(&self, enabled: bool) -> Pin + Send + '_>> { - Box::pin(async move { - let Some(client) = self.client() else { - return; - }; - let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("enabled", serde_json::Value::from(enabled)); - client.track("tui hyperlinks", properties); - let _ = client.shutdown().await; - }) + self.with(|counters| { + counters.flags.insert("tui_hyperlinks_enabled", enabled); + }); + Box::pin(std::future::ready(())) } - fn suspend_used(&self, outcome: &'static str) -> Pin + Send + '_>> { + fn client_exit( + &self, + reason: &'static str, + turn_active: bool, + ) -> Pin + Send + '_>> { Box::pin(async move { + // Every session run of the process shares these counters (the + // agents view hands one telemetry handle to each session it + // opens), so each `tui exit` takes what its run counted. + let counters = std::mem::take( + &mut *self + .counters + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner), + ); let Some(client) = self.client() else { return; }; let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("outcome", serde_json::Value::from(outcome)); - client.track("tui suspend used", properties); + properties.set("exit_reason", serde_json::Value::from(reason)); + properties.set("turn_active", serde_json::Value::from(turn_active)); + for (key, count) in &counters.counts { + properties.set(key, serde_json::Value::from(*count)); + } + for (key, flag) in &counters.flags { + properties.set(key, serde_json::Value::from(*flag)); + } + client.track("tui exit", properties); let _ = client.shutdown().await; }) } - fn agents_view_action( - &self, - action: &'static str, - ) -> Pin + Send + '_>> { + fn command_used(&self, command: &'static str) -> Pin + Send + '_>> { + // `agent command used` (TS `captureAgentCommandUsed`): every + // submitted builtin reports from the client, session commands + // included (the session engine reports none). Box::pin(async move { let Some(client) = self.client() else { return; }; let mut properties = pa_telemetry::base_properties("interactive"); - properties.set("action", serde_json::Value::from(action)); - client.track("tui agents action", properties); + properties.set("command_name", serde_json::Value::from(command)); + client.track("agent command used", properties); let _ = client.shutdown().await; }) } } + +#[cfg(test)] +mod tests { + use pa_tui::interactive::InteractionTelemetry as _; + + use super::CliInteractionTelemetry; + + /// Interactions while telemetry is off never count, so a later + /// `/telemetry on` cannot send them with `tui exit`. + #[test] + fn interactions_while_off_never_count() { + crate::mode::tests::with_clean_telemetry_env(|| { + futures::executor::block_on(count_interactions_while_off()); + }); + } + + async fn count_interactions_while_off() { + let dir = tempfile::TempDir::new().unwrap(); + let agent_dir = dir.path().join("agent"); + pa_core::settings::SettingsManager::create(dir.path(), &agent_dir) + .set_telemetry_enabled(false) + .unwrap(); + let telemetry = CliInteractionTelemetry::new(dir.path().to_path_buf(), agent_dir); + telemetry.scroll_used("page", false).await; + telemetry + .input_stage(String::new(), "received", "ok", 5) + .await; + telemetry.hyperlinks_active(true).await; + { + let counters = telemetry.counters.lock().unwrap(); + assert!(counters.counts.is_empty()); + assert!(counters.flags.is_empty()); + } + pa_core::settings::SettingsManager::create(dir.path(), dir.path().join("agent")) + .set_telemetry_enabled(true) + .unwrap(); + telemetry.scroll_used("page", false).await; + let counters = telemetry.counters.lock().unwrap(); + assert_eq!(counters.counts.get("tui_scroll_count"), Some(&1)); + } + + /// The agents view shares one telemetry handle across the sessions it + /// opens: each `tui exit` reports only its own run's interactions. + #[test] + fn each_tui_exit_reports_only_its_own_run() { + crate::mode::tests::with_clean_telemetry_env(|| { + tokio::runtime::Runtime::new() + .unwrap() + .block_on(report_each_run_once()); + }); + } + + async fn report_each_run_once() { + let dir = tempfile::TempDir::new().unwrap(); + let agent_dir = dir.path().join("agent"); + std::fs::create_dir_all(&agent_dir).unwrap(); + let telemetry = CliInteractionTelemetry::new(dir.path().to_path_buf(), agent_dir.clone()); + telemetry.scroll_used("page", false).await; + telemetry.hyperlinks_active(true).await; + telemetry.client_exit("session_request", false).await; + telemetry.client_exit("ctrl_d", false).await; + let mirror = std::fs::read_to_string(agent_dir.join("telemetry.jsonl")).unwrap(); + let exits: Vec = mirror + .lines() + .map(|line| serde_json::from_str(line).unwrap()) + .filter(|event: &serde_json::Value| event["name"] == "tui exit") + .collect(); + assert_eq!(exits.len(), 2); + assert_eq!(exits[0]["properties"]["tui_scroll_count"], 1); + assert_eq!(exits[0]["properties"]["tui_hyperlinks_enabled"], true); + assert!(exits[1]["properties"].get("tui_scroll_count").is_none()); + assert!(exits[1]["properties"] + .get("tui_hyperlinks_enabled") + .is_none()); + } +} diff --git a/crates/pa-cli/src/interactive_mode/tests.rs b/crates/pa-cli/src/interactive_mode/tests.rs index 75e77b8b735..03a3db03daf 100644 --- a/crates/pa-cli/src/interactive_mode/tests.rs +++ b/crates/pa-cli/src/interactive_mode/tests.rs @@ -355,6 +355,7 @@ fn settings_sink_completes_a_provisioned_home_without_touching_the_choice() { created_at: std::time::Instant::now(), onboarding_id: uuid::Uuid::new_v4().to_string(), ready_emitted: std::sync::atomic::AtomicBool::new(false), + completion_reported: std::sync::atomic::AtomicBool::new(false), probe: StartupModelProbe { cwd: dir.path().to_path_buf(), agent_dir: agent_dir.clone(), @@ -385,6 +386,58 @@ fn settings_sink_completes_a_provisioned_home_without_touching_the_choice() { ); } +/// A flow that ran but did not complete reports `onboarding completed` +/// with the `aborted` outcome (TS `runStartupOnboarding`'s `finally`), and +/// a completed flow never reports a second, aborted outcome. Telemetry is +/// on for this test (the repo's `cargo test` env opts out). +#[test] +fn settings_sink_reports_an_aborted_flow_once() { + crate::mode::tests::with_clean_telemetry_env(|| { + tokio::runtime::Runtime::new() + .unwrap() + .block_on(report_an_aborted_flow_once()); + }); +} + +async fn report_an_aborted_flow_once() { + let dir = tempfile::TempDir::new().expect("temp dir"); + let agent_dir = dir.path().join("agent"); + std::fs::create_dir_all(&agent_dir).expect("agent dir"); + let sink = SettingsOnboardingSink { + cwd: dir.path().to_path_buf(), + agent_dir: agent_dir.clone(), + created_at: std::time::Instant::now(), + onboarding_id: uuid::Uuid::new_v4().to_string(), + ready_emitted: std::sync::atomic::AtomicBool::new(false), + completion_reported: std::sync::atomic::AtomicBool::new(false), + probe: StartupModelProbe { + cwd: dir.path().to_path_buf(), + agent_dir: agent_dir.clone(), + cli_provider: None, + cli_model: None, + models: None, + is_continuing: false, + api_key: None, + }, + }; + // The sink's own report resolves only once the event is delivered. + pa_tui::interactive::OnboardingSink::onboarding_incomplete(&sink, "aborted").await; + let mirror = std::fs::read_to_string(agent_dir.join("telemetry.jsonl")).unwrap(); + let lines: Vec<&str> = mirror.lines().collect(); + assert_eq!(lines.len(), 1); + let event: serde_json::Value = serde_json::from_str(lines[0]).unwrap(); + assert_eq!(event["name"], "onboarding completed"); + assert_eq!(event["properties"]["outcome"], "aborted"); + assert_eq!(event["properties"]["execution_mode"], "interactive"); + + sink.completion_reported + .store(true, std::sync::atomic::Ordering::SeqCst); + assert!( + sink.track_incomplete("aborted").is_none(), + "a reported completion never adds an aborted outcome" + ); +} + /// A fresh home (no choice written) is the one home the question still /// mounts for — the opt-in moment: the flow's `Share` answer persists /// beside the completion flag, and both read back through the next @@ -405,6 +458,7 @@ fn settings_sink_persists_the_fresh_home_answer_with_the_flag() { created_at: std::time::Instant::now(), onboarding_id: uuid::Uuid::new_v4().to_string(), ready_emitted: std::sync::atomic::AtomicBool::new(false), + completion_reported: std::sync::atomic::AtomicBool::new(false), probe: StartupModelProbe { cwd: dir.path().to_path_buf(), agent_dir: agent_dir.clone(), @@ -720,6 +774,8 @@ fn fork_startup_selection_reports_the_ts_contracts() { ); } +// The tilde selector expands a unix HOME path. +#[cfg(unix)] #[test] fn fork_startup_selection_expands_a_tilde_selector() { // The resume selector's convention: a leading `~` resolves against @@ -855,3 +911,190 @@ fn a_fork_launch_never_opens_the_agents_view() { "a --continue with a saved candidate still opens the view" ); } + +/// A sink whose `send_batch` hangs until the test releases it: the +/// stand-in for the analytics POST's network round-trip (the same +/// delivery contract the production sink carries, minus its own +/// timeout — the point is that delivery takes longer than the paint). +#[derive(Default)] +struct GatedSink { + /// Send entry: `notify_one` when a batch reaches the sink; a + /// stored permit keeps the entry wait race-free in both orders. + entered_notify: tokio::sync::Notify, + /// The delivered batches' event names (delivery finished). + delivered: std::sync::Mutex>, + /// The release flag the hanging send waits on. + released: std::sync::atomic::AtomicBool, + /// The hang gate's waker: `notify_waiters` on release, no + /// permits — a stale permit would unhang the sink before the + /// test releases it. + notify: tokio::sync::Notify, + /// Delivery completion: `notify_one` stores a permit when no + /// waiter is registered yet, so the completion future is + /// race-free in both orders and can be awaited directly. + delivered_notify: tokio::sync::Notify, +} + +impl GatedSink { + fn release(&self) { + self.released + .store(true, std::sync::atomic::Ordering::SeqCst); + self.notify.notify_waiters(); + } + + fn delivered_names(&self) -> Vec { + self.delivered.lock().expect("gate lock").clone() + } + + /// The first send's entry future. + fn wait_until_entered(&self) -> impl std::future::Future + '_ { + self.entered_notify.notified() + } + + /// The first delivery's completion future. + fn wait_until_delivered(&self) -> impl std::future::Future + '_ { + self.delivered_notify.notified() + } +} + +impl pa_telemetry::TelemetrySink for GatedSink { + fn send_batch<'a>( + &'a self, + _install_id: &'a str, + events: Vec, + ) -> std::pin::Pin + Send + 'a>> + { + Box::pin(async move { + self.entered_notify.notify_one(); + // Race-free wait, tokio's documented pattern: the waiter + // registers (or consumes a permit) BEFORE the flag check, so + // a release that fires between the check and the await is + // never lost — a naive `while !flag { notified().await }` can + // miss `notify_waiters` and hang. + let notified = self.notify.notified(); + tokio::pin!(notified); + loop { + notified.as_mut().enable(); + if self.released.load(std::sync::atomic::Ordering::SeqCst) { + break; + } + notified.as_mut().await; + notified.set(self.notify.notified()); + } + let mut delivered = self.delivered.lock().expect("gate lock"); + for event in &events { + delivered.push(event.name.clone()); + } + self.delivered_notify.notify_one(); + pa_telemetry::SinkOutcome::Sent + }) + } +} + +/// The interactive startup flush is fire-and-forget from the paint +/// path's perspective: the first frame must never await the tracked +/// `startup` events' delivery. The gated sink stands in for the +/// analytics POST's network round-trip: the flush hand-off must +/// complete while delivery still hangs, and the released drain must +/// still deliver the tracked events. +#[test] +fn the_startup_flush_never_blocks_the_first_frame() { + tokio::runtime::Runtime::new() + .expect("runtime") + .block_on(startup_flush_never_blocks_the_first_frame()); +} + +async fn startup_flush_never_blocks_the_first_frame() { + let sink = std::sync::Arc::new(GatedSink::default()); + let mut config = pa_telemetry::TelemetryClientConfig::new("install-1"); + config.batch_size = 20; + config.flush_interval = std::time::Duration::from_mins(10); + config.sinks = vec![sink.clone() as std::sync::Arc]; + let client = pa_telemetry::TelemetryClient::spawn(config).expect("startup client"); + + // The composition root's startup tracks: the event and the ui_ready + // stage on the one-shot client. + let mut properties = pa_telemetry::base_properties("interactive"); + properties.set("duration_ms", serde_json::Value::from(1)); + client.track("startup", properties); + pa_telemetry::AgentStartupStage { + stage: "ui_ready", + outcome: "completed", + duration_ms: Some(1), + startup_kind: Some("cold"), + timing_scope: Some("system_work"), + } + .track(&client); + + // The paint path's contract, run as its own task so a regression + // back to a blocking flush reds the timeout instead of wedging the + // suite (an inner assert failure reds through the join error). + let hand_off = tokio::time::timeout( + std::time::Duration::from_secs(2), + tokio::spawn(async move { + // The hand-off: while the sink still hangs, it must already + // be done — the first frame paints with delivery pending. + let flush = flush_startup_telemetry(client); + // The batch must have entered the sink before the boundary + // is meaningful: a release that beats the send's entry + // drains without ever hanging, and the hand-off below would + // pass vacuously. The entry deadline sits under the outer + // hand-off bound, so a sink that never starts reports + // here, not as a blocked paint path. + let entered = tokio::time::timeout( + std::time::Duration::from_millis(500), + sink.wait_until_entered(), + ) + .await; + assert!( + entered.is_ok(), + "the tracked startup batch entered the gated sink" + ); + assert!( + sink.delivered_names().is_empty(), + "delivery was still pending when the paint path proceeded" + ); + + // The released drain still delivers the tracked startup + // events: completion is awaited on the sink's own notify, + // the timeout only bounds failure — no polling loop. + sink.release(); + let delivered = tokio::time::timeout( + std::time::Duration::from_secs(1), + sink.wait_until_delivered(), + ) + .await; + assert!( + delivered.is_ok(), + "the released drain delivered the tracked startup events" + ); + let names = sink.delivered_names(); + assert!( + names.contains(&"startup".to_string()), + "the startup event delivered: {names:?}" + ); + assert!( + names.contains(&"agent startup stage".to_string()), + "the ui_ready stage delivered: {names:?}" + ); + + // The quick-exit seam: the composition root joins this + // handle under the shared exit bound when a run ends inside + // the delivery window — the join settles with the drain, so + // the events never die with the runtime teardown. + let joined = tokio::time::timeout(std::time::Duration::from_millis(500), flush).await; + assert!( + matches!(joined, Ok(Ok(()))), + "the exit join settled once the drain delivered" + ); + }), + ) + .await; + match hand_off { + Ok(Ok(())) => {} + Ok(Err(joined)) => panic!("the first-paint contract task panicked: {joined}"), + Err(elapsed) => panic!( + "the flush hand-off completed while the sink still hung (the paint path never waits out delivery): {elapsed}" + ), + } +} diff --git a/crates/pa-cli/src/lib.rs b/crates/pa-cli/src/lib.rs index 8551d463c29..2ae5a231d70 100644 --- a/crates/pa-cli/src/lib.rs +++ b/crates/pa-cli/src/lib.rs @@ -37,6 +37,7 @@ pub(crate) mod daemon_command; pub(crate) mod daemon_discovery; pub(crate) mod daemon_mode; pub(crate) mod daemon_session_list; +pub(crate) mod factory_command; pub(crate) mod file_processor; pub(crate) mod global_flags; pub(crate) mod headless_autonomous; @@ -60,6 +61,7 @@ pub(crate) mod self_update; pub(crate) mod session_export; pub(crate) mod sessions_table_format; pub(crate) mod subscription_login; +pub(crate) mod telemetry_notice; pub(crate) mod traces_login; /// The json stream's event selection a [`RunOptions`] carries. @@ -101,6 +103,10 @@ pub fn main_with_runtime(args: &[String], runtime: &dyn mode::Runtime) -> i32 { fn main_impl(args: &[String], runtime: &dyn mode::Runtime) -> Result { use std::io::IsTerminal; + // Telemetry reports the version `--version` prints: the beta channel + // restamps only the packaged manifest, never the compiled-in version. + pa_telemetry::set_version(crate::config::version()); + let offline_mode = args.iter().any(|arg| arg == "--offline") || crate::config::is_truthy_env_flag( std::env::var(crate::config::ENV_OFFLINE).ok().as_deref(), diff --git a/crates/pa-cli/src/mcp_login.rs b/crates/pa-cli/src/mcp_login.rs index ebb509a319f..1a5f20963c8 100644 --- a/crates/pa-cli/src/mcp_login.rs +++ b/crates/pa-cli/src/mcp_login.rs @@ -1,19 +1,21 @@ //! The composition root's MCP auth flows behind the TUI's -//! `/mcp login ` and `/mcp logout `: resolve the server -//! through the settings + builtin catalog, run the pa-core OAuth login -//! against the inline auth panel (the authorization URL block and the -//! paste fallbacks render in the TUI; the browser launch rides the -//! request), and persist the endpoint-bound credential in the shared auth -//! store. The TS interactive client runs the same flow in its own process -//! (auth-flows.ts `runMcpLogin`) and reloads the session; this build -//! surfaces the activation state instead (the skill gating resolves at -//! session build). +//! `/mcp login ` and `/mcp logout ` plus the `/mcp` view's +//! api-key credential flow (`key `): resolve the server through +//! the settings + builtin catalog, run the pa-core OAuth login against +//! the inline auth panel (the authorization URL block and the paste +//! fallbacks render in the TUI; the browser launch rides the request), +//! and persist the endpoint-bound credential in the shared auth store — +//! the api-key credentials store their key in the auth slot the runtime +//! reads (the web-search key). The TS interactive client runs the same +//! flow in its own process (auth-flows.ts `runMcpLogin`) and reloads the +//! session; this build surfaces the activation state instead (the skill +//! gating resolves at session build). use std::path::PathBuf; use std::pin::Pin; use anyhow::{anyhow, Context, Result}; -use pa_core::auth::AuthStorage; +use pa_core::auth::{AuthCredential, AuthStorage}; use pa_core::mcp::{ McpLoginUi, McpManager, McpManagerOptions, McpServerConfig, OAuthHttp, ReqwestOAuthHttp, }; @@ -170,6 +172,44 @@ impl TerminalMcpAuth { } } + /// The api-key credential flow (the `/mcp` view's key rows): prompt + /// for the ONE key the credential collects (the masked field never + /// renders the secret) and store it in the credential's auth slot — + /// the exact contract the runtime reads (auth.json's `serper` key, + /// the `AuthCredential::ApiKey` form). + async fn api_key_inner( + &self, + credential: &str, + panel: pa_tui::auth_panel::AuthPanelHandle, + ) -> Result { + let label = pa_core::mcp::API_KEY_CREDENTIALS + .iter() + .find(|(id, _)| *id == credential) + .map(|(_, label)| *label) + .ok_or_else(|| anyhow!("{credential} is not a known api-key credential."))?; + let key = panel + .paste_prompt( + &format!("Paste the API key for {label}:"), + PastePromptTone::Text, + PasteStyle::Masked, + ) + .await + .filter(|line| !line.is_empty()) + .ok_or_else(|| anyhow!("Paste cancelled"))?; + let mut auth = AuthStorage::create(&self.agent_dir); + auth.set( + credential, + AuthCredential::ApiKey { + key, + prime_team: None, + }, + ); + if let Some(error) = auth.drain_errors().pop() { + return Err(anyhow!(error)).context("could not save the API key"); + } + Ok(format!("Saved API key for {label}. Web search is ready.")) + } + fn logout_inner(&self, server: &str) -> Result { let provider = format!("mcp:{server}"); let mut auth = AuthStorage::create(&self.agent_dir); @@ -199,6 +239,11 @@ impl ClientAuthCommands for TerminalMcpAuth { Box::pin(async move { auth.paste_inner(&server, panel).await }) } + fn api_key(&self, credential: &str, panel: pa_tui::auth_panel::AuthPanelHandle) -> AuthFuture { + let (auth, credential) = (self.clone(), credential.to_string()); + Box::pin(async move { auth.api_key_inner(&credential, panel).await }) + } + fn logout(&self, server: &str) -> AuthFuture { let (auth, server) = (self.clone(), server.to_string()); Box::pin(async move { auth.logout_inner(&server) }) @@ -461,4 +506,77 @@ mod tests { assert_eq!(status, "linear is not connected."); Ok(()) } + + #[tokio::test] + async fn the_api_key_flow_stores_the_exact_runtime_contract() -> Result<()> { + let dir = tempfile::tempdir()?; + let agent_dir = dir.path().join("agent"); + std::fs::create_dir_all(&agent_dir)?; + let hook = TerminalMcpAuth::new(dir.path().to_path_buf(), agent_dir.clone()); + + // The flow prompts (masked) for the key, then stores it. + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); + let panel = pa_tui::auth_panel::AuthPanelHandle::new(tx); + let flow = tokio::spawn({ + let hook = hook.clone(); + let panel = panel.clone(); + async move { hook.api_key_inner("serper", panel).await } + }); + let request = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()) + .await + .expect("the paste request sends") + .expect("the channel stays open"); + let pa_tui::auth_panel::AuthPanelRequest::PastePrompt { prompt, reply, .. } = request + else { + panic!("expected the paste request") + }; + assert_eq!(prompt, "Paste the API key for Serper (web search):"); + reply + .send(Some("the-serper-key".to_string())) + .expect("the reply lands"); + assert_eq!( + flow.await.expect("the flow ran")?, + "Saved API key for Serper (web search). Web search is ready." + ); + + // The stored credential is the EXACT contract the websearch runtime + // reads: auth.json's `serper` slot, the `api_key` form. + let stored: serde_json::Value = serde_json::from_str( + &std::fs::read_to_string(agent_dir.join("auth.json")).expect("auth.json"), + )?; + assert_eq!(stored["serper"]["type"], "api_key"); + assert_eq!(stored["serper"]["key"], "the-serper-key"); + + // An unknown credential keeps its wording. + let error = hook + .api_key_inner("nope", panel.clone()) + .await + .unwrap_err() + .to_string(); + assert_eq!(error, "nope is not a known api-key credential."); + + // A cancelled prompt answers the cancel (the stored key stays). + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); + let panel = pa_tui::auth_panel::AuthPanelHandle::new(tx); + let flow = tokio::spawn({ + let hook = hook.clone(); + let panel = panel.clone(); + async move { hook.api_key_inner("serper", panel).await } + }); + let request = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()) + .await + .expect("the paste request sends") + .expect("the channel stays open"); + let pa_tui::auth_panel::AuthPanelRequest::PastePrompt { reply, .. } = request else { + panic!("expected the paste request") + }; + reply.send(None).expect("the cancel lands"); + let error = flow.await.expect("the flow ran").unwrap_err().to_string(); + assert_eq!(error, "Paste cancelled"); + let stored: serde_json::Value = serde_json::from_str( + &std::fs::read_to_string(agent_dir.join("auth.json")).expect("auth.json"), + )?; + assert_eq!(stored["serper"]["key"], "the-serper-key"); + Ok(()) + } } diff --git a/crates/pa-cli/src/mode.rs b/crates/pa-cli/src/mode.rs index aee60e724d8..593a7cb4e62 100644 --- a/crates/pa-cli/src/mode.rs +++ b/crates/pa-cli/src/mode.rs @@ -310,10 +310,21 @@ impl Runtime for UnavailableRuntime { /// `PRIME_AGENT_TELEMETRY`), then the settings AND. Returns true when /// telemetry is disabled for this invocation. pub fn telemetry_disabled(settings: &pa_core::settings::SettingsManager) -> bool { - match pa_telemetry::env_telemetry_override() { - Some(enabled) => !enabled, - None => !settings.get_telemetry_enabled(), - } + !pa_core::session_engine::telemetry::telemetry_switch(settings).enabled() +} + +/// The daemon session create's `telemetry_disabled` (TS `telemetryDisabled`): +/// only an environment opt-out rides the wire. A settings opt-out stays the +/// live switch the session's client re-reads, so `/telemetry on` resumes the +/// running session instead of waiting for a new one. +pub(crate) fn create_telemetry_disabled(config: &RuntimeConfig) -> Option { + matches!( + pa_core::session_engine::telemetry::telemetry_switch( + &pa_core::settings::SettingsManager::create(&config.cwd, &config.agent_dir) + ), + pa_core::session_engine::telemetry::TelemetrySwitch::Env { enabled: false, .. } + ) + .then_some(true) } /// Build the runtime config from parsed args, mirroring `runtimeConfigFromArgs`. @@ -389,18 +400,16 @@ pub fn runtime_config_from_args( } #[cfg(test)] -mod tests { +pub(crate) mod tests { use super::*; /// Run the body with the three telemetry env overrides held at a known /// state and restored after (an assertion panic must never leave the /// process env mutated, and a host-exported opt-out must not bleed in). - fn with_clean_telemetry_env(body: impl FnOnce() + std::panic::UnwindSafe) { - // The telemetry tests mutate the same process env and cargo runs - // tests on parallel threads: one test's cleanup could clear - // another's DO_NOT_TRACK mid-assertion, so they serialize here. - static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - let _env = ENV_LOCK + pub(crate) fn with_clean_telemetry_env(body: impl FnOnce() + std::panic::UnwindSafe) { + // The env is process-wide: tests that rewrite it take turns. + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let _guard = LOCK .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); let vars = ["PRIME_AGENT_TELEMETRY", "DO_NOT_TRACK", "PI_OFFLINE"]; diff --git a/crates/pa-cli/src/piped_stdin.rs b/crates/pa-cli/src/piped_stdin.rs index 6079bd01582..63cb337dd9a 100644 --- a/crates/pa-cli/src/piped_stdin.rs +++ b/crates/pa-cli/src/piped_stdin.rs @@ -10,7 +10,7 @@ use std::io::IsTerminal as _; use std::io::Read as _; -use std::sync::mpsc::{Receiver, RecvTimeoutError}; +use std::sync::mpsc::RecvTimeoutError; use std::time::Duration; /// Env override for the no-input window of a non-interactive stdin read, @@ -58,7 +58,10 @@ pub fn read_piped_stdin(idle_timeout_ms: u64) -> Option { } } }); - let (data, idle_expired) = collect_from(&receiver, idle_timeout_ms); + let (data, idle_expired) = collect_from( + |idle_window| receiver.recv_timeout(idle_window), + idle_timeout_ms, + ); if idle_expired { eprintln!( "stdin did not close within {idle_timeout_ms}ms; continuing without waiting for more piped input" @@ -75,11 +78,14 @@ pub fn read_piped_stdin(idle_timeout_ms: u64) -> Option { /// and whether the window (not EOF) ended the read. A live producer /// resets the window on every chunk (TS `scheduleIdleTimeout` re-arms on /// each `data` event); only silence expires it. -fn collect_from(receiver: &Receiver>, idle_timeout_ms: u64) -> (Vec, bool) { +fn collect_from( + mut receive: impl FnMut(Duration) -> Result, RecvTimeoutError>, + idle_timeout_ms: u64, +) -> (Vec, bool) { let idle_window = Duration::from_millis(idle_timeout_ms); let mut data = Vec::new(); loop { - match receiver.recv_timeout(idle_window) { + match receive(idle_window) { Ok(chunk) => data.extend_from_slice(&chunk), Err(RecvTimeoutError::Timeout) => return (data, true), Err(RecvTimeoutError::Disconnected) => return (data, false), @@ -109,7 +115,8 @@ mod tests { sender.send(chunk).expect("send"); } drop(sender); - let (data, idle_expired) = collect_from(&receiver, 5000); + let (data, idle_expired) = + collect_from(|idle_window| receiver.recv_timeout(idle_window), 5000); assert_eq!(data, b"one two"); assert!(!idle_expired); } @@ -119,26 +126,35 @@ mod tests { let (sender, receiver) = std::sync::mpsc::channel(); sender.send(b"first".to_vec()).expect("send"); // The producer stays open but silent: the window expires. - let (data, idle_expired) = collect_from(&receiver, 30); + let (data, idle_expired) = + collect_from(|idle_window| receiver.recv_timeout(idle_window), 30); assert_eq!(data, b"first"); assert!(idle_expired); } #[test] fn a_live_producer_resets_the_window() { - let (sender, receiver) = std::sync::mpsc::channel(); - let producer = std::thread::spawn(move || { - for tick in 0..4 { - std::thread::sleep(Duration::from_millis(20)); - if sender.send(vec![b'0' + tick as u8]).is_err() { - return; - } - } - }); - // Chunks every 20ms keep the 60ms window alive across the run. - let (data, idle_expired) = collect_from(&receiver, 60); + let idle_timeout_ms = 60; + let idle_window = Duration::from_millis(idle_timeout_ms); + let mut receives = [ + Ok(vec![b'0']), + Ok(vec![b'1']), + Ok(vec![b'2']), + Ok(vec![b'3']), + Err(RecvTimeoutError::Disconnected), + ] + .into_iter(); + let (data, idle_expired) = collect_from( + |window| { + assert_eq!( + window, idle_window, + "each receive gets the full idle window" + ); + receives.next().expect("four chunks followed by EOF") + }, + idle_timeout_ms, + ); assert!(!idle_expired); assert_eq!(data, b"0123"); - producer.join().expect("producer finished"); } } diff --git a/crates/pa-cli/src/print_boundary/tests.rs b/crates/pa-cli/src/print_boundary/tests.rs index 4d78b20a1fa..de45cdead4d 100644 --- a/crates/pa-cli/src/print_boundary/tests.rs +++ b/crates/pa-cli/src/print_boundary/tests.rs @@ -76,6 +76,7 @@ async fn faux_engine_with_mock_telemetry( client, execution_mode: Some("print".to_string()), now: None, + telemetry_enabled: None, }; let (engine, dir, model) = faux_engine_with_telemetry(script, settings, None, Some(telemetry)).await; @@ -114,6 +115,8 @@ async fn faux_engine_with_telemetry( }) .expect("a session manager"); let engine = create_session(SessionEngineConfig { + on_late_sent_agent_message: None, + semantic_edges: None, cron_store: None, steering_mode: None, follow_up_mode: None, diff --git a/crates/pa-cli/src/print_goal.rs b/crates/pa-cli/src/print_goal.rs index 2d5f9558c32..a5ca0ecd9ea 100644 --- a/crates/pa-cli/src/print_goal.rs +++ b/crates/pa-cli/src/print_goal.rs @@ -896,6 +896,8 @@ mod tests { let engine = Arc::new( pa_core::session_engine::engine::create_session( pa_core::session_engine::engine::SessionEngineConfig { + on_late_sent_agent_message: None, + semantic_edges: None, cron_store: None, telemetry: None, cwd: dir.path().to_path_buf(), @@ -1498,6 +1500,8 @@ mod tests { let engine = Arc::new( pa_core::session_engine::engine::create_session( pa_core::session_engine::engine::SessionEngineConfig { + on_late_sent_agent_message: None, + semantic_edges: None, cron_store: None, telemetry: None, cwd: dir.path().to_path_buf(), diff --git a/crates/pa-cli/src/print_retry.rs b/crates/pa-cli/src/print_retry.rs new file mode 100644 index 00000000000..3e5a2fcc39e --- /dev/null +++ b/crates/pa-cli/src/print_retry.rs @@ -0,0 +1,128 @@ +//! In-process print cancellation and output composition over pa-core retries. + +use std::sync::Arc; + +use pa_agent::abort::{AbortController, AbortSignal}; +use pa_agent::types::{AssistantMessage, ImageContent}; +use pa_core::session_engine::auto_retry::{AutoRetryEvent, RetryStartReason}; +use pa_core::session_engine::engine::SessionEngine; +use pa_core::session_engine::provider_retry::ProviderRetryPolicy; +use pa_core::session_engine::turn_retry::RetryUpdate; +use pa_types::ai::Model; + +fn print_retry_update(update: RetryUpdate, json_mode: bool) { + if !json_mode { + return; + } + let wire = match update.event { + AutoRetryEvent::Start { + attempt, + max_attempts, + delay_ms, + error_message, + reason, + } => { + let mut wire = serde_json::json!({ + "type": "auto_retry_start", "attempt": attempt, + "maxAttempts": max_attempts, "delayMs": delay_ms, "errorMessage": error_message, + }); + match reason { + RetryStartReason::Quick => {} + RetryStartReason::ConnectionReset => { + wire["reason"] = "connection_reset".into(); + } + RetryStartReason::Backup { backup_model } => { + wire["reason"] = "backup".into(); + wire["backupModel"] = backup_model.into(); + } + } + wire + } + AutoRetryEvent::End { + success, + attempt, + final_error, + restored_model, + } => { + let mut wire = serde_json::json!({"type": "auto_retry_end", "success": success, "attempt": attempt}); + if let Some(error) = final_error { + wire["finalError"] = error.into(); + } + if let Some(model) = restored_model { + wire["restoredModel"] = model.into(); + } + wire + } + }; + println!("{wire}"); + if let Some(row) = update.outcome { + println!( + "{}", + serde_json::json!({"type": "message_start", "message": row}) + ); + println!( + "{}", + serde_json::json!({"type": "message_end", "message": row}) + ); + } +} + +#[allow(clippy::too_many_arguments)] +async fn run_prompt_with_signal( + engine: &Arc, + model: &Model, + prompt: &str, + images: &[ImageContent], + policy: &ProviderRetryPolicy, + signal: &AbortSignal, + emit: E, +) -> anyhow::Result +where + E: Fn(RetryUpdate) -> EF + Send + Sync + 'static, + EF: std::future::Future> + Send, +{ + engine + .run_prompt_with_retry(model.context_window, prompt, images, policy, signal, emit) + .await +} + +pub(super) async fn run_prompt( + engine: &Arc, + model: &Model, + prompt: &str, + images: &[ImageContent], + policy: &ProviderRetryPolicy, + json_mode: bool, +) -> anyhow::Result<()> { + let controller = AbortController::new(); + let signal = controller.signal(); + let run = run_prompt_with_signal( + engine, + model, + prompt, + images, + policy, + &signal, + move |update| { + print_retry_update(update, json_mode); + std::future::ready(Ok(())) + }, + ); + tokio::pin!(run); + let message = tokio::select! { + result = &mut run => result?, + interrupted = tokio::signal::ctrl_c() => { + interrupted?; + controller.abort(); + run.await? + } + }; + if message.stop_reason == pa_agent::types::StopReason::Aborted { + return Err(pa_agent::abort::aborted_error()); + } + Ok(()) +} + +#[cfg(test)] +#[path = "print_retry_tests.rs"] +mod tests; diff --git a/crates/pa-cli/src/print_retry_tests.rs b/crates/pa-cli/src/print_retry_tests.rs new file mode 100644 index 00000000000..05c40614b82 --- /dev/null +++ b/crates/pa-cli/src/print_retry_tests.rs @@ -0,0 +1,319 @@ +//! Direct print retries use the real provider and the same SSE drop detector +//! as daemon sessions, and print selection never concatenates a failed partial. +use super::*; +use pa_core::session_engine::provider_adapter::json_round_trip; +use std::sync::Mutex; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +#[tokio::test] +async fn print_retries_a_real_dropped_stream_and_keeps_only_committed_context() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let requests = Arc::new(Mutex::new(Vec::new())); + let recorded = Arc::clone(&requests); + let server = tokio::spawn(async move { + let dropped = "data: {\"id\":\"c1\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"failed partial\"},\"finish_reason\":null}]}\n\n"; + let healthy = "data: {\"id\":\"c2\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"recovered answer\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n"; + for body in [dropped, healthy, healthy] { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut request = Vec::new(); + let (head_end, length) = loop { + let mut chunk = [0u8; 4096]; + let size = socket.read(&mut chunk).await.unwrap(); + assert!(size > 0); + request.extend_from_slice(&chunk[..size]); + if let Some(end) = request.windows(4).position(|bytes| bytes == b"\r\n\r\n") { + let head = String::from_utf8_lossy(&request[..end]); + let length = head + .lines() + .filter_map(|line| line.split_once(':')) + .find(|(name, _)| name.eq_ignore_ascii_case("content-length")) + .unwrap() + .1 + .trim() + .parse::() + .unwrap(); + if request.len() >= end + 4 + length { + break (end + 4, length); + } + } + }; + recorded.lock().unwrap().push( + serde_json::from_slice::(&request[head_end..head_end + length]) + .unwrap(), + ); + let reply = format!("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()); + socket.write_all(reply.as_bytes()).await.unwrap(); + socket.shutdown().await.unwrap(); + } + }); + let dir = tempfile::TempDir::new().unwrap(); + let model: Model = serde_json::from_value(serde_json::json!({ + "id": "gpt-print", "name": "GPT print", "api": "openai-completions", + "provider": "print-test", "baseUrl": format!("http://{address}/v1"), + "reasoning": false, "input": ["text"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 128_000, "maxTokens": 4096, + })) + .unwrap(); + let manager = pa_core::session::manager::SessionManager::persisted( + dir.path(), + &dir.path().join("sessions"), + ); + let session_path = manager.get_session_file().unwrap().to_path_buf(); + let engine = Arc::new( + pa_core::session_engine::engine::create_session( + pa_core::session_engine::engine::SessionEngineConfig { + cwd: dir.path().to_path_buf(), + agent_dir: dir.path().join("agent"), + model: json_round_trip(&model), + model_info: Some(model.clone()), + stream_fn: Some(pa_core::session_engine::provider_adapter::real_stream_fn( + Some("fixture".to_string()), + model.clone(), + )), + tool_selection: pa_core::session_engine::tool_selection::ToolSelection::NoTools, + session_manager: Some(manager), + custom_system_prompt: Some("test".to_string()), + ..Default::default() + }, + ) + .await + .unwrap(), + ); + let policy = ProviderRetryPolicy { + base_delay_ms: 1, + ..pa_core::session_engine::provider_retry::DEFAULT_PROVIDER_RETRY_POLICY + }; + run_prompt( + &engine, + &model, + "hello", + &[], + &policy, + /*json_mode*/ false, + ) + .await + .unwrap(); + let persistence = engine.session.shared_persistence(); + let entries = persistence.lock().await.get_entries(); + let terminal = pa_core::session_engine::headless::select_from_entries(&entries); + assert_eq!( + terminal.primary.unwrap().stdout_text().as_deref(), + Some("recovered answer") + ); + // Exercise persisted reload, not only the live cleanup. + let reopened = pa_core::session::manager::SessionManager::open( + dir.path(), + &dir.path().join("sessions"), + &session_path, + ); + let reloaded = reopened.get_entries(); + engine + .session + .rebuild_branch_context(reloaded) + .await + .unwrap(); + run_prompt( + &engine, + &model, + "next", + &[], + &policy, + /*json_mode*/ false, + ) + .await + .unwrap(); + server.await.unwrap(); + let requests = requests.lock().unwrap(); + assert_eq!(requests.len(), 3); + assert!(!requests[1].to_string().contains("failed partial")); + assert!(!requests[2].to_string().contains("failed partial")); + assert!(requests[2].to_string().contains("recovered answer")); + engine.abandon_kernel(); +} + +#[tokio::test] +async fn print_cancellation_interrupts_the_observed_retry_wait_without_a_second_request() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let requests = Arc::new(Mutex::new(Vec::new())); + let recorded = Arc::clone(&requests); + let (stop_tx, mut stop_rx) = tokio::sync::oneshot::channel::<()>(); + let server = tokio::spawn(async move { + loop { + let mut socket = tokio::select! { + result = listener.accept() => result.unwrap().0, + _ = &mut stop_rx => break, + }; + let mut request = Vec::new(); + loop { + let mut chunk = [0u8; 4096]; + let size = socket.read(&mut chunk).await.unwrap(); + assert!(size > 0); + request.extend_from_slice(&chunk[..size]); + if let Some(end) = request.windows(4).position(|bytes| bytes == b"\r\n\r\n") { + let head = String::from_utf8_lossy(&request[..end]); + let length = head + .lines() + .filter_map(|line| line.split_once(':')) + .find(|(name, _)| name.eq_ignore_ascii_case("content-length")) + .unwrap() + .1 + .trim() + .parse::() + .unwrap(); + if request.len() >= end + 4 + length { + recorded.lock().unwrap().push( + serde_json::from_slice::( + &request[end + 4..end + 4 + length], + ) + .unwrap(), + ); + break; + } + } + } + let body = "data: {\"id\":\"cut\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"failed partial\"},\"finish_reason\":null}]}\n\n"; + let reply = format!("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()); + socket.write_all(reply.as_bytes()).await.unwrap(); + socket.shutdown().await.unwrap(); + } + }); + let dir = tempfile::tempdir().unwrap(); + let model: Model = serde_json::from_value(serde_json::json!({ + "id": "gpt-print", "name": "GPT print", "api": "openai-completions", "provider": "print-test", + "baseUrl": format!("http://{address}/v1"), "reasoning": false, "input": ["text"], + "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, + "contextWindow": 128_000, "maxTokens": 4096, + })).unwrap(); + let engine = Arc::new( + pa_core::session_engine::engine::create_session( + pa_core::session_engine::engine::SessionEngineConfig { + cwd: dir.path().to_path_buf(), + agent_dir: dir.path().join("agent"), + model: json_round_trip(&model), + model_info: Some(model.clone()), + stream_fn: Some(pa_core::session_engine::provider_adapter::real_stream_fn( + Some("fixture".to_string()), + model.clone(), + )), + tool_selection: pa_core::session_engine::tool_selection::ToolSelection::NoTools, + custom_system_prompt: Some("test".to_string()), + ..Default::default() + }, + ) + .await + .unwrap(), + ); + let controller = AbortController::new(); + let signal = controller.signal(); + let events = Arc::new(Mutex::new(Vec::new())); + let emitted = Arc::clone(&events); + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + let started_tx = Mutex::new(Some(started_tx)); + let policy = ProviderRetryPolicy { + base_delay_ms: 60_000, + ..pa_core::session_engine::provider_retry::DEFAULT_PROVIDER_RETRY_POLICY + }; + let run = run_prompt_with_signal( + &engine, + &model, + "hello", + &[], + &policy, + &signal, + move |update| { + if matches!(update.event, AutoRetryEvent::Start { .. }) { + started_tx.lock().unwrap().take().unwrap().send(()).unwrap(); + } + emitted.lock().unwrap().push(update.event); + std::future::ready(Ok(())) + }, + ); + tokio::pin!(run); + tokio::select! { + result = &mut run => panic!("settled before observed retry: {result:?}"), + result = started_rx => result.unwrap(), + } + // The run has yielded in its idle wait, not in a live agent run. + controller.abort(); + let message = tokio::time::timeout(std::time::Duration::from_secs(1), &mut run) + .await + .expect("cancellation must interrupt the long wait") + .unwrap(); + assert_eq!(message.stop_reason, pa_agent::types::StopReason::Aborted); + assert_eq!( + events.lock().unwrap().last(), + Some(&AutoRetryEvent::End { + success: false, + attempt: 1, + final_error: Some("Retry cancelled".to_string()), + restored_model: None, + }) + ); + stop_tx.send(()).unwrap(); + server.await.unwrap(); + assert_eq!(requests.lock().unwrap().len(), 1); + let entries = engine.session.entries().await; + let outcomes = entries.iter().filter(|entry| matches!(entry, + pa_types::session::FileEntry::CustomMessage { payload, .. } if payload.custom_type == "provider_retry_outcome" + )).count(); + assert_eq!(outcomes, 1); + engine.abandon_kernel(); +} + +#[test] +fn a_zero_budget_connection_reset_outcome_supersedes_failed_partial_tool_cards() { + use pa_tui::chat::{ChatEntry, MessageBlock}; + use pa_tui::snapshot::{event_to_update, transcript_to_entries, TurnUpdate}; + let committed = serde_json::json!({"role": "assistant", "stopReason": "toolUse", "content": [ + {"type": "toolCall", "id": "call_once", "name": "probe", "arguments": {}} + ]}); + let result = serde_json::json!({"role": "toolResult", "toolCallId": "call_once", "toolName": "probe", + "content": [{"type": "text", "text": "ran once"}], "isError": false}); + let failed = serde_json::json!({"role": "assistant", "stopReason": "error", "errorMessage": "aged connection", + "content": [{"type": "text", "text": "failed partial"}, + {"type": "toolCall", "id": "call_cut", "name": "probe", "arguments": {}}]}); + let answer = serde_json::json!({"role": "assistant", "stopReason": "stop", + "content": [{"type": "text", "text": "recovered answer"}]}); + let outcome = pa_core::session_engine::messages::create_provider_retry_outcome_message( + /*success*/ true, + 0, + "aged connection", + ); + let outcome = serde_json::to_value(pa_types::session::AgentMessage::Custom(outcome)).unwrap(); + assert!(matches!( + event_to_update(&serde_json::json!({ + "type": "auto_retry_start", "attempt": 1, "maxAttempts": 1, "delayMs": 0, + "errorMessage": "aged connection", "reason": "connection_reset", + })), + Some(TurnUpdate::AutoRetryStart { + attempt: 1, + max_attempts: 1, + delay_ms: 0, + .. + }) + )); + assert_eq!( + event_to_update(&serde_json::json!({ + "type": "auto_retry_end", "success": true, "attempt": 0, + })), + Some(TurnUpdate::AutoRetryEnd { + success: true, + attempt: 0, + final_error: None, + restored_model: None, + }) + ); + let displayed = transcript_to_entries(&[committed, result, failed, answer, outcome]); + assert_eq!(displayed.len(), 3); + assert!( + matches!(&displayed[0], ChatEntry::Tool(card) if card.id == "call_once" + && card.started && card.result.as_ref().is_some_and(|result| + result.content == vec![serde_json::json!({"type": "text", "text": "ran once"})])) + ); + assert!(matches!(&displayed[1], ChatEntry::Assistant(message) + if matches!(message.blocks.as_slice(), [MessageBlock::Text(text)] if text == "recovered answer"))); + assert!(matches!(&displayed[2], ChatEntry::Status { .. })); +} diff --git a/crates/pa-cli/src/print_runtime.rs b/crates/pa-cli/src/print_runtime.rs index 2a15b33e166..201e18877eb 100644 --- a/crates/pa-cli/src/print_runtime.rs +++ b/crates/pa-cli/src/print_runtime.rs @@ -4,6 +4,9 @@ use std::sync::Arc; +#[path = "print_retry.rs"] +mod retry; + use pa_agent::types::Model as AgentModel; use pa_core::session::discovery::{ find_most_recent_session_for_cwd, resolve_session_path, ResolvedSession, SessionSelectorError, @@ -86,8 +89,8 @@ impl crate::mode::Runtime for PrintRuntime { } } } - // ACP mode: a thin JSON-RPC stdio transport over the same - // in-process session engine the print mode uses. + // ACP mode: a thin JSON-RPC stdio transport over a daemon + // session. AppMode::Acp => match run_acp_mode(options) { Ok(code) => Ok(code), Err(error) => { @@ -109,9 +112,11 @@ impl crate::mode::Runtime for PrintRuntime { } } -/// The ACP headless mode: build the in-process session engine the same way -/// the print mode does, then serve the ACP JSON-RPC surface over stdio until -/// the client disconnects. +/// The ACP headless mode (TS main.ts, `useDaemonClient`): ensure a +/// supervisor is listening (spawning one detached), create the daemon +/// session the CLI session flags select, and serve the ACP surface over it +/// until the client disconnects. Any startup failure is an `Error:` exit 1 +/// before the first ACP frame. fn run_acp_mode(options: &RunOptions) -> Result { let rt = tokio::runtime::Builder::new_multi_thread() .enable_all() @@ -121,76 +126,129 @@ fn run_acp_mode(options: &RunOptions) -> Result { } async fn acp_mode_main(options: &RunOptions) -> Result { - // TS `shouldUseDaemonClient` is true for the ACP mode: the daemon is - // the preferred transport, and the in-process engine stays the - // fallback when no daemon can be reached or served. - if let Some(exit_code) = try_daemon_attached_acp(options).await { - return Ok(exit_code); - } - let config = &options.config; - let engine = build_headless_engine_parts(options, "print").await?; - let exit_code = pa_daemon::acp::run_acp_mode(pa_daemon::acp::AcpOptions { - engine: std::sync::Arc::new(engine.engine), - actual_cwd: config.cwd.clone(), + // The disclosure prints on the ACP client's stderr before any + // transport starts (TS pushes the daemon-created session's diagnostics + // to the client; the Rust stderr surface prints it here). + crate::telemetry_notice::print_if_due(&options.config); + // Flag > env > default: the same `PRIME_AGENT_DAEMON_SOCKET` contract + // as every other mode (the `prime-agent` launcher written by + // install-rust.sh pins that env, so the ACP path must honor it or it + // would target the TypeScript default socket and treat the schema + // mismatch as a stale daemon). + let socket_path = crate::config::resolve_daemon_socket_path(options.daemon_socket.as_deref()); + crate::interactive_mode::ensure_daemon_running(&socket_path, &options.config.cwd) + .await + .map_err(|error| format!("{error:#}"))?; + let (actual_cwd, create) = daemon_acp_create(options)?; + pa_daemon::acp::daemon::run_daemon_attached_acp_mode(pa_daemon::acp::daemon::DaemonAcpOptions { + socket_path, + actual_cwd, product_version: crate::config::version().to_string(), - model: Some(engine.model), - api_key: engine.api_key, - agent_dir: config.agent_dir.clone(), - provider_target: engine.provider_target, - autonomous_config: options - .config - .autonomous - .as_ref() - .map(autonomous_runtime_config), + create, }) .await - .map_err(|error| format!("{error:#}"))?; - Ok(exit_code) + .map_err(|error| format!("{error:#}")) } -/// Try the daemon-attached ACP transport: ensure a supervisor is -/// listening (spawning one detached, TS daemon-launch semantics), then -/// serve the ACP surface over a client-owned daemon session. `None` means -/// the daemon path is unavailable and the in-process engine serves the -/// connection instead (the failure is logged to stderr, never stdout). -async fn try_daemon_attached_acp(options: &RunOptions) -> Option { - if std::env::var_os("PRIME_AGENT_FAUX_SCRIPT").is_some() { - return None; - } - // Flag > env > default: the ACP transport resolves the same - // `PRIME_AGENT_DAEMON_SOCKET` contract as every other mode (the - // `prime-agent` launcher written by install-rust.sh pins that env, - // so the ACP path must honor it or it would target the TypeScript - // default socket and treat the schema mismatch as a stale daemon). - let socket_path = crate::config::resolve_daemon_socket_path(options.daemon_socket.as_deref()); - let cwd = options.config.cwd.clone(); - let result = async { - crate::interactive_mode::ensure_daemon_running(&socket_path, &cwd) - .await - .map_err(|error| format!("{error:#}"))?; - let config = &options.config; - let create_config = config.daemon_create_config(&config.cwd); - pa_daemon::acp::daemon::run_daemon_attached_acp_mode( - pa_daemon::acp::daemon::DaemonAcpOptions { - socket_path, - actual_cwd: config.cwd.clone(), - product_version: crate::config::version().to_string(), - create_config, - }, +/// The ACP daemon session's create (TS main.ts `defaultSessionConfig` + +/// the startup create): the session the flags select, client-owned only +/// for `--no-session`, plus the session's cwd. The worker opens and +/// leases the selected file itself. +fn daemon_acp_create( + options: &RunOptions, +) -> Result<(std::path::PathBuf, pa_types::daemon::DaemonCommand), String> { + use pa_types::daemon::DaemonSessionLifecycle; + let config = &options.config; + let session_dir = replacement_session_dir(options); + let (cwd, session_path, lifecycle) = if options.session.no_session { + ( + config.cwd.clone(), + None, + DaemonSessionLifecycle::ClientOwned, ) - .await - .map_err(|error| format!("{error:#}")) + } else { + let (cwd, session_path) = match select_headless_session(options)? { + HeadlessSession::Fork(source) => { + let fork = pa_core::session::manager::SessionManager::fork_from( + &source, + &config.cwd, + &session_dir, + )?; + ( + config.cwd.clone(), + fork.get_session_file().map(std::path::Path::to_path_buf), + ) + } + HeadlessSession::Open(path) => ( + resumed_session_cwd(&path, &config.cwd, explicit_cwd_override(options))?, + Some(path), + ), + HeadlessSession::Fresh => (config.cwd.clone(), None), + }; + (cwd, session_path, DaemonSessionLifecycle::Resident) + }; + // The CLI session flags, under the TS `runtimeConfigFromArgs` + // names. `--api-key` stays off: the create config is persisted. + let mut create_config = config.daemon_create_config(&cwd); + create_config["sessionDir"] = serde_json::json!(session_dir.display().to_string()); + create_config["executionMode"] = serde_json::json!("acp"); + if let Some(provider) = &config.provider { + create_config["provider"] = serde_json::json!(provider); } - .await; - match result { - Ok(code) => Some(code), - Err(error) => { - eprintln!( - "prime-agent: daemon-attached ACP unavailable, using in-process mode: {error}" - ); - None + if let Some(model) = &config.model { + create_config["model"] = serde_json::json!(model); + } + if let Some(thinking) = config.thinking { + create_config["thinking"] = serde_json::json!(thinking.wire_name()); + } + if let Some(system_prompt) = &config.system_prompt { + create_config["systemPrompt"] = serde_json::json!(system_prompt); + } + if !config.append_system_prompt.is_empty() { + create_config["appendSystemPrompt"] = serde_json::json!(config.append_system_prompt); + } + for (key, paths) in [ + ("skills", &config.skills), + ("promptTemplates", &config.prompt_templates), + ] { + if !paths.is_empty() { + create_config[key] = paths + .iter() + .map(|path| path.display().to_string()) + .collect::>() + .into(); } } + if let Some(autonomous) = &config.autonomous { + create_config["autonomous"] = serde_json::json!(autonomous_runtime_config(autonomous)); + } + // Verification seam (the interactive mode's contract): a scripted daemon + // session from a script FILE path. The product never sets it. + if let Some(script) = std::env::var_os("PRIME_AGENT_FAUX_SCRIPT") { + create_config["script"] = serde_json::Value::String(script.to_string_lossy().to_string()); + } + // Verification seam (the TS child runtime inherits the parent's + // `sessionConfig`): a scripted parent session's children run this script + // FILE. The product never sets it. + if let Some(child_script) = std::env::var_os("PRIME_AGENT_FAUX_CHILD_SCRIPT") { + create_config["childScript"] = + serde_json::Value::String(child_script.to_string_lossy().to_string()); + } + let create = pa_types::daemon::DaemonCommand::Create { + id: None, + session_path: session_path.map(|path| path.display().to_string()), + continue_recent: None, + no_session: options.session.no_session.then_some(true), + name: None, + config: Some(create_config), + telemetry_disabled: crate::mode::create_telemetry_disabled(config), + runtime_metadata: None, + lifecycle: Some(lifecycle), + env: None, + launch_env: None, + rest: serde_json::Map::default(), + }; + Ok((cwd, create)) } /// The RPC headless mode: build the in-process session engine the print @@ -383,7 +441,9 @@ fn run_print_mode(options: &RunOptions) -> Result { async fn print_mode_main(options: &RunOptions) -> Result { crate::print_terminal::track_headless_invocation(options).await; - let headless = build_headless_engine(options, "print").await?; + // `print` or `json`: the telemetry execution mode is the app mode (TS + // main.ts `executionMode: appMode`). + let headless = build_headless_engine(options, options.app_mode.as_str()).await?; let engine = std::sync::Arc::new(headless.engine); let result = async { // The CLI `--goal` seed (TS constructor seeding): a fresh root @@ -415,7 +475,7 @@ async fn print_mode_main(options: &RunOptions) -> Result { /// network; verification harness only, never set by the product. /// /// The switchable provider target the session's stream reads per call -/// (shared with the ACP mode, whose picker model switches swap it live). +/// (shared with the RPC mode, whose picker model switches swap it live). pub type ProviderTargetSlot = std::sync::Arc>>; /// The assembled headless engine plus the model and request auth it runs @@ -426,7 +486,7 @@ struct HeadlessEngine { model: Model, api_key: Option, /// The live provider target: the stream reads it per call, and the - /// ACP mode's picker switches swap it (TS `setModel`'s stream + /// RPC mode's picker switches swap it (TS `setModel`'s stream /// re-registration; `set_model` swaps it without rebuilding the /// session). provider_target: ProviderTargetSlot, @@ -449,6 +509,11 @@ async fn build_headless_engine_parts_with_lease( options: &RunOptions, execution_mode: &str, ) -> Result<(HeadlessEngine, Option), String> { + // Every headless mode discloses immediately (TS main: only the + // interactive `deferTelemetryNoticeForOnboarding` holds the notice + // back behind onboarding; `--list-models` never reaches this + // assembly, matching the TS exit before its diagnostics report). + crate::telemetry_notice::print_if_due(&options.config); let (session_manager, lease) = select_session_manager_with_lease(options)?; if let Ok(script) = std::env::var("PRIME_AGENT_FAUX_SCRIPT") { let engine = @@ -558,6 +623,12 @@ async fn build_headless_engine_with( client: pa_core::session_engine::telemetry::build_client(&settings, &config.agent_dir), execution_mode: Some(execution_mode.to_string()), now: None, + telemetry_enabled: Some( + pa_core::session_engine::telemetry::telemetry_enabled_switch( + &config.cwd, + &config.agent_dir, + ), + ), } }); // TS `sdk.ts` seeds the Agent's queue modes from the settings manager @@ -591,9 +662,28 @@ async fn build_headless_engine_with( .map_err(|error| format!("MCP manager construction failed: {error}"))?; std::sync::Arc::new(std::sync::Mutex::new(manager)) }; + // TS print mode records too (`semanticEdgeLedgerPath`): the session + // id names the ledger under the session artifact dir; `--no-session` + // runs on the in-memory manager (no artifact dir), so the identity is + // ledger-less but the request ids still go on the wire. + let session_manager = session_manager + .unwrap_or_else(|| pa_core::session::manager::SessionManager::in_memory(&config.cwd)); + let semantic_edges = Some( + pa_core::session_engine::semantic_edges::SemanticEdgeIdentity { + session_id: session_manager.get_session_id().to_string(), + ledger_path: pa_core::session_engine::semantic_edges::semantic_edge_ledger_path( + None, + session_manager.get_session_artifact_dir().as_deref(), + ), + parent_session_id: None, + spawned_by_request_id: None, + }, + ); let engine = pa_core::session_engine::engine::create_session( pa_core::session_engine::engine::SessionEngineConfig { + on_late_sent_agent_message: None, cron_store: None, + semantic_edges, telemetry, steering_mode, follow_up_mode, @@ -610,7 +700,7 @@ async fn build_headless_engine_with( tool_selection: config.tool_selection(), generic_mcp_servers: vec![], allow_recursion: None, - session_manager, + session_manager: Some(session_manager), provider_session_id: None, extra_host_handlers: None, conversation_log_path: None, @@ -991,29 +1081,30 @@ fn resolve_thinking_level( map_thinking_level(clamped) } -/// The headless session-manager resolution, mirroring the flag order of -/// TS `createSessionManager` (noSession -> fork -> resume -> continue -> -/// create). `--no-session` never reaches here: the caller passes `None` to -/// the engine, which builds the in-memory manager itself. The opened -/// session's runtime lease returns alongside (a long-lived connection -/// holds it on the engine handle; the one-shot modes forget it for the -/// process lifetime). -fn build_session_manager_with_lease( - options: &RunOptions, -) -> Result< - ( - pa_core::session::manager::SessionManager, - Option, - ), - String, -> { - use pa_core::session::manager::SessionManager; - let cwd = options.config.cwd.clone(); - let session_dir = options +/// The headless session selection, in the flag order of TS +/// `createSessionManager` (noSession -> fork -> resume -> continue -> +/// create). `--no-session` never reaches here: its callers skip the +/// selection. +enum HeadlessSession { + /// Fork this source session into a fresh file. + Fork(std::path::PathBuf), + /// Open this existing session file. + Open(std::path::PathBuf), + Fresh, +} + +/// main.ts `explicitCwdOverride`: with --cwd, the flag's directory wins +/// over the stored session cwd on resume. +fn explicit_cwd_override(options: &RunOptions) -> Option<&std::path::Path> { + options .session - .session_dir - .clone() - .unwrap_or_else(|| options.config.agent_dir.join("sessions")); + .cwd_from_flag + .then_some(options.config.cwd.as_path()) +} + +fn select_headless_session(options: &RunOptions) -> Result { + let cwd = &options.config.cwd; + let session_dir = replacement_session_dir(options); // TS `createSessionManager`'s fork arm: every resolution shape forks — // a GLOBAL session is exactly what --fork is for (a different // project's session copied into this cwd) — with no daemon-active @@ -1023,43 +1114,82 @@ fn build_session_manager_with_lease( // selector's convention; the interactive fork arm matches). let expanded = crate::config::expand_tilde_path(selector); let selector = expanded.to_string_lossy(); - let resolved = resolve_session_path(&selector, &cwd, &session_dir) + let resolved = resolve_session_path(&selector, cwd, &session_dir) .map_err(|error| render_selector_error(&error))?; let source = match resolved { ResolvedSession::Path(path) | ResolvedSession::Local(path) | ResolvedSession::Global { path, .. } => path, }; - let manager = SessionManager::fork_from(&source, &cwd, &session_dir)?; - // The materialized fork leases its own file before the engine - // writes it (the fresh-session rule): another process resuming - // the new file can never become a second writer while this - // engine appends — the source was only read, never leased. - return Ok(lease_fresh_manager(manager)); + return Ok(HeadlessSession::Fork(source)); } - // main.ts `explicitCwdOverride`: with --cwd, the flag's directory wins - // over the stored session cwd on resume. - let explicit_cwd_override = options.session.cwd_from_flag.then_some(cwd.as_path()); if let Some(selector) = &options.session.resume { - let path = resolve_resume_selector(selector, &cwd, &session_dir)?; - let lease = session_open_guard(options.daemon_socket.as_deref(), &path)?; - // A failed open's early return drops the lease (released), never - // leaving an orphaned hold behind. - let manager = open_session_file(&path, &session_dir, &cwd, explicit_cwd_override)?; - return Ok((manager, Some(lease))); - } - if options.session.continue_recent { - let most_recent = find_most_recent_session_for_cwd(&session_dir, &cwd); - return match most_recent { - Some(path) => { - let lease = session_open_guard(options.daemon_socket.as_deref(), &path)?; - let manager = open_session_file(&path, &session_dir, &cwd, explicit_cwd_override)?; - Ok((manager, Some(lease))) + let resolved = resolve_session_path(selector, cwd, &session_dir) + .map_err(|error| render_selector_error(&error))?; + return match resolved { + ResolvedSession::Path(path) | ResolvedSession::Local(path) => { + Ok(HeadlessSession::Open( + std::path::absolute(&path).map_err(|error| error.to_string())?, + )) + } + ResolvedSession::Global { + path: _, + cwd: session_cwd, + } => { + // Headless modes have no fork prompt; mirror the TS non-TTY path. + Err(format!( + "session {selector} belongs to a different project ({}). Pass --fork {selector} to use it here, or run from that project's directory.", + session_cwd.display() + )) } - None => Ok(fresh_session_with_lease(&cwd, &session_dir)), }; } - Ok(fresh_session_with_lease(&cwd, &session_dir)) + if options.session.continue_recent { + // Absolute like the resume arm (TS `setSessionFile` resolves it). + if let Some(path) = find_most_recent_session_for_cwd(&session_dir, cwd) { + return Ok(HeadlessSession::Open( + std::path::absolute(&path).map_err(|error| error.to_string())?, + )); + } + } + Ok(HeadlessSession::Fresh) +} + +/// The in-process session manager for the selected session. The opened +/// session's runtime lease returns alongside (a long-lived connection +/// holds it on the engine handle; the one-shot modes forget it for the +/// process lifetime). +fn build_session_manager_with_lease( + options: &RunOptions, +) -> Result< + ( + pa_core::session::manager::SessionManager, + Option, + ), + String, +> { + let cwd = options.config.cwd.clone(); + let session_dir = replacement_session_dir(options); + match select_headless_session(options)? { + HeadlessSession::Fork(source) => { + let manager = + pa_core::session::manager::SessionManager::fork_from(&source, &cwd, &session_dir)?; + // The materialized fork leases its own file before the engine + // writes it (the fresh-session rule): another process resuming + // the new file can never become a second writer while this + // engine appends — the source was only read, never leased. + Ok(lease_fresh_manager(manager)) + } + HeadlessSession::Open(path) => { + let lease = session_open_guard(options.daemon_socket.as_deref(), &path)?; + // A failed open's early return drops the lease (released), + // never leaving an orphaned hold behind. + let manager = + open_session_file(&path, &session_dir, &cwd, explicit_cwd_override(options))?; + Ok((manager, Some(lease))) + } + HeadlessSession::Fresh => Ok(fresh_session_with_lease(&cwd, &session_dir)), + } } /// Build a FRESH persisted manager and lease its eagerly selected file @@ -1444,16 +1574,14 @@ async fn run_prompts_and_emit( boundary .run_pre_turn(engine, model, api_key.clone()) .await?; - engine - .session - .prompt_with_images( - prompt, - images, - pa_core::session_engine::PromptOptions::default(), - ) + let retry_policy = pa_core::settings::SettingsManager::create( + &options.config.cwd, + &options.config.agent_dir, + ) + .get_provider_retry_policy(); + retry::run_prompt(engine, model, prompt, &images, &retry_policy, json_mode) .await .map_err(|error| format!("{error:#}"))?; - engine.session.agent().wait_for_idle().await; crate::headless_exit::phase("settled-turn boundary start"); // The settled-turn boundary (TS `agent_end`): the overflow // compact-and-retry arm, the turn-boundary requests the kernel @@ -1673,8 +1801,10 @@ async fn build_faux_engine_with( // tests can verify persistence without the network. let engine = pa_core::session_engine::engine::create_session( pa_core::session_engine::engine::SessionEngineConfig { + on_late_sent_agent_message: None, cron_store: None, // Faux verification harness: no product telemetry. + semantic_edges: None, steering_mode: None, follow_up_mode: None, telemetry: None, @@ -1757,6 +1887,7 @@ mod tests { }, context_window: 200_000, max_tokens: 8192, + max_tokens_explicit: false, featured: None, headers: None, compat: None, diff --git a/crates/pa-cli/src/print_session_command.rs b/crates/pa-cli/src/print_session_command.rs index d816ef8d826..797f8fe9162 100644 --- a/crates/pa-cli/src/print_session_command.rs +++ b/crates/pa-cli/src/print_session_command.rs @@ -353,6 +353,8 @@ mod tests { let engine = Arc::new( pa_core::session_engine::engine::create_session( pa_core::session_engine::engine::SessionEngineConfig { + on_late_sent_agent_message: None, + semantic_edges: None, cron_store: None, telemetry: None, cwd: dir.path().to_path_buf(), diff --git a/crates/pa-cli/src/provider_login.rs b/crates/pa-cli/src/provider_login.rs index 3eceeb6c602..84c7570ef44 100644 --- a/crates/pa-cli/src/provider_login.rs +++ b/crates/pa-cli/src/provider_login.rs @@ -10,7 +10,9 @@ use std::path::{Path, PathBuf}; -use pa_core::auth::{AuthCredential, AuthSource, AuthStatus}; +use pa_core::auth::{ + AuthCredential, AuthSource, AuthStatus, SERPER_CREDENTIAL_ID, SERPER_CREDENTIAL_NAME, +}; use pa_core::models::ModelRegistry; use pa_tui::provider_auth::{ AuthFlow, AuthStatusIndicator, AuthStatusStyle, AuthType, ProviderAuthCommands, @@ -70,8 +72,6 @@ const BUILT_IN_PROVIDER_DISPLAY_NAMES: &[(&str, &str)] = &[ ]; const PRIME_INFERENCE_PROVIDER_ID: &str = "prime-inference"; -const SERPER_CREDENTIAL_ID: &str = "serper"; -const SERPER_CREDENTIAL_NAME: &str = "Serper (web search)"; /// The display name of a provider id (the TS map, else the id itself). fn display_name(provider_id: &str) -> String { @@ -240,9 +240,11 @@ impl ProviderAuth { } impl ProviderAuthCommands for ProviderAuth { - /// TS `getLoginProviderOptions`: the subscription rows, the MCP - /// OAuth integrations, the API-key model providers, and the web - /// search credential, sorted TS-style with prime-inference first. + /// TS `getLoginProviderOptions`: the subscription rows and the + /// API-key model providers, sorted TS-style with prime-inference + /// first. The service rows (the MCP OAuth integrations, the + /// web-search credential) live on the `/mcp` view (the operator's + /// 2026-09-24 `/login`-is-providers-only directive). fn login_options(&self) -> ProviderRowsFuture { let provider = self.clone(); Box::pin(async move { @@ -347,7 +349,10 @@ impl ProviderAuthCommands for ProviderAuth { impl ProviderAuth { /// The login row build (blocking: the auth store and MCP manager - /// locks must stay off the async workers). + /// locks must stay off the async workers). The operator's 2026-09-24 + /// directive keeps `/login` providers only: the service rows (the MCP + /// OAuth integrations, the web-search credential) live on the `/mcp` + /// view instead. fn login_rows_blocking(self) -> Vec { let provider = self; { @@ -739,271 +744,4 @@ fn logout_blocking(provider_row: &ProviderRow, agent_dir: &Path) -> ProviderAuth } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn the_status_indicator_maps_the_ts_labels() { - let stored_api_key = AuthCredential::ApiKey { - key: "k".to_string(), - prime_team: None, - }; - // A matching stored credential is configured. - assert_eq!( - status_indicator( - Some(&stored_api_key), - &AuthStatus { - source: Some(AuthSource::Stored), - ..Default::default() - }, - AuthType::ApiKey - ), - Some(AuthStatusIndicator { - style: AuthStatusStyle::Success, - label: "configured".to_string() - }) - ); - // A subscription credential on an api-key row warns. - assert_eq!( - status_indicator( - Some(&AuthCredential::Oauth { - access: "a".to_string(), - refresh: None, - expires: 1, - account_id: None, - endpoint: None, - token_endpoint: None, - client_id: None, - resource: None, - issuer: None, - enterprise_url: None, - }), - &AuthStatus::default(), - AuthType::ApiKey - ), - Some(AuthStatusIndicator { - style: AuthStatusStyle::Warning, - label: "subscription configured".to_string() - }) - ); - // An env key marks the api-key row configured. - assert_eq!( - status_indicator( - None, - &AuthStatus { - source: Some(AuthSource::Environment), - label: Some("OPENAI_API_KEY".to_string()), - ..Default::default() - }, - AuthType::ApiKey - ), - Some(AuthStatusIndicator { - style: AuthStatusStyle::Success, - label: "env: OPENAI_API_KEY".to_string() - }) - ); - // An unconfigured subscription row stays muted. - assert_eq!( - status_indicator(None, &AuthStatus::default(), AuthType::Oauth), - Some(AuthStatusIndicator { - style: AuthStatusStyle::Muted, - label: "unconfigured".to_string() - }) - ); - // An unconfigured api-key row hides its meta (TS inline rule). - assert_eq!( - status_indicator(None, &AuthStatus::default(), AuthType::ApiKey), - None - ); - // A stale credential warns. - assert_eq!( - status_indicator( - None, - &AuthStatus { - source: Some(AuthSource::Stale), - label: Some("expired".to_string()), - ..Default::default() - }, - AuthType::Oauth - ), - Some(AuthStatusIndicator { - style: AuthStatusStyle::Warning, - label: "expired".to_string() - }) - ); - } - - #[test] - fn the_display_names_match_the_ts_map() { - assert_eq!(display_name("openai"), "OpenAI"); - assert_eq!(display_name("google"), "Google Gemini"); - assert_eq!(display_name("prime-inference"), "Prime Inference"); - assert_eq!(display_name("a-custom-provider"), "a-custom-provider"); - } - - #[test] - fn the_api_key_login_rule_matches_ts() { - let mut built_in = std::collections::HashSet::new(); - built_in.insert("anthropic".to_string()); - // The display-name map marks api-key logins. - assert!(is_api_key_login_provider("openai", &built_in)); - // A provider the built-in set does not know is a custom api-key - // provider. - assert!(is_api_key_login_provider("my-endpoint", &built_in)); - // A built-in provider without a display name is not an api-key - // login (its subscription flows apply). - assert!(!is_api_key_login_provider("faux", &{ - let mut set = std::collections::HashSet::new(); - set.insert("faux".to_string()); - set - })); - } - - #[tokio::test] - async fn login_options_list_providers_only() { - let dir = tempfile::tempdir().expect("temp dir"); - let agent = dir.path().join("agent"); - std::fs::create_dir_all(&agent).expect("agent dir"); - std::env::remove_var("PRIME_AGENT_TRACES_API_KEY"); - std::env::remove_var("PRIME_API_KEY"); - let auth = ProviderAuth::new(dir.path(), agent.clone()); - let rows = auth.login_options().await; - // The TS OAuth registry rows render; every subscription flow is - // ported, so every row selects (the flows answer through the - // panel — never an after-selection error wall). - for (id, name) in SUBSCRIPTION_PROVIDERS { - let row = rows - .iter() - .find(|row| row.id == id && row.name == name) - .unwrap_or_else(|| panic!("the {id} subscription row renders")); - assert!( - row.available, - "the {id} row's flow is ported and selectable" - ); - } - // The operator's 2026-09-24 directive: /login is providers only — - // the service rows (MCP OAuth integrations, the web search - // credential) never appear; the /mcp view owns MCP logins. - assert!(!rows.iter().any(|row| row.id == "serper")); - assert!(!rows.iter().any(|row| row.id.starts_with("mcp:"))); - // Prime Inference sorts first among the api-key rows (TS rule). - assert!( - rows.iter() - .position(|row| row.id == PRIME_INFERENCE_PROVIDER_ID) - < rows.iter().position(|row| row.id == "anthropic"), - "prime-inference sorts before the other api-key rows" - ); - } - - /// Port of the TS #2645 warning detection: the ban-risk warning is - /// reported exactly when the active Anthropic credential is the - /// subscription (a stored OAuth login or an `sk-ant-oat` key), and - /// its text names the risk. - #[tokio::test] - async fn the_anthropic_subscription_warning_matches_the_active_credential() { - let dir = tempfile::tempdir().expect("temp dir"); - let agent = dir.path().join("agent"); - std::fs::create_dir_all(&agent).expect("agent dir"); - std::env::remove_var("ANTHROPIC_API_KEY"); - let auth = ProviderAuth::new(dir.path(), agent.clone()); - - // No credential: no warning. - assert_eq!(auth.anthropic_subscription_warning().await, None); - - // A plain API key: no warning (an API key avoids the risk). - let mut storage = pa_core::auth::AuthStorage::create(&agent); - storage.set( - "anthropic", - AuthCredential::ApiKey { - key: "sk-ant-api03-plain".to_string(), - prime_team: None, - }, - ); - assert_eq!(auth.anthropic_subscription_warning().await, None); - - // A subscription token key (TS `isAnthropicSubscriptionAuthKey`). - storage.set( - "anthropic", - AuthCredential::ApiKey { - key: "sk-ant-oat-subscription".to_string(), - prime_team: None, - }, - ); - let warning = auth - .anthropic_subscription_warning() - .await - .expect("a sk-ant-oat key is the subscription"); - assert!(warning.contains("identifies as Claude Code")); - assert!(warning.contains("restricted or banned")); - assert!(warning.contains("An Anthropic API key avoids the risk")); - - // A stored OAuth login is the subscription too. - storage.set( - "anthropic", - AuthCredential::Oauth { - access: "a".to_string(), - refresh: None, - expires: i64::MAX, - endpoint: None, - token_endpoint: None, - client_id: None, - resource: None, - issuer: None, - account_id: None, - enterprise_url: None, - }, - ); - assert!(auth.anthropic_subscription_warning().await.is_some()); - - // An env subscription key resolves active with no stored credential. - storage.remove("anthropic"); - std::env::set_var("ANTHROPIC_API_KEY", "sk-ant-oat-env"); - assert!(auth.anthropic_subscription_warning().await.is_some()); - std::env::remove_var("ANTHROPIC_API_KEY"); - } - - #[tokio::test] - async fn login_stores_an_api_key_and_logout_removes_it() { - let dir = tempfile::tempdir().expect("temp dir"); - let agent = dir.path().join("agent"); - std::fs::create_dir_all(&agent).expect("agent dir"); - let auth = ProviderAuth::new(dir.path(), agent); - let row = ProviderRow { - id: "openai".to_string(), - name: "OpenAI".to_string(), - auth_type: AuthType::ApiKey, - status: None, - flow: AuthFlow::ApiKeyPrompt, - configured: false, - available: true, - }; - match auth.login(&row, Some("sk-test")).await { - ProviderAuthOutcome::Status(message) => { - assert!( - message.starts_with("Saved API key for OpenAI. Credentials saved to "), - "the store status names the credential path: {message}" - ); - } - other => panic!("expected the store status, got {other:?}"), - } - // An empty key answers the TS error. - assert_eq!( - auth.login(&row, Some("")).await, - ProviderAuthOutcome::Error( - "Failed to save API key for OpenAI: API key cannot be empty.".to_string() - ) - ); - // The logout lists the stored credential and removes it. - let stored = auth.logout_options().await; - assert_eq!(stored.len(), 1); - assert_eq!(stored[0].id, "openai"); - assert_eq!( - auth.logout(&stored[0]).await, - ProviderAuthOutcome::Status( - "Removed stored API key for OpenAI. Environment variables and models.json config are unchanged." - .to_string() - ) - ); - assert!(auth.logout_options().await.is_empty()); - } -} +mod tests; diff --git a/crates/pa-cli/src/provider_login/tests.rs b/crates/pa-cli/src/provider_login/tests.rs new file mode 100644 index 00000000000..aeecb07bc81 --- /dev/null +++ b/crates/pa-cli/src/provider_login/tests.rs @@ -0,0 +1,313 @@ +//! The provider-login unit battery (moved beside its module at the +//! 2026-09-30 file-size cap: the flows and their tests stay one tree). + +use super::*; + +#[test] +fn the_status_indicator_maps_the_ts_labels() { + let stored_api_key = AuthCredential::ApiKey { + key: "k".to_string(), + prime_team: None, + }; + // A matching stored credential is configured. + assert_eq!( + status_indicator( + Some(&stored_api_key), + &AuthStatus { + source: Some(AuthSource::Stored), + ..Default::default() + }, + AuthType::ApiKey + ), + Some(AuthStatusIndicator { + style: AuthStatusStyle::Success, + label: "configured".to_string() + }) + ); + // A subscription credential on an api-key row warns. + assert_eq!( + status_indicator( + Some(&AuthCredential::Oauth { + access: "a".to_string(), + refresh: None, + expires: 1, + account_id: None, + endpoint: None, + token_endpoint: None, + client_id: None, + resource: None, + issuer: None, + enterprise_url: None, + }), + &AuthStatus::default(), + AuthType::ApiKey + ), + Some(AuthStatusIndicator { + style: AuthStatusStyle::Warning, + label: "subscription configured".to_string() + }) + ); + // An env key marks the api-key row configured. + assert_eq!( + status_indicator( + None, + &AuthStatus { + source: Some(AuthSource::Environment), + label: Some("OPENAI_API_KEY".to_string()), + ..Default::default() + }, + AuthType::ApiKey + ), + Some(AuthStatusIndicator { + style: AuthStatusStyle::Success, + label: "env: OPENAI_API_KEY".to_string() + }) + ); + // An unconfigured subscription row stays muted. + assert_eq!( + status_indicator(None, &AuthStatus::default(), AuthType::Oauth), + Some(AuthStatusIndicator { + style: AuthStatusStyle::Muted, + label: "unconfigured".to_string() + }) + ); + // An unconfigured api-key row hides its meta (TS inline rule). + assert_eq!( + status_indicator(None, &AuthStatus::default(), AuthType::ApiKey), + None + ); + // A stale credential warns. + assert_eq!( + status_indicator( + None, + &AuthStatus { + source: Some(AuthSource::Stale), + label: Some("expired".to_string()), + ..Default::default() + }, + AuthType::Oauth + ), + Some(AuthStatusIndicator { + style: AuthStatusStyle::Warning, + label: "expired".to_string() + }) + ); +} + +#[test] +fn the_display_names_match_the_ts_map() { + assert_eq!(display_name("openai"), "OpenAI"); + assert_eq!(display_name("google"), "Google Gemini"); + assert_eq!(display_name("prime-inference"), "Prime Inference"); + assert_eq!(display_name("a-custom-provider"), "a-custom-provider"); +} + +#[test] +fn the_api_key_login_rule_matches_ts() { + let mut built_in = std::collections::HashSet::new(); + built_in.insert("anthropic".to_string()); + // The display-name map marks api-key logins. + assert!(is_api_key_login_provider("openai", &built_in)); + // A provider the built-in set does not know is a custom api-key + // provider. + assert!(is_api_key_login_provider("my-endpoint", &built_in)); + // A built-in provider without a display name is not an api-key + // login (its subscription flows apply). + assert!(!is_api_key_login_provider("faux", &{ + let mut set = std::collections::HashSet::new(); + set.insert("faux".to_string()); + set + })); +} + +#[tokio::test] +async fn login_options_list_providers_only() { + let dir = tempfile::tempdir().expect("temp dir"); + let agent = dir.path().join("agent"); + std::fs::create_dir_all(&agent).expect("agent dir"); + std::env::remove_var("PRIME_AGENT_TRACES_API_KEY"); + std::env::remove_var("PRIME_API_KEY"); + let auth = ProviderAuth::new(dir.path(), agent.clone()); + let rows = auth.login_options().await; + // The TS OAuth registry rows render; every subscription flow is + // ported, so every row selects (the flows answer through the + // panel — never an after-selection error wall). + for (id, name) in SUBSCRIPTION_PROVIDERS { + let row = rows + .iter() + .find(|row| row.id == id && row.name == name) + .unwrap_or_else(|| panic!("the {id} subscription row renders")); + assert!( + row.available, + "the {id} row's flow is ported and selectable" + ); + } + // The operator's 2026-09-24 directive: /login is providers only — + // the service rows (MCP OAuth integrations, the web search + // credential) never appear; the /mcp view owns MCP logins. + assert!(!rows.iter().any(|row| row.id == "serper")); + assert!(!rows.iter().any(|row| row.id.starts_with("mcp:"))); + // Prime Inference sorts first among the api-key rows (TS rule). + assert!( + rows.iter() + .position(|row| row.id == PRIME_INFERENCE_PROVIDER_ID) + < rows.iter().position(|row| row.id == "anthropic"), + "prime-inference sorts before the other api-key rows" + ); +} + +/// Port of the TS #2645 warning detection: the ban-risk warning is +/// reported exactly when the active Anthropic credential is the +/// subscription (a stored OAuth login or an `sk-ant-oat` key), and +/// its text names the risk. +#[tokio::test] +async fn the_anthropic_subscription_warning_matches_the_active_credential() { + let dir = tempfile::tempdir().expect("temp dir"); + let agent = dir.path().join("agent"); + std::fs::create_dir_all(&agent).expect("agent dir"); + std::env::remove_var("ANTHROPIC_API_KEY"); + let auth = ProviderAuth::new(dir.path(), agent.clone()); + + // No credential: no warning. + assert_eq!(auth.anthropic_subscription_warning().await, None); + + // A plain API key: no warning (an API key avoids the risk). + let mut storage = pa_core::auth::AuthStorage::create(&agent); + storage.set( + "anthropic", + AuthCredential::ApiKey { + key: "sk-ant-api03-plain".to_string(), + prime_team: None, + }, + ); + assert_eq!(auth.anthropic_subscription_warning().await, None); + + // A subscription token key (TS `isAnthropicSubscriptionAuthKey`). + storage.set( + "anthropic", + AuthCredential::ApiKey { + key: "sk-ant-oat-subscription".to_string(), + prime_team: None, + }, + ); + let warning = auth + .anthropic_subscription_warning() + .await + .expect("a sk-ant-oat key is the subscription"); + assert!(warning.contains("identifies as Claude Code")); + assert!(warning.contains("restricted or banned")); + assert!(warning.contains("An Anthropic API key avoids the risk")); + + // A stored OAuth login is the subscription too. + storage.set( + "anthropic", + AuthCredential::Oauth { + access: "a".to_string(), + refresh: None, + expires: i64::MAX, + endpoint: None, + token_endpoint: None, + client_id: None, + resource: None, + issuer: None, + account_id: None, + enterprise_url: None, + }, + ); + assert!(auth.anthropic_subscription_warning().await.is_some()); + + // An env subscription key resolves active with no stored credential. + storage.remove("anthropic"); + std::env::set_var("ANTHROPIC_API_KEY", "sk-ant-oat-env"); + assert!(auth.anthropic_subscription_warning().await.is_some()); + std::env::remove_var("ANTHROPIC_API_KEY"); +} + +#[tokio::test] +async fn login_stores_an_api_key_and_logout_removes_it() { + let dir = tempfile::tempdir().expect("temp dir"); + let agent = dir.path().join("agent"); + std::fs::create_dir_all(&agent).expect("agent dir"); + let auth = ProviderAuth::new(dir.path(), agent); + let row = ProviderRow { + id: "openai".to_string(), + name: "OpenAI".to_string(), + auth_type: AuthType::ApiKey, + status: None, + flow: AuthFlow::ApiKeyPrompt, + configured: false, + available: true, + }; + match auth.login(&row, Some("sk-test")).await { + ProviderAuthOutcome::Status(message) => { + assert!( + message.starts_with("Saved API key for OpenAI. Credentials saved to "), + "the store status names the credential path: {message}" + ); + } + other => panic!("expected the store status, got {other:?}"), + } + // An empty key answers the TS error. + assert_eq!( + auth.login(&row, Some("")).await, + ProviderAuthOutcome::Error( + "Failed to save API key for OpenAI: API key cannot be empty.".to_string() + ) + ); + // The logout lists the stored credential and removes it. + let stored = auth.logout_options().await; + assert_eq!(stored.len(), 1); + assert_eq!(stored[0].id, "openai"); + assert_eq!( + auth.logout(&stored[0]).await, + ProviderAuthOutcome::Status( + "Removed stored API key for OpenAI. Environment variables and models.json config are unchanged." + .to_string() + ) + ); + assert!(auth.logout_options().await.is_empty()); +} + +#[tokio::test] +async fn a_stored_web_search_key_pairs_with_the_logout_row() { + let dir = tempfile::tempdir().expect("temp dir"); + let agent = dir.path().join("agent"); + std::fs::create_dir_all(&agent).expect("agent dir"); + let auth = ProviderAuth::new(dir.path(), agent); + // A key stored at the runtime's slot (the `/mcp` view's key flow + // writes the same store through the same form). + let row = ProviderRow { + id: "serper".to_string(), + name: "Serper (web search)".to_string(), + auth_type: AuthType::ApiKey, + status: None, + flow: AuthFlow::ApiKeyPrompt, + configured: false, + available: true, + }; + match auth.login(&row, Some("the-key")).await { + ProviderAuthOutcome::Status(message) => { + assert!( + message.starts_with("Saved API key for Serper (web search)."), + "the store status: {message}" + ); + } + other => panic!("expected the store status, got {other:?}"), + } + // The logout row pairs with the stored key: the `/logout` surface + // lists it by its credential name and removes it. + let stored = auth.logout_options().await; + let row = stored + .iter() + .find(|row| row.id == "serper") + .expect("the web-search credential's logout row"); + assert_eq!(row.name, "Serper (web search)"); + assert_eq!( + auth.logout(row).await, + ProviderAuthOutcome::Status( + "Removed stored API key for Serper (web search). Environment variables and models.json config are unchanged." + .to_string() + ) + ); + assert!(auth.logout_options().await.is_empty()); +} diff --git a/crates/pa-cli/src/public_command.rs b/crates/pa-cli/src/public_command.rs index 74524c557cd..79338af7849 100644 --- a/crates/pa-cli/src/public_command.rs +++ b/crates/pa-cli/src/public_command.rs @@ -179,6 +179,7 @@ pub fn handle_public_command(args: &[String]) -> PublicCommandResult { "schedule" => run_nested_agent_command("schedule", "cron", &rest), "status" => run_status(&rest), "doctor" => run_doctor(&rest), + "telemetry" => run_telemetry(&rest), "incident" => run_incident_command(&rest), "shutdown" => run_shutdown(&rest), "package" => run_package(&rest), @@ -187,6 +188,7 @@ pub fn handle_public_command(args: &[String]) -> PublicCommandResult { "model" => rewrite_nested_command("model", "list", "--list-models", &rest), "session" => rewrite_nested_command("session", "export", "--export", &rest), "prompt" => handled_with_exit(crate::prompt_command::run_prompt_command(&rest)), + "factory" => handled_with_exit(crate::factory_command::run_factory_command(&rest)), "config" => { if !rest.is_empty() { return fail(format!("Usage: {APP_NAME} config"), None); @@ -340,8 +342,9 @@ fn validate_schedule_args(args: &[String]) -> bool { } /// Parse `update`'s options into the shared [`crate::self_update::SelfUpdateOptions`]: -/// the TS booleans plus the direct-install pair (`--archive ` with the -/// required `--source `). Returns `None` on a usage failure +/// the TS booleans plus the direct-install pair (`--archive ` with +/// `--source `, which only a managed install takes and requires). +/// Returns `None` on a usage failure /// (already reported). fn parse_update_options(args: &[String]) -> Option { let mut invocation = crate::self_update::SelfUpdateOptions::default(); @@ -410,19 +413,11 @@ fn parse_update_options(args: &[String]) -> Option.", - Some( - "The install source is recorded in the release and future updates resolve from it." - .to_string(), - ), - ); - return None; - } - if !pa_core::update::install::install_source_is_valid( - invocation.source.as_deref().unwrap_or_default(), - ) { + if invocation + .source + .as_deref() + .is_some_and(|source| !pa_core::update::install::install_source_is_valid(source)) + { fail( "--source must be an http(s) URL.", Some(format!("Run \"{APP_NAME} help update\" for usage.")), @@ -489,6 +484,39 @@ fn run_doctor(args: &[String]) -> PublicCommandResult { handled() } +/// `prime-agent telemetry [status|on|off]`: the same report and settings +/// switch as the `/telemetry` slash command, for the current directory's +/// settings scope. +fn run_telemetry(args: &[String]) -> PublicCommandResult { + let usage = || fail(format!("Usage: {APP_NAME} telemetry [status|on|off]"), None); + if args.len() > 1 { + return usage(); + } + let cwd = std::env::current_dir().unwrap_or_default(); + let agent_dir = crate::config::get_agent_dir(); + let mut settings = pa_core::settings::SettingsManager::create(&cwd, &agent_dir); + let report = match args.first().map(String::as_str) { + None | Some("status") => Ok(pa_core::session_engine::telemetry::telemetry_status_text( + &settings, &agent_dir, + )), + Some(choice @ ("on" | "off")) => { + pa_core::session_engine::telemetry::set_telemetry_enabled_text( + &mut settings, + &agent_dir, + choice == "on", + ) + } + Some(_) => return usage(), + }; + match report { + Ok(report) => { + println!("{report}"); + handled() + } + Err(error) => fail(format!("{error:#}"), None), + } +} + /// `prime-agent incident` (TS `runIncidentCommand`): parse the options, /// resolve the window once, and print the timeline. fn run_incident_command(args: &[String]) -> PublicCommandResult { @@ -655,6 +683,44 @@ fn run_update(args: &[String]) -> PublicCommandResult { let Some(options) = parse_update_options(args) else { return handled_failed(); }; + // `--rollback` and `--archive` on an installer install run the bundled + // installer; any other binary stays on the managed-install flow. + if options.rollback || options.archive.is_some() { + if let Some(prefix) = pa_core::update::installer::running_installer_prefix() { + if options.source.is_some() { + return fail( + "--source only applies to managed installs.", + Some( + "An installer install keeps updating from its release channel.".to_string(), + ), + ); + } + if options.channel.is_some() { + return fail( + "--nightly and --stable do not apply to --rollback or --archive.", + Some(format!( + "Switch channels with \"{APP_NAME} update --nightly\" or \"--stable\"." + )), + ); + } + // The CLI's --force is the nightly-switch confirmation skip; + // the installer's own forced daemon stop is a different flag + // the funnel never forwards either — so the local modes refuse + // it instead of silently dropping it. + if options.force { + return fail( + "--force does not apply to --rollback or --archive.", + Some(format!( + "To stop busy daemons first, run \"{APP_NAME} shutdown --force\" and re-run." + )), + ); + } + return handled_with_exit(crate::installer_update::run_local( + &prefix, + options.archive.as_deref(), + )); + } + } let persisted_wire = std::env::current_dir() .ok() .and_then(|cwd| { @@ -673,8 +739,7 @@ fn run_update(args: &[String]) -> PublicCommandResult { } // The installer funnel serves the bare update and the channel flags: // the channel is the flag, else the saved `updateChannel` setting - // (`/nightly on|off`), else the installed one. `--rollback` and - // `--archive` stay on the managed-install flow. + // (`/nightly on|off`), else the installed one. if !options.rollback && options.archive.is_none() { let update = crate::installer_update::UpdateOptions { check: false, @@ -682,6 +747,15 @@ fn run_update(args: &[String]) -> PublicCommandResult { }; return handled_with_exit(crate::installer_update::run(&update)); } + if options.archive.is_some() && options.source.is_none() { + return fail( + "--archive needs --source .", + Some( + "The install source is recorded in the release and future updates resolve from it." + .to_string(), + ), + ); + } handled_with_exit(crate::self_update::run(&options, persisted_wire.as_deref())) } @@ -997,6 +1071,10 @@ mod update_options_tests { Some(std::path::PathBuf::from("/tmp/payload")) ); assert_eq!(invocation.source.as_deref(), Some("https://example.com")); + // An installer install takes the archive alone (the managed flow's + // --source requirement is checked once the install type is known). + let invocation = parse(&["--archive", "/tmp/payload"]).unwrap(); + assert_eq!(invocation.source, None); // The source must be an http(s) URL and must not appear alone. assert!(parse(&[ "--archive", diff --git a/crates/pa-cli/src/subscription_login.rs b/crates/pa-cli/src/subscription_login.rs index f90f01b693a..0508c85baec 100644 --- a/crates/pa-cli/src/subscription_login.rs +++ b/crates/pa-cli/src/subscription_login.rs @@ -19,8 +19,9 @@ use std::path::Path; use std::pin::Pin; use pa_ai::oauth::{ - login_anthropic, login_github_copilot, login_xai, OAuthLoginUi, OAuthPrompt, ProviderHttp, - ANTHROPIC_LOGIN_CANCELLED, COPILOT_LOGIN_CANCELLED, XAI_LOGIN_CANCELLED, + login_anthropic, login_github_copilot, login_xai, AnthropicCredentials, OAuthLoginUi, + OAuthPrompt, ProviderHttp, ANTHROPIC_LOGIN_CANCELLED, COPILOT_LOGIN_CANCELLED, + XAI_LOGIN_CANCELLED, }; use pa_core::auth::{ AuthCredential, AuthStorage, ANTHROPIC_PROVIDER_ID, GITHUB_COPILOT_PROVIDER_ID, XAI_PROVIDER_ID, @@ -147,6 +148,16 @@ pub(crate) async fn run_anthropic_login( if ui.is_cancelled() { return ProviderAuthOutcome::Cancelled; } + store_anthropic_login(agent_dir, provider_name, credentials) +} + +/// TS `completeProviderAuthentication` for Anthropic: store the +/// credential under the provider id and report the TS status. +fn store_anthropic_login( + agent_dir: &Path, + provider_name: &str, + credentials: AnthropicCredentials, +) -> ProviderAuthOutcome { let mut auth = AuthStorage::create(agent_dir); auth.set( ANTHROPIC_PROVIDER_ID, @@ -329,37 +340,22 @@ mod tests { } } - /// One scripted prompt answer: a value (blank included — TS - /// `allowEmpty`'s blank entry) or a cancel. - enum Answer { - Value(String), - Cancel, - } - - /// A scripted surface: the prompt answer, the manual paste, and a - /// shared cancel flag. + /// A scripted surface: the prompt answer and a shared cancel flag. struct ScriptedUi { - prompt: Answer, - manual: Option, + prompt: String, cancelled: Arc, } impl ScriptedUi { fn new() -> Self { ScriptedUi { - prompt: Answer::Value(String::new()), - manual: None, + prompt: String::new(), cancelled: Arc::new(AtomicBool::new(false)), } } fn prompt(mut self, answer: &str) -> Self { - self.prompt = Answer::Value(answer.to_string()); - self - } - - fn cancelled_prompt(mut self) -> Self { - self.prompt = Answer::Cancel; + self.prompt = answer.to_string(); self } @@ -375,11 +371,7 @@ mod tests { &self, _prompt: &OAuthPrompt, ) -> Pin> + Send + '_>> { - let answer = match &self.prompt { - Answer::Value(value) => Some(value.clone()), - Answer::Cancel => None, - }; - Box::pin(std::future::ready(answer)) + Box::pin(std::future::ready(Some(self.prompt.clone()))) } fn on_progress(&self, _message: &str) {} @@ -387,9 +379,7 @@ mod tests { fn on_manual_code_input( &self, ) -> Option> + Send + '_>>> { - self.manual - .as_ref() - .map(|answer| Box::pin(std::future::ready(Some(answer.clone()))) as _) + None } fn is_cancelled(&self) -> bool { @@ -524,27 +514,18 @@ mod tests { assert_eq!(auth.get_api_key("xai"), Some("grok-access".to_string())); } - /// The Anthropic login's run: the flow binds its registered callback - /// port. Skip when another process holds the port — the flow-level - /// tests cover the race; this test covers the store's wire shape. - #[tokio::test] - async fn the_anthropic_login_stores_the_credential_and_reports_the_ts_status() { - let Ok(probe) = std::net::TcpListener::bind(("127.0.0.1", 53_692)) else { - return; // the registered port is busy: this run cannot stage it. - }; - drop(probe); + /// The Anthropic login's store step: the credential's wire shape + /// under the provider id (the flow is covered in pa-ai). + #[test] + fn the_anthropic_login_stores_the_credential_and_reports_the_ts_status() { let dir = tempfile::tempdir().expect("temp dir"); let agent = agent_dir(&dir); - let http = ScriptedHttp::new().queue( - "https://platform.claude.com/v1/oauth/token", - vec![ScriptedHttp::entry( - 200, - r#"{"access_token":"anthropic-access","refresh_token":"anthropic-refresh","expires_in":3600}"#, - )], - ); - let mut ui = ScriptedUi::new().cancelled_prompt(); - ui.manual = Some("http://localhost:53692/callback?code=anthropic-code".to_string()); - match run_anthropic_login(&agent, "Anthropic (Claude Pro/Max)", &http, &ui).await { + let credentials = AnthropicCredentials { + access: "anthropic-access".to_string(), + refresh: "anthropic-refresh".to_string(), + expires: 4_000_000_000_000, + }; + match store_anthropic_login(&agent, "Anthropic (Claude Pro/Max)", credentials) { ProviderAuthOutcome::Status(message) => { assert_eq!( message, @@ -648,120 +629,4 @@ mod tests { ); assert_eq!(expired.get_api_key("xai"), Some("grok-access".to_string())); } - - #[tokio::test] - async fn the_panel_surface_mounts_the_ts_prompts() { - // The panel adapter's seam: on_auth renders the url block (the - // Copilot provider adds its waiting line), the placeholder rides - // the prompt as TS's example line, and the allow-empty prompt - // keeps its blank answer. The url block sends immediately (a - // plain request); the paste/prompt futures are LAZY (the request - // sends on first poll), so the test spawns them and reads the - // requests off the channel's receiving side, bounded by a recv - // timeout that fails the test — never a green-on-timeout retry. - // The prompt futures borrow their adapter, so they drive on a - // select instead of a spawned 'static task (the first poll sends - // the request; the pending future drops with this scope). - let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); - let panel = AuthPanelHandle::new(tx); - let copilot = PanelSubscriptionLoginUi::new(panel, "github-copilot"); - copilot.on_auth( - "https://github.com/login/device", - Some("Enter code: ABCD-1234"), - ); - let url_request = rx.recv().await.expect("the url block sends"); - match url_request { - pa_tui::auth_panel::AuthPanelRequest::AuthUrl { url, instructions } => { - assert_eq!(url, "https://github.com/login/device"); - assert_eq!(instructions.as_deref(), Some("Enter code: ABCD-1234")); - } - _ => panic!("expected the url block request"), - } - // TS `showWaiting` for the Copilot device flow: the dialog's own - // method, on every surface — never the `onProgress` chatter arm. - let waiting = rx.recv().await.expect("the waiting line sends"); - match waiting { - pa_tui::auth_panel::AuthPanelRequest::Waiting { message } => { - assert_eq!(message, "Waiting for browser authentication..."); - } - _ => panic!("expected the waiting line request"), - } - // The domain prompt: the boxed future sends its PastePrompt on - // first poll, then pends on the answer this test never gives. - let mut domain = copilot.on_prompt(&OAuthPrompt { - message: "GitHub Enterprise URL/domain (blank for github.com)".to_string(), - placeholder: Some("company.ghe.com".to_string()), - allow_empty: true, - }); - let prompt_request = tokio::time::timeout(std::time::Duration::from_secs(2), async { - tokio::select! { - request = rx.recv() => request.expect("the channel stays open"), - _ = &mut domain => { - panic!("the prompt future settled without an answer") - } - } - }) - .await - .expect("the prompt request sends once polled"); - match prompt_request { - // TS `showPrompt` renders the placeholder as an example. - pa_tui::auth_panel::AuthPanelRequest::PastePrompt { - prompt, - allow_empty, - .. - } => { - assert_eq!( - prompt, - "GitHub Enterprise URL/domain (blank for github.com) (e.g. company.ghe.com)" - ); - assert!(allow_empty, "the blank entry is the valid default"); - } - _ => panic!("expected the paste prompt request"), - } - // The pending prompt future drops with this scope: no answer is - // expected (the test never mounts the panel). - - // The Anthropic adapter renders the url block without the - // waiting line and arms the TS manual paste. - let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); - let panel = AuthPanelHandle::new(tx); - let anthropic = PanelSubscriptionLoginUi::new(panel, "anthropic"); - anthropic.on_auth( - "https://claude.ai/oauth/authorize", - Some("Complete login in your browser."), - ); - let url_request = rx.recv().await.expect("the url block sends"); - match url_request { - pa_tui::auth_panel::AuthPanelRequest::AuthUrl { url, instructions } => { - assert_eq!(url, "https://claude.ai/oauth/authorize"); - assert_eq!( - instructions.as_deref(), - Some("Complete login in your browser.") - ); - } - _ => panic!("expected the url block request"), - } - let mut manual = anthropic - .on_manual_code_input() - .expect("the panel supplies the paste"); - let paste_request = tokio::time::timeout(std::time::Duration::from_secs(2), async { - tokio::select! { - request = rx.recv() => request.expect("the channel stays open"), - _ = &mut manual => { - panic!("the paste future settled without an answer") - } - } - }) - .await - .expect("the paste request sends once polled"); - match paste_request { - pa_tui::auth_panel::AuthPanelRequest::PastePrompt { prompt, .. } => { - assert_eq!( - prompt, - "Paste redirect URL below, or complete login in browser:" - ); - } - _ => panic!("expected the paste request"), - } - } } diff --git a/crates/pa-cli/src/telemetry_notice.rs b/crates/pa-cli/src/telemetry_notice.rs new file mode 100644 index 00000000000..2d944d85250 --- /dev/null +++ b/crates/pa-cli/src/telemetry_notice.rs @@ -0,0 +1,69 @@ +//! The first-run telemetry disclosure (TS `agent-session-services`) for +//! the headless modes: once per installation the fixed text prints to +//! stderr before a mode's output starts (TS +//! `deferTelemetryNoticeForOnboarding: executionMode === "interactive"`). +//! The interactive mode renders the same disclosure inside the TUI as a +//! session info row (pa-tui's attach) — the alt screen hides a pre-TUI +//! stderr print, so a stderr notice would never be seen there. + +use crate::mode::RuntimeConfig; + +/// Print the once-per-installation telemetry notice when it is due: +/// telemetry enabled (env override, then settings — the `RunOptions` +/// resolution) and not yet shown. +pub(crate) fn print_if_due(config: &RuntimeConfig) { + if config.telemetry_disabled { + return; + } + let mut settings = pa_core::settings::SettingsManager::create(&config.cwd, &config.agent_dir); + if settings.get_telemetry_notice_shown() { + return; + } + eprintln!( + "Prime Agent sends pseudonymous usage and performance metrics without prompts, responses, tool content, file paths, or repository data. Disable this with /telemetry off, telemetry.enabled=false, PRIME_AGENT_TELEMETRY=0, DO_NOT_TRACK=1, or offline mode." + ); + if let Err(error) = settings.set_telemetry_notice_shown(true) { + eprintln!("Warning: could not persist the telemetry notice: {error}"); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::mode::RuntimeConfig; + + fn config_for(dir: &std::path::Path, telemetry_disabled: bool) -> RuntimeConfig { + RuntimeConfig { + cwd: dir.to_path_buf(), + agent_dir: dir.join("agent"), + telemetry_disabled, + ..Default::default() + } + } + + fn notice_shown(dir: &std::path::Path) -> bool { + pa_core::settings::SettingsManager::create(dir, dir.join("agent")) + .get_telemetry_notice_shown() + } + + #[test] + fn headless_discloses_immediately_and_once() { + let dir = tempfile::TempDir::new().expect("temp dir"); + let config = config_for(dir.path(), false); + print_if_due(&config); + assert!(notice_shown(dir.path()), "the first headless run discloses"); + print_if_due(&config); + // The once-per-installation gate is the settings flag, so the + // second call is a no-op by construction. + } + + #[test] + fn disabled_never_discloses() { + let dir = tempfile::TempDir::new().expect("temp dir"); + print_if_due(&config_for(dir.path(), true)); + assert!( + !notice_shown(dir.path()), + "an opted-out run sends no notice" + ); + } +} diff --git a/crates/pa-cli/src/update_flow/successor.rs b/crates/pa-cli/src/update_flow/successor.rs index f496e2388a2..bcb1298e8e3 100644 --- a/crates/pa-cli/src/update_flow/successor.rs +++ b/crates/pa-cli/src/update_flow/successor.rs @@ -70,6 +70,9 @@ pub fn spawn_supervisor( if let Some(roster_path) = roster_path { command.env(UPDATE_ROSTER_ENV, roster_path); } + #[cfg(unix)] + pa_core::platform::process::set_new_session(&mut command); + #[cfg(not(unix))] pa_core::platform::process::set_new_process_group(&mut command); let child = command .spawn() diff --git a/crates/pa-cli/src/update_flow/update_command.rs b/crates/pa-cli/src/update_flow/update_command.rs index 702f6b64951..6d937b267cd 100644 --- a/crates/pa-cli/src/update_flow/update_command.rs +++ b/crates/pa-cli/src/update_flow/update_command.rs @@ -211,7 +211,10 @@ pub async fn run_update_command(options: &UpdateCommandOptions) -> Result { .env_remove(pa_daemon::worker::WORKER_SOCKET_ENV) .env_remove(pa_daemon::worker::WORKER_INSTANCE_ID_ENV) .env_remove(pa_daemon::worker::WORKER_SCRIPT_ENV); - // Detached: own process group, reaped by init, survives this CLI. + // The coordinator must survive the CLI and its terminal closing. + #[cfg(unix)] + pa_core::platform::process::set_new_session(&mut command); + #[cfg(not(unix))] pa_core::platform::process::set_new_process_group(&mut command); let child = command.spawn().with_context(|| { format!( diff --git a/crates/pa-cli/tests/acp_config_option_e2e.rs b/crates/pa-cli/tests/acp_config_option_e2e.rs index c3ab8675b4a..abc0db9d7d3 100644 --- a/crates/pa-cli/tests/acp_config_option_e2e.rs +++ b/crates/pa-cli/tests/acp_config_option_e2e.rs @@ -19,14 +19,17 @@ //! ACP picker e2e (the TS #2455 port): `session/new` advertises the //! standard `model` and `thought_level` select options, and -//! `session/set_config_option` applies selections — the effort picker -//! follows the selected model's supported levels, invalid values are +//! `session/set_config_option` applies selections — the effort select +//! offers the session model's supported levels, invalid values are //! `-32602` invalid params, and real selections publish the -//! `config_option_update` notification. +//! `config_option_update` notification. Model switches are observable: +//! the faux worker resolves a switched selection through the registry +//! (a models.json faux-api model streams through the same scripted +//! provider), so the pickers follow the switch. //! -//! Both transports are covered: the in-process mode (the faux provider -//! with a models.json fixture model, so a switch is observable end to -//! end) and the daemon-attached mode (the worker's own wire commands). +//! The daemon-attached transport covers the surface: the pickers ride +//! the worker's own wire commands, with a models.json fixture in the +//! agent dir for discovery. use std::io::{BufRead, BufReader, Write}; use std::process::{Child, Command, Stdio}; @@ -42,71 +45,11 @@ struct AcpChild { stdin: std::process::ChildStdin, lines: Receiver, next_id: u64, - /// Held so the child's cwd outlives the process. - home: tempfile::TempDir, + /// Held (never read) so the child's cwd outlives the process. + _home: tempfile::TempDir, } impl AcpChild { - /// Spawn the ACP binary with a models.json fixture in place before the - /// process starts (discovery reads it through the registry). - fn spawn_with_models( - args: &[&str], - script: &serde_json::Value, - models_json: Option, - extra_env: &[(String, String)], - ) -> AcpChild { - let home = tempfile::TempDir::new().unwrap(); - let agent_dir = home.path().join("agent"); - std::fs::create_dir_all(&agent_dir).unwrap(); - if let Some(models) = models_json { - std::fs::write(agent_dir.join("models.json"), models.to_string()).unwrap(); - } - let bin = env!("CARGO_BIN_EXE_prime-agent"); - let mut command = Command::new(bin); - command - .args(args) - .env("HOME", home.path()) - // The CLI's agent dir (`PRIME_AGENT_CODING_AGENT_DIR`) is where - // the registry reads models.json from; the runtime env name - // rides along for the kernel-side paths. - .env("PRIME_AGENT_CODING_AGENT_DIR", &agent_dir) - .env("PRIME_AGENT_AGENT_DIR", &agent_dir) - .env("PRIME_AGENT_FAUX_SCRIPT", script.to_string()); - for (key, value) in extra_env { - command.env(key, value); - } - let mut child = command - .current_dir(home.path()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .expect("binary present"); - let stdin = child.stdin.take().expect("stdin piped"); - let stdout = child.stdout.take().expect("stdout piped"); - let (tx, lines) = channel(); - std::thread::spawn(move || { - let reader = BufReader::new(stdout); - for line in reader.lines() { - match line { - Ok(line) => { - if tx.send(line).is_err() { - return; - } - } - Err(_) => return, - } - } - }); - AcpChild { - child, - stdin, - lines, - next_id: 0, - home, - } - } - fn send(&mut self, frame: &Value) { let mut line = serde_json::to_string(&frame).unwrap(); line.push('\n'); @@ -236,67 +179,104 @@ fn faux_models_fixture() -> serde_json::Value { "maxTokens": 4_096, } ], - }, - // A provider whose models discovery admits (the `authHeader` - // config counts as configured request auth) but whose - // credential cannot resolve at request time (a set-but-empty - // env var is a missing credential): a switch to it must refuse - // before anything moves, not install an unauthenticated - // target. - "locked": { - "api": "faux", - "baseUrl": "http://localhost:0", - "authHeader": true, - "apiKey": "PROBE_LOCKED_KEY", - "models": [ - { - "id": "locked-model", - "name": "Locked Model", - "api": "faux", - "baseUrl": "http://localhost:0", - "contextWindow": 128_000, - "maxTokens": 4_096, - } - ], } } }) } -/// The in-process transport: the pickers are advertised at `session/new`, -/// applied through `session/set_config_option` (a real model switch — -/// the engine and the provider target follow), invalid values are invalid -/// params, and changes publish `config_option_update`. +/// The daemon-attached transport: the pickers ride the worker's own wire +/// commands — advertised at `session/new`, the effort selection applied +/// through `set_thinking_level`, invalid values refused, and a real +/// model switch observable (the plain model drops the effort picker, the +/// map model accepts `xhigh`, and the turn after a switch settles). #[test] -fn acp_in_process_config_option_advertises_and_applies() { - let script = json!({ - "reasoning": true, - "responses": ["one answer", "the switched model still streams"], - }); - let mut client = AcpChild::spawn_with_models( - &["--mode", "acp", "--no-session"], - &script, - Some(faux_models_fixture()), - // The locked provider's credential reference resolves to nothing: - // a set-but-empty env var is a missing credential. - &[("PROBE_LOCKED_KEY".to_string(), String::new())], - ); +fn acp_daemon_attached_config_option_pickers() { + let home = tempfile::TempDir::new().unwrap(); + let socket = home.path().join("daemon.sock"); + let agent_dir = home.path().join("agent"); + std::fs::create_dir_all(&agent_dir).unwrap(); + // The worker inherits the CLI's agent dir, so the fixture models ride + // the same discovery. + std::fs::write( + agent_dir.join("models.json"), + faux_models_fixture().to_string(), + ) + .unwrap(); + let script_path = home.path().join("worker-script.json"); + std::fs::write( + &script_path, + json!({ + "engine": "faux", + "reasoning": true, + "responses": ["The Thames flows through London.", "The switched model still streams."], + }) + .to_string(), + ) + .unwrap(); + let bin = env!("CARGO_BIN_EXE_prime-agent"); + let mut child = Command::new(bin) + .args([ + "--mode", + "acp", + "--no-session", + "--daemon-socket", + socket.to_str().unwrap(), + ]) + .env("HOME", home.path()) + .env("PRIME_AGENT_CODING_AGENT_DIR", &agent_dir) + .env("PRIME_AGENT_FAUX_SCRIPT", &script_path) + .env( + pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV, + "15000", + ) + .current_dir(home.path()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("binary present"); + let stdin = child.stdin.take().expect("stdin piped"); + let stdout = child.stdout.take().expect("stdout piped"); + let mut client = { + let (tx, rx) = channel(); + std::thread::spawn(move || { + let reader = BufReader::new(stdout); + for line in reader.lines() { + match line { + Ok(line) => { + if tx.send(line).is_err() { + return; + } + } + Err(_) => return, + } + } + }); + AcpChild { + child, + stdin, + lines: rx, + next_id: 0, + _home: home, + } + }; let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); let new = client.request("session/new", &json!({ "mcpServers": [] })); let (new_response, _) = client.wait_response(new, TIMEOUT); let result = &new_response["result"]; + assert!( + result["sessionId"].is_string(), + "daemon-attached admission succeeds: {new_response}" + ); let session_id = result["sessionId"].as_str().unwrap().to_string(); - // Both pickers advertised: the model select (current model always - // selectable, the discovered fixture model listed) and the effort - // select on the reasoning model's supported levels. + // Both pickers advertised off the worker's connection state: the + // model select (current model always selectable, the discovered + // fixture model listed) and the effort select on the reasoning + // model's supported levels. let options = &result["configOptions"]; - assert_eq!( - options.as_array().map(Vec::len), - Some(2), - "two options: {options}" - ); + assert_eq!(options.as_array().map(Vec::len), Some(2), "{options}"); let model_option = &options[0]; assert_eq!(model_option["id"], "model"); assert_eq!(model_option["type"], "select"); @@ -324,7 +304,8 @@ fn acp_in_process_config_option_advertises_and_applies() { .collect(); assert_eq!(levels, vec!["off", "minimal", "low", "medium", "high"]); - // A valid effort selection applies and publishes the change. + // An effort selection applies through the worker's switch and + // publishes the change. let select = client.request( "session/set_config_option", &select_params(&session_id, "thought_level", &json!("high")), @@ -332,7 +313,7 @@ fn acp_in_process_config_option_advertises_and_applies() { let (response, notifications) = client.wait_response(select, TIMEOUT); assert_eq!( response["result"]["configOptions"][1]["currentValue"], "high", - "the effort picker follows the selection: {response}" + "{response}" ); let updates = config_updates(¬ifications); assert!( @@ -342,14 +323,14 @@ fn acp_in_process_config_option_advertises_and_applies() { "the change publishes config_option_update: {notifications:?}" ); - // Invalid selections are invalid params with the TS reasons. + // Unsupported levels and models are refused with the TS reasons. for (config_id, value, reason) in [ - ("model", json!("missing"), "Unavailable model: missing"), ( "thought_level", json!("sideways"), "Unsupported reasoning effort: sideways", ), + ("model", json!("missing"), "Unavailable model: missing"), ( "unknown", json!("high"), @@ -375,7 +356,6 @@ fn acp_in_process_config_option_advertises_and_applies() { response["error"]["data"]["reason"], "Unknown ACP session: missing-session" ); - // A real model switch: the plain model drops the effort picker (its // supported levels are `off` only) and the effort selection now // refuses. @@ -412,7 +392,7 @@ fn acp_in_process_config_option_advertises_and_applies() { "session/prompt", &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "hello" }] }), ); - let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); + let (prompt_response, _) = client.wait_response(prompt, Duration::from_mins(2)); assert_eq!(prompt_response["result"]["stopReason"], "end_turn"); // Switching back restores the effort picker at the persisted default @@ -480,273 +460,6 @@ fn acp_in_process_config_option_advertises_and_applies() { "the mapped level publishes config_option_update: {notifications:?}" ); - // An uncredentialed model discovery admits (the locked provider's - // auth cannot resolve): the switch refuses BEFORE anything moves — - // the next turn keeps running on the model it still reports. - let locked = r#"["locked","locked-model"]"#; - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "model", &json!(locked)), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!(response["error"]["code"], -32603, "{response}"); - assert!( - response["error"]["data"]["details"] - .as_str() - .unwrap_or_default() - .contains("No API key found"), - "the refusal names the missing credentials: {response}" - ); - // Nothing moved: the map model is still the live selection (the - // no-op re-selection refreshes and reports it). - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "model", &json!(map)), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!( - response["result"]["configOptions"][0]["currentValue"], map, - "the uncredentialed switch mutated nothing: {response}" - ); - - let close = client.request("session/close", &json!({ "sessionId": session_id })); - let (close_response, _) = client.wait_response(close, TIMEOUT); - assert_eq!(close_response["result"], json!({})); -} - -/// The in-process transport's failed settings persist: the switch's live -/// state is applied COHERENTLY (TS `setModel` rejects after the model -/// assignment and the durable row, with the switched session applied), -/// never split — `current_model()` and the agent agree on the switched -/// model even when the settings write fails, so the effort gate answers -/// for the model the turns run on. The settings write fails because the -/// global settings file is a directory (the lock step cannot read it). -#[test] -fn acp_in_process_failed_settings_persist_keeps_the_session_coherent() { - let script = json!({ - "reasoning": true, - "responses": ["one answer", "still streaming on the switched model"], - }); - let mut client = AcpChild::spawn_with_models( - &["--mode", "acp", "--no-session"], - &script, - Some(faux_models_fixture()), - &[], - ); - // The global settings file as a directory: every settings write - // fails at its read step, deterministically. - std::fs::create_dir_all(client.home.path().join("agent").join("settings.json")).unwrap(); - let init = client.request("initialize", &initialize_params()); - let _ = client.wait_response(init, TIMEOUT); - let new = client.request("session/new", &json!({ "mcpServers": [] })); - let (new_response, _) = client.wait_response(new, TIMEOUT); - let result = &new_response["result"]; - let session_id = result["sessionId"].as_str().unwrap().to_string(); - assert_eq!( - result["configOptions"].as_array().map(Vec::len), - Some(2), - "the admission is unaffected: {result}" - ); - - // The switch fails at the settings persist (the live switch and the - // durable row already ran): the RPC reports the failure... - let plain = r#"["faux","plain-model"]"#; - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "model", &json!(plain)), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!(response["error"]["code"], -32603, "{response}"); - assert!( - response["error"]["data"]["details"] - .as_str() - .unwrap_or_default() - .contains("model switch failed"), - "the refusal names the switch: {response}" - ); - - // ...but the session is NOT split: the effort gate answers for the - // model the turns run on (the switched plain model), so `high` is - // unsupported — a stale pre-switch slot would have accepted it. - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "thought_level", &json!("high")), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!(response["error"]["code"], -32602, "{response}"); - assert_eq!( - response["error"]["data"]["reason"], - "Unsupported reasoning effort: high" - ); - - // The live session runs the switched model (TS's reject-with-state- - // applied): the no-op re-selection refreshes and reports it... - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "model", &json!(plain)), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!( - response["result"]["configOptions"][0]["currentValue"], plain, - "the switched model is live and reported: {response}" - ); - assert_eq!( - response["result"]["configOptions"].as_array().map(Vec::len), - Some(1), - "the plain model serves no effort picker: {response}" - ); - - // ...and the next turn still streams on it. - let prompt = client.request( - "session/prompt", - &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "hello" }] }), - ); - let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); - assert_eq!(prompt_response["result"]["stopReason"], "end_turn"); - - let close = client.request("session/close", &json!({ "sessionId": session_id })); - let (close_response, _) = client.wait_response(close, TIMEOUT); - assert_eq!(close_response["result"], json!({})); -} - -/// The daemon-attached transport: the pickers ride the worker's own wire -/// commands — advertised at `session/new`, the effort selection applied -/// through `set_thinking_level`, invalid values refused, and the turn -/// after a switch still settles. -#[test] -fn acp_daemon_attached_config_option_pickers() { - let home = tempfile::TempDir::new().unwrap(); - let socket = home.path().join("daemon.sock"); - let script_path = home.path().join("worker-script.json"); - std::fs::write( - &script_path, - json!({ - "engine": "faux", - "reasoning": true, - "responses": ["The Thames flows through London."], - }) - .to_string(), - ) - .unwrap(); - let bin = env!("CARGO_BIN_EXE_prime-agent"); - let mut child = Command::new(bin) - .args([ - "--mode", - "acp", - "--no-session", - "--daemon-socket", - socket.to_str().unwrap(), - ]) - .env("HOME", home.path()) - .env("PRIME_AGENT_CODING_AGENT_DIR", home.path().join("agent")) - .env("PRIME_AGENT_AGENT_DIR", home.path().join("agent")) - .env("PRIME_AGENT_ACP_DAEMON_SCRIPT", &script_path) - .env( - pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV, - "15000", - ) - .current_dir(home.path()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .expect("binary present"); - let stdin = child.stdin.take().expect("stdin piped"); - let stdout = child.stdout.take().expect("stdout piped"); - let mut client = { - let (tx, rx) = channel(); - std::thread::spawn(move || { - let reader = BufReader::new(stdout); - for line in reader.lines() { - match line { - Ok(line) => { - if tx.send(line).is_err() { - return; - } - } - Err(_) => return, - } - } - }); - AcpChild { - child, - stdin, - lines: rx, - next_id: 0, - home, - } - }; - let init = client.request("initialize", &initialize_params()); - let _ = client.wait_response(init, TIMEOUT); - let new = client.request("session/new", &json!({ "mcpServers": [] })); - let (new_response, _) = client.wait_response(new, TIMEOUT); - let result = &new_response["result"]; - assert!( - result["sessionId"].is_string(), - "daemon-attached admission succeeds: {new_response}" - ); - let session_id = result["sessionId"].as_str().unwrap().to_string(); - - // Both pickers advertised off the worker's connection state. - let options = &result["configOptions"]; - assert_eq!(options.as_array().map(Vec::len), Some(2), "{options}"); - assert_eq!(options[0]["id"], "model"); - assert_eq!(options[0]["currentValue"], r#"["faux","faux-1"]"#); - assert_eq!(options[1]["id"], "thought_level"); - assert_eq!(options[1]["currentValue"], "medium"); - - // An effort selection applies through the worker's switch and - // publishes the change. - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "thought_level", &json!("high")), - ); - let (response, notifications) = client.wait_response(select, TIMEOUT); - assert_eq!( - response["result"]["configOptions"][1]["currentValue"], "high", - "{response}" - ); - let updates = config_updates(¬ifications); - assert!( - updates - .iter() - .any(|update| update["params"]["update"]["configOptions"][1]["currentValue"] == "high"), - "the change publishes config_option_update: {notifications:?}" - ); - - // Unsupported levels and models are refused with the TS reasons; the - // current model re-selects cleanly. - for (config_id, value, reason) in [ - ( - "thought_level", - json!("sideways"), - "Unsupported reasoning effort: sideways", - ), - ("model", json!("missing"), "Unavailable model: missing"), - ( - "unknown", - json!("high"), - "Invalid configuration option: unknown", - ), - ] { - let select = client.request( - "session/set_config_option", - &select_params(&session_id, config_id, &value), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!(response["error"]["code"], -32602, "{config_id}: {response}"); - assert_eq!(response["error"]["data"]["reason"], reason); - } - let select = client.request( - "session/set_config_option", - &select_params(&session_id, "model", &json!(r#"["faux","faux-1"]"#)), - ); - let (response, _) = client.wait_response(select, TIMEOUT); - assert_eq!( - response["result"]["configOptions"][0]["currentValue"], - r#"["faux","faux-1"]"# - ); - // The switched level survives a turn: the prompt still settles. let prompt = client.request( "session/prompt", diff --git a/crates/pa-cli/tests/acp_mode_e2e.rs b/crates/pa-cli/tests/acp_mode_e2e.rs index 34b6bde8c7f..08794441ea5 100644 --- a/crates/pa-cli/tests/acp_mode_e2e.rs +++ b/crates/pa-cli/tests/acp_mode_e2e.rs @@ -17,9 +17,10 @@ clippy::cast_precision_loss )] -//! End-to-end ACP-mode verification: the real binary speaks the ACP -//! JSON-RPC surface over stdio, driven by the scripted faux provider, and -//! the emitted frames are checked against the TS capture corpus +//! End-to-end ACP-mode verification: the real binary serves the ACP +//! JSON-RPC surface over stdio on the daemon-attached transport (a +//! sandboxed supervisor hosting a scripted faux worker), and the emitted +//! frames are checked against the TS capture corpus //! (`crates/pa-daemon/testdata/acp`). use std::io::{BufRead, BufReader, Write}; @@ -33,25 +34,31 @@ use serde_json::{json, Value}; /// child process (its cwd), so it is held on the struct. struct AcpChild { child: Child, - stdin: std::process::ChildStdin, + /// `None` once [`AcpChild::close_stdin`] sent EOF. + stdin: Option, lines: Receiver, next_id: u64, /// Held (never read) so the child's cwd directory outlives the process: /// dropping the tempdir deletes it and the child's `current_dir` fails. - _home: tempfile::TempDir, + /// `None` for a second child sharing another child's home. + _home: Option, spawn_stderr: Option, - /// The sandboxed supervisor socket a daemon-attached child spawned: - /// set only by [`AcpChild::spawn_daemon_attached`], and the drop shuts + /// The sandboxed supervisor socket the child spawned: the drop shuts /// the supervisor down with it (a killed child must not leak the /// supervisor into later test binaries). - daemon_socket: Option, + socket: std::path::PathBuf, } impl AcpChild { /// Wire one spawned process into the reader thread and the handle: /// the tempdir is held on the struct so the child's cwd directory - /// outlives the process. - fn wrap(mut child: std::process::Child, home: tempfile::TempDir) -> AcpChild { + /// outlives the process, and the supervisor socket is the one the + /// drop shuts down. + fn wrap( + mut child: std::process::Child, + home: Option, + socket: std::path::PathBuf, + ) -> AcpChild { let stdin = child.stdin.take().expect("stdin piped"); let stdout = child.stdout.take().expect("stdout piped"); let stderr = child.stderr.take().expect("stderr piped"); @@ -71,78 +78,65 @@ impl AcpChild { }); AcpChild { child, - stdin, + stdin: Some(stdin), lines, next_id: 0, _home: home, spawn_stderr: Some(stderr), - daemon_socket: None, + socket, } } + /// The daemon-attached transport: the child spawns its own sandboxed + /// supervisor on `/daemon.sock` and hosts the scripted worker + /// through the `PRIME_AGENT_FAUX_SCRIPT` create-config seam; + /// the drop shuts the supervisor down. The socket sits on the struct + /// for tests that speak raw daemon commands alongside the ACP frames. fn spawn(args: &[&str], script: &serde_json::Value) -> AcpChild { let home = tempfile::TempDir::new().unwrap(); - let bin = env!("CARGO_BIN_EXE_prime-agent"); - let child = Command::new(bin) - .args(args) - .env("HOME", home.path()) - .env("PRIME_AGENT_AGENT_DIR", home.path().join("agent")) - .env("PRIME_AGENT_FAUX_SCRIPT", script.to_string()) - .current_dir(home.path()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) + let socket = home.path().join("daemon.sock"); + std::fs::write(home.path().join("worker-script.json"), script.to_string()).unwrap(); + let child = daemon_attached_command(home.path(), &socket, args) .spawn() .expect("binary present"); - Self::wrap(child, home) + Self::wrap(child, Some(home), socket) } - /// The daemon-attached transport: the child spawns its own sandboxed - /// supervisor on `/daemon.sock` and hosts the scripted worker - /// through the `PRIME_AGENT_ACP_DAEMON_SCRIPT` create-config seam; - /// the drop shuts the supervisor down. The socket path is returned - /// for tests that speak raw daemon commands alongside the ACP frames. - fn spawn_daemon_attached( + /// The daemon-attached lane whose `rlm.spawn` children are scripted: + /// `/child-script.json` rides the `PRIME_AGENT_FAUX_CHILD_SCRIPT` + /// create-config seam (the worker already consumes the key), and the + /// parent worker's kernel runs the given interpreter (the env flows + /// ACP -> supervisor -> worker). + fn spawn_with_child_script( args: &[&str], script: &serde_json::Value, - ) -> (AcpChild, std::path::PathBuf) { + child_script: &serde_json::Value, + kernel_python: &std::path::Path, + ) -> AcpChild { let home = tempfile::TempDir::new().unwrap(); let socket = home.path().join("daemon.sock"); - let script_path = home.path().join("worker-script.json"); - std::fs::write(&script_path, script.to_string()).unwrap(); - let bin = env!("CARGO_BIN_EXE_prime-agent"); - let child = Command::new(bin) - .args(args) - .arg("--daemon-socket") - .arg(&socket) - .env("HOME", home.path()) - .env("PRIME_AGENT_AGENT_DIR", home.path().join("agent")) - .env("PRIME_AGENT_ACP_DAEMON_SCRIPT", &script_path) - // The ACP child spawns the sandboxed supervisor, which spawns - // the session worker; the supervisor-lost exit (TS - // `exitIfSupervisorOrphanedForTooLong`) runs on this short - // window (the env flows child -> supervisor -> worker) - // instead of the 5-minute default. - .env( - pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV, - "15000", - ) - .current_dir(home.path()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) + let child_script_path = home.path().join("child-script.json"); + std::fs::write(home.path().join("worker-script.json"), script.to_string()).unwrap(); + std::fs::write(&child_script_path, child_script.to_string()).unwrap(); + let child = daemon_attached_command(home.path(), &socket, args) + .env("PRIME_AGENT_FAUX_CHILD_SCRIPT", &child_script_path) + .env("PRIME_AGENT_KERNEL_PYTHON", kernel_python) .spawn() .expect("binary present"); - let mut client = Self::wrap(child, home); - client.daemon_socket = Some(socket.clone()); - (client, socket) + Self::wrap(child, Some(home), socket) } fn send(&mut self, frame: &Value) { let mut line = serde_json::to_string(&frame).unwrap(); line.push('\n'); - self.stdin.write_all(line.as_bytes()).unwrap(); - self.stdin.flush().unwrap(); + let stdin = self.stdin.as_mut().expect("stdin open"); + stdin.write_all(line.as_bytes()).unwrap(); + stdin.flush().unwrap(); + } + + /// Send EOF: the client disconnects. + fn close_stdin(&mut self) { + self.stdin = None; } fn request(&mut self, method: &str, params: &Value) -> u64 { @@ -186,6 +180,85 @@ impl AcpChild { } } } + + /// Read frames until one matches — the predicate readiness signal, + /// never a timer. The frames before it are dropped. + fn wait_frame( + &mut self, + timeout: Duration, + mut frame_matches: impl FnMut(&Value) -> bool, + ) -> Value { + let deadline = Instant::now() + timeout; + loop { + let timeout_left = deadline.saturating_duration_since(Instant::now()); + assert!( + !timeout_left.is_zero(), + "the stream never published the awaited frame" + ); + let line = self + .lines + .recv_timeout(timeout_left) + .expect("the ACP stream stayed open"); + let frame: Value = serde_json::from_str(&line).expect("valid JSON line"); + if frame_matches(&frame) { + return frame; + } + } + } + + /// Read frames until one `sessionUpdate` of `kind` arrives — the + /// observed-event readiness signal, never a timer. The frames + /// before it are dropped. + fn wait_update(&mut self, kind: &str, timeout: Duration) { + self.wait_frame(timeout, |frame| { + frame["params"]["update"]["sessionUpdate"] == kind + }); + } +} + +/// The daemon-attached child's command on `home` and `socket`: the +/// supervisor-lost exit (TS `exitIfSupervisorOrphanedForTooLong`) runs on +/// a short window (the env flows child -> supervisor -> worker) instead +/// of the 5-minute default, and the worker script is +/// `/worker-script.json`. +fn daemon_attached_command( + home: &std::path::Path, + socket: &std::path::Path, + args: &[&str], +) -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_prime-agent")); + command + .args(args) + .arg("--daemon-socket") + .arg(socket) + .env("HOME", home) + .env("PRIME_AGENT_FAUX_SCRIPT", home.join("worker-script.json")) + .env( + pa_daemon::worker::WORKER_SUPERVISOR_LOST_EXIT_MS_ENV, + "15000", + ) + .current_dir(home) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + command +} + +/// The sandbox's single worker descriptor. +fn worker_descriptor(home: &std::path::Path) -> Value { + std::fs::read_dir(home.join(".prime/agent/daemon-workers")) + .expect("descriptor instances") + .flatten() + .flat_map(|instance| { + std::fs::read_dir(instance.path()) + .expect("instance dir") + .flatten() + }) + .filter_map(|file| { + serde_json::from_str::(&std::fs::read_to_string(file.path()).ok()?).ok() + }) + .find(|descriptor| descriptor.get("authenticationToken").is_some()) + .expect("the worker descriptor") } impl Drop for AcpChild { @@ -200,9 +273,7 @@ impl Drop for AcpChild { eprintln!("ACP child stderr: {text}"); } } - if let Some(socket) = self.daemon_socket.take() { - shutdown_sandboxed_daemon(&socket); - } + shutdown_sandboxed_daemon(&self.socket); } } @@ -216,6 +287,17 @@ fn initialize_params() -> Value { const TIMEOUT: Duration = Duration::from_mins(1); +/// Assert a settled turn's stop reason, printing the whole response +/// envelope on mismatch: an `internal_error`'s failure text (the +/// `prime-agent turn failed: ` message) is otherwise lost — the +/// `stopReason: null` arms of `assert_eq!` show only the null. +fn assert_end_turn(response: &Value) { + assert_eq!( + response["result"]["stopReason"], "end_turn", + "the turn's response envelope: {response}" + ); +} + /// The TS initialize response shape (capture `ts-happy_path.jsonl`), with the /// version and sessionId-class fields normalized as volatile. #[test] @@ -259,7 +341,7 @@ fn acp_second_initialize_is_served() { #[test] fn acp_prompt_stream_completion_envelope_and_stop_reason_match_ts() { - let script = json!({ "responses": ["ACP-OK"] }); + let script = json!({ "engine": "faux", "responses": ["ACP-OK"] }); let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); @@ -346,7 +428,7 @@ fn acp_prompt_stream_completion_envelope_and_stop_reason_match_ts() { #[test] fn acp_prompt_chunk_carries_the_assistant_message_id() { - let script = json!({ "responses": ["ACP-OK"] }); + let script = json!({ "engine": "faux", "responses": ["ACP-OK"] }); let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); @@ -519,18 +601,6 @@ fn acp_cancel_without_an_active_turn_is_a_noop() { assert_eq!(close_response["result"], json!({})); } -#[test] -fn acp_initialize_advertises_mcp_capabilities() { - let script = json!({ "responses": ["unused"] }); - let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); - let init = client.request("initialize", &initialize_params()); - let (response, _) = client.wait_response(init, TIMEOUT); - assert_eq!( - response["result"]["agentCapabilities"]["mcpCapabilities"], - json!({ "http": true }) - ); -} - #[test] fn acp_mcp_admission_accepts_valid_servers_and_close_releases() { let script = json!({ "responses": ["unused"] }); @@ -702,73 +772,115 @@ fn acp_mcp_long_names_fail_at_tool_derivation_with_internal_error() { } #[test] -fn acp_daemon_attached_serves_a_client_owned_session() { - // The daemon-attached transport: the binary spawns a supervisor on - // the sandboxed socket, hosts a client-owned scripted session, and - // serves the same ACP surface (chunk + settle envelope + end_turn). - let script = json!({ "engine": "faux", "responses": ["The Thames flows through London."] }); - let (mut client, _socket) = - AcpChild::spawn_daemon_attached(&["--mode", "acp", "--no-session"], &script); +fn acp_daemon_attached_close_clears_the_connection_mcp_servers() { + let script = json!({ "engine": "faux", "responses": ["unused"] }); + let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); + let socket = client.socket.clone(); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); - let new = client.request("session/new", &json!({ "mcpServers": [] })); - let (new_response, _) = client.wait_response(new, Duration::from_mins(1)); - assert!( - new_response["result"]["sessionId"].is_string(), - "daemon-attached admission succeeds: {new_response}" + let new = client.request( + "session/new", + &json!({ "mcpServers": [ + { "name": "close-clear", "type": "http", "url": "https://mcp.invalid/close-clear", "headers": [] }, + ]}), ); + let (new_response, _) = client.wait_response(new, TIMEOUT); let session_id = new_response["result"]["sessionId"] .as_str() - .unwrap() + .expect("admission succeeds") .to_string(); - let prompt = client.request( - "session/prompt", - &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "Name a river." }] }), + let active_session_id = wait_live_session(&socket)["activeSessionId"] + .as_str() + .expect("the daemon session id") + .to_string(); + let owned = probe_acp_mcp_servers(&socket, &active_session_id); + assert_eq!( + owned["success"], false, + "the open session owns its admitted servers: {owned}" + ); + assert_eq!( + owned["error"], + "ACP MCP configuration is owned by another client" ); - let (prompt_response, updates) = client.wait_response(prompt, Duration::from_mins(2)); - assert_eq!(prompt_response["result"]["stopReason"], "end_turn"); - let chunk = updates - .iter() - .find(|update| update["params"]["update"]["sessionUpdate"] == "agent_message_chunk"); - assert!(chunk.is_some(), "the daemon stream maps to ACP chunks"); let close = client.request("session/close", &json!({ "sessionId": session_id })); - let (close_response, _) = client.wait_response(close, Duration::from_mins(1)); + let (close_response, _) = client.wait_response(close, TIMEOUT); assert_eq!(close_response["result"], json!({})); - drop(client); + let released = probe_acp_mcp_servers(&socket, &active_session_id); + assert_eq!( + released["success"], true, + "session/close released the connection's servers: {released}" + ); } #[test] -fn acp_daemon_attached_admits_mcp_servers_through_the_wire() { - let (mut client, _socket) = AcpChild::spawn_daemon_attached( - &["--mode", "acp", "--no-session"], - &json!({ "responses": ["unused"] }), - ); +fn acp_daemon_attached_eof_teardown_clears_the_connection_mcp_servers() { + let script = json!({ "engine": "faux", "responses": ["unused"] }); + let mut client = AcpChild::spawn(&["--mode", "acp"], &script); + let socket = client.socket.clone(); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); - // The admission validation runs in the transport; the servers ride - // the replace_acp_mcp_servers wire command to the worker. let new = client.request( "session/new", &json!({ "mcpServers": [ - { "name": "capture-stdio", "type": "stdio", "command": "cat", "args": [], "env": [] }, + { "name": "eof-clear", "type": "http", "url": "https://mcp.invalid/eof-clear", "headers": [] }, ]}), ); - let (new_response, _) = client.wait_response(new, Duration::from_mins(1)); + let (new_response, _) = client.wait_response(new, TIMEOUT); assert!( new_response["result"]["sessionId"].is_string(), - "{}", - new_response + "admission succeeds: {new_response}" ); - // A schema-invalid entry is dropped before the wire, exactly like - // the in-process path. - let close_id = new_response["result"]["sessionId"] + let active_session_id = wait_live_session(&socket)["activeSessionId"] .as_str() - .unwrap() + .expect("the daemon session id") .to_string(); - let close = client.request("session/close", &json!({ "sessionId": close_id })); - let (close_response, _) = client.wait_response(close, Duration::from_mins(1)); - assert_eq!(close_response["result"], json!({})); - drop(client); + let owned = probe_acp_mcp_servers(&socket, &active_session_id); + assert_eq!( + owned["success"], false, + "the open session owns its admitted servers: {owned}" + ); + client.close_stdin(); + assert!(client.child.wait().expect("the ACP child exits").success()); + let released = probe_acp_mcp_servers(&socket, &active_session_id); + assert_eq!( + released["success"], true, + "the EOF teardown released the connection's servers: {released}" + ); +} + +#[test] +fn acp_mode_daemon_unreachable_exits_1_without_fallback() { + let home = tempfile::TempDir::new().unwrap(); + let socket = home.path().join("daemon.sock"); + std::fs::write(&socket, "not a socket").unwrap(); + std::fs::write( + home.path().join("worker-script.json"), + json!({ "engine": "faux", "responses": ["unused"] }).to_string(), + ) + .unwrap(); + let output = daemon_attached_command(home.path(), &socket, &["--mode", "acp", "--no-session"]) + .output() + .expect("binary present"); + assert_eq!( + output.status.code(), + Some(1), + "exit 1: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert!( + output.stdout.is_empty(), + "no ACP frames on stdout: {}", + String::from_utf8_lossy(&output.stdout) + ); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + stderr.contains("Error: "), + "the startup failure is an error line: {stderr}" + ); + assert!( + stderr.contains("Timed out waiting for the Prime Agent daemon to start"), + "the daemon never became reachable: {stderr}" + ); } #[test] @@ -776,7 +888,7 @@ fn acp_daemon_attached_cancels_mid_turn() { // A scripted worker with a slow turn: the cancel lands while the // turn runs, and the prompt resolves `{stopReason: "cancelled"}` // with no boundary frames — the TS daemon-attached cancel shape. - let (mut client, _socket) = AcpChild::spawn_daemon_attached( + let mut client = AcpChild::spawn( &["--mode", "acp", "--no-session"], &json!({ "responses": [{ "text": "a slow answer", "delayMs": 8000 }] }), ); @@ -807,18 +919,13 @@ fn acp_daemon_attached_cancels_mid_turn() { } #[test] -fn acp_daemon_attached_prompt_after_cancel_runs() { - // A Stop -> resend flow: a mid-turn cancel leaves the worker's - // queued-input admission suspended (TS `requestAbort`), and the next - // ACP prompt resumes it through its streaming behavior (TS sends - // `followUp` + `queueIfBusy: true` on every prompt, acp-mode.ts). - // The first response streams at a fixed token rate, so the test - // cancels only after its first chunk: the turn is observably running, - // and the cancel cannot land before the worker admitted it. - let (mut client, _socket) = AcpChild::spawn_daemon_attached( +fn acp_daemon_attached_overlapping_prompt_is_refused() { + // A second prompt while the first runs is refused; the running turn + // is untouched and the next prompt after its settle runs. + let mut client = AcpChild::spawn( &["--mode", "acp", "--no-session"], &json!({ "engine": "faux", "tokensPerSecond": 2, "responses": [ - { "text": "a paced answer that streams slowly enough to cancel mid-turn" }, + { "text": "a paced answer that streams slowly enough to overlap a prompt" }, "SECOND-OK", ] }), ); @@ -835,71 +942,55 @@ fn acp_daemon_attached_prompt_after_cancel_runs() { &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a slow question" }] }), ); // Readiness is the turn's own first streamed chunk, not a timer. - let deadline = Instant::now() + TIMEOUT; - loop { - let timeout_left = deadline.saturating_duration_since(Instant::now()); - assert!( - !timeout_left.is_zero(), - "the paced turn never streamed a chunk" - ); - let line = client - .lines - .recv_timeout(timeout_left) - .expect("the ACP stream stayed open"); - let frame: Value = serde_json::from_str(&line).expect("valid JSON line"); - if frame["params"]["update"]["sessionUpdate"] == "agent_message_chunk" { - break; - } - } - client.notify("session/cancel", &json!({ "sessionId": session_id })); + client.wait_update("agent_message_chunk", TIMEOUT); + let second = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "the overlap" }] }), + ); + let (second_response, _) = client.wait_response(second, TIMEOUT); + assert_eq!( + second_response["error"]["code"], -32603, + "the overlapping prompt is refused: {second_response}" + ); + assert!( + second_response["error"]["data"]["details"] + .as_str() + .unwrap_or_default() + .contains("A prompt turn is already running for this ACP session"), + "the refusal names the running-turn rule: {second_response}" + ); let (first_response, _) = client.wait_response(first, TIMEOUT); assert_eq!( - first_response["result"]["stopReason"], "cancelled", - "the mid-turn cancel settles the first prompt: {first_response}" + first_response["result"]["stopReason"], "end_turn", + "the refused overlap never touched the running turn: {first_response}" ); - let second = client.request( + let third = client.request( "session/prompt", - &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "the resend" }] }), + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "after the settle" }] }), ); - let (second_response, updates) = client.wait_response(second, TIMEOUT); + let (third_response, updates) = client.wait_response(third, TIMEOUT); assert_eq!( - second_response["result"]["stopReason"], "end_turn", - "the prompt after a cancel runs: {second_response}" + third_response["result"]["stopReason"], "end_turn", + "the turn slot frees at the settle: {third_response}" ); let chunk = updates .iter() .find(|update| update["params"]["update"]["sessionUpdate"] == "agent_message_chunk") - .expect("the resumed turn streams its scripted answer"); + .expect("the next turn streams its scripted answer"); assert_eq!( chunk["params"]["update"]["content"], json!({ "type": "text", "text": "SECOND-OK" }) ); } -/// Stop the sandboxed supervisor a test spawned (the shared-daemon -/// product behavior leaves it running; a test owns its sandbox). -fn shutdown_sandboxed_daemon(socket: &std::path::Path) { - use std::io::Write as _; - let Ok(mut stream) = pa_types::platform::transport::connect_blocking(socket) else { - return; - }; - let frame = format!( - "{{\"type\":\"command\",\"id\":\"shutdown-test\",\"protocol\":{{\"name\":\"prime-agent.daemon\",\"version\":{}}},\"command\":{{\"type\":\"shutdown\"}}}}\n", - pa_types::daemon::DAEMON_PROTOCOL_VERSION - ); - let _ = stream.write_all(frame.as_bytes()); - let _ = stream.flush(); - // The supervisor exits after the shutdown response. - std::thread::sleep(Duration::from_millis(300)); -} - #[test] -fn acp_compact_command_publishes_the_compaction_meta_and_end_turn() { - // The faux session is short, so `/compact` skips (TS - // `CompactionSkippedError`): the observable parity is the - // `compaction: {}` namespaced update and the normal end_turn response. - let script = json!({ "responses": ["one answer"] }); - let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); +fn acp_daemon_attached_close_mid_turn_answers_cancelled() { + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "tokensPerSecond": 2, "responses": [ + { "text": "a paced answer that streams slowly enough to close mid-turn" }, + ] }), + ); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); let new = client.request("session/new", &json!({ "mcpServers": [] })); @@ -908,110 +999,676 @@ fn acp_compact_command_publishes_the_compaction_meta_and_end_turn() { .as_str() .unwrap() .to_string(); - let prompt = client.request( "session/prompt", - &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "/compact" }] }), - ); - let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); - - let compaction = updates - .iter() - .find(|update| { - let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; - meta.get("compaction").is_some_and(|value| !value.is_null()) - }) - .expect("a compaction meta frame"); - let meta = &compaction["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; - assert_eq!( - meta["compaction"], - json!({}), - "a skipped compaction publishes the empty payload" + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a slow question" }] }), ); - assert_eq!(meta["phase"], "event"); - - // The turn settles normally: boundary, completion, terminal, end_turn. - let boundary = updates.iter().any(|update| { - let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; - meta["phase"] == "responseBoundary" && meta["terminalQuiescenceExpected"] == true - }); - assert!(boundary, "updates: {updates:?}"); + client.wait_update("agent_message_chunk", TIMEOUT); + let close = client.request("session/close", &json!({ "sessionId": session_id })); + let (prompt_response, frames) = client.wait_response(prompt, TIMEOUT); assert_eq!( - prompt_response["result"], - json!({ "stopReason": "end_turn" }) + prompt_response["result"]["stopReason"], "cancelled", + "a prompt closed mid-turn answers cancelled: {prompt_response}" ); + let close_response = frames + .into_iter() + .find(|frame| frame["id"] == close) + .unwrap_or_else(|| client.wait_response(close, TIMEOUT).0); + assert_eq!(close_response["result"], json!({})); } -#[test] -fn acp_goal_command_publishes_goal_meta_and_runs_the_continuation() { - // `/goal` start schedules its continuation as the turn's model segment: - // the goal meta frame precedes the streamed answer, and the usage - // accounting publishes a second goal frame after the message settles. - // The tiny budget bounds the goal loop the direct-ACP settle loop now - // hosts (TS parity: the continuation loop runs inside the same - // session/prompt request): the crossing turn's budget-limit steer is - // the second model segment, and the budget_limited goal settles the - // prompt with end_turn instead of looping forever. - let script = json!({ "responses": ["GOAL-PROGRESS", "WRAP-UP"] }); - let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); +/// `initialize` + `session/new` on a daemon-attached child; the ACP session id. +fn initialize_and_new_session(client: &mut AcpChild) -> String { let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); + new_session(client) +} + +fn new_session(client: &mut AcpChild) -> String { let new = client.request("session/new", &json!({ "mcpServers": [] })); let (new_response, _) = client.wait_response(new, TIMEOUT); - let session_id = new_response["result"]["sessionId"] + new_response["result"]["sessionId"] .as_str() - .unwrap() - .to_string(); + .unwrap_or_else(|| panic!("session/new succeeds: {new_response}")) + .to_string() +} +fn assert_prompt_ends_turn(client: &mut AcpChild, session_id: &str, text: &str) -> Vec { let prompt = client.request( "session/prompt", - &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "/goal --budget 5 reply with exactly: GOAL-PROGRESS" }] }), + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": text }] }), ); - let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); - - let goal_frames: Vec<&Value> = updates - .iter() - .filter(|update| { - let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; - meta.get("goal").is_some_and(|value| !value.is_null()) - }) - .collect(); - assert!(!goal_frames.is_empty(), "updates: {updates:?}"); - let first = - &goal_frames[0]["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]["goal"]; - assert_eq!(first["status"], "active"); - assert_eq!(first["objective"], "reply with exactly: GOAL-PROGRESS"); - assert_eq!(first["tokensUsed"], 0); - // A usage update follows the settled message: the tiny budget - // crosses at the first turn, so the goal is budget_limited before - // the wrap-up steer segment runs. - assert!(goal_frames.len() >= 2, "goal frames: {goal_frames:?}"); - let second = - &goal_frames[1]["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]["goal"]; - assert_eq!(second["status"], "budget_limited"); - assert!(second["tokensUsed"].as_u64().unwrap_or(0) > 0); - // The budget-limit wrap-up steer ran as the prompt's second model - // segment (its streamed answer is the second scripted response; the - // faux pacing may split one answer into chunks, so the joined text - // carries the observable contract). - let streamed: String = updates - .iter() - .filter_map(|update| update["params"]["update"]["content"]["text"].as_str()) - .collect(); - assert!(streamed.contains("GOAL-PROGRESS"), "updates: {updates:?}"); - assert!(streamed.contains("WRAP-UP"), "updates: {updates:?}"); + let (prompt_response, updates) = client.wait_response(prompt, Duration::from_mins(2)); assert_eq!( - prompt_response["result"], - json!({ "stopReason": "end_turn" }) + prompt_response["result"]["stopReason"], "end_turn", + "{prompt_response}" ); + updates +} + +/// The supervisor's live sessions (`list`). +fn live_sessions(socket: &std::path::Path) -> Vec { + let list = daemon_request( + socket, + "live-sessions", + &json!({ "type": "list", "includeClientOwned": true }), + ); + list["data"]["sessions"] + .as_array() + .unwrap_or_else(|| panic!("a session list: {list}")) + .clone() +} + +fn probe_acp_mcp_servers(socket: &std::path::Path, active_session_id: &str) -> Value { + daemon_request( + socket, + "acp-mcp-replace", + &json!({ + "type": "replace_acp_mcp_servers", + "activeSessionId": active_session_id, + "ownerId": "acp-e2e-probe", + "servers": [{ + "type": "http", + "name": "probe", + "url": "https://mcp.invalid/probe", + "headers": {}, + }], + }), + ) +} + +fn wait_live_session(socket: &std::path::Path) -> Value { + let deadline = Instant::now() + TIMEOUT; + loop { + let sessions = live_sessions(socket); + if let Some(session) = sessions.first() { + return session.clone(); + } + let timeout_left = deadline.saturating_duration_since(Instant::now()); + assert!( + !timeout_left.is_zero(), + "no daemon session ever registered on {}", + socket.display() + ); + std::thread::sleep(Duration::from_millis(50)); + } } #[test] -fn acp_autonomous_token_limit_maps_to_max_tokens_stop_reason() { +fn acp_daemon_attached_default_session_persists_and_resumes() { + // Without --no-session the daemon session is saved and resident: it + // survives the client's EOF, and --resume binds the same live worker. + let home = tempfile::TempDir::new().unwrap(); + let home_path = home.path().to_path_buf(); + let socket = home_path.join("daemon.sock"); + std::fs::write( + home_path.join("worker-script.json"), + json!({ "engine": "faux", "responses": ["The Nile.", "Everest."] }).to_string(), + ) + .unwrap(); + let child = daemon_attached_command(&home_path, &socket, &["--mode", "acp"]) + .env("DO_NOT_TRACK", "1") + .spawn() + .expect("binary present"); + let mut first = AcpChild::wrap(child, Some(home), socket.clone()); + let session_id = initialize_and_new_session(&mut first); + assert_prompt_ends_turn(&mut first, &session_id, "Name a river."); + + let sessions = live_sessions(&socket); + assert_eq!(sessions.len(), 1, "{sessions:?}"); + let active_session_id = sessions[0]["activeSessionId"].clone(); + let session_file = sessions[0]["sessionFile"] + .as_str() + .unwrap_or_else(|| panic!("a saved session: {sessions:?}")) + .to_string(); + assert!( + std::path::Path::new(&session_file) + .parent() + .is_some_and(|dir| dir.ends_with("agent/sessions")), + "the session is saved in the session dir: {session_file}" + ); + assert!(std::fs::read_to_string(&session_file) + .unwrap() + .contains("Name a river.")); + let descriptor = worker_descriptor(&home_path); + assert_eq!(descriptor["telemetryDisabled"], json!(true), "{descriptor}"); + assert!( + descriptor.get("ownerClientId").is_none(), + "a resident session has no owner: {descriptor}" + ); + + first.close_stdin(); + assert!(first.child.wait().expect("the ACP child exits").success()); + let sessions = live_sessions(&socket); + assert_eq!( + sessions.len(), + 1, + "a resident session survives EOF: {sessions:?}" + ); + assert_eq!(sessions[0]["activeSessionId"], active_session_id); + + // A relative `--resume` selector resolves against the CLI's working + // directory: the agent dir here, not the stored session cwd. + let file_name = std::path::Path::new(&session_file) + .file_name() + .and_then(std::ffi::OsStr::to_str) + .expect("a session file name"); + let agent_dir = std::path::Path::new(&session_file) + .parent() + .and_then(std::path::Path::parent) + .expect("the agent dir holding the session dir"); + let child = daemon_attached_command( + &home_path, + &socket, + &[ + "--mode", + "acp", + "--resume", + &format!("sessions/{file_name}"), + ], + ) + .current_dir(agent_dir) + .spawn() + .expect("binary present"); + let mut second = AcpChild::wrap(child, None, socket.clone()); + let session_id = initialize_and_new_session(&mut second); + assert_prompt_ends_turn(&mut second, &session_id, "Name a mountain."); + let sessions = live_sessions(&socket); + assert_eq!(sessions.len(), 1, "{sessions:?}"); + assert_eq!( + sessions[0]["activeSessionId"], active_session_id, + "--resume binds the live worker" + ); + let saved = std::fs::read_to_string(&session_file).unwrap(); + assert!(saved.contains("Name a river.") && saved.contains("Name a mountain.")); +} + +#[test] +fn acp_daemon_attached_resident_eof_mid_turn_cancels_the_prompt() { + let answer = "a paced answer that streams slowly enough to close mid-turn"; + let mut client = AcpChild::spawn( + &["--mode", "acp"], + &json!({ "engine": "faux", "tokensPerSecond": 2, "responses": [{ "text": answer }] }), + ); + let socket = client.socket.clone(); + let session_id = initialize_and_new_session(&mut client); + let _ = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a slow question" }] }), + ); + client.wait_update("agent_message_chunk", TIMEOUT); + let session = live_sessions(&socket).remove(0); + let active_session_id = session["activeSessionId"].clone(); + let session_file = session["sessionFile"] + .as_str() + .unwrap_or_else(|| panic!("a saved session: {session}")) + .to_string(); + + client.close_stdin(); + assert!(client.child.wait().expect("the ACP child exits").success()); + let idle = daemon_request( + &socket, + "idle", + &json!({ "type": "wait_for_idle", "activeSessionId": active_session_id }), + ); + assert_eq!(idle["success"], true, "{idle}"); + let sessions = live_sessions(&socket); + assert_eq!( + sessions.len(), + 1, + "a resident session survives EOF: {sessions:?}" + ); + assert_eq!(sessions[0]["activeSessionId"], active_session_id); + let saved = std::fs::read_to_string(&session_file).unwrap(); + assert!(saved.contains("a slow question"), "{saved}"); + assert!( + !saved.contains(answer), + "the EOF cancelled the running prompt: {saved}" + ); +} + +/// EOF while `session/new` is still running: the reader awaits the +/// handler before it reads further, so the teardown at EOF releases the +/// session it installs and the process exits. +#[test] +fn acp_daemon_attached_eof_during_session_new_exits() { + let mut client = AcpChild::spawn( + &["--mode", "acp"], + &json!({ "engine": "faux", "responses": ["The Nile."] }), + ); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + client.close_stdin(); + let (new_response, _) = client.wait_response(new, TIMEOUT); + assert!( + new_response["result"]["sessionId"].is_string(), + "session/new answers: {new_response}" + ); + assert!(client.child.wait().expect("the ACP child exits").success()); +} + +#[test] +fn acp_daemon_attached_close_keeps_the_worker_and_new_rebinds_it() { + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "responses": ["The Nile.", "Everest."] }), + ); + let socket = client.socket.clone(); + let first = initialize_and_new_session(&mut client); + let wrong = client.request("session/close", &json!({ "sessionId": "not-the-session" })); + let (wrong_response, _) = client.wait_response(wrong, TIMEOUT); + assert_eq!( + wrong_response["error"]["data"]["details"], "Unknown ACP session: not-the-session", + "{wrong_response}" + ); + assert_prompt_ends_turn(&mut client, &first, "Name a river."); + let active_session_id = live_sessions(&socket)[0]["activeSessionId"].clone(); + let descriptor = worker_descriptor(socket.parent().unwrap()); + let owner = json!(format!("acp:{}", client.child.id())); + assert_eq!(descriptor["ownerClientId"], owner, "{descriptor}"); + + let close = client.request("session/close", &json!({ "sessionId": first })); + let (close_response, _) = client.wait_response(close, TIMEOUT); + assert_eq!(close_response["result"], json!({})); + let sessions = live_sessions(&socket); + assert_eq!(sessions.len(), 1, "close keeps the worker: {sessions:?}"); + assert_eq!(sessions[0]["activeSessionId"], active_session_id); + + let second = new_session(&mut client); + assert_ne!(second, first); + let updates = assert_prompt_ends_turn(&mut client, &second, "Name a mountain."); + let chunk = updates + .iter() + .find(|update| update["params"]["update"]["sessionUpdate"] == "agent_message_chunk") + .expect("a message chunk"); + // The event mapping restarts with the session, like TS. + assert_eq!( + chunk["params"]["update"]["messageId"], + "prime-agent-assistant-1" + ); + let sessions = live_sessions(&socket); + assert_eq!(sessions.len(), 1, "{sessions:?}"); + assert_eq!( + sessions[0]["activeSessionId"], active_session_id, + "session/new binds the same daemon session" + ); +} + +#[test] +fn acp_daemon_attached_prompt_during_cancel_is_refused() { + // A prompt behind a cancel is refused while the stop runs, and the + // cancelled response comes after the stop, so a resend right after it + // runs. + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "tokensPerSecond": 2, "responses": [ + { "text": "a paced answer that streams slowly enough to cancel mid-turn" }, + "AFTER-STOP", + ] }), + ); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + let first = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a slow question" }] }), + ); + client.wait_update("agent_message_chunk", TIMEOUT); + client.notify("session/cancel", &json!({ "sessionId": session_id })); + let second = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "behind the stop" }] }), + ); + let (second_response, _) = client.wait_response(second, TIMEOUT); + assert_eq!( + second_response["error"]["code"], -32603, + "the prompt behind the cancel is refused: {second_response}" + ); + assert_eq!( + second_response["error"]["data"]["details"], + format!("ACP session is cancelling: {session_id}"), + "the refusal names the cancelling window: {second_response}" + ); + let (first_response, _) = client.wait_response(first, TIMEOUT); + assert_eq!( + first_response["result"]["stopReason"], "cancelled", + "the cancel settles the running turn: {first_response}" + ); + // The resend also resumes the queued-input admission the cancel + // suspended (TS sends `followUp` + `queueIfBusy: true`). + let third = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "the resend" }] }), + ); + let (third_response, updates) = client.wait_response(third, TIMEOUT); + assert_eq!( + third_response["result"]["stopReason"], "end_turn", + "the stop cleared before the cancelled response: {third_response}" + ); + let chunk = updates + .iter() + .find(|update| update["params"]["update"]["sessionUpdate"] == "agent_message_chunk") + .expect("the resent turn streams its scripted answer"); + assert_eq!( + chunk["params"]["update"]["content"], + json!({ "type": "text", "text": "AFTER-STOP" }) + ); +} + +#[test] +fn acp_daemon_attached_failed_turn_publishes_only_the_error_boundary() { + // A failed turn (here: the empty-prompt rejection) publishes one + // error boundary and no terminal-quiescence update (TS settle catch). + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "responses": [] }), + ); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [] }), + ); + let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); + assert_eq!( + prompt_response["error"]["code"], -32603, + "the failed turn errors the request: {prompt_response}" + ); + assert!( + prompt_response["error"]["data"]["details"] + .as_str() + .unwrap_or_default() + .contains("Prompt cannot be empty"), + "the worker's failure is the error: {prompt_response}" + ); + let mut boundaries = 0; + for update in &updates { + let body = &update["params"]["update"]; + let meta = &body["_meta"]["ai.primeintellect.prime-agent"]; + assert_ne!( + meta["phase"], "terminalQuiescence", + "a failed turn never publishes a terminal frame: {updates:?}" + ); + if meta["phase"] == "responseBoundary" { + boundaries += 1; + assert_eq!( + meta["terminalQuiescenceExpected"], false, + "the one boundary declares no terminal expectation: {updates:?}" + ); + assert_eq!(meta["outcome"], "error"); + } + } + assert_eq!( + boundaries, 1, + "the failed turn published exactly one correlated error boundary: {updates:?}" + ); +} + +#[cfg(unix)] +#[test] +fn acp_daemon_attached_supervisor_loss_fails_the_prompt() { + // The supervisor dies while a prompt is in flight: the pending + // request must fail fast with an error response, never hang, because + // the link close is the turn's liveness bound (turn-long requests + // carry no timer). + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "tokensPerSecond": 2, "responses": [ + { "text": "a paced answer that streams slowly enough to outlive the supervisor" }, + ] }), + ); + let socket = client.socket.clone(); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "a slow question" }] }), + ); + // Readiness is the turn's own first streamed chunk: the prompt is + // provably in flight before the supervisor goes away. + client.wait_update("agent_message_chunk", TIMEOUT); + // Crash the supervisor (no graceful drain, so the in-flight response + // can never arrive). Its pid comes from the hello frame every new + // connection receives. + let probe = pa_types::platform::transport::connect_blocking(&socket).expect("daemon socket"); + let reader = probe.try_clone_box().expect("daemon socket clone"); + let _ = reader.set_read_timeout(Duration::from_mins(2)); + let hello = BufReader::new(reader) + .lines() + .map(|line| { + serde_json::from_str::(&line.expect("daemon line")).expect("daemon JSON") + }) + .find(|frame| frame["type"] == json!("daemon_hello")) + .expect("the daemon closed without a hello"); + let pid = hello["supervisorPid"].as_u64().expect("supervisor pid"); + drop(probe); + let killed = std::process::Command::new("kill") + .args(["-9", &pid.to_string()]) + .status() + .expect("kill the supervisor"); + assert!(killed.success(), "the supervisor crash did not run"); + let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); + assert_eq!( + prompt_response["error"], + json!({ + "code": -32603, + "message": "Internal error", + "data": { "details": "the daemon connection closed mid-request" } + }), + "the in-flight prompt fails fast on supervisor loss: {prompt_response}" + ); +} + +#[test] +fn acp_daemon_attached_image_only_prompt_reaches_the_session() { + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "responses": ["SAW-IMAGE"] }), + ); + let socket = client.socket.clone(); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + // 1x1 PNG + let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; + let prompt = client.request( + "session/prompt", + &json!({ + "sessionId": session_id, + "prompt": [ + { "type": "image", "data": png, "mimeType": "image/png" }, + ], + }), + ); + let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); + assert_eq!( + prompt_response["result"]["stopReason"], "end_turn", + "an image-only prompt runs a turn: {prompt_response}" + ); + // The stored user row carries the image: an empty text block first, + // then the images (TS `_buildPromptContent`). + let list = daemon_request(&socket, "img-list", &json!({ "type": "list" })); + let active_session_id = &list["data"]["sessions"][0]["activeSessionId"]; + let messages = daemon_request( + &socket, + "img-msgs", + &json!({ "type": "get_messages", "activeSessionId": active_session_id }), + ); + let user = messages["data"]["messages"] + .as_array() + .unwrap() + .iter() + .find(|m| m["role"] == "user") + .unwrap(); + assert_eq!( + user["content"], + json!([ + { "type": "text", "text": "" }, + { "type": "image", "data": png, "mimeType": "image/png" }, + ]) + ); +} + +/// Stop the sandboxed supervisor a test spawned (the shared-daemon +/// product behavior leaves it running; a test owns its sandbox). +fn shutdown_sandboxed_daemon(socket: &std::path::Path) { + use std::io::Write as _; + let Ok(mut stream) = pa_types::platform::transport::connect_blocking(socket) else { + return; + }; + let frame = format!( + "{{\"type\":\"command\",\"id\":\"shutdown-test\",\"protocol\":{{\"name\":\"prime-agent.daemon\",\"version\":{}}},\"command\":{{\"type\":\"shutdown\"}}}}\n", + pa_types::daemon::DAEMON_PROTOCOL_VERSION + ); + let _ = stream.write_all(frame.as_bytes()); + let _ = stream.flush(); + // The supervisor exits after the shutdown response. + std::thread::sleep(Duration::from_millis(300)); +} + +#[test] +fn acp_compact_command_publishes_the_compaction_meta_and_end_turn() { + // The faux session is short, so `/compact` skips (TS + // `CompactionSkippedError`): the observable parity is the + // `compaction: {}` namespaced update and the normal end_turn response. + let script = json!({ "engine": "faux", "responses": ["one answer"] }); + let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "/compact" }] }), + ); + let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); + + let compaction = updates + .iter() + .find(|update| { + let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; + meta.get("compaction").is_some_and(|value| !value.is_null()) + }) + .expect("a compaction meta frame"); + let meta = &compaction["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; + assert_eq!( + meta["compaction"], + json!({}), + "a skipped compaction publishes the empty payload" + ); + assert_eq!(meta["phase"], "event"); + + // The turn settles normally: boundary, completion, terminal, end_turn. + let boundary = updates.iter().any(|update| { + let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; + meta["phase"] == "responseBoundary" && meta["terminalQuiescenceExpected"] == true + }); + assert!(boundary, "updates: {updates:?}"); + assert_eq!( + prompt_response["result"], + json!({ "stopReason": "end_turn" }) + ); +} + +#[test] +fn acp_goal_command_publishes_goal_meta_and_runs_the_continuation() { + // `/goal` start schedules its continuation as the turn's model segment: + // the goal meta frame precedes the streamed answer, and the usage + // accounting publishes a second goal frame after the message settles. + // The tiny budget bounds the goal loop the worker turn loop + // hosts (TS parity: the continuation loop runs inside the same + // session/prompt request): the crossing turn's budget-limit steer is + // the second model segment, and the budget_limited goal settles the + // prompt with end_turn instead of looping forever. + let script = json!({ "engine": "faux", "responses": ["GOAL-PROGRESS", "WRAP-UP"] }); + let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "/goal --budget 5 reply with exactly: GOAL-PROGRESS" }] }), + ); + let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); + + let goal_frames: Vec<&Value> = updates + .iter() + .filter(|update| { + let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]; + meta.get("goal").is_some_and(|value| !value.is_null()) + }) + .collect(); + assert!(!goal_frames.is_empty(), "updates: {updates:?}"); + let first = + &goal_frames[0]["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]["goal"]; + assert_eq!(first["status"], "active"); + assert_eq!(first["objective"], "reply with exactly: GOAL-PROGRESS"); + assert_eq!(first["tokenBudget"], 5); + assert_eq!(first["tokensUsed"], 0); + // A usage update follows the settled message: the tiny budget + // crosses at the first turn, so the goal is budget_limited before + // the wrap-up steer segment runs. + assert!(goal_frames.len() >= 2, "goal frames: {goal_frames:?}"); + let second = + &goal_frames[1]["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]["goal"]; + assert_eq!(second["status"], "budget_limited"); + assert!(second["tokensUsed"].as_u64().unwrap_or(0) > 0); + // The budget-limit wrap-up steer ran as the prompt's second model + // segment (its streamed answer is the second scripted response; the + // faux pacing may split one answer into chunks, so the joined text + // carries the observable contract). + let streamed: String = updates + .iter() + .filter_map(|update| update["params"]["update"]["content"]["text"].as_str()) + .collect(); + assert!(streamed.contains("GOAL-PROGRESS"), "updates: {updates:?}"); + assert!(streamed.contains("WRAP-UP"), "updates: {updates:?}"); + assert_eq!( + prompt_response["result"], + json!({ "stopReason": "end_turn" }) + ); +} + +#[test] +fn acp_autonomous_token_limit_maps_to_max_tokens_stop_reason() { // A one-token budget is exhausted by the first turn: the driver stops // with the token limit, the completion envelope carries the autonomous // accounting, and the stop reason is `max_tokens`. - let script = json!({ "responses": ["an answer"] }); + let script = json!({ "engine": "faux", "responses": ["an answer"] }); let mut client = AcpChild::spawn( &[ "--mode", @@ -1063,7 +1720,7 @@ fn acp_autonomous_token_limit_maps_to_max_tokens_stop_reason() { fn acp_autonomous_disabled_reports_end_turn_without_accounting() { // Without autonomous flags the completion envelope carries no autonomous // meta and the stop reason is end_turn. - let script = json!({ "responses": ["an answer"] }); + let script = json!({ "engine": "faux", "responses": ["an answer"] }); let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); @@ -1093,60 +1750,13 @@ fn acp_autonomous_disabled_reports_end_turn_without_accounting() { ); } -#[test] -fn acp_daemon_attached_publishes_the_goal_update_meta() { - // The daemon worker executes `/goal` and emits the `goal_update` - // session event; the daemon-attached ACP surface maps it to the - // namespaced `_meta.goal` update (TS acp-events.ts case "goal_update"). - // A goal start schedules its continuation as the turn's model segment, - // so the faux engine needs one response. - let (mut client, _socket) = AcpChild::spawn_daemon_attached( - &["--mode", "acp", "--no-session"], - &json!({ "engine": "faux", "responses": ["the goal turn settled"] }), - ); - let init = client.request("initialize", &initialize_params()); - let _ = client.wait_response(init, TIMEOUT); - let new = client.request("session/new", &json!({ "mcpServers": [] })); - let (new_response, _) = client.wait_response(new, Duration::from_mins(1)); - assert!( - new_response["result"]["sessionId"].is_string(), - "daemon-attached admission succeeds: {new_response}" - ); - let session_id = new_response["result"]["sessionId"] - .as_str() - .unwrap() - .to_string(); - let prompt = client.request( - "session/prompt", - &json!({ - "sessionId": session_id, - "prompt": [{ "type": "text", "text": "/goal --budget 500 make the daemon publish goal state" }], - }), - ); - let (prompt_response, updates) = client.wait_response(prompt, Duration::from_mins(2)); - assert_eq!(prompt_response["result"]["stopReason"], "end_turn"); - let goal = updates.iter().find_map(|update| { - let meta = &update["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"]["goal"]; - (!meta.is_null()).then(|| meta.clone()) - }); - let goal = goal.expect("a _meta.goal update reached the ACP surface"); - assert_eq!(goal["status"], "active"); - assert_eq!(goal["objective"], "make the daemon publish goal state"); - assert_eq!(goal["tokenBudget"], 500); - assert_eq!(goal["tokensUsed"], 0); - let close = client.request("session/close", &json!({ "sessionId": session_id })); - let (close_response, _) = client.wait_response(close, Duration::from_mins(1)); - assert_eq!(close_response["result"], json!({})); - drop(client); -} - #[test] fn acp_daemon_attached_reports_autonomous_accounting_and_limit_stop_reason() { // An autonomous run with --max-turns 1: the completion envelope carries // the _meta.autonomous accounting (TS waitForHeadlessCompletion), the // quiescence observation counts the remaining continuations, and the // turn limit surfaces as max_turn_requests (TS acpStopReason). - let (mut client, _socket) = AcpChild::spawn_daemon_attached( + let mut client = AcpChild::spawn( &["--mode", "acp", "--no-session"], &json!({ "engine": "faux", "responses": [ "enabling the run", @@ -1260,7 +1870,7 @@ fn acp_daemon_attached_forwards_cli_session_options() { ) .unwrap(); let skill = skill_dir.to_str().unwrap().to_string(); - let (mut client, socket) = AcpChild::spawn_daemon_attached( + let mut client = AcpChild::spawn( &[ "--mode", "acp", @@ -1275,6 +1885,7 @@ fn acp_daemon_attached_forwards_cli_session_options() { ], &json!({ "engine": "faux", "responses": ["one turn, then the limit stops"] }), ); + let socket = client.socket.clone(); let init = client.request("initialize", &initialize_params()); let _ = client.wait_response(init, TIMEOUT); let new = client.request("session/new", &json!({ "mcpServers": [] })); @@ -1306,8 +1917,8 @@ fn acp_daemon_attached_forwards_cli_session_options() { drop(client); } -/// Spawn with compaction settings written into the agent dir (the -/// in-process session resolves them at session assembly). +/// Spawn with compaction settings written into the worker's agent dir +/// (`/.prime/agent`: the worker inherits the child's HOME). fn spawn_with_compaction_settings( args: &[&str], script: &serde_json::Value, @@ -1315,10 +1926,9 @@ fn spawn_with_compaction_settings( keep_recent_tokens: u64, ) -> AcpChild { let home = tempfile::TempDir::new().unwrap(); - let agent_dir = home.path().join("agent"); - std::fs::create_dir_all(&agent_dir).expect("agent dir"); + std::fs::create_dir_all(home.path().join(".prime/agent")).expect("agent dir"); std::fs::write( - agent_dir.join("settings.json"), + home.path().join(".prime/agent/settings.json"), json!({ "compaction": { "enabled": true, @@ -1329,19 +1939,12 @@ fn spawn_with_compaction_settings( .to_string(), ) .expect("write settings.json"); - let bin = env!("CARGO_BIN_EXE_prime-agent"); - let child = Command::new(bin) - .args(args) - .env("HOME", home.path()) - .env("PRIME_AGENT_CODING_AGENT_DIR", &agent_dir) - .env("PRIME_AGENT_FAUX_SCRIPT", script.to_string()) - .current_dir(home.path()) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) + let socket = home.path().join("daemon.sock"); + std::fs::write(home.path().join("worker-script.json"), script.to_string()).unwrap(); + let child = daemon_attached_command(home.path(), &socket, args) .spawn() .expect("binary present"); - AcpChild::wrap(child, home) + AcpChild::wrap(child, Some(home), socket) } /// The compaction metas among a turn's updates (the ACP `compaction_end` @@ -1371,7 +1974,9 @@ fn compaction_metas(updates: &[Value]) -> Vec { #[test] fn acp_threshold_auto_compaction_publishes_the_compaction_meta() { let script = json!({ + "engine": "faux", "contextWindow": 128_000, + "maxTokens": 4_096, "responses": [ { "text": "turn one reply" }, { "text": "turn two reply" }, @@ -1403,7 +2008,7 @@ fn acp_threshold_auto_compaction_publishes_the_compaction_meta() { }), ); let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); - assert_eq!(prompt_response["result"]["stopReason"], "end_turn"); + assert_end_turn(&prompt_response); let metas = compaction_metas(&updates); assert!(!metas.is_empty(), "the threshold arm ran: {updates:?}"); assert!( @@ -1423,7 +2028,7 @@ fn acp_threshold_auto_compaction_publishes_the_compaction_meta() { }), ); let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); - assert_eq!(prompt_response["result"]["stopReason"], "end_turn"); + assert_end_turn(&prompt_response); let metas = compaction_metas(&updates); let ran = metas .iter() @@ -1436,6 +2041,45 @@ fn acp_threshold_auto_compaction_publishes_the_compaction_meta() { assert!(ran["tokensBefore"].as_u64().unwrap() > 0); } +/// A settled turn's response implies the next prompt is admissible: the +/// turn releases the session's single-prompt slot before its reply +/// leaves, so a client that prompts again the instant it reads the +/// response is admitted. Twenty back-to-back prompts keep every +/// settle-to-next-admission window covered. +#[test] +fn acp_settled_prompt_immediately_admits_the_next_prompt() { + let script = json!({ + "engine": "faux", + "contextWindow": 128_000, + "responses": (0..30) + .map(|index| json!({ "text": format!("settled turn reply {index}") })) + .collect::>(), + }); + let mut client = AcpChild::spawn(&["--mode", "acp", "--no-session"], &script); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let new = client.request("session/new", &json!({ "mcpServers": [] })); + let (new_response, _) = client.wait_response(new, TIMEOUT); + let session_id = new_response["result"]["sessionId"] + .as_str() + .unwrap() + .to_string(); + for turn in 0..20 { + let prompt = client.request( + "session/prompt", + &json!({ + "sessionId": session_id, + "prompt": [{ "type": "text", "text": format!("settled turn {turn}") }], + }), + ); + let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); + assert_eq!( + prompt_response["result"]["stopReason"], "end_turn", + "turn {turn}: the settled turn raced the next prompt's admission: {prompt_response}" + ); + } +} + /// The overflow arm on the ACP turn path: a provider context-overflow /// error runs one compact-and-retry at the boundary and the retried turn /// settles the prompt with `end_turn` instead of the error (TS @@ -1443,6 +2087,7 @@ fn acp_threshold_auto_compaction_publishes_the_compaction_meta() { #[test] fn acp_overflow_recovery_compacts_and_retries_the_turn() { let script = json!({ + "engine": "faux", "contextWindow": 128_000, "responses": [ { "text": "seed reply" }, @@ -1483,37 +2128,16 @@ fn acp_overflow_recovery_compacts_and_retries_the_turn() { // The overflow probe: the arm compacts once (the summarizer consumed // the third scripted response) and the retried turn recovers. The - // seed turn's drain can outlive its response on a loaded runner (the - // overflow retry then bounces off the still-running prompt guard) — - // the probe is re-issued until the session settles (the recovered - // turn's assertion itself is unchanged and strict). - let mut probe_attempts = 0; - let (prompt_response, updates) = loop { - probe_attempts += 1; - let prompt = client.request( - "session/prompt", - &json!({ - "sessionId": session_id, - "prompt": [{ "type": "text", "text": format!("overflow probe {}", "x".repeat(2_000)) }], - }), - ); - let (response, prompt_updates) = client.wait_response(prompt, TIMEOUT); - // ACP `internal_error` carries the refusal text in `data.details` - // (`Internal error` is the generic message) — match both fields. - let refusal_text = response["error"]["data"]["details"] - .as_str() - .or_else(|| response["error"]["message"].as_str()) - .unwrap_or_default(); - let refused = response["error"].is_object() && refusal_text.contains("already running"); - if !refused { - break (response, prompt_updates); - } - assert!( - probe_attempts < 40, - "the session never settled after the seed turn: {response}" - ); - std::thread::sleep(std::time::Duration::from_millis(250)); - }; + // daemon path releases the prompt slot before it answers the seed + // turn, so the probe cannot bounce off the running-turn guard. + let prompt = client.request( + "session/prompt", + &json!({ + "sessionId": session_id, + "prompt": [{ "type": "text", "text": format!("overflow probe {}", "x".repeat(2_000)) }], + }), + ); + let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); assert_eq!( prompt_response["result"], json!({ "stopReason": "end_turn" }), @@ -1524,3 +2148,419 @@ fn acp_overflow_recovery_compacts_and_retries_the_turn() { assert_eq!(metas[0]["summary"], "the summary"); assert!(metas[0]["tokensBefore"].as_u64().unwrap() > 0); } + +/// The kernel Python with prime-agent-runtime installed; set +/// `PA_E2E_KERNEL_PYTHON` to point at an explicit interpreter instead. +/// Without one, the live RLM quiescence lanes below skip (with a note). +fn kernel_python() -> Option { + if let Some(explicit) = std::env::var_os("PA_E2E_KERNEL_PYTHON") { + let explicit = std::path::PathBuf::from(explicit); + assert!( + explicit.exists(), + "PA_E2E_KERNEL_PYTHON {} not found", + explicit.display() + ); + return Some(explicit); + } + let candidate = std::path::PathBuf::from(std::env::var("HOME").map_or_else( + |_| "/home/ubuntu/.prime/agent/kernel-venv/bin/python".to_string(), + |home| format!("{home}/.prime/agent/kernel-venv/bin/python"), + )); + if candidate.exists() { + return Some(candidate); + } + eprintln!( + "kernel python {} not found; skipping live RLM quiescence e2e", + candidate.display() + ); + None +} + +/// The namespaced prime-agent payload of one session/update frame. +fn update_meta(frame: &Value) -> &Value { + &frame["params"]["update"]["_meta"]["ai.primeintellect.prime-agent"] +} + +/// The kernel cell of the spawn turn (the worker's +/// `rlm_quiescence_barrier_e2e` lane): spawn one RLM child through the +/// product `rlm.spawn` surface and record its child id. +fn spawn_cell() -> &'static str { + "handle = await rlm.spawn(\"run the lane task\", name=\"kid\")\nprint(handle.rlm_child_id)" +} + +/// The parent faux script whose turns run the spawn cell; the third +/// response answers the settled child's terminal-notice turn (the no-reply +/// notice the watcher queues on the parent). +fn spawn_parent_script(spawn_cell: &str) -> Value { + json!({ + "engine": "faux", + "responses": [ + { "content": [ + { "type": "toolCall", "name": "ipython", "arguments": { "code": spawn_cell } }, + ] }, + { "text": "spawn turn done" }, + { "text": "notice seen" }, + ], + }) +} + +/// One scripted-children lane: a daemon-attached resident session whose +/// parent turns spawn a held scripted child (resident because the RLM spawn +/// ledger needs the parent's session file — a `--no-session` parent cannot +/// spawn children). The parent's active session id is read at setup, before +/// the spawn: once a child runs, `live_sessions` lists both workers. +fn spawn_lane( + args: &[&str], + child_hold_ms: u64, + kernel_python: &std::path::Path, +) -> (AcpChild, String, String) { + let parent = spawn_parent_script(spawn_cell()); + let child_script = json!({ "responses": [ { "text": "kid done", "delayMs": child_hold_ms } ] }); + let mut client = AcpChild::spawn_with_child_script(args, &parent, &child_script, kernel_python); + let init = client.request("initialize", &initialize_params()); + let _ = client.wait_response(init, TIMEOUT); + let session_id = new_session(&mut client); + let socket = client.socket.clone(); + let active_session_id = live_sessions(&socket).remove(0)["activeSessionId"] + .as_str() + .expect("the parent session id") + .to_string(); + (client, session_id, active_session_id) +} + +/// The ACP settle waits for RLM quiescence (TS #1612): the completion +/// update reports the live outstanding-subagent count, the terminal update +/// reports zero, and the settled child's terminal-notice turn ("notice +/// seen") drains inside the barrier, between the two. +#[test] +fn acp_prompt_settles_after_rlm_quiescence() { + let Some(kernel_python) = kernel_python() else { + return; + }; + let (mut client, session_id, _) = spawn_lane(&["--mode", "acp"], 5_000, &kernel_python); + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "spawn the kid" }] }), + ); + // Readiness is the completion update itself: the parent turn settled, + // the held child turn is still in flight. + let completion = client.wait_frame(TIMEOUT, |frame| { + let meta = update_meta(frame); + meta["phase"] == "event" && !meta["quiescence"].is_null() + }); + assert_eq!( + update_meta(&completion)["quiescence"]["outstandingSubagents"], + 1, + "the completion reports the live child: {completion}" + ); + // The settled child's terminal-notice turn drains inside the barrier: + // its streamed answer lands between the completion and the terminal. + let mut notice_seen = false; + let terminal = client.wait_frame(TIMEOUT, |frame| { + let meta = update_meta(frame); + if meta["phase"] != "terminalQuiescence" { + notice_seen |= frame["params"]["update"]["content"]["text"] + .as_str() + .is_some_and(|text| text.contains("notice seen")); + return false; + } + true + }); + assert!( + notice_seen, + "the settled child's notice turn drained inside the barrier: {terminal}" + ); + assert_eq!( + update_meta(&terminal)["quiescence"]["outstandingSubagents"], + 0, + "the terminal reports a quiet family: {terminal}" + ); + let (prompt_response, updates) = client.wait_response(prompt, TIMEOUT); + assert!( + updates.is_empty(), + "the terminal frame is the last notification before the response: {updates:?}" + ); + assert_eq!( + prompt_response["result"], + json!({ "stopReason": "end_turn" }) + ); +} + +/// A cancel during the settle cancels the outstanding subagents (TS +/// `cancelOutstandingRlmChildren` inside `stopSessionWork`): the prompt +/// answers cancelled once the stop sequence cancelled the held child, and +/// the roster reports the child cancelled. Close goes through the same +/// stop sequence, so it is not retested here. +#[test] +fn acp_cancel_during_settle_cancels_the_subagents() { + let Some(kernel_python) = kernel_python() else { + return; + }; + let (mut client, session_id, active_session_id) = + spawn_lane(&["--mode", "acp"], 120_000, &kernel_python); + let socket = client.socket.clone(); + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "spawn the kid" }] }), + ); + // Readiness is the completion update with the held child outstanding: + // the settle is provably waiting on it. + client.wait_frame(TIMEOUT, |frame| { + let meta = update_meta(frame); + meta["phase"] == "event" && meta["quiescence"]["outstandingSubagents"] == 1 + }); + client.notify("session/cancel", &json!({ "sessionId": session_id })); + let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); + assert_eq!( + prompt_response["result"], + json!({ "stopReason": "cancelled" }), + "the cancel settles the prompt: {prompt_response}" + ); + // The roster shows the child the stop sequence cancelled. + let roster = daemon_request( + &socket, + "children", + &json!({ "type": "get_rlm_children", "activeSessionId": active_session_id }), + ); + let children = roster["data"]["children"] + .as_array() + .unwrap_or_else(|| panic!("a children roster: {roster}")) + .clone(); + assert_eq!(children.len(), 1, "one spawned child: {children:?}"); + assert_eq!( + children[0]["status"], "cancelled", + "the stop cancelled the held child: {children:?}" + ); +} + +/// EOF during the settle exits without waiting for the outstanding +/// subagents (TS aborts the controller and exits): the process is gone +/// while the resident session's held child still runs — EOF does not +/// cancel a resident session's children. +#[test] +fn acp_eof_during_settle_exits_and_leaves_resident_subagents() { + let Some(kernel_python) = kernel_python() else { + return; + }; + let (mut client, session_id, active_session_id) = + spawn_lane(&["--mode", "acp"], 120_000, &kernel_python); + let socket = client.socket.clone(); + client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "spawn the kid" }] }), + ); + client.wait_frame(TIMEOUT, |frame| { + let meta = update_meta(frame); + meta["phase"] == "event" && meta["quiescence"]["outstandingSubagents"] == 1 + }); + client.close_stdin(); + assert!(client.child.wait().expect("the ACP child exits").success()); + // The resident session survives with the held child still running. + let roster = daemon_request( + &socket, + "children", + &json!({ "type": "get_rlm_children", "activeSessionId": active_session_id }), + ); + let children = roster["data"]["children"] + .as_array() + .unwrap_or_else(|| panic!("a children roster: {roster}")) + .clone(); + assert_eq!(children.len(), 1, "one spawned child: {children:?}"); + assert_eq!( + children[0]["status"], "running", + "EOF left the resident child running: {children:?}" + ); +} + +/// A heartbeat change on the bound session broadcasts +/// `heartbeats_changed` to every client; the ACP link publishes the +/// change at origin turn 0 (connection-scoped like TS, never the active +/// prompt turn). +#[test] +fn acp_heartbeat_change_publishes_turn_zero_meta() { + let mut client = AcpChild::spawn( + &["--mode", "acp"], + &json!({ "engine": "faux", "responses": ["The Nile."] }), + ); + let socket = client.socket.clone(); + let _session_id = initialize_and_new_session(&mut client); + let active_session_id = live_sessions(&socket)[0]["activeSessionId"].clone(); + let set = daemon_request( + &socket, + "heartbeat-set", + &json!({ + "type": "heartbeat_set", + "activeSessionId": active_session_id, + "schedule": "every 90 seconds", + "prompt": "check in", + }), + ); + assert_eq!(set["success"], true, "{set}"); + let change = client.wait_frame(TIMEOUT, |frame| { + update_meta(frame)["heartbeatsChanged"] == json!(true) + }); + assert_eq!(update_meta(&change)["promptTurnId"], 0, "{change}"); + assert_eq!(update_meta(&change)["phase"], "event", "{change}"); +} + +/// A bash run another client started on the bound session (the daemon's +/// `execute_bash`) surfaces as one synthetic tool call keyed by run id: +/// the started run, the streamed chunk, and the settled status. +#[test] +fn acp_user_bash_maps_to_a_synthetic_tool_call() { + let mut client = AcpChild::spawn( + &["--mode", "acp"], + &json!({ "engine": "faux", "responses": ["The Nile."] }), + ); + let socket = client.socket.clone(); + let _session_id = initialize_and_new_session(&mut client); + let active_session_id = live_sessions(&socket)[0]["activeSessionId"].clone(); + let run = daemon_request( + &socket, + "bash", + &json!({ + "type": "execute_bash", + "activeSessionId": active_session_id, + "command": "printf hi", + "runId": "r1", + }), + ); + assert_eq!(run["success"], true, "{run}"); + let start = client.wait_frame(TIMEOUT, |frame| { + frame["params"]["update"]["sessionUpdate"] == "tool_call" + }); + assert_eq!( + start["params"]["update"]["toolCallId"], + "prime-agent-bash-r1" + ); + assert_eq!(start["params"]["update"]["title"], "printf hi"); + assert_eq!(start["params"]["update"]["kind"], "execute"); + assert_eq!( + start["params"]["update"]["rawInput"], + json!({ "command": "printf hi" }) + ); + let output = client.wait_frame(TIMEOUT, |frame| { + frame["params"]["update"]["sessionUpdate"] == "tool_call_update" + && frame["params"]["update"]["toolCallId"] == "prime-agent-bash-r1" + && frame["params"]["update"]["status"] == "in_progress" + }); + assert_eq!( + output["params"]["update"]["content"][0]["content"]["text"], "hi", + "{output}" + ); + let end = client.wait_frame(TIMEOUT, |frame| { + frame["params"]["update"]["sessionUpdate"] == "tool_call_update" + && frame["params"]["update"]["toolCallId"] == "prime-agent-bash-r1" + && frame["params"]["update"]["status"] == "completed" + }); + assert!(end["params"]["update"].get("content").is_none(), "{end}"); +} + +#[cfg(unix)] +#[test] +fn acp_daemon_attached_close_stop_failure_fences_the_session() { + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "responses": ["unused"] }), + ); + let session_id = initialize_and_new_session(&mut client); + let probe = + pa_types::platform::transport::connect_blocking(&client.socket).expect("daemon socket"); + let reader = probe.try_clone_box().expect("daemon socket clone"); + let _ = reader.set_read_timeout(Duration::from_mins(2)); + let hello = BufReader::new(reader) + .lines() + .map(|line| { + serde_json::from_str::(&line.expect("daemon line")).expect("daemon JSON") + }) + .find(|frame| frame["type"] == json!("daemon_hello")) + .expect("the daemon closed without a hello"); + let pid = hello["supervisorPid"].as_u64().expect("supervisor pid"); + drop(probe); + let killed = std::process::Command::new("kill") + .args(["-9", &pid.to_string()]) + .status() + .expect("kill the supervisor"); + assert!(killed.success(), "the supervisor crash did not run"); + + let close = client.request("session/close", &json!({ "sessionId": session_id })); + let (close_response, _) = client.wait_response(close, TIMEOUT); + assert_eq!( + close_response["error"]["code"], -32603, + "the failed stop errors the close: {close_response}" + ); + let stop_failure = close_response["error"]["data"]["details"] + .as_str() + .unwrap_or_default() + .to_string(); + assert!( + stop_failure.contains("the daemon connection"), + "the stop error is the fence's message: {stop_failure}" + ); + + let prompt = client.request( + "session/prompt", + &json!({ "sessionId": session_id, "prompt": [{ "type": "text", "text": "after the failed stop" }] }), + ); + let (prompt_response, _) = client.wait_response(prompt, TIMEOUT); + assert_eq!( + prompt_response["error"]["data"]["details"], + format!("ACP session stop failed: {stop_failure}"), + "a prompt while the stop failed answers the TS fence: {prompt_response}" + ); + + let refused = client.request("session/new", &json!({ "mcpServers": [] })); + let (refused_response, _) = client.wait_response(refused, TIMEOUT); + assert_eq!( + refused_response["error"]["data"]["details"], + "prime-agent ACP mode hosts one session per connection; start another prime-agent process for a second session", + "the close-failed session keeps the single-session slot: {refused_response}" + ); + + let close_again = client.request("session/close", &json!({ "sessionId": session_id })); + let (again_response, _) = client.wait_response(close_again, TIMEOUT); + assert_eq!(again_response["error"]["code"], -32603); + assert_ne!( + again_response["error"]["data"]["details"], + format!("Unknown ACP session: {session_id}"), + "the session stays bound after the failed close: {again_response}" + ); + drop(client); +} + +#[test] +fn acp_daemon_attached_close_holds_the_input_pause_until_the_next_session_new() { + let mut client = AcpChild::spawn( + &["--mode", "acp", "--no-session"], + &json!({ "engine": "faux", "responses": ["first", { "text": "FOREIGN", "delayMs": 150_000 }] }), + ); + let socket = client.socket.clone(); + let session_id = initialize_and_new_session(&mut client); + assert_prompt_ends_turn(&mut client, &session_id, "one"); + + let close = client.request("session/close", &json!({ "sessionId": session_id })); + let (close_response, _) = client.wait_response(close, TIMEOUT); + assert_eq!(close_response["result"], json!({})); + + let active_session_id = live_sessions(&socket)[0]["activeSessionId"] + .as_str() + .expect("the resident session") + .to_string(); + let _ = daemon_request( + &socket, + "foreign-follow-up", + &json!({ "type": "follow_up", "activeSessionId": active_session_id, "message": "foreign" }), + ); + let idle = daemon_request( + &socket, + "foreign-idle", + &json!({ "type": "wait_for_idle", "activeSessionId": active_session_id }), + ); + assert!( + idle["success"] == json!(true), + "no foreign turn runs on the resident session while the close holds the input pause: {idle}" + ); + + let _second = new_session(&mut client); + drop(client); +} diff --git a/crates/pa-cli/tests/agents_view_flash_e2e.rs b/crates/pa-cli/tests/agents_view_flash_e2e.rs index 3505bc21b04..69ed1643c8d 100644 --- a/crates/pa-cli/tests/agents_view_flash_e2e.rs +++ b/crates/pa-cli/tests/agents_view_flash_e2e.rs @@ -201,10 +201,9 @@ impl Client { .set_read_timeout(Some(Duration::from_millis(100))) .expect("set timeout"); loop { - line.clear(); match self.reader.read_line(&mut line) { Ok(0) => panic!("supervisor closed the connection"), - Ok(_) if line.trim().is_empty() => {} + Ok(_) if line.trim().is_empty() => line.clear(), Ok(_) => return serde_json::from_str(line.trim()).expect("parse line"), Err(error) => { assert!(Instant::now() < deadline, "timed out reading: {error}"); diff --git a/crates/pa-cli/tests/chat_agents_chat_roundtrip_e2e.rs b/crates/pa-cli/tests/chat_agents_chat_roundtrip_e2e.rs index eb7b4f67610..d1b8af237aa 100644 --- a/crates/pa-cli/tests/chat_agents_chat_roundtrip_e2e.rs +++ b/crates/pa-cli/tests/chat_agents_chat_roundtrip_e2e.rs @@ -349,6 +349,9 @@ async fn the_roundtrip_reentry_renders_the_same_transcript() { /// sequence, and the transcript-unchanged sojourn's re-entry still /// adopts — the re-entry's first draw serves the held packs (the /// observable the byte-identical frames cannot prove on their own). +/// The turn is submitted through the settle barrier, so the exit stashes +/// under the worker's final sequence (the settle update after +/// `agent_end`). #[tokio::test] async fn a_post_turn_sojourn_reentry_still_serves_the_held_packs() { let _handoff_guard = HANDOFF_E2E_LOCK.lock().await; @@ -382,8 +385,10 @@ async fn a_post_turn_sojourn_reentry_still_serves_the_held_packs() { needle: "flow audit clean".to_string(), timeout_ms: 20_000, }, - HeadlessStep::Submit("one more turn".to_string()), - HeadlessStep::WaitIdle { timeout_ms: 30_000 }, + HeadlessStep::SubmitAndSettle { + text: "one more turn".to_string(), + timeout_ms: 30_000, + }, HeadlessStep::WaitRender { needle: "the live turn reply".to_string(), timeout_ms: 20_000, diff --git a/crates/pa-cli/tests/clipboard_auth_commands_e2e.rs b/crates/pa-cli/tests/clipboard_auth_commands_e2e.rs index 57e16faaa67..5a01a16ddc0 100644 --- a/crates/pa-cli/tests/clipboard_auth_commands_e2e.rs +++ b/crates/pa-cli/tests/clipboard_auth_commands_e2e.rs @@ -40,8 +40,8 @@ struct Supervisor { impl Drop for Supervisor { fn drop(&mut self) { - graceful_shutdown(&self.socket); let worker_pids = child_pids_of(self.child.id()); + graceful_shutdown(&self.socket); let _ = self.child.kill(); let _ = self.child.wait(); for pid in worker_pids { @@ -52,9 +52,8 @@ impl Drop for Supervisor { } fn kill_worker(pid: u32) { - unsafe { - libc::kill(pid as i32, libc::SIGKILL); - } + let _ = + pa_core::platform::process::kill_pid(pid as i32, pa_core::platform::process::Signal::Kill); let deadline = Instant::now() + Duration::from_secs(5); while process_alive(pid) { assert!( @@ -65,6 +64,7 @@ fn kill_worker(pid: u32) { } } +#[cfg(target_os = "linux")] fn process_alive(pid: u32) -> bool { std::fs::read_to_string(format!("/proc/{pid}/stat")).is_ok_and(|stat| { let rest = stat @@ -75,6 +75,22 @@ fn process_alive(pid: u32) -> bool { }) } +#[cfg(not(target_os = "linux"))] +fn process_alive(pid: u32) -> bool { + if !pa_core::platform::process::pid_exists(pid) { + return false; + } + let output = Command::new("/bin/ps") + .args(["-p", &pid.to_string(), "-o", "stat="]) + .output() + .expect("query the owned worker state"); + String::from_utf8_lossy(&output.stdout) + .split_whitespace() + .next() + .is_some_and(|state| !state.starts_with('Z') && !state.starts_with('X')) +} + +#[cfg(target_os = "linux")] fn child_pids_of(ppid: u32) -> Vec { let mut pids = Vec::new(); let entries = std::fs::read_dir("/proc").expect("read /proc"); @@ -100,6 +116,24 @@ fn child_pids_of(ppid: u32) -> Vec { pids } +#[cfg(not(target_os = "linux"))] +fn child_pids_of(ppid: u32) -> Vec { + let output = Command::new("/bin/ps") + .args(["-axo", "pid=,ppid="]) + .output() + .expect("query the spawned supervisor's children"); + assert!(output.status.success(), "the native PID/PPID query failed"); + String::from_utf8_lossy(&output.stdout) + .lines() + .filter_map(|line| { + let mut fields = line.split_whitespace(); + let child = fields.next()?.parse::().ok()?; + let parent = fields.next()?.parse::().ok()?; + (parent == ppid).then_some(child) + }) + .collect() +} + fn graceful_shutdown(socket: &Path) { use std::io::{BufRead, BufReader, Write}; use std::os::unix::net::UnixStream; @@ -712,7 +746,7 @@ async fn tui_copy_emits_the_ts_osc52_sequence() { "the empty-transcript error renders:\n{rendered}" ); assert!( - rendered.contains("Copied last agent message to clipboard"), + rendered.contains("Clipboard request sent; paste in the local terminal to verify delivery"), "the copy status renders:\n{rendered}" ); assert!( @@ -788,13 +822,16 @@ async fn tui_copy_toast_coalesces_consecutive_copies_and_auto_dismisses() { // The coalesced count-bump toast renders (observed, not // slept-for): the third copy's ack is the (x3) label. pa_tui::interactive::HeadlessStep::WaitRender { - needle: "Copied last agent message to clipboard (x3)".to_string(), + needle: + "Clipboard request sent; paste in the local terminal to verify delivery (x3)" + .to_string(), timeout_ms: 10_000, }, // Past the toast's TTL: the overlay dismisses (the newest // frame stops carrying the ack). pa_tui::interactive::HeadlessStep::WaitGone { - needle: "Copied last agent message to clipboard".to_string(), + needle: "Clipboard request sent; paste in the local terminal to verify delivery" + .to_string(), timeout_ms: 10_000, }, ], @@ -816,7 +853,7 @@ async fn tui_copy_toast_coalesces_consecutive_copies_and_auto_dismisses() { ); } } - let label = "Copied last agent message to clipboard"; + let label = "Clipboard request sent; paste in the local terminal to verify delivery"; // Every copy registered: the headless OSC 52 sink is one buffer for // the whole run, so the exact TS sequence appears three times // concatenated - one emission per copy. @@ -1039,9 +1076,19 @@ async fn tui_prime_login_escape_keeps_the_default_team_status() { steps: vec![ pa_tui::interactive::HeadlessStep::Submit("/login".to_string()), enter(), - pa_tui::interactive::HeadlessStep::WaitMs(400), + pa_tui::interactive::HeadlessStep::WaitRender { + needle: "Select a Prime Team:".to_string(), + timeout_ms: 10_000, + }, key(crossterm::event::KeyCode::Esc), - pa_tui::interactive::HeadlessStep::WaitMs(400), + pa_tui::interactive::HeadlessStep::WaitRender { + needle: "Saved API key for Prime Inference. Using personal account.".to_string(), + timeout_ms: 10_000, + }, + pa_tui::interactive::HeadlessStep::WaitGone { + needle: "Select a Prime Team:".to_string(), + timeout_ms: 10_000, + }, ], width: 120, height: 36, diff --git a/crates/pa-cli/tests/continue_guard_e2e.rs b/crates/pa-cli/tests/continue_guard_e2e.rs index 27a3aa74d73..10d501569c0 100644 --- a/crates/pa-cli/tests/continue_guard_e2e.rs +++ b/crates/pa-cli/tests/continue_guard_e2e.rs @@ -120,16 +120,16 @@ fn make_session_active(socket: &Path, session_path: &Path, cwd: &Path) -> String writer.write_all(line.as_bytes()).expect("send"); writer.flush().expect("flush"); let deadline = Instant::now() + Duration::from_secs(30); + let mut line = String::new(); loop { assert!(Instant::now() < deadline, "no create response"); - let mut line = String::new(); reader .get_mut() .set_read_timeout(Some(Duration::from_millis(100))) .expect("timeout"); match reader.read_line(&mut line) { Ok(0) => panic!("daemon closed during create"), - Ok(_) if line.trim().is_empty() => {} + Ok(_) if line.trim().is_empty() => line.clear(), Ok(_) => { let response: Value = serde_json::from_str(line.trim()).expect("parse"); assert_eq!(response["success"], true, "create failed: {response}"); diff --git a/crates/pa-cli/tests/daemon_commands_e2e.rs b/crates/pa-cli/tests/daemon_commands_e2e.rs index 53eaf6795e3..1bb3fd7cb9e 100644 --- a/crates/pa-cli/tests/daemon_commands_e2e.rs +++ b/crates/pa-cli/tests/daemon_commands_e2e.rs @@ -212,10 +212,9 @@ impl Wire { .set_read_timeout(Some(Duration::from_millis(100))) .expect("set timeout"); loop { - line.clear(); match self.reader.read_line(&mut line) { Ok(0) => panic!("daemon closed the connection"), - Ok(_) if line.trim().is_empty() => {} + Ok(_) if line.trim().is_empty() => line.clear(), Ok(_) => return serde_json::from_str(line.trim()).expect("parse daemon line"), Err(_) if Instant::now() < deadline => {} Err(error) => panic!("timed out waiting for daemon line: {error}"), @@ -307,12 +306,16 @@ fn normalize_token(token: &str) -> String { if let Some(stripped) = token.strip_suffix(',') { return format!("{},", normalize_token(stripped)); } - if is_age(token) || is_clock(token) || token == "AM" || token == "PM" { - return "