| 1 | # Bounded delegated local note editing |
| 2 | |
| 3 | Status: specification and final implementation/evidence approved by independent |
| 4 | GPT-6 Astra review on 2026-11-01. Local CI and race checks passed. Not deployed. |
| 5 | Extends the existing agent-access branch without changing v1 search/create wire |
| 6 | contracts. Existing authority, custody, guarded-response and roster requirements |
| 7 | remain binding. |
| 8 | |
| 9 | ## Operations |
| 10 | |
| 11 | `notes_get`, scope `notes:read`, takes `{team,id}` and returns |
| 12 | `{id,title,body,revision,url}`. IDs and revision are positive integers; revision |
| 13 | is the existing `Document.Revision`. It reads only locally authored paste notes |
| 14 | (browser, native or agent), identified by `source='paste'`, empty sibling item |
| 15 | coordinate and zero sibling revision. Other sources and missing/deleted rows |
| 16 | return `not_found`, even when they are searchable. No imported upload, connector |
| 17 | or sibling content becomes editable through this API. Notes remain team-shared; |
| 18 | there is no invented creator-only permission rule. |
| 19 | |
| 20 | `notes_update`, scope `notes:write`, takes |
| 21 | `{team,id,client_id,expected_revision,title,body}` and returns |
| 22 | `{id,revision,indexed,url}`. Title/body replace the complete note, with create's |
| 23 | 512-byte title and 1-MiB body bounds and existing trimming/nonblank rules. |
| 24 | `client_id` is a canonical nonzero UUID; `expected_revision` is a positive |
| 25 | integer. Unknown fields are refused. No delete, source move, custody change, |
| 26 | attachment edit, or partial text splice is introduced. |
| 27 | |
| 28 | ## Transaction and retries |
| 29 | |
| 30 | Reserve SQLite's writer before reading the target, authority or receipt. Check |
| 31 | the original delegated credential, Home person identity, current admission and |
| 32 | captured roster lease inside that transaction, before and after all work. |
| 33 | Validate the immutable receipt authority marker. Reuse the existing ingestion |
| 34 | implementation for sealing, chunk replacement, provenance and derived indexing, |
| 35 | with a private existing-document target bound to ID and external coordinate; |
| 36 | never derive a new key from the edited title and never recreate a deleted target. |
| 37 | |
| 38 | An additive receipt table stores a keyed coordinate over authority, subject, |
| 39 | client, team, operation and client UUID; a keyed fingerprint binds all supplied |
| 40 | fields including target and precondition. Resource update and completed receipt |
| 41 | (target, resulting revision) commit atomically. Same UUID with changed request |
| 42 | is `conflict`. For an identical completed retry, first recheck current identity, |
| 43 | write authority and exact live local-note coordinate, then return the original |
| 44 | receipt revision without applying old content or requiring the old precondition |
| 45 | to equal the latest revision. Later edits survive; deletion is `not_found`. |
| 46 | New requests with a stale precondition return `conflict` and change nothing. |
| 47 | An unresolved/malformed receipt fails closed instead of repairing by replay. |
| 48 | |
| 49 | For local paste notes, title changes must also advance Document.Revision; the |
| 50 | existing body-only counter cannot protect full-note replacement from concurrent |
| 51 | renames. Make this narrow domain correction for all paste ingestion writers; |
| 52 | sibling/import revision semantics stay unchanged. Byte-identical normalized |
| 53 | saves keep the existing revision. Body changes keep the ordinary chunk lifecycle. |
| 54 | |
| 55 | Indexing is postcommit derived work under WithoutCancel. A Store mutation lock |
| 56 | serializes ingest/delete/rebuild through their derived-state completion, so an |
| 57 | older refresh cannot overwrite a newer revision. A committed update |
| 58 | reports indexed false on index failure; a retry must never reindex request text |
| 59 | over a newer note. `indexed` describes completion of this receipt's indexing, |
| 60 | not proof the current revision is indexed. A reader obtains current state through |
| 61 | notes_get; the update's revision is explicitly the committed receipt revision. |
| 62 | |
| 63 | ## Response and evidence |
| 64 | |
| 65 | Capture ID, source, external coordinate, sibling-coordinate absence and revision |
| 66 | for reads. Before headers and each chunk, revalidate the current person/lease and |
| 67 | exact note coordinate/revision; suppress a body selected from an older revision. |
| 68 | Mutation receipts contain no title/body; response emission revalidates live local |
| 69 | custody and identity, while allowing a later revision to coexist with the receipt. |
| 70 | |
| 71 | Tests must prove create/get/update and browser/search effects; browser/native |
| 72 | paste support; imported and sibling refusal; title-only conflicts; simultaneous |
| 73 | updates with one winning precondition; replay after later edit/deletion; changed |
| 74 | payload conflicts; receipt/body/chunk rollback on injected failure; index failure |
| 75 | and recovery; authority/revocation while waiting and at final commit; response |
| 76 | identity/custody/revision loss including a multi-chunk body. Run app CI and race |
| 77 | tests. Independent review selects adversarial mutations; record model and evidence. |
| 78 | No merge, deployment, credentials or infrastructure changes are included. |
| 79 | |
| 80 | ## Independent review and mutation evidence — 2026-11-01 |
| 81 | |
| 82 | Independent GPT-6 Astra reviewed the spec and source. Source review required |
| 83 | explicit signed-counter overflow refusal; Calendar additionally rechecks mutation |
| 84 | write permission at emission, standing after database reads, and samples wall |
| 85 | time before the remaining-lease calculation. Those corrections are implemented. |
| 86 | Source approval and deployment status are separate from these test results. |
| 87 | |
| 88 | The independent reviewer selected these mutations; each was applied separately, |
| 89 | failed its behavioral assertion, and was reverted before final checks: |
| 90 | |
| 91 | - `stale-revision`: `TestAgentNoteEditLifecycle` — detected. |
| 92 | - `reapply-receipt`: `TestAgentNoteEditLifecycle` — detected. |
| 93 | - `custody`: `TestAgentNoteEditCustodyAndConcurrency` — detected. |
| 94 | - `final-authority`: `TestAgentNoteUpdateRetryFinalGuardWithoutResponse` — detected. |
| 95 | - `rename-revision`: `TestAgentNoteEditLifecycle` — detected. |
| 96 | - `old-index-publication`: `TestAgentNoteOlderRefreshCannotReplaceNewerIndex` — detected. |
| 97 | |
| 98 | The index ordering test holds an older refresh after it selects its document, |
| 99 | allows a concurrent writer to compete, and verifies the newest title is the only |
| 100 | one indexed. The socket-deadline test observes actual HTTP server deadlines |
| 101 | under a near-expiry roster lease. No mutation result rests on a compiler failure. |
| 102 | |
| 103 | Concurrent identical updates serialize on the existing agent-note mutex through |
| 104 | the final indexed acknowledgement. Lock order is agentNotes then Store.mutationMu; |
| 105 | ordinary ingestion takes only the latter. A replay cannot observe a transient |
| 106 | false acknowledgement while the original request is still completing indexing. |
| 107 | The reviewer requested a controlled pause before the indexed receipt write; the |
| 108 | replay waits, and both calls return the same immutable acknowledgement. |
| 109 | |
| 110 | Final evidence: `make ci` passes; `go test -race ./internal/memoria -run |
| 111 | "Agent|Ingest|Delete|Rebuild" -count=1` passes, followed after the acknowledgement |
| 112 | fix by the focused `AgentNoteConcurrentRetry|AgentNoteEdit|AgentNoteUpdate| |
| 113 | AgentNoteOlderRefresh` race selection and another full CI pass. A seventh mutation |
| 114 | omitting agentNotes serialization fails the controlled concurrent-replay test |
| 115 | with the original transient indexed=false acknowledgement. Final independent |
| 116 | review approved that correction. |
| 117 | |