Files
PaperClipAI/server/src
Dotta 8e2381a1d1 fix(cloud-trial): use stable HMAC-signed proxy tokens across warm OpenCode sessions (#23616)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Cloud starter-inference OpenCode runs in remote Daytona sandboxes
reuse warm OpenCode sessions across consecutive chat turns.
> - Inside the warm sandbox, `startOpenCodeProviderProxy` stays alive
bound to the first run's `PAPERCLIP_OPENROUTER_PROXY_TOKEN`.
> - Because `cloud-trial-proxy.ts` generated a random per-run token and
deleted it in `cleanup()`, every reused-session turn failed on attempt
#1 with HTTP 401 (`Trial proxy token is no longer active on this server
instance`) and posted `"The OpenCode run failed before it completed."`
into the chat before auto-retry recovered.
> - This pull request replaces single-run random tokens with
deterministic HMAC-SHA256 signed per-`(companyId, agentId,
allowedModel)` tokens (`pctrial_v1.<payload>.<sig>`) that remain valid
across warm turns and stack restarts/sleep-wake, while still failing
closed on tampering, `work_requires_model`, `$1` exhaustion, disallowed
models, and provider migration.
> - The benefit is zero spurious 401 failure messages on consecutive
chat turns in warm starter-inference sessions.

## Linked Issues or Issue Description

Refs #23401, #23559, #23571

**Summary**
Use stable HMAC-signed proxy tokens for Cloud starter-inference OpenCode
sessions so reused warm sessions succeed on attempt #1.

**Steps to Reproduce**
1. Start a Cloud starter-inference (`free_inference_1`) chat with an
OpenCode agent in a warm Daytona sandbox.
2. Send a first message and wait for the reply.
3. Send a second message in the same chat (`sessionReused: true`).
4. Observe HTTP 401 (`Trial proxy token is no longer active on this
server instance`) and `"The OpenCode run failed before it completed."`
before auto-retry starts a fresh process.

**Expected Behavior**
Consecutive messages on a reused warm OpenCode session authenticate
cleanly on attempt #1 with zero 401 failures, while tokens still fail
closed on tampering, `work_requires_model`, `$1` exhaustion, disallowed
models, or migration to a user provider.

## What Changed

- `server/src/services/cloud-trial-proxy.ts`: Replaced single-run random
`pctrial_` tokens with deterministic HMAC-SHA256 signed
`pctrial_v1.<payload>.<sig>` tokens derived from the stack's stable
secret (`PAPERCLIP_CLOUD_CONNECTOR_SIGN_PRIVATE_KEY` /
`PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN` / `PAPERCLIP_AGENT_JWT_SECRET` /
`BETTER_AUTH_SECRET`, with a per-process random fallback). Kept
`cleanup()` probing OpenRouter usage without deleting the token on
normal run exit, and added `revokeCompanyTokens(companyId)`.
- `server/src/services/cloud-trial-inference.ts`: Called
`proxy.revokeCompanyTokens(companyId)` upon `migrateTrialAgents`
completion and exposed `revokeCompanyTokens`.
- `server/src/__tests__/cloud-trial-inference.test.ts`: Added regression
test verifying warm session token reuse after `cleanup()`, survival
across a fresh controller restart, HTTP 401 on tampered tokens, and HTTP
401 after `revokeCompanyTokens`.

## Verification

- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/cloud-trial-inference.test.ts
src/__tests__/cloud-trial-proxy-middleware-order.test.ts` — passed (8/8
tests).
- `pnpm --filter @paperclipai/server typecheck` — passed (0 errors).

## Risks

- Low risk. Tokens are HMAC-SHA256 verified with `timingSafeEqual`,
strictly scoped to `(companyId, agentId, allowedModel)`, never contain
the raw OpenRouter key, and fail closed on `work_requires_model`, `$1`
budget exhaustion, disallowed models, and company migration.

## Model Used

- Google Gemini (Antigravity agentic runtime, thinking mode with tool
use and local code execution).

## 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
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge
2026-12-03 08:54:03 -06:00
..
…
…
…
…
…
…