@abhishek944/pi-firstmate 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +187 -2
- package/config.example.json +6 -0
- package/dist/config.d.ts +32 -0
- package/dist/config.js +97 -0
- package/dist/herdr-banner.d.ts +42 -0
- package/dist/herdr-banner.js +265 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +779 -0
- package/dist/skill-allowlist.d.ts +25 -0
- package/dist/skill-allowlist.js +34 -0
- package/dist/tool-gate.d.ts +30 -0
- package/dist/tool-gate.js +108 -0
- package/dist/worker.d.ts +34 -0
- package/dist/worker.js +82 -0
- package/herdr/firstmate-banner/README.md +5 -0
- package/herdr/firstmate-banner/main.js +161 -0
- package/herdr/firstmate-banner/mod.json +1 -0
- package/package.json +56 -4
- package/skills/close/SKILL.md +61 -0
- package/skills/firstmate/SKILL.md +129 -0
- package/src/config.ts +118 -0
- package/src/herdr-banner.ts +269 -0
- package/src/index.ts +886 -0
- package/src/skill-allowlist.ts +50 -0
- package/src/tool-gate.ts +128 -0
- package/src/worker.ts +101 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exact-name filtering for First Mate's model-facing skill advertisement.
|
|
3
|
+
*
|
|
4
|
+
* Pi discovers skills from user, project, package and explicit locations, then
|
|
5
|
+
* advertises each discovered skill's name, description and path in the system
|
|
6
|
+
* prompt. This module only narrows that per-run advertisement for the
|
|
7
|
+
* `pi-firstmate.coordinatorSkills` setting. It never loads, unloads, reorders or
|
|
8
|
+
* mutates skills, and Pi keeps its own complete list for `/skill:<name>`
|
|
9
|
+
* expansion, so explicit skill commands and already loaded instructions are
|
|
10
|
+
* unaffected.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface NamedSkill {
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SkillAllowlistResult<T extends NamedSkill> {
|
|
18
|
+
/** Skills that stay advertised, in Pi's discovery order. */
|
|
19
|
+
skills: T[];
|
|
20
|
+
/** Configured names no discovered skill provides, in configured order. */
|
|
21
|
+
unknown: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Apply exact names to a discovered skill list. Matching is exact (no globs,
|
|
26
|
+
* case folding or whitespace trimming), and neither input is mutated.
|
|
27
|
+
*/
|
|
28
|
+
export function applySkillAllowlist<T extends NamedSkill>(
|
|
29
|
+
skills: readonly T[],
|
|
30
|
+
names: readonly string[],
|
|
31
|
+
): SkillAllowlistResult<T> {
|
|
32
|
+
const allowed = new Set(names);
|
|
33
|
+
const advertised: T[] = [];
|
|
34
|
+
const discovered = new Set<string>();
|
|
35
|
+
|
|
36
|
+
for (const skill of skills) {
|
|
37
|
+
discovered.add(skill.name);
|
|
38
|
+
if (allowed.has(skill.name)) advertised.push(skill);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const unknown: string[] = [];
|
|
42
|
+
const reported = new Set<string>();
|
|
43
|
+
for (const name of names) {
|
|
44
|
+
if (discovered.has(name) || reported.has(name)) continue;
|
|
45
|
+
reported.add(name);
|
|
46
|
+
unknown.push(name);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { skills: advertised, unknown };
|
|
50
|
+
}
|
package/src/tool-gate.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coordinator tool gating for First Mate.
|
|
3
|
+
*
|
|
4
|
+
* While coordinator mode is effective, only the four inspection tools and tools
|
|
5
|
+
* registered by the approved orchestration packages stay available. Other
|
|
6
|
+
* active tools are removed from the model-facing set, and every call, including
|
|
7
|
+
* a nested call issued through `ctx.executeTool()`, is checked again at Pi's
|
|
8
|
+
* `tool_call` boundary.
|
|
9
|
+
*
|
|
10
|
+
* This is tool-call enforcement, not an OS sandbox. An allowed tool can still
|
|
11
|
+
* perform whatever its own implementation exposes, so permission, trust and
|
|
12
|
+
* authorization boundaries remain in force.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { dirname, join } from "node:path";
|
|
17
|
+
import type { ExtensionAPI, SourceInfo } from "@earendil-works/pi-coding-agent";
|
|
18
|
+
|
|
19
|
+
const INSPECTION_TOOLS = new Set(["read", "grep", "find", "ls"]);
|
|
20
|
+
|
|
21
|
+
/** Installed packages whose tools stay available to a coordinator. */
|
|
22
|
+
export const APPROVED_PACKAGES = new Set([
|
|
23
|
+
"@abhishek944/pi-tasks",
|
|
24
|
+
"@abhishek944/pi-loop-monitor",
|
|
25
|
+
"@abhishek944/pi-decisions",
|
|
26
|
+
"@abhishek944/pi-herdr",
|
|
27
|
+
"@abhishek944/pi-schedulers",
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
const COORDINATOR_ONLY_PACKAGES = new Set(["@abhishek944/pi-schedulers"]);
|
|
31
|
+
|
|
32
|
+
export const COORDINATOR_TOOLS_DESCRIPTION =
|
|
33
|
+
"read, grep, find, ls and installed pi-tasks, pi-loop-monitor, pi-decisions, pi-herdr and coordinator-only pi-schedulers tools";
|
|
34
|
+
|
|
35
|
+
type ToolHost = Pick<ExtensionAPI, "getActiveTools" | "getAllTools" | "setActiveTools">;
|
|
36
|
+
|
|
37
|
+
export interface ToolGate {
|
|
38
|
+
/** Actual installed registrations that must never enter the ordinary-tool baseline. */
|
|
39
|
+
coordinatorOnlyNames(): Set<string>;
|
|
40
|
+
/** Whether coordinator mode may call this tool. */
|
|
41
|
+
permits(toolName: string): boolean;
|
|
42
|
+
/** Hide every active tool outside the allowlist and return the hidden names. */
|
|
43
|
+
hide(): string[];
|
|
44
|
+
/** Actionable explanation for a rejected direct or nested call. */
|
|
45
|
+
reason(toolName: string): string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
49
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sourceBaseDir(sourceInfo: SourceInfo): string | undefined {
|
|
53
|
+
if (sourceInfo.baseDir !== undefined) return sourceInfo.baseDir;
|
|
54
|
+
if (sourceInfo.path.startsWith("builtin:") || sourceInfo.path.startsWith("<")) return undefined;
|
|
55
|
+
return dirname(sourceInfo.path);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createToolGate(pi: ToolHost): ToolGate {
|
|
59
|
+
const sourcePackages = new Map<string, string | null>();
|
|
60
|
+
|
|
61
|
+
// Package identity comes from the registration's installed package root, not
|
|
62
|
+
// from a tool name list or self-declared annotations. The extension entry can
|
|
63
|
+
// live in `dist/` (explicit extension paths) or the package root (discovered
|
|
64
|
+
// packages), so walk to the nearest manifest. A missing or unreadable manifest
|
|
65
|
+
// stays unapproved so new or unexpected tools fail closed.
|
|
66
|
+
function sourcePackage(sourceInfo: SourceInfo): string | null {
|
|
67
|
+
const baseDir = sourceBaseDir(sourceInfo);
|
|
68
|
+
if (baseDir === undefined) return null;
|
|
69
|
+
const cached = sourcePackages.get(baseDir);
|
|
70
|
+
if (cached !== undefined) return cached;
|
|
71
|
+
|
|
72
|
+
let packageName: string | null = null;
|
|
73
|
+
let directory = baseDir;
|
|
74
|
+
while (true) {
|
|
75
|
+
try {
|
|
76
|
+
const manifest: unknown = JSON.parse(readFileSync(join(directory, "package.json"), "utf8"));
|
|
77
|
+
packageName = isRecord(manifest) && typeof manifest.name === "string" ? manifest.name : null;
|
|
78
|
+
break;
|
|
79
|
+
} catch {
|
|
80
|
+
const parent = dirname(directory);
|
|
81
|
+
if (parent === directory) break;
|
|
82
|
+
directory = parent;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
sourcePackages.set(baseDir, packageName);
|
|
86
|
+
return packageName;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Rebuild from the live registry so tools registered after session start are
|
|
90
|
+
// classified from their actual installation instead of a stale snapshot.
|
|
91
|
+
function allowedNames(): Set<string> {
|
|
92
|
+
const allowed = new Set(INSPECTION_TOOLS);
|
|
93
|
+
for (const tool of pi.getAllTools()) {
|
|
94
|
+
if (APPROVED_PACKAGES.has(sourcePackage(tool.sourceInfo) ?? "")) allowed.add(tool.name);
|
|
95
|
+
}
|
|
96
|
+
return allowed;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
coordinatorOnlyNames() {
|
|
101
|
+
return new Set(pi.getAllTools()
|
|
102
|
+
.filter((tool) => COORDINATOR_ONLY_PACKAGES.has(sourcePackage(tool.sourceInfo) ?? ""))
|
|
103
|
+
.map((tool) => tool.name));
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
permits(toolName) {
|
|
107
|
+
if (INSPECTION_TOOLS.has(toolName)) return true;
|
|
108
|
+
try {
|
|
109
|
+
return allowedNames().has(toolName);
|
|
110
|
+
} catch {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
hide() {
|
|
116
|
+
const allowed = allowedNames();
|
|
117
|
+
const active = pi.getActiveTools();
|
|
118
|
+
const kept = active.filter((name) => allowed.has(name));
|
|
119
|
+
const hidden = active.filter((name) => !allowed.has(name));
|
|
120
|
+
if (hidden.length > 0) pi.setActiveTools(kept);
|
|
121
|
+
return hidden;
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
reason(toolName) {
|
|
125
|
+
return `First Mate coordinator mode blocks ${JSON.stringify(toolName)}. Only ${COORDINATOR_TOOLS_DESCRIPTION} are available in this coordinator session, including for nested tool calls. This is an intentional coordinator restriction, not a missing permission: do not ask to enable the tool. Check that an installed delegation backend is ready and delegate the work through its orchestration tools, or run /firstmate off before doing it locally.`;
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
package/src/worker.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrow, local Core worker classification for First Mate.
|
|
3
|
+
*
|
|
4
|
+
* `HC_TERMINAL_ID` is present in every Core-hosted Pi process, including the
|
|
5
|
+
* primary coordinator, so it is only a precondition. A worker is verified from
|
|
6
|
+
* the labels that pi-herdr AgentCreate writes: `pi.task` and `pi.session`.
|
|
7
|
+
* Partial labels or a failed lookup are reported as unknown instead of guessed.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ExecResult, ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
|
|
12
|
+
export type WorkerClassification =
|
|
13
|
+
| { kind: "explicit-worker" }
|
|
14
|
+
| { kind: "not-core" }
|
|
15
|
+
| { kind: "primary"; terminalId: string }
|
|
16
|
+
| { kind: "worker"; terminalId: string; task: string; parentSessionId: string; parent?: string }
|
|
17
|
+
| { kind: "unknown"; terminalId: string; message: string };
|
|
18
|
+
|
|
19
|
+
type CoreExec = Pick<ExtensionAPI, "exec">;
|
|
20
|
+
|
|
21
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
22
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function readLabel(labels: Record<string, unknown>, key: string): string | undefined {
|
|
26
|
+
const value = labels[key];
|
|
27
|
+
if (typeof value !== "string") return undefined;
|
|
28
|
+
const trimmed = value.trim();
|
|
29
|
+
return trimmed === "" ? undefined : trimmed;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function firstLine(value: string): string {
|
|
33
|
+
const line = value.split("\n", 1)[0]?.trim() ?? "";
|
|
34
|
+
return line === "" ? "no diagnostic output" : line;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Classify the current process as a primary Core session, a verified task
|
|
39
|
+
* worker, a non-Core session, or unknown when the Core lookup cannot be trusted.
|
|
40
|
+
*/
|
|
41
|
+
export async function classifyCoreWorker(
|
|
42
|
+
pi: CoreExec,
|
|
43
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
44
|
+
): Promise<WorkerClassification> {
|
|
45
|
+
const terminalId = env.HC_TERMINAL_ID?.trim();
|
|
46
|
+
if (!terminalId) return { kind: "not-core" };
|
|
47
|
+
|
|
48
|
+
let result: ExecResult;
|
|
49
|
+
try {
|
|
50
|
+
result = await pi.exec("hc", ["get", terminalId, "--json"]);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
53
|
+
return { kind: "unknown", terminalId, message: `hc get ${terminalId} failed: ${firstLine(message)}` };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (result.code !== 0) {
|
|
57
|
+
const detail = result.stderr.trim() === "" ? result.stdout.trim() : result.stderr.trim();
|
|
58
|
+
const suffix = detail === ""
|
|
59
|
+
? "; no diagnostic output (is hc installed and this terminal live?)"
|
|
60
|
+
: `: ${firstLine(detail)}`;
|
|
61
|
+
return {
|
|
62
|
+
kind: "unknown",
|
|
63
|
+
terminalId,
|
|
64
|
+
message: `hc get ${terminalId} exited with code ${result.code}${suffix}`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let parsed: unknown;
|
|
69
|
+
try {
|
|
70
|
+
parsed = JSON.parse(result.stdout);
|
|
71
|
+
} catch {
|
|
72
|
+
return { kind: "unknown", terminalId, message: `hc get ${terminalId} returned invalid JSON` };
|
|
73
|
+
}
|
|
74
|
+
if (!isRecord(parsed)) {
|
|
75
|
+
return { kind: "unknown", terminalId, message: `hc get ${terminalId} returned unexpected JSON` };
|
|
76
|
+
}
|
|
77
|
+
if (typeof parsed.address === "string" && parsed.address !== terminalId) {
|
|
78
|
+
return {
|
|
79
|
+
kind: "unknown",
|
|
80
|
+
terminalId,
|
|
81
|
+
message: `hc get ${terminalId} returned terminal ${parsed.address}`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const labels = isRecord(parsed.labels) ? parsed.labels : {};
|
|
86
|
+
const task = readLabel(labels, "pi.task");
|
|
87
|
+
const parentSessionId = readLabel(labels, "pi.session");
|
|
88
|
+
|
|
89
|
+
if (task === undefined && parentSessionId === undefined) return { kind: "primary", terminalId };
|
|
90
|
+
if (task === undefined || parentSessionId === undefined) {
|
|
91
|
+
const missing = task === undefined ? "pi.task" : "pi.session";
|
|
92
|
+
return {
|
|
93
|
+
kind: "unknown",
|
|
94
|
+
terminalId,
|
|
95
|
+
message: `Core terminal ${terminalId} has a partial pi-herdr label set (missing ${missing})`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const parent = typeof parsed.parent === "string" && parsed.parent.trim() !== "" ? parsed.parent : undefined;
|
|
100
|
+
return { kind: "worker", terminalId, task, parentSessionId, ...(parent ? { parent } : {}) };
|
|
101
|
+
}
|