@ftrdotdev/pi-delegates 0.0.0-stage → 0.2.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 +128 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +130 -0
- package/README.md +456 -2
- package/ROADMAP.md +179 -0
- package/TESTING.md +896 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +94 -4
- package/skills/pi-delegates/SKILL.md +176 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +184 -0
- package/src/docs-contract.test.ts +106 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +163 -0
- package/src/domain/failure.ts +33 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/model-source.ts +30 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1332 -0
- package/src/domain/orchestration/control.ts +457 -0
- package/src/domain/orchestration/fanout.test.ts +524 -0
- package/src/domain/orchestration/fanout.ts +372 -0
- package/src/domain/orchestration/gates.test.ts +685 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +372 -0
- package/src/domain/orchestration/launch.ts +691 -0
- package/src/domain/orchestration/requests.test.ts +695 -0
- package/src/domain/orchestration/requests.ts +782 -0
- package/src/domain/orchestration/review.test.ts +292 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +265 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1566 -0
- package/src/domain/orchestration/workflow.ts +778 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +689 -0
- package/src/domain/settlement.test.ts +186 -0
- package/src/domain/settlement.ts +137 -0
- package/src/domain/shapes.test.ts +185 -0
- package/src/domain/skills.test.ts +477 -0
- package/src/domain/skills.ts +114 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +215 -0
- package/src/domain/toolkit.ts +374 -0
- package/src/domain/turn-budget.test.ts +37 -0
- package/src/domain/turn-budget.ts +23 -0
- package/src/domain/view.test.ts +803 -0
- package/src/domain/view.ts +491 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +485 -0
- package/src/extension/commands.ts +230 -0
- package/src/extension/entry.test.ts +1276 -0
- package/src/extension/entry.ts +380 -0
- package/src/extension/gate-alerts.test.ts +237 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1343 -0
- package/src/extension/inspector-command.ts +685 -0
- package/src/extension/prompt.test.ts +108 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/retry-observer.test.ts +292 -0
- package/src/extension/retry-observer.ts +150 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/settle-alerts.test.ts +579 -0
- package/src/extension/settle-alerts.ts +175 -0
- package/src/extension/shortcut.test.ts +62 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +1294 -0
- package/src/extension/tools.ts +553 -0
- package/src/extension/version.test.ts +115 -0
- package/src/extension/version.ts +97 -0
- package/src/goldens.test.ts +294 -0
- package/src/harness-policy.test.ts +76 -0
- package/src/layers.test.ts +309 -0
- package/src/layers.ts +405 -0
- package/src/package-manifest.test.ts +170 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +386 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +359 -0
- package/src/presentation/inspector.test.ts +682 -0
- package/src/presentation/inspector.ts +442 -0
- package/src/presentation/overlay.test.ts +553 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +365 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +535 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/status.test.ts +43 -0
- package/src/presentation/status.ts +30 -0
- package/src/presentation/strip.test.ts +203 -0
- package/src/presentation/strip.ts +67 -0
- package/src/presentation/wording.test.ts +388 -0
- package/src/presentation/wording.ts +294 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +128 -0
- package/src/runtime/clock.test.ts +31 -0
- package/src/runtime/clock.ts +31 -0
- package/src/runtime/config.test.ts +63 -0
- package/src/runtime/config.ts +92 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +33 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +152 -0
- package/src/runtime/handle.ts +54 -0
- package/src/runtime/log.test.ts +140 -0
- package/src/runtime/log.ts +91 -0
- package/src/runtime/mcp.test.ts +1450 -0
- package/src/runtime/mcp.ts +1005 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/runtime-state.test.ts +25 -0
- package/src/runtime/runtime-state.ts +36 -0
- package/src/runtime/session-tag.ts +11 -0
- package/src/runtime/session.test.ts +352 -0
- package/src/runtime/session.ts +263 -0
- package/src/runtime/storage.ts +29 -0
- package/src/test-hygiene.test.ts +97 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { AgentChange, Extension, ToolRegistration } from "@earendil-works/pi-durable";
|
|
2
|
+
import { assert, it } from "@effect/vitest";
|
|
3
|
+
import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
|
|
4
|
+
import { Effect, Result } from "effect";
|
|
5
|
+
import { definitionPairArbitrary } from "../../test-support/arbitraries.ts";
|
|
6
|
+
import {
|
|
7
|
+
createCatalogue,
|
|
8
|
+
DefinitionWidened,
|
|
9
|
+
presets,
|
|
10
|
+
type AgentDefinition,
|
|
11
|
+
} from "./definitions.ts";
|
|
12
|
+
import { CODING_EXTENSION, SKILLS_EXTENSION } from "../domain/names.ts";
|
|
13
|
+
import {
|
|
14
|
+
bindToolkit,
|
|
15
|
+
expandToolNames,
|
|
16
|
+
resolveStoredToolkit,
|
|
17
|
+
ToolkitUnresolved,
|
|
18
|
+
} from "./toolkit.ts";
|
|
19
|
+
import { planLaunch, type HostDefaults } from "./orchestration/launch.ts";
|
|
20
|
+
|
|
21
|
+
const host: HostDefaults = {
|
|
22
|
+
model: { provider: "stub", modelId: "stub-1" },
|
|
23
|
+
thinkingLevel: "low",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const fakeExtension = (name: string): Extension => ({ name, tools: [] });
|
|
27
|
+
const fakeTool = (name: string): ToolRegistration =>
|
|
28
|
+
({ name, parameters: {} }) as unknown as ToolRegistration;
|
|
29
|
+
|
|
30
|
+
const resolver = (
|
|
31
|
+
extensions: readonly string[],
|
|
32
|
+
tools: readonly string[],
|
|
33
|
+
): Parameters<typeof bindToolkit>[1] => ({
|
|
34
|
+
availableExtensions: extensions,
|
|
35
|
+
availableTools: tools,
|
|
36
|
+
availableSkills: [],
|
|
37
|
+
extension: (name) => (extensions.includes(name) ? fakeExtension(name) : undefined),
|
|
38
|
+
tool: (name) => (tools.includes(name) ? fakeTool(name) : undefined),
|
|
39
|
+
skill: () => undefined,
|
|
40
|
+
mcpServers: [],
|
|
41
|
+
codemodeTools: [],
|
|
42
|
+
unconnected: undefined,
|
|
43
|
+
matchTools: () => [],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const toolsOf = (change: AgentChange): readonly string[] =>
|
|
47
|
+
Array.isArray(change.tools) ? change.tools.map((tool) => tool.name) : [];
|
|
48
|
+
|
|
49
|
+
const scout = presets.find((preset) => preset.name === "scout")!;
|
|
50
|
+
|
|
51
|
+
it.effect("a definition's tools replace the inherited selection", () =>
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const bound = yield* bindToolkit(scout, resolver([CODING_EXTENSION], ["read", "bash"]));
|
|
54
|
+
assert.deepStrictEqual(bound.names.tools, ["read"]);
|
|
55
|
+
assert.deepStrictEqual(bound.names.extensions, [CODING_EXTENSION]);
|
|
56
|
+
assert.deepStrictEqual(toolsOf(bound.change), ["read"]);
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
it.effect("an unresolved name fails and lists the available names", () =>
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
const failure = yield* bindToolkit(scout, resolver([], ["bash"])).pipe(Effect.flip);
|
|
63
|
+
assert.ok(failure instanceof ToolkitUnresolved);
|
|
64
|
+
assert.ok(failure.message.includes(CODING_EXTENSION), failure.message);
|
|
65
|
+
assert.ok(failure.message.includes("bash"), failure.message);
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
it("an inline-only definition gets coding and its own tool subset", async () => {
|
|
70
|
+
const catalogue = createCatalogue();
|
|
71
|
+
const inline: AgentDefinition = {
|
|
72
|
+
name: "nomad",
|
|
73
|
+
role: "audit",
|
|
74
|
+
prompt: { role: "audit" },
|
|
75
|
+
tools: ["read"],
|
|
76
|
+
extensions: ["not-installed"],
|
|
77
|
+
approval: { gates: [{ name: "ship", tools: ["bash"] }] },
|
|
78
|
+
};
|
|
79
|
+
const plan = await Effect.runPromise(
|
|
80
|
+
planLaunch(
|
|
81
|
+
{ kind: "single", agent: "nomad", task: "look", definition: inline },
|
|
82
|
+
catalogue,
|
|
83
|
+
host,
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
assert.deepStrictEqual(plan.definition.extensions, [CODING_EXTENSION]);
|
|
87
|
+
assert.deepStrictEqual(plan.definition.approval.gates, []);
|
|
88
|
+
assert.deepStrictEqual(plan.definition.tools, ["read"]);
|
|
89
|
+
assert.deepStrictEqual(plan.definition.model, host.model);
|
|
90
|
+
assert.strictEqual(plan.definition.thinkingLevel, "low");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("an omitted model takes the host's current model and thinking level", async () => {
|
|
94
|
+
const planned = await Effect.runPromise(
|
|
95
|
+
planLaunch({ kind: "single", agent: "scout", task: "look" }, createCatalogue(), host),
|
|
96
|
+
);
|
|
97
|
+
assert.deepStrictEqual(planned.definition.model, host.model);
|
|
98
|
+
assert.strictEqual(planned.definition.thinkingLevel, host.thinkingLevel);
|
|
99
|
+
|
|
100
|
+
const named = await Effect.runPromise(
|
|
101
|
+
planLaunch(
|
|
102
|
+
{
|
|
103
|
+
kind: "single",
|
|
104
|
+
agent: "scout",
|
|
105
|
+
task: "look",
|
|
106
|
+
definition: {
|
|
107
|
+
name: "scout",
|
|
108
|
+
role: "audit",
|
|
109
|
+
prompt: { role: "audit" },
|
|
110
|
+
model: { provider: "other", modelId: "other-1" },
|
|
111
|
+
thinkingLevel: "high",
|
|
112
|
+
tools: ["read"],
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
createCatalogue(),
|
|
116
|
+
host,
|
|
117
|
+
),
|
|
118
|
+
);
|
|
119
|
+
assert.deepStrictEqual(named.definition.model, { provider: "other", modelId: "other-1" });
|
|
120
|
+
assert.strictEqual(named.definition.thinkingLevel, "high");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("an unknown definition name fails and lists the presets", async () => {
|
|
124
|
+
const failure = await planLaunch(
|
|
125
|
+
{ kind: "single", agent: "nope", task: "look" },
|
|
126
|
+
createCatalogue(),
|
|
127
|
+
host,
|
|
128
|
+
).pipe(Effect.flip, Effect.runPromise);
|
|
129
|
+
for (const part of ["nope", "scout", "builder", "reviewer"]) {
|
|
130
|
+
assert.ok(failure.message.includes(part), failure.message);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it.prop(
|
|
135
|
+
"an inline definition never widens the toolkit or the rules",
|
|
136
|
+
{ pair: definitionPairArbitrary },
|
|
137
|
+
({ pair }) => {
|
|
138
|
+
const catalogue = createCatalogue([pair.registered]);
|
|
139
|
+
const widening = pair.inline.tools.filter((name) => !pair.registered.tools.includes(name));
|
|
140
|
+
const outcome = planLaunch(
|
|
141
|
+
{ kind: "single", agent: pair.registered.name, task: "look", definition: pair.inline },
|
|
142
|
+
catalogue,
|
|
143
|
+
host,
|
|
144
|
+
).pipe(Effect.result, Effect.runSync);
|
|
145
|
+
if (widening.length > 0) {
|
|
146
|
+
assert.ok(Result.isFailure(outcome), "an inline definition with extra tools must be refused");
|
|
147
|
+
assert.ok(outcome.failure instanceof DefinitionWidened);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
assert.ok(Result.isSuccess(outcome));
|
|
151
|
+
const plan = outcome.success.definition;
|
|
152
|
+
assert.deepStrictEqual(plan.extensions, pair.registered.extensions);
|
|
153
|
+
assert.deepStrictEqual(plan.approval, pair.registered.approval);
|
|
154
|
+
assert.deepStrictEqual(plan.tools, pair.inline.tools);
|
|
155
|
+
assert.deepStrictEqual(plan.prompt, pair.registered.prompt);
|
|
156
|
+
},
|
|
157
|
+
{ arbitrary: { runs: PROPERTY_RUNS } },
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
it("an inline definition that adds a registered tool is refused", async () => {
|
|
161
|
+
const failure = await planLaunch(
|
|
162
|
+
{
|
|
163
|
+
kind: "single",
|
|
164
|
+
agent: "scout",
|
|
165
|
+
task: "look",
|
|
166
|
+
definition: {
|
|
167
|
+
name: "scout",
|
|
168
|
+
role: "audit",
|
|
169
|
+
prompt: { role: "audit" },
|
|
170
|
+
tools: ["read", "bash"],
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
createCatalogue(),
|
|
174
|
+
host,
|
|
175
|
+
).pipe(Effect.flip, Effect.runPromise);
|
|
176
|
+
assert.ok(failure instanceof DefinitionWidened);
|
|
177
|
+
assert.ok(failure.message.includes("bash"), failure.message);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it.effect("a definition that selects skills and declares them names the extension once", () =>
|
|
181
|
+
Effect.gen(function* () {
|
|
182
|
+
const definition: AgentDefinition = {
|
|
183
|
+
...scout,
|
|
184
|
+
extensions: [CODING_EXTENSION, SKILLS_EXTENSION],
|
|
185
|
+
skills: ["alpha"],
|
|
186
|
+
};
|
|
187
|
+
const bound = yield* bindToolkit(definition, {
|
|
188
|
+
...resolver([CODING_EXTENSION, SKILLS_EXTENSION], ["read"]),
|
|
189
|
+
availableSkills: ["alpha"],
|
|
190
|
+
skill: (name) => (name === "alpha" ? ({} as never) : undefined),
|
|
191
|
+
});
|
|
192
|
+
assert.deepStrictEqual(bound.names.extensions, [CODING_EXTENSION, SKILLS_EXTENSION]);
|
|
193
|
+
assert.deepStrictEqual(bound.names.skills, ["alpha"]);
|
|
194
|
+
}),
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
it("a stored name that no longer resolves is reported as missing", () => {
|
|
198
|
+
const missing = resolveStoredToolkit(
|
|
199
|
+
{
|
|
200
|
+
extensions: ["coding-tools", "gone-extension"],
|
|
201
|
+
tools: ["read", "gone-tool"],
|
|
202
|
+
} as unknown as Parameters<typeof resolveStoredToolkit>[0],
|
|
203
|
+
resolver(["coding-tools"], ["read"]),
|
|
204
|
+
).missing;
|
|
205
|
+
assert.deepStrictEqual(missing, ["gone-extension", "gone-tool"]);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("the tool names expand as a Result when a pattern matches no tool", () => {
|
|
209
|
+
const result = expandToolNames(["mcp__missing__*"], resolver([], [])) as {
|
|
210
|
+
readonly _tag?: string;
|
|
211
|
+
readonly failure?: ToolkitUnresolved;
|
|
212
|
+
};
|
|
213
|
+
assert.strictEqual(result._tag, "Failure");
|
|
214
|
+
assert.ok(result.failure?.message.includes("mcp__missing__*"), result.failure?.message);
|
|
215
|
+
});
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import {
|
|
2
|
+
configure,
|
|
3
|
+
defineDoc,
|
|
4
|
+
type AgentChange,
|
|
5
|
+
type AgentState,
|
|
6
|
+
type ConversationId,
|
|
7
|
+
type ConversationRecord,
|
|
8
|
+
type Extension,
|
|
9
|
+
type JsonObject,
|
|
10
|
+
type TaskId,
|
|
11
|
+
type ToolRegistration,
|
|
12
|
+
type Tx,
|
|
13
|
+
} from "@earendil-works/pi-durable";
|
|
14
|
+
import { Effect, Result, Schema } from "effect";
|
|
15
|
+
import { CODEMODE_TOOL_NAME } from "@earendil-works/pi-durable/codemode";
|
|
16
|
+
import type { Skill } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import {
|
|
18
|
+
CODEMODE_EXTENSION,
|
|
19
|
+
GATE_GUARD_EXTENSION,
|
|
20
|
+
RUNS_EXTENSION,
|
|
21
|
+
RUN_TOOL_NAMES,
|
|
22
|
+
SKILLS_EXTENSION,
|
|
23
|
+
TOOL_SEARCH_EXTENSION,
|
|
24
|
+
} from "./names.ts";
|
|
25
|
+
import type { AgentDefinition } from "./definitions.ts";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One gate rule. A gate parks the matching tool call before it runs, until a
|
|
29
|
+
* `delegate_control` approve or deny decides it.
|
|
30
|
+
*
|
|
31
|
+
* Rules belong to a registered definition. Presets carry none, and an inline
|
|
32
|
+
* definition cannot set one.
|
|
33
|
+
*/
|
|
34
|
+
export const ApprovalRule = Schema.Struct({
|
|
35
|
+
/**
|
|
36
|
+
* The gate name. It is unique within a run, so one decision covers this name
|
|
37
|
+
* in every child of the run. Two definitions with the same gate name share
|
|
38
|
+
* one decision.
|
|
39
|
+
*/
|
|
40
|
+
name: Schema.String,
|
|
41
|
+
/**
|
|
42
|
+
* Tool names this rule watches, for example `["bash"]`. Each name must
|
|
43
|
+
* resolve at launch. A name outside this list never parks.
|
|
44
|
+
*/
|
|
45
|
+
tools: Schema.Array(Schema.String),
|
|
46
|
+
/**
|
|
47
|
+
* Case-sensitive substring of the raw `bash` command. It applies to `bash`
|
|
48
|
+
* only: while it is set, another listed tool never matches. Omit it to park
|
|
49
|
+
* every call of a listed tool.
|
|
50
|
+
*/
|
|
51
|
+
match: Schema.optional(Schema.String),
|
|
52
|
+
});
|
|
53
|
+
export type ApprovalRule = typeof ApprovalRule.Type;
|
|
54
|
+
|
|
55
|
+
/** A `Schema.Struct`, so the type, the document encoding and the arbitrary have one source. */
|
|
56
|
+
export const ToolkitRecord = Schema.Struct({
|
|
57
|
+
definition: Schema.String,
|
|
58
|
+
/** The launch run that owns gates: this run or a fanout parent. */
|
|
59
|
+
runId: Schema.String,
|
|
60
|
+
/** The run task that owns this run's children: the task a scripted child is owned by. */
|
|
61
|
+
runTaskId: Schema.Finite,
|
|
62
|
+
/** The child run whose conversation owns this toolkit. */
|
|
63
|
+
childRunId: Schema.String,
|
|
64
|
+
approval: Schema.Struct({ gates: Schema.Array(ApprovalRule) }),
|
|
65
|
+
/** The registered definition's declared skills, which the prompt section renders. */
|
|
66
|
+
skills: Schema.Array(Schema.String),
|
|
67
|
+
});
|
|
68
|
+
export type ToolkitRecord = typeof ToolkitRecord.Type;
|
|
69
|
+
|
|
70
|
+
const VERSION_WITH_SKILLS = 2;
|
|
71
|
+
const VERSION_WITH_RUN_TASK = 3;
|
|
72
|
+
|
|
73
|
+
/** Two migrations: version 1 predates skills, versions below 3 predate the run task id. */
|
|
74
|
+
export const migrateToolkit = (
|
|
75
|
+
value: JsonObject,
|
|
76
|
+
fromVersion: number,
|
|
77
|
+
): ToolkitRecord & JsonObject =>
|
|
78
|
+
({
|
|
79
|
+
...value,
|
|
80
|
+
...(fromVersion < VERSION_WITH_SKILLS ? { skills: [] } : {}),
|
|
81
|
+
...(fromVersion < VERSION_WITH_RUN_TASK ? { runTaskId: 0 } : {}),
|
|
82
|
+
}) as unknown as ToolkitRecord & JsonObject;
|
|
83
|
+
|
|
84
|
+
export const ToolkitDoc = defineDoc<ToolkitRecord & JsonObject>({
|
|
85
|
+
kind: "delegates.toolkit",
|
|
86
|
+
version: VERSION_WITH_RUN_TASK,
|
|
87
|
+
scope: "conversation",
|
|
88
|
+
history: "latest",
|
|
89
|
+
fork: "current",
|
|
90
|
+
initial: () => ({
|
|
91
|
+
definition: "",
|
|
92
|
+
runId: "",
|
|
93
|
+
runTaskId: 0,
|
|
94
|
+
childRunId: "",
|
|
95
|
+
approval: { gates: [] },
|
|
96
|
+
skills: [],
|
|
97
|
+
}),
|
|
98
|
+
migrate: migrateToolkit,
|
|
99
|
+
checkpointWhen: () => true,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export class ToolkitUnresolved extends Schema.TaggedError<
|
|
103
|
+
ToolkitUnresolved,
|
|
104
|
+
{ readonly brand: unique symbol }
|
|
105
|
+
>()("ToolkitUnresolved", { message: Schema.String }) {}
|
|
106
|
+
|
|
107
|
+
export interface ToolkitResolver {
|
|
108
|
+
readonly availableExtensions: readonly string[];
|
|
109
|
+
readonly availableTools: readonly string[];
|
|
110
|
+
readonly availableSkills: readonly string[];
|
|
111
|
+
readonly extension: (name: string) => Extension | undefined;
|
|
112
|
+
readonly tool: (name: string) => ToolRegistration | undefined;
|
|
113
|
+
readonly skill: (name: string) => Skill | undefined;
|
|
114
|
+
/** The connected MCP servers, derived from the snapshot's `mcp:*` extensions. */
|
|
115
|
+
readonly mcpServers: readonly McpServerTools[];
|
|
116
|
+
/** Every MCP tool whose effective exposure is `codemode`: selecting one selects `codemode`. */
|
|
117
|
+
readonly codemodeTools: readonly string[];
|
|
118
|
+
/** Configured but not connected; `undefined` means this runtime has no MCP layer. */
|
|
119
|
+
readonly unconnected: readonly string[] | undefined;
|
|
120
|
+
/** Every available tool name a `mcp__<server>__<prefix>*` pattern selects. */
|
|
121
|
+
readonly matchTools: (pattern: string) => readonly string[];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** One connected server as the binder sees it: its extension, namespace and tool names. */
|
|
125
|
+
export interface McpServerTools {
|
|
126
|
+
readonly extension: string;
|
|
127
|
+
readonly namespace: string;
|
|
128
|
+
readonly tools: readonly string[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface BoundToolkit {
|
|
132
|
+
/** Extensions and tools as objects, written by `configure`. */
|
|
133
|
+
readonly change: AgentChange;
|
|
134
|
+
/** What `pi.agent` records: names, not objects. */
|
|
135
|
+
readonly names: {
|
|
136
|
+
readonly extensions: readonly string[];
|
|
137
|
+
readonly tools: readonly string[];
|
|
138
|
+
readonly skills: readonly string[];
|
|
139
|
+
};
|
|
140
|
+
readonly approval: { readonly gates: readonly ApprovalRule[] };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const GATE_GUARD = GATE_GUARD_EXTENSION;
|
|
144
|
+
|
|
145
|
+
/** One sentence of a refusal message; an empty unresolved list is omitted. */
|
|
146
|
+
const sentence = (label: string, names: readonly string[]): string =>
|
|
147
|
+
names.length === 0 ? "" : `${label}: ${names.join(", ")}.`;
|
|
148
|
+
|
|
149
|
+
/** One sentence of a refusal message; an empty available list still says `(none)`. */
|
|
150
|
+
const available = (label: string, names: readonly string[]): string =>
|
|
151
|
+
`${label}: ${names.join(", ") || "(none)"}.`;
|
|
152
|
+
|
|
153
|
+
/** A pattern must name its server: `mcp__<server>__*` or `mcp__<server>__<prefix>*`. */
|
|
154
|
+
const MCP_PATTERN = /^mcp__[A-Za-z0-9_]+__[A-Za-z0-9_]*\*$/;
|
|
155
|
+
|
|
156
|
+
const namespacesOf = (resolver: ToolkitResolver): string =>
|
|
157
|
+
resolver.mcpServers.map((server) => server.namespace).join(", ") || "(none)";
|
|
158
|
+
|
|
159
|
+
/** A pattern outside the grammar is refused before anything is resolved. */
|
|
160
|
+
const grammarFailure = (pattern: string, resolver: ToolkitResolver): ToolkitUnresolved =>
|
|
161
|
+
ToolkitUnresolved.make({
|
|
162
|
+
message:
|
|
163
|
+
`Only "mcp__<server>__*" patterns are allowed: ${pattern}. ` +
|
|
164
|
+
`Available namespaces: ${namespacesOf(resolver)}.`,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
/** A pattern that matches nothing names what this runtime has, and what is configured but down. */
|
|
168
|
+
const patternMessage = (pattern: string, resolver: ToolkitResolver): string => {
|
|
169
|
+
const server = resolver.mcpServers.find((entry) => pattern.startsWith(`${entry.namespace}__`));
|
|
170
|
+
const parts = [
|
|
171
|
+
server === undefined
|
|
172
|
+
? `No MCP server matches ${pattern}.`
|
|
173
|
+
: `No tool of ${server.namespace} matches ${pattern}.`,
|
|
174
|
+
`Available namespaces: ${namespacesOf(resolver)}.`,
|
|
175
|
+
...(server === undefined
|
|
176
|
+
? []
|
|
177
|
+
: [`The ${server.namespace} server offers: ${server.tools.join(", ") || "(none)"}.`]),
|
|
178
|
+
];
|
|
179
|
+
const unconnected = resolver.unconnected ?? [];
|
|
180
|
+
if (unconnected.length > 0) {
|
|
181
|
+
parts.push(`Configured but not connected: ${unconnected.join(", ")}.`);
|
|
182
|
+
}
|
|
183
|
+
return parts.join(" ");
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/** Expands the declared names: a plain name stays exact, a pattern must be an MCP pattern. */
|
|
187
|
+
export const expandToolNames = (
|
|
188
|
+
declared: readonly string[],
|
|
189
|
+
resolver: ToolkitResolver,
|
|
190
|
+
): Result.Result<readonly string[], ToolkitUnresolved> => {
|
|
191
|
+
const expanded: string[] = [];
|
|
192
|
+
for (const name of declared) {
|
|
193
|
+
if (!name.includes("*")) {
|
|
194
|
+
expanded.push(name);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (!MCP_PATTERN.test(name)) {
|
|
198
|
+
return Result.fail(grammarFailure(name, resolver));
|
|
199
|
+
}
|
|
200
|
+
const matched = resolver.matchTools(name);
|
|
201
|
+
if (matched.length === 0) {
|
|
202
|
+
return Result.fail(ToolkitUnresolved.make({ message: patternMessage(name, resolver) }));
|
|
203
|
+
}
|
|
204
|
+
expanded.push(...matched);
|
|
205
|
+
}
|
|
206
|
+
return Result.succeed(expanded);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Resolves names to registry objects; the names go into `pi.agent` and
|
|
211
|
+
* `ToolkitDoc`. Extensions are the definition's selection plus `skills` when it
|
|
212
|
+
* declares skills and `gate-guard` when its rules are non-empty.
|
|
213
|
+
*/
|
|
214
|
+
export const bindToolkit = (
|
|
215
|
+
definition: AgentDefinition,
|
|
216
|
+
resolver: ToolkitResolver,
|
|
217
|
+
): Effect.Effect<BoundToolkit, ToolkitUnresolved> => {
|
|
218
|
+
const skills = definition.skills ?? [];
|
|
219
|
+
const codemode = definition.codemode === "runs";
|
|
220
|
+
const reserved = definition.extensions.filter(
|
|
221
|
+
(name) => name === CODEMODE_EXTENSION || name === RUNS_EXTENSION,
|
|
222
|
+
);
|
|
223
|
+
if (reserved.length > 0) {
|
|
224
|
+
return Effect.fail(
|
|
225
|
+
ToolkitUnresolved.make({
|
|
226
|
+
message: `Extension "${reserved[0]}" is reserved; set codemode: "runs" on the definition instead.`,
|
|
227
|
+
}),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
const expansion = expandToolNames(definition.tools, resolver);
|
|
231
|
+
if (Result.isFailure(expansion)) return Effect.fail(expansion.failure);
|
|
232
|
+
const expanded = expansion.success;
|
|
233
|
+
// A codemode-exposed tool needs the codemode extension; a deferred one needs `tool_search`.
|
|
234
|
+
const needsCodemode = expanded.some((name) => resolver.codemodeTools.includes(name));
|
|
235
|
+
const needsToolSearch = expanded.some((name) => resolver.tool(name)?.deferred ?? false);
|
|
236
|
+
const mcpExtensions = resolver.mcpServers
|
|
237
|
+
.filter((server) => server.tools.some((tool) => expanded.includes(tool)))
|
|
238
|
+
.map((server) => server.extension);
|
|
239
|
+
const extensionNames = [
|
|
240
|
+
...new Set([
|
|
241
|
+
...definition.extensions,
|
|
242
|
+
...(skills.length > 0 && !definition.extensions.includes(SKILLS_EXTENSION)
|
|
243
|
+
? [SKILLS_EXTENSION]
|
|
244
|
+
: []),
|
|
245
|
+
...(codemode ? [CODEMODE_EXTENSION, RUNS_EXTENSION] : []),
|
|
246
|
+
...(needsCodemode && !codemode ? [CODEMODE_EXTENSION] : []),
|
|
247
|
+
...(definition.approval.gates.length > 0 ? [GATE_GUARD] : []),
|
|
248
|
+
...mcpExtensions,
|
|
249
|
+
...(needsToolSearch ? [TOOL_SEARCH_EXTENSION] : []),
|
|
250
|
+
]),
|
|
251
|
+
];
|
|
252
|
+
const missingExtensions: string[] = [];
|
|
253
|
+
const extensions: Extension[] = [];
|
|
254
|
+
for (const name of extensionNames) {
|
|
255
|
+
const extension = resolver.extension(name);
|
|
256
|
+
if (extension === undefined) missingExtensions.push(name);
|
|
257
|
+
else extensions.push(extension);
|
|
258
|
+
}
|
|
259
|
+
const missingTools: string[] = [];
|
|
260
|
+
const tools: ToolRegistration[] = [];
|
|
261
|
+
const enabled = [...expanded, ...(codemode ? [CODEMODE_TOOL_NAME, ...RUN_TOOL_NAMES] : [])];
|
|
262
|
+
for (const name of enabled) {
|
|
263
|
+
const tool = resolver.tool(name);
|
|
264
|
+
if (tool === undefined) missingTools.push(name);
|
|
265
|
+
else tools.push(tool);
|
|
266
|
+
}
|
|
267
|
+
const missingSkills = skills.filter((name) => resolver.skill(name) === undefined);
|
|
268
|
+
if (missingExtensions.length > 0 || missingTools.length > 0 || missingSkills.length > 0) {
|
|
269
|
+
return Effect.fail(
|
|
270
|
+
ToolkitUnresolved.make({
|
|
271
|
+
message: [
|
|
272
|
+
sentence("Unresolved extensions", missingExtensions),
|
|
273
|
+
sentence("Unresolved tools", missingTools),
|
|
274
|
+
sentence("Unresolved skills", missingSkills),
|
|
275
|
+
available("Available extensions", resolver.availableExtensions),
|
|
276
|
+
available("Available tools", resolver.availableTools),
|
|
277
|
+
available("Available skills", resolver.availableSkills),
|
|
278
|
+
]
|
|
279
|
+
.filter((part) => part.length > 0)
|
|
280
|
+
.join(" "),
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return Effect.succeed({
|
|
286
|
+
change: {
|
|
287
|
+
extensions,
|
|
288
|
+
tools,
|
|
289
|
+
...(definition.model === undefined ? {} : { model: definition.model }),
|
|
290
|
+
...(definition.thinkingLevel === undefined
|
|
291
|
+
? {}
|
|
292
|
+
: { thinkingLevel: definition.thinkingLevel }),
|
|
293
|
+
instructions: instructionsFor(definition),
|
|
294
|
+
...(definition.cwd === undefined ? {} : { cwd: definition.cwd }),
|
|
295
|
+
},
|
|
296
|
+
names: { extensions: extensionNames, tools: enabled, skills },
|
|
297
|
+
approval: definition.approval,
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export const instructionsFor = (definition: AgentDefinition): string =>
|
|
302
|
+
[definition.prompt.shared, definition.prompt.role]
|
|
303
|
+
.filter((part): part is string => part !== undefined && part.length > 0)
|
|
304
|
+
.join("\n\n");
|
|
305
|
+
|
|
306
|
+
/** Both records in the commit that creates the conversation. */
|
|
307
|
+
export const createBoundChild = async (
|
|
308
|
+
tx: Tx,
|
|
309
|
+
runId: string,
|
|
310
|
+
childRunId: string,
|
|
311
|
+
taskId: TaskId,
|
|
312
|
+
bound: BoundToolkit,
|
|
313
|
+
definition: AgentDefinition,
|
|
314
|
+
maxTurns: number,
|
|
315
|
+
): Promise<ConversationRecord> => {
|
|
316
|
+
const record = await tx.createConversation({ ownership: { kind: "task", taskId } });
|
|
317
|
+
await configure(tx, record.id, { ...bound.change, maxTurns });
|
|
318
|
+
const doc = await tx.doc(ToolkitDoc, record.id);
|
|
319
|
+
doc.definition = definition.name;
|
|
320
|
+
doc.runId = runId;
|
|
321
|
+
doc.runTaskId = taskId;
|
|
322
|
+
doc.childRunId = childRunId;
|
|
323
|
+
doc.approval = {
|
|
324
|
+
gates: bound.approval.gates.map((rule) => ({
|
|
325
|
+
name: rule.name,
|
|
326
|
+
tools: [...rule.tools],
|
|
327
|
+
...(rule.match === undefined ? {} : { match: rule.match }),
|
|
328
|
+
})),
|
|
329
|
+
};
|
|
330
|
+
doc.skills = [...bound.names.skills];
|
|
331
|
+
return record;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/** The names a stored agent records that the current registry and catalogue cannot resolve. */
|
|
335
|
+
export const resolveStoredToolkit = (
|
|
336
|
+
state: AgentState | undefined,
|
|
337
|
+
resolver: ToolkitResolver,
|
|
338
|
+
skills: readonly string[] = [],
|
|
339
|
+
): {
|
|
340
|
+
readonly missing: readonly string[];
|
|
341
|
+
readonly available: {
|
|
342
|
+
readonly extensions: readonly string[];
|
|
343
|
+
readonly tools: readonly string[];
|
|
344
|
+
};
|
|
345
|
+
} => {
|
|
346
|
+
const missing: string[] = [];
|
|
347
|
+
// A read-only runtime connects to nothing, so it cannot verify an MCP name.
|
|
348
|
+
const verifyMcp = resolver.unconnected !== undefined;
|
|
349
|
+
if (Array.isArray(state?.extensions)) {
|
|
350
|
+
for (const name of state.extensions) {
|
|
351
|
+
if (!verifyMcp && name.startsWith("mcp:")) continue;
|
|
352
|
+
if (resolver.extension(name) === undefined) missing.push(name);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (Array.isArray(state?.tools)) {
|
|
356
|
+
for (const name of state.tools) {
|
|
357
|
+
if (!verifyMcp && name.startsWith("mcp__")) continue;
|
|
358
|
+
if (resolver.tool(name) === undefined) missing.push(name);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
for (const name of skills) {
|
|
362
|
+
if (resolver.skill(name) === undefined) missing.push(name);
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
missing,
|
|
366
|
+
available: {
|
|
367
|
+
extensions: resolver.availableExtensions,
|
|
368
|
+
tools: resolver.availableTools,
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/** A conversation id as a plain number, for documents and views. */
|
|
374
|
+
export const conversationNumber = (id: ConversationId): number => id;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { resolveMaxTurns } from "./turn-budget.ts";
|
|
3
|
+
|
|
4
|
+
const full = {
|
|
5
|
+
child: 60,
|
|
6
|
+
request: 80,
|
|
7
|
+
definition: 100,
|
|
8
|
+
configDefault: 120,
|
|
9
|
+
ceiling: 400,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
it("the resolver takes the first present level", () => {
|
|
13
|
+
assert.strictEqual(resolveMaxTurns(full), 60);
|
|
14
|
+
assert.strictEqual(resolveMaxTurns({ ...full, child: undefined }), 80);
|
|
15
|
+
assert.strictEqual(resolveMaxTurns({ ...full, child: undefined, request: undefined }), 100);
|
|
16
|
+
assert.strictEqual(
|
|
17
|
+
resolveMaxTurns({
|
|
18
|
+
configDefault: full.configDefault,
|
|
19
|
+
ceiling: full.ceiling,
|
|
20
|
+
}),
|
|
21
|
+
120,
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("a value above the ceiling clamps silently", () => {
|
|
26
|
+
const resolved = resolveMaxTurns({ child: 9_000, configDefault: 120, ceiling: 400 });
|
|
27
|
+
assert.strictEqual(resolved, 400);
|
|
28
|
+
assert.strictEqual(typeof resolved, "number");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("an unusable budget floors at one round", () => {
|
|
32
|
+
// The wire accepts any finite number; a zero or negative budget would end the
|
|
33
|
+
// child before its first round.
|
|
34
|
+
assert.strictEqual(resolveMaxTurns({ child: 0, configDefault: 120, ceiling: 400 }), 1);
|
|
35
|
+
assert.strictEqual(resolveMaxTurns({ child: -5, configDefault: 120, ceiling: 400 }), 1);
|
|
36
|
+
assert.strictEqual(resolveMaxTurns({ request: 0, configDefault: 120, ceiling: 400 }), 1);
|
|
37
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The smallest usable budget: one round. */
|
|
2
|
+
export const MIN_MAX_TURNS = 1;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* One rule resolves the four levels once per child: child, then request, then
|
|
6
|
+
* definition, then the config default; the first present value wins. The result
|
|
7
|
+
* is clamped silently to `caps.maxTurns`, and floors at one round: a wire value
|
|
8
|
+
* of zero or less would end a child before its first tool round.
|
|
9
|
+
*/
|
|
10
|
+
export const resolveMaxTurns = (input: {
|
|
11
|
+
readonly child?: number | undefined;
|
|
12
|
+
readonly request?: number | undefined;
|
|
13
|
+
readonly definition?: number | undefined;
|
|
14
|
+
readonly configDefault: number;
|
|
15
|
+
readonly ceiling: number;
|
|
16
|
+
}): number =>
|
|
17
|
+
Math.min(
|
|
18
|
+
Math.max(
|
|
19
|
+
input.child ?? input.request ?? input.definition ?? input.configDefault,
|
|
20
|
+
MIN_MAX_TURNS,
|
|
21
|
+
),
|
|
22
|
+
input.ceiling,
|
|
23
|
+
);
|