fix: require a successful probe result before handoff

Adapt the bounded probe-completion foundation from PR #130 by aksalatdev (source commit 535202bebe038fa1e0648f5df809d5ccfc26d071), not its mixed stream-salvage implementation.

Require a non-failed call followed by a matching probe_done result with neither isError nor explicit details.ok:false. Preserve older clean acknowledgments, saved attempts, and all existing handoff/retry/provider policies. Share the native completion seam with phase detection and exclude explicit refusals from UI phases.

Verify retained failed-probe Retry recovery with real agent/tool composition and synthetic engines: one fresh conclusion, one handoff, then teacher selection, without stored-tool replay. Focused tests 58/58; full tests 2473/2473; typecheck and full ESLint pass in isolated storage with scrubbed provider credentials.

Co-authored-by: aksalatdev <73862605+aksalatdev@users.noreply.github.com>
This commit is contained in:
amosblomqvist
2026-10-27 20:49:04 +02:00
co-authored by aksalatdev
parent 354bd34c0c
commit f01fdf3260
7 changed files with 178 additions and 32 deletions
+61 -2
View File
@@ -13,12 +13,13 @@ import * as path from "node:path";
import { after, describe, it } from "node:test";
import { probeDoneTool } from "../src/agent/tools/probe.ts";
import type { SteerMessage } from "../src/core/contracts.ts";
import type { LhMessage } from "../src/core/native-messages.ts";
import { hasProbeDone, type LhMessage } from "../src/core/native-messages.ts";
import { phaseAt3Lh } from "../src/core/phases.ts";
import { HANDOFF_TEXT } from "../src/core/contracts.ts";
import { chatStore } from "../src/store/chats.ts";
import { PATHS } from "../config.ts";
const dataDir = path.join(PATHS.root, "data");
const dataDir = PATHS.data;
fs.mkdirSync(dataDir, { recursive: true });
const createdChats: string[] = [];
@@ -142,3 +143,61 @@ describe("probe_done: the probe→planning seam", () => {
assert.equal(res.ok, true, "ok:true even when the queue is lost — the next client message constructs the teacher off the transcript seam");
});
});
/** A real record result, distinct from the calling assistant message. */
function probeResult(overrides: Partial<Extract<LhMessage, { role: "toolResult" }>> = {}): LhMessage {
return {
id: "probe-result", role: "toolResult", toolCallId: "call_probe", toolName: "probe_done",
content: [{ type: "text", text: "ok" }], isError: false, timestamp: 1,
...overrides,
};
}
const probeCall = () => assistantWithCall("probe", "probe_done", { assessment: ASSESSMENT });
const failedProbeCall = (stopReason: "error" | "aborted"): LhMessage => ({ ...probeCall(), stopReason } as LhMessage);
const incompleteProbes: Array<[string, () => LhMessage[]]> = [
["unmatched call", () => [probeCall()]],
["errored result", () => [probeCall(), probeResult({ isError: true })]],
["explicit refusal without isError", () => [probeCall(), probeResult({ details: { ok: false, error: "refused" } })]],
["result without a call", () => [probeResult()]],
["wrong call id", () => [probeCall(), probeResult({ toolCallId: "other" })]],
["wrong result tool name", () => [probeCall(), probeResult({ toolName: "plan_init" })]],
["wrong call tool name", () => [assistantWithCall("probe", "plan_init", {}), probeResult()]],
["result before the call", () => [probeResult(), probeCall()]],
["failed calling generation", () => [failedProbeCall("error"), probeResult({ details: { ok: true } })]],
["aborted calling generation", () => [failedProbeCall("aborted"), probeResult({ details: { ok: true } })]],
];
describe("probe completion: matched successful results only (PR130 foundation)", () => {
for (const [name, messages] of incompleteProbes) {
it(`${name} keeps probing open and allows a fresh conclusion`, async () => {
const chatId = newChat(`Probe recovery: ${name}`);
const before = messages();
const snapshot = structuredClone(before);
chatStore.saveMessages(chatId, before);
assert.equal(hasProbeDone(before), false);
assert.equal(phaseAt3Lh(before), "probe");
const delivered: SteerMessage[] = [];
const tools = probeDoneTool({ chatId, chatStore, deliver: (s) => delivered.push(s) });
assert.equal((await execute(tools, { assessment: ASSESSMENT })).ok, true);
assert.equal(delivered.length, 1, "only the fresh invocation queues a handoff");
assert.deepEqual(before, snapshot, "the scans never mutate history");
assert.deepEqual(chatStore.getChat(chatId)!.messages, snapshot, "the tool does not delete or replay saved attempts");
});
}
for (const [name, result] of [
["current acknowledgment", probeResult({ details: { ok: true } })],
["older clean acknowledgment without details", probeResult()],
["older clean acknowledgment without ok", probeResult({ details: { assessment: ASSESSMENT } })],
] as const) {
it(`${name} concludes exactly at the matching result`, () => {
const messages = [probeCall(), result];
const before = structuredClone(messages);
assert.equal(hasProbeDone(messages), true);
assert.equal(phaseAt3Lh(messages, 0), "probe", "a future result cannot conclude an earlier prefix");
assert.equal(phaseAt3Lh(messages, 1), "plan");
assert.deepEqual(messages, before);
});
}
});