repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(goal): resume after compaction

84900faf9f82e94097a1f1781a5a4a1215cd074b

Review <review@example.invalid> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

CHANGELOG.md

Modified
12 12 assistant tail replay do not leak sibling branch data; refresh live goal state
13 13 on tree navigation and cancel stale continuations on tree navigation or
14 14 session shutdown
15 +- Resume active goals after successful compaction while preserving paused,
16 + blocked, failed, and cross-session goal states
16 18 ## 0.0.3

extensions/goal/__tests__/unit.test.ts

Modified
205 + it("resumes active goal after successful compaction", async () => {
206 + vi.useFakeTimers();
207 + try {
208 + const harness = createCompactionHarness();
209 + await emitHarness(harness, "before_agent_start", {
210 + prompt:
211 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
212 + systemPrompt: "base",
213 + });
214 + harness.sent.length = 0;
216 + await emitHarness(harness, "session_before_compact", {
217 + preparation: {},
218 + branchEntries: [],
219 + signal: new AbortController().signal,
220 + });
221 + await emitHarness(harness, "session_compact", {
222 + compactionEntry: {},
223 + fromExtension: false,
224 + });
225 + await vi.runAllTimersAsync();
227 + expect(
228 + harness.sent.map(
229 + (entry) => (entry.message as Record<string, unknown>).customType,
230 + ),
231 + ).toContain("goal-continuation");
232 + } finally {
233 + vi.useRealTimers();
235 + });
237 + it("does not resume after compaction when goal was paused before compaction", async () => {
238 + vi.useFakeTimers();
239 + try {
240 + const harness = createCompactionHarness();
241 + const pausedBranch = [
243 + type: "custom",
244 + customType: GOAL_STATE_CUSTOM_TYPE,
245 + data: goal({ id: "paused", status: "paused" }),
246 + },
247 + ];
248 + harness.setBranch(pausedBranch);
249 + await emitHarness(harness, "session_tree", {
250 + oldLeafId: "old",
251 + newLeafId: "paused",
252 + });
253 + harness.sent.length = 0;
255 + await emitHarness(harness, "session_before_compact", {
256 + preparation: {},
257 + branchEntries: [],
258 + signal: new AbortController().signal,
259 + });
260 + await emitHarness(harness, "session_compact", {
261 + compactionEntry: {},
262 + fromExtension: false,
263 + });
264 + await vi.runAllTimersAsync();
266 + expect(
267 + harness.sent.map(
268 + (entry) => (entry.message as Record<string, unknown>).customType,
269 + ),
270 + ).not.toContain("goal-continuation");
271 + } finally {
272 + vi.useRealTimers();
274 + });
276 + it("does not resume after compaction when goal stopped being active", async () => {
277 + vi.useFakeTimers();
278 + try {
279 + const harness = createCompactionHarness();
280 + await emitHarness(harness, "before_agent_start", {
281 + prompt:
282 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
283 + systemPrompt: "base",
284 + });
285 + const active = __goalTest.latestGoalState({
286 + sessionManager: harness.ctx.sessionManager,
287 + } as never);
288 + harness.sent.length = 0;
290 + await emitHarness(harness, "session_before_compact", {
291 + preparation: {},
292 + branchEntries: [],
293 + signal: new AbortController().signal,
294 + });
295 + harness.setBranch([
297 + type: "custom",
298 + customType: GOAL_STATE_CUSTOM_TYPE,
299 + data: goal({ id: active?.id, status: "blocked" }),
300 + },
301 + ]);
302 + await emitHarness(harness, "session_tree", {
303 + oldLeafId: "old",
304 + newLeafId: "blocked",
305 + });
306 + await emitHarness(harness, "session_compact", {
307 + compactionEntry: {},
308 + fromExtension: false,
309 + });
310 + await vi.runAllTimersAsync();
312 + expect(
313 + harness.sent.map(
314 + (entry) => (entry.message as Record<string, unknown>).customType,
315 + ),
316 + ).not.toContain("goal-continuation");
317 + } finally {
318 + vi.useRealTimers();
320 + });
322 + it("does not resume after compaction when goal moved to another session", async () => {
323 + vi.useFakeTimers();
324 + try {
325 + const harness = createCompactionHarness();
326 + await emitHarness(harness, "before_agent_start", {
327 + prompt:
328 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
329 + systemPrompt: "base",
330 + });
331 + const active = __goalTest.latestGoalState({
332 + sessionManager: harness.ctx.sessionManager,
333 + } as never);
334 + harness.sent.length = 0;
336 + await emitHarness(harness, "session_before_compact", {
337 + preparation: {},
338 + branchEntries: [],
339 + signal: new AbortController().signal,
340 + });
341 + harness.setBranch([
343 + type: "custom",
344 + customType: GOAL_STATE_CUSTOM_TYPE,
345 + data: goal({ id: active?.id, sessionId: "other-session" }),
346 + },
347 + ]);
348 + await emitHarness(harness, "session_tree", {
349 + oldLeafId: "old",
350 + newLeafId: "other",
351 + });
352 + await emitHarness(harness, "session_compact", {
353 + compactionEntry: {},
354 + fromExtension: false,
355 + });
356 + await vi.runAllTimersAsync();
358 + expect(
359 + harness.sent.map(
360 + (entry) => (entry.message as Record<string, unknown>).customType,
361 + ),
362 + ).not.toContain("goal-continuation");
363 + } finally {
364 + vi.useRealTimers();
366 + });
368 + it("does not resume after failed compaction without session_compact", async () => {
369 + vi.useFakeTimers();
370 + try {
371 + const harness = createCompactionHarness();
372 + await emitHarness(harness, "before_agent_start", {
373 + prompt:
374 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
375 + systemPrompt: "base",
376 + });
377 + harness.sent.length = 0;
379 + await emitHarness(harness, "session_before_compact", {
380 + preparation: {},
381 + branchEntries: [],
382 + signal: new AbortController().signal,
383 + });
384 + await vi.runAllTimersAsync();
386 + expect(
387 + harness.sent.map(
388 + (entry) => (entry.message as Record<string, unknown>).customType,
389 + ),
390 + ).not.toContain("goal-continuation");
391 + } finally {
392 + vi.useRealTimers();
394 + });
205 396 it("extracts text content from strings and text parts", () => {
206 397 expect(__goalTest.textFromContent("plain")).toBe("plain");
207 398 expect(

extensions/goal/index.ts

Modified
156 156 queued: boolean;
157 157 scheduled: boolean;
158 158 compacting: boolean;
159 + resumeAfterCompaction?: { goalId: string; turn: number };
161 162 type ProbeState = {
2680 2681 continuation.queued = false;
2681 2682 continuation.scheduled = false;
2682 2683 continuation.compacting = false;
2684 + continuation.resumeAfterCompaction = undefined;
2683 2685 removeTerminalInput?.();
2684 2686 removeTerminalInput = ctx.hasUI
2685 2687 ? ctx.ui.onTerminalInput((data) => {
2706 2708 continuation.queued = false;
2707 2709 continuation.scheduled = false;
2708 2710 continuation.compacting = false;
2711 + continuation.resumeAfterCompaction = undefined;
2709 2712 renderGoalUi(ctx, state);
2710 2713 syncUpdateGoalTool();
2797 2800 continuation.queued = false;
2798 2801 continuation.scheduled = false;
2799 2802 continuation.compacting = true;
2803 + continuation.resumeAfterCompaction = undefined;
2800 2804 removeTerminalInput?.();
2801 2805 removeTerminalInput = undefined;
2802 2806 renderGoalUi(ctx, undefined);
2803 2807 setToolActive(pi, "update_goal", false);
2806 - pi.on("session_before_compact", () => {
2810 + pi.on("session_before_compact", (_event, ctx) => {
2811 + continuation.resumeAfterCompaction =
2812 + state && state.status === "active" && ownsGoal(ctx, state)
2813 + ? { goalId: state.id, turn: state.turnsUsed + 1 }
2814 + : undefined;
2807 2815 continuation.compacting = true;
2808 2816 continuation.queued = false;
2809 2817 continuation.scheduled = false;
2812 - pi.on("session_compact", () => {
2820 + pi.on("session_compact", (_event, ctx) => {
2813 2821 continuation.compacting = false;
2814 2822 pi.appendEntry(GOAL_COMPACTION_ENTRY, {
2815 2823 event: "after",
2816 2824 summary: goalSummary(state, continuation),
2817 2825 timestamp: Date.now(),
2827 + const resume = continuation.resumeAfterCompaction;
2828 + continuation.resumeAfterCompaction = undefined;
2829 + state = latestGoalState(ctx);
2830 + if (
2831 + resume &&
2832 + state?.id === resume.goalId &&
2833 + state.status === "active" &&
2834 + state.turnsUsed + 1 === resume.turn &&
2835 + ownsGoal(ctx, state)
2836 + ) {
2837 + scheduleContinuation(pi, ctx, state, continuation);
2821 2841 pi.on("agent_end", async (_event, ctx) => {