fix(ida): let one-shot commands exit after retaining cleanup (#1841)

* fix(ida): let one-shot commands exit after retaining cleanup

When a headless IDA request outlives timeoutMs (for example idb_open on
a large binary still running auto-analysis), REA cannot prove the
worker stopped and correctly retains the upstream connection and
private workspace. The retained stdio child and its pipes stayed
referenced, so a one-shot CLI printed its cleanup_incomplete result and
then never exited, keeping the supervisor, worker and database alive.

Detach the retained connection's child process and pipes from the event
loop when cleanup fails. A long-lived MCP server can still retry
through the retained connection; a one-shot CLI exits with status 1,
and the supervisor observes end of input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): report an unverified upstream worker after failed cleanup

Upstream ida-pro-mcp spawns headless workers in their own session and
deliberately leaves them running when its supervisor exits, so neither
a retained connection nor REA exiting proves that a timed-out worker
stopped. When cleanup fails before the owned worker's release is
confirmed, the cleanup diagnostics now name its private session and
state that it may still be running until it finishes and reaches its
idle timeout. Failures after a confirmed release, or before any open
was attempted, do not report a worker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): name the inventory sessions that block worker release

When idb_close confirmed the requested worker gone but the follow-up
inventory still found a different peer on the private input, the
upstream_worker diagnostic named the released session and could
misdirect manual cleanup. Record the session ids of the inventory
entries that actually blocked release and report those; fall back to
the requested private session only when no inventory evidence exists
(an incomplete open or an unanswered request). The field becomes
session_ids, since more than one entry can block release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): keep blocker evidence and backend in cleanup diagnostics

The release diagnostic reset its observed blockers on every close, so a
retry whose inventory was unavailable fell back to the already-released
session; and it kept only session ids, describing a blocking GUI
session with detached-worker guidance.

Blocker evidence is now replaced only by a successful inventory, and
the diagnostic becomes upstream_sessions with each session's id and
backend, with guidance that distinguishes workers from GUI sessions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): stop naming a worker after its close was confirmed

When idb_close confirmed the requested worker's release but the
following inventory failed, no blocker evidence existed yet and the
diagnostic fell back to the confirmed-absent worker. After a confirmed
close the diagnostic now reports an empty session list, meaning any
remaining same-input sessions are unknown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ida): group cleanup diagnostic cases in their own suite

The added cases pushed the reaped-worker describe block past the
150-line limit enforced by lint:built.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): keep ownership and pre-close peers in cleanup diagnostics

Blocking sessions dropped the parsed ownership fact, so an external
unowned worker looked the same as REA's own; and recovery inventory
recorded same-input peers only when the requested worker was missing,
so a peer seen beside it was lost if the following close failed.

Each reported session now carries owned (null when no inventory has
reported it), and recovery records every same-input session until a
later successful inventory replaces that evidence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): drop a confirmed worker from retained cleanup blockers

Recovery inventory now records the requested worker beside any peers,
so once idb_close confirmed its release a failing follow-up inventory
still reported it as unreleased. Remove the confirmed session from the
retained blockers at close confirmation and keep the remaining peers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ida): retry cleanup through a detached stdio connection

The retry coverage used a recording connection whose detach() only
counts calls, so it could not show that detaching keeps a retained
connection usable. Run a real stdio supervisor in a long-lived host,
fail the first post-close inventory, and require the retry through the
same detached connection to confirm release and remove the workspace.
The test fails if detach() destroys the pipes instead of unref-ing them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ida): close the stdio supervisor in failed retry tests

Merge main and give its new IdaNativeProtocol connection double the
required detach() method. The real-stdio retry test now closes its
connection in an unconditional teardown, so a failing assertion cannot
leave the supervisor waiting on stdin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ida): share the cleanup diagnostic expectation in session tests

Repeated upstream_sessions assertions and the unrelated live peer
literal pushed repository duplication over the jscpd threshold. Build
the expected diagnostic with one helper and reuse one peer constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ida): distinguish observed ownership from unreported input bindings

* fix(ida): keep requested-ID conflicts in recovery cleanup

Recovery inventory kept only same-input entries, unlike the startup and
post-close filters. After an explicitly rejected open, an entry under
the requested session ID on another input was dropped, so cleanup could
close the connection, remove the private workspace and report success
while that session stayed registered. Match the requested ID as well so
the entry is retained as a blocker and cleanup stays incomplete.

Also split the cleanup diagnostics suite under the lint:built function
length limit, and make two startup-inventory fixtures pass a defined
session id so the test file typechecks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Xingchuan Ma <xingchuan.ma@msn.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
This commit is contained in:
Max
2026-10-13 21:36:52 +08:00
committed by GitHub
co-authored by Xingchuan Ma Claude Opus 5.5 morluto
parent 811bd9f635
commit 5dd2ce3125
9 changed files with 596 additions and 17 deletions
+251
View File
@@ -16,6 +16,18 @@ afterEach(async () => {
for (const root of roots.splice(0))
await rm(root, { recursive: true, force: true });
});
/** A failed close whose diagnostic names exactly these unverified sessions. */
const reportsSessions = (...sessions: readonly object[]) => ({
error: {
diagnostics: { upstream_sessions: { state: "unverified", sessions } },
},
});
const LIVE_PEER = {
session_id: "unrelated-live-peer",
backend: "worker",
owned: false,
} as const;
const fixture = async (
mode: "attached" | "headless" = "attached",
timeoutMs = 1000,
@@ -792,10 +804,19 @@ describe("headless IDA session ownership and cleanup", () => {
it("retains the private workspace and reports cleanup failure when worker release is unverified", async () => {
const { producer, client, root } = await fixture("headless");
await client.execute("health", {});
const sessionId = producer.database;
producer.failClose = true;
const result = await client.close();
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error.cleanupIncomplete).toBe(true);
expect(result).toMatchObject(
reportsSessions({
session_id: sessionId,
backend: "worker",
owned: true,
input_path: producer.inputPath,
}),
);
expect(
(await readdir(root)).some((name) => name.startsWith("rea-ida-")),
).toBe(true);
@@ -843,10 +864,20 @@ describe("headless IDA reaped worker cleanup", () => {
const result = await client.close();
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error.cleanupIncomplete).toBe(true);
// The released session must not be named as the remaining worker.
expect(result).toMatchObject(reportsSessions(LIVE_PEER));
expect(producer.closes).toBe(0);
expect(
(await readdir(root)).some((name) => name.startsWith("rea-ida-")),
).toBe(true);
// A retry whose inventory is unavailable keeps the observed blocker.
producer.beforeCall = async (name) => {
if (name === "idb_list") throw new Error("inventory unavailable");
};
const retried = await client.close();
expect(retried.ok).toBe(false);
expect(retried).toMatchObject(reportsSessions(LIVE_PEER));
});
it("retries independent inventory verification after an absent-session response", async () => {
@@ -899,6 +930,203 @@ describe("headless IDA reaped worker cleanup", () => {
});
});
/** A rejected open response, with helpers for separately inventoried workers. */
const rejectedOpenFixture = async () => {
const { producer, client } = await fixture("headless");
producer.overrideCall = (name) =>
name === "idb_open" ? { error: "open did not complete" } : undefined;
expect((await client.execute("health", {})).ok).toBe(false);
const open = producer.calls.find(({ name }) => name === "idb_open")?.args;
const entry = (session_id: string, owned: boolean) => ({
session_id,
input_path: String(open?.input_path),
owned,
backend: "worker",
is_active: true,
});
return {
producer,
client,
requested: String(open?.preferred_session_id),
entry,
};
};
describe("headless IDA cleanup diagnostics", () => {
it("does not name a worker whose close was confirmed when inventory then fails", async () => {
const { producer, client } = await fixture("headless");
expect((await client.execute("health", {})).ok).toBe(true);
producer.beforeCall = async (name) => {
if (name === "idb_list") throw new Error("inventory unavailable");
};
const result = await client.close();
expect(result.ok).toBe(false);
expect(producer.calls.map(({ name }) => name)).toContain("idb_close");
expect(result).toMatchObject(reportsSessions());
});
it("retains peers and confirmed ownership from successful startup when close fails", async () => {
const { producer, client } = await fixture("headless");
producer.overrideCall = (name) => {
if (name === "idb_list")
return {
sessions: [
{
session_id: String(producer.database),
input_path: producer.inputPath,
owned: true,
backend: "worker",
is_active: true,
},
{
session_id: "startup-peer",
input_path: producer.inputPath,
owned: false,
backend: "worker",
is_active: false,
},
],
};
if (name === "idb_close") return { error: "close rejected" };
return undefined;
};
expect((await client.execute("health", {})).ok).toBe(true);
const result = await client.close();
expect(result).toMatchObject(
reportsSessions(
{
session_id: producer.database,
backend: "worker",
owned: true,
input_path: producer.inputPath,
},
{
session_id: "startup-peer",
backend: "worker",
owned: false,
input_path: producer.inputPath,
},
),
);
});
it("preserves an observed conflicting input binding after close", async () => {
const { producer, client } = await fixture("headless");
expect((await client.execute("health", {})).ok).toBe(true);
const requested = String(producer.database);
producer.overrideCall = (name) =>
name === "idb_list"
? {
sessions: [
{
session_id: requested,
input_path: "/different/input",
owned: false,
backend: "worker",
is_active: true,
},
],
}
: undefined;
const result = await client.close();
expect(result).toMatchObject(
reportsSessions({
session_id: requested,
backend: "worker",
owned: false,
input_path: "/different/input",
}),
);
});
it("keeps peers seen beside the requested worker when its close fails", async () => {
const { producer, client, requested, entry } = await rejectedOpenFixture();
producer.overrideCall = (name) => {
if (name === "idb_list")
return {
sessions: [
entry(requested, true),
entry("unrelated-live-peer", false),
],
};
if (name === "idb_close") return { error: "close rejected" };
return undefined;
};
const result = await client.close();
expect(result.ok).toBe(false);
expect(result).toMatchObject(
reportsSessions(
{ session_id: requested, backend: "worker", owned: true },
LIVE_PEER,
),
);
});
});
describe("headless IDA cleanup diagnostics for observed peers", () => {
it("drops the requested worker from blockers once its close is confirmed", async () => {
const { producer, client, requested, entry } = await rejectedOpenFixture();
let inventories = 0;
producer.beforeCall = async (name) => {
if (name === "idb_list" && ++inventories > 1)
throw new Error("inventory unavailable");
};
producer.overrideCall = (name) => {
if (name === "idb_list")
return {
sessions: [
entry(requested, true),
entry("unrelated-live-peer", false),
],
};
if (name === "idb_close")
return {
success: true,
session_id: requested,
saved: false,
owned: true,
backend: "worker",
};
return undefined;
};
const result = await client.close();
expect(result.ok).toBe(false);
expect(result).toMatchObject(reportsSessions(LIVE_PEER));
});
it("reports a blocking GUI session by its backend", async () => {
const { producer, client } = await fixture("headless");
expect((await client.execute("health", {})).ok).toBe(true);
const sessionId = producer.database;
producer.overrideCall = (name) => {
if (name === "idb_close")
return { error: `Session not found: ${sessionId}` };
if (name === "idb_list")
return {
sessions: [
{
session_id: "",
input_path: producer.inputPath,
owned: false,
backend: "gui",
is_active: true,
},
],
};
return undefined;
};
const result = await client.close();
expect(result.ok).toBe(false);
expect(result).toMatchObject(
reportsSessions({ session_id: "", backend: "gui", owned: false }),
);
});
});
/** A completed open response is not proof that its private Worker was ever released. */
describe("headless IDA unresolved successful-open ownership", () => {
it.each(["matching", "foreign-input", "malformed"] as const)(
@@ -976,6 +1204,29 @@ describe("headless IDA unresolved successful-open ownership", () => {
expect(producer.closes).toBe(1);
expect(await readdir(root)).toEqual(["target.elf"]);
});
it("retains cleanup when a rejected open leaves the requested ID on another input", async () => {
const { producer, client, requested } = await rejectedOpenFixture();
producer.overrideCall = (name) =>
name === "idb_list"
? {
sessions: [
{
session_id: requested,
input_path: "/different/input",
owned: false,
backend: "worker",
is_active: true,
},
],
}
: undefined;
const result = await client.close();
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error.cleanupIncomplete).toBe(true);
expect(producer.closes).toBe(0);
});
});
describe("headless IDA cleanup verification", () => {