Skip to content

backup: durable intents for pairing, pin and deposit (§9.3) - #25

Merged
Yoshiofthewire merged 12 commits into
mainfrom
kyguard/intents
Nov 25, 2026
Merged

Yoshiofthewire merged 12 commits into
mainfrom
kyguard/intents

Conversation

@Yoshiofthewire

@Yoshiofthewire Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

In one line: KyRecovery pairing claims, recovery-key pins and deposits now leave a durable record before they start. A crash or a failed audit write can no longer lose what happened.

What changed

  • A new operation_intents table (migration 16). A pairing claim, a pin and a deposit each commit a pending row before the effect starts.
  • The action's audit record is the completion append, in the same transaction that ends the intent (complete, failed or outcome_unknown). The record is noted on the intent first. So when the append fails, the action answers 500 instead of success, and the record is appended later. Nothing is only logged any more (DECISIONS 19 replaced).
  • Startup and every deposit (under the backup-run lock) settle pending deposits first. Pairing and pin settle intents older than 20 minutes. A settled intent ends at its noted outcome, else outcome_unknown with a critical operation.outcome_unknown record.
  • A later successful deposit supersedes outcome_unknown deposits. Every run seals a fresh capsule, so lost bytes are never replayed.
  • A KyRecovery pairing whose answer was lost ends outcome_unknown (the code may have been spent).
  • Settings publication: the exempt-network list and its audit record commit in one transaction. Backup export sends no capsule bytes without its record. Unpair and schedule answer 500 when their record fails.

Review this

  • internal/store/intents.go: Settle and Reconcile. The note-then-settle order is what keeps a failed append from being lost, and settle is the supersede rule.
  • internal/backup/run.go: where the deposit intent is taken (after collecting, only when paired) and how the run's outcome maps to a state.
  • internal/api/backup_handlers.go: the pairing and pin handlers. Each failure picks failed or outcome_unknown.

Checked: full suite with -race, plus the FIPS profile, on SQLite and PostgreSQL 17 at 7716dec. All 8 mutations below fail their tests.
Not checked: the e2e compose suite locally (Major has no free Docker address pool; it runs in CI).
Decided for you: an extra failed state and an outcome column beyond the spec's states (DECISIONS 19b). A 20-minute staleness bound for pairing and pin, which hold no shared lock (19a).
Review fixes (fa9213a): a noted outcome is authenticated (HMAC over operation ID, kind, subject, state, record) before reconciliation replays it, and a failing note ends outcome_unknown with operation.outcome_rejected and is never appended (security P2). Settling is one conditional UPDATE, and reconciliation moves only pending intents, so concurrent reconcilers append once (Guide). Checked at fa9213a: race and FIPS suites on SQLite (k83) and PostgreSQL 17 (k82), exit 0.
You need to: review. Held unmerged by rule. At the stack join, drop the duplicate operation_intents table from the broker stack's migration 100.

§13 Phase 0 checklist: Chained audit append on both drivers, transaction rules, fixed inherited paths (§9.3): the transaction rules and inherited paths that #19 left. Acceptance 12, the external-effect clause.

Detail

Mutation table (each mutation makes the named test fail)

# Property Mutation Failing test
I1 A deposit has a durable intent before it leaves skip Begin in the collect callback TestStartupReconcilesADepositLostInACrash
I2 A retry reconciles the lost deposit first drop the reconcile at the start of backup.Run TestRetriedDepositReconcilesFirstAndSupersedes
I3 A later success supersedes unknown deposits skip the supersede update TestReconcileEndsLostDepositsAndASuccessSupersedesThem
I4 A failed completion append is replayed from the note note on a state no row has TestFailedCompletionAppendIsReplayedFromTheIntent
I5 Startup settles pending deposits skip the deposit reconcile in ReconcileIntents TestStartupReconcilesADepositLostInACrash
I6 A pin whose record fails answers 500 ignore the settle error TestPinKeySettlesItsIntent
I7 A lost pairing answer ends outcome_unknown settle it failed TestLostPairingClaimEndsOutcomeUnknown
I8 Settings and their record commit together drop the append from UpdateSettings TestExemptNetworksRollBackWithTheirAudit
I9 A forged note is never replayed skip the MAC check TestReconcileRejectsAForgedNote (edited and written)
I10 Concurrent reconcilers append once reconcile from outcome_unknown too TestConcurrentReconcilersSettleOnce (30 records for 10; both drivers)

All run on K hosts (k83), SQLite.

Checks at 7716dec

Stack

#1 → #3 → #7 → #17 → #18 → #19 → this.

Stack merge (hawthorn fix-auth-3): head 9dd679a merges kyguard/audit-chain (plain merge commits, no rebase; 005efec merged it first, and 9dd679a adds a content-free merge of the pushed commit below), so this PR carries #7's fail-closed SSO sign-in (9220dd7) and every fix below it. The only conflicts were in docs/DECISIONS.md, and both sides were kept. Go vet passes at this head; CI runs the suites.

Round 4 (hawthorn): Guide "Unreconciled Intents" and "Lost Outcome"

  • Lost Outcome (e2fee3e): a reconciler settles an intent only if its outcome is still the note it read (AND outcome = ? on the conditional UPDATE). A note written after the read leaves the intent pending, and the next pass replays it instead of stranding it as outcome_unknown.
  • Unreconciled Intents (e2fee3e): the elected scheduled worker calls backup.ReconcileStale every tick. It settles pairing and pin intents older than StaleIntent, so an intent too young at startup no longer waits for another request of its kind. Startup uses the same function. DECISIONS 19a is updated (0e0092d).
Property Mutation Test Result
A stale reconciler cannot end a just-noted intent unknown drop the outcome = ? condition TestReconcilerDoesNotStrandANoteWrittenAfterItsRead fails (stale reconciler settled the noted intent)
A young pairing or pin intent is settled later without a new request drop ReconcileStale from the worker tick TestBackupLoopReconcilesStalePairingAndPinIntents fails (intent still pending)

Affected packages (store, backup, cmd/server, api) pass with -race on k82. Also merges kyguard/audit-chain (main, #17, #18).

Round 4b (hawthorn): joined with #8's operation_intents

  • main now has Broker listener, CA and pairing (§4.1) #8's migration 100, which creates operation_intents. This PR's migration 16 created the same table, so a fresh database would refuse to start. Migration 16 is gone. Migration 200 (kyguard_intents_outcome, in migrations/kyguard_intents.go) rebuilds the table, keeping its rows, as the superset this PR needs: outcome, the failed state and a (kind, state) index. SQLite cannot change a CHECK in place, hence the rebuild. Versions from 200 keep the auth stack clear of the broker stack's 100+.
  • Both stacks used kind = 'pairing': a broker pairing in Broker listener, CA and pairing (§4.1) #8, a KyRecovery pairing claim here. Each reconciler would have settled the other's intents (Broker listener, CA and pairing (§4.1) #8's startup sweep runs on pairing). The KyRecovery claim is now recovery_pairing.
  • The two stacks' duplicate IntentPending and IntentComplete constants are now one set, and IntentOutcomeUnknown stays as an alias.
  • TestRegistryIsAscending fails if file-order init appends ever put a lower version after a higher one. Note for the reporting stack's join: kyguard_reporting.go sorts after kyguard_intents.go, so its 102 and 103 would land after 200 and this test will flag it.
  • DECISIONS 19d records all of this.
Property Mutation Test Result
A KyRecovery pairing claim is its own kind BeginIntent(..., "pairing", ...) in the handler TestLostPairingClaimEndsOutcomeUnknown fails
The worker settles KyRecovery pairings, not broker ones ReconcileStale on pairing TestBackupLoopReconcilesStalePairingAndPinIntents fails
Neither reconciler ends the other's intents — (pins the design) TestRecoveryAndBrokerPairingIntentsStaySeparate passes

Checks: race + FIPS on SQLite and PostgreSQL 17, at the bottom (#18 @ bd2fb58: SQLite k81 exit 0, PostgreSQL own container exit 0) and the top (#28 @ 0cb6d83: SQLite k82 exit 0, PostgreSQL own container exit 0). Affected packages were also run at this head on k82.

Round 4c (hawthorn): security P2 and Guide "Lost Completion"

  • Security (P2, internal/store/intents.go) (9763317): a noted outcome can be replayed into the chain only once. Completion records now start with operation_id=<id>. Replaying a note checks, in the settling transaction, for a completion record of that operation already in the chain, and if one exists it ends the intent outcome_rejected (reason=note_consumed). Every settle clears the note in the same transaction as its record.
  • Guide, Lost Completion (9763317): the owning process notes its outcome even on an intent already reconciled outcome_unknown, and reconciliation replays noted unknown intents, so a failed append there is no longer lost. A reconciler now settles only from the exact state it read, so two concurrent reconcilers still append once.
Property Mutation Test Result
A consumed note is not replayed again skip the consumed check on replay TestAConsumedNoteIsNotReplayedAgain fails (2 deposit records)
A note on an unknown intent survives a failed append note only pending intents TestANoteOnAnUnknownIntentIsReplayed fails (stays outcome_unknown)
Concurrent reconcilers append once (existing) TestConcurrentReconcilersSettleOnce passes; it caught a first draft that matched either state

Checks: race + FIPS, SQLite and PostgreSQL 17, at the bottom (#19 @ 896dc31: SQLite on Major with every K host busy, exit 0; PostgreSQL own container, exit 0) and the top (#28 @ 5909f87: SQLite k81 exit 0, PostgreSQL own container exit 0).

🤖 Generated with Claude Code

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 5 🔵🔵🔵🔵🔵

🧪 PR contains tests

⚡ No major issues detected


Advisory only: this comment never gates the pull request.
gpt-6.1-sol using pr-agent on behalf of Yoshi

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Security review - cleared

Detail withheld: this repository is public, and posting an unfixed finding here would be public disclosure. The full report was posted to the hand-off board at https://myslop.urlxl.us/f/pr-reviewer-busnes-app-kyguard-server-25, where it is readable by the operator and expires seven days after the last post.


gpt-6.1-sol using security-audit on behalf of Yoshi

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Autonomous review - 1/1 personas cleared

  • security: cleared

Merging remains a human decision; this bot never approves.


gpt-6.1-sol using security-audit on behalf of Yoshi

Spec §9.3: an external effect commits an operation_intents row before it
starts, and its audit record is the completion append in the same
transaction that ends the intent. The record is noted on the intent
first, so a failed append fails the action (500, ErrNotRecorded) and is
replayed when the intent is reconciled; nothing is only logged.

Startup and every deposit (under the backup-run lock) settle pending
deposits; pairing and pin settle intents older than 20 minutes. A
pending intent ends at its noted outcome or outcome_unknown, and a
later successful deposit supersedes unknown ones. A lost KyRecovery
pairing answer ends outcome_unknown.

Settings publication commits the exempt-network list with its record;
backup export sends no capsule without its record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Security review P2: reconciliation replayed a persisted outcome no one
authenticated, so a database writer could have a forged record
appended under the chain key. Notes now carry an HMAC binding operation
ID, kind, subject, state and record; a failing note ends
outcome_unknown with operation.outcome_rejected and is never appended.

PR Reviewer Guide: settling is one conditional UPDATE, and
reconciliation moves only pending intents, so concurrent reconcilers
append one record per intent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Fixed in fa9213a. Security P2: noted outcomes carry an HMAC (key derived from the audit key) binding operation ID, kind, subject, state and the exact record; reconciliation replays only authenticated notes, and a failing one ends outcome_unknown with operation.outcome_rejected without appending the forged record (TestReconcileRejectsAForgedNote). Guide (concurrent settlement): settling is one conditional UPDATE … RETURNING, and reconciliation moves only pending intents (TestConcurrentReconcilersSettleOnce, both drivers). Each fails under its mutation.

Yoshiofthewire and others added 5 commits November 25, 2026 09:29
… into kyguard/intents

Drops audit-chain's "scope of this PR" decision 19: this PR builds what it deferred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Picks up the stack's merge commits as pushed. No content change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A reconciler settles only if the note it read is unchanged, so a note
written after its read stays pending and is replayed, not stranded as
outcome_unknown. The elected worker reconciles pairing and pin intents
older than StaleIntent each tick, so one too young at startup is not left
pending until the next request of its kind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
Brings #17's step-up retry and #18/#25 fixes up. web/dist rebuilt from
the merged sources with Node 26.13.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire and others added 4 commits November 25, 2026 16:09
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Joins this stack's operation_intents with the one #8's merged migration
100 creates. Migration 16 is gone; migration 200 rebuilds the table as
the superset (outcome, failed, (kind, state) index), rows kept. The
KyRecovery pairing intent is kind recovery_pairing, so the broker sweep
and this reconciler never settle each other's intents. A test fails on
a registry out of version order. DECISIONS 19d.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The broker stack and this one each declared IntentPending and
IntentComplete. store.go keeps the states; IntentOutcomeUnknown stays as
the broker's alias of IntentUnknown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
Migration 17 (cli_login_codes) stays; migration 16 is gone (#25 moved
the intents table to migration 200). migrate_postgres keeps both lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire
Yoshiofthewire changed the base branch from kyguard/audit-chain to main November 25, 2026 23:16
Security review: a writer without the audit key could reset a settled
intent and replay its still-valid note into a second success record
(and, for a deposit, supersede unknown deposits). Completion records
now name their operation first; replaying a note checks, in the
settling transaction, for a completion record of that operation in the
chain and ends outcome_rejected (note_consumed) if one exists. Every
settle clears the note with its record.

Guide (Lost Completion): the owning process notes its outcome on an
intent reconciled outcome_unknown too, and reconciliation replays noted
unknown intents, so a failed append there is not lost. A reconciler
settles only from the state it read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant