@eduardo191labs/opencode-free-pi 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -211,6 +211,20 @@ export function settleWindow(samples) {
211
211
  return undefined;
212
212
  return Math.min(MAX_WINDOW, Math.max(MIN_WINDOW, Math.floor((best * WINDOW_SAFETY) / 1_000) * 1_000));
213
213
  }
214
+ /**
215
+ * Stores a measured window and writes it immediately. Called after every
216
+ * accepted probe, so a process that exits mid-verification keeps the smallest
217
+ * proven value instead of losing the whole run.
218
+ */
219
+ async function commitWindow(deps, tokens, modelID) {
220
+ const limits = (cached ??= emptyLimits());
221
+ if (limits.window && limits.window.tokens >= tokens)
222
+ return limits.window.tokens;
223
+ limits.window = { tokens, verifiedAt: Date.now(), model: modelID };
224
+ loaded = true;
225
+ await persist();
226
+ return tokens;
227
+ }
214
228
  /**
215
229
  * Checks the compiled window against the gateway and then grows past it while
216
230
  * the gateway keeps accepting larger prompts. The start value is a floor, not a
@@ -231,6 +245,8 @@ export async function verifyGatewayWindow(deps, options = {}) {
231
245
  let accepted = 0;
232
246
  let charsPerToken = INITIAL_CHARS_PER_TOKEN;
233
247
  let target = Math.max(MIN_WINDOW, Math.min(fallback, MAX_WINDOW));
248
+ let verified;
249
+ const floor = Math.min(fallback, CONTEXT_WINDOW);
234
250
  for (let attempt = 0; attempt < MAX_PROBES; attempt += 1) {
235
251
  const chars = Math.max(MIN_WINDOW * charsPerToken, Math.floor(target * charsPerToken * 0.99));
236
252
  const result = await sendProbe(deps, modelID, chars);
@@ -238,7 +254,7 @@ export async function verifyGatewayWindow(deps, options = {}) {
238
254
  // A network failure or a rejected body proves nothing; keep what the
239
255
  // earlier probes already established.
240
256
  if (samples.length === 0)
241
- return undefined;
257
+ return verified;
242
258
  break;
243
259
  }
244
260
  if (result.status === "too_long") {
@@ -247,7 +263,7 @@ export async function verifyGatewayWindow(deps, options = {}) {
247
263
  // The compiled window itself is too large: step down as before.
248
264
  target = Math.floor(target * 0.85);
249
265
  if (target < MIN_WINDOW)
250
- return undefined;
266
+ break;
251
267
  continue;
252
268
  }
253
269
  const next = Math.floor((accepted + target) / 2);
@@ -271,6 +287,11 @@ export async function verifyGatewayWindow(deps, options = {}) {
271
287
  else {
272
288
  accepted = Math.floor(chars / charsPerToken);
273
289
  }
290
+ const settled = settleWindow(samples);
291
+ if (settled) {
292
+ // Never go below the window that already worked for this account.
293
+ verified = await commitWindow(deps, Math.max(settled, floor), modelID);
294
+ }
274
295
  if (accepted >= MAX_WINDOW)
275
296
  break;
276
297
  const next = Math.min(MAX_WINDOW, Math.floor(accepted * 1.2));
@@ -278,17 +299,8 @@ export async function verifyGatewayWindow(deps, options = {}) {
278
299
  break;
279
300
  target = next;
280
301
  }
281
- const settled = settleWindow(samples);
282
- if (!settled)
302
+ if (!verified)
283
303
  return undefined;
284
- // Keep the previous floor when growth probes only prove less: the CLI's 200k
285
- // is already known to work, so a small upstream probe model must not shrink
286
- // every session's window.
287
- const verified = Math.max(settled, Math.min(fallback, CONTEXT_WINDOW));
288
- const limits = (cached ??= emptyLimits());
289
- limits.window = { tokens: verified, verifiedAt: Date.now(), model: modelID };
290
- loaded = true;
291
- await persist();
292
304
  deps.log.info("free-pi verified the gateway context window", {
293
305
  model: modelID,
294
306
  window: verified,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Ad-supported free-pi provider plugin for OpenCode on Windows, Linux, macOS and Termux",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -269,6 +269,24 @@ export function settleWindow(
269
269
  );
270
270
  }
271
271
 
272
+ /**
273
+ * Stores a measured window and writes it immediately. Called after every
274
+ * accepted probe, so a process that exits mid-verification keeps the smallest
275
+ * proven value instead of losing the whole run.
276
+ */
277
+ async function commitWindow(
278
+ deps: Deps,
279
+ tokens: number,
280
+ modelID: string,
281
+ ): Promise<number> {
282
+ const limits = (cached ??= emptyLimits());
283
+ if (limits.window && limits.window.tokens >= tokens) return limits.window.tokens;
284
+ limits.window = { tokens, verifiedAt: Date.now(), model: modelID };
285
+ loaded = true;
286
+ await persist();
287
+ return tokens;
288
+ }
289
+
272
290
  /**
273
291
  * Checks the compiled window against the gateway and then grows past it while
274
292
  * the gateway keeps accepting larger prompts. The start value is a floor, not a
@@ -292,6 +310,8 @@ export async function verifyGatewayWindow(
292
310
  let accepted = 0;
293
311
  let charsPerToken = INITIAL_CHARS_PER_TOKEN;
294
312
  let target = Math.max(MIN_WINDOW, Math.min(fallback, MAX_WINDOW));
313
+ let verified: number | undefined;
314
+ const floor = Math.min(fallback, CONTEXT_WINDOW);
295
315
 
296
316
  for (let attempt = 0; attempt < MAX_PROBES; attempt += 1) {
297
317
  const chars = Math.max(
@@ -302,7 +322,7 @@ export async function verifyGatewayWindow(
302
322
  if (result.status === "failed") {
303
323
  // A network failure or a rejected body proves nothing; keep what the
304
324
  // earlier probes already established.
305
- if (samples.length === 0) return undefined;
325
+ if (samples.length === 0) return verified;
306
326
  break;
307
327
  }
308
328
  if (result.status === "too_long") {
@@ -310,7 +330,7 @@ export async function verifyGatewayWindow(
310
330
  if (accepted === 0) {
311
331
  // The compiled window itself is too large: step down as before.
312
332
  target = Math.floor(target * 0.85);
313
- if (target < MIN_WINDOW) return undefined;
333
+ if (target < MIN_WINDOW) break;
314
334
  continue;
315
335
  }
316
336
  const next = Math.floor((accepted + target) / 2);
@@ -333,22 +353,18 @@ export async function verifyGatewayWindow(
333
353
  } else {
334
354
  accepted = Math.floor(chars / charsPerToken);
335
355
  }
356
+ const settled = settleWindow(samples);
357
+ if (settled) {
358
+ // Never go below the window that already worked for this account.
359
+ verified = await commitWindow(deps, Math.max(settled, floor), modelID);
360
+ }
336
361
  if (accepted >= MAX_WINDOW) break;
337
362
  const next = Math.min(MAX_WINDOW, Math.floor(accepted * 1.2));
338
363
  if (next <= accepted) break;
339
364
  target = next;
340
365
  }
341
366
 
342
- const settled = settleWindow(samples);
343
- if (!settled) return undefined;
344
- // Keep the previous floor when growth probes only prove less: the CLI's 200k
345
- // is already known to work, so a small upstream probe model must not shrink
346
- // every session's window.
347
- const verified = Math.max(settled, Math.min(fallback, CONTEXT_WINDOW));
348
- const limits = (cached ??= emptyLimits());
349
- limits.window = { tokens: verified, verifiedAt: Date.now(), model: modelID };
350
- loaded = true;
351
- await persist();
367
+ if (!verified) return undefined;
352
368
  deps.log.info("free-pi verified the gateway context window", {
353
369
  model: modelID,
354
370
  window: verified,