Skip to content

fix(part): IRC /part semantics — leave one room, keep sidecar - #125

Merged
joelteply merged 1 commit into
canaryfrom
fix/part-keeps-sidecar
Apr 26, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/part-keeps-sidecar

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #122. Pre-fix: `airc part` from primary scope nuked the #general sidecar too (because cmd_teardown's pgrep-children walker reached the sidecar via the parent-child relationship). IRC convention is /part leaves ONE channel; the lobby should persist.

Fix

  • `cmd_teardown` reads `AIRC_TEARDOWN_PART_ONLY=1` and excludes sidecar's airc.pid + descendants from the kill set when set.
  • `cmd_part` sets the flag before calling cmd_teardown, making part scope-local.
  • Also: removed redundant sidecar-PID append to primary's airc.pid (cmd_teardown already walks sidecar's own pidfile explicitly via the .general block).

Test plan

  • `bash test/integration.sh part_keeps_sidecar` — 6/6 pass (sidecar survives primary's part; subsequent full teardown still reaps it)
  • Manual: spawn primary + sidecar, run `airc part` from primary, verify sidecar bash PID still alive via `kill -0`

Test bugfix included

read first PID from `airc.pid` via `awk '{print $1; exit}'` instead of `head -1` — host-mode pidfile has multiple space-separated PIDs on one line.

🤖 Generated with Claude Code

Pre-fix: 'airc part' from a primary scope (e.g. #useideem) called
cmd_teardown which (post-#122) cleaned BOTH the primary scope AND
the #general sidecar — so leaving #useideem also dropped the lobby
unintentionally. IRC convention: /part leaves ONE channel; the lobby
should persist. Caught while testing the multi-room model after #122
landed.

Two related issues fixed:

1. cmd_teardown's pgrep-children walker would catch the sidecar bash
   (forked from primary) and kill it via the descendant tree even
   though the sidecar's PID isn't in primary's airc.pid (after #122
   we stopped appending it). Added AIRC_TEARDOWN_PART_ONLY=1 guard:
   when set, cmd_teardown's exclusion list reads sidecar's airc.pid
   + descendants and removes them from the kill set.

2. cmd_part now sets local AIRC_TEARDOWN_PART_ONLY=1 before calling
   cmd_teardown. This also short-circuits the explicit sidecar-block
   in cmd_teardown (gist + pidfile cleanup for the .general scope),
   so part is fully scope-local.

Also: removed the redundant 'echo $sidecar_pid >> primary/airc.pid'
from spawn_general_sidecar_if_wanted. The sidecar already has its
own scope's airc.pid, and cmd_teardown's explicit sidecar-block
walks that pidfile directly. Appending was unnecessary AND was
exactly what made cmd_part nuke the sidecar via the primary's
pidfile walk in pre-#122 testing.

Test: scenario_part_keeps_sidecar — 6 assertions:
  - primary + sidecar both running pre-part
  - airc part removes primary's airc.pid
  - sidecar's main bash PID still alive after primary's part (the
    load-bearing assertion — was the regression)
  - sidecar scope dir + pidfile preserved
  - sidecar room_name preserved
  - subsequent 'airc teardown' (no flag) DOES reap the sidecar
    (verifies full-kill semantic still works for the explicit case)

Test bugfix included: read first PID from sidecar's airc.pid via
awk '{print \$1; exit}' instead of head -1. host-mode airc.pid is
'\$\$ PAIR_PID hb_pid' on one line, so head -1 returned the whole
line and kill -0 failed on the multi-PID string.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 26, 2026 18:08
@joelteply
joelteply merged commit c8a3055 into canary Apr 26, 2026
@joelteply
joelteply deleted the fix/part-keeps-sidecar branch April 26, 2026 18:08

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

Adjusts airc part to follow IRC semantics by leaving only the current room’s scope running, while preserving the #general sidecar subscription introduced in #122.

Changes:

  • Add AIRC_TEARDOWN_PART_ONLY=1 path so cmd_teardown skips sidecar cleanup and excludes sidecar PIDs from the primary kill set when invoked via cmd_part.
  • Stop appending the sidecar PID into the primary scope’s airc.pid (sidecar teardown is handled via the sidecar scope pidfile).
  • Add an integration scenario part_keeps_sidecar validating sidecar survival after part and reaping on subsequent teardown.

Reviewed changes

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

File Description
airc Implements part-only teardown semantics and removes sidecar PID coupling to the primary pidfile.
test/integration.sh Adds a regression scenario ensuring /part does not kill the #general sidecar and that a later full teardown still cleans it up.

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

Comment thread airc
Comment on lines 852 to +863
( env -u AIRC_PORT "${_env_args[@]}" "$0" connect --room general ) &
local _sidecar_pid=$!

# Append to primary's pidfile so cmd_teardown kills the sidecar
# process tree along with the primary's. (Sidecar's own scope has
# its own pidfile for sidecar's descendants — that gets cleaned by
# the next sidecar's auto-stale-pidfile recovery on reconnect.)
echo "$_sidecar_pid" >> "$_primary_scope/airc.pid"
# Sidecar's own scope writes its own airc.pid for its bash + descendants.
# cmd_teardown walks $AIRC_WRITE_DIR.general/airc.pid explicitly to kill
# the sidecar tree, so we DON'T append the sidecar bash PID to the
# primary's airc.pid. Earlier (PR #122) we did — but that broke
# cmd_part, which walks the same primary pidfile and ends up killing
# the sidecar too even though IRC semantics say `/part` should leave
# only the current channel and keep others (e.g. #general) alive.
# Pidfiles per scope; cmd_teardown is the only path that walks both.
:
Comment thread airc
Comment on lines 4378 to +4386
# kill there. Without this, killing the primary leaves an orphan
# #general gist on the gh account AND an orphan sidecar process that
# the primary's pidfile descendant-walk wouldn't catch (sidecar's
# bash isn't a child of cmd_teardown — it was forked detached).
#
# Guard: AIRC_TEARDOWN_PART_ONLY=1 (set by cmd_part) skips the sidecar
# block. IRC `/part` should leave only the current channel; the
# sidecar (#general lobby) should keep running. cmd_teardown without
# this flag is the "kill everything in this scope tree" semantic.
Comment thread test/integration.sh
Comment on lines +2576 to +2580
sleep 1

! kill -0 "$_sc_pid" 2>/dev/null \
&& pass "subsequent airc teardown DOES reap the sidecar (full kill semantic preserved)" \
|| fail "sidecar survived even airc teardown — over-skip bug"
joelteply added a commit that referenced this pull request Apr 26, 2026
refactor(platform): extract proc/port/file/platform adapters; route all callsites through them

Joel, 2026-04-26: 'we mess each other up if we dont abstract.' / 'fixing
one [platform] can degrade the other.' Multiple AIs working on the same
codebase across Mac/Linux/WSL/Windows-Git-Bash repeatedly retouched the
same inline pgrep/lsof/stat patterns; each tweak risked diverging from
the others. The fix is to consolidate all platform-specific shell calls
behind named adapters so business logic stays platform-agnostic and
porting work happens in one place per capability.

## New adapters (top of file, just before relay_ssh)

- `proc_children <pid>` — direct children of PID, one per line. pgrep -P
  primary; ps -axo pid,ppid + awk fallback for envs without pgrep
  (Git Bash for Windows ships only msys coreutils).

- `proc_parent <pid>` — PPID. ps -p X -o ppid= cross-platform.

- `proc_cmdline <pid>` — full argv. ps -p X -o command=.

- `proc_airc_pids_matching <regex>` — airc-related PIDs owned by current
  user, used by `airc teardown --all`. pgrep -u + -f primary; ps + awk
  fallback.

- `port_listeners <port>` — PIDs listening on a TCP port. lsof primary
  (mac/most linux); ss fallback (modern linux); netstat -ano fallback
  (Windows native + minimal containers). Empty output = nobody listening.

- `file_size <path>` — bytes. stat -f%z (BSD) → stat -c%s (GNU) → wc -c
  fallback (POSIX-portable).

- `detect_platform` — emits macos/linux/wsl/windows-bash/unknown. WSL
  detection unified via /proc/version content match. Reserved for the
  rare case where a top-level decision genuinely depends on platform
  (e.g. Tailscale.app launching) — most callers should use the proc_/
  port_/file_ adapters which handle differences internally.

## Callsites converted

- 8 inline `pgrep -P <pid>` sites → `proc_children`
- 2 inline `pgrep -u $(id -u) -f <pattern>` sites → `proc_airc_pids_matching`
- 4 inline `lsof -tiTCP:<port> -sTCP:LISTEN` sites → `port_listeners`
- 3 inline `ps -p <pid> -o ppid= | tr -d ' '` and `ps -p <pid> -o command=`
  sites → `proc_parent` / `proc_cmdline`
- 1 inline `stat -f%z X || stat -c%s X || echo 0` site → `file_size`

Inline `case "$(uname -s)"` blocks for tailscale-down guidance and
machine_id lookup were NOT converted to `detect_platform`. Those blocks
do platform-specific WORK (different commands per platform), not
platform-specific abstraction of one shape — converting them would be
larger surface-area for marginal cleanup. detect_platform stays
available for future per-platform decisions.

## Tests

- scenario_platform_adapters — 8 assertions exercising each adapter
  with a synthetic process tree + listener. proc_children sees a
  spawned sleep; proc_parent returns the harness PID; proc_cmdline
  contains 'sleep'; port_listeners finds a python listener on a
  random high port; file_size returns 11 for 'hello world' and 0 for
  missing files; detect_platform returns a valid value matching uname.
- scenario_general_sidecar_default fixed — was reading
  primary's airc.pid for sidecar PID, stale since #125 separated
  sidecar pidfiles. Now reads ${home}.general/airc.pid via
  awk '{print $1; exit}' to extract first PID from the
  multi-PID host-mode line.

## Mac/Linux regression sweep
Full suite (mode=all) ran post-refactor:
- 194 assertions pass
- 1 flake (rename propagation in scenario_tabs — passed on retry, pre-
  existing intermittent timing issue, not caused by this refactor)

No new platform dependencies introduced. The adapters add fallback
paths that activate on platforms the inline calls didn't support (e.g.
Git Bash without pgrep), so this is strictly more portable than what
was on canary before, while behavior on Mac/Linux is unchanged.

## Why now

Joel asked to take this on while he prepares to rebuild the Windows
side. With adapters in place, each Windows fallback bug becomes a
one-helper edit with a contained blast radius. Without adapters, every
Windows fix risks breaking the Mac/Linux paths it touches.

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