Skip to content

The NDC commit's 30s lock timeout is reasoned but never measured — if a save routinely holds the lock longer, the knob is doing less than it claims #226

Closed

Description

@fdaviddpt

Where the number came from

#223 / PR #224 gave the backgrounded NDC commit a bounded wait for save.lock (REMEMBER_NDC_COMMIT_LOCK_TIMEOUT, default 30s) rather than the parent's timeout 0.

The choice of bounded over zero is well-founded and is not what this issue questions: with 0, the first contender the commit loses to is its own parent, which releases that same lock a handful of instructions after backgrounding it — so a fast Haiku round would skip and duplicate its span every time.

The choice of 30 specifically is not founded on anything. Nobody has measured how long a real save-session.sh holds save.lock end to end.

Why the number might be wrong

The parent acquires at save-session.sh:102 and releases from its EXIT trap, so it holds the lock for the whole save — including its own summarize Haiku call. If that routinely runs longer than 30s, then a competing NDC commit waiting on it times out, skips, and duplicates its span into today-*.md — which is exactly the outcome the bounded wait was chosen to avoid, arrived at anyway.

The plausible sequence is ordinary, not adversarial: save A backgrounds its NDC round and exits; save B starts and holds the lock through its summarize; A's NDC round returns from its own Haiku call up to 180s later and waits on B.

If that is the common shape, 30s buys little and the honest fix is a larger default, not a knob nobody sets.

What would settle it

Instrument lock_acquire / lock_release in save-session.sh across a normal working day and record the held-duration distribution. Then set the default from the tail of that distribution rather than from intuition.

Worth noting the failure is benign either way — a timeout skips and leaves now.md whole, which loses no data and logs the duplicate as routine. This is a tuning issue, not a correctness one. It is filed because the reasoning in the code currently reads more confident than the evidence behind it, and a future reader would reasonably take 30 as measured.

Self-reported by the agent that implemented #224, unprompted, as part of its own report. Filed rather than left in a transcript.

Activity

  1. fdaviddpt commented on Jul 30, 2026

    @fdaviddpt
    ContributorAuthor

    Answered with a number, which is what this issue asked for

    Measured while implementing #225 (PR #234, merged 85c642b), by polling the lock directory at 1ms:

    operation hold time
    NDC append ~30ms
    retire loop, 3-file backlog ~200ms (~65ms/file — fork cost, not I/O)

    The new staging lock's timeout is 10s, which covers roughly 150 staging files at that rate. That is a number derived from a measurement rather than a plausible-sounding round figure, which was this issue's complaint.

    What this does and does not settle

    save.lock's own 30s is untouched, and still unmeasured. #234 deliberately did not consolidate onto it: save-session.sh holds save.lock across its own summarize Haiku call, so anything queued behind it waits behind a model call — which is how #142 happened. So the 30s still covers what it covered before, no more and no less, and this issue's original complaint stands for that lock. What changed is that the operations #225 was about no longer run under it.

    I am leaving this open rather than closing it, because the question it asks — is the knob doing what its reasoning claims — is now answered for one lock and not the other.

    An incidental result worth recording

    Losing the staging lock skips the append and therefore the truncate, so now.md keeps every byte and the next round re-summarizes it. That is no duplicate at all — strictly better than the pre-existing commit-skip path, which appends first and can duplicate. A timeout on this lock is now a genuinely safe outcome rather than a lesser evil, which is part of why 10s is defensible rather than merely generous.

  2. added 2 commits that reference this issue on Jul 30, 2026
  3. fdaviddpt commented on Jul 30, 2026

    @fdaviddpt
    ContributorAuthor

    Instrument shipped in #244 (9f4fa11). The 30s default is unchanged — that number is the output of the measurement this enables, not part of it, so this issue stays open until there is data.

    What is now possible: REMEMBER_LOCK_TIMING=1 records one TSV row per lock use (ts_ms lock event outcome wait_ms held_ms precision pid) and scripts/lock-timing-report.sh gives per-lock p50/p90/p99/max plus a timeouts count. That last column is the direct answer to this issue — a timeout leaves only a log line today, so the one event that would prove 30s too small is precisely the one missing from any distribution built from holds alone.

    It sits in lib-lock.sh rather than save-session.sh, so staging.lock is measured alongside and #234's hand-polled figures become reproducible rather than a one-off.

    Opt-in, because always-on is self-defeating here: save-session.sh runs on a PostToolUse hook, and #227/#230 are both about spawn cost on that path while #204 is an external report of the plugin blocking a user's prompts. Off costs one string comparison and zero spawns, pinned by a spawn-count test that goes red if the two paths ever cost the same. The log caps at 5000 lines and stops with a # CAPPED line rather than rolling, because for a timeout distribution the tail is the whole point.

    One finding worth recording, because it nearly shipped as coverage that wasn't. The first version's clock-conversion tests injected a reading by assigning EPOCHREALTIME. On bash ≥ 5 that variable is regenerated on every reference, so the assignment never reached the code under test — the tests were green while asserting nothing, on Linux, which is the platform whose precision tier they claimed to cover. macOS's bash 3.2 treats it as an ordinary variable, so it worked there. Caught only because the ubuntu legs went red for the right reason. The conversions are now functions of their argument, which cannot no-op.

    Still outstanding, and why this stays open: nobody has run a working day with it on. Until someone does, 30 remains a plausible-sounding round figure — which was this issue's original complaint and is still true.

  4. fdaviddpt commented on Jul 31, 2026

    @fdaviddpt
    ContributorAuthor

    Measured. The knob is doing less than it claims, and the gap is about 8x.

    Method: the repo's own tests/test_save_session_gates.py harness in a temp dir, pipeline.shell stubbed so no model call is made. No live .remember was touched.

    save.lock hold, non-model floor: p50 1.22s (min 1.12, max 1.50, n=10).
    With a 2s stubbed summarizer: p50 3.19s. The relationship is linear, so hold ≈ 1.2s + summarizer latency.

    The summarizer's own bound is what settles it. haiku.py:491 defaults timeout: int = 120, and spawn_guard.claim(timeout=timeout) at :526 runs inside the lock, before the subprocess that also gets timeout=timeout. So the worst-case hold is ≈ 1.2 + 120 + 120 ≈ 241s.

    30s is right for the common case and cannot cover the tail by construction. A save that waits on the spawn guard, or a genuinely slow Haiku call, guarantees the NDC commit skips and duplicates a span. save-session.sh:789 calls that "the routine outcome of losing this race, not an error", which is fair — but the knob's stated purpose is to avoid it, and at 30 it cannot.

    Two ways out, and they are not equivalent:

    1. Raise the default to ~250s to match the code's real bound. Honest, but it means a genuinely stuck save blocks the next one for four minutes.
    2. Move the spawn_guard.claim outside the lock, which drops the bound to ~121s and halves the worst case without changing the timeout's meaning.

    (2) looks better to me, because the spawn guard is a global-concurrency device and holding a per-file lock across it is what makes the two bounds stack in the first place. But that is a design call on the guard's semantics, not just a constant.

    One finding about the instrument itself, which is arguably part of this issue. The shipped recorder ran at precision s on this machine (macOS bash 3.2.57, no bash 5, no GNU date). Every sub-second hold recorded as 0 or 1000ms. So on the maintainer's own platform the built-in timing can answer the tail question but cannot see the floor at all — the p50 above had to be wall-clocked from Python instead. A measurement tool that silently rounds the thing you are measuring to zero is the same defect class this repo keeps filing, one layer in.

  5. fdaviddpt commented on Aug 1, 2026

    @fdaviddpt
    ContributorAuthor

    Correcting my own 2026-07-31 comment: the arithmetic is wrong, and the option I proposed in it would have changed nothing.

    I wrote that the worst-case hold is 1.2 + 120 + 120 ≈ 241s because spawn_guard.claim(timeout=timeout) runs inside the lock before a subprocess that also gets timeout. claim() does not block. I verified this rather than take it on report — pipeline/spawn_guard.py:213-260 contains no sleep, no wait loop, no poll. timeout is used on exactly one line, :223:

    stale_after = timeout + STALE_GRACE_SECONDS

    — a staleness threshold handed to _census (age < stale_after and _pid_alive(pid)), not a wait. claim() censuses a directory and either raises SummarizerSpawnDeclined immediately or returns a slot. Milliseconds.

    So option (2) in my comment — "move spawn_guard.claim outside the lock, drops the bound to ~121s" — is a no-op. There is nothing there to move.

    The realistic bound is ≈121s (1.2 + 120). The second 120 does exist, but elsewhere: the isolation retry _run(isolate_hooks=False) at haiku.py:584 re-enters subprocess.run with the same timeout — and it only fires when the first attempt exits non-zero, whereas a TimeoutExpired raises RuntimeError at :559 and never reaches it. So ≈241s needs a first attempt that fails slowly and non-zero, which is not the timeout path. My number was accidentally the right ceiling for the wrong reason.

    And the framing I brought to this was wrong too

    I approached this as the same defect class claude-supertool shipped a fix for today (its #702/#658/#716): an outer budget below the inner one it guards. It is not.

    That was a detector whose budget sat under the thing it watched — it could only fire for a slow machine, never for a hang, so a hang read as green. The false green was the defect.

    REMEMBER_NDC_COMMIT_LOCK_TIMEOUT is a patience budget. Firing is the bad outcome, not the catch. A patience budget shorter than the hold it waits on is not blind: it does its job when a holder is genuinely stuck and merely gives up early in the tail. Nothing goes silently green — save-session.sh:836 logs the skip, now.md is untouched, and the cost is a visible duplicate in today-*.md. Opposite failures; the fix that was right there does not follow here.

    Raising the default is not free — measured

    lock_acquire busy-spins: lib-lock.sh:598-599 runs date +%s plus sleep 0.05 per iteration, on top of mkdir, _lock_try_steal and _lock_try_adopt. A contended 5s wait, measured:

    real 4.629s   user 0.389s   sys 0.684s
    

    ≈1.07s CPU per 5s waited — about 21% of a core, continuously. Extrapolated: 30s ≈ 6.4s CPU, 121s ≈ 26s, 241s ≈ 52s. That burn happens in a backgrounded subshell in parallel with the summarize it is waiting on, on the PostToolUse path that #227, #230 and #204 are all about. Deriving the default from the summarizer's bound would roughly quadruple it.

    Closing position: no change, and why

    30s stays. It covers the common case (hold ≈ 1.2s + summarizer latency; a competing save's typical summarize is seconds), and the case it does not cover is one where the competing save is already failing at its own 120s wall — there the NDC skip is a second-order symptom, not the problem.

    Deriving from the summarizer bound is also less self-maintaining than it sounds: the effective 120 comes from shell.py:598's argv default, not haiku.py's signature, so bash would have to spawn Python to ask (a spawn, on that path) or restate the constant — the drift, relocated.

    The real fix remains taking the model call out from under the lock, and it is not a move. save.lock also serialises the summarizers themselves (#168: two saves, two Haiku calls, two position writes; and #204), so calling outside the lock naively reinstates both. It needs the NDC pattern applied to the parent — snapshot, call unlocked, re-acquire, re-validate position, commit — which is a design change to the save path. That is the owner's call, not something to slip in under a tuning issue.

    Leaving this open on that basis rather than closing it: the number is now defended by measurement instead of intuition, which is what the issue asked for, but the structural question it exposed is still open.

    Note for whoever touches the docs: README.md:319 still says save.lock's 30s "still is not" measured, and CHANGELOG.md:306 carries the same framing. Defensible as written — the default is still not derived from a measurement — but the hold now is.

  6. added
    priority:medium
    on Aug 5, 2026
  7. added a commit that references this issue on Aug 5, 2026
  8. fdaviddpt commented on Aug 7, 2026

    @fdaviddpt
    ContributorAuthor

    Closing as answered — the measurement this asked for exists, the conclusion is no change, and the one action item the thread left has already shipped.

    The number: save.lock hold is p50 1.22s on the non-model floor (min 1.12, max 1.50, n=10), rising linearly with summarizer latency — 3.19s against a 2s stub. So hold ≈ 1.2s + summarizer latency, and the real upper bound is ≈121s, not the ≈241s an earlier comment computed: spawn_guard.claim() does not block (pipeline/spawn_guard.py:213-260 has no wait; timeout is used once at :223 as a staleness threshold), which also makes the proposed "move claim out of the lock" fix a no-op.

    30s stays, for two measured reasons: it covers the common case, and raising it is not free — lock_acquire busy-spins (scripts/lib-lock.sh:598-599) at ≈1.07s CPU per 5s waited, about 21% of a core, on the PostToolUse path that #227/#230/#204 are all about.

    And the distinction that says why the org's usual fix does not apply here: this is a patience budget, not a detector. A detector whose budget sits under what it watches renders a hang as green — the false green is the defect. A patience budget firing early is visible by construction: scripts/save-session.sh:836 logs the skip, now.md is untouched, and the cost is a duplicate span in today-*.md. Opposite failure modes. Nothing goes silently green.

    The stale doc line is fixed: 8de00a7 docs: the save.lock hold is measured now, so stop saying it is not (#226) (#310). grep 'still is not' now returns zero across the repo, and README.md:446-452 carries the measured account including the busy-spin cost. CHANGELOG.md:536 still says the measurement had not been taken — that is the released 0.16.0 entry describing what was true when #244 shipped, it is correct, and rewriting it to match a later measurement would falsify the record.

    What is genuinely left is a different issue: take the model call out from under save.lock via the NDC pattern (snapshot → call unlocked → re-acquire → re-validate position → commit). That is a design change to the save path — save.lock also serialises the summarizers themselves (#168, #204), so calling outside it naively reinstates both — and it deserves its own title rather than living on a tuning issue.

  9. added a commit that references this issue on Oct 5, 2026
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

    priority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions