pi-cicd 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/AGENTS.md ADDED
@@ -0,0 +1,25 @@
1
+ # pi-ci Development Notes
2
+
3
+ Pi extension for headless CI mode.
4
+
5
+ ## Rules
6
+
7
+ - Keep `index.ts` minimal; re-export from `src/` modules.
8
+ - Avoid `any`; use `unknown` plus validation.
9
+ - After code changes, run `npm test` from `pi-ci/` unless explicitly told not to.
10
+
11
+ ## Important commands
12
+
13
+ ```bash
14
+ npm test
15
+ npm run typecheck
16
+ ```
17
+
18
+ ## Important paths
19
+
20
+ - `index.ts` — extension entry point
21
+ - `src/headless/` — core headless mode (exit codes, answers, idle, JSONL, orchestrator)
22
+ - `src/ci/` — CI pipeline, PR creation, test runner, reports
23
+ - `src/tools/` — /ci status command
24
+ - `src/config.ts` — configuration loading
25
+ - `test/unit/` — unit tests
package/index.ts ADDED
@@ -0,0 +1,52 @@
1
+ /**
2
+ * pi-ci — Pi extension entry point.
3
+ *
4
+ * Registers the /ci status command and CI lifecycle hooks.
5
+ */
6
+
7
+ import type { CIEvent, CIOptions, ExitCode } from "./src/types.ts";
8
+ import { EXIT_CODES } from "./src/types.ts";
9
+ import {
10
+ ciStatusHandler,
11
+ createRunTracker,
12
+ clearRuns,
13
+ registerRun,
14
+ type CIRunRecord,
15
+ } from "./src/tools/ci_status.ts";
16
+ import { CIPipeline, type PipelineResult } from "./src/ci/pipeline.ts";
17
+ import { generateReport } from "./src/ci/report.ts";
18
+
19
+ // Re-export for consumers
20
+ export { EXIT_CODES } from "./src/types.ts";
21
+ export { resolveExitCode } from "./src/headless/exit-codes.ts";
22
+ export { loadAnswers, matchAnswer, parseAnswers } from "./src/headless/answer-injector.ts";
23
+ export { IdleDetector } from "./src/headless/idle-detector.ts";
24
+ export { CIEventCollector, writeCIEvent } from "./src/headless/jsonl-stream.ts";
25
+ export { HeadlessOrchestrator } from "./src/headless/orchestrator.ts";
26
+ export { CIPipeline } from "./src/ci/pipeline.ts";
27
+ export { createPR, detectBaseBranch } from "./src/ci/pr-creator.ts";
28
+ export { parseTestResults } from "./src/ci/test-runner.ts";
29
+ export { generateReport } from "./src/ci/report.ts";
30
+ export { ciStatusHandler, registerRun, clearRuns, createRunTracker } from "./src/tools/ci_status.ts";
31
+ export { loadCiConfig, DEFAULT_CONFIG } from "./src/config.ts";
32
+ export type { PiCiConfig } from "./src/config.ts";
33
+
34
+ /**
35
+ * Extension API type (minimal — avoids hard dep on pi-coding-agent types).
36
+ */
37
+ interface ExtensionAPI {
38
+ registerCommand?: (name: string, handler: (args: unknown) => string | Promise<string>) => void;
39
+ on?: (event: string, handler: (...args: unknown[]) => void) => void;
40
+ }
41
+
42
+ /**
43
+ * Default export — Pi extension registration.
44
+ */
45
+ export default function piCiExtension(pi: ExtensionAPI): void {
46
+ // Register /ci status command
47
+ if (pi.registerCommand) {
48
+ pi.registerCommand("ci", (args: unknown) => {
49
+ return ciStatusHandler(args);
50
+ });
51
+ }
52
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "pi-cicd",
3
+ "version": "0.1.1",
4
+ "description": "Pi extension for headless CI mode with structured exit codes, answer injection, and pipeline automation",
5
+ "author": "baphuongna",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "keywords": [
9
+ "pi-package",
10
+ "pi",
11
+ "pi-coding-agent",
12
+ "ci",
13
+ "headless",
14
+ "automation"
15
+ ],
16
+ "files": [
17
+ "*.ts",
18
+ "src/**/*.ts",
19
+ "README.md",
20
+ "AGENTS.md",
21
+ "tsconfig.json"
22
+ ],
23
+ "scripts": {
24
+ "check": "npm run typecheck && npm test",
25
+ "typecheck": "tsc --noEmit",
26
+ "test": "node --experimental-strip-types --test --test-concurrency=1 --test-timeout=30000 test/unit/*.test.ts"
27
+ },
28
+ "pi": {
29
+ "extensions": [
30
+ "./index.ts"
31
+ ]
32
+ },
33
+ "peerDependencies": {
34
+ "@mariozechner/pi-coding-agent": "*"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "@mariozechner/pi-coding-agent": {
38
+ "optional": true
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * pi-ci — CI pipeline wrapper.
3
+ *
4
+ * Provides single, plan, review, and supervised execution modes.
5
+ */
6
+
7
+ import type { CIEvent, CIOptions, CIPipelineMode, ExitCode, TestSummary } from "../types.ts";
8
+ import { EXIT_CODES } from "../types.ts";
9
+ import { HeadlessOrchestrator, type OrchestratorHooks } from "../headless/orchestrator.ts";
10
+ import { loadAnswers } from "../headless/answer-injector.ts";
11
+ import { parseTestResults } from "./test-runner.ts";
12
+ import { generateReport } from "./report.ts";
13
+
14
+ export interface PipelineResult {
15
+ exitCode: ExitCode;
16
+ events: CIEvent[];
17
+ report: string;
18
+ testSummary?: TestSummary;
19
+ }
20
+
21
+ export class CIPipeline {
22
+ private readonly options: CIOptions;
23
+ private readonly hooks: OrchestratorHooks;
24
+
25
+ constructor(options: CIOptions, hooks: OrchestratorHooks) {
26
+ this.options = options;
27
+ this.hooks = hooks;
28
+ }
29
+
30
+ /**
31
+ * Execute the pipeline in the configured mode.
32
+ */
33
+ async execute(): Promise<PipelineResult> {
34
+ switch (this.options.mode) {
35
+ case "single":
36
+ return this.executeSingle();
37
+ case "plan":
38
+ return this.executePlan();
39
+ case "review":
40
+ return this.executeReview();
41
+ case "supervised":
42
+ return this.executeSupervised();
43
+ default:
44
+ throw new Error(`Unknown CI pipeline mode: ${this.options.mode as string}`);
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Single task mode — run one prompt to completion.
50
+ */
51
+ private async executeSingle(): Promise<PipelineResult> {
52
+ const answers = await this.loadAnswers();
53
+ const orchestrator = new HeadlessOrchestrator(answers, this.options, this.hooks);
54
+ const result = await orchestrator.run(this.options.prompt, "single");
55
+
56
+ return {
57
+ exitCode: result.exitCode,
58
+ events: result.events,
59
+ report: generateReport(result.events),
60
+ };
61
+ }
62
+
63
+ /**
64
+ * Plan mode — execute steps from a plan file.
65
+ *
66
+ * Each step becomes a sequential orchestrator invocation. If any step fails,
67
+ * the pipeline stops.
68
+ */
69
+ private async executePlan(): Promise<PipelineResult> {
70
+ const allEvents: CIEvent[] = [];
71
+ let finalExitCode: ExitCode = EXIT_CODES.SUCCESS;
72
+ let totalDuration = 0;
73
+
74
+ // Plan steps are provided via the executeStep hook — the orchestrator
75
+ // iterates over steps internally.
76
+ const answers = await this.loadAnswers();
77
+ const orchestrator = new HeadlessOrchestrator(answers, this.options, this.hooks);
78
+ const result = await orchestrator.run(this.options.prompt, "plan");
79
+
80
+ allEvents.push(...result.events);
81
+ finalExitCode = result.exitCode;
82
+ totalDuration = result.durationMs;
83
+
84
+ return {
85
+ exitCode: finalExitCode,
86
+ events: allEvents,
87
+ report: generateReport(allEvents),
88
+ };
89
+ }
90
+
91
+ /**
92
+ * PR review mode — review a PR by number.
93
+ */
94
+ private async executeReview(): Promise<PipelineResult> {
95
+ const answers = await this.loadAnswers();
96
+ const orchestrator = new HeadlessOrchestrator(answers, this.options, this.hooks);
97
+ const prompt = this.options.prNumber
98
+ ? `Review PR #${this.options.prNumber}`
99
+ : this.options.prompt;
100
+ const result = await orchestrator.run(prompt, "single");
101
+
102
+ return {
103
+ exitCode: result.exitCode,
104
+ events: result.events,
105
+ report: generateReport(result.events),
106
+ };
107
+ }
108
+
109
+ /**
110
+ * Supervised mode — stdin/stdout forwarding for an external orchestrator.
111
+ */
112
+ private async executeSupervised(): Promise<PipelineResult> {
113
+ const answers = await this.loadAnswers();
114
+ const orchestrator = new HeadlessOrchestrator(answers, this.options, this.hooks);
115
+ const result = await orchestrator.run(this.options.prompt, "single");
116
+
117
+ return {
118
+ exitCode: result.exitCode,
119
+ events: result.events,
120
+ report: generateReport(result.events),
121
+ };
122
+ }
123
+
124
+ private async loadAnswers() {
125
+ if (this.options.answersFile) {
126
+ return loadAnswers(this.options.answersFile);
127
+ }
128
+ return [];
129
+ }
130
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * pi-ci — PR creation via the GitHub CLI (`gh`).
3
+ *
4
+ * Wraps `gh pr create` with structured error handling.
5
+ */
6
+
7
+ import { execFile } from "node:child_process";
8
+ import type { PROptions, PRResult } from "../types.ts";
9
+
10
+ /**
11
+ * Create a pull request using the `gh` CLI.
12
+ *
13
+ * Throws if `gh` is not installed or the command fails.
14
+ */
15
+ export async function createPR(options: PROptions): Promise<PRResult> {
16
+ const args = ["pr", "create", "--title", options.title];
17
+
18
+ if (options.body) {
19
+ args.push("--body", options.body);
20
+ }
21
+ if (options.base) {
22
+ args.push("--base", options.base);
23
+ }
24
+ if (options.head) {
25
+ args.push("--head", options.head);
26
+ }
27
+ if (options.draft) {
28
+ args.push("--draft");
29
+ }
30
+ for (const label of options.labels ?? []) {
31
+ args.push("--label", label);
32
+ }
33
+
34
+ const output = await runGh(args);
35
+
36
+ // Parse the PR URL from the output
37
+ const urlMatch = output.match(/https:\/\/github\.com\/[^\s]+\/pull\/(\d+)/);
38
+ if (!urlMatch) {
39
+ throw new Error(`Failed to parse PR URL from gh output: ${output}`);
40
+ }
41
+
42
+ return {
43
+ url: urlMatch[0],
44
+ number: parseInt(urlMatch[1], 10),
45
+ };
46
+ }
47
+
48
+ /**
49
+ * Detect the default (base) branch for the current repository.
50
+ */
51
+ export async function detectBaseBranch(): Promise<string> {
52
+ const output = await runGh(["repo", "view", "--json", "defaultBranchRef", "--jq", ".defaultBranchRef.name"]);
53
+ return output.trim() || "main";
54
+ }
55
+
56
+ /**
57
+ * Execute a `gh` command and return stdout.
58
+ */
59
+ function runGh(args: string[]): Promise<string> {
60
+ return new Promise((resolve, reject) => {
61
+ execFile("gh", args, { timeout: 60_000 }, (err, stdout, stderr) => {
62
+ if (err) {
63
+ const message = stderr?.trim() || err.message;
64
+ if (err.code === "ENOENT") {
65
+ reject(new Error("gh CLI is not installed. Install it from https://cli.github.com"));
66
+ } else {
67
+ reject(new Error(`gh ${args.join(" ")} failed: ${message}`));
68
+ }
69
+ return;
70
+ }
71
+ resolve(stdout);
72
+ });
73
+ });
74
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * pi-ci — CI report generation.
3
+ *
4
+ * Produces JSONL or human-readable summary reports from collected CI events.
5
+ */
6
+
7
+ import type { CIEvent, CIEndEvent, CICostEvent, CITestEvent } from "../types.ts";
8
+ import { isCIEndEvent, isCICostEvent, isCITestEvent } from "../headless/jsonl-stream.ts";
9
+
10
+ export interface ReportOptions {
11
+ format: "jsonl" | "summary";
12
+ }
13
+
14
+ /**
15
+ * Generate a report from a list of CI events.
16
+ */
17
+ export function generateReport(events: CIEvent[], options?: ReportOptions): string {
18
+ const format = options?.format ?? "jsonl";
19
+
20
+ if (format === "summary") {
21
+ return generateSummary(events);
22
+ }
23
+
24
+ // JSONL: one event per line
25
+ return events.map((e) => JSON.stringify(e)).join("\n");
26
+ }
27
+
28
+ /**
29
+ * Generate a human-readable summary.
30
+ */
31
+ function generateSummary(events: CIEvent[]): string {
32
+ const lines: string[] = ["=== CI Run Summary ===", ""];
33
+
34
+ let totalTestsPassed = 0;
35
+ let totalTestsFailed = 0;
36
+ let totalCostUsd = 0;
37
+ let exitCode = -1;
38
+ let durationMs = 0;
39
+
40
+ for (const event of events) {
41
+ if (isCITestEvent(event)) {
42
+ totalTestsPassed += event.passed;
43
+ totalTestsFailed += event.failed;
44
+ }
45
+ if (isCICostEvent(event)) {
46
+ totalCostUsd += event.cost_usd;
47
+ }
48
+ if (isCIEndEvent(event)) {
49
+ exitCode = event.exit_code;
50
+ durationMs = event.duration_ms;
51
+ }
52
+ }
53
+
54
+ lines.push(`Exit Code: ${exitCode}`);
55
+ lines.push(`Duration: ${(durationMs / 1000).toFixed(1)}s`);
56
+ lines.push(`Tests: ${totalTestsPassed} passed, ${totalTestsFailed} failed`);
57
+ if (totalCostUsd > 0) {
58
+ lines.push(`Cost: $${totalCostUsd.toFixed(4)}`);
59
+ }
60
+
61
+ const status = exitCode === 0 ? "SUCCESS" : exitCode === 10 ? "BLOCKED" : exitCode === 11 ? "CANCELLED" : "ERROR";
62
+ lines.push(`Status: ${status}`);
63
+
64
+ return lines.join("\n");
65
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * pi-ci — Test result parsing for TAP, Jest, and Vitest output formats.
3
+ */
4
+
5
+ import type { TestSummary } from "../types.ts";
6
+
7
+ export type TestOutputFormat = "tap" | "jest" | "vitest";
8
+
9
+ /**
10
+ * Parse test output and return a summary.
11
+ */
12
+ export function parseTestResults(output: string, format: TestOutputFormat): TestSummary {
13
+ if (!output || !output.trim()) {
14
+ return { passed: 0, failed: 0, total: 0, duration_ms: 0 };
15
+ }
16
+
17
+ switch (format) {
18
+ case "tap":
19
+ return parseTap(output);
20
+ case "jest":
21
+ return parseJest(output);
22
+ case "vitest":
23
+ return parseVitest(output);
24
+ default:
25
+ return { passed: 0, failed: 0, total: 0, duration_ms: 0 };
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Parse TAP (Test Anything Protocol) output.
31
+ *
32
+ * Example:
33
+ * 1..5
34
+ * ok 1 - first test
35
+ * not ok 2 - failing test
36
+ * ok 3 - third test
37
+ */
38
+ function parseTap(output: string): TestSummary {
39
+ let passed = 0;
40
+ let failed = 0;
41
+
42
+ const lines = output.split("\n");
43
+ for (const line of lines) {
44
+ const trimmed = line.trim();
45
+ if (trimmed.startsWith("ok ")) {
46
+ passed++;
47
+ } else if (trimmed.startsWith("not ok ")) {
48
+ failed++;
49
+ }
50
+ }
51
+
52
+ return {
53
+ passed,
54
+ failed,
55
+ total: passed + failed,
56
+ duration_ms: 0, // TAP doesn't have standard duration
57
+ };
58
+ }
59
+
60
+ /**
61
+ * Parse Jest-style test output.
62
+ *
63
+ * Looks for patterns like:
64
+ * Tests: 5 passed, 2 failed, 7 total
65
+ * Time: 3.456 s
66
+ */
67
+ function parseJest(output: string): TestSummary {
68
+ let passed = 0;
69
+ let failed = 0;
70
+ let total = 0;
71
+ let durationMs = 0;
72
+
73
+ // Match test summary line
74
+ const testMatch = output.match(
75
+ /Tests:\s*(\d+)\s*passed(?:,\s*(\d+)\s*failed)?(?:,\s*(\d+)\s*total)?/,
76
+ );
77
+ if (testMatch) {
78
+ passed = parseInt(testMatch[1], 10);
79
+ failed = parseInt(testMatch[2] || "0", 10);
80
+ total = parseInt(testMatch[3] || String(passed + failed), 10);
81
+ } else {
82
+ // Fallback: count individual test lines
83
+ const passMatches = output.match(/✓|PASS/g);
84
+ const failMatches = output.match(/✕|FAIL/g);
85
+ passed = passMatches?.length ?? 0;
86
+ failed = failMatches?.length ?? 0;
87
+ total = passed + failed;
88
+ }
89
+
90
+ // Match time
91
+ const timeMatch = output.match(/Time:\s*([\d.]+)\s*s/);
92
+ if (timeMatch) {
93
+ durationMs = Math.round(parseFloat(timeMatch[1]) * 1000);
94
+ }
95
+
96
+ return { passed, failed, total, duration_ms: durationMs };
97
+ }
98
+
99
+ /**
100
+ * Parse Vitest-style test output.
101
+ *
102
+ * Looks for patterns like:
103
+ * Tests 5 passed | 2 failed (7)
104
+ * Duration 3.45s
105
+ */
106
+ function parseVitest(output: string): TestSummary {
107
+ let passed = 0;
108
+ let failed = 0;
109
+ let total = 0;
110
+ let durationMs = 0;
111
+
112
+ // Match test summary line: "Tests 5 passed | 2 failed (7)"
113
+ const testMatch = output.match(
114
+ /Tests\s+(\d+)\s+passed(?:\s*\|\s*(\d+)\s+failed)?(?:\s*\((\d+)\))?/,
115
+ );
116
+ if (testMatch) {
117
+ passed = parseInt(testMatch[1], 10);
118
+ failed = parseInt(testMatch[2] || "0", 10);
119
+ total = parseInt(testMatch[3] || String(passed + failed), 10);
120
+ }
121
+
122
+ // Match duration: "Duration 3.45s"
123
+ const durMatch = output.match(/Duration\s+([\d.]+)s/);
124
+ if (durMatch) {
125
+ durationMs = Math.round(parseFloat(durMatch[1]) * 1000);
126
+ }
127
+
128
+ return { passed, failed, total, duration_ms: durationMs };
129
+ }
package/src/config.ts ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * pi-ci — Configuration loading and defaults.
3
+ *
4
+ * Reads `.pi/pi-ci.json` from the given directory (or cwd) and merges with defaults.
5
+ */
6
+
7
+ import { readFile } from "node:fs/promises";
8
+ import { join } from "node:path";
9
+
10
+ export interface PiCiReportConfig {
11
+ format: "jsonl" | "summary";
12
+ includeCost: boolean;
13
+ includeEdits: boolean;
14
+ includeTests: boolean;
15
+ }
16
+
17
+ export interface PiCiExitCodeConfig {
18
+ success: number;
19
+ error: number;
20
+ blocked: number;
21
+ cancelled: number;
22
+ needsInput: number;
23
+ }
24
+
25
+ export interface PiCiConfig {
26
+ enabled: boolean;
27
+ idleTimeoutMs: number;
28
+ maxRetries: number;
29
+ retryBackoffMaxMs: number;
30
+ exitCodes: PiCiExitCodeConfig;
31
+ report: PiCiReportConfig;
32
+ }
33
+
34
+ const DEFAULT_CONFIG: PiCiConfig = {
35
+ enabled: true,
36
+ idleTimeoutMs: 15_000,
37
+ maxRetries: 3,
38
+ retryBackoffMaxMs: 30_000,
39
+ exitCodes: {
40
+ success: 0,
41
+ error: 1,
42
+ blocked: 10,
43
+ cancelled: 11,
44
+ needsInput: 12,
45
+ },
46
+ report: {
47
+ format: "jsonl",
48
+ includeCost: true,
49
+ includeEdits: true,
50
+ includeTests: true,
51
+ },
52
+ };
53
+
54
+ /**
55
+ * Load pi-ci configuration from `.pi/pi-ci.json` (if present) merged with defaults.
56
+ */
57
+ export async function loadCiConfig(cwd?: string): Promise<PiCiConfig> {
58
+ const dir = cwd ?? process.cwd();
59
+ const configPath = join(dir, ".pi", "pi-ci.json");
60
+
61
+ let text: string;
62
+ try {
63
+ text = await readFile(configPath, "utf-8");
64
+ } catch {
65
+ return { ...DEFAULT_CONFIG };
66
+ }
67
+
68
+ const raw: unknown = JSON.parse(text);
69
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
70
+ return { ...DEFAULT_CONFIG };
71
+ }
72
+
73
+ const user = raw as Record<string, unknown>;
74
+ return {
75
+ enabled: typeof user.enabled === "boolean" ? user.enabled : DEFAULT_CONFIG.enabled,
76
+ idleTimeoutMs:
77
+ typeof user.idleTimeoutMs === "number" ? user.idleTimeoutMs : DEFAULT_CONFIG.idleTimeoutMs,
78
+ maxRetries:
79
+ typeof user.maxRetries === "number" ? user.maxRetries : DEFAULT_CONFIG.maxRetries,
80
+ retryBackoffMaxMs:
81
+ typeof user.retryBackoffMaxMs === "number"
82
+ ? user.retryBackoffMaxMs
83
+ : DEFAULT_CONFIG.retryBackoffMaxMs,
84
+ exitCodes: {
85
+ ...DEFAULT_CONFIG.exitCodes,
86
+ ...(typeof user.exitCodes === "object" && user.exitCodes !== null
87
+ ? (user.exitCodes as Partial<PiCiExitCodeConfig>)
88
+ : {}),
89
+ },
90
+ report: {
91
+ ...DEFAULT_CONFIG.report,
92
+ ...(typeof user.report === "object" && user.report !== null
93
+ ? (user.report as Partial<PiCiReportConfig>)
94
+ : {}),
95
+ },
96
+ };
97
+ }
98
+
99
+ export { DEFAULT_CONFIG };