Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

tape-fold: don't synthesize tool results for aborted assistant messages (#239)

Regan Bell0f0e0adccce2
diff --git a/src/harness/tape-fold.ts b/src/harness/tape-fold.ts
index 5414fd5c8..8ab97adfb 100644
--- a/src/harness/tape-fold.ts
+++ b/src/harness/tape-fold.ts
@@ -177,6 +177,11 @@ function contextEvent(row: TapeRecord): TapeEvent | null {
type Foldable = { out: unknown[]; boundaries: Array<{ pos: number; entrySeq: number }> };
+function assistantDroppedAtReplay(m: unknown): boolean {
+ const msg = m as { role?: string; stopReason?: string };
+ return msg?.role === "assistant" && (msg.stopReason === "aborted" || msg.stopReason === "error");
+}
+
function healDanglingCalls(out: unknown[], at: number): void {
const answered = new Set<string>();
for (const m of out) {
@@ -186,7 +191,7 @@ function healDanglingCalls(out: unknown[], at: number): void {
const missing: Array<{ id: string; name: string }> = [];
for (const m of out) {
const msg = m as { role?: string; content?: unknown };
- if (msg?.role !== "assistant" || !Array.isArray(msg.content)) continue;
+ if (msg?.role !== "assistant" || !Array.isArray(msg.content) || assistantDroppedAtReplay(m)) continue;
for (const block of msg.content) {
const b = block as { type?: string; id?: string; name?: string };
if (b?.type === "toolCall" && typeof b.id === "string" && !answered.has(b.id)) {
@@ -309,7 +314,7 @@ export function lintFold(messages: readonly unknown[]): FoldLint {
if (b?.type === "toolCall" && typeof b.id === "string") {
if (seenCallIds.has(b.id)) problems.push(`#${i}: duplicate tool call id ${b.id}`);
seenCallIds.add(b.id);
- openCalls.add(b.id);
+ if (!assistantDroppedAtReplay(m)) openCalls.add(b.id);
}
}
} else if (msg.role === "toolResult") {
diff --git a/test/tape-fold.test.ts b/test/tape-fold.test.ts
index f257fd8bb..c6d3604e5 100644
--- a/test/tape-fold.test.ts
+++ b/test/tape-fold.test.ts
@@ -129,6 +129,84 @@ test("interrupt event heals dangling tool calls with error results", () => {
assert.ok(lintFold(out).ok);
});
+test("aborted assistant's dangling tool call is not healed — pi drops the message at replay", () => {
+ seq = 0;
+ const abortedAssistant = row({
+ kind: "message",
+ payload: {
+ role: "assistant",
+ content: [{ type: "toolCall", id: "c9", name: "exec", arguments: { cmd: "sleep" } }],
+ timestamp: 2,
+ stopReason: "aborted",
+ },
+ });
+ const rows = [
+ user("q"),
+ assistant([{ type: "toolCall", id: "c1", name: "exec", arguments: {} }]),
+ toolResult("c1", "ok"),
+ abortedAssistant,
+ row({ kind: "context_event", payload: { event: "interrupt" } }),
+ user("next turn"),
+ ];
+ assert.ok(!tapeNeedsInterruptHeal(rows.slice(0, 4)), "aborted dangler needs no heal");
+ const out = foldTape(rows) as Array<{ role: string; toolCallId?: string }>;
+ assert.ok(
+ !out.some((m) => m.role === "toolResult" && m.toolCallId === "c9"),
+ "no synthetic result for a call pi will drop with its aborted message",
+ );
+ assert.ok(lintFold(out).ok);
+});
+
+test("a poisoned tape — errored assistants, consecutive users, pre-existing interrupt — serves clean", () => {
+ seq = 0;
+ const erroredAssistant = () =>
+ row({
+ kind: "message",
+ payload: { role: "assistant", content: [], timestamp: 4, stopReason: "error" },
+ });
+ const rows = [
+ user("q"),
+ assistant([{ type: "toolCall", id: "c1", name: "exec", arguments: {} }]),
+ toolResult("c1", "ok"),
+ row({
+ kind: "message",
+ payload: {
+ role: "assistant",
+ content: [{ type: "toolCall", id: "c9", name: "exec", arguments: {} }],
+ timestamp: 2,
+ stopReason: "aborted",
+ },
+ }),
+ row({ kind: "context_event", payload: { event: "interrupt" } }),
+ user("next turn"),
+ erroredAssistant(),
+ user("retry note"),
+ erroredAssistant(),
+ ];
+ const out = foldTape(rows) as Array<{ role: string; toolCallId?: string }>;
+ assert.ok(!out.some((m) => m.role === "toolResult" && m.toolCallId === "c9"));
+ assert.ok(lintFold(out).ok);
+ assert.ok(!tapeNeedsInterruptHeal(rows));
+});
+
+test("lintFold rejects a toolResult answering an aborted assistant's call", () => {
+ seq = 0;
+ const rows = [
+ user("q"),
+ row({
+ kind: "message",
+ payload: {
+ role: "assistant",
+ content: [{ type: "toolCall", id: "c9", name: "exec", arguments: {} }],
+ timestamp: 2,
+ stopReason: "aborted",
+ },
+ }),
+ toolResult("c9", "orphaned on the wire"),
+ ];
+ assert.ok(!lintFold(foldTape(rows)).ok);
+});
+
test("audience filter withholds message rows the whole room isn't entitled to, never events", () => {
seq = 0;
const me = { id: "U1", type: "internal" } as unknown as Principal;