Repository navigation
Commit f5e10cc
authored
* fix: plan-agent tool-use — mandate exploration + reword misleading warning
When the plan agent runs on a tool-capable but compliant model (e.g.
GPT-5.5 via `altimate-default`), it often emits a plan from prompt
context alone without ever reading the repo. The existing safety net in
`processor.ts` catches this and surfaces a warning suggesting the user
"switch to a model with stronger tool-use via /model". That diagnosis
is misleading — the model IS tool-capable; it's a prompt-engineering
issue, not a capability issue.
This patch addresses both halves of the problem:
- **`plan.txt`**: adds a `## Investigate before drafting (REQUIRED)`
section that explicitly mandates at least one read-only tool call
(`read`/`grep`/`glob`/`list`/`explore`/read-only `bash`) before any
plan content. Stronger Claude models already infer this; weaker
compliant models need it spelled out. Includes an explicit escape
hatch for the trivial-task case so the rule doesn't fire spuriously.
- **`processor.ts`**: rewords the synthetic warning to describe the
observed behavior (`produced a plan without first reading any files`)
and offer two concrete remedies — reply asking it to explore first,
or `/model` to a more eager-to-investigate tier (Claude Sonnet/Opus).
No longer blames "tool-use capability."
- **`plan-reminder-anthropic.txt`**: deleted. The file was never
imported anywhere in the codebase — it was an upstream OpenCode
artifact that survived the rename in commit `a4eba2e6e` ("tweak: plan
prompt"). Removing dead config.
- Regression test in `plan-layer-e2e.test.ts` locks the explore-first
contract into the prompt so a future cleanup can't silently delete
it.
All 160 affected tests pass; typecheck clean.
* fix: plan-agent tool-use — warning copy covers refusal case too
Field test surfaced a second failure mode the prior warning misframed:
when the planner refuses the request outright ("I'm sorry, but I cannot
assist…" — observed on `altimate-default` for a benign "add a YOLO-mode
toggle button" request), the trip-wire fires (plan + finish=stop + zero
tools) but the previous copy told the user the model "produced a plan
without first reading any files." It did not — it refused.
Reword the warning to be accurate for **both** failure modes without
pattern-matching refusal text (brittle, locale-specific):
- describe the observed symptom only ("stopped without calling any tools")
- enumerate three common causes (prompt-context plan / content-policy
refusal / under-specified request)
- offer three concrete recoveries (ask to investigate, rephrase, /model)
The trip-wire condition is unchanged; only the synthetic text the user
sees is updated. Telemetry event shape is unchanged.
This builds on d21f9b1 and supersedes the warning copy from that commit.
* fix: altimate-backend models hit Claude-fallback prompt that triggered GPT-5.x refusals
Root cause of the plan-mode refusals on `altimate-backend/altimate-default`:
the system-prompt router in `session/system.ts` selects a base prompt by
string-matching `model.api.id`. The gateway's api.id is the opaque alias
`altimate-default` (kept stable for backward compatibility — users persist
it in `model.json` favorites/recents), which matches none of the patterns
(`gpt-5`, `gpt-`, `gemini-`, `claude`, `trinity`), so it falls through to
`PROMPT_ANTHROPIC_WITHOUT_TODO` (`qwen.txt`).
`qwen.txt` begins with two hard "MUST refuse if borderline/malicious"
Claude-style directives. The upstream gateway model is GPT-5.x (an
OpenAI-trained model), and when those directives stack with plan.txt's
"STRICTLY FORBIDDEN: ANY modifications. ZERO exceptions." language, the
combination is enough to push GPT-5.x into refusing benign developer
requests (confirmed repro: "plan a feature to add a button called verify
output in the UI of altimate-code" → "I'm sorry, but I cannot assist with
that request.").
Fix: route altimate-backend models by `model.family` (which the provider
definition explicitly pins to `"openai"` for `altimate-default` at
`provider.ts:1291`) before the api.id string-match fallthrough. The
gateway-routed GPT-5.x model now gets `codex_header.txt` (its native
prompt) which contains zero refusal language.
Touches:
- `packages/opencode/src/session/system.ts`: family-based routing for
altimate-backend; falls through to existing logic otherwise.
- `packages/opencode/test/session/system.test.ts`: regression tests
locking the routing contract for the three relevant cases (altimate
openai, altimate anthropic, non-altimate openai).
This is the actual fix for #887 / AI-6957 — supersedes the warning-copy
and plan.txt changes from d21f9b1 / 6bbcfbf as the *user-visible*
behavior change. Those earlier commits remain correct: stronger
exploration mandate in plan.txt and accurate warning copy are still
improvements, just not the load-bearing fix.
Typecheck clean. 620 tests pass.
* fix: hoist <system-reminder> to system role for non-Anthropic models + warning false-positive
Two related bugs the trace finally revealed, after the qwen.txt theory in
b07da4e didn't actually fix the refusal in production:
1. **Root cause of GPT-5.x refusals**: prompt.ts:2049 attaches PROMPT_PLAN
(wrapped in `<system-reminder>...</system-reminder>` tags) as a text
part on the latest user message. Claude is trained to recognize that
tag as authoritative system-level instructions even when delivered in
a user-role message. OpenAI / Google / etc. models are NOT — they see
a user message containing a `<system-reminder>` block with aggressive
constraint language (STRICTLY FORBIDDEN / ZERO exceptions / ABSOLUTE
CONSTRAINT / MUST) and pattern-match it as a prompt-injection attempt.
GPT-5.x via `altimate-backend/altimate-default` reliably returned
"I'm sorry, but I cannot assist with that request." for benign
developer requests like "plan a feature to add a button…". Confirmed
by reading the actual LLM payload via temporary debug instrumentation
in `llm.ts` (since removed).
Fix in `llm.ts`: for non-Anthropic-like models, walk
`input.messages` and extract any `<system-reminder>`-wrapped text
parts from user-role messages, then append them to the system array
so they arrive as proper `role:"system"` messages. User messages that
become empty after extraction are dropped. Anthropic-like models keep
the existing behavior.
Detection of Anthropic-like uses providerID + family + api.npm + the
existing api.id substring patterns, matching the heuristics already
used in `provider/transform.ts:284-295` for cache-control handling.
2. **Counter scope bug in plan-no-tool warning**: `sessionToolCallsMade`
in `processor.ts:57` was claimed (comment line 207) to be session-
scoped, but `SessionProcessor.create()` is called per-step by
`loop()` in `prompt.ts:485`, so the counter resets each step. A
multi-step plan session (read → grep → read → … → final text)
false-positives on the last text-only step. The warning fires *after*
the agent has successfully explored the codebase and produced a
plan — confusing the user into thinking the session failed.
Fix: at warning-check time, also scan `streamInput.messages` for any
prior assistant tool-call content. The session has used tools iff
either the per-step counter is non-zero OR the history contains a
tool-call. Regression test added in `processor.test.ts`.
The earlier `system.ts` family-based routing (commit b07da4e) stays.
It was based on a wrong root-cause theory, but the change itself is
correct: a model with `family: "openai"` should not fall through to
qwen.txt (an Anthropic-style fallback). Worth keeping as defense in
depth even though it wasn't the load-bearing fix.
Touches:
- `packages/opencode/src/session/llm.ts`: system-reminder hoist.
- `packages/opencode/src/session/processor.ts`: warning false-positive
fix + comment block explaining the per-step scoping.
- `packages/opencode/test/session/processor.test.ts`: regression test
for the warning false-positive.
Typecheck clean. 124 affected tests pass.
This is the actual fix for #887 / AI-6957.
* fix: address PR #888 bot review — trust provenance, biome lint, safe defaults
Triage of automated review feedback on #888 — addresses the genuine
findings, skips the rest with reasons.
## Addressed
**Trust provenance for `<system-reminder>` hoisting**
(CodeRabbit `llm.ts:106`, cubic `llm.ts:94` — both P1/Major)
Moves the hoist from `llm.ts` to `prompt.ts`, where the `synthetic`
flag on `MessageV2.TextPart` is still available. The hoist now
qualifies a part only if `synthetic === true` AND text begins with
`<system-reminder>` — raw user text starting with the same characters
is left in place and never gains system-role priority. The synthetic
flag is the trust boundary: it is only set by `insertReminders` when
altimate-code itself injects PROMPT_PLAN / BUILD_SWITCH. The
`isAnthropicLikeModel` helper is colocated with the new hoist.
**Safe default for unknown families on altimate-backend**
(cubic `system.ts:46` — P2)
`SystemPrompt.provider` previously fell through to
`PROMPT_ANTHROPIC_WITHOUT_TODO` when an altimate-backend model had a
family value not in the explicit list — recreating the original
routing problem the moment a new family value appears in config. Now
defaults unknown families to `PROMPT_CODEX` because the gateway is
registered as `@ai-sdk/openai-compatible`. Regression test added.
**Case-insensitive family lookup**
(cubic `system.ts:45` — P2)
`family` is a free-form string in the model schema. Now compared via
`?.toLowerCase()`. Regression test covers `Anthropic` / `ANTHROPIC` /
`anthropic` parity.
**Biome `noSwitchDeclarations` lint**
(CodeRabbit `processor.ts:367` — Minor)
Wraps the `finish-step` case body in a block so
`sessionHasPriorToolCalls` can't leak.
**Strengthened anthropic-family regression test**
(CodeRabbit `system.test.ts:62` — Minor)
Asserts exact equality with a baseline anthropic routing call instead
of two loose negative regex checks.
**Removed conversation artifact**
Test comment referenced "the 2026-06-04 comment claimed" — date was
a leftover from the investigation timeline. Reworded to describe the
behavior directly without anchoring to today's date.
**Reframed `system.ts` routing comment**
Earlier wording attributed the GPT-5.x refusal fix to this routing
change. The actual fix is the user-message `<system-reminder>` hoist
in `prompt.ts`. Comment now correctly frames this as defense in depth.
## Skipped (with reasons)
- **dev-punia-altimate `llm.ts:78`** — "may incorrectly drop user
messages that contain both a `<system-reminder>` part and other
valid content." Misreads the code. The filter only drops messages
where `content.length === 0` AFTER extraction; messages with both a
reminder and other content keep their other content. Not applicable
to the new prompt.ts hoist either, which operates on parts not
whole messages.
- **dev-punia-altimate `processor.ts:355`** — "tool calls in earlier
steps may not be preserved in current `streamInput.messages`." Not
the case: `message-v2.ts` `toModelMessages` (lines 707-769) emits
every completed tool call as a `tool-${name}`-typed part on the
assistant message, which AI SDK's `convertToModelMessages` lowers
to `tool-call` content. Verified by reading the conversion path.
- **dev-punia-altimate `llm.ts:64`** — "`altimate_change` marker
is not permitted in CLAUDE.md, use `// NOTE: altimate`." Incorrect
— the `// altimate_change start/end` convention is used throughout
the codebase (grep finds hundreds of occurrences across packages/
opencode/src). Matching existing project style.
Typecheck clean. 605 of 612 session tests pass (7 unrelated skips).
* fix: tighten hoist trust boundary — `insertReminders` returns explicit candidates
PR #888 review surfaced a real prompt-injection vector that the
previous `synthetic + prefix` trust check could not close.
The `MessageV2.TextPart.synthetic` flag is set by *several* code paths
in `session/prompt.ts`, not just by reminder injection. In particular,
the file/resource expansion paths at lines ~1729/1751/1801 attach
*user-supplied content* (MCP resource bodies, `data:` URL decoded
text, file attachments) as text parts with `synthetic: true`. A
malicious file or resource whose body began with `<system-reminder>`
would therefore have been hoisted into the system role on
non-Anthropic models — exactly the privilege escalation the prior
fix was meant to prevent. Caught by both CodeRabbit (PR #888 thread,
`prompt.ts:2082`) and cubic (`prompt.ts:2078`).
This commit narrows the trust boundary the way the reviewers
suggested: `insertReminders` now returns the explicit list of parts
it appended, and the loop hoists *only* those. `synthetic + prefix`
inference is gone.
- `insertReminders` return shape: `Promise<{ messages, trustedReminderParts }>`.
Each `userMessage.parts.push(...)` site also pushes the same part
into `trustedReminderParts` so the caller has the candidates
without having to re-derive them.
- `hoistSyntheticReminders` helper removed. The loop iterates
`reminderResult.trustedReminderParts` directly: pushes the text
into the system array, marks the part `ignored = true` so
`toModelMessages` skips it in the user message.
- `isAnthropicLikeModel` retained (still the gate for whether to
hoist or leave reminders in-place).
- Two regression tests added in `plan-layer-e2e.test.ts` that
re-read `prompt.ts` to enforce the contract: the hoist must
consume `trustedReminderParts` and must not re-introduce a
`hoistSyntheticReminders`-style scan.
Also removes the diagnostic dump from `llm.ts` (added in an earlier
WIP for #888 investigation, not for shipping). The dump path under
`~/.local/share/altimate-code/log/plan-debug/` is no longer written.
Typecheck clean. 128 affected tests pass.
* fix: J1 + J2 from PR #888 multi-LLM review
J1 — exact family-string match misses real gateway values
`system.ts` (and `prompt.ts:isAnthropicLikeModel`) compared
`model.family` exactly against `"anthropic"` / `"gemini"`. But the
model registry (`models-api.json`) uses *specific* family values like
`claude-sonnet`, `claude-haiku`, `gemini-pro`, `gemini-flash`,
`gpt-codex`, etc. — never the coarse literals. Any config-added
Claude or Gemini model on the altimate-backend gateway therefore
silently fell through to `PROMPT_CODEX` — recreating the GH #887
misrouting class this PR was meant to fix. Latent today because the
only statically-registered altimate-backend model is `altimate-default`
with `family: "openai"` (coarse).
Fix: shared `familyVendor(family)` helper in `src/provider/family.ts`
that maps `claude` / `claude-*` / `anthropic` → `"anthropic"`,
`gemini` / `gemini-*` → `"gemini"`, `openai` / `openai-compatible` /
`gpt` / `gpt-*` → `"openai"`. Used by both `SystemPrompt.provider`
(altimate-backend block) and `isAnthropicLikeModel`. `isAnthropicLikeModel`
also tightens the api.id substring matches (lowercased; `claude-`
anchored to the last URL segment so `foo-claude-bench` doesn't
false-match; `anthropic-`/`anthropic/` anchored to the path).
J2 — experimental plan-mode reminders replay as user-role
`<system-reminder>` on turn 2+
Under `OPENCODE_EXPERIMENTAL_PLAN_MODE=true`, `insertReminders` calls
`Session.updatePart(...)` to persist the entering-plan-mode and
build-switch reminder parts. The persisted row was written *without*
`ignored: true`. The caller mutated `part.ignored = true` only on the
in-memory copy, so on the next turn the DB-reloaded part had
`ignored: undefined`, slipped past the `!part.ignored` filter in
`toModelMessages`, and re-entered the user-role payload as
`<system-reminder>...</system-reminder>` — re-triggering the GPT-5.x
refusal class this PR fixes. Bug was flag-gated so the default path
was unaffected, but the PR ships tests pinned to the experimental
branches so it shipped the bug latent under the flag.
Fix: push the hoist decision *into* `insertReminders`. The function
now takes `model`, computes `nonAnthropic = !isAnthropicLikeModel(...)`
once, and stamps `ignored: true` onto every trusted reminder at
insertion time — including the `Session.updatePart` payloads, so the
persisted row carries the flag across turns. Caller in `loop()`
no longer mutates `part.ignored`; the contract is now owned by
`insertReminders`. The hoist branch in `loop()` reduces to a simple
`.map(p => p.text)` over `trustedReminderParts`.
Tests
- New: `test/provider/family.test.ts` table-drives `familyVendor`
against every coarse-vs-specific case (claude/claude-haiku/
claude-opus/claude-sonnet/gemini/gemini-pro/gemini-flash/gpt/
gpt-codex), case-insensitivity, and the unknown→undefined contract.
- `test/session/plan-layer-e2e.test.ts`: regression guards that
(a) `system.ts` and `prompt.ts:isAnthropicLikeModel` both consume
`familyVendor` and no exact `family === "anthropic"` shape lingers
in the altimate-backend block; (b) `insertReminders` takes `model`
and stamps `nonAnthropic ? { ignored: true } : {}` onto persisted
reminder writes; (c) the caller no longer mutates `part.ignored`.
154 affected tests pass. Typecheck clean.
Addresses #888 review J1, J2, and N1 (case-sensitivity / over-broad
api.id substring) and N2 in spirit (`isAnthropicLikeModel` now has its
classifier under direct test via `familyVendor`).
---------
Co-authored-by: Haider <[email protected]>
1 parent cdf89a6 commit f5e10cc
10 files changed
Lines changed: 425 additions & 84 deletions
File tree
- packages/opencode
- src
- provider
- session
- prompt
- test
- provider
- session
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
306 | | - | |
| 306 | + | |
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
| |||
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
351 | 368 | | |
352 | 369 | | |
353 | 370 | | |
354 | | - | |
| 371 | + | |
355 | 372 | | |
356 | 373 | | |
357 | 374 | | |
| |||
381 | 398 | | |
382 | 399 | | |
383 | 400 | | |
384 | | - | |
385 | | - | |
386 | | - | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
387 | 408 | | |
388 | 409 | | |
389 | 410 | | |
| |||
424 | 445 | | |
425 | 446 | | |
426 | 447 | | |
| 448 | + | |
427 | 449 | | |
428 | 450 | | |
429 | 451 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
688 | 691 | | |
689 | 692 | | |
690 | 693 | | |
691 | | - | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
692 | 702 | | |
693 | 703 | | |
694 | 704 | | |
| 705 | + | |
695 | 706 | | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
696 | 712 | | |
697 | 713 | | |
698 | 714 | | |
| |||
968 | 984 | | |
969 | 985 | | |
970 | 986 | | |
| 987 | + | |
971 | 988 | | |
972 | 989 | | |
973 | 990 | | |
| |||
2039 | 2056 | | |
2040 | 2057 | | |
2041 | 2058 | | |
2042 | | - | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
| 2082 | + | |
| 2083 | + | |
| 2084 | + | |
| 2085 | + | |
| 2086 | + | |
| 2087 | + | |
| 2088 | + | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
| 2093 | + | |
| 2094 | + | |
| 2095 | + | |
| 2096 | + | |
| 2097 | + | |
| 2098 | + | |
| 2099 | + | |
| 2100 | + | |
| 2101 | + | |
| 2102 | + | |
| 2103 | + | |
2043 | 2104 | | |
2044 | | - | |
| 2105 | + | |
2045 | 2106 | | |
2046 | 2107 | | |
2047 | 2108 | | |
2048 | 2109 | | |
2049 | | - | |
| 2110 | + | |
2050 | 2111 | | |
2051 | 2112 | | |
2052 | 2113 | | |
2053 | 2114 | | |
2054 | 2115 | | |
2055 | 2116 | | |
2056 | | - | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
2057 | 2121 | | |
2058 | 2122 | | |
2059 | 2123 | | |
2060 | | - | |
| 2124 | + | |
2061 | 2125 | | |
2062 | 2126 | | |
2063 | 2127 | | |
2064 | 2128 | | |
2065 | 2129 | | |
2066 | 2130 | | |
2067 | | - | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
2068 | 2135 | | |
2069 | | - | |
| 2136 | + | |
2070 | 2137 | | |
2071 | 2138 | | |
2072 | 2139 | | |
| |||
2085 | 2152 | | |
2086 | 2153 | | |
2087 | 2154 | | |
| 2155 | + | |
2088 | 2156 | | |
2089 | 2157 | | |
| 2158 | + | |
2090 | 2159 | | |
2091 | | - | |
| 2160 | + | |
2092 | 2161 | | |
2093 | 2162 | | |
2094 | 2163 | | |
| |||
2188 | 2257 | | |
2189 | 2258 | | |
2190 | 2259 | | |
| 2260 | + | |
2191 | 2261 | | |
2192 | 2262 | | |
2193 | | - | |
| 2263 | + | |
| 2264 | + | |
2194 | 2265 | | |
2195 | | - | |
| 2266 | + | |
2196 | 2267 | | |
| 2268 | + | |
2197 | 2269 | | |
2198 | 2270 | | |
2199 | 2271 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
23 | 39 | | |
24 | 40 | | |
25 | 41 | | |
| |||
0 commit comments