fix(agents): classify ACP no-output stalls

This commit is contained in:
Peter Steinberger
2026-05-17 07:19:58 +01:00
parent a46d2e2b06
commit aaadf721e3
10 changed files with 334 additions and 8 deletions

View File

@@ -5,7 +5,12 @@ import {
onAgentEvent,
resetAgentEventsForTest,
} from "../../infra/agent-events.js";
import { emitAcpLifecycleError, formatAcpLifecycleError } from "./attempt-execution.js";
import {
emitAcpLifecycleError,
emitAcpPromptSubmitted,
emitAcpRuntimeEvent,
formatAcpLifecycleError,
} from "./attempt-execution.js";
let captured: AgentEventPayload[] = [];
let unsubscribe: (() => void) | undefined;
@@ -18,6 +23,57 @@ beforeEach(() => {
});
});
describe("ACP diagnostic events", () => {
it("emits prompt-submitted state with proxy env names but not values", () => {
const previous = process.env.HTTPS_PROXY;
process.env.HTTPS_PROXY = "http://proxy.example.invalid:8080";
try {
emitAcpPromptSubmitted({
runId: "run-prompt",
sessionKey: "agent:codex:acp:child",
at: 123,
});
} finally {
if (previous === undefined) {
delete process.env.HTTPS_PROXY;
} else {
process.env.HTTPS_PROXY = previous;
}
}
const event = captured[0];
expect(event?.stream).toBe("acp");
expect(event?.sessionKey).toBe("agent:codex:acp:child");
expect(event?.data).toMatchObject({
phase: "prompt_submitted",
at: 123,
proxyEnvKeys: expect.arrayContaining(["HTTPS_PROXY"]),
});
expect(JSON.stringify(event?.data)).not.toContain("proxy.example.invalid");
});
it("emits sanitized non-text runtime events for parent relay diagnostics", () => {
emitAcpRuntimeEvent({
runId: "run-status",
event: {
type: "status",
text: "connecting token=sk-abcdefghijklmnopqrstuvwxyz123456",
tag: "session_info_update",
},
});
const event = captured[0];
expect(event?.stream).toBe("acp");
expect(event?.data).toMatchObject({
phase: "runtime_event",
eventType: "status",
tag: "session_info_update",
});
expect(String(event?.data.text)).toContain("connecting");
expect(String(event?.data.text)).not.toContain("sk-abcdefghijklmnopqrstuvwxyz123456");
});
});
afterEach(() => {
unsubscribe?.();
unsubscribe = undefined;

View File

@@ -5,6 +5,8 @@ export {
emitAcpLifecycleEnd,
emitAcpLifecycleError,
emitAcpLifecycleStart,
emitAcpPromptSubmitted,
emitAcpRuntimeEvent,
persistAcpTurnTranscript,
persistCliTurnTranscript,
runAgentAttempt,

View File

@@ -1,5 +1,6 @@
import type { AgentMessage } from "@earendil-works/pi-agent-core";
import { formatAcpErrorChain } from "../../acp/runtime/errors.js";
import type { AcpRuntimeEvent } from "../../acp/runtime/types.js";
import { normalizeReplyPayload } from "../../auto-reply/reply/normalize-reply.js";
import type { ThinkLevel, VerboseLevel } from "../../auto-reply/thinking.js";
import { appendSessionTranscriptMessage } from "../../config/sessions/transcript-append.js";
@@ -11,6 +12,7 @@ import type { SessionEntry } from "../../config/sessions/types.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { emitAgentEvent } from "../../infra/agent-events.js";
import { readErrorName } from "../../infra/errors.js";
import { redactSensitiveText } from "../../logging/redact.js";
import { createSubsystemLogger } from "../../logging/subsystem.js";
import { annotateInterSessionPromptText } from "../../sessions/input-provenance.js";
import { emitSessionTranscriptUpdate } from "../../sessions/transcript-events.js";
@@ -722,6 +724,93 @@ export function emitAcpLifecycleStart(params: { runId: string; startedAt: number
});
}
const ACP_PROXY_ENV_KEYS = [
"HTTP_PROXY",
"HTTPS_PROXY",
"ALL_PROXY",
"http_proxy",
"https_proxy",
"all_proxy",
] as const;
function resolvePresentProxyEnvKeys(env: NodeJS.ProcessEnv = process.env): string[] {
return ACP_PROXY_ENV_KEYS.filter((key) => {
const value = env[key];
return typeof value === "string" && value.trim().length > 0;
});
}
function sanitizeAcpDiagnosticText(value: string): string {
return redactSensitiveText(value).replace(/\s+/g, " ").trim().slice(0, 240);
}
function acpRuntimeEventDiagnostics(event: AcpRuntimeEvent): Record<string, unknown> {
if (event.type === "status") {
return {
eventType: event.type,
text: sanitizeAcpDiagnosticText(event.text),
...(event.tag ? { tag: event.tag } : {}),
};
}
if (event.type === "tool_call") {
return {
eventType: event.type,
text: sanitizeAcpDiagnosticText(event.text),
...(event.tag ? { tag: event.tag } : {}),
...(event.status ? { status: sanitizeAcpDiagnosticText(event.status) } : {}),
...(event.title ? { title: sanitizeAcpDiagnosticText(event.title) } : {}),
...(event.toolCallId ? { toolCallId: sanitizeAcpDiagnosticText(event.toolCallId) } : {}),
};
}
if (event.type === "error") {
return {
eventType: event.type,
message: sanitizeAcpDiagnosticText(event.message),
...(event.code ? { code: sanitizeAcpDiagnosticText(event.code) } : {}),
...(typeof event.retryable === "boolean" ? { retryable: event.retryable } : {}),
};
}
if (event.type === "done") {
return {
eventType: event.type,
...(event.stopReason ? { stopReason: sanitizeAcpDiagnosticText(event.stopReason) } : {}),
};
}
return {
eventType: event.type,
stream: event.stream ?? "output",
};
}
export function emitAcpPromptSubmitted(params: { runId: string; sessionKey?: string; at: number }) {
emitAgentEvent({
runId: params.runId,
stream: "acp",
...(params.sessionKey ? { sessionKey: params.sessionKey } : {}),
data: {
phase: "prompt_submitted",
at: params.at,
proxyEnvKeys: resolvePresentProxyEnvKeys(),
},
});
}
export function emitAcpRuntimeEvent(params: {
runId: string;
event: AcpRuntimeEvent;
sessionKey?: string;
}) {
emitAgentEvent({
runId: params.runId,
stream: "acp",
...(params.sessionKey ? { sessionKey: params.sessionKey } : {}),
data: {
phase: "runtime_event",
...acpRuntimeEventDiagnostics(params.event),
},
});
}
export function emitAcpLifecycleEnd(params: { runId: string }) {
emitAgentEvent({
runId: params.runId,