Skip to content

Port upstream 0.67.0: Stay Awake for live local agent sessions - #659

Closed
Finesssee wants to merge 2 commits into
port/upstream-0.67.0from
port/micro-0.67.0-stay-awake
Closed

Finesssee wants to merge 2 commits into
port/upstream-0.67.0from
port/micro-0.67.0-stay-awake

Conversation

@Finesssee

Copy link
Copy Markdown
Collaborator

Summary

New opt-in setting stay_awake_enabled (default off), toggle in Settings -> Advanced -> Agent Sessions. While on, the desktop shell runs a local-only agent-process scan every 30 s (independent of agent_sessions_enabled, no SSH/Tailscale discovery, no transcript files) and holds exactly one system idle-sleep prevention while at least one live local Codex, Claude, or Pi/OMP agent process exists. Idle processes waiting for a prompt count; file-only Codex rollouts and remote sessions never do.

  • Released on: a scan with no agent process, toggle off (immediately, from the settings command), app exit (RunEvent::Exit).
  • A failed acquire leaves nothing held and retries on the next scan. Stale scan completions are ignored once the setting is off or the app is shutting down.
  • Implementation: dedicated long-lived thread calling SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED) to hold and SetThreadExecutionState(ES_CONTINUOUS) to clear (the flag is per thread, so it is not called from a tokio worker). No display-required flag; lid close and explicit sleep still sleep the PC.
  • Native scan: uses a ToolHelp process snapshot (no PowerShell every 30 s). Records go through the same classifier as the CIM path (WindowsProcessOutputParser::record, extracted from parse, behavior unchanged).
  • Tray menu shows a disabled row "Stay Awake: local agent session is live" while held.

Approved dependency change: added the Win32_System_Power feature to the existing windows = "0.58" in rust/Cargo.toml. No new crate, no Cargo.lock change.

Upstream reference

  • Release 0.67.0 bullet: Stay Awake for live local agent sessions.
  • Tag-pinned (v0.67.0): Sources/CodexBar/AgentSessionPowerAssertion.swift, AgentSessionsStore.swift (updatePowerAssertion, localMonitoringEnabled), docs/agent-sessions-design.md "Stay Awake"; tests StayAwakeTests.swift.
  • Spec: port-audit/0.67.0.md PR 6.

Ported / Deferred

Ported: setting, scheduler, native scan, power assertion policy + Windows impl, tray status row, Settings toggle, en-US locale keys.

Deferred:

  • Flyout / tray-panel status line (React surfaces): only the native tray menu row is shown. It needs a bridge event for the held state; left out to keep this PR small.
  • Non-English locale text for the three new keys (falls back to en-US per catalog policy).
  • Portable-preferences allowlist entry: that export (upstream PR 5) is not in this base yet; stay_awake_enabled must stay out of the consent-free set when it lands.
  • The toggle lives in Settings -> Advanced (where the Agent Sessions section is in this port), not the Menu tab named in the spec.

Validation

Toolchain cargo +1.98.0, E-core wrappers, slot-2 target dir.

  • cargo +1.98.0 fmt --all: clean
  • cargo +1.98.0 clippy --workspace --all-targets -- -D warnings: pass
  • cargo +1.98.0 test -p codexbar -- --test-threads=4: 2173 passed, 0 failed, 1 ignored (includes 9 power_assertion tests: acquire/release counts for session appears, idle, disappears, toggle off, stale scan after disable, shutdown, acquire failure retry; a real SetThreadExecutionState acquire/release test; 4 native_processes tests including a live snapshot)
  • cargo +1.98.0 test -p codexbar-desktop-tauri -- --test-threads=4: 462 passed, 1 failed. The failure is commands::tests::bootstrap_payload_exposes_every_provider_variant (catalog 79 vs 78), unrelated to this change; it reads the machine's real settings (a deprecated provider enabled locally) and no provider code is touched here. New stay_awake_row_appears_only_while_held passes.
  • pnpm exec vitest run src/surfaces/settings/tabs/AdvancedTab.test.tsx src/i18n: 6 passed
  • pnpm run lint: no findings in touched files (existing warnings elsewhere); pnpm run build: pass

Not run: native check with powercfg /requests (needs the desktop app; coordinator).

Size notes: settings.rs (+5) and tray_bridge.rs (+4) were already over 1000 lines; no file crossed the limit.

Affected areas

  • Settings model / bridge / frontend settings tab
  • Tray menu
  • Shared backend (agent_sessions, new power_assertion)
  • Providers, CLI, cookies

UI proof

Pending: coordinator will capture CUA proof on a fresh build (Settings toggle, tray status row, powercfg /requests showing the system request while a Codex/Claude process runs and cleared after toggle off).

@coderabbitai

coderabbitai Bot commented Sep 29, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Finesssee

Finesssee commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

CUA UI proof (local, fresh build) - partial

  • Build commit: 55ff7fb2 (PR head, detached). pnpm --dir apps/desktop-tauri install --frozen-lockfile then pnpm --dir apps/desktop-tauri run tauri:build:debug via the E-core wrapper, exit 0. Binary: ...\cargo-target\slot-cua\debug\codexbar-desktop-tauri.exe.
  • Isolation: dirs::config_dir() ignores an APPDATA override on Windows, so the launched build used a temporary, uncommitted local patch in rust/src/logging.rs::config_root (env CODEXBAR_PROOF_CONFIG_ROOT) pointing at a temp settings.json (enabled_providers: [], stay_awake_enabled: false). The patch was reverted; the PR diff is unchanged. The real settings were never read or written. Launched with CODEXBAR_PROOF_MODE=settings:advanced.
  • Driven with cua-driver call list_windows / get_window_state / click (UIA toggle on the checkbox, using a session label so element tokens stay valid).

Assertions

  • PASS: Settings > Advanced > Agent Sessions shows the new row "Stay awake while a local agent session is live" with helper "Prevents idle sleep while a local Codex, Claude, or Pi/OMP process is running. The display can still turn off, and closing the lid still sleeps the PC. Disabled by default." (UIA texts).
  • PASS: default state is off (UIA selected=false), independent of "Show active Codex, Claude, and Pi/OMP sessions", which stays off.
  • PASS: clicking the toggle turns it on (UIA selected=true, screenshot shows checked box); the temp settings.json was rewritten by the save path.
  • PASS: clicking again turns it off (UIA selected=false); the "Show active ... sessions" toggle stayed off throughout (Stay Awake does not depend on it).
  • PASS: Settings window renders dark theme with theme auto.
  • PASS: no errors or warnings in the desktop log during the toggle on/off sequence.
  • NOT VERIFIED: tray menu row "Stay Awake: local agent session is live". The taskbar overflow flyout was not reachable through cua-driver (no addressable Shell_TrayWnd window; a desktop-scope click returned foreground_unavailable), so the native tray menu was not opened. It is covered only by the PR's unit test stay_awake_row_appears_only_while_held.
  • NOT VERIFIED: powercfg /requests showing the system request while a Codex/Claude process runs and cleared after toggle off. powercfg /requests needs an elevated shell, which this session does not have. Live Codex and Claude processes were running on the machine during the toggle-on window, so the hold path was exercised, but the OS-level request was not observed. Release on "no agent process" was not exercised (agent processes could not be stopped).

Local artifacts (not committed; contain no personal data)

  • %LOCALAPPDATA%\Temp\port-audit\proof\659\01-before-advanced.png (toggle off)
  • %LOCALAPPDATA%\Temp\port-audit\proof\659\02-after-toggle-on.png (toggle on)
  • %LOCALAPPDATA%\Temp\port-audit\proof\659\03-after-toggle-off.png (toggle off again)
  • UIA dumps: 01-before-uia.txt, 02-after-toggle-on-uia.json, 03-after-toggle-off-uia.json; build log ..\proof-659-build.log

@Finesssee

Copy link
Copy Markdown
Collaborator Author

Reviewed by Codex gpt-6-luna (xhigh); verified and validated by Claude

Thermo-nuclear review of PR #659 (5 findings, 4 fixed, 1 left):

  • P2 rust/src/power_assertion.rs (acquire): a timed-out acquire could still complete on the worker thread and leave Windows blocking sleep after the policy recorded a failed acquire. Fixed: a Clear request is queued after a timed-out Hold.
  • P2 rust/src/agent_sessions/native_processes.rs: process enumeration errors were treated as an empty or partial list, which could release a hold from an incomplete scan. Fixed: only ERROR_NO_MORE_FILES ends the scan normally; other errors propagate so the current hold state is kept.
  • P2 rust/src/agent_sessions/native_processes.rs: a Node-hosted Pi/OMP session appears as node.exe and cannot be classified without its command line. Left: the Windows path deliberately avoids harvesting command lines for privacy, and the image name alone cannot tell Pi/OMP from unrelated Node processes.
  • P2 apps/desktop-tauri/src/surfaces/TrayPanel.tsx: the spec calls for a held-state indicator in the flyout but the PR only showed it in the native tray menu. Fixed: added a get_stay_awake_status command, a stay-awake-changed event, a useStayAwakeStatus hook and a status line (existing TrayStayAwakeActive key).
  • P2 rust/src/power_assertion.rs (set_enabled): could re-enable after shutdown, letting the scheduler start scans during teardown. Fixed: shutdown is terminal.

@Finesssee

Copy link
Copy Markdown
Collaborator Author

Fixes pushed at c3ee700: 4 of 5 findings fixed; 1 left (Node-hosted Pi/OMP detection, needs command lines which are deliberately not harvested). Validated with cargo +1.98.0 fmt --all; clippy --workspace --all-targets -D warnings (clean, one Codex must_use lint corrected by Claude); cargo test -p codexbar (power_assertion, agent_sessions, locale: 87 passed); cargo test -p codexbar-desktop-tauri (stay_awake, tray: 101 passed); vitest TrayPanel.test.tsx and src/i18n (29 passed); pnpm lint (pre-existing warnings only). UI changed (flyout Stay Awake status line); CUA proof not re-run.

@Finesssee

Finesssee commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

CUA proof (rerun)

  • Build commit: c3ee700c (current PR head, detached). pnpm --dir apps/desktop-tauri install --frozen-lockfile then pnpm --dir apps/desktop-tauri run tauri:build:debug (E-core wrapper, CARGO_TARGET_DIR=...\cargo-target\proof-1), exit 0.
  • Proof-only patch (throwaway, restored, never committed): root Cargo.toml [patch.crates-io] dirs = { path = ".../port-audit/proof-shim/dirs" }, which redirects config/data dirs under CODEXBAR_PROOF_HOME. No source patches and no provider mocks (this PR needs no provider data). Settings seeded with {"enabled_providers":[],"theme":"auto","stay_awake_enabled":false}; home, provider homes and keys are isolated dummies.
  • Driving: cua-driver call list_windows / get_window_state / click only (UIA invoke/toggle, background delivery; proof windows kept on the second monitor; no foreground, keyboard or mouse input). Launched with CODEXBAR_PROOF_MODE=settings:advanced, then trayPanel.
# Assertion Result
1 No real email/account from the machine visible in any capture PASS
2 Settings > Advanced > Agent Sessions has "Stay awake while a local agent session is live" with the helper text (display can still turn off, lid close still sleeps, disabled by default); default selected=false, independent of "Show active ... sessions" PASS
3 UIA toggle on -> selected=true (screenshot shows checked box); "Show active ... sessions" stays off PASS
4 After restart in trayPanel mode the setting persisted on and the panel shows "Stay Awake: local agent session is live" (live claude.exe/codex.exe processes exist on this machine) PASS
5 Settings reopened from the panel shows the toggle still on; toggle off -> selected=false PASS
6 After restart with the setting off, the panel no longer shows the Stay Awake status PASS
7 Dark theme under auto (settings window and tray panel) PASS

Not verified: powercfg /requests (needs elevation) and release-on-no-agent-process (the machine's own agent processes cannot be stopped). The native tray menu row was not opened; the tray panel status line and the settings toggle were exercised.

Screenshots (local, not committed; %LOCALAPPDATA%\Win-CodexBar\port-audit\proof\659\shots\): 01-initial.png, 02-toggle-on.png, 03-tray-on.png, 05-advanced-still-on.png, 06-toggle-off.png, 07-tray-off.png. Kit: ...\proof\659\ (PLAN.md, launch.sh, proof-only.patch).

@Finesssee

Copy link
Copy Markdown
Collaborator Author

Adversarial validation (lane-B review)

Head validated: c3ee700c ("Address thermo review"). Spec: 0.67.0.md PR 6 (item 4, Stay Awake).

Verdict: no blocking defects. Spec conformance verified in source at head:

  • Opt-in setting stay_awake_enabled (default false, local, not consent-exposed in portable preferences per the spec's "excluded or treated as consent" clause — it is excluded).
  • Local-only scan every 30 s (SCAN_INTERVAL), independent of agent_sessions_enabled; no remote/SSH discovery, no menu list.
  • Exactly one idle-sleep prevention while at least one live local agent session exists: SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED) held on a dedicated thread; released on next scan with none, on toggle off, and on app exit; a failed acquire retries on the next scan (nothing held on failure).
  • Live-session rule matches the spec: a session counts only when pid > 0 and the process is live; file-only Codex rollouts and remote sessions never count (native_processes.rs any(process.pid > 0 && process.is_agent())).
  • Menu status indicator: "Stay Awake: local agent session is live" row in the tray menu while held (tray_menu.rs, test at line 350); the Settings toggle lives in AdvancedTab.
  • PowerShell scan cost concern from the audit is addressed: the scan uses native_processes.rs (Win32 snapshot), not Get-CimInstance.

Validation re-run at head (pinned 1.98.0, E-cores): cargo +1.98.0 fmt --all -- --check clean; cargo +1.98.0 clippy --workspace --all-targets -- -D warnings clean; cargo +1.98.0 test -p codexbar --lib 2175 passed / 0 failed (1 ignored); cargo +1.98.0 test -p codexbar-desktop-tauri 462 passed / 1 failed (the documented #684 bootstrap_payload_exposes_every_provider_variant baseline); frontend: pnpm test 67 files / 403 tests passed; pnpm run check-locale and pnpm run build unaffected (Rust + shell wiring only).

UI proof: waived per user 2026-10-02 directive (fast-track); tray-menu indicator and toggle are covered by the new tests. (The full E2E sleep-prevention behavior is OS-observable only; the assertion lifecycle is unit-tested in power_assertion.rs.)

@Finesssee

Copy link
Copy Markdown
Collaborator Author

UI proof (browser-use)

Combined build of integrate/v0.70.0-ports at ddd85594 (all 40 port PRs and the follow-ups). Debug desktop build in proof mode, isolated config/data dirs, driven by browser-use over the WebView2 DevTools protocol with DOM events only (no mouse or keyboard input, no focus changes). All data is synthetic: local mock servers for L1, a seeded usage snapshot plus synthetic local logs for L2.

Scenario Check Result
L1 extras (preferences, keys, toggles) #659 Stay Awake toggle persists; flyout status line follows the held state ('Stay Awake: local agent session is live' iff held) PASS
L1 extras (preferences, keys, toggles) #659 Stay Awake off: released immediately, flyout status line gone PASS

Every surface also passed the privacy check (no email-like text, account e-mail nodes or profile paths in the DOM) and theme auto rendered dark on the tray flyout, float bar and settings windows.

Validation at ddd85594: cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test -p codexbar (3567 passed, 0 failed) and cargo test -p codexbar-desktop-tauri (606 passed, 0 failed). Screenshots were captured for each scenario and kept with the local proof kit.

@Finesssee

Copy link
Copy Markdown
Collaborator Author

Shipped in v0.70.0: this PR's head is included in main via #735 (merge commit 9d0a37a). Closing as integrated.

@Finesssee Finesssee closed this Oct 3, 2026
junglesub-bot Bot pushed a commit to junglesub/Win-CodexBar that referenced this pull request Oct 4, 2026
… agent sessions

Conflicts: the release builds the shell from a pre-generated context
(`let context = tauri::generate_context!()`, used for the single-instance
foreground hand-off), so the PR's build/run-with-exit-hook switch now
calls `.build(context)` instead of generating the context twice.
Command list keeps the release get_currency_rates next to
get_stay_awake_status; tray_bridge keeps the release tray_presentation
import next to MenuState; lib.rs and tauri.ts keep both additions.
junglesub-bot Bot pushed a commit to junglesub/Win-CodexBar that referenced this pull request Oct 4, 2026
The preferences document test requires every Settings field to be either
portable or excluded. Fields that reached the release after nesszer#654's base:

- Portable: cost_reporting_period and preferred_currency_code (nesszer#654 asks
  for both once they land), switcher_shortcuts (nesszer#700 deferred it to this
  document; upstream 0.70.0 exports switcherShortcuts). Validators accept
  only stored forms: canonical periods, AUTO or a supported code, and
  overrides that pass the switcher shortcut rules.
- Excluded: stay_awake_enabled (side-effect toggle, nesszer#659),
  cost_usage_bucket_time_zone (pinned to the machine zone on first launch,
  no settings control), and menu_bar_color_pace, the stacked tray provider
  picks and credential_expiry_notifications_enabled, which upstream 0.70.0
  also keeps out of its portable keys.

null on a field omitted when empty (switcher_shortcuts) now clears it
instead of failing. A desktop import that changes the reporting period
resets the local cost cache, as update_settings does.
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