Skip to content

Commit 77ffd92

Browse files
committed
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).
1 parent a88f406 commit 77ffd92

6 files changed

Lines changed: 98 additions & 74 deletions

File tree

‎packages/opencode/src/session/llm.ts‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -64,48 +64,6 @@ export namespace LLM {
6464
])
6565
const isCodex = provider.id === "openai" && auth?.type === "oauth"
6666

67-
// altimate_change start — for non-Anthropic models, hoist `<system-reminder>`-wrapped
68-
// synthetic text parts out of user messages and into the system array.
69-
//
70-
// Plan mode (and a few other paths) attaches PROMPT_PLAN as a text part on the latest
71-
// user message, wrapped in `<system-reminder>...</system-reminder>` tags — an Anthropic-
72-
// Claude convention that Claude is trained to recognize as legitimate system-level
73-
// instructions even when it arrives in a user-role message. OpenAI / Google / etc. are
74-
// NOT trained on that convention. When GPT-5.x via the altimate gateway sees a user
75-
// message that contains a `<system-reminder>` block with aggressive constraint language
76-
// (STRICTLY FORBIDDEN, ZERO exceptions, MUST, ABSOLUTE CONSTRAINT), it pattern-matches
77-
// it as a prompt-injection attempt and emits a content-policy refusal ("I'm sorry, but
78-
// I cannot assist with that request.") — reliably, even on benign requests like
79-
// "add a button". GH #887 / AI-6957. Hoisting these parts into actual system-role
80-
// messages routes around the misinterpretation.
81-
const isAnthropicLike =
82-
input.model.providerID === "anthropic" ||
83-
input.model.providerID === "google-vertex-anthropic" ||
84-
input.model.family === "anthropic" ||
85-
input.model.api.npm === "@ai-sdk/anthropic" ||
86-
input.model.api.id.includes("anthropic") ||
87-
input.model.api.id.includes("claude")
88-
const hoistedReminders: string[] = []
89-
if (!isAnthropicLike) {
90-
input.messages = input.messages.map((m) => {
91-
if (m.role !== "user" || !Array.isArray(m.content)) return m
92-
const kept: typeof m.content = []
93-
for (const part of m.content) {
94-
if (part.type === "text" && typeof part.text === "string" && part.text.trimStart().startsWith("<system-reminder>")) {
95-
hoistedReminders.push(part.text)
96-
} else {
97-
kept.push(part)
98-
}
99-
}
100-
return { ...m, content: kept }
101-
})
102-
// Drop user messages that became empty (had only system-reminder parts).
103-
input.messages = input.messages.filter(
104-
(m) => !(m.role === "user" && Array.isArray(m.content) && m.content.length === 0),
105-
)
106-
}
107-
// altimate_change end
108-
10967
const system = []
11068
system.push(
11169
[
@@ -114,10 +72,6 @@ export namespace LLM {
11472
...(input.agent.prompt ? [input.agent.prompt] : isCodex ? [] : SystemPrompt.provider(input.model)),
11573
// any custom prompt passed into this call
11674
...input.system,
117-
// altimate_change start — hoisted reminders appended after the base provider
118-
// prompt so the model sees them as authoritative session-level instructions.
119-
...hoistedReminders,
120-
// altimate_change end
12175
// any custom prompt from last user message
12276
...(input.user.system ? [input.user.system] : []),
12377
]

‎packages/opencode/src/session/processor.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export namespace SessionProcessor {
303303
})
304304
break
305305

306-
case "finish-step":
306+
case "finish-step": {
307307
const usage = Session.getUsage({
308308
model: input.model,
309309
usage: value.usage,
@@ -445,6 +445,7 @@ export namespace SessionProcessor {
445445
needsCompaction = true
446446
}
447447
break
448+
}
448449

449450
case "text-start":
450451
currentText = {

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,14 @@ export namespace SessionPrompt {
694694
session,
695695
})
696696

697+
// altimate_change start — hoist synthetic <system-reminder> parts into the
698+
// system prompt for non-Anthropic-like models. The synthetic flag is the
699+
// trust boundary: only parts written by insertReminders (PROMPT_PLAN /
700+
// BUILD_SWITCH) qualify; raw user text starting with `<system-reminder>`
701+
// is left untouched and never gains system-role priority. See #887/#888.
702+
const hoistedReminders = hoistSyntheticReminders(msgs, model)
703+
// altimate_change end
704+
697705
// altimate_change start — plan refinement detection and telemetry
698706
if (agent.name === "plan") {
699707
// Check if plan file has been written in a previous step
@@ -968,6 +976,7 @@ export namespace SessionPrompt {
968976
...(skills ? [skills] : []),
969977
...(knowledgeInjection ? [knowledgeInjection] : []),
970978
...(await InstructionPrompt.system()),
979+
...hoistedReminders,
971980
]
972981
const format = lastUser.format ?? { type: "text" }
973982
if (format.type === "json_schema") {
@@ -2039,6 +2048,43 @@ export namespace SessionPrompt {
20392048
}
20402049
}
20412050

2051+
// altimate_change start — trust-aware <system-reminder> hoist for non-Anthropic models
2052+
function isAnthropicLikeModel(model: Provider.Model): boolean {
2053+
return (
2054+
model.providerID === "anthropic" ||
2055+
model.providerID === "google-vertex-anthropic" ||
2056+
model.family?.toLowerCase() === "anthropic" ||
2057+
model.api.npm === "@ai-sdk/anthropic" ||
2058+
model.api.id.includes("anthropic") ||
2059+
model.api.id.includes("claude")
2060+
)
2061+
}
2062+
2063+
function hoistSyntheticReminders(messages: MessageV2.WithParts[], model: Provider.Model): string[] {
2064+
// Anthropic-family models recognize <system-reminder>...</system-reminder> on a
2065+
// user-role text part as authoritative — keep that behavior unchanged.
2066+
// Other models (observed: GPT-5.x via altimate-backend gateway) pattern-match
2067+
// those tags inside user content as prompt-injection attempts and refuse.
2068+
// For them, lift our internally-generated reminders into a proper system-role
2069+
// message. The synthetic flag is the trust boundary — only parts we ourselves
2070+
// wrote in insertReminders qualify; raw user text starting with the same
2071+
// characters is left intact and never gains system priority.
2072+
if (isAnthropicLikeModel(model)) return []
2073+
const hoisted: string[] = []
2074+
for (const msg of messages) {
2075+
if (msg.info.role !== "user") continue
2076+
for (const part of msg.parts) {
2077+
if (part.type !== "text") continue
2078+
if (!part.synthetic) continue
2079+
if (!part.text.trimStart().startsWith("<system-reminder>")) continue
2080+
hoisted.push(part.text)
2081+
part.ignored = true
2082+
}
2083+
}
2084+
return hoisted
2085+
}
2086+
// altimate_change end
2087+
20422088
async function insertReminders(input: { messages: MessageV2.WithParts[]; agent: Agent.Info; session: Session.Info }) {
20432089
const userMessage = input.messages.findLast((msg) => msg.info.role === "user")
20442090
if (!userMessage) return input.messages

‎packages/opencode/src/session/system.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ export namespace SystemPrompt {
3333
// before the api.id string-match fallthrough. The gateway's model.api.id is
3434
// the opaque alias `altimate-default` (kept stable for backward compat —
3535
// users persist it in model.json), which matches none of the patterns below.
36-
// Without this guard it fell through to PROMPT_ANTHROPIC_WITHOUT_TODO
37-
// (qwen.txt), whose leading "MUST refuse if borderline/malicious" Claude-
38-
// style directives, stacked on plan.txt's "STRICTLY FORBIDDEN/ZERO
39-
// exceptions" language, pushed the upstream GPT-5.x model into refusing
40-
// benign requests like "add a button to the UI" (see #887 / AI-6957).
41-
// Family is the authoritative signal — the provider definition pins it.
36+
// Defense in depth: without this guard a future provider misconfiguration
37+
// could route an OpenAI-backed gateway model to PROMPT_ANTHROPIC_WITHOUT_TODO.
38+
// Family lookup is case-insensitive since `family` is a free-form string in
39+
// the Model schema. Unknown families default to PROMPT_CODEX because the
40+
// gateway is registered as `@ai-sdk/openai-compatible`.
4241
if (model.providerID === "altimate-backend") {
43-
if (model.family === "openai") return [PROMPT_CODEX]
44-
if (model.family === "anthropic") return [PROMPT_ANTHROPIC]
45-
if (model.family === "gemini") return [PROMPT_GEMINI]
42+
const family = model.family?.toLowerCase()
43+
if (family === "anthropic") return [PROMPT_ANTHROPIC]
44+
if (family === "gemini") return [PROMPT_GEMINI]
45+
return [PROMPT_CODEX]
4646
}
4747
// altimate_change end
4848
if (model.api.id.includes("gpt-5")) return [PROMPT_CODEX]

‎packages/opencode/test/session/processor.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,10 @@ describe("plan-agent no-tool-generation detection", () => {
544544
})
545545

546546
// Regression guard: SessionProcessor.create() is called per-step by loop(),
547-
// so sessionToolCallsMade is per-step in practice (not session-wide as the
548-
// 2026-06-04 comment claimed). A multi-step plan-mode session that runs many
549-
// tools and then produces a final text-only step would false-positive without
550-
// also consulting the conversation history. See processor.ts comment block.
547+
// so sessionToolCallsMade is per-step in practice. A multi-step plan-mode
548+
// session that runs many tools and then produces a final text-only step
549+
// would false-positive without also consulting the conversation history.
550+
// See processor.ts comment block.
551551
test("does not fire on final text-only step when prior steps used tools", () => {
552552
const result = simulateFinishStep({
553553
...baseOpts,

‎packages/opencode/test/session/system.test.ts‎

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { SystemPrompt } from "../../src/session/system"
66
import type { Provider } from "../../src/provider/provider"
77
import { tmpdir } from "../fixture/fixture"
88

9-
function makeModel(overrides: Partial<Provider.Model> & { apiId: string; family?: string; providerID?: string }): Provider.Model {
9+
function makeModel(overrides: { apiId: string; family?: string; providerID?: string }): Provider.Model {
1010
return {
1111
id: overrides.apiId as any,
12-
providerID: (overrides.providerID ?? "test") as any,
12+
providerID: (overrides.providerID ?? "test") as Provider.Model["providerID"],
1313
api: { id: overrides.apiId, url: "", npm: "@ai-sdk/openai-compatible" },
1414
name: overrides.apiId,
1515
family: overrides.family,
@@ -32,16 +32,10 @@ function makeModel(overrides: Partial<Provider.Model> & { apiId: string; family?
3232
}
3333

3434
describe("session.system.provider routing", () => {
35-
// Regression guard for the altimate-default routing bug (GH #887 / AI-6957).
36-
// Before this fix, `altimate-default` (api.id literal, kept stable for backward
37-
// compatibility) matched none of the string-based conditions and fell through
38-
// to PROMPT_ANTHROPIC_WITHOUT_TODO (qwen.txt), whose Claude-style "MUST refuse
39-
// if borderline/malicious" directives caused the upstream GPT-5.x gateway
40-
// model to refuse benign requests in plan mode.
4135
// `codex_header.txt` (GPT-5 prompt) is the only base prompt with `## Editing
42-
// constraints` / `apply_patch` — codex-specific. The Claude-style fallback
43-
// (`qwen.txt`) is the only base prompt that begins with "Refuse to write code
44-
// or explain code that may be used maliciously".
36+
// constraints` / `apply_patch`. The Claude-style fallback (`qwen.txt`) is the
37+
// only base prompt that begins with "Refuse to write code or explain code
38+
// that may be used maliciously".
4539

4640
test("altimate-default routes to GPT-5 prompt via provider+family, not the Anthropic fallback", () => {
4741
const prompts = SystemPrompt.provider(
@@ -53,12 +47,41 @@ describe("session.system.provider routing", () => {
5347
expect(prompts[0]).not.toMatch(/Refuse to write code or explain code that may be used maliciously/)
5448
})
5549

56-
test("altimate-backend anthropic-family routes to Claude prompt, not GPT-5 codex", () => {
50+
test("altimate-backend anthropic-family routes to the same Claude prompt as direct anthropic", () => {
5751
const prompts = SystemPrompt.provider(
5852
makeModel({ apiId: "altimate-something", providerID: "altimate-backend", family: "anthropic" }),
5953
)
60-
expect(prompts[0]).not.toMatch(/## Editing constraints/)
61-
expect(prompts[0]).not.toMatch(/Refuse to write code or explain code that may be used maliciously/)
54+
const baselineAnthropic = SystemPrompt.provider(
55+
makeModel({ apiId: "claude-3-7-sonnet", providerID: "anthropic", family: "anthropic" }),
56+
)
57+
expect(prompts).toEqual(baselineAnthropic)
58+
})
59+
60+
test("altimate-backend with unknown / missing family defaults to GPT-5 codex (gateway is openai-compatible)", () => {
61+
// Without this default the unknown-family branch would fall through to
62+
// PROMPT_ANTHROPIC_WITHOUT_TODO, recreating the GH #887 routing problem
63+
// the moment a new family value appears in config.
64+
for (const family of [undefined, "", "unknown-future-family"]) {
65+
const prompts = SystemPrompt.provider(
66+
makeModel({ apiId: "altimate-default", providerID: "altimate-backend", family }),
67+
)
68+
expect(prompts[0]).toMatch(/## Editing constraints/)
69+
expect(prompts[0]).not.toMatch(/Refuse to write code or explain code that may be used maliciously/)
70+
}
71+
})
72+
73+
test("altimate-backend family lookup is case-insensitive", () => {
74+
const lower = SystemPrompt.provider(
75+
makeModel({ apiId: "altimate-default", providerID: "altimate-backend", family: "anthropic" }),
76+
)
77+
const upper = SystemPrompt.provider(
78+
makeModel({ apiId: "altimate-default", providerID: "altimate-backend", family: "Anthropic" }),
79+
)
80+
const mixed = SystemPrompt.provider(
81+
makeModel({ apiId: "altimate-default", providerID: "altimate-backend", family: "ANTHROPIC" }),
82+
)
83+
expect(upper).toEqual(lower)
84+
expect(mixed).toEqual(lower)
6285
})
6386

6487
test("non-altimate openai models still use the existing api.id matching", () => {

0 commit comments

Comments
 (0)