Skip to content

Commit 0bf53f1

Browse files
committed
mason: fix ctx-wrapup review findings
1 parent ebd5b2c commit 0bf53f1

14 files changed

Lines changed: 777 additions & 190 deletions

‎packages/pi-plugin/src/commands/ctx-wrapup.test.ts‎

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ import {
88
} from "@magic-context/core/features/magic-context/compartment-storage";
99
import { runMigrations } from "@magic-context/core/features/magic-context/migrations";
1010
import { initializeDatabase } from "@magic-context/core/features/magic-context/storage-db";
11-
import { setPendingPiCompactionMarkerState } from "@magic-context/core/features/magic-context/storage-meta-persisted";
11+
import {
12+
getPendingPiCompactionMarkerState,
13+
getWrapupInProgressState,
14+
setPendingPiCompactionMarkerState,
15+
} from "@magic-context/core/features/magic-context/storage-meta-persisted";
1216
import { Database } from "@magic-context/core/shared/sqlite";
1317
import { closeQuietly } from "@magic-context/core/shared/sqlite-helpers";
18+
import {
19+
consumeDeferredHistoryRefresh,
20+
consumeDeferredMaterialization,
21+
} from "../context-handler";
1422
import {
1523
parseWrapupArgs,
1624
type RegisterCtxWrapupDeps,
@@ -93,14 +101,15 @@ function deps(
93101
historianChunkTokens: 10,
94102
memoryEnabled: false,
95103
autoPromote: false,
96-
runPiHistorianForWrapup: mock(async () => {
104+
runPiHistorianForWrapup: mock(async (args) => {
97105
const sessionId = args.sessionId;
98106
const start = getLastCompartmentEndMessage(db, sessionId) + 1;
99107
const end = Math.min(
100108
args.boundarySnapshot.eligibleEndOrdinal - 1,
101109
start + 2,
102110
);
103111
appendRange(db, sessionId, start, end);
112+
args.onPublished?.();
104113
}),
105114
...overrides,
106115
};
@@ -142,14 +151,84 @@ describe("Pi /ctx-wrapup", () => {
142151
}
143152
});
144153

154+
it("signals deferred history and materialization after a wrapup publish", async () => {
155+
const db = createDb();
156+
try {
157+
const sessionId = "pi-wrapup-signals";
158+
const result = await runPiWrapup(
159+
pi().api,
160+
deps(db, {
161+
runPiHistorianForWrapup: mock(async (args) => {
162+
appendRange(db, sessionId, 1, 3);
163+
args.onPublished?.();
164+
}),
165+
}),
166+
ctx(sessionId, 8),
167+
sessionId,
168+
2,
169+
);
170+
171+
expect(result).toContain("## Magic Wrapup");
172+
expect(consumeDeferredHistoryRefresh(sessionId)).toBe(true);
173+
expect(consumeDeferredMaterialization(sessionId)).toBe(true);
174+
} finally {
175+
closeQuietly(db);
176+
}
177+
});
178+
179+
it("aborts when wrapup marker ownership is lost and leaves the foreign marker", async () => {
180+
const db = createDb();
181+
try {
182+
const sessionId = "pi-ownership-lost";
183+
let calls = 0;
184+
const result = await runPiWrapup(
185+
pi().api,
186+
deps(db, {
187+
runPiHistorianForWrapup: mock(async (args) => {
188+
calls += 1;
189+
appendRange(db, sessionId, 1, 3);
190+
args.onPublished?.();
191+
const state = getWrapupInProgressState(db, sessionId);
192+
expect(state).not.toBeNull();
193+
db.prepare(
194+
"UPDATE session_meta SET wrapup_in_progress_state = ? WHERE session_id = ?",
195+
).run(
196+
JSON.stringify({
197+
...state,
198+
holderId: "foreign-holder",
199+
updatedAt: Date.now(),
200+
expiresAt: Date.now() + 60_000,
201+
}),
202+
sessionId,
203+
);
204+
}),
205+
}),
206+
ctx(sessionId, 10),
207+
sessionId,
208+
2,
209+
);
210+
211+
expect(result).toContain("## Magic Wrapup — Partial");
212+
expect(result).toContain(
213+
"another process took over this session's wrapup",
214+
);
215+
expect(calls).toBe(1);
216+
expect(getWrapupInProgressState(db, sessionId)?.holderId).toBe(
217+
"foreign-holder",
218+
);
219+
} finally {
220+
closeQuietly(db);
221+
}
222+
});
223+
145224
it("stops when the pending Pi marker cannot be drained after a chunk", async () => {
146225
const db = createDb();
147226
try {
148227
const sessionId = "pi-marker-pending";
149228
const result = await runPiWrapup(
150229
pi().api,
151230
deps(db, {
152-
runPiHistorianForWrapup: mock(async () => {
231+
runPiHistorianForWrapup: mock(async (args) => {
153232
appendRange(db, sessionId, 1, 3);
154233
setPendingPiCompactionMarkerState(db, sessionId, {
155234
firstKeptEntryId: "m-4",
@@ -159,6 +238,7 @@ describe("Pi /ctx-wrapup", () => {
159238
summary: "pending marker",
160239
publishedAt: Date.now(),
161240
});
241+
args.onPublished?.();
162242
}),
163243
}),
164244
ctx(sessionId, 8),
@@ -172,6 +252,11 @@ describe("Pi /ctx-wrapup", () => {
172252
);
173253
expect(result).toContain("appendCompaction unavailable");
174254
expect(getLastCompartmentEndMessage(db, sessionId)).toBe(3);
255+
expect(getPendingPiCompactionMarkerState(db, sessionId)).toEqual(
256+
expect.objectContaining({ ordinal: 3, endMessageId: "m-3" }),
257+
);
258+
expect(consumeDeferredHistoryRefresh(sessionId)).toBe(true);
259+
expect(consumeDeferredMaterialization(sessionId)).toBe(true);
175260
} finally {
176261
closeQuietly(db);
177262
}

‎packages/pi-plugin/src/commands/ctx-wrapup.ts‎

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import { resolveWrapupProtectedTailBoundary } from "@magic-context/core/hooks/ma
2828
import { setRawMessageProvider } from "@magic-context/core/hooks/magic-context/read-session-chunk";
2929
import type { SubagentRunner } from "@magic-context/core/shared/subagent-runner";
3030
import { applyDeferredPiCompactionMarker } from "../compaction-marker-manager-pi";
31+
import {
32+
signalPiDeferredHistoryRefresh,
33+
signalPiDeferredMaterialization,
34+
} from "../context-handler";
3135
import { ensureProjectRegisteredFromPiDirectory } from "../embedding-bootstrap";
3236
import { runPiHistorian } from "../pi-historian-runner";
3337
import { isPiRecompInFlight } from "../pi-recomp-runner";
@@ -201,8 +205,32 @@ export async function runPiWrapup(
201205
);
202206
}
203207

208+
let ownershipLost = false;
209+
const ownershipLostReason =
210+
"another process took over this session's wrapup";
211+
const markOwnershipLost = (): void => {
212+
if (ownershipLost) return;
213+
ownershipLost = true;
214+
};
215+
const renewWrapupMarker = (
216+
updates: Parameters<typeof updateWrapupInProgress>[3],
217+
): boolean => {
218+
const updated = updateWrapupInProgress(
219+
deps.db,
220+
sessionId,
221+
holderId,
222+
updates,
223+
);
224+
if (!updated) {
225+
markOwnershipLost();
226+
return false;
227+
}
228+
return true;
229+
};
204230
const renewal = setInterval(() => {
205-
updateWrapupInProgress(deps.db, sessionId, holderId, {});
231+
renewWrapupMarker({
232+
lastCompartmentEnd: getLastCompartmentEndMessage(deps.db, sessionId),
233+
});
206234
}, 60_000);
207235
try {
208236
sendCtxStatusMessage(pi, {
@@ -218,6 +246,23 @@ export async function runPiWrapup(
218246
let failure: string | null = null;
219247

220248
for (;;) {
249+
if (ownershipLost) {
250+
failure = `${ownershipLostReason}; wrapped up through message ${lastEnd}. Run /ctx-wrapup again to continue.`;
251+
break;
252+
}
253+
if (
254+
!renewWrapupMarker({
255+
chunkIndex,
256+
lastCompartmentEnd: getLastCompartmentEndMessage(
257+
deps.db,
258+
sessionId,
259+
),
260+
})
261+
) {
262+
failure = `${ownershipLostReason}; wrapped up through message ${lastEnd}. Run /ctx-wrapup again to continue.`;
263+
break;
264+
}
265+
221266
const plan = resolveWrapupProtectedTailBoundary({
222267
db: deps.db,
223268
sessionId,
@@ -234,18 +279,26 @@ export async function runPiWrapup(
234279
if (plan.snapshot.eligibleEndOrdinal <= plan.snapshot.offset) break;
235280

236281
chunkIndex += 1;
237-
updateWrapupInProgress(deps.db, sessionId, holderId, {
238-
chunkIndex,
239-
lastCompartmentEnd: getLastCompartmentEndMessage(deps.db, sessionId),
240-
targetEligibleEndOrdinal: plan.targetEligibleEndOrdinal,
241-
expectedChunks: Math.max(
282+
if (
283+
!renewWrapupMarker({
242284
chunkIndex,
243-
estimateChunks(
244-
plan.snapshot.trueRawEligibleTokens,
245-
deps.historianChunkTokens,
285+
lastCompartmentEnd: getLastCompartmentEndMessage(
286+
deps.db,
287+
sessionId,
246288
),
247-
),
248-
});
289+
targetEligibleEndOrdinal: plan.targetEligibleEndOrdinal,
290+
expectedChunks: Math.max(
291+
chunkIndex,
292+
estimateChunks(
293+
plan.snapshot.trueRawEligibleTokens,
294+
deps.historianChunkTokens,
295+
),
296+
),
297+
})
298+
) {
299+
failure = `${ownershipLostReason}; wrapped up through message ${lastEnd}. Run /ctx-wrapup again to continue.`;
300+
break;
301+
}
249302
sendCtxStatusMessage(pi, {
250303
title: "/ctx-wrapup",
251304
text: `## Magic Wrapup\n\nChunk ${chunkIndex}: wrapping messages ${plan.snapshot.offset}-${plan.snapshot.eligibleEndOrdinal - 1} (~${plan.snapshot.trueRawEligibleTokens.toLocaleString()} eligible tokens remain).`,
@@ -255,8 +308,14 @@ export async function runPiWrapup(
255308
const leaseHolder = await acquireCompartmentLeaseEventually(
256309
deps.db,
257310
sessionId,
258-
holderId,
311+
renewWrapupMarker,
259312
);
313+
if (!leaseHolder) {
314+
failure = ownershipLost
315+
? `${ownershipLostReason}; wrapped up through message ${lastEnd}. Run /ctx-wrapup again to continue.`
316+
: "Another Magic Context rebuild started while wrapup was waiting. Run /ctx-wrapup again to continue.";
317+
break;
318+
}
260319
const leaseRenewal = setInterval(() => {
261320
renewCompartmentLease(deps.db, sessionId, leaseHolder);
262321
}, COMPARTMENT_LEASE_RENEWAL_MS);
@@ -303,9 +362,14 @@ export async function runPiWrapup(
303362
}),
304363
ensureProjectRegistered: ensureProjectRegisteredFromPiDirectory,
305364
forceDrainQuota: true,
306-
forceKeepLastCompartment: plan.finalChunk,
307-
onPublished: () =>
308-
updateStatusLine(ctx, { db: deps.db, projectIdentity: ctx.cwd }),
365+
// The runner applies this only to the actual final chunk; token-capped
366+
// chunks are downgraded based on readSessionChunk().hasMore.
367+
forceKeepLastCompartment: true,
368+
onPublished: () => {
369+
updateStatusLine(ctx, { db: deps.db, projectIdentity: ctx.cwd });
370+
signalPiDeferredHistoryRefresh(sessionId);
371+
signalPiDeferredMaterialization(sessionId);
372+
},
309373
});
310374
} finally {
311375
clearInterval(leaseRenewal);
@@ -369,13 +433,15 @@ function resolvePiContextLimit(ctx: ExtensionCommandContext): number {
369433
async function acquireCompartmentLeaseEventually(
370434
db: ContextDatabase,
371435
sessionId: string,
372-
wrapupHolderId: string,
373-
): Promise<string> {
436+
renewWrapupMarker: (
437+
updates: Parameters<typeof updateWrapupInProgress>[3],
438+
) => boolean,
439+
): Promise<string | null> {
374440
for (;;) {
375441
const holderId = crypto.randomUUID();
376442
const lease = acquireCompartmentLease(db, sessionId, holderId);
377443
if (lease) return holderId;
378-
updateWrapupInProgress(db, sessionId, wrapupHolderId, {});
444+
if (!renewWrapupMarker({})) return null;
379445
await new Promise((resolve) => setTimeout(resolve, LEASE_WAIT_MS));
380446
}
381447
}

‎packages/pi-plugin/src/pi-historian-runner.test.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ function successXmlWithUserObservation(observation: string) {
8585
return `${successXml()}\n<user_observations>\n* ${observation}\n</user_observations>`;
8686
}
8787

88+
function twoCompartmentSuccessXml() {
89+
return `<compartment start="1" end="2" title="Initial Pi slice">Summarized the first Pi turn.</compartment>
90+
<compartment start="3" end="4" title="Provisional Pi slice">Summarized the provisional Pi turn.</compartment>
91+
<PROJECT_RULES>
92+
* Pi mid-loop wrapup facts must promote.
93+
</PROJECT_RULES>`;
94+
}
95+
8896
function makeBoundarySnapshot(
8997
overrides: Partial<ProtectedTailBoundarySnapshot> = {},
9098
): ProtectedTailBoundarySnapshot {
@@ -139,6 +147,8 @@ async function runHistorianWith(args: {
139147
boundarySnapshot?: ProtectedTailBoundarySnapshot;
140148
refreshBoundarySnapshot?: () => ProtectedTailBoundarySnapshot;
141149
providerMessages?: ReturnType<typeof rawMessages>;
150+
forceKeepLastCompartment?: boolean;
151+
historianChunkTokens?: number;
142152
beforeRun?: (db: ReturnType<typeof createTestDb>) => void;
143153
ensureProjectRegistered?: Parameters<
144154
typeof runPiHistorian
@@ -156,7 +166,7 @@ async function runHistorianWith(args: {
156166
provider: { readMessages: () => args.providerMessages ?? rawMessages() },
157167
runner,
158168
historianModel: "test/model",
159-
historianChunkTokens: 20_000,
169+
historianChunkTokens: args.historianChunkTokens ?? 20_000,
160170
twoPass: args.twoPass,
161171
memoryEnabled: args.memoryEnabled,
162172
autoPromote: args.autoPromote,
@@ -168,6 +178,7 @@ async function runHistorianWith(args: {
168178
refreshBoundarySnapshot: args.refreshBoundarySnapshot,
169179
compartmentLeaseHolderId: holderId,
170180
ensureProjectRegistered: args.ensureProjectRegistered,
181+
forceKeepLastCompartment: args.forceKeepLastCompartment,
171182
});
172183
return { db, runner };
173184
}
@@ -528,6 +539,55 @@ describe("runPiHistorian", () => {
528539
}
529540
});
530541

542+
it("downgrades forced final keep on token-capped chunks and skips promotion only on the actual final chunk", async () => {
543+
const projectPath = resolveProjectIdentity(process.cwd());
544+
const longMessages = rawMessages(10).map((message) => ({
545+
...message,
546+
parts: [
547+
{
548+
type: "text",
549+
text: `${message.parts[0]?.text ?? "message"} alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu alpha beta gamma delta`,
550+
},
551+
],
552+
}));
553+
const midLoop = await runHistorianWith({
554+
outputs: [twoCompartmentSuccessXml()],
555+
providerMessages: longMessages,
556+
boundarySnapshot: makeBoundarySnapshot({
557+
protectedTailStart: 7,
558+
protectedTailStartMessageId: "m7",
559+
eligibleEndOrdinal: 7,
560+
eligibleEndMessageId: "m6",
561+
}),
562+
historianChunkTokens: 100,
563+
forceKeepLastCompartment: true,
564+
});
565+
try {
566+
expect(getCompartments(midLoop.db, "ses-historian")).toHaveLength(1);
567+
expect(getCompartments(midLoop.db, "ses-historian")[0]?.endMessage).toBe(
568+
2,
569+
);
570+
expect(
571+
getMemoriesByProject(midLoop.db, projectPath).map(
572+
(memory) => memory.content,
573+
),
574+
).toContain("Pi mid-loop wrapup facts must promote.");
575+
} finally {
576+
closeQuietly(midLoop.db);
577+
}
578+
579+
const finalChunk = await runHistorianWith({
580+
outputs: [successXml("Pi final wrapup facts stay deferred.")],
581+
forceKeepLastCompartment: true,
582+
});
583+
try {
584+
expect(getCompartments(finalChunk.db, "ses-historian")).toHaveLength(1);
585+
expect(getMemoriesByProject(finalChunk.db, projectPath)).toEqual([]);
586+
} finally {
587+
closeQuietly(finalChunk.db);
588+
}
589+
});
590+
531591
it("promotes memories only when memoryEnabled and autoPromote allow it", async () => {
532592
const projectPath = resolveProjectIdentity(process.cwd());
533593
const allowed = await runHistorianWith({

0 commit comments

Comments
 (0)