[PR #8865] [MERGED] fix(mari): restate hidden plans and fix OpenRouter credit/context failures #8851

Closed
opened 2026-11-07 17:40:13 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8865
Author: @mayhair
Created: 11/5/2026
Status: ✅ Merged
Merged: 11/5/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/mari-withheld-reply-openrouter-context


📝 Commits (5)

  • 01ccc0c fix(mari): restate withheld replies and recover from context overflow
  • 96ce63c fix(mari): cap output budget and recover from OpenRouter credit limits
  • 0d5a84d fix(mari): withheld-reply notice on the Pi runtime, output cap scoped to the model, loop regressions (review)
  • 647a886 fix(mari): review follow-ups
  • 05b8f07 fix(mari): retry a 402 with Max Tokens switched off at any balance

📊 Changes

3 files changed (+687 additions, -28 deletions)

View changed files

📝 packages/server/src/services/professor-mari/pi-runtime.ts (+12 -8)
📝 packages/server/src/services/professor-mari/workspace-agent.service.ts (+191 -20)
➕ scripts/regressions/professor-mari/agent-loop-resilience.regression.ts (+484 -0)

📄 Description

Closes #8866

Why this change

Two Professor Mari workspace-agent bugs reported by users:

  1. Mari references a plan the user never saw. She writes a full answer, then does a few more checks and ends with something like "I checked X and Y, the original plan I made is still there", but the plan isn't visible anywhere.
    • Cause 1: only the final round's say is saved as her answer; earlier rounds collapse into the step timeline. When she writes the plan, then runs checks, her last round ("the original plan is still there") is all that remains as the reply, and nothing in the protocol told her this.
    • Cause 2: the completion-claim review (reviewRoundClaim) and the "no commands but stop: false" path both drop a frame's say before it is shown to the user. The frame still goes into Mari's history, and nothing tells her it was withheld, so she treats the plan as already shown. Her hidden reasoning is never shown either, and the protocol didn't say so.
  2. OpenRouter models fail with 402. Kimi K3.1 Pro fails before its first action; Gemini 4 Argon fails after one. Both show OpenRouter API error 402: This request requires more credits, or fewer max_tokens. You requested up to 929637 tokens, but can only afford 176108.
    • Cause: Mari used the connection's Max Tokens as her per-reply output budget. On OpenRouter connections that value is often copied from the model's whole window (943,718 for a 1M-context Kimi K3.1 Pro). OpenRouter reserves credits for the prompt plus every requested output token, so the call is refused before the model runs. Gemini's smaller limit survives the first call, then the growing prompt (command output is appended each step) pushes the reservation past the balance.
    • A related gap: a model missing from the catalog with no Max Context set got no client-side trimming, so long runs could also hit "maximum context length is N tokens".

What changed

All changes are in packages/server/src/services/professor-mari/workspace-agent.service.ts:

  • MARI_WITHHELD_REPLY_NOTICE: added to the claim-audit repair prompt and to the no-commands/no-stop repair prompt (when that frame had a say). It tells Mari the user saw none of the previous reply and that she must restate the full plan or answer instead of pointing back to it.
  • Protocol rule: the say field rule now states that hidden reasoning is never shown, that withheld replies must be restated, and that only the final say is kept as the answer, so a plan written before follow-up checks must be repeated there.
  • Output budget cap: mariMaxOutputTokens now returns at most MARI_MAX_OUTPUT_TOKENS (32,768). Mari's replies are short JSON command frames; the cap still leaves room for a large single write plus hidden reasoning on models that can't turn it off. Smaller configured limits are unchanged.
  • Limit recovery in chatCompleteWorkspace (shared by the classic and pi runtimes), up to 2 retries per call:
    • 402 credit limit: parseAffordableMaxTokens reads "can only afford N"; Mari retries with 90% of N as max_tokens. Below 1,024 tokens it surfaces the 402 instead.
    • Context overflow: parseContextOverflowError recognizes "maximum context length is N tokens", context_length_exceeded, "context … too long" and similar. Mari retries with the stated window (or 75% of the configured one), so the existing fitter trims old history.
    • Learned limits stick for the rest of the run, keyed by the run's abort signal (the pi runtime passes a copy of the base options, so mutating them would not persist).
    • Aborts and unrelated errors still throw.
  • History tagging: Mari's in-run assistant frames are now tagged contextKind: "history". Before this, trimming removed old command results but left her replies behind, stacked back to back.
  • New regression: scripts/regressions/professor-mari/agent-loop-resilience.regression.ts.

Maintainer follow-up commits (0d5a84d8b7, 647a8866eb), after an independent review:

  • The Pi runtime (the default) gets the fix too. A Pi turn whose text is withheld (claim audit, held claim, or a turn cut off during a tool call) now steers with MARI_WITHHELD_REPLY_NOTICE, and piRuntimePrompt carries the same rule in Pi's terms. The notice no longer says "in say", since Pi has no say field.
  • The output cap only where it is needed. Instead of capping every provider at 32,768, mariMaxOutputTokens clamps a configured or default Max Tokens to the model's known max output, which fixes values copied from the whole window. The 32,768 cap applies only to OpenRouter connections. A known model's catalog limit never lowers a value the user set within the model's real limit.
  • Learned limits reach the prompts. After a 402 retry, Pi's output-limit repair quotes the learned limit, not the run-start one. With Max Tokens switched off in the send map, the retry sends the lowered limit anyway.
  • Real regressions. agent-loop-resilience.regression.ts now runs the real classic loop and the Pi steer path with stubbed connections. It covers the notice (present when text is withheld, absent otherwise), the 402 retry, the retry bound, abort, learned context windows across calls, the unknown-model overflow case, and contextKind: "history" on in-run frames. 18 single mutations of the fix each fail it.

Validation

  • Reporter tested the branch on Termux: the missing-plan replies no longer happen (seen with Claude models), and Kimi K3.1 Pro and Gemini 4 Argon on OpenRouter now complete Mari tasks.
  • scripts/regressions/professor-mari/agent-loop-resilience.regression.ts: passes.
  • scripts/regressions/android-local-auth.regression.ts: passes.
  • Server tsc --noEmit (the server's lint script): passes, no errors.
  • All scripts/regressions/professor-mari* regressions: 28/30 pass. shell-sandbox and sandbox-secrets fail on the Termux test device on a file-move assertion in code this PR doesn't touch; please confirm they pass in CI.
  • Full pnpm check / pnpm regression not run locally (the Termux device runs out of memory on the full build).

Manual verification needed

  • Run pnpm check
  • Run pnpm regression
  • In the browser: ask Mari for a multi-step change that triggers a verification read, and confirm her final message contains the full plan or answer, with no reference to "the original plan".
  • On OpenRouter with moonshotai/kimi-k3.1-pro and the connection's Max Tokens at its auto-filled ~943k, ask Mari a multi-step question (e.g. "read and see if there's any character called Laura"). Confirm she completes it with no 402.
  • Same with google/gemini-4-argon: confirm she gets past the first action and finishes.
  • With a low OpenRouter balance, confirm the server log shows [Professor Mari] Credit limit from ... retrying with maxTokens=... and the run continues.
  • With a model whose real window is smaller than its connection's Max Context, confirm a long run logs Context overflow ... retrying and continues.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/8865 **Author:** [@mayhair](https://github.com/mayhair) **Created:** 11/5/2026 **Status:** ✅ Merged **Merged:** 11/5/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/mari-withheld-reply-openrouter-context` --- ### 📝 Commits (5) - [`01ccc0c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/01ccc0c09310c80c71d996e2c9e3e6c77e4035b9) fix(mari): restate withheld replies and recover from context overflow - [`96ce63c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/96ce63cd0f1d1a04c9d003f46e618fd51e74edf9) fix(mari): cap output budget and recover from OpenRouter credit limits - [`0d5a84d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0d5a84d8b7ed462dba20f5f4ba8889450761d8d1) fix(mari): withheld-reply notice on the Pi runtime, output cap scoped to the model, loop regressions (review) - [`647a886`](https://github.com/Pasta-Devs/Marinara-Engine/commit/647a8866ebb53e96efcb41d85e65059bff2d1cfe) fix(mari): review follow-ups - [`05b8f07`](https://github.com/Pasta-Devs/Marinara-Engine/commit/05b8f07698fad63efff8ceef8b68175314224b7a) fix(mari): retry a 402 with Max Tokens switched off at any balance ### 📊 Changes **3 files changed** (+687 additions, -28 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/professor-mari/pi-runtime.ts` (+12 -8) 📝 `packages/server/src/services/professor-mari/workspace-agent.service.ts` (+191 -20) ➕ `scripts/regressions/professor-mari/agent-loop-resilience.regression.ts` (+484 -0) </details> ### 📄 Description Closes #8866 ## Why this change Two Professor Mari workspace-agent bugs reported by users: 1. **Mari references a plan the user never saw.** She writes a full answer, then does a few more checks and ends with something like "I checked X and Y, the original plan I made is still there", but the plan isn't visible anywhere. - **Cause 1:** only the final round's `say` is saved as her answer; earlier rounds collapse into the step timeline. When she writes the plan, then runs checks, her last round ("the original plan is still there") is all that remains as the reply, and nothing in the protocol told her this. - **Cause 2:** the completion-claim review (`reviewRoundClaim`) and the "no commands but `stop: false`" path both drop a frame's `say` before it is shown to the user. The frame still goes into Mari's history, and nothing tells her it was withheld, so she treats the plan as already shown. Her hidden reasoning is never shown either, and the protocol didn't say so. 2. **OpenRouter models fail with 402.** Kimi K3.1 Pro fails before its first action; Gemini 4 Argon fails after one. Both show `OpenRouter API error 402: This request requires more credits, or fewer max_tokens. You requested up to 929637 tokens, but can only afford 176108.` - **Cause:** Mari used the connection's Max Tokens as her per-reply output budget. On OpenRouter connections that value is often copied from the model's whole window (943,718 for a 1M-context Kimi K3.1 Pro). OpenRouter reserves credits for the prompt plus every requested output token, so the call is refused before the model runs. Gemini's smaller limit survives the first call, then the growing prompt (command output is appended each step) pushes the reservation past the balance. - A related gap: a model missing from the catalog with no Max Context set got no client-side trimming, so long runs could also hit "maximum context length is N tokens". ## What changed All changes are in `packages/server/src/services/professor-mari/workspace-agent.service.ts`: - **`MARI_WITHHELD_REPLY_NOTICE`**: added to the claim-audit repair prompt and to the no-commands/no-stop repair prompt (when that frame had a `say`). It tells Mari the user saw none of the previous reply and that she must restate the full plan or answer instead of pointing back to it. - **Protocol rule**: the `say` field rule now states that hidden reasoning is never shown, that withheld replies must be restated, and that only the final `say` is kept as the answer, so a plan written before follow-up checks must be repeated there. - **Output budget cap**: `mariMaxOutputTokens` now returns at most `MARI_MAX_OUTPUT_TOKENS` (32,768). Mari's replies are short JSON command frames; the cap still leaves room for a large single write plus hidden reasoning on models that can't turn it off. Smaller configured limits are unchanged. - **Limit recovery** in `chatCompleteWorkspace` (shared by the classic and pi runtimes), up to 2 retries per call: - **402 credit limit**: `parseAffordableMaxTokens` reads "can only afford N"; Mari retries with 90% of N as `max_tokens`. Below 1,024 tokens it surfaces the 402 instead. - **Context overflow**: `parseContextOverflowError` recognizes "maximum context length is N tokens", `context_length_exceeded`, "context … too long" and similar. Mari retries with the stated window (or 75% of the configured one), so the existing fitter trims old history. - Learned limits stick for the rest of the run, keyed by the run's abort signal (the pi runtime passes a copy of the base options, so mutating them would not persist). - Aborts and unrelated errors still throw. - **History tagging**: Mari's in-run assistant frames are now tagged `contextKind: "history"`. Before this, trimming removed old command results but left her replies behind, stacked back to back. - **New regression**: `scripts/regressions/professor-mari/agent-loop-resilience.regression.ts`. Maintainer follow-up commits (0d5a84d8b7, 647a8866eb), after an independent review: - **The Pi runtime (the default) gets the fix too.** A Pi turn whose text is withheld (claim audit, held claim, or a turn cut off during a tool call) now steers with `MARI_WITHHELD_REPLY_NOTICE`, and `piRuntimePrompt` carries the same rule in Pi's terms. The notice no longer says "in say", since Pi has no `say` field. - **The output cap only where it is needed.** Instead of capping every provider at 32,768, `mariMaxOutputTokens` clamps a configured or default Max Tokens to the model's known max output, which fixes values copied from the whole window. The 32,768 cap applies only to OpenRouter connections. A known model's catalog limit never lowers a value the user set within the model's real limit. - **Learned limits reach the prompts.** After a 402 retry, Pi's output-limit repair quotes the learned limit, not the run-start one. With Max Tokens switched off in the send map, the retry sends the lowered limit anyway. - **Real regressions.** `agent-loop-resilience.regression.ts` now runs the real classic loop and the Pi steer path with stubbed connections. It covers the notice (present when text is withheld, absent otherwise), the 402 retry, the retry bound, abort, learned context windows across calls, the unknown-model overflow case, and `contextKind: "history"` on in-run frames. 18 single mutations of the fix each fail it. ## Validation - Reporter tested the branch on Termux: the missing-plan replies no longer happen (seen with Claude models), and Kimi K3.1 Pro and Gemini 4 Argon on OpenRouter now complete Mari tasks. - `scripts/regressions/professor-mari/agent-loop-resilience.regression.ts`: passes. - `scripts/regressions/android-local-auth.regression.ts`: passes. - Server `tsc --noEmit` (the server's `lint` script): passes, no errors. - All `scripts/regressions/professor-mari*` regressions: 28/30 pass. `shell-sandbox` and `sandbox-secrets` fail on the Termux test device on a file-move assertion in code this PR doesn't touch; please confirm they pass in CI. - Full `pnpm check` / `pnpm regression` not run locally (the Termux device runs out of memory on the full build). ### Manual verification needed - [ ] Run `pnpm check` - [ ] Run `pnpm regression` - [x] In the browser: ask Mari for a multi-step change that triggers a verification read, and confirm her final message contains the full plan or answer, with no reference to "the original plan". - [x] On OpenRouter with `moonshotai/kimi-k3.1-pro` and the connection's Max Tokens at its auto-filled ~943k, ask Mari a multi-step question (e.g. "read and see if there's any character called Laura"). Confirm she completes it with no 402. - [x] Same with `google/gemini-4-argon`: confirm she gets past the first action and finishes. - [ ] With a low OpenRouter balance, confirm the server log shows `[Professor Mari] Credit limit from ... retrying with maxTokens=...` and the run continues. - [ ] With a model whose real window is smaller than its connection's Max Context, confirm a long run logs `Context overflow ... retrying` and continues. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:40:13 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#8851
No description provided.