Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions packages/acp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import { each, race, stream, until } from "effection";
import type { Channel, Operation } from "effection";
import type { AgentPromptCheckpoint, AgentPromptEvent, Session } from "@executablemd/agent/api";
import type { AcpRuntimeTurn, AcpRuntimeTurnResult } from "./acpx-runtime.ts";
import type {
AcpRuntimeTurn,
AcpRuntimeTurnResult,
AcpRuntimeTurnResultError,
} from "./acpx-runtime.ts";
import { checkpointFromResult } from "./checkpoint.ts";
import { XmdResponse, XMD_RESPONSE, xmdPromptEvent } from "./xmd-response.ts";
import type { Registration } from "./permission-bridge.ts";
Expand Down Expand Up @@ -182,29 +186,44 @@ function mapResult(result: AcpRuntimeTurnResult, requestId: string): AgentPrompt
}
return terminal;
}
const failure = new Error(result.error.message);
return {
type: "terminal",
status: "failed",
error: withPromptDiagnostic(new Error(result.error.message), result.error, {
stage: "runtime-turn",
requestId,
}),
};
}

export type PromptDiagnosticSource =
| { stage: "runtime-turn"; requestId: string }
| { stage: "held-session-ensure" | "prompt-ensure" };

export function withPromptDiagnostic(
failure: Error,
facts: AcpRuntimeTurnResultError,
source: PromptDiagnosticSource,
): Error {
Object.defineProperty(failure, "executablemd.agent.prompt-diagnostic", {
value: {
provider: "acp",
operation: "session/prompt",
stage: "runtime-turn",
requestId,
...(result.error.code === undefined ? {} : { code: result.error.code }),
...(result.error.detailCode === undefined ? {} : { detailCode: result.error.detailCode }),
...(result.error.retryable === undefined ? {} : { retryable: result.error.retryable }),
...(result.error.origin === undefined ? {} : { origin: result.error.origin }),
...(result.error.acp === undefined
...source,
...(facts.code === undefined ? {} : { code: facts.code }),
...(facts.detailCode === undefined ? {} : { detailCode: facts.detailCode }),
...(facts.retryable === undefined ? {} : { retryable: facts.retryable }),
...(facts.origin === undefined ? {} : { origin: facts.origin }),
...(facts.acp === undefined
? {}
: {
acp: {
code: result.error.acp.code,
message: result.error.acp.message,
...(result.error.acp.details === undefined
? {}
: { details: result.error.acp.details }),
code: facts.acp.code,
message: facts.acp.message,
...(facts.acp.details === undefined ? {} : { details: facts.acp.details }),
},
}),
},
});
return { type: "terminal", status: "failed", error: failure };
return failure;
}
74 changes: 69 additions & 5 deletions packages/acp/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import type {
AcpRuntimeOptions,
AcpRuntimeTurn,
AcpRuntimeTurnResult,
AcpRuntimeTurnResultError,
AcpRuntimeUsageBreakdown,
AcpRuntimeUsageCost,
AcpSessionRecord,
Expand All @@ -125,7 +126,8 @@ import {
createPermissionBridge,
strictPermissions,
} from "./permission-bridge.ts";
import { consumeTurn } from "./events.ts";
import { consumeTurn, withPromptDiagnostic } from "./events.ts";
import type { PromptDiagnosticSource } from "./events.ts";
import { capturePromptUsage, turnUsage } from "./usage.ts";
import type { PromptUsageHost, TurnUsage } from "./usage.ts";
import { checkpointFromResult } from "./checkpoint.ts";
Expand Down Expand Up @@ -1932,6 +1934,7 @@ function* useAcpxProviderState(
* this ensure itself may perform. Never kept on the entry it returns.
*/
requestedModel?: string;
resumeFailureStage?: "held-session-ensure" | "prompt-ensure";
}

function* ensureFromPrepared(
Expand Down Expand Up @@ -2033,7 +2036,13 @@ function* useAcpxProviderState(
// reported as an ordinary reattachment's is: the adapter's account,
// with nothing created in its place.
if (attachment === undefined) {
throw sessionResumeFailure(error, prepared.details.sessionKey);
throw sessionResumeFailure(
error,
prepared.details.sessionKey,
intent.resumeFailureStage === undefined
? undefined
: { stage: intent.resumeFailureStage },
);
}
// An exact resume that the provider could not perform: it has no such
// history, or it cannot resume by name at all. Either way the
Expand Down Expand Up @@ -3183,6 +3192,7 @@ function* useAcpxProviderState(
...(resumeSessionId === undefined ? {} : { attachment: { resumeSessionId } }),
state,
...modelRequest(desired),
resumeFailureStage: "prompt-ensure",
materialization:
constructing && adapterFor(agentName)?.acceptance === "first-turn-acceptance",
deferEstablished:
Expand Down Expand Up @@ -3316,7 +3326,10 @@ function* useAcpxProviderState(
try {
yield* until(turn.promptStarted);
} catch (error) {
throw sessionResumeFailure(error, entry.session.sessionKey);
throw sessionResumeFailure(error, entry.session.sessionKey, {
stage: "runtime-turn",
requestId: turn.requestId,
});
}
}

Expand Down Expand Up @@ -4941,6 +4954,9 @@ function* useAcpxProviderState(
: { build: construction.build, verifyReopening: construction.verifyReopening }),
...(resumeSessionId === undefined ? {} : { attachment: { resumeSessionId } }),
state,
...(selectedAgent === undefined
? {}
: { resumeFailureStage: "held-session-ensure" }),
...(setup?.requestedModel === undefined
? {}
: { requestedModel: setup.requestedModel }),
Expand Down Expand Up @@ -5058,7 +5074,11 @@ function nativeIdentityRefusal(error: unknown): AttachmentRefused | undefined {
}
}

function sessionResumeFailure(error: unknown, sessionKey: string): unknown {
function sessionResumeFailure(
error: unknown,
sessionKey: string,
source?: PromptDiagnosticSource,
): unknown {
const refusal = nativeIdentityRefusal(error);
if (refusal) {
return refusal;
Expand All @@ -5068,11 +5088,55 @@ function sessionResumeFailure(error: unknown, sessionKey: string): unknown {
error !== null &&
Reflect.get(error, "detailCode") === "SESSION_RESUME_REQUIRED"
) {
return new SessionResumeRequiredError(sessionKey, { cause: error });
const failure = new SessionResumeRequiredError(sessionKey);
if (source === undefined) {
return failure;
}
const code = Reflect.get(error, "outputCode");
const origin = Reflect.get(error, "origin");
const retryable = Reflect.get(error, "retryable");
return withPromptDiagnostic(
failure,
{
message: failure.message,
code: typeof code === "string" && code.trim().length > 0 ? code : "RUNTIME",
detailCode: "SESSION_RESUME_REQUIRED",
origin: origin === "cli" || origin === "queue" || origin === "acp" ? origin : "runtime",
...(typeof retryable === "boolean" ? { retryable } : {}),
acp: resumeProtocolFacts(error),
},
source,
);
}
return error;
}

function resumeProtocolFacts(error: unknown, depth = 0): AcpRuntimeTurnResultError["acp"] {
if (depth > 5 || typeof error !== "object" || error === null || Array.isArray(error)) {
return undefined;
}
const code = Reflect.get(error, "code");
const message = Reflect.get(error, "message");
if (
typeof code === "number" &&
Number.isFinite(code) &&
typeof message === "string" &&
message.length > 0
) {
const data = Reflect.get(error, "data");
const details =
typeof data === "object" && data !== null ? Reflect.get(data, "details") : undefined;
return { code, message, ...(typeof details === "string" ? { details } : {}) };
}
for (const key of ["acp", "error", "cause"]) {
const facts = resumeProtocolFacts(Reflect.get(error, key), depth + 1);
if (facts !== undefined) {
return facts;
}
}
return undefined;
}

export class SessionResumeRequiredError extends Error {
constructor(sessionKey: string, options?: ErrorOptions) {
super(
Expand Down
4 changes: 4 additions & 0 deletions packages/acp/tests/certification-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function answer(request) {
reply(id,{sessionId,configOptions:options(),...(mode === "standard" || mode === "standard-wait" ? {} : {_meta:{agentSessionId:native()}})});
} else if (method === "session/load") {
sessionId=params.sessionId;
if (mode === "distinct-refused") {
send({jsonrpc:"2.0",id,error:{code:-32041,message:"Internal error",data:{details:"Release the active writer before reconnecting.",unrelated:"private unrelated marker"}}});
return;
}
fs.readFile(file(),"utf8",(error,text) => {
if (error) { send({jsonrpc:"2.0",id,error:{code:-32603,message:"missing controlled history"}});return; }
marker=JSON.parse(text).marker;
Expand Down
98 changes: 94 additions & 4 deletions packages/acp/tests/host-conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { Ok, scoped, spawn, suspend, until, withResolvers } from "effection";
import type { Operation, Result } from "effection";
import { Component } from "@executablemd/core";
import { Agent } from "@executablemd/agent/api";
import { installAgentComponents } from "@executablemd/agent/host";
import { installAgentComponents, useAgentPromptPublisher } from "@executablemd/agent/host";
import type { FunctionComponentDefinition, Json } from "@executablemd/core";
import type { Session } from "@executablemd/agent/api";
import { useAgentConversation } from "@executablemd/agent/host";
Expand Down Expand Up @@ -1738,14 +1738,15 @@ describe(
);

describe(
"a held Codex conversation across a host CLI upgrade",
"resume retention: a held Codex conversation across a host CLI upgrade",
{ sanitizeOps: false, sanitizeResources: false },
() => {
for (const [mode, failure, errorName] of [
["distinct", undefined, undefined],
["normal", "reopened a different native conversation", "AttachmentRefused"],
["standard", "did not confirm the retained native conversation", "AttachmentRefused"],
["missing", "could not be resumed", "SessionResumeRequiredError"],
["distinct-refused", "could not be resumed", "SessionResumeRequiredError"],
] as const) {
it(`requires the new build's live identity (${mode})`, function* () {
yield* withCertificationFixture(function* (f) {
Expand Down Expand Up @@ -1779,9 +1780,29 @@ describe(
target.observer.observation.path = "/opt/builds/codex-0.167.1";
target.observer.observation.versionOutput = "codex-cli 0.167.1\n";
target.observer.observation.digest = "b".repeat(64);
const begun: string[] = [];
const published: unknown[] = [];
const provider = target.installation(mode === "missing" ? command : f.command(mode));
const resumed = yield* withConversation(
stream,
target.installation(mode === "missing" ? command : f.command(mode)),
{
...provider,
*install() {
if (provider.install !== undefined) {
yield* provider.install();
}
yield* useAgentPromptPublisher({
*begin(text) {
begun.push(text);
return {};
},
*publish(publication) {
published.push(publication.begun);
yield* publication.append();
},
});
},
},
(chat) => chat.prompt("recall", { model: "selected", effort: "high" }),
"codex",
);
Expand Down Expand Up @@ -1822,7 +1843,76 @@ describe(
expect(methods).not.toContain("session/set_config_option");
expect(methods).not.toContain("session/prompt");
expect(yield* until(target.store.load(key))).toEqual(record);
expect(stream.snapshot().map(serializeDurableEvent)).toEqual(lines);
if (errorName === "SessionResumeRequiredError") {
const after = stream.snapshot();
expect(after.slice(0, lines.length).map(serializeDurableEvent)).toEqual(lines);
expect(after).toHaveLength(lines.length + 1);
const saved = turns(after).at(-1);
const expectedError = {
message: resumed.value.error.message,
name: errorName,
diagnostic: {
provider: "acp",
operation: "session/prompt",
stage: "held-session-ensure",
code: "RUNTIME",
detailCode: "SESSION_RESUME_REQUIRED",
origin: "acp",
retryable: true,
acp:
mode === "missing"
? { code: -32603, message: "missing controlled history" }
: {
code: -32041,
message: "Internal error",
details: "Release the active writer before reconnecting.",
},
},
};
expect(saved?.name).toBe("turn:2");
expect(saved?.record).toEqual({
sequence: expect.any(Number),
agent: "codex",
sessionKey: "",
status: "failed",
text: "",
error: expectedError,
});
const event = after.at(-1);
expect(event?.type === "yield" && event.description.input).toBe("recall");
expect(begun).toEqual([]);
expect(published).toEqual([undefined]);
expect(resumed.value.error.cause).toBe(undefined);
expect(
Reflect.get(resumed.value.error, "executablemd.agent.prompt-diagnostic"),
).toEqual(expectedError.diagnostic);
if (mode === "distinct-refused") {
const attemptedCalls = (yield* f.calls()).length;
const later = yield* withConversation(
stream,
target.installation(f.command("distinct-next")),
(chat) => chat.prompt("recover"),
"codex",
);
expect(answer(later)).toBe("remembered");
const sent = (yield* f.calls()).slice(attemptedCalls);
expect(sent.filter((call) => call.method === "session/prompt")).toMatchObject([
{ sessionId: record.acpSessionId },
]);
expect(sent.map((call) => call.method)).not.toContain("session/new");
expect(turns(stream.snapshot()).map((turn) => turn.name)).toEqual([
"turn:1",
"turn:2",
"turn:3",
]);
expect((yield* until(target.store.load(key)))?.agentSessionId).toBe(
record.agentSessionId,
);
expect(yield* target.ownership.routes.read(tuple)).toEqual(route);
}
} else {
expect(stream.snapshot().map(serializeDurableEvent)).toEqual(lines);
}
}
});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/acp/tests/released-peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ function respond(request) {
if (controlDirectory && request.method === "session/load") {
fs.access(path.join(controlDirectory, "fail-load"), error => {
if (error) loaded();
else send({jsonrpc: "2.0", id: request.id, error: {code: -32603, message: "Controlled cold load failure"}});
else fs.readFile(path.join(controlDirectory, "fail-load"), "utf8", (error, text) => {
if (error) throw error;
const failure = text === "enabled" ? {code: -32603, message: "Controlled cold load failure"} : JSON.parse(text);
send({jsonrpc: "2.0", id: request.id, error: failure});
});
});
} else loaded();
} else if (request.method === "session/set_config_option") {
Expand Down
Loading
Loading