Skip to content

feat: retire a signed-out session key (POST /session/retire) - #678

Merged
samgbafa merged 11 commits into
mainfrom
feat/tc-3443-session-retire
Nov 21, 2026
Merged

samgbafa merged 11 commits into
mainfrom
feat/tc-3443-session-retire

Conversation

@samgbafa

@samgbafa samgbafa commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Sign-out option B, "retire the key", node half. Details: TC-3443. Draft until Bobbie's PASS. #677 (TC-1500) is merged; main is merged into this branch (a42ea3c) with both batch Existing arms kept and check_holder before the retire check in save_owner_renewal_lease.

What changes

  • POST /session/retire. The header is Authorization: Bearer <UCAN>, issued by the session did:key to this node's DID. It must carry exactly {"urn:tinycloud:session:self":{"tinycloud.session/retire":[{}]}}, no prf, a nonce, and exp <= now + 120 s (nbf allows 30 s skew). The signature is verified offline.
    • Optional JSON body {"delegations": ["<delegation>", ...]}: the session's root delegations, each encoded exactly as POST /delegate takes it in its Authorization header; at most 8, body at most 512 KiB, unknown fields refused. The client must send every root it holds for the session key (all spaces, including any signed but never posted). Each must be to the retiring DID, or the whole request is refused. Each is admitted exactly as /delegate admits it (Policy/v3 admission, signature, precheck with check_holder, then process_with) inside the retire's own write transaction under the DID lock, so the row covers it and posting it later cannot bring the key back (N2). Any refusal answers 401 session_retire_invalid: delegation <i>: <reason> and nothing is written.
    • (L2) A carried root that has expired is skipped (it can never be posted) and its index returned in "skipped". One not valid yet (nbf ahead) gets every check but its time window (signature, issuer standing, capability rules, holder), is not stored, and the row lasts at least until it expires.
    • (L1) Locks are taken as transact_verified takes them: chain guards, in-process space sequence guards (TC-732), the SQLite writer lock, then in the transaction the space sequence locks, the key's lock, and the originals' renewal locks. /delegate takes a space's sequence lock before the key's lock too, so the two never deadlock.
    • 200 {"did","retiredAt","recorded","expiresAt","skipped"}. A repeat call answers the first retiredAt and the stored expiresAt. "recorded": false (no expiresAt) means this node holds no live grant for the key and none was carried, so nothing was written (F2). skipped is omitted when empty.
    • 401 session_retire_invalid: <reason>.
    • 429 once one IP sends more than session_retire.rate_limit_per_minute (default 10) requests a minute. The limiter is only a throttle: it trusts the IP Rocket reports (X-Real-IP), and its per-IP map never evicts (pre-existing, not fixed here). The live-grant gate is what bounds the table.
    • 503 session_retire_unavailable: … on a database error.
  • Storage. A new session_retire(did, retired_at, expires_at) table and a nullable delegation.first_seen_at, set when this node first stores a delegation. A resubmitted delegation keeps its first time. Legacy rows stay NULL and count as stored before any retire.
  • What a retire refuses (all 401 session_retired: …). The key can no longer use any delegated authority. Authority it holds as the owner of a space or network is unaffected (root authority is checked first):
    • the key's invocations under delegated authority;
    • a delegation it signs that this node had not stored before the retire. This is checked on every new-delegation path: the pool precheck, validate_with (single /delegate, batch process_with, Policy/v3 roots and sessions), and save_owner_renewal_lease. A delegation already stored, resubmitted singly or in a batch, still answers as already registered;
    • at invoke, a chain delegation from the key first seen after the retire (the race window);
    • (C2) renewal-lease registration, and new renewal stamps, for a delegation the key holds.
  • Not refused: delegations the key made before sign-out, and their descendants. Sign-out does not end those; Revoke does. The owner can still post a session root.
  • (C1) Lifetime. A row lasts at least session_retire.ttl_secs (default 30 days; 0 refuses to start), and until the latest end among the delegations, renewal stamps (new_end) and active renewal leases (lease_expiry) this node stores for the key. A grant with no expiry keeps it for good. A delegation to the key, or a lease on an original it holds, stored after the retire raises the row's expiry; nothing lowers it. Lookups ignore expired rows, which are pruned on each retire call and on the invocation-replay cleanup tick.
  • (N1) No race. The retire (prune, live-grant check, end computation, insert or raise) is one write transaction. On SQLite it holds the database write lock. On PostgreSQL it takes a per-DID advisory lock, then the renewal lock of each original the key holds (the lock stamp issuance takes); delegation::save takes the same DID lock for a did:key delegatee inside its existing actor-insert statement, so registration gains no round trip.
  • Cost. The invoke-time retire lookup rides the existing auth-graph snapshot query: one join branch and one column, with no extra round trip. Query-count budgets are unchanged. Intake does one primary-key lookup, and only for did:key delegators.
  • Known limit (F4). Signed KV URL tickets and open hook streams the key obtained before the retire keep working for up to 300 s.

Known gap (follow-up TC-3673)

A host only knows the retires posted to it, and records one only if it stores a live grant for the key or is given the key's roots in the request. Sign-out covers only those: a root the client does not send and this node does not hold is not covered. A host that starts hosting the space after the sign-out never gets the marker. It accepts the key until the session root expires (at most 7 days by default). TC-3673 tracks the fix: the retire record goes to the registry and is imported when a node admits a space.

Review notes (Bobbie, PASS WITH NOTES at 8bf52aa; follow-up TC-3692)

  • delegation::save takes each did:key delegatee's retire lock in event order, so two batches naming the same two delegatees in opposite order could deadlock on PostgreSQL. PostgreSQL aborts one side, so it fails closed, and no production caller sends a batch with more than one delegatee. TC-3692: sort the locks, or document the invariant.
  • The L1 race test runs both operations through one SpaceDatabase, whose in-process guard serializes them, so it cannot catch a PostgreSQL lock-order regression. The order is right by code reading (S → DID → O). TC-3692: a two-instance test.
  • Accepted as is: a carried, not-yet-valid, never-expiring root keeps that key's retire row forever. Bobbie judged this no worse than /delegate.

Migration

m20261121_000000_session_retire adds the table and the column. down drops both. There are up→down→up tests on SQLite and PostgreSQL.

Tests

  • core: models::session_retire::tests::* (request verification for each malformed case; idempotence; TTL and prune; no row for a key without a live grant)
  • core: db::session_retire_tests::{sqlite,postgres}_retired_session_refuses_itself_and_new_children_only, which covers:
    • self-revoke;
    • a pre-retire child still works;
    • a grandchild under a different delegator is untouched;
    • a post-retire child is refused on both the single and batch paths;
    • a stored child resubmitted answers Existing on both paths;
    • the owner can still post a root.
  • core: db::session_retire_tests::{sqlite,postgres}_child_first_seen_after_retire_is_refused_at_invoke, first_seen_is_set_on_insert_and_kept_on_resubmit, expired_retire_does_not_refuse_and_is_pruned
  • core: db::session_retire_tests::{sqlite,postgres}_retire_row_outlasts_every_grant_the_key_holds (60-day root sets the end; a later grant raises it; nothing lowers it; NULL expiry is never pruned)
  • core: db::session_retire_tests::{sqlite,postgres}_retire_holds_past_its_ttl_while_a_longer_grant_lives (retire + 31 days: invoke still refused, the intake lookup still finds the row, prune deletes nothing)
  • core: db::session_retire_tests::{sqlite,postgres}_retire_racing_a_delegation_to_the_key_covers_it (N1); renewal::tests::postgres_retire_waits_for_a_stamp_in_flight_on_an_original_it_holds (N1, PostgreSQL lock)
  • core: db::session_retire_tests::{sqlite,postgres}_carried_root_is_admitted_and_covered (N2 thief case: an unstored root carried in the retire is stored and covered; posting it later answers Existing; invoke refused at retire + 31 days), {sqlite,postgres}_retire_with_nothing_to_record_writes_nothing, {sqlite,postgres}_refused_carried_delegation_writes_nothing (wrong delegatee; non-owner root beside a valid one; nothing stored)
  • core: db::session_retire_tests::{sqlite,postgres}_carried_roots_outside_their_time_window (L2: expired root skipped and reported; future-nbf root from a non-owner refused; future-nbf owner root covered, not stored), sqlite_carried_retire_racing_a_delegate_in_its_space / postgres_carried_retire_racing_a_delegate_in_its_space_does_not_deadlock (L1: a carrying retire and a /delegate to the same key in the same space, joined, four rounds)
  • core: renewal::registration_tests::lease_for_an_original_held_by_a_retired_session_key_is_refused (checked past a 1 s TTL); frozen_proofless_owner_lease_registers_active_generation_one (extended: the row lasts to the lease expiry); renewal::tests::sqlite_issue_caps_to_ancestor_and_replay_is_atomic (extended: a declined stamp spends no stamp or replay, also past the TTL; a stamp racing a retire is covered; a stamp committed before the retire sets the end)
  • node: config::tests::tc3443_startup_rejects_a_zero_session_retire_ttl
  • core: migrations::m20261121_000000_session_retire::tests::{sqlite,postgres}_session_retire_up_down_up
  • node: tests/session_retire_routes.rs, four tests:
    • the end-to-end retire;
    • six malformed requests, each refused with 401;
    • the per-IP 429;
    • a retire carrying the session root (bare retire recorded:false; foreign key, not-JSON, nine entries, non-delegation entry each 401; carried retire recorded:true; the root posted later answers 200 and the key's invoke 401).

Local results at 8bf52aa: core lib 1257/1257, session_retire_routes 4/4, did_resolution_routes 9/9, node operation_retry 16/16, clippy clean, fmt clean. The PostgreSQL variants and postgres_round_trip_budget run in CI; Bobbie's reviewer ran the PostgreSQL 14 variants at a42ea3c (42/42, round-trip budget 9/9). session_retire is classified RETAINED for space purge (keyed by a session did:key, names no space, expires_at-bounded).

Release

Merges only on Naomi's GO naming the head SHA, and only after v1.26.17 is tagged from 4a93060 (so 1.26.17 stays #677 alone). It ships in its own node release after 1.26.17, through Amos (it adds a migration). The release note must carry the bold statements from the CHANGELOG.

test-ruling: TC-3443 tinycloud-node renewal/registration_tests.rs moves lease setup into the shared frozen_lease_graph helper and frozen_proofless_owner_lease_registers_active_generation_one gains a retire-row-lasts-to-lease-expiry check; renewal.rs sqlite_issue_caps_to_ancestor_and_replay_is_atomic gains a C2 block (a declined stamp past a 1 s TTL, a concurrent stamp + retire, a pre-retire stamp setting the row's end); delegation literals in existing fixtures gain first_seen_at: Default::default(); additions only, no assertion removed or loosened (2026-11-21)

Sign-out option B. A session did:key signs a 120 s, proofless UCAN to this node with
tinycloud.session/retire on urn:tinycloud:session:self; the node records (did, retired_at)
idempotently for session_retire.ttl_secs (default 30 days) behind a per-IP rate limit.

- The retired key invokes nothing (401 session_retired:).
- A delegation it signs that this node had not stored before the retire is refused on
  every new-delegation path (precheck, validate_with, save_owner_renewal_lease); a stored
  one resubmitted, single or batch, answers as already registered.
- Invoke-time chain check: a chain delegation from the key first seen after the retire
  is refused (new delegation.first_seen_at; NULL counts as before).
- No renewal lease or stamp for a delegation the key holds.
- Delegations made before sign-out keep working; Revoke ends them. Hosts that start
  hosting the space after sign-out never get the marker (follow-up TC-3673).
- Retire lookups ride the existing graph snapshot query; no extra invoke round trip.

Details: TC-3443
A retire row expired after ttl_secs even if the key still held a longer-lived grant,
so a retired key could act again. The row now lasts until the latest-expiring stored
delegation (or renewal stamp) held by the key ends; NULL expiry keeps it for good; a
delegation to the key stored later raises it (on PostgreSQL inside the existing insert
statement, no extra round trip). ttl_secs is the minimum.

Details: TC-3443
Completes b95475f, whose selection missed this line.

Details: TC-3443
…0 refused (TC-3443)

Bobbie's review of #678:
- N1: the retire (prune, live-grant check, end computation, insert/raise) is one write
  transaction. On PostgreSQL it takes a per-DID advisory lock and the renewal lock of each
  original the key holds; delegation::save takes the same DID lock for a did:key delegatee
  inside its existing actor-insert statement (no extra round trip).
- F1: active renewal leases (lease_expiry) count toward the row's end, and registering a
  lease raises it; session_retire.ttl_secs = 0 refuses to start.
- F2: a row is written only for a DID holding a live delegation, stamp or lease; the
  route answers the same 200 otherwise, so a forged X-Real-IP cannot fill the table.
- F3/F4: wording (authority the key holds as owner is unaffected) and the <=300 s
  signed-KV ticket / hook stream tail documented as a known limit.

Details: TC-3443
…ded (TC-3443)

Bobbie's N2: with the live-grant gate, a retire on a host that stored no grant for
the key wrote nothing yet answered 200, and the key's root posted afterwards made it
usable there. POST /session/retire now takes an optional {"delegations": [...]} body
(the /delegate header encoding, at most 8, each delegatee must be the retiring DID or
the whole request is refused). Carried roots go through policy admission, precheck
and process_with inside the retire's own write transaction, under the DID lock, so the
row covers them. The response gains recorded and expiresAt; recorded:false means
nothing was written. Docs: the client sends every root it holds for the key.
Adds a PostgreSQL stamp-vs-retire lock test.

Details: TC-3443
Completes b98b4b4, whose selection left out the rest of db.rs.

Details: TC-3443
Conflicts resolved:
- delegation::process_with keeps both Existing arms: a stored delegation
  refused as SessionRetired (TC-3443) or UnverifiableHolder (TC-1500)
  answers Existing on resubmit.
- delegation::save_owner_renewal_lease runs check_holder, then the retire
  check.
- CHANGELOG/README keep both entries; tinycloud.toml drops the removed
  did_resolution block and keeps session_retire.

Details: TC-3443
… not a refusal (TC-3443)

CI at a42ea3c:
- space_purge_table_inventory: session_retire names a did, so it needs a purge
  classification. RETAINED: keyed by a session did:key, names no space,
  expires_at-bounded and pruned.
- operation_retry::delegate_route_answers_the_safe_503_when_the_retry_is_lost_too:
  precheck now reads the retire table for a did:key issuer, and a lost
  connection there was followed by a second pool read (is it stored?) that spent
  the retry's injected loss. delegate_verified now returns a storage failure from
  precheck as it is; only refusals ask whether the bytes are already stored.

Details: TC-3443
…ed roots outside their window (TC-3443)

Bobbie's PASS WITH NOTES on a42ea3c:
- L1: a retire carrying a root took the key's lock, then the space sequence lock,
  while /delegate takes them the other way round: a PostgreSQL deadlock. The retire
  now takes locks as transact_verified does: chain guards, the in-process space
  sequence guards (TC-732), the SQLite writer lock, then in the transaction the space
  sequence locks, the key's lock, and (in retire_in) the originals' renewal locks.
  The carried roots are stored with those sequence locks marked held.
- L2: a carried root that has expired is skipped and its index returned in
  `skipped`; one not valid yet is checked (signature, issuer standing, capability
  rules, holder) but not stored, and the row lasts at least until it expires.

Details: TC-3443
@samgbafa
samgbafa marked this pull request as ready for review November 21, 2026 22:56
@samgbafa
samgbafa merged commit 62d1e93 into main Nov 21, 2026
34 checks passed
samgbafa pushed a commit that referenced this pull request Nov 21, 2026
Conflict in tinycloud-core/src/migrations/mod.rs: #678 (session retire)
and this branch both added a new migration module and Migrator entry.
Resolved by keeping both; session retire runs first by version-string
order (m20261121 < m20261122).
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