Skip to content

Spec: Windows foundations — a separate event-log lock, LF checkouts, a Rust test fake, the Windows npm names #805

Done
3 of 3 issues completed

Description

@danielrispler

Problem Statement

Native Windows support is coming (docs/studio/research/windows.md, "Recommended path"), but not yet. A few parts of plotkeeper get harder to change once people use it, because their shape lands on disk or in other people's checkouts. Today they are shaped for macOS and Linux only:

  • The event log's lock is the log itself. Every writer opens events.jsonl for append and locks that file (hook/src/lib.rs:122-145 at 8e7faab21), and the prune checks it still holds the same file by (dev, ino) (hook/src/lib.rs:306-310). On Windows a lock on the log blocks the read loop's reads, and a lock on an append-only handle fails. The CI spike of 2026-11-28 showed both: error 33 for a reader while another process held the lock, and "lock acquisition failed" on an append-only handle (run 46141976334, probe lock-blocks-reader, lock-append-only). (dev, ino) is Unix-only (std::os::unix::fs::MetadataExt).
  • Line endings follow the checkout's machine. The repository has no .gitattributes. On the Windows runner Git for Windows checked out 794 files with CRLF (same run, survey), so frozen samples, fixtures and snapshot texts would differ byte for byte on a Windows checkout.
  • Tests fake agents with shell scripts. 38 #!/bin/sh fakes across 21 files (grep -rc '#!/bin/sh' --include='*.rs' hook server understanding workbench test-support core wayfinder system-map at 8e7faab21), each made executable with Unix permission bits. None of them can run on Windows, and the number grew from 25 to 36 in three days (windows.md, "Foundational changes: now or later"). And the spike found that a bare bash started from Rust on Windows is WSL's launcher, not Git Bash (windows.md, "CI spike results").

Daniel has no other users yet, so these are cheapest now (windows.md, "Foundational changes: now or later").

Solution

Three changes, none of which changes what plotkeeper does on macOS or Linux:

  1. The event log gets its own lock file, events.lock beside events.jsonl in the data folder. Every writer of the log, the hook, the answer hook and the server, takes that lock, then opens the log, appends and lets go. The prune holds the same lock while it puts the new log in the old one's place. Nothing locks events.jsonl itself any more, so a reader never meets a lock, and the same-file check by (dev, ino) goes away: a writer opens the log only after it holds the lock, so it always opens the file the last prune left.
  2. The repository checks out with LF everywhere. A .gitattributes marks text files LF on every machine and the images binary.
  3. Tests fake programs with one Rust fake from test-support, not shell scripts. A test asks for a fake of a name (claude, agy, git, …) in a folder, with what it prints, what it exits with and what it records (its arguments, folder, stdin, environment). The fake is a real executable on every platform. New tests use it, and AGENTS.md says so. The existing fakes move over in their own time.

The research note docs/studio/research/windows.md, with its CI spike results, lands in the repository with the first ticket.

User Stories

  1. As Daniel, I want the event log never locked itself, so that a future Windows build's read loop can read it while hooks append.
  2. As Daniel, I want every writer of the log to take one lock file, so that two hooks that fire together still never interleave their lines.
  3. As Daniel, I want the server's own lines (StudioRename, StudioDelete, StudioPromptName, a SessionEnd with reason process_gone) to take the same lock as the hooks, so that its lines never interleave with a hook's.
  4. As Daniel, I want the answer hook's lines to take the same lock, so that a card's answer line is written whole.
  5. As Daniel, I want a prune to hold the lock file while it puts the new log in the old one's place, so that a hook's line waiting for the lock lands in the new log, never in the replaced one.
  6. As Daniel, I want a hook that waited for the lock through a prune to append to the new log, so that no line is lost to a swap, as today.
  7. As Daniel, I want the hook to drop an event to drops.txt when the lock never comes within its patience, as today, so that an agent never waits on plotkeeper.
  8. As Daniel, I want Codex's and Cursor's shorter waits for the lock (1 second) kept, so that their hooks still fit inside the time their entries allow.
  9. As Daniel, I want OpenCode's order kept, the session's record locked until its line is in the log, so that its lines stay in the order the hook took them.
  10. As Daniel, I want the lock file made on first use, so that a data folder from before this change needs no step.
  11. As Daniel, I want an old log, archive and state to read as before, so that no history changes.
  12. As Daniel, I want plotkeeper uninstall to keep treating the data folder as history, so that the lock file is left with the log like any other file there.
  13. As Daniel, I want the storage doc to name events.lock and say which writers take it, so that the log's contract is written where its readers look.
  14. As Daniel, I want the lock rule to be a law on the system map with tagged tests, so that a later change that writes the log without the lock fails npm run check.
  15. As Daniel, I want the same-file check by device and inode gone, so that the hook crate has one Unix-only item fewer for the Windows port.
  16. As Daniel, I want no change in how long a hook takes in the common case, so that the agents feel the same.
  17. As a contributor on Windows, I want a checkout with LF line endings, so that the frozen samples and fixtures match their bytes and the tests that read them pass.
  18. As Daniel, I want images and other binary files marked binary, so that no line-ending rule ever touches them.
  19. As Daniel, I want the new .gitattributes to change no file in the index, so that the change is one new file, listed in the system map's ignored, and no churn.
  20. As Daniel, I want the files that hold no line end (single-line JSON samples) left as they are, so that their bytes stay the same.
  21. As a test author, I want one call that makes a fake program of a given name in a folder, so that I stop writing shell scripts and setting permission bits by hand.
  22. As a test author, I want to say what the fake prints on stdout and stderr and what it exits with, so that I can fake an agent's success, its failure and its "not logged in".
  23. As a test author, I want the fake to record its arguments, its folder, its stdin and its environment to a file I can read, so that I can assert how the code under test called it.
  24. As a test author, I want to change what a fake does between two calls, so that a test can fake a first failure and a later success, as server/tests/writer.rs does today.
  25. As a test author, I want the fake to be able to wait before it exits, so that tests of deadlines and kills still have a slow program to stop.
  26. As a test author, I want the fake to need no shell, Python or Node, so that it runs the same on macOS, Linux and Windows.
  27. As a test author, I want the fake's file named as the platform expects (claude on Unix, claude.exe on Windows), so that the code under test finds it as it would find the real program.
  28. As Daniel, I want AGENTS.md "Tests" to say new fakes use the Rust fake and never a #!/bin/sh script or a bare bash/sh, so that the debt stops growing while the old fakes wait for their port.
  29. As Daniel, I want at least one existing shell fake moved to the Rust fake in this spec, so that the fake is proven against a real test before others copy it.
  30. As Daniel, I want the fake's own behaviour tested in test-support, so that a test that uses it can trust what it records.
  31. As Daniel, I want the research note and its spike results in the repository, so that this spec and the later Windows spec cite a durable source.

Implementation Decisions

The event log's lock.

  • The hook crate owns the lock, as it owns the log's name and path today. Its public log API keeps its shape for callers: append a line with a patience, get the log open under the lock for a write, prune. The value it hands back for a write holds both the lock and the open log, and lets both go when dropped.
  • The lock file is events.lock in the data folder, beside events.jsonl. It is opened for read and write and created if missing, and locked exclusively with std's file lock, the same lock call as today on a different file. The lock file is never written, read, truncated, replaced or deleted. Its bytes mean nothing.
  • A writer takes the lock first and opens the log for append after. Since the prune replaces the log only while it holds the lock, a writer that holds the lock always opens the current log, so the loop that re-opened a replaced log and the (dev, ino) comparison both go.
  • The prune reads the log and writes events.jsonl.next without the lock, as today, then takes the lock (60 seconds of patience, as today), copies the tail hooks appended meanwhile, removes the state file, renames the new log into place, and lets the lock go. Its check that the log was not replaced during the prune, by comparing the file it read with the one it holds, becomes a check that the log's length did not shrink below the bytes it read. Only another prune can replace the log, and only one plotkeeper runs at a time (the data folder's lock, server/src/lock.rs).
  • The waits stay as they are: 5 seconds for a hook, 1 second for Codex and Cursor lines, 60 seconds for the server and the prune.
  • OpenCode's order stays: the hook takes the session record's lock, then the log's lock, as today.
  • An upgrade needs no step: the hook copies in bin/ are refreshed at each start (docs/studio/plugin.md, the start), so after a restart every writer takes events.lock. A hook process already running from an old copy during that restart locks the old way for one event. Two such writers no longer exclude each other, but each still writes its line in one write to a file opened for append. The window is one restart, and Daniel is the only user (windows.md, "Foundational changes: now or later").
  • The storage doc (docs/studio/behaviour/storage.md) adds events.lock to the data folder's files and states the rule: every writer of events.jsonl holds events.lock while it writes, a prune swaps the log only while holding it, and readers take no lock.
  • A new law on the system map, log-writers-hold-the-log-lock, on: "hook", backing: "check": "Every line written to the event log is written whole, under events.lock; a prune replaces the log only while holding it, and no reader takes it." The tests below carry @law: log-writers-hold-the-log-lock. Since system-map check only sees that a tagged test exists, one of them is a source scan, as no_git_or_gh_is_built_outside_the_runner (workbench/tests/run.rs:321) is for the runner's law: it fails when a shipped Rust file other than the hook crate's log code names the event log (events.jsonl or EVENT_LOG) and opens a file to write (OpenOptions, File::create, fs::write). So a later writer that skips the lock fails npm run check.

Line endings.

  • .gitattributes at the root: * text=auto eol=lf, and the image types the repository holds (*.png, plus *.jpg, *.gif, *.ico, *.webp) marked binary. Every text file in the index is already LF (git ls-files --eol at 8e7faab21: 789 i/lf, 6 i/-text images, 3 i/none single-line files, 2 empty; git ls-files --eol | awk '{print $1}' | sort | uniq -c), so the change renormalises nothing. .gitattributes joins the root files in system-map.json's ignored list, or system-map check fails on a file no part owns.
  • No per-file exceptions. A file that must keep CRLF (none today) would get its own line later.

The Rust fake.

  • The fake lives in test-support, which the tests of hook, understanding, workbench, system-map and server already use (their Cargo.toml dev-dependencies); core and wayfinder hold no shell fake and do not take it on. A test calls one function with a folder, a name and a behaviour, and gets back the fake's path. The behaviour says what to print on stdout and stderr, the exit code, an optional wait before exiting, and whether to record the call. A test can rewrite the behaviour between calls.
  • The fake is the test's own executable, started under another name. test-support places a hard link to the running test binary at <folder>/<name>, with .exe on Windows, or a copy where a hard link cannot be made (another filesystem, such as a tmpfs temp folder), and writes the behaviour beside it. A symbolic link will not do: on Linux the process's own path then reads as the test binary's, not the fake's. Code that runs before the test harness's own main checks whether the process was started as a fake, by looking for a behaviour file beside its own path (std::env::current_exe), and if so acts out the behaviour and exits without starting the harness. This needs no shell, no second build step and no tool beyond Rust, so it runs the same on all three platforms.
  • What a fake records goes to a JSON file beside it, one entry per call: the arguments, the working folder, stdin, and the environment.
  • The code that runs before main is the ctor crate's attribute, a dependency of test-support, which is itself only ever a dev-dependency, so nothing shipped carries it.
  • workbench/tests/run.rs's fake (it prints its arguments, folder, environment and stdin, the richest of the existing fakes) moves to the Rust fake in this spec. Its tests check the same facts, read from the fake's record instead of its printed lines (workbench/tests/run.rs:64-71). The other 37 stay until the Windows port moves them.
  • AGENTS.md "Tests" gains one rule: a test that needs a fake program uses test-support's fake, never a #!/bin/sh script, and no code or test starts sh or bash by bare name (on Windows a bare bash from Rust is WSL's launcher: windows.md, "CI spike results").

The research note. docs/studio/research/windows.md lands with the first ticket's PR (#813), as cross-platform.md landed with #712.

Made without Daniel.

  • Q1: The lock is a separate file, not a lock on a byte range of the log past its end. Set aside: a range lock (LockFileEx with an offset) needs windows-sys and has no std counterpart. Why: the spike showed a separate file works on both Windows images (lock-file-beside-log), and std's lock covers it.
  • Q2: An upgrade's in-flight old hook is not handled. Set aside: writers taking both locks for a release. Why: one event in one restart, a single user, and appends that are still one write.
  • Q3: The prune's "replaced during the prune" check becomes a length check. Set aside: keeping (dev, ino) on Unix only. Why: it is the Unix-only item the port must remove anyway, and only a second prune could replace the log, which the data folder's lock already prevents.
  • Q4: The fake is the test binary re-run under another name (ctor). Set aside: a separate fake binary built by a nested cargo build at test time (slow under nextest's one process per test, and it contends for cargo's lock), and a bin in each package (four extra shipped binaries). Why: no build step, nothing new shipped.
  • Q5: Only one existing fake moves now. Set aside: moving all 38 (the Windows port's job, about a day: windows.md, slice list). Why: the rule stops new ones, and one move proves the fake.
  • Q6: Claude Code's exec form is not in this spec. Set aside: exec form on every platform now. Why: Claude Code before 2.1.139 drops args and runs command through a shell, and the unquoted macOS hook path holds a space, so recording would stop with no error (windows.md, "Claude Code's exec form and older versions"). Exec form goes into the Windows spec, for Windows only.

Testing Decisions

A good test here drives the public seam and checks what a user or another process sees: the lines in events.jsonl, the files in the data folder, what a fake recorded. None asserts how the lock is taken inside.

  • The log's lock is tested through the hook crate's public log API, in hook/tests/prune.rs, which already tests the prune and the wait through a swap. Cases, extending its table where it has one:
    • A line waiting for the lock through a prune lands in the new log (the existing a_line_waiting_for_the_lock_lands_in_the_log_that_replaced_it, hook/tests/prune.rs:175, rewritten to hold events.lock rather than the log).
    • While another holder has events.lock, the log still opens and reads whole, and an append waits and then lands after it lets go.
    • Many appends from threads at once give whole lines, none interleaved, in a log that ends with a newline.
    • An append with a patience shorter than the holder's hold gives false and writes nothing.
    • A data folder with no events.lock gets one on the first append, and an existing log keeps its lines.
    • A prune's tail copy keeps the lines appended between its read and its swap.
    • The source scan of the law (Implementation Decisions).
      All carry @law: log-writers-hold-the-log-lock. Three existing tests lock events.jsonl directly to hold the hooks back, and move to holding events.lock: a_held_lock_gives_a_drops_line_and_no_log_line_once_its_patience_runs_out (hook/tests/deadline.rs:34-38), the Cursor case at hook/tests/harness_trees.rs:324-327, and the prune case above. The OpenCode case at hook/tests/harness_trees.rs:395, which holds the lock through studio_hook::locked, keeps working through the API.
  • The server's lines go through append_to (server/src/lib.rs:586); one case in an existing server test that writes a Studio… line, holding events.lock from the test, checks it waits and lands whole.
  • The Rust fake is tested in test-support's own tests: a fake prints what it was told, exits with its code, records arguments, folder, stdin and environment, waits when told, and does the second behaviour after a rewrite. workbench/tests/run.rs's tests check the same facts as before, from the record.
  • .gitattributes has no test. The PR shows git ls-files --eol before and after: every text file attr/text=auto eol=lf, and no file in the index changed.
  • No Windows CI job in this spec. These changes are tested on macOS and Linux CI as today; the Windows spec adds the Windows job.

Prior art: hook/tests/prune.rs for the log and the prune, server/tests/hook_copies.rs for the data folder on a temporary home, and test-support's LauncherGuard for a helper of its own with tests.

Out of Scope

  • Any Windows build, CI job, package or code path. That is the native core v1 spec (windows.md, "Recommended path", step 4).
  • Claude Code's exec-form hooks (Q6), which go to the Windows spec, for Windows only.
  • Moving the other 37 shell fakes.
  • The run's lock and the session records' locks in turns/, which the Windows spec takes on with the process runner.
  • Any change to plotkeeper's own npm package, its os field or its launcher's message on Windows.
  • Reserving the Windows npm names (plotkeeper-win32-x64, plotkeeper-win32-arm64) ahead of the build. npm's policy calls a package "squatted if the package has no genuine function" and lets it remove accounts that squat (Dispute Resolution, "Squatting"), and a 0.0.0 placeholder publish was refused on 2026-11-28 with "Package name triggered spam detection". The names are published with the Windows build. A name taken meanwhile costs only a rename: plotkeeper's own optionalDependencies choose which packages install, so a stranger's package never reaches a user.
  • The README's Windows and WSL2 lines; the WSL2 result from the spike goes into the README with the Windows spec.
  • Versioned bin/<version>/ hook folders, set aside by the research (windows.md, "Foundational changes: now or later").

Further Notes

  • Every number above was read at 8e7faab21 or in the spike run of 2026-11-28; the run's logs and artifacts expire with GitHub's retention, and windows.md holds what they showed.
  • The second spike round (Codex's hook form, a Job Object killing a real grandchild) belongs to the Windows spec, not this one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CVqACdL6QruKkvHdjdYPVr

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. added
    ready-for-agent
    on Nov 28, 2026
  2. danielrispler commented on Nov 28, 2026

    @danielrispler
    OwnerAuthor

    Scope change, 2026-11-28: the npm placeholders are out of this spec.

    Daniel's publish of plotkeeper-win32-x64@0.0.0 was refused: 403 Forbidden - PUT https://registry.npmjs.org/plotkeeper-win32-x64 - Package name triggered spam detection. Neither name is published (npm view plotkeeper-win32-x64 version and npm view plotkeeper-win32-arm64 version give E404).

    npm's Dispute Resolution policy, "Squatting", rules out a retry or an appeal: "It is against npm's Terms of Use to publish a package … simply for the purposes of reserving it for future use", "Package names are considered squatted if the package has no genuine function", and "Accounts violating the name squatting policy may be removed or renamed without notice." The account at risk publishes plotkeeper and its platform packages.

    Daniel's decision: drop the reservation. The Windows packages are published with the Windows build. A name taken meanwhile costs a rename only: plotkeeper's optionalDependencies choose which packages install, so a stranger's package never reaches a user.

    Changed in the body: the Problem Statement's npm bullet, Solution item 4, user stories 31–33, Implementation Decisions "The npm names", Q7 and the Testing Decisions line are removed; Out of Scope gains the reservation with this reason. No ticket held the npm work (#811, #812 and #813 are unchanged). docs/studio/research/windows.md, which lands with #813, says the same.

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

    ready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions