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

Function resolvePiPressureContextLimit

packages/pi-plugin/src/index.ts:226–250  ·  view source on GitHub ↗
(args: {
	db: ContextDatabase;
	sessionId: string;
	piContextWindow: number;
})

Source from the content-addressed store, hash-verified

224}
225
226function resolvePiPressureContextLimit(args: {
227 db: ContextDatabase;
228 sessionId: string;
229 piContextWindow: number;
230}): number {
231 // Pi reports the model's context window directly (ctx.getContextUsage() /
232 // ctx.getModel().contextWindow) — its own authoritative source. We no longer
233 // consult models.dev for Pi. Sanity-bound the reported value so a transient
234 // garbage window can't poison pressure (mirrors OpenCode's SDK sane bound).
235 let effectiveContextLimit = isSaneLimit(args.piContextWindow)
236 ? args.piContextWindow
237 : 0;
238 try {
239 const overflowState = getOverflowState(args.db, args.sessionId);
240 if (overflowState.detectedContextLimit > 0) {
241 effectiveContextLimit =
242 effectiveContextLimit > 0
243 ? Math.min(effectiveContextLimit, overflowState.detectedContextLimit)
244 : overflowState.detectedContextLimit;
245 }
246 } catch (err) {
247 warn("message_end: getOverflowState failed:", err);
248 }
249 return effectiveContextLimit;
250}
251
252export async function persistPiPressureFromMessageEnd(args: {
253 db: ContextDatabase;

Callers 1

Calls 3

isSaneLimitFunction · 0.90
getOverflowStateFunction · 0.90
warnFunction · 0.70

Tested by

no test coverage detected