Files
PaperClipAI/server
Dotta b066a3ddd3 fix(cloud-trial): mount internal OpenRouter proxy before actorMiddleware (#23571)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Cloud starter-inference OpenCode runs in remote plugin Runners
authenticate to `/api/internal/cloud-inference/v1/*` using an ephemeral
per-run `pctrial_` Bearer token.
> - In `server/src/app.ts`, `/api/internal/cloud-inference` was mounted
after `actorMiddleware`.
> - In authenticated deployments, `actorMiddleware` intercepted every
`Authorization: Bearer pctrial_...` request, tried to verify it as an
agent JWT/API key, and returned HTTP 401 (`Agent token did not verify`)
before the proxy route ever ran.
> - This pull request mounts `/api/internal/cloud-inference` before
`actorMiddleware` (alongside `voiceWebhookRoutes`, which uses the same
pre-`actorMiddleware` pattern) and adds a full `createApp` regression
test in `authenticated` deployment mode.
> - The benefit is that remote Cloud starter-inference OpenCode runs
authenticate cleanly through `/api/internal/cloud-inference/v1/*` and
stream real replies.

## Linked Issues or Issue Description

Refs #23401, #23559

**Summary**
Mount `cloudInferenceInternalProxyRoutes` before `actorMiddleware` in
`server/src/app.ts` so ephemeral `pctrial_` Bearer tokens are not
rejected as invalid agent tokens.

**Steps to Reproduce**
1. Run Paperclip in `authenticated` deployment mode with Cloud starter
inference enabled.
2. Start a starter-inference OpenCode run in a remote plugin Runner
environment.
3. Observe HTTP 401 (`Unauthorized: Agent token did not verify; obtain
fresh credentials and retry`) when OpenCode calls
`/api/internal/cloud-inference/v1/chat/completions`.

**Expected Behavior**
Requests to `/api/internal/cloud-inference/v1/*` reach
`cloudInferenceInternalProxyRoutes`, validate the active per-run
`pctrial_` token, and forward to OpenRouter with the server-held starter
key.

## What Changed

- `server/src/app.ts`: Moved `cloudTrialInferenceController`
initialization and `app.use("/api/internal/cloud-inference",
cloudInferenceInternalProxyRoutes(cloudTrialInferenceController))` above
`app.use(actorMiddleware(...))`, alongside `voiceWebhookRoutes`.
- `server/src/__tests__/cloud-trial-proxy-middleware-order.test.ts`:
Added a full `createApp` regression test (`deploymentMode:
"authenticated"`) verifying that unknown `pctrial_` tokens reach
`cloudInferenceInternalProxyRoutes`
(`proxy_token_not_found_on_instance`) and active `pctrial_` tokens proxy
cleanly to upstream OpenRouter with the server-held key.

## Verification

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

## Risks

- Low risk. `/api/internal/cloud-inference` strictly authenticates every
request against active per-run `pctrial_` tokens in memory using
`timingSafeEqual` and rejects all unauthenticated or unknown tokens with
HTTP 401.

## 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-02 18:45:16 -06:00
..