@eduardo191labs/opencode-free-pi 0.7.14 → 0.7.16

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.
package/dist/plugin.js CHANGED
@@ -216,6 +216,10 @@ async function setup(ctx) {
216
216
  tokenFromCredential(credential) ?? (await readCredentialsToken());
217
217
  await registerIntegration(ctx, deps);
218
218
  await ctx.provider.transform((editor) => {
219
+ // Re-index on every transform: a learned context/output ceiling changes the
220
+ // built model, and the diet and maxTokens cap read these maps. Refreshing
221
+ // only at setup would keep budgeting against the old window after a reload.
222
+ indexModelState();
219
223
  applyProvider(editor, PROVIDER_ID, "Free Pi", buildMain(), state.providerToken);
220
224
  if (includeDefense && currentDefense.length > 0) {
221
225
  applyProvider(editor, DEFENSE_PROVIDER_ID, "Free Pi Defense", buildDefense(), state.providerToken);
@@ -5,7 +5,9 @@ import { tokenFromCredential } from "../auth/credential.js";
5
5
  import { handleFreePiError } from "../errors/handle.js";
6
6
  import { parseFreePiError } from "../errors/parse.js";
7
7
  import { noteStepTokens } from "../catalog/live.js";
8
+ import { SESSION_TRACK_LIMIT } from "../constants.js";
8
9
  import { forgetToolStats, isFreePiSession } from "./tools.js";
10
+ import { trimSet } from "../utils/async.js";
9
11
  import { beginExecution, cancelAllFinalizeTimers, endExecution, finalizeFreePiSession, forgetFreePiSession, } from "./track.js";
10
12
  import { markWireSessionDirty, } from "./wire.js";
11
13
  import { recordUsage } from "./usage.js";
@@ -69,6 +71,9 @@ async function handleEvent(ctx, deps, event) {
69
71
  return;
70
72
  if (typeof event.data?.parentID === "string") {
71
73
  deps.state.subagentSessions.add(sessionID);
74
+ // Bound the set: a child whose deletion event is missed (crash, restart)
75
+ // would otherwise grow it forever and keep marking requests as children.
76
+ trimSet(deps.state.subagentSessions, SESSION_TRACK_LIMIT);
72
77
  }
73
78
  return;
74
79
  }
@@ -134,6 +139,7 @@ async function handleEvent(ctx, deps, event) {
134
139
  forgetFreePiSession(deps, sessionID);
135
140
  forgetToolStats(deps, sessionID);
136
141
  deps.state.subagentSessions.delete(sessionID);
142
+ deps.state.lastModelBySession.delete(sessionID);
137
143
  return;
138
144
  }
139
145
  case "location.shutdown": {
@@ -115,8 +115,13 @@ function applyRecentWindow(deps, event, window) {
115
115
  part.text.startsWith(RECENT_WINDOW_MARKER))
116
116
  : false);
117
117
  if (!already) {
118
- messages.push({ role: "user", content: [{ type: "text", text: guidance }] });
119
- event.messages = messages;
118
+ // A fresh array: OpenCode reuses the event across hooks, and appending to
119
+ // the shared array would carry this block into the stored conversation and
120
+ // the next, unrelated request.
121
+ event.messages = [
122
+ ...messages,
123
+ { role: "user", content: [{ type: "text", text: guidance }] },
124
+ ];
120
125
  }
121
126
  // Keep the same text for the next request: if the summary compressed the
122
127
  // newest turns anyway, the agent still sees what it was doing.
@@ -393,9 +398,11 @@ async function applyModelRequest(deps, event) {
393
398
  event.kind === "compaction"
394
399
  ? AD_CONTEXT_OPT_OUT
395
400
  : (turn.context ?? AD_CONTEXT_OPT_OUT);
396
- if (token) {
401
+ if (token && event.kind !== "compaction") {
397
402
  // V1 awaited the first ad fetch for the turn so the impression lands
398
403
  // before the model request. Later steps reuse the settled promise.
404
+ // Compaction opts out of the ad context, so it must not fetch or record a
405
+ // new impression for a request the user never sees.
399
406
  await ensureAdImpression(deps, token, event.sessionID, turn).catch(() => undefined);
400
407
  }
401
408
  }
@@ -163,6 +163,10 @@ export function noteSpuriousOverflow(deps, sessionID) {
163
163
  const now = Date.now();
164
164
  const previous = deps.state.spuriousOverflows.get(sessionID);
165
165
  const count = previous && now - previous.at <= STORM_WINDOW_MS ? previous.count + 1 : 1;
166
+ // Delete first so an existing session moves to the end of the insertion
167
+ // order: `Map.set` on a present key keeps its old position, and the trim
168
+ // below would otherwise evict a session that is still being rejected.
169
+ deps.state.spuriousOverflows.delete(sessionID);
166
170
  deps.state.spuriousOverflows.set(sessionID, { at: now, count });
167
171
  if (deps.state.spuriousOverflows.size > STORM_LIMIT_PER_SESSION) {
168
172
  const oldest = deps.state.spuriousOverflows.keys().next().value;
@@ -101,10 +101,18 @@ export function forgetFreePiSession(deps, sessionID) {
101
101
  if (!sessionID)
102
102
  return;
103
103
  cancelFinalizeTimer(deps, sessionID);
104
+ // A session evicted from the bounded tracking set never reached
105
+ // `finalizeFreePiSession`, so its server slot would stay open until the
106
+ // gateway times it out. Release it here unless the finalize already did.
107
+ const released = deps.state.finalizedSessions.has(sessionID);
108
+ const wireID = wireSessionID(deps, sessionID);
104
109
  deps.state.freePiSessions.delete(sessionID);
105
110
  deps.state.finalizedSessions.delete(sessionID);
106
111
  deps.state.activeExecutions.delete(sessionID);
107
112
  deps.state.sessionActivity.delete(sessionID);
108
113
  deps.state.trackedModels.delete(sessionID);
109
114
  forgetWireSessionID(deps, sessionID);
115
+ if (!released) {
116
+ void closeFreePiSession(deps, wireID).catch(() => undefined);
117
+ }
110
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.7.14",
3
+ "version": "0.7.16",
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",
@@ -24,7 +24,7 @@
24
24
  "scripts": {
25
25
  "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
26
26
  "typecheck": "tsc -p tsconfig.json --noEmit",
27
- "test": "npm run build && node test/catalog.mjs && node test/compaction.mjs && node test/diet.mjs && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/live.mjs && node test/hooks.mjs && node test/title.mjs && node test/errors.mjs && node test/vision.mjs && node test/agents.mjs && node test/wire.mjs && node test/network.mjs && node test/tools.mjs",
27
+ "test": "npm run build && node test/catalog.mjs && node test/compaction.mjs && node test/diet.mjs && node test/smoke.mjs && node test/registry.mjs && node test/models.mjs && node test/live.mjs && node test/hooks.mjs && node test/title.mjs && node test/errors.mjs && node test/overflow.mjs && node test/vision.mjs && node test/agents.mjs && node test/wire.mjs && node test/network.mjs && node test/tools.mjs",
28
28
  "prepublishOnly": "npm run build",
29
29
  "postinstall": "node scripts/postinstall.mjs"
30
30
  },
package/src/plugin.ts CHANGED
@@ -266,6 +266,10 @@ async function setup(ctx: Plugin.Context): Promise<() => Promise<void>> {
266
266
  await registerIntegration(ctx, deps);
267
267
 
268
268
  await ctx.provider.transform((editor) => {
269
+ // Re-index on every transform: a learned context/output ceiling changes the
270
+ // built model, and the diet and maxTokens cap read these maps. Refreshing
271
+ // only at setup would keep budgeting against the old window after a reload.
272
+ indexModelState();
269
273
  applyProvider(
270
274
  editor,
271
275
  PROVIDER_ID,
@@ -8,7 +8,9 @@ import type { Deps } from "../deps.js";
8
8
  import { handleFreePiError } from "../errors/handle.js";
9
9
  import { parseFreePiError } from "../errors/parse.js";
10
10
  import { noteStepTokens } from "../catalog/live.js";
11
+ import { SESSION_TRACK_LIMIT } from "../constants.js";
11
12
  import { forgetToolStats, isFreePiSession } from "./tools.js";
13
+ import { trimSet } from "../utils/async.js";
12
14
  import {
13
15
  beginExecution,
14
16
  cancelAllFinalizeTimers,
@@ -90,6 +92,9 @@ async function handleEvent(
90
92
  if (typeof sessionID !== "string") return;
91
93
  if (typeof event.data?.parentID === "string") {
92
94
  deps.state.subagentSessions.add(sessionID);
95
+ // Bound the set: a child whose deletion event is missed (crash, restart)
96
+ // would otherwise grow it forever and keep marking requests as children.
97
+ trimSet(deps.state.subagentSessions, SESSION_TRACK_LIMIT);
93
98
  }
94
99
  return;
95
100
  }
@@ -158,6 +163,7 @@ async function handleEvent(
158
163
  forgetFreePiSession(deps, sessionID);
159
164
  forgetToolStats(deps, sessionID);
160
165
  deps.state.subagentSessions.delete(sessionID);
166
+ deps.state.lastModelBySession.delete(sessionID);
161
167
  return;
162
168
  }
163
169
  case "location.shutdown": {
@@ -233,8 +233,13 @@ function applyRecentWindow(
233
233
  : false,
234
234
  );
235
235
  if (!already) {
236
- messages.push({ role: "user", content: [{ type: "text", text: guidance }] });
237
- event.messages = messages;
236
+ // A fresh array: OpenCode reuses the event across hooks, and appending to
237
+ // the shared array would carry this block into the stored conversation and
238
+ // the next, unrelated request.
239
+ event.messages = [
240
+ ...messages,
241
+ { role: "user", content: [{ type: "text", text: guidance }] },
242
+ ];
238
243
  }
239
244
  // Keep the same text for the next request: if the summary compressed the
240
245
  // newest turns anyway, the agent still sees what it was doing.
@@ -549,9 +554,11 @@ async function applyModelRequest(deps: Deps, event: any): Promise<void> {
549
554
  event.kind === "compaction"
550
555
  ? AD_CONTEXT_OPT_OUT
551
556
  : (turn.context ?? AD_CONTEXT_OPT_OUT);
552
- if (token) {
557
+ if (token && event.kind !== "compaction") {
553
558
  // V1 awaited the first ad fetch for the turn so the impression lands
554
559
  // before the model request. Later steps reuse the settled promise.
560
+ // Compaction opts out of the ad context, so it must not fetch or record a
561
+ // new impression for a request the user never sees.
555
562
  await ensureAdImpression(deps, token, event.sessionID, turn).catch(
556
563
  () => undefined,
557
564
  );
@@ -187,6 +187,10 @@ export function noteSpuriousOverflow(deps: Deps, sessionID: string): number {
187
187
  const previous = deps.state.spuriousOverflows.get(sessionID);
188
188
  const count =
189
189
  previous && now - previous.at <= STORM_WINDOW_MS ? previous.count + 1 : 1;
190
+ // Delete first so an existing session moves to the end of the insertion
191
+ // order: `Map.set` on a present key keeps its old position, and the trim
192
+ // below would otherwise evict a session that is still being rejected.
193
+ deps.state.spuriousOverflows.delete(sessionID);
190
194
  deps.state.spuriousOverflows.set(sessionID, { at: now, count });
191
195
  if (deps.state.spuriousOverflows.size > STORM_LIMIT_PER_SESSION) {
192
196
  const oldest = deps.state.spuriousOverflows.keys().next().value;
@@ -120,10 +120,18 @@ export async function finalizeFreePiSession(
120
120
  export function forgetFreePiSession(deps: Deps, sessionID: string): void {
121
121
  if (!sessionID) return;
122
122
  cancelFinalizeTimer(deps, sessionID);
123
+ // A session evicted from the bounded tracking set never reached
124
+ // `finalizeFreePiSession`, so its server slot would stay open until the
125
+ // gateway times it out. Release it here unless the finalize already did.
126
+ const released = deps.state.finalizedSessions.has(sessionID);
127
+ const wireID = wireSessionID(deps, sessionID);
123
128
  deps.state.freePiSessions.delete(sessionID);
124
129
  deps.state.finalizedSessions.delete(sessionID);
125
130
  deps.state.activeExecutions.delete(sessionID);
126
131
  deps.state.sessionActivity.delete(sessionID);
127
132
  deps.state.trackedModels.delete(sessionID);
128
133
  forgetWireSessionID(deps, sessionID);
134
+ if (!released) {
135
+ void closeFreePiSession(deps, wireID).catch(() => undefined);
136
+ }
129
137
  }