@buffcore/cli 0.8.42 → 0.8.43
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/CHANGELOG.md +11 -0
- package/dist/commands/design/references.d.ts +9 -0
- package/dist/commands/design/references.js +20 -1
- package/dist/commands/extension/places-approval.d.ts +1 -1
- package/dist/commands/prototype/comp-record.d.ts +27 -0
- package/dist/commands/prototype/comp-record.js +85 -0
- package/dist/commands/prototype/comp-words.d.ts +12 -0
- package/dist/commands/prototype/comp-words.js +24 -0
- package/dist/commands/prototype/comp.d.ts +26 -0
- package/dist/commands/prototype/comp.js +71 -0
- package/dist/commands/prototype/milestone-versions.d.ts +1 -1
- package/dist/commands/prototype/record-look-child.js +4 -1
- package/dist/commands/prototype/snapshot-command.js +1 -1
- package/dist/commands/prototype/snapshot-setup.d.ts +11 -0
- package/dist/commands/prototype/snapshot-setup.js +29 -5
- package/dist/commands/prototype/versions.d.ts +3 -3
- package/dist/commands/ui/agents-route.d.ts +2 -0
- package/dist/commands/ui/agents-route.js +8 -5
- package/dist/commands/ui/approval-authority.d.ts +1 -1
- package/dist/commands/ui/approve.js +7 -1
- package/dist/commands/ui/chat/codex-idle.d.ts +1 -0
- package/dist/commands/ui/chat/fresh-critique.d.ts +28 -0
- package/dist/commands/ui/chat/fresh-critique.js +121 -0
- package/dist/commands/ui/chat/fresh-launch.d.ts +31 -0
- package/dist/commands/ui/chat/fresh-launch.js +122 -0
- package/dist/commands/ui/chat/fresh-prompts.d.ts +17 -0
- package/dist/commands/ui/chat/fresh-prompts.js +76 -0
- package/dist/commands/ui/chat/fresh-report.d.ts +8 -0
- package/dist/commands/ui/chat/fresh-report.js +36 -0
- package/dist/commands/ui/chat/fresh-routes.d.ts +3 -0
- package/dist/commands/ui/chat/fresh-routes.js +64 -0
- package/dist/commands/ui/chat/fresh-settings.d.ts +12 -0
- package/dist/commands/ui/chat/fresh-settings.js +66 -0
- package/dist/commands/ui/chat/fresh-store.d.ts +17 -0
- package/dist/commands/ui/chat/fresh-store.js +67 -0
- package/dist/commands/ui/chat/fresh.d.ts +31 -0
- package/dist/commands/ui/chat/fresh.en.d.ts +7 -0
- package/dist/commands/ui/chat/fresh.en.js +7 -0
- package/dist/commands/ui/chat/fresh.es.d.ts +7 -0
- package/dist/commands/ui/chat/fresh.es.js +7 -0
- package/dist/commands/ui/chat/fresh.js +112 -0
- package/dist/commands/ui/chat/limit-mentions.d.ts +2 -2
- package/dist/commands/ui/chat/limit-mentions.js +22 -2
- package/dist/commands/ui/chat/limits.d.ts +8 -1
- package/dist/commands/ui/chat/limits.en.d.ts +2 -0
- package/dist/commands/ui/chat/limits.en.js +2 -0
- package/dist/commands/ui/chat/limits.es.js +2 -0
- package/dist/commands/ui/chat/limits.js +13 -1
- package/dist/commands/ui/chat/parse-claude.js +7 -3
- package/dist/commands/ui/chat/permission-ask.js +3 -0
- package/dist/commands/ui/chat/planning-limits.js +15 -0
- package/dist/commands/ui/chat/publish-limits.d.ts +4 -0
- package/dist/commands/ui/chat/publish-limits.js +97 -0
- package/dist/commands/ui/chat/recorded-turn.js +3 -0
- package/dist/commands/ui/chat/stream.js +4 -1
- package/dist/commands/ui/chat/turn-guard.d.ts +2 -0
- package/dist/commands/ui/chat/turn-guard.js +12 -0
- package/dist/commands/ui/chat/turn-state.d.ts +5 -0
- package/dist/commands/ui/chat/turn.js +1 -0
- package/dist/commands/ui/comp-approval.d.ts +6 -0
- package/dist/commands/ui/comp-approval.js +42 -0
- package/dist/commands/ui/host.js +3 -0
- package/dist/commands/ui/index.js +18 -6
- package/dist/commands/ui/loop-approve.d.ts +4 -4
- package/dist/commands/ui/places-change.d.ts +1 -1
- package/dist/commands/ui/plan-structure.d.ts +3 -0
- package/dist/commands/ui/plan-structure.en.d.ts +3 -0
- package/dist/commands/ui/plan-structure.en.js +3 -0
- package/dist/commands/ui/plan-structure.es.d.ts +3 -0
- package/dist/commands/ui/plan-structure.es.js +3 -0
- package/dist/commands/ui/plan-structure.js +12 -4
- package/dist/commands/ui/prototype-comp-route.d.ts +7 -0
- package/dist/commands/ui/prototype-comp-route.js +109 -0
- package/dist/commands/ui/prototype-main.d.ts +2 -1
- package/dist/commands/ui/prototype-scenes-child.js +4 -1
- package/dist/commands/ui/prototype-snapshot-session.d.ts +1 -1
- package/dist/commands/ui/prototype-snapshot-session.js +3 -0
- package/dist/commands/ui/prototype-snapshot.d.ts +1 -1
- package/dist/commands/ui/routes.d.ts +6 -3
- package/dist/commands/ui/routes.js +3 -3
- package/dist/commands/ui/save-publish.js +3 -0
- package/dist/commands/ui/server.d.ts +7 -0
- package/dist/commands/ui/server.js +7 -3
- package/dist/commands/ui/structure-surface.d.ts +6 -0
- package/dist/commands/ui/structure-surface.js +94 -0
- package/dist/commands/ui/studio-routes.js +4 -0
- package/dist/commands/ui/studio-state.d.ts +6 -4
- package/dist/commands/ui/studio-versions.d.ts +3 -3
- package/dist/ui/page.js +81 -81
- package/docs/PROCESS.md +5 -0
- package/docs/PROTOTYPE.md +20 -0
- package/docs/WORKBENCH.md +2 -2
- package/package.json +11 -11
- package/skills/ape-critique/SKILL.md +9 -3
- package/skills/ape-design-system/SKILL.md +2 -1
- package/skills/ape-prototype/SKILL.md +5 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// The critique as its own turn (0.8.43): after a passing full snapshot (taken from the Prototype
|
|
2
|
+
// page, or by the Prototype step's agent during its turn) a fresh, read-only turn reads it and the
|
|
3
|
+
// studio writes design/critique.md from its answer. One at a time, one queued, and never twice for
|
|
4
|
+
// the same report in the same checkout.
|
|
5
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { CRITIQUE_FILE } from '@buffcore/contracts/workbench';
|
|
8
|
+
import { factsChanged } from '../merge-events.js';
|
|
9
|
+
import { runFresh, stopFresh } from './fresh-launch.js';
|
|
10
|
+
import { critiquePrompt, critiqueText } from './fresh-prompts.js';
|
|
11
|
+
import { passingFullReport } from './fresh-report.js';
|
|
12
|
+
import { plainPath } from './store.js';
|
|
13
|
+
const REPORT = /^prototype\/snapshots\/[a-f0-9]{7,64}$/;
|
|
14
|
+
const keyOf = ({ source, report }) => (report ? `${source.checkout}|${report}` : '');
|
|
15
|
+
export class CritiqueTurns {
|
|
16
|
+
run;
|
|
17
|
+
queued;
|
|
18
|
+
closed = false;
|
|
19
|
+
root;
|
|
20
|
+
start;
|
|
21
|
+
stop;
|
|
22
|
+
store;
|
|
23
|
+
settings;
|
|
24
|
+
constructor(root, start, stop, store, settings) {
|
|
25
|
+
this.root = root;
|
|
26
|
+
this.start = start;
|
|
27
|
+
this.stop = stop;
|
|
28
|
+
this.store = store;
|
|
29
|
+
this.settings = settings;
|
|
30
|
+
}
|
|
31
|
+
/** A Prototype turn ended: a passing full snapshot it took itself (since it began) starts one. */
|
|
32
|
+
async afterTurn(startedAt) {
|
|
33
|
+
const report = await passingFullReport(this.root, startedAt).catch(() => undefined);
|
|
34
|
+
if (report)
|
|
35
|
+
this.after({ checkout: this.root }, report);
|
|
36
|
+
}
|
|
37
|
+
/** A full snapshot passed: start its critique, or queue it behind the one running. */
|
|
38
|
+
after(source, report) {
|
|
39
|
+
const pending = { source, ...(report && REPORT.test(report) ? { report } : {}) };
|
|
40
|
+
const key = keyOf(pending);
|
|
41
|
+
if (this.closed || (key && (this.store.critiqued(key) || this.run?.key === key)))
|
|
42
|
+
return false;
|
|
43
|
+
if (this.agent() !== 'claude')
|
|
44
|
+
return false;
|
|
45
|
+
if (this.run) {
|
|
46
|
+
this.queued = pending;
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
this.begin(pending, key);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
agent() {
|
|
53
|
+
const settings = this.settings();
|
|
54
|
+
return settings?.work.validation ?? settings?.default ?? 'claude';
|
|
55
|
+
}
|
|
56
|
+
begin(pending, key) {
|
|
57
|
+
const settings = this.settings();
|
|
58
|
+
const choice = {
|
|
59
|
+
...(settings?.model?.validation ? { model: settings.model.validation } : {}),
|
|
60
|
+
...(settings?.effort?.validation ? { effort: settings.effort.validation } : {}),
|
|
61
|
+
};
|
|
62
|
+
const { source, report } = pending;
|
|
63
|
+
const record = {
|
|
64
|
+
agent: 'claude',
|
|
65
|
+
at: new Date().toISOString(),
|
|
66
|
+
...(source.change ? { change: source.change } : {}),
|
|
67
|
+
...(report ? { report } : {}),
|
|
68
|
+
};
|
|
69
|
+
keep(() => this.store.saveCritique({ ...record, state: 'running' }));
|
|
70
|
+
const folder = source.checkout;
|
|
71
|
+
const prompt = critiquePrompt(report);
|
|
72
|
+
const run = runFresh(this.start, { folder, prompt, checks: true, ...choice });
|
|
73
|
+
this.run = { ...run, key };
|
|
74
|
+
void run.result.then(({ text, seconds }) => {
|
|
75
|
+
this.run = undefined;
|
|
76
|
+
if (this.closed)
|
|
77
|
+
return;
|
|
78
|
+
const body = text === undefined ? undefined : critiqueText(text);
|
|
79
|
+
const written = !!body && keep(() => writeCritique(folder, body));
|
|
80
|
+
if (written && key)
|
|
81
|
+
keep(() => this.store.markCritiqued(key));
|
|
82
|
+
keep(() => this.store.saveCritique({
|
|
83
|
+
...record,
|
|
84
|
+
seconds,
|
|
85
|
+
state: written ? 'done' : 'failed',
|
|
86
|
+
...(written ? { file: CRITIQUE_FILE } : {}),
|
|
87
|
+
}));
|
|
88
|
+
if (written)
|
|
89
|
+
factsChanged('unsaved');
|
|
90
|
+
const next = this.queued;
|
|
91
|
+
this.queued = undefined;
|
|
92
|
+
if (next)
|
|
93
|
+
this.after(next.source, next.report);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
running() {
|
|
97
|
+
return !!this.run;
|
|
98
|
+
}
|
|
99
|
+
close() {
|
|
100
|
+
this.closed = true;
|
|
101
|
+
this.queued = undefined;
|
|
102
|
+
if (this.run)
|
|
103
|
+
stopFresh(this.run, this.stop);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** A record that cannot be written never fails the turn that started it. */
|
|
107
|
+
export function keep(write) {
|
|
108
|
+
try {
|
|
109
|
+
write();
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** The studio writes the critique; the agent's turn wrote nothing. Never through a link. */
|
|
117
|
+
function writeCritique(folder, body) {
|
|
118
|
+
const design = plainPath(join(folder, 'design'));
|
|
119
|
+
mkdirSync(design, { recursive: true });
|
|
120
|
+
writeFileSync(plainPath(join(design, 'critique.md')), body);
|
|
121
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ChildProcess } from 'node:child_process';
|
|
2
|
+
import type { AgentEffort } from '@buffcore/contracts/workbench';
|
|
3
|
+
import type { Start } from '../runner.ts';
|
|
4
|
+
import { stopTree } from '../runner.ts';
|
|
5
|
+
export interface FreshLaunch {
|
|
6
|
+
/** The product folder the turn reads. */
|
|
7
|
+
folder: string;
|
|
8
|
+
prompt: string;
|
|
9
|
+
model?: string;
|
|
10
|
+
effort?: AgentEffort;
|
|
11
|
+
/** The critique may run the design and i18n checks; the review runs nothing. */
|
|
12
|
+
checks?: boolean;
|
|
13
|
+
/** The person's home, whose Claude settings give their denies and sign-in (tests set it). */
|
|
14
|
+
home?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface FreshResult {
|
|
17
|
+
/** The agent's final answer; absent when the turn failed. */
|
|
18
|
+
text?: string;
|
|
19
|
+
seconds: number;
|
|
20
|
+
}
|
|
21
|
+
/** The read-only arguments for one fresh Claude turn. */
|
|
22
|
+
export declare function freshArgs(launch: FreshLaunch, settings: string): string[];
|
|
23
|
+
/** A running fresh turn: its child, to stop it, and its result. */
|
|
24
|
+
export interface FreshRun {
|
|
25
|
+
child?: ChildProcess;
|
|
26
|
+
result: Promise<FreshResult>;
|
|
27
|
+
}
|
|
28
|
+
/** Start one fresh turn; its result never rejects (a failure has no text). */
|
|
29
|
+
export declare function runFresh(start: Start, launch: FreshLaunch): FreshRun;
|
|
30
|
+
/** Stop a fresh turn's process tree. */
|
|
31
|
+
export declare function stopFresh(run: FreshRun, stop?: typeof stopTree): void;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { stopTree } from '../runner.js';
|
|
2
|
+
import { stdinInput } from './attachments.js';
|
|
3
|
+
import { secretReads } from './claude-permissions.js';
|
|
4
|
+
import { AGENT_DEFS } from './defs.js';
|
|
5
|
+
import { openFreshSettings, settingDenies } from './fresh-settings.js';
|
|
6
|
+
import { scrubLaunchIds } from './launch-ids.js';
|
|
7
|
+
import { claudeAdapter } from './parse-claude.js';
|
|
8
|
+
import { COMMENT_PATH } from './permissions.js';
|
|
9
|
+
import { PLANNING_LIMITS } from './planning-limits.js';
|
|
10
|
+
import { jsonLine } from './prompt.js';
|
|
11
|
+
import { launchArguments } from './turn-guard.js';
|
|
12
|
+
import { turnOverrides } from './turn-overrides.js';
|
|
13
|
+
/**
|
|
14
|
+
* Checks the critique may run, exactly as written: no prefix rule, so no added argument or
|
|
15
|
+
* redirection to a file matches. The review runs none.
|
|
16
|
+
*/
|
|
17
|
+
const CRITIQUE_CHECKS = ['pnpm ape design check', 'pnpm ape i18n check'];
|
|
18
|
+
/** Every tool that writes, runs or reaches out, or starts another agent; reads stay. */
|
|
19
|
+
const WRITES = ['Edit', 'Write', 'NotebookEdit', 'WebFetch', 'WebSearch', 'Task', 'Agent'];
|
|
20
|
+
/** The read-only arguments for one fresh Claude turn. */
|
|
21
|
+
export function freshArgs(launch, settings) {
|
|
22
|
+
const turn = AGENT_DEFS.find((def) => def.id === 'claude')?.turn;
|
|
23
|
+
if (!turn)
|
|
24
|
+
throw new Error('claude has no headless turn');
|
|
25
|
+
const checks = launch.checks
|
|
26
|
+
? CRITIQUE_CHECKS.flatMap((command) => [`Bash(${command})`, `PowerShell(${command})`])
|
|
27
|
+
: [];
|
|
28
|
+
return [
|
|
29
|
+
...turn.start,
|
|
30
|
+
'--permission-mode',
|
|
31
|
+
'dontAsk',
|
|
32
|
+
// No user, project or local settings: their allow rules could widen what dontAsk runs.
|
|
33
|
+
'--setting-sources=',
|
|
34
|
+
'--settings',
|
|
35
|
+
settings,
|
|
36
|
+
'--allowedTools',
|
|
37
|
+
'Read',
|
|
38
|
+
'Glob',
|
|
39
|
+
'Grep',
|
|
40
|
+
...checks,
|
|
41
|
+
'--disallowedTools',
|
|
42
|
+
...WRITES,
|
|
43
|
+
...(launch.checks ? [] : ['Bash', 'PowerShell']),
|
|
44
|
+
`Read(//**/${COMMENT_PATH}/**)`,
|
|
45
|
+
...secretReads(launch.folder),
|
|
46
|
+
...PLANNING_LIMITS,
|
|
47
|
+
...settingDenies(launch.folder, launch.home),
|
|
48
|
+
...turnOverrides('claude', { model: launch.model, effort: launch.effort }).args,
|
|
49
|
+
...turn.stdin,
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
/** Start one fresh turn; its result never rejects (a failure has no text). */
|
|
53
|
+
export function runFresh(start, launch) {
|
|
54
|
+
const started = Date.now();
|
|
55
|
+
const seconds = () => Math.max(0, Math.floor((Date.now() - started) / 1000));
|
|
56
|
+
const settings = openFreshSettings(launch.home);
|
|
57
|
+
const args = freshArgs(launch, settings.value);
|
|
58
|
+
const allowed = launchArguments(args);
|
|
59
|
+
const release = () => {
|
|
60
|
+
allowed();
|
|
61
|
+
settings.close();
|
|
62
|
+
};
|
|
63
|
+
let child;
|
|
64
|
+
try {
|
|
65
|
+
child = start({ program: 'pnpm', args: ['exec', 'claude', ...args], json: true }, launch.folder);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
release();
|
|
69
|
+
return { result: Promise.resolve({ seconds: seconds() }) };
|
|
70
|
+
}
|
|
71
|
+
const result = new Promise((done) => {
|
|
72
|
+
const adapter = claudeAdapter();
|
|
73
|
+
let pending = '';
|
|
74
|
+
let answer;
|
|
75
|
+
let last;
|
|
76
|
+
let failed = false;
|
|
77
|
+
const line = (text) => {
|
|
78
|
+
const record = jsonLine(text);
|
|
79
|
+
if (record.type === 'result') {
|
|
80
|
+
if (record.is_error === true || record.subtype !== 'success')
|
|
81
|
+
failed = true;
|
|
82
|
+
else if (typeof record.result === 'string')
|
|
83
|
+
answer = record.result;
|
|
84
|
+
}
|
|
85
|
+
for (const event of adapter.parse(text)) {
|
|
86
|
+
if (event.kind === 'text')
|
|
87
|
+
last = event.text;
|
|
88
|
+
if (event.kind === 'error')
|
|
89
|
+
failed = true;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
child.stdout?.setEncoding('utf8');
|
|
93
|
+
child.stdout?.on('data', (chunk) => {
|
|
94
|
+
const lines = (pending + chunk).split('\n');
|
|
95
|
+
pending = lines.pop() ?? '';
|
|
96
|
+
for (const text of lines)
|
|
97
|
+
if (text.trim())
|
|
98
|
+
line(text);
|
|
99
|
+
});
|
|
100
|
+
let ended = false;
|
|
101
|
+
const end = (code) => {
|
|
102
|
+
if (ended)
|
|
103
|
+
return;
|
|
104
|
+
ended = true;
|
|
105
|
+
release();
|
|
106
|
+
if (pending.trim())
|
|
107
|
+
line(pending);
|
|
108
|
+
const text = failed || code !== 0 ? undefined : (answer ?? last);
|
|
109
|
+
done({ ...(text?.trim() ? { text: scrubLaunchIds(text) } : {}), seconds: seconds() });
|
|
110
|
+
};
|
|
111
|
+
child.once('error', () => end(1));
|
|
112
|
+
child.once('close', (code) => end(code));
|
|
113
|
+
});
|
|
114
|
+
child.stdin?.on('error', () => { });
|
|
115
|
+
child.stdin?.end(stdinInput('claude', launch.prompt, []));
|
|
116
|
+
return { child, result };
|
|
117
|
+
}
|
|
118
|
+
/** Stop a fresh turn's process tree. */
|
|
119
|
+
export function stopFresh(run, stop = stopTree) {
|
|
120
|
+
if (run.child)
|
|
121
|
+
stop(run.child, true);
|
|
122
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Chat, type ReviewStep } from '@buffcore/contracts/workbench';
|
|
2
|
+
/** A review step whose turn just wrote one of its own files, by the turn's own edit calls. */
|
|
3
|
+
export declare function reviewStepOf(chat: Chat, firstMessage: number): ReviewStep | undefined;
|
|
4
|
+
/** The step's last recorded model and effort, as its own turn asked for them. */
|
|
5
|
+
export declare function stepChoice(chat: Chat): {
|
|
6
|
+
model?: string | undefined;
|
|
7
|
+
effort?: "high" | "low" | "max" | "medium" | "xhigh" | undefined;
|
|
8
|
+
};
|
|
9
|
+
export declare function reviewPrompt(step: ReviewStep): string;
|
|
10
|
+
export declare function critiquePrompt(report: string | undefined): string;
|
|
11
|
+
/** The findings in a reviewer's answer: one JSON object, perhaps fenced; else none. */
|
|
12
|
+
export declare function parseFindings(text: string): string[] | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The critique's Markdown, without a fence the agent may have wrapped it in; none unless it has
|
|
15
|
+
* the skill's shape (a reply such as "I couldn't write the file" is not a critique).
|
|
16
|
+
*/
|
|
17
|
+
export declare function critiqueText(text: string): string | undefined;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// What a fresh turn is asked, and how its answer is read: the review's findings (at most seven, one
|
|
2
|
+
// sentence each; anything malformed is dropped quietly) and the critique's Markdown.
|
|
3
|
+
import { FINDING_LENGTH, MAX_FINDINGS, REVIEW_STEPS, stepOwnsFile, stepSaveFiles, } from '@buffcore/contracts/workbench';
|
|
4
|
+
import { createI18n, matchLocale } from '@buffcore/i18n';
|
|
5
|
+
import { en } from './fresh.en.js';
|
|
6
|
+
import { es } from './fresh.es.js';
|
|
7
|
+
import { isPlanning } from './permissions.js';
|
|
8
|
+
import { writtenFiles } from './turn-scenes.js';
|
|
9
|
+
const messages = createI18n({ catalogs: [{ en, es }] });
|
|
10
|
+
const t = (key, values) => messages.t(matchLocale((process.env.LC_ALL || process.env.LC_MESSAGES || process.env.LANG || 'en')
|
|
11
|
+
.replace('_', '-')
|
|
12
|
+
.split('.')[0], messages.locales), key, values);
|
|
13
|
+
/** A review step whose turn just wrote one of its own files, by the turn's own edit calls. */
|
|
14
|
+
export function reviewStepOf(chat, firstMessage) {
|
|
15
|
+
const step = REVIEW_STEPS.find((name) => name === chat.page);
|
|
16
|
+
if (!step || chat.change || !isPlanning(chat.work) || chat.title !== step)
|
|
17
|
+
return;
|
|
18
|
+
const written = [...writtenFiles(chat, firstMessage)];
|
|
19
|
+
return written.some((file) => stepOwnsFile(step, file)) ? step : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** The step's last recorded model and effort, as its own turn asked for them. */
|
|
22
|
+
export function stepChoice(chat) {
|
|
23
|
+
const turn = chat.messages.findLast((message) => message.turn)?.turn;
|
|
24
|
+
const model = turn?.model.requested ?? undefined;
|
|
25
|
+
const effort = turn?.effort.requested ?? undefined;
|
|
26
|
+
return {
|
|
27
|
+
...(model && model !== 'auto' ? { model } : {}),
|
|
28
|
+
...(effort && effort !== 'auto' ? { effort } : {}),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const files = (steps) => steps.flatMap((step) => stepSaveFiles[step]).join(', ') || t('fresh.none');
|
|
32
|
+
export function reviewPrompt(step) {
|
|
33
|
+
const earlier = REVIEW_STEPS.slice(0, REVIEW_STEPS.indexOf(step));
|
|
34
|
+
const shape = JSON.stringify({ findings: [t('fresh.example')] });
|
|
35
|
+
return `${t('fresh.review', { step, files: files([step]), earlier: files(earlier) })}\n${shape}\n${t('fresh.reviewLimit')}\n`;
|
|
36
|
+
}
|
|
37
|
+
export function critiquePrompt(report) {
|
|
38
|
+
return `${t('fresh.critique', { report: report ? `${report}/report.json` : 'prototype/snapshots/' })}\n`;
|
|
39
|
+
}
|
|
40
|
+
/** The findings in a reviewer's answer: one JSON object, perhaps fenced; else none. */
|
|
41
|
+
export function parseFindings(text) {
|
|
42
|
+
const start = text.indexOf('{');
|
|
43
|
+
const end = text.lastIndexOf('}');
|
|
44
|
+
if (start < 0 || end <= start)
|
|
45
|
+
return;
|
|
46
|
+
let value;
|
|
47
|
+
try {
|
|
48
|
+
value = JSON.parse(text.slice(start, end + 1));
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const list = value?.findings;
|
|
54
|
+
if (!Array.isArray(list))
|
|
55
|
+
return;
|
|
56
|
+
const findings = list
|
|
57
|
+
.filter((item) => typeof item === 'string')
|
|
58
|
+
.map((item) => item.replace(/\s+/g, ' ').trim())
|
|
59
|
+
.filter((item) => item.length > 0 && item.length <= FINDING_LENGTH)
|
|
60
|
+
.slice(0, MAX_FINDINGS);
|
|
61
|
+
// Findings that were all malformed are no answer, never a clean read.
|
|
62
|
+
return list.length && !findings.length ? undefined : findings;
|
|
63
|
+
}
|
|
64
|
+
/** The report's shape in the ape-critique skill: its title and the sections it always has. */
|
|
65
|
+
const CRITIQUE_SECTIONS = [/^## Scores\b/m, /^## Keep\b/m, /^## Fix\b/m];
|
|
66
|
+
/**
|
|
67
|
+
* The critique's Markdown, without a fence the agent may have wrapped it in; none unless it has
|
|
68
|
+
* the skill's shape (a reply such as "I couldn't write the file" is not a critique).
|
|
69
|
+
*/
|
|
70
|
+
export function critiqueText(text) {
|
|
71
|
+
const fenced = /^```(?:markdown|md)?\r?\n([\s\S]*?)\r?\n```\s*$/.exec(text.trim());
|
|
72
|
+
const body = (fenced?.[1] ?? text).trim();
|
|
73
|
+
if (!/^# Critique\b/.test(body) || !CRITIQUE_SECTIONS.every((section) => section.test(body)))
|
|
74
|
+
return;
|
|
75
|
+
return `${body.slice(0, 200_000)}\n`;
|
|
76
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Chat } from '@buffcore/contracts/workbench';
|
|
2
|
+
/** A Prototype step's own chat: its turns draw the scenes and take the snapshots. */
|
|
3
|
+
export declare const prototypeTurn: (chat: Chat) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* The newest report's folder (`prototype/snapshots/<commit>`) when it is full, passed and was
|
|
6
|
+
* written at or after `since` (the turn's start): a report the turn never took does not count.
|
|
7
|
+
*/
|
|
8
|
+
export declare function passingFullReport(root: string, since: number): Promise<string | undefined>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The snapshot a Prototype turn's own `ape prototype snapshot` wrote: the newest report under
|
|
2
|
+
// prototype/snapshots/, read only. A full one (no scene filter) with no problems passed.
|
|
3
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
const isReport = (value) => {
|
|
6
|
+
const report = value;
|
|
7
|
+
return (typeof report?.commit === 'string' &&
|
|
8
|
+
/^[a-f0-9]{7,64}$/.test(report.commit) &&
|
|
9
|
+
typeof report.createdAt === 'string' &&
|
|
10
|
+
Array.isArray(report.problems));
|
|
11
|
+
};
|
|
12
|
+
/** A Prototype step's own chat: its turns draw the scenes and take the snapshots. */
|
|
13
|
+
export const prototypeTurn = (chat) => chat.page === 'prototype' && chat.title === 'prototype' && chat.work === 'design';
|
|
14
|
+
/**
|
|
15
|
+
* The newest report's folder (`prototype/snapshots/<commit>`) when it is full, passed and was
|
|
16
|
+
* written at or after `since` (the turn's start): a report the turn never took does not count.
|
|
17
|
+
*/
|
|
18
|
+
export async function passingFullReport(root, since) {
|
|
19
|
+
const dir = join(root, 'prototype', 'snapshots');
|
|
20
|
+
const folders = await readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
21
|
+
const reports = await Promise.all(folders
|
|
22
|
+
.filter((folder) => folder.isDirectory() && !folder.isSymbolicLink())
|
|
23
|
+
.map((folder) => readFile(join(dir, folder.name, 'report.json'), 'utf8').then((text) => {
|
|
24
|
+
const report = JSON.parse(text);
|
|
25
|
+
return isReport(report) && report.commit === folder.name ? report : undefined;
|
|
26
|
+
}, () => undefined)));
|
|
27
|
+
const newest = reports
|
|
28
|
+
.filter((report) => report !== undefined)
|
|
29
|
+
.sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0];
|
|
30
|
+
if (!newest || newest.scene !== undefined || newest.problems.length)
|
|
31
|
+
return;
|
|
32
|
+
const created = Date.parse(newest.createdAt);
|
|
33
|
+
if (!(created >= since))
|
|
34
|
+
return;
|
|
35
|
+
return `prototype/snapshots/${newest.commit}`;
|
|
36
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The fresh turns' routes: read a step's review or the critique, Take or Leave a finding, and the
|
|
2
|
+
// review's setting (Settings › Agents), kept in plan/agents.md with the other agent settings.
|
|
3
|
+
import { freshAnswerBody, freshReplySchema, freshStepSchema, reviewSettingBody, } from '@buffcore/contracts/workbench';
|
|
4
|
+
import { readAgentsFile, writeAgentsFile } from '../../agents-file.js';
|
|
5
|
+
import { queueAgentsWrite } from '../agents-route.js';
|
|
6
|
+
import { factsChanged } from '../merge-events.js';
|
|
7
|
+
import { t } from './prompt.js';
|
|
8
|
+
const invalid = { status: 400, body: { error: t('chat.invalid') } };
|
|
9
|
+
async function saveSetting(root, on) {
|
|
10
|
+
const before = (await readAgentsFile(root)) ?? {
|
|
11
|
+
default: 'claude',
|
|
12
|
+
work: {},
|
|
13
|
+
level: 'guide',
|
|
14
|
+
};
|
|
15
|
+
const { review: _old, ...rest } = before;
|
|
16
|
+
if ((before.review !== false) === on)
|
|
17
|
+
return;
|
|
18
|
+
await writeAgentsFile(root, on ? rest : { ...rest, review: false });
|
|
19
|
+
factsChanged('levels', 'unsaved');
|
|
20
|
+
}
|
|
21
|
+
export function freshRoutes(fresh, root) {
|
|
22
|
+
return [
|
|
23
|
+
{
|
|
24
|
+
method: 'GET',
|
|
25
|
+
path: '/api/fresh/:step',
|
|
26
|
+
right: 'read:chats',
|
|
27
|
+
handle: ({ params }) => {
|
|
28
|
+
const step = freshStepSchema.safeParse(params.step);
|
|
29
|
+
if (!step.success)
|
|
30
|
+
return invalid;
|
|
31
|
+
return { status: 200, body: freshReplySchema.parse(fresh.read(step.data)) };
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
method: 'POST',
|
|
36
|
+
path: '/api/fresh/:step/answer',
|
|
37
|
+
right: 'build',
|
|
38
|
+
origin: true,
|
|
39
|
+
handle: async ({ params, body }) => {
|
|
40
|
+
const step = freshStepSchema.safeParse(params.step);
|
|
41
|
+
const answer = freshAnswerBody.safeParse(await body().catch(() => undefined));
|
|
42
|
+
if (!step.success || step.data === 'prototype' || !answer.success)
|
|
43
|
+
return invalid;
|
|
44
|
+
const { id, answer: choice } = answer.data;
|
|
45
|
+
return fresh.answer(step.data, id, choice)
|
|
46
|
+
? { status: 200, body: { ok: true } }
|
|
47
|
+
: { status: 404, body: { error: t('chat.missing') } };
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
method: 'POST',
|
|
52
|
+
path: '/api/agents/review',
|
|
53
|
+
right: 'settings',
|
|
54
|
+
origin: true,
|
|
55
|
+
handle: async ({ body }) => {
|
|
56
|
+
const setting = reviewSettingBody.safeParse(await body().catch(() => undefined));
|
|
57
|
+
if (!setting.success)
|
|
58
|
+
return invalid;
|
|
59
|
+
await queueAgentsWrite(root, () => saveSetting(root, setting.data.on));
|
|
60
|
+
return { status: 202, body: { ok: true } };
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The deny rules of the product's and the person's settings, as plain values a launch can quote. */
|
|
2
|
+
export declare function settingDenies(folder: string, home?: string): string[];
|
|
3
|
+
/** The turn's flag settings: hooks off, and the person's sign-in keys when they have any. */
|
|
4
|
+
export declare function freshSettings(home?: string): Record<string, unknown>;
|
|
5
|
+
/**
|
|
6
|
+
* The `--settings` value for one launch: the fixed JSON when the person has no sign-in keys, else
|
|
7
|
+
* a file of the launch's own (their values never pass through a command line), removed with it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function openFreshSettings(home?: string): {
|
|
10
|
+
value: string;
|
|
11
|
+
close: () => void;
|
|
12
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// What a fresh turn keeps of the person's and the product's Claude settings, though it loads none
|
|
2
|
+
// of their files (`--setting-sources=`, so no allow rule can widen what it runs): their deny rules,
|
|
3
|
+
// and the person's sign-in setup. Never their allow rules, hooks or MCP servers.
|
|
4
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { homedir, tmpdir } from 'node:os';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { PLANNING_SETTINGS } from './permissions.js';
|
|
8
|
+
/** The product's settings files, under its folder. */
|
|
9
|
+
const PROJECT_SETTINGS = ['.claude/settings.json', '.claude/settings.local.json'];
|
|
10
|
+
/** The person's settings keys that only say how Claude Code signs in or reports. */
|
|
11
|
+
const SIGN_IN_KEYS = [
|
|
12
|
+
'env',
|
|
13
|
+
'apiKeyHelper',
|
|
14
|
+
'awsAuthRefresh',
|
|
15
|
+
'awsCredentialExport',
|
|
16
|
+
'otelHeadersHelper',
|
|
17
|
+
];
|
|
18
|
+
function settingsFile(path) {
|
|
19
|
+
try {
|
|
20
|
+
const value = JSON.parse(readFileSync(path, 'utf8'));
|
|
21
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
22
|
+
? value
|
|
23
|
+
: {};
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const denies = (settings) => {
|
|
30
|
+
const deny = settings.permissions?.deny;
|
|
31
|
+
return Array.isArray(deny)
|
|
32
|
+
? deny.filter((rule) => typeof rule === 'string' && rule.length <= 300 && !/["%!\r\n]/.test(rule))
|
|
33
|
+
: [];
|
|
34
|
+
};
|
|
35
|
+
/** The deny rules of the product's and the person's settings, as plain values a launch can quote. */
|
|
36
|
+
export function settingDenies(folder, home = homedir()) {
|
|
37
|
+
return [
|
|
38
|
+
...PROJECT_SETTINGS.map((file) => join(folder, file)),
|
|
39
|
+
join(home, '.claude', 'settings.json'),
|
|
40
|
+
]
|
|
41
|
+
.flatMap((file) => denies(settingsFile(file)))
|
|
42
|
+
.slice(0, 200);
|
|
43
|
+
}
|
|
44
|
+
/** The turn's flag settings: hooks off, and the person's sign-in keys when they have any. */
|
|
45
|
+
export function freshSettings(home = homedir()) {
|
|
46
|
+
const user = settingsFile(join(home, '.claude', 'settings.json'));
|
|
47
|
+
const signIn = Object.fromEntries(SIGN_IN_KEYS.filter((key) => user[key] !== undefined).map((key) => [key, user[key]]));
|
|
48
|
+
return { ...JSON.parse(PLANNING_SETTINGS), ...signIn };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The `--settings` value for one launch: the fixed JSON when the person has no sign-in keys, else
|
|
52
|
+
* a file of the launch's own (their values never pass through a command line), removed with it.
|
|
53
|
+
*/
|
|
54
|
+
export function openFreshSettings(home = homedir()) {
|
|
55
|
+
const settings = freshSettings(home);
|
|
56
|
+
if (Object.keys(settings).length === 1)
|
|
57
|
+
return { value: PLANNING_SETTINGS, close: () => { } };
|
|
58
|
+
const folder = mkdtempSync(join(tmpdir(), 'ape-fresh-'));
|
|
59
|
+
const file = join(folder, 'settings.json');
|
|
60
|
+
if (/["%!\r\n]/.test(file)) {
|
|
61
|
+
rmSync(folder, { recursive: true, force: true });
|
|
62
|
+
return { value: PLANNING_SETTINGS, close: () => { } };
|
|
63
|
+
}
|
|
64
|
+
writeFileSync(file, JSON.stringify(settings), { mode: 0o600 });
|
|
65
|
+
return { value: file, close: () => rmSync(folder, { recursive: true, force: true }) };
|
|
66
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type FreshCritique, type FreshReview, type ReviewStep } from '@buffcore/contracts/workbench';
|
|
2
|
+
export declare class FreshStore {
|
|
3
|
+
private readonly root;
|
|
4
|
+
constructor(root: string);
|
|
5
|
+
private file;
|
|
6
|
+
private read;
|
|
7
|
+
private write;
|
|
8
|
+
review(step: ReviewStep): FreshReview | undefined;
|
|
9
|
+
saveReview(step: ReviewStep, review: FreshReview): void;
|
|
10
|
+
critique(): FreshCritique | undefined;
|
|
11
|
+
saveCritique(critique: FreshCritique): void;
|
|
12
|
+
/** A studio that died left turns marked running: none runs now, so they failed. */
|
|
13
|
+
recover(): void;
|
|
14
|
+
/** Every report already critiqued, as `<checkout>|<report>`: none runs twice. */
|
|
15
|
+
critiqued(key: string): boolean;
|
|
16
|
+
markCritiqued(key: string): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Where fresh turns' answers are kept: beside the chats, in `.ape/fresh/` (never in git). One small
|
|
2
|
+
// file per planning step's review and one for the latest critique, replaced atomically.
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { freshCritiqueSchema, freshReviewSchema, REVIEW_STEPS, } from '@buffcore/contracts/workbench';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { plainPath } from './store.js';
|
|
9
|
+
const critiquedSchema = z.array(z.string().max(4000)).max(500);
|
|
10
|
+
export class FreshStore {
|
|
11
|
+
root;
|
|
12
|
+
constructor(root) {
|
|
13
|
+
this.root = root;
|
|
14
|
+
}
|
|
15
|
+
file(name) {
|
|
16
|
+
const ape = plainPath(join(this.root, '.ape'));
|
|
17
|
+
const folder = plainPath(join(ape, 'fresh'));
|
|
18
|
+
return plainPath(join(folder, `${name}.json`));
|
|
19
|
+
}
|
|
20
|
+
read(name, schema) {
|
|
21
|
+
try {
|
|
22
|
+
const parsed = schema.safeParse(JSON.parse(readFileSync(this.file(name), 'utf8')));
|
|
23
|
+
return parsed.success ? parsed.data : undefined;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
write(name, record) {
|
|
30
|
+
const file = this.file(name);
|
|
31
|
+
mkdirSync(join(file, '..'), { recursive: true, mode: 0o700 });
|
|
32
|
+
const temp = plainPath(`${file}.${randomUUID()}.tmp`);
|
|
33
|
+
writeFileSync(temp, JSON.stringify(record), { mode: 0o600 });
|
|
34
|
+
renameSync(temp, file);
|
|
35
|
+
}
|
|
36
|
+
review(step) {
|
|
37
|
+
return this.read(`review-${step}`, freshReviewSchema);
|
|
38
|
+
}
|
|
39
|
+
saveReview(step, review) {
|
|
40
|
+
this.write(`review-${step}`, freshReviewSchema.parse(review));
|
|
41
|
+
}
|
|
42
|
+
critique() {
|
|
43
|
+
return this.read('critique', freshCritiqueSchema);
|
|
44
|
+
}
|
|
45
|
+
saveCritique(critique) {
|
|
46
|
+
this.write('critique', freshCritiqueSchema.parse(critique));
|
|
47
|
+
}
|
|
48
|
+
/** A studio that died left turns marked running: none runs now, so they failed. */
|
|
49
|
+
recover() {
|
|
50
|
+
for (const step of REVIEW_STEPS) {
|
|
51
|
+
const review = this.review(step);
|
|
52
|
+
if (review?.state === 'running')
|
|
53
|
+
this.saveReview(step, { ...review, state: 'failed' });
|
|
54
|
+
}
|
|
55
|
+
const critique = this.critique();
|
|
56
|
+
if (critique?.state === 'running')
|
|
57
|
+
this.saveCritique({ ...critique, state: 'failed' });
|
|
58
|
+
}
|
|
59
|
+
/** Every report already critiqued, as `<checkout>|<report>`: none runs twice. */
|
|
60
|
+
critiqued(key) {
|
|
61
|
+
return this.read('critiqued', critiquedSchema)?.includes(key) ?? false;
|
|
62
|
+
}
|
|
63
|
+
markCritiqued(key) {
|
|
64
|
+
const known = this.read('critiqued', critiquedSchema) ?? [];
|
|
65
|
+
this.write('critiqued', [...known.filter((item) => item !== key), key].slice(-500));
|
|
66
|
+
}
|
|
67
|
+
}
|