Skip to content

feat: allow composite queries to call the management canister query methods - #10987

Merged
mraszyk merged 11 commits into
masterfrom
mraszyk/composite-query-ic00-calls
Aug 5, 2026
Merged

mraszyk merged 11 commits into
masterfrom
mraszyk/composite-query-ic00-calls

Conversation

@mraszyk

@mraszyk mraszyk commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Composite queries can now call the management canister methods that can be invoked by users as non-replicated query calls, i.e. the methods listed in QueryMethod: fetch_canister_logs, canister_status, list_canisters and canister_metrics. The calls are executed against the state of the own subnet on behalf of the calling canister, so they can only target canisters hosted by the own subnet and they are subject to the same access control as the corresponding queries sent by end users.

Implementation notes:

  • The execution of the management canister query methods is factored out of the user query path into the new subnet_query module which is shared by both the user query path and the new composite query path.
  • Requests to the management canister made by a composite query are no longer routed based on the method and the payload, but resolved to the own subnet and handled by the query handler.
  • The instructions consumed while producing the reply (applies to fetch_canister_logs and list_canisters) are charged towards the instruction limit of the whole call graph, in addition to the base overhead charged for every query call.
  • Query contexts calling the management canister are never cached: their results may depend on parts of the state that the query cache does not track, e.g. the cycles balance of a canister other than the queried one.

…ery methods

Composite queries can now call the management canister methods that can be
served from a state snapshot in the non-replicated mode, i.e. the methods
listed in `QueryMethod`: `fetch_canister_logs`, `canister_status`,
`list_canisters` and `canister_metrics`. The calls are executed against the
state of the own subnet on behalf of the calling canister, so they can only
target canisters hosted by the own subnet and they are subject to the same
access control as the corresponding queries sent by end users.

The feature is gated behind the (disabled by default) config flag
`composite_query_ic00_calls` to enable a gradual rollout.

Implementation notes:
- The execution of the management canister query methods is factored out of
  the user query path into the new `subnet_query` module which is shared by
  both the user query path and the new composite query path.
- Requests to ic:00 made by a composite query in the non-replicated mode are
  no longer routed based on the method and the payload, but resolved to the
  own subnet and handled by the query handler. Consequently, the flag telling
  `resolve_destination` about composite queries only refers to non-replicated
  composite queries now and is thus renamed accordingly. Composite queries
  executed in the replicated mode route their calls to ic:00 like any other
  replicated message, which also means that `fetch_canister_logs` is now
  accessible to them subject to `replicated_inter_canister_log_fetch`.
- The instructions consumed while producing the reply (`list_canisters` is
  linear in the number of canisters on the subnet) are charged towards the
  instruction limit of the whole call graph, in addition to the base overhead
  charged for every query call.
- Query contexts calling the management canister are never cached: their
  results may depend on parts of the state that the query cache does not
  track, e.g. the cycles balance of a canister other than the queried one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the feat label Jul 31, 2026
mraszyk and others added 6 commits August 3, 2026 11:19
Requests to the management canister made by a composite query are now resolved
to the own subnet at the call site of `resolve_destination` instead of inside
it: `resolve_destination` no longer takes a flag telling it about composite
queries and is not called for composite queries at all.

Also fix comments to refer to the management canister instead of ic:00 and to
state that composite queries are always executed in the non-replicated mode
(a composite query called in the replicated mode is rejected before its
execution), rather than claiming that replicated composite queries route their
calls to the management canister like any other replicated message.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…mposite_query

Every composite query is executed in the non-replicated mode, so the qualifier
is redundant: the flag is now only used to decide whether a request to the
management canister is resolved to the own subnet instead of being routed based
on the method and the payload.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…e management canister

- Charge the base overhead of a query call to the management canister only
  after rejecting calls to methods that are not management canister query
  methods, in the same way as calls to non-existing methods of a canister are
  not charged.
- Reject calls to management canister methods that cannot be executed in the
  non-replicated mode with the error of `parse_query_method`, i.e. the same
  error as for such a query sent by an end user to the management canister.
- Charge the same number of instructions for `fetch_canister_logs` in the
  non-replicated mode as in the replicated mode by introducing the shared
  helper `fetch_canister_logs_reply` (analogously to `list_canisters`).
- Test that the result of a composite query is not cached if the management
  canister is called from a reply callback of a nested composite query call.
- Do not test `list_canisters` itself in the composite query test and tighten
  the instruction limit test to the number of calls the limit is set up for.
- Fix comments: query methods can also be executed in the replicated mode.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…omposite query call are not cached

Complements the test for a call to the management canister made from a reply
callback: here the call is made by the callee of a nested composite query call,
i.e. one level down in the call graph of the query context whose result must
not be cached.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…call to the management canister

Assert the exact reject message including the own subnet ID instead of just
checking that the message contains "not found": the latter would also pass if
the call failed because the canister whose logs are fetched was not found.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…ry methods

The instructions consumed while producing the reply of a management canister
query method are still charged towards the instruction limit of the whole call
graph, but they are no longer added to the instruction observation and the
measurement scope:
- the instruction observation is only used to charge the instructions of a
  canister http transform, and a composite query cannot be used as a transform
  (see `QueryContext::run`), so the management canister cannot be called on
  that path at all;
- the measurement scope tracks the executions of canister messages, while the
  execution of a management canister query method is already observed by
  `observe_subnet_query_message`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Copilot AI 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.

Pull request overview

This PR enables composite queries to call selected management canister query methods (fetch_canister_logs, canister_status, list_canisters, canister_metrics) by routing such calls to the caller’s own subnet and executing them against local subnet state, gated behind the disabled-by-default composite_query_ic00_calls flag.

Changes:

  • Factor management-canister query execution into a shared subnet_query module and reuse it from both end-user query handling and composite-query handling.
  • Route composite-query ic:00 calls to the local subnet and execute them in the query handler, including instruction accounting and metrics.
  • Ensure composite-query contexts that call the management canister are never cached; add/extend tests and metrics to cover the new behavior.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
rs/test_utilities/execution_environment/src/lib.rs Adds a test-builder helper to enable the composite_query_ic00_calls feature flag.
rs/execution_environment/tests/canister_logging.rs Updates state-machine tests for fetch_canister_logs behavior when composite-query ic00 calls are enabled/disabled.
rs/execution_environment/src/query_handler/tests.rs Adds query-handler tests covering composite-query calls into management canister query methods and instruction charging.
rs/execution_environment/src/query_handler/subnet_query.rs Introduces a shared implementation for executing management canister query methods against a local subnet state.
rs/execution_environment/src/query_handler/query_context.rs Adds composite-query handling for routed-to-subnet management calls, instruction charging, and tracks ic00_calls for cache bypass.
rs/execution_environment/src/query_handler/query_cache/tests.rs Adds tests asserting composite-query results are never cached when management canister is called (including nested/callback cases).
rs/execution_environment/src/query_handler/query_cache.rs Skips caching when ic00_calls > 0 and adds a dedicated invalidation metric.
rs/execution_environment/src/query_handler.rs Refactors management-canister query handling to use subnet_query and wires ic00_calls into cache insertion.
rs/execution_environment/src/metrics.rs Adjusts subnet-query observation to accept encoded replies (Vec<u8>) rather than WasmResult.
rs/execution_environment/src/execution/response.rs Removes composite-query origin checks in replicated response handling (composite queries handled via query handler).
rs/execution_environment/src/execution/call_or_task.rs Removes composite-query detection in replicated execution paths (composite queries are non-replicated).
rs/execution_environment/src/canister_logs.rs Factors out fetch_canister_logs_reply to share reply generation + instruction accounting across replicated/non-replicated paths.
rs/embedders/src/wasmtime_embedder/system_api/sandbox_safe_system_state.rs Routes composite-query management-canister requests to the local subnet instead of payload-based routing.
rs/embedders/src/wasmtime_embedder/system_api/routing.rs Simplifies management-canister routing API and removes composite-query special-casing from routing logic.
rs/config/src/execution_environment.rs Adds the composite_query_ic00_calls config flag (disabled by default) and documents it.
Suppressed comments (1)

rs/execution_environment/tests/canister_logging.rs:487

  • Typo in comment: "relatioship" → "relationship".
    // There are 3 actors with the following controller relatioship: user -> canister_a -> canister_b.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@mraszyk
mraszyk marked this pull request as ready for review August 3, 2026 15:47
@mraszyk
mraszyk requested a review from a team as a code owner August 3, 2026 15:47
@zeropath-ai

zeropath-ai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

✅ No security or compliance issues detected. Reviewed everything up to 276ff9a.

Security Overview
Detected Code Changes
Change Type Relevant files
Refactor ► rs/embedders/src/wasmtime_embedder/system_api/routing.rs
    Update resolve_destination documentation and logic; remove is_composite_query parameter and related branching for FetchCanisterLogs
► rs/embedders/src/wasmtime_embedder/system_api/sandbox_safe_system_state.rs
    Adapt routing usage to new destination resolution and composite query handling
Enhancement ► rs/execution_environment/benches/management_canister/canister_logging.rs
    Align benchmark expectations with updated fetch_canister_logsResponse signature
► rs/execution_environment/src/canister_logs.rs
    Use new fetch_canister_logs_response signature and adjust instruction accounting
► rs/execution_environment/src/execution/call_or_task.rs
    Pass explicit is_composite_query handling as false in certain calls
► rs/execution_environment/src/execution/response.rs
    Disable composite-query specific path for is_composite_query in responses
► rs/execution_environment/src/hypervisor.rs
    Extend composite query handling to include additional ApiType variants for routing decisions
► rs/execution_environment/src/metrics.rs
    Add observe_subnet_query_message signature change to accommodate non-replicated management canister queries
► rs/execution_environment/src/query_handler.rs
    Adjust imports and wiring for subnet_query and ic00 call handling
► rs/execution_environment/src/query_handler/query_cache.rs
    Introduce ic00 call invalidation path and related metrics
► rs/execution_environment/src/query_handler/query_cache/tests.rs
    Update tests to reflect ic00 call invalidation and non-caching behavior for management canister interactions
► rs/execution_environment/src/query_handler/query_context.rs
    Propagate CanisterManager reference and ic00_calls tracking; switch to metrics-based error handling in place of direct counters
► rs/execution_environment/src/query_handler/query_context.rs
    Update call graph execution to use subnet_query and adjusted metrics handling
► rs/execution_environment/src/query_handler/query_context.rs
    Adjust creation to include ic00_calls counter and pass metrics where needed
Bug Fix ► rs/execution_environment/src/query_handler/query_context.rs
    Fix usage of composite query handling by routing management canister calls accordingly
► rs/execution_environment/src/query_handler.rs
    Fix imports to align with new subnet_query module and management canister query handling
Other ► Numerous in-file comments and doc updates across routing, sandbox_safe_system_state, hypervisor, and query handling to reflect new composite query routing semantics and management canister interactions

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

Can't claim to have found that one myself, but pasting here:

ic:00 calls from a composite callback still go through resolve_destination

hypervisor.rs:330 sets the flag as matches!(api_type, ApiType::CompositeQuery { .. }), which excludes CompositeReplyCallback and CompositeRejectCallback. Both variants flow through the same Hypervisor::execute from query_context.rs:665, so a call made from a reply/reject callback reaches sandbox_safe_system_state.rs:474 and is routed by method+payload.

Comment thread rs/execution_environment/src/metrics.rs Resolved
mraszyk and others added 2 commits August 5, 2026 13:08
…s_response

`fetch_canister_logs_response` returned the record count and content size
solely so that `fetch_canister_logs_reply` could feed them to
`fetch_canister_logs_instructions`; the only other caller (the benchmark
entry point) discarded both. Merge the two into a single
`fetch_canister_logs_response` returning the Candid-encoded reply together
with the instructions consumed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
… subnet

`is_composite_query` only matched `ApiType::CompositeQuery`, so it was false
while executing the reply and reject callbacks of a composite query. Both of
those can make calls themselves, so a call to the management canister from a
callback was routed by `resolve_destination` based on the method and the
payload instead of being pinned to the own subnet.

Since the query handler recognizes a management canister call solely by its
receiver being the own subnet ID, such a call reached the intended handler
only when routing happened to resolve to the own subnet anyway. A target on
another subnet was instead rejected with `CanisterNotFound`, and an
unroutable target, an undecodable payload, or an unknown method name were
rejected by the routing layer rather than by the query handler.

Match all composite API types instead. `CompositeCleanup` cannot currently
make calls, but is included so that the flag means "is part of a composite
query" rather than "can currently make calls".

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@mraszyk

mraszyk commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Can't claim to have found that one myself, but pasting here:

ic:00 calls from a composite callback still go through resolve_destination
hypervisor.rs:330 sets the flag as matches!(api_type, ApiType::CompositeQuery { .. }), which excludes CompositeReplyCallback and CompositeRejectCallback. Both variants flow through the same Hypervisor::execute from query_context.rs:665, so a call made from a reply/reject callback reaches sandbox_safe_system_state.rs:474 and is routed by method+payload.

Good point! Fixed here: 536c79f

The happy path already worked, but this unifies the error message on the failure path.

Remove the dedicated `composite_query_ic00_calls` feature flag and thus enable
calls to the management canister query methods from composite queries by
default.

Requests from a composite query to the management canister were already routed
unconditionally to the own subnet, so with the flag disabled such a request was
simply rejected with `CanisterNotFound` by the query handler. Dropping the flag
makes the query handler execute the requested query method instead. The two
tests asserting the flag-disabled behavior are removed, and
`composite_query_cache_never_caches_calls_to_management_canister` is renamed and
updated: the `raw_rand` call it makes is now rejected by the query handler as
not being a query method, and the query context is marked as calling the
management canister.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated no new comments.

Suppressed comments (1)

rs/execution_environment/src/query_handler/tests.rs:1602

  • This test expects the composite query call to an unknown management-canister method to fail with CanisterDidNotReply, but composite-query IC00 calls are now routed to the query handler on the local subnet, so parse_query_method will reject the call and the universal canister’s on_reject(...append_and_reply()) should turn it into an Ok(WasmResult::Reply(...)) (similar to the raw_rand test above).
    let err = test
        .non_replicated_query(
            canister_id,
            "composite_query",
            ic00_composite_query("unknown", Encode!().unwrap()),

@mraszyk
mraszyk added this pull request to the merge queue Aug 5, 2026
Merged via the queue into master with commit 525a442 Aug 5, 2026
43 checks passed
@mraszyk
mraszyk deleted the mraszyk/composite-query-ic00-calls branch August 5, 2026 15:58
mraszyk added a commit to dfinity/developer-docs that referenced this pull request Aug 18, 2026
…ethods (#342)

Reflect dfinity/ic#10987: composite query methods and their callbacks
can call the management canister query methods `canister_status`,
`canister_metrics`, `fetch_canister_logs`, and `list_canisters`.

Such a call is not routed based on the method name and the argument: it
is always executed against the state of the subnet hosting the calling
canister and can thus only target canisters hosted by that subnet. The
access control is the same as for the corresponding query call submitted
by a user, with the calling canister as the caller. Calls to all other
management canister methods are rejected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants