Repository navigation
Description
Summary
The system prompt injected into free-tier requests carries an <env> block whose Working directory and Workspace root are the gateway process's own cwd, resolved once via os.Getwd() in opencodeEnvironment() (internal/gateway/opencode_prompt.go:51). Every client receives the gateway's directory, never its own, so agents that trust this block operate on the wrong path.
Actual behavior
A Claude Code client launched with cwd D:\Projects connected through a gateway running from D:\ai-configs\opencode2api receives, at the top of its context:
<env>
Working directory: D:\ai-configs\opencode2api
Workspace root folder: D:\ai-configs\opencode2api
Is directory a git repo: yes
Platform: win32
Today's date: ...
</env>
The client's own (correct) environment section appears later in the prompt, so the two sources conflict. In practice an agent followed the injected block and ran a repo-wide command against the gateway's directory instead of the client's project.
Expected behavior
The injected env block carries only facts the gateway can state truthfully. Fields it cannot know (client cwd, git-repo status) are omitted, or the values are operator-configurable.
Steps to reproduce
- Start the gateway from any directory, e.g.
cd /somewhere && ./opencode2api -config config.json. - Send a free-model request from a client running in a different directory.
- Observe the upstream request body (or the model's view):
<env>reports/somewherefor every client regardless of their actual cwd.
Environment
- Version: v1.4.7 (confirmed identical at HEAD
b296a61) - Platform: Windows 11, gateway started via scheduled task; reproduces from any launch directory
- Configuration:
anonymous: true, free model, v2 lane
Root cause
internal/gateway/opencode_prompt.go:51-61:
opencodeEnvOnce.Do(func() {
directory := ""
if current, err := os.Getwd(); err == nil {
directory = filepath.Clean(strings.TrimSpace(current))
}
if directory == "" || directory == "." {
directory = "/workspace"
}
opencodeWorkDir = directory
})os.Getwd() returns the gateway process's directory. The same block hardcodes Is directory a git repo: yes regardless of what the client has open. For a multi-user or remote deployment the value also leaks the operator's local paths.
Proposed fix
Either of:
- Omit client-unknowable fields from the injected env: keep
model,Platform,Today's date; dropWorking directory,Workspace root folder, and the hardcoded git-repo line. This preserves the abuse-check shape of the prompt without asserting false facts. - Configurable override: an optional config field (e.g. under
modelsorupstream) letting operators set a neutral directory string for the injected block.
Option 1 is sufficient and needs no configuration.
Verification
- Reproduced the wrong-directory injection against a local v1.4.7 gateway (client cwd and injected value compared in the transcript).
- Confirmed via source read that
sync.Oncepins the value for the process lifetime, so restarting the gateway from a different directory is the only way to change it.
Activity
Swcmb commentedon Nov 8, 2026
Refining the proposed fix after a closer look at the shape question: omitting fields changes the block's structure, and a safer variant is to keep every field and pin the directory to
/workspace— the code's own fallback whenos.Getwd()fails ("the official container path",opencode_prompt.go:57-59).Why this preserves the abuse-check shape:
Working directory— every legitimate CLI session reports a different cwd, and an official CLI running in a container reports exactly/workspace. Sessions with that value have necessarily been accepted before. What the checks can anchor on is the base prompt text, the field labels and structure, the official tool set, and streaming — all preserved when only the value is pinned./workspacekeeps the block field-for-field identical to a containerized official CLI session: same labels, same field count, same order.Minimal diff:
(
Is directory a git repo: yescan stay: real sessions report both values depending on what they have open, so the upstream cannot require one.)fix(gateway): harden free-tier retries and client compatibility
jasonxu114514 commentedon Nov 16, 2026
Implemented the configurable alternative on main in 14faec7: 14faec7 . Set upstream.prompt_environment with working_directory, workspace_root, platform and is_git_repo; README documents defaults and usage. Official prompt/env structure is preserved and shell descriptions follow the explicit platform. Defaults remain unchanged. This is deployment-wide manual configuration, not automatic client detection; existing official client prompts are untouched. All shaping paths share it and WebUI saves preserve it. Offline tests, vet and Windows/Linux builds passed; zz_ tests were removed afterward as requested. Arbitrary overrides have not been live-validated upstream. Closing as addressed through the optional configuration approach; operators must opt in.