fix(task): forward extension paths to subagents, rebind extensions per session

Same shape of bug the reviewer flagged for custom tools: forwarding
`LoadExtensionsResult` from parent to subagent reused Extension instances
whose factories closed over the parent's `ExtensionAPI` — cwd, eventBus,
and runtime all pointed at the parent. Any tool/handler/command that
referenced `api.exec()`, `api.events`, or `api.runtime` still acted on the
parent session/worktree from inside an isolated subagent.

Forward only the path list; each session rebuilds extensions through
`loadExtensions` so factories see the right `ExtensionAPI`.

- `extensibility/extensions/loader.ts`: extract `discoverExtensionPaths`
  (FS scan only) from `discoverAndLoadExtensions`. The combined helper now
  composes the two. New export added to the package barrel.
- `sdk.ts`:
  - Add `discoverSessionExtensionPaths()` (the `disableExtensionDiscovery`-aware
    path-only counterpart of `loadSessionExtensions`).
  - Add `preloadedExtensionPaths?: string[]` to `CreateAgentSessionOptions`.
    Three loader branches: `preloadedExtensions` (CLI same-process reuse,
    still shallow-cloned), `preloadedExtensionPaths` (subagent: skip scan,
    reload locally), or full discovery.
  - Document `preloadedExtensions` as same-process-only; subagent
    forwarding MUST use `preloadedExtensionPaths`.
- `tools/index.ts`: `ToolSession.extensionsResult` → `extensionPaths:
  string[]` for the same reason.
- `task/executor.ts` and `task/index.ts`: forward `extensionPaths`. Drop
  the forward for the isolated `runSubprocess` branch — worktree cwd ≠
  parent cwd, so the subagent re-discovers extensions against its own
  tree.
- New `test/sdk-extensions-per-session-binding.test.ts` pins the contract:
  two `loadExtensions` calls on the same path with different `cwd` and
  different `EventBus` instances yield distinct Extension + runtime
  objects whose factories close over the per-call bindings.
- Updated `executor-pass-through` and `sdk-preloaded-extensions-isolation`
  tests for the new option name and comment context.

Refs PR review on #2193
This commit is contained in:
roboomp
2026-06-09 14:19:47 +00:00
parent dde53a8f18
commit fe34d0f392
10 changed files with 232 additions and 69 deletions
+7 -4
View File
@@ -18,7 +18,6 @@ import type { ToolPathWithSource } from "../extensibility/custom-tools";
import type { CustomTool } from "../extensibility/custom-tools/types";
import { runExtensionCompact, runExtensionSetModel } from "../extensibility/extensions/compact-handler";
import { getSessionSlashCommands } from "../extensibility/extensions/get-commands-handler";
import type { LoadExtensionsResult } from "../extensibility/extensions/types";
import { buildSkillPromptMessage, type Skill } from "../extensibility/skills";
import type { HindsightSessionState } from "../hindsight/state";
import type { LocalProtocolOptions } from "../internal-urls";
@@ -195,8 +194,12 @@ export interface ExecutorOptions {
workspaceTree?: WorkspaceTree;
/** Parent-discovered rules, forwarded to skip rule discovery in the subagent. */
rules?: Rule[];
/** Parent-loaded extensions, forwarded via `preloadedExtensions` to skip discovery. */
preloadedExtensions?: LoadExtensionsResult;
/**
* Parent's discovered extension source paths. Forwarded to skip the
* extension FS scan in the subagent; the subagent then re-binds each
* extension against its own `ExtensionAPI` (cwd, eventBus, runtime).
*/
preloadedExtensionPaths?: string[];
/**
* Parent's discovered custom-tool source paths. Forwarded to skip the
* `.omp/tools/` FS scan in the subagent; the subagent then re-binds each
@@ -1298,7 +1301,7 @@ export async function runSubprocess(options: ExecutorOptions): Promise<SingleRes
promptTemplates: options.promptTemplates,
workspaceTree: options.workspaceTree,
rules: options.rules,
preloadedExtensions: options.preloadedExtensions,
preloadedExtensionPaths: options.preloadedExtensionPaths,
preloadedCustomToolPaths: options.preloadedCustomToolPaths,
systemPrompt: defaultPrompt => {
const subagentPrompt = prompt.render(subagentSystemPromptTemplate, {