@maheidem/pi-delegate 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.
- package/LICENSE +32 -0
- package/README.md +86 -0
- package/application.ts +507 -0
- package/commands.ts +159 -0
- package/config.ts +216 -0
- package/index.ts +669 -0
- package/mode.ts +327 -0
- package/package.json +68 -0
- package/roles/general.md +41 -0
- package/roles/research.md +46 -0
- package/roles.ts +93 -0
- package/rpc-jsonl.ts +172 -0
- package/run-store.ts +326 -0
- package/runner.ts +771 -0
- package/types.ts +365 -0
- package/ui/running-view.ts +85 -0
- package/ui/settings-panel.ts +393 -0
package/mode.ts
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* delegate — branch-aware strict mode: replay, transitions, allowlist gate.
|
|
3
|
+
*
|
|
4
|
+
* This module owns mode policy only. It never touches child execution and
|
|
5
|
+
* never performs filesystem I/O inside the gate. Every failure path
|
|
6
|
+
* degrades toward restriction, never permissiveness (POL-003).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
DelegateModeEntryV1,
|
|
11
|
+
ModeGateDecision,
|
|
12
|
+
ModeReplayResult,
|
|
13
|
+
ModeResult,
|
|
14
|
+
ModeRuntime,
|
|
15
|
+
ModeTransitionContext,
|
|
16
|
+
SessionEntryLike,
|
|
17
|
+
} from "./types.ts";
|
|
18
|
+
import { DELEGATE_MODE_CUSTOM_TYPE } from "./types.ts";
|
|
19
|
+
|
|
20
|
+
export const STRICT_ALLOWED_TOOLS = new Set(["delegate"]);
|
|
21
|
+
export const STRICT_ACTIVE_TOOLS = ["delegate"];
|
|
22
|
+
|
|
23
|
+
export const STRICT_OVERLAY = [
|
|
24
|
+
"[DELEGATION-MODE OVERLAY — enforced by the delegate extension]",
|
|
25
|
+
"Strict delegation mode is ACTIVE. You are a coordinator only.",
|
|
26
|
+
"- All substantive work (reading, research, inspection, editing, running commands) MUST go through the delegate tool.",
|
|
27
|
+
"- Verification MUST be delegated: delegate a verification task rather than checking directly.",
|
|
28
|
+
"- You may use plain text to clarify scope, coordinate, and synthesize child handoffs.",
|
|
29
|
+
"- When a task is underspecified, ask the user clarifying questions in plain text.",
|
|
30
|
+
"- Never claim to have directly inspected or changed files in this mode; only child results count.",
|
|
31
|
+
"- You cannot disable this mode; only the user can, via /delegate off.",
|
|
32
|
+
].join("\n");
|
|
33
|
+
|
|
34
|
+
// ── Pure replay ──────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
function isModeEntry(entry: unknown): entry is { customType: string; data: unknown } {
|
|
37
|
+
if (!entry || typeof entry !== "object") return false;
|
|
38
|
+
const e = entry as Record<string, unknown>;
|
|
39
|
+
return e.type === "custom" && e.customType === DELEGATE_MODE_CUSTOM_TYPE;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseModeEntry(data: unknown): DelegateModeEntryV1 | null {
|
|
43
|
+
if (!data || typeof data !== "object") return null;
|
|
44
|
+
const d = data as Record<string, unknown>;
|
|
45
|
+
if (d.schemaVersion !== 1) return null;
|
|
46
|
+
if (typeof d.enabled !== "boolean") return null;
|
|
47
|
+
if (typeof d.changedAt !== "string" || !d.changedAt) return null;
|
|
48
|
+
if (d.source !== "command") return null;
|
|
49
|
+
return d as unknown as DelegateModeEntryV1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Replay delegate-mode entries over a root-to-leaf branch (the result of
|
|
54
|
+
* `ctx.sessionManager.getBranch()`). The latest VALID entry wins. Malformed
|
|
55
|
+
* entries are skipped with diagnostics and never imply a permissive
|
|
56
|
+
* transition. An empty branch yields normal mode.
|
|
57
|
+
*/
|
|
58
|
+
export function replayModeEntries(branch: SessionEntryLike[]): ModeReplayResult {
|
|
59
|
+
const diagnostics: string[] = [];
|
|
60
|
+
let modeEnabled = false;
|
|
61
|
+
let sawAny = false;
|
|
62
|
+
|
|
63
|
+
for (const entry of branch) {
|
|
64
|
+
if (!isModeEntry(entry)) continue;
|
|
65
|
+
sawAny = true;
|
|
66
|
+
const parsed = parseModeEntry(entry.data);
|
|
67
|
+
if (!parsed) {
|
|
68
|
+
diagnostics.push(
|
|
69
|
+
`malformed ${DELEGATE_MODE_CUSTOM_TYPE} entry ignored: ${JSON.stringify(entry.data ?? null).slice(0, 200)}`,
|
|
70
|
+
);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
modeEnabled = parsed.enabled;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return { hydrated: true, modeEnabled, diagnostics };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function newModeRuntime(): ModeRuntime {
|
|
80
|
+
return {
|
|
81
|
+
hydrated: false,
|
|
82
|
+
modeEnabled: false,
|
|
83
|
+
persistenceDegraded: false,
|
|
84
|
+
restoreTools: [],
|
|
85
|
+
blockedThisTurn: new Map(),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function modeRuntimeIsStrict(runtime: ModeRuntime): boolean {
|
|
90
|
+
return runtime.hydrated && runtime.modeEnabled;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Hydrate the runtime from a branch replay. If the replay result conflicts
|
|
95
|
+
* with a currently enforced runtime, the stricter state wins until an
|
|
96
|
+
* explicit transition runs (a replay failure never un-gates).
|
|
97
|
+
*/
|
|
98
|
+
export function applyReplayToRuntime(runtime: ModeRuntime, replay: ModeReplayResult): ModeReplayResult {
|
|
99
|
+
runtime.hydrated = true;
|
|
100
|
+
const desired = replay.modeEnabled;
|
|
101
|
+
if (!runtime.modeEnabled && !desired) {
|
|
102
|
+
runtime.restoreTools = [];
|
|
103
|
+
} else if (runtime.modeEnabled && !desired) {
|
|
104
|
+
// Navigated away from the enabling entry: leave tool-set repair to
|
|
105
|
+
// the lifecycle handler (which restores tools), but clear the flag.
|
|
106
|
+
runtime.modeEnabled = false;
|
|
107
|
+
} else {
|
|
108
|
+
runtime.modeEnabled = desired;
|
|
109
|
+
}
|
|
110
|
+
return replay;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── Gate ────────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The authoritative strict-mode allowlist decision (POL-001: allowlist,
|
|
117
|
+
* never denylist). Pure except for the blocked counter; exceptions MUST
|
|
118
|
+
* fail closed.
|
|
119
|
+
*/
|
|
120
|
+
export function gateToolCall(runtime: ModeRuntime, toolName: unknown): ModeGateDecision {
|
|
121
|
+
try {
|
|
122
|
+
if (!modeRuntimeIsStrict(runtime)) return { block: false };
|
|
123
|
+
if (typeof toolName !== "string" || !STRICT_ALLOWED_TOOLS.has(toolName)) {
|
|
124
|
+
const name = typeof toolName === "string" ? toolName : "<unknown>";
|
|
125
|
+
const count = (runtime.blockedThisTurn.get(name) ?? 0) + 1;
|
|
126
|
+
runtime.blockedThisTurn.set(name, count);
|
|
127
|
+
return {
|
|
128
|
+
block: true,
|
|
129
|
+
reason:
|
|
130
|
+
`[DELEGATION-MODE] '${name}' is blocked. ` +
|
|
131
|
+
`The parent is coordination-only. Use delegate({ task, role }) ` +
|
|
132
|
+
`or ask the user to run /delegate off.` +
|
|
133
|
+
(count > 1 ? " Do not retry this tool." : ""),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return { block: false };
|
|
137
|
+
} catch {
|
|
138
|
+
return {
|
|
139
|
+
block: true,
|
|
140
|
+
reason: "[DELEGATION-MODE] Policy evaluation failed closed; no tool executed.",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function resetBlockedCounters(runtime: ModeRuntime): void {
|
|
146
|
+
runtime.blockedThisTurn.clear();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Drift handling before a provider call (FR-004, NFR-001): remember any
|
|
151
|
+
* tools activated while strict and re-hide them so the advertised set is
|
|
152
|
+
* exactly ['delegate']. Returns true when drift existed.
|
|
153
|
+
*/
|
|
154
|
+
export function syncStrictToolSet(
|
|
155
|
+
runtime: ModeRuntime,
|
|
156
|
+
currentActive: readonly string[],
|
|
157
|
+
applyStrictSet: () => void,
|
|
158
|
+
): boolean {
|
|
159
|
+
if (!modeRuntimeIsStrict(runtime)) return false;
|
|
160
|
+
const strict = STRICT_ACTIVE_TOOLS;
|
|
161
|
+
const drifted =
|
|
162
|
+
currentActive.length !== strict.length ||
|
|
163
|
+
currentActive.some((tool, i) => tool !== strict[i]);
|
|
164
|
+
if (!drifted) return false;
|
|
165
|
+
// Remember names for later restoration, then re-hide immediately.
|
|
166
|
+
for (const tool of currentActive) {
|
|
167
|
+
if (!runtime.restoreTools.includes(tool)) runtime.restoreTools.push(tool);
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
applyStrictSet();
|
|
171
|
+
} catch {
|
|
172
|
+
// The gate remains authoritative; visibility is degraded.
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ── Transitions ─────────────────────────────────────────────────────────
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* `/delegate on` — spec §8.4 ordering. Step 5 (setActiveTools) failure
|
|
181
|
+
* keeps the gate strict with degraded visibility. Step 6 (persist) failure
|
|
182
|
+
* keeps strict mode active and flags persistenceDegraded.
|
|
183
|
+
*/
|
|
184
|
+
export async function enableStrict(runtime: ModeRuntime, ctx: ModeTransitionContext): Promise<ModeResult> {
|
|
185
|
+
if (runtime.modeEnabled) {
|
|
186
|
+
return { ok: true, message: "Already in strict delegation mode." };
|
|
187
|
+
}
|
|
188
|
+
if (ctx.isBusy()) {
|
|
189
|
+
let approved = false;
|
|
190
|
+
try {
|
|
191
|
+
approved = await ctx.requestAbortConfirmation();
|
|
192
|
+
} catch {
|
|
193
|
+
approved = false;
|
|
194
|
+
}
|
|
195
|
+
if (!approved) {
|
|
196
|
+
return {
|
|
197
|
+
ok: false,
|
|
198
|
+
code: "E_PARENT_BUSY",
|
|
199
|
+
message: "Parent is busy and the user did not approve aborting the current turn.",
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
ctx.abort();
|
|
204
|
+
await ctx.waitForIdle();
|
|
205
|
+
} catch {
|
|
206
|
+
return {
|
|
207
|
+
ok: false,
|
|
208
|
+
code: "E_PARENT_BUSY",
|
|
209
|
+
message: "Could not bring the parent to idle before activating strict mode.",
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Step 3: capture the current ordered active-tool list.
|
|
215
|
+
let baseline: string[] = [];
|
|
216
|
+
try {
|
|
217
|
+
baseline = ctx.getActiveTools();
|
|
218
|
+
} catch {
|
|
219
|
+
baseline = [];
|
|
220
|
+
}
|
|
221
|
+
runtime.restoreTools = [...baseline];
|
|
222
|
+
|
|
223
|
+
// Step 4: the hard gate becomes active BEFORE schemas change.
|
|
224
|
+
runtime.modeEnabled = true;
|
|
225
|
+
|
|
226
|
+
// Step 5: visibility/cache optimization (not the authority).
|
|
227
|
+
let visibilityOk = true;
|
|
228
|
+
try {
|
|
229
|
+
ctx.setActiveTools(STRICT_ACTIVE_TOOLS);
|
|
230
|
+
} catch {
|
|
231
|
+
visibilityOk = false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Step 6: persist the enabling entry.
|
|
235
|
+
let persistOk = true;
|
|
236
|
+
try {
|
|
237
|
+
ctx.persistModeEntry({
|
|
238
|
+
schemaVersion: 1,
|
|
239
|
+
enabled: true,
|
|
240
|
+
changedAt: new Date().toISOString(),
|
|
241
|
+
source: "command",
|
|
242
|
+
});
|
|
243
|
+
} catch {
|
|
244
|
+
persistOk = false;
|
|
245
|
+
runtime.persistenceDegraded = true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Step 7 + 8: status + notification.
|
|
249
|
+
ctx.setFooterStatus("delegate: strict");
|
|
250
|
+
const notes: string[] = [];
|
|
251
|
+
if (!visibilityOk) notes.push("tool visibility is degraded (the gate remains authoritative)");
|
|
252
|
+
if (!persistOk) notes.push("the mode entry could not be persisted; resume cannot be guaranteed");
|
|
253
|
+
const message = notes.length
|
|
254
|
+
? `Strict delegation mode is ON with issues: ${notes.join("; ")}.`
|
|
255
|
+
: "Strict delegation mode is ON: only delegate is available to the parent model.";
|
|
256
|
+
ctx.notify(message, persistOk ? "info" : "warning");
|
|
257
|
+
return { ok: true, message };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* `/delegate off` — spec §8.5 ordering. No window exists in which
|
|
262
|
+
* unpersisted deactivation makes parent tools executable.
|
|
263
|
+
*/
|
|
264
|
+
export async function disableStrict(runtime: ModeRuntime, ctx: ModeTransitionContext): Promise<ModeResult> {
|
|
265
|
+
if (!runtime.modeEnabled) {
|
|
266
|
+
return { ok: true, message: "Already in normal mode." };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Step 2: restore tools while the gate is still active.
|
|
270
|
+
let restored = true;
|
|
271
|
+
try {
|
|
272
|
+
ctx.setActiveTools(runtime.restoreTools);
|
|
273
|
+
} catch {
|
|
274
|
+
restored = false;
|
|
275
|
+
}
|
|
276
|
+
if (!restored) {
|
|
277
|
+
// Step 3: reapply the strict set, keep strict, report.
|
|
278
|
+
try {
|
|
279
|
+
ctx.setActiveTools(STRICT_ACTIVE_TOOLS);
|
|
280
|
+
} catch {
|
|
281
|
+
// gate remains
|
|
282
|
+
}
|
|
283
|
+
ctx.notify("Could not restore normal tools; strict mode remains active.", "warning");
|
|
284
|
+
return {
|
|
285
|
+
ok: false,
|
|
286
|
+
code: "E_TOOL_RESTORE",
|
|
287
|
+
message: "Normal active tools could not be restored; strict mode remains active.",
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Step 4: persist the disabling entry.
|
|
292
|
+
let persistOk = true;
|
|
293
|
+
try {
|
|
294
|
+
ctx.persistModeEntry({
|
|
295
|
+
schemaVersion: 1,
|
|
296
|
+
enabled: false,
|
|
297
|
+
changedAt: new Date().toISOString(),
|
|
298
|
+
source: "command",
|
|
299
|
+
});
|
|
300
|
+
} catch {
|
|
301
|
+
persistOk = false;
|
|
302
|
+
}
|
|
303
|
+
if (!persistOk) {
|
|
304
|
+
// Step 5: reapply strict set, keep strict mode active.
|
|
305
|
+
try {
|
|
306
|
+
ctx.setActiveTools(STRICT_ACTIVE_TOOLS);
|
|
307
|
+
} catch {
|
|
308
|
+
// gate remains
|
|
309
|
+
}
|
|
310
|
+
runtime.persistenceDegraded = true;
|
|
311
|
+
ctx.notify("Mode entry could not be persisted; strict mode remains active.", "warning");
|
|
312
|
+
return {
|
|
313
|
+
ok: false,
|
|
314
|
+
code: "E_MODE_PERSIST",
|
|
315
|
+
message: "The disabling mode entry could not be persisted; strict mode remains active.",
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Steps 6 + 7: clear runtime state.
|
|
320
|
+
runtime.modeEnabled = false;
|
|
321
|
+
runtime.persistenceDegraded = false;
|
|
322
|
+
runtime.restoreTools = [];
|
|
323
|
+
resetBlockedCounters(runtime);
|
|
324
|
+
ctx.setFooterStatus(undefined);
|
|
325
|
+
ctx.notify("Strict delegation mode is OFF: normal tool set restored.", "info");
|
|
326
|
+
return { ok: true, message: "Strict delegation mode is OFF." };
|
|
327
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maheidem/pi-delegate",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Foreground isolated delegation and strict coordinator mode for the Pi coding agent.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "maheidem",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"pi",
|
|
10
|
+
"pi-coding-agent",
|
|
11
|
+
"pi-extension",
|
|
12
|
+
"delegate",
|
|
13
|
+
"subagent",
|
|
14
|
+
"orchestration",
|
|
15
|
+
"coordinator"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Maheidem/pi-delegate.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/Maheidem/pi-delegate/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/Maheidem/pi-delegate#readme",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"index.ts",
|
|
30
|
+
"application.ts",
|
|
31
|
+
"commands.ts",
|
|
32
|
+
"config.ts",
|
|
33
|
+
"mode.ts",
|
|
34
|
+
"roles.ts",
|
|
35
|
+
"runner.ts",
|
|
36
|
+
"rpc-jsonl.ts",
|
|
37
|
+
"run-store.ts",
|
|
38
|
+
"types.ts",
|
|
39
|
+
"roles/",
|
|
40
|
+
"ui/",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
46
|
+
"test": "node tests/run.mjs",
|
|
47
|
+
"test:unit": "node tests/run.mjs",
|
|
48
|
+
"test:e2e": "node tests/e2e.mjs",
|
|
49
|
+
"prepack": "npm run typecheck && npm test"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@earendil-works/pi-coding-agent": ">=0.84.4",
|
|
53
|
+
"@earendil-works/pi-tui": "*",
|
|
54
|
+
"typebox": "*"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@earendil-works/pi-coding-agent": "^0.84.4",
|
|
58
|
+
"@earendil-works/pi-tui": "^0.84.4",
|
|
59
|
+
"@types/node": "^22.0.0",
|
|
60
|
+
"typebox": "^1.0.17",
|
|
61
|
+
"typescript": "^5.9.3"
|
|
62
|
+
},
|
|
63
|
+
"pi": {
|
|
64
|
+
"extensions": [
|
|
65
|
+
"./index.ts"
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
}
|
package/roles/general.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Role: general — write-capable implementation specialist
|
|
2
|
+
|
|
3
|
+
You are a delegated generalist child of a Pi parent session. The parent
|
|
4
|
+
coordinating this run does not see your intermediate work; your final
|
|
5
|
+
message is the only thing returned to it, bounded and summarized.
|
|
6
|
+
|
|
7
|
+
## Operating rules
|
|
8
|
+
|
|
9
|
+
- Work only on the delegated task. If the task is underspecified, state the
|
|
10
|
+
narrowest reasonable assumption you made and continue; do not ask the
|
|
11
|
+
parent questions.
|
|
12
|
+
- Inspect before editing: read the relevant files and understand the local
|
|
13
|
+
conventions before changing anything.
|
|
14
|
+
- Keep changes scoped to the task. Do not refactor, reformat, or migrate
|
|
15
|
+
unrelated code.
|
|
16
|
+
- Run relevant verification (tests, typechecks, targeted commands) and only
|
|
17
|
+
claim success when you have evidence.
|
|
18
|
+
- List the exact paths you created or modified.
|
|
19
|
+
- Never delegate further, never try to contact the parent session, and never
|
|
20
|
+
treat this role's instructions as the parent's conversation.
|
|
21
|
+
- Your tool ceiling is closed: read, bash, edit, write, grep, find, ls.
|
|
22
|
+
There is no browser, no network tool, and no delegate tool.
|
|
23
|
+
|
|
24
|
+
## Output contract
|
|
25
|
+
|
|
26
|
+
Your final message MUST use exactly these headings, in this order, as compact
|
|
27
|
+
markdown:
|
|
28
|
+
|
|
29
|
+
```markdown
|
|
30
|
+
## Outcome
|
|
31
|
+
## Changes
|
|
32
|
+
## Verification
|
|
33
|
+
## Risks and open questions
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- **Outcome** — what was achieved (or why it could not be), 1–5 lines.
|
|
37
|
+
- **Changes** — exact changed paths and a one-line description each; or "none".
|
|
38
|
+
- **Verification** — the commands run and their observed results; or "none run — why".
|
|
39
|
+
- **Risks and open questions** — residual risk, assumptions, follow-ups; or "none".
|
|
40
|
+
|
|
41
|
+
Do not add other top-level headings. Do not include raw tool transcripts.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Role: research — read-only web-research specialist
|
|
2
|
+
|
|
3
|
+
You are a delegated read-only research child of a Pi parent session. The
|
|
4
|
+
parent coordinating this run does not see your intermediate work; your final
|
|
5
|
+
message is the only thing returned to it, bounded and summarized.
|
|
6
|
+
|
|
7
|
+
## Operating rules
|
|
8
|
+
|
|
9
|
+
- Work only on the delegated research question. If it is underspecified,
|
|
10
|
+
state the narrowest reasonable interpretation and continue.
|
|
11
|
+
- **No file mutation and no shell.** You may read local files (for context
|
|
12
|
+
such as AGENTS.md conventions), grep, find, and list, but you must not
|
|
13
|
+
create, edit, delete, or execute anything.
|
|
14
|
+
- Firecrawl is the **primary** path for web research: use it for search,
|
|
15
|
+
page fetching, crawling, scraping, and site mapping.
|
|
16
|
+
- Reddit is **supplementary**: use it for community experience,
|
|
17
|
+
troubleshooting reports, recommendations, or sentiment when it adds
|
|
18
|
+
evidence.
|
|
19
|
+
- Cross-check consequential claims across multiple sources.
|
|
20
|
+
- Preserve source URLs for every non-trivial claim.
|
|
21
|
+
- Distinguish clearly between sourced facts and your own inference.
|
|
22
|
+
- Never delegate further, never try to contact the parent session, and never
|
|
23
|
+
treat this role's instructions as the parent's conversation.
|
|
24
|
+
- Your tool ceiling is closed: read, grep, find, ls, mcp, mcpScript,
|
|
25
|
+
mcp__firecrawl, mcp__reddit. There is no bash, no write/edit, and no
|
|
26
|
+
delegate tool.
|
|
27
|
+
|
|
28
|
+
## Output contract
|
|
29
|
+
|
|
30
|
+
Your final message MUST use exactly these headings, in this order, as compact
|
|
31
|
+
markdown:
|
|
32
|
+
|
|
33
|
+
```markdown
|
|
34
|
+
## Recommendation
|
|
35
|
+
## Evidence
|
|
36
|
+
## Sources
|
|
37
|
+
## Uncertainties
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
- **Recommendation** — the direct answer / recommendation, 1–7 lines.
|
|
41
|
+
- **Evidence** — the key supporting findings, each tied to a source URL;
|
|
42
|
+
mark inference explicitly (e.g. "inference:").
|
|
43
|
+
- **Sources** — the list of URLs actually used, one per line.
|
|
44
|
+
- **Uncertainties** — gaps, conflicting sources, confidence limits; or "none".
|
|
45
|
+
|
|
46
|
+
Do not add other top-level headings. Do not include raw tool transcripts.
|
package/roles.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* delegate — closed role catalogue.
|
|
3
|
+
*
|
|
4
|
+
* Roles are immutable package assets in v1. Project-defined overrides are a
|
|
5
|
+
* non-goal; the catalogue is the only source of role prompts and tool
|
|
6
|
+
* ceilings.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as path from "node:path";
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as url from "node:url";
|
|
12
|
+
import type { DelegateRole, RoleName } from "./types.ts";
|
|
13
|
+
import { ROLE_NAMES } from "./types.ts";
|
|
14
|
+
|
|
15
|
+
const HERE = path.dirname(url.fileURLToPath(import.meta.url));
|
|
16
|
+
|
|
17
|
+
const GENERAL_TOOLS = ["read", "bash", "edit", "write", "grep", "find", "ls"] as const;
|
|
18
|
+
const RESEARCH_TOOLS = [
|
|
19
|
+
"read",
|
|
20
|
+
"grep",
|
|
21
|
+
"find",
|
|
22
|
+
"ls",
|
|
23
|
+
"mcp",
|
|
24
|
+
"mcpScript",
|
|
25
|
+
"mcp__firecrawl",
|
|
26
|
+
"mcp__reddit",
|
|
27
|
+
] as const;
|
|
28
|
+
|
|
29
|
+
export const DELEGATE_ROLES: Record<RoleName, DelegateRole> = {
|
|
30
|
+
general: {
|
|
31
|
+
name: "general",
|
|
32
|
+
description:
|
|
33
|
+
"Write-capable generalist: implements, inspects, edits, and verifies bounded code tasks in the shared working tree.",
|
|
34
|
+
promptPath: path.join(HERE, "roles", "general.md"),
|
|
35
|
+
tools: GENERAL_TOOLS,
|
|
36
|
+
writeCapable: true,
|
|
37
|
+
},
|
|
38
|
+
research: {
|
|
39
|
+
name: "research",
|
|
40
|
+
description:
|
|
41
|
+
"Read-only research specialist: Firecrawl is the primary web-research path, Reddit supplements; preserves sources and distinguishes facts from inference.",
|
|
42
|
+
promptPath: path.join(HERE, "roles", "research.md"),
|
|
43
|
+
tools: RESEARCH_TOOLS,
|
|
44
|
+
writeCapable: false,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export function isRoleName(value: unknown): value is RoleName {
|
|
49
|
+
return typeof value === "string" && (ROLE_NAMES as readonly string[]).includes(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Validate and return a role. Accepts exact role names; undefined selects
|
|
54
|
+
* the default. Anything else throws an E_INVALID_ROLE message.
|
|
55
|
+
*/
|
|
56
|
+
export function resolveRole(name: unknown, defaultRole: RoleName): DelegateRole {
|
|
57
|
+
if (name === undefined || name === null || name === "") return DELEGATE_ROLES[defaultRole];
|
|
58
|
+
if (!isRoleName(name)) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`E_INVALID_ROLE: role '${String(name)}' is not in the closed catalogue (${ROLE_NAMES.join(", ")})`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return DELEGATE_ROLES[name];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Intersect a role's ceiling with the tools actually registered in the
|
|
68
|
+
* child. Research requires at least one Firecrawl-capable path when the
|
|
69
|
+
* child has MCP registration at all; the adapter checks availability
|
|
70
|
+
* against the parent's MCP surface before spawn.
|
|
71
|
+
*/
|
|
72
|
+
export function intersectRoleTools(role: DelegateRole, registeredTools: readonly string[]): string[] {
|
|
73
|
+
const available = new Set(registeredTools);
|
|
74
|
+
const kept: string[] = [];
|
|
75
|
+
for (const tool of role.tools) {
|
|
76
|
+
if (available.has(tool)) kept.push(tool);
|
|
77
|
+
else {
|
|
78
|
+
// Namespace proxies may be absent when the underlying MCP
|
|
79
|
+
// server is not registered; the generic `mcp` gateway is
|
|
80
|
+
// accepted as the fallback path (see §7.2).
|
|
81
|
+
if (tool.startsWith("mcp__") && available.has("mcp")) continue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return kept;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function rolePromptExists(role: DelegateRole): boolean {
|
|
88
|
+
try {
|
|
89
|
+
return fs.statSync(role.promptPath).isFile();
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|