Skip to content

perf(context-guard): the PostToolBatch rewritten-snapshot case is unmeasured and likely still starts the resolver (follows #4392) #5298

Closed as not planned

Description

@kyle-sexton

Follows #4392 (closed). Parent: #4373. Found by the 2026-09-29 audit of the unattended Cursor run.

What #4392 required

The triage brief: "a PostToolBatch fire with no zone change costs 1 process whether or not the snapshot was rewritten", plus a Windows/WSL re-run of the census.

What shipped (#5050, #5088)

zone-crossing-inject.sh (cg_try_coalesce) reuses the cached body only when cached_body equals SNAP_BODY, or, with no zones.json, when only used_percentage moved inside the same shipped band and every other byte is equal.

Between two PostToolBatch fires a model call happens, so the token fields and current_usage normally move and the body differs. The rewritten-snapshot case therefore probably still starts the resolver. This is an inference from the code and the synthetic fixtures; there is no live measurement either way, and both closing PRs verified fixtures on a Linux host only.

Fix PR #5297 (Refs, not Closes) makes Windows-shaped payloads (backslashes in transcript_path and cwd) eligible for the no-jq path and pins the hook's copies of the shipped bands and staleness window to the resolver. It does not change how the rewritten-snapshot case skips the resolver.

Owner steps (operator action, not doable unattended)

  1. On the Windows Git Bash host, run scripts/hook-census.sh context-guard PostToolBatch zone-crossing-inject.sh with a real payload whose transcript_path and cwd carry backslashes, on main and on fix/audit-context-guard (harness lineage melodic-software/dotfiles#873).
  2. In a live session, compare zone-crossing-inject PostToolBatch hook durations before and after the 0.7.85 release from the local OTEL store (/claude-ops:observability hook latency). The resolver path measured about 90 ms on Windows and the fast path a few ms, so the distribution shows the share of fires that still resolve.

Decision packet (owner-reserved)

Question: how should the rewritten-snapshot case skip the resolver?

  • A) The hook computes bands itself from used_percentage plus token occupancy, window class and version gate. A third copy of resolver logic, parity-test burden, no cross-plugin sync.
  • B) Resolver-owned reuse bounds: change the canonical scripts/context-zone.sh to expose the interval in which the zone cannot change. Single authority, but sync-context-zone.sh copies it into plugins/plugin-quality and --check-bump forces that plugin's bump, so it is a cross-plugin change.
  • C) The tee computes and caches the zone at write time (the issue's option 2). Moves cost into the statusline process that perf(context-guard): stop spawning a dozen processes on every statusline render #4675 just slimmed, and needs the resolver or a copy there.
  • D) Measure first with the steps above and change nothing until the share of resolving fires is known.

RECOMMENDED: D, then B if the share of resolving fires is high. Basis: judgment plus the code reading above (A duplicates logic the resolver owns, C works against #4675, and no measurement exists yet).

Unblocks: the rewritten-snapshot acceptance criterion of #4392.

Two conditional deletions that wait on the same measurement

They point in opposite directions:

  • Delete the captured_at-only coalesce arm if it fires rarely (the tee's 60 s no-change floor already suppresses that non-event, so three layers suppress it).
  • Delete the percentage-band branch cg_pct_placeholder/cg_same_percentage_band if it never fires live (used_percentage derives from token fields that also change), or keep it if option A or B builds on it.

Informational

The PLAN.md k x S bands (p50 <= 4 x S, p95 <= 5 x S) were written by the implementing agent and the owner merged #5088. Edit here if different values are wanted.

Acceptance

Activity

  1. added
    needs-owner-decision
    needs-human
    on Sep 29, 2026
  2. kyle-sexton commented on Sep 29, 2026

    @kyle-sexton
    MemberAuthor

    Owner decision (2026-09-29): Option D, measure first and change nothing yet, then Option B (resolver-owned reuse bounds in scripts/context-zone.sh, synced into plugins/plugin-quality) if the share of PostToolBatch fires that still start the resolver is high. The owner accepted the recommendation with no note.

    Next: the measurement needs the Windows Git Bash host and the local OTEL store, so the owner runs the two steps under "Owner steps" and records the census and hook-duration numbers, with the resolving-fire share, on this issue. The issue keeps needs-human. Once the share is recorded, an agent implements B (or A/C) and the two conditional deletions.

  3. removed
    needs-owner-decision
    on Sep 29, 2026
  4. kyle-sexton commented on Sep 30, 2026

    @kyle-sexton
    MemberAuthor

    Resolution (2026-09-29, owner-approved consensus)

    Ran the approved plan (fleet run, read-only) on melo-desk-001. It answers part of the plan and leaves the acceptance criteria open. Nothing was installed, deleted, fetched or changed; the only write was a scratch dir, C:\Users\KyleSexton\claude-lane-sandbox\issue5298\ (SQL files).

    Reach. The windows-shell alias to this same host fails from the WSL lane: ssh.exe melo-desk-001 reaches sshd, then Host key verification failed. (no pinned key for the host itself). Fixing it edits known_hosts, which I did not do. I ran the same read-only commands through WSL interop instead (powershell.exe, Windows lane, hostname = melo-desk-001).

    Probe. Git Bash 2.55 is present (C:\Program Files\Git\bin\bash.exe), as are duckdb.exe (WinGet Links) and jq. Checkout D:\repos\github.com\melodic-software\claude-code-plugins exists but is main...origin/main [behind 436] at bf8aaca78 (2026-09-24). Bringing it current is a fetch/pull, so I left it. OTEL store: C:\ProgramData\local-otel\cc-store\cc-logs.json (hot, 2026-09-22 to now) plus a cold parquet.

    Step 2, census: cannot be run on Windows. scripts/hook-census.sh needs Linux strace -ff -qq -s 400 -e signal=none -o ... (the script marks itself Linux-only, lines 65-66). Git Bash ships cygwin strace 3.6.9:

    $ bash -lc "strace -ff -qq -s 400 -e signal=none -o /tmp/x.trace true"
    strace.exe: unknown option -- s
    

    So there is no spawns-per-fire number for backslash payloads from this host, and the Windows census that #4392 asked for is not met. It needs a Windows-capable counter, or a WSL-lane run with a Windows-shaped payload, which does not measure Windows process cost.

    Step 3, hook durations: the store cannot isolate this hook. hook_execution_complete for PostToolBatch carries total_duration_ms and num_hooks (always 2) with no hook script name. The two registered PostToolBatch hooks are claude-ops session-event-log.sh and context-guard zone-crossing-inject.sh, run in parallel, so the value is the slower of the two. Batch total_duration_ms, split at merge times (#5088 merged 2026-09-29T00:10Z, #5297 at 14:57:54Z; I found no separate release timestamp for 0.7.85, so merge times stand in for it):

    lane     period          n      p05    p50    p95    pct>40ms
    windows  before #5088    1193   79.0   102.0  275.2  100.0
    windows  #5088-#5297     582    47.0   57.0   92.0   100.0
    windows  after #5297     98     93.9   114.0  142.1  100.0
    wsl      before #5088    14192  3.0    21.0   51.0   8.4
    wsl      #5088-#5297     26984  23.0   32.0   120.0  32.9
    wsl      after #5297     37508  26.0   45.0   206.0  59.9
    

    Every Windows batch is over 40 ms, even the 5th percentile, so the proposed 40 ms fast/resolving cut-off cannot separate the paths: node + exec-bash.mjs + bash startup alone costs more than that on Windows, for both hooks. The resolving-fire share is not computable from this store. Only 98 Windows fires fall after #5297, and the 0.7.90 install landed 2026-09-29T20:07Z, so the sample after the real fix is smaller still. The WSL rows mix this session with others and show the batch getting slower after #5088, but that is not attributable to one hook.

    Against the acceptance criteria

    • Owner picks A/B/C/D: the owner picked D, then B if the share is high, on 2026-09-29. Unchanged here.
    • Windows census and hook-duration numbers recorded, with the resolving-fire share: not met. The census is not runnable on Windows, and the share is not derivable from the batch-level OTEL field.
    • Rewritten-snapshot criterion of perf(context-guard): zone-crossing-inject fires 16 times per turn on PostToolBatch #4392 met or waived: not met, not waived (owner-reserved).

    The issue stays open with needs-human. What would produce the number: (1) a per-hook duration source, for example zone-crossing-inject.sh recording its own elapsed time and path (fast, coalesced or resolving), or hook-level spans if cc-traces.json starts carrying them (it is empty now); or (2) a Windows-capable spawn counter for hook-census.sh. Either is a code change and an owner decision, so option B is not started.

  5. kyle-sexton commented on Oct 1, 2026

    @kyle-sexton
    MemberAuthor

    Owner decision (2026-10-01): approved the recommendation ("go with recommended", the owner's reply to the batch decision page that listed this question and its recommendation).

    Question: the measure-first step cannot run as planned (the Windows census fails and the OTEL store cannot isolate this hook), so which route gets the resolving-fire share?

    Chosen: option A. A small instrumentation PR (Refs #5298, not Closes) makes plugins/context-guard/hooks/zone-crossing-inject.sh log one telemetry record per fire with its path (fast, coalesced or resolving) and elapsed ms, reusing START_EPOCH and hook::emit_telemetry. After a few days of data on melo-desk-001, the share is recorded here, and the 2026-09-29 decision (then option B if the share is high) governs from there.

    Next: an agent opens the draft instrumentation PR and bumps the context-guard version. Labels changed to agent-ready.

  6. added
    agent-ready
    and removed
    needs-human
    on Oct 1, 2026
  7. kyle-sexton commented on Oct 1, 2026

    @kyle-sexton
    MemberAuthor

    Measurement attempt, 2026-10-01. I could not produce the Windows share, and a few days of data would not produce it either until a sink change lands. Nothing from the 2026-09-29 decision (option B if the share is high) follows yet. This issue stays open.

    The 2026-10-01 decision chose option A: the instrumentation PR (#5667, merged 14:01Z today, shipped as context-guard 0.7.96) plus a few days of data on melo-desk-001, then the share is recorded here.

    What I checked on melo-desk-001

    • Plugin version on the Windows lane: context-guard 0.7.92 at user scope. The per-fire records arrive in 0.7.96, so that lane emits none. The other Windows-lane entry I found is 0.7.65 (project scope).
    • Store: the Windows checkout (D:\repos\github.com\melodic-software\claude-code-plugins, at bf8aaca78 from 2026-09-24) has HOOK_TELEMETRY_SINK wired in its settings but no .observability/claude folder at all, so no hook telemetry has ever been written there.
    • Time: the instrumentation is about 7 hours old. The decision asks for days.
    • No fire is already recorded anywhere I can read. The WSL lane's store (1,829 hook rows) has no zone-crossing-inject row. That lane's user-scope context-guard is 0.8.0, which includes the instrumentation, but it was updated at 16:23Z today, so sessions that started earlier ran older code. I did not establish why no row exists yet.

    The finding that matters most: the reference sink drops path.

    .claude/hooks/hook-telemetry-sink.sh (and the claude-ops copy) store only hook, exit_code, subject, tool and changed from the envelope's data. The hook's data.path (fast, coalesced or resolving) never reaches the store. I confirmed it by running the installed 0.8.0 hook once with a capture sink, then feeding that envelope to the repo's sink:

    envelope sent by the hook:
    {"schema_version":"1.1","timestamp":"2026-10-01T21:05:12Z","hook":"zone-crossing-inject","hook_event":"PostToolBatch","status":"ok","duration_ms":3,"session_id":"s5298","data":{"path":"fast","zone":"unknown"}}
    
    line the sink wrote:
    {"ts":"2026-10-01T21:05:12Z","session_id":"s5298","hook_event_name":"PostToolBatch","status":"success","duration_ms":3,"source":"envelope","hook":"zone-crossing-inject","exit_code":0,"subject":"","tool":""}
    

    So with the sink as it is, the store keeps each fire's own duration_ms and nothing about which path it took. The share could only be inferred from the duration distribution. The 2026-09-29 comment found that the batch-level duration cannot separate the paths on Windows, because it includes node and bash startup. The hook's own duration_ms leaves that startup out and might separate them, but there is no Windows data yet to show it.

    What is left, in order

    1. Code change (agent can do it, Refs perf(context-guard): the PostToolBatch rewritten-snapshot case is unmeasured and likely still starts the resolver (follows #4392) #5298, not Closes): make the sink keep the path. Smallest options: (a) the sink copies data.path into its record as path, in both the repo-local sink and the claude-ops reference sink; or (b) the hook also sends the path as data.subject, which the sink already stores. The owner picks, or says "agent's choice".
    2. Operator step on melo-desk-001's Windows lane: update context-guard to 0.7.96 or later (claude plugin update context-guard@melodic-software). I did not do this: it is a user-scope change on that host, the task did not name it, and it cannot produce the share until step 1 is released. Sessions must then run in a checkout that wires the sink; the D: checkout is far behind main, and bringing it current is the owner's call.
    3. After a few days of Windows use: compute the share of PostToolBatch fires per path from the store and record it here. If it is high, option B follows per the 2026-09-29 decision.
  8. kyle-sexton commented on Oct 4, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during work-loop.

    Which work class should this item carry?

    This item carries agent-ready but no work-class: label, so the admission gate fails closed and human-gates it. The lane removed agent-ready and applied needs-human. Stamping one dispatchable class (read-only, mechanical, or scoped) and flipping the role back through /work-items:attend-queue makes it dispatchable.

    Lane: work-loop, instance melo-worker-1, cycle 1.

  9. added
    needs-human
    and removed
    agent-ready
    on Oct 4, 2026
  10. self-assigned this
    on Oct 4, 2026
  11. kyle-sexton commented on Oct 4, 2026

    @kyle-sexton
    MemberAuthor
    No description provided.
  12. kyle-sexton commented on Oct 4, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during attend-queue.

    Operator direction (2026-10-04): "go with the recommendations." This records that decision.

    Escalation answered: park behind draft PR #6152, priority low, no class stamped. #6152 deletes zone-crossing-inject.sh and its PostToolBatch registration, which removes this cost entirely. Close this as superseded when #6152 merges. Revisit, and do not start option B, only if #6152 is abandoned.

    The hypothesis holds by code reading and a sandbox probe. cg_try_coalesce (zone-crossing-inject.sh:689-727) reuses the cached zone only when the snapshot is byte-identical or only used_percentage changed. The status line rewrites the token fields after each model call, so most PostToolBatch fires resolve. The share is unmeasured: the telemetry sink still drops data.path (.claude/hooks/hook-telemetry-sink.sh:76-78,133).

    Found: the percentage-band arm can never fire on real snapshots, because remaining_percentage always moves with used_percentage. The test fixture holds remaining_percentage fixed (zone-crossing-inject.test.sh:48), which hides this. If #6152 is abandoned, fix it together with the sink change, as a C3 item. Research: attend-queue slice intake-5298.

  13. removed their assignment
    on Oct 4, 2026
  14. kyle-sexton commented on Oct 13, 2026

    @kyle-sexton
    MemberAuthor

    Decision recorded. The owner approved applying independently verified recommendations on 2026-10-12.

    Asked how the PostToolBatch zone hook could skip the resolver when the snapshot was rewritten. #6152 (merged 2026-10-04) moved context-guard onto mods and deleted the shell hook, which removes the cost the issue measured.

    Verified by an independent fresh-context check (confirmed, risk low): gh issue view 5298 (OPEN; owner direction 2026-10-04: close as superseded when #6152 merges); gh pr view 6152 MERGED 2026-10-04; origin/main plugins/context-guard/hooks: no zone-crossing-inject.sh; hooks.json registers only PostCompact and SessionStart plus modules; register.tsx resolves the zone in-process; origin/main plugins/context-guard/CHANGELOG.md:210

    Closing: Superseded: #6152 merged and deleted zone-crossing-inject.sh; the owner's 2026-10-04 decision said close when #6152 merges

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

    needs-human

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions