[PR #4913] [MERGED] feat(capability): scoped roleplay context, world-state reads, and structured actions for packages #6540

Closed
opened 2026-10-23 04:11:23 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/4913
Author: @Gunterlie
Created: 8/11/2026
Status: ✅ Merged
Merged: 8/13/2026
Merged by: @Gunterlie

Base: staging ← Head: at-the-service-of-your-majesty


📝 Commits (10+)

  • f13a74f feat(capability): expose scoped world state to package prompts
  • e52fcd5 feat(capability): route structured conversation actions
  • e1b4134 feat(capability): advertise package conversation actions
  • fe236a8 feat(capability): validate action payloads, idempotent dispatch, roleplay events
  • 175a140 feat(capability): read + audience-filter roleplay events into the prompt
  • 3cc365f fix(capability): stop truncating action payloads that contain ]
  • ee6718b fix(capability): scope roleplay-event storage per chat instead of a global scan
  • 8337e08 fix(capability): make conversation actions retry-safe
  • 204120f Merge branch 'staging' into at-the-service-of-your-majesty
  • e1149c7 docs(capability): clarify action handler retries

📊 Changes

13 files changed (+518 additions, -30 deletions)

View changed files

📝 packages/server/src/db/schema/capability-documents.ts (+18 -11)
📝 packages/server/src/routes/generate.routes.ts (+75 -12)
📝 packages/server/src/services/capability-packages/capability-command-registry.service.ts (+81 -4)
📝 packages/server/src/services/capability-packages/capability-module-runtime.service.ts (+9 -3)
📝 packages/server/src/services/capability-packages/capability-persistence.service.ts (+75 -0)
📝 packages/server/src/services/capability-packages/capability-prompt-context.service.ts (+4 -0)
➕ packages/server/src/services/capability-packages/capability-roleplay-events.service.ts (+81 -0)
📝 packages/server/src/services/conversation/character-commands.ts (+1 -0)
📝 packages/server/src/services/generation/conversation-command-runtime.ts (+4 -0)
📝 packages/server/src/services/storage/chats.storage.ts (+32 -0)
📝 packages/shared/src/schemas/capability-package.schema.ts (+1 -0)
📝 packages/shared/src/types/capability-runtime.ts (+36 -0)
➕ scripts/regressions/capability-conversation-actions.regression.ts (+101 -0)

📄 Description

What

Four deterministic capability APIs that let a feature package participate in roleplay generation — no new background LLM work; all plumbing runs off data the Engine already has.

  1. Prompt context in every mode. Package prompt-context contributors previously ran only on the Game path; they now run in Conversation and Roleplay too, and receive the resolved targetCharacterIds + personaId for the turn.
  2. Read-only World State projection. getGameState(chatId) on the package persistence host returns the latest committed snapshot (date, time, location, weather, temperature, present characters, source message + swipe). Optional, so packages feature-detect and degrade on older Engines.
  3. Structured conversation actions. Registered capability commands now preserve their payload instead of dropping it. A package registers a handler, an optional payload validator, and a size cap; the parser rejects oversized/invalid payloads before dispatch. Actions carry chat, source message, swipe, and target character.
  4. Roleplay events (read + write). appendRoleplayEvent persists a durable event with an audience (public / user-only / {characterIds}) and an idempotency key. A new Engine-owned reader (collectRoleplayEventContext) injects the recent, in-audience events back into the prompt — a private event is only shown to a turn that can write every character in its audience, so a group generation can't leak it.

Idempotency: dispatch is keyed on chat:message:swipe:command and backed by a persistent claim on the source message's swipe, so regeneration or restart can't double-fire an action or duplicate an event.

Why

Feature packages (Virtual Phone is the driving case) could read the story but had no scoped, safe way to feed state back into generation or to act during a turn. These are the smallest primitives that close that loop without a second model call.

Fixed during review

  • Action payloads containing ] were truncated. The command parser/stripper matched up to the first ], so any JSON payload with a bracket in it (an array, a bracketed note) produced invalid JSON and left a stray }] in the visible transcript when stripped. Fixed both regexes to match a full {…} run; regression added.
  • Roleplay-event storage scanned across all chats. appendRoleplayEvent's idempotency check and the new prompt-time read both scoped the synthetic document owner to a flat __engine__, capped at a 200-row global window — on a busy multi-chat instance, this chat's own recent events could fall outside that window. Both sides now scope by __engine__:<chatId>, so reads and the idempotency check are chat-local instead of a global scan.

Validation

  • Shared build + server tsc clean on the current staging base.
  • capability-conversation-actions regression: payload preservation (including bracket-safe payloads), oversized/validator rejection, dispatch idempotency, audience-filter truth table, per-chat event-owner scoping.
  • Prompt regression suite passes.
  • Exercised end-to-end by the Virtual Phone package (send_message / note_to_self / post_noodle actions, scoped prompt context, World State clock, private + public events).

Not yet run in a live Engine — validated by types + regressions only.

Known shortcut (flagged, not blocking)

  • dispatchedActions is an in-memory Set that never clears — the persistent message-extra claim on the message swipe is the real guard; the Set can be dropped or bounded on a follow-up.

Happy to split into smaller PRs (world-state read / prompt-context / actions / events) if that's easier to land.

Summary by CodeRabbit

  • New Features

    • AI-generated content now incorporates relevant game state, roleplay events, selected characters, and persona context.
    • Added capability conversation actions with payload validation, size limits, descriptions, and examples.
    • Added audience-aware roleplay event storage and retrieval with duplicate prevention.
    • Added support for reading the latest game-state snapshot.
    • Capability commands can now execute actions reliably without duplicate processing.
  • Bug Fixes

    • Improved handling of malformed command metadata and complex JSON payloads.
    • Failed actions can now be retried safely.

🔄 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/4913 **Author:** [@Gunterlie](https://github.com/Gunterlie) **Created:** 8/11/2026 **Status:** ✅ Merged **Merged:** 8/13/2026 **Merged by:** [@Gunterlie](https://github.com/Gunterlie) **Base:** `staging` ← **Head:** `at-the-service-of-your-majesty` --- ### 📝 Commits (10+) - [`f13a74f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f13a74ff797247b63402c4c6ed97a325fe088886) feat(capability): expose scoped world state to package prompts - [`e52fcd5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e52fcd5ff9d0e780ff398f3fb737e221a0fef86f) feat(capability): route structured conversation actions - [`e1b4134`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e1b41347795676d7ed4ebe156ac3c4c74962473e) feat(capability): advertise package conversation actions - [`fe236a8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fe236a8554028f8524d0891607dc24199625e099) feat(capability): validate action payloads, idempotent dispatch, roleplay events - [`175a140`](https://github.com/Pasta-Devs/Marinara-Engine/commit/175a1400468d8f493689135d8e02a365fe486e57) feat(capability): read + audience-filter roleplay events into the prompt - [`3cc365f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3cc365fe215e631b1e786c309ba8b18a50886fec) fix(capability): stop truncating action payloads that contain ] - [`ee6718b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ee6718bcf707e2ac3226b6bcf3e5c32fdf28ff1a) fix(capability): scope roleplay-event storage per chat instead of a global scan - [`8337e08`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8337e08dcd1c4e1ccf33ac0cda7d3cf84181d9a4) fix(capability): make conversation actions retry-safe - [`204120f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/204120f64f91d06a6a00976e120fa51758a0fad7) Merge branch 'staging' into at-the-service-of-your-majesty - [`e1149c7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e1149c78bf4b7db0a8a888a81790b2ba0895d706) docs(capability): clarify action handler retries ### 📊 Changes **13 files changed** (+518 additions, -30 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/db/schema/capability-documents.ts` (+18 -11) 📝 `packages/server/src/routes/generate.routes.ts` (+75 -12) 📝 `packages/server/src/services/capability-packages/capability-command-registry.service.ts` (+81 -4) 📝 `packages/server/src/services/capability-packages/capability-module-runtime.service.ts` (+9 -3) 📝 `packages/server/src/services/capability-packages/capability-persistence.service.ts` (+75 -0) 📝 `packages/server/src/services/capability-packages/capability-prompt-context.service.ts` (+4 -0) ➕ `packages/server/src/services/capability-packages/capability-roleplay-events.service.ts` (+81 -0) 📝 `packages/server/src/services/conversation/character-commands.ts` (+1 -0) 📝 `packages/server/src/services/generation/conversation-command-runtime.ts` (+4 -0) 📝 `packages/server/src/services/storage/chats.storage.ts` (+32 -0) 📝 `packages/shared/src/schemas/capability-package.schema.ts` (+1 -0) 📝 `packages/shared/src/types/capability-runtime.ts` (+36 -0) ➕ `scripts/regressions/capability-conversation-actions.regression.ts` (+101 -0) </details> ### 📄 Description ## What Four deterministic capability APIs that let a feature package participate in roleplay generation — no new background LLM work; all plumbing runs off data the Engine already has. 1. **Prompt context in every mode.** Package prompt-context contributors previously ran only on the Game path; they now run in Conversation and Roleplay too, and receive the resolved `targetCharacterIds` + `personaId` for the turn. 2. **Read-only World State projection.** `getGameState(chatId)` on the package persistence host returns the latest committed snapshot (date, time, location, weather, temperature, present characters, source message + swipe). Optional, so packages feature-detect and degrade on older Engines. 3. **Structured conversation actions.** Registered capability commands now preserve their payload instead of dropping it. A package registers a handler, an optional payload validator, and a size cap; the parser rejects oversized/invalid payloads before dispatch. Actions carry chat, source message, swipe, and target character. 4. **Roleplay events (read + write).** `appendRoleplayEvent` persists a durable event with an audience (`public` / `user-only` / `{characterIds}`) and an idempotency key. A new Engine-owned reader (`collectRoleplayEventContext`) injects the recent, in-audience events back into the prompt — a private event is only shown to a turn that can write every character in its audience, so a group generation can't leak it. Idempotency: dispatch is keyed on `chat:message:swipe:command` and backed by a persistent claim on the source message's swipe, so regeneration or restart can't double-fire an action or duplicate an event. ## Why Feature packages (Virtual Phone is the driving case) could read the story but had no scoped, safe way to feed state back into generation or to act during a turn. These are the smallest primitives that close that loop without a second model call. ## Fixed during review - **Action payloads containing `]` were truncated.** The command parser/stripper matched up to the first `]`, so any JSON payload with a bracket in it (an array, a bracketed note) produced invalid JSON and left a stray `}]` in the visible transcript when stripped. Fixed both regexes to match a full `{…}` run; regression added. - **Roleplay-event storage scanned across all chats.** `appendRoleplayEvent`'s idempotency check and the new prompt-time read both scoped the synthetic document owner to a flat `__engine__`, capped at a 200-row global window — on a busy multi-chat instance, this chat's own recent events could fall outside that window. Both sides now scope by `__engine__:<chatId>`, so reads and the idempotency check are chat-local instead of a global scan. ## Validation - Shared build + server `tsc` clean on the current staging base. - `capability-conversation-actions` regression: payload preservation (including bracket-safe payloads), oversized/validator rejection, dispatch idempotency, audience-filter truth table, per-chat event-owner scoping. - Prompt regression suite passes. - Exercised end-to-end by the Virtual Phone package (`send_message` / `note_to_self` / `post_noodle` actions, scoped prompt context, World State clock, private + public events). **Not yet run in a live Engine** — validated by types + regressions only. ## Known shortcut (flagged, not blocking) - `dispatchedActions` is an in-memory `Set` that never clears — the persistent message-extra claim on the message swipe is the real guard; the Set can be dropped or bounded on a follow-up. Happy to split into smaller PRs (world-state read / prompt-context / actions / events) if that's easier to land. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * AI-generated content now incorporates relevant game state, roleplay events, selected characters, and persona context. * Added capability conversation actions with payload validation, size limits, descriptions, and examples. * Added audience-aware roleplay event storage and retrieval with duplicate prevention. * Added support for reading the latest game-state snapshot. * Capability commands can now execute actions reliably without duplicate processing. * **Bug Fixes** * Improved handling of malformed command metadata and complex JSON payloads. * Failed actions can now be retried safely. <!-- 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-23 04:11:23 +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#6540
No description provided.