@eduardo191labs/opencode-free-pi 0.7.1 → 0.7.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.
@@ -1,5 +1,5 @@
1
1
  /** Builds and redacts the ad context sent with a user turn. */
2
- import { AD_CONTEXT_MAX_CHARS } from "../constants.js";
2
+ import { AD_CONTEXT_MAX_CHARS, TURN_REMINDER_MARKER } from "../constants.js";
3
3
  import { cleanText } from "../utils/text.js";
4
4
  const AD_SECRET_PATTERNS = [
5
5
  [
@@ -107,7 +107,9 @@ export function latestUserText(messages) {
107
107
  if (roleOf(message) !== "user")
108
108
  continue;
109
109
  const text = cleanText(typeof message.text === "string" ? message.text : contentText(message.content));
110
- if (text)
110
+ // The plugin appends this after the ad turn is resolved; ignore it if the
111
+ // event is reused so the next ad never describes the reminder itself.
112
+ if (text && !text.startsWith(TURN_REMINDER_MARKER))
111
113
  return text;
112
114
  }
113
115
  return "";
@@ -59,7 +59,7 @@ export declare const DEFAULT_THINKING_LEVELS: readonly ["none", "minimal", "low"
59
59
  * models show: skipping analysis, inventing verification, and ignoring the
60
60
  * context the conversation already established.
61
61
  */
62
- export declare const FREE_PI_GUIDANCE = "# free-pi working rules\n- Answer the user's latest request and keep their constraints; do not switch to a different task.\n- Slow down on multi-step work (arithmetic, counting, logic, dates, units, planning, debugging): write out each step, recompute the result a second way, and only then answer. On those tasks a correct answer after a dozen explicit steps beats a fast guess; do not stop at the first plausible number.\n- Read the files involved before editing and run the project's checks afterwards. Never say a test, build or command passed unless you ran it and saw it pass.\n- Treat the conversation above as memory: reuse earlier decisions, file paths, identifiers and results. If something is genuinely missing, say what is missing instead of guessing.\n- Use the available tools (read, grep, glob, bash, edit) for real work; do not describe hypothetical commands as if they ran.\n- Delegate large or self-contained work with OpenCode's native `subagent` tool: `free-pi/scout` and `free-pi/planner` for read-only recon and planning, `free-pi/worker` for a full task, `free-pi/reviewer` for review. A subagent runs in its own context and reports back; never launch a subagent from inside one. The `subagent` tool returns a child `sessionID` and OpenCode injects the child's report when it goes idle; to inspect what that child actually did, call `free_pi_subagent_read` with its `sessionID` (omit it to list this session's children).\n- Load a skill with the native `skill` tool when its description matches the task.\n- `AGENTS.md` and the files listed under `instructions` are the project's memory. Follow them; do not restate or replace them.\n- Never claim to have compacted, summarized or cleared context unless that actually happened.\n- Continue until the request is finished, then report concisely what changed and what you verified.";
62
+ export declare const FREE_PI_GUIDANCE = "# free-pi working rules\n- Answer the user's latest request and keep their constraints; do not switch to a different task.\n- Slow down on multi-step work (arithmetic, counting, logic, dates, units, planning, debugging): write out each step, recompute the result a second way, and only then answer. On those tasks a correct answer after a dozen explicit steps beats a fast guess; do not stop at the first plausible number.\n- Read the files involved before editing and run the project's checks afterwards. Never say a test, build or command passed unless you ran it and saw it pass.\n- Treat the conversation above as memory: reuse earlier decisions, file paths, identifiers and results. If something is genuinely missing, say what is missing instead of guessing.\n- Use the available tools (read, grep, glob, bash, edit) for real work; do not describe hypothetical commands as if they ran.\n- Delegate large or self-contained work with OpenCode's native `subagent` tool: `free-pi/scout` and `free-pi/planner` for read-only recon and planning, `free-pi/worker` for a full task, `free-pi/reviewer` for review. A subagent runs in its own context and reports back; never launch a subagent from inside one. The `subagent` tool returns a child `sessionID` and OpenCode injects the child's report when it goes idle; to inspect what that child actually did, call `free_pi_subagent_read` with its `sessionID` (omit it to list this session's children).\n- Load a skill with the native `skill` tool when its description matches the task.\n- `AGENTS.md` and the files listed under `instructions` are the project's memory. Follow them; do not restate or replace them.\n- Never claim to have compacted, summarized or cleared context unless that actually happened.\n- Never answer with a session summary or with `## Objective` / `## Work State` blocks; that format belongs to compaction. If the work is unfinished, take the next concrete action instead of describing the state.\n- Continue until the request is finished, then report concisely what changed and what you verified.";
63
63
  export declare const FREE_PI_GUIDANCE_MARKER = "# free-pi working rules";
64
64
  /**
65
65
  * Appended to OpenCode's compaction request. Its default summary prompt asks
@@ -68,6 +68,16 @@ export declare const FREE_PI_GUIDANCE_MARKER = "# free-pi working rules";
68
68
  */
69
69
  export declare const FREE_PI_COMPACTION_GUIDANCE = "# free-pi compaction rules\n- Keep every unresolved user requirement and constraint, using their wording when a later agent must act on them.\n- For each step in the work state, write the conclusion it proved, not only the file or command: \"read X and confirmed Y\", \"build failed with Z\". A list of actions without their findings makes the next agent repeat the work.\n- Keep decisions and their reasons, exact file paths and symbols, commands with the observed result, error strings, and work still pending.\n- Preserve details that explain why the current state looks the way it does; the next agent cannot re-read the history this summary replaces.\n- Do not invent progress or results that are not in the conversation.";
70
70
  export declare const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
71
+ /**
72
+ * Repeats the two rules the free models break when the prompt is long: they
73
+ * stop to describe the session state instead of acting, and they answer a
74
+ * request to search from memory. It is sent as the last message of a primary
75
+ * request because the free models read that position, while rules buried in a
76
+ * 100k-token system prompt are easy to ignore. It is not a user instruction and
77
+ * never becomes part of the stored conversation.
78
+ */
79
+ export declare const TURN_REMINDER = "# free-pi turn check (not a user request)\nYou are in a normal working turn, not a compaction.\n- Never answer with `## Objective`, `## Requirements`, `## Work State` or a session summary; that format belongs only to a compaction. Earlier sections with those names are history, not an answer template.\n- If the user asked to search, find, analyze or verify something, call the matching tool first (grep/glob/shell for files, websearch/webfetch for the web) and report what it returned.\n- If the task is unfinished, call the next tool now instead of describing the state.";
80
+ export declare const TURN_REMINDER_MARKER = "# free-pi turn check";
71
81
  /**
72
82
  * OpenCode keeps only the latest exchange beside a summary, so after a
73
83
  * compaction the turns that mattered a minute ago disappear unless the summary
package/dist/constants.js CHANGED
@@ -83,6 +83,7 @@ export const FREE_PI_GUIDANCE = `# free-pi working rules
83
83
  - Load a skill with the native \`skill\` tool when its description matches the task.
84
84
  - \`AGENTS.md\` and the files listed under \`instructions\` are the project's memory. Follow them; do not restate or replace them.
85
85
  - Never claim to have compacted, summarized or cleared context unless that actually happened.
86
+ - Never answer with a session summary or with \`## Objective\` / \`## Work State\` blocks; that format belongs to compaction. If the work is unfinished, take the next concrete action instead of describing the state.
86
87
  - Continue until the request is finished, then report concisely what changed and what you verified.`;
87
88
  export const FREE_PI_GUIDANCE_MARKER = "# free-pi working rules";
88
89
  /**
@@ -97,6 +98,20 @@ export const FREE_PI_COMPACTION_GUIDANCE = `# free-pi compaction rules
97
98
  - Preserve details that explain why the current state looks the way it does; the next agent cannot re-read the history this summary replaces.
98
99
  - Do not invent progress or results that are not in the conversation.`;
99
100
  export const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
101
+ /**
102
+ * Repeats the two rules the free models break when the prompt is long: they
103
+ * stop to describe the session state instead of acting, and they answer a
104
+ * request to search from memory. It is sent as the last message of a primary
105
+ * request because the free models read that position, while rules buried in a
106
+ * 100k-token system prompt are easy to ignore. It is not a user instruction and
107
+ * never becomes part of the stored conversation.
108
+ */
109
+ export const TURN_REMINDER = `# free-pi turn check (not a user request)
110
+ You are in a normal working turn, not a compaction.
111
+ - Never answer with \`## Objective\`, \`## Requirements\`, \`## Work State\` or a session summary; that format belongs only to a compaction. Earlier sections with those names are history, not an answer template.
112
+ - If the user asked to search, find, analyze or verify something, call the matching tool first (grep/glob/shell for files, websearch/webfetch for the web) and report what it returned.
113
+ - If the task is unfinished, call the next tool now instead of describing the state.`;
114
+ export const TURN_REMINDER_MARKER = "# free-pi turn check";
100
115
  /**
101
116
  * OpenCode keeps only the latest exchange beside a summary, so after a
102
117
  * compaction the turns that mattered a minute ago disappear unless the summary
@@ -2,7 +2,7 @@
2
2
  import { ensureAdImpression } from "../ads/impression.js";
3
3
  import { resolveAdTurn } from "../ads/resolve.js";
4
4
  import { resolveToken } from "../auth/token.js";
5
- import { AD_CONTEXT_HEADER, AD_CONTEXT_OPT_OUT, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, CONTEXT_DIET_MIN_TOKENS, CONTEXT_DIET_SHARE, DEFENSE_PROVIDER_ID, FREE_PI_COMPACTION_GUIDANCE, FREE_PI_COMPACTION_MARKER, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER, MAX_OUTPUT_TOKENS, MAX_OUTPUT_TOKENS_CEILING, PROVIDER_ID, RECENT_WINDOW_CARRY_MARKER, RECENT_WINDOW_CARRY_MS, RECENT_WINDOW_MARKER, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, TRANSPORT_RETRY_BASE_MS, TRANSPORT_RETRY_MAX, TRANSPORT_RETRY_MAX_DELAY_MS, } from "../constants.js";
5
+ import { AD_CONTEXT_HEADER, AD_CONTEXT_OPT_OUT, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, CONTEXT_DIET_MIN_TOKENS, CONTEXT_DIET_SHARE, DEFENSE_PROVIDER_ID, FREE_PI_COMPACTION_GUIDANCE, FREE_PI_COMPACTION_MARKER, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER, MAX_OUTPUT_TOKENS, MAX_OUTPUT_TOKENS_CEILING, PROVIDER_ID, RECENT_WINDOW_CARRY_MARKER, RECENT_WINDOW_CARRY_MS, RECENT_WINDOW_MARKER, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, TRANSPORT_RETRY_BASE_MS, TRANSPORT_RETRY_MAX, TRANSPORT_RETRY_MAX_DELAY_MS, TURN_REMINDER, TURN_REMINDER_MARKER, } from "../constants.js";
6
6
  import { currentLiveLimits, learnedContextWindow, noteRejectedPrompt, } from "../catalog/live.js";
7
7
  import { closeFreePiSession } from "../api/session.js";
8
8
  import { rewriteContextOverflowResponse, rewriteLimitResponse, } from "../errors/limit.js";
@@ -58,7 +58,7 @@ function fitCompactionMessages(deps, event, reserveTokens = 0) {
58
58
  promptTokens: trimmed.tokens,
59
59
  });
60
60
  }
61
- /** Adds a system block once, even when OpenCode reuses the same event. */
61
+ /** Adds a system block once, without mutating the array OpenCode may reuse. */
62
62
  function appendSystemGuidance(event, text, marker) {
63
63
  const system = Array.isArray(event.system) ? event.system : [];
64
64
  if (system.some((part) => part?.type === "text" &&
@@ -66,8 +66,25 @@ function appendSystemGuidance(event, text, marker) {
66
66
  part.text.startsWith(marker))) {
67
67
  return;
68
68
  }
69
- system.push({ type: "text", text });
70
- event.system = system;
69
+ // A fresh array: OpenCode reuses the event across hooks, and pushing into the
70
+ // shared array would carry this block into the next, unrelated request.
71
+ event.system = [...system, { type: "text", text }];
72
+ }
73
+ /**
74
+ * Removes guidance blocks that belong to another kind of request. OpenCode can
75
+ * hand the same event to several hooks, so the compaction rules have to be
76
+ * stripped before a primary turn: otherwise the model reads them as an order to
77
+ * answer with a summary and burns the turn writing "## Objective / ## Work
78
+ * State" instead of doing the work.
79
+ */
80
+ function stripSystemGuidance(event, markers) {
81
+ if (!Array.isArray(event.system))
82
+ return;
83
+ const kept = event.system.filter((part) => !(part?.type === "text" &&
84
+ typeof part.text === "string" &&
85
+ markers.some((marker) => part.text.startsWith(marker))));
86
+ if (kept.length !== event.system.length)
87
+ event.system = kept;
71
88
  }
72
89
  /**
73
90
  * Reads the newest turns of the session from the native API. That history is
@@ -133,6 +150,26 @@ function appendCarriedWindow(deps, event) {
133
150
  return;
134
151
  appendSystemGuidance(event, carriedWindowGuidance(carried.text), RECENT_WINDOW_CARRY_MARKER);
135
152
  }
153
+ /**
154
+ * Puts the two rules weak free models forget on long conversations at the end
155
+ * of a primary request. The ad turn is resolved before this runs, so the
156
+ * reminder never leaks into `x-ad-context`. A fresh array keeps it out of the
157
+ * stored conversation and out of a later compaction request.
158
+ */
159
+ function appendTurnReminder(event) {
160
+ const messages = Array.isArray(event.messages) ? event.messages : [];
161
+ const already = messages.some((message) => Array.isArray(message?.content)
162
+ ? message.content.some((part) => part?.type === "text" &&
163
+ typeof part.text === "string" &&
164
+ part.text.startsWith(TURN_REMINDER_MARKER))
165
+ : false);
166
+ if (already)
167
+ return;
168
+ event.messages = [
169
+ ...messages,
170
+ { role: "user", content: [{ type: "text", text: TURN_REMINDER }] },
171
+ ];
172
+ }
136
173
  /**
137
174
  * Collapses stale tool payloads before a request goes out. Long free-pi
138
175
  * sessions reach 100k-170k token prompts, and at that size the free models
@@ -211,11 +248,13 @@ async function applyGeneration(deps, event, kind) {
211
248
  else if (kind === "primary") {
212
249
  // The free models tend to skip planning and claim work they did not do;
213
250
  // the CLI gets a much larger agent prompt. This is the compact equivalent.
251
+ stripSystemGuidance(event, [FREE_PI_COMPACTION_MARKER]);
214
252
  appendSystemGuidance(event, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER);
215
253
  applyContextDiet(deps, event);
216
254
  appendCarriedWindow(deps, event);
217
255
  }
218
256
  else {
257
+ stripSystemGuidance(event, [FREE_PI_COMPACTION_MARKER]);
219
258
  applyContextDiet(deps, event);
220
259
  }
221
260
  const agent = event.agent;
@@ -228,6 +267,9 @@ async function applyGeneration(deps, event, kind) {
228
267
  if (turn) {
229
268
  rememberPending(deps, pendingKeyOf(event.sessionID, agent, kind), turn);
230
269
  }
270
+ // After the ad turn: the reminder must not be mistaken for the user prompt.
271
+ if (kind === "primary")
272
+ appendTurnReminder(event);
231
273
  }
232
274
  /** Adds auth and per-turn ad headers to the outgoing model request. */
233
275
  async function applyModelRequest(deps, event) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.7.1",
3
+ "version": "0.7.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",
@@ -1,6 +1,6 @@
1
1
  /** Builds and redacts the ad context sent with a user turn. */
2
2
 
3
- import { AD_CONTEXT_MAX_CHARS } from "../constants.js";
3
+ import { AD_CONTEXT_MAX_CHARS, TURN_REMINDER_MARKER } from "../constants.js";
4
4
  import { cleanText } from "../utils/text.js";
5
5
 
6
6
  const AD_SECRET_PATTERNS: Array<[string, RegExp]> = [
@@ -138,7 +138,9 @@ export function latestUserText(messages: readonly AdMessage[]): string {
138
138
  const text = cleanText(
139
139
  typeof message.text === "string" ? message.text : contentText(message.content),
140
140
  );
141
- if (text) return text;
141
+ // The plugin appends this after the ad turn is resolved; ignore it if the
142
+ // event is reused so the next ad never describes the reminder itself.
143
+ if (text && !text.startsWith(TURN_REMINDER_MARKER)) return text;
142
144
  }
143
145
  return "";
144
146
  }
package/src/constants.ts CHANGED
@@ -90,6 +90,7 @@ export const FREE_PI_GUIDANCE = `# free-pi working rules
90
90
  - Load a skill with the native \`skill\` tool when its description matches the task.
91
91
  - \`AGENTS.md\` and the files listed under \`instructions\` are the project's memory. Follow them; do not restate or replace them.
92
92
  - Never claim to have compacted, summarized or cleared context unless that actually happened.
93
+ - Never answer with a session summary or with \`## Objective\` / \`## Work State\` blocks; that format belongs to compaction. If the work is unfinished, take the next concrete action instead of describing the state.
93
94
  - Continue until the request is finished, then report concisely what changed and what you verified.`;
94
95
  export const FREE_PI_GUIDANCE_MARKER = "# free-pi working rules";
95
96
 
@@ -106,6 +107,21 @@ export const FREE_PI_COMPACTION_GUIDANCE = `# free-pi compaction rules
106
107
  - Do not invent progress or results that are not in the conversation.`;
107
108
  export const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
108
109
 
110
+ /**
111
+ * Repeats the two rules the free models break when the prompt is long: they
112
+ * stop to describe the session state instead of acting, and they answer a
113
+ * request to search from memory. It is sent as the last message of a primary
114
+ * request because the free models read that position, while rules buried in a
115
+ * 100k-token system prompt are easy to ignore. It is not a user instruction and
116
+ * never becomes part of the stored conversation.
117
+ */
118
+ export const TURN_REMINDER = `# free-pi turn check (not a user request)
119
+ You are in a normal working turn, not a compaction.
120
+ - Never answer with \`## Objective\`, \`## Requirements\`, \`## Work State\` or a session summary; that format belongs only to a compaction. Earlier sections with those names are history, not an answer template.
121
+ - If the user asked to search, find, analyze or verify something, call the matching tool first (grep/glob/shell for files, websearch/webfetch for the web) and report what it returned.
122
+ - If the task is unfinished, call the next tool now instead of describing the state.`;
123
+ export const TURN_REMINDER_MARKER = "# free-pi turn check";
124
+
109
125
  /**
110
126
  * OpenCode keeps only the latest exchange beside a summary, so after a
111
127
  * compaction the turns that mattered a minute ago disappear unless the summary
@@ -31,6 +31,8 @@ import {
31
31
  TRANSPORT_RETRY_BASE_MS,
32
32
  TRANSPORT_RETRY_MAX,
33
33
  TRANSPORT_RETRY_MAX_DELAY_MS,
34
+ TURN_REMINDER,
35
+ TURN_REMINDER_MARKER,
34
36
  } from "../constants.js";
35
37
  import {
36
38
  currentLiveLimits,
@@ -148,7 +150,7 @@ function fitCompactionMessages(
148
150
  );
149
151
  }
150
152
 
151
- /** Adds a system block once, even when OpenCode reuses the same event. */
153
+ /** Adds a system block once, without mutating the array OpenCode may reuse. */
152
154
  function appendSystemGuidance(
153
155
  event: any,
154
156
  text: string,
@@ -165,8 +167,29 @@ function appendSystemGuidance(
165
167
  ) {
166
168
  return;
167
169
  }
168
- system.push({ type: "text", text });
169
- event.system = system;
170
+ // A fresh array: OpenCode reuses the event across hooks, and pushing into the
171
+ // shared array would carry this block into the next, unrelated request.
172
+ event.system = [...system, { type: "text", text }];
173
+ }
174
+
175
+ /**
176
+ * Removes guidance blocks that belong to another kind of request. OpenCode can
177
+ * hand the same event to several hooks, so the compaction rules have to be
178
+ * stripped before a primary turn: otherwise the model reads them as an order to
179
+ * answer with a summary and burns the turn writing "## Objective / ## Work
180
+ * State" instead of doing the work.
181
+ */
182
+ function stripSystemGuidance(event: any, markers: readonly string[]): void {
183
+ if (!Array.isArray(event.system)) return;
184
+ const kept = event.system.filter(
185
+ (part: any) =>
186
+ !(
187
+ part?.type === "text" &&
188
+ typeof part.text === "string" &&
189
+ markers.some((marker) => part.text.startsWith(marker))
190
+ ),
191
+ );
192
+ if (kept.length !== event.system.length) event.system = kept;
170
193
  }
171
194
 
172
195
  /**
@@ -252,6 +275,31 @@ function appendCarriedWindow(deps: Deps, event: any): void {
252
275
  );
253
276
  }
254
277
 
278
+ /**
279
+ * Puts the two rules weak free models forget on long conversations at the end
280
+ * of a primary request. The ad turn is resolved before this runs, so the
281
+ * reminder never leaks into `x-ad-context`. A fresh array keeps it out of the
282
+ * stored conversation and out of a later compaction request.
283
+ */
284
+ function appendTurnReminder(event: any): void {
285
+ const messages = Array.isArray(event.messages) ? event.messages : [];
286
+ const already = messages.some((message: any) =>
287
+ Array.isArray(message?.content)
288
+ ? message.content.some(
289
+ (part: any) =>
290
+ part?.type === "text" &&
291
+ typeof part.text === "string" &&
292
+ part.text.startsWith(TURN_REMINDER_MARKER),
293
+ )
294
+ : false,
295
+ );
296
+ if (already) return;
297
+ event.messages = [
298
+ ...messages,
299
+ { role: "user", content: [{ type: "text", text: TURN_REMINDER }] },
300
+ ];
301
+ }
302
+
255
303
  /**
256
304
  * Collapses stale tool payloads before a request goes out. Long free-pi
257
305
  * sessions reach 100k-170k token prompts, and at that size the free models
@@ -345,10 +393,12 @@ async function applyGeneration(
345
393
  } else if (kind === "primary") {
346
394
  // The free models tend to skip planning and claim work they did not do;
347
395
  // the CLI gets a much larger agent prompt. This is the compact equivalent.
396
+ stripSystemGuidance(event, [FREE_PI_COMPACTION_MARKER]);
348
397
  appendSystemGuidance(event, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER);
349
398
  applyContextDiet(deps, event);
350
399
  appendCarriedWindow(deps, event);
351
400
  } else {
401
+ stripSystemGuidance(event, [FREE_PI_COMPACTION_MARKER]);
352
402
  applyContextDiet(deps, event);
353
403
  }
354
404
 
@@ -362,6 +412,9 @@ async function applyGeneration(
362
412
  if (turn) {
363
413
  rememberPending(deps, pendingKeyOf(event.sessionID, agent, kind), turn);
364
414
  }
415
+
416
+ // After the ad turn: the reminder must not be mistaken for the user prompt.
417
+ if (kind === "primary") appendTurnReminder(event);
365
418
  }
366
419
 
367
420
  /** Adds auth and per-turn ad headers to the outgoing model request. */