# Bounded delegated local note editing

Status: specification and final implementation/evidence approved by independent
GPT-6 Astra review on 2026-11-01. Local CI and race checks passed. Not deployed.
Extends the existing agent-access branch without changing v1 search/create wire
contracts. Existing authority, custody, guarded-response and roster requirements
remain binding.

## Operations

`notes_get`, scope `notes:read`, takes `{team,id}` and returns
`{id,title,body,revision,url}`. IDs and revision are positive integers; revision
is the existing `Document.Revision`. It reads only locally authored paste notes
(browser, native or agent), identified by `source='paste'`, empty sibling item
coordinate and zero sibling revision. Other sources and missing/deleted rows
return `not_found`, even when they are searchable. No imported upload, connector
or sibling content becomes editable through this API. Notes remain team-shared;
there is no invented creator-only permission rule.

`notes_update`, scope `notes:write`, takes
`{team,id,client_id,expected_revision,title,body}` and returns
`{id,revision,indexed,url}`. Title/body replace the complete note, with create's
512-byte title and 1-MiB body bounds and existing trimming/nonblank rules.
`client_id` is a canonical nonzero UUID; `expected_revision` is a positive
integer. Unknown fields are refused. No delete, source move, custody change,
attachment edit, or partial text splice is introduced.

## Transaction and retries

Reserve SQLite's writer before reading the target, authority or receipt. Check
the original delegated credential, Home person identity, current admission and
captured roster lease inside that transaction, before and after all work.
Validate the immutable receipt authority marker. Reuse the existing ingestion
implementation for sealing, chunk replacement, provenance and derived indexing,
with a private existing-document target bound to ID and external coordinate;
never derive a new key from the edited title and never recreate a deleted target.

An additive receipt table stores a keyed coordinate over authority, subject,
client, team, operation and client UUID; a keyed fingerprint binds all supplied
fields including target and precondition. Resource update and completed receipt
(target, resulting revision) commit atomically. Same UUID with changed request
is `conflict`. For an identical completed retry, first recheck current identity,
write authority and exact live local-note coordinate, then return the original
receipt revision without applying old content or requiring the old precondition
to equal the latest revision. Later edits survive; deletion is `not_found`.
New requests with a stale precondition return `conflict` and change nothing.
An unresolved/malformed receipt fails closed instead of repairing by replay.

For local paste notes, title changes must also advance Document.Revision; the
existing body-only counter cannot protect full-note replacement from concurrent
renames. Make this narrow domain correction for all paste ingestion writers;
sibling/import revision semantics stay unchanged. Byte-identical normalized
saves keep the existing revision. Body changes keep the ordinary chunk lifecycle.

Indexing is postcommit derived work under WithoutCancel. A Store mutation lock
serializes ingest/delete/rebuild through their derived-state completion, so an
older refresh cannot overwrite a newer revision. A committed update
reports indexed false on index failure; a retry must never reindex request text
over a newer note. `indexed` describes completion of this receipt's indexing,
not proof the current revision is indexed. A reader obtains current state through
notes_get; the update's revision is explicitly the committed receipt revision.

## Response and evidence

Capture ID, source, external coordinate, sibling-coordinate absence and revision
for reads. Before headers and each chunk, revalidate the current person/lease and
exact note coordinate/revision; suppress a body selected from an older revision.
Mutation receipts contain no title/body; response emission revalidates live local
custody and identity, while allowing a later revision to coexist with the receipt.

Tests must prove create/get/update and browser/search effects; browser/native
paste support; imported and sibling refusal; title-only conflicts; simultaneous
updates with one winning precondition; replay after later edit/deletion; changed
payload conflicts; receipt/body/chunk rollback on injected failure; index failure
and recovery; authority/revocation while waiting and at final commit; response
identity/custody/revision loss including a multi-chunk body. Run app CI and race
tests. Independent review selects adversarial mutations; record model and evidence.
No merge, deployment, credentials or infrastructure changes are included.

## Independent review and mutation evidence — 2026-11-01

Independent GPT-6 Astra reviewed the spec and source. Source review required
explicit signed-counter overflow refusal; Calendar additionally rechecks mutation
write permission at emission, standing after database reads, and samples wall
time before the remaining-lease calculation. Those corrections are implemented.
Source approval and deployment status are separate from these test results.

The independent reviewer selected these mutations; each was applied separately,
failed its behavioral assertion, and was reverted before final checks:

- `stale-revision`: `TestAgentNoteEditLifecycle` — detected.
- `reapply-receipt`: `TestAgentNoteEditLifecycle` — detected.
- `custody`: `TestAgentNoteEditCustodyAndConcurrency` — detected.
- `final-authority`: `TestAgentNoteUpdateRetryFinalGuardWithoutResponse` — detected.
- `rename-revision`: `TestAgentNoteEditLifecycle` — detected.
- `old-index-publication`: `TestAgentNoteOlderRefreshCannotReplaceNewerIndex` — detected.

The index ordering test holds an older refresh after it selects its document,
allows a concurrent writer to compete, and verifies the newest title is the only
one indexed. The socket-deadline test observes actual HTTP server deadlines
under a near-expiry roster lease. No mutation result rests on a compiler failure.

Concurrent identical updates serialize on the existing agent-note mutex through
the final indexed acknowledgement. Lock order is agentNotes then Store.mutationMu;
ordinary ingestion takes only the latter. A replay cannot observe a transient
false acknowledgement while the original request is still completing indexing.
The reviewer requested a controlled pause before the indexed receipt write; the
replay waits, and both calls return the same immutable acknowledgement.

Final evidence: `make ci` passes; `go test -race ./internal/memoria -run
"Agent|Ingest|Delete|Rebuild" -count=1` passes, followed after the acknowledgement
fix by the focused `AgentNoteConcurrentRetry|AgentNoteEdit|AgentNoteUpdate|
AgentNoteOlderRefresh` race selection and another full CI pass. A seventh mutation
omitting agentNotes serialization fails the controlled concurrent-replay test
with the original transient indexed=false acknowledgement. Final independent
review approved that correction.
