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)
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
15 +- Resume active goals after successful compaction while preserving paused,
16 + blocked, failed, and cross-session goal states
extensions/goal/__tests__/unit.test.ts
Modified 205 + it(
"resumes active goal after successful compaction",
async ()
=> {
206 + vi.
useFakeTimers();
208 + const harness
= createCompactionHarness();
209 + await emitHarness(harness,
"before_agent_start", {
211 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
212 + systemPrompt:
"base",
214 + harness.sent.
length = 0;
216 + await emitHarness(harness,
"session_before_compact", {
219 + signal:
new AbortController().signal,
221 + await emitHarness(harness,
"session_compact", {
222 + compactionEntry: {},
223 + fromExtension:
false,
225 + await vi.
runAllTimersAsync();
229 + (entry)
=> (entry.message
as Record<
string,
unknown>).customType,
231 + ).
toContain(
"goal-continuation");
233 + vi.
useRealTimers();
237 + it(
"does not resume after compaction when goal was paused before compaction",
async ()
=> {
238 + vi.
useFakeTimers();
240 + const harness
= createCompactionHarness();
241 + const pausedBranch
= [
244 + customType: GOAL_STATE_CUSTOM_TYPE,
245 + data:
goal({ id:
"paused", status:
"paused" }),
248 + harness.
setBranch(pausedBranch);
249 + await emitHarness(harness,
"session_tree", {
251 + newLeafId:
"paused",
253 + harness.sent.
length = 0;
255 + await emitHarness(harness,
"session_before_compact", {
258 + signal:
new AbortController().signal,
260 + await emitHarness(harness,
"session_compact", {
261 + compactionEntry: {},
262 + fromExtension:
false,
264 + await vi.
runAllTimersAsync();
268 + (entry)
=> (entry.message
as Record<
string,
unknown>).customType,
270 + ).not.
toContain(
"goal-continuation");
272 + vi.
useRealTimers();
276 + it(
"does not resume after compaction when goal stopped being active",
async ()
=> {
277 + vi.
useFakeTimers();
279 + const harness
= createCompactionHarness();
280 + await emitHarness(harness,
"before_agent_start", {
282 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
283 + systemPrompt:
"base",
285 + const active
= __goalTest.
latestGoalState({
286 + sessionManager: harness.ctx.sessionManager,
288 + harness.sent.
length = 0;
290 + await emitHarness(harness,
"session_before_compact", {
293 + signal:
new AbortController().signal,
295 + harness.
setBranch([
298 + customType: GOAL_STATE_CUSTOM_TYPE,
299 + data:
goal({ id: active?.
id, status:
"blocked" }),
302 + await emitHarness(harness,
"session_tree", {
304 + newLeafId:
"blocked",
306 + await emitHarness(harness,
"session_compact", {
307 + compactionEntry: {},
308 + fromExtension:
false,
310 + await vi.
runAllTimersAsync();
314 + (entry)
=> (entry.message
as Record<
string,
unknown>).customType,
316 + ).not.
toContain(
"goal-continuation");
318 + vi.
useRealTimers();
322 + it(
"does not resume after compaction when goal moved to another session",
async ()
=> {
323 + vi.
useFakeTimers();
325 + const harness
= createCompactionHarness();
326 + await emitHarness(harness,
"before_agent_start", {
328 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
329 + systemPrompt:
"base",
331 + const active
= __goalTest.
latestGoalState({
332 + sessionManager: harness.ctx.sessionManager,
334 + harness.sent.
length = 0;
336 + await emitHarness(harness,
"session_before_compact", {
339 + signal:
new AbortController().signal,
341 + harness.
setBranch([
344 + customType: GOAL_STATE_CUSTOM_TYPE,
345 + data:
goal({ id: active?.
id, sessionId:
"other-session" }),
348 + await emitHarness(harness,
"session_tree", {
350 + newLeafId:
"other",
352 + await emitHarness(harness,
"session_compact", {
353 + compactionEntry: {},
354 + fromExtension:
false,
356 + await vi.
runAllTimersAsync();
360 + (entry)
=> (entry.message
as Record<
string,
unknown>).customType,
362 + ).not.
toContain(
"goal-continuation");
364 + vi.
useRealTimers();
368 + it(
"does not resume after failed compaction without session_compact",
async ()
=> {
369 + vi.
useFakeTimers();
371 + const harness
= createCompactionHarness();
372 + await emitHarness(harness,
"before_agent_start", {
374 + "Start working toward this standing goal.\n\n<objective>ship it</objective>",
375 + systemPrompt:
"base",
377 + harness.sent.
length = 0;
379 + await emitHarness(harness,
"session_before_compact", {
382 + signal:
new AbortController().signal,
384 + await vi.
runAllTimersAsync();
388 + (entry)
=> (entry.message
as Record<
string,
unknown>).customType,
390 + ).not.
toContain(
"goal-continuation");
392 + vi.
useRealTimers();
205 396 it(
"extracts text content from strings and text parts", ()
=> {
206 397 expect(__goalTest.
textFromContent(
"plain")).
toBe(
"plain");
extensions/goal/index.ts
Modified 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 }
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);
2832 + state?.
id === resume.goalId
&& 2833 + state.
status === "active" && 2834 + state.turnsUsed
+ 1 === resume.turn
&& 2835 + ownsGoal(ctx, state)
2837 + scheduleContinuation(pi, ctx, state, continuation);
2821 2841 pi.
on(
"agent_end",
async (_event, ctx)
=> {