Skip to content

feat(sessions): one snapshot per session per pass, and Sessions::Checks for the quota scan's checks (task #211, step 1) - #671

Merged
NOX73 merged 5 commits into
developfrom
feature/session-checks
Nov 9, 2026
Merged

NOX73 merged 5 commits into
developfrom
feature/session-checks

Conversation

@NOX73

@NOX73 NOX73 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

Step 1 of task #211 (decision 11.1): the mechanism, the interface, and the four checks that ran on the quota scan's read. Step 2 (the no-output watchdog and the token-refresh sweep) is a separate PR stacked on this one.

The mechanism: Sessions::Monitor. session_monitor_workflow (replaces quota_error_scan_workflow, every minute, now with overlap: skip) runs Activities::Workflow::MonitorSessionsActivity. For each live workflow step (running/ready, with a container) a pass:

  1. asks each check applies_to?; a session no check applies to is not read;
  2. collects the parts the applicable checks need;
  3. reads them with one exec (Sessions::Snapshot.read → Snapshot::ExecSource, through ContainerRuntime.for_session, capped at scan_op_timeout);
  4. hands the snapshot to the checks in order until one stops;
  5. writes the checks' kept state in one UPDATE (Checks::KeptState).

Every pass logs [Sessions::Monitor] pass read=<n> duration_ms=<ms> … and returns read and duration_ms with its counts. Monitor.snapshot gives the same read on demand (step 2 uses it).

The parts registry: Sessions::Snapshot::Parts::REGISTRY. scrollback (capture-pane -S -1000), log_mtime (stat of /tmp/terminal_output.log) and clients (tmux list-clients with the container clock). Snapshot::Command builds one script from the requested parts. Each part sits between a header and a footer that carries its exit status and a random marker for each read, so one failing part leaves the others readable and pane text cannot forge a delimiter. The shared helpers are Snapshot#screen (the last 40 nonblank lines through ScreenDigest), #screen_digest, #last_output_at (counted only from a read that also captured the pane), #quiet_for? and Clients#input_within?. The source can be swapped per runtime (Snapshot.source_for, for task 8c).

The interface: Sessions::Checks::Base. Each check implements .applies_to?, .parts, .kept_keys and #call(snapshot), and #call returns a Checks::Result (an outcome, plus whether processing stops). The checks run in today's order:

  • Checks::QuotaError
  • Checks::BlockingPrompt
  • Checks::AuthPause (its actions stay in Sessions::AuthPause)
  • Checks::TurnEnd (replaces Sessions::TurnEndNudge)

The *Detector classes stay as the text parsers.

Re-check before acting, and conditional state changes. Base#fail_session → new SessionService.fail_session_if(session:, expected_state:, error_message:) { |locked| … }. Under the row lock (for an admitted session, through the new SessionAdmissionService.fail_if! under the writer lock), it checks that the session is still in the state the pass read, that the check still applies, and that the check's own condition still holds. The state change happens in the same transaction, the method reports whether it applied, and only the Temporal signal goes after the commit. This closes the gap #666 accepted in give_up. Sessions::AuthPause#pause!, #unpause_quietly and #expire! are compare-and-sets too. A change that did not apply returns :taken_over and stops processing.

Removed reads:

  • the scan's own capture-pane exec;
  • the second stat exec on a quota hit;
  • AuthPause's stat exec (LiveLogReader#tail(lines: 1));
  • TurnEndNudge's tmux list-clients exec;
  • the separate screen normalization in AuthPause#banner_in.

Kept state (each check owns its keys, written once per session per pass)

Check Kept between passes Written by its action, under the row lock
QuotaError — —
BlockingPrompt — —
AuthPause — auth_paused_at, auth_pause_reason, auth_renewed_at, auth_heal_attempts, auth_heal_attempted_at (delivery's resume! and login_refused! also write them)
TurnEnd turn_end_screen, turn_end_seen_at turn_end_nudges (counted before typing)

Inventory

Checked against the code. The quota scan (4 checks), the no-output watchdog and RefreshExpiringTokensActivity#parked? are the only pieces that read a live terminal to decide something. Outside the mechanism:

  • ScanDeadContainersActivity (runtime API, no exec);
  • CleanupStaleActivity (DB timestamps);
  • Sessions::LogTail / LiveLogReader (the UI tail, get_session_log, session_log): reads for people;
  • AgentBaseStrategy's launch-time wait for the shell prompt: part of starting the CLI.

Behaviour

Thresholds, messages, skips and order are unchanged. Differences:

  • The schedule now has overlap: skip (decision 8).
  • When tmux is gone, the pane is now empty for every check (the old scan still got a pane from its || true capture while the stat exec failed). No check acted on that case before.
  • A check whose compare-and-set does not apply now leaves the session alone; before, it failed or paused the session anyway.

Related issues

Board task #211. Follows #666 (#208).

Type of change

  • Refactor / chore

How was this tested?

The existing tests were ported, with the same assertions:

  • scan_quota_errors_activity_test → test/temporal/activities/workflow/monitor_sessions_activity_test.rb
  • the auth-pause and turn-end activity tests
  • turn_end_nudge_test → test/services/sessions/checks/turn_end_test.rb (the stale-copy cases now return :taken_over instead of nil)
  • auth_pause_test

New tests:

  • test/services/sessions/snapshot_test.rb: one exec, only the requested parts, per-part failure, mtime only with a captured pane, marker-looking pane text, unreachable container.
  • test/services/sessions/checks/interface_test.rb: every check is a Base, the order, no shared kept keys, one-UPDATE kept-state write, key ownership.
  • test/services/session_service_fail_session_if_test.rb: a session finished after the read stays finished; a guard that no longer holds; the admitted path.
  • The pass: one exec per session, sessions no check applies to are not read, a session that moved to finishing during the read is not failed (quota and blocking prompt), the auth pause does not pause a finishing session or fail an unpaused one, and every pass logs its duration and read count.

Run on Coder (aixle-prod-2c822a0f, checkout e7ba0d17): rubocop on the 36 changed Ruby files passed, and 654 targeted Rails tests passed with 0 failures. The suites covered sessions, the workflow activities, session/admission services, credential delivery and sync, the Temporal registry and service, hosted-runtime-unchanged, runner session launch, the runner contract, the detectors, complete-step, agent credentials, docs, config and resources.

Checklist

  • All my commits are signed off for the DCO (git commit -s).
  • I added/updated tests where it makes sense.
  • I updated documentation where it makes sense (docs/architecture/workflows.md → "Session monitoring"; docs/design/agent-credential-lifecycle.md).
  • My commits use clear, descriptive messages.

Aixle Flow added 5 commits November 8, 2026 17:08
…ks for the quota scan's checks (task #211, step 1)

Signed-off-by: Aixle Flow <[email protected]>
…nd the session's locks, heal counters under the lock, a real-shell snapshot test (task #211, CR round 1)

- Sessions::AuthPause#pause! and #unpause! lock the workflow run, then the
  session, and change the marker and the run's state in one transaction.
  A delivery can no longer land between the marker and the run's pause,
  and two sessions of one run unpaused at once resume it.
- #resume! returns the unpause's result and logs only what happened. The
  resume prompt is still typed before the compare-and-set.
- #heal! counts its attempt under the session's row lock, re-reading the
  counter and, from the pass, heal_due? there.
- A test runs Snapshot::Command#argv through /bin/sh with stub tmux and stat.

Signed-off-by: Aixle Flow <[email protected]>
… and turn-end confirmation, a threaded unpause test (task #211, CR round 2)

- Sessions::AuthPause#pause! redacts the banner before storing auth_pause_reason.
- #expire! and #unpause_quietly take the auth_paused_at the pass observed and act only
  if the locked row still holds it; #expire! computes the expiry from the locked row.
- Checks::TurnEnd#give_up requires turn_end_screen and turn_end_seen_at to match the
  confirmation it acts on, under the lock.
- The two-sessions-of-one-run unpause test moves to auth_pause_concurrency_test.rb,
  on two threads with their own connections.

Signed-off-by: Aixle Flow <[email protected]>
…he concurrency test's sessions have a prompt (task #211, CR round 2)

Signed-off-by: Aixle Flow <[email protected]>
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Thanks for your pull request! Before we can merge it, we need you to sign our Contributor License Agreement. It is a one-time step and covers all your future contributions.

To sign, reply to this pull request with exactly:


I have read the CLA Document and I hereby sign the CLA


Aixle Flow seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@NOX73
NOX73 merged commit ed446d4 into develop Nov 9, 2026
33 of 34 checks passed
@NOX73
NOX73 deleted the feature/session-checks branch November 9, 2026 11:22
aixle-com Bot pushed a commit that referenced this pull request Nov 9, 2026
Step 1 (#671) was squash-merged into develop as ed446d4. Conflicts are
resolved to step 2's version: develop already holds step 1 exactly, and

Signed-off-by: Alex <[email protected]>
#670 touched none of the conflicted files.
aixle-com Bot pushed a commit that referenced this pull request Nov 10, 2026
Conflicts resolved:
- Files touched on staging only by the pre-squash branches of task #209 (PR #669)
  and task #211 (PRs #667/#668) take develop's version, which carries the
  reviewed, merged form of the same work (#670, #671, #672).
- test/integration/web/company/billing_test.rb: keeps the billing redesign's
  (#646) test body with develop's 14-day trial values (#678).
- CHANGELOG.md: develop's 1.11.0 entry for the 14-day trial; the billing
  redesign entry already present above is kept once.

Signed-off-by: Alex <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant