|
| 1 | +# Bulk Per-File Revisions — Boundary Spec |
| 2 | + |
| 3 | +This document scopes the server-side change to `/fs/bulk` so the response |
| 4 | +includes per-file revisions, and the matching simplification in the mount |
| 5 | +reconciler so it no longer needs a follow-up `ReadFile` to discover revisions. |
| 6 | + |
| 7 | +## 1. Files touched |
| 8 | + |
| 9 | +| File | Create/Modify | Reason | |
| 10 | +| ---- | ------------- | ------ | |
| 11 | +| `internal/httpapi/server.go` | Modify | `handleBulkWrite` must build and return a `results` array with `{path, revision, contentType}` for every successfully written file. | |
| 12 | +| `internal/httpapi/server_test.go` | Modify | Add a test asserting the new `results` field is populated for successful writes and that failed entries are absent from `results` (they continue to appear in `errors`). | |
| 13 | +| `internal/relayfile/store.go` | Modify | `BulkWrite` and `BulkWriteFork` already compute per-file revisions internally; change their signatures to surface those revisions (and content types) so `handleBulkWrite` can forward them. | |
| 14 | +| `internal/relayfile/types.go` (or wherever `BulkWriteFile` / `BulkWriteError` live) | Modify | Add a shared `BulkWriteResult` struct (`Path`, `Revision`, `ContentType`) so server and mount wire types stay aligned. | |
| 15 | +| `internal/mountsync/types.go` | Modify | `BulkWriteResponse.Results` already exists; verify field names and JSON tags match the server shape exactly (`results`, `path`, `revision`, `contentType`). Remove the `Files []RemoteFile` branch if it is only there as a compatibility shim — keep it only if it is still populated by another code path. | |
| 16 | +| `internal/mountsync/syncer.go` | Modify | `reconcileBulkWrite`: when the bulk response already provides a revision for the path, use it directly and skip the `ReadFile` round-trip. Retain the `ReadFile` fallback only for the defensive empty-revision case. | |
| 17 | +| `internal/mountsync/syncer_test.go` | Modify | Tighten `TestBulkMigrationReducesHTTPCalls` to assert zero total requests on `/fs/file` (not just zero `POST`s). Remove the now-redundant `fsFilePostCount` counter. | |
| 18 | + |
| 19 | +## 2. New wire shape for `/fs/bulk` response |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "written": 3, |
| 24 | + "errorCount": 1, |
| 25 | + "errors": [ |
| 26 | + { |
| 27 | + "path": "locked.txt", |
| 28 | + "code": "permission_denied", |
| 29 | + "message": "agent lacks write permission" |
| 30 | + } |
| 31 | + ], |
| 32 | + "results": [ |
| 33 | + { |
| 34 | + "path": "docs/a.md", |
| 35 | + "revision": "rev_01HW...", |
| 36 | + "contentType": "text/markdown" |
| 37 | + }, |
| 38 | + { |
| 39 | + "path": "docs/b.md", |
| 40 | + "revision": "rev_01HW...", |
| 41 | + "contentType": "text/markdown" |
| 42 | + }, |
| 43 | + { |
| 44 | + "path": "src/x.go", |
| 45 | + "revision": "rev_01HW...", |
| 46 | + "contentType": "text/x-go" |
| 47 | + } |
| 48 | + ], |
| 49 | + "correlationId": "corr_..." |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Notes: |
| 54 | + |
| 55 | +- `results` contains one entry per successfully written file, in the same |
| 56 | + order `BulkWrite` processed them. Entries in `errors` are **not** present in |
| 57 | + `results` (and vice versa). |
| 58 | +- `contentType` is emitted with `omitempty`; absent when the store did not |
| 59 | + resolve one. |
| 60 | +- `written` continues to equal `len(results)`. |
| 61 | + |
| 62 | +## 3. Backward compatibility |
| 63 | + |
| 64 | +- **Old SDK / client reads new response:** Extra top-level `results` field is |
| 65 | + ignored by standard JSON decoders (all existing SDKs use struct decoding with |
| 66 | + unknown-field tolerance or `map[string]any`). No existing field changes |
| 67 | + meaning, so old clients keep working. |
| 68 | +- **New mount reads old server response:** `BulkWriteResponse.Results` is |
| 69 | + `omitempty` on the wire and decodes to `nil` / empty. `revisionsByPath()` |
| 70 | + returns an empty map, and `reconcileBulkWrite` falls back to the existing |
| 71 | + `ReadFile` code path to recover the revision. This makes the rollout safe in |
| 72 | + either order (server-first or client-first). |
| 73 | +- No version gate, feature flag, or capability negotiation is required. |
| 74 | + |
| 75 | +## 4. `Store.BulkWrite` signature change |
| 76 | + |
| 77 | +Current: |
| 78 | + |
| 79 | +```go |
| 80 | +func (s *Store) BulkWrite(workspaceID string, files []BulkWriteFile) (int, []BulkWriteError) |
| 81 | +func (s *Store) BulkWriteFork(workspaceID, forkID string, files []BulkWriteFile) (int, []BulkWriteError) |
| 82 | +``` |
| 83 | + |
| 84 | +Proposed: |
| 85 | + |
| 86 | +```go |
| 87 | +func (s *Store) BulkWrite(workspaceID string, files []BulkWriteFile) (written int, results []BulkWriteResult, errors []BulkWriteError) |
| 88 | +func (s *Store) BulkWriteFork(workspaceID, forkID string, files []BulkWriteFile) (written int, results []BulkWriteResult, errors []BulkWriteError) |
| 89 | +``` |
| 90 | + |
| 91 | +Where `BulkWriteResult` is: |
| 92 | + |
| 93 | +```go |
| 94 | +type BulkWriteResult struct { |
| 95 | + Path string `json:"path"` |
| 96 | + Revision string `json:"revision"` |
| 97 | + ContentType string `json:"contentType,omitempty"` |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +- `written == len(results)` is an invariant maintained by both methods. |
| 102 | +- `BulkWriteFork` receives the identical treatment — same struct, same |
| 103 | + ordering guarantee, same invariant. This keeps fork and main-branch writes |
| 104 | + symmetric for the mount code that consumes them. |
| 105 | +- The new `BulkWriteResult` type lives next to `BulkWriteFile` / |
| 106 | + `BulkWriteError` in the `relayfile` package and is re-aliased from |
| 107 | + `internal/mountsync/types.go` the same way those two are today. |
| 108 | + |
| 109 | +## 5. Mount-side change |
| 110 | + |
| 111 | +In `reconcileBulkWrite`: |
| 112 | + |
| 113 | +- Before calling `ReadFile`, consult the `revisionsByPath()` map built from |
| 114 | + `BulkWriteResponse.Results`. If the map contains a non-empty revision for |
| 115 | + `pendingWrite.remotePath`, use it directly and skip the `ReadFile` call. |
| 116 | +- Preserve the `ReadFile` fallback for the defensive case where the server |
| 117 | + returned no `results` entry or an empty revision (covers rolling deploys and |
| 118 | + future server-side bugs). |
| 119 | +- `contentType` resolution follows the same pattern: prefer the value from |
| 120 | + `BulkWriteResponse.Results`, fall back to `ReadFile` only if the bulk |
| 121 | + response did not supply one *and* the local snapshot lacks one. |
| 122 | +- The function's signature and the caller in the syncer loop do not change; |
| 123 | + only the internal branching changes. |
| 124 | + |
| 125 | +Net effect: the happy path goes from `POST /fs/bulk` + N × `GET /fs/file` down |
| 126 | +to a single `POST /fs/bulk`. |
| 127 | + |
| 128 | +## 6. Test updates |
| 129 | + |
| 130 | +### Server (`internal/httpapi/server_test.go`) |
| 131 | + |
| 132 | +Add a test (or extend an existing one in `TestBulkWriteEndpoint`) that: |
| 133 | + |
| 134 | +- Submits a bulk write with at least one writable file and one file that will |
| 135 | + fail (e.g. permission denied or invalid path). |
| 136 | +- Asserts `results` contains exactly the successful paths, each with a |
| 137 | + non-empty `revision` and the expected `contentType`. |
| 138 | +- Asserts the failed file is present in `errors` and **absent** from |
| 139 | + `results`. |
| 140 | +- Asserts `len(results) == written` and `len(errors) == errorCount`. |
| 141 | + |
| 142 | +### Mount (`internal/mountsync/syncer_test.go`) |
| 143 | + |
| 144 | +Tighten `TestBulkMigrationReducesHTTPCalls`: |
| 145 | + |
| 146 | +- Replace the `fsFilePostCount atomic.Int32` POST-only counter with a single |
| 147 | + counter (or path slice) that records **every** request whose path matches |
| 148 | + `/fs/file` regardless of method. |
| 149 | +- Assert that counter is `0` after the bulk sync completes. |
| 150 | +- Remove the now-dead `fsFilePostCount` declaration, the POST filter inside |
| 151 | + the handler, and the post-check log line that reports `GET /fs/file` |
| 152 | + read-backs (there should be none). |
| 153 | +- Keep the assertion that exactly one `POST /fs/bulk` was made. |
| 154 | + |
| 155 | +## 7. Deliberately out of scope |
| 156 | + |
| 157 | +- FUSE mount path (`cmd/relayfile-fuse`, `internal/fuse/...`) — unchanged. |
| 158 | +- SDK / TypeScript bindings — no wire-type regeneration in this change; JS |
| 159 | + consumers continue to ignore the extra field until a separate follow-up. |
| 160 | +- Observer / dashboard surfaces — they consume events, not the bulk response; |
| 161 | + no change. |
| 162 | +- Any change to `errors` shape, `correlationId` semantics, or the `forkID` |
| 163 | + branching logic. |
| 164 | + |
| 165 | +## 8. Acceptance gates |
| 166 | + |
| 167 | +All three must pass, exactly as written: |
| 168 | + |
| 169 | +```bash |
| 170 | +go test ./internal/httpapi/... -count=1 -timeout 120s |
| 171 | +go test ./internal/mountsync/... -count=1 -timeout 120s |
| 172 | +go build ./... |
| 173 | +``` |
| 174 | + |
| 175 | +BOUNDARY_READY |
0 commit comments