@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.
Files changed (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +117 -0
  3. package/cockpit/android-chrome-192x192.png +0 -0
  4. package/cockpit/android-chrome-512x512.png +0 -0
  5. package/cockpit/app.js +1710 -0
  6. package/cockpit/apple-touch-icon.png +0 -0
  7. package/cockpit/favicon-16x16.png +0 -0
  8. package/cockpit/favicon-32x32.png +0 -0
  9. package/cockpit/favicon.ico +0 -0
  10. package/cockpit/index.html +180 -0
  11. package/cockpit/markdown-editor.js +93 -0
  12. package/cockpit/req0-logo-horiz.png +0 -0
  13. package/cockpit/req0-logo-stacked.png +0 -0
  14. package/cockpit/site.webmanifest +19 -0
  15. package/cockpit/styles.css +1710 -0
  16. package/cockpit/tokens.css +118 -0
  17. package/dist/adapter.d.ts +5 -0
  18. package/dist/adapter.js +16 -0
  19. package/dist/browser.d.ts +33 -0
  20. package/dist/browser.js +524 -0
  21. package/dist/cli.d.ts +2 -0
  22. package/dist/cli.js +265 -0
  23. package/dist/compile.d.ts +5 -0
  24. package/dist/compile.js +456 -0
  25. package/dist/diff.d.ts +15 -0
  26. package/dist/diff.js +114 -0
  27. package/dist/fix-from-proof.d.ts +13 -0
  28. package/dist/fix-from-proof.js +61 -0
  29. package/dist/gates.d.ts +7 -0
  30. package/dist/gates.js +7 -0
  31. package/dist/implement-brief.d.ts +9 -0
  32. package/dist/implement-brief.js +142 -0
  33. package/dist/implement.d.ts +70 -0
  34. package/dist/implement.js +615 -0
  35. package/dist/improve-log.d.ts +11 -0
  36. package/dist/improve-log.js +38 -0
  37. package/dist/improve-review.d.ts +45 -0
  38. package/dist/improve-review.js +182 -0
  39. package/dist/improve.d.ts +16 -0
  40. package/dist/improve.js +127 -0
  41. package/dist/jev-pack.d.ts +2 -0
  42. package/dist/jev-pack.js +114 -0
  43. package/dist/jev.d.ts +23 -0
  44. package/dist/jev.js +142 -0
  45. package/dist/pack.d.ts +101 -0
  46. package/dist/pack.js +609 -0
  47. package/dist/personas.d.ts +13 -0
  48. package/dist/personas.js +88 -0
  49. package/dist/proof-report.d.ts +8 -0
  50. package/dist/proof-report.js +82 -0
  51. package/dist/proof.d.ts +45 -0
  52. package/dist/proof.js +394 -0
  53. package/dist/qa-plan.d.ts +6 -0
  54. package/dist/qa-plan.js +58 -0
  55. package/dist/ready.d.ts +29 -0
  56. package/dist/ready.js +317 -0
  57. package/dist/runtime.d.ts +25 -0
  58. package/dist/runtime.js +125 -0
  59. package/dist/server.d.ts +15 -0
  60. package/dist/server.js +588 -0
  61. package/dist/stack.d.ts +35 -0
  62. package/dist/stack.js +220 -0
  63. package/dist/stages.d.ts +15 -0
  64. package/dist/stages.js +345 -0
  65. package/dist/template.d.ts +5 -0
  66. package/dist/template.js +55 -0
  67. package/dist/types.d.ts +282 -0
  68. package/dist/types.js +12 -0
  69. package/docs/requirement-template.md +341 -0
  70. package/package.json +81 -0
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,265 @@
1
+ #!/usr/bin/env node
2
+ import { watch } from "node:fs";
3
+ import { createServer } from "node:net";
4
+ import path from "node:path";
5
+ import { spawn } from "node:child_process";
6
+ import { loadEnvFile } from "node:process";
7
+ import { fileURLToPath } from "node:url";
8
+ import { parseAdapterId } from "./adapter.js";
9
+ import { isRequirementId } from "./compile.js";
10
+ import { adapterInstructions, ImplementDisabledError, ImplementLockedError } from "./implement.js";
11
+ import { FixFromProofLockedError } from "./fix-from-proof.js";
12
+ import { StackRequiredError, UnknownStackError } from "./stack.js";
13
+ import { checkPack, compilePack, createPack, findRequirementsDir, finishImplement, implementPack, provePack, resolveCockpitPack, resolvePack } from "./pack.js";
14
+ import { ProveLockedError } from "./proof.js";
15
+ import { createAppState, createCockpitServer, refresh } from "./server.js";
16
+ loadProjectEnv();
17
+ const args = process.argv.slice(2);
18
+ const command = args[0] ?? "start";
19
+ try {
20
+ await main(command, args.slice(1));
21
+ }
22
+ catch (err) {
23
+ console.error(err instanceof Error ? err.message : err);
24
+ process.exit(1);
25
+ }
26
+ function loadProjectEnv() {
27
+ const starts = [process.cwd(), path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")];
28
+ const seen = new Set();
29
+ for (const start of starts) {
30
+ let dir = path.resolve(start);
31
+ for (let i = 0; i < 8; i++) {
32
+ if (seen.has(dir))
33
+ break;
34
+ seen.add(dir);
35
+ try {
36
+ loadEnvFile(path.join(dir, ".env"));
37
+ }
38
+ catch {
39
+ // missing .env is fine; keep walking
40
+ }
41
+ const parent = path.dirname(dir);
42
+ if (parent === dir)
43
+ break;
44
+ dir = parent;
45
+ }
46
+ }
47
+ }
48
+ async function main(command, rest) {
49
+ const cwd = process.cwd();
50
+ if (command === "compile") {
51
+ const pack = await resolvePack(cwd);
52
+ if (!pack) {
53
+ throw new Error("No requirement pack here. Open a docs/requirements/<id> folder, or create one.");
54
+ }
55
+ const result = await compilePack(pack);
56
+ const errors = result.health.spec.findings.filter((f) => f.severity === "error");
57
+ for (const f of result.health.spec.findings) {
58
+ console.error(`${pack.id}:${f.line} ${f.severity}: ${f.message}`);
59
+ }
60
+ console.log(`Spec ${result.health.spec.state} · Ready ${result.health.ready.state} — ${result.health.howThisIsGoing}`);
61
+ if (errors.length)
62
+ process.exit(2);
63
+ return;
64
+ }
65
+ if (command === "check") {
66
+ const pack = await resolvePack(cwd);
67
+ if (!pack) {
68
+ throw new Error("No requirement pack here. Open a docs/requirements/<id> folder, or create one.");
69
+ }
70
+ const result = await checkPack(pack);
71
+ console.log(`Ready ${result.health.ready.state} — ${result.health.howThisIsGoing}`);
72
+ for (const finding of result.health.ready.findings) {
73
+ console.error(`${finding.severity}: ${finding.message}`);
74
+ }
75
+ if (result.health.ready.reason === "no_api_key")
76
+ process.exit(3);
77
+ if (result.health.ready.state === "blocked")
78
+ process.exit(2);
79
+ return;
80
+ }
81
+ if (command === "implement") {
82
+ const pack = await resolvePack(cwd);
83
+ if (!pack) {
84
+ throw new Error("No requirement pack here. Open a docs/requirements/<id> folder, or create one.");
85
+ }
86
+ const adapter = adapterFromArgs(rest);
87
+ const stack = flagValue(rest, "stack");
88
+ const fromProof = rest.includes("--from-proof");
89
+ try {
90
+ const launched = await implementPack(pack, {
91
+ ...(adapter ? { adapter } : {}),
92
+ ...(stack ? { stack } : {}),
93
+ ...(fromProof ? { fromProof: true } : {}),
94
+ onLogLine: (line) => console.log(line),
95
+ });
96
+ let result = launched.result;
97
+ let message = launched.message;
98
+ const used = adapter ?? result.health.build.adapter ?? "cursor";
99
+ if (launched.launch?.finished) {
100
+ console.log(`Build running — ${message}`);
101
+ const done = await finishImplement(pack, launched.launch, used);
102
+ result = done.result;
103
+ message = done.message;
104
+ }
105
+ console.log(`Build ${result.health.build.state} — ${message}`);
106
+ console.log(adapterInstructions(used, fromProof ? pack.fixFromProofBrief : pack.implementBrief));
107
+ if (result.health.build.state === "failed")
108
+ process.exit(2);
109
+ }
110
+ catch (err) {
111
+ if (err instanceof ImplementLockedError ||
112
+ err instanceof ImplementDisabledError ||
113
+ err instanceof FixFromProofLockedError ||
114
+ err instanceof StackRequiredError ||
115
+ err instanceof UnknownStackError) {
116
+ console.error(err.message);
117
+ process.exit(2);
118
+ }
119
+ throw err;
120
+ }
121
+ return;
122
+ }
123
+ if (command === "prove") {
124
+ const pack = await resolvePack(cwd);
125
+ if (!pack) {
126
+ throw new Error("No requirement pack here. Open a docs/requirements/<id> folder, or create one.");
127
+ }
128
+ try {
129
+ const proved = await provePack(pack, {
130
+ onProgress: (message, level) => {
131
+ if (level === "error")
132
+ console.error(message);
133
+ else
134
+ console.log(message);
135
+ },
136
+ });
137
+ console.log(`${proved.message} — Proof ${proved.result.health.proof.state}`);
138
+ for (const finding of proved.result.health.proof.findings) {
139
+ console.error(`${finding.severity}: ${finding.message}`);
140
+ }
141
+ if (proved.result.health.proof.state === "failed")
142
+ process.exit(2);
143
+ }
144
+ catch (err) {
145
+ if (err instanceof ProveLockedError) {
146
+ console.error(err.message);
147
+ process.exit(2);
148
+ }
149
+ throw err;
150
+ }
151
+ return;
152
+ }
153
+ if (command === "create") {
154
+ const id = rest[0];
155
+ if (id) {
156
+ const requirementsDir = (await findRequirementsDir(cwd)) ?? path.join(cwd, "docs", "requirements");
157
+ const created = await createPack(path.join(requirementsDir, id));
158
+ console.log(`Created ${created.root}`);
159
+ return;
160
+ }
161
+ if (isRequirementId(path.basename(cwd))) {
162
+ const created = await createPack(cwd);
163
+ console.log(`Created ${created.root}`);
164
+ return;
165
+ }
166
+ throw new Error("Usage: req0 create <kebab-id> or run inside an empty pack folder.");
167
+ }
168
+ if (command === "start") {
169
+ await startCockpit(cwd);
170
+ return;
171
+ }
172
+ throw new Error(`Unknown command "${command}". Try: req0 start | req0 create | req0 compile | req0 check | req0 implement | req0 prove`);
173
+ }
174
+ async function startCockpit(cwd) {
175
+ const pack = await resolveCockpitPack(cwd);
176
+ const state = createAppState(cwd, pack);
177
+ await refresh(state);
178
+ const port = await pickPort(4370);
179
+ const server = createCockpitServer(state);
180
+ server.listen(port, "127.0.0.1", () => {
181
+ const url = `http://127.0.0.1:${port}`;
182
+ console.log(`Req0 cockpit: ${url}`);
183
+ if (state.pack)
184
+ console.log(`Pack: ${state.pack.root}`);
185
+ else
186
+ console.log("Requirement list. Open a pack or create one from the cockpit.");
187
+ openBrowser(url);
188
+ });
189
+ const requirementsDir = await findRequirementsDir(cwd);
190
+ const watchTargets = requirementsDir
191
+ ? [requirementsDir]
192
+ : state.pack
193
+ ? [state.pack.requirement, path.dirname(state.pack.personas)]
194
+ : [];
195
+ if (watchTargets.length === 0)
196
+ return;
197
+ let timer;
198
+ const schedule = () => {
199
+ if (!state.pack)
200
+ return;
201
+ clearTimeout(timer);
202
+ timer = setTimeout(() => {
203
+ void refresh(state).catch((err) => {
204
+ console.error(err instanceof Error ? err.message : err);
205
+ });
206
+ }, 80);
207
+ };
208
+ for (const watchPath of watchTargets) {
209
+ try {
210
+ const watcher = watch(watchPath, { recursive: Boolean(requirementsDir) }, () => schedule());
211
+ watcher.on("error", (err) => {
212
+ console.error(`File watch unavailable (${err.message}). Save from the cockpit still compiles.`);
213
+ });
214
+ }
215
+ catch (err) {
216
+ console.error(`File watch unavailable (${err instanceof Error ? err.message : err}). Save from the cockpit still compiles.`);
217
+ }
218
+ }
219
+ }
220
+ async function pickPort(start) {
221
+ for (let port = start; port < start + 20; port++) {
222
+ const free = await isFree(port);
223
+ if (free)
224
+ return port;
225
+ }
226
+ throw new Error("No free port near 4370.");
227
+ }
228
+ function flagValue(args, name) {
229
+ const prefix = `--${name}=`;
230
+ const hit = args.find((arg) => arg.startsWith(prefix));
231
+ if (!hit)
232
+ return undefined;
233
+ return hit.slice(prefix.length);
234
+ }
235
+ function adapterFromArgs(args) {
236
+ const flagged = flagValue(args, "adapter");
237
+ if (flagged !== undefined) {
238
+ const id = parseAdapterId(flagged);
239
+ if (!id)
240
+ throw new Error(`Unknown adapter "${flagged}". Use cursor, copilot, or manual.`);
241
+ return id;
242
+ }
243
+ if (args[0] === "manual" || args[0] === "cursor" || args[0] === "copilot")
244
+ return args[0];
245
+ return null;
246
+ }
247
+ function isFree(port) {
248
+ return new Promise((resolve) => {
249
+ const probe = createServer();
250
+ probe.once("error", () => resolve(false));
251
+ probe.listen(port, "127.0.0.1", () => {
252
+ probe.close(() => resolve(true));
253
+ });
254
+ });
255
+ }
256
+ function openBrowser(url) {
257
+ try {
258
+ const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "cmd" : "xdg-open";
259
+ const args = process.platform === "win32" ? ["/c", "start", url] : [url];
260
+ spawn(cmd, args, { detached: true, stdio: "ignore" }).unref();
261
+ }
262
+ catch {
263
+ console.log("Open that URL in your browser.");
264
+ }
265
+ }
@@ -0,0 +1,5 @@
1
+ import { type CompileResult, type Finding, type Health } from "./types.js";
2
+ export declare function isRequirementId(id: string): boolean;
3
+ export declare function compileMarkdown(requirementId: string, markdown: string): CompileResult;
4
+ export declare function buildHealth(requirementId: string, specState: Health["spec"]["state"], findings: Finding[]): Health;
5
+ export declare function healthToStatusMarkdown(health: Health): string;