Skip to content

Load workspace modules on demand - #13406

Closed
eunomie wants to merge 6 commits into
dagger:mainfrom
eunomie:module-loading
Closed

eunomie wants to merge 6 commits into
dagger:mainfrom
eunomie:module-loading

Conversation

@eunomie

@eunomie eunomie commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

Problem

Workspace module loading is one-shot: the first schema-needing request loads every configured module, so dagger generate|check|up|call|functions <module> pays for all its siblings, and a single broken or stale module blocks operating on any other module — for generate, including the very module you were trying to fix. The existing narrowing (peek root fields → drop pending modules) is restricted to SingleQuery clients because dropped modules can't come back in a later request.

Alternative design to #13380, which extends the request peeks and hints instead. Design doc included: hack/designs/demand-driven-module-loading.md.

Fix

Make loading additive and demand-driven: each request loads only the modules it can possibly touch; the rest stay pending and load when a later request demands them. The dagql schema is already rebuilt from the served set per request, so the session schema grows monotonically — narrowing becomes deferral rather than exclusion, safe for open multi-request sessions, and the root-field demand generalizes from SingleQuery to every client.

The demand source is the natural one for each query shape:

  • Root fields naming a pending module load just that module (existing entrypoint fallback preserved); full-schema fields (__schema, …) load everything; anything unrecognized conservatively loads everything.
  • currentWorkspace { checks|generators|services(include: …) } validates against the core schema, so loading moves into the resolvers via a new Query.Server.EnsureWorkspaceModules hook — they receive include natively, no request peeking or variable resolution. Since the CLI already sends include as the command's functional argument, dagger generate|check|up <module> narrows with zero CLI change.
  • currentTypeDefs gains an optional include load hint consumed at resolver entry through the same hook; the CLI passes its leading positional token, narrowing dagger call/functions <module>. Because loading now happens inside the resolver, the result cache also mixes in the client ID (the pre-request load previously made the schema-digest key workspace-unique as a side effect).

Other behavior consequences:

  • Extra (-m) modules keep loading eagerly with sticky errors (explicitly requested).
  • Ambient module failures are per-module and non-sticky: a broken module only fails requests that demand it, and stays pending so later demands report its load error. Full listings still load everything and surface it.
  • Entrypoint arbitration is split across batches: extras outrank ambient candidates; multiple ambient entrypoint declarations load together to preserve the existing conflict detection.

The currentTypeDefs(include:) phase (the API addition, the CLI hint, its tests, and the SDK/docs regeneration commit) is independent and can be dropped to keep only the engine-side phase. Two engine-only refactors close the series: per-module load state consolidated into a single lifecycle map (pending → served | failed), and the shared dedupe→arbitrate→serve tail of ambient/extra batch loading extracted into one helper.

Tests

  • Unit: demand filters — root-field selection with served-module recognition, selector-include module-name resolution (module, module:item, entrypoint/typo fallbacks, already-served patterns).
  • Integration: TestWorkspace{Generate,Check,Up,Call}NarrowsToRequestedModule against a new generators-broken fixture (healthy dang module with a generator, a check, and a service, alongside a module with invalid source): targeting the healthy module never loads the broken one — generate -y exercising the multi-request path — while full listings still surface it. The existing single-query narrowing test passes unchanged on the generalized path, as do the full TestWorkspace/TestGenerators suites against a dev engine.

@eunomie
eunomie force-pushed the module-loading branch 4 times, most recently from 2da8d00 to 1559d7b Compare June 12, 2026 15:23
@eunomie
eunomie force-pushed the module-loading branch 5 times, most recently from 863439a to 63dc046 Compare June 23, 2026 07:12
@eunomie
eunomie marked this pull request as ready for review June 23, 2026 07:27
@eunomie
eunomie requested review from a team as code owners June 23, 2026 07:27
@shykes

shykes commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

currentTypeDefs gains an optional include load hint consumed at resolver entry through the same hook; the CLI passes its leading positional token, narrowing dagger call/functions

Can you explain this part please?

@eunomie

eunomie commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

The way modules are loaded and introspected from the CLI changed.
Before we were loading all the modules and then read the currentTypeDefs results that contains everything.
With this PR we call currentTypeDefs without to load the modules and by giving a "hint" about the module to load. The hint is the first element on the CLI but it can be a module or a function from an entrypoint module, that's why it's just a hint and not a strong requirement. And then only the corresponding module is loaded

Before

sequenceDiagram
    autonumber
    participant CLI
    participant H as Engine HTTP handler<br/>(session.go)
    participant L as Module loader
    participant S as dagql schema
    participant R as currentTypeDefs resolver
    participant C as dagql result cache

    Note over CLI: dagger call MODULE fn --arg ...
    CLI->>H: query { currentTypeDefs { ... } }<br/>PLAIN doc, no include
    Note right of CLI: multi-request session →<br/>SingleQuery peek/narrowing does NOT apply

    H->>H: ensureWorkspaceLoaded() (discover)
    H->>L: ensureModulesLoaded() — ALL pending
    L-->>H: MODULE ok, sibling-A ok, sibling-BROKEN FAILS
    Note over H,L: one broken sibling fails the whole request

    H->>S: build schema = core + MODULE + sibling-A + sibling-BROKEN
    H->>R: resolve currentTypeDefs
    R->>C: key = CurrentSchemaInput = digest(served schema)
    Note over C: served schema already contains workspace modules<br/>→ key differs per workspace for free (no client ID needed)
    R-->>CLI: typedefs (everything served)
    Note over CLI: build command tree → send actual call (2nd request)
Loading

After

sequenceDiagram
    autonumber
    participant CLI
    participant H as Engine HTTP handler<br/>(session.go)
    participant S as dagql schema
    participant R as currentTypeDefs resolver
    participant C as dagql result cache
    participant L as Module loader

    Note over CLI: dagger call MODULE fn --arg ...<br/>extract leading positional token → "MODULE"
    CLI->>H: query { currentTypeDefs(include: ["MODULE"]) { ... } }<br/>old engine rejects arg → fall back to PLAIN doc (load-all)

    H->>H: ensureWorkspaceLoaded() (discover)
    Note over H: ambient modules NOT loaded here (only -m extras)
    H->>S: build schema = core only
    Note over S: currentTypeDefs is a core field →<br/>query validates with no modules loaded

    H->>R: resolve currentTypeDefs(include: ["MODULE"])
    R->>C: key = CurrentSchemaInput(core only) + PerClientInput(client ID)
    Note over C: schema digest is core-only → identical across workspaces<br/>PerClientInput (client ID) is what prevents an A/B collision
    R->>L: EnsureWorkspaceModulesForTypeDefs(["MODULE"])
    L-->>R: MODULE ok (+ entrypoint module if configured)
    Note over L: sibling-A, sibling-BROKEN stay PENDING (never evaluated)
    R-->>CLI: typedefs for MODULE
    Note over CLI: build command tree → send actual call (2nd request)
Loading

eunomie added a commit to eunomie/dagger that referenced this pull request Jun 23, 2026
Document an alternative to dagger#13406 (demand-driven workspace module
loading) that keeps the same engine foundation but moves the
`dagger call`/`functions` narrowing signal out of the client. Instead of
a new public `currentTypeDefs(include:)` argument and the client-side
document building, version gating and SDK regeneration it implies, the
CLI names its typedefs introspection operation after the target module
and the engine peeks the operation name. The same query works against
every engine, so the CLI carries no engine-version fallback.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jun 23, 2026
Document an alternative to dagger#13406 (demand-driven workspace module
loading) that keeps the same engine foundation but moves the
`dagger call`/`functions` narrowing signal out of the client. Instead of
a new public `currentTypeDefs(include:)` argument and the client-side
document building, version gating and SDK regeneration it implies, the
CLI names its typedefs introspection operation after the target module
and the engine peeks the operation name. The same query works against
every engine, so the CLI carries no engine-version fallback.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jun 23, 2026
Document an alternative to dagger#13406 (demand-driven workspace module
loading) that keeps the same engine foundation but moves the
`dagger call`/`functions` narrowing signal out of the client. Instead of
a new public `currentTypeDefs(include:)` argument and the client-side
document building, version gating and SDK regeneration it implies, the
CLI names its typedefs introspection operation after the target module
and the engine peeks the operation name. The same query works against
every engine, so the CLI carries no engine-version fallback.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jun 23, 2026
Document an alternative to dagger#13406 (demand-driven workspace module
loading) that keeps the same engine foundation but moves the
`dagger call`/`functions` narrowing signal out of the client. Instead of
a new public `currentTypeDefs(include:)` argument and the client-side
document building, version gating and SDK regeneration it implies, the
CLI names its typedefs introspection operation after the target module
and the engine peeks the operation name. The same query works against
every engine, so the CLI carries no engine-version fallback.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jun 23, 2026
Document an alternative to dagger#13406 (demand-driven workspace module
loading) that keeps the same engine foundation but moves the
`dagger call`/`functions` narrowing signal out of the client. Instead of
a new public `currentTypeDefs(include:)` argument and the client-side
document building, version gating and SDK regeneration it implies, the
CLI names its typedefs introspection operation after the target module
and the engine peeks the operation name. The same query works against
every engine, so the CLI carries no engine-version fallback.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added 3 commits June 25, 2026 10:23
Document the design for loading workspace modules on demand instead of
all-at-once: every request loads only the modules it can possibly touch,
the rest stay pending and load when a later request (or a workspace
resolver) demands them. Narrowing becomes deferral rather than exclusion,
which makes it safe for open multi-request sessions without any new
client-server contract.

Signed-off-by: Yves Brissaud <[email protected]>
Replace the one-shot ensureModulesLoaded with per-request, per-module
loading. Each request loads only the pending workspace modules it
demands -- root fields naming a module load just that module (plus the
existing entrypoint fallback), full-schema fields load everything -- and
unselected modules stay pending, loading when a later request demands
them. The dagql schema is already rebuilt from the served set per
request, so the session schema grows monotonically: narrowing is
deferral, not exclusion, and is therefore safe for open multi-request
sessions, generalizing the previous SingleQuery-only peek to every
client.

Extra (-m) modules are explicitly requested, so they keep loading
eagerly on the first request with sticky errors. Ambient module failures
become per-module: a broken module only fails requests that demand it,
and stays pending so later demands report its load error. Entrypoint
arbitration is split across batches: extras load first and outrank
ambient candidates; multiple ambient entrypoint declarations load
together to preserve the existing conflict detection. Served identities
and names are tracked so cross-batch loads stay deduplicated and demand
filters keep recognizing already-loaded modules.

Two on-demand hooks (EnsureWorkspaceModules for the workspace selector
resolvers, EnsureWorkspaceModulesForTypeDefs for targeted typedefs
introspection) are provided here and wired up by the following patches.
Cancellation and deadline errors are never recorded as module failures
-- an interrupted load stays retryable by later demands in the session.

Signed-off-by: Yves Brissaud <[email protected]>
The entire currentWorkspace API and currentTypeDefs return core types, so
queries like `currentWorkspace { generators(include: ["good"]) }` and the
CLI's typedefs introspection validate against the core schema with no
modules loaded -- only the resolver's result needs them. Move workspace
module loading into the resolvers via the Query.Server hooks added in the
previous patch:

  - checks/generators/services call EnsureWorkspaceModules with their
    native include argument, so `dagger generate|check|up <module>` loads
    only the named module with no CLI or API change (the include is
    already the command's functional argument), and an unrelated broken
    or stale module can no longer block the operation.

  - currentTypeDefs gains an optional include argument consumed through
    EnsureWorkspaceModulesForTypeDefs: patterns naming workspace modules
    load just those (plus the entrypoint module, whose functions are
    proxied onto the Query root), and no include loads every pending
    module -- preserving bare `dagger functions`/`shell`. The argument is
    gated to v1.0.0+ so it stays out of the base schema view.

Because loading now happens inside the resolver, the currentTypeDefs
result cache also mixes in the client ID: the schema digest at call time
no longer distinguishes which workspace the session is in, so without it
sessions in different workspaces would share a cache entry.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added 3 commits June 25, 2026 10:23
`dagger call` and `dagger functions` build their command tree from a
currentTypeDefs introspection that previously loaded every workspace
module. Pass the leading positional token (extracted with the same
flag-skipping logic as the function-name telemetry) as the include hint:
only the first token can name a workspace module (later tokens are
functions or arguments within it), so targeting a module no longer loads
its siblings, and an unrelated broken or stale module cannot block the
call.

Engines that predate the include argument reject any document naming it
at validation, even with a null variable. The plain typedefs document
therefore stays as-is for unscoped introspections; a derived
include-scoped document is sent only when a hint exists, and an "Unknown
argument" rejection falls back to the plain document, loading every
workspace module exactly like older CLIs.

Signed-off-by: Yves Brissaud <[email protected]>
Add a generators-broken workspace fixture -- a healthy dang module
exposing a generator, a check, and a service, alongside a module with
intentionally invalid source -- and lock in that `dagger generate`,
`check`, `up`, `call`, and `functions` targeting the healthy module never
load the broken one, while full listings still load every module and
surface it. The generate test runs with -y, exercising the multi-request
path where later requests must keep recognizing the already-loaded
module.

A second fixture (call-narrowing) covers the demand shapes single-word
module names cannot: a module declared as "goodMod" is targeted through
its kebab-case CLI command name (`dagger call good-mod ...`, and
`dagger generate -l good-mod` for the selector path), and with a
workspace entrypoint configured, calling one of its root-proxied
functions loads the entrypoint module alone. In both cases the broken
sibling module stays unloaded.

Signed-off-by: Yves Brissaud <[email protected]>
Regenerate the SDK client bindings and reference docs for the new
currentTypeDefs include argument.

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie
eunomie marked this pull request as draft July 1, 2026 09:25
eunomie added a commit to eunomie/dagger that referenced this pull request Jul 2, 2026
Design for extending the demand-driven workspace module loading (merged
for generate/check/up in dagger#13548) to dagger api call and api functions.

The introspection these commands build their command tree from
(currentTypeDefs) names no module, so the target must cross the wire:
the chosen design carries the CLI's leading command token in a new
ClientMetadata workspace_module_scope hint, resolved engine-side with
the same kebab/entrypoint rules as the merged demand filters and
consumed one-shot by the first request whose only full-schema demand is
currentTypeDefs. No GraphQL API change, no SDK regeneration, no version
gating; old engines ignore the unknown metadata key and old CLIs simply
do not send it.

Includes the investigation behind the choice: the merged foundation,
why call/functions are the hard case, a post-mortem of the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543), and the alternatives ranked.

Signed-off-by: Yves Brissaud <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

eunomie added a commit to eunomie/dagger that referenced this pull request Jul 16, 2026
Design for extending the demand-driven workspace module loading (merged
for generate/check/up in dagger#13548) to dagger api call and api functions.

The introspection these commands build their command tree from
(currentTypeDefs) names no module, so the target must cross the wire:
the chosen design carries the CLI's leading command token in a new
ClientMetadata workspace_module_scope hint, resolved engine-side with
the same kebab/entrypoint rules as the merged demand filters and
consumed one-shot by the first request whose only full-schema demand is
currentTypeDefs. No GraphQL API change, no SDK regeneration, no version
gating; old engines ignore the unknown metadata key and old CLIs simply
do not send it.

Includes the investigation behind the choice: the merged foundation,
why call/functions are the hard case, a post-mortem of the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543), and the alternatives ranked.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jul 16, 2026
Design for extending the demand-driven workspace module loading (merged
for generate/check/up in dagger#13548) to dagger api call and api functions.

The introspection these commands build their command tree from
(currentTypeDefs) names no module, so the target must cross the wire:
the chosen design carries the CLI's leading command token in a new
ClientMetadata workspace_module_scope hint, resolved engine-side with
the same kebab/entrypoint rules as the merged demand filters and
consumed one-shot by the first request whose only full-schema demand is
currentTypeDefs. No GraphQL API change, no SDK regeneration, no version
gating; old engines ignore the unknown metadata key and old CLIs simply
do not send it.

Includes the investigation behind the choice: the merged foundation,
why call/functions are the hard case, a post-mortem of the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543), and the alternatives ranked.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jul 16, 2026
Design for extending the demand-driven workspace module loading (merged
for generate/check/up in dagger#13548) to dagger api call and api functions.

The introspection these commands build their command tree from
(currentTypeDefs) names no module, so the target must cross the wire:
the chosen design carries the CLI's leading command token in a new
ClientMetadata workspace_module_scope hint, resolved engine-side with
the same kebab/entrypoint rules as the merged demand filters and
consumed one-shot by the first request whose only full-schema demand is
currentTypeDefs. No GraphQL API change, no SDK regeneration, no version
gating; old engines ignore the unknown metadata key and old CLIs simply
do not send it.

Includes the investigation behind the choice: the merged foundation,
why call/functions are the hard case, a post-mortem of the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543), and the alternatives ranked.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jul 16, 2026
Document, in hack/designs/done, how dagger api call and api functions
narrow workspace module loading to their target: the CLI forwards its
leading command-line word as the workspace_module_scope client metadata
hint and the engine resolves it, completing the demand-driven loading
that generate/check/up already had. Covers the behavior table, the
one-shot-per-session rule, nested-client plumbing, why the target rides
connection metadata rather than a new API surface, and the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543).

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request Jul 16, 2026
Document, in hack/designs/done, how dagger api call and api functions
narrow workspace module loading to their target: the CLI forwards its
leading command-line word as the workspace_module_scope client metadata
hint and the engine resolves it, completing the demand-driven loading
that generate/check/up already had. Covers the behavior table, the
one-shot-per-session rule, nested-client plumbing, why the target rides
connection metadata rather than a new API surface, and the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543).

Signed-off-by: Yves Brissaud <[email protected]>
tiborvass pushed a commit to eunomie/dagger that referenced this pull request Jul 20, 2026
Document, in hack/designs/done, how dagger api call and api functions
narrow workspace module loading to their target: the CLI forwards its
leading command-line word as the workspace_module_scope client metadata
hint and the engine resolves it, completing the demand-driven loading
that generate/check/up already had. Covers the behavior table, the
one-shot-per-session rule, nested-client plumbing, why the target rides
connection metadata rather than a new API surface, and the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543).

Signed-off-by: Yves Brissaud <[email protected]>
tiborvass pushed a commit to eunomie/dagger that referenced this pull request Jul 21, 2026
Document, in hack/designs/done, how dagger api call and api functions
narrow workspace module loading to their target: the CLI forwards its
leading command-line word as the workspace_module_scope client metadata
hint and the engine resolves it, completing the demand-driven loading
that generate/check/up already had. Covers the behavior table, the
one-shot-per-session rule, nested-client plumbing, why the target rides
connection metadata rather than a new API surface, and the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543).

Signed-off-by: Yves Brissaud <[email protected]>
tiborvass pushed a commit to eunomie/dagger that referenced this pull request Jul 21, 2026
Document, in hack/designs/done, how dagger api call and api functions
narrow workspace module loading to their target: the CLI forwards its
leading command-line word as the workspace_module_scope client metadata
hint and the engine resolves it, completing the demand-driven loading
that generate/check/up already had. Covers the behavior table, the
one-shot-per-session rule, nested-client plumbing, why the target rides
connection metadata rather than a new API surface, and the prior
attempts (dagger#13380, dagger#13406, dagger#13539, dagger#13543).

Signed-off-by: Yves Brissaud <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions Bot closed this Jul 23, 2026
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.

2 participants