Skip to content

fix(daemon,install): scope-aware idempotency (#2 from b69f's daemon audit) - #411

Merged
joelteply merged 1 commit into
canaryfrom
fix/daemon-install-scope-aware
May 2, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/daemon-install-scope-aware

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Why

b69f hit it 2026-05-02:

  1. Installed daemon while in /c/.airc-src
  2. Re-ran AIRC_INSTALL_YES=1 bash install.sh from ~/continuum
  3. install.sh saw "any airc daemon registered" → no-op'd the prompt
  4. ~/continuum had no daemon serving it; surface looked installed but launcher .bat pointed at the wrong scope

The cmd_daemon_install function itself IS idempotent (force-overwrites .bat + reg add //f). The bug was install.sh's coarse pre-check — airc_daemon_is_installed answered "any daemon anywhere?" not "the daemon for THIS scope?".

Pairs with #410 (Mac's #3 status honesty + #5 join tip) — both make the daemon stop lying about its own state.

What

New: airc_daemon_is_installed_for_scope <scope> in lib_daemon_detect.sh

Stricter sibling of airc_daemon_is_installed. Returns 0 only when the registered launcher / unit / plist is wired to the given scope:

OS Check
darwin plist's EnvironmentVariables.AIRC_HOME == scope
linux/wsl systemd unit has Environment="AIRC_HOME=<scope>"
windows registry value contains <scope>/airc-daemon.bat AND .bat exists on disk

Plain airc_daemon_is_installed is preserved unchanged for callers that want "any daemon presence" (post-disconnect tip, status banner).

install.sh updated

Both branches (AIRC_INSTALL_YES=1 + interactive prompt) now use airc_daemon_is_installed_for_scope "$INSTALL_DAEMON_SCOPE" where INSTALL_DAEMON_SCOPE mirrors cmd_daemon.sh::_daemon_scope: ${AIRC_HOME:-$(pwd)/.airc}.

Net effect:

Scenario Pre-fix Post-fix
AIRC_INSTALL_YES=1 + already installed for THIS scope no-op no-op (unchanged)
AIRC_INSTALL_YES=1 + registered for DIFFERENT scope no-op (silent miss) reinstall for new scope
Interactive + already installed for THIS scope skip prompt skip prompt (unchanged)
Interactive + registered for DIFFERENT scope skip prompt (silent miss) prompt with clear "registered for a different scope; reinstall and wire to <new>?"

Tests

End-to-end smoke on Windows Git Bash with this branch:

$ cd ~/continuum && airc daemon install
$ airc_daemon_is_installed_for_scope ~/continuum/.airc; echo $?
0     ← was 1 pre-fix when registry pointed at different scope
$ airc_daemon_is_installed_for_scope /tmp/other/.airc; echo $?
1
$ airc daemon uninstall
$ airc_daemon_is_installed_for_scope ~/continuum/.airc; echo $?
1

Existing 11/11 monitor_formatter tests still pass.

Pairs with b69f's 5-bug daemon audit

Bug Owner Status
#1 launcher absolute paths Mac #408+#409 merged
#2 scope-aware install b69f THIS PR
#3 status honesty Mac #410 merged
#4 Windows crashloop b69f research, next
#5 join-path tip Mac #410 merged

🤖 Generated with Claude Code

…kips when daemon is registered for a DIFFERENT scope (#2 from b69f's daemon audit)

## Why

b69f hit it 2026-05-02:
1. Installed daemon while in /c/.airc-src
2. Re-ran AIRC_INSTALL_YES=1 install.sh from ~/continuum
3. install.sh saw "any airc daemon registered" → no-op'd the prompt
4. ~/continuum had no daemon serving it; surface looked installed
   from `airc daemon status`'s old behavior, but launcher .bat
   pointed at the wrong scope

The cmd_daemon_install function itself IS idempotent (force-overwrites
.bat + reg add //f). The bug was install.sh's coarse pre-check —
`airc_daemon_is_installed` answered "any daemon anywhere?" not "the
daemon for THIS scope?".

Pairs with #410 (Mac's #3 status honesty + #5 join tip) — both make
the daemon stop lying about its own state.

## What

### New: `airc_daemon_is_installed_for_scope <scope>` in lib_daemon_detect.sh

Stricter sibling of `airc_daemon_is_installed`. Returns 0 only when
the registered launcher / unit / plist is wired to the given scope:

- darwin: plist's EnvironmentVariables.AIRC_HOME == scope
- linux/wsl: systemd unit has Environment="AIRC_HOME=<scope>"
- windows: registry value contains <scope>/airc-daemon.bat AND .bat
  exists on disk

Plain `airc_daemon_is_installed` is preserved unchanged for callers
that want "any daemon presence" (post-disconnect tip, status banner).

### install.sh updated

Both branches (AIRC_INSTALL_YES=1 + interactive prompt) now use
`airc_daemon_is_installed_for_scope "$INSTALL_DAEMON_SCOPE"` where
INSTALL_DAEMON_SCOPE mirrors cmd_daemon.sh::_daemon_scope:
${AIRC_HOME:-$(pwd)/.airc}.

Net effect:
- AIRC_INSTALL_YES=1 + already installed for THIS scope → no-op
- AIRC_INSTALL_YES=1 + registered for DIFFERENT scope → reinstall
  (cmd_daemon_install regenerates .bat + reg add //f overwrites)
- Interactive + already installed for THIS scope → skip prompt
- Interactive + registered for DIFFERENT scope → prompt with clearer
  "registered for a different scope; reinstall and wire to <new>?"

## Tests

End-to-end smoke (this branch, on Windows Git Bash):

  $ airc daemon install                        # in ~/continuum
  $ airc_daemon_is_installed_for_scope ~/continuum/.airc; echo $?
  0   ← was 1 pre-fix when registry pointed at different scope
  $ airc_daemon_is_installed_for_scope /tmp/other/.airc; echo $?
  1
  $ airc daemon uninstall
  $ airc_daemon_is_installed_for_scope ~/continuum/.airc; echo $?
  1

Existing 11/11 monitor_formatter tests still pass.

## Pairs with the 5-bug audit

- #1 (#408+#409 Mac): launcher absolute paths
- #2 THIS PR: scope-aware install
- #3+#5 (#410 Mac): status honesty + join-path tip
- #4 still open: Windows crashloop research (b69f next)

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 2, 2026 16:05
@joelteply
joelteply merged commit 5e2e593 into canary May 2, 2026
8 checks passed
@joelteply
joelteply deleted the fix/daemon-install-scope-aware branch May 2, 2026 16:06

Copilot AI 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.

Pull request overview

This PR makes daemon installation idempotency scope-aware so install.sh only skips daemon setup when the registered autostart entry is already wired to the current AIRC scope. It fits into the codebase’s project-scoped daemon model, where daemon install/status behavior depends on AIRC_HOME or the current working directory’s .airc.

Changes:

  • Add airc_daemon_is_installed_for_scope <scope> to perform platform-specific scope-aware daemon detection.
  • Update install.sh to use the new scope-aware check for both auto-install and interactive prompt flows.
  • Improve installer messaging when a daemon exists but is registered for a different scope.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
lib/airc_bash/lib_daemon_detect.sh Adds the new platform-specific scope-aware daemon detection helper.
install.sh Switches installer daemon idempotency checks and prompt text to be scope-aware.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread install.sh
elif [ "${AIRC_INSTALL_YES:-0}" = "1" ]; then
if airc_daemon_is_installed; then
info "AIRC_INSTALL_YES=1 — airc daemon already installed (no-op)"
if airc_daemon_is_installed_for_scope "$INSTALL_DAEMON_SCOPE"; then
Comment on lines +127 to +132
# Windows form is what cmd_daemon writes, but defense-in-depth.
case "$got_value" in
*"$target_bat_win"*)
[ -f "$target_bat_unix" ] && return 0
return 1
;;
Comment on lines +101 to +103
# Match Environment="AIRC_HOME=<scope>" or Environment=AIRC_HOME=<scope>.
grep -qE "Environment=\"?AIRC_HOME=${target_scope//\//\\/}\"?($|[[:space:]])" "$unit_path" \
&& return 0
Comment thread install.sh
Comment on lines +917 to +931
if airc_daemon_is_installed_for_scope "$INSTALL_DAEMON_SCOPE"; then
info "AIRC_INSTALL_YES=1 — airc daemon already installed for this scope (no-op)"
else
info "AIRC_INSTALL_YES=1 — installing airc daemon"
if airc_daemon_is_installed; then
info "AIRC_INSTALL_YES=1 — daemon registered for a different scope; reinstalling for $INSTALL_DAEMON_SCOPE"
else
info "AIRC_INSTALL_YES=1 — installing airc daemon"
fi
if "$BIN_DIR/airc" daemon install; then
ok "airc daemon installed"
else
warn "airc daemon install returned non-zero (continuing — re-run manually if needed)"
fi
fi
elif airc_daemon_is_installed; then
info "airc daemon already installed (skipping prompt)"
elif airc_daemon_is_installed_for_scope "$INSTALL_DAEMON_SCOPE"; then
joelteply added a commit that referenced this pull request May 2, 2026
…ills 10-min takeover blackout (#4) (#412)

fix(monitor,daemon): skip bearer-state fallback in daemon context — kills 10-min takeover blackout (#4 from b69f's daemon audit)

## Why

Last bug from b69f's 2026-05-02 daemon audit. Repro:

1. Interactive `airc connect` running, writing bearer_state.json
2. Process dies (Claude session end, Ctrl-C, crash)
3. Daemon launcher (.bat / launchd / systemd) starts new airc connect
4. New airc connect calls _monitor_alive_with_bearer_fallback
5. Phase 1 (kill -0 on pidfile PIDs): all dead — falls through to Phase 2
6. Phase 2 (bearer_state freshness): last_recv_ts within 600s window → "yes"
7. New airc connect early-exits "monitor already running"
8. Launcher restarts every 5s, repeats until 600s window expires
9. **10-minute substrate blackout while daemon can't take over**

Then daemon.err fills with hundreds of `airc connect exited. Restarting in 5s.` lines per minute. Joel observed exactly this pattern 2026-05-02 calling the daemon path "brittle as hell."

Phase 2 was added (#370) for Codex sandbox blindness — kill -0 lies in some sandboxes about cross-process visibility. The fallback was supposed to disambiguate. But in daemon context (which sets `AIRC_BACKGROUND_OK=1`), kill -0 is reliable; the fallback only causes false positives.

## What

Three-line skip at the top of Phase 2: when `AIRC_BACKGROUND_OK=1`, trust kill -0 and return "no" if all pidfile PIDs are dead.

```bash
if [ "${AIRC_BACKGROUND_OK:-0}" = "1" ]; then
  echo "no"
  return 0
fi
```

Effect: daemon takeover is immediate (next .bat iteration after interactive death) instead of delayed 10 minutes.

## Why this is safe

- Codex / sandbox sessions don't set `AIRC_BACKGROUND_OK=1` — they get the existing fallback behavior. Phase 2's purpose is preserved for them.
- Daemon launcher (cmd_daemon.sh:233) explicitly sets `AIRC_BACKGROUND_OK=1` before spawning airc connect. That's the marker.
- launchd plist (cmd_daemon.sh:160-162) and systemd unit (cmd_daemon.sh:331) also set it. Cross-platform consistent.
- In daemon context the only thing populating bearer_state is the previously-running airc connect; that connect is now dead (Phase 1 confirmed), so there's no "real but blind" monitor — only a stale state file.

## Out of scope (follow-up)

When daemon legitimately CAN'T take over because another airc connect IS running (user has both interactive + daemon, dual-monitor scenario), the launcher still spams 5s restart cycles. Distinct exit code from "monitor already running" → longer .bat sleep would be the polish. Filing as follow-up; this PR is the hot-path fix.

## Tests

- bash -n passes
- 11/11 monitor_formatter tests pass
- Diff is 3 functional lines + comment; sandbox path unchanged
- Smoke test scaffolding to repro the blackout requires a real Windows daemon install + interactive death; will rely on Mac dogfood + integration suite

## Closes b69f's daemon audit

| Bug | PR |
|-----|----|
| #1 launcher absolute paths | #408 + #409 (Mac) |
| #2 scope-aware install | #411 (b69f) |
| #3 status honesty | #410 (Mac) |
| **#4 daemon takeover blackout** | **THIS PR** |
| #5 join-path tip | #410 (Mac) |

5/5 audit bugs in PRs. Daemon path on Windows should be substantially more honest after merge.

Co-authored-by: Claude Opus 4.7 (1M context) <[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.

2 participants