Skip to content

perf(runtime): avoid scanning diagnostics for opening progress - #1997

Merged
zerob13 merged 2 commits into
mainfrom
dimagent/fix-594-opening-progress-scan
Nov 23, 2026
Merged

zerob13 merged 2 commits into
mainfrom
dimagent/fix-594-opening-progress-scan

Conversation

@zerob13

@zerob13 zerob13 commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

On current main, a progress report from InputQueue recovery builds a full rutis diagnostics snapshot to find the reporting fiber. The native sample during the 1,000-Session run showed InputQueue::rebuild → opening_advanced → CtxHost::diagnostics on the creation hot path. The loader's live row query already uses subtree indexes in the published 0.23.0 release adopted by Dim. rutis #1312 · Dim #1941

Report the context's instance identity directly and resolve its subtree membership when the inactivity deadline is checked, using the diagnostics snapshot that check already takes. Dispatch and receipt reports that already know the branch group keep using its plugin ID. The existing live member reads continue to cover nested instances created during required-part startup. Startup progress contract · Assembly lifecycle

Fixes #594

Verification

Apple M4, Rust 1.100.0, locked release build with thin LTO, two Tokio workers, mock model, in-memory Sessions and an isolated DIMCODE_HOME. Compared main e4d981bd2 with 9b9d93f36 using xtask measure. Each batch keeps its Sessions and main branches live until after the query probe. The probe measures the last Session and its main branch with 100 warm-up calls and 1,000 timed calls per scope.

Loaded Sessions Main mean create PR mean create Main query p50 (Session / branch) PR query p50 (Session / branch)
1 19.08 ms 17.94 ms 333 / 208 ns 333 / 208 ns
10 1.70 ms 1.52 ms 333 / 208 ns 333 / 208 ns
100 3.39 ms 1.52 ms 334 / 208 ns 333 / 208 ns
1,000 31.50 ms 1.21 ms 542 / 334 ns 500 / 458 ns

The 1-Session batch includes first-use initialization. Each timed query returned three Session-subtree rows or one main-branch row. At 1,000 Sessions the PR meets the 20 ms creation target, and Agent shutdown returned to zero live Tokio tasks. Target in #367

  • Passed all five release tests in agent_runtime_flow's opening_progress and lifecycle_review groups. They cover genuine progress past the deadline, sibling progress isolation, required-row failure, cancellation and rollback error retention.
  • The base commit's CI passed the late nested-instance and optional-row cleanup flow and the nested-instance cancellation rollback flow. Base CI job
  • Passed rustfmt for the touched files and git diff --check. The temporary query probe stays outside the commit.

Design rule checklist

  • Scope, commands/results and handover. N/A because the existing runtime opening owns progress and the change only addresses its internal notification.
  • Timeline before, batch, context and facts. N/A because the notification is on the rutis bus and the change never writes the Timeline.
  • Infrastructure. The runtime's startup progress contract provides the requirement and the runtime implements and verifies it.
  • Core mechanisms and internal state. The runtime uses public rutis context identity and its existing timeout snapshot for any reporting plugin.
  • Cross-language faces and configuration. N/A because the change introduces no service or configuration contract.
  • Missing mechanism capabilities. N/A because rutis 0.23.0 already supplies both context identity and live row lookup.

Documentation

  • Existing documentation owns the startup progress and assembly contracts linked above. ADRs, design documents, generated service surfaces and delivery records need no edits.

@yyhhyyyyyy yyhhyyyyyy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review: perf(runtime): avoid scanning diagnostics for opening progress

Verdict: APPROVE

I reviewed the full main...head diff at 9b9d93f against How Dim uses rutis A3, single-tree assembly, and the rutis 0.23.0 identity, diagnostics and event code. The removed diagnostics call is off the report path, so the performance claim is plausible; I did not rerun xtask measure. The opening contract is preserved.

Correctness

  • Identity is the same key on both sides. Ctx::instance() returns the fiber's InstanceId (rutis-0.23.0/src/ctx.rs:1203-1205), and RuntimeDiagnostics publishes that same fiber.instance (rutis-0.23.0/src/ctx.rs:1345-1346). InstanceId is process unique and never reused (rutis-0.23.0/src/key.rs:10-38), so the string key cannot alias a different row.
  • Membership is still resolved before the instance lookup. by_instance filters snapshot rows to members by plugin id and only then matches plugin.instance (crates/dim-agent-runtime/src/assembly.rs:155-161), and subtree still walks the snapshot's parent links (crates/dim-agent-runtime/src/assembly.rs:2275-2293). A report from a sibling, another Session, a row outside the roots, or a row that left the snapshot before the deadline is ignored, as before. The old code also failed to count a removed row because members came from the same deadline snapshot.
  • Nested and required-part rows stay covered. The required-part mount path is ctx.plugin (crates/dim-agent-runtime/src/assembly.rs:1976-1979), which records the mounting fiber as parent, and nested loader instances attach the same way, so a live descendant resolves through subtree. I also ran an uncommitted nested probe (parent apply mounts a reporting child, wait_active on the parent with a 150 ms interval while the child works for 360 ms) and it passed, which confirms the child's instance report renews the parent's opening.
  • Dispatch and receipt reports keep their plugin id. TimelineContext emits OpeningAdvanced::Plugin(inner.view.id) during Loading (crates/dim-agent-runtime/src/timeline_context.rs:187-193) and receipt completion emits OpeningAdvanced::Plugin(scope) with the branch view id (crates/dim-agent-runtime/src/config.rs:382-385). The branch opening's roots are exactly that view id (crates/dim-agent-runtime/src/timeline_context.rs:117), so those reports still match. The enum variants are distinct, so a plugin id cannot be read as an instance id.
  • Deadline behavior is preserved. First activation still renews once through the HashMap::entry check (crates/dim-agent-runtime/src/assembly.rs:80-95), a second activation after a row restart still does not renew by itself, a report from outside the subtree still does not renew, and a missing row or an ended future still produces the same timeout. The new cancellation_token gate tightens rather than loosens this: a cancelled generation reports nothing, which matches A3's "Restarting the same row does not renew it".
  • Cancellation and rollback error retention are untouched: the guard is before the emit only, and no future, receipt or error path changed.

Notes, not blocking

  1. crates/dim-agent-runtime/src/assembly.rs:45-46: the /// contract still describes only the no-live-fiber case, but the new gate also suppresses reports from a live fiber whose generation was cancelled (rollback, restart, reload). That is the more common case and worth stating so callers do not expect a report to renew a failing opening.
  2. crates/dim-agent-runtime/tests/opening_progress.rs:39-45: the new sibling case covers root-level filtering, and the first test covers the instance path for a directly registered root plugin. The production caller InputQueue recovery runs under a branch, and the SlowAssemblyRow fixture in transport_flow does call opening_advanced from a required row, but that test uses the default 10 s startup interval and asserts the note counter, not renewal of a shorter interval. No committed test calls opening_advanced from a nested row while a shorter interval is running, and the base flows cited in the PR body do not call it, so they do not cover the new identity mapping. A nested variant of this test would pin it, as noted above.

Verification

  • cargo test --locked -p dim-agent-runtime --test agent_runtime_flow: 33 passed, including both opening_progress tests and lifecycle_review.
  • cargo clippy --locked -p dim-agent-runtime --all-targets -- -D warnings: clean.
  • cargo fmt --all -- --check: clean.
  • nested_instances_preserve_optional_rows_and_cleanup_across_create_restore_and_failure in integration_flow: passed on the head, so the nested-instance create, restore and rollback path still works.
  • All builds and tests used an isolated HOME, TMPDIR and XDG directories with CARGO_TARGET_DIR under the review data directory; no product home was touched and no processes were left running.

Merge and CI

The head is a descendant of the given origin/main (e4d981b) and git merge-tree --write-tree origin/main HEAD is clean. Open PR #1905 also touches assembly.rs and still merges cleanly at tree level; #1911 is already conflicting with main independently of this change. At review time the PR is MERGEABLE with verify (linux) still running and macos-native passing; the other jobs were skipped by path filters. I did not wait for CI.

if let Some(id) = id {
let _ = ctx.emit(OpeningAdvanced(id));
if !ctx.cancellation_token().is_cancelled() {
let _ = ctx.emit(OpeningAdvanced::Instance(ctx.instance().to_string()));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new cancellation_token().is_cancelled() guard is a caller-visible behavior change that the /// above does not state. A live fiber whose generation was cancelled (rollback, restart, reload) still appears in diagnostics, so before this change its opening_advanced call emitted and could renew an inactivity interval; now it reports nothing. I agree with the guard because it matches A3's "Restarting the same row does not renew it", and the no-live-fiber sentence above remains true because a destroyed fiber yields a pre-cancelled token. The missing case is the cancelled generation. Failure scenario: a caller keeps reporting during teardown, when the row is still live, and now silently loses those reports with no hint in the contract that cancellation gates them. Fix: extend the sentence to say that a context whose generation was cancelled reports nothing. Not blocking.

gap: Duration::from_millis(60),
reports,
});
if !reports {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new sibling case covers filtering for a root-level sibling, and opened(true) covers the instance path for a directly registered root plugin. No committed test calls opening_advanced from a mounted or loader-created nested row while a shorter inactivity interval is running. The SlowAssemblyRow fixture in the_startup_note_reports_assembly_progress_until_the_server_opens does call it from a required row, but that test uses the default 10 s startup interval and asserts the note counter, not renewal of a shorter interval; InputQueue recovery under a branch is the production caller, and the rows whose identity this change resolves through snapshot.plugins are nested. The base tests cited in the PR body (nested_instances_preserve_optional_rows_and_cleanup_across_create_restore_and_failure, a_row_cancelled_while_its_child_waits_ends_the_wait) do not call opening_advanced, so they do not cover this path. Failure scenario: a regression that resolves the instance key incorrectly for loader-created rows would make an opening whose only progress comes from such a row time out after one interval, and no test would fail. Fix: add a nested variant of this test, for example a parent apply that mounts a reporting child and calls wait_active on the parent with a 150 ms interval while the child works for 360 ms. I ran that variant locally and it passes. Not blocking.

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at head 9b9d93f.

The change removes a real cost from a hot path for a stated reason: an opening progress report was building a full diagnostics snapshot just to learn its own plugin id, and the assembly path now reports identity directly and resolves subtree membership at the deadline check it already takes. if !ctx.cancellation_token().is_cancelled() guarding the emit is the right pairing — a cancelled assembly should not renew anyone's opening. Widening active to the identity enum and opening the deadline read over members keeps the "one snapshot, one read" shape.

Blocking: none from the change's own design.

verify (linux) and ci-ok failed on this head, in three places, none of which this PR's files reach:

managed_oauth_flow::managed_provider_selection_http_recovery_and_restart_flow
agents::sessions::tests::exists_answers_loaded_unloaded_closed_and_deleted
goal_product_flow::goal_evaluator_judge_creates_an_evaluator_and_completes_on_done
125 passed; 1 failed (and two more suites)

Those three are timeout-shaped, and two of them (managed_oauth_flow, goal_product_flow) have failed on unrelated heads in this same window, so I am not reading them as a regression here. Not approving while a required check is red.

Non-blocking:

  • crates/dim-agent-runtime/src/assembly.rs — OpeningAdvanced is #[serde(untagged)] over Plugin(PluginId) and Instance(String). If both serialize as strings, a deserialize can only ever produce Plugin, so an Instance identity silently becomes a plugin id on any round trip. The enum distinguishes two identity spaces in memory and loses that distinction on the wire; if the event never crosses a wire the untagged marker and the visitor cost both earn nothing, and if it does, the variant that was added cannot survive the trip.

Verification: not run — I read the four changed files' diffs and three of the failing test names; I did not read the new opening_progress.rs bodies or the rutis CtxHost side.

@zerob13
zerob13 merged commit d0082af into main Nov 23, 2026
eric8810 added a commit that referenced this pull request Nov 26, 2026
`BranchLifecycle::opening_budget` (a service-skip) and
`BranchLifecycle::admit_opening_with` (a Rust-only method) come from main
(#1997/#2198 era, `fbd65fa82` and `b802a3f7d`), not from this branch; they are
in the allowlist now so the check can run against the merged tree.

Entries: 171 -> 173, both from main.

Refs #2015
Refs #2127
eric8810 added a commit that referenced this pull request Nov 26, 2026
`BranchLifecycle::opening_budget` (a service-skip) and
`BranchLifecycle::admit_opening_with` (a Rust-only method) come from main
(#1997/#2198 era, `fbd65fa82` and `b802a3f7d`), not from this branch; they are
in the allowlist now so the check can run against the merged tree.

Entries: 171 -> 173, both from main.

Refs #2015
Refs #2127
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.

Assembly: creating an instance scans every loader entry five times

3 participants