opencode-copilot-delegate 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # opencode-copilot-delegate
2
2
 
3
+ ## 0.11.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 31dfb28: Fix host-side double registration of `copilot_delegate`, `copilot_output`, and `copilot_cancel` when the plugin is listed in both a user-level and project-level `opencode.json`.
8
+
9
+ Previously, the per-process register-once guard returned the cached real hooks to every duplicate factory invocation in the same PID. The OpenCode host iterates each plugin source's returned hook surface and registers every tool entry it finds, even when two sources return the same JS reference. That meant each tool appeared twice in the LLM-visible tool catalog under dual-source configurations.
10
+
11
+ The guard now returns empty hooks (`{}`) on duplicate invocations so the host has nothing to register a second time. The first invocation still runs `doInit` once and receives the real hooks; subsequent invocations in the same PID receive `{}` and emit a one-time warning. Heavy initialization (agent discovery, orphan reaping, RPC server startup) still runs at most once per process.
12
+
13
+ Single-source configurations are unaffected.
14
+
15
+ ### Patch Changes
16
+
17
+ - e019e86: Stabilize the cancel-process-tree test against CI runner load by polling `process.kill(pid, 0)` with a 2s deadline instead of asserting synchronously. Test-only change; no plugin behavior or consumer surface affected.
18
+
19
+ ## 0.10.1
20
+
21
+ ### Patch Changes
22
+
23
+ - 98a827e: Fix the `/copilot-status` TUI freeze caused by re-entrant dialog close handling when pressing Escape.
24
+
3
25
  ## 0.10.0
4
26
 
5
27
  ### Minor Changes
package/dist/index.js CHANGED
@@ -9881,7 +9881,8 @@ async function plugInOnce({
9881
9881
  onDuplicate?.(currentPid);
9882
9882
  } catch {}
9883
9883
  }
9884
- return existing.hooksPromise;
9884
+ await existing.hooksPromise;
9885
+ return { isFirst: false, hooks: {} };
9885
9886
  }
9886
9887
  const hooksPromise = doInit();
9887
9888
  g[SINGLETON_KEY] = {
@@ -9890,7 +9891,8 @@ async function plugInOnce({
9890
9891
  hooksPromise,
9891
9892
  warned: false
9892
9893
  };
9893
- return hooksPromise;
9894
+ const hooks = await hooksPromise;
9895
+ return { isFirst: true, hooks };
9894
9896
  }
9895
9897
 
9896
9898
  // src/runtime/rpc-server.ts
@@ -24478,20 +24480,23 @@ async function initializePlugin(input) {
24478
24480
  }
24479
24481
  };
24480
24482
  }
24481
- var CopilotDelegate = async (input) => plugInOnce({
24482
- doInit: () => initializePlugin(input),
24483
- onDuplicate: (pid) => {
24484
- const message = `[copilot-delegate] duplicate factory invocation in same process (pid=${pid}); reusing existing hooks. Multiple opencode.json sources may list this plugin.`;
24485
- console.warn(message);
24486
- input.client.app.log({
24487
- body: {
24488
- service: "copilot-delegate",
24489
- level: "warn",
24490
- message
24491
- }
24492
- }).catch(() => {});
24493
- }
24494
- });
24483
+ var CopilotDelegate = async (input) => {
24484
+ const result = await plugInOnce({
24485
+ doInit: () => initializePlugin(input),
24486
+ onDuplicate: (pid) => {
24487
+ const message = `[copilot-delegate] duplicate factory invocation in same process (pid=${pid}); returning empty hooks so the host does not double-register tools. Multiple opencode.json sources may list this plugin.`;
24488
+ console.warn(message);
24489
+ input.client.app.log({
24490
+ body: {
24491
+ service: "copilot-delegate",
24492
+ level: "warn",
24493
+ message
24494
+ }
24495
+ }).catch(() => {});
24496
+ }
24497
+ });
24498
+ return result.hooks;
24499
+ };
24495
24500
  var src_default = CopilotDelegate;
24496
24501
  export {
24497
24502
  wireRpcServerCleanup,
@@ -1,35 +1,49 @@
1
1
  /**
2
- * Per-process singleton guard for the plugin factory.
2
+ * Per-process register-once guard for the plugin factory.
3
3
  *
4
4
  * OpenCode invokes the plugin factory more than once per process when the
5
5
  * same plugin is referenced by multiple config sources (for example a
6
6
  * user-level `~/.config/opencode/opencode.json` AND a project-level
7
7
  * `opencode.json`). Each invocation evaluates the plugin module fresh —
8
- * module-local variables reset between calls — so the singleton state
9
- * must live on `globalThis` to persist across module instances within
10
- * the same process.
8
+ * module-local variables reset between calls — so the guard state must
9
+ * live on `globalThis` to persist across module instances within the
10
+ * same process.
11
11
  *
12
- * On the first invocation the init runs normally and the resulting hooks
13
- * promise is cached on `globalThis`. On subsequent invocations within
14
- * the same PID the cached promise is returned and `onDuplicate` is
15
- * invoked exactly once (subsequent duplicates are silent so logs do not
16
- * spam). Across PIDs the singleton is treated as absent and init runs
17
- * fresh — `globalThis` is per-process, but the explicit PID guard adds
18
- * defensive belt-and-suspenders against any state-leakage edge case.
12
+ * On the first invocation `doInit` runs and the resulting hooks promise
13
+ * is cached on `globalThis`; the caller receives `{ isFirst: true, hooks }`.
14
+ * On every subsequent invocation in the same PID `doInit` is skipped, the
15
+ * cached promise is awaited (so sticky rejections still propagate),
16
+ * `onDuplicate` fires exactly once, and the caller receives
17
+ * `{ isFirst: false, hooks: {} as T }`. Across PIDs the guard is treated
18
+ * as absent and init runs fresh — `globalThis` is per-process, but the
19
+ * explicit PID check adds defensive belt-and-suspenders against any
20
+ * state-leakage edge case.
21
+ *
22
+ * **Why empty hooks on duplicate invocations.** A whole-hooks singleton
23
+ * that returns the same hooks reference to both invocations does not
24
+ * deduplicate host-side tool registration: OpenCode iterates each plugin
25
+ * source's returned hook surface and registers every tool entry it finds,
26
+ * even when two sources return the same JS reference. Returning `{}` from
27
+ * the duplicate path is the only shape that prevents host-side double
28
+ * registration of `copilot_delegate`, `copilot_output`, and
29
+ * `copilot_cancel`. Verified empirically against
30
+ * `client.tool.list({...})`: under whole-hooks reuse a dual-source config
31
+ * lists each tool twice; under empty-hooks-on-duplicate it lists each
32
+ * tool once.
19
33
  *
20
34
  * The cached value is the init Promise rather than the resolved value so
21
35
  * a second invocation that arrives while the first is still awaiting
22
36
  * `mkdir` / `reapOrphans` converges on the same init result instead of
23
37
  * starting a parallel init.
24
38
  *
25
- * **Known limitation — rejected init is cached.** If `doInit()` rejects,
39
+ * **Known limitation — rejected init is sticky.** If `doInit()` rejects,
26
40
  * the rejected promise is stored on `globalThis` and every subsequent
27
- * invocation in the same PID returns the same rejection without
41
+ * invocation in the same PID surfaces the same rejection without
28
42
  * retrying init. This is intentional: re-running heavy init on every
29
- * call would defeat the singleton's purpose. Recovery requires a
30
- * process restart. OpenCode currently surfaces a failed plugin factory
31
- * as a startup error rather than retrying within the process, so this
32
- * matches the upstream contract.
43
+ * call would defeat the guard's purpose. Recovery requires a process
44
+ * restart. OpenCode currently surfaces a failed plugin factory as a
45
+ * startup error rather than retrying within the process, so this matches
46
+ * the upstream contract.
33
47
  */
34
48
  export interface PlugInOnceOptions<T> {
35
49
  /** Heavy init work that should run at most once per process. */
@@ -47,10 +61,27 @@ export interface PlugInOnceOptions<T> {
47
61
  pid?: number;
48
62
  }
49
63
  /**
50
- * Run `doInit` at most once per process, returning the cached hooks
51
- * promise on subsequent invocations within the same PID.
64
+ * Result envelope for `plugInOnce(...)`.
65
+ *
66
+ * - `isFirst: true` — caller should return `hooks` (the real hooks from
67
+ * `doInit`) to OpenCode.
68
+ * - `isFirst: false` — caller MUST return `hooks` (which is an empty `{}`)
69
+ * so the host loader does not register tools or hooks twice.
70
+ *
71
+ * Callers that just `return result.hooks` do the right thing in both
72
+ * cases without needing to inspect `isFirst` — the empty object is what
73
+ * the host should register on the duplicate source.
74
+ */
75
+ export interface PlugInOnceResult<T> {
76
+ isFirst: boolean;
77
+ hooks: T;
78
+ }
79
+ /**
80
+ * Run `doInit` at most once per process; on duplicate invocations resolve
81
+ * to empty hooks so the OpenCode host does not double-register tools and
82
+ * hooks under dual-source configurations.
52
83
  */
53
- export declare function plugInOnce<T>({ doInit, onDuplicate, pid, }: PlugInOnceOptions<T>): Promise<T>;
84
+ export declare function plugInOnce<T>({ doInit, onDuplicate, pid, }: PlugInOnceOptions<T>): Promise<PlugInOnceResult<T>>;
54
85
  /**
55
86
  * Test-only: clear the singleton state so the next invocation re-runs
56
87
  * init. Must not be called in production code paths.
@@ -1,9 +1,7 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
- import type { TuiPluginApi } from '@opencode-ai/plugin/tui';
3
2
  import type { RpcClient } from '../rpc-client';
4
3
  import type { ModalListTask } from './row';
5
4
  type ConfirmCardProps = {
6
- Dialog: TuiPluginApi['ui']['Dialog'];
7
5
  task: ModalListTask;
8
6
  rpc: Pick<RpcClient, 'tasksCancel'>;
9
7
  onConfirm: () => void;
@@ -1,17 +1,14 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
- import type { TuiPluginApi } from '@opencode-ai/plugin/tui';
3
2
  import type { RpcClient } from '../rpc-client';
4
- import { type ModalListTask } from './row';
5
3
  export type ModalListClock = {
6
4
  now(): number;
7
5
  setInterval(fn: () => void, delayMs: number): unknown;
8
6
  clearInterval(handle: unknown): void;
9
7
  };
10
8
  type ModalListProps = {
11
- Dialog: TuiPluginApi['ui']['Dialog'];
12
9
  rpc: Pick<RpcClient, 'tasksList'>;
13
10
  onClose: () => void;
14
- onCancelTask?: (task: ModalListTask) => void;
11
+ onLoadError?: (error: unknown) => void;
15
12
  initialTaskId?: string;
16
13
  clock?: ModalListClock;
17
14
  };