Skip to content

Commit 3c44c22

Browse files
kazemek21claude
andcommitted
fix(pi): let a run taken on leave its run after a retry takes its request
A retry of a request a run taken on again holds replaces its receipt, so that receipt skipped its cleanup when the run ended and, having no Stop, refused settings in the next fresh run. It now leaves the run whoever holds the request; the request's activity and reports stay with the retry. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Kyle Zemek <[email protected]>
1 parent bb0b998 commit 3c44c22

2 files changed

Lines changed: 114 additions & 4 deletions

File tree

‎src/bundled/pi/runtime.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,6 +2821,109 @@ it.each([
28212821
}
28222822
});
28232823

2824+
it("changes Buzz settings in a fresh run after a retry took over a run taken on", async () => {
2825+
let waits = 0;
2826+
const recovered = deferred<unknown>();
2827+
let settled = false;
2828+
const thread = message("5", "Deploy is stuck");
2829+
// The first delivery's run, the one in another thread that takes it on
2830+
// again, the first one's retry, and a fresh run in the first conversation.
2831+
const runs = [
2832+
{ conversation: CONVERSATION, submission: 1, placed: true },
2833+
{ conversation: CONVERSATION + 1, submission: 2, placed: true },
2834+
// Pi takes the retry as the request it already has, placing nothing.
2835+
{ conversation: CONVERSATION, submission: 1, placed: false },
2836+
{ conversation: CONVERSATION, submission: 3, placed: true },
2837+
];
2838+
const f = setup({
2839+
deliver: (params, process) => {
2840+
const run = runs[f.host.requests("deliver").length - 1];
2841+
if (!run) throw new Error("No more deliveries");
2842+
if (run.placed)
2843+
process.send({
2844+
method: "event",
2845+
params: {
2846+
agent: self,
2847+
conversation: run.conversation,
2848+
type: "placed",
2849+
request: params.request,
2850+
entry: 1,
2851+
},
2852+
});
2853+
return { submission: run.submission, conversation: run.conversation };
2854+
},
2855+
// Two hosts stop while the first run is waited for, which is given up on;
2856+
// the run taken on again ends while its request is retried.
2857+
wait: ({ submission }, process) => {
2858+
if (submission !== 1) return new Promise(() => {});
2859+
if (++waits > 2) return recovered.promise;
2860+
queueMicrotask(() => process.exit(1, "Killed\n"));
2861+
return new Promise(() => {});
2862+
},
2863+
unfinished: () => ({
2864+
submissions:
2865+
f.host.processes.length > 2 && !settled
2866+
? [
2867+
{
2868+
submission: 1,
2869+
conversation: CONVERSATION,
2870+
request: f.host.requests("deliver")[0]?.params.request,
2871+
context: { channel, root: root.id },
2872+
},
2873+
]
2874+
: [],
2875+
}),
2876+
});
2877+
f.session.read.mockImplementation(async (filters?: { ids?: string[] }[]) =>
2878+
filters?.[0]?.ids?.includes(thread.id) ? [thread] : [root],
2879+
);
2880+
const set = vi.fn((_key: unknown, value: unknown, check: () => void) => {
2881+
check();
2882+
return { key: "example.mood", value };
2883+
});
2884+
Object.assign(f.agent, { settings: { list: () => [], get: vi.fn(), set } });
2885+
try {
2886+
const mention = reply("2", "@Pi look");
2887+
const first = f.deliver({ type: "mention", event: mention });
2888+
await first.run;
2889+
await f.settled(first);
2890+
const other = f.deliver({
2891+
type: "mention",
2892+
event: message("6", "@Pi too", {
2893+
created_at: 110,
2894+
tags: [
2895+
["h", channel],
2896+
["e", thread.id, "", "reply"],
2897+
["p", self],
2898+
],
2899+
}),
2900+
});
2901+
await other.run;
2902+
await vi.waitFor(() => expect(waits).toBe(3));
2903+
const retry = f.deliver({ type: "mention", event: mention });
2904+
await retry.run;
2905+
await vi.waitFor(() => expect(f.host.requests("deliver")).toHaveLength(3));
2906+
settled = true;
2907+
recovered.resolve({ status: "done", answer: 3, text: "" });
2908+
await f.settled(retry);
2909+
const next = f.deliver({ type: "mention", event: reply("4", "@Pi again") });
2910+
await next.run;
2911+
await vi.waitFor(() => expect(next.acknowledged).toHaveBeenCalled());
2912+
expect(
2913+
await f.runtime.tool({
2914+
agent: self,
2915+
conversation: CONVERSATION,
2916+
context: { channel, root: root.id },
2917+
name: "settings_set",
2918+
arguments: { key: "example.mood", value: "busy" },
2919+
}),
2920+
).not.toMatchObject({ isError: true });
2921+
expect(set).toHaveReturned();
2922+
} finally {
2923+
await f.runtime.dispose();
2924+
}
2925+
});
2926+
28242927
it.each([
28252928
["the delivery it came in", false],
28262929
["a delivery that joined since", true],

‎src/bundled/pi/runtime.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,14 +904,19 @@ export class PiRuntime {
904904
active.delete(scope);
905905
if (!active.size) entry.active.delete(conversation);
906906
}
907-
// Settled or given up on, even if a host that stopped never said its run
908-
// ended: a run Pi still holds is taken on again, with no Stop.
907+
if (receipt) this.leave(entry, receipt);
908+
this.notify();
909+
}
910+
911+
/** Settled or given up on, a delivery leaves its run, even if a host that
912+
* stopped never said it ended: a run Pi still holds is taken on again, with
913+
* no Stop. */
914+
private leave(entry: Entry, receipt: Receipt) {
909915
for (const [conversation, run] of entry.runs) {
910-
const at = receipt ? run.indexOf(receipt) : -1;
916+
const at = run.indexOf(receipt);
911917
if (at >= 0) run.splice(at, 1);
912918
if (!run.length) entry.runs.delete(conversation);
913919
}
914-
this.notify();
915920
}
916921

917922
/** Opens the agent's storage in the host, with its Buzz tools as they are
@@ -1091,7 +1096,9 @@ export class PiRuntime {
10911096
await this.report(handle, where, errorText(error));
10921097
})
10931098
.finally(() => {
1099+
// A delivery of its request since keeps the request's activity.
10941100
if (ours()) this.forget(entry, request);
1101+
else this.leave(entry, receipt);
10951102
});
10961103
return receipt.settled;
10971104
});

0 commit comments

Comments
 (0)