Skip to content

fix(web): acknowledge managed native saves after durable storage - #2737

Open
Srbino wants to merge 2 commits into
storytold:mainfrom
Srbino:feat/host-managed-save-ack
Open

Srbino wants to merge 2 commits into
storytold:mainfrom
Srbino:feat/host-managed-save-ack

Conversation

@Srbino

@Srbino Srbino commented Oct 27, 2026 •

Copy link
Copy Markdown

With an embedded editor, Ctrl+S currently marks the workbook saved immediately after posting bytes to the parent, before durable storage has succeeded. A host can now opt into ?host=parent&save=managed: Save requests a native snapshot save, and only an acknowledgement identifying the latest exported request, document, and revision records that exact workbook as the saved baseline. Edits made while storage is pending remain dirty; stale or foreign acknowledgements cannot clear them. Legacy host saves remain unchanged.

Builds on #2736 and relates to #2730. The follow-up also caps native validation input at 32 MiB before copying it and reports when no workbook is active, addressing the two minor review findings on #2736. Same-origin parent and source checks remain in force.

Validation: regression tests first reproduced the missing acknowledgement state and the real Chromium Ctrl+S premature dirty reset. Rust tests cover an older snapshot acknowledged after a new edit, exact latest acknowledgement, stale/foreign/new-document acknowledgements, and allocation-free 31/32/32 MiB + 1 length boundaries. Full cargo xtask ci passed all 10 gates, with the pinned craft-fonts input and an existing Chromium SwiftShader Vulkan driver for the headless screenshot test. Independent source review checked the exact Arc baseline, acknowledgement identity guards, bounded latest pending snapshot, and before-copy length check. The browser fixture contains two sheets, formulas, formats, row height, merged cells and more than 4 MiB of native state; browser checks cover the managed lifecycle, oversized validation, and unchanged legacy save.

Browser scope: Ctrl+S was exercised with a 4,304,375-byte native workbook, an intervening edit, stale/foreign acknowledgements, exact latest acknowledgement, oversized validation, and a reload into legacy mode. Web Save As and other XLSX workbook download paths use the same Services.download callback and therefore the same managed-save guard (source checked); their separate menus/dialogs were not browser driven. Non-workbook exports retain their existing behavior.

Summary by CodeRabbit

  • New Features
    • Parent-host integrations can open native JSON workbooks, export snapshots with document revisions, validate files up to 32 MiB, and receive notifications when documents change or close.
    • Optional managed-save mode lets a host handle Save requests and acknowledge the latest exported snapshot. Changes made after export remain unsaved; standard XLSX Save behavior is unchanged when managed-save mode is off.
  • Documentation
    • Updated web documentation with host messaging, validation, export, and managed-save behavior.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 45f5ab1 and 4d22c86.

Files selected for processing: 6
  • README.md
  • apps/gridcraft-web/src/host.rs
  • apps/gridcraft-web/src/main.rs
  • apps/gridcraft-web/src/managed_save.rs
  • apps/gridcraft-web/src/native_document.rs
  • apps/gridcraft-web/src/web.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

The Web app adds native workbook export and validation, optional managed-save handling, and parent-window messages for these operations. The bridge also reports document revision changes and closure.

Changes: Workbook Host Integration

Layer / File(s) Summary
Native workbook snapshots
apps/gridcraft-web/src/native_document.rs, apps/gridcraft-web/src/main.rs
The app exports validated JSON workbook bytes with document and revision identifiers. Validation checks JSON input, engine errors, warnings, and a 32 MiB size limit. Tests cover exports, validation, and draft behavior.
Managed-save acknowledgment
apps/gridcraft-web/src/managed_save.rs
ManagedSaves tracks the latest snapshot. It updates the active workbook’s saved baseline only when the acknowledgment and active document match. Tests cover stale or mismatched acknowledgments and document switching.
Parent bridge requests and notifications
apps/gridcraft-web/src/host.rs, apps/gridcraft-web/src/web.rs, apps/gridcraft-web/src/main.rs, README.md
The parent bridge queues native export, validation, and managed-save work for sync. It checks message origin, request fields, queue limits, and byte payloads. It reports document changes and closure. The Web app enables managed mode with save=managed, and the README documents the protocol.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ParentWindow
  participant Bridge
  participant SheetApp
  participant NativeDocument
  participant ManagedSaves
  ParentWindow->>Bridge: Send native export request
  Bridge->>SheetApp: Process queued request during sync
  SheetApp->>NativeDocument: Export active workbook
  NativeDocument-->>Bridge: Return bytes, document, and revision
  Bridge-->>ParentWindow: Post native export response
  Bridge->>ParentWindow: Post managed save request
  ParentWindow->>Bridge: Send matching save acknowledgment
  Bridge->>ManagedSaves: Acknowledge request, document, and revision
Loading

Suggested reviewers: claude, echelon

Merge Risk: Minimal · up to 4d22c

Managed Ctrl+S reaches the parent save request, and acknowledgments report dirty-state changes. The change is mergeable after normal checks.

Security architecture risk: Low · up to 4d22c

The new save flow preserves unsaved edits and rejects stale or foreign acknowledgements. Access remains restricted to the same-origin parent. Host-side durable storage and recovery behavior remain integration dependencies that could not be verified here.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The new browser-facing authority is confined to the framed editor and its accepted same-origin parent. That parent can request workbook export, submit validation input and attest to persistence; the inspected paths do not grant those operations to other origins or unrelated windows.

Trust Boundaries and Controls

  • observed — Every new incoming message branch follows exact origin and parent-window source checks, and replies target the captured origin. Acknowledgement identity checks bind the saved baseline to an exported snapshot, but do not independently prove storage durability: the authenticated parent remains trusted to make that assertion.

Resilience and Maintainability Implications

  • observed — The saved-state transition uses the retained workbook snapshot rather than the current edited workbook. Later edits remain dirty, and document identity guards prevent acknowledgement from modifying a replacement document. This preserves state ownership during delayed or repeated messages.

Hardening Proposals

  • proposed — Specify host retry and acknowledgement-admission behavior, including queue saturation and frame reloads, so recovery does not depend on undocumented assumptions while retaining the current fail-closed dirty-state behavior.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 31.82% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: acknowledging managed native saves after durable storage. It is concise and specific.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 31.82% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 22 functions across 5 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@Harrison-Booth

Copy link
Copy Markdown
Contributor

Static source review of 4d22c867b7; I did not run Rust or browser tests.

Case Observed behavior
Two exports; ACK for older export arrives after newer export Rejected: remember replaces the single pending identity.
Edit after export; then ACK The captured Arc<Workbook> becomes saved; the edited workbook remains dirty.
Undo to the acknowledged snapshot Undo restores its saved Arc, so pointer-based dirty state clears.
Storage fails / no ACK arrives No baseline update; the workbook stays dirty.
Switch A→B, then ACK A while B is active Rejected by document UID; the pending A snapshot remains and can be acknowledged if A is active again.
Late ACK after a newer export Rejected by request/document/revision identity.

One uncovered request/export race: RequestSave sends A's document and revision, but does not retain or check them. If the user switches to B before the host handles that message, the host's subsequent export-native serializes B, remember installs B as pending, and ACK B sets B.saved although Save was requested on A. Please bind the export/ACK to the save request's document (or reject a mismatched target); a focused Rust regression should queue Save on A, switch to B before export, and assert ACK cannot clear B while A remains dirty.

I checked git merge-tree --write-tree upstream/main HEAD: it produced a clean merge tree. The save code also overlaps the stacked open changes in #2744 and #2788. The forge currently reports this PR as mergeable with unstable merge state.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReviewerA — source-read COMMENT (not an approval)

I read the head of the doc comments: the web app's host bridge gains gridcraft:changed ({ document, revision, dirty }, sent whenever the active workbook changes, even when it is already dirty, so the host can debounce a fresh native export), gridcraft:export-native (from the parent: { requestId }; replies with the native JSON without claiming a durable save) and gridcraft:save-ack (the parent, after storing the export durably, sends { requestId, document, revision }, and a matching acknowledgement records that export as the saved baseline; edits after the export stay unsaved; old or foreign acknowledgements are ignored). The design is careful about the one hard point of such a bridge: "saved" is claimed only after the host says it stored exactly that revision. In the code I read, the messages are accepted only from the same-origin parent and answered with post_message to that origin (never *). Check that the revision is checked against the current state at acknowledgement time (an edit during the export is not marked saved), that a request id used twice is refused, and that memory for pending exports is bounded. This uses the same host protocol as deckcraft#983 and soundcraft#866, so the three should share the message names and rules (soundcraft#866 also allows a vouched cross-origin embedder; this one and deckcraft#983 are same-origin only). I did not read the rest. Add tests for a stale acknowledgement, a wrong document, an edit after export, and a message from another origin. Nothing was run.

I ran nothing and have no exact-head CI result for this PR, so this is not an approval. Where a diff was long I read its code hunks rather than every generated or documentation line.

Status: waiting_author.

@reimon

reimon commented Nov 23, 2026

Copy link
Copy Markdown

We confirmed the save-state gap from #1020 in a real browser on the official GridCraft v0.19.1 web build. In a same-origin iframe with ?host=parent, we opened a synthetic XLSX, changed A20 with cell.set, and ran file.save. The parent received a valid XLSX containing the edited cell, retained it in memory, and deliberately performed zero writes and sent no acknowledgement. Chrome 158 observed gridcraft:dirty(true) → gridcraft:save → gridcraft:dirty(false).

Thanks for adding the managed native-save path in this PR. Our embedding needs the host to persist the actual XLSX file. Is there a supported way to bind an XLSX export to the same document and revision as the managed save, so the host can acknowledge it only after that exact file is written? The regression we would like to cover is that failed or delayed XLSX storage leaves the workbook pending and allows retry, and that acknowledging an older XLSX never clears a newer edit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants