Skip to content

Broker-local rotation scheduler (§3.4, §4.4) - #12

Merged
Yoshiofthewire merged 20 commits into
mainfrom
kyguard/broker-schedule
Nov 26, 2026
Merged

Yoshiofthewire merged 20 commits into
mainfrom
kyguard/broker-schedule

Conversation

@Yoshiofthewire

@Yoshiofthewire Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

In one line: Brokers now run rotation schedules themselves, by their own clock, with no Control Plane trigger: only the latest due occurrence runs, never twice, and refusals back off per the minimum-age rules.

What changed

  • Machine.Tick evaluates each account's Custodian-signed rotation_schedule; Schedule ticks every minute and on nudges (§3.4). Nudges carry no content and cannot cause anything a tick would not.
  • Outstanding = due in the past and after the floor max(last consumed due, policy persisted at, current commit); only the latest outstanding occurrence runs; only release consumes it.
  • A refused attempt sets next_attempt_at = max(min-age deadline, now + interval); three refusals stop the account until a new policy epoch (§4.4). A minimum age above the interval runs nothing and reports rotation_failed / schedule_below_min_age.
  • The daemon starts the scheduler on each channel connection, after sync and recovery.

Review this

Checked: full suite -race + GODEBUG=fips140=on, SQLite (k82) and PostgreSQL 17 (k83), exit 0; 8 mutations each failing its test (Detail). Not checked: reviewers and CodeQL (pending).

Decided for you (docs/DECISIONS.md 46–47): skipped occurrences are implicit (the released occurrence's due time becomes the floor); the refusal counter is per account and resets on release and on a new policy epoch, while next_attempt_at and the min-age deadline carry across epochs.

You need to: nothing. manual_rotation (which also resets the counter) arrives with the transaction PR.

§13 checklist: Phase 1A "Broker-local scheduler, outstanding-occurrence rule, attempts, nudges, dedup"; acceptance 2 (no nudge), 8 (repeated nudges, past anchor_utc), 9 (retry timing, three-refusal stop, min age above interval) with a fake target. Stacked on #11.

Detail
Property Mutation Failing test
Policy persistence moves the floor drop it from floor TestOutstanding, TestSchedulerRunsDueOccurrencesOnce
Occurrence consumed once don't record LastConsumedDue TestSchedulerRunsDueOccurrencesOnce
Due must be strictly after the floor Before instead of !After TestOutstanding
Wait for next_attempt_at drop the check TestRefusedAttemptsRetryThenStop
Stop after three refusals drop the check same
New epoch lifts the stop don't reset same
Min age above interval runs nothing drop the check TestMinimumAgeAboveTheIntervalRunsNothing
Min-age deadline delays retry ignore it TestRefusalWaitsForTheMinimumAge

Review fixes (25 Nov: Guide "Unpopulated metadata")

Property Mutation Failing test
Minimum age read from the target drop the assignment at login TestTheTargetsMinimumAgeIsReadAtLogin, TestRefusalWaitsForTheMinimumAge
A lost reservation does not block the schedule Tick skips settling TestTickSettlesALostReservation

Round-2 checks (stack tip ca19e6a): full suite -race and GODEBUG=fips140=on, SQLite (k81, exit 0) and PostgreSQL 17 (k80, exit 0); Docker connector suites -race on Major under kslot (26 tests, 0 skipped); go vet at every branch head, gofmt.

Review fixes, round 3 (25 Nov, Guide "Minimum-age enforcement")

  • The minimum age read at the pre-rotation login is now checked before the change is sent. Tick only saw the journaled value, so the first login that reported an age above the interval still rotated a target that accepted the change. A scheduled occurrence now stops there: health rotation_failed (schedule_below_min_age), run aborted (restart_before_send), nothing sent (§4.4). The flag is set only by Rotate, so manual and lease-cleanup rotations (Checkout, possession proofs, leases, manual rotation (§3.4, §3.6) #16) keep their own retry rules (5c2ef81).
  • TestTheTargetsMinimumAgeIsReadAtLogin now covers this. The target would accept the change, because its last change is 30 days old. TestRefusalWaitsForTheMinimumAge uses a minimum age equal to the interval, which still runs. For a scheduled occurrence, the minimum age (at most the interval) plus the last change can never pass now + interval. So the deadline term in refused only matters for lease cleanup, where the floor is 1 h.
Property Mutation Failing test
The age read at login stops a scheduled change drop the recheck TestTheTargetsMinimumAgeIsReadAtLogin (the change was written)
The recheck applies to scheduled occurrences Rotate does not mark its context scheduled same

Round-3 checks (stack tip a805e71): full suite -race and GODEBUG=fips140=on, SQLite (k83, exit 0) and PostgreSQL 17 (k83, exit 0); Docker connector suites -race on Major under kslot (30 tests, 0 skipped, exit 0); go vet and the broker/store tests at every merged branch head; gofmt.

Review fixes, round 3b (25 Nov, security review at d7a9315)

Property Mutation Failing test
No reconnect while a rotation is in flight defer cancel() without waiting TestReconnectWaitsForTheRotationInFlight ("the daemon reconnected while its rotation was still running")

Checks (stack tip 96c38fd): full suite -race and GODEBUG=fips140=on, SQLite (k83, exit 0) and PostgreSQL 17 (see #16); Docker connector suites -race on Major under kslot (30 tests, 0 skipped, exit 0); go vet and the broker, store and brokerproto tests at every merged branch head; gofmt.

Round 4 (25 Nov): main merged up

Checks: at #8 ec97af5 the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot, exit 0) and on PostgreSQL 17 (Major under kslot with its own postgres:17-alpine container, exit 0). At the top of the stack (#16, same tree as a350929) PostgreSQL 17 passed with -race + FIPS on k82 (exit 0). gofmt and go vet (also -tags docker) are clean at every branch head. The mutation proofs ran on throwaway copies.

Round 4b (25 Nov)

Checks (round 4b): at #8 f212849 and at #16 bdb9adf, the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot; every K host was busy) and on PostgreSQL 17 (Major under kslot, own postgres:17-alpine container), all exit 0. #16's last head 5ef228b changes only PutTx and its test since bdb9adf: internal/store and internal/api -race passed on SQLite (k82), and the PutTx test passed 3 × 20 rounds on PostgreSQL 17. gofmt and go vet (also -tags docker) are clean at every branch head. The mutations ran on throwaway copies.

Round 4c (25 Nov)

Checks (round 4c): at #16 9fb8788, the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot) and on PostgreSQL 17 (Major under kslot, own postgres:17-alpine container), both exit 0. gofmt and go vet (also -tags docker) are clean at every changed branch head. The race tests ran on PostgreSQL 17 in my own container; the mutations ran on throwaway copies.

Round 5 (25 Nov): security review at f9073a3

Property Mutation Failing test
A policy accepted between listing and starting is the one checked; nothing reaches the target the old Tick (select under the lock, Rotate after it) TestTickRechecksAPolicyAcceptedMeanwhile

Checks (round 5): the full suite ran at the bottom (#11) and top (#16) of the stack (see #16). The broker package passed at this head. The mutation ran on a throwaway copy.

Round 5b (26 Nov): Guide "Stalled rotations" at 1b4cf38

  • tick used to settle only a reserving run. Now any open run is finished as after a restart (45399e7). Rotate holds the mutex throughout, so a run tick sees open has stopped, not one in progress, and recovery never writes twice. A scheduled run stopped by a failed Control Plane call on an open channel no longer waits for a reconnect.
Property Mutation Failing test
A stopped open run is finished by the next tick, with one write recover only reserving runs TestTickFinishesAStoppedRun

🤖 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: 3 🔵🔵🔵⚪⚪

🧪 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-12, 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

@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-schedule branch from 52fc62f to 4425d13 Compare November 25, 2026 09:53
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch 2 times, most recently from 6a7e405 to 0f9b80e Compare November 25, 2026 09:59
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-schedule branch from 4425d13 to 8e4bd31 Compare November 25, 2026 09:59
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from 0f9b80e to f4ddc83 Compare November 25, 2026 10:52
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-schedule branch from 8e4bd31 to fff702c Compare November 25, 2026 10:56
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from f4ddc83 to 1c391e8 Compare November 25, 2026 11:13
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-schedule branch from fff702c to 6c5a94c Compare November 25, 2026 11:14
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from 1c391e8 to a9c8349 Compare November 25, 2026 12:43
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-schedule branch 2 times, most recently from ea302d9 to d901b32 Compare November 25, 2026 12:50
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from 75c24bf to 06c3cd3 Compare November 25, 2026 12:56
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-schedule branch from d901b32 to 01699e1 Compare November 25, 2026 12:56
Yoshiofthewire and others added 3 commits November 25, 2026 08:29
The broker evaluates every account's Custodian-signed schedule by its own
clock, every minute and when nudged, and runs only the latest outstanding
occurrence: due in the past and after the floor max(last consumed due,
policy persisted at, current envelope commit). Only release consumes an
occurrence, so repeated ticks and nudges never run one twice, and a
policy edit with a past anchor runs nothing due before it was accepted.
A refused attempt waits until max(minimum-age deadline, now + interval);
three in a row stop the account until a new policy epoch. A minimum
password age above the interval runs nothing (schedule_below_min_age).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The scheduler's minimum-age fields now come from the connector at every
pre-rotation login instead of only from tests.

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

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

Guide Unpopulated metadata fixed in c0e8e69 (head now 053d64f): the connector's AgeReporter is read at every pre-rotation login, so MinAgeSeconds and MinAgeDeadline come from the target. TestTheTargetsMinimumAgeIsReadAtLogin sets them only on the fake target and sees both the refusal deadline and schedule_below_min_age; dropping the assignment fails it. DECISIONS 74.

Yoshiofthewire and others added 2 commits November 25, 2026 09:33
Tick checks only the journaled minimum age, so the first pre-rotation
login that reported an age above the schedule interval still sent the
change, and a target that accepted it rotated. A scheduled occurrence now
stops there: rotation_failed (schedule_below_min_age), aborted
restart_before_send, nothing sent (§4.4). Manual and lease-cleanup
rotations build their own context and are not affected.

The two tests that relied on a refusal above the interval now use an
age equal to the interval, and the read-at-login test covers the stop.

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

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

Fixed in 5c2ef81. After PasswordAge is read at the pre-rotation login, a scheduled occurrence whose minimum age is longer than the interval now stops before Change. It sets rotation_failed (schedule_below_min_age) and aborts with restart_before_send, and nothing is sent. Manual and lease-cleanup rotations are unaffected.

TestTheTargetsMinimumAgeIsReadAtLogin uses a target that would accept the change (last change 30 days ago, minimum age 2 days, daily schedule) and checks that nothing is written. Dropping the recheck makes it fail.

Yoshiofthewire and others added 2 commits November 25, 2026 10:03
The scheduler goroutine outlived its connection. After a dropped
channel, Run built a new Machine (with its own mutex) on the same
journal while the old one could still be inside a rotation. The new
Recover could then send a second probe, and the old copy could
overwrite the new result, including a quarantine (§4.3 rules 5 and 6).
serve now cancels its context and waits for every goroutine it started.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	internal/broker/broker_test.go
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
Tick only re-sent journaled enrollment receipts. An enrollment that
stopped earlier (the Control Plane had accepted it, then a journal save
failed) stayed open with no receipts. It blocked rotation and held the
Control Plane lock until the next reconnect. Tick now resumes any open
enrollment through recoverEnrollment, as a restart does: no second bind.

serve also starts enrollments through its WaitGroup, so none outlives
the connection (security review of #12).

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

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

Security review at d7a9315: addressed in 42a173b, with a regression test. The mutation is recorded in the PR body.

@Yoshiofthewire
Yoshiofthewire changed the base branch from kyguard/broker-rotation to main November 26, 2026 00:20
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

addressed in 5425b6e

Yoshiofthewire and others added 2 commits November 25, 2026 19:41
Only a reserving run was settled, so a scheduled run stopped by a failed
Control Plane call on an open channel stayed open until a reconnect (PR
Reviewer Guide "Stalled rotations" on #12).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 26, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Guide "Stalled rotations": fixed in 45399e7. tick now finishes any open run as after a restart, not only a reserving one. Rotate holds the mutex throughout, so a run tick sees open has stopped, not one in progress, and recovery never writes twice. TestTickFinishesAStoppedRun fails if only reserving runs are recovered.

@Yoshiofthewire
Yoshiofthewire merged commit c29ad07 into main Nov 26, 2026
16 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.

1 participant