@eduardo191labs/opencode-free-pi 0.7.2 → 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 "";
@@ -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
@@ -98,6 +98,20 @@ export const FREE_PI_COMPACTION_GUIDANCE = `# free-pi compaction rules
98
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.
99
99
  - Do not invent progress or results that are not in the conversation.`;
100
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";
101
115
  /**
102
116
  * OpenCode keeps only the latest exchange beside a summary, so after a
103
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";
@@ -150,6 +150,26 @@ function appendCarriedWindow(deps, event) {
150
150
  return;
151
151
  appendSystemGuidance(event, carriedWindowGuidance(carried.text), RECENT_WINDOW_CARRY_MARKER);
152
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
+ }
153
173
  /**
154
174
  * Collapses stale tool payloads before a request goes out. Long free-pi
155
175
  * sessions reach 100k-170k token prompts, and at that size the free models
@@ -247,6 +267,9 @@ async function applyGeneration(deps, event, kind) {
247
267
  if (turn) {
248
268
  rememberPending(deps, pendingKeyOf(event.sessionID, agent, kind), turn);
249
269
  }
270
+ // After the ad turn: the reminder must not be mistaken for the user prompt.
271
+ if (kind === "primary")
272
+ appendTurnReminder(event);
250
273
  }
251
274
  /** Adds auth and per-turn ad headers to the outgoing model request. */
252
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.2",
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
@@ -107,6 +107,21 @@ export const FREE_PI_COMPACTION_GUIDANCE = `# free-pi compaction rules
107
107
  - Do not invent progress or results that are not in the conversation.`;
108
108
  export const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
109
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
+
110
125
  /**
111
126
  * OpenCode keeps only the latest exchange beside a summary, so after a
112
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,
@@ -273,6 +275,31 @@ function appendCarriedWindow(deps: Deps, event: any): void {
273
275
  );
274
276
  }
275
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
+
276
303
  /**
277
304
  * Collapses stale tool payloads before a request goes out. Long free-pi
278
305
  * sessions reach 100k-170k token prompts, and at that size the free models
@@ -385,6 +412,9 @@ async function applyGeneration(
385
412
  if (turn) {
386
413
  rememberPending(deps, pendingKeyOf(event.sessionID, agent, kind), turn);
387
414
  }
415
+
416
+ // After the ad turn: the reminder must not be mistaken for the user prompt.
417
+ if (kind === "primary") appendTurnReminder(event);
388
418
  }
389
419
 
390
420
  /** Adds auth and per-turn ad headers to the outgoing model request. */