Skip to content

Commit bb0b998

Browse files
kazemek21claude
andcommitted
fix(pi): keep a run's settings check in step with host recovery
A host that stops never says its run ended, so a stopped delivery stayed in the run and refused settings in the next one, and a run given up on and taken on again was checked against its old receipt, which had a Stop. A delivery now leaves its run when it settles or is given up on, and a run taken on joins it without a Stop, so settings are refused there. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Kyle Zemek <[email protected]>
1 parent a168430 commit bb0b998

2 files changed

Lines changed: 116 additions & 4 deletions

File tree

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,105 @@ it.each([
27222722
},
27232723
);
27242724

2725+
// Pi says nothing of a run's end after its host stops.
2726+
it.each([
2727+
[
2728+
"changes Buzz settings in a fresh run after a stopped one's host stopped",
2729+
true,
2730+
],
2731+
[
2732+
"refuses a Buzz settings change in a run taken on after its turn gave up",
2733+
false,
2734+
],
2735+
])("%s", async (_, stopped) => {
2736+
let waits = 0;
2737+
const f = setup({
2738+
deliver: (params, process) => {
2739+
process.send({
2740+
method: "event",
2741+
params: {
2742+
agent: self,
2743+
conversation: CONVERSATION,
2744+
type: "placed",
2745+
request: params.request,
2746+
entry: 1,
2747+
},
2748+
});
2749+
return {
2750+
submission: f.host.requests("deliver").length,
2751+
conversation: CONVERSATION,
2752+
};
2753+
},
2754+
// A host stops while it waits for the first run: a stopped run settles
2755+
// in the next, and one not stopped is given up on when that stops too.
2756+
wait: ({ submission }, process) => {
2757+
if (submission !== 1) return new Promise(() => {});
2758+
if (++waits > (stopped ? 1 : 2))
2759+
return stopped
2760+
? { status: "unanswered", reason: "aborted" }
2761+
: new Promise(() => {});
2762+
queueMicrotask(() => process.exit(1, "Killed\n"));
2763+
return new Promise(() => {});
2764+
},
2765+
abort: () => ({}),
2766+
// Pi's storage still holds a run given up on, which a delivery takes on.
2767+
unfinished: () => ({
2768+
submissions:
2769+
!stopped && f.host.processes.length > 2
2770+
? [
2771+
{
2772+
submission: 1,
2773+
conversation: CONVERSATION,
2774+
request: f.host.requests("deliver")[0]?.params.request,
2775+
context: { channel, root: root.id },
2776+
},
2777+
]
2778+
: [],
2779+
}),
2780+
});
2781+
const set = vi.fn((_key: unknown, value: unknown, check: () => void) => {
2782+
check();
2783+
return { key: "example.mood", value };
2784+
});
2785+
Object.assign(f.agent, { settings: { list: () => [], get: vi.fn(), set } });
2786+
try {
2787+
const interruptible = vi.fn<NonNullable<Delivery["interruptible"]>>();
2788+
const first = f.deliver(
2789+
{ type: "mention", event: reply("2", "@Pi look") },
2790+
{ interruptible },
2791+
);
2792+
await first.run;
2793+
await vi.waitFor(() => expect(first.acknowledged).toHaveBeenCalled());
2794+
if (stopped) void interruptible.mock.calls[0]?.[0]();
2795+
await f.settled(first);
2796+
const next = f.deliver({
2797+
type: "mention",
2798+
event: reply("4", "@Pi again"),
2799+
});
2800+
await next.run;
2801+
await vi.waitFor(() => expect(next.acknowledged).toHaveBeenCalled());
2802+
const changed = await f.runtime.tool({
2803+
agent: self,
2804+
conversation: CONVERSATION,
2805+
context: { channel, root: root.id },
2806+
name: "settings_set",
2807+
arguments: { key: "example.mood", value: "busy" },
2808+
});
2809+
if (stopped) {
2810+
expect(changed).not.toMatchObject({ isError: true });
2811+
expect(set).toHaveReturned();
2812+
} else {
2813+
expect(changed).toMatchObject({
2814+
isError: true,
2815+
content: [{ text: expect.stringMatching(/stopped or has ended/) }],
2816+
});
2817+
expect(set).not.toHaveReturned();
2818+
}
2819+
} finally {
2820+
await f.runtime.dispose();
2821+
}
2822+
});
2823+
27252824
it.each([
27262825
["the delivery it came in", false],
27272826
["a delivery that joined since", true],

‎src/bundled/pi/runtime.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ type Entry = {
167167
/** The request each conversation's activity shows, by its scope. */
168168
active: Map<number, Map<string, Receipt>>;
169169
/** Each conversation's deliveries Pi placed in its current run, including
170-
* those a later one replaced, until Pi says it ended: Stop of any stops it. */
170+
* those a later one replaced, and the runs taken on in it, until Pi says it
171+
* ended or they settle: Stop of any stops it, and a taken-on one has none. */
171172
runs: Map<number, Receipt[]>;
172173
/** The latest taking-on of the runs Pi's storage holds that nothing here
173174
* waits for; absent until the agent opens here, or after one failed. */
@@ -856,9 +857,7 @@ export class PiRuntime {
856857
if (!receipt) return;
857858
active ??= new Map();
858859
entry.active.set(event.conversation, active);
859-
const run = entry.runs.get(event.conversation);
860-
if (run) run.push(receipt);
861-
else entry.runs.set(event.conversation, [receipt]);
860+
this.join(entry, event.conversation, receipt);
862861
const before = active.get(receipt.scope);
863862
if (!before || receipt.step) {
864863
if (before !== receipt) before?.superseded?.();
@@ -890,6 +889,12 @@ export class PiRuntime {
890889
return true;
891890
}
892891

892+
private join(entry: Entry, conversation: number, receipt: Receipt) {
893+
const run = entry.runs.get(conversation);
894+
if (run) run.push(receipt);
895+
else entry.runs.set(conversation, [receipt]);
896+
}
897+
893898
private forget(entry: Entry, request: string) {
894899
const receipt = entry.receipts.get(request);
895900
entry.receipts.delete(request);
@@ -899,6 +904,13 @@ export class PiRuntime {
899904
active.delete(scope);
900905
if (!active.size) entry.active.delete(conversation);
901906
}
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.
909+
for (const [conversation, run] of entry.runs) {
910+
const at = receipt ? run.indexOf(receipt) : -1;
911+
if (at >= 0) run.splice(at, 1);
912+
if (!run.length) entry.runs.delete(conversation);
913+
}
902914
this.notify();
903915
}
904916

@@ -1060,6 +1072,7 @@ export class PiRuntime {
10601072
const active = entry.active.get(conversation) ?? new Map();
10611073
active.set(receipt.scope, receipt);
10621074
entry.active.set(conversation, active);
1075+
this.join(entry, conversation, receipt);
10631076
waits.push(() => {
10641077
receipt.settled = this.settle(
10651078
entry,

0 commit comments

Comments
 (0)