Skip to content

Make SessionStart's Python probe lazy, and drop always-on forks in memory injection and bootstrap (#662, #664, #666, part of #660) - #675

Merged
fdaviddpt merged 7 commits into
mainfrom
fix/662
Sep 12, 2026
Merged

fdaviddpt merged 7 commits into
mainfrom
fix/662

Conversation

@fdaviddpt

@fdaviddpt fdaviddpt commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #662
Closes #664
Closes #666

Part of #660.

What was wrong

Three always-on fork sources on the SessionStart foreground path, each independently filed under #660's spawn-budget umbrella:

What changed

  • scripts/detect-tools.sh: a lazy _remember_python() resolver, called only at first actual need and cached the same way the existing PATH-keyed verdict cache already was. Opted into via _REMEMBER_LAZY_PYTHON=1, set only by session-start-hook.sh -- every other sourcer (post-tool-hook.sh, save-session.sh, run-consolidation.sh, doctor.sh) genuinely invokes $PYTHON -m pipeline.shell right after sourcing, so eager detection there is real work, not waste.
  • scripts/lib-memory-context.sh: one batched wc -c per group (present files; compact-mode deferred set; the up-to-10 rotated slices actually printed), parsed via plain read; basename replaced with ${MFILE##*/}; rotated-slice listing via a glob array instead of ls | sort.
  • scripts/session-start-hook.sh, scripts/bootstrap-dirs.sh: the prune, staging count and stale-config sweep skip their pipelines when a glob check shows nothing to do; the staging count is a glob array walked with a case, zero forks.
  • scripts/lib-clock.sh: the bash >= 4.2 printf '%(FMT)T' builtin now handles %s (POSIX strftime, not the GNU extension it was lumped with); builtin and date +%s are pinned byte-identical by the new test.
  • scripts/lib-memory-dir.sh, scripts/lib-slug.sh, scripts/log.sh: a declare -f _remember_python >/dev/null 2>&1 && _remember_python guard immediately before each existing python spawn -- a builtin check, no new spawn, a no-op outside lazy mode.
  • changelog.d/662.changed.md; tests/test_session_start_promo_spawn_budget_660.py (+313 lines, 6 tests).

Self-review findings (developer lane)

  • oss:auditor caught that the first commit used declare -A in lib-memory-context.sh. bash 3.2 silently accepts declare -A as a plain indexed array, then dies with syntax error: operand expected at the first ${arr[$key]} lookup, so the whole MEMORY section render aborted with nothing injected. Fixed in 8e2322f with printf -v + ${!name} indirection (the pattern lib-lock.sh already uses), verified against the real /bin/bash 3.2.57. A static regression test greps scripts/ for any reintroduced declare -A; the first single-regex version of that check did NOT fire when the bug was deliberately reintroduced, so it was rewritten as a two-stage grep and re-verified to fire.
  • Explore reviewer spawn never returned; nothing from it is folded in. Treat the branch as reviewed by the auditor plus the maintainer pass below.

Maintainer review (this PR's third commit, 80b8013)

The developer lane ran its own test file but not the whole suite. The whole suite (2370 passed, 47 skipped, 1 failed + 1 collection error on macOS, 10m45s) caught two guards the change moved out from under:

Static: grep -rn 'declare -A' scripts/ finds only the comment explaining why not; bash -n under /bin/bash 3.2.57 is clean for every touched script.

CI round 1 (80b8013): 12/12 pytest legs red, all on the review commit, not the developer's diff

Every pytest leg on all three OSes failed the same two tests in tests/test_sanctioned_divergence_state_440.py -- the unit tests of the allowance helper, which unpacked _SANCTIONED_DIVERGENCE[rel] as a single tuple after 80b8013 had made it a list. The developer's own change was otherwise green across the matrix (ubuntu: 2405 passed; windows: 1049 passed, 1371 skipped). 80b8013 had been pushed on a targeted run of the #298 file rather than the whole suite -- the same lapse I called out in the developer's lane, repeated here. f8521e0 fixes the three tests for the list shape, adds a mixed-state test the list newly makes possible, and this time the whole suite ran first: 2372 passed, 47 skipped on macOS.

CI round 2 (f8521e0): 12 green, 4 red -- the macOS legs, and this one IS the developer's diff

All four macos-latest legs failed tests/test_start_context_cache_668.py::test_compact_mode_never_reads_or_writes_the_context_cache with rc 127 and an empty stderr. Reproduced on /bin/bash 3.2.57 with the render's stderr visible: lib-memory-context.sh: line 127: _remember_present[@]: unbound variable. At compact every non-identity file is skipped, so with no identity file the batched-wc present list is empty, and for MFILE in "${arr[@]}" over an empty array is a fatal unbound-variable error under set -u on every bash before 4.4 -- the #668 publish path runs under it. bash 5 (the developer's Homebrew bash, the Linux and Windows legs) has no such error, which is why the developer's "observed on macOS" claim and my own full-suite run both missed it: both ran under bash 5. 5773e62 count-guards the three loops that could see an empty array (every other new [@] expansion was already behind a ${#arr[@]} -gt 0 check) and adds a regression test that runs the compact render under set -u on the real /bin/bash when it is below 4.4, verified to fail with the guard removed. Whole suite with /bin/bash first on PATH (CI's macOS shape): 2372 passed, 1 failed -- test_log_sh.py's does-not-parse diagnostic, which fails identically on main under that PATH and passes on CI's macOS legs, so it is not this change's.

Cross-platform claims

  • Observed: macOS (Darwin 24.3) -- full suite under Homebrew bash 5, and again with /bin/bash 3.2.57 first on PATH after round 2; both bash-3.2 crashes (declare -A, empty array under set -u) reproduced and fixed directly.
  • Reasoned, not observed: Linux and Windows/Git-Bash. The diff uses only POSIX sh / bash-3.2 constructs (glob arrays, parameter expansion, printf -v, indirect expansion, wc -c); %s in lib-clock.sh rests on %s being POSIX strftime rather than a GNU extension. CI's 3-OS x 4-interpreter matrix is the evidence for those; nothing in the diff was run there.

Generated with Claude Code: https://claude.com/claude-code
https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5

[AI-generated]

fdaviddpt and others added 7 commits September 12, 2026 05:49
…mory injection and bootstrap (#662, #664, #666)

detect-tools.sh unconditionally probed python3/python/py on every SessionStart
even though $PYTHON is only read by four jq-less fallback call sites that never
fire when jq is present. A new _remember_python() resolver defers that probe to
first actual use, opted into only by session-start-hook.sh (every other sourcer
genuinely needs $PYTHON right after sourcing).

lib-memory-context.sh's MEMORY section render forked one wc + one tr + one
basename per memory file; now batches wc -c once per group and uses parameter
expansion for basename. The rotated-slice listing uses a glob array instead of
ls | sort.

session-start-hook.sh's capture-alive.d prune and staging-file count, and
bootstrap-dirs.sh's stale-config sweep, now skip their fork-heavy pipelines
entirely when a cheap glob-array check shows there is nothing to do.
lib-clock.sh's builtin clock path now accepts %s (real POSIX strftime, not the
GNU-only extension it was lumped in with) instead of always shelling out to
date for it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…found in self-review

`declare -A` is silently ACCEPTED by bash 3.2 (it creates a plain indexed
array instead of erroring), so the fault surfaces one step later at the very
next `${arr[$key]}` lookup -- a fatal "syntax error: operand expected" that
aborts _remember_render_memory_section with nothing injected and nothing
visible beyond stderr. Stock macOS /bin/bash is 3.2.57 and this repo
documents it as a supported floor (lib-clock.sh, lib-lock.sh's own
_lock_timing_key comment) specifically because it lacks associative arrays.

Replaced with printf -v + indirect (${!name}) expansion, the same
named-variable-per-key pattern lib-lock.sh already uses for the identical
reason, confirmed directly against the real /bin/bash 3.2.57 on this machine.
Added a regression test: a static two-stage grep that fires on any
`declare -A` reintroduced under scripts/ (the first, single-regex attempt at
this silently failed to fire on a line where declare -A is the first token --
caught only by deliberately reintroducing the bug and finding the check
stayed green), plus a functional run against the real /bin/bash when it
actually identifies as bash < 4.

Found by the oss:auditor self-review spawn against the first commit on this
branch, before anything was pushed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…und in the full suite

The developer lane ran its own test file but not the whole suite; the
whole suite caught two collection/assertion failures in tests that pin
the code this change edits:

- tests/test_windows_glob_backslash_517.py extracted two blocks by a
  trailing `ls ...` marker that #666 replaced with a glob array. The
  end markers now name the new last line of each block (the `unset` in
  session-start-hook.sh's staging count; the HAS_MEMORY assignment in
  lib-memory-context.sh's rotated-slice listing). All 23 tests pass
  against the extracted blocks, so the #517 backslash property holds
  for the rewritten code too.

- tests/test_case_divergence_298.py byte-pins lib-slug.sh and
  lib-memory-dir.sh to origin/main so nothing adds a spawn to the
  per-tool-call path. The #662 lazy-PYTHON guard is a `declare -f`
  builtin check placed immediately before an existing python spawn,
  on branches that only run when that spawn runs -- no new spawn --
  so it is sanctioned via the test's own _SANCTIONED_DIVERGENCE
  mechanism, which now takes a list of substitutions per file since
  lib-memory-dir.sh carries #429's as well. Positive control: an
  unsanctioned `git rev-parse` inserted next to the guard still fails
  the test.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…SANCTIONED_DIVERGENCE

CI on #675 failed 12/12 pytest legs on the same two tests in
tests/test_sanctioned_divergence_state_440.py: they unpacked
_SANCTIONED_DIVERGENCE[rel] as a single (old, new) tuple, and 80b8013
turned it into a list so lib-memory-dir.sh can carry both #429's and
#662's allowance. 80b8013 was pushed on a targeted run of the #298
file only; the whole suite would have caught this before CI did, and
did this time: 2372 passed, 47 skipped on macOS.

The three tests now build their synthetic origin/main text from every
pair for the file and assert each pair on its own; a fourth pins the
mixed state (one allowance landed, another still open) that the list
shape newly makes possible, and skips itself when a file carries only
one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…he compact render (found by CI's macOS legs)

PR #675 round 2: all four macos-latest pytest legs failed
tests/test_start_context_cache_668.py::test_compact_mode_never_reads_or_writes_the_context_cache
with rc 127 and an empty stderr. Reproduced on /bin/bash 3.2.57 with
the render's stderr visible:

    lib-memory-context.sh: line 127: _remember_present[@]: unbound variable

At SESSION_START_SOURCE=compact every non-identity file is skipped, so
with no identity file the batched-wc present list is empty, and
`for MFILE in "${arr[@]}"` over an EMPTY array is a fatal unbound-
variable error under `set -u` on every bash before 4.4 -- which the
#668 publish path runs under. bash 5 (the developer lane's Homebrew
bash, and the Linux/Windows legs) does not have the error, which is why
it reached CI.

Three loops count-guarded: the main present-file loop and the compact
deferred-list loop in lib-memory-context.sh, and the staging-count loop
in session-start-hook.sh (an empty staging dir is the common case).
Every other new `[@]` expansion in the diff was already behind a
`${#arr[@]} -gt 0` check.

Regression test in tests/test_session_start_promo_spawn_budget_660.py
runs the compact render under `set -u` on the real /bin/bash when it is
below 4.4 (skips with the reason stated elsewhere), with a positive
control on the same bash; verified to fail with the guard removed.
Whole suite with /bin/bash first on PATH (CI's macOS shape): 2372
passed, 47 skipped, 1 failed -- tests/test_log_sh.py's
does-not-parse diagnostic, which fails identically on main under that
PATH and passes on CI's macOS legs, so it is not this change's.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…ent lazy mode in detect-tools.sh

The first draft of test_capture_seen_prune_is_gated_on_the_threshold ran only
with no capture-alive.d at all -- a shape that never forked under the old code
either, so it would have passed against it. Seed 2 entries for the negative
half and CAPTURE_SEEN_KEEP+1 for a paired positive control that must see the
`ls -t` prune fire and leave exactly CAPTURE_SEEN_KEEP behind (CLAUDE.md: a
negative assertion needs a positive control). Found by the Explore self-review
on #675.

detect-tools.sh's header now names the _REMEMBER_LAZY_PYTHON=1 mode #662
added, and what PYTHON and the exit code mean under it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…of executing an empty $PYTHON

Review finding on #675: with _REMEMBER_LAZY_PYTHON=1 and no interpreter on
PATH, _remember_python returns 1 and _jq_fallback went on to run `$PYTHON -`
as the empty word -- `-: command not found`, exit 127, empty output. The
callers defaulted correctly either way; this makes the failure the plain
return 1 it was meant to be.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant