Compare commits

..

1 Commits

Author SHA1 Message Date
234dc56c5f fix(fleet): watch viewer-session leak + workdir test settle-race
Some checks failed
ci/woodpecker/push/ci Pipeline failed
ci/woodpecker/pr/ci Pipeline failed
The 2 deferred F3 items from the #595/#599 reviews:
- agent watch: wrap the interactive attach in try/finally and kill the grouped
  viewer session in finally — so an attach that throws or an interrupted process
  never leaves a stray <agent>-watch-<pid> session behind (the leak you found).
- test-start-agent-session.sh Test 1: pane_current_path briefly reflects the tmux
  server's cwd until the pane process establishes its -c start dir; poll until it
  settles. Fixes the cwd-dependent false failure that aborted the suite before the
  heartbeat tests (6/7) — those now run.

Verified: full shell suite green from /tmp (was failing); prettier clean.

Refs #588 #542

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 18:38:06 -05:00
4 changed files with 27 additions and 52 deletions

View File

@@ -129,7 +129,7 @@ _start_heartbeat_sidecar() {
# references to any variables from this script's environment. # references to any variables from this script's environment.
local sidecar_script local sidecar_script
sidecar_script=$(printf \ sidecar_script=$(printf \
'hb=%q; pid=%q; iv=%q; mkdir -p "$(dirname "$hb")"; while kill -0 "$pid" 2>/dev/null; do tmp="$hb.tmp.$$"; printf "ts=%%s\npid=%%s\nstatus=ok\n" "$(date +%%Y-%%m-%%dT%%H:%%M:%%S%%z)" "$pid" > "$tmp" && mv "$tmp" "$hb"; sleep "$iv"; done' \ 'hb=%s; pid=%s; iv=%s; mkdir -p "$(dirname "$hb")"; while kill -0 "$pid" 2>/dev/null; do tmp="$hb.tmp.$$"; printf "ts=%%s\npid=%%s\nstatus=ok\n" "$(date +%%Y-%%m-%%dT%%H:%%M:%%S%%z)" "$pid" > "$tmp" && mv "$tmp" "$hb"; sleep "$iv"; done' \
"$hb_file" "$pane_pid" "$interval") "$hb_file" "$pane_pid" "$interval")
# setsid + disown ensures the sidecar survives this script exiting. # setsid + disown ensures the sidecar survives this script exiting.

View File

@@ -32,8 +32,15 @@ MOSAIC_AGENT_COMMAND='bash --noprofile --norc -i' \
"$START" "$AGENT" "$START" "$AGENT"
tmux -L "$SOCKET" has-session -t "=$AGENT:0.0" || fail "agent session was not created" tmux -L "$SOCKET" has-session -t "=$AGENT:0.0" || fail "agent session was not created"
actual_dir=$(tmux -L "$SOCKET" display-message -p -t "=$AGENT:0.0" '#{pane_current_path}') # Retry: pane_current_path briefly reflects the tmux server's cwd until the pane
[ "$actual_dir" = "$WORKDIR" ] || fail "agent workdir mismatch: $actual_dir" # process establishes its own cwd (the -c start dir). Poll until it settles.
actual_dir=""
for _ in $(seq 1 30); do
actual_dir=$(tmux -L "$SOCKET" display-message -p -t "=$AGENT:0.0" '#{pane_current_path}')
[ "$actual_dir" = "$WORKDIR" ] && break
sleep 0.1
done
[ "$actual_dir" = "$WORKDIR" ] || fail "agent workdir mismatch: $actual_dir (expected $WORKDIR)"
# ── Test 2: idempotency (duplicate start prints 'already running') ───────────── # ── Test 2: idempotency (duplicate start prints 'already running') ─────────────
MOSAIC_TMUX_SOCKET="$SOCKET" \ MOSAIC_TMUX_SOCKET="$SOCKET" \

View File

@@ -2892,33 +2892,3 @@ describe('fleet init wizard', () => {
expect(content).toContain('name: coder0'); expect(content).toContain('name: coder0');
}); });
}); });
describe('fleet ps — heartbeat path resolution', () => {
const savedRunDir = process.env.MOSAIC_HEARTBEAT_RUN_DIR;
const savedHome = process.env.MOSAIC_HOME;
afterEach(() => {
if (savedRunDir === undefined) delete process.env.MOSAIC_HEARTBEAT_RUN_DIR;
else process.env.MOSAIC_HEARTBEAT_RUN_DIR = savedRunDir;
if (savedHome === undefined) delete process.env.MOSAIC_HOME;
else process.env.MOSAIC_HOME = savedHome;
});
it('honors MOSAIC_HEARTBEAT_RUN_DIR (matches the writer sidecar override)', () => {
process.env.MOSAIC_HEARTBEAT_RUN_DIR = '/run/hb';
expect(heartbeatPath('agent-x', '/any/home')).toBe(join('/run/hb', 'agent-x.hb'));
});
it('honors MOSAIC_HOME when no explicit mosaicHome is given', () => {
delete process.env.MOSAIC_HEARTBEAT_RUN_DIR;
process.env.MOSAIC_HOME = '/custom/mhome';
expect(heartbeatPath('agent-y')).toBe(join('/custom/mhome', 'fleet', 'run', 'agent-y.hb'));
});
it('falls back to <mosaicHome>/fleet/run by default', () => {
delete process.env.MOSAIC_HEARTBEAT_RUN_DIR;
delete process.env.MOSAIC_HOME;
expect(heartbeatPath('agent-z', '/home/u/.config/mosaic')).toBe(
join('/home/u/.config/mosaic', 'fleet', 'run', 'agent-z.hb'),
);
});
});

View File

@@ -152,16 +152,13 @@ export function resolveFleetPaths(mosaicHome = defaultMosaicHome()): FleetPaths
} }
function defaultMosaicHome(): string { function defaultMosaicHome(): string {
// Honor MOSAIC_HOME so the reader matches the writer sidecar (and the launcher), return join(homedir(), '.config', 'mosaic');
// even when MOSAIC_HOME is set in the shell without an explicit --mosaic-home flag.
return process.env.MOSAIC_HOME ?? join(homedir(), '.config', 'mosaic');
} }
function assertDefaultMosaicHomeForSystemd(mosaicHome: string): void { function assertDefaultMosaicHomeForSystemd(mosaicHome: string): void {
const literalHome = join(homedir(), '.config', 'mosaic'); if (resolve(mosaicHome) !== resolve(defaultMosaicHome())) {
if (resolve(mosaicHome) !== resolve(literalHome)) {
throw new Error( throw new Error(
`install-systemd only supports the default Mosaic home (${literalHome}) because the user systemd units use %h/.config/mosaic paths.`, `install-systemd only supports the default Mosaic home (${defaultMosaicHome()}) because the user systemd units use %h/.config/mosaic paths.`,
); );
} }
} }
@@ -478,10 +475,7 @@ export function parseTmuxListSessions(output: string): string[] {
* Returns the heartbeat file path for an agent. * Returns the heartbeat file path for an agent.
*/ */
export function heartbeatPath(agentName: string, mosaicHome = defaultMosaicHome()): string { export function heartbeatPath(agentName: string, mosaicHome = defaultMosaicHome()): string {
// Honor MOSAIC_HEARTBEAT_RUN_DIR (the writer sidecar's override); otherwise the return join(mosaicHome, 'fleet', 'run', `${agentName}.hb`);
// canonical <mosaicHome>/fleet/run. Keeps reader and writer on the same path.
const runDir = process.env.MOSAIC_HEARTBEAT_RUN_DIR ?? join(mosaicHome, 'fleet', 'run');
return join(runDir, `${agentName}.hb`);
} }
/** /**
@@ -1444,15 +1438,19 @@ export function registerFleetAgentCommands(
await runChecked(runner, buildAgentWatchCreateViewerCommand(agent, viewerName, socketName)); await runChecked(runner, buildAgentWatchCreateViewerCommand(agent, viewerName, socketName));
const [bin, args] = splitCommand(buildAgentWatchAttachCommand(viewerName, socketName)); let exitCode = 0;
const exitCode = await iRunner(bin, args); try {
const [bin, args] = splitCommand(buildAgentWatchAttachCommand(viewerName, socketName));
// Best-effort cleanup of the viewer session regardless of how the user detached. exitCode = await iRunner(bin, args);
// Errors here are intentionally suppressed — the agent session is unaffected. } finally {
const killResult = await runner( // ALWAYS clean up the viewer session — even if attach threw or the process was
...splitCommand(buildAgentWatchKillViewerCommand(viewerName, socketName)), // interrupted — so stale grouped *-watch-* sessions never accumulate. Errors here
); // are intentionally suppressed; the agent session is unaffected.
void killResult; // result is intentionally ignored const killResult = await runner(
...splitCommand(buildAgentWatchKillViewerCommand(viewerName, socketName)),
);
void killResult;
}
if (exitCode !== 0) { if (exitCode !== 0) {
process.exitCode = exitCode; process.exitCode = exitCode;