pi-pstack-workflow-kit 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.
- package/LICENSE +21 -0
- package/README.md +122 -2
- package/agents/ci-watcher.md +1 -0
- package/agents/comment-sicko.md +1 -0
- package/agents/poteto-agent.md +1 -0
- package/agents/quality-agent.md +1 -0
- package/agents/worker.md +3 -0
- package/extensions/catalog-ui.ts +86 -0
- package/extensions/delegate.ts +357 -0
- package/extensions/forge.ts +245 -0
- package/extensions/git.ts +23 -0
- package/extensions/index.ts +25 -0
- package/extensions/models.ts +115 -0
- package/extensions/parent-diff.ts +63 -0
- package/extensions/plan.ts +181 -0
- package/extensions/records.ts +81 -0
- package/extensions/session.ts +124 -0
- package/extensions/support.ts +46 -0
- package/extensions/worktrees.ts +188 -0
- package/lib/catalog-data.json +560 -0
- package/lib/catalog.ts +111 -0
- package/lib/jobs.ts +5 -0
- package/lib/models.ts +124 -0
- package/lib/pool.ts +29 -0
- package/lib/state.ts +127 -0
- package/lib/transcript.ts +44 -0
- package/lib/validation.ts +16 -0
- package/package.json +68 -4
- package/reference/GAPS.md +21 -0
- package/reference/PARITY.md +169 -0
- package/reference/benny/config.example.json +12 -0
- package/reference/benny/repro.md +3 -0
- package/reference/benny/setup.md +3 -0
- package/reference/benny/triage.md +3 -0
- package/reference/parity.json +3095 -0
- package/reference/rules/no-inline-imports.md +3 -0
- package/reference/rules/typescript-exhaustive-switch.md +3 -0
- package/reference/source-map.json +8594 -0
- package/scripts/job-input.ts +191 -0
- package/scripts/run-job.mjs +119 -0
- package/skills/architect/SKILL.md +16 -0
- package/skills/arena/SKILL.md +16 -0
- package/skills/automate-me/SKILL.md +10 -0
- package/skills/benchmark-checklist/SKILL.md +10 -0
- package/skills/blast-radius/SKILL.md +10 -0
- package/skills/bro/SKILL.md +10 -0
- package/skills/check-compiler-errors/SKILL.md +10 -0
- package/skills/control-cli/SKILL.md +10 -0
- package/skills/control-ui/SKILL.md +10 -0
- package/skills/correct/SKILL.md +10 -0
- package/skills/create-formal-verification-skill/SKILL.md +16 -0
- package/skills/create-verification-skill/SKILL.md +16 -0
- package/skills/deslop/SKILL.md +10 -0
- package/skills/figure-it-out/SKILL.md +17 -0
- package/skills/fix-ci/SKILL.md +10 -0
- package/skills/fix-merge-conflicts/SKILL.md +10 -0
- package/skills/get-pr-comments/SKILL.md +10 -0
- package/skills/how/SKILL.md +10 -0
- package/skills/in-your-own-words/SKILL.md +11 -0
- package/skills/interrogate/SKILL.md +17 -0
- package/skills/ketchup/SKILL.md +10 -0
- package/skills/ketchup-video/SKILL.md +10 -0
- package/skills/loop-on-ci/SKILL.md +10 -0
- package/skills/maintain-formal-verification-skill/SKILL.md +16 -0
- package/skills/maintain-verification-skill/SKILL.md +16 -0
- package/skills/make-bot-ui/SKILL.md +10 -0
- package/skills/make-pr-easy-to-review/SKILL.md +10 -0
- package/skills/new-branch-and-pr/SKILL.md +10 -0
- package/skills/no-comments/SKILL.md +16 -0
- package/skills/poteto-help/SKILL.md +11 -0
- package/skills/poteto-mode/SKILL.md +60 -0
- package/skills/poteto-mode/playbooks/authoring-a-skill.md +20 -0
- package/skills/poteto-mode/playbooks/autonomous-run.md +20 -0
- package/skills/poteto-mode/playbooks/autopilot-full.md +25 -0
- package/skills/poteto-mode/playbooks/autopilot-stack.md +20 -0
- package/skills/poteto-mode/playbooks/babysit.md +21 -0
- package/skills/poteto-mode/playbooks/bug-fix.md +26 -0
- package/skills/poteto-mode/playbooks/eval.md +20 -0
- package/skills/poteto-mode/playbooks/feature.md +26 -0
- package/skills/poteto-mode/playbooks/hillclimb.md +24 -0
- package/skills/poteto-mode/playbooks/investigation.md +19 -0
- package/skills/poteto-mode/playbooks/multi-phase-plan.md +19 -0
- package/skills/poteto-mode/playbooks/opening-a-pr.md +20 -0
- package/skills/poteto-mode/playbooks/pause-safely.md +18 -0
- package/skills/poteto-mode/playbooks/perf-issue.md +24 -0
- package/skills/poteto-mode/playbooks/prototype.md +21 -0
- package/skills/poteto-mode/playbooks/refactoring.md +23 -0
- package/skills/poteto-mode/playbooks/runtime-forensics.md +20 -0
- package/skills/poteto-mode/playbooks/session-pickup.md +20 -0
- package/skills/poteto-mode/playbooks/shipping.md +24 -0
- package/skills/poteto-mode/playbooks/trace-forensics.md +20 -0
- package/skills/poteto-mode/playbooks/visual-parity.md +22 -0
- package/skills/poteto-mode/playbooks/worktree-cleanup.md +19 -0
- package/skills/pr-review-canvas/SKILL.md +10 -0
- package/skills/principle-attack-the-premise/SKILL.md +12 -0
- package/skills/principle-boundary-discipline/SKILL.md +12 -0
- package/skills/principle-build-the-lever/SKILL.md +12 -0
- package/skills/principle-encode-lessons-in-structure/SKILL.md +12 -0
- package/skills/principle-exhaust-the-design-space/SKILL.md +12 -0
- package/skills/principle-experience-first/SKILL.md +12 -0
- package/skills/principle-explain-the-number/SKILL.md +12 -0
- package/skills/principle-fix-root-causes/SKILL.md +12 -0
- package/skills/principle-foundational-thinking/SKILL.md +12 -0
- package/skills/principle-guard-the-context-window/SKILL.md +12 -0
- package/skills/principle-laziness-protocol/SKILL.md +12 -0
- package/skills/principle-make-operations-idempotent/SKILL.md +12 -0
- package/skills/principle-migrate-callers-then-delete-legacy-apis/SKILL.md +12 -0
- package/skills/principle-minimize-reader-load/SKILL.md +12 -0
- package/skills/principle-model-the-domain/SKILL.md +12 -0
- package/skills/principle-never-block-on-the-human/SKILL.md +12 -0
- package/skills/principle-outcome-oriented-execution/SKILL.md +12 -0
- package/skills/principle-prove-it-works/SKILL.md +12 -0
- package/skills/principle-redesign-from-first-principles/SKILL.md +12 -0
- package/skills/principle-separate-before-serializing-shared-state/SKILL.md +12 -0
- package/skills/principle-sequence-verifiable-units/SKILL.md +12 -0
- package/skills/principle-subtract-before-you-add/SKILL.md +12 -0
- package/skills/principle-test-behavior-not-implementation/SKILL.md +12 -0
- package/skills/principle-type-system-discipline/SKILL.md +12 -0
- package/skills/recall/SKILL.md +10 -0
- package/skills/reflect/SKILL.md +10 -0
- package/skills/review-and-ship/SKILL.md +10 -0
- package/skills/run-smoke-tests/SKILL.md +10 -0
- package/skills/setup-pstack/SKILL.md +17 -0
- package/skills/sharpen/SKILL.md +11 -0
- package/skills/show-me-your-work/SKILL.md +16 -0
- package/skills/statechart/SKILL.md +10 -0
- package/skills/swarm/SKILL.md +16 -0
- package/skills/tdd/SKILL.md +10 -0
- package/skills/teach/SKILL.md +17 -0
- package/skills/technical-writing/SKILL.md +10 -0
- package/skills/thermo-nuclear-code-quality-review/SKILL.md +10 -0
- package/skills/typescript-best-practices/SKILL.md +10 -0
- package/skills/unslop/SKILL.md +10 -0
- package/skills/verify-this/SKILL.md +17 -0
- package/skills/weekly-review/SKILL.md +10 -0
- package/skills/what-did-i-get-done/SKILL.md +10 -0
- package/skills/why/SKILL.md +10 -0
- package/skills/workflow-from-chats/SKILL.md +10 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
|
|
4
|
+
import { errorMessage, isRecord, parseJson } from "../lib/validation.ts";
|
|
5
|
+
import { fail, result, text } from "./support.ts";
|
|
6
|
+
|
|
7
|
+
const checksFields =
|
|
8
|
+
"number,url,headRefOid,state,isDraft,mergeable,mergeStateStatus,statusCheckRollup,reviewDecision,baseRefName";
|
|
9
|
+
|
|
10
|
+
export interface Proof {
|
|
11
|
+
number: number;
|
|
12
|
+
head: string;
|
|
13
|
+
evidence: string;
|
|
14
|
+
independent: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface PullRequestStatus {
|
|
17
|
+
number: number;
|
|
18
|
+
url: string;
|
|
19
|
+
headRefOid: string;
|
|
20
|
+
state: string;
|
|
21
|
+
isDraft: boolean;
|
|
22
|
+
mergeable: string;
|
|
23
|
+
mergeStateStatus: string;
|
|
24
|
+
statusCheckRollup: { status?: string; conclusion?: string; state?: string }[] | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const passingConclusions = new Set(["SUCCESS", "NEUTRAL", "SKIPPED"]);
|
|
28
|
+
const checksPass = (pr: PullRequestStatus) =>
|
|
29
|
+
!!pr.statusCheckRollup?.length &&
|
|
30
|
+
pr.statusCheckRollup.every((check) =>
|
|
31
|
+
check.status === "COMPLETED"
|
|
32
|
+
? passingConclusions.has(check.conclusion ?? "")
|
|
33
|
+
: check.state === "SUCCESS",
|
|
34
|
+
);
|
|
35
|
+
const mergeReady = (proof: Proof, pr: PullRequestStatus) =>
|
|
36
|
+
!!proof.evidence?.trim() &&
|
|
37
|
+
proof.independent &&
|
|
38
|
+
proof.head === pr.headRefOid &&
|
|
39
|
+
pr.state === "OPEN" &&
|
|
40
|
+
!pr.isDraft &&
|
|
41
|
+
pr.mergeable === "MERGEABLE" &&
|
|
42
|
+
pr.mergeStateStatus === "CLEAN" &&
|
|
43
|
+
checksPass(pr);
|
|
44
|
+
|
|
45
|
+
type Approved = Proof & { url: string };
|
|
46
|
+
|
|
47
|
+
export function verifiedPrefix(proofs: Proof[], statuses: PullRequestStatus[]): Approved[] {
|
|
48
|
+
const byNumber = new Map(statuses.map((status) => [status.number, status]));
|
|
49
|
+
const prefix: Approved[] = [];
|
|
50
|
+
for (const proof of proofs) {
|
|
51
|
+
const pr = byNumber.get(proof.number);
|
|
52
|
+
if (!pr || !mergeReady(proof, pr)) break;
|
|
53
|
+
const { number, head, evidence, independent } = proof;
|
|
54
|
+
prefix.push({ number, head, evidence, independent, url: pr.url });
|
|
55
|
+
}
|
|
56
|
+
return prefix;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function prUrl(value: unknown, number: number): value is string {
|
|
60
|
+
return (
|
|
61
|
+
typeof value === "string" &&
|
|
62
|
+
/^https:\/\/[a-z0-9.:-]+\/[a-z0-9_.-]+\/[a-z0-9_.-]+\/pull\/\d+$/i.test(value) &&
|
|
63
|
+
value.endsWith(`/pull/${number}`)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isStatus(value: unknown, number: number): value is PullRequestStatus {
|
|
68
|
+
return (
|
|
69
|
+
isRecord(value) &&
|
|
70
|
+
value.number === number &&
|
|
71
|
+
prUrl(value.url, number) &&
|
|
72
|
+
typeof value.headRefOid === "string" &&
|
|
73
|
+
/^[0-9a-f]{40}$/i.test(value.headRefOid) &&
|
|
74
|
+
typeof value.state === "string" &&
|
|
75
|
+
typeof value.isDraft === "boolean" &&
|
|
76
|
+
typeof value.mergeable === "string" &&
|
|
77
|
+
typeof value.mergeStateStatus === "string" &&
|
|
78
|
+
(value.statusCheckRollup === null ||
|
|
79
|
+
(Array.isArray(value.statusCheckRollup) &&
|
|
80
|
+
value.statusCheckRollup.every(
|
|
81
|
+
(check: unknown) =>
|
|
82
|
+
isRecord(check) &&
|
|
83
|
+
[check.status, check.conclusion, check.state].every(
|
|
84
|
+
(field) => field === undefined || typeof field === "string",
|
|
85
|
+
),
|
|
86
|
+
)))
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function fetchPr(
|
|
91
|
+
pi: ExtensionAPI,
|
|
92
|
+
ctx: ExtensionContext,
|
|
93
|
+
id: number | string,
|
|
94
|
+
signal?: AbortSignal,
|
|
95
|
+
) {
|
|
96
|
+
const r = await pi.exec("gh", ["pr", "view", String(id), "--json", checksFields], {
|
|
97
|
+
cwd: ctx.cwd,
|
|
98
|
+
signal,
|
|
99
|
+
timeout: 30000,
|
|
100
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
101
|
+
});
|
|
102
|
+
if (r.truncated || r.killed) fail("gh pr view failed: output truncated or process stopped");
|
|
103
|
+
if (r.code !== 0) fail(`gh pr view failed: ${r.stderr}`);
|
|
104
|
+
const payload = parseJson(r.stdout, "gh pr response");
|
|
105
|
+
const number = typeof id === "number" ? id : Number(id.split("/").at(-1));
|
|
106
|
+
if (!isStatus(payload, number) || (typeof id === "string" && payload.url !== id)) {
|
|
107
|
+
fail(`Unexpected GitHub PR response for ${id}`);
|
|
108
|
+
}
|
|
109
|
+
return payload;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const mergeRoutes = ["shipping", "autopilot-full"];
|
|
113
|
+
|
|
114
|
+
async function previewPrefix(
|
|
115
|
+
pi: ExtensionAPI,
|
|
116
|
+
ctx: ExtensionContext,
|
|
117
|
+
proofs: Proof[],
|
|
118
|
+
signal?: AbortSignal,
|
|
119
|
+
) {
|
|
120
|
+
signal?.throwIfAborted();
|
|
121
|
+
const reads = await Promise.allSettled(
|
|
122
|
+
proofs.map((proof) => fetchPr(pi, ctx, proof.number, signal)),
|
|
123
|
+
);
|
|
124
|
+
signal?.throwIfAborted();
|
|
125
|
+
const statuses = reads.flatMap((read) => (read.status === "fulfilled" ? [read.value] : []));
|
|
126
|
+
const prefix = verifiedPrefix(proofs, statuses);
|
|
127
|
+
const next = reads[prefix.length];
|
|
128
|
+
const lookupFailure =
|
|
129
|
+
next?.status === "rejected"
|
|
130
|
+
? { number: proofs[prefix.length].number, error: errorMessage(next.reason) }
|
|
131
|
+
: undefined;
|
|
132
|
+
if (!prefix.length)
|
|
133
|
+
fail(
|
|
134
|
+
lookupFailure
|
|
135
|
+
? `Could not check PR #${lookupFailure.number}: ${lookupFailure.error}`
|
|
136
|
+
: "No verified merge-ready prefix",
|
|
137
|
+
);
|
|
138
|
+
return { prefix, lookupFailure };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function mergePrefix(
|
|
142
|
+
pi: ExtensionAPI,
|
|
143
|
+
ctx: ExtensionContext,
|
|
144
|
+
prefix: Approved[],
|
|
145
|
+
requireActivePlan: () => void,
|
|
146
|
+
signal?: AbortSignal,
|
|
147
|
+
) {
|
|
148
|
+
const merged: { number: number; head: string; url: string }[] = [];
|
|
149
|
+
for (const proof of prefix) {
|
|
150
|
+
const stopped = { merged, stopped: proof.number };
|
|
151
|
+
let fresh;
|
|
152
|
+
try {
|
|
153
|
+
requireActivePlan();
|
|
154
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Recheck each approved PR immediately before merging it.
|
|
155
|
+
fresh = await fetchPr(pi, ctx, proof.url, signal);
|
|
156
|
+
requireActivePlan();
|
|
157
|
+
} catch (error) {
|
|
158
|
+
return {
|
|
159
|
+
...stopped,
|
|
160
|
+
reason: "Could not recheck readiness; merge not attempted",
|
|
161
|
+
error: errorMessage(error),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (!mergeReady(proof, fresh)) {
|
|
165
|
+
return { ...stopped, reason: "Head or readiness changed" };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const uncertain = {
|
|
169
|
+
...stopped,
|
|
170
|
+
unconfirmed: proof.number,
|
|
171
|
+
reason: "Merge not confirmed; inspect forge state",
|
|
172
|
+
};
|
|
173
|
+
try {
|
|
174
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Stack merges must run in order.
|
|
175
|
+
const r = await pi.exec(
|
|
176
|
+
"gh",
|
|
177
|
+
["pr", "merge", proof.url, "--merge", "--match-head-commit", proof.head],
|
|
178
|
+
{ cwd: ctx.cwd, signal, timeout: 60000, maxBuffer: 1024 * 1024 },
|
|
179
|
+
);
|
|
180
|
+
if (r.code !== 0 || r.killed || r.truncated) return { ...uncertain, error: r.stderr };
|
|
181
|
+
|
|
182
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Confirm the merge before advancing.
|
|
183
|
+
const after = await fetchPr(pi, ctx, proof.url, signal);
|
|
184
|
+
if (after.state !== "MERGED" || after.headRefOid !== proof.head) return uncertain;
|
|
185
|
+
merged.push({ number: proof.number, head: after.headRefOid, url: after.url });
|
|
186
|
+
} catch (error) {
|
|
187
|
+
return { ...uncertain, error: errorMessage(error) };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { merged, ceiling: prefix.at(-1)?.number };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function registerForge(pi: ExtensionAPI, currentRoute: () => string | undefined) {
|
|
194
|
+
pi.registerTool({
|
|
195
|
+
name: "pstack_pr",
|
|
196
|
+
label: "Pstack PR state",
|
|
197
|
+
description: "Fetch live GitHub PR head and CI state. Requires configured gh. No writes.",
|
|
198
|
+
parameters: Type.Object({ number: Type.Integer({ minimum: 1 }) }),
|
|
199
|
+
async execute(_id, p, signal, _update, ctx) {
|
|
200
|
+
return result(await fetchPr(pi, ctx, p.number, signal));
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
pi.registerTool({
|
|
204
|
+
name: "pstack_ship",
|
|
205
|
+
label: "Pstack shipping gate",
|
|
206
|
+
description:
|
|
207
|
+
"Merge an explicitly approved contiguous verified GitHub PR prefix bottom-up. Requires independent evidence per head and fresh green checks, then explicit UI confirmation. Never used for autopilot-stack or Benny.",
|
|
208
|
+
executionMode: "sequential",
|
|
209
|
+
parameters: Type.Object({
|
|
210
|
+
proofs: Type.Array(
|
|
211
|
+
Type.Object({
|
|
212
|
+
number: Type.Integer({ minimum: 1 }),
|
|
213
|
+
head: Type.String(),
|
|
214
|
+
evidence: Type.String(),
|
|
215
|
+
independent: Type.Boolean(),
|
|
216
|
+
}),
|
|
217
|
+
{ minItems: 1, maxItems: 25 },
|
|
218
|
+
),
|
|
219
|
+
}),
|
|
220
|
+
async execute(_id, p, signal, _update, ctx) {
|
|
221
|
+
const requireActivePlan = () => {
|
|
222
|
+
if (!ctx.hasUI || !mergeRoutes.includes(currentRoute() ?? ""))
|
|
223
|
+
fail("Merging requires a shipping or autopilot-full plan and interactive approval");
|
|
224
|
+
};
|
|
225
|
+
requireActivePlan();
|
|
226
|
+
if (new Set(p.proofs.map((proof) => proof.number)).size !== p.proofs.length)
|
|
227
|
+
fail("Duplicate PR");
|
|
228
|
+
const { prefix, lookupFailure } = await previewPrefix(pi, ctx, p.proofs, signal);
|
|
229
|
+
requireActivePlan();
|
|
230
|
+
if (!(await ctx.ui.confirm("Merge these exact PR heads bottom-up?", text(prefix))))
|
|
231
|
+
fail("Merge not authorized");
|
|
232
|
+
signal?.throwIfAborted();
|
|
233
|
+
const outcome = await mergePrefix(pi, ctx, prefix, requireActivePlan, signal);
|
|
234
|
+
const unconfirmed = "unconfirmed" in outcome ? outcome.unconfirmed : undefined;
|
|
235
|
+
return result({
|
|
236
|
+
...outcome,
|
|
237
|
+
lookupFailure,
|
|
238
|
+
unmerged: p.proofs
|
|
239
|
+
.slice(outcome.merged.length)
|
|
240
|
+
.map((proof) => proof.number)
|
|
241
|
+
.filter((number) => number !== unconfirmed),
|
|
242
|
+
});
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { fail } from "./support.ts";
|
|
4
|
+
|
|
5
|
+
export function gitIn(
|
|
6
|
+
pi: ExtensionAPI,
|
|
7
|
+
cwd: string,
|
|
8
|
+
signal?: AbortSignal,
|
|
9
|
+
maxBuffer = 4 * 1024 * 1024,
|
|
10
|
+
) {
|
|
11
|
+
async function probe(args: string[], at = cwd, limit = maxBuffer) {
|
|
12
|
+
const r = await pi.exec("git", args, { cwd: at, signal, timeout: 30000, maxBuffer: limit });
|
|
13
|
+
return r.truncated || r.killed ? undefined : r;
|
|
14
|
+
}
|
|
15
|
+
async function run(args: string[], at = cwd) {
|
|
16
|
+
const r = await probe(args, at);
|
|
17
|
+
if (!r) fail(`git ${args[0]} failed: output truncated or process stopped`);
|
|
18
|
+
if (r.code !== 0) fail(`git ${args[0]} failed: ${r.stderr}`);
|
|
19
|
+
return r.stdout;
|
|
20
|
+
}
|
|
21
|
+
return { probe, run };
|
|
22
|
+
}
|
|
23
|
+
export type Git = ReturnType<typeof gitIn>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { activePlan } from "../lib/state.ts";
|
|
4
|
+
import { registerCatalog } from "./catalog-ui.ts";
|
|
5
|
+
import { registerDelegation } from "./delegate.ts";
|
|
6
|
+
import { registerForge } from "./forge.ts";
|
|
7
|
+
import { registerModelSetup } from "./models.ts";
|
|
8
|
+
import { registerPlan } from "./plan.ts";
|
|
9
|
+
import { registerRecords } from "./records.ts";
|
|
10
|
+
import { createSession } from "./session.ts";
|
|
11
|
+
import { notifier } from "./support.ts";
|
|
12
|
+
import { registerWorktrees } from "./worktrees.ts";
|
|
13
|
+
|
|
14
|
+
export default function pstack(pi: ExtensionAPI) {
|
|
15
|
+
const notify = notifier(pi);
|
|
16
|
+
const session = createSession(pi);
|
|
17
|
+
const activeWorktrees = new Set<string>();
|
|
18
|
+
registerModelSetup(pi, notify);
|
|
19
|
+
registerPlan(pi, session, notify);
|
|
20
|
+
registerCatalog(pi, notify);
|
|
21
|
+
registerRecords(pi, notify);
|
|
22
|
+
registerWorktrees(pi, activeWorktrees);
|
|
23
|
+
registerDelegation(pi, activeWorktrees);
|
|
24
|
+
registerForge(pi, () => activePlan(session.state())?.route);
|
|
25
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
aliases,
|
|
8
|
+
applyConfigPatch,
|
|
9
|
+
budgets,
|
|
10
|
+
defaultConfig,
|
|
11
|
+
effort,
|
|
12
|
+
judgeRole,
|
|
13
|
+
panelRoles,
|
|
14
|
+
parseModelConfig,
|
|
15
|
+
roles,
|
|
16
|
+
validateConfig,
|
|
17
|
+
type Role,
|
|
18
|
+
type RoleConfig,
|
|
19
|
+
} from "../lib/models.ts";
|
|
20
|
+
import { parseJson } from "../lib/validation.ts";
|
|
21
|
+
import { agentDir, atomicWrite, fail, hasErrorCode, text, type Notify } from "./support.ts";
|
|
22
|
+
|
|
23
|
+
const modelConfigPath = () => join(agentDir(), "pstack-models.json");
|
|
24
|
+
export async function readConfig(configPath = modelConfigPath()): Promise<RoleConfig> {
|
|
25
|
+
let source: string;
|
|
26
|
+
try {
|
|
27
|
+
source = await readFile(configPath, "utf8");
|
|
28
|
+
} catch (e) {
|
|
29
|
+
if (hasErrorCode(e, "ENOENT")) return defaultConfig();
|
|
30
|
+
throw e;
|
|
31
|
+
}
|
|
32
|
+
return parseModelConfig(parseJson(source, "pstack model configuration"));
|
|
33
|
+
}
|
|
34
|
+
export function availableWorkerModels(ctx: ExtensionContext) {
|
|
35
|
+
const extensionProviders = new Set(ctx.modelRegistry.getRegisteredProviderIds());
|
|
36
|
+
const models = ctx.scopedModels.length
|
|
37
|
+
? ctx.scopedModels.map((m) => m.model)
|
|
38
|
+
: ctx.modelRegistry.getAvailable();
|
|
39
|
+
const ids = models
|
|
40
|
+
.filter((m) => !extensionProviders.has(m.provider))
|
|
41
|
+
.map((m) => `${m.provider}/${m.id}`);
|
|
42
|
+
return [...new Set(ids)].toSorted();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function chooseRole(ctx: ExtensionContext, role: Role, available: string[]) {
|
|
46
|
+
const panel = panelRoles.has(role);
|
|
47
|
+
const options = [
|
|
48
|
+
...(role === judgeRole ? [] : aliases),
|
|
49
|
+
...available,
|
|
50
|
+
...(panel ? ["Done"] : []),
|
|
51
|
+
];
|
|
52
|
+
const chosen: string[] = [];
|
|
53
|
+
while (chosen.length < (panel ? 8 : 1)) {
|
|
54
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Show prior selections in the next dialog.
|
|
55
|
+
const value = await ctx.ui.select(`${role}: ${chosen.join(", ") || "(none)"}`, options);
|
|
56
|
+
if (!value) return undefined;
|
|
57
|
+
if (value === "Done") break;
|
|
58
|
+
chosen.push(value);
|
|
59
|
+
}
|
|
60
|
+
return chosen;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function chooseConfig(ctx: ExtensionContext, config: RoleConfig, available: string[]) {
|
|
64
|
+
const budget = await ctx.ui.select(
|
|
65
|
+
"Reasoning budget",
|
|
66
|
+
budgets.map((b) => `${b} (${effort("feature, refactoring", b)})`),
|
|
67
|
+
);
|
|
68
|
+
if (!budget) return undefined;
|
|
69
|
+
const selections = { ...config.roles };
|
|
70
|
+
for (const role of roles) {
|
|
71
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Pi shows one selection dialog at a time.
|
|
72
|
+
const action = await ctx.ui.select(`${role}: ${selections[role].join(", ") || "(empty)"}`, [
|
|
73
|
+
"Keep current",
|
|
74
|
+
"Choose model(s)",
|
|
75
|
+
]);
|
|
76
|
+
if (!action) return undefined;
|
|
77
|
+
if (action === "Keep current") continue;
|
|
78
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Finish choosing this role before the next.
|
|
79
|
+
const chosen = await chooseRole(ctx, role, available);
|
|
80
|
+
if (!chosen) return undefined;
|
|
81
|
+
selections[role] = chosen;
|
|
82
|
+
}
|
|
83
|
+
return { ...config, budget: budget.split(" ")[0], roles: selections };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function registerModelSetup(pi: ExtensionAPI, notify: Notify) {
|
|
87
|
+
pi.registerCommand("setup-pstack", {
|
|
88
|
+
description: "Configure pstack role models and reasoning budget",
|
|
89
|
+
handler: async (args, ctx) => {
|
|
90
|
+
const available = availableWorkerModels(ctx);
|
|
91
|
+
const parent = ctx.model && `${ctx.model.provider}/${ctx.model.id}`;
|
|
92
|
+
const warning =
|
|
93
|
+
parent && !available.includes(parent)
|
|
94
|
+
? `auto and inherit-parent resolve to ${parent}, which is unavailable to workers in this session. Select an available model explicitly.`
|
|
95
|
+
: undefined;
|
|
96
|
+
const configPath = modelConfigPath();
|
|
97
|
+
const current = await readConfig(configPath);
|
|
98
|
+
const input = args.trim();
|
|
99
|
+
if (input === "show" || (!input && !ctx.hasUI)) {
|
|
100
|
+
notify(ctx, text({ config: current, available, path: configPath, warning }));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (!ctx.hasUI) fail("Use /setup-pstack interactively to confirm model configuration.");
|
|
104
|
+
if (warning) notify(ctx, warning);
|
|
105
|
+
const chosen = input
|
|
106
|
+
? applyConfigPatch(current, parseJson(input, "setup arguments"))
|
|
107
|
+
: await chooseConfig(ctx, current, available);
|
|
108
|
+
if (!chosen) return;
|
|
109
|
+
const config = validateConfig(chosen, available);
|
|
110
|
+
if (!(await ctx.ui.confirm("Save pstack role configuration?", text(config)))) return;
|
|
111
|
+
await atomicWrite(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
112
|
+
notify(ctx, `Saved ${configPath}. Parent model unchanged.`);
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
import { gitIn } from "./git.ts";
|
|
7
|
+
|
|
8
|
+
const diffBytes = 2 * 1024 * 1024;
|
|
9
|
+
const untrackedLimit = 500;
|
|
10
|
+
|
|
11
|
+
export interface ParentDiff {
|
|
12
|
+
path: string;
|
|
13
|
+
omitted: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function saveParentDiff(
|
|
17
|
+
pi: ExtensionAPI,
|
|
18
|
+
cwd: string,
|
|
19
|
+
dir: string,
|
|
20
|
+
signal?: AbortSignal,
|
|
21
|
+
): Promise<ParentDiff | undefined> {
|
|
22
|
+
const git = gitIn(pi, cwd, signal, diffBytes);
|
|
23
|
+
const top = await git.probe(["rev-parse", "--show-toplevel"]);
|
|
24
|
+
if (top?.code !== 0) return undefined;
|
|
25
|
+
const repo = top.stdout.replace(/\r?\n$/, "");
|
|
26
|
+
const [tracked, listed] = await Promise.all([
|
|
27
|
+
git.probe(["diff", "--no-relative", "HEAD"], repo),
|
|
28
|
+
git.probe(["ls-files", "--others", "--exclude-standard", "-z"], repo),
|
|
29
|
+
]);
|
|
30
|
+
if (tracked?.code !== 0 || listed?.code !== 0) return undefined;
|
|
31
|
+
const files = listed.stdout.split("\0").filter(Boolean);
|
|
32
|
+
let remaining = diffBytes - Buffer.byteLength(tracked.stdout);
|
|
33
|
+
if (remaining < 0) return undefined;
|
|
34
|
+
const kept = [tracked.stdout];
|
|
35
|
+
const omitted: string[] = [];
|
|
36
|
+
const limit = Math.min(files.length, untrackedLimit);
|
|
37
|
+
let next = 0;
|
|
38
|
+
while (next < limit && remaining > 0) {
|
|
39
|
+
const batch = files.slice(next, Math.min(next + 8, limit));
|
|
40
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Apply the byte budget before fetching another batch.
|
|
41
|
+
const patches = await Promise.allSettled(
|
|
42
|
+
batch.map((file) =>
|
|
43
|
+
git.probe(["diff", "--no-index", "--", "/dev/null", file], repo, remaining),
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
for (const [i, patch] of patches.entries()) {
|
|
47
|
+
const r = patch.status === "fulfilled" ? patch.value : undefined;
|
|
48
|
+
if (r && (r.code === 0 || r.code === 1)) {
|
|
49
|
+
const bytes = Buffer.byteLength(r.stdout);
|
|
50
|
+
if (bytes <= remaining) {
|
|
51
|
+
kept.push(r.stdout);
|
|
52
|
+
remaining -= bytes;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
omitted.push(batch[i]);
|
|
57
|
+
}
|
|
58
|
+
next += batch.length;
|
|
59
|
+
}
|
|
60
|
+
const path = join(dir, "parent.diff");
|
|
61
|
+
await writeFile(path, kept.join(""), { mode: 0o600 });
|
|
62
|
+
return { path, omitted: omitted.concat(files.slice(next)) };
|
|
63
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
|
|
3
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
|
|
6
|
+
import { nativeNode, routes } from "../lib/catalog.ts";
|
|
7
|
+
import {
|
|
8
|
+
isTaskStatus,
|
|
9
|
+
planComplete,
|
|
10
|
+
startPlan,
|
|
11
|
+
stopped,
|
|
12
|
+
summary,
|
|
13
|
+
taskStatuses,
|
|
14
|
+
updateTask,
|
|
15
|
+
wakeConfig,
|
|
16
|
+
} from "../lib/state.ts";
|
|
17
|
+
import { sendWorkflow } from "./catalog-ui.ts";
|
|
18
|
+
import { mergeRoutes } from "./forge.ts";
|
|
19
|
+
import type { Session } from "./session.ts";
|
|
20
|
+
import {
|
|
21
|
+
artifacts,
|
|
22
|
+
atomicWrite,
|
|
23
|
+
fail,
|
|
24
|
+
result,
|
|
25
|
+
text,
|
|
26
|
+
unreachable,
|
|
27
|
+
type Notify,
|
|
28
|
+
stringEnum,
|
|
29
|
+
} from "./support.ts";
|
|
30
|
+
|
|
31
|
+
const planParameters = Type.Object({
|
|
32
|
+
action: stringEnum(["start", "custom", "show", "update", "finish"]),
|
|
33
|
+
route: Type.Optional(Type.String()),
|
|
34
|
+
goal: Type.Optional(Type.String()),
|
|
35
|
+
steps: Type.Optional(Type.Array(Type.String())),
|
|
36
|
+
task: Type.Optional(Type.Integer()),
|
|
37
|
+
status: Type.Optional(stringEnum(taskStatuses)),
|
|
38
|
+
evidence: Type.Optional(Type.String()),
|
|
39
|
+
});
|
|
40
|
+
async function approveMergeRoute(ctx: ExtensionContext, route: string, goal: string) {
|
|
41
|
+
if (!mergeRoutes.includes(route)) return;
|
|
42
|
+
const approved =
|
|
43
|
+
ctx.hasUI &&
|
|
44
|
+
(await ctx.ui.confirm(
|
|
45
|
+
`Start the merge-capable ${route} plan?`,
|
|
46
|
+
`${goal}\nThis route enables pstack_ship; each merge still needs its own approval.`,
|
|
47
|
+
));
|
|
48
|
+
if (!approved)
|
|
49
|
+
fail(`Starting ${route} requires operator approval; use /pstack-run ${route} <goal>`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function registerPlan(pi: ExtensionAPI, session: Session, notify: Notify) {
|
|
53
|
+
pi.registerCommand("poteto-mode", {
|
|
54
|
+
description: "Run a task with poteto-mode, or persist it with on/off",
|
|
55
|
+
handler: async (args, ctx) => {
|
|
56
|
+
const value = args.trim();
|
|
57
|
+
if (value === "on" || value === "off") return session.commit(ctx, { mode: value === "on" });
|
|
58
|
+
if (value) return sendWorkflow(pi, "poteto-mode", value);
|
|
59
|
+
if (!ctx.hasUI) return notify(ctx, "Use /poteto-mode on or /poteto-mode <goal>.");
|
|
60
|
+
const goal = await ctx.ui.input("Goal and observable completion predicate");
|
|
61
|
+
if (goal) sendWorkflow(pi, "poteto-mode", goal);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
pi.registerCommand("pstack-run", {
|
|
65
|
+
description: "Run an explicit playbook: /pstack-run <route> <goal>",
|
|
66
|
+
handler: async (args, ctx) => {
|
|
67
|
+
const [route, ...words] = args.trim().split(/\s+/);
|
|
68
|
+
const goal = words.join(" ");
|
|
69
|
+
if (!routes.includes(route) || !goal)
|
|
70
|
+
return notify(ctx, `Choose a route and goal:\n${routes.join(", ")}`);
|
|
71
|
+
session.commit(ctx, { plan: startPlan(route, goal) });
|
|
72
|
+
sendWorkflow(pi, "pb-" + route, goal);
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
pi.registerCommand("pstack-status", {
|
|
76
|
+
description: "Show current goal, route, tasks and loop",
|
|
77
|
+
handler: async (_args, ctx) => notify(ctx, text(summary(session.state()))),
|
|
78
|
+
});
|
|
79
|
+
pi.registerCommand("pstack-clear", {
|
|
80
|
+
description: "Clear the current plan and goal; preserve artifacts",
|
|
81
|
+
handler: async (_args, ctx) => session.commit(ctx, { goal: "", plan: null, wake: null }),
|
|
82
|
+
});
|
|
83
|
+
pi.registerCommand("pstack-goal", {
|
|
84
|
+
description: "Set or clear a persistent branch goal",
|
|
85
|
+
handler: async (args, ctx) => {
|
|
86
|
+
const goal = args.trim();
|
|
87
|
+
if (!goal) return notify(ctx, session.state().goal || "No goal");
|
|
88
|
+
session.commit(ctx, { goal: goal === "clear" ? "" : goal });
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
pi.registerCommand("pstack-loop", {
|
|
92
|
+
description: "Start a bounded interactive wake loop: <seconds> <max> <predicate>, or stop",
|
|
93
|
+
handler: async (args, ctx) => {
|
|
94
|
+
const input = args.trim();
|
|
95
|
+
if (input === "stop") return session.stopWake(ctx);
|
|
96
|
+
if (!input) return notify(ctx, text(session.state().wake));
|
|
97
|
+
if (ctx.mode !== "tui") fail("Wake loops require an open interactive Pi session.");
|
|
98
|
+
const [seconds, max, ...words] = input.split(/\s+/);
|
|
99
|
+
const next = wakeConfig(Number(seconds), Number(max), words.join(" "));
|
|
100
|
+
if (await ctx.ui.confirm("Start bounded autonomous wake loop?", text(next)))
|
|
101
|
+
session.startWake(ctx, next);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
pi.registerCommand("pstack-pause", {
|
|
105
|
+
description: "Pause wakes and preserve a session resume note",
|
|
106
|
+
handler: async (_args, ctx) => {
|
|
107
|
+
session.stopWake(ctx);
|
|
108
|
+
const path = join(artifacts(ctx), "resume.json");
|
|
109
|
+
const note = {
|
|
110
|
+
...summary(session.state()),
|
|
111
|
+
cwd: ctx.cwd,
|
|
112
|
+
session: ctx.sessionManager.getSessionFile(),
|
|
113
|
+
};
|
|
114
|
+
await atomicWrite(path, JSON.stringify(note, null, 2));
|
|
115
|
+
notify(ctx, `Paused. Resume note: ${path}`);
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
pi.registerTool({
|
|
120
|
+
name: "pstack_plan",
|
|
121
|
+
label: "Pstack plan",
|
|
122
|
+
description:
|
|
123
|
+
"Maintain branch-local plan. Start with a route and goal or custom steps for figure-it-out. Mark tasks with real evidence. Finish only when all are done or justified skips.",
|
|
124
|
+
executionMode: "sequential",
|
|
125
|
+
parameters: planParameters,
|
|
126
|
+
async execute(_id, p, _signal, _update, ctx) {
|
|
127
|
+
const { plan, wake } = session.state();
|
|
128
|
+
let instructions: string | undefined;
|
|
129
|
+
switch (p.action) {
|
|
130
|
+
case "show":
|
|
131
|
+
return result(summary(session.state()));
|
|
132
|
+
case "start": {
|
|
133
|
+
const route = p.route;
|
|
134
|
+
const goal = p.goal ?? "";
|
|
135
|
+
if (!route || !routes.includes(route))
|
|
136
|
+
fail("Choose a known playbook or use action custom");
|
|
137
|
+
await approveMergeRoute(ctx, route, goal);
|
|
138
|
+
instructions = nativeNode("pb-" + route).instructions;
|
|
139
|
+
session.commit(ctx, { plan: startPlan(route, goal) });
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case "custom":
|
|
143
|
+
session.commit(ctx, { plan: startPlan("figure-it-out", p.goal ?? "", p.steps) });
|
|
144
|
+
break;
|
|
145
|
+
case "update":
|
|
146
|
+
if (p.task === undefined || !isTaskStatus(p.status)) fail("Specify task and status");
|
|
147
|
+
session.commit(ctx, { plan: updateTask(plan, p.task, p.status, p.evidence) });
|
|
148
|
+
break;
|
|
149
|
+
case "finish":
|
|
150
|
+
if (!plan || !planComplete(plan) || !p.evidence?.trim())
|
|
151
|
+
fail("Incomplete plan or missing final evidence");
|
|
152
|
+
session.commit(ctx, {
|
|
153
|
+
plan: { ...plan, finishedEvidence: p.evidence },
|
|
154
|
+
wake: stopped(wake),
|
|
155
|
+
});
|
|
156
|
+
break;
|
|
157
|
+
default:
|
|
158
|
+
unreachable(p.action);
|
|
159
|
+
}
|
|
160
|
+
return result({ state: summary(session.state()), instructions });
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
pi.registerTool({
|
|
164
|
+
name: "pstack_wake",
|
|
165
|
+
label: "Pstack wake status",
|
|
166
|
+
description:
|
|
167
|
+
"Inspect or stop an operator-started wake loop. Starting requires the explicit /pstack-loop command.",
|
|
168
|
+
executionMode: "sequential",
|
|
169
|
+
parameters: Type.Object({
|
|
170
|
+
action: stringEnum(["show", "stop"]),
|
|
171
|
+
evidence: Type.Optional(Type.String()),
|
|
172
|
+
}),
|
|
173
|
+
async execute(_id, p, _signal, _update, ctx) {
|
|
174
|
+
if (p.action === "stop") {
|
|
175
|
+
if (!p.evidence?.trim()) fail("Provide stop evidence or blocker");
|
|
176
|
+
session.stopWake(ctx, p.evidence);
|
|
177
|
+
}
|
|
178
|
+
return result(session.state().wake);
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
}
|