Skip to content

feat(bin): teleport a running task to a remote second mate and back - #65

Merged
alandomio merged 4 commits into
mainfrom
fm/teleport-task
Oct 20, 2026
Merged

alandomio merged 4 commits into
mainfrom
fm/teleport-task

Conversation

@alandomio

Copy link
Copy Markdown
Owner

Intent

Implement "teleport" for firstmate: move a running task (worker) from this home to a remote second mate and back, per the captain's decisions (2026-10-15): the unit is a running ship or scout worker that continues on the remote; work travels via git as a WIP commit on fm/teleport/, pushed, the remote recreates the worktree from it and the WIP commit is dropped after takeover (no rsync); context travels by resuming the Claude transcript (~/.claude/projects//.jsonl, claude --resume) when the harness is claude, otherwise brief plus a generated progress note in the fm-control relaunch style; the transcript travels only over the existing fm-on.sh SSH channel and is deleted on the laptop after successful takeover; the remote second mate takes the task into its own backlog by reusing bin/fm-backlog-handoff.sh and supervises it; teleport back is the same mechanism reversed; default target is the alwayson second mate.

Deliver:

  1. bin/fm-teleport.sh [--to ] (default alwayson) and --back: stop the local worker via fm-control (interrupt, verify idle), WIP-commit everything (tracked and untracked, honouring .gitignore) onto fm/teleport/, push, hand the backlog item over with fm-backlog-handoff.sh, have the remote home recreate the worktree from that ref and relaunch the worker there through the existing remote secondmate control path (fm-on.sh), resuming the Claude transcript when harness=claude (copy the session jsonl over the fm-on SSH channel, path-mapped), else relaunch with brief plus a generated progress note. Delete the local transcript copy and tear down the local worktree only after the remote confirms takeover; on any failure leave the local task intact and runnable. --back reverses it.
  2. The sleep reminder: when the laptop is about to sleep (or a lid-close signal is available) and local workers are running, raise one notification "N tasks running, teleport out?" through the existing wedge-alarm/notification path. Manual only, no automatic handoff.
  3. Docs: update docs/remote-secondmates.md (it said individual workers cannot move) and add a teleport section; the script header owns flags.
  4. Tests in the repo's existing test style covering: WIP ref creation, refusal on dirty remote, failure leaves local intact, back-teleport, non-claude fallback.
    Do not run a real teleport against the live alwayson home; use test fixtures.

Implementation decisions a reviewer should know:

  • fm-teleport.sh is both the operator command and a set of protocol verbs (describe, check, pack, chunk-get, chunk-put, adopt, abort, release, backlog-export, backlog-start) that each run in the home owning that side, locally or through fm-on.sh, so out and --back share one code path with the roles swapped.
  • The WIP commit is built through a private GIT_INDEX_FILE (read-tree HEAD, add -A, write-tree, commit-tree), so snapshotting never moves the worker's HEAD, branch, index, or files; trailers Firstmate-Teleport and Teleport-Branch make it self-describing. Restore checks out the branch at the WIP parent and lays the WIP tree in as uncommitted changes (read-tree -u --reset then reset), refusing to move an existing local branch that is not an ancestor.
  • The payload (data// records, transcript, last status lines) is a tarball split into sha256-checked chunks because fm-on's remote job input is capped at 1 MiB.
  • Adoption goes through fm-spawn.sh with a new --teleport flag that restores into the fresh pooled worktree after the base freshen and, for a Claude session, launches claude --resume <id> with an arrival note as the prompt. Refused with --relaunch, --secondmate, batch spawns, and the orca backend (orca owns its worktrees).
  • The adopting home path-maps the sending home's absolute paths (records, transcript, worktree) to its own, because briefs bake absolute status-file paths.
  • Ordering: remote check, pack, transfer, adopt (waits for the agent to read alive, rolls itself back on failure), backlog handoff (rolls the remote back on failure), then release on the sending side, then delete the WIP ref. Release and abort only clean up a worktree whose content still equals the snapshot, detaching it at the WIP commit so the normal landed-work teardown passes without --force; they refuse when another task records the same worktree (treehouse can hand one worktree to two tasks). abort refuses a task that this home did not adopt.
  • Teleport refuses during an active no-mistakes validation run (fm-crew-state run-step working or parked).
  • The away-mode wedge alarm's channel code moved verbatim into bin/fm-alert-lib.sh (title made a parameter) so fm-teleport.sh remind [--lid] reuses the same channels; remind is rate-limited and wired by the operator to a sleep hook (sleepwatcher, systemd system-sleep), since macOS offers no built-in pre-sleep hook to a script.
  • FM_TELEPORT_BIN and FM_TELEPORT_PROBE are test seams; tests use real git and tasks-axi with fixture homes.
  • Live evidence: claude -p --resume resumed a transcript copied into another directory's project folder with paths rewritten by fm_teleport_map_paths and recalled its earlier context.

What Changed

  • Adds bin/fm-teleport.sh (plus bin/fm-teleport-lib.sh). It moves a running worker to a remote second mate (alwayson by default) and brings it back with --back. The command stops the worker and snapshots its work as a WIP commit on fm/teleport/<task> through a private index, so the worker's HEAD, index and files never move. It sends the task records and transcript as sha256-checked chunks over fm-on.sh and adopts the task on the remote. It then hands over the backlog item with fm-backlog-handoff.sh and cleans up the local side only after the remote confirms takeover; if any step fails, the local task stays intact and runnable. fm-spawn.sh gets a --teleport flag that restores the snapshot into a fresh pooled worktree and runs claude --resume with paths mapped to the new home (non-Claude harnesses get the brief plus a generated progress note).
  • Moves the wedge-alarm notification channels out of fm-supervise-daemon.sh into a shared bin/fm-alert-lib.sh. This adds fm-teleport.sh remind [--lid], a rate-limited "N tasks running, teleport out?" notification meant to be hooked to sleep or lid-close.
  • Updates docs/remote-secondmates.md (individual workers can now move, plus a new teleport section) and docs/wedge-alarm.md. Adds tests/fm-teleport.test.sh with fixture homes covering WIP ref creation, refusal on a dirty remote, failure leaving the local task intact, teleporting back and the non-Claude fallback.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The latest fix round only expands a warning string, the matching docs sentence, and one test assertion; all three agree and match the captain's instruction.

Testing

I ran the focused teleport suite. It drives the full CLI flows (out, --back, failure and rollback paths, the backlog hold and handoff warnings, a note round trip, and the sleep reminder) against fixture homes. All 14 scenarios pass. I also ran the daemon test as a regression check on the shared notification path, and it passes. No real teleport was run against the live alwayson home, as the intent requires. There is no UI surface, so the CLI test transcript is the evidence.

Evidence: fm-teleport test transcript

Source: fm-teleport test transcript

ok - teleport refuses a dirty or already-occupied remote before touching the worker
ok - a failed teleport leaves the local task intact and runnable
ok - pack snapshots tracked and untracked work onto fm/teleport/<task> without moving the worker
ok - teleport out moves work, records, transcript, and backlog item to the remote and cleans up here
ok - teleport --back reverses the move
ok - a non-Claude task relaunches from its brief plus a generated progress note
ok - a Claude task without a transcript falls back to brief plus progress note
ok - a failed backlog handoff after takeover warns instead of rolling the remote back
ok - an early backlog handoff failure holds the local item instead of leaving it dispatchable
ok - a round trip replaces the earlier teleport note
ok - teleport refuses mid-validation
ok - abort refuses a task this home did not adopt
ok - remind raises one rate-limited notification only while workers run
ok - fm-spawn refuses --teleport with relaunch, secondmate, or batch spawns

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 3 issues found → auto-fixed (3) ✅
  • ⚠️ bin/fm-teleport.sh:540 - Rolling back the remote after a failed backlog handoff (teleport_out, the abort call at line 540) goes through retire_copy, and retire_copy refuses unless the remote worktree still equals the snapshot. The adopted worker is already alive at that point and may have edited files, so the abort fails. The result: the remote keeps a live copy, the local copy stays idle, and the only signal is a warning telling the operator to abort by hand. A narrower case to check: if fm-spawn --teleport fails after writing the meta but before restoring the work, the worktree never matched the snapshot, so cmd_adopt's abort also dies and the receiving home refuses later retries. Consider interrupting the adopted worker before the handoff, or running the handoff before the adopted worker launches.
  • ℹ️ bin/fm-teleport.sh:383 - For a non-Claude worker, or a Claude worker with no transcript, adopt appends a progress note to data/<task>/brief.md. The brief travels with the task, so each round trip (out then --back) adds another 'Teleport note' section and an older note can contradict the current state. Consider replacing an earlier teleport note instead of appending a new one.
  • ℹ️ bin/fm-teleport.sh:527 - When pack, transfer, or adopt fails, the pushed fm/teleport/<task> ref stays on origin and state/teleport/<task>/out stays on disk. Nothing breaks, because a retry force-pushes and recreates the staging directory, but the ref is left behind after a failed teleport.

🔧 Fix: Keep remote ownership on handoff failure; dedupe teleport notes
1 warning still open:

  • ⚠️ bin/fm-teleport.sh:545 - When fm-backlog-handoff fails before it stages an outbox, the item stays in the local backlog. It was reopened at line 540, so it sits in a dispatchable section. Release then tears down the local copy, and nothing marks the item as owned by the remote, so the local dispatcher can spawn the task again while the remote worker keeps running it. The only guard is the warning asking the operator to move it by hand. Possible remedy: put the item back in 'In flight' locally (or annotate it) on that path so it is not re-dispatched. This changes the backlog behaviour the captain specified, so it needs their decision.

🔧 Fix: Hold local backlog item when teleport handoff fails early
1 info still open:

  • ℹ️ bin/fm-teleport.sh:548 - When the handoff fails before staging, the local item gets a parked hold, and the warning tells the operator to run bin/fm-backlog-handoff.sh &lt;sm&gt; &lt;task&gt; by hand. That handoff moves the item along with its parked hold, and the manual path never runs backlog-start on the remote. The second mate's backlog can therefore end up with the task parked and not in 'In flight', even though its worker is running. Consider having the warning also say to clear the hold (tasks-axi unhold) and mark the item in flight on the remote. Another option is a small recovery verb that does both.

🔧 Fix: Spell out full recovery in early handoff failure warning
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-teleport.test.sh (14 scenarios: WIP ref creation without moving the worker, refusal on a dirty or occupied remote, a failed teleport leaving the local task intact, a full out move including transcript and backlog, --back, the non-Claude fallback and the Claude fallback with no transcript, a failed handoff after takeover warning instead of rolling back, an early handoff failure placing a parked hold, a round trip leaving one teleport note, refusal during validation, abort refusing a task this home did not adopt, rate-limited remind, fm-spawn --teleport refusals)
  • bash tests/fm-daemon.test.sh (regression check after the alert channel code moved into fm-alert-lib)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Alan Domio added 4 commits October 16, 2026 09:55
bin/fm-teleport.sh moves a ship or scout worker into a remote second-mate
home (default alwayson) and back with --back. The work travels as one WIP
commit on fm/teleport/<task> built through a private index, the task's
records travel path-mapped, and a Claude worker resumes its own transcript
in the new worktree; other harnesses relaunch from the brief plus a
generated progress note. The backlog item follows the task. Until the
adopting home confirms its worker is alive, any failure rolls the adopting
side back and leaves the sending task intact with its worker idle.

fm-spawn.sh gains --teleport to restore the work into a fresh pooled
worktree and resume the Claude session. The away-mode alert channels move
into bin/fm-alert-lib.sh so `fm-teleport.sh remind` can raise the manual
"N tasks running, teleport out?" notification through them.
@alandomio
alandomio merged commit 56d9e09 into main Oct 20, 2026
alandomio pushed a commit that referenced this pull request Oct 20, 2026
Conflicts resolved keeping both sides:
- bin/fm-brief.sh: upstream's shared infra rule, wait block and inbox
  section kept; the Haiku subagent section from #66 added after them.
- bin/fm-spawn.sh: --teleport from #65 ported onto upstream's
  record-backed launch brief; the arrival note is published as the
  launch record and the Claude launch resumes the teleported session.
- bin/fm-supervise-daemon.sh: wedge alarm helpers live in
  bin/fm-alert-lib.sh (#65); they were unchanged on our side since the
  merge base, so nothing is lost. Upstream's delivery-failure fields kept.
- tests/fm-teleport.test.sh: covers the teleported Claude launch on the
  record-backed brief path.

Co-Authored-By: Claude Opus 5.5 <[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