| 1 | # Storage Sync Flow for `cn serve` |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | The `--id <storageId>` flag enables the `cn serve` command to periodically persist session state to an external Continue-managed storage bucket. On startup, the CLI exchanges the provided `storageId` for two pre-signed S3 URLs - one for `session.json` and one for `diff.txt` - and then pushes fresh copies of those files every 30 seconds. |
| 6 | |
| 7 | This document captures the responsibilities for both the CLI and backend components so we can iterate on the feature together. |
| 8 | |
| 9 | ## CLI Responsibilities |
| 10 | |
| 11 | - **Flag plumbing**: When `cn serve` is invoked with `--id <storageId>`, the CLI treats that value as an opaque identifier. |
| 12 | - **API key auth**: The CLI attaches the user-level Continue API key (same mechanism we already use for other authenticated requests) to backend calls. |
| 13 | - **Presign handshake**: |
| 14 | 1. On startup, issue `POST https://api.continue.dev/agents/storage/presigned-url` with JSON payload `{ "storageId": "<storageId>" }`. |
| 15 | 2. Expect a response payload containing two pre-signed `PUT` URLs and their target object keys: |
| 16 | ```json |
| 17 | { |
| 18 | "session": { |
| 19 | "key": "sessions/<sessionId>/session.json", |
| 20 | "putUrl": "https://<s3-host>/..." |
| 21 | }, |
| 22 | "diff": { |
| 23 | "key": "sessions/<sessionId>/diff.txt", |
| 24 | "putUrl": "https://<s3-host>/..." |
| 25 | } |
| 26 | } |
| 27 | ``` |
| 28 | 3. If the call fails, log and continue without remote storage (no retries yet). |
| 29 | - **Periodic uploads**: |
| 30 | - Every 30 seconds (configurable later), serialize the in-memory session to `session.json` and fetch the `/diff` payload to produce `diff.txt`. |
| 31 | - Upload both artifacts using their respective `PUT` URLs. For now we overwrite the same objects each cycle. |
| 32 | - Errors should be logged but non-fatal; the server keeps running. |
| 33 | - If the repo check fails (no git repo or missing `main`), `diff.txt` uploads an empty string and we log the condition once for debugging. |
| 34 | |
| 35 | ## Backend Responsibilities |
| 36 | |
| 37 | - **Endpoint surface**: `POST /agents/storage/presigned-url` accepts a JSON body `{ "storageId": string }`. |
| 38 | - **Authentication**: Leverage the caller's Continue API key (the request arrives with the standard `Authorization: Bearer <apiKey>` header). Apply normal auth/tenant validation so users can only request URLs tied to their account/org. |
| 39 | - **URL issuance**: |
| 40 | - Resolve `storageId` into the desired S3 prefix (e.g., `sessions/<org>/<storageId>/`). |
| 41 | - Generate two short-lived pre-signed `PUT` URLs: one for `session.json`, one for `diff.txt`. |
| 42 | - Return both URLs and their keys in the response payload described above. |
| 43 | - **Expiration**: The initial implementation can hand out URLs with a generous TTL (e.g., 60 minutes). Later we will add CLI-side refresh logic before expiry. |
| 44 | |
| 45 | ## Open Questions & Future Enhancements |
| 46 | |
| 47 | - **URL refresh**: When the presigned URLs expire we currently have no refresh cycle. We'd need either a renewal endpoint or to re-call the existing endpoint on failure. |
| 48 | - **Upload cadence**: The 30-second interval is hard-coded for now. Consider making it configurable in both CLI and backend policies. |
| 49 | - **Error telemetry**: Decide if repeated upload failures should trip analytics or circuit breakers. |
| 50 | - **Diff source**: `diff.txt` currently mirrors the `/diff` endpoint response. Confirm backend expectations for format and size limits. |
| 51 | - **Security**: We might want to sign responses or enforce stricter scope on `storageId` mapping (e.g., require both org + storageId and validate ownership). |
| 52 | |
| 53 | --- |
| 54 | |
| 55 | This document should evolve alongside implementation details; update it whenever the API contract or client behavior changes. |