@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,143 @@
1
+ import type { Detach } from "../runtime/observe.ts";
2
+ import { observeSessionDoc } from "../runtime/observe.ts";
3
+ import { GateDoc, GateIndexDoc } from "../domain/orchestration/gates.ts";
4
+ import { errorFields } from "../runtime/log.ts";
5
+ import type { SessionRuntime } from "../runtime/session.ts";
6
+ import type { GateNotice } from "../presentation/wording.ts";
7
+
8
+ /** A retry only happens while the gate index is absent; once attached, changes drive it. */
9
+ export const GATE_ALERT_RETRY_MS = 1_000;
10
+
11
+ /** The surface gate alerts need, independent of whether the inspector is active. */
12
+ export interface GateAlertHost {
13
+ readonly notifyGate: (gate: GateNotice, shortcut: string) => void;
14
+ readonly readGateAlerts: () => ReadonlySet<string>;
15
+ readonly appendGateAlert: (gateId: string) => void;
16
+ }
17
+
18
+ export interface GateAlertDeps {
19
+ readonly runtime: SessionRuntime;
20
+ readonly host: GateAlertHost;
21
+ readonly shortcut: string;
22
+ }
23
+
24
+ export interface GateAlertWatch {
25
+ readonly close: () => Promise<void>;
26
+ }
27
+
28
+ /**
29
+ * One session-level gate watcher. It starts with the runtime, not with the
30
+ * inspector, so turning the inspector off does not silence an approval. The
31
+ * gate index attaches when it exists; while it is absent, one timer retries.
32
+ * Announced ids are session entries, so a reload does not repeat an alert.
33
+ */
34
+ export const startGateAlerts = (deps: GateAlertDeps): GateAlertWatch => {
35
+ let closed = false;
36
+ let detach: Detach | undefined;
37
+ let retry: unknown;
38
+ let opening: Promise<void> = Promise.resolve();
39
+ let chain: Promise<void> = Promise.resolve();
40
+ const announced = new Set<string>();
41
+
42
+ const log = (error: unknown): void => {
43
+ deps.runtime.log.write("warn", "inspector.gate_alert_failed", errorFields(error));
44
+ };
45
+
46
+ try {
47
+ for (const id of deps.host.readGateAlerts()) announced.add(id);
48
+ } catch (error) {
49
+ log(error);
50
+ }
51
+
52
+ const check = (): Promise<void> => {
53
+ chain = chain
54
+ .then(async () => {
55
+ if (closed) return;
56
+ const index = await deps.runtime.snapshot(GateIndexDoc);
57
+ if (index === undefined) return;
58
+ for (const key of index.keys) {
59
+ if (closed) return;
60
+ const record = await deps.runtime.snapshot(GateDoc, key);
61
+ if (record?.status !== "pending") continue;
62
+ const id = `gate:${record.runId}:${record.gate}`;
63
+ if (announced.has(id)) continue;
64
+ announced.add(id);
65
+ try {
66
+ deps.host.notifyGate(
67
+ {
68
+ id,
69
+ runId: record.runId,
70
+ gate: record.gate,
71
+ childRunIds: [...record.childRunIds],
72
+ },
73
+ deps.shortcut,
74
+ );
75
+ deps.host.appendGateAlert(id);
76
+ } catch (error) {
77
+ log(error);
78
+ }
79
+ }
80
+ })
81
+ .catch(log);
82
+ return chain;
83
+ };
84
+
85
+ const scheduleAttach = (): void => {
86
+ if (closed || retry !== undefined) return;
87
+ retry = deps.runtime.timers.setTimeout(() => {
88
+ retry = undefined;
89
+ attach();
90
+ }, GATE_ALERT_RETRY_MS);
91
+ };
92
+
93
+ const attach = (): void => {
94
+ if (closed) return;
95
+ opening = observeSessionDoc(deps.runtime, GateIndexDoc, {
96
+ advance: () => {
97
+ void check();
98
+ },
99
+ end: (kind, reason) => {
100
+ if (closed) return;
101
+ if (kind === "failed") log(reason ?? "the gate index failed");
102
+ detach = undefined;
103
+ scheduleAttach();
104
+ },
105
+ })
106
+ .then((next) => {
107
+ if (closed) {
108
+ next?.();
109
+ return;
110
+ }
111
+ if (next === undefined) {
112
+ scheduleAttach();
113
+ return;
114
+ }
115
+ detach = next;
116
+ })
117
+ .catch((error) => {
118
+ log(error);
119
+ scheduleAttach();
120
+ });
121
+ };
122
+
123
+ const unsubscribe = deps.runtime.onState((state) => {
124
+ if (state.kind === "closed" || state.kind === "failing") void close();
125
+ });
126
+
127
+ const close = async (): Promise<void> => {
128
+ if (closed) return;
129
+ closed = true;
130
+ unsubscribe();
131
+ if (retry !== undefined) {
132
+ deps.runtime.timers.clearTimeout(retry);
133
+ retry = undefined;
134
+ }
135
+ await opening;
136
+ detach?.();
137
+ detach = undefined;
138
+ await chain;
139
+ };
140
+
141
+ attach();
142
+ return { close };
143
+ };
@@ -0,0 +1,48 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { coexistenceVerdict } from "./guard.ts";
3
+
4
+ const REMOVAL = "pi remove npm:pi-subagents";
5
+
6
+ it("the verdict refuses a tool from pi-subagents", () => {
7
+ const verdict = coexistenceVerdict({
8
+ tools: [{ name: "subagent", source: "npm:pi-subagents" }],
9
+ commands: [],
10
+ });
11
+ assert.strictEqual(verdict.kind, "refused");
12
+ if (verdict.kind !== "refused") return;
13
+ assert.strictEqual(verdict.conflict, "pi-subagents");
14
+ assert.ok(verdict.message.includes("pi-delegates"), verdict.message);
15
+ assert.ok(verdict.message.includes("pi-subagents"), verdict.message);
16
+ assert.ok(verdict.message.includes("enabled"), verdict.message);
17
+ assert.ok(verdict.message.includes(REMOVAL), verdict.message);
18
+ });
19
+
20
+ it("the verdict refuses a command from pi-subagents", () => {
21
+ const verdicts = [
22
+ coexistenceVerdict({
23
+ tools: [],
24
+ commands: [{ name: "subagents", source: "npm:pi-subagents@^0.70.1" }],
25
+ }),
26
+ coexistenceVerdict({
27
+ tools: [],
28
+ commands: [{ name: "subagents", source: "/pkg/node_modules/pi-subagents" }],
29
+ }),
30
+ ];
31
+ for (const verdict of verdicts) {
32
+ assert.strictEqual(verdict.kind, "refused");
33
+ if (verdict.kind !== "refused") continue;
34
+ assert.ok(verdict.message.includes(REMOVAL), verdict.message);
35
+ }
36
+ });
37
+
38
+ it("the verdict clears when only unrelated extensions are loaded", () => {
39
+ const verdict = coexistenceVerdict({
40
+ tools: [{ name: "tracker", source: "npm:@ftrdotdev/pi-tracker" }],
41
+ commands: [{ name: "tracker", source: "npm:@ftrdotdev/pi-tracker" }],
42
+ });
43
+ assert.deepStrictEqual(verdict, { kind: "clear" });
44
+ });
45
+
46
+ it("the verdict clears when no extension is loaded", () => {
47
+ assert.deepStrictEqual(coexistenceVerdict({ tools: [], commands: [] }), { kind: "clear" });
48
+ });
@@ -0,0 +1,31 @@
1
+ export type HostInstallSignals = {
2
+ readonly tools: readonly { readonly name: string; readonly source: string }[];
3
+ readonly commands: readonly { readonly name: string; readonly source: string }[];
4
+ };
5
+
6
+ export type CoexistenceVerdict =
7
+ | { readonly kind: "clear" }
8
+ | { readonly kind: "refused"; readonly conflict: string; readonly message: string };
9
+
10
+ const CONFLICT = "pi-subagents";
11
+
12
+ /** `npm:pi-subagents`, `npm:pi-subagents@^0.70.1`, or a bare path segment. */
13
+ const namesPiSubagents = (source: string): boolean =>
14
+ source.replace(/^npm:/, "").split("@")[0] === CONFLICT || source.includes(`/${CONFLICT}`);
15
+
16
+ /** One pure decision over the loaded host signals. */
17
+ export const coexistenceVerdict = (signals: HostInstallSignals): CoexistenceVerdict => {
18
+ const visible =
19
+ signals.tools.some((tool) => namesPiSubagents(tool.source)) ||
20
+ signals.commands.some((command) => namesPiSubagents(command.source));
21
+
22
+ if (!visible) return { kind: "clear" };
23
+ return {
24
+ kind: "refused",
25
+ conflict: CONFLICT,
26
+ message:
27
+ `@ftrdotdev/pi-delegates cannot load while ${CONFLICT} is enabled: both packages claim the same ` +
28
+ `delegation surface. Disable it for this run, or remove it with pi remove npm:${CONFLICT}, ` +
29
+ `then restart Pi.`,
30
+ };
31
+ };
@@ -0,0 +1,20 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { makeStubPi } from "../../test-support/stub-pi.ts";
3
+ import { readHostSignals } from "./host-signals.ts";
4
+
5
+ it("reads tools and extension commands, not skill or prompt commands", () => {
6
+ const stub = makeStubPi({
7
+ tools: [{ name: "subagent", source: "npm:pi-subagents" }],
8
+ commands: [
9
+ { name: "subagents", source: "npm:pi-subagents" },
10
+ { name: "skill:pi-subagents", source: "npm:pi-subagents", kind: "skill" },
11
+ { name: "review", source: "npm:pi-subagents", kind: "prompt" },
12
+ ],
13
+ });
14
+ stub.activate();
15
+
16
+ assert.deepStrictEqual(readHostSignals(stub.api), {
17
+ tools: [{ name: "subagent", source: "npm:pi-subagents" }],
18
+ commands: [{ name: "subagents", source: "npm:pi-subagents" }],
19
+ });
20
+ });
@@ -0,0 +1,13 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import type { HostInstallSignals } from "./guard.ts";
3
+
4
+ /** Keeps the loaded host reads out of the guard's signature. */
5
+ export const readHostSignals = (pi: ExtensionAPI): HostInstallSignals => ({
6
+ tools: pi.getAllTools().map((tool) => ({ name: tool.name, source: tool.sourceInfo.source })),
7
+ commands: pi
8
+ .getCommands()
9
+ // Skills and prompt templates also appear here; only an extension command
10
+ // can claim the same delegation surface as @ftrdotdev/pi-delegates.
11
+ .filter((command) => command.source === "extension")
12
+ .map((command) => ({ name: command.name, source: command.sourceInfo.source })),
13
+ });