Skip to content

Resolve get_workflow_state's scheduled/cron ancestry live, not from the cache - #2864

Merged
mstomar125 merged 3 commits into
mainfrom
issue-2828-cron-cache-staleness
Sep 23, 2026
Merged

mstomar125 merged 3 commits into
mainfrom
issue-2828-cron-cache-staleness

Conversation

@mstomar125

@mstomar125 mstomar125 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #2828.

get_workflow_state's scheduled/cron candidate fan-out read node_type ancestry from PlaybookLifecycleManager::ancestor_cache (via lm.ancestors_of), which is refreshed asynchronously by PlaybookEngine and stays stale after a failed refresh until the next live event retries it. get_workflow_state has no access to PlaybookEngine's ancestry_dirty flag or its refresh routine — it is called with only a lifecycle manager and a node service — so it had no way to detect or repair that staleness. A scheduled Play registered on a base type could silently vanish from a subtype node's response, with no error and no degraded_reasons entry, right after an extends edit or during a refresh-failure retry window.

Fix

Resolve the extends chain live instead, via NodeService::resolve_type_chain — the existing uncached resolver already used for one-off reads (its own doc comment: "Resolved live rather than cached... this is the uncached resolver for write paths and one-off reads") — once per get_workflow_state call, and use it for the scheduled/cron eligibility test in place of the cache read. A resolution failure now goes through the same degraded_reasons reporting every other schema/extends-chain lookup in this function already uses, instead of silently dropping candidates.

effective_fields (the property_changed candidate field set) and ancestry (the scheduled/cron eligibility chain) are resolved from a single resolve_field_owners call — its third return value is the same live chain resolve_type_chain computes internally — rather than two independent calls, so the common path does one extends-chain walk, not two.

Scope

The graph-event candidate path (lookup_rules → ancestor_keys) still consults PlaybookLifecycleManager::ancestor_cache for its own ancestor fan-out, and PlaybookEngine's refresh semantics (ancestry_dirty, refresh_ancestor_cache) are unchanged — both are out of scope per the issue's stated non-goals. That fan-out is shared with the live trigger-dispatch hot path (engine.rs/cel.rs), which cannot afford a DB query per event the way this out-of-band, read-only diagnostic call can; re-architecting it is a larger change better suited to its own issue if it's ever needed. degraded_reasons's doc comment now notes this explicitly: an empty vec means no live resolution failed, not that the graph-event half is guaranteed fresh.

The issue's second concern (multiple independent async schema reads with no single consistent snapshot across a whole response) is also left out of scope — it's a much broader architectural change (snapshotting arbitrarily-deep relationship-target schemas across every possible condition-path walk) than the concrete, timing-dependent gap this PR closes, and the issue explicitly allows scoping down to a well-documented partial fix.

Sharper framing on the above, worth flagging explicitly: before this PR, both the scheduled/cron and graph-event halves read the same cache, so during a staleness window they were wrong together (silently missing a base-registered Play in agreement). After this PR, cron resolves live while graph-event still reads the cache, so during that same window the two halves of one response can now disagree — a new internal-inconsistency failure mode, not just the original bug left in place. Filed as a follow-up: #2870, which also records why a cheap same-PR mitigation (surfacing ancestry_dirty, or a per-call cache-vs-live comparison) isn't straightforward given the current cache design.

Known trade-off

Because effective_fields and ancestry now share one resolve_field_owners call, a transient DB failure on that call degrades both halves together (fields to the node's own directly-declared set, ancestry to the node's own type) — reported as one degraded_reasons entry. Pre-PR, the cron half read the cache independently and so couldn't be taken down by a resolve_field_owners failure, but that independence bought silent staleness, not real resilience: the cache read never fails, but it also never tells a caller it might be wrong. Considered reintroducing a second, independent resolve_type_chain call in just the failure branch to recover that narrow case, but decided against it — it only helps when the failure happens strictly after chain resolution succeeds inside resolve_field_owners (a narrow sub-case, since resolve_field_owners resolves the chain first and a chain-resolution failure would sink both calls identically), and it re-adds exactly the kind of defensive double-resolution the fix above removes, for an already-rare, already-transparent (via degraded_reasons) edge case. Explicit-and-narrower over silent-and-stale is the intended trade here.

Testing

  • Updated scheduled_trigger_on_ancestor_type_is_returned_for_subtype_node to exercise a real extends edge (via handle_create_schema) instead of manually seeding ancestor_cache — the old version would no longer exercise the code path being tested after this change.
  • Added scheduled_trigger_on_ancestor_type_survives_stale_ancestor_cache, which seeds the cache with a deliberately wrong/stale ancestry entry while the real schema graph declares a genuine extends edge, and asserts the scheduled Play is still found — the direct regression test for the bug this PR fixes.
  • cargo test -p nodespace-core --lib playbook:: — 447 passed, 0 failed.
  • Pre-push gate (full pyramid, including Tauri-seam integration tests) passed.

Test plan

  • cargo test -p nodespace-core --lib playbook::workflow_state — 14/14 passed
  • cargo test -p nodespace-core --lib playbook:: — 447/447 passed
  • cargo clippy -p nodespace-core --lib --all-targets -- -D warnings — clean
  • Pre-push gate (bun run test:all + cargo build --bin nodespaced + test:e2e) — passed

🤖 Generated with Claude Code

mstomar125 and others added 3 commits September 23, 2026 06:37
…he cache

The scheduled/cron candidate fan-out read node_type ancestry from
PlaybookLifecycleManager::ancestor_cache (via lm.ancestors_of), which is
refreshed asynchronously by PlaybookEngine and stays stale after a failed
refresh until the next event retries it. get_workflow_state has no access
to PlaybookEngine's ancestry_dirty flag or its refresh routine, so it had
no way to detect or repair that staleness — a scheduled Play registered on
a base type could silently vanish from a subtype node's response with no
error and no degraded_reasons entry, right after an extends edit or during
a refresh-failure retry window.

Resolve the extends chain live instead, via NodeService::resolve_type_chain
(the existing uncached resolver already used for one-off reads), once per
call, and use it for the scheduled/cron eligibility test in place of the
cache read. A resolution failure is recorded in degraded_reasons like every
other schema lookup in this function, rather than silently dropping
candidates.

The graph-event candidate path (lookup_rules -> ancestor_keys) still
consults the cache for its own ancestor fan-out and PlaybookEngine's
refresh semantics are unchanged — both are out of scope here per the
issue's stated non-goals (the live trigger-dispatch hot path cannot afford
a query per event the way this out-of-band diagnostic call can).

Updates the existing ancestor-fan-out regression test to exercise a real
extends edge instead of manually seeding the cache, and adds a new test
that seeds the cache with a deliberately wrong/stale entry to prove the
fan-out no longer depends on it.

Closes #2828

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…stead of a second call

Code review on PR #2864 flagged that get_workflow_state was resolving
node_type's extends chain twice per call: once inside resolve_field_owners
(discarded as _chain), and again via a separate resolve_type_chain call
for the scheduled/cron candidate fan-out. Both walk the same
get_extends_parent_map() table scan, so this doubled that portion of the
call's DB I/O, opened a TOCTOU window where a concurrent schema write
between the two awaits could give the property_changed and scheduled/cron
halves of one response different ancestry snapshots, and would report a
single underlying resolution failure as two separately-worded
degraded_reasons entries.

resolve_field_owners's third return value is the same chain
resolve_type_chain would produce (it calls resolve_type_chain internally
and returns the result verbatim), so reuse it directly instead of a second
call, and fold the two degradation messages into one.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
… gap

degraded_reasons only records live resolution failures (an Err from a
schema/extends-chain call). The graph-event candidate path's own ancestor
fan-out (lookup_rules -> ancestor_keys) still reads
PlaybookLifecycleManager::ancestor_cache directly rather than resolving
live, and a cache read that returns a stale-but-successful answer never
registers as a failure. An empty degraded_reasons could therefore read as
stronger confidence than it actually provides for that half of the
response. Documents the distinction rather than changing behavior — this
gap is the PR's stated non-goal, shared with the live trigger-dispatch
path.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125
mstomar125 merged commit 0c6a252 into main Sep 23, 2026
@mstomar125
mstomar125 deleted the issue-2828-cron-cache-staleness branch September 23, 2026 05:33
mstomar125 added a commit that referenced this pull request Sep 23, 2026
* Surface ancestor-cache refresh failures in get_workflow_state (closes #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]>

* Address review: clarify ancestry_dirty doc-comment precision

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]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
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's cron/scheduled and graph-event candidates read inconsistent extends-chain views (cache vs. live, no shared snapshot)

1 participant