[PR #5793] [MERGED] feat(mari): applied mutations verify themselves via a store read-back #6937

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5793
Author: @kolacheee
Created: 9/2/2026
Status: ✅ Merged
Merged: 9/3/2026
Merged by: @kolacheee

Base: staging ← Head: feat/mari-write-readback


📝 Commits (7)

  • a13a330 feat(mari): applied mutations verify themselves via a store read-back
  • 620b0f8 fix(mari): anchor read-back detection to an engine-written sentinel
  • 993320e fix(mari): address adversarial review on the read-back (never-weaken holds)
  • 3ee319b fix(mari): pin the merged sentinel ordering after the #5776 rebase
  • 7702bcd fix(mari): make a store-observed persistence failure sticky (review round)
  • d2e7589 fix(mari): verify derived book syncs and key the mismatch alarm (CodeRabbit)
  • af2dc00 fix(mari): carry positional CLI targets in the mismatch key (CodeRabbit)

📊 Changes

8 files changed (+757 additions, -41 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/services/lorebook/character-book-sync.ts (+20 -5)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+168 -3)
📝 packages/server/src/services/professor-mari/workspace-agent.service.ts (+142 -25)
📝 packages/shared/src/types/professor-mari-workspace.ts (+36 -0)
📝 scripts/regressions/mari-guard-sibling-paths.regression.ts (+4 -1)
📝 scripts/regressions/mari-polish.regression.ts (+4 -7)
➕ scripts/regressions/mari-write-readback.regression.ts (+382 -0)

📄 Description

Linked issue

Follow-up to #5754 (the "verification round" issue closed by #5755), implementing the structural direction discussed there: engine-verified outcomes carried on results, per the maintainer's guidance that protecting against silent persistent failures takes priority. No separate issue exists for this follow-up; it was scoped in the #5754 discussion.

Why this change

  • The #5754 fix made Mari verify app-data changes with a follow-up read — but the verification itself still rides the model: she reads, compares, and reports. A model that misreads (or skips the comparison) can still claim success over a change that didn't persist, and every verification read costs a round.
  • This makes applied app-data mutations verify themselves in the engine: after applyPlan (and character-book sync), the engine re-reads the affected rows through the same store layer reads use, compares the persisted state against the intended change column-by-column, and carries the outcome structurally on the result. Mari's claims about persistence stop depending on her own diligence — the engine either confirmed the write or it didn't.

What changed

  • buildReadBack (mari-db.service.ts): post-apply, re-reads every applied row via getRawById and compares the known-column patch against the persisted raw values (stableJson equality); statuses verified / mismatch / unavailable, with capped mismatch details. Cascade child deletions (rows the store deletes for a parent) are asserted absent even though they ride the plan with apply:false. Dry-runs get no read-back. Never throws — a read-back failure degrades to unavailable, which still demands a manual read.
  • Position-zero engine sentinels: commandAppData/commandMariDirect emit Readback: store-verified / Readback: store-mismatch as the output's first line, and the guard trusts only startsWith at position zero — everything later in an output can contain model-authored text (echoed rows, command strings), so substring detection would be forgeable. Same anchoring pattern as #5775's staging marker and #5787's dry-run sentinel; in the mari-CLI runtime the read-back and dry-run sentinels are mutually exclusive (a read-back only rides applied mutations, a dry-run never applies), so position zero stays deterministic.
  • Debt semantics in resolveWorkspaceMutationVerification: every applied mutation without a store-verified read-back adds its own verification debt; only a later successful read clears outstanding debt; a self-verified mutation is debt-free for itself but never pays off an earlier mutation's debt (an adversarial review proved the naive single-boolean form did exactly that). Composed with the staged state from #5775 (priority: unverified > staged > verified/none) and the dry-run gate from #5787.
  • Guard never weakens: a mutation whose result carries no readBack (file writes/edits, copy/move, bash mutations, mari image/code/theme writes) still requires a manual verification read exactly as before, and unavailable read-backs keep that requirement too. A mismatch is sticky: the engine positively observed that a write failed, so no read can clear it — the resolver carries a dedicated mismatch state whose interception coaching tells Mari to report the failure plainly, and only a later store-verified retry (an engine-observed persisted change) clears the alarm. The #5740 diagnostics record reports a mismatched round as failed.
  • Prompt guidance keyed on the readBack field (not prose): results carrying it have verified themselves — no separate verification read needed; results without it keep the read-then-claim coaching. The write-then-verify boilerplate was removed only from bullets covering commands that now self-verify.
  • New mari-write-readback regression lane: a live file-backed-store round-trip (create/update read back verified; dry-run carries no read-back), sentinel end-to-end and forgery cases, debt-semantics orderings, readBackValuesMatch units, and source pins on the emitter gates/ordering, the cascade condition, and the record-mismatch consult.
  • CHANGELOG entry under [Unreleased].

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

  • Commands run locally (all passing): node ./scripts/run-regressions.mjs --filter mari-write-readback (live-store round-trip lane), plus the mari-guard-sibling-paths, prompt, mari-polish, mari-understood-request, and mari-ask-latch lanes; pnpm --filter @marinara-engine/server lint; pnpm build.
  • The branch was adversarially reviewed in three multi-agent rounds across its life (original content, the never-weaken hardening, and a final round on the reconciliation with #5775/#5787); confirmed findings are folded in.
  • Manual browser verification that would still be valuable: ask Mari to update a character and watch her report completion without an extra verification read; then ask her to update something with a deliberately conflicting concurrent edit to see the mismatch path refuse a success claim.

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)

UI evidence (if applicable)

Not applicable — server-side verification behavior only.

Summary by CodeRabbit

  • New Features

    • Applied database and character-book changes are now checked against values saved in the data store.
    • Verification results identify confirmed saves, unavailable checks, or mismatches.
    • Cascade deletions are checked to ensure affected records were removed.
  • Bug Fixes

    • Persistence mismatches are surfaced clearly instead of being reported as successful changes.
    • Failed writes remain unresolved until a later store-verified retry succeeds.
  • Behavior Updates

    • Successful store verification removes the need for a separate confirmation read.
    • Bash and file-based changes continue to require confirmation reads.

🔄 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/5793 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/2/2026 **Status:** ✅ Merged **Merged:** 9/3/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/mari-write-readback` --- ### 📝 Commits (7) - [`a13a330`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a13a330b109fd1e34601bb8982e24361eca6b665) feat(mari): applied mutations verify themselves via a store read-back - [`620b0f8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/620b0f8d3f62772b001065bf284fa278ecb51b3f) fix(mari): anchor read-back detection to an engine-written sentinel - [`993320e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/993320e8f4d567ab1745ba83ecbbed8adff96b31) fix(mari): address adversarial review on the read-back (never-weaken holds) - [`3ee319b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3ee319ba6e1b6edb821ef45ce1a06c780ea060cf) fix(mari): pin the merged sentinel ordering after the #5776 rebase - [`7702bcd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7702bcd230ead5326d849e3d9a9a45cdc54a9ca9) fix(mari): make a store-observed persistence failure sticky (review round) - [`d2e7589`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d2e7589e645abaa297cb2e7720c88bde2fa91227) fix(mari): verify derived book syncs and key the mismatch alarm (CodeRabbit) - [`af2dc00`](https://github.com/Pasta-Devs/Marinara-Engine/commit/af2dc003a3048801fc1ea955003ae204f2247443) fix(mari): carry positional CLI targets in the mismatch key (CodeRabbit) ### 📊 Changes **8 files changed** (+757 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/services/lorebook/character-book-sync.ts` (+20 -5) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+168 -3) 📝 `packages/server/src/services/professor-mari/workspace-agent.service.ts` (+142 -25) 📝 `packages/shared/src/types/professor-mari-workspace.ts` (+36 -0) 📝 `scripts/regressions/mari-guard-sibling-paths.regression.ts` (+4 -1) 📝 `scripts/regressions/mari-polish.regression.ts` (+4 -7) ➕ `scripts/regressions/mari-write-readback.regression.ts` (+382 -0) </details> ### 📄 Description ## Linked issue Follow-up to #5754 (the "verification round" issue closed by #5755), implementing the structural direction discussed there: engine-verified outcomes carried on results, per the maintainer's guidance that protecting against silent persistent failures takes priority. No separate issue exists for this follow-up; it was scoped in the #5754 discussion. ## Why this change - The #5754 fix made Mari *verify* app-data changes with a follow-up read — but the verification itself still rides the model: she reads, compares, and reports. A model that misreads (or skips the comparison) can still claim success over a change that didn't persist, and every verification read costs a round. - This makes applied app-data mutations **verify themselves in the engine**: after `applyPlan` (and character-book sync), the engine re-reads the affected rows through the same store layer reads use, compares the persisted state against the intended change column-by-column, and carries the outcome structurally on the result. Mari's claims about persistence stop depending on her own diligence — the engine either confirmed the write or it didn't. ## What changed - `buildReadBack` (mari-db.service.ts): post-apply, re-reads every applied row via `getRawById` and compares the known-column patch against the persisted raw values (`stableJson` equality); statuses `verified` / `mismatch` / `unavailable`, with capped mismatch details. Cascade child deletions (rows the store deletes for a parent) are asserted absent even though they ride the plan with `apply:false`. Dry-runs get no read-back. Never throws — a read-back failure degrades to `unavailable`, which still demands a manual read. - **Position-zero engine sentinels**: `commandAppData`/`commandMariDirect` emit `Readback: store-verified` / `Readback: store-mismatch` as the output's first line, and the guard trusts only `startsWith` at position zero — everything later in an output can contain model-authored text (echoed rows, command strings), so substring detection would be forgeable. Same anchoring pattern as #5775's staging marker and #5787's dry-run sentinel; in the mari-CLI runtime the read-back and dry-run sentinels are mutually exclusive (a read-back only rides applied mutations, a dry-run never applies), so position zero stays deterministic. - **Debt semantics in `resolveWorkspaceMutationVerification`**: every applied mutation without a store-verified read-back adds its own verification debt; only a later successful read clears outstanding debt; a self-verified mutation is debt-free for itself but never pays off an earlier mutation's debt (an adversarial review proved the naive single-boolean form did exactly that). Composed with the staged state from #5775 (priority: unverified > staged > verified/none) and the dry-run gate from #5787. - **Guard never weakens**: a mutation whose result carries no `readBack` (file writes/edits, copy/move, bash mutations, mari image/code/theme writes) still requires a manual verification read exactly as before, and `unavailable` read-backs keep that requirement too. A **`mismatch` is sticky**: the engine positively observed that a write failed, so no read can clear it — the resolver carries a dedicated `mismatch` state whose interception coaching tells Mari to report the failure plainly, and only a later store-*verified* retry (an engine-observed persisted change) clears the alarm. The #5740 diagnostics record reports a mismatched round as `failed`. - Prompt guidance keyed on the `readBack` **field** (not prose): results carrying it have verified themselves — no separate verification read needed; results without it keep the read-then-claim coaching. The write-then-verify boilerplate was removed only from bullets covering commands that now self-verify. - New `mari-write-readback` regression lane: a live file-backed-store round-trip (create/update read back `verified`; dry-run carries no read-back), sentinel end-to-end and forgery cases, debt-semantics orderings, `readBackValuesMatch` units, and source pins on the emitter gates/ordering, the cascade condition, and the record-mismatch consult. - CHANGELOG entry under [Unreleased]. ## 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 - Commands run locally (all passing): `node ./scripts/run-regressions.mjs --filter mari-write-readback` (live-store round-trip lane), plus the `mari-guard-sibling-paths`, `prompt`, `mari-polish`, `mari-understood-request`, and `mari-ask-latch` lanes; `pnpm --filter @marinara-engine/server lint`; `pnpm build`. - The branch was adversarially reviewed in three multi-agent rounds across its life (original content, the never-weaken hardening, and a final round on the reconciliation with #5775/#5787); confirmed findings are folded in. - Manual browser verification that would still be valuable: ask Mari to update a character and watch her report completion without an extra verification read; then ask her to update something with a deliberately conflicting concurrent edit to see the mismatch path refuse a success claim. ## Docs and release impact - [ ] No docs changes needed - [x] 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) ## UI evidence (if applicable) Not applicable — server-side verification behavior only. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Applied database and character-book changes are now checked against values saved in the data store. - Verification results identify confirmed saves, unavailable checks, or mismatches. - Cascade deletions are checked to ensure affected records were removed. - **Bug Fixes** - Persistence mismatches are surfaced clearly instead of being reported as successful changes. - Failed writes remain unresolved until a later store-verified retry succeeds. - **Behavior Updates** - Successful store verification removes the need for a separate confirmation read. - Bash and file-based changes continue to require confirmation reads. <!-- 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:15:03 +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#6937
No description provided.