Skip to content

fix(cleanup): the orphaned-key sweep skips a just-minted agent key and names what it revokes (#4259) - #4260

Merged
vybe merged 2 commits into
devfrom
feature/4259-key-revocation-race
Nov 5, 2026
Merged

vybe merged 2 commits into
devfrom
feature/4259-key-revocation-race

Conversation

@vybe

@vybe vybe commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

crud.create_agent mints the agent's MCP key before the container exists and writes the ownership row only after it does. A cleanup tick inside that window revoked a healthy agent's key one second after it was minted; its first MCP call failed 401 and nothing repaired it. On an instance that creates agents from a hook, this is a coin flip per person.

  • deactivate_orphaned_agent_keys skips keys younger than 10 minutes (ORPHANED_KEY_GRACE_SECONDS) by their own created_at, against a Python-computed ISO-Z cutoff (Invariant #16), never by container state. A key the create path failed to roll back is still revoked on the first tick after the window.
  • The function returns the revoked agent names; the [Cleanup] log line names them.
  • A key whose agent row is live is never orphaned at any age (pinned).
  • AC5: the key table has no deactivation timestamp, so a race victim cannot be told from a deliberate revocation. No blind reactivation; docs/migrations/ORPHANED_KEY_SWEEP_RACE_2026-11.md gives the candidate query and the per-key repair.

Test plan

  • tests/unit/test_4259_key_sweep_startup_grace.py — 6 tests, all red with the fix reverted (the minted-seconds-ago key is revoked; the log line carries no names)
  • test_1745_agent_key_revocation.py adapted to the list return (its fixture wrote created_at="t", which would have read as "younger than the grace")
  • Cleanup suites (1745, 2845, 912, cleanup_unreachable_orphan): 72 passed

Fixes #4259

🤖 Generated with Claude Code

vybe and others added 2 commits November 4, 2026 20:11
…d names what it revokes (#4259)

`crud.create_agent` mints the agent's MCP key before the container exists and
writes the ownership row only after it does, so for a few seconds a healthy
new agent looks exactly like a ghost. A cleanup tick inside that window
revoked the key one second after it was minted; the agent's first MCP call
failed 401 and nothing repaired it.

`deactivate_orphaned_agent_keys` now skips keys younger than
ORPHANED_KEY_GRACE_SECONDS (10 minutes) by their own `created_at`, compared
against a Python-computed ISO-Z cutoff (Invariant #16), never by container
state; a key the create path failed to roll back is still revoked on the
first tick after the window. The function returns the revoked agent names
and the `[Cleanup]` log line names them. A key whose agent row is live is
never orphaned at any age (pinned).

Existing instances: the key table carries no deactivation timestamp, so a
race victim cannot be told from a deliberate revocation — no blind
reactivation; `docs/migrations/ORPHANED_KEY_SWEEP_RACE_2026-11.md` gives the
candidate query and the per-key repair.

Red first: all six tests in `test_4259_key_sweep_startup_grace.py` fail with
the fix reverted (the minted-seconds-ago key is revoked; the log line carries
no names).

Fixes #4259

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ace is pinned (#4259)

Mechanical, per the merge-train note on the PR: the operator note misquoted the
[Cleanup] line, and no test sat inside the window, so a grace of 30 s stayed green.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vybe

vybe commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

merge-train: pushed one mechanical commit to this branch — the operator note now quotes the [Cleanup] Deactivated N MCP key(s) belonging to agents … line as the code emits it, and a test pins a key aged nine minutes inside the grace window (a grace of 30 s stayed green before). No behaviour change. Validated READY at c1e4e6d; merging alone on this train once Tier 1 is green on the new head.

@trinity-ability trinity-ability left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: validated READY at c1e4e6d (lane B, /validate-pr + /review); two mechanical commits on top, Tier 1 green on 5e72d76. Merged alone, the other members of this train are being fixed.

@vybe
vybe merged commit 146029d into dev Nov 5, 2026
27 checks passed
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.

2 participants