@pal-code/core 0.0.13 → 0.0.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pal-code/core",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "Core runtime for pal — agent loop, providers, tools, sessions",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
package/src/extensions.ts CHANGED
@@ -34,6 +34,7 @@ import {realpathSync} from 'node:fs';
34
34
  import {homedir} from 'node:os';
35
35
  import {isAbsolute, join, relative} from 'node:path';
36
36
  import type {ToolDef} from './tools/definitions.js';
37
+ import {clearExtensionTools} from './tools/definitions.js';
37
38
  import type {ToolConfig} from './config.js';
38
39
  import type {LifecycleHandlers} from './lifecycle.js';
39
40
  import {clearRoleProvider} from './agent/roles.js';
@@ -205,6 +206,9 @@ export async function loadExtensions(
205
206
  // Clear any previously-registered role provider *before* (re)loading. If the
206
207
  // extension that provides it is gone or fails, its roles must not linger.
207
208
  clearRoleProvider();
209
+ // Same for extension tools: clear previously-registered ones *before*
210
+ // (re)loading, so a removed extension or MCP server's tools don't linger on /reload.
211
+ clearExtensionTools();
208
212
 
209
213
  for (const pkg of extensionPackages) {
210
214
  try {
package/src/index.ts CHANGED
@@ -108,6 +108,7 @@ export {
108
108
  CORE_TOOLS,
109
109
  getAllTools,
110
110
  registerExtensionTools,
111
+ clearExtensionTools,
111
112
  getExtensionTools,
112
113
  } from './tools/definitions.js';
113
114
  export type {ToolDef} from './tools/definitions.js';
package/src/lifecycle.ts CHANGED
@@ -71,7 +71,9 @@ export type LifecycleHandlers = {
71
71
  /**
72
72
  * Replace the registered lifecycle hooks. Called when extensions load and on
73
73
  * `/reload` — replace rather than merge so a removed extension's hooks neither
74
- * linger nor duplicate (same semantics as `registerExtensionTools`).
74
+ * linger nor duplicate. (Unlike `registerExtensionTools`, which merges — tools
75
+ * may register themselves from inside an extension's `setup()` before the load
76
+ * list arrives.)
75
77
  */
76
78
  export function registerExtensionLifecycle(hooks: readonly LifecycleHandlers[]): void {
77
79
  registry.lifecycleHooks = [...hooks];
@@ -22,10 +22,36 @@ export interface ToolDef {
22
22
  * so that `delegate` can hand the *full* toolset to child agents. Without this
23
23
  * a role like `librarian`, whose tools are all extension-provided, would spawn
24
24
  * a child with no usable tools at all.
25
+ *
26
+ * Two registration paths feed this: extensions like @pal-code/mcp register
27
+ * discovered tools from inside their `setup()` (packages/mcp/src/index.ts:58),
28
+ * while the rest arrive via `loadExtensions`' returned list registered by the
29
+ * CLI (packages/cli/src/turn.ts:58).
30
+ */
31
+ /**
32
+ * Merge the registered extension tools by `function.name` — a later
33
+ * registration of the same name wins (upsert). Called on load and on `/reload`.
34
+ *
35
+ * Merge, not replace: replacing wiped whichever path registered first — e.g.
36
+ * turn.ts's register of the load list erased the 34 MCP tools mcp's `setup()`
37
+ * had already registered ("34 MCP tools wiped to 4"). Removal of reload-stale
38
+ * tools is `clearExtensionTools()`'s job, called at load start.
39
+ * Non-array input still resets the registry (defensive).
25
40
  */
26
- /** Replace the registered extension tools. Called on load and on `/reload`. */
27
41
  export function registerExtensionTools(tools: ToolDef[]): void {
28
- registry.extensionTools = Array.isArray(tools) ? [...tools] : [];
42
+ if (!Array.isArray(tools)) {
43
+ registry.extensionTools = [];
44
+ return;
45
+ }
46
+ // O(n): one Map keyed on function.name; existing entries keep their order.
47
+ const byName = new Map(registry.extensionTools.map((t) => [t.function.name, t]));
48
+ for (const tool of tools) byName.set(tool.function.name, tool);
49
+ registry.extensionTools = [...byName.values()];
50
+ }
51
+
52
+ /** Drop every registered extension tool — called before (re)loading extensions. */
53
+ export function clearExtensionTools(): void {
54
+ registry.extensionTools = [];
29
55
  }
30
56
 
31
57
  /** The extension tools currently registered (read-only view). */