Skip to content

[ryoku] shell: night light schedule reconciles once on load and change - #928

Merged
neur0map merged 1 commit into
Ryoku-dev:unstable-devfrom
LangerSword:fix/nightlight-schedule-reconcile
Nov 30, 2026
Merged

neur0map merged 1 commit into
Ryoku-dev:unstable-devfrom
LangerSword:fix/nightlight-schedule-reconcile

Conversation

@LangerSword

@LangerSword LangerSword commented Nov 21, 2026 •

Copy link
Copy Markdown

Fixes #919.

The schedule tick compared its wanted state only against its own previous desire, so enabling Follow the Sun (or a clock window) during daylight left an already-running backend on until the next edge, and a light left on across a reboot survived the whole day with the schedule active.

This implements the one-shot reconciliation suggested in the issue:

  • schedPending is set in loadSchedule() for a restored sun/clock schedule and in setSchedule() when the mode changes to one.
  • tickSchedule() performs one comparison of the wanted state against the backend's real state (n.running()) while the flag is set, acts on the difference, then clears the flag and records the desire. The existing last-desire comparison resumes afterwards, so a manual toggle stays honoured until the next edge.
  • An intent that fails to land keeps the flag set and retries on the next tick, matching the existing failure behaviour.

Tests, driven through the real intent path with the existing buildNlFake harness:

  • TestNightlightScheduleReconcilesRunningBackend: the light is on before the schedule exists; enabling a daylight window turns it off on the very next tick, and the following tick sends nothing (one-shot, no fighting).
  • TestNightlightScheduleReconcilesAfterLoad: a light left on with a persisted daylight schedule is turned off by a fresh daemon on its first tick after loadSchedule(), and a manual re-toggle before that still wins.

Full ryoku-shell module suite passes (go test ./... in ryoku/shell/ipc, gofmt and vet clean). CHANGELOG entry added under Unreleased/Fixed.

(Prepared with an AI agent as the AGENTS.md workflow invites; every behaviour here was verified against the real code and test suite on current main.)

RetriggerView in GreptileConfidence Score: 4/5

The startup ordering issue still needs a fix before merging.

Fix All in Claude CodeFindings

  1. P1 Boot restore can escape reconciliation. ▶
Fix with agent prompt
### Issue 1
ryoku/shell/ipc/nightlight_schedule.go:283-299
During a daytime clock window, the first tick can run before the separately started `ryoku-cmd-nightlight restore` creates the light process. Here, `want` and `n.running()` are both false, so the tick clears `schedPending` without removing the saved enabled marker. The restore command can then turn the light on. With `schedLastDesire` already false, the schedule leaves it on until the next clock edge.

Order restore and the first schedule check, or otherwise ensure a restore that finishes later cannot escape that check. Add a boot test where the saved marker exists before the light process starts.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Night Light schedules now check the running light once after startup or a schedule edit, while leaving later manual choices alone.

  • Active night-light schedules now match the backend once when loaded or changed.

Reviews (8) · Last reviewed commit: "[ryoku] shell: reconcile night light sch..." · Reviewed by Greptile

Copilot AI lite review requested due to automatic review settings November 21, 2026 00:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 1 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

Comment thread ryoku/shell/ipc/nightlight_schedule.go Resolved
Comment thread ryoku/shell/ipc/nightlight_schedule.go Resolved
@neur0map

neur0map commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

🦊 mika's review

Incremental review: new commits since the last checkpoint.

Summary

  • Reconcile Night Light once when schedules load or change.
  • Preserve manual toggles after reconciliation until the next schedule edge.
  • Test backend commands for one-shot reconciliation.

Change diagram

flowchart LR
  Schedule["Load or set schedule"] --> Arm["Arm reconciliation"]
  Arm --> Tick["Schedule tick"]
  Tick --> Backend["Night Light backend"]
  Tick --> Desire["Record last desire"]
Loading

Estimated review effort: 4/5 · risk: medium

📁 Walkthrough details

CHANGELOG.md

  • Document the Night Light schedule reconciliation fix under Unreleased.
    • Add a Fixed entry referencing issue 919.

ryoku/shell/ipc/nightlight.go

  • Add a schedule reconciliation counter to Night Light state.
    • Track armed reconciliation passes across schedule changes.

ryoku/shell/ipc/nightlight_schedule.go

  • Reconcile active schedules against the running backend after loading or setting them.
  • Resume edge-based updates afterward so manual toggles remain effective.
    • Arm reconciliation on schedule load and active schedule calls.
    • Discard stale armed snapshots and retain the arm after intent failures.
    • Record the desired state after a successful reconciliation.

ryoku/shell/ipc/nightlight_schedule_test.go

  • Test reconciliation when enabling a schedule and loading a persisted schedule with the backend running.
  • Count commands to check one-shot behavior and manual toggle preservation.
    • Add running-backend and loaded-schedule tests.
    • Widen clock-window offsets in existing tests.

ryoku/shell/ipc/nightlight_watch_test.go

  • Log fake Night Light command invocations for schedule tests.
    • Append each fake command verb to a temporary invocation log.

No actionable findings. Mika looked for bugs, security issues, and risks in the diff and came up empty on clear defects.

🛠 Deterministic checks run
  • golangci-lint: not installed (skip)

🚦 Merge recommendation: changes_required


🦊 Generated by Mika — the Ryoku org's self-hosted AI code reviewer. Machine findings: verify before merging.

@neur0map neur0map added area:ryoku Ryoku project area type:bug A reported problem labels Nov 21, 2026
Comment thread ryoku/shell/ipc/nightlight_schedule.go Outdated
Comment on lines +283 to +299
if pending {
if want != n.running() {
var err error
if want {
err = n.intent("on")
} else {
err = n.intent("off")
}
if err != nil {
log.Printf("ryoku-shell: night light schedule tick failed: %v", err)
return // retry on the next tick: do not record an action that did not land
}
}
n.schedMu.Lock()
n.schedPending = false
n.schedLastDesire = want
n.schedMu.Unlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Boot restore can escape reconciliation. During a daytime clock window, the first tick can run before the separately started ryoku-cmd-nightlight restore creates the light process. Here, want and n.running() are both false, so the tick clears schedPending without removing the saved enabled marker. The restore command can then turn the light on. With schedLastDesire already false, the schedule leaves it on until the next clock edge.

Order restore and the first schedule check, or otherwise ensure a restore that finishes later cannot escape that check. Add a boot test where the saved marker exists before the light process starts.

Prompt To Fix With AI
This is a comment left during a code review.
Path: ryoku/shell/ipc/nightlight_schedule.go
Line: 283-299

Comment:
**Boot restore can escape reconciliation.** During a daytime clock window, the first tick can run before the separately started `ryoku-cmd-nightlight restore` creates the light process. Here, `want` and `n.running()` are both false, so the tick clears `schedPending` without removing the saved enabled marker. The restore command can then turn the light on. With `schedLastDesire` already false, the schedule leaves it on until the next clock edge.

Order restore and the first schedule check, or otherwise ensure a restore that finishes later cannot escape that check. Add a boot test where the saved marker exists before the light process starts.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

@LangerSword
LangerSword force-pushed the fix/nightlight-schedule-reconcile branch from 3bf55bd to 14cb985 Compare November 21, 2026 00:38
@LangerSword
LangerSword changed the base branch from main to unstable-dev November 21, 2026 00:38
@LangerSword

Copy link
Copy Markdown
Author

All three findings addressed, and retargeted to unstable-dev.

  • The arm is now a counter (schedArmed). The tick snapshots it and only clears when it still matches, so a setSchedule racing the tick keeps its own arm and reconciles on its own pass; the stale tick records nothing.
  • Any edit of an active window now arms the reconciliation, including bounds-only changes that keep the mode: the user just asked for this window in every case.
  • The off-window in the clock tests is minutes wide now (cur ± 3), so a minute rollover mid-test cannot flip the wanted state. The two pre-existing tests that shared the same fragile pattern get the same widening.

Full ryoku-shell module suite passes, gofmt and vet clean.

@neur0map
neur0map dismissed their stale review November 21, 2026 00:39

Blocking findings were addressed (verified at 14cb985c87).

Comment thread ryoku/shell/ipc/nightlight_schedule.go Resolved
@LangerSword

Copy link
Copy Markdown
Author

Follow-up hardening from the overlap review on #929: the armed reconciliation tick now re-reads the schedule mode under the lock before it acts, so a schedule disabled or switched while a tick was in flight is not applied by that stale tick. The newer mode gets its own kick, and an off leaves the light alone.

The same theoretical mid-flight window exists in the pre-existing last-desire path; I left that untouched here to keep the change scoped, happy to harden it the same way if you want.

Full ryoku-shell suite passes, gofmt and vet clean.

@neur0map neur0map left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🔴 Changes requested — 0 critical and 1 major issue(s): see the inline comments and the walkthrough for fixes and an agent-ready prompt.

Comment on lines +297 to +308
n.schedMu.Lock()
moved := n.schedMode != mode
n.schedMu.Unlock()
if moved {
return
}
if want != n.running() {
var err error
if want {
err = n.intent("on")
} else {
err = n.intent("off")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🟠 major · Possible Bug · fix: medium effort

The mode check is released before the backend action, so a concurrent call can disable the schedule after the check and still have this tick change the light. Bounds-only changes are not checked at all, so an old window can also apply its stale desire before the new one is reconciled. Serialize schedule changes with the action or otherwise validate the complete schedule atomically with applying it.

Reply here to discuss, say it's fixed, or explain why it's intended — no mention needed.

@LangerSword
LangerSword force-pushed the fix/nightlight-schedule-reconcile branch from 4926d5a to 1d6667b Compare November 21, 2026 14:29
@LangerSword

Copy link
Copy Markdown
Author

Both findings addressed, with one honest boundary stated.

The stale-tick gap (nightlight_schedule.go:308): the armed tick now re-reads the arm counter and the mode under the lock before it acts. Every active-mode edit bumps the counter, and a disable moves the mode, so a bounds-only change, a margin change, a mode switch, and a disable all make the in-flight tick stand down. The newer state reconciles on its own kick.

The boundary I will not paper over: a schedule that is disabled while the intent script is executing (the window between the last check and the script finishing, roughly a second) can still have that one toggle land. That window is not something my reconciliation introduced; the pre-existing last-desire path has had the identical window since the scheduler was written, and closing it means holding the schedule lock across a process spawn or building an undo path. If you want it closed properly, I would rather do that as its own change covering both paths than bolt a half-fix onto the armed pass here. Say the word and I will build it.

The test gap (nightlight_schedule_test.go:350): valid and fixed. The fake backend now appends every invocation to a log beside its temp file, and the tests count commands instead of watching frames: the settled schedule sends nothing, and a manual toggle after reconciliation is not fought on the next tick.

Full ryoku-shell suite passes, gofmt and vet clean.

@LangerSword

Copy link
Copy Markdown
Author

@neur0map Following up on #928. The round-2 fixes landed in head 1d6667b, but your CHANGES_REQUESTED review is still pinned to the older aa4c864, so mika hasn't seen the new code. Could you re-run the review or dismiss that stale one? Happy to handle whatever comes back.

@neur0map

Copy link
Copy Markdown
Member

I've reviewed 5 pushes on this PR automatically, so I'll stop doing full passes here and only check whether open threads get fixed. Add the mika:review label or comment @ryoku-mika review for another full review.

@LangerSword
LangerSword force-pushed the fix/nightlight-schedule-reconcile branch from 723ba7d to ef18cfd Compare November 23, 2026 14:42
@LangerSword

Copy link
Copy Markdown
Author

Fixed the remaining scheduling race: schedule edits and backend actions are serialized, with regression and race tests for in-flight ticks, bounds edits, and manual toggles. New head ef18cfd.

@LangerSword

Copy link
Copy Markdown
Author

@ryoku-mika review

@Sipper1236 Sipper1236 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good fix, but every active schedule write increments schedArmed even when all values are unchanged; a no-op save can undo a manual toggle. Only arm it when the schedule changes.

@LangerSword
LangerSword force-pushed the fix/nightlight-schedule-reconcile branch from ef18cfd to e7b8c35 Compare November 24, 2026 16:25
@LangerSword

Copy link
Copy Markdown
Author

Addressed the live finding on ef18cfd; new head is e7b8c35 (squashed, single commit, PR head verified equal to local HEAD).

Finding: every active setSchedule() write incremented schedArmed, even when mode, margin and clock bounds were all unchanged. The bar style re-sends the whole schedule after a manual toggle and temperature edit, so that no-op save re-armed reconciliation and the next tick turned the light straight back off (Sipper1236; same issue in the Greptile P1 inline comment).

Fix: setSchedule() now normalizes the request against the current schedule first — absent fields keep their value and wild clock bounds clamp into the day — then arms only when the effective mode, margin or bounds actually differ. A no-op save leaves both the arm and schedLastDesire untouched; real mode/margin/bounds edits still reconcile. The schedActionMu serialization and arm-counter race guard are unchanged.

Tests: added TestNightlightNoOpScheduleSaveKeepsManualToggle: a no-op save after a manual toggle leaves the light on with no new backend command and no arm, while a real bounds change arms and reconciles. Confirmed the test fails against the pre-fix source (a no-op save armed 1 reconciliations) and passes after. Full module: go test ./... -count=1 -> ok ryoku-shell 30.428s; go vet ./... clean; gofmt -l . empty.

@ryoku-mika review

Arm the one-shot backend reconciliation only when the active schedule really
changes (mode, margin or clock bounds), so a no-op re-save after a manual
toggle leaves the user's choice alone. Serialize ticks with schedule edits and
re-read the arm under the lock so an in-flight tick cannot apply a stale desire.

Regression tests cover a no-op save after a manual toggle, a real bounds edit,
in-flight edits, and boot reconciliation.

Note: Fixed: Night Light schedule changes cannot be overtaken by an in-flight tick
Note: Fixed: A no-op Night Light schedule save no longer undoes a manual toggle
@LangerSword
LangerSword force-pushed the fix/nightlight-schedule-reconcile branch from e7b8c35 to 2fa7cea Compare November 24, 2026 16:28
@LangerSword

Copy link
Copy Markdown
Author

Head advanced to 2fa7cea: commit message only, restoring the Note: Fixed: release-note trailers that the squash had dropped (one for the in-flight-tick fix and one for the no-op save fix). No code changes since the passing run — go test ./... -count=1, go vet ./... and gofmt -l . all clean on this tree. PR head verified equal to local HEAD.

@ryoku-mika review

@LangerSword

Copy link
Copy Markdown
Author

@ryoku-mika review

Fresh pass requested on head 2fa7cea55f90. The no-op save no longer arms reconciliation, and the in-flight tick path is serialized. The earlier CHANGES_REQUESTED reviews point to older commits.

@LangerSword

Copy link
Copy Markdown
Author

Re-audited current head 2fa7cea. No new diff was needed: schedActionMu, the stale-arm check, command-count assertions, and the no-op save guard are all present. Sabotage checks on those guards make the relevant tests fail, so they are meaningful.

go test -race -count=1 ./..., go vet ./..., and gofmt -l are clean in ryoku/shell/ipc. The earlier CHANGES_REQUESTED review points to an older commit.

@ryoku-mika review

Initial draft by me; final wording written with Hermes Agent.

@neur0map
neur0map merged commit b1c4ac5 into Ryoku-dev:unstable-dev Nov 30, 2026
2 checks passed
@neur0map

Copy link
Copy Markdown
Member

Thanks for the work here, merged with a small boot fix on my side and it goes out with the next update

neur0map added a commit that referenced this pull request Nov 30, 2026
On hyperion the watcher is the night light's live publisher and each
test's fake backend has its own name. The in-flight tick test now starts
the watcher for its boot state, and both caps-gated tests report the
fake backend buildNlFake made, so the watcher finds it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ryoku Ryoku project area type:bug A reported problem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Night Light schedule never turns off a backend it did not start (Follow the Sun stays on through daylight)

4 participants