feat: optimize heartbeat token usage

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Dotta
2026-03-13 08:49:11 -05:00
parent 2246d5f1eb
commit 7d1748b3a7
28 changed files with 800 additions and 95 deletions

View File

@@ -0,0 +1,40 @@
# Token Optimization Implementation Report
Implemented the token-optimization plan across heartbeat orchestration, issue context APIs, adapter prompt construction, skill exposure, and agent configuration UX.
The main behavior changes are:
- Heartbeat telemetry now normalizes sessioned local adapter usage as per-run deltas instead of blindly trusting cumulative session totals.
- Timer and manual wakes now preserve task sessions by default; fresh sessions are forced only for explicit `forceFreshSession` wakes or new issue assignment wakes.
- Heartbeat session rotation is now policy-driven in the control plane, with a handoff note injected when a session is compacted and restarted.
- Paperclip issue context now has incremental APIs: `GET /api/agents/me/inbox-lite`, `GET /api/issues/:id/heartbeat-context`, and comment delta queries via `GET /api/issues/:id/comments?after=...&order=asc`.
- The `paperclip` skill now teaches agents to use those compact/incremental APIs first, while keeping full-thread fetches as a cold-start fallback.
- All local adapters now separate first-session bootstrap prompts from per-heartbeat prompt templates, and emit prompt size metrics in invocation metadata.
- Adapter create flows now persist `bootstrapPromptTemplate` correctly.
- The agent config UI now explains the difference between bootstrap prompts and heartbeat prompts and warns about prompt churn.
- Runtime skill defaults now include `paperclip`, `para-memory-files`, and `paperclip-create-agent`. `create-agent-adapter` was moved to `.agents/skills/create-agent-adapter`.
Files with the most important implementation work:
- `server/src/services/heartbeat.ts`
- `server/src/services/issues.ts`
- `server/src/routes/issues.ts`
- `server/src/routes/agents.ts`
- `server/src/routes/access.ts`
- `skills/paperclip/SKILL.md`
- `packages/adapters/*/src/server/execute.ts`
- `packages/adapters/*/src/ui/build-config.ts`
- `ui/src/components/AgentConfigForm.tsx`
Verification completed successfully:
- `pnpm -r typecheck`
- `pnpm test:run`
- `pnpm build`
While verifying, I also fixed two existing embedded-postgres typing mismatches so repo-wide `typecheck` and `build` pass again:
- `packages/db/src/migration-runtime.ts`
- `cli/src/commands/worktree.ts`
Next useful follow-up is measuring the before/after effect in real runs now that telemetry is less misleading and prompt/session reuse behavior is consistent across adapters.