Skip to content

Surface ancestor-cache refresh failures in get_workflow_state - #2891

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2870-graph-event-ancestry
Sep 23, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2870-graph-event-ancestry

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Follow-up from #2828/PR #2864. get_workflow_state's scheduled/cron candidate fan-out was fixed to resolve node_type's extends ancestry live, but the graph-event candidate path (lookup_rules -> ancestor_keys in packages/core/src/playbook/lifecycle.rs) still reads the possibly-stale PlaybookLifecycleManager::ancestor_cache. After #2864 the two halves of a response can disagree during a staleness window instead of being wrong together, with no signal recorded either way.

This issue's own text documents two directions already prototyped and rejected during #2828's review (a naive per-call cache-vs-live comparison that false-positived on most of workflow_state.rs's existing tests, and a full signature change threading an engine handle through every call site including the Michael-owned packages/agent/src/local_agent/ lane). I found a third path that avoids both problems:

  • NodeService already has an established OnceLock-injection pattern for exactly this class of problem — playbook_lifecycle is injected post-construction (the play engine is built from an already-constructed NodeService, so it can't be passed at NodeService::new time) and read back internally wherever needed.
  • I added a parallel playbook_ancestry_dirty: Arc<OnceLock<Arc<AtomicBool>>> field mirroring that pattern exactly, plus a PlaybookEngine::ancestry_dirty() accessor mirroring the existing lifecycle() one, wired together in assembly.rs right next to the existing set_playbook_lifecycle call.
  • get_workflow_state reads the flag off the node_service argument it already receives — no new function parameter, so no signature change and no other call site needs touching: not the daemon's gRPC handler, not the local-agent tool (packages/agent/src/local_agent/tools.rs — confirmed untouched, verified via a full workspace cargo check), not the CLI (its response is an opaque result_json blob, so the new degraded_reasons entry flows through automatically).
  • The signal is purely additive/opt-in: the OnceLock defaults to unset everywhere it isn't explicitly wired (every existing test, most non-daemon callers), so nothing that doesn't wire it observes any behavior change.

When PlaybookEngine::ancestry_dirty is set (a cache refresh most recently failed and hasn't yet succeeded again — the accurate signal per the issue's own analysis), get_workflow_state now records a degraded_reasons entry naming the failure, so a caller can tell the graph-event candidates may be missing a rule registered on an ancestor type.

Scope note (documented in code)

This covers the one class of staleness that's both unbounded in duration and cheaply/precisely detectable: a failed refresh that hasn't yet been retried. It does not cover the ordinary sub-millisecond propagation lag between a schema write committing and the play engine's async event loop refreshing the cache in response — that's bounded, inherent to the in-process pub/sub architecture, and shared by every other in-memory index built the same way (e.g. the TriggerIndex itself), not the risk this diagnostic exists to flag. Doc comments on WorkflowState::degraded_reasons and get_workflow_state spell this out explicitly.

Direction 3 (making lookup_rules's ancestor fan-out resolve live, matching the scheduled/cron path) remains explicitly out of scope — it's the live trigger-dispatch hot path shared with engine.rs/cel.rs, which cannot afford a DB read per event, same reasoning as #2828/#2864.

Files touched

  • packages/core/src/playbook/engine.rs — new ancestry_dirty() accessor.
  • packages/core/src/services/node_service/mod.rs — new field/setter/getter mirroring playbook_lifecycle.
  • packages/daemon/src/services/assembly.rs — one new wiring line.
  • packages/core/src/playbook/workflow_state.rs — the actual check + doc-comment updates + 2 new tests.

Test plan

  • cargo check -p nodespace-core -p nodespace-daemon (compiles nodespace-agent transitively — confirms the restricted lane is untouched)
  • cargo test -p nodespace-core --lib playbook::workflow_state — 16/16 passed (2 new: flag-set surfaces the reason; flag-unset stays silent, the exact false-positive mode that killed the earlier-rejected approach)
  • cargo test -p nodespace-core --lib playbook::engine — 18/18 passed (untouched behavior)
  • cargo test -p nodespace-core --lib services::node_service — 162/162 passed
  • cargo test -p nodespace-daemon --lib get_workflow_state — 2/2 passed
  • bun run quality:fix — clean (frontend, scripts, desktop-app, skill package, cargo fmt, cargo clippy --all-targets -D warnings)
  • Full pre-push gate (test:all + cargo build --bin nodespaced + test:e2e + Tauri-seam integration tests) — all green

Closes #2870.

🤖 Generated with Claude Code

…2870)

get_workflow_state's graph-event candidate path (lookup_rules -> ancestor_keys)
still reads PlaybookLifecycleManager::ancestor_cache directly, unlike the
scheduled/cron path fixed in #2864 which resolves extends ancestry live. After
#2864 the two halves of a response can disagree during a staleness window
instead of being wrong together, with no signal either way.

Mirror the existing NodeService::playbook_lifecycle OnceLock-injection pattern
for PlaybookEngine::ancestry_dirty, wired once from assembly.rs. get_workflow_state
reads it off the node_service argument it already receives, so no signature
change is needed and no other call site (daemon's gRPC handler, the local-agent
tool, or the CLI, which just forwards an opaque JSON blob) needs touching. When
the flag is set, a degraded_reasons entry names the failed refresh so a caller
can tell the graph-event candidates may be missing a rule registered on an
ancestor type.

This covers the accurate, unbounded-duration staleness case (a failed refresh
not yet retried) but not the ordinary sub-millisecond lag between a schema
write and the engine's async refresh reacting to it — documented as out of
scope, same as it is for every other in-memory index built the same way.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Pragmatic Code Review — PR #2891

Scope confirmed via gh pr diff --name-only: bun.lock, packages/core/src/playbook/engine.rs, packages/core/src/playbook/workflow_state.rs, packages/core/src/services/node_service/mod.rs, packages/daemon/src/services/assembly.rs. packages/agent/src/local_agent/ is untouched — verified directly against the diff, not just the PR description's claim. Good.

1. NodeService OnceLock wiring (node_service/mod.rs)

Traced this line-by-line against the existing playbook_lifecycle field it's explicitly mirroring: field type (Arc<OnceLock<Arc<AtomicBool>>> vs. Arc<OnceLock<Arc<RwLock<...>>>>), Clone impl entry, NodeService::new initializer, set_* (silent-no-op-on-second-call via let _ = ....set(...)), and the pub(crate) getter returning Option<&Arc<AtomicBool>> — all mirror the sibling field exactly, field-for-field. grep-verified there's exactly one production construction site (assembly.rs) and it wires both fields together; every other set_playbook_lifecycle call site is a test file that doesn't need the new field (and correctly doesn't call the new setter). No missed construction site, no ordering issue: the flag is injected from the same already-constructed playbook_engine handle, after PlaybookEngine::new, same as lifecycle().

2. PlaybookEngine::ancestry_dirty() accessor (engine.rs)

The underlying ancestry_dirty: Arc<AtomicBool> field, its Relaxed-ordering store-true-on-failure/store-false-on-success in refresh_ancestor_cache, and its default (AtomicBool::new(false)) all pre-date this PR — this PR only adds the accessor, matching lifecycle()'s shape and visibility exactly. Returning &Arc<AtomicBool> (not a clone) is correct and consistent.

3. get_workflow_state check + docs (workflow_state.rs)

The check is well-placed (right after candidate_refs is built, doesn't gate on whether candidate_refs is non-empty — correctly "process-wide" per its own comment), uses is_some_and idiomatically, and correctly treats None (unwired) as "no signal," not "known fresh" — this is the one thing the previously-rejected cache-comparison approach got wrong, and it's directly asserted by the second new test. Ordering::Relaxed on the read matches the field's existing write-side ordering (engine.rs), so no new synchronization concern. Doc-comment updates on degraded_reasons and the function are accurate for what they claim; I did not find overclaiming in the covered/not-covered split.

One real gap I found, worth documenting even if not worth blocking on: playbook_engine.start() (which runs load_active_plays → the first refresh_ancestor_cache) is tokio::spawned in assembly.rs, not awaited before the gRPC service becomes reachable. Between daemon startup and that first refresh completing, ancestry_dirty reads false (its constructed default) even though ancestor_cache hasn't been populated yet at all — the same "never-populated vs. known-fresh" ambiguity the issue's rejected approach #2 discusses, now applying to this flag's own default instead of the cache. It's not a regression (this window was already silently unsignaled pre-PR) and not this PR's field/default to change (that's pre-existing engine.rs behavior from #2828/#2864), but the doc comment's "the ancestor-cache refresh most recently attempted (if any) also succeeded" phrasing glosses over the "none attempted yet" sub-case reading as success. [Improvement], non-blocking: consider a one-line addendum to that doc comment calling out the startup window explicitly.

Nit: the new code comment above the check says "Process-wide, not per-node-type" — build_database_services is called once per database in this multi-DB daemon, so the flag is really per-database (one PlaybookEngine/NodeService pair per database_id), not literally process-wide when multiple databases are loaded in one process. Wording only, no functional impact.

4. assembly.rs wiring line

One line, added immediately after the analogous set_playbook_lifecycle call, with a comment explaining the shared reasoning rather than duplicating it. Correct and minimal.

5. New tests

Both do what they claim:

  • ancestry_dirty_flag_surfaces_as_a_degraded_reason — sets the flag directly via the new setter (bypassing the real engine, which is fine — it's unit-testing the NodeService → get_workflow_state wiring, not engine internals, which already have their own coverage at engine.rs:2045-2134) and asserts a matching degraded_reasons entry via a substring check that's appropriately loose (won't break on message wording tweaks) while still specific ("extends-ancestry cache" + "refresh").
  • no_wired_playbook_engine_does_not_report_ancestry_degradation — the regression guard for the exact failure mode #2870 explains killed the earlier-rejected approach (a None signal misread as "dirty" or "stale"). Directly useful, not a tautology.

Gap: neither test exercises the real assembly.rs wiring end-to-end (an actual PlaybookEngine with a genuinely failed refresh_ancestor_cache, observed through get_workflow_state via the full injection path). That's consistent with the sibling playbook_lifecycle feature's existing test coverage (also not covered end-to-end through assembly.rs in the suite), so it's not a new gap this PR introduces — not blocking.

6. General code quality

Doc comments are dense but precise and cross-reference correctly (ancestry_dirty() ↔ set_playbook_ancestry_dirty ↔ playbook_ancestry_dirty() ↔ the check site) — no drift found between what a comment claims and what the referenced code does. No security concerns — this is a read-only internal diagnostic flag with no new external input or trust-boundary crossing. Performance: one extra Relaxed atomic load per get_workflow_state call, on an explicitly non-hot-path out-of-band diagnostic — negligible. bun.lock's "configVersion": 0 addition is an incidental bun-tooling artifact, unrelated to this change but harmless.

7. Restricted lane

Confirmed clean — see top of this review.


Recommendation: APPROVE

This is a precise, minimal, well-scoped change that mirrors an established pattern exactly, adds a real (if intentionally partial) diagnostic signal, and is honestly documented about what it does and doesn't cover — including in this review's one addition (the startup-window edge case), which doesn't undercut the "net positive" case for merging as-is.

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

Per-database, not process-wide (the daemon runs one PlaybookEngine/
NodeService pair per database id) — wording fix. Also name the brief
process-startup window before the first refresh_ancestor_cache runs,
where the flag reads false because nothing has failed yet, not because
the cache is known fresh — same pre-existing "absent means no ancestry"
default ancestor_cache already had, not a new gap.

Addresses non-blocking findings from PR review (1 Improvement, 1 Nit) on
PR #2891, for issue #2870. Doc-only change, no logic touched — no
re-review needed per /address-review's own criteria (simple documentation
update, trivial and low-risk).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Addressed the review's two non-blocking findings, both doc-comment wording fixes in packages/core/src/playbook/workflow_state.rs:

  • [Improvement] Named the brief process-startup window before this database's PlaybookEngine::start has run its first refresh_ancestor_cache — ancestry_dirty reads false there too, not because the cache is known fresh but because nothing has failed yet. Clarified this is ancestor_cache's own pre-existing "absent means no ancestry" default, not a new gap this PR introduces.
  • [Nit] "Process-wide" → "per-database" (the daemon runs one PlaybookEngine/NodeService pair per database id).

No logic changed, no new tests needed — pure doc-comment precision. Per /address-review's own criteria this is a "simple documentation update... trivial and low-risk," so no re-review needed. cargo test -p nodespace-core --lib playbook::workflow_state still 16/16, and the full pre-push gate passed again on this commit.

Ready to merge, pending sign-off.

@mstomar125
mstomar125 merged commit 5842692 into main Sep 23, 2026
@mstomar125
mstomar125 deleted the issue-2870-graph-event-ancestry branch September 23, 2026 08:26
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.

get_workflow_state: graph-event candidates can disagree with scheduled/cron candidates during an ancestor-cache staleness window

1 participant