[PR #7737] [MERGED] feat(state-editor): add reusable State Editor for roleplay game state #7904

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7737
Author: @ylli-selmani
Created: 10/16/2026
Status: ✅ Merged
Merged: 10/16/2026
Merged by: @ylli-selmani

Base: staging ← Head: feat/state-editor


📝 Commits (8)

  • cbf5cf8 feat(state-editor): add a reusable State Editor and open it on roleplay game state
  • 1f81d18 feat(state-editor): share the JSON field with Agent Suite and link it to the State Editor
  • e0a2d17 feat(state-editor): open chat variables in the State Editor
  • 67f443f fix(state-editor): use the same plain label on the tracker toolbar button
  • dda4023 feat(state-editor): show which message and swipe the game state belongs to
  • 068c7b4 fix(state-editor): keep warnings steady and fill the snapshot label as messages load
  • 1f7203e fix(state-editor): localize the invalid-JSON and unknown-section messages
  • 46c765f fix(state-editor): refuse to save a section the tracker changed meanwhile

📊 Changes

16 files changed (+1857 additions, -21 deletions)

View changed files

📝 packages/client/src/components/chat/AgentSuiteModal.tsx (+64 -19)
📝 packages/client/src/components/layout/ModalRenderer.tsx (+12 -0)
➕ packages/client/src/components/modals/ChatVariablesEditorModal.tsx (+157 -0)
➕ packages/client/src/components/modals/GameStateEditorModal.tsx (+309 -0)
➕ packages/client/src/components/ui/state-editor/StateEditor.tsx (+494 -0)
➕ packages/client/src/components/ui/state-editor/StateJsonField.tsx (+95 -0)
📝 packages/client/src/features/chat-settings/sections/ChatVariablesSection.tsx (+8 -0)
📝 packages/client/src/features/tracker-panel/components/TrackerDataSidebar.tsx (+5 -0)
📝 packages/client/src/features/tracker-panel/components/TrackerSidebarHeader.tsx (+27 -1)
➕ packages/client/src/lib/state-editor/chat-variables-editor-source.ts (+92 -0)
➕ packages/client/src/lib/state-editor/game-state-editor-source.ts (+75 -0)
➕ packages/client/src/lib/state-editor/state-editor-json.ts (+205 -0)
➕ packages/client/src/lib/state-editor/state-editor.types.ts (+22 -0)
📝 packages/client/src/localization/locales/en.json (+82 -0)
➕ scripts/regressions/state-editor.regression.ts (+208 -0)
📝 scripts/regressions/tsconfig.client-lanes.json (+2 -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

Part of #7736 (this PR covers slices 1 to 3; slices 4 and 5 come in a second PR).

Why this change

  • When a tracker agent gets the game state wrong, there's no comfortable way to fix it in bulk or to find where a value lives. Agent Suite's raw-JSON textarea edits one slice at a time with no search or preview, and editing the storage files doesn't stick while the server runs.
  • Chat variables have the same problem once there are more than a handful: one row at a time, no search, no bulk paste.

What changed

Slices

  • 1. State Editor + roleplay game state (cbf5cf8, label fix 67f443f, snapshot label dda4023, review fixes 068c7b4, 1f7203e and 46c765f). A new { } button ("Open in State Editor") in the tracker panel's settings toolbar opens a State Editor on the current turn's game state: a sections list (chip row on phones), search across every key and value (a result jumps to its line), a JSON editor per section with inline parse errors, and a per-section diff under Review changes before saving. Changed sections are saved in one game-state PATCH aimed at the turn being viewed. A line under the search says which snapshot is open: message #N (the number the chat shows), swipe X of Y, a Latest badge and a one-line excerpt; the raw message id is in its tooltip.
  • 2. Agent Suite uses the shared JSON field (1f81d18). Agent Suite's JSON tracker blocks get the State Editor's line-numbered field, show parse errors while typing and keep Save disabled until the JSON parses. The Tracker data header gets an Open in State Editor link. Text blocks keep their plain textarea.
  • 3. Chat variables source (e0a2d17). Chat Settings → Chat Variables gets a Search and edit all in the State Editor link that opens the chat's {{name}} variables as one JSON map with search, review and save. Saving sends only the names that changed (null for removed ones) through the same metadata save queue as the section, so a {{setvar}} saved meanwhile under another name survives. New names follow the Chat Variables naming rules; names {{setvar}} stored with dots or dashes stay editable. The section list hides when a source has a single section, and the review subtitle only mentions "the turn you're viewing" for game state.

How: the editor (components/ui/state-editor/StateEditor.tsx) only knows sections and a save function. The game-state source reuses AGENT_SUITE_TRACKER_SLICES, so validation and patch building stay shared with Agent Suite; several slices rewrite all of playerStats, so their patches are folded one onto the other instead of built from the same snapshot. Before saving, queued tracker-panel edits are flushed and a fresh snapshot is fetched; if the turn moved on (new reply or swipe), the save is refused with a Reload option instead of overwriting the new turn. On the same turn, a changed section that the tracker also updated after the editor opened is refused the same way, because sections save whole and would undo that update; the opened snapshot stays the baseline (no refetch on focus or reconnect). The snapshot label reads the message from the chat's already-loaded pages (no extra request) and follows those caches as they fill; its #N is the chat's message count minus the loaded messages after it. The JSON field with its line-number gutter (StateJsonField.tsx) is shared by the State Editor and Agent Suite. The chat-variables source (lib/state-editor/chat-variables-editor-source.ts) diffs the draft against the map the editor opened with and checks names, value length and the 500-variable cap before sending. Warnings use the static app accent, and the invalid-JSON lead and unknown-section message are localized (the parser's own detail follows the lead).

Known limits, on purpose:

  • The "turn moved on" and "section changed" checks are client-side, so a write landing in the instant between that check and the PATCH isn't caught by the server. The PATCH still names the exact messageId and swipeIndex the editor opened, so the edit lands on that snapshot and never on the new turn. A server-enforced precondition would close the gap fully; it touches the server route, so it's noted for the second PR instead of growing this client-only one.
  • The tracker slices' own validation messages (for example "Present characters must be a JSON array") are still English. They predate this PR, are shared with Agent Suite and pinned by open-issues.regression.ts, so turning them into localized error codes is tracked in #7738.

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

Manual verification notes

  • Run in a cloud dev copy (desktop 1440×900 and a 390×844 touch viewport): opened the editor from the tracker toolbar and from Agent Suite's Open in State Editor link, searched mood, jumped to Gwendolyn's mood, edited it plus a quest objective, broke and reverted a section's JSON, reviewed the diff, saved, and confirmed the new values via GET /api/chats/:id/game-state. In Agent Suite, broke a tracker block's JSON and saw the inline error with Save disabled.
  • Chat variables (desktop): opened from Chat Variables, searched gull, changed one value, removed one and added one, reviewed and saved, and confirmed the map via GET /api/chats/:id. Adding user is refused at save with "belongs to a built-in macro".
  • Tested by Ylli on a real install from a phone (production build of this branch, over VPN); the snapshot label came from that test.
  • Full pnpm check passed in the cloud copy and on Windows (0 errors). Regressions state-editor, open-issues and token-estimator pass.
  • Local CodeRabbit (coderabbit review --committed --base origin/staging): one trivial finding (the toolbar button said "Edit raw game state"), fixed in 67f443f.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) 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)

Docs are deliberately deferred: the user docs for the State Editor land with the second PR for #7736 (slices 4 and 5), once all entry points exist, and that PR opens the [docs-i18n] follow-up issue for the translated docs. This PR changes no docs.

UI evidence (if applicable)

Which snapshot is open

State Editor header: Editing message #2, swipe 1, Latest badge and an excerpt of the message

Review changes (desktop)

Review changes: per-section diffs for Present Characters and Active Quests

Phone: open, search, jump to the match

Phone: tracker toolbar, editor, search results, jump to line

Phone: invalid JSON, review, saved

Phone: invalid JSON warning, review diff, saved toast

Agent Suite: shared JSON field, then a live parse error (slice 2)

Agent Suite tracker block with line numbers and the Open in State Editor link, and the same block showing a JSON parse error

Chat variables: the new link, and the review before saving (slice 3)

Chat Variables section with the Search and edit all link, and the State Editor review diff for the variables map

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a State Editor for reviewing and editing chat variables and game state, with JSON validation, search, formatting, and change previews.
    • Added entry points to open the editor from chat settings and the tracker panel.
    • Added safeguards for unsaved edits and changes to game state while editing.
  • Bug Fixes
    • Invalid JSON drafts can no longer be saved, and validation errors appear as you edit.

🔄 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/7737 **Author:** [@ylli-selmani](https://github.com/ylli-selmani) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@ylli-selmani](https://github.com/ylli-selmani) **Base:** `staging` ← **Head:** `feat/state-editor` --- ### 📝 Commits (8) - [`cbf5cf8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cbf5cf8d24266520a28fcae21a21e8c319eac573) feat(state-editor): add a reusable State Editor and open it on roleplay game state - [`1f81d18`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1f81d188b393b2805a398c2416418a069d6a7339) feat(state-editor): share the JSON field with Agent Suite and link it to the State Editor - [`e0a2d17`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e0a2d170c22a9ff127da7bb73860a3139882b536) feat(state-editor): open chat variables in the State Editor - [`67f443f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/67f443f31f8b824b5bd26e20c0779814cdc0f9e6) fix(state-editor): use the same plain label on the tracker toolbar button - [`dda4023`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dda4023fe40dac7137b04a34a9105d0051af6eae) feat(state-editor): show which message and swipe the game state belongs to - [`068c7b4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/068c7b4bcd5f421153ec12c2e52941f8e0c38f0c) fix(state-editor): keep warnings steady and fill the snapshot label as messages load - [`1f7203e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1f7203e56a4bcc699d875bff28c457cce0ebbb48) fix(state-editor): localize the invalid-JSON and unknown-section messages - [`46c765f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/46c765fb343ce80d5912729a72cf9ff213474435) fix(state-editor): refuse to save a section the tracker changed meanwhile ### 📊 Changes **16 files changed** (+1857 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/chat/AgentSuiteModal.tsx` (+64 -19) 📝 `packages/client/src/components/layout/ModalRenderer.tsx` (+12 -0) ➕ `packages/client/src/components/modals/ChatVariablesEditorModal.tsx` (+157 -0) ➕ `packages/client/src/components/modals/GameStateEditorModal.tsx` (+309 -0) ➕ `packages/client/src/components/ui/state-editor/StateEditor.tsx` (+494 -0) ➕ `packages/client/src/components/ui/state-editor/StateJsonField.tsx` (+95 -0) 📝 `packages/client/src/features/chat-settings/sections/ChatVariablesSection.tsx` (+8 -0) 📝 `packages/client/src/features/tracker-panel/components/TrackerDataSidebar.tsx` (+5 -0) 📝 `packages/client/src/features/tracker-panel/components/TrackerSidebarHeader.tsx` (+27 -1) ➕ `packages/client/src/lib/state-editor/chat-variables-editor-source.ts` (+92 -0) ➕ `packages/client/src/lib/state-editor/game-state-editor-source.ts` (+75 -0) ➕ `packages/client/src/lib/state-editor/state-editor-json.ts` (+205 -0) ➕ `packages/client/src/lib/state-editor/state-editor.types.ts` (+22 -0) 📝 `packages/client/src/localization/locales/en.json` (+82 -0) ➕ `scripts/regressions/state-editor.regression.ts` (+208 -0) 📝 `scripts/regressions/tsconfig.client-lanes.json` (+2 -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 Part of #7736 (this PR covers slices 1 to 3; slices 4 and 5 come in a second PR). ## Why this change - When a tracker agent gets the game state wrong, there's no comfortable way to fix it in bulk or to find where a value lives. Agent Suite's raw-JSON textarea edits one slice at a time with no search or preview, and editing the storage files doesn't stick while the server runs. - Chat variables have the same problem once there are more than a handful: one row at a time, no search, no bulk paste. ## What changed **Slices** - [x] **1. State Editor + roleplay game state** (`cbf5cf8`, label fix `67f443f`, snapshot label `dda4023`, review fixes `068c7b4`, `1f7203e` and `46c765f`). A new `{ }` button ("Open in State Editor") in the tracker panel's settings toolbar opens a State Editor on the current turn's game state: a sections list (chip row on phones), search across every key and value (a result jumps to its line), a JSON editor per section with inline parse errors, and a per-section diff under **Review changes** before saving. Changed sections are saved in one game-state PATCH aimed at the turn being viewed. A line under the search says which snapshot is open: message #N (the number the chat shows), swipe X of Y, a Latest badge and a one-line excerpt; the raw message id is in its tooltip. - [x] **2. Agent Suite uses the shared JSON field** (`1f81d18`). Agent Suite's JSON tracker blocks get the State Editor's line-numbered field, show parse errors while typing and keep Save disabled until the JSON parses. The Tracker data header gets an **Open in State Editor** link. Text blocks keep their plain textarea. - [x] **3. Chat variables source** (`e0a2d17`). Chat Settings → Chat Variables gets a **Search and edit all in the State Editor** link that opens the chat's `{{name}}` variables as one JSON map with search, review and save. Saving sends only the names that changed (null for removed ones) through the same metadata save queue as the section, so a `{{setvar}}` saved meanwhile under another name survives. New names follow the Chat Variables naming rules; names `{{setvar}}` stored with dots or dashes stay editable. The section list hides when a source has a single section, and the review subtitle only mentions "the turn you're viewing" for game state. How: the editor (`components/ui/state-editor/StateEditor.tsx`) only knows sections and a save function. The game-state source reuses `AGENT_SUITE_TRACKER_SLICES`, so validation and patch building stay shared with Agent Suite; several slices rewrite all of `playerStats`, so their patches are folded one onto the other instead of built from the same snapshot. Before saving, queued tracker-panel edits are flushed and a fresh snapshot is fetched; if the turn moved on (new reply or swipe), the save is refused with a Reload option instead of overwriting the new turn. On the same turn, a changed section that the tracker also updated after the editor opened is refused the same way, because sections save whole and would undo that update; the opened snapshot stays the baseline (no refetch on focus or reconnect). The snapshot label reads the message from the chat's already-loaded pages (no extra request) and follows those caches as they fill; its #N is the chat's message count minus the loaded messages after it. The JSON field with its line-number gutter (`StateJsonField.tsx`) is shared by the State Editor and Agent Suite. The chat-variables source (`lib/state-editor/chat-variables-editor-source.ts`) diffs the draft against the map the editor opened with and checks names, value length and the 500-variable cap before sending. Warnings use the static app accent, and the invalid-JSON lead and unknown-section message are localized (the parser's own detail follows the lead). **Known limits, on purpose:** - The "turn moved on" and "section changed" checks are client-side, so a write landing in the instant between that check and the PATCH isn't caught by the server. The PATCH still names the exact `messageId` and `swipeIndex` the editor opened, so the edit lands on that snapshot and never on the new turn. A server-enforced precondition would close the gap fully; it touches the server route, so it's noted for the second PR instead of growing this client-only one. - The tracker slices' own validation messages (for example "Present characters must be a JSON array") are still English. They predate this PR, are shared with Agent Suite and pinned by `open-issues.regression.ts`, so turning them into localized error codes is tracked in #7738. ## Validation - [x] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Run in a cloud dev copy (desktop 1440×900 and a 390×844 touch viewport): opened the editor from the tracker toolbar and from Agent Suite's Open in State Editor link, searched `mood`, jumped to Gwendolyn's mood, edited it plus a quest objective, broke and reverted a section's JSON, reviewed the diff, saved, and confirmed the new values via `GET /api/chats/:id/game-state`. In Agent Suite, broke a tracker block's JSON and saw the inline error with Save disabled. - Chat variables (desktop): opened from Chat Variables, searched `gull`, changed one value, removed one and added one, reviewed and saved, and confirmed the map via `GET /api/chats/:id`. Adding `user` is refused at save with "belongs to a built-in macro". - Tested by Ylli on a real install from a phone (production build of this branch, over VPN); the snapshot label came from that test. - Full `pnpm check` passed in the cloud copy and on Windows (0 errors). Regressions `state-editor`, `open-issues` and `token-estimator` pass. - Local CodeRabbit (`coderabbit review --committed --base origin/staging`): one trivial finding (the toolbar button said "Edit raw game state"), fixed in `67f443f`. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) 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) Docs are deliberately deferred: the user docs for the State Editor land with the second PR for #7736 (slices 4 and 5), once all entry points exist, and that PR opens the `[docs-i18n]` follow-up issue for the translated docs. This PR changes no docs. ## UI evidence (if applicable) **Which snapshot is open** <img src="https://raw.githubusercontent.com/ylli-selmani/Marinara-Engine-Ylli-Edits/pr-assets/state-editor/10-desktop-snapshot-label.png" width="800" alt="State Editor header: Editing message #2, swipe 1, Latest badge and an excerpt of the message"> **Review changes (desktop)** <img src="https://raw.githubusercontent.com/ylli-selmani/Marinara-Engine-Ylli-Edits/pr-assets/state-editor/5-desktop-review.png" width="800" alt="Review changes: per-section diffs for Present Characters and Active Quests"> **Phone: open, search, jump to the match** <img src="https://raw.githubusercontent.com/ylli-selmani/Marinara-Engine-Ylli-Edits/pr-assets/state-editor/6-phone-open-search-jump.png" width="800" alt="Phone: tracker toolbar, editor, search results, jump to line"> **Phone: invalid JSON, review, saved** <img src="https://raw.githubusercontent.com/ylli-selmani/Marinara-Engine-Ylli-Edits/pr-assets/state-editor/7-phone-invalid-review-saved.png" width="800" alt="Phone: invalid JSON warning, review diff, saved toast"> **Agent Suite: shared JSON field, then a live parse error (slice 2)** <img src="https://raw.githubusercontent.com/ylli-selmani/Marinara-Engine-Ylli-Edits/pr-assets/state-editor/8-desktop-agent-suite.png" width="800" alt="Agent Suite tracker block with line numbers and the Open in State Editor link, and the same block showing a JSON parse error"> **Chat variables: the new link, and the review before saving (slice 3)** <img src="https://raw.githubusercontent.com/ylli-selmani/Marinara-Engine-Ylli-Edits/pr-assets/state-editor/9-desktop-chat-variables.png" width="800" alt="Chat Variables section with the Search and edit all link, and the State Editor review diff for the variables map"> 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a State Editor for reviewing and editing chat variables and game state, with JSON validation, search, formatting, and change previews. * Added entry points to open the editor from chat settings and the tracker panel. * Added safeguards for unsaved edits and changes to game state while editing. * **Bug Fixes** * Invalid JSON drafts can no longer be saved, and validation errors appear as you edit. <!-- 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:08:42 +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#7904
No description provided.