oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

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