@richjava/req0 0.2.0 → 0.4.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,28 @@ 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, startAuthor, settleAuthor, launchAuthor, stopAuthor, saveAuthorArtifact, readAuthorRun } from "./pack.js";
7
7
  import { IgnoreBuildLockedError } from "./ready.js";
8
8
  import { ImproveLockedError } from "./improve.js";
9
+ import { applyAuthorGate, AuthorArtifactError, AuthorLockedError } from "./author.js";
9
10
  import { acceptImproveReview, applyImproveReviewGate, evaluateImproveReview, ImproveLintError, ImproveReviewPendingError, rejectImproveReview, } from "./improve-review.js";
10
11
  import { inspectProductRepo, needsStackChoice, recordProjectSetup, recordStack, UnknownStackError } from "./stack.js";
11
12
  import { adapterDisplayName, parseAdapterId } from "./adapter.js";
12
13
  import { pipelineView, sectionCatalog } from "./stages.js";
14
+ import { AGENT_QUESTIONS_WAITING, AgentAnswersError, applyAgentQuestionsGate, isOpenQuestions, readAgentQuestions, } from "./agent-questions.js";
13
15
  const cockpitDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../cockpit");
14
16
  export function createAppState(cwd, pack) {
15
- return { cwd, pack, last: null, implementActivity: [], proveActivity: [], busy: null, implementLaunch: null };
17
+ return {
18
+ cwd,
19
+ pack,
20
+ last: null,
21
+ implementActivity: [],
22
+ proveActivity: [],
23
+ authorActivity: [],
24
+ busy: null,
25
+ implementLaunch: null,
26
+ authorLaunch: null,
27
+ };
16
28
  }
17
29
  export async function refresh(state) {
18
30
  if (!state.pack) {
@@ -310,13 +322,18 @@ async function handle(state, req, res) {
310
322
  sendJson(res, { error: "No pack selected." }, 400);
311
323
  return;
312
324
  }
313
- if (!state.implementLaunch || state.busy !== "implement") {
325
+ const waiting = isOpenQuestions(await readAgentQuestions(state.pack));
326
+ if ((!state.implementLaunch || state.busy !== "implement") && !waiting) {
314
327
  sendJson(res, { ...(await snapshot(state)), error: "Implement is not running." }, 409);
315
328
  return;
316
329
  }
317
330
  try {
318
- const stopped = await stopImplement(state.pack, state.implementLaunch);
331
+ const stopped = waiting && (!state.implementLaunch || state.busy !== "implement")
332
+ ? await discardAgentQuestions(state.pack, state.implementLaunch)
333
+ : await stopImplement(state.pack, state.implementLaunch);
319
334
  state.last = stopped.result;
335
+ state.busy = null;
336
+ state.implementLaunch = null;
320
337
  pushActivity(state, stopped.message, "ok");
321
338
  await sendJson(res, { ...(await snapshot(state)), message: stopped.message });
322
339
  }
@@ -324,7 +341,7 @@ async function handle(state, req, res) {
324
341
  sendJson(res, {
325
342
  ...(await snapshot(state)),
326
343
  error: err instanceof Error ? err.message : "Stop failed.",
327
- }, err instanceof StopImplementLockedError ? 409 : 500);
344
+ }, err instanceof StopImplementLockedError || err instanceof AgentAnswersError ? 409 : 500);
328
345
  }
329
346
  return;
330
347
  }
@@ -352,6 +369,99 @@ async function handle(state, req, res) {
352
369
  }
353
370
  return;
354
371
  }
372
+ if (req.method === "POST" && url.pathname === "/api/agent-answers") {
373
+ if (!state.pack) {
374
+ sendJson(res, { error: "No pack selected." }, 400);
375
+ return;
376
+ }
377
+ if (state.busy) {
378
+ sendJson(res, { ...(await snapshot(state)), error: "Another action is already running." }, 409);
379
+ return;
380
+ }
381
+ const body = await readJson(req);
382
+ const raw = body["answers"];
383
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
384
+ sendJson(res, { error: "answers must be an object of question id to value." }, 400);
385
+ return;
386
+ }
387
+ const answers = {};
388
+ for (const [id, value] of Object.entries(raw)) {
389
+ answers[id] = String(value ?? "");
390
+ }
391
+ const skipAll = body["skipAll"] === true;
392
+ const adapter = parseAdapterId(body["adapter"]) ?? "cursor";
393
+ try {
394
+ const round = await readAgentQuestions(state.pack);
395
+ const logChannel = round?.channel === "author" ? "author" : "implement";
396
+ const answered = await answerAgentQuestions(state.pack, answers, {
397
+ adapter,
398
+ skipAll,
399
+ onLogLine: (line) => pushActivity(state, line, "info", logChannel),
400
+ });
401
+ state.last = answered.result;
402
+ const summary = Object.entries(answers)
403
+ .filter(([, value]) => value.trim())
404
+ .map(([id, value]) => `${id}: ${value.trim()}`)
405
+ .join("; ");
406
+ pushActivity(state, summary ? `Owner answered. ${summary}` : skipAll ? "Owner skipped this round." : "Owner answered. Continuing.", "ok", answered.channel === "author" ? "author" : "implement");
407
+ if (answered.launch?.finished) {
408
+ state.busy =
409
+ answered.channel === "author" ? "author" : answered.channel === "improve" ? "improve" : "implement";
410
+ if (answered.channel === "implement")
411
+ state.implementLaunch = answered.launch;
412
+ if (answered.channel === "author")
413
+ state.authorLaunch = answered.launch;
414
+ await yieldEventLoop();
415
+ await sendJson(res, {
416
+ ...(await snapshot(state)),
417
+ started: true,
418
+ message: answered.message,
419
+ });
420
+ if (answered.channel === "implement") {
421
+ void runImplement(state, state.pack, answered.launch, adapter);
422
+ }
423
+ else if (answered.channel === "author") {
424
+ void runAuthor(state, state.pack, answered.launch, adapter);
425
+ }
426
+ else {
427
+ void runImproveWait(state, answered.launch);
428
+ }
429
+ return;
430
+ }
431
+ await sendJson(res, { ...(await snapshot(state)), message: answered.message });
432
+ }
433
+ catch (err) {
434
+ sendJson(res, {
435
+ ...(await snapshot(state)),
436
+ error: err instanceof Error ? err.message : "Could not continue.",
437
+ }, err instanceof AgentAnswersError ? 400 : 409);
438
+ }
439
+ return;
440
+ }
441
+ if (req.method === "POST" && url.pathname === "/api/agent-questions/discard") {
442
+ if (!state.pack) {
443
+ sendJson(res, { error: "No pack selected." }, 400);
444
+ return;
445
+ }
446
+ try {
447
+ const discarded = await discardAgentQuestions(state.pack, discardedLaunch(state, (await readAgentQuestions(state.pack))?.channel));
448
+ state.last = discarded.result;
449
+ state.busy = null;
450
+ if (discarded.channel === "implement")
451
+ state.implementLaunch = null;
452
+ if (discarded.channel === "author")
453
+ state.authorLaunch = null;
454
+ pushActivity(state, discarded.message, "ok", discarded.channel === "author" ? "author" : "implement");
455
+ await sendJson(res, { ...(await snapshot(state)), message: discarded.message });
456
+ }
457
+ catch (err) {
458
+ sendJson(res, {
459
+ ...(await snapshot(state)),
460
+ error: err instanceof Error ? err.message : "Could not discard questions.",
461
+ }, err instanceof AgentAnswersError ? 409 : 500);
462
+ }
463
+ return;
464
+ }
355
465
  if (req.method === "POST" && url.pathname === "/api/prove") {
356
466
  if (!state.pack) {
357
467
  sendJson(res, { error: "No pack selected." }, 400);
@@ -370,6 +480,91 @@ async function handle(state, req, res) {
370
480
  void runProve(state, pack);
371
481
  return;
372
482
  }
483
+ if (req.method === "POST" && url.pathname === "/api/author/start") {
484
+ if (!state.pack) {
485
+ sendJson(res, { error: "No pack selected." }, 400);
486
+ return;
487
+ }
488
+ if (state.busy) {
489
+ sendJson(res, { ...(await snapshot(state)), error: "Another action is already running." }, 409);
490
+ return;
491
+ }
492
+ const body = await readJson(req);
493
+ const description = String(body["description"] ?? "").trim();
494
+ const adapter = parseAdapterId(body["adapter"]) ?? "cursor";
495
+ try {
496
+ clearActivity(state, "author");
497
+ const started = await startAuthor(state.pack, description, {
498
+ adapter,
499
+ onLogLine: (line) => pushActivity(state, line, "info", "author"),
500
+ });
501
+ state.last = started.result;
502
+ pushActivity(state, started.message, started.launch?.ok === false ? "error" : "ok", "author");
503
+ if (started.launch?.finished) {
504
+ state.busy = "author";
505
+ state.authorLaunch = started.launch;
506
+ await yieldEventLoop();
507
+ await sendJson(res, {
508
+ ...(await snapshot(state)),
509
+ started: true,
510
+ message: started.message,
511
+ });
512
+ void runAuthor(state, state.pack, started.launch, adapter);
513
+ return;
514
+ }
515
+ await sendJson(res, { ...(await snapshot(state)), message: started.message });
516
+ }
517
+ catch (err) {
518
+ sendJson(res, {
519
+ ...(await snapshot(state)),
520
+ error: err instanceof Error ? err.message : "Could not start authoring.",
521
+ }, err instanceof AuthorLockedError || err instanceof AgentAnswersError ? 409 : 500);
522
+ }
523
+ return;
524
+ }
525
+ if (req.method === "POST" && url.pathname === "/api/author/stop") {
526
+ if (!state.pack) {
527
+ sendJson(res, { error: "No pack selected." }, 400);
528
+ return;
529
+ }
530
+ try {
531
+ const stopped = await stopAuthor(state.pack, state.authorLaunch);
532
+ state.last = stopped.result;
533
+ state.busy = null;
534
+ state.authorLaunch = null;
535
+ pushActivity(state, stopped.message, "ok", "author");
536
+ await sendJson(res, { ...(await snapshot(state)), message: stopped.message });
537
+ }
538
+ catch (err) {
539
+ sendJson(res, {
540
+ ...(await snapshot(state)),
541
+ error: err instanceof Error ? err.message : "Could not stop authoring.",
542
+ }, 500);
543
+ }
544
+ return;
545
+ }
546
+ if (req.method === "POST" && url.pathname === "/api/author/artifact") {
547
+ if (!state.pack) {
548
+ sendJson(res, { error: "No pack selected." }, 400);
549
+ return;
550
+ }
551
+ const body = await readJson(req);
552
+ const filename = String(body["filename"] ?? "");
553
+ const mime = String(body["mime"] ?? "");
554
+ const data = String(body["data"] ?? "");
555
+ try {
556
+ const saved = await saveAuthorArtifact(state.pack, filename, mime, data);
557
+ pushActivity(state, `Saved context/${saved}.`, "ok", "author");
558
+ await sendJson(res, { ...(await snapshot(state)), filename: saved, message: `Saved context/${saved}.` });
559
+ }
560
+ catch (err) {
561
+ sendJson(res, {
562
+ ...(await snapshot(state)),
563
+ error: err instanceof Error ? err.message : "Could not save the upload.",
564
+ }, err instanceof AuthorArtifactError ? 400 : 500);
565
+ }
566
+ return;
567
+ }
373
568
  if (req.method === "POST" && url.pathname === "/api/create-in-place") {
374
569
  if (!state.pack) {
375
570
  sendJson(res, { error: "No pack folder selected." }, 400);
@@ -402,11 +597,24 @@ async function snapshot(state) {
402
597
  if (state.pack && !state.last) {
403
598
  state.last = await compilePack(state.pack);
404
599
  }
600
+ const authorRun = state.pack ? await readAuthorRun(state.pack) : null;
405
601
  const improveReview = state.pack ? await evaluateImproveReview(state.pack) : null;
602
+ const agentQuestions = state.pack ? await readAgentQuestions(state.pack) : null;
603
+ const openQuestions = isOpenQuestions(agentQuestions) ? agentQuestions : null;
406
604
  if (improveReview?.files.length && !state.implementActivity.some((line) => line.message === "Improve patch ready to review.")) {
407
605
  pushActivity(state, "Improve patch ready to review.", "ok");
408
606
  }
409
- const health = state.last ? applyImproveReviewGate(state.last.health, improveReview) : null;
607
+ if (openQuestions && openQuestions.channel === "author" && !state.authorActivity.some((line) => line.message === AGENT_QUESTIONS_WAITING)) {
608
+ pushActivity(state, AGENT_QUESTIONS_WAITING, "info", "author");
609
+ }
610
+ else if (openQuestions && openQuestions.channel !== "author" && !state.implementActivity.some((line) => line.message === AGENT_QUESTIONS_WAITING)) {
611
+ pushActivity(state, AGENT_QUESTIONS_WAITING);
612
+ }
613
+ let health = state.last ? applyImproveReviewGate(state.last.health, openQuestions ? null : improveReview) : null;
614
+ if (health)
615
+ health = applyAuthorGate(health, authorRun);
616
+ if (health)
617
+ health = applyAgentQuestionsGate(health, openQuestions);
410
618
  let proofReport = "";
411
619
  if (state.pack) {
412
620
  try {
@@ -432,8 +640,11 @@ async function snapshot(state) {
432
640
  productRepo: await inspectProductRepo(await inspectFrom(state)),
433
641
  implementActivity: state.implementActivity,
434
642
  proveActivity: state.proveActivity,
643
+ authorActivity: state.authorActivity,
435
644
  busy: state.busy,
436
645
  improveReview,
646
+ agentQuestions,
647
+ authorRun,
437
648
  };
438
649
  }
439
650
  async function inspectFrom(state) {
@@ -467,10 +678,30 @@ async function decideImproveReview(state, res, decision) {
467
678
  message: decision === "accept" ? "Kept the patch. Check when you are ready." : "Restored the spec from before Improve.",
468
679
  });
469
680
  }
681
+ async function runImproveWait(state, launch) {
682
+ try {
683
+ if (launch.finished)
684
+ await launch.finished;
685
+ if (state.pack && isOpenQuestions(await readAgentQuestions(state.pack))) {
686
+ pushActivity(state, AGENT_QUESTIONS_WAITING);
687
+ }
688
+ }
689
+ catch (err) {
690
+ pushActivity(state, err instanceof Error ? err.message : "Improve failed.", "error");
691
+ }
692
+ finally {
693
+ state.busy = null;
694
+ }
695
+ }
470
696
  async function runImprove(state, pack, adapter) {
471
697
  try {
472
698
  const launched = await launchImprove(pack, { adapter });
473
699
  pushActivity(state, launched.message, launched.ok ? "ok" : "error");
700
+ if (launched.finished)
701
+ await launched.finished;
702
+ if (isOpenQuestions(await readAgentQuestions(pack))) {
703
+ pushActivity(state, AGENT_QUESTIONS_WAITING);
704
+ }
474
705
  }
475
706
  catch (err) {
476
707
  pushActivity(state, err instanceof Error ? err.message : "Improve failed.", "error");
@@ -484,7 +715,8 @@ async function runImplement(state, pack, launch, adapter) {
484
715
  const done = await finishImplement(pack, launch, adapter);
485
716
  if (state.pack?.root === pack.root)
486
717
  state.last = done.result;
487
- pushActivity(state, done.message, done.result.health.build.state === "failed" ? "error" : "ok");
718
+ const waiting = done.message === AGENT_QUESTIONS_WAITING;
719
+ pushActivity(state, done.message, done.result.health.build.state === "failed" ? "error" : waiting ? "info" : "ok");
488
720
  }
489
721
  catch (err) {
490
722
  pushActivity(state, err instanceof Error ? err.message : "Implement failed.", "error");
@@ -496,6 +728,47 @@ async function runImplement(state, pack, launch, adapter) {
496
728
  state.implementLaunch = null;
497
729
  }
498
730
  }
731
+ async function runAuthor(state, pack, launch, adapter) {
732
+ let current = launch;
733
+ try {
734
+ while (true) {
735
+ const settled = await settleAuthor(pack, current);
736
+ if (state.pack?.root === pack.root)
737
+ state.last = settled.result;
738
+ pushActivity(state, settled.message, settled.action === "done" ? "ok" : settled.action === "capped" ? "error" : "info", "author");
739
+ if (settled.action === "wait_questions" || settled.action === "done" || settled.action === "capped") {
740
+ break;
741
+ }
742
+ if (settled.action !== "relaunch")
743
+ break;
744
+ const next = await launchAuthor(pack, {
745
+ adapter,
746
+ onLogLine: (line) => pushActivity(state, line, "info", "author"),
747
+ });
748
+ pushActivity(state, next.message, next.ok ? "ok" : "error", "author");
749
+ if (!next.finished)
750
+ break;
751
+ current = next;
752
+ state.authorLaunch = next;
753
+ }
754
+ }
755
+ catch (err) {
756
+ pushActivity(state, err instanceof Error ? err.message : "Authoring failed.", "error", "author");
757
+ }
758
+ finally {
759
+ if (state.busy === "author")
760
+ state.busy = null;
761
+ if (state.authorLaunch === current || state.authorLaunch === launch)
762
+ state.authorLaunch = null;
763
+ }
764
+ }
765
+ function discardedLaunch(state, channel) {
766
+ if (channel === "author")
767
+ return state.authorLaunch;
768
+ if (channel === "implement")
769
+ return state.implementLaunch;
770
+ return state.implementLaunch;
771
+ }
499
772
  async function runProve(state, pack) {
500
773
  try {
501
774
  const proved = await provePack(pack, {
@@ -519,6 +792,8 @@ function clearActivity(state, channel) {
519
792
  state.implementActivity = [];
520
793
  if (!channel || channel === "prove")
521
794
  state.proveActivity = [];
795
+ if (!channel || channel === "author")
796
+ state.authorActivity = [];
522
797
  }
523
798
  function pushActivity(state, message, level = "info", channel = "implement") {
524
799
  const line = { at: new Date().toISOString(), level, message };
@@ -526,6 +801,10 @@ function pushActivity(state, message, level = "info", channel = "implement") {
526
801
  state.proveActivity = [...state.proveActivity, line].slice(-80);
527
802
  return;
528
803
  }
804
+ if (channel === "author") {
805
+ state.authorActivity = [...state.authorActivity, line].slice(-80);
806
+ return;
807
+ }
529
808
  state.implementActivity = [...state.implementActivity, line].slice(-80);
530
809
  }
531
810
  async function yieldEventLoop() {
package/dist/types.d.ts CHANGED
@@ -66,6 +66,8 @@ export type StageBoard = {
66
66
  ignoreBuild?: StageAction;
67
67
  /** Shown while Implement is running. */
68
68
  stopImplement?: StageAction;
69
+ /** Shown while authoring is running. */
70
+ stopAuthor?: StageAction;
69
71
  };
70
72
  /** Cockpit display only. health.json still uses spec / ready / build / proof. */
71
73
  export type PipelineTone = "idle" | "draft" | "ok" | "bad" | "progress";
@@ -200,6 +202,10 @@ export type BuildRun = {
200
202
  pid?: number;
201
203
  /** Owner chose Ignore. Prove may run even if state is failed. */
202
204
  ignored?: boolean;
205
+ /** Agent exited to wait for derived/agent-questions.json. Not a health.json key. */
206
+ waitingOnQuestions?: boolean;
207
+ /** This run was Fix from proof. Used when resuming after answers. */
208
+ fromProof?: boolean;
203
209
  };
204
210
  export type ProofState = "not_yet" | "passed" | "failed" | "needs_review" | "stale";
205
211
  export type ProofFinding = {
@@ -237,7 +243,7 @@ export type ProofRun = {
237
243
  cases: ProofCaseResult[];
238
244
  };
239
245
  export type ActivityLevel = "info" | "ok" | "error";
240
- export type ActivityChannel = "implement" | "prove";
246
+ export type ActivityChannel = "implement" | "prove" | "author";
241
247
  export type ActivityLine = {
242
248
  at: string;
243
249
  level: ActivityLevel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@richjava/req0",
3
- "version": "0.2.0",
3
+ "version": "0.4.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",