mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 01:24:30 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - When a run starts, Core claims it, prepares the host, acquires and prepares the execution environment, restores the saved session, sets up tools, and starts the agent's runner. > - The task view shows "Thinking" for all of that time, so a slow cold start looks like a slow model. > - Core already times each of these steps (`heartbeat.prepare.*`, `native.controller.*` and the native run trace), but nothing tells the board which step is running. > - This pull request adds a closed list of startup stages and a process-local tracker that follows those timed steps. It sends the current stage to the board as a live event and adds it to active run reads. > - The benefit is that the board can say what a run is doing before the agent's turn starts. The UI change comes in a stacked pull request. ## Linked Issues or Issue Description No public issue exists. **Subsystem affected** Run execution status (`server/src/services/heartbeat*`, native runtime trace) and the shared run types. **Problem or motivation** From the claim to the agent's first turn, the task view always says "Thinking". On a test instance over 24 hours, the median time before the first agent event was 10 to 50 seconds. Most of it was environment setup, session migration (median 0.3 s, P90 14 s) and tool setup (median 8 s, P90 18 s), not model time. A user cannot tell a slow start from a slow model. **Proposed solution** Name the startup steps that every execution environment goes through and report the current one. Derive the stages from the steps Core already times, so the stage and the trace spans cannot drift apart. **Alternatives considered** Free text from each environment, which gives inconsistent wording and lets an environment show arbitrary text. Grouping by where the work runs, which breaks the order because Core does host work both before and after it acquires the environment. ## What Changed - `packages/shared`: add `RUN_STARTUP_STAGES` (`getting_ready`, `starting_environment`, `preparing_workspace`, `restoring_session`, `connecting_tools`, `starting_agent`), the `heartbeat.run.startup_stage` live event type and its payload type, and the `startupStage` and `startupSessionResumed` run fields. - `server/src/services/run-startup-stage.ts`: a process-local tracker. One table maps timed step names to stages. A run only moves forward within one execution attempt. Startup ends at the agent turn, turn submission or first agent activity (native runs), or when the heartbeat enters a direct adapter. When startup ends, the stage clears but the attempt keeps its resumed-session flag until the attempt ends. The store is bounded (512 runs, and a 1 hour TTL while a stage shows). - The preparation timing helper (`measure`) and the native run trace (`start`, which `record` also uses) report each step to the tracker. The heartbeat starts tracking when a run starts executing and stops when the execution ends. The native executor reports a resumed provider session. - `tests/runner-e2e/source-sdk-public-inputs.json`: regenerated, because the pinned shared SDK sources changed. - `decorateHeartbeatRunRuntimeStatus` adds `startupStage` and `startupSessionResumed` to active run reads and clears them for ended runs. - The native session executor ends startup when a run that reattached after a restart sees its turn already under way: a replayed `turn.submitted` or `turn.started`, or agent output, including output that arrives before this attempt saw its own `turn.started`. ## Verification - `npx vitest run src/services/run-startup-stage.test.ts` (13 tests): stage order, the forward-only rule, skipped stages, the end of startup, unknown runs, the resumed session (also after the turn starts), a new attempt, company and agent checks, TTL and the size bound, the real timing helper and native trace hooks, and active run reads. - `npx vitest run src/__tests__/heartbeat-execution-configuration.test.ts`: a new case runs a registered direct adapter through real preparation on the test Postgres. Each run publishes `getting_ready`, `starting_environment`, `preparing_workspace`, then the end of startup, and no stage shows while the adapter runs. - `cd tests/runner-e2e && npx vitest run source-sdk-public-inputs.test.mjs source-sdk-public-type.test.ts source-sdk-evidence.test.ts` (59 tests) after the registry refresh. - The CI failure in `adapter-utils` `run-fault-matrix.test.ts` (`case_14c`) is in a package this change does not touch. It passed three times locally. - Existing tests near the change pass: `native-run-trace.test.ts`, `native-run-trace-context.test.ts`, `heartbeat/preparation-timing-context.test.ts`, `heartbeat-run-runtime-status.test.ts`, `realtime/live-event-delivery.test.ts` (108 tests). - `npx vitest run src/services/native-runtime/native-session-executor.test.ts -t "native startup stage after a restart|native resumed preparation timing"` (16 tests): a reattached run leaves startup on a replayed turn start and on new agent output. Both new cases fail without the fix. - `pnpm --filter @paperclipai/shared typecheck` and `pnpm --filter @paperclipai/server typecheck` pass. - CI runs the full suite. ## Risks - Low risk. The tracker is in memory only and is never persisted. A server restart clears it, and the board then shows what it shows today. No run, session or launch state changes shape, so no saved state needs a migration. - Every hook is wrapped so that a tracker error cannot fail a run or its trace. - The live event uses the existing `heartbeat.run.*` delivery rule: it needs a `runId` and the viewer's run read access. - One extra live event per stage change (at most about seven for each run attempt). ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, Honeycomb trace queries for the timing data). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>