@namzu/sdk 0.4.2 → 0.4.4
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 +46 -0
- package/dist/advisory/context.test.d.ts +16 -0
- package/dist/advisory/context.test.d.ts.map +1 -0
- package/dist/advisory/context.test.js +92 -0
- package/dist/advisory/context.test.js.map +1 -0
- package/dist/advisory/evaluator.test.d.ts +34 -0
- package/dist/advisory/evaluator.test.d.ts.map +1 -0
- package/dist/advisory/evaluator.test.js +172 -0
- package/dist/advisory/evaluator.test.js.map +1 -0
- package/dist/advisory/executor.test.d.ts +35 -0
- package/dist/advisory/executor.test.d.ts.map +1 -0
- package/dist/advisory/executor.test.js +233 -0
- package/dist/advisory/executor.test.js.map +1 -0
- package/dist/advisory/registry.test.d.ts +16 -0
- package/dist/advisory/registry.test.d.ts.map +1 -0
- package/dist/advisory/registry.test.js +62 -0
- package/dist/advisory/registry.test.js.map +1 -0
- package/dist/bridge/a2a/agent-card.test.d.ts +24 -0
- package/dist/bridge/a2a/agent-card.test.d.ts.map +1 -0
- package/dist/bridge/a2a/agent-card.test.js +118 -0
- package/dist/bridge/a2a/agent-card.test.js.map +1 -0
- package/dist/bridge/a2a/mapper.test.d.ts +29 -0
- package/dist/bridge/a2a/mapper.test.d.ts.map +1 -0
- package/dist/bridge/a2a/mapper.test.js +265 -0
- package/dist/bridge/a2a/mapper.test.js.map +1 -0
- package/dist/bridge/a2a/message.test.d.ts +20 -0
- package/dist/bridge/a2a/message.test.d.ts.map +1 -0
- package/dist/bridge/a2a/message.test.js +116 -0
- package/dist/bridge/a2a/message.test.js.map +1 -0
- package/dist/bridge/a2a/task.test.d.ts +29 -0
- package/dist/bridge/a2a/task.test.d.ts.map +1 -0
- package/dist/bridge/a2a/task.test.js +198 -0
- package/dist/bridge/a2a/task.test.js.map +1 -0
- package/dist/bridge/mcp/connector/adapter.test.d.ts +27 -0
- package/dist/bridge/mcp/connector/adapter.test.d.ts.map +1 -0
- package/dist/bridge/mcp/connector/adapter.test.js +203 -0
- package/dist/bridge/mcp/connector/adapter.test.js.map +1 -0
- package/dist/bridge/sse/mapper.test.d.ts +27 -0
- package/dist/bridge/sse/mapper.test.d.ts.map +1 -0
- package/dist/bridge/sse/mapper.test.js +271 -0
- package/dist/bridge/sse/mapper.test.js.map +1 -0
- package/dist/bridge/tools/connector/adapter.d.ts +2 -2
- package/dist/bridge/tools/connector/adapter.test.d.ts +28 -0
- package/dist/bridge/tools/connector/adapter.test.d.ts.map +1 -0
- package/dist/bridge/tools/connector/adapter.test.js +182 -0
- package/dist/bridge/tools/connector/adapter.test.js.map +1 -0
- package/dist/bridge/tools/connector/definitions.test.d.ts +23 -0
- package/dist/bridge/tools/connector/definitions.test.d.ts.map +1 -0
- package/dist/bridge/tools/connector/definitions.test.js +158 -0
- package/dist/bridge/tools/connector/definitions.test.js.map +1 -0
- package/dist/bridge/tools/connector/router.test.d.ts +21 -0
- package/dist/bridge/tools/connector/router.test.d.ts.map +1 -0
- package/dist/bridge/tools/connector/router.test.js +139 -0
- package/dist/bridge/tools/connector/router.test.js.map +1 -0
- package/dist/bus/breaker.test.d.ts +41 -0
- package/dist/bus/breaker.test.d.ts.map +1 -0
- package/dist/bus/breaker.test.js +242 -0
- package/dist/bus/breaker.test.js.map +1 -0
- package/dist/bus/index.d.ts +3 -1
- package/dist/bus/index.d.ts.map +1 -1
- package/dist/bus/index.js +18 -11
- package/dist/bus/index.js.map +1 -1
- package/dist/bus/index.test.d.ts +25 -0
- package/dist/bus/index.test.d.ts.map +1 -0
- package/dist/bus/index.test.js +151 -0
- package/dist/bus/index.test.js.map +1 -0
- package/dist/bus/lock.test.d.ts +44 -0
- package/dist/bus/lock.test.d.ts.map +1 -0
- package/dist/bus/lock.test.js +226 -0
- package/dist/bus/lock.test.js.map +1 -0
- package/dist/bus/ownership.test.d.ts +26 -0
- package/dist/bus/ownership.test.d.ts.map +1 -0
- package/dist/bus/ownership.test.js +205 -0
- package/dist/bus/ownership.test.js.map +1 -0
- package/dist/config/runtime.d.ts +28 -28
- package/dist/connector/BaseConnector.test.d.ts +21 -0
- package/dist/connector/BaseConnector.test.d.ts.map +1 -0
- package/dist/connector/BaseConnector.test.js +108 -0
- package/dist/connector/BaseConnector.test.js.map +1 -0
- package/dist/connector/builtins/http.test.d.ts +30 -0
- package/dist/connector/builtins/http.test.d.ts.map +1 -0
- package/dist/connector/builtins/http.test.js +232 -0
- package/dist/connector/builtins/http.test.js.map +1 -0
- package/dist/connector/builtins/webhook.test.d.ts +20 -0
- package/dist/connector/builtins/webhook.test.d.ts.map +1 -0
- package/dist/connector/builtins/webhook.test.js +113 -0
- package/dist/connector/builtins/webhook.test.js.map +1 -0
- package/dist/connector/execution/factory.test.d.ts +16 -0
- package/dist/connector/execution/factory.test.d.ts.map +1 -0
- package/dist/connector/execution/factory.test.js +64 -0
- package/dist/connector/execution/factory.test.js.map +1 -0
- package/dist/connector/execution/remote.test.d.ts +16 -0
- package/dist/connector/execution/remote.test.d.ts.map +1 -0
- package/dist/connector/execution/remote.test.js +53 -0
- package/dist/connector/execution/remote.test.js.map +1 -0
- package/dist/connector/mcp/adapter.test.d.ts +34 -0
- package/dist/connector/mcp/adapter.test.d.ts.map +1 -0
- package/dist/connector/mcp/adapter.test.js +199 -0
- package/dist/connector/mcp/adapter.test.js.map +1 -0
- package/dist/probe/context.d.ts +8 -0
- package/dist/probe/context.d.ts.map +1 -0
- package/dist/probe/context.js +7 -0
- package/dist/probe/context.js.map +1 -0
- package/dist/probe/errors.d.ts +12 -0
- package/dist/probe/errors.d.ts.map +1 -0
- package/dist/probe/errors.js +21 -0
- package/dist/probe/errors.js.map +1 -0
- package/dist/probe/index.d.ts +5 -0
- package/dist/probe/index.d.ts.map +1 -0
- package/dist/probe/index.js +4 -0
- package/dist/probe/index.js.map +1 -0
- package/dist/probe/registry.d.ts +24 -0
- package/dist/probe/registry.d.ts.map +1 -0
- package/dist/probe/registry.js +228 -0
- package/dist/probe/registry.js.map +1 -0
- package/dist/probe/registry.test.d.ts +7 -0
- package/dist/probe/registry.test.d.ts.map +1 -0
- package/dist/probe/registry.test.js +310 -0
- package/dist/probe/registry.test.js.map +1 -0
- package/dist/provider/instrumentation.d.ts +9 -0
- package/dist/provider/instrumentation.d.ts.map +1 -0
- package/dist/provider/instrumentation.js +104 -0
- package/dist/provider/instrumentation.js.map +1 -0
- package/dist/provider/instrumentation.test.d.ts +2 -0
- package/dist/provider/instrumentation.test.d.ts.map +1 -0
- package/dist/provider/instrumentation.test.js +152 -0
- package/dist/provider/instrumentation.test.js.map +1 -0
- package/dist/public-runtime.d.ts +5 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +4 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +3 -0
- package/dist/public-types.d.ts.map +1 -1
- package/dist/rag/chunking.test.d.ts +20 -0
- package/dist/rag/chunking.test.d.ts.map +1 -0
- package/dist/rag/chunking.test.js +92 -0
- package/dist/rag/chunking.test.js.map +1 -0
- package/dist/rag/context-assembler.test.d.ts +19 -0
- package/dist/rag/context-assembler.test.d.ts.map +1 -0
- package/dist/rag/context-assembler.test.js +98 -0
- package/dist/rag/context-assembler.test.js.map +1 -0
- package/dist/rag/embedding.test.d.ts +19 -0
- package/dist/rag/embedding.test.d.ts.map +1 -0
- package/dist/rag/embedding.test.js +115 -0
- package/dist/rag/embedding.test.js.map +1 -0
- package/dist/rag/ingestion.test.d.ts +22 -0
- package/dist/rag/ingestion.test.d.ts.map +1 -0
- package/dist/rag/ingestion.test.js +99 -0
- package/dist/rag/ingestion.test.js.map +1 -0
- package/dist/rag/knowledge-base.test.d.ts +17 -0
- package/dist/rag/knowledge-base.test.d.ts.map +1 -0
- package/dist/rag/knowledge-base.test.js +77 -0
- package/dist/rag/knowledge-base.test.js.map +1 -0
- package/dist/rag/rag-tool.test.d.ts +21 -0
- package/dist/rag/rag-tool.test.d.ts.map +1 -0
- package/dist/rag/rag-tool.test.js +149 -0
- package/dist/rag/rag-tool.test.js.map +1 -0
- package/dist/rag/retriever.test.d.ts +26 -0
- package/dist/rag/retriever.test.d.ts.map +1 -0
- package/dist/rag/retriever.test.js +180 -0
- package/dist/rag/retriever.test.js.map +1 -0
- package/dist/rag/vector-store.test.d.ts +38 -0
- package/dist/rag/vector-store.test.d.ts.map +1 -0
- package/dist/rag/vector-store.test.js +175 -0
- package/dist/rag/vector-store.test.js.map +1 -0
- package/dist/registry/ManagedRegistry.test.d.ts +21 -0
- package/dist/registry/ManagedRegistry.test.d.ts.map +1 -0
- package/dist/registry/ManagedRegistry.test.js +98 -0
- package/dist/registry/ManagedRegistry.test.js.map +1 -0
- package/dist/registry/Registry.test.d.ts +18 -0
- package/dist/registry/Registry.test.d.ts.map +1 -0
- package/dist/registry/Registry.test.js +79 -0
- package/dist/registry/Registry.test.js.map +1 -0
- package/dist/registry/agent/definitions.test.d.ts +15 -0
- package/dist/registry/agent/definitions.test.d.ts.map +1 -0
- package/dist/registry/agent/definitions.test.js +84 -0
- package/dist/registry/agent/definitions.test.js.map +1 -0
- package/dist/registry/connector/definitions.test.d.ts +13 -0
- package/dist/registry/connector/definitions.test.d.ts.map +1 -0
- package/dist/registry/connector/definitions.test.js +41 -0
- package/dist/registry/connector/definitions.test.js.map +1 -0
- package/dist/registry/connector/scoped.test.d.ts +21 -0
- package/dist/registry/connector/scoped.test.d.ts.map +1 -0
- package/dist/registry/connector/scoped.test.js +115 -0
- package/dist/registry/connector/scoped.test.js.map +1 -0
- package/dist/registry/plugin/index.test.d.ts +12 -0
- package/dist/registry/plugin/index.test.d.ts.map +1 -0
- package/dist/registry/plugin/index.test.js +69 -0
- package/dist/registry/plugin/index.test.js.map +1 -0
- package/dist/registry/tool/execute.test.d.ts +42 -0
- package/dist/registry/tool/execute.test.d.ts.map +1 -0
- package/dist/registry/tool/execute.test.js +281 -0
- package/dist/registry/tool/execute.test.js.map +1 -0
- package/dist/runtime/query/events.d.ts +3 -1
- package/dist/runtime/query/events.d.ts.map +1 -1
- package/dist/runtime/query/events.js +6 -1
- package/dist/runtime/query/events.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +3 -1
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +30 -1
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/iteration/phases/advisory.test.d.ts +42 -0
- package/dist/runtime/query/iteration/phases/advisory.test.d.ts.map +1 -0
- package/dist/runtime/query/iteration/phases/advisory.test.js +334 -0
- package/dist/runtime/query/iteration/phases/advisory.test.js.map +1 -0
- package/dist/test-setup.d.ts +22 -0
- package/dist/test-setup.d.ts.map +1 -0
- package/dist/test-setup.js +23 -0
- package/dist/test-setup.js.map +1 -0
- package/dist/types/bus/index.d.ts +46 -2
- package/dist/types/bus/index.d.ts.map +1 -1
- package/dist/types/doctor/check.d.ts +41 -0
- package/dist/types/doctor/check.d.ts.map +1 -0
- package/dist/types/doctor/check.js +2 -0
- package/dist/types/doctor/check.js.map +1 -0
- package/dist/types/doctor/index.d.ts +2 -0
- package/dist/types/doctor/index.d.ts.map +1 -0
- package/dist/types/doctor/index.js +2 -0
- package/dist/types/doctor/index.js.map +1 -0
- package/dist/types/probe/event-kind.d.ts +6 -0
- package/dist/types/probe/event-kind.d.ts.map +1 -0
- package/dist/types/probe/event-kind.js +2 -0
- package/dist/types/probe/event-kind.js.map +1 -0
- package/dist/types/probe/event-of.d.ts +5 -0
- package/dist/types/probe/event-of.d.ts.map +1 -0
- package/dist/types/probe/event-of.js +2 -0
- package/dist/types/probe/event-of.js.map +1 -0
- package/dist/types/probe/index.d.ts +4 -0
- package/dist/types/probe/index.d.ts.map +1 -0
- package/dist/types/probe/index.js +2 -0
- package/dist/types/probe/index.js.map +1 -0
- package/dist/types/probe/registry.d.ts +27 -0
- package/dist/types/probe/registry.d.ts.map +1 -0
- package/dist/types/probe/registry.js +2 -0
- package/dist/types/probe/registry.js.map +1 -0
- package/dist/utils/logger.d.ts +1 -1
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +5 -0
- package/dist/utils/logger.js.map +1 -1
- package/dist/vault/instrumentation.d.ts +11 -0
- package/dist/vault/instrumentation.d.ts.map +1 -0
- package/dist/vault/instrumentation.js +32 -0
- package/dist/vault/instrumentation.js.map +1 -0
- package/dist/vault/instrumentation.test.d.ts +2 -0
- package/dist/vault/instrumentation.test.d.ts.map +1 -0
- package/dist/vault/instrumentation.test.js +80 -0
- package/dist/vault/instrumentation.test.js.map +1 -0
- package/package.json +4 -1
- package/src/advisory/context.test.ts +109 -0
- package/src/advisory/evaluator.test.ts +192 -0
- package/src/advisory/executor.test.ts +272 -0
- package/src/advisory/registry.test.ts +75 -0
- package/src/bridge/a2a/agent-card.test.ts +140 -0
- package/src/bridge/a2a/mapper.test.ts +293 -0
- package/src/bridge/a2a/message.test.ts +138 -0
- package/src/bridge/a2a/task.test.ts +235 -0
- package/src/bridge/mcp/connector/adapter.test.ts +230 -0
- package/src/bridge/sse/mapper.test.ts +422 -0
- package/src/bridge/tools/connector/adapter.test.ts +224 -0
- package/src/bridge/tools/connector/definitions.test.ts +183 -0
- package/src/bridge/tools/connector/router.test.ts +159 -0
- package/src/bus/breaker.test.ts +274 -0
- package/src/bus/index.test.ts +183 -0
- package/src/bus/index.ts +21 -10
- package/src/bus/lock.test.ts +265 -0
- package/src/bus/ownership.test.ts +243 -0
- package/src/connector/BaseConnector.test.ts +130 -0
- package/src/connector/builtins/http.test.ts +290 -0
- package/src/connector/builtins/webhook.test.ts +138 -0
- package/src/connector/execution/factory.test.ts +75 -0
- package/src/connector/execution/remote.test.ts +63 -0
- package/src/connector/mcp/adapter.test.ts +249 -0
- package/src/probe/context.ts +14 -0
- package/src/probe/errors.ts +27 -0
- package/src/probe/index.ts +4 -0
- package/src/probe/registry.test.ts +480 -0
- package/src/probe/registry.ts +276 -0
- package/src/provider/instrumentation.test.ts +192 -0
- package/src/provider/instrumentation.ts +139 -0
- package/src/public-runtime.ts +17 -0
- package/src/public-types.ts +3 -0
- package/src/rag/chunking.test.ts +107 -0
- package/src/rag/context-assembler.test.ts +114 -0
- package/src/rag/embedding.test.ts +130 -0
- package/src/rag/ingestion.test.ts +114 -0
- package/src/rag/knowledge-base.test.ts +106 -0
- package/src/rag/rag-tool.test.ts +167 -0
- package/src/rag/retriever.test.ts +210 -0
- package/src/rag/vector-store.test.ts +196 -0
- package/src/registry/ManagedRegistry.test.ts +118 -0
- package/src/registry/Registry.test.ts +91 -0
- package/src/registry/agent/definitions.test.ts +100 -0
- package/src/registry/connector/definitions.test.ts +51 -0
- package/src/registry/connector/scoped.test.ts +129 -0
- package/src/registry/plugin/index.test.ts +85 -0
- package/src/registry/tool/execute.test.ts +330 -0
- package/src/runtime/query/events.ts +6 -1
- package/src/runtime/query/executor.ts +34 -0
- package/src/runtime/query/iteration/phases/advisory.test.ts +412 -0
- package/src/test-setup.ts +24 -0
- package/src/types/bus/index.ts +54 -2
- package/src/types/doctor/check.ts +53 -0
- package/src/types/doctor/index.ts +9 -0
- package/src/types/probe/event-kind.ts +8 -0
- package/src/types/probe/event-of.ts +3 -0
- package/src/types/probe/index.ts +11 -0
- package/src/types/probe/registry.ts +36 -0
- package/src/utils/logger.ts +6 -1
- package/src/vault/instrumentation.test.ts +98 -0
- package/src/vault/instrumentation.ts +56 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Current-code invariants asserted (2026-04-21, ses_006 Phase 3):
|
|
3
|
+
*
|
|
4
|
+
* - `ToolRegistry` extends `ManagedRegistry<ToolDefinition>` with
|
|
5
|
+
* `idField: 'name'` and an additional `availability` map keyed by
|
|
6
|
+
* tool name.
|
|
7
|
+
* - `register(tool)` defaults availability to 'active'; the array
|
|
8
|
+
* overload uses the second arg (if it's a string) as the initial
|
|
9
|
+
* state for all tools.
|
|
10
|
+
* - Tier validation: if `tool.tier` is set AND `tierConfig` is
|
|
11
|
+
* configured, the tier id must be in `tierConfig.tiers`; otherwise
|
|
12
|
+
* register throws. Tools without a tier (even with tierConfig
|
|
13
|
+
* configured) are always accepted.
|
|
14
|
+
* - `unregister` + `clear` drop both the Registry entry and the
|
|
15
|
+
* availability entry.
|
|
16
|
+
* - `activate` / `defer` take a list of names; each throws (via
|
|
17
|
+
* `getOrThrow`) if the name is unknown, then toggles availability.
|
|
18
|
+
* - `suspendAll` flips every `active` tool to `suspended`;
|
|
19
|
+
* `deferred` / `suspended` tools are left alone. `hasSuspended`
|
|
20
|
+
* reports true iff at least one tool is suspended.
|
|
21
|
+
* - `getAvailability(name)` returns 'active' as a default even for
|
|
22
|
+
* unknown names (this is non-obvious but is the current behavior).
|
|
23
|
+
* - `searchDeferred(q)` is a case-insensitive filter against name OR
|
|
24
|
+
* description of every DEFERRED tool.
|
|
25
|
+
* - `assignTiers(mapping)` mutates `tool.tier` on existing tools;
|
|
26
|
+
* throws via `getOrThrow` on unknown name; throws if the tier id
|
|
27
|
+
* is not in `tierConfig.tiers`.
|
|
28
|
+
* - `toTierGuidance` returns null without a guidanceTemplate; calls
|
|
29
|
+
* the template with every defined tier otherwise.
|
|
30
|
+
* - `toPromptSection`: returns '' when no active or deferred tools;
|
|
31
|
+
* otherwise produces `<available_tools>` + `<deferred_tools>`
|
|
32
|
+
* fragments.
|
|
33
|
+
* - `toLLMTools`: converts active + suspended tools (filtered by
|
|
34
|
+
* `toolNames` if provided) into LLM tool schemas via
|
|
35
|
+
* `zodToJsonSchema`. When `tierConfig.labelInDescription` is true,
|
|
36
|
+
* the description is prefixed with the tier label.
|
|
37
|
+
* - `execute`: validates availability → permissionMode plan gate →
|
|
38
|
+
* zod.safeParse → calls tool.execute inside an OTEL span; wraps
|
|
39
|
+
* thrown errors as `{success:false, error}`.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
43
|
+
import { z } from 'zod'
|
|
44
|
+
|
|
45
|
+
import type { RunId } from '../../types/ids/index.js'
|
|
46
|
+
import type { ToolContext, ToolDefinition, ToolTierConfig } from '../../types/tool/index.js'
|
|
47
|
+
|
|
48
|
+
import { ToolRegistry } from './execute.js'
|
|
49
|
+
|
|
50
|
+
function makeTool(name: string, overrides: Partial<ToolDefinition> = {}): ToolDefinition {
|
|
51
|
+
return {
|
|
52
|
+
name,
|
|
53
|
+
description: `${name} tool`,
|
|
54
|
+
inputSchema: z.object({ k: z.string().optional() }),
|
|
55
|
+
async execute() {
|
|
56
|
+
return { success: true, output: `${name}-ran` }
|
|
57
|
+
},
|
|
58
|
+
...overrides,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function makeContext(overrides: Partial<ToolContext> = {}): ToolContext {
|
|
63
|
+
return {
|
|
64
|
+
runId: 'run_1' as RunId,
|
|
65
|
+
workingDirectory: '/tmp',
|
|
66
|
+
abortSignal: new AbortController().signal,
|
|
67
|
+
env: {},
|
|
68
|
+
log: () => {},
|
|
69
|
+
...overrides,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe('ToolRegistry — register + availability', () => {
|
|
74
|
+
it('single-tool register: default availability is active', () => {
|
|
75
|
+
const r = new ToolRegistry()
|
|
76
|
+
r.register(makeTool('read'))
|
|
77
|
+
expect(r.getAvailability('read')).toBe('active')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('array-register: second arg sets availability for all', () => {
|
|
81
|
+
const r = new ToolRegistry()
|
|
82
|
+
r.register([makeTool('a'), makeTool('b')], 'deferred')
|
|
83
|
+
expect(r.getAvailability('a')).toBe('deferred')
|
|
84
|
+
expect(r.getAvailability('b')).toBe('deferred')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('getAvailability returns active for unknown names (current default)', () => {
|
|
88
|
+
const r = new ToolRegistry()
|
|
89
|
+
expect(r.getAvailability('never-registered')).toBe('active')
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('unregister drops both the tool + the availability entry', () => {
|
|
93
|
+
const r = new ToolRegistry()
|
|
94
|
+
r.register(makeTool('a'))
|
|
95
|
+
expect(r.unregister('a')).toBe(true)
|
|
96
|
+
expect(r.get('a')).toBeUndefined()
|
|
97
|
+
expect(r.listIds()).toEqual([])
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('clear empties both maps', () => {
|
|
101
|
+
const r = new ToolRegistry()
|
|
102
|
+
r.register(makeTool('a'))
|
|
103
|
+
r.clear()
|
|
104
|
+
expect(r.listIds()).toEqual([])
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
describe('ToolRegistry — tier validation', () => {
|
|
109
|
+
const tierConfig: ToolTierConfig = {
|
|
110
|
+
tiers: [
|
|
111
|
+
{ id: 'safe', label: 'Safe', priority: 1 },
|
|
112
|
+
{ id: 'danger', label: 'Danger', priority: 10 },
|
|
113
|
+
],
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
it('accepts tools without a tier even when tierConfig is configured', () => {
|
|
117
|
+
const r = new ToolRegistry({ tierConfig })
|
|
118
|
+
expect(() => r.register(makeTool('a'))).not.toThrow()
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('accepts tools whose tier is in the config', () => {
|
|
122
|
+
const r = new ToolRegistry({ tierConfig })
|
|
123
|
+
expect(() => r.register(makeTool('a', { tier: 'safe' }))).not.toThrow()
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('throws when tier id is unknown', () => {
|
|
127
|
+
const r = new ToolRegistry({ tierConfig })
|
|
128
|
+
expect(() => r.register(makeTool('a', { tier: 'bogus' }))).toThrow(/not defined/)
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
describe('ToolRegistry — activate / defer / suspend', () => {
|
|
133
|
+
it('activate + defer throw via getOrThrow on unknown name', () => {
|
|
134
|
+
const r = new ToolRegistry()
|
|
135
|
+
expect(() => r.activate(['unknown'])).toThrow(/Not found/)
|
|
136
|
+
expect(() => r.defer(['unknown'])).toThrow(/Not found/)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('activate sets state to active', () => {
|
|
140
|
+
const r = new ToolRegistry()
|
|
141
|
+
r.register([makeTool('a')], 'deferred')
|
|
142
|
+
r.activate(['a'])
|
|
143
|
+
expect(r.getAvailability('a')).toBe('active')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('defer sets state to deferred', () => {
|
|
147
|
+
const r = new ToolRegistry()
|
|
148
|
+
r.register(makeTool('a'))
|
|
149
|
+
r.defer(['a'])
|
|
150
|
+
expect(r.getAvailability('a')).toBe('deferred')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('suspendAll flips active → suspended; leaves deferred / suspended alone', () => {
|
|
154
|
+
const r = new ToolRegistry()
|
|
155
|
+
r.register(makeTool('a'))
|
|
156
|
+
r.register(makeTool('b'))
|
|
157
|
+
r.register([makeTool('c')], 'deferred')
|
|
158
|
+
r.suspendAll()
|
|
159
|
+
expect(r.getAvailability('a')).toBe('suspended')
|
|
160
|
+
expect(r.getAvailability('b')).toBe('suspended')
|
|
161
|
+
expect(r.getAvailability('c')).toBe('deferred')
|
|
162
|
+
expect(r.hasSuspended()).toBe(true)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('hasSuspended returns false when none are suspended', () => {
|
|
166
|
+
const r = new ToolRegistry()
|
|
167
|
+
r.register(makeTool('a'))
|
|
168
|
+
expect(r.hasSuspended()).toBe(false)
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
describe('ToolRegistry — searchDeferred', () => {
|
|
173
|
+
it('filters only deferred tools by name OR description (case-insensitive)', () => {
|
|
174
|
+
const r = new ToolRegistry()
|
|
175
|
+
r.register([makeTool('alpha', { description: 'does alpha' })], 'deferred')
|
|
176
|
+
r.register([makeTool('beta', { description: 'does BETA' })], 'deferred')
|
|
177
|
+
r.register(makeTool('gamma'))
|
|
178
|
+
expect(r.searchDeferred('alpha').map((t) => t.name)).toEqual(['alpha'])
|
|
179
|
+
expect(r.searchDeferred('beta').map((t) => t.name)).toEqual(['beta'])
|
|
180
|
+
expect(r.searchDeferred('does').map((t) => t.name)).toEqual(['alpha', 'beta'])
|
|
181
|
+
expect(r.searchDeferred('gamma')).toEqual([])
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('ToolRegistry — tier mutation + guidance', () => {
|
|
186
|
+
const tierConfig: ToolTierConfig = {
|
|
187
|
+
tiers: [
|
|
188
|
+
{ id: 'safe', label: 'Safe', priority: 1 },
|
|
189
|
+
{ id: 'danger', label: 'Danger', priority: 10 },
|
|
190
|
+
],
|
|
191
|
+
guidanceTemplate: (tiers) => `tiers: ${tiers.map((t) => t.id).join(',')}`,
|
|
192
|
+
labelInDescription: true,
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
it('assignTiers sets the tier on each mapped tool', () => {
|
|
196
|
+
const r = new ToolRegistry({ tierConfig })
|
|
197
|
+
r.register(makeTool('a'))
|
|
198
|
+
r.assignTiers({ a: 'safe' })
|
|
199
|
+
expect(r.get('a')?.tier).toBe('safe')
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('assignTiers throws when the tier is unknown', () => {
|
|
203
|
+
const r = new ToolRegistry({ tierConfig })
|
|
204
|
+
r.register(makeTool('a'))
|
|
205
|
+
expect(() => r.assignTiers({ a: 'bogus' })).toThrow(/not defined/)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('toTierGuidance returns null without a template', () => {
|
|
209
|
+
const r = new ToolRegistry()
|
|
210
|
+
expect(r.toTierGuidance()).toBeNull()
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('toTierGuidance renders via the template', () => {
|
|
214
|
+
const r = new ToolRegistry({ tierConfig })
|
|
215
|
+
expect(r.toTierGuidance()).toBe('tiers: safe,danger')
|
|
216
|
+
})
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
describe('ToolRegistry — toPromptSection + toLLMTools', () => {
|
|
220
|
+
it('toPromptSection returns empty when the registry is empty', () => {
|
|
221
|
+
const r = new ToolRegistry()
|
|
222
|
+
expect(r.toPromptSection()).toBe('')
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('toPromptSection renders available + deferred fragments', () => {
|
|
226
|
+
const r = new ToolRegistry()
|
|
227
|
+
r.register(makeTool('a'))
|
|
228
|
+
r.register([makeTool('b')], 'deferred')
|
|
229
|
+
const s = r.toPromptSection()
|
|
230
|
+
expect(s).toContain('<available_tools>')
|
|
231
|
+
expect(s).toContain('- a: a tool')
|
|
232
|
+
expect(s).toContain('<deferred_tools>')
|
|
233
|
+
expect(s).toContain('- b')
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('toLLMTools: converts active + suspended tools', () => {
|
|
237
|
+
const r = new ToolRegistry()
|
|
238
|
+
r.register(makeTool('a'))
|
|
239
|
+
r.register([makeTool('b')], 'deferred')
|
|
240
|
+
r.register([makeTool('c')], 'suspended')
|
|
241
|
+
|
|
242
|
+
const schemas = r.toLLMTools()
|
|
243
|
+
const names = schemas.map((t) => t.function.name).sort()
|
|
244
|
+
expect(names).toEqual(['a', 'c'])
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
it('toLLMTools: prefixes description with tier label when labelInDescription is true', () => {
|
|
248
|
+
const tierConfig: ToolTierConfig = {
|
|
249
|
+
tiers: [{ id: 'safe', label: 'Safe', priority: 1 }],
|
|
250
|
+
labelInDescription: true,
|
|
251
|
+
}
|
|
252
|
+
const r = new ToolRegistry({ tierConfig })
|
|
253
|
+
r.register(makeTool('a', { tier: 'safe' }))
|
|
254
|
+
const schemas = r.toLLMTools()
|
|
255
|
+
expect(schemas[0]?.function.description).toBe('[Safe] a tool')
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
describe('ToolRegistry — execute', () => {
|
|
260
|
+
it('returns error when tool is not active (e.g. deferred)', async () => {
|
|
261
|
+
const r = new ToolRegistry()
|
|
262
|
+
r.register([makeTool('a')], 'deferred')
|
|
263
|
+
const result = await r.execute('a', {}, makeContext())
|
|
264
|
+
expect(result.success).toBe(false)
|
|
265
|
+
expect(result.error).toMatch(/deferred and cannot be executed/)
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('blocks non-read-only tools in plan mode', async () => {
|
|
269
|
+
const r = new ToolRegistry()
|
|
270
|
+
r.register(makeTool('write', { isReadOnly: () => false }))
|
|
271
|
+
const result = await r.execute(
|
|
272
|
+
'write',
|
|
273
|
+
{},
|
|
274
|
+
makeContext({
|
|
275
|
+
permissionContext: { mode: 'plan', runId: 'run_1', workingDirectory: '/tmp' },
|
|
276
|
+
}),
|
|
277
|
+
)
|
|
278
|
+
expect(result.success).toBe(false)
|
|
279
|
+
expect(result.permissionDenied).toBe(true)
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
it('allows read-only tools in plan mode', async () => {
|
|
283
|
+
const r = new ToolRegistry()
|
|
284
|
+
const execute = vi.fn(async () => ({ success: true, output: 'ok' }))
|
|
285
|
+
r.register(makeTool('read', { isReadOnly: () => true, execute }))
|
|
286
|
+
const result = await r.execute(
|
|
287
|
+
'read',
|
|
288
|
+
{},
|
|
289
|
+
makeContext({
|
|
290
|
+
permissionContext: { mode: 'plan', runId: 'run_1', workingDirectory: '/tmp' },
|
|
291
|
+
}),
|
|
292
|
+
)
|
|
293
|
+
expect(result.success).toBe(true)
|
|
294
|
+
expect(execute).toHaveBeenCalled()
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('returns error when input fails zod validation', async () => {
|
|
298
|
+
const r = new ToolRegistry()
|
|
299
|
+
r.register(
|
|
300
|
+
makeTool('strict', {
|
|
301
|
+
inputSchema: z.object({ required: z.string() }),
|
|
302
|
+
}),
|
|
303
|
+
)
|
|
304
|
+
const result = await r.execute('strict', { required: 123 }, makeContext())
|
|
305
|
+
expect(result.success).toBe(false)
|
|
306
|
+
expect(result.error).toMatch(/Invalid input/)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('wraps thrown errors in the execute function', async () => {
|
|
310
|
+
const r = new ToolRegistry()
|
|
311
|
+
r.register(
|
|
312
|
+
makeTool('bad', {
|
|
313
|
+
async execute() {
|
|
314
|
+
throw new Error('boom')
|
|
315
|
+
},
|
|
316
|
+
}),
|
|
317
|
+
)
|
|
318
|
+
const result = await r.execute('bad', {}, makeContext())
|
|
319
|
+
expect(result.success).toBe(false)
|
|
320
|
+
expect(result.error).toMatch(/execution failed: boom/)
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
it('returns the tool result on happy path', async () => {
|
|
324
|
+
const r = new ToolRegistry()
|
|
325
|
+
r.register(makeTool('good'))
|
|
326
|
+
const result = await r.execute('good', {}, makeContext())
|
|
327
|
+
expect(result.success).toBe(true)
|
|
328
|
+
expect(result.output).toBe('good-ran')
|
|
329
|
+
})
|
|
330
|
+
})
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { PlanEvent, PlanManager } from '../../manager/plan/lifecycle.js'
|
|
2
2
|
import type { RunPersistence } from '../../manager/run/persistence.js'
|
|
3
|
+
import { buildProbeContext } from '../../probe/context.js'
|
|
4
|
+
import { type ProbeRegistry, probe as defaultProbeRegistry } from '../../probe/registry.js'
|
|
3
5
|
import type { ActivityEvent, ActivityStore } from '../../store/activity/memory.js'
|
|
4
6
|
import type { RunId } from '../../types/ids/index.js'
|
|
5
7
|
import type { RunEvent } from '../../types/run/index.js'
|
|
@@ -10,12 +12,15 @@ export type EmitEvent = (event: RunEvent) => Promise<void>
|
|
|
10
12
|
export class EventTranslator {
|
|
11
13
|
private pendingEvents: RunEvent[] = []
|
|
12
14
|
private runMgr: RunPersistence
|
|
15
|
+
private probes: ProbeRegistry
|
|
13
16
|
|
|
14
|
-
constructor(runMgr: RunPersistence) {
|
|
17
|
+
constructor(runMgr: RunPersistence, probeRegistry: ProbeRegistry = defaultProbeRegistry) {
|
|
15
18
|
this.runMgr = runMgr
|
|
19
|
+
this.probes = probeRegistry
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
readonly emitEvent: EmitEvent = async (event: RunEvent): Promise<void> => {
|
|
23
|
+
this.probes.dispatch(event, buildProbeContext({ runId: event.runId }))
|
|
19
24
|
this.pendingEvents.push(event)
|
|
20
25
|
await this.runMgr.getRunStore().appendEvent(event)
|
|
21
26
|
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { extractFromToolCall, extractFromToolResult } from '../../compaction/extractor.js'
|
|
2
2
|
import type { WorkingStateManager } from '../../compaction/manager.js'
|
|
3
3
|
import type { PluginLifecycleManager } from '../../plugin/lifecycle.js'
|
|
4
|
+
import { buildProbeContext } from '../../probe/context.js'
|
|
5
|
+
import { ProbeVetoError } from '../../probe/errors.js'
|
|
6
|
+
import { type ProbeRegistry, probe as defaultProbeRegistry } from '../../probe/registry.js'
|
|
4
7
|
import type { ActivityStore } from '../../store/activity/memory.js'
|
|
5
8
|
import type { RunId } from '../../types/ids/index.js'
|
|
6
9
|
import type { InvocationState } from '../../types/invocation/index.js'
|
|
@@ -44,17 +47,20 @@ export class ToolExecutor {
|
|
|
44
47
|
private emitEvent: EmitEvent
|
|
45
48
|
private log: Logger
|
|
46
49
|
private workingStateManager?: WorkingStateManager
|
|
50
|
+
private probes: ProbeRegistry
|
|
47
51
|
|
|
48
52
|
constructor(
|
|
49
53
|
config: ToolExecutorConfig,
|
|
50
54
|
activityStore: ActivityStore,
|
|
51
55
|
emitEvent: EmitEvent,
|
|
52
56
|
log: Logger,
|
|
57
|
+
probes: ProbeRegistry = defaultProbeRegistry,
|
|
53
58
|
) {
|
|
54
59
|
this.config = config
|
|
55
60
|
this.activityStore = activityStore
|
|
56
61
|
this.emitEvent = emitEvent
|
|
57
62
|
this.log = log
|
|
63
|
+
this.probes = probes
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
setWorkingStateManager(manager: WorkingStateManager): void {
|
|
@@ -150,6 +156,34 @@ export class ToolExecutor {
|
|
|
150
156
|
input,
|
|
151
157
|
})
|
|
152
158
|
|
|
159
|
+
const vetoOutcome = this.probes.queryVeto(
|
|
160
|
+
{
|
|
161
|
+
type: 'tool_executing',
|
|
162
|
+
runId: this.config.runId,
|
|
163
|
+
toolName,
|
|
164
|
+
input,
|
|
165
|
+
},
|
|
166
|
+
buildProbeContext({ runId: this.config.runId }),
|
|
167
|
+
)
|
|
168
|
+
if (vetoOutcome.action === 'deny') {
|
|
169
|
+
const probeName = vetoOutcome.probeName ?? 'unnamed'
|
|
170
|
+
const reason = vetoOutcome.reason ?? 'no reason provided'
|
|
171
|
+
const veto = new ProbeVetoError(probeName, reason, 'tool_executing')
|
|
172
|
+
this.log.warn('Tool call denied by probe', {
|
|
173
|
+
runId: this.config.runId,
|
|
174
|
+
tool: toolName,
|
|
175
|
+
probeName,
|
|
176
|
+
reason,
|
|
177
|
+
})
|
|
178
|
+
if (activity) {
|
|
179
|
+
this.activityStore.fail(activity.id, veto.message)
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
toolCallId: toolCall.id,
|
|
183
|
+
output: `Error: ${veto.message}`,
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
153
187
|
if (this.workingStateManager) {
|
|
154
188
|
extractFromToolCall(this.workingStateManager, toolName, JSON.stringify(input))
|
|
155
189
|
}
|