zen-free 0.1.18: the environment block names a real directory, since Zen refuses "Working directory: unknown"
shanmao_10826 on magpie's #help, 10-29: every free model (deepseek-v4.1-
flash-free and others) answers 429 "Free usage limit reached. Free
models work in OpenCode", while OpenCode itself on the same machine works.
Cause, found against opencode.ai from the Linux box (xiaomi-gpu WSL) on
2026-10-29, deepseek-v4.1-flash-free, one request each, minutes apart,
same IP, only the system prompt and stub-tool text changed:
- the plugin's own request through loader.fetch (0.1.17): 429
FreeUsageLimitError, the reporter's message;
- "You are opencode." + <env> Working directory: /tmp/x: 200;
- the plugin's exact block "<env>\n Working directory: unknown\n</env>"
(alone, or after a prompt, either tool description): 429;
- the same block with /tmp/x for "unknown": 200;
- the plugin's stub description "Not available. Never call this tool."
with a path in the block: 200.
So Zen now refuses the literal placeholder 0.1.15 to 0.1.17 added, the
plugin's fingerprint, as it refused the plugin's old User-Agent on 10-26.
Headers were diffed against OpenCode v2.2.0 (packages/core/src/session/
model-request.ts, app.ts useragent, plugin/provider/opencode.ts): the
same set the plugin sends; nothing new on the request since 2.0.25. Zen's
server code is no longer public (packages/console's handler went on
10-11), so this was found by bisecting real requests.
Fix: the block added is OpenCode 2.2.0's own (instructions/builtins.ts:
the intro line, Working directory, Workspace root folder, Is directory a
git repo, Platform; not its line about OpenCode's temp folder), with the
directory the agent's own prompt or first user message names (Codex's
<cwd>, a "Working directory:" line, Kimi's "The current working directory
is `...`"; absolute paths only), or the user's home folder when none is
named. A prompt that already has a block goes as it is, as before.
Run:
- Real, through the fixed plugin's loader.fetch from the same IP right
after the 429s: deepseek-v4.1-flash-free with no prompt 200, with an
agent prompt naming its directory 200, glm-5.3-flash-free 200.
- New test (the block names the stated or home directory, never a
placeholder; Codex/Kimi/Windows/Anthropic cases): red on origin/main's
provider.mjs (1 fail of 44 in index.test.mjs), green with it.
- HOME=$(mktemp -d) bun test in packages/zen-free: 201 pass, 0 fail.
- HOME=$(mktemp -d) bun test at the repo root: 3131 pass, 1 fail, the
fail is layerfs "item 3: an import skips secret-shaped files", red the
same at origin/main cc3f000, filed as #477.
- bun scripts/check.mjs: ok.
All on the Linux box (wsl-run); mote was unreachable.
Not tried: the reporter's own agent through magpie (they didn't say
which); a Responses or Anthropic free model (Zen lists none free); a
Windows or macOS host (the block's Platform and home path differ there).
This commit is contained in:
@@ -88,8 +88,13 @@ gets a small allowance a day and then `429 FreeUsageLimitError` ("Free
|
||||
models work in OpenCode") until 00:00 UTC, while OpenCode's go through.
|
||||
So a request whose system messages have no such block gets one added
|
||||
after the agent's own prompt (Anthropic `system` and Responses
|
||||
`instructions` likewise), its working directory said as unknown; a
|
||||
prompt that has one, as OpenCode's does, goes as it is.
|
||||
`instructions` likewise); a prompt that has one, as OpenCode's does, goes
|
||||
as it is. The block added is OpenCode's own, with the working directory
|
||||
the agent's prompt names (Codex's `<cwd>`, a `Working directory:` line,
|
||||
"the current working directory is ...") or the user's home folder when it
|
||||
names none. A fixed placeholder there is refused: since 2026-10-29 Zen
|
||||
answers `Working directory: unknown`, which 0.1.15 to 0.1.17 sent, with
|
||||
that 429 at once.
|
||||
|
||||
Now and then Zen closes a streamed Chat Completions reply before its
|
||||
`[DONE]`. When the reply has said something and no tool call of it is
|
||||
|
||||
@@ -4,6 +4,7 @@ import { once } from "node:events";
|
||||
import { readFileSync } from "node:fs";
|
||||
import test from "node:test";
|
||||
import { protocols, declaredTools, toolReply } from "./tool-fixtures.mjs";
|
||||
import { addEnvironment } from "./provider.mjs";
|
||||
|
||||
const subject = import("./index.mjs").then((module) => ({
|
||||
default: { server: module.ZenFreePlugin },
|
||||
@@ -711,3 +712,43 @@ for (const { protocol, path, model } of protocols) {
|
||||
assert.equal(systemText(protocol, sent), own);
|
||||
});
|
||||
}
|
||||
|
||||
// Zen answers 429 FreeUsageLimitError at once to the block 0.1.15 to
|
||||
// 0.1.17 added, `Working directory: unknown`, and serves the same request
|
||||
// with a path there (shanmao_10826 on magpie's Discord, 2026-10-29). The
|
||||
// block names the directory the agent's prompt names, or the home folder.
|
||||
test("the environment block names a real directory, never a fixed placeholder", () => {
|
||||
const home = "/home/someone";
|
||||
const added = (protocol, body) => {
|
||||
addEnvironment(body, protocol, home);
|
||||
return systemText(protocol, body);
|
||||
};
|
||||
const line = (text) => /Working directory: (.*)/.exec(text)?.[1];
|
||||
// none named: the home folder, in OpenCode's own block
|
||||
let text = added("chat", { messages: [{ role: "user", content: "hi" }] });
|
||||
assert.match(text, ENV);
|
||||
assert.equal(line(text), home);
|
||||
assert.doesNotMatch(text, /unknown/);
|
||||
assert.match(text, /^Here is some useful information about the environment you are running in:\n<env>\n/);
|
||||
assert.match(text, new RegExp(`Workspace root folder: ${home}\\n Is directory a git repo: (yes|no)\\n Platform: ${process.platform}\\n</env>$`));
|
||||
// Codex's <cwd> in its first user message
|
||||
text = added("responses", {
|
||||
instructions: "You are Codex.",
|
||||
input: [{ role: "user", content: [{ type: "input_text", text: "<environment_context>\n <cwd>/Users/me/app</cwd>\n</environment_context>" }] }],
|
||||
});
|
||||
assert.equal(line(text), "/Users/me/app");
|
||||
// Kimi's sentence, a Windows path, in the system prompt
|
||||
text = added("chat", {
|
||||
messages: [
|
||||
{ role: "system", content: "You are Kimi. The current working directory is `C:\\work\\repo`." },
|
||||
{ role: "user", content: "hi" },
|
||||
],
|
||||
});
|
||||
assert.equal(line(text.split("<env>")[1]), "C:\\work\\repo");
|
||||
// Anthropic: a "Working directory:" line outside an <env> block
|
||||
text = added("anthropic", { system: "Working directory: /srv/proj.\nBe brief.", messages: [] });
|
||||
assert.equal(line(text.split("<env>")[1]), "/srv/proj");
|
||||
// a relative or bare word is not a directory
|
||||
text = added("chat", { messages: [{ role: "system", content: "Working directory: unknown" }, { role: "user", content: "hi" }] });
|
||||
assert.equal(line(text.split("<env>")[1]), home);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-zen-free-auth",
|
||||
"version": "0.1.17",
|
||||
"version": "0.1.18",
|
||||
"description": "OpenCode Zen free models through the public credential, with native streaming protocols",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { existsSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
PROVIDER,
|
||||
SDK,
|
||||
@@ -123,11 +126,26 @@ function normalizeTools(body, protocol) {
|
||||
// say; the same request with the block added is served (tried against
|
||||
// opencode.ai on 2026-10-25, deepseek-v4.1-flash-free and
|
||||
// glm-5.3-flash-free). In a user message or a tool description it doesn't
|
||||
// count, and `<ENV>` or `working directory:` don't either. The directory
|
||||
// isn't known here, and the agent's own prompt says it, so it is told as
|
||||
// unknown rather than as a path that would mislead the model.
|
||||
const ENVIRONMENT = "<env>\n Working directory: unknown\n</env>";
|
||||
// count, and `<ENV>` or `working directory:` don't either.
|
||||
//
|
||||
// Since 2026-10-29 Zen answers 429 FreeUsageLimitError at once to the
|
||||
// literal line 0.1.15 to 0.1.17 added, `Working directory: unknown`, while
|
||||
// the same request with a path there is served (shanmao_10826 on magpie's
|
||||
// Discord): fixed text that only this plugin sends is its fingerprint, as
|
||||
// its old User-Agent was. So the block is OpenCode 2.2.0's own
|
||||
// (packages/core/src/instructions/builtins.ts, without its line about
|
||||
// OpenCode's temp folder), with the directory the agent's own prompt
|
||||
// names, as OpenCode names its own, or the user's home folder when the
|
||||
// prompt names none.
|
||||
const HAS_ENVIRONMENT = /<env>[\s\S]*Working directory:[\s\S]*<\/env>/;
|
||||
// How agents name their working directory: Codex's `<cwd>`, Claude Code's
|
||||
// and others' `Working directory:`, Kimi's "The current working directory
|
||||
// is `...`". Only an absolute path counts.
|
||||
const ABSOLUTE = String.raw`((?:~?/|[A-Za-z]:[\\/]|\\\\)[^\s\x60'"<>]*)`;
|
||||
const STATED_DIRECTORY = [
|
||||
new RegExp(String.raw`<cwd>\s*${ABSOLUTE}\s*</cwd>`),
|
||||
new RegExp(String.raw`working directory(?: is)?:?\s+\x60?${ABSOLUTE}`, "i"),
|
||||
];
|
||||
|
||||
function textOf(content) {
|
||||
if (typeof content === "string") return content;
|
||||
@@ -135,17 +153,56 @@ function textOf(content) {
|
||||
return content.map((part) => (typeof part?.text === "string" ? part.text : "")).join("\n");
|
||||
}
|
||||
|
||||
function withEnvironment(content) {
|
||||
if (typeof content === "string") return content ? `${content}\n\n${ENVIRONMENT}` : ENVIRONMENT;
|
||||
if (Array.isArray(content)) return [...content, { type: "text", text: ENVIRONMENT }];
|
||||
return ENVIRONMENT;
|
||||
}
|
||||
|
||||
const SYSTEM_ROLES = ["system", "developer"];
|
||||
|
||||
export function addEnvironment(body, protocol) {
|
||||
// The agent's working directory as its own prompt (system messages,
|
||||
// instructions) or its first user message says it.
|
||||
export function statedDirectory(body, protocol) {
|
||||
const messages = protocol === "responses" ? body.input : body.messages;
|
||||
const texts = [textOf(protocol === "anthropic" ? body.system : body.instructions)];
|
||||
if (Array.isArray(messages)) {
|
||||
for (const message of messages)
|
||||
if (SYSTEM_ROLES.includes(message?.role)) texts.push(textOf(message.content));
|
||||
texts.push(textOf(messages.find((message) => message?.role === "user")?.content));
|
||||
} else if (typeof messages === "string") {
|
||||
texts.push(messages);
|
||||
}
|
||||
for (const text of texts) {
|
||||
for (const pattern of STATED_DIRECTORY) {
|
||||
const found = pattern.exec(text)?.[1]?.replace(/[.,;:)\]]+$/, "");
|
||||
if (found && found !== "/" && found !== "~/") return found;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// OpenCode's block, said for `directory`.
|
||||
export function environment(directory) {
|
||||
let git = false;
|
||||
try {
|
||||
git = existsSync(join(directory.replace(/^~(?=\/)/, homedir()), ".git"));
|
||||
} catch {}
|
||||
return [
|
||||
"Here is some useful information about the environment you are running in:",
|
||||
"<env>",
|
||||
` Working directory: ${directory}`,
|
||||
` Workspace root folder: ${directory}`,
|
||||
` Is directory a git repo: ${git ? "yes" : "no"}`,
|
||||
` Platform: ${process.platform}`,
|
||||
"</env>",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function withEnvironment(content, block) {
|
||||
if (typeof content === "string") return content ? `${content}\n\n${block}` : block;
|
||||
if (Array.isArray(content)) return [...content, { type: "text", text: block }];
|
||||
return block;
|
||||
}
|
||||
|
||||
export function addEnvironment(body, protocol, home = homedir()) {
|
||||
const block = () => environment(statedDirectory(body, protocol) ?? home);
|
||||
if (protocol === "anthropic") {
|
||||
if (!HAS_ENVIRONMENT.test(textOf(body.system))) body.system = withEnvironment(body.system);
|
||||
if (!HAS_ENVIRONMENT.test(textOf(body.system))) body.system = withEnvironment(body.system, block());
|
||||
return;
|
||||
}
|
||||
const messages = protocol === "chat" ? body.messages : body.input;
|
||||
@@ -155,16 +212,16 @@ export function addEnvironment(body, protocol) {
|
||||
if (system.some((message) => HAS_ENVIRONMENT.test(textOf(message.content)))) return;
|
||||
if (protocol === "responses") {
|
||||
if (!HAS_ENVIRONMENT.test(textOf(body.instructions)))
|
||||
body.instructions = withEnvironment(body.instructions);
|
||||
body.instructions = withEnvironment(body.instructions, block());
|
||||
return;
|
||||
}
|
||||
// Added to the agent's own system message, so the prompt stays one
|
||||
// system message, the same on every turn (its cache holds); one is put
|
||||
// first when the agent sent none.
|
||||
const first = system.find((message) => message.role === "system");
|
||||
if (first) first.content = withEnvironment(first.content);
|
||||
else if (Array.isArray(messages)) messages.unshift({ role: "system", content: ENVIRONMENT });
|
||||
else body.messages = [{ role: "system", content: ENVIRONMENT }];
|
||||
if (first) first.content = withEnvironment(first.content, block());
|
||||
else if (Array.isArray(messages)) messages.unshift({ role: "system", content: block() });
|
||||
else body.messages = [{ role: "system", content: block() }];
|
||||
}
|
||||
|
||||
const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
Reference in New Issue
Block a user