aft icon indicating copy to clipboard operation
aft copied to clipboard

Bridge: a foreground bash result keeps an idle session open (the idle reaper does not reap it)

Open iceteaSA opened this issue 6 days ago • 2 comments

Summary

The bridge's idle reaper does not remove a session after that session runs one foreground bash command. The session record keeps its tool route and its bg_events subscription until the plugin stops. In #1476 you wrote that a project goes idle only when its last route closes. Thus a project where one open session ran a foreground command does not go idle.

Tested on v0.68.1 (the latest release) and on main at df341f683.

Cause

Line numbers are at v0.68.1.

  1. A foreground bash reply has a task_id, a terminal status and an output_preview (crates/aft/src/commands/bash_orchestrate.rs:1270-1277).
  2. observeBackgroundReply records each bash reply that has a task_id and a terminal status as "terminal" in record.backgroundTasks (packages/aft-bridge/src/subc-transport.ts:2153-2162: "Terminal tasks remain pinned until the daemon explicitly ACKs them."). It removes the entry only when a bash_ack_completions reply names the task (:2194-2215).
  3. The module completes the foreground result itself: acknowledge_terminal_bash_reply (crates/aft/src/response_finalize.rs:228-268) acknowledges it for the session. Thus bash_drain_completions does not give it to the plugin, and the plugin does not send an ACK for it.
  4. reapIdleSessions skips a record while record.backgroundTasks.size > 0 (subc-transport.ts:2072). The entry from step 2 stays, so the record stays.

Session deletion does not help here. On main, the OpenCode 2 deletion handler closes the routes of a deleted session (#1476). But most sessions stay open, or nobody deletes them. For those sessions, the idle reaper is the only path.

Test

Add this file as packages/aft-bridge/src/__tests__/session-reap-gaps.test.ts:

import { expect, test } from "bun:test";
import { SubcTransportPool } from "../subc-transport.js";
import { observeFreshSessionStart } from "../transport.js";
import { TEST_PROJECT_ROOT } from "./subc-test-roots.js";

function makePool(reply: Record<string, unknown> = { success: true, text: "read" }) {
  return new SubcTransportPool({
    connectionFile: "/fake",
    harness: "pi",
    connect: async () => ({
      routeOpen: async () => ({ channel: 1, epoch: 1 }),
      request: async () => ({ structuredContent: reply }),
      subscribe: () => {
        throw new Error("no background handler is registered");
      },
      closeRouteChannel: async () => undefined,
      close: () => undefined,
    }),
  });
}

test("control: an observed task-free session is reaped after the idle TTL", async () => {
  const pool = makePool();
  try {
    observeFreshSessionStart(pool, TEST_PROJECT_ROOT, "fresh", "new");
    await pool.toolCall(TEST_PROJECT_ROOT, { sessionID: "fresh" }, "read");
    expect(pool.__retainedSessionCountsForTests().sessions).toBe(1);
    await pool.reapIdleSessions(Date.now() + 16 * 60_000);
    expect(pool.__retainedSessionCountsForTests().sessions).toBe(0);
  } finally {
    await pool.shutdown();
  }
});

test("a delivered foreground bash result does not pin an observed idle session", async () => {
  const pool = makePool({
    success: true,
    text: "ready",
    task_id: "bash-0123456789abcdef",
    status: "completed",
    exit_code: 0,
    output_preview: "ready\n",
  });
  try {
    observeFreshSessionStart(pool, TEST_PROJECT_ROOT, "fresh", "new");
    await pool.toolCall(TEST_PROJECT_ROOT, { sessionID: "fresh" }, "bash", {
      command: "echo ready",
      background: false,
    });
    await pool.reapIdleSessions(Date.now() + 16 * 60_000);
    expect(pool.__retainedSessionCountsForTests().sessions).toBe(0);
  } finally {
    await pool.shutdown();
  }
});

Run: cd packages/aft-bridge && bun test src/__tests__/session-reap-gaps.test.ts

Result on v0.68.1 and on main at df341f683: the control passes. The foreground test fails with Expected: 0, Received: 1.

Possible repair

Change observeBackgroundReply for a bash or powershell reply that has a string output_preview. Do not record that reply as pending terminal work. acknowledge_terminal_bash_reply uses the same test to identify an inline result that the module completed. A fast background launch reply has no output_preview, so it stays recorded until its ACK.

We also saw the same pin after a bash request that the module refuses before launch (invalid_request). main has a repair for that case (b8d0bca75).

iceteaSA avatar Nov 27 '26 21:11 iceteaSA

Thanks, confirmed. Your test reproduced it exactly. The bridge recorded an inline foreground bash result as pending background work, but the module had already acknowledged that result. So no acknowledgement ever cleared it, and the idle reaper skipped the session.

The fix makes the bridge use the module's own rule, and both sides now name that rule in a comment. A terminal bash or powershell reply that carries its output no longer pins the session. Neither does a terminal bash_watch for a task the session tracks. Detached commands and fast background launches still wait for their acknowledgement. Inline PowerShell results were also missing from the module's rule, and that's fixed too. Your test is included, along with cases for PowerShell and bash_watch. It will ship in the next AFT release, and I'll update this issue when it's out.

aft-alfonso[bot] avatar Nov 28 '26 09:11 aft-alfonso[bot]

Retest on PR #1536 at commit abd64ed00. The six subsequent commits on the PR (head d273f2eb3) do not change the files of this repair.

The regression test from this issue gives 4 pass, 0 fail (15 assertions) at that commit. After the bridge receives a foreground bash result, the idle reaper removes the session when the idle TTL ends. On v0.68.0, v0.68.1 and main at df341f683, the same test kept 1 session.

The test uses fake module replies. I did not test with a live daemon.

iceteaSA avatar Nov 28 '26 19:11 iceteaSA