@ftrdotdev/pi-delegates 0.0.0-stage → 0.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/LICENSE +21 -0
  3. package/OPERATIONS.md +57 -0
  4. package/README.md +448 -2
  5. package/ROADMAP.md +158 -0
  6. package/TESTING.md +1061 -0
  7. package/agents.ts +8 -0
  8. package/examples/README.md +114 -0
  9. package/examples/gated.ts +28 -0
  10. package/index.ts +9 -0
  11. package/package.json +93 -4
  12. package/skills/pi-delegates/SKILL.md +147 -0
  13. package/skills/pi-delegates/references/control.md +159 -0
  14. package/skills/pi-delegates/references/definitions.md +163 -0
  15. package/skills/pi-delegates/references/gates.md +175 -0
  16. package/skills/pi-delegates/references/launch.md +156 -0
  17. package/src/domain/definitions.test.ts +41 -0
  18. package/src/domain/definitions.ts +161 -0
  19. package/src/domain/ids.ts +13 -0
  20. package/src/domain/names.ts +35 -0
  21. package/src/domain/orchestration/admission.ts +78 -0
  22. package/src/domain/orchestration/control.test.ts +1298 -0
  23. package/src/domain/orchestration/control.ts +436 -0
  24. package/src/domain/orchestration/fanout.test.ts +419 -0
  25. package/src/domain/orchestration/fanout.ts +334 -0
  26. package/src/domain/orchestration/gates.test.ts +686 -0
  27. package/src/domain/orchestration/gates.ts +360 -0
  28. package/src/domain/orchestration/launch.test.ts +173 -0
  29. package/src/domain/orchestration/launch.ts +581 -0
  30. package/src/domain/orchestration/requests.test.ts +700 -0
  31. package/src/domain/orchestration/requests.ts +769 -0
  32. package/src/domain/orchestration/review.test.ts +214 -0
  33. package/src/domain/orchestration/review.ts +168 -0
  34. package/src/domain/orchestration/run-wait.test.ts +262 -0
  35. package/src/domain/orchestration/run-wait.ts +256 -0
  36. package/src/domain/orchestration/workflow-section.test.ts +62 -0
  37. package/src/domain/orchestration/workflow-section.ts +99 -0
  38. package/src/domain/orchestration/workflow.test.ts +1425 -0
  39. package/src/domain/orchestration/workflow.ts +705 -0
  40. package/src/domain/run-records.test.ts +27 -0
  41. package/src/domain/run-records.ts +527 -0
  42. package/src/domain/shapes.test.ts +165 -0
  43. package/src/domain/skills.test.ts +466 -0
  44. package/src/domain/skills.ts +97 -0
  45. package/src/domain/tool-schema.test.ts +46 -0
  46. package/src/domain/tool-schema.ts +20 -0
  47. package/src/domain/toolkit.test.ts +201 -0
  48. package/src/domain/toolkit.ts +379 -0
  49. package/src/domain/view.test.ts +739 -0
  50. package/src/domain/view.ts +459 -0
  51. package/src/example.test.ts +15 -0
  52. package/src/extension/commands.test.ts +481 -0
  53. package/src/extension/commands.ts +227 -0
  54. package/src/extension/entry.test.ts +1118 -0
  55. package/src/extension/entry.ts +342 -0
  56. package/src/extension/gate-alerts.test.ts +235 -0
  57. package/src/extension/gate-alerts.ts +143 -0
  58. package/src/extension/guard.test.ts +48 -0
  59. package/src/extension/guard.ts +31 -0
  60. package/src/extension/host-signals.test.ts +20 -0
  61. package/src/extension/host-signals.ts +13 -0
  62. package/src/extension/inspector-command.test.ts +1191 -0
  63. package/src/extension/inspector-command.ts +638 -0
  64. package/src/extension/prompt.test.ts +110 -0
  65. package/src/extension/prompt.ts +50 -0
  66. package/src/extension/request-alerts.test.ts +183 -0
  67. package/src/extension/request-alerts.ts +181 -0
  68. package/src/extension/revocation.test.ts +81 -0
  69. package/src/extension/revocation.ts +84 -0
  70. package/src/extension/shortcut.test.ts +64 -0
  71. package/src/extension/shortcut.ts +105 -0
  72. package/src/extension/tools.test.ts +993 -0
  73. package/src/extension/tools.ts +505 -0
  74. package/src/extension/version.test.ts +123 -0
  75. package/src/extension/version.ts +97 -0
  76. package/src/harness-policy.test.ts +74 -0
  77. package/src/package-manifest.test.ts +157 -0
  78. package/src/presentation/actions.test.ts +221 -0
  79. package/src/presentation/actions.ts +85 -0
  80. package/src/presentation/activity.test.ts +387 -0
  81. package/src/presentation/activity.ts +66 -0
  82. package/src/presentation/close.test.ts +361 -0
  83. package/src/presentation/inspector.test.ts +679 -0
  84. package/src/presentation/inspector.ts +440 -0
  85. package/src/presentation/overlay.test.ts +555 -0
  86. package/src/presentation/overlay.ts +229 -0
  87. package/src/presentation/repaint.test.ts +329 -0
  88. package/src/presentation/shortcut.ts +2 -0
  89. package/src/presentation/snapshot.test.ts +530 -0
  90. package/src/presentation/snapshot.ts +306 -0
  91. package/src/presentation/strip.test.ts +196 -0
  92. package/src/presentation/strip.ts +74 -0
  93. package/src/presentation/wording.test.ts +238 -0
  94. package/src/presentation/wording.ts +222 -0
  95. package/src/runtime/bridge.test.ts +131 -0
  96. package/src/runtime/bridge.ts +134 -0
  97. package/src/runtime/config.test.ts +44 -0
  98. package/src/runtime/config.ts +87 -0
  99. package/src/runtime/context.test.ts +19 -0
  100. package/src/runtime/context.ts +9 -0
  101. package/src/runtime/execution-env.test.ts +35 -0
  102. package/src/runtime/execution-env.ts +15 -0
  103. package/src/runtime/extensions.test.ts +86 -0
  104. package/src/runtime/extensions.ts +140 -0
  105. package/src/runtime/log.test.ts +128 -0
  106. package/src/runtime/log.ts +68 -0
  107. package/src/runtime/mcp.test.ts +1453 -0
  108. package/src/runtime/mcp.ts +938 -0
  109. package/src/runtime/models.test.ts +145 -0
  110. package/src/runtime/models.ts +259 -0
  111. package/src/runtime/observe.test.ts +50 -0
  112. package/src/runtime/observe.ts +150 -0
  113. package/src/runtime/session.test.ts +359 -0
  114. package/src/runtime/session.ts +425 -0
  115. package/src/runtime/timers.test.ts +31 -0
  116. package/src/runtime/timers.ts +10 -0
@@ -0,0 +1,97 @@
1
+ import { formatSkillsForPrompt, loadSkills, type Skill } from "@earendil-works/pi-coding-agent";
2
+ import { Effect } from "effect";
3
+
4
+ /** Where Pi finds skills for one session. */
5
+ export interface SkillRoots {
6
+ readonly cwd: string;
7
+ readonly agentDir: string;
8
+ readonly skillPaths: readonly string[];
9
+ }
10
+
11
+ /** `code` is stable (`skill.<problem>`); `detail` names the file. */
12
+ export interface SkillDiagnostic {
13
+ readonly code: string;
14
+ readonly detail: string;
15
+ }
16
+
17
+ export interface SkillsService {
18
+ readonly all: () => readonly Skill[];
19
+ readonly find: (name: string) => Skill | undefined;
20
+ readonly resolve: (names: readonly string[]) => readonly Skill[];
21
+ readonly render: (skills: readonly Skill[]) => string;
22
+ /** Scan problems and skills the live catalogue dropped. */
23
+ readonly diagnostics: () => readonly SkillDiagnostic[];
24
+ /** Re-scans the roots; bodies stay uncached. */
25
+ readonly reload: Effect.Effect<void>;
26
+ }
27
+
28
+ const NONE = "(none)";
29
+
30
+ const namesOf = (skills: readonly Skill[]): string =>
31
+ skills.map((skill) => skill.name).join(", ") || NONE;
32
+
33
+ interface Scan {
34
+ readonly skills: readonly Skill[];
35
+ readonly diagnostics: readonly SkillDiagnostic[];
36
+ }
37
+
38
+ /** One pass over Pi's roots. It caches the file list, never a body. */
39
+ const scan = (roots: SkillRoots | undefined): Scan => {
40
+ if (roots === undefined) return { skills: [], diagnostics: [] };
41
+ const result = loadSkills({
42
+ cwd: roots.cwd,
43
+ agentDir: roots.agentDir,
44
+ skillPaths: [...roots.skillPaths],
45
+ includeDefaults: true,
46
+ });
47
+ return {
48
+ skills: result.skills,
49
+ diagnostics: result.diagnostics.map((entry) => ({
50
+ code: `skill.${entry.type}`,
51
+ detail: `${entry.path ?? "skills"}: ${entry.message}`,
52
+ })),
53
+ };
54
+ };
55
+
56
+ /**
57
+ * The accessors read a closure-held list, so the durable section and tool
58
+ * callbacks stay free of an Effect run; only `reload` scans.
59
+ */
60
+ const serviceFor = (roots: SkillRoots | undefined): SkillsService => {
61
+ let scanned = scan(roots);
62
+ let dropped: SkillDiagnostic[] = [];
63
+ const all = (): readonly Skill[] => scanned.skills;
64
+ const find = (name: string): Skill | undefined =>
65
+ scanned.skills.find((skill) => skill.name === name);
66
+ const resolve = (names: readonly string[]): readonly Skill[] => {
67
+ const found: Skill[] = [];
68
+ for (const name of names) {
69
+ const skill = find(name);
70
+ if (skill !== undefined) {
71
+ found.push(skill);
72
+ continue;
73
+ }
74
+ const detail = `Skill "${name}" is not in the catalogue. Available: ${namesOf(all())}.`;
75
+ if (!dropped.some((entry) => entry.detail === detail)) {
76
+ dropped.push({ code: "skill.missing", detail });
77
+ }
78
+ }
79
+ return found;
80
+ };
81
+ const render = (skills: readonly Skill[]): string => formatSkillsForPrompt([...skills]);
82
+ const diagnostics = (): readonly SkillDiagnostic[] => [...dropped, ...scanned.diagnostics];
83
+ const reload = Effect.sync(() => {
84
+ scanned = scan(roots);
85
+ dropped = [];
86
+ });
87
+ return { all, find, resolve, render, diagnostics, reload };
88
+ };
89
+
90
+ /**
91
+ * One catalogue per runtime; per-conversation restriction comes from `ToolkitDoc.skills`.
92
+ * The scan is synchronous, so the runtime holds the service directly: no Layer to unwrap.
93
+ */
94
+ export const createSkills = (roots: SkillRoots | undefined): SkillsService => serviceFor(roots);
95
+
96
+ /** A catalogue over no roots: viewers and tests that never resolve a skill. */
97
+ export const emptySkills = (): SkillsService => serviceFor(undefined);
@@ -0,0 +1,46 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
3
+ import { Arbitrary, Schema } from "effect";
4
+ import { throws } from "../../test-support/assertions.ts";
5
+ import { decode, declared } from "./tool-schema.ts";
6
+
7
+ /** A struct whose encoded side differs from its type: the seam must decode, not pass through. */
8
+ const Counted = Schema.Struct({
9
+ count: Schema.FiniteFromString,
10
+ label: Schema.String.check(Schema.isMaxLength(12)),
11
+ });
12
+
13
+ const countedArbitrary = Arbitrary.schema(
14
+ Schema.Struct({
15
+ count: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
16
+ label: Schema.String.check(Schema.isMaxLength(12)),
17
+ }),
18
+ );
19
+
20
+ it.prop(
21
+ "decode reads back the type that encode wrote as its encoded side",
22
+ { value: countedArbitrary },
23
+ ({ value }) => {
24
+ const encoded = Schema.encodeSync(Counted)(value);
25
+ assert.strictEqual(typeof encoded.count, "string");
26
+ assert.deepStrictEqual(decode(Counted, encoded), value);
27
+ },
28
+ { arbitrary: { runs: PROPERTY_RUNS } },
29
+ );
30
+
31
+ it("the declared document is the object schema the model sees", () => {
32
+ const document = declared(Counted).json as {
33
+ readonly type?: string;
34
+ readonly properties?: Record<string, unknown>;
35
+ };
36
+
37
+ assert.strictEqual(document.type, "object");
38
+ assert.deepStrictEqual(Object.keys(document.properties ?? {}).sort(), ["count", "label"]);
39
+ });
40
+
41
+ it("decode refuses arguments the schema does not accept", () => {
42
+ throws(
43
+ () => decode(Counted, { count: "not-a-number", label: "x" }),
44
+ (error: unknown) => error instanceof Error,
45
+ );
46
+ });
@@ -0,0 +1,20 @@
1
+ import { Schema } from "effect";
2
+
3
+ /** One Effect Schema with the JSON Schema durable validates and the model sees. */
4
+ export interface DeclaredSchema<T> {
5
+ readonly schema: Schema.ConstraintDecoder<T>;
6
+ readonly json: object;
7
+ }
8
+
9
+ /**
10
+ * The tool declaration seam: Effect Schema is the one source. `json` goes to a
11
+ * durable `ToolRegistration`; `decode` reads the validated arguments back.
12
+ */
13
+ export const declared = <T>(schema: Schema.ConstraintDecoder<T>): DeclaredSchema<T> => ({
14
+ schema,
15
+ json: Schema.toJsonSchemaDocument(schema).schema,
16
+ });
17
+
18
+ /** The arguments durable validated, decoded to their TypeScript shape. */
19
+ export const decode = <T>(schema: Schema.ConstraintDecoder<T>, args: unknown): T =>
20
+ Schema.decodeUnknownSync(schema)(args);
@@ -0,0 +1,201 @@
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 { bindToolkit, resolveStoredToolkit, ToolkitUnresolved } from "./toolkit.ts";
14
+ import { planLaunch, type HostDefaults } from "./orchestration/launch.ts";
15
+
16
+ const host: HostDefaults = {
17
+ model: { provider: "stub", modelId: "stub-1" },
18
+ thinkingLevel: "low",
19
+ };
20
+
21
+ const fakeExtension = (name: string): Extension => ({ name, tools: [] });
22
+ const fakeTool = (name: string): ToolRegistration =>
23
+ ({ name, parameters: {} }) as unknown as ToolRegistration;
24
+
25
+ const resolver = (
26
+ extensions: readonly string[],
27
+ tools: readonly string[],
28
+ ): Parameters<typeof bindToolkit>[1] => ({
29
+ availableExtensions: extensions,
30
+ availableTools: tools,
31
+ availableSkills: [],
32
+ extension: (name) => (extensions.includes(name) ? fakeExtension(name) : undefined),
33
+ tool: (name) => (tools.includes(name) ? fakeTool(name) : undefined),
34
+ skill: () => undefined,
35
+ mcpServers: [],
36
+ codemodeTools: [],
37
+ unconnected: undefined,
38
+ matchTools: () => [],
39
+ });
40
+
41
+ const toolsOf = (change: AgentChange): readonly string[] =>
42
+ Array.isArray(change.tools) ? change.tools.map((tool) => tool.name) : [];
43
+
44
+ const scout = presets.find((preset) => preset.name === "scout")!;
45
+
46
+ it.effect("a definition's tools replace the inherited selection", () =>
47
+ Effect.gen(function* () {
48
+ const bound = yield* bindToolkit(scout, resolver([CODING_EXTENSION], ["read", "bash"]));
49
+ assert.deepStrictEqual(bound.names.tools, ["read"]);
50
+ assert.deepStrictEqual(bound.names.extensions, [CODING_EXTENSION]);
51
+ assert.deepStrictEqual(toolsOf(bound.change), ["read"]);
52
+ }),
53
+ );
54
+
55
+ it.effect("an unresolved name fails and lists the available names", () =>
56
+ Effect.gen(function* () {
57
+ const failure = yield* bindToolkit(scout, resolver([], ["bash"])).pipe(Effect.flip);
58
+ assert.ok(failure instanceof ToolkitUnresolved);
59
+ assert.ok(failure.message.includes(CODING_EXTENSION), failure.message);
60
+ assert.ok(failure.message.includes("bash"), failure.message);
61
+ }),
62
+ );
63
+
64
+ it("an inline-only definition gets coding and its own tool subset", async () => {
65
+ const catalogue = createCatalogue();
66
+ const inline: AgentDefinition = {
67
+ name: "nomad",
68
+ role: "audit",
69
+ prompt: { role: "audit" },
70
+ tools: ["read"],
71
+ extensions: ["not-installed"],
72
+ approval: { gates: [{ name: "ship", tools: ["bash"] }] },
73
+ };
74
+ const plan = await Effect.runPromise(
75
+ planLaunch(
76
+ { kind: "single", agent: "nomad", task: "look", definition: inline },
77
+ catalogue,
78
+ host,
79
+ ),
80
+ );
81
+ assert.deepStrictEqual(plan.definition.extensions, [CODING_EXTENSION]);
82
+ assert.deepStrictEqual(plan.definition.approval.gates, []);
83
+ assert.deepStrictEqual(plan.definition.tools, ["read"]);
84
+ assert.deepStrictEqual(plan.definition.model, host.model);
85
+ assert.strictEqual(plan.definition.thinkingLevel, "low");
86
+ });
87
+
88
+ it("an omitted model takes the host's current model and thinking level", async () => {
89
+ const planned = await Effect.runPromise(
90
+ planLaunch({ kind: "single", agent: "scout", task: "look" }, createCatalogue(), host),
91
+ );
92
+ assert.deepStrictEqual(planned.definition.model, host.model);
93
+ assert.strictEqual(planned.definition.thinkingLevel, host.thinkingLevel);
94
+
95
+ const named = await Effect.runPromise(
96
+ planLaunch(
97
+ {
98
+ kind: "single",
99
+ agent: "scout",
100
+ task: "look",
101
+ definition: {
102
+ name: "scout",
103
+ role: "audit",
104
+ prompt: { role: "audit" },
105
+ model: { provider: "other", modelId: "other-1" },
106
+ thinkingLevel: "high",
107
+ tools: ["read"],
108
+ },
109
+ },
110
+ createCatalogue(),
111
+ host,
112
+ ),
113
+ );
114
+ assert.deepStrictEqual(named.definition.model, { provider: "other", modelId: "other-1" });
115
+ assert.strictEqual(named.definition.thinkingLevel, "high");
116
+ });
117
+
118
+ it("an unknown definition name fails and lists the presets", async () => {
119
+ const failure = await planLaunch(
120
+ { kind: "single", agent: "nope", task: "look" },
121
+ createCatalogue(),
122
+ host,
123
+ ).pipe(Effect.flip, Effect.runPromise);
124
+ for (const part of ["nope", "scout", "builder", "reviewer"]) {
125
+ assert.ok(failure.message.includes(part), failure.message);
126
+ }
127
+ });
128
+
129
+ it.prop(
130
+ "an inline definition never widens the toolkit or the rules",
131
+ { pair: definitionPairArbitrary },
132
+ ({ pair }) => {
133
+ const catalogue = createCatalogue([pair.registered]);
134
+ const widening = pair.inline.tools.filter((name) => !pair.registered.tools.includes(name));
135
+ const outcome = planLaunch(
136
+ { kind: "single", agent: pair.registered.name, task: "look", definition: pair.inline },
137
+ catalogue,
138
+ host,
139
+ ).pipe(Effect.result, Effect.runSync);
140
+ if (widening.length > 0) {
141
+ assert.ok(Result.isFailure(outcome), "an inline definition with extra tools must be refused");
142
+ assert.ok(outcome.failure instanceof DefinitionWidened);
143
+ return;
144
+ }
145
+ assert.ok(Result.isSuccess(outcome));
146
+ const plan = outcome.success.definition;
147
+ assert.deepStrictEqual(plan.extensions, pair.registered.extensions);
148
+ assert.deepStrictEqual(plan.approval, pair.registered.approval);
149
+ assert.deepStrictEqual(plan.tools, pair.inline.tools);
150
+ assert.deepStrictEqual(plan.prompt, pair.registered.prompt);
151
+ },
152
+ { arbitrary: { runs: PROPERTY_RUNS } },
153
+ );
154
+
155
+ it("an inline definition that adds a registered tool is refused", async () => {
156
+ const failure = await planLaunch(
157
+ {
158
+ kind: "single",
159
+ agent: "scout",
160
+ task: "look",
161
+ definition: {
162
+ name: "scout",
163
+ role: "audit",
164
+ prompt: { role: "audit" },
165
+ tools: ["read", "bash"],
166
+ },
167
+ },
168
+ createCatalogue(),
169
+ host,
170
+ ).pipe(Effect.flip, Effect.runPromise);
171
+ assert.ok(failure instanceof DefinitionWidened);
172
+ assert.ok(failure.message.includes("bash"), failure.message);
173
+ });
174
+
175
+ it.effect("a definition that selects skills and declares them names the extension once", () =>
176
+ Effect.gen(function* () {
177
+ const definition: AgentDefinition = {
178
+ ...scout,
179
+ extensions: [CODING_EXTENSION, SKILLS_EXTENSION],
180
+ skills: ["alpha"],
181
+ };
182
+ const bound = yield* bindToolkit(definition, {
183
+ ...resolver([CODING_EXTENSION, SKILLS_EXTENSION], ["read"]),
184
+ availableSkills: ["alpha"],
185
+ skill: (name) => (name === "alpha" ? ({} as never) : undefined),
186
+ });
187
+ assert.deepStrictEqual(bound.names.extensions, [CODING_EXTENSION, SKILLS_EXTENSION]);
188
+ assert.deepStrictEqual(bound.names.skills, ["alpha"]);
189
+ }),
190
+ );
191
+
192
+ it("a stored name that no longer resolves is reported as missing", () => {
193
+ const missing = resolveStoredToolkit(
194
+ {
195
+ extensions: ["coding-tools", "gone-extension"],
196
+ tools: ["read", "gone-tool"],
197
+ } as unknown as Parameters<typeof resolveStoredToolkit>[0],
198
+ resolver(["coding-tools"], ["read"]),
199
+ ).missing;
200
+ assert.deepStrictEqual(missing, ["gone-extension", "gone-tool"]);
201
+ });