@richjava/req0 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 +117 -0
- package/cockpit/android-chrome-192x192.png +0 -0
- package/cockpit/android-chrome-512x512.png +0 -0
- package/cockpit/app.js +1710 -0
- package/cockpit/apple-touch-icon.png +0 -0
- package/cockpit/favicon-16x16.png +0 -0
- package/cockpit/favicon-32x32.png +0 -0
- package/cockpit/favicon.ico +0 -0
- package/cockpit/index.html +180 -0
- package/cockpit/markdown-editor.js +93 -0
- package/cockpit/req0-logo-horiz.png +0 -0
- package/cockpit/req0-logo-stacked.png +0 -0
- package/cockpit/site.webmanifest +19 -0
- package/cockpit/styles.css +1710 -0
- package/cockpit/tokens.css +118 -0
- package/dist/adapter.d.ts +5 -0
- package/dist/adapter.js +16 -0
- package/dist/browser.d.ts +33 -0
- package/dist/browser.js +524 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +265 -0
- package/dist/compile.d.ts +5 -0
- package/dist/compile.js +456 -0
- package/dist/diff.d.ts +15 -0
- package/dist/diff.js +114 -0
- package/dist/fix-from-proof.d.ts +13 -0
- package/dist/fix-from-proof.js +61 -0
- package/dist/gates.d.ts +7 -0
- package/dist/gates.js +7 -0
- package/dist/implement-brief.d.ts +9 -0
- package/dist/implement-brief.js +142 -0
- package/dist/implement.d.ts +70 -0
- package/dist/implement.js +615 -0
- package/dist/improve-log.d.ts +11 -0
- package/dist/improve-log.js +38 -0
- package/dist/improve-review.d.ts +45 -0
- package/dist/improve-review.js +182 -0
- package/dist/improve.d.ts +16 -0
- package/dist/improve.js +127 -0
- package/dist/jev-pack.d.ts +2 -0
- package/dist/jev-pack.js +114 -0
- package/dist/jev.d.ts +23 -0
- package/dist/jev.js +142 -0
- package/dist/pack.d.ts +101 -0
- package/dist/pack.js +609 -0
- package/dist/personas.d.ts +13 -0
- package/dist/personas.js +88 -0
- package/dist/proof-report.d.ts +8 -0
- package/dist/proof-report.js +82 -0
- package/dist/proof.d.ts +45 -0
- package/dist/proof.js +394 -0
- package/dist/qa-plan.d.ts +6 -0
- package/dist/qa-plan.js +58 -0
- package/dist/ready.d.ts +29 -0
- package/dist/ready.js +317 -0
- package/dist/runtime.d.ts +25 -0
- package/dist/runtime.js +125 -0
- package/dist/server.d.ts +15 -0
- package/dist/server.js +588 -0
- package/dist/stack.d.ts +35 -0
- package/dist/stack.js +220 -0
- package/dist/stages.d.ts +15 -0
- package/dist/stages.js +345 -0
- package/dist/template.d.ts +5 -0
- package/dist/template.js +55 -0
- package/dist/types.d.ts +282 -0
- package/dist/types.js +12 -0
- package/docs/requirement-template.md +341 -0
- package/package.json +81 -0
package/dist/server.js
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
import { readFile, stat } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { hasJevAccess, JevRequestError, JevUnavailableError } from "./jev.js";
|
|
6
|
+
import { implementPack, finishImplement, ignoreBuild, launchImprove, stopImplement, StopImplementLockedError, writeImproveBrief, checkPack, compilePack, createPack, deletePack, findRequirementsDir, listPacks, provePack, summarizePacks, writeRequirement } from "./pack.js";
|
|
7
|
+
import { IgnoreBuildLockedError } from "./ready.js";
|
|
8
|
+
import { ImproveLockedError } from "./improve.js";
|
|
9
|
+
import { acceptImproveReview, applyImproveReviewGate, evaluateImproveReview, ImproveLintError, ImproveReviewPendingError, rejectImproveReview, } from "./improve-review.js";
|
|
10
|
+
import { inspectProductRepo, needsStackChoice, recordProjectSetup, recordStack, UnknownStackError } from "./stack.js";
|
|
11
|
+
import { adapterDisplayName, parseAdapterId } from "./adapter.js";
|
|
12
|
+
import { pipelineView, sectionCatalog } from "./stages.js";
|
|
13
|
+
const cockpitDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../cockpit");
|
|
14
|
+
export function createAppState(cwd, pack) {
|
|
15
|
+
return { cwd, pack, last: null, activity: [], busy: null, implementLaunch: null };
|
|
16
|
+
}
|
|
17
|
+
export async function refresh(state) {
|
|
18
|
+
if (!state.pack) {
|
|
19
|
+
state.last = null;
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
state.last = await compilePack(state.pack);
|
|
23
|
+
return state.last;
|
|
24
|
+
}
|
|
25
|
+
export function createCockpitServer(state) {
|
|
26
|
+
return createServer((req, res) => {
|
|
27
|
+
void handle(state, req, res);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async function handle(state, req, res) {
|
|
31
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
32
|
+
try {
|
|
33
|
+
if (url.pathname === "/api/events") {
|
|
34
|
+
// Retired. A hanging EventSource pins Chrome/Electron refresh forever.
|
|
35
|
+
// Finish immediately so leftover clients cannot stall a reload.
|
|
36
|
+
const body = "Gone. The cockpit polls GET /api/state.\n";
|
|
37
|
+
res.writeHead(410, {
|
|
38
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
39
|
+
"Cache-Control": "no-store",
|
|
40
|
+
Connection: "close",
|
|
41
|
+
"Content-Length": Buffer.byteLength(body),
|
|
42
|
+
});
|
|
43
|
+
res.end(body);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (req.method === "GET" && url.pathname === "/api/state") {
|
|
47
|
+
await sendJson(res, await snapshot(state));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (req.method === "POST" && url.pathname === "/api/select") {
|
|
51
|
+
const body = await readJson(req);
|
|
52
|
+
const id = String(body["id"] ?? "");
|
|
53
|
+
const requirementsDir = await findRequirementsDir(state.cwd);
|
|
54
|
+
if (!requirementsDir) {
|
|
55
|
+
sendJson(res, { error: "No docs/requirements directory found." }, 400);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const packs = await listPacks(requirementsDir);
|
|
59
|
+
const chosen = packs.find((p) => p.id === id);
|
|
60
|
+
if (!chosen) {
|
|
61
|
+
sendJson(res, { error: `Unknown pack "${id}".` }, 404);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
state.pack = chosen;
|
|
65
|
+
state.activity = [];
|
|
66
|
+
await refresh(state);
|
|
67
|
+
await sendJson(res, await snapshot(state));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (req.method === "POST" && url.pathname === "/api/home") {
|
|
71
|
+
state.pack = null;
|
|
72
|
+
state.last = null;
|
|
73
|
+
state.activity = [];
|
|
74
|
+
await sendJson(res, await snapshot(state));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (req.method === "POST" && url.pathname === "/api/delete") {
|
|
78
|
+
const body = await readJson(req);
|
|
79
|
+
const id = String(body["id"] ?? "").trim();
|
|
80
|
+
const requirementsDir = await findRequirementsDir(state.cwd);
|
|
81
|
+
if (!requirementsDir) {
|
|
82
|
+
sendJson(res, { error: "No docs/requirements directory found." }, 400);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const packs = await listPacks(requirementsDir);
|
|
86
|
+
const chosen = packs.find((p) => p.id === id);
|
|
87
|
+
if (!chosen) {
|
|
88
|
+
sendJson(res, { error: `Unknown pack "${id}".` }, 404);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (state.busy && state.pack?.root === chosen.root) {
|
|
92
|
+
sendJson(res, { error: "Stop the current run before deleting this requirement." }, 409);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (state.pack?.root === chosen.root) {
|
|
96
|
+
state.pack = null;
|
|
97
|
+
state.last = null;
|
|
98
|
+
state.activity = [];
|
|
99
|
+
}
|
|
100
|
+
await deletePack(chosen);
|
|
101
|
+
await sendJson(res, await snapshot(state));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (req.method === "POST" && url.pathname === "/api/create") {
|
|
105
|
+
const body = await readJson(req);
|
|
106
|
+
const id = String(body["id"] ?? "").trim();
|
|
107
|
+
const requirementsDir = (await findRequirementsDir(state.cwd)) ?? path.join(state.cwd, "docs", "requirements");
|
|
108
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(id)) {
|
|
109
|
+
sendJson(res, { error: "Id must be kebab-case [a-z0-9-]+." }, 400);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
state.pack = await createPack(path.join(requirementsDir, id));
|
|
113
|
+
await refresh(state);
|
|
114
|
+
await sendJson(res, await snapshot(state));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (req.method === "PUT" && url.pathname === "/api/requirement") {
|
|
118
|
+
if (!state.pack) {
|
|
119
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const body = await readJson(req);
|
|
123
|
+
const markdown = String(body["markdown"] ?? "");
|
|
124
|
+
try {
|
|
125
|
+
state.last = await writeRequirement(state.pack, markdown);
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
sendJson(res, {
|
|
129
|
+
...(await snapshot(state)),
|
|
130
|
+
error: err instanceof Error ? err.message : "Could not save.",
|
|
131
|
+
}, err instanceof ImproveReviewPendingError ? 409 : 500);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
await sendJson(res, await snapshot(state));
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (req.method === "POST" && url.pathname === "/api/check-jev") {
|
|
138
|
+
if (!state.pack) {
|
|
139
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
state.last = await checkPack(state.pack);
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
if (err instanceof JevUnavailableError) {
|
|
147
|
+
state.last = await compilePack(state.pack);
|
|
148
|
+
await sendJson(res, { ...(await snapshot(state)), error: err.message }, 409);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (err instanceof JevRequestError) {
|
|
152
|
+
sendJson(res, { ...(await snapshot(state)), error: err.message }, 502);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
throw err;
|
|
156
|
+
}
|
|
157
|
+
await sendJson(res, await snapshot(state));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (req.method === "POST" && url.pathname === "/api/improve") {
|
|
161
|
+
if (!state.pack) {
|
|
162
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (state.busy) {
|
|
166
|
+
await sendJson(res, {
|
|
167
|
+
...(await snapshot(state)),
|
|
168
|
+
started: true,
|
|
169
|
+
message: state.busy === "improve" ? "Improve is already running." : "Another action is already running.",
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const body = await readJson(req);
|
|
174
|
+
const adapter = parseAdapterId(body["adapter"]) ?? "cursor";
|
|
175
|
+
try {
|
|
176
|
+
const written = await writeImproveBrief(state.pack, state.last);
|
|
177
|
+
state.last = written.result;
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
sendJson(res, {
|
|
181
|
+
...(await snapshot(state)),
|
|
182
|
+
error: err instanceof Error ? err.message : "Improve failed.",
|
|
183
|
+
}, err instanceof ImproveLockedError || err instanceof ImproveReviewPendingError ? 409 : 500);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const pack = state.pack;
|
|
187
|
+
state.busy = "improve";
|
|
188
|
+
state.activity = [];
|
|
189
|
+
pushActivity(state, "Wrote derived/improve-brief.md.");
|
|
190
|
+
if (adapter === "manual") {
|
|
191
|
+
pushActivity(state, "Manual adapter: open the brief in any coding agent.", "ok");
|
|
192
|
+
state.busy = null;
|
|
193
|
+
await sendJson(res, {
|
|
194
|
+
...(await snapshot(state)),
|
|
195
|
+
started: false,
|
|
196
|
+
message: "Wrote derived/improve-brief.md. Open it in any coding agent.",
|
|
197
|
+
});
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
pushActivity(state, `Launching ${adapterDisplayName(adapter)} to patch requirement.md…`);
|
|
201
|
+
await yieldEventLoop();
|
|
202
|
+
await sendJson(res, {
|
|
203
|
+
...(await snapshot(state)),
|
|
204
|
+
started: true,
|
|
205
|
+
message: `Wrote the brief. Launching ${adapterDisplayName(adapter)}…`,
|
|
206
|
+
});
|
|
207
|
+
void runImprove(state, pack, adapter);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (req.method === "POST" && url.pathname === "/api/improve/accept") {
|
|
211
|
+
await decideImproveReview(state, res, "accept");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (req.method === "POST" && url.pathname === "/api/improve/reject") {
|
|
215
|
+
await decideImproveReview(state, res, "reject");
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (req.method === "POST" && url.pathname === "/api/stack") {
|
|
219
|
+
const repo = await inspectProductRepo(await inspectFrom(state));
|
|
220
|
+
if (!repo.root) {
|
|
221
|
+
sendJson(res, { error: "No product repo root, so the stack cannot be recorded." }, 400);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const body = await readJson(req);
|
|
225
|
+
try {
|
|
226
|
+
await recordStack(repo.root, String(body["id"] ?? ""));
|
|
227
|
+
await refresh(state);
|
|
228
|
+
await sendJson(res, await snapshot(state));
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
sendJson(res, { error: err instanceof Error ? err.message : "Could not record stack." }, err instanceof UnknownStackError ? 400 : 409);
|
|
232
|
+
}
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (req.method === "POST" && url.pathname === "/api/setup") {
|
|
236
|
+
const repo = await inspectProductRepo(await inspectFrom(state));
|
|
237
|
+
if (!repo.root) {
|
|
238
|
+
sendJson(res, { error: "No product repo root, so setup cannot be recorded." }, 400);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const body = await readJson(req);
|
|
242
|
+
const coding = body["coding"];
|
|
243
|
+
if (coding !== "manual" && coding !== "cursor" && coding !== "copilot") {
|
|
244
|
+
sendJson(res, { error: "Choose I'll write the code, Cursor, or Copilot." }, 400);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const stackId = typeof body["stack"] === "string" && body["stack"].trim() ? String(body["stack"]) : undefined;
|
|
248
|
+
if (coding !== "manual" && !stackId && needsStackChoice({ ...repo, implement: true })) {
|
|
249
|
+
sendJson(res, { error: "Choose a stack for Cursor or Copilot on an empty repo." }, 400);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
await recordProjectSetup(repo.root, stackId ? { coding, stack: stackId } : { coding });
|
|
254
|
+
await refresh(state);
|
|
255
|
+
await sendJson(res, await snapshot(state));
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
sendJson(res, { error: err instanceof Error ? err.message : "Could not record setup." }, err instanceof UnknownStackError ? 400 : 409);
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (req.method === "POST" && url.pathname === "/api/implement") {
|
|
263
|
+
if (!state.pack) {
|
|
264
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (state.busy === "implement") {
|
|
268
|
+
await sendJson(res, { ...(await snapshot(state)), started: true, message: "Implement is already running." });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const body = await readJson(req);
|
|
272
|
+
const adapter = parseAdapterId(body["adapter"]) ?? "cursor";
|
|
273
|
+
const stack = typeof body["stack"] === "string" && body["stack"].trim() ? String(body["stack"]) : undefined;
|
|
274
|
+
const fromProof = body["fromProof"] === true;
|
|
275
|
+
try {
|
|
276
|
+
state.activity = [];
|
|
277
|
+
pushActivity(state, fromProof ? "Fix from proof started." : "Implement started.");
|
|
278
|
+
const launched = await implementPack(state.pack, {
|
|
279
|
+
...(stack ? { adapter, stack } : { adapter }),
|
|
280
|
+
...(fromProof ? { fromProof: true } : {}),
|
|
281
|
+
onLogLine: (line) => pushActivity(state, line),
|
|
282
|
+
});
|
|
283
|
+
state.last = launched.result;
|
|
284
|
+
if (launched.launch?.finished) {
|
|
285
|
+
state.busy = "implement";
|
|
286
|
+
state.implementLaunch = launched.launch;
|
|
287
|
+
pushActivity(state, launched.message);
|
|
288
|
+
await yieldEventLoop();
|
|
289
|
+
await sendJson(res, {
|
|
290
|
+
...(await snapshot(state)),
|
|
291
|
+
started: true,
|
|
292
|
+
message: launched.message,
|
|
293
|
+
});
|
|
294
|
+
void runImplement(state, state.pack, launched.launch, adapter);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
pushActivity(state, launched.message, launched.result.health.build.state === "failed" ? "error" : "ok");
|
|
298
|
+
await sendJson(res, { ...(await snapshot(state)), message: launched.message });
|
|
299
|
+
}
|
|
300
|
+
catch (err) {
|
|
301
|
+
sendJson(res, {
|
|
302
|
+
...(await snapshot(state)),
|
|
303
|
+
error: err instanceof Error ? err.message : "Implement failed.",
|
|
304
|
+
}, 409);
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (req.method === "POST" && url.pathname === "/api/stop-implement") {
|
|
309
|
+
if (!state.pack) {
|
|
310
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (!state.implementLaunch || state.busy !== "implement") {
|
|
314
|
+
sendJson(res, { ...(await snapshot(state)), error: "Implement is not running." }, 409);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
const stopped = await stopImplement(state.pack, state.implementLaunch);
|
|
319
|
+
state.last = stopped.result;
|
|
320
|
+
pushActivity(state, stopped.message, "ok");
|
|
321
|
+
await sendJson(res, { ...(await snapshot(state)), message: stopped.message });
|
|
322
|
+
}
|
|
323
|
+
catch (err) {
|
|
324
|
+
sendJson(res, {
|
|
325
|
+
...(await snapshot(state)),
|
|
326
|
+
error: err instanceof Error ? err.message : "Stop failed.",
|
|
327
|
+
}, err instanceof StopImplementLockedError ? 409 : 500);
|
|
328
|
+
}
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (req.method === "POST" && url.pathname === "/api/ignore-build") {
|
|
332
|
+
if (!state.pack) {
|
|
333
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const body = await readJson(req);
|
|
337
|
+
if (typeof body["successful"] !== "boolean") {
|
|
338
|
+
sendJson(res, { error: "successful must be true or false." }, 400);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
const ignored = await ignoreBuild(state.pack, body["successful"]);
|
|
343
|
+
state.last = ignored.result;
|
|
344
|
+
pushActivity(state, ignored.message, "ok");
|
|
345
|
+
await sendJson(res, { ...(await snapshot(state)), message: ignored.message });
|
|
346
|
+
}
|
|
347
|
+
catch (err) {
|
|
348
|
+
sendJson(res, {
|
|
349
|
+
...(await snapshot(state)),
|
|
350
|
+
error: err instanceof Error ? err.message : "Ignore failed.",
|
|
351
|
+
}, err instanceof IgnoreBuildLockedError ? 409 : 500);
|
|
352
|
+
}
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (req.method === "POST" && url.pathname === "/api/prove") {
|
|
356
|
+
if (!state.pack) {
|
|
357
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (state.busy === "prove") {
|
|
361
|
+
await sendJson(res, { ...(await snapshot(state)), started: true, message: "Prove is already running." });
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const pack = state.pack;
|
|
365
|
+
state.busy = "prove";
|
|
366
|
+
state.activity = [];
|
|
367
|
+
pushActivity(state, "Prove started.");
|
|
368
|
+
await yieldEventLoop();
|
|
369
|
+
await sendJson(res, { ...(await snapshot(state)), started: true, message: "Prove started." });
|
|
370
|
+
void runProve(state, pack);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (req.method === "POST" && url.pathname === "/api/create-in-place") {
|
|
374
|
+
if (!state.pack) {
|
|
375
|
+
sendJson(res, { error: "No pack folder selected." }, 400);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
state.pack = await createPack(state.pack.root);
|
|
379
|
+
await refresh(state);
|
|
380
|
+
await sendJson(res, await snapshot(state));
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (req.method === "GET" && (url.pathname === "/" || url.pathname.startsWith("/"))) {
|
|
384
|
+
const served = await serveCockpit(url.pathname, res);
|
|
385
|
+
if (!served) {
|
|
386
|
+
res.writeHead(404);
|
|
387
|
+
res.end("Not found");
|
|
388
|
+
}
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
res.writeHead(404);
|
|
392
|
+
res.end("Not found");
|
|
393
|
+
}
|
|
394
|
+
catch (err) {
|
|
395
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
396
|
+
res.end(JSON.stringify({ error: err instanceof Error ? err.message : "Server error" }));
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
async function snapshot(state) {
|
|
400
|
+
const requirementsDir = await findRequirementsDir(state.cwd);
|
|
401
|
+
const packs = requirementsDir ? await listPacks(requirementsDir) : [];
|
|
402
|
+
if (state.pack && !state.last) {
|
|
403
|
+
state.last = await compilePack(state.pack);
|
|
404
|
+
}
|
|
405
|
+
const improveReview = state.pack ? await evaluateImproveReview(state.pack) : null;
|
|
406
|
+
if (improveReview?.files.length && !state.activity.some((line) => line.message === "Improve patch ready to review.")) {
|
|
407
|
+
pushActivity(state, "Improve patch ready to review.", "ok");
|
|
408
|
+
}
|
|
409
|
+
const health = state.last ? applyImproveReviewGate(state.last.health, improveReview) : null;
|
|
410
|
+
let proofReport = "";
|
|
411
|
+
if (state.pack) {
|
|
412
|
+
try {
|
|
413
|
+
proofReport = await readFile(state.pack.proofReport, "utf8");
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
proofReport = "";
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return {
|
|
420
|
+
cwd: state.cwd,
|
|
421
|
+
packId: state.pack?.id ?? null,
|
|
422
|
+
packRoot: state.pack?.root ?? null,
|
|
423
|
+
packs: packs.map((p) => p.id),
|
|
424
|
+
packList: await summarizePacks(packs),
|
|
425
|
+
markdown: state.last?.markdown ?? "",
|
|
426
|
+
health,
|
|
427
|
+
spec: state.last?.spec ?? null,
|
|
428
|
+
pipeline: pipelineView(health, Boolean(state.pack)),
|
|
429
|
+
sections: sectionCatalog(),
|
|
430
|
+
proofReport,
|
|
431
|
+
hasApiKey: hasJevAccess(),
|
|
432
|
+
productRepo: await inspectProductRepo(await inspectFrom(state)),
|
|
433
|
+
activity: state.activity,
|
|
434
|
+
busy: state.busy,
|
|
435
|
+
improveReview,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
async function inspectFrom(state) {
|
|
439
|
+
if (state.pack)
|
|
440
|
+
return state.pack.root;
|
|
441
|
+
const requirementsDir = await findRequirementsDir(state.cwd);
|
|
442
|
+
return requirementsDir ?? path.join(state.cwd, "docs", "requirements");
|
|
443
|
+
}
|
|
444
|
+
async function decideImproveReview(state, res, decision) {
|
|
445
|
+
if (!state.pack) {
|
|
446
|
+
sendJson(res, { error: "No pack selected." }, 400);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
try {
|
|
450
|
+
if (decision === "accept")
|
|
451
|
+
await acceptImproveReview(state.pack);
|
|
452
|
+
else
|
|
453
|
+
await rejectImproveReview(state.pack);
|
|
454
|
+
}
|
|
455
|
+
catch (err) {
|
|
456
|
+
sendJson(res, {
|
|
457
|
+
...(await snapshot(state)),
|
|
458
|
+
error: err instanceof Error ? err.message : "Improve review failed.",
|
|
459
|
+
}, err instanceof ImproveReviewPendingError || err instanceof ImproveLintError ? 409 : 500);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
state.last = await compilePack(state.pack);
|
|
463
|
+
pushActivity(state, decision === "accept" ? "Accepted the Improve patch." : "Rejected the Improve patch.", "ok");
|
|
464
|
+
await sendJson(res, {
|
|
465
|
+
...(await snapshot(state)),
|
|
466
|
+
message: decision === "accept" ? "Kept the patch. Check when you are ready." : "Restored the spec from before Improve.",
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
async function runImprove(state, pack, adapter) {
|
|
470
|
+
try {
|
|
471
|
+
const launched = await launchImprove(pack, { adapter });
|
|
472
|
+
pushActivity(state, launched.message, launched.ok ? "ok" : "error");
|
|
473
|
+
}
|
|
474
|
+
catch (err) {
|
|
475
|
+
pushActivity(state, err instanceof Error ? err.message : "Improve failed.", "error");
|
|
476
|
+
}
|
|
477
|
+
finally {
|
|
478
|
+
state.busy = null;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
async function runImplement(state, pack, launch, adapter) {
|
|
482
|
+
try {
|
|
483
|
+
const done = await finishImplement(pack, launch, adapter);
|
|
484
|
+
if (state.pack?.root === pack.root)
|
|
485
|
+
state.last = done.result;
|
|
486
|
+
pushActivity(state, done.message, done.result.health.build.state === "failed" ? "error" : "ok");
|
|
487
|
+
}
|
|
488
|
+
catch (err) {
|
|
489
|
+
pushActivity(state, err instanceof Error ? err.message : "Implement failed.", "error");
|
|
490
|
+
}
|
|
491
|
+
finally {
|
|
492
|
+
if (state.busy === "implement")
|
|
493
|
+
state.busy = null;
|
|
494
|
+
if (state.implementLaunch === launch)
|
|
495
|
+
state.implementLaunch = null;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
async function runProve(state, pack) {
|
|
499
|
+
try {
|
|
500
|
+
const proved = await provePack(pack, {
|
|
501
|
+
onProgress: (message, level) => pushActivity(state, message, level),
|
|
502
|
+
});
|
|
503
|
+
state.last = proved.result;
|
|
504
|
+
}
|
|
505
|
+
catch (err) {
|
|
506
|
+
const message = err instanceof Error ? err.message : "Prove failed.";
|
|
507
|
+
pushActivity(state, message, "error");
|
|
508
|
+
if (err instanceof JevUnavailableError && state.pack) {
|
|
509
|
+
state.last = await compilePack(state.pack);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
finally {
|
|
513
|
+
state.busy = null;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function pushActivity(state, message, level = "info") {
|
|
517
|
+
const line = { at: new Date().toISOString(), level, message };
|
|
518
|
+
state.activity = [...state.activity, line].slice(-80);
|
|
519
|
+
}
|
|
520
|
+
async function yieldEventLoop() {
|
|
521
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
522
|
+
}
|
|
523
|
+
async function serveCockpit(pathname, res) {
|
|
524
|
+
const relative = pathname === "/" ? "index.html" : pathname.replace(/^\//, "");
|
|
525
|
+
if (relative.includes(".."))
|
|
526
|
+
return false;
|
|
527
|
+
const file = path.join(cockpitDir, relative);
|
|
528
|
+
if (!file.startsWith(cockpitDir))
|
|
529
|
+
return false;
|
|
530
|
+
try {
|
|
531
|
+
if (relative === "index.html") {
|
|
532
|
+
const html = await cacheBustHtml(await readFile(file, "utf8"));
|
|
533
|
+
res.writeHead(200, {
|
|
534
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
535
|
+
"Cache-Control": "no-store",
|
|
536
|
+
});
|
|
537
|
+
res.end(html);
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
const data = await readFile(file);
|
|
541
|
+
res.writeHead(200, {
|
|
542
|
+
"Content-Type": contentType(file),
|
|
543
|
+
"Cache-Control": "no-store",
|
|
544
|
+
});
|
|
545
|
+
res.end(data);
|
|
546
|
+
return true;
|
|
547
|
+
}
|
|
548
|
+
catch {
|
|
549
|
+
return false;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
async function cacheBustHtml(html) {
|
|
553
|
+
let out = html;
|
|
554
|
+
for (const asset of ["tokens.css", "styles.css", "app.js", "markdown-editor.js"]) {
|
|
555
|
+
const { mtimeMs } = await stat(path.join(cockpitDir, asset));
|
|
556
|
+
out = out.replaceAll(`/${asset}"`, `/${asset}?v=${Math.floor(mtimeMs)}"`);
|
|
557
|
+
}
|
|
558
|
+
return out;
|
|
559
|
+
}
|
|
560
|
+
function contentType(file) {
|
|
561
|
+
if (file.endsWith(".css"))
|
|
562
|
+
return "text/css; charset=utf-8";
|
|
563
|
+
if (file.endsWith(".js"))
|
|
564
|
+
return "text/javascript; charset=utf-8";
|
|
565
|
+
if (file.endsWith(".svg"))
|
|
566
|
+
return "image/svg+xml";
|
|
567
|
+
if (file.endsWith(".png"))
|
|
568
|
+
return "image/png";
|
|
569
|
+
if (file.endsWith(".ico"))
|
|
570
|
+
return "image/x-icon";
|
|
571
|
+
if (file.endsWith(".webmanifest"))
|
|
572
|
+
return "application/manifest+json";
|
|
573
|
+
return "text/html; charset=utf-8";
|
|
574
|
+
}
|
|
575
|
+
async function readJson(req) {
|
|
576
|
+
const chunks = [];
|
|
577
|
+
for await (const chunk of req) {
|
|
578
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
579
|
+
}
|
|
580
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
581
|
+
if (!raw.trim())
|
|
582
|
+
return {};
|
|
583
|
+
return JSON.parse(raw);
|
|
584
|
+
}
|
|
585
|
+
function sendJson(res, body, status = 200) {
|
|
586
|
+
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
587
|
+
res.end(`${JSON.stringify(body)}\n`);
|
|
588
|
+
}
|
package/dist/stack.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AdapterId, ProductRepo, Req0Config, StackChoice } from "./types.js";
|
|
2
|
+
export type CodingChoice = "manual" | "cursor" | "copilot";
|
|
3
|
+
export declare const REQ0_CONFIG_FILE = "req0.json";
|
|
4
|
+
export declare const ENV_EXAMPLE_FILE = ".env.example";
|
|
5
|
+
export declare const AMPLIFY_ENV_EXAMPLE = "# Copy to .env. Names only. Do not put real values in this file.\n# ampx does not read this file. Export the vars, or run: npx ampx configure profile\nAWS_REGION=\n# Named profile (then: npx ampx sandbox --once --profile <name>):\nAWS_PROFILE=\n# Or access keys instead of AWS_PROFILE:\n# AWS_ACCESS_KEY_ID=\n# AWS_SECRET_ACCESS_KEY=\n# AWS_SESSION_TOKEN= # temporary/SSO creds only \u2014 not IAM user keys\n";
|
|
6
|
+
export declare const DEFAULT_STACK: StackChoice;
|
|
7
|
+
export declare const AMPLIFY_IDENTITY_HINT = "ampx reads AWS_PROFILE, inherited AWS_ACCESS_KEY_ID, or ~/.aws \u2014 not the .env file. If none of those exist, stop and tell the owner to run npx ampx configure profile. Do not ask for keys. Do not print env values.";
|
|
8
|
+
export declare const AMPLIFY_GEN2_STACK: StackChoice;
|
|
9
|
+
export declare const KNOWN_STACKS: StackChoice[];
|
|
10
|
+
export declare const STACK_CHOICE_HINT: string;
|
|
11
|
+
export declare function isAmplifyStack(stack: StackChoice): boolean;
|
|
12
|
+
export declare function resolveStackId(id: string): StackChoice | null;
|
|
13
|
+
export declare function needsStackChoice(repo: ProductRepo): boolean;
|
|
14
|
+
export declare function needsCodingChoice(repo: ProductRepo): boolean;
|
|
15
|
+
export declare function needsSetupGate(repo: ProductRepo): boolean;
|
|
16
|
+
export declare class StackRequiredError extends Error {
|
|
17
|
+
readonly code = "stack_required";
|
|
18
|
+
constructor(message?: string);
|
|
19
|
+
}
|
|
20
|
+
export declare class UnknownStackError extends Error {
|
|
21
|
+
readonly code = "unknown_stack";
|
|
22
|
+
constructor(id: string);
|
|
23
|
+
}
|
|
24
|
+
export declare function findRepoRootFromPack(packRoot: string): string | null;
|
|
25
|
+
export declare function readReq0Config(repoRoot: string): Promise<Req0Config | null>;
|
|
26
|
+
export declare function writeReq0Config(repoRoot: string, config: Req0Config): Promise<void>;
|
|
27
|
+
export declare function inspectProductRepo(packRoot: string): Promise<ProductRepo>;
|
|
28
|
+
export declare function recordStack(repoRoot: string, id: string): Promise<StackChoice>;
|
|
29
|
+
export declare function recordProjectSetup(repoRoot: string, input: {
|
|
30
|
+
coding: CodingChoice;
|
|
31
|
+
stack?: string;
|
|
32
|
+
}): Promise<Req0Config>;
|
|
33
|
+
export declare function writeAmplifyEnvExampleIfMissing(repoRoot: string, stack: StackChoice): Promise<boolean>;
|
|
34
|
+
export declare function hasCustomerApp(repoRoot: string): Promise<boolean>;
|
|
35
|
+
export declare function stackInstruction(repo: ProductRepo, adapter: AdapterId): string;
|