@richjava/req0 0.2.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,13 +3,14 @@ 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
16
  return { cwd, pack, last: null, implementActivity: [], proveActivity: [], busy: null, implementLaunch: null };
@@ -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);
@@ -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;
492
+ const agentQuestions = state.pack ? await readAgentQuestions(state.pack) : null;
493
+ const openQuestions = isOpenQuestions(agentQuestions) ? agentQuestions : null;
406
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 {
@@ -434,6 +527,7 @@ async function snapshot(state) {
434
527
  proveActivity: state.proveActivity,
435
528
  busy: state.busy,
436
529
  improveReview,
530
+ agentQuestions,
437
531
  };
438
532
  }
439
533
  async function inspectFrom(state) {
@@ -467,10 +561,30 @@ async function decideImproveReview(state, res, decision) {
467
561
  message: decision === "accept" ? "Kept the patch. Check when you are ready." : "Restored the spec from before Improve.",
468
562
  });
469
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
+ }
470
579
  async function runImprove(state, pack, adapter) {
471
580
  try {
472
581
  const launched = await launchImprove(pack, { adapter });
473
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
+ }
474
588
  }
475
589
  catch (err) {
476
590
  pushActivity(state, err instanceof Error ? err.message : "Improve failed.", "error");
@@ -484,7 +598,8 @@ async function runImplement(state, pack, launch, adapter) {
484
598
  const done = await finishImplement(pack, launch, adapter);
485
599
  if (state.pack?.root === pack.root)
486
600
  state.last = done.result;
487
- 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");
488
603
  }
489
604
  catch (err) {
490
605
  pushActivity(state, err instanceof Error ? err.message : "Implement failed.", "error");
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 = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@richjava/req0",
3
- "version": "0.2.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",