| 1 | # Delegated range editing |
| 2 | |
| 3 | Status: specification and final source independently approved by a separate |
| 4 | GPT-6 Astra agent, 2026-11-01. Validation is recorded below. |
| 5 | Author: GPT-6 Astra, 2026-11-01. Paul authorized everyday editing APIs through |
| 6 | the suite's existing API, CLI and MCP connection. This extends the approved |
| 7 | 2026-10-25 agent-access design; its identity, namespace, privacy, roster and |
| 8 | response-emission requirements remain binding. Existing v1 operations and |
| 9 | their response shapes do not change. |
| 10 | |
| 11 | ## Contract |
| 12 | |
| 13 | Add `sheets_set_range`, scope `sheets:write`, readOnly false, idempotent true. |
| 14 | The same registry descriptor serves HTTP, CLI and MCP, independent of the |
| 15 | configured team. All six top-level fields below are required; unknown |
| 16 | top-level and cell fields are refused. |
| 17 | |
| 18 | ```json |
| 19 | { |
| 20 | "team": "team-id", |
| 21 | "sheet_id": "42", |
| 22 | "tab_id": "43", |
| 23 | "expected_revision": "17", |
| 24 | "client_id": "1dc98332-7165-43c5-8f98-9546f10146a8", |
| 25 | "cells": [ |
| 26 | {"row": 0, "column": 0, "raw": "21"}, |
| 27 | {"row": 0, "column": 1, "raw": "=A1*2"} |
| 28 | ] |
| 29 | } |
| 30 | ``` |
| 31 | |
| 32 | Sheet and tab IDs retain their canonical positive decimal string form, at |
| 33 | most MaxInt64. Revision is the existing tab snapshot's positive decimal |
| 34 | next-stamp watermark, also at most MaxInt64; never a JSON number or a cell's |
| 35 | last-written stamp. Leading zeros, signs, fractions and overflow are invalid. |
| 36 | The nonzero UUID must be its canonical lowercase hyphenated spelling. |
| 37 | Team is 1–128 characters and must equal the instance's configured team. |
| 38 | |
| 39 | `cells` is an ordered sparse batch of 1–500 distinct coordinates, with rows |
| 40 | 0–4999, columns 0–599 and raw input at most 8192 UTF-8 bytes per cell. The |
| 41 | operation updates exactly those cells; omitted coordinates are unchanged. |
| 42 | An empty raw string blanks a cell. Formula text uses the current store |
| 43 | parser and recalculation engine, with exactly `SetBatch` semantics. This is |
| 44 | literal raw input, without clipboard reference translation. Preserve input |
| 45 | order, since it determines native batch stamps and intermediate evaluation. |
| 46 | Malformed Unicode, invalid formulas as judged by `ValidateCellWrites`, |
| 47 | duplicate coordinates and requests above 1 MiB are refused before writing. |
| 48 | Existing store limits, including the 5000-cell recalculation cap and maximum |
| 49 | cells per tab, still refuse the entire batch. |
| 50 | |
| 51 | Success is a small acknowledgement, never a cached range: |
| 52 | |
| 53 | ```json |
| 54 | {"sheet_id":"42","tab_id":"43","revision":"19","url":"https://sheets.example/sheets/42"} |
| 55 | ``` |
| 56 | |
| 57 | All four response fields are required. `revision` is the immutable next-stamp |
| 58 | watermark immediately after this mutation committed. A completed retry |
| 59 | returns that same revision, even if later browser edits advanced the tab. |
| 60 | Read `sheets_get_range` for current cells and current revision. Existing |
| 61 | `sheets_create` retry semantics remain unchanged. Output is bounded by the |
| 62 | existing 1 MiB limit before commit. |
| 63 | |
| 64 | `conflict` covers a new request against a stale revision, or reuse of a |
| 65 | client UUID with different target, expected revision, cell order or content. |
| 66 | The caller re-reads and chooses a new mutation UUID after a stale revision; |
| 67 | the service never rebases or automatically retries an edit at a newer |
| 68 | revision. Invalid input, unavailable storage/authority, unauthorized scope |
| 69 | and missing/inaccessible targets retain the existing error vocabulary. |
| 70 | Authority is checked before revealing a revision conflict. |
| 71 | |
| 72 | ## Authority and visibility |
| 73 | |
| 74 | Separate the registry's creation-membership requirement from `sheets:write`: |
| 75 | only create needs creator eligibility. Range editing requires current |
| 76 | `SheetEditor` reach to the existing sheet and a tab belonging to that sheet. |
| 77 | Use existing explicit-grant precedence, invalid-grant refusal, deletion and |
| 78 | filling predicates. A viewer cannot edit. Independent editor shares continue |
| 79 | to work under NotMember or Unknown exactly as browser writes do. A self-grant |
| 80 | or team rung still needs Member from the request's one retained Access. |
| 81 | |
| 82 | At writer reservation and immediately before commit, reverify only the |
| 83 | original delegation verifier, require unchanged subject/client/grant/team/ |
| 84 | scope, resolve the keyed Home link through `ResolveOn` on the caller's |
| 85 | transaction, compare stable local person ID and address, and check current |
| 86 | sheet editor reach and tab ownership on that transaction. Never invoke a |
| 87 | HomeOnly fallback, create a person, refresh the roster under the writer or |
| 88 | query through the outer database pool. Both fresh writes and completed |
| 89 | receipt retries pass these checks and the existing team write guard. |
| 90 | |
| 91 | Remember the target sheet for the existing final/per-chunk response checks; |
| 92 | the mutation acknowledgement additionally requires current editor reach at |
| 93 | emission, including an intervening editor-to-viewer downgrade. Preserve the |
| 94 | retained-lease socket deadline clamp. A failed emission after a committed |
| 95 | write is recoverable using the same UUID; it cannot roll back a commit. |
| 96 | |
| 97 | ## Native transaction and cache discipline |
| 98 | |
| 99 | Extend the store's existing `SetBatch` seam with a bounded conditional batch |
| 100 | entrypoint; keep existing browser callers and their behavior unchanged. |
| 101 | The canonical handle mutex covers the revision decision, transaction and |
| 102 | post-commit cache adoption. One SQLite writer transaction contains the |
| 103 | namespace check/reservation, current authority, receipt decision, precondition, |
| 104 | all native SetBatch input/cache/event writes, receipt/attribution and final |
| 105 | authority check. Reserve the writer before reading the transactional state. |
| 106 | |
| 107 | Store code owns the lock and transaction; app-owned callbacks receive that |
| 108 | transaction for authorization and receipts. They must not call `store.Open`, |
| 109 | another locking handle method or an outer-pool lookup while the lock/writer |
| 110 | is held. The native batch implementation is factored and reused, not copied |
| 111 | into the adapter. A fresh unregistered staged handle is built on the writer |
| 112 | transaction. Its next stamp is at least the canonical handle's watermark, |
| 113 | matching existing SetBatch recovery semantics. Compare expected_revision to |
| 114 | that authoritative current watermark before allocating coordinates or applying |
| 115 | inputs. No staged mutation reaches the canonical handle on any rollback. |
| 116 | |
| 117 | After a successful fresh commit, adopt the staged graph, raw values, axes, |
| 118 | keys and next stamp into the canonical handle exactly as SetBatch does. |
| 119 | Release the lock before app-level reads. Publish the native complete batch |
| 120 | report (including derived cells, actor, final stamp and stamp count) to the |
| 121 | existing browser live channel only after commit, and touch the sheet's list |
| 122 | activity timestamp using the existing behavior. A completed retry publishes |
| 123 | no duplicate report, consumes no stamps and creates no new native events. |
| 124 | Never report a truncated calculation as a successful edit. |
| 125 | |
| 126 | ## Durable retries and attribution |
| 127 | |
| 128 | Add an additive edit-receipt table, separate from creation receipts. Its |
| 129 | unique coordinate is an HMAC over the operation name, both authority issuers, |
| 130 | team, subject, delegated client and client UUID. Its content fingerprint |
| 131 | includes target IDs, expected revision and the exact ordered decoded cell |
| 132 | values. No raw cells or formulas are stored in receipts or logs. Store target |
| 133 | IDs, the immutable applied revision and the authenticated local member ID; |
| 134 | seal the recoverable delegated client attribution with a receipt-specific |
| 135 | purpose. The native events continue to name their real local actor; the |
| 136 | receipt's tab-filtered first/last native input-event IDs tie this edit to that |
| 137 | client. Capture the range around SetBatch under the reserved writer and require |
| 138 | one input event per requested cell. Preserve it on replay. Native anonymous |
| 139 | recalc markers remain unchanged; no new correlation is claimed for those rows. |
| 140 | |
| 141 | The existing immutable key/issuer/team marker must refuse adoption when |
| 142 | either creation receipts or edit receipts exist without its binding. All |
| 143 | receipt and attribution writes share the cell transaction. Completed receipt |
| 144 | lookup precedes the expected-current-revision comparison, after current |
| 145 | authority, so retries still succeed following unrelated later edits. A |
| 146 | payload mismatch conflicts and never changes the stored receipt. A failed |
| 147 | transaction leaves no receipt and no partial input/cache/event/axis changes. |
| 148 | Concurrent identical submissions result in one edit and one acknowledgement. |
| 149 | Reopening the database and app preserves replay behavior. |
| 150 | |
| 151 | ## Review and validation gates |
| 152 | |
| 153 | Independent highest-capability spec approval precedes implementation and |
| 154 | independent source/evidence review follows it. Required meaningful gates: |
| 155 | |
| 156 | - Real linked-account HTTPS grant/gateway/MCP read-edit-read; compare raw and |
| 157 | calculated formulas, blanking, events and native UI-store reads. Observe |
| 158 | the complete live report after commit and none on refusal or replay. |
| 159 | - Two editors starting from one revision: exactly one succeeds, the other |
| 160 | conflicts without overwriting. Exact retry after a later native edit returns |
| 161 | the original revision and preserves that edit, including after app restart. |
| 162 | - Duplicate concurrent first requests, changed payload/target/order/revision, |
| 163 | foreign tab, viewer and invalid explicit grant; editor share under removed |
| 164 | or Unknown membership, and team-rung lease expiry. |
| 165 | - Real SQL-trigger failures after partial native writes or receipt insertion |
| 166 | roll back cells, caches, events, axes, receipt and attribution; canonical |
| 167 | cached handle and a freshly reopened handle agree afterward. |
| 168 | - Initial and final token revoke, keyed-link corruption, unlink and editor |
| 169 | downgrade, tested at the domain method independently of emission. Direct |
| 170 | completed-retry final guard tests prevent an HTTP guard masking omissions. |
| 171 | - Revision boundaries, batch/parser/recalculation limits, malformed Unicode, |
| 172 | request/output bounds, immutable namespace and raw-database privacy checks. |
| 173 | - Add the delegated path to `TestTeamEveryWriteRouteRefusesALapseBeforeCommit` |
| 174 | or its shared enumerated helper with equivalent transaction assertions. |
| 175 | - Effective runtime-failing mutants independently bypass revision comparison, |
| 176 | receipt payload comparison, final writer guard, transaction-local editor |
| 177 | permission, receipt persistence, and rollback-safe cache adoption. Restore |
| 178 | every mutant; focused race and the full pinned `make test` gate must pass |
| 179 | on the restored final tree. |
| 180 | |
| 181 | This slice changes cell inputs only. Formatting, row/column structure, tabs, |
| 182 | sharing, deletion and imports retain their existing surfaces. It carries no |
| 183 | merge, canary or deployment approval. |
| 184 | |
| 185 | ## Independent review record |
| 186 | |
| 187 | On 2026-11-01 a separate GPT-6 Astra agent approved this specification before |
| 188 | implementation, then independently reviewed the final source and selected |
| 189 | adversarial mutations. Final source approval covers this bounded editing |
| 190 | operation and its native transaction/authority integration. It does not approve |
| 191 | main merge, canary behavior or deployment. |
| 192 | |
| 193 | The reviewer also approved the additive first/last input-event receipt range: |
| 194 | only this tab's native input events, with an exact per-cell count and real |
| 195 | local actor assertions. Seven selected mutants failed at runtime: missing |
| 196 | revision check, repeated edit on retry, missing final writer check, cache |
| 197 | adoption after rollback, omitted native recalculation, missing editor authority |
| 198 | at emission, and swallowed batch-apply failure. Every original was restored. |
| 199 | |
| 200 | Final restored-tree validation on 2026-11-01 passed with Go 1.26.5: |
| 201 | `GOTOOLCHAIN=go1.26.5 make test` (including store/live race checks), followed |
| 202 | by `GOTOOLCHAIN=go1.26.5 go test -race ./internal/sheets -run |
| 203 | 'TestAgentRangeEdit|TestAgentGatewayToRealSheets|TestTeamEveryWrite' -count=1 |
| 204 | -timeout 240s` (22.207s). The full gate also checked the additive migration |
| 205 | against the generated schema. The real HTTPS CLI/MCP integration covers the |
| 206 | new operation and its calculated read result. |
| 207 | |