xtrm-cli 2.1.20 → 2.1.29
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/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/extensions/beads.ts +99 -0
- package/extensions/core/adapter.ts +45 -0
- package/extensions/core/lib.ts +3 -0
- package/extensions/core/logger.ts +45 -0
- package/extensions/core/runner.ts +71 -0
- package/extensions/main-guard-post-push.ts +44 -0
- package/extensions/main-guard.ts +126 -0
- package/extensions/quality-gates.ts +67 -0
- package/extensions/service-skills.ts +88 -0
- package/extensions/xtrm-loader.ts +89 -0
- package/package.json +1 -1
- package/src/commands/install-pi.ts +12 -0
- package/src/commands/install-project.ts +1 -1
- package/test/extensions/beads.test.ts +166 -0
- package/test/extensions/extension-harness.ts +85 -0
- package/test/extensions/main-guard.test.ts +77 -0
- package/test/extensions/quality-gates.test.ts +79 -0
- package/test/extensions/xtrm-loader.test.ts +53 -0
- package/test/install-pi.test.ts +39 -1
- package/test/install-project.test.ts +29 -26
- package/vitest.config.ts +1 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolCallEvent, ToolResultEvent } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { isToolCallEventType, isBashToolResult } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import { SubprocessRunner, EventAdapter, Logger } from "./core/lib";
|
|
6
|
+
|
|
7
|
+
const logger = new Logger({ namespace: "beads" });
|
|
8
|
+
|
|
9
|
+
export default function (pi: ExtensionAPI) {
|
|
10
|
+
const getCwd = (ctx: any) => ctx.cwd || process.cwd();
|
|
11
|
+
const isBeadsProject = (cwd: string) => fs.existsSync(path.join(cwd, ".beads"));
|
|
12
|
+
|
|
13
|
+
// Get session ID from sessionManager (UUID, consistent with hooks)
|
|
14
|
+
const getSessionId = (ctx: any): string => {
|
|
15
|
+
return ctx.sessionManager?.getSessionId?.() ?? process.pid.toString();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const getSessionClaim = async (sessionId: string, cwd: string): Promise<string | null> => {
|
|
19
|
+
const result = await SubprocessRunner.run("bd", ["kv", "get", `claimed:${sessionId}`], { cwd });
|
|
20
|
+
if (result.code === 0) return result.stdout.trim();
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const hasTrackableWork = async (cwd: string): Promise<boolean> => {
|
|
25
|
+
const result = await SubprocessRunner.run("bd", ["list"], { cwd });
|
|
26
|
+
if (result.code === 0 && result.stdout.includes("Total:")) {
|
|
27
|
+
const m = result.stdout.match(/Total:\s*\d+\s+issues?\s*\((\d+)\s+open,\s*(\d+)\s+in progress\)/);
|
|
28
|
+
if (m) {
|
|
29
|
+
const open = parseInt(m[1], 10);
|
|
30
|
+
const inProgress = parseInt(m[2], 10);
|
|
31
|
+
return (open + inProgress) > 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
38
|
+
const cwd = getCwd(ctx);
|
|
39
|
+
if (!isBeadsProject(cwd)) return undefined;
|
|
40
|
+
|
|
41
|
+
const sessionId = getSessionId(ctx);
|
|
42
|
+
|
|
43
|
+
if (EventAdapter.isMutatingFileTool(event)) {
|
|
44
|
+
const claim = await getSessionClaim(sessionId, cwd);
|
|
45
|
+
if (!claim) {
|
|
46
|
+
const hasWork = await hasTrackableWork(cwd);
|
|
47
|
+
if (hasWork) {
|
|
48
|
+
if (ctx.hasUI) {
|
|
49
|
+
ctx.ui.notify("Beads: Edit blocked. Claim an issue first.", "warning");
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
block: true,
|
|
53
|
+
reason: `No active issue claim for this session (${sessionId}).\n bd update <id> --claim`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (isToolCallEventType("bash", event)) {
|
|
60
|
+
const command = event.input.command;
|
|
61
|
+
if (command && /\bgit\s+commit\b/.test(command)) {
|
|
62
|
+
const claim = await getSessionClaim(sessionId, cwd);
|
|
63
|
+
if (claim) {
|
|
64
|
+
return {
|
|
65
|
+
block: true,
|
|
66
|
+
reason: `Resolve open claim [${claim}] before committing.`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return undefined;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
pi.on("tool_result", async (event, ctx) => {
|
|
76
|
+
if (isBashToolResult(event)) {
|
|
77
|
+
const command = event.input.command;
|
|
78
|
+
const sessionId = getSessionId(ctx);
|
|
79
|
+
|
|
80
|
+
if (command && /\bbd\s+update\b/.test(command) && /--claim\b/.test(command)) {
|
|
81
|
+
const issueMatch = command.match(/\bbd\s+update\s+(\S+)/);
|
|
82
|
+
if (issueMatch) {
|
|
83
|
+
const issueId = issueMatch[1];
|
|
84
|
+
const cwd = getCwd(ctx);
|
|
85
|
+
await SubprocessRunner.run("bd", ["kv", "set", `claimed:${sessionId}`, issueId], { cwd });
|
|
86
|
+
const claimNotice = `\n\n✅ **Beads**: Session \`${sessionId}\` claimed issue \`${issueId}\`. File edits are now unblocked.`;
|
|
87
|
+
return { content: [...event.content, { type: "text", text: claimNotice }] };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (command && /\bbd\s+close\b/.test(command) && !event.isError) {
|
|
92
|
+
const reminder = "\n\n**Beads Insight**: Work completed. Consider if this session produced insights worth persisting via `bd remember`.";
|
|
93
|
+
const newContent = [...event.content, { type: "text", text: reminder }];
|
|
94
|
+
return { content: newContent };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolCallEvent } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export class EventAdapter {
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the tool event is a mutating file operation (write, edit, etc).
|
|
6
|
+
*/
|
|
7
|
+
static isMutatingFileTool(event: ToolCallEvent<any, any>): boolean {
|
|
8
|
+
const tools = [
|
|
9
|
+
"write",
|
|
10
|
+
"edit",
|
|
11
|
+
"replace_content",
|
|
12
|
+
"replace_lines",
|
|
13
|
+
"delete_lines",
|
|
14
|
+
"insert_at_line",
|
|
15
|
+
"create_text_file",
|
|
16
|
+
"rename_symbol",
|
|
17
|
+
"replace_symbol_body",
|
|
18
|
+
"insert_after_symbol",
|
|
19
|
+
"insert_before_symbol",
|
|
20
|
+
];
|
|
21
|
+
return tools.includes(event.toolName);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Extracts the target path from a tool input, resolving against the current working directory.
|
|
26
|
+
*/
|
|
27
|
+
static extractPathFromToolInput(event: ToolCallEvent<any, any>, cwd: string): string | null {
|
|
28
|
+
const input = event.input;
|
|
29
|
+
if (!input) return null;
|
|
30
|
+
|
|
31
|
+
const pathRaw = input.path || input.file || input.filePath;
|
|
32
|
+
if (typeof pathRaw === "string") {
|
|
33
|
+
return pathRaw; // Usually Pi passes absolute paths anyway or paths relative to root
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Safely formats a block reason string to ensure UI readiness.
|
|
41
|
+
*/
|
|
42
|
+
static formatBlockReason(prefix: string, details: string): string {
|
|
43
|
+
return `${prefix}: ${details}`;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
2
|
+
|
|
3
|
+
export interface LoggerOptions {
|
|
4
|
+
namespace: string;
|
|
5
|
+
level?: LogLevel;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class Logger {
|
|
9
|
+
private namespace: string;
|
|
10
|
+
private level: LogLevel;
|
|
11
|
+
|
|
12
|
+
constructor(options: LoggerOptions) {
|
|
13
|
+
this.namespace = options.namespace;
|
|
14
|
+
this.level = options.level || "info";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private shouldLog(level: LogLevel): boolean {
|
|
18
|
+
const levels: LogLevel[] = ["debug", "info", "warn", "error"];
|
|
19
|
+
return levels.indexOf(level) >= levels.indexOf(this.level);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
debug(message: string, ...args: any[]) {
|
|
23
|
+
if (this.shouldLog("debug")) {
|
|
24
|
+
console.debug(`[${this.namespace}] DEBUG: ${message}`, ...args);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
info(message: string, ...args: any[]) {
|
|
29
|
+
if (this.shouldLog("info")) {
|
|
30
|
+
console.info(`[${this.namespace}] INFO: ${message}`, ...args);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
warn(message: string, ...args: any[]) {
|
|
35
|
+
if (this.shouldLog("warn")) {
|
|
36
|
+
console.warn(`[${this.namespace}] WARN: ${message}`, ...args);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
error(message: string, ...args: any[]) {
|
|
41
|
+
if (this.shouldLog("error")) {
|
|
42
|
+
console.error(`[${this.namespace}] ERROR: ${message}`, ...args);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { execFile, spawnSync } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
|
|
4
|
+
const execFileAsync = promisify(execFile);
|
|
5
|
+
|
|
6
|
+
export interface RunOptions {
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
cwd?: string;
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
input?: string; // Standard input
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface RunResult {
|
|
14
|
+
code: number;
|
|
15
|
+
stdout: string;
|
|
16
|
+
stderr: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class SubprocessRunner {
|
|
20
|
+
/**
|
|
21
|
+
* Run a command deterministically with a timeout and optional stdin.
|
|
22
|
+
*/
|
|
23
|
+
static async run(
|
|
24
|
+
command: string,
|
|
25
|
+
args: string[],
|
|
26
|
+
options: RunOptions = {}
|
|
27
|
+
): Promise<RunResult> {
|
|
28
|
+
const timeout = options.timeoutMs ?? 10000;
|
|
29
|
+
const cwd = options.cwd ?? process.cwd();
|
|
30
|
+
const env = { ...process.env, ...options.env };
|
|
31
|
+
|
|
32
|
+
if (options.input !== undefined) {
|
|
33
|
+
// Use spawnSync for stdin support if input is provided
|
|
34
|
+
const result = spawnSync(command, args, {
|
|
35
|
+
cwd,
|
|
36
|
+
env,
|
|
37
|
+
input: options.input,
|
|
38
|
+
encoding: "utf8",
|
|
39
|
+
timeout,
|
|
40
|
+
maxBuffer: 1024 * 1024 * 10,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
code: result.status ?? 1,
|
|
45
|
+
stdout: (result.stdout ?? "").trim(),
|
|
46
|
+
stderr: (result.stderr ?? "").trim(),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const result = await execFileAsync(command, args, {
|
|
52
|
+
timeout,
|
|
53
|
+
cwd,
|
|
54
|
+
env,
|
|
55
|
+
maxBuffer: 1024 * 1024 * 10,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
code: 0,
|
|
60
|
+
stdout: result.stdout.trim(),
|
|
61
|
+
stderr: result.stderr.trim(),
|
|
62
|
+
};
|
|
63
|
+
} catch (error: any) {
|
|
64
|
+
return {
|
|
65
|
+
code: error.code ?? 1,
|
|
66
|
+
stdout: (error.stdout ?? "").trim(),
|
|
67
|
+
stderr: (error.stderr ?? error.message ?? "").trim(),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolResultEvent } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { isBashToolResult } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import { SubprocessRunner, Logger } from "./core/lib";
|
|
4
|
+
|
|
5
|
+
const logger = new Logger({ namespace: "main-guard-post-push" });
|
|
6
|
+
|
|
7
|
+
export default function (pi: ExtensionAPI) {
|
|
8
|
+
const getProtectedBranches = (): string[] => {
|
|
9
|
+
const env = process.env.MAIN_GUARD_PROTECTED_BRANCHES;
|
|
10
|
+
if (env) return env.split(",").map(b => b.trim()).filter(Boolean);
|
|
11
|
+
return ["main", "master"];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
pi.on("tool_result", async (event, ctx) => {
|
|
15
|
+
const cwd = ctx.cwd || process.cwd();
|
|
16
|
+
if (!isBashToolResult(event) || event.isError) return undefined;
|
|
17
|
+
|
|
18
|
+
const cmd = event.input.command.trim();
|
|
19
|
+
if (!/\bgit\s+push\b/.test(cmd)) return undefined;
|
|
20
|
+
|
|
21
|
+
// Check if we pushed to a protected branch
|
|
22
|
+
const protectedBranches = getProtectedBranches();
|
|
23
|
+
const tokens = cmd.split(/\s+/);
|
|
24
|
+
const lastToken = tokens[tokens.length - 1];
|
|
25
|
+
if (protectedBranches.some(b => lastToken === b || lastToken.endsWith(`:${b}`))) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Success! Suggest PR workflow
|
|
30
|
+
const reminder = "\n\n**Main-Guard**: Push successful. Next steps:\n" +
|
|
31
|
+
" 1. `gh pr create --fill` (if not already open)\n" +
|
|
32
|
+
" 2. `gh pr merge --squash` (once approved)\n" +
|
|
33
|
+
" 3. `git checkout main && git reset --hard origin/main` (sync local)";
|
|
34
|
+
|
|
35
|
+
const newContent = [...event.content];
|
|
36
|
+
newContent.push({ type: "text", text: reminder });
|
|
37
|
+
|
|
38
|
+
if (ctx.hasUI) {
|
|
39
|
+
ctx.ui.notify("Main-Guard: Suggesting PR workflow", "info");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return { content: newContent };
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolCallEvent } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { isToolCallEventType } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import { SubprocessRunner, EventAdapter, Logger } from "./core/lib";
|
|
4
|
+
|
|
5
|
+
const logger = new Logger({ namespace: "main-guard" });
|
|
6
|
+
|
|
7
|
+
export default function (pi: ExtensionAPI) {
|
|
8
|
+
const getProtectedBranches = (): string[] => {
|
|
9
|
+
const env = process.env.MAIN_GUARD_PROTECTED_BRANCHES;
|
|
10
|
+
if (env) return env.split(",").map(b => b.trim()).filter(Boolean);
|
|
11
|
+
return ["main", "master"];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const getCurrentBranch = async (cwd: string): Promise<string | null> => {
|
|
15
|
+
const result = await SubprocessRunner.run("git", ["branch", "--show-current"], { cwd });
|
|
16
|
+
if (result.code === 0) return result.stdout;
|
|
17
|
+
return null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const protectedPaths = [".env", ".git/", "node_modules/"];
|
|
21
|
+
|
|
22
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
23
|
+
const cwd = ctx.cwd || process.cwd();
|
|
24
|
+
|
|
25
|
+
// 1. Safety Check: Protected Paths (Global)
|
|
26
|
+
if (EventAdapter.isMutatingFileTool(event)) {
|
|
27
|
+
const path = EventAdapter.extractPathFromToolInput(event, cwd);
|
|
28
|
+
if (path && protectedPaths.some((p) => path.includes(p))) {
|
|
29
|
+
const reason = `Path "${path}" is protected. Edits to sensitive system files are restricted.`;
|
|
30
|
+
if (ctx.hasUI) {
|
|
31
|
+
ctx.ui.notify(`Safety: Blocked edit to protected path`, "error");
|
|
32
|
+
}
|
|
33
|
+
return { block: true, reason };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 2. Safety Check: Dangerous Commands (Global)
|
|
38
|
+
if (isToolCallEventType("bash", event)) {
|
|
39
|
+
const cmd = event.input.command.trim();
|
|
40
|
+
if (cmd.includes("rm -rf") && !cmd.includes("--help")) {
|
|
41
|
+
if (ctx.hasUI) {
|
|
42
|
+
const ok = await ctx.ui.confirm("Dangerous Command", `Allow execution of: ${cmd}?`);
|
|
43
|
+
if (!ok) return { block: true, reason: "Blocked by user confirmation" };
|
|
44
|
+
} else {
|
|
45
|
+
return { block: true, reason: "Dangerous command 'rm -rf' blocked in non-interactive mode" };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 3. Main-Guard: Branch Protection
|
|
51
|
+
const protectedBranches = getProtectedBranches();
|
|
52
|
+
const branch = await getCurrentBranch(cwd);
|
|
53
|
+
|
|
54
|
+
if (branch && protectedBranches.includes(branch)) {
|
|
55
|
+
// A. Mutating File Tools on Main
|
|
56
|
+
if (EventAdapter.isMutatingFileTool(event)) {
|
|
57
|
+
const reason = `On protected branch '${branch}'. Checkout a feature branch first: \`git checkout -b feature/<name>\``;
|
|
58
|
+
if (ctx.hasUI) {
|
|
59
|
+
ctx.ui.notify(`Main-Guard: Blocked edit on ${branch}`, "error");
|
|
60
|
+
}
|
|
61
|
+
return { block: true, reason };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// B. Bash Commands on Main
|
|
65
|
+
if (isToolCallEventType("bash", event)) {
|
|
66
|
+
const cmd = event.input.command.trim();
|
|
67
|
+
|
|
68
|
+
// Emergency override
|
|
69
|
+
if (process.env.MAIN_GUARD_ALLOW_BASH === "1") return undefined;
|
|
70
|
+
|
|
71
|
+
// Enforce squash-only PR merges
|
|
72
|
+
if (/^gh\s+pr\s+merge\b/.test(cmd)) {
|
|
73
|
+
if (!/--squash\b/.test(cmd)) {
|
|
74
|
+
const reason = "Squash only: use `gh pr merge --squash` (or MAIN_GUARD_ALLOW_BASH=1)";
|
|
75
|
+
return { block: true, reason };
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Safe allowlist
|
|
81
|
+
const SAFE_BASH_PATTERNS = [
|
|
82
|
+
/^git\s+(status|log|diff|branch|show|describe|fetch|remote|config)\b/,
|
|
83
|
+
/^git\s+pull\b/,
|
|
84
|
+
/^git\s+stash\b/,
|
|
85
|
+
/^git\s+worktree\b/,
|
|
86
|
+
/^git\s+checkout\s+-b\s+\S+/,
|
|
87
|
+
/^git\s+switch\s+-c\s+\S+/,
|
|
88
|
+
...protectedBranches.map(b => new RegExp(`^git\\s+reset\\s+--hard\\s+origin/${b}\\b`)),
|
|
89
|
+
/^gh\s+/,
|
|
90
|
+
/^bd\s+/,
|
|
91
|
+
/^touch\s+\.beads\//,
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
if (SAFE_BASH_PATTERNS.some(p => p.test(cmd))) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Specific blocks
|
|
99
|
+
if (/\bgit\s+commit\b/.test(cmd)) {
|
|
100
|
+
return { block: true, reason: `No commits on '${branch}' — use a feature branch.` };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (/\bgit\s+push\b/.test(cmd)) {
|
|
104
|
+
const tokens = cmd.split(/\s+/);
|
|
105
|
+
const lastToken = tokens[tokens.length - 1];
|
|
106
|
+
const explicitProtected = protectedBranches.some(b => lastToken === b || lastToken.endsWith(`:${b}`));
|
|
107
|
+
const impliedProtected = tokens.length <= 3 && protectedBranches.includes(branch);
|
|
108
|
+
|
|
109
|
+
if (explicitProtected || impliedProtected) {
|
|
110
|
+
return { block: true, reason: `No direct push to '${branch}' — push a feature branch and open a PR.` };
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Default deny
|
|
116
|
+
const reason = `Bash restricted on '${branch}'. Allowed: git status/log/diff/pull/stash, gh, bd.\n Exit: git checkout -b feature/<name>`;
|
|
117
|
+
if (ctx.hasUI) {
|
|
118
|
+
ctx.ui.notify("Main-Guard: Command blocked", "error");
|
|
119
|
+
}
|
|
120
|
+
return { block: true, reason };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return undefined;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolResultEvent } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { SubprocessRunner, EventAdapter, Logger } from "./core/lib";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
|
|
6
|
+
const logger = new Logger({ namespace: "quality-gates" });
|
|
7
|
+
|
|
8
|
+
export default function (pi: ExtensionAPI) {
|
|
9
|
+
pi.on("tool_result", async (event, ctx) => {
|
|
10
|
+
if (!EventAdapter.isMutatingFileTool(event)) return undefined;
|
|
11
|
+
|
|
12
|
+
const cwd = ctx.cwd || process.cwd();
|
|
13
|
+
const filePath = EventAdapter.extractPathFromToolInput(event, cwd);
|
|
14
|
+
if (!filePath) return undefined;
|
|
15
|
+
|
|
16
|
+
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(cwd, filePath);
|
|
17
|
+
const ext = path.extname(fullPath);
|
|
18
|
+
|
|
19
|
+
let scriptPath: string | null = null;
|
|
20
|
+
let runner: string = "node";
|
|
21
|
+
|
|
22
|
+
if ([".ts", ".tsx", ".js", ".jsx"].includes(ext)) {
|
|
23
|
+
scriptPath = path.join(cwd, ".claude", "hooks", "quality-check.cjs");
|
|
24
|
+
runner = "node";
|
|
25
|
+
} else if (ext === ".py") {
|
|
26
|
+
scriptPath = path.join(cwd, ".claude", "hooks", "quality-check.py");
|
|
27
|
+
runner = "python3";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!scriptPath || !fs.existsSync(scriptPath)) return undefined;
|
|
31
|
+
|
|
32
|
+
const hookInput = JSON.stringify({
|
|
33
|
+
tool_name: event.toolName,
|
|
34
|
+
tool_input: event.input,
|
|
35
|
+
cwd: cwd,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const result = await SubprocessRunner.run(runner, [scriptPath], {
|
|
39
|
+
cwd,
|
|
40
|
+
input: hookInput,
|
|
41
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: cwd },
|
|
42
|
+
timeoutMs: 30000,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (result.code === 0) {
|
|
46
|
+
if (result.stderr && result.stderr.trim()) {
|
|
47
|
+
const newContent = [...event.content];
|
|
48
|
+
newContent.push({ type: "text", text: `\n\n**Quality Gate**: ${result.stderr.trim()}` });
|
|
49
|
+
return { content: newContent };
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (result.code === 2) {
|
|
55
|
+
const newContent = [...event.content];
|
|
56
|
+
newContent.push({ type: "text", text: `\n\n**Quality Gate FAILED**:\n${result.stderr || result.stdout || "Unknown error"}` });
|
|
57
|
+
|
|
58
|
+
if (ctx.hasUI) {
|
|
59
|
+
ctx.ui.notify(`Quality Gate failed for ${path.basename(fullPath)}`, "error");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { isError: true, content: newContent };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return undefined;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ExtensionAPI, ToolCallEvent, ToolResultEvent } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { isToolCallEventType, isBashToolResult } from "@mariozechner/pi-coding-agent";
|
|
3
|
+
import { SubprocessRunner, Logger } from "./core/lib";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
|
|
7
|
+
const logger = new Logger({ namespace: "service-skills" });
|
|
8
|
+
|
|
9
|
+
export default function (pi: ExtensionAPI) {
|
|
10
|
+
const getCwd = (ctx: any) => ctx.cwd || process.cwd();
|
|
11
|
+
|
|
12
|
+
// 1. Catalog Injection
|
|
13
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
14
|
+
const cwd = getCwd(ctx);
|
|
15
|
+
const catalogerPath = path.join(cwd, ".claude", "skills", "using-service-skills", "scripts", "cataloger.py");
|
|
16
|
+
if (!fs.existsSync(catalogerPath)) return undefined;
|
|
17
|
+
|
|
18
|
+
const result = await SubprocessRunner.run("python3", [catalogerPath], {
|
|
19
|
+
cwd,
|
|
20
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: cwd }
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
if (result.code === 0 && result.stdout.trim()) {
|
|
24
|
+
return { systemPrompt: event.systemPrompt + "\n\n" + result.stdout.trim() };
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// 2. Territory Activation
|
|
30
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
31
|
+
const cwd = getCwd(ctx);
|
|
32
|
+
const activatorPath = path.join(cwd, ".claude", "skills", "using-service-skills", "scripts", "skill_activator.py");
|
|
33
|
+
if (!fs.existsSync(activatorPath)) return undefined;
|
|
34
|
+
|
|
35
|
+
const hookInput = JSON.stringify({
|
|
36
|
+
tool_name: event.toolName === "bash" ? "Bash" : event.toolName,
|
|
37
|
+
tool_input: event.input,
|
|
38
|
+
cwd: cwd
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const result = await SubprocessRunner.run("python3", [activatorPath], {
|
|
42
|
+
cwd,
|
|
43
|
+
input: hookInput,
|
|
44
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: cwd },
|
|
45
|
+
timeoutMs: 5000
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (result.code === 0 && result.stdout.trim()) {
|
|
49
|
+
try {
|
|
50
|
+
const parsed = JSON.parse(result.stdout.trim());
|
|
51
|
+
const context = parsed.hookSpecificOutput?.additionalContext;
|
|
52
|
+
if (context && ctx.hasUI) {
|
|
53
|
+
ctx.ui.notify(context, "info");
|
|
54
|
+
}
|
|
55
|
+
} catch (e) {
|
|
56
|
+
logger.error("Failed to parse skill_activator output", e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// 3. Drift Detection
|
|
63
|
+
pi.on("tool_result", async (event, ctx) => {
|
|
64
|
+
const cwd = getCwd(ctx);
|
|
65
|
+
const driftDetectorPath = path.join(cwd, ".claude", "skills", "updating-service-skills", "scripts", "drift_detector.py");
|
|
66
|
+
if (!fs.existsSync(driftDetectorPath)) return undefined;
|
|
67
|
+
|
|
68
|
+
const hookInput = JSON.stringify({
|
|
69
|
+
tool_name: event.toolName === "bash" ? "Bash" : event.toolName,
|
|
70
|
+
tool_input: event.input,
|
|
71
|
+
cwd: cwd
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const result = await SubprocessRunner.run("python3", [driftDetectorPath], {
|
|
75
|
+
cwd,
|
|
76
|
+
input: hookInput,
|
|
77
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: cwd },
|
|
78
|
+
timeoutMs: 10000
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (result.code === 0 && result.stdout.trim()) {
|
|
82
|
+
const newContent = [...event.content];
|
|
83
|
+
newContent.push({ type: "text", text: "\n\n" + result.stdout.trim() });
|
|
84
|
+
return { content: newContent };
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { Logger } from "./core/lib";
|
|
5
|
+
|
|
6
|
+
const logger = new Logger({ namespace: "xtrm-loader" });
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Recursively find markdown files in a directory.
|
|
10
|
+
*/
|
|
11
|
+
function findMarkdownFiles(dir: string, basePath: string = ""): string[] {
|
|
12
|
+
const results: string[] = [];
|
|
13
|
+
if (!fs.existsSync(dir)) return results;
|
|
14
|
+
|
|
15
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
16
|
+
for (const entry of entries) {
|
|
17
|
+
const relativePath = basePath ? `${basePath}/${entry.name}` : entry.name;
|
|
18
|
+
if (entry.isDirectory()) {
|
|
19
|
+
results.push(...findMarkdownFiles(path.join(dir, entry.name), relativePath));
|
|
20
|
+
} else if (entry.isFile() && entry.name.endsWith(".md")) {
|
|
21
|
+
results.push(relativePath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return results;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default function (pi: ExtensionAPI) {
|
|
28
|
+
let projectContext: string = "";
|
|
29
|
+
|
|
30
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
31
|
+
const cwd = ctx.cwd;
|
|
32
|
+
const contextParts: string[] = [];
|
|
33
|
+
|
|
34
|
+
// 1. Architecture & Roadmap
|
|
35
|
+
const roadmapPaths = [
|
|
36
|
+
path.join(cwd, "architecture", "project_roadmap.md"),
|
|
37
|
+
path.join(cwd, "ROADMAP.md"),
|
|
38
|
+
path.join(cwd, "architecture", "index.md")
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
for (const p of roadmapPaths) {
|
|
42
|
+
if (fs.existsSync(p)) {
|
|
43
|
+
const content = fs.readFileSync(p, "utf8");
|
|
44
|
+
contextParts.push(`## Project Roadmap & Architecture (${path.relative(cwd, p)})\n\n${content}`);
|
|
45
|
+
break; // Only load the first one found
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 2. Project Rules (.claude/rules)
|
|
50
|
+
const rulesDir = path.join(cwd, ".claude", "rules");
|
|
51
|
+
if (fs.existsSync(rulesDir)) {
|
|
52
|
+
const ruleFiles = findMarkdownFiles(rulesDir);
|
|
53
|
+
if (ruleFiles.length > 0) {
|
|
54
|
+
const rulesContent = ruleFiles.map(f => {
|
|
55
|
+
const content = fs.readFileSync(path.join(rulesDir, f), "utf8");
|
|
56
|
+
return `### Rule: ${f}\n${content}`;
|
|
57
|
+
}).join("\n\n");
|
|
58
|
+
contextParts.push(`## Project Rules\n\n${rulesContent}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 3. Project Skills (.claude/skills)
|
|
63
|
+
const skillsDir = path.join(cwd, ".claude", "skills");
|
|
64
|
+
if (fs.existsSync(skillsDir)) {
|
|
65
|
+
const skillFiles = findMarkdownFiles(skillsDir);
|
|
66
|
+
if (skillFiles.length > 0) {
|
|
67
|
+
const skillsContent = skillFiles.map(f => {
|
|
68
|
+
// We only want to list the paths/names so the agent knows what it can read
|
|
69
|
+
return `- ${f} (Path: .claude/skills/${f})`;
|
|
70
|
+
}).join("\n");
|
|
71
|
+
contextParts.push(`## Available Project Skills\n\nExisting service skills and workflows found in .claude/skills/:\n\n${skillsContent}\n\nUse the read tool to load any of these skills if relevant to the current task.`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
projectContext = contextParts.join("\n\n---\n\n");
|
|
76
|
+
|
|
77
|
+
if (projectContext && ctx.hasUI) {
|
|
78
|
+
ctx.ui.notify("XTRM-Loader: Project context and skills indexed", "info");
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
pi.on("before_agent_start", async (event) => {
|
|
83
|
+
if (!projectContext) return undefined;
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
systemPrompt: event.systemPrompt + "\n\n# Project Intelligence Context\n\n" + projectContext
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|