MCPcopy Create free account
hub / github.com/cortexkit/magic-context / materializeM0Pi

Function materializeM0Pi

packages/pi-plugin/src/inject-compartments-pi.ts:1322–1496  ·  view source on GitHub ↗
(
	state: PiM0M1State,
	db: ContextDatabase,
)

Source from the content-addressed store, hash-verified

1320}
1321
1322export function materializeM0Pi(
1323 state: PiM0M1State,
1324 db: ContextDatabase,
1325): {
1326 m0: string;
1327 m1: string;
1328 snapshotMarkers: PiM0SnapshotMarkers;
1329 renderedMemoryIds: number[];
1330} {
1331 // Phase 1 (no lock): read markers + render. Rendering can be slow, so we do
1332 // it OUTSIDE the write lock to keep the BEGIN IMMEDIATE critical section tiny.
1333 const docs = readProjectDocsForPiM0(state);
1334 const foldMaterializedAt = Date.now();
1335 const frozen = readFrozenM0InputsPi(state, db, docs, foldMaterializedAt);
1336 const snapshotMarkers = frozen.markers;
1337
1338 const snapshotMemories = frozen.memories;
1339 const snapshotCompartments = frozen.compartments;
1340 const snapshotUserProfile = frozen.userProfile;
1341 const renderedMemoryIds = renderedMemoryIdsForPi(
1342 state,
1343 snapshotMemories,
1344 frozen.workspace,
1345 db,
1346 );
1347 // Over-budget tightening loop (matches OpenCode materializeM0): if the
1348 // rendered m[0] exceeds the history budget, escalate the decay pressure and
1349 // re-render up to 3x so tight budgets demote more aggressively. Without this,
1350 // Pi would select different (looser) tiers than OpenCode under budget pressure.
1351 let decayPressureMultiplier = 1;
1352 let m0 = renderM0Pi(
1353 state,
1354 db,
1355 docs.renderedBlock,
1356 decayPressureMultiplier,
1357 snapshotMemories,
1358 snapshotCompartments,
1359 snapshotUserProfile,
1360 frozen.workspace,
1361 );
1362 const historyBudget =
1363 state.historyBudgetTokens ?? DEFAULT_HISTORY_BUDGET_TOKENS;
1364 let attempts = 0;
1365 while (
1366 historyBudget > 0 &&
1367 historySliceTokensPi(m0) > historyBudget * 1.05 &&
1368 attempts < 3
1369 ) {
1370 decayPressureMultiplier *= 1.15;
1371 m0 = renderM0Pi(
1372 state,
1373 db,
1374 docs.renderedBlock,
1375 decayPressureMultiplier,
1376 snapshotMemories,
1377 snapshotCompartments,
1378 snapshotUserProfile,
1379 frozen.workspace,

Callers 2

materializeM0PiWithRetryFunction · 0.85

Calls 12

persistCachedM0Function · 0.90
readProjectDocsForPiM0Function · 0.85
readFrozenM0InputsPiFunction · 0.85
renderedMemoryIdsForPiFunction · 0.85
renderM0PiFunction · 0.85
historySliceTokensPiFunction · 0.85
readCurrentMarkersFunction · 0.85
renderM1PiWithMetadataFunction · 0.85
setCachedBoundaryFunction · 0.85
runMethod · 0.65
prepareMethod · 0.45

Tested by

no test coverage detected