fe34d0f392
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
136 lines
4.9 KiB
TypeScript
136 lines
4.9 KiB
TypeScript
/**
|
|
* Verifies parent-discovered rules, extensions, and custom tools are forwarded
|
|
* to `createAgentSession` so subagents skip the FS scans the parent already
|
|
* paid for. Regression guard for issue #2190.
|
|
*/
|
|
import { afterEach, describe, expect, it, vi } from "bun:test";
|
|
import type { Rule } from "@oh-my-pi/pi-coding-agent/capability/rule";
|
|
import type { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
|
|
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
|
import type { ToolPathWithSource } from "@oh-my-pi/pi-coding-agent/extensibility/custom-tools";
|
|
import type { LoadExtensionsResult } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types";
|
|
import type { CreateAgentSessionResult } from "@oh-my-pi/pi-coding-agent/sdk";
|
|
import * as sdkModule from "@oh-my-pi/pi-coding-agent/sdk";
|
|
import type { AgentSession, AgentSessionEvent, PromptOptions } from "@oh-my-pi/pi-coding-agent/session/agent-session";
|
|
import { runSubprocess } from "@oh-my-pi/pi-coding-agent/task/executor";
|
|
import type { AgentDefinition } from "@oh-my-pi/pi-coding-agent/task/types";
|
|
import { EventBus } from "@oh-my-pi/pi-coding-agent/utils/event-bus";
|
|
|
|
function createMockSession(onPrompt: (params: { emit: (event: AgentSessionEvent) => void }) => void): AgentSession {
|
|
const listeners: Array<(event: AgentSessionEvent) => void> = [];
|
|
const emit = (event: AgentSessionEvent) => {
|
|
for (const listener of listeners) listener(event);
|
|
};
|
|
const session = {
|
|
state: { messages: [] },
|
|
agent: { state: { systemPrompt: ["test"] } },
|
|
model: undefined,
|
|
extensionRunner: undefined,
|
|
sessionManager: { appendSessionInit: () => {} },
|
|
getActiveToolNames: () => ["read", "yield"],
|
|
setActiveToolsByName: async (_toolNames: string[]) => {},
|
|
subscribe: (listener: (event: AgentSessionEvent) => void) => {
|
|
listeners.push(listener);
|
|
return () => {
|
|
const index = listeners.indexOf(listener);
|
|
if (index >= 0) listeners.splice(index, 1);
|
|
};
|
|
},
|
|
prompt: async (_text: string, _options?: PromptOptions) => {
|
|
onPrompt({ emit });
|
|
},
|
|
waitForIdle: async () => {},
|
|
getLastAssistantMessage: () => undefined,
|
|
abort: async () => {},
|
|
dispose: async () => {},
|
|
};
|
|
return session as unknown as AgentSession;
|
|
}
|
|
|
|
function yieldEmittingSession(): AgentSession {
|
|
return createMockSession(({ emit }) => {
|
|
emit({
|
|
type: "tool_execution_end",
|
|
toolCallId: "tool-pass-through",
|
|
toolName: "yield",
|
|
result: {
|
|
content: [{ type: "text", text: "Result submitted." }],
|
|
details: { status: "success", data: { ok: true } },
|
|
},
|
|
isError: false,
|
|
});
|
|
});
|
|
}
|
|
|
|
function createSessionResult(session: AgentSession): CreateAgentSessionResult {
|
|
return {
|
|
session,
|
|
extensionsResult: { extensions: [], errors: [], runtime: {} as unknown } as unknown as LoadExtensionsResult,
|
|
setToolUIContext: () => {},
|
|
eventBus: new EventBus(),
|
|
};
|
|
}
|
|
|
|
const baseAgent: AgentDefinition = {
|
|
name: "task",
|
|
description: "test",
|
|
systemPrompt: "test",
|
|
source: "bundled",
|
|
};
|
|
|
|
const baseOptions = {
|
|
cwd: "/tmp",
|
|
agent: baseAgent,
|
|
task: "do work",
|
|
index: 0,
|
|
id: "subagent-pass-through",
|
|
settings: Settings.isolated(),
|
|
modelRegistry: { refresh: async () => {} } as unknown as ModelRegistry,
|
|
enableLsp: false,
|
|
};
|
|
|
|
describe("runSubprocess parent-discovery pass-through (issue #2190)", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("forwards rules, preloadedExtensionPaths, and preloadedCustomToolPaths to createAgentSession", async () => {
|
|
const session = yieldEmittingSession();
|
|
const spy = vi.spyOn(sdkModule, "createAgentSession").mockResolvedValue(createSessionResult(session));
|
|
|
|
const rules: Rule[] = [{ name: "rule-a" } as unknown as Rule];
|
|
const preloadedExtensionPaths = ["/abs/parent/.omp/extensions/foo.ts"];
|
|
const preloadedCustomToolPaths: ToolPathWithSource[] = [
|
|
{ path: "tools/x.ts", source: { provider: "config", providerName: "Config", level: "project" } },
|
|
];
|
|
|
|
const result = await runSubprocess({
|
|
...baseOptions,
|
|
rules,
|
|
preloadedExtensionPaths,
|
|
preloadedCustomToolPaths,
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
const forwarded = spy.mock.calls[0]?.[0];
|
|
// Identity, not equality: passing a clone would defeat the perf fix.
|
|
expect(forwarded?.rules).toBe(rules);
|
|
expect(forwarded?.preloadedExtensionPaths).toBe(preloadedExtensionPaths);
|
|
expect(forwarded?.preloadedCustomToolPaths).toBe(preloadedCustomToolPaths);
|
|
});
|
|
|
|
it("forwards undefined when the parent has not pre-discovered state", async () => {
|
|
const session = yieldEmittingSession();
|
|
const spy = vi.spyOn(sdkModule, "createAgentSession").mockResolvedValue(createSessionResult(session));
|
|
|
|
const result = await runSubprocess({ ...baseOptions });
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
const forwarded = spy.mock.calls[0]?.[0];
|
|
expect(forwarded?.rules).toBeUndefined();
|
|
expect(forwarded?.preloadedExtensionPaths).toBeUndefined();
|
|
expect(forwarded?.preloadedCustomToolPaths).toBeUndefined();
|
|
});
|
|
});
|