[PR #7223] [MERGED] fix(agents): run the include macro in agent prompts #7631

Closed
opened 2026-10-24 04:06:01 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7223
Author: @SpicyMarinara
Created: 10/7/2026
Status: ✅ Merged
Merged: 10/7/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/include-macro-agent-prompts-7212


📝 Commits (5)

  • 3bf7431 fix(agents): run the include macro in agent prompts
  • e31434b fix(agents): fill includes in a retried agent's author's note
  • 338f59e Merge remote-tracking branch 'origin/staging' into fix/include-macro-agent-prompts-7212
  • bd81980 Merge origin/staging into fix/include-macro-agent-prompts-7212
  • 50088bc Merge origin/staging into fix/include-macro-agent-prompts-7212

📊 Changes

8 files changed (+129 additions, -12 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/routes/generate.routes.ts (+12 -1)
📝 packages/server/src/routes/generate/retry-agents-route.ts (+4 -1)
📝 packages/server/src/services/agents/agent-executor.ts (+1 -0)
📝 packages/server/src/services/prompt/index.ts (+1 -0)
📝 packages/server/src/services/prompt/macro-context.ts (+16 -8)
📝 packages/shared/src/types/agent.ts (+3 -1)
📝 scripts/regressions/lorebook-include-macro.regression.ts (+91 -1)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7212

Thanks to @usagirei for the report, including the agent debug log that showed {{include::Time}} going to the provider unchanged while {{random...}} on the next line was filled in.

Why this change

{{include::ENTRY}} and {{include::BOOK::ENTRY}} (#6912) work in presets, cards, entries and author's notes, and docs/prompts/macros.md lists agent prompt templates as a place where macros work. In agent prompts, though, the include macro reached the model unchanged. Agent prompts are resolved with their own macro context (buildAgentPromptMacroContext), and that context never received the lorebook source the include macro reads. The chat prompt also loads that source only when one of its texts uses the macro, and agent prompt templates were never part of that check.

The reporter wants one shared agent (for example a world tracker) to pick up chat-specific instructions from a lorebook entry with the same name in each chat, without making a separate agent per chat.

What changed

  • AgentContext gets an optional lorebookIncludes field, and buildAgentPromptMacroContext passes it into the macro context. Agent prompts now go through the same expandLorebookIncludes step as the chat prompt. Nothing is copied or reimplemented, so the rules are the same: a name is looked up in the lorebooks this chat uses (its own, its characters' and persona's, and global ones), BOOK::ENTRY and IDs reach any lorebook, disabled entries can be included, and an include that leads back to itself becomes empty.
  • Main generation route: agent prompt templates join the chat prompt's macroSources (next to the agent settings, which already carry alternate prompt templates), so the include source is loaded when an agent prompt uses the macro. The agent context reuses promptMacroContext.lorebookIncludes.
  • Character activity agents get their context before the chat's macro context exists, so they load the source through the same helper.
  • Retry Agents route: same change as the main route. It also checks the chat's author's note, which a retried agent can read, so an include in the note is filled in on a retry as it is on a live turn. Before, a retry loaded lorebooks only when an agent prompt or a message used the macro, so the note's include reached the retried agent as written.
  • The "load only when used, warn and leave the macro as written on failure" block in buildPromptMacroContext moved into loadLorebookIncludesFor, which both the chat prompt and the character activity path use.
  • Regression: scripts/regressions/lorebook-include-macro.regression.ts now runs a real turn with a pre-generation agent, a character activity agent and a post-processing agent, then retries the post-processing agent. Each prompt must contain the included text for both the short form (chat lorebook, disabled entry) and the BOOK::ENTRY form (a disabled lorebook the chat doesn't use). A short form naming an entry in a lorebook the chat doesn't use must come out empty, and no request may still contain a raw {{include::. A last retry checks the author's note case with an agent whose own prompt has no include. It fails on staging and passes here. I also removed each route change one at a time, and the matching assertion failed each time.

Side effect: {{lorebooksize::ID}} in an agent prompt used to read 0 unless the chat prompt also used it. On the main and retry routes it now gives the real count, because the counts use the same source check. I checked this with a throwaway run, not a kept assertion. Character activity agents still get 0 for it because their context never had counts. That's out of scope here.

Also not changed here, because no macro is filled in there today, not just this one: the author's note that character activity agents read, and the Illustrator template that Game mode copies into its image prompt writer.

Agent-package boundary: this is Engine-owned macro and context handling. Package-owned agents run through the same Engine executor and get the fix with no package change. AgentContext.lorebookIncludes is optional and only the Engine reads it, so vendored shared snapshots in Marinara-Agents don't need it.

Contexts without the include source behave as before. If an agent prompt doesn't use the macro, nothing is loaded. If loading fails, a warning is logged and the macro stays as written, the same as in the chat prompt. Professor Mari doesn't render agent prompt templates, so it isn't affected.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Commands run by the agents: pnpm check (exit 0 on both commits), node ./scripts/run-regressions.mjs --filter scripts/regressions/lorebook-include-macro.regression.ts (fails on staging, passes here), pnpm regression:prompt, and these regressions, all passing: agent-activation-questions, agent-activation, agent-connection-parameters, agent-context-generation, agent-previous-output, agent-progress, agent-retry-batching, agent-runtime, agent-scene-check, capability-agent-runtime, character-activity-agent, generate-agent-abort, manual-agent-retry-resolution, decision-conditionals, capability-prompt-preview, illustrator-*, advanced-memory-post-generation, chat-variable-history and others that build a prompt macro context. Local CodeRabbit (coderabbit review --agent --committed) found nothing on the first commit (3bf74311b). For the second commit (e31434bf6) the local run was rate limited, so only the GitHub review covers it.

Also checked with throwaway runs (not kept): two post-processing agents batched into one request both get their includes; an alternate prompt template chosen for the chat gets them too; {{user}}, {{char}} and {{#if}} in an agent prompt come out the same as before; an entry that includes itself stops; and a short form can't reach a lorebook that belongs to another chat. BOOK::ENTRY can, which is how the macro already works in chat prompts (#6912). In a shared room, lorebook listing is already limited to the room's lorebooks, so includes are too.

Manual verification notes

  • Manually verify with debug mode on: add a lorebook entry named Time to a lorebook that the chat uses, put {{include::Time}} in a custom agent's prompt, enable the agent for the chat, and send a message. The [agent-debug] ... provider request log should show the entry text instead of the macro.
  • Manually verify the long form: put {{include::<another lorebook's name>::<entry name>}} in the same prompt for a lorebook that isn't added to the chat, and confirm its text appears in the agent request.
  • Manually verify an edited built-in agent (for example World State) with {{include::Time}} in its prompt, and check that Retry Agents on the last message sends the included text too.
  • Manually verify that a chat without the named entry sends nothing in place of the macro, and that an agent prompt without the macro behaves as before.
  • Manually verify the author's note on a retry: put {{include::Time}} in the chat's author's note (and nowhere in the agent's prompt), then use Retry Agents on a built-in agent such as World State. Its provider request in the debug log should show the entry text inside the author's notes.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

CHANGELOG.md has a new [Unreleased] entry. Docs under docs/ don't change: docs/prompts/macros.md already lists agent prompt templates as a place where macros work, and this PR makes that true for the include macro. No translations are affected.

UI evidence (if applicable)

Not applicable. This is a server-side prompt change.

Summary by CodeRabbit

  • Bug Fixes
    • Lorebook include macros now resolve in agent prompts, including custom agents, edited built-in agents, and retried agents.
    • Agent prompts use the same lorebooks as chat prompts. Includes in chat author notes are also resolved when retrying agents.
    • Lorebook includes in configured agent prompts and settings are resolved during generation, rather than passed to the model as unprocessed macros.

🔄 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/7223 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/7/2026 **Status:** ✅ Merged **Merged:** 10/7/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/include-macro-agent-prompts-7212` --- ### 📝 Commits (5) - [`3bf7431`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3bf74311b9d827e3d2e6bbf7d901e2ad49fdeae4) fix(agents): run the include macro in agent prompts - [`e31434b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e31434bf681895224b4da9ef8ce03dccdba5ccf5) fix(agents): fill includes in a retried agent's author's note - [`338f59e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/338f59ef915f224f0c1cb1dc629fb02c7240e615) Merge remote-tracking branch 'origin/staging' into fix/include-macro-agent-prompts-7212 - [`bd81980`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bd819801e1a6650aad22d7a1e82f864a0bfadb00) Merge origin/staging into fix/include-macro-agent-prompts-7212 - [`50088bc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/50088bc3614e71343fdd421d485ebc633344e64f) Merge origin/staging into fix/include-macro-agent-prompts-7212 ### 📊 Changes **8 files changed** (+129 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+12 -1) 📝 `packages/server/src/routes/generate/retry-agents-route.ts` (+4 -1) 📝 `packages/server/src/services/agents/agent-executor.ts` (+1 -0) 📝 `packages/server/src/services/prompt/index.ts` (+1 -0) 📝 `packages/server/src/services/prompt/macro-context.ts` (+16 -8) 📝 `packages/shared/src/types/agent.ts` (+3 -1) 📝 `scripts/regressions/lorebook-include-macro.regression.ts` (+91 -1) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. ## Linked issue Closes #7212 Thanks to @usagirei for the report, including the agent debug log that showed `{{include::Time}}` going to the provider unchanged while `{{random...}}` on the next line was filled in. ## Why this change `{{include::ENTRY}}` and `{{include::BOOK::ENTRY}}` (#6912) work in presets, cards, entries and author's notes, and `docs/prompts/macros.md` lists agent prompt templates as a place where macros work. In agent prompts, though, the include macro reached the model unchanged. Agent prompts are resolved with their own macro context (`buildAgentPromptMacroContext`), and that context never received the lorebook source the include macro reads. The chat prompt also loads that source only when one of its texts uses the macro, and agent prompt templates were never part of that check. The reporter wants one shared agent (for example a world tracker) to pick up chat-specific instructions from a lorebook entry with the same name in each chat, without making a separate agent per chat. ## What changed - `AgentContext` gets an optional `lorebookIncludes` field, and `buildAgentPromptMacroContext` passes it into the macro context. Agent prompts now go through the same `expandLorebookIncludes` step as the chat prompt. Nothing is copied or reimplemented, so the rules are the same: a name is looked up in the lorebooks this chat uses (its own, its characters' and persona's, and global ones), `BOOK::ENTRY` and IDs reach any lorebook, disabled entries can be included, and an include that leads back to itself becomes empty. - Main generation route: agent prompt templates join the chat prompt's `macroSources` (next to the agent settings, which already carry alternate prompt templates), so the include source is loaded when an agent prompt uses the macro. The agent context reuses `promptMacroContext.lorebookIncludes`. - Character activity agents get their context before the chat's macro context exists, so they load the source through the same helper. - Retry Agents route: same change as the main route. It also checks the chat's author's note, which a retried agent can read, so an include in the note is filled in on a retry as it is on a live turn. Before, a retry loaded lorebooks only when an agent prompt or a message used the macro, so the note's include reached the retried agent as written. - The "load only when used, warn and leave the macro as written on failure" block in `buildPromptMacroContext` moved into `loadLorebookIncludesFor`, which both the chat prompt and the character activity path use. - Regression: `scripts/regressions/lorebook-include-macro.regression.ts` now runs a real turn with a pre-generation agent, a character activity agent and a post-processing agent, then retries the post-processing agent. Each prompt must contain the included text for both the short form (chat lorebook, disabled entry) and the `BOOK::ENTRY` form (a disabled lorebook the chat doesn't use). A short form naming an entry in a lorebook the chat doesn't use must come out empty, and no request may still contain a raw `{{include::`. A last retry checks the author's note case with an agent whose own prompt has no include. It fails on `staging` and passes here. I also removed each route change one at a time, and the matching assertion failed each time. Side effect: `{{lorebooksize::ID}}` in an agent prompt used to read `0` unless the chat prompt also used it. On the main and retry routes it now gives the real count, because the counts use the same source check. I checked this with a throwaway run, not a kept assertion. Character activity agents still get `0` for it because their context never had counts. That's out of scope here. Also not changed here, because no macro is filled in there today, not just this one: the author's note that character activity agents read, and the Illustrator template that Game mode copies into its image prompt writer. Agent-package boundary: this is Engine-owned macro and context handling. Package-owned agents run through the same Engine executor and get the fix with no package change. `AgentContext.lorebookIncludes` is optional and only the Engine reads it, so vendored shared snapshots in Marinara-Agents don't need it. Contexts without the include source behave as before. If an agent prompt doesn't use the macro, nothing is loaded. If loading fails, a warning is logged and the macro stays as written, the same as in the chat prompt. Professor Mari doesn't render agent prompt templates, so it isn't affected. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Commands run by the agents: `pnpm check` (exit 0 on both commits), `node ./scripts/run-regressions.mjs --filter scripts/regressions/lorebook-include-macro.regression.ts` (fails on `staging`, passes here), `pnpm regression:prompt`, and these regressions, all passing: `agent-activation-questions`, `agent-activation`, `agent-connection-parameters`, `agent-context-generation`, `agent-previous-output`, `agent-progress`, `agent-retry-batching`, `agent-runtime`, `agent-scene-check`, `capability-agent-runtime`, `character-activity-agent`, `generate-agent-abort`, `manual-agent-retry-resolution`, `decision-conditionals`, `capability-prompt-preview`, `illustrator-*`, `advanced-memory-post-generation`, `chat-variable-history` and others that build a prompt macro context. Local CodeRabbit (`coderabbit review --agent --committed`) found nothing on the first commit (3bf74311b). For the second commit (e31434bf6) the local run was rate limited, so only the GitHub review covers it. Also checked with throwaway runs (not kept): two post-processing agents batched into one request both get their includes; an alternate prompt template chosen for the chat gets them too; `{{user}}`, `{{char}}` and `{{#if}}` in an agent prompt come out the same as before; an entry that includes itself stops; and a short form can't reach a lorebook that belongs to another chat. `BOOK::ENTRY` can, which is how the macro already works in chat prompts (#6912). In a shared room, lorebook listing is already limited to the room's lorebooks, so includes are too. ### Manual verification notes - Manually verify with debug mode on: add a lorebook entry named `Time` to a lorebook that the chat uses, put `{{include::Time}}` in a custom agent's prompt, enable the agent for the chat, and send a message. The `[agent-debug] ... provider request` log should show the entry text instead of the macro. - Manually verify the long form: put `{{include::<another lorebook's name>::<entry name>}}` in the same prompt for a lorebook that isn't added to the chat, and confirm its text appears in the agent request. - Manually verify an edited built-in agent (for example World State) with `{{include::Time}}` in its prompt, and check that **Retry Agents** on the last message sends the included text too. - Manually verify that a chat without the named entry sends nothing in place of the macro, and that an agent prompt without the macro behaves as before. - Manually verify the author's note on a retry: put `{{include::Time}}` in the chat's author's note (and nowhere in the agent's prompt), then use **Retry Agents** on a built-in agent such as World State. Its provider request in the debug log should show the entry text inside the author's notes. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) `CHANGELOG.md` has a new `[Unreleased]` entry. Docs under `docs/` don't change: `docs/prompts/macros.md` already lists agent prompt templates as a place where macros work, and this PR makes that true for the include macro. No translations are affected. ## UI evidence (if applicable) Not applicable. This is a server-side prompt change. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Lorebook include macros now resolve in agent prompts, including custom agents, edited built-in agents, and retried agents. * Agent prompts use the same lorebooks as chat prompts. Includes in chat author notes are also resolved when retrying agents. * Lorebook includes in configured agent prompts and settings are resolved during generation, rather than passed to the model as unprocessed macros. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:06:01 +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#7631
No description provided.