Skip to content

fix(bin): derive each task's presentation cursor offsets once per wake drain - #85

Merged
max-metaplanet merged 5 commits into
mainfrom
fm/wake-drain-shared-cursor
Oct 17, 2026
Merged

max-metaplanet merged 5 commits into
mainfrom
fm/wake-drain-shared-cursor

Conversation

@max-metaplanet

@max-metaplanet max-metaplanet commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Intent

Implement the session-start-slow report's fix 3: share one computed unread/cursor snapshot across fm-wake-drain.sh's presentation sections instead of each re-deriving it via fm-classify-lib.sh. Prove it by before/after timing on a disposable copy, same output.
Context from the session-start investigation: the Claude SessionStart hook hits its 120s bound and truncates in the bootstrap stage. One stage later, bin/fm-wake-drain.sh's sections (print_status_outcome_backstop_section, print_branch_outcomes_section, print_record_divergence_section, print_unread_status_section) each call fm-classify-lib.sh cursor/ident functions per task; the trace showed ~47x more status_presentation_cursor_offset calls than status lines, 26.4s on 108 status lines, and the lead home has ~19x more.

What Changed

  • bin/fm-classify-lib.sh adds a shared presentation cursor index (status_presentation_index_open / status_presentation_index_close). It reads .status-presentation-cursor once for the whole fleet and reuses the identity and size that status_presentation_snapshot already captured. With those inputs it computes each task's presented and backstop offsets once, by calling the existing status_presentation_cursor_offset and status_outcome_backstop_cursor_offset functions. While the index is open, both functions return the stored values. When it is closed they work exactly as before. The index stays closed when there is no manifest yet (the first drain) and when any task's offset cannot be computed, so the original error still comes from the real function.
  • bin/fm-wake-drain.sh print_status_presentation opens the index from its snapshot before the annotation and section passes, and closes it before releasing the presentation lock. The sections, acknowledgement and commit now share one set of offsets instead of each recomputing them per task.
  • New tests/fm-wake-drain-shared-cursor.test.sh uses a multi-task fixture to check:
    • shared offsets match what each function returns on its own;
    • an append made after the snapshot does not move a shared offset;
    • presented lines are not replayed by the next drain;
    • file identity is read once per task, not once per section;
    • the first drain without a manifest still presents;
    • a malformed manifest leaves the index closed and the drain still fails as before.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The index is a memo that gets its values by calling the existing cursor functions, and only print_status_presentation opens it. That function holds the presentation lock and runs in a subshell, and it closes the index before returning. The index stays closed when there is no manifest or any derivation fails, and tasks missing from the snapshot fall back to the uncached path. Nothing writes the manifest before the final commit, so the memoized values cannot go stale within one presentation.

Testing

  • ⏭️ Test - skipped

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

⏭️ **Test** - skipped

Step was skipped.

✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

One drain presents several sections and then acknowledges and commits, and
every one of those passes needed the same two facts per task: the byte offset
already presented and the byte offset the outcome backstop already covered.
Each pass re-read the whole fleet cursor manifest and re-stat'ed the status
file, so a twelve-task home derived the same two answers sixty times.

Add a shared presentation cursor index to fm-classify-lib.sh, opened once per
presentation by fm-wake-drain.sh and closed after the commit. It derives each
task's answers by calling status_presentation_cursor_offset and
status_outcome_backstop_cursor_offset themselves, so those functions stay the
one owner of what the answers are, including every malformed-manifest and
missing-row edge; the derivation reads the manifest once for the whole fleet
and reuses the identity and size status_presentation_snapshot already captured
under the presentation lock. With the index closed, which is how every caller
outside a drain presentation uses them, both functions behave exactly as
before. The index stays closed when no manifest exists yet, because that first
read falls back to each task's legacy cursor file, and when any derivation
fails, so the failure surfaces from the real call rather than from a memo.

Measured on a disposable copy of a 12-task, 108-status-line home: manifest
reads 60 to 1, stat forks 531 to 291, identity derivations 10 to 5 per task,
CPU median 5.76s to 4.14s. Fifteen interleaved before/after pairs were
identical on stdout, stderr, exit status and committed cursor offsets.

What a section reports and when the cursor advances are unchanged.
One drain presents several sections and then acknowledges and commits, and
every one of those passes needed the same two facts per task: the byte offset
already presented and the byte offset the outcome backstop already covered.
Each pass re-read the whole fleet cursor manifest and re-stat'ed the status
file, so a twelve-task home derived the same two answers sixty times.

Add a shared presentation cursor index to fm-classify-lib.sh, opened once per
presentation by fm-wake-drain.sh and closed after the commit. It derives each
task's answers by calling status_presentation_cursor_offset and
status_outcome_backstop_cursor_offset themselves, so those functions stay the
one owner of what the answers are, including every malformed-manifest and
missing-row edge; the derivation reads the manifest once for the whole fleet
and reuses the identity and size status_presentation_snapshot already captured
under the presentation lock. With the index closed, which is how every caller
outside a drain presentation uses them, both functions behave exactly as
before. The index stays closed when no manifest exists yet, because that first
read falls back to each task's legacy cursor file, and when any derivation
fails, so the failure surfaces from the real call rather than from a memo.

Measured on a disposable copy of a 12-task, 108-status-line home: manifest
reads 60 to 1, stat forks 531 to 291, identity derivations 10 to 5 per task,
CPU median 5.76s to 4.14s. Fifteen interleaved before/after pairs were
identical on stdout, stderr, exit status and committed cursor offsets.

What a section reports and when the cursor advances are unchanged.
@max-metaplanet max-metaplanet added the fm-ci-validated Deliberately validated by this repository CI instead of the local no-mistakes pipeline label Oct 16, 2026
The case declares its comparison variables in the outer function, but they
belong to the inner `bash -c` that sources the library and has its own scope,
so ShellCheck reported six SC2034 findings and CI's Lint job failed.

Declare only the two names this shell actually uses. Local changed-file lint
excludes SC2034 by design (bin/fm-lint.sh header), which is why this passed
locally and failed in CI; linting the changed files by explicit path
reproduces CI's posture and now passes.
@max-metaplanet max-metaplanet changed the title perf(drain): derive each task's cursor offsets once per presentation fix(bin): derive each task's presentation cursor offsets once per wake drain Oct 17, 2026
@max-metaplanet
max-metaplanet merged commit 36ef08d into main Oct 17, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fm-ci-validated Deliberately validated by this repository CI instead of the local no-mistakes pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant