repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(goal): defer visible messages until idle

2fd15d344b5dc9057c0520e063ba5740453c3a4a

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/goal/__tests__/unit.test.ts

Modified
91 91 options?: Record<string, unknown>,
92 92 ) => {
93 93 sent.push({ message, options });
94 + entries.push({ type: "custom_message", ...message });
95 96 sendUserMessage: (content: string) => {
96 97 sent.push({ message: { content }, options: { triggerTurn: true } });
187 188 it("records user steering as one criterion event, not a judge verdict", async () => {
188 - const harness = createCompactionHarness({ idle: false });
189 - await emitHarness(harness, "before_agent_start", {
190 - prompt:
191 - "Start working toward this standing goal.\n\n<objective>ship it</objective>",
192 - systemPrompt: "base",
193 - });
189 + vi.useFakeTimers();
190 + try {
191 + const harness = createCompactionHarness({ idle: false });
192 + await emitHarness(harness, "before_agent_start", {
193 + prompt:
194 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
195 + systemPrompt: "base",
196 + });
195 - await emitHarness(harness, "input", {
196 - text: "use simpler code",
197 - source: "user",
198 - });
198 + await emitHarness(harness, "input", {
199 + text: "use simpler code",
200 + source: "user",
201 + });
202 + await vi.runAllTimersAsync();
200 - const latest = __goalTest.latestGoalState({
201 - sessionManager: harness.ctx.sessionManager,
202 - } as never);
203 - const visibleCriterionEvents = [
204 - ...harness.entries.filter(
204 + const latest = __goalTest.latestGoalState({
205 + sessionManager: harness.ctx.sessionManager,
206 + } as never);
207 + const visibleCriterionEvents = harness.entries.filter(
205 208 (entry) =>
206 209 entry.type === "custom_message" &&
207 210 entry.customType === "goal-event" &&
208 211 entry.display === true,
209 - ),
210 - ...harness.sent.filter(
211 - (entry) =>
212 - (entry.message as Record<string, unknown>).customType ===
213 - "goal-event" &&
214 - (entry.message as Record<string, unknown>).display === true,
215 - ),
216 - ];
212 + );
218 - expect(latest?.criteria).toContain("use simpler code");
219 - expect(latest?.lastVerdict).toBeUndefined();
220 - expect(latest?.lastReason).toBeUndefined();
221 - expect(visibleCriterionEvents).toHaveLength(1);
214 + expect(latest?.criteria).toContain("use simpler code");
215 + expect(latest?.lastVerdict).toBeUndefined();
216 + expect(latest?.lastReason).toBeUndefined();
217 + expect(visibleCriterionEvents).toHaveLength(1);
218 + } finally {
219 + vi.useRealTimers();
224 223 it("applies a happy judge as one atomic completion transition", async () => {

extensions/goal/index.ts

Modified
1038 1038 function appendVisibleCustomMessage(
1039 1039 pi: ExtensionAPI,
1040 + ctx: ExtensionContext,
1040 1041 customType: string,
1041 1042 content: string,
1042 1043 details: Record<string, unknown>,
1043 1044 ): void {
1044 - pi.sendMessage({ customType, content, display: true, details });
1045 + const send = () =>
1046 + pi.sendMessage(
1047 + { customType, content, display: true, details },
1048 + { triggerTurn: false },
1049 + );
1050 + if (ctx.isIdle()) {
1051 + send();
1052 + return;
1054 + setTimeout(() => {
1055 + try {
1056 + send();
1057 + } catch (error) {
1058 + if (!isStaleContextError(error)) throw error;
1060 + }, 0);
1047 1063 function appendGoalEvent(
1048 1064 pi: ExtensionAPI,
1049 - _ctx: ExtensionContext,
1065 + ctx: ExtensionContext,
1050 1066 event: string,
1051 1067 state: GoalState | undefined,
1052 1068 data: Record<string, unknown> = {},
1064 1080 const suffix = reason ?? text;
1065 1081 appendVisibleCustomMessage(
1083 + ctx,
1067 1084 GOAL_EVENT_ENTRY,
1068 1085 `goal ${event}${suffix ? ` · ${cleanGoalText(suffix)}` : ""}`,
1069 1086 payload,
2094 2111 function appendJudge(
2095 2112 pi: ExtensionAPI,
2096 - _ctx: ExtensionContext,
2113 + ctx: ExtensionContext,
2097 2114 event: "start" | "verdict",
2098 2115 state: GoalState,
2099 2116 verdict?: JudgeVerdict,
2110 2127 if (event !== "start") return;
2111 2128 appendVisibleCustomMessage(
2130 + ctx,
2113 2131 GOAL_JUDGE_ENTRY,
2114 2132 `goal judge running · turn ${data.turn}`,
3040 3058 pi.appendEntry(GOAL_EVENT_ENTRY, payload);
3041 3059 appendVisibleCustomMessage(
3061 + ctx,
3043 3062 GOAL_JUDGE_ENTRY,
3044 3063 `goal judge passed → goal complete · turn ${turnsUsed} · ${judgeDisplayReason(verdict.reason)}`,
3045 3064 { ...payload, verdict },
3065 3084 appendVisibleCustomMessage(
3086 + ctx,
3067 3087 GOAL_JUDGE_ENTRY,
3068 3088 `goal judge sees blocker · turn ${turnsUsed} · ${judgeDisplayReason(verdict.reason)}`,
3069 3089 { goalId: state.id, turn: turnsUsed, verdict },
3116 3136 appendVisibleCustomMessage(
3138 + ctx,
3118 3139 GOAL_JUDGE_ENTRY,
3119 3140 `goal judge rejected completion · turn ${turnsUsed} · ${judgeDisplayReason(verdict.reason)}`,
3120 3141 { goalId: state.id, turn: turnsUsed, verdict },