@richjava/req0 0.1.0 → 0.3.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/dist/server.js CHANGED
@@ -3,16 +3,17 @@ import { readFile, stat } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
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";
6
+ import { implementPack, finishImplement, ignoreBuild, launchImprove, stopImplement, StopImplementLockedError, writeImproveBrief, checkPack, compilePack, createPack, deletePack, findRequirementsDir, listPacks, provePack, summarizePacks, writeRequirement, answerAgentQuestions, discardAgentQuestions } from "./pack.js";
7
7
  import { IgnoreBuildLockedError } from "./ready.js";
8
8
  import { ImproveLockedError } from "./improve.js";
9
9
  import { acceptImproveReview, applyImproveReviewGate, evaluateImproveReview, ImproveLintError, ImproveReviewPendingError, rejectImproveReview, } from "./improve-review.js";
10
10
  import { inspectProductRepo, needsStackChoice, recordProjectSetup, recordStack, UnknownStackError } from "./stack.js";
11
11
  import { adapterDisplayName, parseAdapterId } from "./adapter.js";
12
12
  import { pipelineView, sectionCatalog } from "./stages.js";
13
+ import { AGENT_QUESTIONS_WAITING, AgentAnswersError, applyAgentQuestionsGate, isOpenQuestions, readAgentQuestions, } from "./agent-questions.js";
13
14
  const cockpitDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../cockpit");
14
15
  export function createAppState(cwd, pack) {
15
- return { cwd, pack, last: null, activity: [], busy: null, implementLaunch: null };
16
+ return { cwd, pack, last: null, implementActivity: [], proveActivity: [], busy: null, implementLaunch: null };
16
17
  }
17
18
  export async function refresh(state) {
18
19
  if (!state.pack) {
@@ -62,7 +63,7 @@ async function handle(state, req, res) {
62
63
  return;
63
64
  }
64
65
  state.pack = chosen;
65
- state.activity = [];
66
+ clearActivity(state);
66
67
  await refresh(state);
67
68
  await sendJson(res, await snapshot(state));
68
69
  return;
@@ -70,7 +71,7 @@ async function handle(state, req, res) {
70
71
  if (req.method === "POST" && url.pathname === "/api/home") {
71
72
  state.pack = null;
72
73
  state.last = null;
73
- state.activity = [];
74
+ clearActivity(state);
74
75
  await sendJson(res, await snapshot(state));
75
76
  return;
76
77
  }
@@ -95,7 +96,7 @@ async function handle(state, req, res) {
95
96
  if (state.pack?.root === chosen.root) {
96
97
  state.pack = null;
97
98
  state.last = null;
98
- state.activity = [];
99
+ clearActivity(state);
99
100
  }
100
101
  await deletePack(chosen);
101
102
  await sendJson(res, await snapshot(state));
@@ -185,7 +186,7 @@ async function handle(state, req, res) {
185
186
  }
186
187
  const pack = state.pack;
187
188
  state.busy = "improve";
188
- state.activity = [];
189
+ clearActivity(state, "implement");
189
190
  pushActivity(state, "Wrote derived/improve-brief.md.");
190
191
  if (adapter === "manual") {
191
192
  pushActivity(state, "Manual adapter: open the brief in any coding agent.", "ok");
@@ -273,7 +274,7 @@ async function handle(state, req, res) {
273
274
  const stack = typeof body["stack"] === "string" && body["stack"].trim() ? String(body["stack"]) : undefined;
274
275
  const fromProof = body["fromProof"] === true;
275
276
  try {
276
- state.activity = [];
277
+ clearActivity(state, "implement");
277
278
  pushActivity(state, fromProof ? "Fix from proof started." : "Implement started.");
278
279
  const launched = await implementPack(state.pack, {
279
280
  ...(stack ? { adapter, stack } : { adapter }),
@@ -310,13 +311,18 @@ async function handle(state, req, res) {
310
311
  sendJson(res, { error: "No pack selected." }, 400);
311
312
  return;
312
313
  }
313
- if (!state.implementLaunch || state.busy !== "implement") {
314
+ const waiting = isOpenQuestions(await readAgentQuestions(state.pack));
315
+ if ((!state.implementLaunch || state.busy !== "implement") && !waiting) {
314
316
  sendJson(res, { ...(await snapshot(state)), error: "Implement is not running." }, 409);
315
317
  return;
316
318
  }
317
319
  try {
318
- const stopped = await stopImplement(state.pack, state.implementLaunch);
320
+ const stopped = waiting && (!state.implementLaunch || state.busy !== "implement")
321
+ ? await discardAgentQuestions(state.pack, state.implementLaunch)
322
+ : await stopImplement(state.pack, state.implementLaunch);
319
323
  state.last = stopped.result;
324
+ state.busy = null;
325
+ state.implementLaunch = null;
320
326
  pushActivity(state, stopped.message, "ok");
321
327
  await sendJson(res, { ...(await snapshot(state)), message: stopped.message });
322
328
  }
@@ -324,7 +330,7 @@ async function handle(state, req, res) {
324
330
  sendJson(res, {
325
331
  ...(await snapshot(state)),
326
332
  error: err instanceof Error ? err.message : "Stop failed.",
327
- }, err instanceof StopImplementLockedError ? 409 : 500);
333
+ }, err instanceof StopImplementLockedError || err instanceof AgentAnswersError ? 409 : 500);
328
334
  }
329
335
  return;
330
336
  }
@@ -352,6 +358,86 @@ async function handle(state, req, res) {
352
358
  }
353
359
  return;
354
360
  }
361
+ if (req.method === "POST" && url.pathname === "/api/agent-answers") {
362
+ if (!state.pack) {
363
+ sendJson(res, { error: "No pack selected." }, 400);
364
+ return;
365
+ }
366
+ if (state.busy) {
367
+ sendJson(res, { ...(await snapshot(state)), error: "Another action is already running." }, 409);
368
+ return;
369
+ }
370
+ const body = await readJson(req);
371
+ const raw = body["answers"];
372
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
373
+ sendJson(res, { error: "answers must be an object of question id to value." }, 400);
374
+ return;
375
+ }
376
+ const answers = {};
377
+ for (const [id, value] of Object.entries(raw)) {
378
+ answers[id] = String(value ?? "");
379
+ }
380
+ const adapter = parseAdapterId(body["adapter"]) ?? "cursor";
381
+ try {
382
+ const answered = await answerAgentQuestions(state.pack, answers, {
383
+ adapter,
384
+ onLogLine: (line) => pushActivity(state, line),
385
+ });
386
+ state.last = answered.result;
387
+ const summary = Object.entries(answers)
388
+ .filter(([, value]) => value.trim())
389
+ .map(([id, value]) => `${id}: ${value.trim()}`)
390
+ .join("; ");
391
+ pushActivity(state, summary ? `Owner answered. ${summary}` : "Owner answered. Continuing.", "ok");
392
+ if (answered.launch?.finished) {
393
+ state.busy = answered.channel === "improve" ? "improve" : "implement";
394
+ if (answered.channel === "implement")
395
+ state.implementLaunch = answered.launch;
396
+ await yieldEventLoop();
397
+ await sendJson(res, {
398
+ ...(await snapshot(state)),
399
+ started: true,
400
+ message: answered.message,
401
+ });
402
+ if (answered.channel === "implement") {
403
+ void runImplement(state, state.pack, answered.launch, adapter);
404
+ }
405
+ else {
406
+ void runImproveWait(state, answered.launch);
407
+ }
408
+ return;
409
+ }
410
+ await sendJson(res, { ...(await snapshot(state)), message: answered.message });
411
+ }
412
+ catch (err) {
413
+ sendJson(res, {
414
+ ...(await snapshot(state)),
415
+ error: err instanceof Error ? err.message : "Could not continue.",
416
+ }, err instanceof AgentAnswersError ? 400 : 409);
417
+ }
418
+ return;
419
+ }
420
+ if (req.method === "POST" && url.pathname === "/api/agent-questions/discard") {
421
+ if (!state.pack) {
422
+ sendJson(res, { error: "No pack selected." }, 400);
423
+ return;
424
+ }
425
+ try {
426
+ const discarded = await discardAgentQuestions(state.pack, state.implementLaunch);
427
+ state.last = discarded.result;
428
+ state.busy = null;
429
+ state.implementLaunch = null;
430
+ pushActivity(state, discarded.message, "ok");
431
+ await sendJson(res, { ...(await snapshot(state)), message: discarded.message });
432
+ }
433
+ catch (err) {
434
+ sendJson(res, {
435
+ ...(await snapshot(state)),
436
+ error: err instanceof Error ? err.message : "Could not discard questions.",
437
+ }, err instanceof AgentAnswersError ? 409 : 500);
438
+ }
439
+ return;
440
+ }
355
441
  if (req.method === "POST" && url.pathname === "/api/prove") {
356
442
  if (!state.pack) {
357
443
  sendJson(res, { error: "No pack selected." }, 400);
@@ -363,8 +449,8 @@ async function handle(state, req, res) {
363
449
  }
364
450
  const pack = state.pack;
365
451
  state.busy = "prove";
366
- state.activity = [];
367
- pushActivity(state, "Prove started.");
452
+ clearActivity(state, "prove");
453
+ pushActivity(state, "Prove started.", "info", "prove");
368
454
  await yieldEventLoop();
369
455
  await sendJson(res, { ...(await snapshot(state)), started: true, message: "Prove started." });
370
456
  void runProve(state, pack);
@@ -403,10 +489,17 @@ async function snapshot(state) {
403
489
  state.last = await compilePack(state.pack);
404
490
  }
405
491
  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.")) {
492
+ const agentQuestions = state.pack ? await readAgentQuestions(state.pack) : null;
493
+ const openQuestions = isOpenQuestions(agentQuestions) ? agentQuestions : null;
494
+ if (improveReview?.files.length && !state.implementActivity.some((line) => line.message === "Improve patch ready to review.")) {
407
495
  pushActivity(state, "Improve patch ready to review.", "ok");
408
496
  }
409
- const health = state.last ? applyImproveReviewGate(state.last.health, improveReview) : null;
497
+ if (openQuestions && !state.implementActivity.some((line) => line.message === AGENT_QUESTIONS_WAITING)) {
498
+ pushActivity(state, AGENT_QUESTIONS_WAITING);
499
+ }
500
+ let health = state.last ? applyImproveReviewGate(state.last.health, openQuestions ? null : improveReview) : null;
501
+ if (health)
502
+ health = applyAgentQuestionsGate(health, openQuestions);
410
503
  let proofReport = "";
411
504
  if (state.pack) {
412
505
  try {
@@ -430,9 +523,11 @@ async function snapshot(state) {
430
523
  proofReport,
431
524
  hasApiKey: hasJevAccess(),
432
525
  productRepo: await inspectProductRepo(await inspectFrom(state)),
433
- activity: state.activity,
526
+ implementActivity: state.implementActivity,
527
+ proveActivity: state.proveActivity,
434
528
  busy: state.busy,
435
529
  improveReview,
530
+ agentQuestions,
436
531
  };
437
532
  }
438
533
  async function inspectFrom(state) {
@@ -466,10 +561,30 @@ async function decideImproveReview(state, res, decision) {
466
561
  message: decision === "accept" ? "Kept the patch. Check when you are ready." : "Restored the spec from before Improve.",
467
562
  });
468
563
  }
564
+ async function runImproveWait(state, launch) {
565
+ try {
566
+ if (launch.finished)
567
+ await launch.finished;
568
+ if (state.pack && isOpenQuestions(await readAgentQuestions(state.pack))) {
569
+ pushActivity(state, AGENT_QUESTIONS_WAITING);
570
+ }
571
+ }
572
+ catch (err) {
573
+ pushActivity(state, err instanceof Error ? err.message : "Improve failed.", "error");
574
+ }
575
+ finally {
576
+ state.busy = null;
577
+ }
578
+ }
469
579
  async function runImprove(state, pack, adapter) {
470
580
  try {
471
581
  const launched = await launchImprove(pack, { adapter });
472
582
  pushActivity(state, launched.message, launched.ok ? "ok" : "error");
583
+ if (launched.finished)
584
+ await launched.finished;
585
+ if (isOpenQuestions(await readAgentQuestions(pack))) {
586
+ pushActivity(state, AGENT_QUESTIONS_WAITING);
587
+ }
473
588
  }
474
589
  catch (err) {
475
590
  pushActivity(state, err instanceof Error ? err.message : "Improve failed.", "error");
@@ -483,7 +598,8 @@ async function runImplement(state, pack, launch, adapter) {
483
598
  const done = await finishImplement(pack, launch, adapter);
484
599
  if (state.pack?.root === pack.root)
485
600
  state.last = done.result;
486
- pushActivity(state, done.message, done.result.health.build.state === "failed" ? "error" : "ok");
601
+ const waiting = done.message === AGENT_QUESTIONS_WAITING;
602
+ pushActivity(state, done.message, done.result.health.build.state === "failed" ? "error" : waiting ? "info" : "ok");
487
603
  }
488
604
  catch (err) {
489
605
  pushActivity(state, err instanceof Error ? err.message : "Implement failed.", "error");
@@ -498,13 +614,13 @@ async function runImplement(state, pack, launch, adapter) {
498
614
  async function runProve(state, pack) {
499
615
  try {
500
616
  const proved = await provePack(pack, {
501
- onProgress: (message, level) => pushActivity(state, message, level),
617
+ onProgress: (message, level) => pushActivity(state, message, level, "prove"),
502
618
  });
503
619
  state.last = proved.result;
504
620
  }
505
621
  catch (err) {
506
622
  const message = err instanceof Error ? err.message : "Prove failed.";
507
- pushActivity(state, message, "error");
623
+ pushActivity(state, message, "error", "prove");
508
624
  if (err instanceof JevUnavailableError && state.pack) {
509
625
  state.last = await compilePack(state.pack);
510
626
  }
@@ -513,9 +629,19 @@ async function runProve(state, pack) {
513
629
  state.busy = null;
514
630
  }
515
631
  }
516
- function pushActivity(state, message, level = "info") {
632
+ function clearActivity(state, channel) {
633
+ if (!channel || channel === "implement")
634
+ state.implementActivity = [];
635
+ if (!channel || channel === "prove")
636
+ state.proveActivity = [];
637
+ }
638
+ function pushActivity(state, message, level = "info", channel = "implement") {
517
639
  const line = { at: new Date().toISOString(), level, message };
518
- state.activity = [...state.activity, line].slice(-80);
640
+ if (channel === "prove") {
641
+ state.proveActivity = [...state.proveActivity, line].slice(-80);
642
+ return;
643
+ }
644
+ state.implementActivity = [...state.implementActivity, line].slice(-80);
519
645
  }
520
646
  async function yieldEventLoop() {
521
647
  await new Promise((resolve) => setImmediate(resolve));
package/dist/types.d.ts CHANGED
@@ -200,6 +200,10 @@ export type BuildRun = {
200
200
  pid?: number;
201
201
  /** Owner chose Ignore. Prove may run even if state is failed. */
202
202
  ignored?: boolean;
203
+ /** Agent exited to wait for derived/agent-questions.json. Not a health.json key. */
204
+ waitingOnQuestions?: boolean;
205
+ /** This run was Fix from proof. Used when resuming after answers. */
206
+ fromProof?: boolean;
203
207
  };
204
208
  export type ProofState = "not_yet" | "passed" | "failed" | "needs_review" | "stale";
205
209
  export type ProofFinding = {
@@ -237,6 +241,7 @@ export type ProofRun = {
237
241
  cases: ProofCaseResult[];
238
242
  };
239
243
  export type ActivityLevel = "info" | "ok" | "error";
244
+ export type ActivityChannel = "implement" | "prove";
240
245
  export type ActivityLine = {
241
246
  at: string;
242
247
  level: ActivityLevel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@richjava/req0",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Requirement operating system: spec packs, a local cockpit, Jev, coding-agent implement, and browser proof.",
5
5
  "license": "MIT",
6
6
  "type": "module",