Skip to content

fix(#285): host airc msg publishes to gist (silent-loss + TDD) - #286

Merged
joelteply merged 1 commit into
canaryfrom
fix/issue-285-host-publishes-to-gist
Apr 29, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/issue-285-host-publishes-to-gist

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Closes #285. Two compounding bugs caused host `airc msg` to silently lose every broadcast and DM:

  1. `cmd_send.sh` host path was local-append-only — relic of pre-3c when SSH bearer's tail-F was the wire. After Phase 3c moved joiners to GhBearer polling the gist, a local-only host append disappeared.
  2. `gh gist edit GIST file` silent no-op — when the target file doesn't exist in the gist, gh CLI returns exit 0 but writes nothing. Production gists start with an airc-invite seed (NO messages.jsonl), so EVERY first host send hit the trap.

TDD

  • `scenario_gh_send_creates_messages_jsonl` — bearer-level: seed gist with airc-invite (matching production), invoke bearer_cli send, assert messages.jsonl appears with the marker.
  • `scenario_host_msg_publishes_to_gist` — end-to-end: real `airc connect --room`, real `airc msg`, assert gist content updated.

Both scenarios fail on canary tip (12fa1fc) and pass after this fix.

Why existing 123 integration tests didn't catch this

  • Most scenarios use `--no-room --no-gist` for speed → bypass gh entirely → test rig still exercises the pre-3c TCP-pair path.
  • `scenario_bearer_gh` uses real gh BUT pre-seeds the gist with messages.jsonl. Production starts with airc-invite seed → first send is a CREATE → silent-no-op trap.

The seed shape was the difference. New scenarios use production-shaped seeds.

Files

  • `lib/airc_bash/cmd_send.sh` — host path now also calls bearer_cli send with --room-gist-id
  • `lib/airc_core/bearer_gh.py` — `_gh_gist_write_file` reads the gist file list first, chooses correct subcommand (-a for add, plain edit for replace)
  • `test/integration.sh` — two new TDD scenarios

Not covered (separate issues)

Both will follow the same TDD-first pattern.

🤖 Generated with Claude Code

Two bugs compounded into the worst silent-loss class — host broadcasts
returned 'delivered' but the gist was never written; joiners polling
the gist saw nothing; user surface lied success.

## Bug 1: cmd_send.sh host-path was local-append-only
The else-branch (host case, line ~363 pre-fix) ended with:

    echo "\$full_msg" >> "\$MESSAGES"

with a comment "Joiners' SSH tails will pick it up" — relic of pre-3c
when SSH bearer's tail -F over SSH was the wire. Phase 3c (#281) deleted
SSH wholesale; joiners now poll the gist via GhBearer; a local-only
append disappears.

Fix: after the local audit append, ALSO call bearer_cli send with the
host's --room-gist-id so GhBearer publishes to the gist. Same wrap-if-
recipient-known logic as the joiner branch, so DMs to peers with
stored x25519_pub get encrypted and broadcasts go plaintext.

## Bug 2: GhBearer._gh_gist_write_file silent no-op on first send
\`gh gist edit GIST_ID file\` (no flag) returns exit 0 BUT silently does
nothing when the target filename doesn't exist in the gist. The
fallback chain ('try plain edit; on non-zero, retry with -a') never
fires because the first call returns 0 without writing. Production
gists start with only an airc-invite seed (no messages.jsonl), so
EVERY first host send hit this trap.

Fix: read the gist's file list FIRST, choose the correct subcommand:
  - file already in gist  → \`gh gist edit GIST file\`        (replace)
  - file NOT in gist      → \`gh gist edit GIST -a file\`     (add)

One extra GET, but eliminates the silent-no-op trap. Defense-in-depth:
if the existence check disagrees with reality (concurrent peer added
the file between our GET and our edit), retry the OTHER subcommand
once before giving up.

## Why the existing 123 integration tests didn't catch this
- All scenarios that exercise multi-peer messaging used \`--no-room
  --no-gist\` for speed. That flag bypasses gh entirely; the test rig
  rode on the SSH-bearer TCP-handshake path. After Phase 3c moved
  production to GhBearer, the test rig kept exercising a stripped-down
  path that doesn't match production architecture.
- scenario_bearer_gh DOES use a real gh gist BUT pre-seeds it with
  messages.jsonl (the 'happy path' shape). Production gists start with
  airc-invite seed (NOT messages.jsonl), so the first send is a CREATE
  not a REPLACE — exactly the bug. The seed shape masked it.

Two new TDD scenarios cover both paths post-fix:

- \`gh_send_creates_messages_jsonl\` (unit-of-bearer): seeds gist with
  airc-invite (matching production), runs bearer_cli send directly,
  asserts gist now contains messages.jsonl with the marker. Pre-fix:
  bearer reports 'delivered', gist unchanged. Post-fix: green.
- \`host_msg_publishes_to_gist\` (end-to-end cmd_send→bearer): spawns a
  real host with --room (no --no-gist), runs \`airc msg <marker>\`,
  asserts gist's messages.jsonl contains the marker. Pre-fix: cmd_send
  never invoked bearer_cli on host path. Post-fix: green.

Verified locally:
- gh_send_creates_messages_jsonl: 5/5 (was 3/2 before fix)
- host_msg_publishes_to_gist: 2/2 (was 1/1 before fix)

Closes #285. #283 (sidecar) and #284 (cross-room DM) are tracked
separately and will get their own TDD + fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 29, 2026 05:56
@joelteply
joelteply merged commit bb0cdff into canary Apr 29, 2026
8 checks passed
@joelteply
joelteply deleted the fix/issue-285-host-publishes-to-gist branch April 29, 2026 05:59

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

Fixes a Phase 3c regression where host-side airc msg could silently fail to publish to the room gist (and where gh gist edit can “succeed” without writing when the target file doesn’t yet exist), closing #285.

Changes:

  • Update host-side cmd_send to publish outbound messages to the room gist (in addition to local audit logging).
  • Fix GhBearer gist writes to correctly add messages.jsonl on first send (choose gh gist edit -a vs replace based on gist file presence).
  • Add two new integration scenarios to reproduce and prevent the silent-loss regressions.

Reviewed changes

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

File Description
lib/airc_bash/cmd_send.sh Host-path now calls bearer_cli send with --room-gist-id so joiners polling the gist receive host messages.
lib/airc_core/bearer_gh.py _gh_gist_write_file detects whether messages.jsonl exists and uses the correct gh gist edit mode to avoid silent no-op.
test/integration.sh Adds TDD integration scenarios covering first-send file creation and end-to-end host publish to gist.

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

Comment thread lib/airc_bash/cmd_send.sh
Comment on lines +402 to +403
echo " ⚠ Gist publish failed (kind=${_host_kind:-empty}); broadcast did not reach joiners." >&2
echo " Local audit log has the message; joiners polling the gist see nothing." >&2

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

The warning message says “broadcast did not reach joiners”, but this branch also handles DMs (peer_name != all). Consider rewording to “message did not reach…” (and keep output formatting consistent with other ⚠ warnings in the repo, which typically use two spaces after the symbol).

Suggested change
echo " ⚠ Gist publish failed (kind=${_host_kind:-empty}); broadcast did not reach joiners." >&2
echo " Local audit log has the message; joiners polling the gist see nothing." >&2
echo " ⚠ Gist publish failed (kind=${_host_kind:-empty}); message did not reach its remote destination." >&2
echo " Local audit log has the message; the remote recipient(s) see nothing." >&2

Copilot uses AI. Check for mistakes.
Comment thread test/integration.sh
fail "post-send: gist messages.jsonl missing marker (content len: ${#content})"
fi

trap - EXIT

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

These scenarios set a new trap ... EXIT, which overwrites the suite-level trap cleanup_all EXIT installed earlier in the file, and later trap - EXIT clears the EXIT handler entirely. If the script exits unexpectedly after this point (or later scenarios fail), the global cleanup won’t run. Consider chaining cleanup (include cleanup_all in the EXIT trap) or saving/restoring the previous EXIT trap handler instead of clearing it.

Suggested change
trap - EXIT
trap cleanup_all EXIT

Copilot uses AI. Check for mistakes.
Comment thread test/integration.sh
fi
pass "host published room gist: $gid"

trap "gh gist delete '$gid' --yes 2>/dev/null || true" EXIT

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

Same EXIT-trap issue as above: this overwrites the global trap cleanup_all EXIT and later trap - EXIT removes EXIT cleanup for the remainder of the test run. Please chain or restore the previous EXIT handler so suite cleanup remains reliable even on early exit.

Suggested change
trap "gh gist delete '$gid' --yes 2>/dev/null || true" EXIT
local __airc_prev_exit_trap __airc_prev_return_trap
__airc_prev_exit_trap=$(trap -p EXIT)
__airc_prev_return_trap=$(trap -p RETURN)
trap "gh gist delete '$gid' --yes 2>/dev/null || true; if [ -n \"\$__airc_prev_exit_trap\" ]; then eval \"\$__airc_prev_exit_trap\"; fi" EXIT
trap 'if [ -n "$__airc_prev_exit_trap" ]; then eval "$__airc_prev_exit_trap"; else trap - EXIT; fi; if [ -n "$__airc_prev_return_trap" ]; then eval "$__airc_prev_return_trap"; else trap - RETURN; fi' RETURN

Copilot uses AI. Check for mistakes.
Comment thread test/integration.sh

# Run airc msg from the host scope, broadcasting a unique marker.
local marker="tdd-host-publish-marker-$(date +%s%N)"
AIRC_HOME=/tmp/airc-it-tdd-h/state "$AIRC" msg "$marker" >/dev/null 2>&1 || true

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

airc msg is invoked with || true, which can hide real failures (e.g., cmd_send dying due to monitor down / auth failure). Since this scenario is asserting end-to-end delivery, it should treat a non-zero exit as a test failure (capture stderr/stdout for diagnosis) to avoid masking regressions.

Suggested change
AIRC_HOME=/tmp/airc-it-tdd-h/state "$AIRC" msg "$marker" >/dev/null 2>&1 || true
local send_out send_status
send_out=$(AIRC_HOME=/tmp/airc-it-tdd-h/state "$AIRC" msg "$marker" 2>&1)
send_status=$?
if [ "$send_status" -ne 0 ]; then
fail "airc msg failed with exit $send_status: $send_out"
trap - EXIT
gh gist delete "$gid" --yes 2>/dev/null || true
cleanup_all; return
fi

Copilot uses AI. Check for mistakes.
Comment thread test/integration.sh
Comment on lines +2885 to +2886
gh gist delete "$gid" --yes 2>/dev/null || true
cleanup_all

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

This scenario deletes the room gist before tearing down the host process (cleanup_all runs after gh gist delete). If the host is still running its heartbeat / gist writes, this can introduce flaky behavior or noisy failures. Prefer stopping the host/monitor first (or ensure teardown happens before deleting the gist).

Suggested change
gh gist delete "$gid" --yes 2>/dev/null || true
cleanup_all
cleanup_all
gh gist delete "$gid" --yes 2>/dev/null || true

Copilot uses AI. Check for mistakes.
Comment on lines +162 to +172
# Detect whether messages.jsonl exists in the gist BEFORE choosing
# subcommand. Single extra GET, but eliminates the silent-no-op
# trap. If the GET fails, default to -a (add) since that path
# surfaces real errors when the file already exists (gh complains
# about duplicate filename), whereas plain edit silently no-ops.
existing = _gh_api_get(gist_id)
file_exists_in_gist = (
existing is not None
and isinstance(existing.get("files"), dict)
and _MESSAGES_FILE in existing["files"]
)

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

GhBearer.send() already calls _gh_api_get(gist_id) (line ~288) and then _gh_gist_write_file() calls _gh_api_get(gist_id) again to determine file existence. This doubles API calls per send and increases rate-limit/latency cost. Consider passing the already-fetched gist (or just a file_exists_in_gist boolean derived from it) into _gh_gist_write_file to avoid the extra GET while still avoiding the gh CLI silent-no-op trap.

Copilot uses AI. Check for mistakes.
Comment thread test/integration.sh
Comment on lines +3414 to 3416
gh_send_creates_messages_jsonl) scenario_gh_send_creates_messages_jsonl ;;
host_msg_publishes_to_gist) scenario_host_msg_publishes_to_gist ;;
""|all)

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

The new scenarios are added as selectable MODEs, but they aren’t included in the default ""|all) run list below. Since these are the regression tests for #285, consider invoking them from the all branch as well (they already self-skip when gh isn’t authed, similar to scenario_bearer_gh). Otherwise the main CI/smoke path may not exercise the fix.

Copilot uses AI. Check for mistakes.
Comment thread lib/airc_bash/cmd_send.sh
Comment on lines +392 to +405
if [ -n "$_host_room_gist_id" ]; then
local _host_outcome
_host_outcome=$(printf '%s' "$_host_wire_msg" | "$AIRC_PYTHON" -m airc_core.bearer_cli send \
"$peer_name" "$active_channel" \
--room-gist-id "$_host_room_gist_id")
local _host_kind
_host_kind=$(printf '%s' "$_host_outcome" | "$AIRC_PYTHON" -c 'import json,sys; print(json.load(sys.stdin).get("kind",""))' 2>/dev/null)
case "$_host_kind" in
delivered) : ;;
*)
echo " ⚠ Gist publish failed (kind=${_host_kind:-empty}); broadcast did not reach joiners." >&2
echo " Local audit log has the message; joiners polling the gist see nothing." >&2
;;
esac

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

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

In the host-send branch, non-delivered outcomes from bearer_cli send only emit a warning but the command still proceeds to print the normal success confirmation and exits 0. That reintroduces a “success-shaped surface on failed delivery” (now for gist publish failures). Consider handling auth_failure / transient_failure similarly to the joiner branch (e.g., die for auth_failure, and/or queue to pending.jsonl / return non-zero for transient failures) so failed gist publishes aren’t treated as success.

Copilot uses AI. Check for mistakes.