Skip to content

Commit a59043c

Browse files
kazemek21claude
andcommitted
fix(agents): refuse settings in a Pi run whose stopped delivery was replaced, and announce failed saves
Pi rechecked only the deliveries in a run when a settings call arrived and those placed after it, so a delivery stopped and replaced just before the call was missed. Stop of any delivery stops the run, so the call now checks every delivery placed in it. A failed change of the Change Buzz settings switch now also goes to an alert, so screen readers announce it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Kyle Zemek <[email protected]>
1 parent 6d66406 commit a59043c

4 files changed

Lines changed: 93 additions & 17 deletions

File tree

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,66 @@ it("changes Buzz settings only in a delivered run the owner hasn't stopped", asy
25632563
}
25642564
});
25652565

2566+
it("refuses a Buzz settings change in a run whose stopped delivery was replaced before the call", async () => {
2567+
const answered = deferred<unknown>();
2568+
const f = setup({
2569+
deliver: (params, process) => {
2570+
process.send({
2571+
method: "event",
2572+
params: {
2573+
agent: self,
2574+
conversation: CONVERSATION,
2575+
type: "placed",
2576+
request: params.request,
2577+
entry: 1,
2578+
},
2579+
});
2580+
return { submission: 1, conversation: CONVERSATION };
2581+
},
2582+
wait: () => answered.promise,
2583+
// Pi hasn't finished the stopped run when the next delivery is placed.
2584+
abort: () => ({}),
2585+
});
2586+
const set = vi.fn((_key: unknown, value: unknown, check: () => void) => {
2587+
check();
2588+
return { key: "example.mood", value };
2589+
});
2590+
Object.assign(f.agent, { settings: { list: () => [], get: vi.fn(), set } });
2591+
try {
2592+
const interruptible = vi.fn<NonNullable<Delivery["interruptible"]>>();
2593+
const first = f.deliver(
2594+
{ type: "mention", event: reply("2", "@Pi look") },
2595+
{ interruptible },
2596+
);
2597+
await first.run;
2598+
await vi.waitFor(() => expect(first.acknowledged).toHaveBeenCalled());
2599+
void interruptible.mock.calls[0]?.[0]();
2600+
const next = f.deliver({
2601+
type: "mention",
2602+
event: reply("4", "@Pi again"),
2603+
});
2604+
await next.run;
2605+
await vi.waitFor(() => expect(first.superseded).toHaveBeenCalled());
2606+
expect(
2607+
await f.runtime.tool({
2608+
agent: self,
2609+
conversation: CONVERSATION,
2610+
context: { channel, root: root.id },
2611+
name: "settings_set",
2612+
arguments: { key: "example.mood", value: "busy" },
2613+
}),
2614+
).toMatchObject({
2615+
isError: true,
2616+
content: [{ text: expect.stringMatching(/stopped or has ended/) }],
2617+
});
2618+
expect(set).toHaveBeenCalledOnce();
2619+
expect(set).not.toHaveReturned();
2620+
} finally {
2621+
answered.resolve({ status: "unanswered", reason: "aborted" });
2622+
await f.runtime.dispose();
2623+
}
2624+
});
2625+
25662626
it.each([
25672627
["the delivery it came in", false],
25682628
["a delivery that joined since", true],

‎src/bundled/pi/runtime.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type Receipt = Pick<Delivery, "step" | "acknowledged" | "superseded"> & {
140140
stopped?: () => boolean;
141141
};
142142
/** Each run's deliveries in the order Pi placed them, including those a later
143-
* one replaced, so a call can recheck all that joined while it waited. */
143+
* one replaced: Stop of any of them stops the run. */
144144
const placements = new WeakMap<Map<string, Receipt>, readonly Receipt[]>();
145145

146146
type Entry = {
@@ -793,7 +793,6 @@ export class PiRuntime {
793793
const run = entry?.active.get(call.conversation);
794794
// Later deliveries can replace these in `run` while the call waits.
795795
const delivered = [...(run?.values() ?? [])];
796-
const placedBefore = (run && placements.get(run)?.length) ?? 0;
797796
const client =
798797
entry?.handle &&
799798
appClient(entry.handle, {
@@ -818,16 +817,17 @@ export class PiRuntime {
818817
remembered: () => this.forgetMemory(call.agent),
819818
...(signal ? { signal } : {}),
820819
// Settings change only in a delivered run the owner hasn't stopped:
821-
// neither a delivery the call came in nor any placed in it since.
820+
// Stop of any delivery placed in it, even one since replaced, stops it.
822821
turn: () => {
823-
const since = (run && placements.get(run)?.slice(placedBefore)) ?? [];
824822
if (
825823
!run ||
826824
!delivered.length ||
827825
entry.active.get(call.conversation) !== run ||
828-
[...delivered, ...since, ...run.values()].some(
829-
(receipt) => receipt.stopped?.() !== false,
830-
)
826+
[
827+
...delivered,
828+
...(placements.get(run) ?? []),
829+
...run.values(),
830+
].some((receipt) => receipt.stopped?.() !== false)
831831
)
832832
throw new Error("This turn was stopped or has ended.");
833833
},

‎src/features/agents/AgentManage.test.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ it("lets the owner allow and stop an agent changing Buzz settings, showing a fai
9191
);
9292
const toggle = screen.getByRole("switch", { name: /Change Buzz settings/ });
9393
expect(toggle).not.toBeChecked();
94+
expect(screen.queryByRole("alert")).toBeNull();
9495
await user.click(toggle);
9596
expect(allowSettings).toHaveBeenLastCalledWith(agent.key, true);
9697
rerender(
@@ -108,6 +109,10 @@ it("lets the owner allow and stop an agent changing Buzz settings, showing a fai
108109
"It can't change settings now, but this wasn't saved: disk full",
109110
),
110111
);
112+
// Announced, since it arrives after the switch was used.
113+
expect(screen.getByRole("alert")).toHaveTextContent(
114+
"It can't change settings now, but this wasn't saved: disk full",
115+
);
111116
});
112117

113118
it("doesn't claim the agent can't change settings when turning it off was refused", async () => {
@@ -129,6 +134,9 @@ it("doesn't claim the agent can't change settings when turning it off was refuse
129134
"Couldn't turn it off: Not here",
130135
),
131136
);
137+
expect(screen.getByRole("alert")).toHaveTextContent(
138+
"Couldn't turn it off: Not here",
139+
);
132140
});
133141

134142
it("offers no settings switch for an agent that runs elsewhere", () => {

‎src/features/agents/AgentManage.tsx‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,24 @@ export function AgentSettingsAccess({
343343
setPending(false);
344344
}
345345
};
346+
// One child, so the settings group draws no divider for the alert.
346347
return (
347-
<SwitchPreferenceRow
348-
label="Change Buzz settings"
349-
description={
350-
error ||
351-
"Lets it change appearance and where new threads open on this device when you ask."
352-
}
353-
checked={!!agent.settingsAllowed}
354-
disabled={pending}
355-
onCheckedChange={(allowed) => void change(allowed)}
356-
/>
348+
<div>
349+
<SwitchPreferenceRow
350+
label="Change Buzz settings"
351+
description={
352+
error ||
353+
"Lets it change appearance and where new threads open on this device when you ask."
354+
}
355+
checked={!!agent.settingsAllowed}
356+
disabled={pending}
357+
onCheckedChange={(allowed) => void change(allowed)}
358+
/>
359+
{error && (
360+
<span role="alert" className="sr-only">
361+
{error}
362+
</span>
363+
)}
364+
</div>
357365
);
358366
}

0 commit comments

Comments
 (0)