@gonrocca/zero-pi 0.1.58 → 0.1.59
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/README.md +13 -5
- package/extensions/autotune-extension.ts +40 -2
- package/extensions/autotune.ts +67 -0
- package/extensions/sdd-agents.ts +28 -0
- package/extensions/zero-checkpoint-extension.ts +62 -0
- package/extensions/zero-checkpoint.ts +92 -0
- package/extensions/zero-validate.ts +23 -2
- package/package.json +4 -1
- package/prompts/orchestrator.md +25 -10
- package/prompts/phases/build.md +1 -2
- package/prompts/phases/plan.md +8 -0
package/README.md
CHANGED
|
@@ -75,9 +75,12 @@ into `/forge` for you.
|
|
|
75
75
|
| ------- | ------------ |
|
|
76
76
|
| **Strict TDD** | The build phase drives RED → GREEN → TRIANGULATE → REFACTOR with a TDD Cycle Evidence table; veredicto audits it. On by default, runtime-gated on a test runner; `tdd.mode: "off"` disables it. |
|
|
77
77
|
| **`/zero-models`** | Pick the model + provider + thinking level for each SDD phase — a boxed-window picker, or set one directly. Direct assignments are validated against pi's model registry when available. |
|
|
78
|
-
| **
|
|
78
|
+
| **Phase tool gating** | Generated `zero-*` sub-agents get phase-specific tool allowlists: explore/veredicto are read-only, plan writes SDD artifacts, build edits code. |
|
|
79
|
+
| **Dependency-aware tasks** | `tasks.md` is validated as a task graph with mandatory `depends:` edges, topological ordering, and review workload totals. |
|
|
80
|
+
| **Autotune** | Learns which model fits each phase from your run history and re-tunes itself; optional `autotuneBudget.maxPhaseCostUsd` suppresses costly step-ups. |
|
|
79
81
|
| **`/zero-doctor`** | Preflight diagnostics for zero-pi: package install, Node version, pi-subagents, generated phase agents, model config, `.sdd/config`, run history, git, and `gh` auth. |
|
|
80
82
|
| **`/zero-cost`** | Aggregates a run's sub-agent `meta.json` files into a per-phase token/cost/duration report — no schema change, reads what pi already writes. |
|
|
83
|
+
| **`/zero-checkpoint`** | Writes patch-based worktree checkpoints under `.sdd/<slug>/checkpoints/` before risky build batches. |
|
|
81
84
|
| **`/zero-sync` / `/zero-archive`** | Folds each run's spec delta into a canonical, project-wide spec store and archives approved runs. |
|
|
82
85
|
| **Git / PR / Issues** | `/zero-branch`, `/zero-git-validate`, `/zero-pr`, and `/zero-issue` keep branches and GitHub links audit-ready. |
|
|
83
86
|
| **Run memory** | Every run recalls and saves traces to Cortex, so runs learn from each other. |
|
|
@@ -102,6 +105,7 @@ into `/forge` for you.
|
|
|
102
105
|
| `/zero-validate <slug>` | Validate proposal/spec/design/tasks artifacts, including task schema and per-domain specs. |
|
|
103
106
|
| `/zero-status` | Show each `.sdd/` run's artifact, sync, latest-verdict, and GitHub-link status. |
|
|
104
107
|
| `/zero-cost [<slug>]` | Report tokens (in/out/cache), USD cost, duration, and tool-count per SDD phase for a run — `<slug>` for a specific run, no argument for the most recent. |
|
|
108
|
+
| `/zero-checkpoint [<slug>] [--json]` | Save `diff.patch`, `status.txt`, `head.txt`, `meta.json`, and a review-before-running `restore.sh` under `.sdd/<slug>/checkpoints/<id>/`. |
|
|
105
109
|
| `/zero-branch <slug>` | Create/reuse the configured SDD Git branch and persist `branch`/`baseBranch`. |
|
|
106
110
|
| `/zero-git-validate <slug>` | Check worktree, branch, remote, `gh auth`, and verdict gating before PR/archive. |
|
|
107
111
|
| `/zero-pr <slug>` | Create an audit-ready GitHub PR from a run that already has verdict `pasa`. |
|
|
@@ -111,7 +115,7 @@ into `/forge` for you.
|
|
|
111
115
|
|
|
112
116
|
### Git / PR / Issues
|
|
113
117
|
|
|
114
|
-
Recommended flow: `/zero-branch <slug>` → `/zero-issue <slug>` → `/forge <slug>` → `/zero-git-validate <slug> --for=pr` → `/zero-pr <slug>` → `/zero-archive <slug>`.
|
|
118
|
+
Recommended flow: `/zero-branch <slug>` → `/zero-issue <slug>` → `/forge <slug>` (the orchestrator validates plan artifacts and can create `/zero-checkpoint` before build) → `/zero-git-validate <slug> --for=pr` → `/zero-pr <slug>` → `/zero-archive <slug>`.
|
|
115
119
|
|
|
116
120
|
`links.json` is forward-compatible and may contain:
|
|
117
121
|
|
|
@@ -182,14 +186,18 @@ conversation-resume note.
|
|
|
182
186
|
"models": { "explore": "claude-haiku-4-5", "build": "claude-sonnet-4-6" },
|
|
183
187
|
"providers": { "explore": "anthropic", "build": "anthropic" },
|
|
184
188
|
"thinking": { "explore": "low", "build": "high" },
|
|
185
|
-
"autotune": "ask"
|
|
189
|
+
"autotune": "ask",
|
|
190
|
+
"autotuneBudget": { "maxPhaseCostUsd": 4, "minSamples": 3 }
|
|
186
191
|
}
|
|
187
192
|
```
|
|
188
193
|
|
|
189
194
|
The `thinking` map sets each phase's pi effort level — one of `off`, `minimal`,
|
|
190
195
|
`low`, `medium`, `high`, `xhigh`. A phase with no entry gets no `thinking:` line
|
|
191
|
-
in its generated sub-agent (no aggressive default).
|
|
192
|
-
|
|
196
|
+
in its generated sub-agent (no aggressive default). `autotuneBudget` is optional:
|
|
197
|
+
when `maxPhaseCostUsd` is set, autotune will not step a blamed phase up to a more
|
|
198
|
+
expensive tier if that phase/model is already at or above the average USD
|
|
199
|
+
ceiling with enough cost samples (`minSamples`, default 3). Set thinking from
|
|
200
|
+
the picker's after-model thinking screen, or directly:
|
|
193
201
|
|
|
194
202
|
```
|
|
195
203
|
/zero-models build=anthropic/claude-sonnet-4-6 thinking=high
|
|
@@ -19,17 +19,20 @@
|
|
|
19
19
|
// dependency-free: `node:fs`/`node:os`/`node:path`
|
|
20
20
|
// only, plus minimal local interfaces for the pi API.
|
|
21
21
|
|
|
22
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
22
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
23
23
|
import { homedir } from "node:os";
|
|
24
24
|
import { join } from "node:path";
|
|
25
25
|
|
|
26
26
|
import {
|
|
27
27
|
aggregate,
|
|
28
|
+
aggregateCostStats,
|
|
28
29
|
decideAdjustments,
|
|
29
30
|
readAutotuneMode,
|
|
30
31
|
readRunRecords,
|
|
31
32
|
type Adjustment,
|
|
33
|
+
type AutotuneCostGuard,
|
|
32
34
|
} from "./autotune.ts";
|
|
35
|
+
import { parseMeta, type PhaseMeta } from "./zero-cost.ts";
|
|
33
36
|
|
|
34
37
|
/** The SDD phases, in pipeline order. Mirrors `zero-models.ts`. */
|
|
35
38
|
const PHASES = ["explore", "plan", "build", "veredicto"] as const;
|
|
@@ -104,6 +107,39 @@ function readCurrentModels(data: Record<string, unknown>): Partial<Record<Phase,
|
|
|
104
107
|
return models;
|
|
105
108
|
}
|
|
106
109
|
|
|
110
|
+
function readCostGuard(data: Record<string, unknown>): AutotuneCostGuard {
|
|
111
|
+
const raw = isObject(data.autotuneBudget) ? data.autotuneBudget : {};
|
|
112
|
+
const guard: AutotuneCostGuard = {};
|
|
113
|
+
if (typeof raw.maxPhaseCostUsd === "number" && Number.isFinite(raw.maxPhaseCostUsd) && raw.maxPhaseCostUsd > 0) {
|
|
114
|
+
guard.maxPhaseCostUsd = raw.maxPhaseCostUsd;
|
|
115
|
+
}
|
|
116
|
+
if (typeof raw.minSamples === "number" && Number.isFinite(raw.minSamples) && raw.minSamples > 0) {
|
|
117
|
+
guard.minSamples = raw.minSamples;
|
|
118
|
+
}
|
|
119
|
+
return guard;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function readAllPhaseMetas(): PhaseMeta[] {
|
|
123
|
+
const root = join(homedir(), ".pi", "agent", "sessions");
|
|
124
|
+
if (!existsSync(root)) return [];
|
|
125
|
+
const out: PhaseMeta[] = [];
|
|
126
|
+
for (const session of readdirSync(root, { withFileTypes: true })) {
|
|
127
|
+
if (!session.isDirectory()) continue;
|
|
128
|
+
const artifacts = join(root, session.name, "subagent-artifacts");
|
|
129
|
+
if (!existsSync(artifacts)) continue;
|
|
130
|
+
for (const f of readdirSync(artifacts)) {
|
|
131
|
+
if (!f.endsWith("_meta.json")) continue;
|
|
132
|
+
try {
|
|
133
|
+
const meta = parseMeta(JSON.parse(readFileSync(join(artifacts, f), "utf8")));
|
|
134
|
+
if (meta) out.push(meta);
|
|
135
|
+
} catch {
|
|
136
|
+
// skip unreadable / malformed meta
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
|
|
107
143
|
/**
|
|
108
144
|
* Derive `knownModels` — the distinct, non-empty model ids the autotune logic
|
|
109
145
|
* may step toward. Per the design this is the union of every model seen in the
|
|
@@ -177,7 +213,9 @@ function evaluateAndTune(ctx: PiSessionContext): void {
|
|
|
177
213
|
const knownModels = deriveKnownModels(currentModels, loggedModels);
|
|
178
214
|
|
|
179
215
|
const stats = aggregate(records);
|
|
180
|
-
const
|
|
216
|
+
const costGuard = readCostGuard(data);
|
|
217
|
+
const costStats = costGuard.maxPhaseCostUsd ? aggregateCostStats(readAllPhaseMetas()) : new Map();
|
|
218
|
+
const adjustments: Adjustment[] = decideAdjustments(stats, currentModels, knownModels, costStats, costGuard);
|
|
181
219
|
if (adjustments.length === 0) return; // nothing to do — return silently.
|
|
182
220
|
|
|
183
221
|
if (mode === "auto") {
|
package/extensions/autotune.ts
CHANGED
|
@@ -265,6 +265,29 @@ export interface PhaseModelStat {
|
|
|
265
265
|
avgReplantear: number | null;
|
|
266
266
|
}
|
|
267
267
|
|
|
268
|
+
export interface PhaseCostSample {
|
|
269
|
+
phase: unknown;
|
|
270
|
+
model: unknown;
|
|
271
|
+
usage: unknown;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export interface PhaseCostStat {
|
|
275
|
+
phase: (typeof RECORD_PHASES)[number];
|
|
276
|
+
model: string;
|
|
277
|
+
samples: number;
|
|
278
|
+
totalCost: number;
|
|
279
|
+
avgCost: number;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export interface AutotuneCostGuard {
|
|
283
|
+
/** Optional per-phase average cost ceiling in USD. Absent/invalid disables the guard. */
|
|
284
|
+
maxPhaseCostUsd?: number;
|
|
285
|
+
/** Minimum cost samples before the ceiling can suppress a step-up. Default: MIN_COST_SAMPLES. */
|
|
286
|
+
minSamples?: number;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export const MIN_COST_SAMPLES = 3;
|
|
290
|
+
|
|
268
291
|
/** Map key for a `(phase, model)` bucket. */
|
|
269
292
|
function statKey(phase: string, model: string): string {
|
|
270
293
|
return `${phase} ${model}`;
|
|
@@ -287,6 +310,38 @@ function statKey(phase: string, model: string): string {
|
|
|
287
310
|
* `avgReplantear` accumulator. `avgCorregir`/`avgReplantear` are `null` when a
|
|
288
311
|
* pair has no v2 evidence. An empty input yields an empty map.
|
|
289
312
|
*/
|
|
313
|
+
export function aggregateCostStats(samples: readonly PhaseCostSample[]): Map<string, PhaseCostStat> {
|
|
314
|
+
const acc = new Map<string, { phase: (typeof RECORD_PHASES)[number]; model: string; samples: number; totalCost: number }>();
|
|
315
|
+
for (const sample of samples) {
|
|
316
|
+
const phase = sample.phase;
|
|
317
|
+
if (typeof phase !== "string" || !(RECORD_PHASES as readonly string[]).includes(phase)) continue;
|
|
318
|
+
const model = sample.model;
|
|
319
|
+
if (typeof model !== "string" || model === "") continue;
|
|
320
|
+
const usage = isObject(sample.usage) ? sample.usage : {};
|
|
321
|
+
const cost = usage.cost;
|
|
322
|
+
if (typeof cost !== "number" || !Number.isFinite(cost)) continue;
|
|
323
|
+
const key = statKey(phase, model);
|
|
324
|
+
let bucket = acc.get(key);
|
|
325
|
+
if (!bucket) {
|
|
326
|
+
bucket = { phase: phase as (typeof RECORD_PHASES)[number], model, samples: 0, totalCost: 0 };
|
|
327
|
+
acc.set(key, bucket);
|
|
328
|
+
}
|
|
329
|
+
bucket.samples += 1;
|
|
330
|
+
bucket.totalCost += cost;
|
|
331
|
+
}
|
|
332
|
+
const stats = new Map<string, PhaseCostStat>();
|
|
333
|
+
for (const [key, bucket] of acc) {
|
|
334
|
+
stats.set(key, {
|
|
335
|
+
phase: bucket.phase,
|
|
336
|
+
model: bucket.model,
|
|
337
|
+
samples: bucket.samples,
|
|
338
|
+
totalCost: bucket.totalCost,
|
|
339
|
+
avgCost: bucket.samples > 0 ? bucket.totalCost / bucket.samples : 0,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
return stats;
|
|
343
|
+
}
|
|
344
|
+
|
|
290
345
|
export function aggregate(records: RunRecord[]): Map<string, PhaseModelStat> {
|
|
291
346
|
interface Acc {
|
|
292
347
|
samples: number;
|
|
@@ -505,6 +560,8 @@ export function decideAdjustments(
|
|
|
505
560
|
stats: Map<string, PhaseModelStat>,
|
|
506
561
|
currentModels: Partial<Record<(typeof RECORD_PHASES)[number], string>>,
|
|
507
562
|
knownModels: readonly string[],
|
|
563
|
+
costStats: Map<string, PhaseCostStat> = new Map(),
|
|
564
|
+
costGuard: AutotuneCostGuard = {},
|
|
508
565
|
): Adjustment[] {
|
|
509
566
|
const adjustments: Adjustment[] = [];
|
|
510
567
|
|
|
@@ -522,6 +579,16 @@ export function decideAdjustments(
|
|
|
522
579
|
const threshold = phase === "build" ? HIGH_AVG_CORREGIR : HIGH_AVG_REPLANTEAR;
|
|
523
580
|
if (measure === null || !(measure > threshold)) continue;
|
|
524
581
|
|
|
582
|
+
const maxPhaseCostUsd = costGuard.maxPhaseCostUsd;
|
|
583
|
+
if (typeof maxPhaseCostUsd === "number" && Number.isFinite(maxPhaseCostUsd) && maxPhaseCostUsd > 0) {
|
|
584
|
+
const minCostSamples =
|
|
585
|
+
typeof costGuard.minSamples === "number" && Number.isFinite(costGuard.minSamples) && costGuard.minSamples > 0
|
|
586
|
+
? Math.floor(costGuard.minSamples)
|
|
587
|
+
: MIN_COST_SAMPLES;
|
|
588
|
+
const cost = costStats.get(statKey(phase, currentModel));
|
|
589
|
+
if (cost && cost.samples >= minCostSamples && cost.avgCost >= maxPhaseCostUsd) continue;
|
|
590
|
+
}
|
|
591
|
+
|
|
525
592
|
const to = stepUp(currentModel, knownModels);
|
|
526
593
|
if (to === null) continue; // already at top tier, or untierable
|
|
527
594
|
|
package/extensions/sdd-agents.ts
CHANGED
|
@@ -32,6 +32,32 @@ export type Phase = (typeof PHASES)[number];
|
|
|
32
32
|
*/
|
|
33
33
|
export const SUPPORT_MODULES = ["strict-tdd.md", "strict-tdd-verify.md"] as const;
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Phase-specific builtin tool allowlists for generated pi-subagents agents.
|
|
37
|
+
*
|
|
38
|
+
* Explore and veredicto are intentionally read-only at the mutation-tool layer:
|
|
39
|
+
* they can inspect files and run scoped verification commands, but they cannot
|
|
40
|
+
* edit. Plan can write only SDD artifacts; build is the only phase with the
|
|
41
|
+
* full mutation set for product code. This is enforced by pi-subagents' `tools:`
|
|
42
|
+
* frontmatter key, so an accidental phase prompt drift does not silently grant
|
|
43
|
+
* broad editing power to every child.
|
|
44
|
+
*/
|
|
45
|
+
export const PHASE_TOOLS: Record<Phase, readonly string[]> = {
|
|
46
|
+
explore: ["read", "bash"],
|
|
47
|
+
plan: ["read", "bash", "write", "edit"],
|
|
48
|
+
build: ["read", "bash", "write", "edit"],
|
|
49
|
+
veredicto: ["read", "bash"],
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Non-build phases may mention implementation terms while using `bash`; do not
|
|
53
|
+
* let pi-subagents' implementation completion guard classify them as writers. */
|
|
54
|
+
export const PHASE_COMPLETION_GUARD: Record<Phase, boolean> = {
|
|
55
|
+
explore: false,
|
|
56
|
+
plan: false,
|
|
57
|
+
build: true,
|
|
58
|
+
veredicto: false,
|
|
59
|
+
};
|
|
60
|
+
|
|
35
61
|
/** Absolute path of the runtime support dir the phase prompts reference. */
|
|
36
62
|
export function supportModulesDir(): string {
|
|
37
63
|
return join(homedir(), ".pi", "agent", "agents", "zero", "support");
|
|
@@ -75,6 +101,8 @@ export function buildAgentFile(
|
|
|
75
101
|
// defensive: callers already validate, but the file builder must never write
|
|
76
102
|
// a bad level into agent frontmatter.
|
|
77
103
|
if (thinking && isThinkingLevel(thinking)) front.push(`thinking: ${thinking}`);
|
|
104
|
+
front.push(`tools: ${PHASE_TOOLS[phase].join(", ")}`);
|
|
105
|
+
if (!PHASE_COMPLETION_GUARD[phase]) front.push("completionGuard: false");
|
|
78
106
|
front.push(
|
|
79
107
|
"systemPromptMode: replace",
|
|
80
108
|
"inheritProjectContext: true",
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
|
|
5
|
+
import { createCheckpoint, formatCheckpointReport, parseCheckpointArgs, type CheckpointRunner } from "./zero-checkpoint.ts";
|
|
6
|
+
|
|
7
|
+
const SDD_DIR = ".sdd";
|
|
8
|
+
|
|
9
|
+
type NotifyType = "info" | "warning" | "error";
|
|
10
|
+
interface PiCommandContext { ui: { notify(message: string, type?: NotifyType): void } }
|
|
11
|
+
interface PiExtensionAPI { registerCommand(name: string, options: { description?: string; handler: (args: string, ctx: PiCommandContext) => void | Promise<void> }): void }
|
|
12
|
+
|
|
13
|
+
function resolveSlug(explicit: string | null): string | null {
|
|
14
|
+
if (explicit) return explicit;
|
|
15
|
+
try {
|
|
16
|
+
const candidates = readdirSync(SDD_DIR, { withFileTypes: true })
|
|
17
|
+
.filter((e) => e.isDirectory() && e.name !== "specs" && e.name !== "archive" && existsSync(join(SDD_DIR, e.name)))
|
|
18
|
+
.map((e) => e.name);
|
|
19
|
+
return candidates.length === 1 ? candidates[0] : null;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function defaultRunner(cwd: string): CheckpointRunner {
|
|
26
|
+
return {
|
|
27
|
+
run(command, args) {
|
|
28
|
+
try {
|
|
29
|
+
const r = spawnSync(command, [...args], { cwd, encoding: "utf8" });
|
|
30
|
+
return {
|
|
31
|
+
status: typeof r.status === "number" ? r.status : 1,
|
|
32
|
+
stdout: r.stdout ?? "",
|
|
33
|
+
stderr: r.stderr ?? "",
|
|
34
|
+
error: r.error?.message,
|
|
35
|
+
};
|
|
36
|
+
} catch (err) {
|
|
37
|
+
return { status: 1, stdout: "", stderr: "", error: err instanceof Error ? err.message : String(err) };
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function runCheckpoint(args: string, ctx: PiCommandContext, runner = defaultRunner(process.cwd())): void {
|
|
44
|
+
const notify = (m: string, t?: NotifyType) => { try { ctx.ui.notify(m, t); } catch {} };
|
|
45
|
+
const parsed = parseCheckpointArgs(args ?? "");
|
|
46
|
+
const slug = resolveSlug(parsed.slug);
|
|
47
|
+
if (!slug) { notify("zero-checkpoint: no hay un único run — corré /zero-checkpoint <slug>", "warning"); return; }
|
|
48
|
+
const result = createCheckpoint(slug, process.cwd(), runner);
|
|
49
|
+
if (parsed.json) notify(JSON.stringify(result, null, 2), result.ok ? "info" : "error");
|
|
50
|
+
else notify(formatCheckpointReport(result), result.ok ? "info" : "error");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default function register(pi?: PiExtensionAPI): void {
|
|
54
|
+
if (!pi || typeof pi.registerCommand !== "function") return;
|
|
55
|
+
pi.registerCommand("zero-checkpoint", {
|
|
56
|
+
description: "Crea un checkpoint patch-based del worktree actual dentro de .sdd/<slug>/checkpoints/",
|
|
57
|
+
handler: (args: string, ctx: PiCommandContext): void => {
|
|
58
|
+
try { if (ctx?.ui?.notify) runCheckpoint(args ?? "", ctx); }
|
|
59
|
+
catch (err) { try { ctx.ui.notify(`zero-checkpoint: ${err instanceof Error ? err.message : String(err)}`, "error"); } catch {} }
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export interface CheckpointRunner {
|
|
5
|
+
run(command: string, args: readonly string[]): { status: number; stdout: string; stderr: string; error?: string };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface CheckpointResult {
|
|
9
|
+
ok: boolean;
|
|
10
|
+
slug: string;
|
|
11
|
+
id: string;
|
|
12
|
+
dir: string;
|
|
13
|
+
head: string;
|
|
14
|
+
dirty: boolean;
|
|
15
|
+
untracked: string[];
|
|
16
|
+
message?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function checkpointId(date = new Date()): string {
|
|
20
|
+
const pad = (n: number): string => String(n).padStart(2, "0");
|
|
21
|
+
return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseCheckpointArgs(args: string): { slug: string | null; json: boolean } {
|
|
25
|
+
const parts = args.trim().split(/\s+/).filter(Boolean);
|
|
26
|
+
let slug: string | null = null;
|
|
27
|
+
let json = false;
|
|
28
|
+
for (const part of parts) {
|
|
29
|
+
if (part === "--json") json = true;
|
|
30
|
+
else if (!slug) slug = part;
|
|
31
|
+
}
|
|
32
|
+
return { slug, json };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function untrackedFromStatus(status: string): string[] {
|
|
36
|
+
return status
|
|
37
|
+
.split(/\r?\n/)
|
|
38
|
+
.filter((line) => line.startsWith("?? "))
|
|
39
|
+
.map((line) => line.slice(3).trim())
|
|
40
|
+
.filter(Boolean);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function formatCheckpointReport(result: CheckpointResult): string {
|
|
44
|
+
if (!result.ok) return `zero-checkpoint: ${result.message ?? "falló"}`;
|
|
45
|
+
const lines = [
|
|
46
|
+
`zero-checkpoint: ${result.slug} @ ${result.id}`,
|
|
47
|
+
` dir: ${result.dir}`,
|
|
48
|
+
` head: ${result.head}`,
|
|
49
|
+
result.dirty ? " estado: cambios capturados en diff.patch" : " estado: worktree limpio (checkpoint base)",
|
|
50
|
+
];
|
|
51
|
+
if (result.untracked.length > 0) {
|
|
52
|
+
lines.push(` aviso: ${result.untracked.length} untracked no se incluyen en diff.patch (${result.untracked.slice(0, 5).join(", ")}${result.untracked.length > 5 ? ", …" : ""})`);
|
|
53
|
+
}
|
|
54
|
+
lines.push(" restore: revisá restore.sh antes de ejecutarlo");
|
|
55
|
+
return lines.join("\n");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createCheckpoint(
|
|
59
|
+
slug: string,
|
|
60
|
+
cwd: string,
|
|
61
|
+
runner: CheckpointRunner,
|
|
62
|
+
now = new Date(),
|
|
63
|
+
): CheckpointResult {
|
|
64
|
+
const root = runner.run("git", ["rev-parse", "--show-toplevel"]);
|
|
65
|
+
if (root.status !== 0) return { ok: false, slug, id: "", dir: "", head: "", dirty: false, untracked: [], message: "este cwd no parece estar dentro de un repo git" };
|
|
66
|
+
const repoRoot = root.stdout.trim() || cwd;
|
|
67
|
+
|
|
68
|
+
const head = runner.run("git", ["rev-parse", "--short", "HEAD"]);
|
|
69
|
+
if (head.status !== 0) return { ok: false, slug, id: "", dir: "", head: "", dirty: false, untracked: [], message: "no pude resolver HEAD" };
|
|
70
|
+
|
|
71
|
+
const status = runner.run("git", ["status", "--short"]);
|
|
72
|
+
const diff = runner.run("git", ["diff", "--binary", "HEAD"]);
|
|
73
|
+
if (status.status !== 0 || diff.status !== 0) return { ok: false, slug, id: "", dir: "", head: head.stdout.trim(), dirty: false, untracked: [], message: "no pude leer el estado git" };
|
|
74
|
+
|
|
75
|
+
const id = checkpointId(now);
|
|
76
|
+
const dir = join(repoRoot, ".sdd", slug, "checkpoints", id);
|
|
77
|
+
mkdirSync(dir, { recursive: true });
|
|
78
|
+
const untracked = untrackedFromStatus(status.stdout);
|
|
79
|
+
const dirty = status.stdout.trim() !== "";
|
|
80
|
+
|
|
81
|
+
writeFileSync(join(dir, "head.txt"), `${head.stdout.trim()}\n`, "utf8");
|
|
82
|
+
writeFileSync(join(dir, "status.txt"), status.stdout, "utf8");
|
|
83
|
+
writeFileSync(join(dir, "diff.patch"), diff.stdout, "utf8");
|
|
84
|
+
writeFileSync(join(dir, "meta.json"), `${JSON.stringify({ slug, id, head: head.stdout.trim(), dirty, untracked, createdAt: now.toISOString() }, null, 2)}\n`, "utf8");
|
|
85
|
+
writeFileSync(
|
|
86
|
+
join(dir, "restore.sh"),
|
|
87
|
+
`#!/usr/bin/env bash\nset -euo pipefail\n# Review before running. This restores tracked files to checkpoint ${id}.\ngit reset --hard ${head.stdout.trim()}\ngit apply --3way ${JSON.stringify(join(dir, "diff.patch"))}\n`,
|
|
88
|
+
"utf8",
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return { ok: true, slug, id, dir, head: head.stdout.trim(), dirty, untracked };
|
|
92
|
+
}
|
|
@@ -17,6 +17,7 @@ export interface TaskRecord {
|
|
|
17
17
|
id: string;
|
|
18
18
|
title: string;
|
|
19
19
|
files: { path: string; isNew: boolean }[];
|
|
20
|
+
depends: string[] | null;
|
|
20
21
|
evidence: string | null;
|
|
21
22
|
review: number | null;
|
|
22
23
|
reviewRaw: string | null;
|
|
@@ -27,6 +28,13 @@ export interface WorkloadSection {
|
|
|
27
28
|
declaredTotal: number | null;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
export function parseDepends(raw: string): string[] {
|
|
32
|
+
const value = raw.trim();
|
|
33
|
+
if (value === "" || value === "[]" || /^(none|n\/a|na)$/i.test(value)) return [];
|
|
34
|
+
const ids = [...value.matchAll(/\bT\d+\b/g)].map((m) => m[0]);
|
|
35
|
+
return [...new Set(ids)];
|
|
36
|
+
}
|
|
37
|
+
|
|
30
38
|
export function parseTasks(text: string): { tasks: TaskRecord[]; workload: WorkloadSection | null; defects: ValidationDefect[] } {
|
|
31
39
|
const tasks: TaskRecord[] = [];
|
|
32
40
|
const defects: ValidationDefect[] = [];
|
|
@@ -43,7 +51,7 @@ export function parseTasks(text: string): { tasks: TaskRecord[]; workload: Workl
|
|
|
43
51
|
for (const line of lines) {
|
|
44
52
|
const task = line.match(/^\s*- \[[ xX]\]\s+\*\*(T\d+)\.\s+([^*]+)\*\*/) ?? line.match(/^###\s+(T\d+)\s+[—-]\s+(.+)$/) ?? line.match(/^##\s+\[[ xX]\]\s+(T\d+)\s*(?:[—-]\s*(.+))?$/);
|
|
45
53
|
if (task) {
|
|
46
|
-
current = { id: task[1], title: (task[2] ?? "").trim(), files: [], evidence: null, review: null, reviewRaw: null };
|
|
54
|
+
current = { id: task[1], title: (task[2] ?? "").trim(), files: [], depends: null, evidence: null, review: null, reviewRaw: null };
|
|
47
55
|
tasks.push(current);
|
|
48
56
|
collectingFiles = false;
|
|
49
57
|
continue;
|
|
@@ -54,6 +62,11 @@ export function parseTasks(text: string): { tasks: TaskRecord[]; workload: Workl
|
|
|
54
62
|
pushFile(line);
|
|
55
63
|
continue;
|
|
56
64
|
}
|
|
65
|
+
if (/^\s*-\s+depends:/.test(line)) {
|
|
66
|
+
collectingFiles = false;
|
|
67
|
+
current.depends = parseDepends(line.replace(/^\s*-\s+depends:\s*/, ""));
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
57
70
|
if (/^\s*-\s+evidence:/.test(line)) {
|
|
58
71
|
collectingFiles = false;
|
|
59
72
|
current.evidence = line.replace(/^\s*-\s+evidence:\s*/, "").trim();
|
|
@@ -73,8 +86,16 @@ export function parseTasks(text: string): { tasks: TaskRecord[]; workload: Workl
|
|
|
73
86
|
}
|
|
74
87
|
}
|
|
75
88
|
|
|
76
|
-
|
|
89
|
+
const order = new Map(tasks.map((task, index) => [task.id, index] as const));
|
|
90
|
+
for (const [index, task] of tasks.entries()) {
|
|
77
91
|
if (task.files.length === 0) defects.push({ kind: "missing-files", task: task.id, message: `${task.id} is missing files list` });
|
|
92
|
+
if (task.depends === null) defects.push({ kind: "missing-depends", task: task.id, message: `${task.id} is missing depends list` });
|
|
93
|
+
else for (const dep of task.depends) {
|
|
94
|
+
const depIndex = order.get(dep);
|
|
95
|
+
if (dep === task.id) defects.push({ kind: "self-depends", task: task.id, message: `${task.id} cannot depend on itself` });
|
|
96
|
+
else if (depIndex === undefined) defects.push({ kind: "unknown-depends", task: task.id, message: `${task.id} depends on unknown ${dep}` });
|
|
97
|
+
else if (depIndex > index) defects.push({ kind: "forward-depends", task: task.id, message: `${task.id} depends on later task ${dep}` });
|
|
98
|
+
}
|
|
78
99
|
if (task.evidence === null || task.evidence === "") defects.push({ kind: "missing-evidence", task: task.id, message: `${task.id} is missing evidence` });
|
|
79
100
|
if (task.reviewRaw === null) defects.push({ kind: "missing-review", task: task.id, message: `${task.id} is missing review estimate` });
|
|
80
101
|
else if (task.review === null) defects.push({ kind: "non-integer-review", task: task.id, message: `${task.id} review estimate is not an integer` });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gonrocca/zero-pi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.59",
|
|
4
4
|
"description": "zero-pi — an installable layer for pi (pi.dev): the zero spec-driven development workflow (explore → plan → build → veredicto) with per-phase model autotune and token-efficient batched builds. Adds capability to pi without modifying pi.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"./extensions/zero-status-extension.ts",
|
|
38
38
|
"./extensions/zero-doctor-extension.ts",
|
|
39
39
|
"./extensions/zero-cost-extension.ts",
|
|
40
|
+
"./extensions/zero-checkpoint-extension.ts",
|
|
40
41
|
"./extensions/zero-pr-extension.ts",
|
|
41
42
|
"./extensions/zero-branch-extension.ts",
|
|
42
43
|
"./extensions/zero-git-validate-extension.ts",
|
|
@@ -72,6 +73,8 @@
|
|
|
72
73
|
"extensions/zero-doctor-extension.ts",
|
|
73
74
|
"extensions/zero-cost.ts",
|
|
74
75
|
"extensions/zero-cost-extension.ts",
|
|
76
|
+
"extensions/zero-checkpoint.ts",
|
|
77
|
+
"extensions/zero-checkpoint-extension.ts",
|
|
75
78
|
"extensions/format-tokens.ts",
|
|
76
79
|
"extensions/pr-body.ts",
|
|
77
80
|
"extensions/sdd-links.ts",
|
package/prompts/orchestrator.md
CHANGED
|
@@ -112,6 +112,14 @@ findings, file dumps — into a brief; reference them by path. Re-passing contex
|
|
|
112
112
|
the sub-agent can read for itself is wasted tokens on every invocation, and a
|
|
113
113
|
batched build issues many briefs.
|
|
114
114
|
|
|
115
|
+
## Plan quality gate
|
|
116
|
+
|
|
117
|
+
After **plan** returns, run `/zero-validate <slug>` when the command is available. Treat structural validation errors as a plan failure: summarize the defects, re-run **plan** with those exact defects once, and validate again before entering build. Warnings (for example an intentionally-missing optional proposal) can proceed only when you name the warning in the phase summary. The gate specifically protects the dependency graph: every task must carry `files`, `depends`, `evidence`, and `review`; dependencies must point backward to known task ids; and the review workload total must match.
|
|
118
|
+
|
|
119
|
+
## Pre-build checkpoint
|
|
120
|
+
|
|
121
|
+
Before the first **build** batch of every round, run `/zero-checkpoint <slug>` when available. It writes a patch-based checkpoint under `.sdd/<slug>/checkpoints/<id>/` so a risky build has a concrete restore trail. If the command is missing or reports untracked files that were not captured, continue only after surfacing that risk in the build phase-start summary. Do not run destructive restore commands automatically; the checkpoint only records evidence and a reviewed `restore.sh`.
|
|
122
|
+
|
|
115
123
|
## Build batching
|
|
116
124
|
|
|
117
125
|
The **build** phase is not one monolithic sub-agent that implements every
|
|
@@ -122,23 +130,30 @@ bounded batches, each a fresh `zero-build` sub-agent.
|
|
|
122
130
|
Before delegating build — a fresh build phase, or a `corregir`/`replantear`
|
|
123
131
|
re-run — drive this loop:
|
|
124
132
|
|
|
125
|
-
1. Read the unchecked (`[ ]`) tasks from `tasks.md` in listed order
|
|
126
|
-
`review: ~N changed lines` estimates from the
|
|
127
|
-
|
|
128
|
-
|
|
133
|
+
1. Read the unchecked (`[ ]`) tasks from `tasks.md` in listed order, their
|
|
134
|
+
`depends:` edges, and their `review: ~N changed lines` estimates from the
|
|
135
|
+
`## Review Workload` section.
|
|
136
|
+
2. Treat the task order as a validated topological order. A task is eligible for
|
|
137
|
+
the next batch only when every dependency in its `depends:` list is already
|
|
138
|
+
checked `[x]` or is also included earlier in the same contiguous batch. If an
|
|
139
|
+
unchecked task depends on a later/unknown/unchecked-outside-batch task, stop
|
|
140
|
+
and re-run **plan** — do not improvise a new order in build.
|
|
141
|
+
3. Group the eligible unchecked tasks into ordered batches with this exact,
|
|
142
|
+
deterministic rule:
|
|
129
143
|
- Walk the unchecked tasks in order, accumulating into the current batch.
|
|
130
144
|
- Start a new batch when adding the next task would push the batch's summed
|
|
131
145
|
estimate over **800 changed lines**, or when the current batch already
|
|
132
146
|
holds **4 tasks** — whichever comes first.
|
|
133
147
|
- A single task whose own estimate exceeds 800 is its own batch.
|
|
134
148
|
- If estimates are missing or unparseable, group by the 4-task cap alone.
|
|
135
|
-
|
|
149
|
+
4. Invoke `zero-build` once per batch, in listed order. Each brief is a fresh
|
|
136
150
|
sub-agent (no carried conversation) and names the batch's task numbers
|
|
137
|
-
explicitly (for example, "implement tasks 4–6 only, then return").
|
|
138
|
-
|
|
139
|
-
the
|
|
140
|
-
|
|
141
|
-
|
|
151
|
+
explicitly (for example, "implement tasks 4–6 only, then return"). Include the
|
|
152
|
+
dependency constraint in the brief: "do not start a task until its `depends:`
|
|
153
|
+
entries are `[x]`." Emit the build phase-start line for each batch, noting
|
|
154
|
+
the batch as `lote <i>/<n>`, so the loop stays visible. Wait for each batch to
|
|
155
|
+
return before starting the next.
|
|
156
|
+
5. Repeat until `tasks.md` has no `[ ]` task left, then run **veredicto** once.
|
|
142
157
|
Never run veredicto between batches.
|
|
143
158
|
|
|
144
159
|
**Single-batch features behave exactly like before:** when every unchecked task
|
package/prompts/phases/build.md
CHANGED
|
@@ -23,8 +23,7 @@ re-reading the same large file repeatedly is the main avoidable token cost. If
|
|
|
23
23
|
the code roots are missing or wrong, run a single targeted search to fix them,
|
|
24
24
|
then proceed — never fall back to scanning the whole tree.
|
|
25
25
|
|
|
26
|
-
Implement the planned tasks in order, test-first where practical. Keep every
|
|
27
|
-
change within the plan's scope — do not expand it on your own initiative.
|
|
26
|
+
Implement the planned tasks in dependency order, test-first where practical. `tasks.md` is a dependency-aware graph: every task has a `depends:` line. Before starting a task, verify each listed dependency is already `[x]`; if a dependency is unchecked, complete that dependency first (when it is in your assigned batch) or stop and report the blocked task (when it is outside the assigned batch). Never skip ahead just because a later task looks easier. Keep every change within the plan's scope — do not expand it on your own initiative.
|
|
28
27
|
|
|
29
28
|
## Strict TDD gate
|
|
30
29
|
|
package/prompts/phases/plan.md
CHANGED
|
@@ -73,6 +73,7 @@ Use this exact checklist shape so build/validate/review can parse it without red
|
|
|
73
73
|
- `<root>/src/example.ts`
|
|
74
74
|
- `<root>/src/example.test.ts` (new)
|
|
75
75
|
- detail: concrete implementation notes and boundaries.
|
|
76
|
+
- depends: []
|
|
76
77
|
- evidence: `npm test -- example` passes, or the exact manual check expected.
|
|
77
78
|
- review: ~120 changed lines
|
|
78
79
|
```
|
|
@@ -81,9 +82,14 @@ Rules:
|
|
|
81
82
|
- Task ids are monotonic `T###`; keep existing completed ids stable on resume.
|
|
82
83
|
- Add `[P]` only for tasks that are truly parallel-safe, and `[Story:S1]`/`[Story:S2]` when the plan has independently testable stories.
|
|
83
84
|
- `files:` is mandatory and uses exact paths; append `(new)` for created files.
|
|
85
|
+
- `depends:` is mandatory. Use `depends: []` for root tasks, or a comma-separated list of earlier task ids (`depends: T001, T002`) for prerequisites. Dependencies must point only backward so the written task order is already a valid topological order.
|
|
84
86
|
- `evidence:` is mandatory and names a concrete verification command or artifact.
|
|
85
87
|
- Keep `## Review Workload` present and synchronized with the task list.
|
|
86
88
|
|
|
89
|
+
## Dependency graph discipline
|
|
90
|
+
|
|
91
|
+
Treat `tasks.md` as a dependency-aware task graph, not just a checklist. Split work so every task has explicit prerequisites, no self-dependencies, no unknown dependencies, and no dependency on a later task. Use the graph to mark real parallelism: a task can get `[P]` only when all of its dependencies are complete and it does not touch the same files as another candidate parallel task.
|
|
92
|
+
|
|
87
93
|
## TDD-shaped tasks
|
|
88
94
|
|
|
89
95
|
Unless the project sets `tdd.mode: "off"` in `.sdd/config.json`, the build phase
|
|
@@ -109,6 +115,8 @@ GREEN cycle and the TDD Cycle Evidence table.
|
|
|
109
115
|
|
|
110
116
|
## Constitution / Steering check
|
|
111
117
|
|
|
118
|
+
Before finalizing tasks, run `/zero-validate <slug>` when available, or manually apply the same checks: all four artifacts present, every task has `files`, `depends`, `evidence`, and `review`, dependency edges point backward to known task ids, review workload totals match, and the spec delta passes guardrails. Fix the artifacts before returning if the validation fails.
|
|
119
|
+
|
|
112
120
|
Before finalizing tasks, check project steering/constitution files when present (`.sdd/constitution.md`, `.sdd/steering.md`, `.kiro/steering/*`, or project equivalents). If absent, mark `n/a`; absence is not a blocker. Include this table in `design.md` or `tasks.md`:
|
|
113
121
|
|
|
114
122
|
| rule | status | waiver |
|