Skip to content

🔗 fix: Refuse Uploads and Fail Readiness When the File Server Uses Another Redis - #398

Merged
danny-avila merged 1 commit into
mainfrom
danny-avila/file-server-store-check
Nov 15, 2026
Merged

danny-avila merged 1 commit into
mainfrom
danny-avila/file-server-store-check

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

Code API and the file server it forwards uploads to must share one Redis. Nothing enforced that or wrote it down, and a pair that did not share one kept answering 200 while every upload it accepted was unusable. This change makes that pairing fail at upload time and at the health check.

What broke

  • On /upload and /upload/batch, the API writes session:<session_id> to its Redis and forwards the bytes to FILE_SERVER_URL.
  • The file server reads session:<session_id> from its own Redis and writes upload:<sessionKey><session_id><fileId> there.
  • A download, an /exec input and a workspace import_file are each authorized only if that record exists, under that session key, in the API's Redis.

A deployment paired a remote-bridge API that had its own Redis with another tier's file server, which used a different one. The file server found no session, stored each object anyway, wrote the record as upload:null<session_id><fileId>, and answered 200.

How it stayed silent

  • Startup checked nothing about the pairing, so both services came up healthy.
  • The upload succeeded from the caller's point of view.
  • The only symptom appeared later and elsewhere: every import_file returned 403 IMPORT_SOURCE_UNAUTHORIZED, which reads as an ownership problem, not a deployment one.

What happens now

1. The file server refuses an upload whose session is not in its Redis.

It answers 409 UPLOAD_SESSION_UNKNOWN, stores nothing, writes no record, and logs an error naming the likely cause (API and file server on different Redis instances, or a session older than SESSION_CACHE_TTL). Both upload routes go through the same check. The multipart route previously swallowed per-file errors and answered 200 with an empty list; for this error it now answers 409.

Existing callers already treat a failed upload as a failure, so no caller changes were needed:

Caller Result
/upload 500, with the file server's answer in the API log
/upload/batch the file is reported as an error
Sandbox output upload the file is left out of the execution result instead of being listed with an id that cannot be downloaded
Egress gateway releases the upload reservation and passes the 409 through

Why refuse instead of keeping the bytes and skipping the record: I traced every path that uploads to the file server, and each one has its session registered by the API first.

Upload path Session registered by
/upload, /upload/batch recordSessionOwnership, awaited before the bytes are forwarded
Sandbox outputs (direct, via the egress gateway, or via the bridge relay) /exec and the programmatic routes, before the job is queued; a grant can only write to that output session
Import and PTC grants cannot upload (max_upload_bytes: 0)

So an unknown session means the Redis is not shared, the session outlived SESSION_CACHE_TTL, or Redis lost the key. In all three the stored object would be unreadable through every route: each reader requires the record under the real session key, reads also require the live session: key, and nothing recreates upload records (rehydrate-session-cache.ts restores session keys only). Keeping the object would only leave bytes nobody can read, which in the reported case landed in another deployment's bucket. No authorization check was changed or relaxed.

2. The API checks the pairing at startup and reports a mismatch through /v1/health.

The API writes a random key with a 60-second TTL to its Redis and asks the file server, over a new route behind the existing internal-service auth (X-CodeAPI-Internal-Token), whether it can see it.

Result Behavior
File server sees the key one info log; the check ends
File server does not see the key, confirmed by a second probe one second later error log; /v1/health answers 503 with {"status":"not ready","checks":{"fileServerStore":"mismatch"}}; re-checked every 30 seconds and cleared as soon as the file server is corrected, without an API restart
Cannot tell one warning naming the reason; the API serves normally; retried in the background (2 seconds, doubling to 60)

I gated /v1/health instead of exiting at startup. It is the API's only health endpoint, the Helm chart and Compose files already use it as the readiness signal, and it already reports an unreachable Redis as 503. Exiting would also turn one bad probe into a crash and would need a restart to recover after the file server is fixed. The Helm chart uses the same path for liveness, so a mismatched pod is also restarted until the pairing is corrected.

Startup waits only for the first result, and each probe is bounded at 5 seconds end to end, including the Redis write (the API's client queues commands while disconnected). Only the random id is sent; the file server derives the store-probe:<id> key itself, so the route cannot be used to test for any other key. Neither the key nor the token is logged. CODEAPI_FILE_SERVER_STORE_CHECK=false skips the startup check; the file server's refusal is not configurable.

3. The remote-bridge guide documents the requirement and what the two checks report, with a matching entry under common failures in the worker runbook.

Version skew

  • New API, older file server: the probe route answers 404, which is "cannot tell", not a mismatch. The API starts, logs one warning, and keeps retrying, so the check concludes once the file server is upgraded.
  • Older API, new file server: no probe is sent. The only difference is that an upload for an unknown session is refused instead of stored unreadably.
  • File server not up yet, or internal token mismatch: also "cannot tell".
  • Rollback of the file server while a mismatch is known: the state returns to "cannot tell" and the API serves again.

Not covered

  • local-api.ts, the development-only entry point, does not run the startup check.
  • An egress gateway whose EGRESS_GATEWAY_FILE_SERVER_URL names a different file server than the API's FILE_SERVER_URL is not probed. The file server's own refusal still applies to it.
  • Who owns the upload record, and the key shapes, are unchanged.

Change Type

  • Bug fix
  • Documentation update

Testing

  • file-server.test.ts starts the real file server process against a real Redis. Only S3 is stubbed, and only for the calls a zero-byte upload makes. It covers:
    • shared Redis: the record is written under the real session key, the real import authorization passes, and the probe reports shared;
    • session registered in another Redis: 409, nothing stored, no upload: key in either Redis, the cause in the file server log, import still refused, and the probe reports split;
    • the multipart route, the probe route's auth and input validation, and a rejected token reported as "cannot tell".
  • store-pairing.test.ts runs the probe and the monitor against two real Redis instances and the real probe router over HTTP. It covers shared, split with recovery after correction, an older file server without the route (API healthy, one warning), a file server that comes up late, both timeouts, probe-key expiry, a single contradicted mismatch, and the /v1/health responses.
  • Against the file server from main, all five file-server.test.ts tests fail.
  • Not exercised: the two lifecycle.ts call sites and the /v1/health wiring in api-server.ts and service-api.ts are not run by a test (those modules open the production Redis connection on import); the handler and the monitor they call are. No deployment was touched.

Test Configuration

cd service
bun install --frozen-lockfile
bun test ./src/file-server.test.ts ./src/store-pairing.test.ts ./src/internal-redirect-policy.test.ts
bun --bun run build
bun run test
bunx tsc --noEmit -p tsconfig.json
bunx eslint src/store-pairing.ts src/upload-record.ts src/health.ts src/store-pairing.test.ts src/file-server.test.ts
  • The focused files pass (32 tests), and the build succeeds.
  • bun run test: 1263 pass, 17 skip, 5 fail. The same five (job-cancellation-commit.test.ts, tool-input-signature.test.ts) fail identically on an untouched main checkout on this machine. One earlier run also failed a timing-dependent case in bridge/workspace-store.test.ts, which passed alone three times and in the next full run.
  • tsc reports the same 7 errors as main, none in a file this PR touches. ESLint is clean on the new files.

Checklist

  • I traced every caller of the file server's upload routes before choosing to refuse.
  • I left every authorization check as it was.
  • I added tests for the shared, split and older-file-server cases.
  • I updated the remote-bridge documentation.

…other Redis

The API registers each upload session in Redis and the file server reads it
back to write the `upload:` record that later authorizes the object. Nothing
enforced that the two share a Redis. A file server on another Redis found no
session, stored the object anyway under `upload:null…`, and reported success;
every later download, execution input and `import_file` was then refused.

- File server: refuse an upload whose session is not in its Redis with
  `409 UPLOAD_SESSION_UNKNOWN`, store nothing, and log the cause.
- API: check at startup that `FILE_SERVER_URL` sees a short-lived random key
  written to the API's Redis, through the authenticated
  `POST /internal/store-probe`. A confirmed mismatch makes `/v1/health`
  answer 503 until a re-check passes. An unreachable or older file server is
  inconclusive: one warning, background retries, never a failure.
- Document the pairing requirement in the remote-bridge guide.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T13:53:45.445108Z 145d904 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 145d904360

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 145d904360

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

This is the final review pass for this PR, at head 145d904. Please report anything still outstanding: correctness of the file server's unknown-session refusal on both upload routes, the store probe's auth and timeout bounds, the health gating and its version-skew behavior, and the tests.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 145d904360

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila merged commit 65fcd88 into main Nov 15, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant