Skip to content

Commit c3eee8a

Browse files
committed
Offer explicit Retry after safe held resume failures
1 parent a07b399 commit c3eee8a

9 files changed

Lines changed: 185 additions & 61 deletions

File tree

‎packages/acp/src/provider.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5403,7 +5403,10 @@ function sessionResumeFailure(
54035403
error !== null &&
54045404
Reflect.get(error, "detailCode") === "SESSION_RESUME_REQUIRED"
54055405
) {
5406-
const failure = new SessionResumeRequiredError(sessionKey);
5406+
const failure = new SessionResumeRequiredError(
5407+
sessionKey,
5408+
source?.stage === "held-session-ensure" ? "held" : "ordinary",
5409+
);
54075410
if (source === undefined) {
54085411
return failure;
54095412
}
@@ -5453,10 +5456,11 @@ function resumeProtocolFacts(error: unknown, depth = 0): AcpRuntimeTurnResultErr
54535456
}
54545457

54555458
export class SessionResumeRequiredError extends Error {
5456-
constructor(sessionKey: string, options?: ErrorOptions) {
5459+
constructor(sessionKey: string, caller: "held" | "ordinary" = "ordinary") {
54575460
super(
5458-
`Session "${sessionKey}" could not be resumed. Name a different <Session> to start a new conversation.`,
5459-
options,
5461+
caller === "held"
5462+
? `Conversation "${sessionKey}" could not be resumed. Retry the saved request to try the same conversation again.`
5463+
: `Session "${sessionKey}" could not be resumed. Retry this operation with the same <Session> if its caller permits; a different <Session> starts a new conversation.`,
54605464
);
54615465
this.name = "SessionResumeRequiredError";
54625466
}

‎packages/acp/tests/released-runtime.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,8 @@ describe(
13171317
const saved = prompt.error;
13181318
expect(prompt.status).toBe("failed");
13191319
expect(prompt.text).toBe("");
1320-
expect(saved.message).toContain("Name a different <Session>");
1320+
expect(saved.message).toContain("could not be resumed");
1321+
expect(saved.message).not.toContain("Name a different <Session>");
13211322
const sensitive = detail?.startsWith("ghp_") === true;
13221323
expect(saved).toEqual({
13231324
message: saved.message,
@@ -1559,7 +1560,7 @@ describe(
15591560
yield* ensureDir(docs);
15601561

15611562
const facts = [
1562-
`Session "${sessionKey}" could not be resumed. Name a different <Session> to start a new conversation.`,
1563+
`Session "${sessionKey}" could not be resumed. Retry this operation with the same <Session> if its caller permits; a different <Session> starts a new conversation.`,
15631564
"Resume diagnostic (provider=acp, operation=session/ensure, stage=session-ensure)",
15641565
"Normalization origin: acp",
15651566
'Runtime code: "RUNTIME" (normalizer fallback)',

‎packages/cli/src/repl/sidekick/model.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ export interface SidekickRequest {
584584
* whether this request was the one that ran it.
585585
*/
586586
readonly turn: number | undefined;
587-
readonly resumeRefused?: true;
587+
readonly payloadUnreconstructable?: true;
588588
}
589589

590590
/**
@@ -632,10 +632,10 @@ export function retryable(
632632
const request = known.requests.find(
633633
(one) => one.conversation === conversation && one.kind === "message" && one.message === message,
634634
);
635-
if (request?.resumeRefused === true) {
635+
if (request?.payloadUnreconstructable === true) {
636636
return Err(
637637
new SidekickProjectionError(
638-
"This conversation could not be resumed and is not offered for Retry.",
638+
"This request carried feedback whose exact payload cannot be reconstructed for Retry.",
639639
),
640640
);
641641
}
@@ -723,7 +723,8 @@ export function requestsFromHistory(
723723
message: number | undefined;
724724
turn: number | undefined;
725725
outcomes: number;
726-
resumeRefused?: true;
726+
carriedFeedback?: true;
727+
payloadUnreconstructable?: true;
727728
}[] = [];
728729
/** The request whose records are still being read, per conversation. */
729730
const current = new Map<string, (typeof requests)[number]>();
@@ -733,6 +734,7 @@ export function requestsFromHistory(
733734
// A message carrying recovered feedback retains the final composed input
734735
// after its original source, before either has reached a canonical turn.
735736
if (active?.kind === "message" && active.outcomes === 0) {
737+
active.carriedFeedback = true;
736738
continue;
737739
}
738740
}
@@ -773,16 +775,15 @@ export function requestsFromHistory(
773775
continue;
774776
}
775777
const outcome = parsed.value.result;
776-
if (
777-
outcome.status === "ok" &&
778-
parsePromptRecord(outcome.value)?.error?.diagnostic?.stage === "held-session-ensure"
779-
) {
780-
// Automatic feedback writes its own request in begin, which this attempt
781-
// never reached. Only an explicit, still-open message can own it.
778+
const saved = outcome.status === "ok" ? parsePromptRecord(outcome.value) : undefined;
779+
if (saved?.error?.diagnostic?.stage === "held-session-ensure") {
780+
// Automatic feedback has not begun a request at this boundary.
782781
if (holding.kind !== "message" || holding.outcomes !== 0) {
783782
continue;
784783
}
785-
holding.resumeRefused = true;
784+
if (holding.carriedFeedback === true) {
785+
holding.payloadUnreconstructable = true;
786+
}
786787
}
787788
holding.outcomes += 1;
788789
if (holding.outcomes > 1) {
@@ -805,7 +806,9 @@ export function requestsFromHistory(
805806
at: held.at,
806807
message: held.message,
807808
turn: held.turn,
808-
...(held.resumeRefused === true ? { resumeRefused: true as const } : {}),
809+
...(held.payloadUnreconstructable === true
810+
? { payloadUnreconstructable: true as const }
811+
: {}),
809812
}),
810813
),
811814
),

‎packages/cli/src/repl/sidekick/owner.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,8 +2898,7 @@ export function useSidekickOwner(input: SidekickOwnerInput): Operation<SidekickO
28982898
if (
28992899
sending.kind === "message" &&
29002900
sending.options.recovery?.decision.action === "continue-original" &&
2901-
!live.begun &&
2902-
(attempted === undefined || attempted.resumeRefused)
2901+
!live.begun
29032902
) {
29042903
return Err(new SidekickContinuationUnavailable(failure, { cause: replied.error }));
29052904
}
@@ -3270,7 +3269,6 @@ function* turnForRequest(
32703269
readonly status: string;
32713270
readonly text: string;
32723271
readonly withheld: boolean;
3273-
readonly resumeRefused: boolean;
32743272
readonly error: PromptRecord["error"];
32753273
}
32763274
| undefined
@@ -3312,7 +3310,6 @@ function* turnForRequest(
33123310
status: saved.status,
33133311
text: saved.text,
33143312
withheld: saved.withheld === true,
3315-
resumeRefused: held.resumeRefused === true,
33163313
error: saved.error,
33173314
};
33183315
}

‎packages/cli/tests/repl-sidekick-recovery.test.ts‎

Lines changed: 117 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,16 @@ describe("resume retention: exact request ownership", () => {
283283
]);
284284
expect(reading.turns.at(-1)).toMatchObject({ asked: "person", title: "new request" });
285285
expect(reading.delivered).toEqual([]);
286+
expect(reading.requests.at(-1)?.payloadUnreconstructable).toBe(true);
286287
expect(retryable(reading, CHAT, 2).ok).toBe(false);
288+
const postBegin = project([
289+
...prefix,
290+
next,
291+
feedback,
292+
turn(2, "", { status: "failed", error: { message: "post-begin failure" } }),
293+
]);
294+
expect(postBegin.requests.at(-1)?.payloadUnreconstructable).toBe(undefined);
295+
expect(retryable(postBegin, CHAT, 2)).toEqual(Ok(next.text));
287296
const ordinary = project([
288297
...prefix,
289298
next,
@@ -5183,53 +5192,129 @@ describe("resume retention and mounted retained recovery corrections", () => {
51835192
expect(held.host.fake.prompts).toHaveLength(2);
51845193
});
51855194

5186-
it("keeps Start new chat and no Retry after a classified eager original refusal is saved", function* () {
5195+
it("retries the correct request after two unknown pre-send resume failures", function* () {
51875196
const held = yield* retained();
5188-
held.host.fake.refuseSessionResume(held.session, "Release the active writer.");
5189-
const before = yield* readSidekickRecords(held.chat.location.sidekick);
5197+
yield* decision(held, false);
51905198
yield* scoped(function* () {
51915199
const page = yield* useReplPage({
51925200
host: { ...held.host, profile: held.profile },
51935201
location: held.location,
51945202
});
5195-
yield* sendMessage(page, savedText);
5196-
yield* page.until(
5197-
"the saved fallback and new chat action",
5198-
(one) => one.shows("could not be resumed") && one.shows("[Start new chat]"),
5199-
);
5200-
expect(page.screen().shows("Release the active writer.")).toBe(true);
5201-
expect(page.screen().shows("[Retry]")).toBe(false);
5203+
yield* page.press("[Use saved instruction]");
5204+
held.host.fake.script({ reply: "<Output>Recovery settled.</Output>" });
5205+
yield* intoComposer(page);
5206+
yield* page.key("Enter");
5207+
yield* page.until("the settled recovery", (one) => one.shows("Recovery settled."));
52025208
page.repl.terminal.end();
52035209
yield* awaited("the REPL joining", () => page.repl.outcome() !== undefined);
52045210
});
5205-
const after = yield* readSidekickRecords(held.chat.location.sidekick);
5206-
const reading = project(after);
5207-
expect(held.host.fake.prompts).toHaveLength(2);
5208-
expect(reading.active).toBe(held.conversation);
5209-
expect(reading.turns.at(-1)).toMatchObject({
5210-
status: "failed",
5211-
sessionKey: undefined,
5212-
asked: "person",
5213-
title: savedText,
5211+
const text = "Retry this exact request.";
5212+
const secondText = "Deliver the second request only.";
5213+
const prompts = held.host.fake.prompts.length;
5214+
const ensured = held.host.fake.ensured.length;
5215+
held.host.fake.refuseSessionResume(held.session, "unknown pre-send fault", true);
5216+
const page = yield* useReplPage({
5217+
host: { ...held.host, profile: held.profile },
5218+
location: held.location,
52145219
});
5215-
expect(after.slice(before.length).filter((record) => record.kind === "feedback")).toEqual([]);
5216-
expect(JSON.stringify(after)).not.toContain("unrelated synthetic RPC payload");
5217-
const attempt = reading.requests.at(-1);
5218-
expect(attempt?.resumeRefused).toBe(true);
5219-
if (attempt?.message === undefined) {
5220-
throw new Error("The eager attempt has no durable message");
5220+
yield* sendMessage(page, text);
5221+
yield* page.until("the retryable resume failure", (one) => one.shows("unknown pre-send fault"));
5222+
expect(page.screen().shows("Name a different <Session>")).toBe(false);
5223+
expect(held.host.fake.prompts).toHaveLength(prompts);
5224+
expect(held.host.fake.ensured.length).toBeGreaterThan(ensured);
5225+
expect(
5226+
held.host.fake.ensured.slice(ensured).every((one) => one.resumeSessionId !== undefined),
5227+
).toBe(true);
5228+
const failed = project(yield* readSidekickRecords(held.chat.location.sidekick));
5229+
const request = failed.requests.at(-1);
5230+
expect(request?.payloadUnreconstructable).toBe(undefined);
5231+
if (request?.message === undefined) {
5232+
throw new Error("The failed request was not retained");
52215233
}
5222-
expect(retryable(reading, held.conversation, attempt.message).ok).toBe(false);
5223-
const reopened = yield* useReplPage({
5234+
expect(retryable(failed, held.conversation, request.message)).toEqual(Ok(text));
5235+
expect(page.screen().shows("[retry]")).toBe(true);
5236+
held.host.fake.refuseSessionResume(held.session, "unknown pre-send fault", true);
5237+
yield* page.press("[X]");
5238+
yield* page.until("the dismissed failure", (one) => one.shows("dismissed"));
5239+
yield* page.press("[Resume queue]");
5240+
yield* page.until("the released queue", (one) => !one.shows("[Resume queue]"));
5241+
yield* sendMessage(page, secondText);
5242+
yield* awaited("the second failed ensure", () => held.host.fake.ensured.length > ensured + 1);
5243+
expect(held.host.fake.prompts).toHaveLength(prompts);
5244+
const twice = project(yield* readSidekickRecords(held.chat.location.sidekick));
5245+
const second = twice.requests.at(-1);
5246+
if (second?.message === undefined) {
5247+
throw new Error("Second request was not retained");
5248+
}
5249+
expect(retryable(twice, held.conversation, second.message)).toEqual(Ok(secondText));
5250+
held.host.fake.script({ reply: "<Output>Retried once.</Output>" });
5251+
yield* page.until(
5252+
"the second failure notice",
5253+
(one) => one.shows(secondText) && one.shows("Click X or press Esc"),
5254+
);
5255+
yield* page.key("Escape");
5256+
yield* page.until(
5257+
"the closed second failure notice",
5258+
(one) => !one.shows("Click X or press Esc"),
5259+
);
5260+
yield* page.press("[Resume queue]");
5261+
yield* page.until("the released second queue", (one) => !one.shows("[Resume queue]"));
5262+
yield* page.press("[retry]", { after: secondText });
5263+
yield* page.until("the retry's reply", (one) => one.shows("Retried once."));
5264+
expect(held.host.fake.prompts).toHaveLength(prompts + 1);
5265+
expect(held.host.fake.prompts.at(-1)).toContain(secondText);
5266+
expect(held.host.fake.prompts.at(-1)).not.toContain(text);
5267+
expect(held.host.fake.turns.at(-1)?.handle.sessionKey).toBe(held.session);
5268+
expect(
5269+
held.host.fake.ensured.slice(ensured).every((one) => one.resumeSessionId !== undefined),
5270+
).toBe(true);
5271+
});
5272+
5273+
it("keeps the saved recovery instruction and Start new chat after a pre-send failure", function* () {
5274+
const held = yield* retained();
5275+
yield* decision(held, false);
5276+
const prompts = held.host.fake.prompts.length;
5277+
held.host.fake.refuseSessionResume(held.session, "unknown recovery fault", true);
5278+
const page = yield* useReplPage({
5279+
host: { ...held.host, profile: held.profile },
5280+
location: held.location,
5281+
});
5282+
yield* page.press("[Use saved instruction]");
5283+
yield* intoComposer(page);
5284+
yield* page.key("Enter");
5285+
yield* page.until("saved recovery failure", (one) => one.shows("unknown recovery fault"));
5286+
expect(held.host.fake.prompts).toHaveLength(prompts);
5287+
expect(page.screen().shows(savedText)).toBe(true);
5288+
expect(page.screen().shows("[Start new chat]")).toBe(true);
5289+
});
5290+
5291+
it("refuses Retry when the resumed native identity differs", function* () {
5292+
const held = yield* retained();
5293+
yield* decision(held, false);
5294+
const page = yield* useReplPage({
52245295
host: { ...held.host, profile: held.profile },
52255296
location: held.location,
52265297
});
5227-
yield* reopened.until(
5228-
"the retained local failure",
5229-
(one) => one.shows("Release the active writer.") && one.shows("could not be resumed"),
5298+
yield* page.press("[Use saved instruction]");
5299+
yield* intoComposer(page);
5300+
held.host.fake.script({ reply: "<Output>Recovered.</Output>" });
5301+
yield* page.key("Enter");
5302+
yield* page.until("the completed recovery", (one) => one.shows("Recovered."));
5303+
held.host.fake.identity("a different native conversation");
5304+
const prompts = held.host.fake.prompts.length;
5305+
yield* sendMessage(page, "Do not send to a different conversation.");
5306+
yield* page.until("the identity refusal", (one) =>
5307+
one.shows("reopened a different native conversation"),
52305308
);
5231-
expect(reopened.screen().shows("[Retry]")).toBe(false);
5232-
expect(yield* readSidekickRecords(held.chat.location.sidekick)).toEqual(after);
5309+
expect(held.host.fake.prompts).toHaveLength(prompts);
5310+
const reading = project(yield* readSidekickRecords(held.chat.location.sidekick));
5311+
const request = reading.requests.at(-1);
5312+
if (request?.message === undefined) {
5313+
throw new Error("The refused request was not retained");
5314+
}
5315+
expect(reading.recoveryPending).toBe(false);
5316+
expect(request.payloadUnreconstructable).toBe(undefined);
5317+
expect(retryable(reading, held.conversation, request.message).ok).toBe(false);
52335318
});
52345319

52355320
for (const mode of ["prepared", "missing load"] as const) {

‎packages/cli/tests/support/fake-acp.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export interface FakeAcp {
299299
*/
300300
unavailable(agent: string, reason: Error): void;
301301
missingSession(sessionKey: string): void;
302-
refuseSessionResume(sessionKey: string, details?: string): void;
302+
refuseSessionResume(sessionKey: string, details?: string, retryable?: boolean): void;
303303
/**
304304
* Report this native conversation identity instead of a derived one.
305305
*
@@ -621,7 +621,7 @@ export function createFakeAcp(): FakeAcp {
621621
missingSession(sessionKey) {
622622
missingSessions.add(sessionKey);
623623
},
624-
refuseSessionResume(sessionKey, details) {
624+
refuseSessionResume(sessionKey, details, retryable) {
625625
const rpc = Object.assign(new Error("Internal error"), {
626626
code: -32041,
627627
data: { details, unrelated: "unrelated synthetic RPC payload" },
@@ -630,6 +630,7 @@ export function createFakeAcp(): FakeAcp {
630630
sessionKey,
631631
Object.assign(new Error("Raw load failure", { cause: rpc }), {
632632
detailCode: "SESSION_RESUME_REQUIRED",
633+
...(retryable === undefined ? {} : { retryable }),
633634
}),
634635
);
635636
},

‎specs/acp-client-spec.md‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,16 @@ resume, the element fails with the name `SessionResumeRequiredError` and no raw
12351235
upstream cause, its body and later content do not run, and no Prompt record,
12361236
attempted input, ordinal, usage or `started` event is created.
12371237

1238+
An unknown failure of an established same-session load or resume is a failed
1239+
pre-send attempt. The provider preserves the saved identity, creates no session,
1240+
and sends no prompt. It reports `SessionResumeRequiredError` with the ordinary
1241+
Session remedy: repeat the operation with the same `<Session>` when its caller
1242+
permits it; a different `<Session>` starts another conversation. Neither an ACP
1243+
code, provider name, message nor retryable hint decides eligibility. An identity
1244+
mismatch refuses; an attempt whose prompt delivery is uncertain cannot use this
1245+
pre-send rule. Classification never resends automatically. A workflow reports
1246+
the failure under its retained run contract and offers no per-Prompt Retry.
1247+
12381248
ACP attaches the [selected bounded facts](#saved-prompt-failure-details) to the
12391249
refusal under `executablemd.agent.prompt-diagnostic` with provider `acp`, stage
12401250
`session-ensure` and operation `session/ensure`. The operation names the
@@ -1248,7 +1258,7 @@ appends one line per present fact, in this order and with these labels; string
12481258
values use JSON string rendering so an embedded line break stays unambiguous:
12491259

12501260
```text
1251-
Session "<key>" could not be resumed. Name a different <Session> to start a new conversation.
1261+
Session "<key>" could not be resumed. Retry this operation with the same <Session> if its caller permits; a different <Session> starts a new conversation.
12521262
Resume diagnostic (provider=acp, operation=session/ensure, stage=session-ensure)
12531263
Normalization origin: acp
12541264
Runtime code: "RUNTIME" (normalizer fallback)
@@ -2092,8 +2102,8 @@ its caller supplied, from Prompt `timeout` or the enclosing AgentProvider
20922102
load/resume capability refuses even an unused record. Failed loading never replaces a used or
20932103
uncertain record; the released `-32603` fallback remains only for a provably
20942104
never-prompted record. Supported resume-required errors become
2095-
`SessionResumeRequiredError`, naming the Session and a different Session
2096-
element as the remedy, before `started`. An ordinary Session's refusal
2105+
`SessionResumeRequiredError`, naming a later attempt with the same Session as
2106+
the available remedy when the caller permits it, before `started`. An ordinary Session's failure
20972107
carries the bounded facts of
20982108
[Ordinary Session resume failure details](#ordinary-session-resume-failure-details)
20992109
under the `session-ensure` stage; Agent screens them and owns the message.

0 commit comments

Comments
 (0)