@adhd/agent-mcp 2.0.1 → 2.1.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/AGENTS.md +236 -0
- package/CHANGELOG.md +115 -0
- package/LICENSE +21 -0
- package/README.md +201 -0
- package/docs/architecture-and-security.md +405 -0
- package/docs/marketing/.catalog/capabilities.md +143 -0
- package/docs/marketing/.catalog/consumer.md +111 -0
- package/docs/marketing/.catalog/distribution.md +223 -0
- package/docs/marketing/.catalog/doc-conformance.md +237 -0
- package/docs/marketing/.catalog/doc-ops.md +98 -0
- package/docs/marketing/.catalog/metrics.md +119 -0
- package/docs/marketing/.catalog/required-tooling.md +224 -0
- package/docs/marketing/.catalog/review.md +164 -0
- package/docs/provider-call-audit.md +254 -0
- package/llms.txt +133 -0
- package/package.json +30 -51
- package/src/__tests__/integration/harness.d.ts +871 -39
- package/src/__tests__/integration/harness.d.ts.map +1 -1
- package/src/__tests__/integration/harness.js +93 -87
- package/src/__tests__/integration/harness.js.map +1 -1
- package/src/__tests__/integration/scripted-provider.d.ts +1 -2
- package/src/__tests__/integration/scripted-provider.d.ts.map +1 -1
- package/src/__tests__/integration/scripted-provider.js +1 -3
- package/src/__tests__/integration/scripted-provider.js.map +1 -1
- package/src/config.d.ts +6 -69
- package/src/config.d.ts.map +1 -1
- package/src/config.js +1 -50
- package/src/config.js.map +1 -1
- package/src/db/client.d.ts +1516 -2
- package/src/db/client.d.ts.map +1 -1
- package/src/db/client.js +4 -2
- package/src/db/client.js.map +1 -1
- package/src/db/migrate-runner.d.ts +0 -28
- package/src/db/migrate-runner.d.ts.map +1 -1
- package/src/db/migrate-runner.js +0 -24
- package/src/db/migrate-runner.js.map +1 -1
- package/src/db/migrate.d.ts +0 -7
- package/src/db/migrate.d.ts.map +1 -1
- package/src/db/migrate.js +0 -7
- package/src/db/migrate.js.map +1 -1
- package/src/db/schema.d.ts +0 -1260
- package/src/db/schema.d.ts.map +1 -1
- package/src/db/schema.js +2 -179
- package/src/db/schema.js.map +1 -1
- package/src/index.d.ts +3 -42
- package/src/index.d.ts.map +1 -1
- package/src/index.js +32 -123
- package/src/index.js.map +1 -1
- package/src/logger.d.ts +0 -8
- package/src/logger.d.ts.map +1 -1
- package/src/logger.js +1 -12
- package/src/logger.js.map +1 -1
- package/src/scripts/agent-mcp-tail.d.ts +3 -0
- package/src/scripts/agent-mcp-tail.d.ts.map +1 -0
- package/src/scripts/agent-mcp-tail.js +227 -0
- package/src/scripts/agent-mcp-tail.js.map +1 -0
- package/src/server.d.ts +13 -34
- package/src/server.d.ts.map +1 -1
- package/src/server.js +234 -326
- package/src/server.js.map +1 -1
- package/src/store/agent-store.d.ts +3 -16
- package/src/store/agent-store.d.ts.map +1 -1
- package/src/store/agent-store.js +38 -41
- package/src/store/agent-store.js.map +1 -1
- package/src/streaming/chat-gateway.d.ts +17 -0
- package/src/streaming/chat-gateway.d.ts.map +1 -0
- package/src/streaming/chat-gateway.js +336 -0
- package/src/streaming/chat-gateway.js.map +1 -0
- package/src/streaming/event-bus.d.ts +4 -0
- package/src/streaming/event-bus.d.ts.map +1 -1
- package/src/streaming/event-bus.js +14 -1
- package/src/streaming/event-bus.js.map +1 -1
- package/src/streaming/sse-server.d.ts +3 -14
- package/src/streaming/sse-server.d.ts.map +1 -1
- package/src/streaming/sse-server.js +37 -37
- package/src/streaming/sse-server.js.map +1 -1
- package/src/utils/load-env.d.ts +0 -14
- package/src/utils/load-env.d.ts.map +1 -1
- package/src/utils/load-env.js +0 -18
- package/src/utils/load-env.js.map +1 -1
- package/drizzle/0000_nifty_sasquatch.sql +0 -55
- package/drizzle/0001_task_usage.sql +0 -16
- package/drizzle/0002_lumpy_silver_centurion.sql +0 -2
- package/drizzle/0003_nifty_shriek.sql +0 -2
- package/drizzle/0004_brief_shocker.sql +0 -4
- package/drizzle/0005_clear_lenny_balinger.sql +0 -25
- package/drizzle/0006_composed_prompts_cache.sql +0 -24
- package/drizzle/meta/0000_snapshot.json +0 -387
- package/drizzle/meta/0001_snapshot.json +0 -495
- package/drizzle/meta/0002_snapshot.json +0 -509
- package/drizzle/meta/0003_snapshot.json +0 -523
- package/drizzle/meta/0004_snapshot.json +0 -551
- package/drizzle/meta/0005_snapshot.json +0 -545
- package/drizzle/meta/0006_snapshot.json +0 -651
- package/drizzle/meta/_journal.json +0 -55
- package/src/clients/http-client.d.ts +0 -24
- package/src/clients/http-client.d.ts.map +0 -1
- package/src/clients/http-client.js +0 -98
- package/src/clients/http-client.js.map +0 -1
- package/src/clients/in-process.d.ts +0 -27
- package/src/clients/in-process.d.ts.map +0 -1
- package/src/clients/in-process.js +0 -37
- package/src/clients/in-process.js.map +0 -1
- package/src/clients/index.d.ts +0 -7
- package/src/clients/index.d.ts.map +0 -1
- package/src/clients/index.js +0 -5
- package/src/clients/index.js.map +0 -1
- package/src/clients/registry.d.ts +0 -54
- package/src/clients/registry.d.ts.map +0 -1
- package/src/clients/registry.js +0 -164
- package/src/clients/registry.js.map +0 -1
- package/src/clients/stdio-client.d.ts +0 -16
- package/src/clients/stdio-client.d.ts.map +0 -1
- package/src/clients/stdio-client.js +0 -97
- package/src/clients/stdio-client.js.map +0 -1
- package/src/clients/tool-naming.d.ts +0 -44
- package/src/clients/tool-naming.d.ts.map +0 -1
- package/src/clients/tool-naming.js +0 -65
- package/src/clients/tool-naming.js.map +0 -1
- package/src/clients/types.d.ts +0 -18
- package/src/clients/types.d.ts.map +0 -1
- package/src/clients/types.js +0 -2
- package/src/clients/types.js.map +0 -1
- package/src/engine/dag-engine.d.ts +0 -62
- package/src/engine/dag-engine.d.ts.map +0 -1
- package/src/engine/dag-engine.js +0 -158
- package/src/engine/dag-engine.js.map +0 -1
- package/src/engine/hooks.d.ts +0 -24
- package/src/engine/hooks.d.ts.map +0 -1
- package/src/engine/hooks.js +0 -50
- package/src/engine/hooks.js.map +0 -1
- package/src/engine/orchestrator.d.ts +0 -40
- package/src/engine/orchestrator.d.ts.map +0 -1
- package/src/engine/orchestrator.js +0 -507
- package/src/engine/orchestrator.js.map +0 -1
- package/src/engine/policy.d.ts +0 -66
- package/src/engine/policy.d.ts.map +0 -1
- package/src/engine/policy.js +0 -112
- package/src/engine/policy.js.map +0 -1
- package/src/engine/prompt-resolver.d.ts +0 -89
- package/src/engine/prompt-resolver.d.ts.map +0 -1
- package/src/engine/prompt-resolver.js +0 -96
- package/src/engine/prompt-resolver.js.map +0 -1
- package/src/engine/queue.d.ts +0 -29
- package/src/engine/queue.d.ts.map +0 -1
- package/src/engine/queue.js +0 -56
- package/src/engine/queue.js.map +0 -1
- package/src/plugins/index.d.ts +0 -2
- package/src/plugins/index.d.ts.map +0 -1
- package/src/plugins/index.js +0 -2
- package/src/plugins/index.js.map +0 -1
- package/src/plugins/loader.d.ts +0 -65
- package/src/plugins/loader.d.ts.map +0 -1
- package/src/plugins/loader.js +0 -222
- package/src/plugins/loader.js.map +0 -1
- package/src/plugins/usage-plugin.d.ts +0 -27
- package/src/plugins/usage-plugin.d.ts.map +0 -1
- package/src/plugins/usage-plugin.js +0 -148
- package/src/plugins/usage-plugin.js.map +0 -1
- package/src/providers/anthropic.d.ts +0 -13
- package/src/providers/anthropic.d.ts.map +0 -1
- package/src/providers/anthropic.js +0 -260
- package/src/providers/anthropic.js.map +0 -1
- package/src/providers/claudecli.d.ts +0 -153
- package/src/providers/claudecli.d.ts.map +0 -1
- package/src/providers/claudecli.js +0 -510
- package/src/providers/claudecli.js.map +0 -1
- package/src/providers/factory.d.ts +0 -10
- package/src/providers/factory.d.ts.map +0 -1
- package/src/providers/factory.js +0 -24
- package/src/providers/factory.js.map +0 -1
- package/src/providers/index.d.ts +0 -6
- package/src/providers/index.d.ts.map +0 -1
- package/src/providers/index.js +0 -5
- package/src/providers/index.js.map +0 -1
- package/src/providers/openai.d.ts +0 -14
- package/src/providers/openai.d.ts.map +0 -1
- package/src/providers/openai.js +0 -162
- package/src/providers/openai.js.map +0 -1
- package/src/providers/types.d.ts +0 -36
- package/src/providers/types.d.ts.map +0 -1
- package/src/providers/types.js +0 -2
- package/src/providers/types.js.map +0 -1
- package/src/store/composed-prompt-store.d.ts +0 -24
- package/src/store/composed-prompt-store.d.ts.map +0 -1
- package/src/store/composed-prompt-store.js +0 -75
- package/src/store/composed-prompt-store.js.map +0 -1
- package/src/store/index.d.ts +0 -5
- package/src/store/index.d.ts.map +0 -1
- package/src/store/index.js +0 -5
- package/src/store/index.js.map +0 -1
- package/src/store/session-store.d.ts +0 -45
- package/src/store/session-store.d.ts.map +0 -1
- package/src/store/session-store.js +0 -197
- package/src/store/session-store.js.map +0 -1
- package/src/store/task-store.d.ts +0 -55
- package/src/store/task-store.d.ts.map +0 -1
- package/src/store/task-store.js +0 -186
- package/src/store/task-store.js.map +0 -1
- package/src/tools/agent-crud.d.ts +0 -15
- package/src/tools/agent-crud.d.ts.map +0 -1
- package/src/tools/agent-crud.js +0 -32
- package/src/tools/agent-crud.js.map +0 -1
- package/src/tools/session.d.ts +0 -33
- package/src/tools/session.d.ts.map +0 -1
- package/src/tools/session.js +0 -73
- package/src/tools/session.js.map +0 -1
- package/src/tools/task.d.ts +0 -86
- package/src/tools/task.d.ts.map +0 -1
- package/src/tools/task.js +0 -406
- package/src/tools/task.js.map +0 -1
- package/src/tools/usage.d.ts +0 -60
- package/src/tools/usage.d.ts.map +0 -1
- package/src/tools/usage.js +0 -174
- package/src/tools/usage.js.map +0 -1
- package/src/types/agent.d.ts +0 -2
- package/src/types/agent.d.ts.map +0 -1
- package/src/types/agent.js +0 -3
- package/src/types/agent.js.map +0 -1
- package/src/types/execution.d.ts +0 -2
- package/src/types/execution.d.ts.map +0 -1
- package/src/types/execution.js +0 -3
- package/src/types/execution.js.map +0 -1
- package/src/types/index.d.ts +0 -2
- package/src/types/index.d.ts.map +0 -1
- package/src/types/index.js +0 -2
- package/src/types/index.js.map +0 -1
- package/src/types/mcp.d.ts +0 -2
- package/src/types/mcp.d.ts.map +0 -1
- package/src/types/mcp.js +0 -3
- package/src/types/mcp.js.map +0 -1
- package/src/types/message.d.ts +0 -2
- package/src/types/message.d.ts.map +0 -1
- package/src/types/message.js +0 -3
- package/src/types/message.js.map +0 -1
- package/src/types/session.d.ts +0 -2
- package/src/types/session.d.ts.map +0 -1
- package/src/types/session.js +0 -3
- package/src/types/session.js.map +0 -1
- package/src/types/task.d.ts +0 -2
- package/src/types/task.d.ts.map +0 -1
- package/src/types/task.js +0 -3
- package/src/types/task.js.map +0 -1
- package/src/utils/ids.d.ts +0 -2
- package/src/utils/ids.d.ts.map +0 -1
- package/src/utils/ids.js +0 -5
- package/src/utils/ids.js.map +0 -1
- package/src/utils/logger.d.ts +0 -2
- package/src/utils/logger.d.ts.map +0 -1
- package/src/utils/logger.js +0 -5
- package/src/utils/logger.js.map +0 -1
- package/src/utils/timestamps.d.ts +0 -2
- package/src/utils/timestamps.d.ts.map +0 -1
- package/src/utils/timestamps.js +0 -4
- package/src/utils/timestamps.js.map +0 -1
- package/src/validation/agent.d.ts +0 -400
- package/src/validation/agent.d.ts.map +0 -1
- package/src/validation/agent.js +0 -251
- package/src/validation/agent.js.map +0 -1
- package/src/validation/agents.d.ts +0 -2
- package/src/validation/agents.d.ts.map +0 -1
- package/src/validation/agents.js +0 -3
- package/src/validation/agents.js.map +0 -1
- package/src/validation/errors.d.ts +0 -32
- package/src/validation/errors.d.ts.map +0 -1
- package/src/validation/errors.js +0 -42
- package/src/validation/errors.js.map +0 -1
- package/src/validation/execution.d.ts +0 -86
- package/src/validation/execution.d.ts.map +0 -1
- package/src/validation/execution.js +0 -28
- package/src/validation/execution.js.map +0 -1
- package/src/validation/index.d.ts +0 -9
- package/src/validation/index.d.ts.map +0 -1
- package/src/validation/index.js +0 -9
- package/src/validation/index.js.map +0 -1
- package/src/validation/mcp.d.ts +0 -42
- package/src/validation/mcp.d.ts.map +0 -1
- package/src/validation/mcp.js +0 -28
- package/src/validation/mcp.js.map +0 -1
- package/src/validation/message.d.ts +0 -43
- package/src/validation/message.d.ts.map +0 -1
- package/src/validation/message.js +0 -23
- package/src/validation/message.js.map +0 -1
- package/src/validation/session.d.ts +0 -49
- package/src/validation/session.d.ts.map +0 -1
- package/src/validation/session.js +0 -34
- package/src/validation/session.js.map +0 -1
- package/src/validation/task.d.ts +0 -146
- package/src/validation/task.d.ts.map +0 -1
- package/src/validation/task.js +0 -95
- package/src/validation/task.js.map +0 -1
- package/src/validation/tasks.d.ts +0 -2
- package/src/validation/tasks.d.ts.map +0 -1
- package/src/validation/tasks.js +0 -3
- package/src/validation/tasks.js.map +0 -1
- package/src/validation/usage.d.ts +0 -89
- package/src/validation/usage.d.ts.map +0 -1
- package/src/validation/usage.js +0 -98
- package/src/validation/usage.js.map +0 -1
|
@@ -1,507 +0,0 @@
|
|
|
1
|
-
import { logger } from "../logger.js";
|
|
2
|
-
import { config } from "../config.js";
|
|
3
|
-
import { ToolError } from "../validation/errors.js";
|
|
4
|
-
import { generateId } from "../utils/ids.js";
|
|
5
|
-
import { nowIso } from "../utils/timestamps.js";
|
|
6
|
-
import { emitTaskEvent } from "../streaming/event-bus.js";
|
|
7
|
-
import { windowMessages } from "../store/session-store.js";
|
|
8
|
-
// ── HITL (Human-in-the-Loop) support ─────────────────────────────────────────
|
|
9
|
-
/** Built-in tool name intercepted before any MCP client dispatch. */
|
|
10
|
-
const HITL_TOOL_NAME = "request_human_input";
|
|
11
|
-
/**
|
|
12
|
-
* Tool definition advertised to the model when `allowHumanInput === true` on
|
|
13
|
-
* the agent and the task is non-ephemeral.
|
|
14
|
-
*
|
|
15
|
-
* Name encodes as `builtin__request_human_input`:
|
|
16
|
-
* - server = "builtin" (both OpenAI and Anthropic split on first "__")
|
|
17
|
-
* - tool = "request_human_input" ← matches HITL_TOOL_NAME
|
|
18
|
-
*
|
|
19
|
-
* The existing Phase-1 intercept (`tc.tool === HITL_TOOL_NAME`) catches it
|
|
20
|
-
* before Phase-2 dispatch, so it is never forwarded to any MCP client.
|
|
21
|
-
*/
|
|
22
|
-
const HITL_BUILTIN_TOOL_DEFINITION = {
|
|
23
|
-
name: "builtin__request_human_input",
|
|
24
|
-
description: "Pause the task to ask the human operator a question. The task suspends " +
|
|
25
|
-
"until a human answers via task_resume. Use when you need human confirmation, " +
|
|
26
|
-
"a decision, or missing information you cannot obtain yourself.",
|
|
27
|
-
inputSchema: {
|
|
28
|
-
type: "object",
|
|
29
|
-
properties: {
|
|
30
|
-
prompt: {
|
|
31
|
-
type: "string",
|
|
32
|
-
description: "The question to ask the human.",
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
required: ["prompt"],
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Module-scoped resolver map: taskId → resolver function.
|
|
40
|
-
* Populated when an orchestrator suspends awaiting human input.
|
|
41
|
-
* Entries are cleared when resolved or when the task is cancelled.
|
|
42
|
-
*/
|
|
43
|
-
const hitlResolvers = new Map();
|
|
44
|
-
/**
|
|
45
|
-
* Resolve a suspended HITL task with the provided user input.
|
|
46
|
-
* Returns `true` if the resolver was found and called; `false` if the
|
|
47
|
-
* process restarted and the in-memory resolver no longer exists.
|
|
48
|
-
*/
|
|
49
|
-
export function resolveHitl(taskId, userInput) {
|
|
50
|
-
const resolve = hitlResolvers.get(taskId);
|
|
51
|
-
if (!resolve)
|
|
52
|
-
return false;
|
|
53
|
-
hitlResolvers.delete(taskId);
|
|
54
|
-
resolve(userInput);
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
/** Duck-type check — avoids importing the budget plugin to identify its errors. */
|
|
58
|
-
function isEnforcementError(err) {
|
|
59
|
-
return (typeof err === "object" &&
|
|
60
|
-
err !== null &&
|
|
61
|
-
err.isEnforcementError === true);
|
|
62
|
-
}
|
|
63
|
-
/** No-op IHookRegistry used as a fallback when none is provided. */
|
|
64
|
-
const noopHooks = {
|
|
65
|
-
register: () => undefined,
|
|
66
|
-
emit: async () => undefined,
|
|
67
|
-
registerEnforcement: () => undefined,
|
|
68
|
-
enforce: async () => undefined,
|
|
69
|
-
};
|
|
70
|
-
export class Orchestrator {
|
|
71
|
-
async run(input) {
|
|
72
|
-
const { executionContext, registry, provider, policy, taskStore, sessionStore, signal, taskId, hooks = noopHooks, isEphemeral = false, } = input;
|
|
73
|
-
// Working copy of messages — we append to this as the loop progresses
|
|
74
|
-
const currentMessages = [...input.messages];
|
|
75
|
-
const contextLimit = config.server.contextLimit;
|
|
76
|
-
// BUG-002: track sessions opened by delegation during this task so we can
|
|
77
|
-
// close them on failure or cancellation, preventing orphaned active sessions
|
|
78
|
-
// that make sub-agents undeletable (AGENT_HAS_ACTIVE_SESSIONS).
|
|
79
|
-
// On success we leave them open — the caller has the session IDs and may
|
|
80
|
-
// want to continue using them. Only failure/cancel paths close them.
|
|
81
|
-
const delegationSessions = new Set();
|
|
82
|
-
let taskSucceeded = false;
|
|
83
|
-
try {
|
|
84
|
-
// Mark task as running
|
|
85
|
-
taskStore.updateStatus(taskId, "running");
|
|
86
|
-
emitTaskEvent({ type: "status_change", taskId, status: "running" });
|
|
87
|
-
await hooks.emit("task:start", { executionContext, messages: currentMessages, rootTaskId: executionContext.rootTaskId });
|
|
88
|
-
let finalContent = "";
|
|
89
|
-
// Tool-use loop — break on "completed" stop reason, throw on cancellation/policy
|
|
90
|
-
let looping = true;
|
|
91
|
-
while (looping) {
|
|
92
|
-
// Check for cancellation at the top of each iteration
|
|
93
|
-
if (signal.aborted) {
|
|
94
|
-
throw new ToolError("PROVIDER_ERROR", "Task was cancelled");
|
|
95
|
-
}
|
|
96
|
-
// Compose per-iteration signal: task cancellation OR provider timeout
|
|
97
|
-
const composedSignal = AbortSignal.any([
|
|
98
|
-
signal,
|
|
99
|
-
AbortSignal.timeout(executionContext.agentDefinition.provider.timeoutMs ?? 60_000),
|
|
100
|
-
]);
|
|
101
|
-
// Gather available tools from the registry
|
|
102
|
-
const tools = await registry.listAllTools();
|
|
103
|
-
// Append the built-in HITL tool when the agent opts in AND the
|
|
104
|
-
// task is durable (non-ephemeral has a DB row for the resume token).
|
|
105
|
-
if (executionContext.agentDefinition.allowHumanInput === true && !isEphemeral) {
|
|
106
|
-
tools.push(HITL_BUILTIN_TOOL_DEFINITION);
|
|
107
|
-
}
|
|
108
|
-
// Emit MODEL_REQUEST event
|
|
109
|
-
taskStore.appendEvent({
|
|
110
|
-
taskId,
|
|
111
|
-
type: "MODEL_REQUEST",
|
|
112
|
-
payload: { messageCount: currentMessages.length, toolCount: tools.length },
|
|
113
|
-
});
|
|
114
|
-
logger.debug({
|
|
115
|
-
taskId,
|
|
116
|
-
sessionId: executionContext.sessionId,
|
|
117
|
-
agentName: executionContext.agentName,
|
|
118
|
-
messageCount: currentMessages.length,
|
|
119
|
-
}, "MODEL_REQUEST");
|
|
120
|
-
// Emit pre:model_request hook (observational — errors swallowed)
|
|
121
|
-
await hooks.emit("pre:model_request", { executionContext, messages: currentMessages, tools });
|
|
122
|
-
// Enforcement hooks — throws propagate; budget violations surface as BUDGET_EXCEEDED
|
|
123
|
-
try {
|
|
124
|
-
await hooks.enforce("pre:model_request", { executionContext, messages: currentMessages, tools });
|
|
125
|
-
}
|
|
126
|
-
catch (err) {
|
|
127
|
-
if (isEnforcementError(err)) {
|
|
128
|
-
throw new ToolError("BUDGET_EXCEEDED", err.message);
|
|
129
|
-
}
|
|
130
|
-
throw err;
|
|
131
|
-
}
|
|
132
|
-
// Call the LLM provider
|
|
133
|
-
let providerResponse;
|
|
134
|
-
try {
|
|
135
|
-
const messagesToSend = contextLimit > 0
|
|
136
|
-
? windowMessages(currentMessages, contextLimit)
|
|
137
|
-
: currentMessages;
|
|
138
|
-
providerResponse = await provider.chat({
|
|
139
|
-
messages: messagesToSend,
|
|
140
|
-
tools: tools.length > 0 ? tools : undefined,
|
|
141
|
-
signal: composedSignal,
|
|
142
|
-
// executeTool is used by providers (e.g. claudecli) that manage
|
|
143
|
-
// their own internal tool loop. Standard providers (anthropic,
|
|
144
|
-
// openai) return stopReason "tool_calls" and ignore this.
|
|
145
|
-
executeTool: async (server, tool, args) => {
|
|
146
|
-
const client = await registry.getClient(server);
|
|
147
|
-
try {
|
|
148
|
-
const result = await client.callTool(tool, args, composedSignal);
|
|
149
|
-
return { result, isError: false };
|
|
150
|
-
}
|
|
151
|
-
catch (error) {
|
|
152
|
-
return {
|
|
153
|
-
result: error instanceof Error ? error.message : String(error),
|
|
154
|
-
isError: true,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
catch (error) {
|
|
161
|
-
// Order matters — see [inv:provider-error-dispatch]:
|
|
162
|
-
// cancellation first, then timeout, then auth, then rate-limit, then generic.
|
|
163
|
-
// signal.aborted = user-initiated task cancel; composedSignal.aborted-only = timeout.
|
|
164
|
-
if (signal.aborted) {
|
|
165
|
-
throw new ToolError("PROVIDER_ERROR", "Task was cancelled");
|
|
166
|
-
}
|
|
167
|
-
if (composedSignal.aborted ||
|
|
168
|
-
(error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError"))) {
|
|
169
|
-
const ms = executionContext.agentDefinition.provider.timeoutMs ?? 60_000;
|
|
170
|
-
throw new ToolError("PROVIDER_TIMEOUT", `Provider call timed out after ${ms}ms. Increase timeoutMs on the agent's provider config.`);
|
|
171
|
-
}
|
|
172
|
-
if (error instanceof Error && (error.constructor.name === "AuthenticationError" ||
|
|
173
|
-
("status" in error && error.status === 401))) {
|
|
174
|
-
throw new ToolError("PROVIDER_AUTH_ERROR", `Provider authentication failed: ${error.message}. ` +
|
|
175
|
-
`Set ANTHROPIC_AUTH_TOKEN (run \`claude setup-token\` to obtain an OAuth access token) or use authTokenEnv in the provider config`);
|
|
176
|
-
}
|
|
177
|
-
if (error instanceof Error && (("status" in error && error.status === 429) ||
|
|
178
|
-
error.message?.includes("rate limit") ||
|
|
179
|
-
error.message?.includes("429"))) {
|
|
180
|
-
throw new ToolError("PROVIDER_RATE_LIMITED", `Provider rate limit exceeded: ${error.message}`);
|
|
181
|
-
}
|
|
182
|
-
if (error instanceof Error && (("code" in error && error.code === "context_length_exceeded") ||
|
|
183
|
-
error.message?.includes("context_length_exceeded") ||
|
|
184
|
-
error.message?.includes("prompt is too long"))) {
|
|
185
|
-
throw new ToolError("CONTEXT_WINDOW_EXCEEDED", `Context window exceeded. Set AGENT_MCP_CONTEXT_LIMIT to enable automatic truncation.`);
|
|
186
|
-
}
|
|
187
|
-
throw new ToolError("PROVIDER_ERROR", `Provider call failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
188
|
-
}
|
|
189
|
-
const assistantMessage = {
|
|
190
|
-
...providerResponse.message,
|
|
191
|
-
sessionId: executionContext.sessionId,
|
|
192
|
-
};
|
|
193
|
-
// Persist and append the assistant message
|
|
194
|
-
await sessionStore.appendMessage(executionContext.sessionId, assistantMessage);
|
|
195
|
-
currentMessages.push(assistantMessage);
|
|
196
|
-
await hooks.emit("post:model_response", {
|
|
197
|
-
executionContext,
|
|
198
|
-
stopReason: providerResponse.stopReason,
|
|
199
|
-
toolCallCount: assistantMessage.toolCalls?.length ?? 0,
|
|
200
|
-
tokenUsage: providerResponse.usage,
|
|
201
|
-
});
|
|
202
|
-
await hooks.emit("message:appended", { executionContext, message: assistantMessage });
|
|
203
|
-
// Emit MODEL_RESPONSE event
|
|
204
|
-
taskStore.appendEvent({
|
|
205
|
-
taskId,
|
|
206
|
-
type: "MODEL_RESPONSE",
|
|
207
|
-
payload: {
|
|
208
|
-
stopReason: providerResponse.stopReason,
|
|
209
|
-
hasContent: !!assistantMessage.content,
|
|
210
|
-
toolCallCount: assistantMessage.toolCalls?.length ?? 0,
|
|
211
|
-
},
|
|
212
|
-
});
|
|
213
|
-
logger.debug({
|
|
214
|
-
taskId,
|
|
215
|
-
agentName: executionContext.agentName,
|
|
216
|
-
stopReason: providerResponse.stopReason,
|
|
217
|
-
}, "MODEL_RESPONSE");
|
|
218
|
-
if (providerResponse.stopReason === "completed") {
|
|
219
|
-
finalContent = assistantMessage.content ?? "";
|
|
220
|
-
looping = false;
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
// Process tool calls
|
|
224
|
-
const toolCalls = assistantMessage.toolCalls ?? [];
|
|
225
|
-
// Phase 1 — serial pre-dispatch loop (policy + count).
|
|
226
|
-
// Uses variable name `tc` to distinguish from the old sequential per-tool dispatch
|
|
227
|
-
// that has been replaced by Phase 2 (Promise.all concurrent execution below).
|
|
228
|
-
//
|
|
229
|
-
// HITL intercept: `request_human_input` is intercepted here (before Phase 2)
|
|
230
|
-
// and never reaches the MCP client. [inv:request-human-input-intercept]
|
|
231
|
-
const hitlResults = new Map(); // toolCall.id → userInput
|
|
232
|
-
for (const tc of toolCalls) {
|
|
233
|
-
if (signal.aborted) {
|
|
234
|
-
throw new ToolError("PROVIDER_ERROR", "Task was cancelled before tool call");
|
|
235
|
-
}
|
|
236
|
-
// ── HITL intercept ──────────────────────────────────────
|
|
237
|
-
if (tc.tool === HITL_TOOL_NAME) {
|
|
238
|
-
// Ephemeral tasks have no session row — cannot persist resume_token
|
|
239
|
-
// (no DB row means task_resume cannot validate it and HITL cannot
|
|
240
|
-
// be resumed after a process restart). Reject via isEphemeral flag.
|
|
241
|
-
if (isEphemeral) {
|
|
242
|
-
throw new ToolError("VALIDATION_ERROR", "request_human_input is not supported for ephemeral tasks");
|
|
243
|
-
}
|
|
244
|
-
// 1. Generate a resumeToken
|
|
245
|
-
const resumeToken = crypto.randomUUID();
|
|
246
|
-
// 2. Persist suspension to DB BEFORE awaiting [inv:resume-token-db-persisted]
|
|
247
|
-
// This ordering ensures the token survives a process restart.
|
|
248
|
-
await taskStore.updateStatus(taskId, "awaiting_input", { resumeToken });
|
|
249
|
-
emitTaskEvent({ type: "status_change", taskId, status: "awaiting_input" });
|
|
250
|
-
// 3. Register resolver and await userInput.
|
|
251
|
-
// Wire into the AbortSignal so task_cancel unblocks this promise —
|
|
252
|
-
// without this, cancelling an awaiting_input task leaves the promise
|
|
253
|
-
// pending forever and the orchestrator's async context leaks.
|
|
254
|
-
let abortHandler;
|
|
255
|
-
const userInput = await new Promise((resolve, reject) => {
|
|
256
|
-
hitlResolvers.set(taskId, resolve);
|
|
257
|
-
abortHandler = () => {
|
|
258
|
-
hitlResolvers.delete(taskId);
|
|
259
|
-
reject(new ToolError("PROVIDER_ERROR", "Task cancelled while awaiting human input"));
|
|
260
|
-
};
|
|
261
|
-
signal.addEventListener("abort", abortHandler, { once: true });
|
|
262
|
-
}).finally(() => {
|
|
263
|
-
// Remove the abort listener once the promise settles so a
|
|
264
|
-
// later cancellation of this (now-resumed) task does not fire
|
|
265
|
-
// a stale handler / leak a listener on the long-lived signal.
|
|
266
|
-
if (abortHandler)
|
|
267
|
-
signal.removeEventListener("abort", abortHandler);
|
|
268
|
-
});
|
|
269
|
-
// 4. Mark running again
|
|
270
|
-
await taskStore.updateStatus(taskId, "running");
|
|
271
|
-
emitTaskEvent({ type: "status_change", taskId, status: "running" });
|
|
272
|
-
// 5. Store userInput to inject as tool result in Phase 3
|
|
273
|
-
hitlResults.set(tc.id, userInput);
|
|
274
|
-
// Return resume token to the caller via event log; continue to next tc
|
|
275
|
-
taskStore.appendEvent({
|
|
276
|
-
taskId,
|
|
277
|
-
type: "TOOL_CALL",
|
|
278
|
-
payload: { tool: HITL_TOOL_NAME, callId: tc.id, resumeToken },
|
|
279
|
-
});
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
// ── end HITL intercept ──────────────────────────────────
|
|
283
|
-
// Resolve the REAL {server,tool}: OpenAI-compatible/local models
|
|
284
|
-
// rewrite '-' → '_' in tool names, so the returned name may not
|
|
285
|
-
// match what we advertised. Resolving here keeps the qualified
|
|
286
|
-
// name correct for the policy delegation check (otherwise a
|
|
287
|
-
// mangled name would bypass the allowedAgents gate) and dispatch.
|
|
288
|
-
const resolved = registry.resolveToolName?.(`${tc.server}__${tc.tool}`) ??
|
|
289
|
-
{ server: tc.server, tool: tc.tool };
|
|
290
|
-
const qualifiedToolName = `${resolved.server}__${resolved.tool}`;
|
|
291
|
-
await hooks.emit("pre:tool_call", {
|
|
292
|
-
executionContext,
|
|
293
|
-
toolName: qualifiedToolName,
|
|
294
|
-
callId: tc.id,
|
|
295
|
-
toolInput: tc.arguments,
|
|
296
|
-
});
|
|
297
|
-
policy.check({
|
|
298
|
-
executionContext,
|
|
299
|
-
targetTool: qualifiedToolName,
|
|
300
|
-
targetAgentName: qualifiedToolName === "agent-mcp__agent"
|
|
301
|
-
? tc.arguments?.name
|
|
302
|
-
: undefined,
|
|
303
|
-
});
|
|
304
|
-
// Increment AFTER policy.check — see [inv:toolCallCount-increment-after-check].
|
|
305
|
-
// policy.check enforces "allow while toolCallCount < max" (it expects the
|
|
306
|
-
// count of calls already accounted for), so incrementing BEFORE the check
|
|
307
|
-
// fired the cap one call early (effective max-1). Counting per tool here —
|
|
308
|
-
// still inside the serial pre-dispatch loop, not Phase 3 — keeps the limit
|
|
309
|
-
// enforced WITHIN a single concurrent batch: each tool's check sees the
|
|
310
|
-
// running count from prior tools in this same loop.
|
|
311
|
-
executionContext.toolCallCount++;
|
|
312
|
-
}
|
|
313
|
-
// Phase 2 — Promise.all concurrent execution.
|
|
314
|
-
// HITL calls are excluded (already handled in Phase 1 — their results
|
|
315
|
-
// are in hitlResults and will be injected in Phase 3).
|
|
316
|
-
const nonHitlToolCalls = toolCalls.filter(tc => tc.tool !== HITL_TOOL_NAME);
|
|
317
|
-
const toolResults = await Promise.all(nonHitlToolCalls.map(async (toolCall) => {
|
|
318
|
-
const resolved = registry.resolveToolName?.(`${toolCall.server}__${toolCall.tool}`) ??
|
|
319
|
-
{ server: toolCall.server, tool: toolCall.tool };
|
|
320
|
-
const qualifiedToolName = `${resolved.server}__${resolved.tool}`;
|
|
321
|
-
// Emit tool_call SSE event before dispatch
|
|
322
|
-
emitTaskEvent({
|
|
323
|
-
type: "tool_call",
|
|
324
|
-
taskId,
|
|
325
|
-
toolName: qualifiedToolName,
|
|
326
|
-
toolCallId: toolCall.id,
|
|
327
|
-
input: toolCall.arguments,
|
|
328
|
-
});
|
|
329
|
-
taskStore.appendEvent({
|
|
330
|
-
taskId,
|
|
331
|
-
type: "TOOL_CALL",
|
|
332
|
-
payload: { tool: qualifiedToolName, callId: toolCall.id },
|
|
333
|
-
});
|
|
334
|
-
logger.info({ taskId, agentName: executionContext.agentName, tool: qualifiedToolName, callId: toolCall.id }, "TOOL_CALL");
|
|
335
|
-
let toolResult;
|
|
336
|
-
let isError = false;
|
|
337
|
-
try {
|
|
338
|
-
const client = await registry.getClient(resolved.server);
|
|
339
|
-
// Thread the composed task-cancel/timeout signal so a
|
|
340
|
-
// cancel mid-batch interrupts this in-flight call
|
|
341
|
-
// instead of waiting for the whole batch (DEBT-003).
|
|
342
|
-
toolResult = await client.callTool(resolved.tool, toolCall.arguments, composedSignal);
|
|
343
|
-
// BUG-002: capture session IDs opened by delegation so we
|
|
344
|
-
// can close them on failure, preventing undeletable sub-agents.
|
|
345
|
-
if (qualifiedToolName === "agent-mcp__agent" && toolResult != null) {
|
|
346
|
-
const maybeId = toolResult["session_id"];
|
|
347
|
-
if (typeof maybeId === "string") {
|
|
348
|
-
delegationSessions.add(maybeId);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
catch (error) {
|
|
353
|
-
// Re-throw fatal ToolError codes — these abort the entire task, not just this call.
|
|
354
|
-
// See [inv:fatal-policy-codes] in _shared.md.
|
|
355
|
-
const FATAL_CODES = ["MAX_DEPTH_EXCEEDED", "MAX_TOOL_LOOPS_EXCEEDED", "DELEGATION_NOT_ALLOWED"];
|
|
356
|
-
if (error instanceof ToolError && FATAL_CODES.includes(error.code)) {
|
|
357
|
-
throw error;
|
|
358
|
-
}
|
|
359
|
-
isError = true;
|
|
360
|
-
toolResult = error instanceof Error ? error.message : String(error);
|
|
361
|
-
logger.warn({ taskId, tool: qualifiedToolName, error: toolResult }, "TOOL_RESULT error");
|
|
362
|
-
}
|
|
363
|
-
taskStore.appendEvent({
|
|
364
|
-
taskId,
|
|
365
|
-
type: "TOOL_RESULT",
|
|
366
|
-
payload: { callId: toolCall.id, tool: qualifiedToolName, isError },
|
|
367
|
-
});
|
|
368
|
-
// Emit tool_result SSE event after result received
|
|
369
|
-
emitTaskEvent({
|
|
370
|
-
type: "tool_result",
|
|
371
|
-
taskId,
|
|
372
|
-
toolCallId: toolCall.id,
|
|
373
|
-
content: toolResult,
|
|
374
|
-
});
|
|
375
|
-
logger.debug({ taskId, tool: qualifiedToolName, isError }, "TOOL_RESULT");
|
|
376
|
-
await hooks.emit("post:tool_call", {
|
|
377
|
-
executionContext,
|
|
378
|
-
toolName: qualifiedToolName,
|
|
379
|
-
callId: toolCall.id,
|
|
380
|
-
toolInput: toolCall.arguments,
|
|
381
|
-
result: toolResult,
|
|
382
|
-
isError,
|
|
383
|
-
});
|
|
384
|
-
return { toolCall, toolResult, isError };
|
|
385
|
-
}));
|
|
386
|
-
// Phase 3 — serial result append (preserves order per [inv:message-order]).
|
|
387
|
-
// Build a lookup for non-HITL results; then iterate original toolCalls
|
|
388
|
-
// order so HITL and non-HITL results are interleaved correctly.
|
|
389
|
-
const toolResultByCallId = new Map(toolResults.map(r => [r.toolCall.id, r]));
|
|
390
|
-
for (const tc of toolCalls) {
|
|
391
|
-
let toolResult;
|
|
392
|
-
let isError = false;
|
|
393
|
-
if (hitlResults.has(tc.id)) {
|
|
394
|
-
// HITL tool: inject userInput as the result
|
|
395
|
-
toolResult = hitlResults.get(tc.id);
|
|
396
|
-
}
|
|
397
|
-
else {
|
|
398
|
-
const r = toolResultByCallId.get(tc.id);
|
|
399
|
-
if (!r)
|
|
400
|
-
continue; // should not happen
|
|
401
|
-
toolResult = r.toolResult;
|
|
402
|
-
isError = r.isError;
|
|
403
|
-
}
|
|
404
|
-
const toolResultMessage = {
|
|
405
|
-
id: generateId(),
|
|
406
|
-
sessionId: executionContext.sessionId,
|
|
407
|
-
role: "tool",
|
|
408
|
-
toolResults: [{
|
|
409
|
-
toolCallId: tc.id, // [inv:call-id-keying]
|
|
410
|
-
result: toolResult,
|
|
411
|
-
isError,
|
|
412
|
-
}],
|
|
413
|
-
createdAt: nowIso(),
|
|
414
|
-
};
|
|
415
|
-
await sessionStore.appendMessage(executionContext.sessionId, toolResultMessage);
|
|
416
|
-
currentMessages.push(toolResultMessage);
|
|
417
|
-
await hooks.emit("message:appended", { executionContext, message: toolResultMessage });
|
|
418
|
-
}
|
|
419
|
-
// Guard: provider signalled tool_calls but sent no tool call blocks —
|
|
420
|
-
// prevents infinite spin on a malformed response.
|
|
421
|
-
if (providerResponse.stopReason === "tool_calls" &&
|
|
422
|
-
(assistantMessage.toolCalls ?? []).length === 0) {
|
|
423
|
-
finalContent = assistantMessage.content ?? "";
|
|
424
|
-
looping = false;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
// Task completed successfully
|
|
428
|
-
taskStore.updateStatus(taskId, "completed", {
|
|
429
|
-
result: finalContent,
|
|
430
|
-
completedAt: nowIso(),
|
|
431
|
-
});
|
|
432
|
-
emitTaskEvent({ type: "status_change", taskId, status: "completed" });
|
|
433
|
-
taskStore.appendEvent({
|
|
434
|
-
taskId,
|
|
435
|
-
type: "TASK_COMPLETED",
|
|
436
|
-
payload: { result: finalContent },
|
|
437
|
-
});
|
|
438
|
-
logger.info({ taskId, agentName: executionContext.agentName }, "TASK_COMPLETED");
|
|
439
|
-
await hooks.emit("task:completed", { executionContext, result: finalContent });
|
|
440
|
-
// Emit done on the successful completion path
|
|
441
|
-
emitTaskEvent({ type: "done", taskId, result: finalContent, error: null });
|
|
442
|
-
taskSucceeded = true;
|
|
443
|
-
return { result: finalContent };
|
|
444
|
-
}
|
|
445
|
-
catch (error) {
|
|
446
|
-
// Determine if this is a cancellation or a real failure
|
|
447
|
-
const isCancelled = signal.aborted;
|
|
448
|
-
if (isCancelled) {
|
|
449
|
-
// Status update handled by TaskStore.cancel() in most cases;
|
|
450
|
-
// update here only if not already cancelled
|
|
451
|
-
try {
|
|
452
|
-
taskStore.updateStatus(taskId, "cancelled", {
|
|
453
|
-
cancelledAt: nowIso(),
|
|
454
|
-
// Persist the reason so a later SSE terminal-on-connect read
|
|
455
|
-
// surfaces it instead of error:null (matches the live-bus done).
|
|
456
|
-
error: "Task was cancelled",
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
catch {
|
|
460
|
-
// already cancelled — ignore
|
|
461
|
-
}
|
|
462
|
-
emitTaskEvent({ type: "status_change", taskId, status: "cancelled" });
|
|
463
|
-
taskStore.appendEvent({ taskId, type: "TASK_CANCELLED" });
|
|
464
|
-
await hooks.emit("task:cancelled", { executionContext });
|
|
465
|
-
// Emit done on the cancellation path — SSE clients must not hang
|
|
466
|
-
emitTaskEvent({ type: "done", taskId, result: null, error: "Task was cancelled" });
|
|
467
|
-
}
|
|
468
|
-
else {
|
|
469
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
470
|
-
taskStore.updateStatus(taskId, "failed", {
|
|
471
|
-
error: errorMessage,
|
|
472
|
-
});
|
|
473
|
-
emitTaskEvent({ type: "status_change", taskId, status: "failed" });
|
|
474
|
-
taskStore.appendEvent({
|
|
475
|
-
taskId,
|
|
476
|
-
type: "TASK_FAILED",
|
|
477
|
-
payload: { error: errorMessage },
|
|
478
|
-
});
|
|
479
|
-
logger.error({ taskId, agentName: executionContext.agentName, error }, "TASK_FAILED");
|
|
480
|
-
await hooks.emit("task:failed", { executionContext, error: errorMessage });
|
|
481
|
-
// Emit done on the failure path
|
|
482
|
-
emitTaskEvent({ type: "done", taskId, result: null, error: errorMessage });
|
|
483
|
-
}
|
|
484
|
-
throw error;
|
|
485
|
-
}
|
|
486
|
-
finally {
|
|
487
|
-
// BUG-002: close delegation-scoped sessions on failure/cancellation so
|
|
488
|
-
// sub-agents don't become undeletable (AGENT_HAS_ACTIVE_SESSIONS guard).
|
|
489
|
-
// On success the caller keeps the session IDs; on failure/cancel they're
|
|
490
|
-
// orphaned and must be reaped here.
|
|
491
|
-
if (!taskSucceeded) {
|
|
492
|
-
for (const sessionId of delegationSessions) {
|
|
493
|
-
try {
|
|
494
|
-
sessionStore.close(sessionId);
|
|
495
|
-
}
|
|
496
|
-
catch {
|
|
497
|
-
// Already closed or not found — ignore
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
// Per-task registry teardown — always runs
|
|
502
|
-
await registry.closeAll();
|
|
503
|
-
taskStore.unregisterCancellation(taskId);
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
//# sourceMappingURL=orchestrator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"orchestrator.js","sourceRoot":"","sources":["../../../../../../packages/ai/agent-mcp/src/engine/orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAItC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAM1D,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,gFAAgF;AAEhF,qEAAqE;AACrE,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAE7C;;;;;;;;;;GAUG;AACH,MAAM,4BAA4B,GAAG;IACjC,IAAI,EAAE,8BAA8B;IACpC,WAAW,EACP,yEAAyE;QACzE,+EAA+E;QAC/E,gEAAgE;IACpE,WAAW,EAAE;QACT,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE;YACR,MAAM,EAAE;gBACJ,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,gCAAgC;aAChD;SACJ;QACD,QAAQ,EAAE,CAAC,QAAQ,CAAC;KACvB;CACK,CAAC;AAEX;;;;GAIG;AACH,MAAM,aAAa,GAAG,IAAI,GAAG,EAAuC,CAA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KAAK,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE,gBAAgB,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC;gBAE3E,gCAAgC;gBAChC,aAAa,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC;YAC/E,CAAC;YAED,MAAM,KAAK,CAAC;QAChB,CAAC;gBAAS,CAAC;YACP,uEAAuE;YACvE,yEAAyE;YACzE,yEAAyE;YACzE,oCAAoC;YACpC,IAAI,CAAC,aAAa,EAAE,CAAC;gBACjB,KAAK,MAAM,SAAS,IAAI,kBAAkB,EAAE,CAAC;oBACzC,IAAI,CAAC;wBACD,YAAY,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;oBAClC,CAAC;oBAAC,MAAM,CAAC;wBACL,uCAAuC;oBAC3C,CAAC;gBACL,CAAC;YACL,CAAC;YACD,2CAA2C;YAC3C,MAAM,QAAQ,CAAC,QAAQ,EAAE,CAAC;YAC1B,SAAS,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;QAC7C,CAAC;IACL,CAAC;CACJ"}
|
package/src/engine/policy.d.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { ExecutionContext } from "../validation/index.js";
|
|
2
|
-
/** Minimal PolicyTemplate shape consumed from @adhd/agent-policy. */
|
|
3
|
-
export interface AgentPolicyTemplateRule {
|
|
4
|
-
/** Policy type slug — "rate" | "permission" (others are ignored here). */
|
|
5
|
-
type: string;
|
|
6
|
-
/** Structured rule parameters deserialized from the JSON column. */
|
|
7
|
-
rules: Record<string, unknown>;
|
|
8
|
-
}
|
|
9
|
-
export interface PolicyConfig {
|
|
10
|
-
/** Absolute maximum recursion depth. Agent definitions cannot exceed this. */
|
|
11
|
-
serverMaxDepth: number;
|
|
12
|
-
/** Absolute maximum tool call loops per task. Agent definitions cannot exceed this. */
|
|
13
|
-
serverMaxToolLoops: number;
|
|
14
|
-
/**
|
|
15
|
-
* Server-level allowed agents allowlist.
|
|
16
|
-
* - undefined → unrestricted (any agent may be called)
|
|
17
|
-
* - string[] → only agents in this list may be called
|
|
18
|
-
* Per-agent allowedAgents takes full precedence over this when defined.
|
|
19
|
-
*/
|
|
20
|
-
serverAllowedAgents?: string[];
|
|
21
|
-
/**
|
|
22
|
-
* Agent-policy template rules resolved for the current agent.
|
|
23
|
-
*
|
|
24
|
-
* When present, PolicyEngine reads rate/permission limits from these
|
|
25
|
-
* templates and falls back to the serverMax* defaults only when no
|
|
26
|
-
* matching template rule supplies the limit. Consuming the @adhd/agent-policy
|
|
27
|
-
* template SHAPE here keeps policy semantics in one place.
|
|
28
|
-
*
|
|
29
|
-
* Pass the output of `AgentPolicyStore.resolveForAgent(agentSlug)` merged
|
|
30
|
-
* with `PolicyTemplateStore.read(policySlug)` to supply template rules.
|
|
31
|
-
*/
|
|
32
|
-
policyTemplateRules?: AgentPolicyTemplateRule[];
|
|
33
|
-
}
|
|
34
|
-
export interface PolicyCheckInput {
|
|
35
|
-
executionContext: ExecutionContext;
|
|
36
|
-
/**
|
|
37
|
-
* The fully-qualified tool name being invoked: "<server>__<tool>".
|
|
38
|
-
* The allowedAgents check only applies when this is "agent-mcp__agent".
|
|
39
|
-
*/
|
|
40
|
-
targetTool: string;
|
|
41
|
-
/**
|
|
42
|
-
* When targetTool === "agent-mcp__agent", the name of the target agent.
|
|
43
|
-
* Used for the allowedAgents check (#3).
|
|
44
|
-
*/
|
|
45
|
-
targetAgentName?: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* PolicyEngine enforces the three SPEC §9 safety invariants:
|
|
49
|
-
*
|
|
50
|
-
* 1. Recursion depth limit
|
|
51
|
-
* 2. Tool call loop limit
|
|
52
|
-
* 3. AllowedAgents list (per-agent override takes full precedence over server default)
|
|
53
|
-
*
|
|
54
|
-
* Limits are resolved in priority order:
|
|
55
|
-
* 1. Per-agent agentDefinition fields (highest — always wins)
|
|
56
|
-
* 2. @adhd/agent-policy template rules (when policyTemplateRules is supplied)
|
|
57
|
-
* 3. PolicyConfig server defaults (hardcoded fallback)
|
|
58
|
-
*
|
|
59
|
-
* Throws ToolError on any violation. Does not mutate state.
|
|
60
|
-
*/
|
|
61
|
-
export declare class PolicyEngine {
|
|
62
|
-
private readonly config;
|
|
63
|
-
constructor(config: PolicyConfig);
|
|
64
|
-
check(input: PolicyCheckInput): void;
|
|
65
|
-
}
|
|
66
|
-
//# sourceMappingURL=policy.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../../../../../../packages/ai/agent-mcp/src/engine/policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAqB/D,qEAAqE;AACrE,MAAM,WAAW,uBAAuB;IACpC,0EAA0E;IAC1E,IAAI,EAAE,MAAM,CAAC;IACb,oEAAoE;IACpE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,YAAY;IACzB,8EAA8E;IAC9E,cAAc,EAAE,MAAM,CAAC;IACvB,uFAAuF;IACvF,kBAAkB,EAAE,MAAM,CAAC;IAC3B;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B;;;;;;;;;;OAUG;IACH,mBAAmB,CAAC,EAAE,uBAAuB,EAAE,CAAC;CACnD;AAED,MAAM,WAAW,gBAAgB;IAC7B,gBAAgB,EAAE,gBAAgB,CAAC;IACnC;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAyCD;;;;;;;;;;;;;GAaG;AACH,qBAAa,YAAY;IACT,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,YAAY;IAEjD,KAAK,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI;CA2EvC"}
|