Skip to content

Calc: calculate missing results without replacing saved formula caches - #2308

Open
mrzv wants to merge 2 commits into
storytold:mainfrom
mrzv:feat-recalc-missing
Open

mrzv wants to merge 2 commits into
storytold:mainfrom
mrzv:feat-recalc-missing

Conversation

@mrzv

@mrzv mrzv commented Oct 22, 2026 •

Copy link
Copy Markdown

Problem

Read-only embedders can use saved formula results, or call recalc_all and replace them. They cannot fill only missing results while preserving cached unsupported functions, volatile results, errors and formula metadata.

Change

Add Calc::recalc_missing(&mut Workbook):

  • Evaluate only formula cells whose result is Value::Empty; all other saved results remain fixed inputs.
  • Keep formula metadata visible to ISFORMULA and FORMULATEXT, and retain cached anchors' existing spill values/ranges.
  • Restrict spill/dynamic follow-up rounds to the initially missing formulas.
  • Restore the full dependency graph before returning, so later cells_changed calls retain normal editing behavior.
  • Document that this does not validate cache freshness or reconstruct cached spill data absent from the workbook.

The API is additive; importers, UI open policy and recalc_all are unchanged. This is distinct from an all-or-none open recalculation option.

Coverage

Regressions cover cached UDFs, stale/volatile results, error and empty-text caches, missing chains, formula metadata, cached spill references, new spills, repeated calls and subsequent incremental editing. An external public-API consumer exercises the read-only and editing lifecycle.

Verification

  • Native workspace formatting, all-target Clippy (-D warnings), tests, assets and layering gates passed.
  • The four focused recalc_missing regressions and an external public-API smoke passed after the final stale-spill fix.
  • Wasm workspace check, parity check and generated keytip check passed.
  • Final formatting and calculator all-target Clippy passed.

The default Homebrew compiler did not have the Wasm standard library installed; the Wasm check passed with the installed rustup stable compiler via a command-local RUSTC override. No global toolchain settings were changed.

Summary by CodeRabbit

  • New Features
    • Calculate formulas with missing cached results while preserving existing cached values, formula metadata, and cached spill data.
    • Update dependent formulas using newly calculated results. Repeating the calculation avoids unnecessary recalculation.
    • Unparseable formulas with missing results display a #NAME? error.
    • Dynamic cycles detected during calculation remain reported until an edit breaks them.
  • Documentation
    • Architecture documentation describes this calculation behavior and its limitations.

Add Calc::recalc_missing for read-only consumers that need to fill absent results while treating existing formula caches as fixed inputs. Preserve formula metadata and cached spill state, restrict dynamic follow-up rounds to the missing set, and clear obsolete spills when an unparseable missing anchor becomes an error.

Restore the full dependency graph before returning so later incremental edits retain normal calculation behavior. Cover cached values, metadata, spill dependencies, repeated invocation and the editing lifecycle, and document the cache-freshness and missing-spill limitations.

Model: openai-codex/gpt-6.1-sol
@coderabbitai

coderabbitai Bot commented Oct 22, 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 c6723fd and 7f2cdeb.

Files selected for processing: 3
  • crates/calc/src/recalc.rs
  • crates/calc/src/tests.rs
  • docs/architecture.md

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

Walkthrough

Calc::recalc_missing calculates formulas with empty cached values and uses nonempty cached results as fixed inputs. It handles unparseable formulas, restores the full dependency graph, and retains detected dynamic cycles. Tests cover formula evaluation, spill behavior, repeated calls, and incremental recalculation.

Changes: Missing-result recalculation

Layer / File(s) Summary
Calculation API and behavior
crates/calc/src/recalc.rs, docs/architecture.md
Adds Calc::recalc_missing. It evaluates parseable formulas with empty cached values, assigns #NAME? to unparseable formulas, restores the full dependency graph, and retains detected dynamic cycles. The architecture document describes the API and its limits.
Behavior validation
crates/calc/src/tests.rs
Adds tests for cached-value preservation, dependency chains, errors, spill state, repeated calls, incremental recalculation, and dynamic cycles.

Priority: Normal

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

Change: Feature

Possibly related issues

  • Issue 2048: This PR preserves cached results during missing-result recalculation, but does not change the XLSX importer that drops saved non-anchor values.
  • Issue 2137: This PR can retain cached spill values during missing-result recalculation, but does not change the XLSX importer that drops absent spill caches.

Suggested reviewers: nihilist-narwhal, prxnvt

Merge Risk: Minimal · up to 7f2cd

The new opt-in calculation behavior has no established issue requiring a fix before merge. The reported checks should be confirmed as part of normal merge validation.

Security architecture risk: Low · up to 7f2cd

The opt-in mode reuses existing calculation capabilities and preserves saved results by design. No introduced security vulnerability was identified. Callers must not treat the operation as validation of saved values, and recovery after abnormal interruption is not established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — Within the inspected path, workbook formulas and cached values can influence derived results, spill state, and calculation diagnostics in the supplied Workbook. The new entrypoint changes evaluation selection rather than adding evaluator authority or a cross-service execution path.

Trust Boundaries and Controls

  • observed — The caller explicitly chooses to trust saved nonempty results. A workbook producer can therefore influence newly calculated outputs through those saved inputs; recalc_missing does not authenticate or validate them. This is an explicit contract limitation, not an evidenced bypass of an existing validation control.

Resilience and Maintainability Implications

  • inferred — Graph restoration is evidenced for normal completion, including ordinary formula-error results, but is not an atomic rollback guarantee after panic unwinding. The new method replaces graph state and may write workbook state before restoration, without a rollback guard. Existing recalc_all also mutates in place; no PR-specific attacker-triggered unwind was established, and external callers' recovery behavior is unavailable.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 2 files. (1 skipped: … 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 names the main change: a new Calc::recalc_missing API that calculates formulas with missing (Value::Empty) results while keeping saved formula caches unchanged. The raw summary and PR object…
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: Title check

Explanation

The title names the main change: a new Calc::recalc_missing API that calculates formulas with missing (Value::Empty) results while keeping saved formula caches unchanged. The raw summary and PR objectives both describe this as the central purpose, so the title matches the changeset and is specific enough for a teammate scanning history.

Full details: Docstring Coverage

Explanation

Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 2 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.

Merge current upstream main into the published recalc-missing branch without
rewriting its review history. Keep both architecture histories and upstream
multi-command engine documentation alongside the cache-preserving API notes.

Retain cycles detected while evaluating missing formulas across the final
full-graph rebuild. Dynamic references can close loops that the static graph
cannot rediscover, so combine fresh detections with rebuilt cycle information
rather than discarding them while restoring normal incremental editing.

Model: openai-codex/gpt-6.1-sol
@Harrison-Booth

Copy link
Copy Markdown
Contributor

Review notes (not a formal review) on head 7f2cdeb.

Checked

  • Read the diff: Calc::recalc_missing in crates/calc/src/recalc.rs and its five tests in crates/calc/src/tests.rs. Only formula cells whose cached value is Value::Empty go into the graph; every other cached result stays as it is, including errors, empty text and volatile functions; unparseable missing formulas get #NAME?; the full graph is rebuilt before returning, and cycles found through dynamic reads are kept after the rebuild.
  • cargo fmt --all -- --check passes on this head.
  • Nothing in the repository calls recalc_missing yet (it is an additive API, as the description says), so it changes no existing behaviour.

Not checked

  • I did not run the Rust tests, so I have not seen the five tests pass; upstream shows only CodeRabbit as a check on this head. There is no Excel behaviour to compare here: what Excel does with cached results on open depends on its calculation settings and the file, and I did not try it.

@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 diff: the calculation engine can compute missing results without replacing saved formula caches. The new function calculates only formulas whose cached result is empty, using every other formula's cached result as a fixed input (even errors, empty text, volatile functions and unknown user-defined functions), keeping formula metadata for ISFORMULA and FORMULATEXT, and retaining the spills of cached anchors; it is meant for read-only consumers that prefer saved results over recalculation. The documentation is candid about the limits (it does not validate cache freshness or rebuild an anchor's absent spill range). That makes the function safe for viewers, and its name and doc should keep that scope so nobody uses it for editing. The same bounded recalculation rounds apply to untrusted workbooks. A test with a workbook that has some cached and some missing values is the proof. I have not run it, and no exact-head CI result exists for this head. 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.

@mrzv

mrzv commented Nov 1, 2026

Copy link
Copy Markdown
Author

@FredApps Agreed on the scope: this is an explicit cache-preserving calculation policy for read-only consumers, not a freshness check or a replacement for normal editor recalculation. The public Calc::recalc_missing rustdoc states that it calculates only Value::Empty formula results, trusts every nonempty saved result (including errors, empty text, volatile formulas and unknown UDFs), and neither validates freshness nor reconstructs absent spill data. It uses the existing evaluator and bounded follow-up rounds. Normal full recalculation remains recalc_all; the full dependency graph is restored before returning, so later cells_changed calls work normally and can replace the saved results.

The mixed-cache regression is already present in crates/calc/src/tests.rs: recalc_missing_is_repeatable_and_restores_incremental_editing sets A1=2, B1==A1*2 with a deliberately stale saved result of 99, and C1==B1+1 without a result. Missing-only calculation preserves B1=99 and computes C1=100; a repeated call does no further calculation. Editing A1 to 3 through normal incremental calculation then produces B1=6 and C1=7. The companion recalc_missing_preserves_cached_results_and_formula_metadata test covers saved errors, empty text, volatile/UDF results and formula metadata; further regressions cover missing chains, cached spills and dynamic-cycle reporting.

These regressions passed locally on the published head 7f2cdebc, along with the workspace gates. That is local verification rather than a claim of independent exact-head CI approval. Application open-policy integration is deliberately separate; follow-up #2824 builds on this API with the original contributor history retained.

@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.

Re-review at 7f2cdebc.

My 11-01 points are covered.

Scope. The Calc::recalc_missing rustdoc keeps it to read-only consumers. It says plainly that the function trusts every non-empty saved result and does not check freshness. Normal recalculation remains recalc_all.

Implementation. I read the code in crates/calc/src/recalc.rs:

  • Only formulas with an empty cached value go into the graph, so cached formulas stay fixed inputs through the follow-up rounds.
  • An unparseable missing formula gets #NAME?, and its stale spill is removed.
  • The full graph is rebuilt afterwards, so cells_changed works as usual.
  • Cycles found through dynamic reads are kept.

Tests. The mixed-cache test I asked for is recalc_missing_is_repeatable_and_restores_incremental_editing: a stale cached 99 stays, the missing dependent is computed, and normal editing then replaces both. The other four recalc_missing_* tests cover errors, metadata, chains, spills and cycles.

CI is green on this head. The only conflict with main is docs/architecture.md. Once that is merged and CI is green again, I have nothing further. Nothing calls the API yet, which is expected since #2824 builds on it. I did not run the tests.

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.

3 participants