Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(adb): preserve command owner cancellation outcomes
morlutoc4475d4a6739
diff --git a/src/adb/AdbCommand.test.ts b/src/adb/AdbCommand.test.tsindex 63bb5ab0a..a4fbe0c70 100644--- a/src/adb/AdbCommand.test.ts+++ b/src/adb/AdbCommand.test.ts@@ -1,6 +1,9 @@import { expect, it, vi } from "vitest";-import { execFileBytes } from "../process/ExecFileOutput.js";-import { probeAdbPathExistence } from "./AdbCommand.js";+import {+ ExecFileOutputCancelledError,+ execFileBytes,+} from "../process/ExecFileOutput.js";+import { probeAdbPathExistence, runAdbCapture } from "./AdbCommand.js";vi.mock("../process/ExecFileOutput.js", async (importOriginal) => ({...(await importOriginal<typeof import("../process/ExecFileOutput.js")>()),@@ -64,7 +67,6 @@ it.each([it.each([[{ killed: true }, "AnalysisTimeoutError"],[{ outputTruncated: true }, "AnalysisResourceConstraintError"],- [{ code: "ABORT_ERR" }, "AnalysisCancelledError"],] as const)("preserves a stopped or incomplete probe: %j",async (extra, tag) => {@@ -110,3 +112,63 @@ it.each(["stdout", "stderr"] as const)(});},);++it.each([null, "caller stopped", new Error("caller stopped")] as const)(+ "uses the command owner's exact %s cancellation reason",+ async (reason) => {+ const cancelled = Object.assign(new ExecFileOutputCancelledError(reason), {+ stdout: "partial",+ stderr: "diagnostic",+ stdoutBytes: 7,+ stderrBytes: 10,+ });+ vi.mocked(execFileBytes).mockRejectedValueOnce(cancelled);+ const result = await probe();+ expect(result).toMatchObject({+ ok: false,+ error: { _tag: "AnalysisCancelledError" },+ });+ if (result.ok) throw new Error("Expected owned cancellation");+ expect(result.error.cause).toBe(reason);+ expect(result.error.capturedOutput).toMatchObject({+ stdout: "partial",+ stderr: "diagnostic",+ stdout_bytes: 7,+ stderr_bytes: 10,+ });+ },+);++it("does not infer cancellation from a foreign ABORT_ERR code", async () => {+ const failure = Object.assign(new Error("foreign code"), {+ code: "ABORT_ERR",+ stdout: "",+ stderr: "",+ });+ vi.mocked(execFileBytes).mockRejectedValueOnce(failure);+ expect(await probe()).toMatchObject({+ ok: false,+ error: { _tag: "AnalysisProtocolError" },+ });+});++it("keeps a physical failure even when the later abort reason is that same error", async () => {+ const controller = new AbortController();+ const failure = new Error("physical command failure");+ vi.mocked(execFileBytes).mockImplementationOnce(async () => {+ controller.abort(failure);+ throw failure;+ });+ const result = await runAdbCapture(+ "inspect_adb_device",+ "device",+ "/host/adb",+ [],+ { timeoutMs: 100, maxOutputBytes: 1024 },+ controller.signal,+ );+ expect(result).toMatchObject({+ ok: false,+ error: { _tag: "AnalysisProtocolError", cause: failure },+ });+});diff --git a/src/adb/AdbCommand.ts b/src/adb/AdbCommand.tsindex 5ab13e4b5..6c4b255aa 100644--- a/src/adb/AdbCommand.ts+++ b/src/adb/AdbCommand.ts@@ -5,10 +5,14 @@ import {AnalysisTimeoutError,AnalysisUnsupportedTargetError,} from "../domain/analysisErrorCore.js";-import type { AnalysisError } from "../domain/analysisErrorBase.js";+import type {+ AnalysisCapturedOutput,+ AnalysisError,+} from "../domain/analysisErrorBase.js";import { err, ok, type Result } from "../domain/result.js";import {execFileBytes,+ ExecFileOutputCancelledError,execFileOutputFailure,type ExecFileOutputFailure,} from "../process/ExecFileOutput.js";@@ -16,6 +20,23 @@ import { packageChangeFailureReason } from "./AdbShellOutput.js";const STDERR_EXCERPT_BYTES = 512;+/** Project parsed command-owner facts without reinterpreting the rejection. */+export const adbCommandCapturedOutput = (+ failure: ExecFileOutputFailure,+): AnalysisCapturedOutput => ({+ stdout: failure.stdout,+ stderr: failure.stderr,+ ...(failure.stdoutBytes === undefined+ ? {}+ : { stdout_bytes: failure.stdoutBytes }),+ ...(failure.stderrBytes === undefined+ ? {}+ : { stderr_bytes: failure.stderrBytes }),+ exit_code: typeof failure.code === "number" ? failure.code : null,+ signal: failure.signal,+ truncated: failure.outputTruncated,+});+/** Parsed command failure owned by this adapter, separate from its public tag. */class AdbProtocolError extends AnalysisProtocolError {constructor(@@ -71,8 +92,15 @@ export const runAdbCapture = async ();} catch (cause) {const failure = execFileOutputFailure(cause);- if (signal?.aborted || failure?.code === "ABORT_ERR")- return err(new AnalysisCancelledError(operation, { cause }));+ if (cause instanceof ExecFileOutputCancelledError)+ return err(+ new AnalysisCancelledError(operation, {+ cause: cause.cause,+ ...(failure === undefined+ ? {}+ : { capturedOutput: adbCommandCapturedOutput(failure) }),+ }),+ );if (failure === undefined)return err(new AnalysisProtocolError(`adb failed for ${operation}`, { cause }),@@ -226,19 +254,7 @@ export const probeAdbPathExistence = async (new AnalysisProtocolError(error.message, {cause: error.cause,diagnostics: { destination_existence: "unknown" },- capturedOutput: {- stdout: failure.stdout,- stderr: failure.stderr,- ...(failure.stdoutBytes === undefined- ? {}- : { stdout_bytes: failure.stdoutBytes }),- ...(failure.stderrBytes === undefined- ? {}- : { stderr_bytes: failure.stderrBytes }),- exit_code: typeof failure.code === "number" ? failure.code : null,- signal: failure.signal,- truncated: failure.outputTruncated,- },+ capturedOutput: adbCommandCapturedOutput(failure),}),);};