(args: {
db: ContextDatabase;
sessionId: string;
piContextWindow: number;
})
| 224 | } |
| 225 | |
| 226 | function 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 | |
| 252 | export async function persistPiPressureFromMessageEnd(args: { |
| 253 | db: ContextDatabase; |
no test coverage detected