Files
software-mansion__argent/packages/tool-server/test/blueprints/js-runtime-debugger-recoverable.test.ts
filip131311 4975ff490c fix(debugger): cut debugger tool fail rate — structured not-connected results, classified CDP failures, narrow recovery (#610)
## Why

PostHog (30d) shows the debugger tool family is argent's least reliable
surface:

| tool | fail rate | dominant causes |
|---|---|---|
| `debugger-status` | **37%** | METRO_NO_TARGETS /
SERVICE_INITIALIZATION_FAILED / zod |
| `debugger-connect` | **24%** | SERVICE_INITIALIZATION_FAILED (~7k, >1k
users), METRO_NO_TARGETS |
| `debugger-log-registry` | 41.7% raw / 4.2% excl. storms | one user
retry-looped it **12,742×** on NO_TARGETS |

Three design issues drive this: (1) "app/Metro not ready yet" — an
expected precondition — is reported as a tool *error*, which both
inflates fail rates and invites agent retry storms; (2) every CDP
init/lifecycle fault lands in telemetry as the catch-all
`REGISTRY_SERVICE_INITIALIZATION_FAILED` with `error_kind=unknown`; (3)
~600 users/30d hit zod validation failures with zero visibility into
which parameter was wrong.

## What

**1. Classified failures** (`d71efa43`) — six former plain-`Error` sites
in `cdp-client.ts` / `target-selection.ts` now carry dedicated
`DEBUGGER_CDP_*` / `DEBUGGER_TARGET_DEVICE_MISMATCH` signals (messages
byte-identical). The registry's two causeless "terminating window"
`ServiceInitializationError`s get their own
`REGISTRY_SERVICE_TERMINATING` code.

**2. Narrow recovery** (`ff9c1955`) — `recoverable()` on both debugger
blueprints, scoped to windows where retry is provably safe: Metro
recovers only `DEBUGGER_CDP_NOT_CONNECTED` (request never left the host;
a detected socket death already self-heals for the *next* call via the
terminated cascade — recovering `CONNECTION_CLOSED` there would be dead
code, see the blueprint comment). Chromium also recovers
`CONNECTION_CLOSED` for the tab-switch reconnect window.
`REQUEST_TIMEOUT` stays unrecoverable (double-execution risk). A
reachability test proves the retry actually fires for a RUNNING node.

**3. Structured not-connected results** (`48b91ffd`) — `debugger-status`
and `debugger-log-registry` become factories that return `{ status:
"not_connected", reason, detail, guidance }` for classified
preconditions (`metro_not_running | no_app_connected | device_mismatch |
cdp_unreachable | stale_connection | reconnecting`) and still throw on
unexpected faults. `detail` preserves the original error text for
string-matching agents. `debugger-status` gains a socket-state gate
(Metro: dispose stale node; Chromium: report transient reconnect, no
dispose — the client belongs to the ChromiumCdp dependency).
log-registry keeps **no** gate on purpose: captured logs are readable
over a dead socket, and disposal would destroy the post-crash log file.
Flows/run-sequence map a not_connected result to a **failed step**
(mirrors `isUnmetUiWaitResult`) so recorded connectivity gates don't
silently pass. `debugger-evaluate` re-codes agent-expression throws as
`DEBUGGER_EVALUATE_EXPRESSION_THREW`.

**4. Telemetry + guidance + docs** (`7602a70d`) —
`tool:fail.invalid_params` (schema-declared names only; unknown keys →
literal `"unrecognized_keys"`; capped emit-side at 16), new
`debugger:tool_outcome` event (coded enum; joins
`tool:invoke`/`complete` via `tool_invocation_id` —
`ai_client`/`duration` live on the joined row), anti-retry-loop guidance
appended to the Metro discovery errors (first sentences verbatim —
skills match on them), six skills updated, Telemetry.md bumped to v1.03
with the new coded diagnostics disclosed.

## Dashboard migration note

`debugger-status` / `debugger-log-registry` precondition failures stop
emitting `tool:fail` — track them via `debugger:tool_outcome` (`outcome
!= 'connected'`). Per-AI-client breakdowns of those outcomes need a join
on `tool_invocation_id` against `tool:complete`.

## Testing

- Full suites green: tool-server 3,142, telemetry 290, registry 87. New
coverage: recoverable truth tables (incl. non-Error inputs), status
not-connected matrix
(Metro/Chromium/terminating-window/stale/dispose-side-effect/fall-through-rethrow),
log-registry rows, zod `invalid_params` derivation + emission-level
guard, flow gate, evaluate rewrap, sanitize-level enum checks.
- Live-tested against a booted iOS simulator + RN app and an Electron
target (details in PR comments).

Plan hardened through 3 adversarial review rounds (46 confirmed findings
addressed) before implementation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-08-10 11:51:45 +02:00

97 lines
4.0 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { FAILURE_CODES, FailureError, type FailureSignal } from "@argent/registry";
import { jsRuntimeDebuggerBlueprint } from "../../src/blueprints/js-runtime-debugger";
import { chromiumJsRuntimeDebuggerBlueprint } from "../../src/blueprints/chromium-js-runtime-debugger";
/**
* Truth table for `recoverable()` on the Metro debugger blueprint — the
* predicate the registry's dispose-and-retry-once self-heal consults for a
* RUNNING node — plus a pin that the Chromium wrapper deliberately declares
* NO recoverable() at all.
*
* Metro (JsRuntimeDebugger): ONLY the send()-guard rejection (NOT_CONNECTED) is
* recoverable — the one window where the node can still be RUNNING and the
* request provably never left the host. CONNECTION_CLOSED is deliberately
* excluded (delivered-request double-execution risk; node has left RUNNING by
* then anyway), as is REQUEST_TIMEOUT (request may have taken effect; a hung
* runtime is not fixed by reconnecting).
*
* Chromium: recovery would dispose the wrapper node, whose dispose closes the
* LogFileWriter and thereby UNLINKS the session's captured console log — and
* it buys nothing, because a tab-switch reconnect re-points the same client
* object so the cached node heals for the next call without any dispose. See
* the blueprint comment.
*/
function coded(
error_code: FailureSignal["error_code"],
error_kind: FailureSignal["error_kind"] = "network"
) {
return new FailureError("x", {
error_code,
failure_stage: "test_stage",
failure_area: "tool_server",
error_kind,
});
}
const RECOVERABLE_METRO = [FAILURE_CODES.DEBUGGER_CDP_NOT_CONNECTED];
// Codes that must be non-recoverable on the Metro blueprint.
const NEVER_RECOVERABLE = [
FAILURE_CODES.DEBUGGER_METRO_NOT_RUNNING,
FAILURE_CODES.DEBUGGER_METRO_NO_TARGETS,
FAILURE_CODES.DEBUGGER_TARGET_DEVICE_MISMATCH,
FAILURE_CODES.DEBUGGER_CDP_REQUEST_TIMEOUT,
FAILURE_CODES.DEBUGGER_CDP_CONNECT_FAILED,
FAILURE_CODES.DEBUGGER_CDP_SOCKET_CLOSED_BEFORE_OPEN,
FAILURE_CODES.DEBUGGER_CDP_RUNTIME_EXCEPTION,
FAILURE_CODES.DEBUGGER_CDP_PROTOCOL_ERROR,
FAILURE_CODES.JS_RUNTIME_CDP_DISCONNECTED,
];
describe("jsRuntimeDebuggerBlueprint.recoverable (Metro)", () => {
it.each(RECOVERABLE_METRO)("returns true for %s", (code) => {
expect(jsRuntimeDebuggerBlueprint.recoverable!(coded(code))).toBe(true);
});
it("returns false for CONNECTION_CLOSED — delivered request, double-execution risk", () => {
expect(
jsRuntimeDebuggerBlueprint.recoverable!(coded(FAILURE_CODES.DEBUGGER_CDP_CONNECTION_CLOSED))
).toBe(false);
});
it.each(NEVER_RECOVERABLE)("returns false for %s", (code) => {
expect(jsRuntimeDebuggerBlueprint.recoverable!(coded(code))).toBe(false);
});
});
describe("chromiumJsRuntimeDebuggerBlueprint has NO recovery", () => {
it("declares no recoverable() — the registry must never dispose-and-retry this node", () => {
// Load-bearing absence: Registry._recoverFailedServices treats a missing
// recoverable() as never-recover, so the wrapper node is never disposed on
// a failing call — which is what keeps the captured console log file on
// disk (dispose → LogFileWriter.close() → fs.unlinkSync). Reintroducing a
// recoverable() here re-opens the log-deletion hole this pin guards.
expect(chromiumJsRuntimeDebuggerBlueprint.recoverable).toBeUndefined();
});
});
describe("recoverable() with non-FailureError inputs (Metro)", () => {
it("plain Error is not recoverable", () => {
expect(jsRuntimeDebuggerBlueprint.recoverable!(new Error("socket hang up"))).toBe(false);
});
it("a bare string does not throw and is not recoverable", () => {
expect(jsRuntimeDebuggerBlueprint.recoverable!("socket hang up")).toBe(false);
});
it("undefined does not throw and is not recoverable", () => {
expect(jsRuntimeDebuggerBlueprint.recoverable!(undefined)).toBe(false);
});
it("null does not throw and is not recoverable", () => {
expect(jsRuntimeDebuggerBlueprint.recoverable!(null)).toBe(false);
});
});