Skip to content

feat(session-flow): skip the observer's analysis for short sessions and record its usage - #18951

Merged
kyle-sexton merged 3 commits into
mainfrom
feat/observer-min-session-size
Nov 25, 2026
Merged

kyle-sexton merged 3 commits into
mainfrom
feat/observer-min-session-size

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

No related issue: owner turned the observer on for every session (melodic-software/dotfiles#1803); this keeps short automated sessions from each costing an analysis run and makes the cost measurable.

Summary

The session-flow detached observer now skips its post-session analysis for short sessions and records each run's token usage.

Fix

  • New option observer_min_lines (default 300; 0 analyzes every session): below it, the observer logs and records analysis: {"skipped": ..., "lines": N} in its status file, runs no claude -p and writes no ledger. Observations are kept, so a resumed session counts its earlier lines. Threaded through observer-arm.sh, arm_observer.py, the running-retro arm block, plugin.json and the Config table in reference/observer.md.
  • Each analysis run's reported usage (input, cache creation, cache read, output tokens, duration_ms, total_cost_usd) is merged into the status file and logged, failed runs included.
  • Basis for 300: a full session replayed on 2026-11-24 distilled 3,128 lines (status-replay-de32eb16.json lines_observed); automated sweeps that run as interactive sessions are far shorter. It is a default the user can change.

Verification

  • observer.test.sh: 93 tests OK, 5 new (below the threshold skips and records; at the threshold runs; unset takes the plugin.json default; usage stored; usage stored on a failed run). With the check disabled the skip test fails, and with < changed to <= the at-threshold test fails; both restored.
  • validate-plugins.sh --only session-flow, changelog fragments, sync-plugin-options-docs.py --check, ruff check and shellcheck pass.

🤖 Generated with Claude Code

kyle-sexton and others added 2 commits November 24, 2026 20:27
…ord run usage

Add observer_min_lines (default 300): the detached observer skips the
post-session claude -p analysis when fewer distilled transcript lines are
pending, records the skip in its status file and log, writes no ledger,
and keeps the observations so a resumed session counts them. Threaded
through the SessionStart hook, the running-retro arm block, the launcher
and observer.py; the default lives only in plugin.json.

Each analysis run that returns JSON, failed runs included, now writes its
reported usage tokens, duration_ms and total_cost_usd under the status
file's analysis key and one log line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T01:31:39.998916Z 0faaf1c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0faaf1c599

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

PR body contract — issue linkage

This PR body does not yet satisfy the issue-linkage contract:

  • Missing a "## Related" section. List related PRs, ADRs, or decision-log entries this PR does not close.

Edit the body and this comment updates itself on the next run.

@github-actions github-actions Bot added the needs-issue-linkage Pull request body has no issue linkage line; set by the check-contract step, advisory. label Nov 25, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-review: 2 inline finding(s) on 0faaf1c5994581d79c37700950687b75c70d5b65; the summary is in this lane's pull request comment.

claude-lane: claude-review

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 7bb7bf78f8cf09743c9591acf61718509a5e8e37: read 7 of 7 changed file(s); 0 finding(s), 0 as inline comments.

Code review (incremental, blocking only): 0faaf1c5 → 7bb7bf78

This lane reviewed an earlier head, so this review reports blocking findings only. I read every hunk of all 7 files in the incremental diff. I also checked the code they call at head: run(), _tail_until_idle's status writes, _write_status, _prior_status, _find_session_ledger and _cleanup_observations.

Both earlier findings are fixed:

  1. The resumed session's short tail is fixed. _run_analysis now applies the min_lines gate only while _find_session_ledger() is None (observer.py#L653-L662). The docstring says the observations are deleted only after a run that consumed them, and the code matches it. run() calls _cleanup_observations() only when _run_analysis returns True. Before any ledger exists, that happens only for an empty observations file, so the pending line count really is the session's total. A follow-up to an analyzed session always runs. test_short_follow_up_to_an_analyzed_session_is_analyzed would fail if this broke.
  2. Usage lost on resume is fixed. Runs and skips now go into self.analysis_runs. That list is loaded from the prior status at construction and re-attached on every _write_status call, merge or not (observer.py#L623-L633). Two tests would fail if this broke: test_analysis_runs_survive_a_poll_write_and_a_new_observer covers the per-poll rewrite and a re-arm, and test_every_analysis_run_survives_later_status_writes covers two runs in one session.

The changelog fragment, the README, reference/observer.md and the manifest description now describe what the code does.

No closing issue: the PR links none, so there are no acceptance criteria to check.

Spend: running analysis on every short follow-up to an analyzed session adds prepaid claude -p runs that the earlier head skipped (the base branch also ran them). That is subscription or credit usage, not a hold.

Loosened checks would be marked: fail (lane default, because the prompt has no loosened_check_verdict: or loosened_check_patterns: lines); patterns: 0. The prompt has no loosened_check_decision_record: line either, so I did not check the citation requirement. No loosening found: the tests now check against a recorded claude -p result fixture, and none of their checks is weaker than before.

I ran nothing. Any test results in the PR body are the author's claims and unverified.

Advisory and manifest lines (2)
  • manifest userConfig.observer_min_lines: pass, only the description changed (default 300 and min 0 are unchanged), and it now matches the gate the code applies
  • manifest version: pass, the changelog fragment still declares bump: minor

Verifier dropped 0 of 0 candidates.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-security-review: 1 inline finding(s) on 0faaf1c5994581d79c37700950687b75c70d5b65; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 7bb7bf78f8cf09743c9591acf61718509a5e8e37: read 7 of 7 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #18951, incremental since 0faaf1c5

This review covered only what changed since the last head. It was blocking only, because this lane already reviewed an earlier head. I read all 7 files in .claude-lane/incremental.diff and found no security issues.

What I checked

  • observer.py size gate. The observer_min_lines gate now applies only while the session has no ledger (_find_session_ledger() is None). A follow-up to an analyzed session is always analyzed. This changes how often the observer's own local claude -p run happens. It opens no input path and sends nothing new off the machine. The skip still returns False, so the observations are kept, as before.
  • The new analysis_runs list. It is loaded through _prior_status() from the observer's own status-<session>.json in its local work dir. That function now also rejects a status file whose top level is not a JSON object. Entries are written with json.dumps and are never executed, interpolated into a shell string, or used as a path or URL. _record_usage still copies only the same fixed usage keys, duration_ms and total_cost_usd. No token, secret or credential is involved.
  • The new fixture fixtures/claude-p-result.json. It is a sample claude -p result body with all-zero UUIDs and a placeholder credential_expires_at. It contains no real credential or secret.
  • Docs and changelog (README.md, reference/observer.md, the .changes fragment). These are prose updates that match the code change. No standing instruction is removed or softened.
  • Trust controls. No trusted path, trust control, token scope, kill switch or merge hold is touched in this increment.
  • Manifest. The only manifest change is the description of userConfig.observer_min_lines, which is in the inert set. The flag on adding the key was posted at 0faaf1c5 (thread). The key's type, default (300) and min (0) are unchanged in this increment, so I did not raise it again here.

Spend: follow-up sessions after an analyzed one now always get a claude -p analysis run. That uses prepaid or subscription usage only and sets no billing setting, so it is not a hold.

critical-findings: 0

Manifest lines (1)
  • manifest userConfig.observer_min_lines.description: pass

…ry run's usage

The size gate now applies only to a session with no ledger yet, so a
short follow-up to an analyzed session is analyzed instead of skipped.
Run and skip records accumulate in the status file's analysis_runs list,
which every status write carries, so per-poll rewrites, resumed watches
and re-arms no longer drop earlier runs. The analysis-run tests now
derive their JSON from a real captured claude -p --output-format json
result, ids and expiry blanked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 347bbf6 Nov 25, 2026
63 checks passed
@kyle-sexton
kyle-sexton deleted the feat/observer-min-session-size branch November 25, 2026 02:09
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) needs-issue-linkage Pull request body has no issue linkage line; set by the check-contract step, advisory.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant