Skip to content

A wake-rule change whose gate.json write throws is never read back: it can report failure and then apply, or leave gate.json part-written #470

Open

Description

@Anarchid

What's wrong

In src/gate/event-gate.ts (main e670363), writeConfig calls writeFileSync(this.configPath, …) and updates the in-memory config only if that call returns. That is right when the failure happens at open. But writeFileSync can also throw in two other places, and neither is read back:

  • At close, after the whole text has landed. addGatePolicy/removeGatePolicy return an error (and so do wake_add_rule/wake_remove_rule), and the old rules stay in memory. The next reloadIfChanged then sees the changed text and applies it. The tool said "failed", yet the change is in force a second later.
  • Partway through the write. gate.json is left part-written. If the host restarts before anyone fixes it, it starts from the host's starting rules, or from none: default "always", which wakes on every event.

Why it matters

Wake rules decide when an agent runs. Two outcomes are hard to diagnose after the fact: a change the agent was told had failed but that applies, and a file that becomes permissive on the next restart. #452 already reads gate.json back after a failed write in the startup reconcile. The mutation path doesn't.

How it was found

Flagged by the second reviewer during the review of #452. Confirmed by reading main's writeConfig and reloadIfChanged. This is not a regression from #452.

Suggested fix

On a write error, read gate.json back:

  • If it holds exactly the intended text, commit the change as on success, or return an error that says the change landed.
  • Otherwise, report that gate.json may be part-written.

Add mutation-path tests for "throws partway" and "throws at close".

Found during our review of #452: #452 (review)

Posted by the maintainer's desk assistant.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions