@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/cockpit/app.js CHANGED
@@ -45,6 +45,8 @@ const els = {
45
45
  createNew: document.getElementById("create-new"),
46
46
  createError: document.getElementById("create-error"),
47
47
  homeBtn: document.getElementById("home-btn"),
48
+ projectName: document.getElementById("project-name"),
49
+ subbar: document.getElementById("subbar"),
48
50
  allRequirements: document.getElementById("all-requirements"),
49
51
  packHome: document.getElementById("pack-home"),
50
52
  packWorkspace: document.getElementById("pack-workspace"),
@@ -75,6 +77,26 @@ const els = {
75
77
  reviewDiff: document.getElementById("review-diff"),
76
78
  reviewAccept: document.getElementById("review-accept"),
77
79
  reviewReject: document.getElementById("review-reject"),
80
+ questionActions: document.getElementById("question-actions"),
81
+ questionContinue: document.getElementById("question-continue"),
82
+ questionStop: document.getElementById("question-stop"),
83
+ questionSkipAll: document.getElementById("question-skip-all"),
84
+ defineQuestions: document.getElementById("define-questions"),
85
+ defineTabs: document.getElementById("define-tabs"),
86
+ defineTabQuestions: document.getElementById("define-tab-questions"),
87
+ defineTabActivity: document.getElementById("define-tab-activity"),
88
+ defineActivity: document.getElementById("define-activity"),
89
+ authorDescribe: document.getElementById("author-describe"),
90
+ authorDescription: document.getElementById("author-description"),
91
+ authorStart: document.getElementById("author-start"),
92
+ implementTabs: document.getElementById("implement-tabs"),
93
+ implementTabQuestions: document.getElementById("implement-tab-questions"),
94
+ implementTabActivity: document.getElementById("implement-tab-activity"),
95
+ implementActivityLabel: document.getElementById("implement-activity-label"),
96
+ implementQuestions: document.getElementById("implement-questions"),
97
+ discardDialog: document.getElementById("discard-questions-dialog"),
98
+ discardCancel: document.getElementById("discard-questions-cancel"),
99
+ discardConfirm: document.getElementById("discard-questions-confirm"),
78
100
  };
79
101
 
80
102
  let state = null;
@@ -103,6 +125,14 @@ let proveLines = [];
103
125
  let lastImplementKey = "";
104
126
  let lastProveKey = "";
105
127
  let qaTab = "activity";
128
+ let implementTab = "questions";
129
+ let defineTab = "questions";
130
+ let authorLines = [];
131
+ let lastAuthorKey = "";
132
+ const SKIP_ANSWER = "__skip__";
133
+ let questionDrafts = {};
134
+ let questionDraftKey = "";
135
+ let lastQuestionsKey = "";
106
136
  let loadInFlight = false;
107
137
  let loadAgain = false;
108
138
  let loadTimer;
@@ -112,10 +142,11 @@ function applyActivities(payload) {
112
142
  if (!payload) return;
113
143
  if (Array.isArray(payload.implementActivity)) implementLines = payload.implementActivity;
114
144
  if (Array.isArray(payload.proveActivity)) proveLines = payload.proveActivity;
145
+ if (Array.isArray(payload.authorActivity)) authorLines = payload.authorActivity;
115
146
  }
116
147
 
117
148
  function lastBusyMessage(kind) {
118
- const lines = kind === "prove" ? proveLines : implementLines;
149
+ const lines = kind === "prove" ? proveLines : kind === "author" ? authorLines : implementLines;
119
150
  return lines[lines.length - 1]?.message ?? "";
120
151
  }
121
152
 
@@ -206,11 +237,18 @@ function renderSetupGate(snapshot) {
206
237
  }
207
238
  }
208
239
 
240
+ function directoryName(cwd) {
241
+ if (typeof cwd !== "string") return "";
242
+ const parts = cwd.split(/[/\\]/).filter(Boolean);
243
+ return parts.at(-1) ?? "";
244
+ }
245
+
209
246
  function render() {
210
247
  const health = state.health;
211
248
  const packId = state.packId;
212
249
  const onHome = !packId;
213
250
  const packList = Array.isArray(state.packList) ? state.packList : [];
251
+ if (els.projectName) els.projectName.textContent = directoryName(state.cwd);
214
252
  els.title.textContent = packId ? `Requirement: ${packId.replaceAll("-", " ")}` : "Requirements";
215
253
  els.going.textContent = packId
216
254
  ? (health?.howThisIsGoing ?? "")
@@ -221,17 +259,26 @@ function render() {
221
259
 
222
260
  const action = health?.nextAction;
223
261
  const review = state.improveReview;
224
- if (review?.files?.length) actionNotice = "";
225
- const canStop = action?.id === "stop-implement";
226
- const nextEnabled = Boolean(action?.enabled) && !(actionBusy && !canStop && !review?.files?.length);
262
+ const questions = openQuestions();
263
+ syncQuestionDrafts(questions);
264
+ maybeOpenQuestions(questions);
265
+ if (review?.files?.length || questions) actionNotice = "";
266
+ const canStop = action?.id === "stop-implement" || action?.id === "stop-author";
267
+ const continueReady = action?.id !== "answer-questions" || questionsContinueEnabled(questions);
268
+ const describeReady = action?.id !== "author-start" || Boolean(String(els.authorDescription?.value ?? "").trim());
269
+ const nextEnabled =
270
+ Boolean(action?.enabled) &&
271
+ continueReady &&
272
+ describeReady &&
273
+ !(actionBusy && !canStop && !review?.files?.length && !questions);
227
274
  const nextLabel =
228
- actionBusy && headerBusyLabel && !canStop && !review?.files?.length
275
+ actionBusy && headerBusyLabel && !canStop && !review?.files?.length && !questions
229
276
  ? headerBusyLabel
230
277
  : action?.label ?? "Create a requirement";
231
278
  paintActionButton(els.next, {
232
279
  id: action?.id ?? "create",
233
280
  label: nextLabel,
234
- hint: review ? action?.hint || "" : actionNotice || action?.hint || "",
281
+ hint: questions || review ? action?.hint || "" : actionNotice || action?.hint || "",
235
282
  enabled: nextEnabled,
236
283
  primary: true,
237
284
  hidden: false,
@@ -244,6 +291,7 @@ function render() {
244
291
  els.next.hidden = gated || onHome;
245
292
  els.packHome?.classList.toggle("hidden", !onHome);
246
293
  els.packWorkspace?.classList.toggle("hidden", onHome);
294
+ els.subbar?.classList.toggle("hidden", onHome);
247
295
  els.allRequirements?.classList.toggle("hidden", onHome);
248
296
  els.createBar?.classList.toggle("hidden", !onHome);
249
297
  if (!onHome) hidePackMenu();
@@ -257,10 +305,10 @@ function render() {
257
305
 
258
306
  if (onHome) return;
259
307
 
260
- if (review && currentView !== "define") currentView = "define";
308
+ if (review && !questions && currentView !== "define") currentView = "define";
261
309
  if (currentView === "implement" && health?.build?.owned === false) currentView = "define";
262
310
 
263
- if (!applying && !state.improveReview) {
311
+ if (!applying && !state.improveReview && questions?.channel !== "improve" && questions?.channel !== "author") {
264
312
  applying = true;
265
313
  markdownEditor.setValue(state.markdown ?? "");
266
314
  applying = false;
@@ -269,7 +317,11 @@ function render() {
269
317
  renderPipeline();
270
318
  renderStageNav();
271
319
  showView(currentView);
320
+ renderAuthorDescribe();
272
321
  renderReview();
322
+ renderQuestions();
323
+ renderDefineLayout();
324
+ renderImplementLayout();
273
325
  renderSpecPane();
274
326
  renderJudgmentPane();
275
327
  renderRunStatus();
@@ -404,6 +456,11 @@ async function selectPack(id) {
404
456
  takeState(body);
405
457
  currentView = "define";
406
458
  qaTab = "activity";
459
+ implementTab = "questions";
460
+ defineTab = "questions";
461
+ lastQuestionsKey = "";
462
+ authorLines = [];
463
+ if (els.authorDescription) els.authorDescription.value = "";
407
464
  actionNotice = "";
408
465
  render();
409
466
  }
@@ -419,22 +476,408 @@ async function goHome() {
419
476
  takeState(body);
420
477
  currentView = "define";
421
478
  qaTab = "activity";
479
+ implementTab = "questions";
480
+ defineTab = "questions";
481
+ lastQuestionsKey = "";
482
+ authorLines = [];
422
483
  actionNotice = "";
423
484
  render();
424
485
  }
425
486
 
487
+ function openQuestions() {
488
+ const round = state?.agentQuestions;
489
+ if (!round || round.status !== "open" || !Array.isArray(round.questions) || !round.questions.length) {
490
+ return null;
491
+ }
492
+ return round;
493
+ }
494
+
495
+ function questionsContinueEnabled(round = openQuestions()) {
496
+ if (!round) return false;
497
+ return round.questions.every((question) => {
498
+ if (!question.required) return true;
499
+ return Boolean(String(questionDrafts[question.id] ?? "").trim());
500
+ });
501
+ }
502
+
503
+ function syncQuestionDrafts(round) {
504
+ const key = round
505
+ ? `${round.channel}|${round.at}|${round.questions.map((question) => question.id).join(",")}`
506
+ : "";
507
+ if (key === questionDraftKey) return;
508
+ questionDraftKey = key;
509
+ questionDrafts = {};
510
+ if (!round) return;
511
+ for (const question of round.questions) {
512
+ questionDrafts[question.id] = question.answer?.skipped
513
+ ? SKIP_ANSWER
514
+ : question.answer?.optionId || question.answer?.text || question.answer?.artifact || "";
515
+ }
516
+ }
517
+
518
+ function maybeOpenQuestions(round) {
519
+ const key = round ? `${round.channel}|${round.at}` : "";
520
+ if (key && key !== lastQuestionsKey) {
521
+ currentView = round.channel === "implement" ? "implement" : "define";
522
+ if (round.channel === "implement") implementTab = "questions";
523
+ else defineTab = "questions";
524
+ }
525
+ lastQuestionsKey = key;
526
+ }
527
+
528
+ function collectedAnswers(round = openQuestions()) {
529
+ const answers = {};
530
+ if (!round) return answers;
531
+ for (const question of round.questions) {
532
+ answers[question.id] = String(questionDrafts[question.id] ?? "");
533
+ }
534
+ return answers;
535
+ }
536
+
537
+ async function uploadAuthorArtifact(questionId, file, status) {
538
+ status.textContent = "Uploading…";
539
+ try {
540
+ const data = await fileToBase64(file);
541
+ const res = await fetch("/api/author/artifact", {
542
+ method: "POST",
543
+ headers: { "Content-Type": "application/json" },
544
+ body: JSON.stringify({ filename: file.name, mime: file.type || mimeFromName(file.name), data }),
545
+ });
546
+ const body = await res.json();
547
+ if (!res.ok) {
548
+ status.textContent = body.error ?? "Could not save the upload.";
549
+ return;
550
+ }
551
+ questionDrafts[questionId] = body.filename ?? file.name;
552
+ status.textContent = `Saved context/${questionDrafts[questionId]}`;
553
+ if (body.health || body.packId) state = { ...state, ...body };
554
+ applyActivities(body);
555
+ paintQuestionContinue();
556
+ } catch (err) {
557
+ status.textContent = err instanceof Error ? err.message : "Could not save the upload.";
558
+ }
559
+ }
560
+
561
+ function mimeFromName(name) {
562
+ if (/\.png$/i.test(name)) return "image/png";
563
+ if (/\.jpe?g$/i.test(name)) return "image/jpeg";
564
+ if (/\.webp$/i.test(name)) return "image/webp";
565
+ return "";
566
+ }
567
+
568
+ function fileToBase64(file) {
569
+ return new Promise((resolve, reject) => {
570
+ const reader = new FileReader();
571
+ reader.onload = () => {
572
+ const result = String(reader.result ?? "");
573
+ const comma = result.indexOf(",");
574
+ resolve(comma >= 0 ? result.slice(comma + 1) : result);
575
+ };
576
+ reader.onerror = () => reject(new Error("Could not read the file."));
577
+ reader.readAsDataURL(file);
578
+ });
579
+ }
580
+
581
+ function paintQuestionContinue() {
582
+ const round = openQuestions();
583
+ const askingDefine = round?.channel === "improve" || round?.channel === "author";
584
+ const ready = questionsContinueEnabled(round);
585
+ paintActionButton(els.questionContinue, {
586
+ id: "answer-questions",
587
+ label: "Continue",
588
+ hint: "Answer each required question, then Continue. You may skip a question or all of them.",
589
+ enabled: Boolean(askingDefine && ready && !actionBusy),
590
+ primary: true,
591
+ hidden: !askingDefine,
592
+ });
593
+ paintActionButton(els.questionSkipAll, {
594
+ id: "skip-all-questions",
595
+ label: "Skip all",
596
+ hint: "Skip this round. The agent will not assume answers.",
597
+ enabled: Boolean(askingDefine && !actionBusy),
598
+ primary: false,
599
+ hidden: !askingDefine,
600
+ });
601
+ const action = state?.health?.nextAction;
602
+ if (action?.id === "answer-questions") {
603
+ paintActionButton(els.next, {
604
+ id: action.id,
605
+ label: action.label,
606
+ hint: action.hint || "",
607
+ enabled: Boolean(action.enabled && ready && !actionBusy),
608
+ primary: true,
609
+ hidden: false,
610
+ });
611
+ }
612
+ if (action?.id === "author-start") {
613
+ paintActionButton(els.next, {
614
+ id: action.id,
615
+ label: action.label,
616
+ hint: action.hint || "",
617
+ enabled: Boolean(action.enabled && String(els.authorDescription?.value ?? "").trim() && !actionBusy),
618
+ primary: true,
619
+ hidden: false,
620
+ });
621
+ }
622
+ }
623
+
624
+ function fillQuestionPanel(panel, round) {
625
+ const key = `${round.channel}|${round.at}|${round.questions.map((question) => question.id).join(",")}`;
626
+ if (panel.dataset.sig === key) {
627
+ syncQuestionCardSelection(panel);
628
+ return;
629
+ }
630
+ panel.dataset.sig = key;
631
+ panel.innerHTML = "";
632
+ for (const question of round.questions) {
633
+ panel.append(renderQuestionCard(question));
634
+ }
635
+ }
636
+
637
+ function syncQuestionCardSelection(panel) {
638
+ for (const card of panel.querySelectorAll(".question-card")) {
639
+ const id = card.dataset.questionId;
640
+ const value = questionDrafts[id] ?? "";
641
+ for (const btn of card.querySelectorAll(".stack-choice")) {
642
+ btn.classList.toggle("is-selected", btn.dataset.optionId === value);
643
+ }
644
+ }
645
+ }
646
+
647
+ function renderQuestionCard(question) {
648
+ const article = document.createElement("article");
649
+ article.className = "question-card";
650
+ article.dataset.questionId = question.id;
651
+ const heading = document.createElement("div");
652
+ heading.className = "question-card-row";
653
+ const prompt = document.createElement("p");
654
+ prompt.className = "question-card-prompt";
655
+ prompt.textContent = question.prompt;
656
+ heading.append(prompt);
657
+ const skip = document.createElement("button");
658
+ skip.type = "button";
659
+ skip.className = "btn-secondary";
660
+ skip.textContent = questionDrafts[question.id] === SKIP_ANSWER ? "Skipped" : "Skip";
661
+ skip.addEventListener("click", () => {
662
+ questionDrafts[question.id] = questionDrafts[question.id] === SKIP_ANSWER ? "" : SKIP_ANSWER;
663
+ render();
664
+ });
665
+ heading.append(skip);
666
+ article.append(heading);
667
+ if (question.kind === "choice") {
668
+ const choices = document.createElement("div");
669
+ choices.className = "question-choices";
670
+ (question.options ?? []).forEach((option, index) => {
671
+ const btn = document.createElement("button");
672
+ btn.type = "button";
673
+ btn.className = "stack-choice";
674
+ btn.dataset.optionId = option.id;
675
+ btn.classList.toggle("is-selected", questionDrafts[question.id] === option.id);
676
+ const label = document.createElement("strong");
677
+ label.textContent = option.label;
678
+ btn.append(label);
679
+ if (index === 0) {
680
+ const suggested = document.createElement("span");
681
+ suggested.className = "question-suggested";
682
+ suggested.textContent = "Suggested";
683
+ btn.append(suggested);
684
+ }
685
+ btn.addEventListener("click", () => {
686
+ questionDrafts[question.id] = option.id;
687
+ render();
688
+ });
689
+ choices.append(btn);
690
+ });
691
+ article.append(choices);
692
+ if (question.allowCustom) {
693
+ const input = document.createElement("input");
694
+ input.className = "input";
695
+ input.type = "text";
696
+ const current = questionDrafts[question.id] ?? "";
697
+ const optionIds = new Set((question.options ?? []).map((option) => option.id));
698
+ input.value = optionIds.has(current) || current === SKIP_ANSWER ? "" : current;
699
+ input.placeholder = "Something else";
700
+ input.addEventListener("input", () => {
701
+ questionDrafts[question.id] = input.value;
702
+ paintQuestionContinue();
703
+ });
704
+ article.append(input);
705
+ }
706
+ } else if (question.kind === "artifact") {
707
+ const file = document.createElement("input");
708
+ file.className = "input";
709
+ file.type = "file";
710
+ file.accept = (question.accept ?? ["image/png", "image/jpeg", "image/webp"]).join(",");
711
+ const status = document.createElement("p");
712
+ status.className = "question-card-meta";
713
+ const current = questionDrafts[question.id] ?? "";
714
+ status.textContent =
715
+ current && current !== SKIP_ANSWER ? `Saved context/${current}` : "Optional. PNG, JPG, or WebP.";
716
+ file.addEventListener("change", () => {
717
+ const picked = file.files?.[0];
718
+ if (!picked) return;
719
+ void uploadAuthorArtifact(question.id, picked, status);
720
+ });
721
+ article.append(file, status);
722
+ } else {
723
+ const input = document.createElement("input");
724
+ input.className = "input";
725
+ input.type = "text";
726
+ input.value = questionDrafts[question.id] === SKIP_ANSWER ? "" : (questionDrafts[question.id] ?? "");
727
+ input.placeholder = question.required ? "Required" : "Optional";
728
+ input.addEventListener("input", () => {
729
+ questionDrafts[question.id] = input.value;
730
+ paintQuestionContinue();
731
+ });
732
+ article.append(input);
733
+ }
734
+ if (question.required) {
735
+ const meta = document.createElement("p");
736
+ meta.className = "question-card-meta";
737
+ meta.textContent = "Required";
738
+ article.append(meta);
739
+ }
740
+ return article;
741
+ }
742
+
743
+ function renderQuestions() {
744
+ const round = openQuestions();
745
+ const askingDefine = round?.channel === "improve" || round?.channel === "author";
746
+ if (els.questionActions) els.questionActions.classList.toggle("hidden", !askingDefine);
747
+ if (els.defineQuestions) {
748
+ els.defineQuestions.classList.toggle("hidden", !askingDefine);
749
+ els.defineQuestions.hidden = !askingDefine;
750
+ if (askingDefine) fillQuestionPanel(els.defineQuestions, round);
751
+ else {
752
+ els.defineQuestions.innerHTML = "";
753
+ delete els.defineQuestions.dataset.sig;
754
+ }
755
+ }
756
+ if (askingDefine && round?.channel === "improve") {
757
+ els.workspace?.classList.add("is-review");
758
+ markdownEditor.setHidden(true);
759
+ if (els.save) els.save.classList.add("hidden");
760
+ if (els.workspaceLabel) els.workspaceLabel.textContent = "Agent questions";
761
+ }
762
+ paintActionButton(els.questionContinue, {
763
+ id: "answer-questions",
764
+ label: "Continue",
765
+ hint: "Answer each required question, then Continue. You may skip a question or all of them.",
766
+ enabled: Boolean(askingDefine && questionsContinueEnabled(round) && !actionBusy),
767
+ primary: true,
768
+ hidden: !askingDefine,
769
+ });
770
+ paintActionButton(els.questionSkipAll, {
771
+ id: "skip-all-questions",
772
+ label: "Skip all",
773
+ hint: "Skip this round. The agent will not assume answers.",
774
+ enabled: Boolean(askingDefine && !actionBusy),
775
+ primary: false,
776
+ hidden: !askingDefine,
777
+ });
778
+ paintActionButton(els.questionStop, {
779
+ id: "discard-questions",
780
+ label: "Stop",
781
+ hint: "Stop and discard unanswered questions.",
782
+ enabled: Boolean(askingDefine && !actionBusy),
783
+ primary: false,
784
+ hidden: !askingDefine,
785
+ });
786
+ if (els.implementQuestions) {
787
+ if (round?.channel === "implement") fillQuestionPanel(els.implementQuestions, round);
788
+ else {
789
+ els.implementQuestions.innerHTML = "";
790
+ delete els.implementQuestions.dataset.sig;
791
+ }
792
+ }
793
+ }
794
+
795
+ function offeringAuthor() {
796
+ return state?.health?.nextAction?.id === "author-start";
797
+ }
798
+
799
+ function authoringActive() {
800
+ const status = state?.authorRun?.status;
801
+ return status === "running" || status === "waiting_questions" || state?.busy === "author";
802
+ }
803
+
804
+ function renderAuthorDescribe() {
805
+ const offer = offeringAuthor();
806
+ if (els.authorDescribe) {
807
+ els.authorDescribe.classList.toggle("hidden", !offer);
808
+ els.authorDescribe.hidden = !offer;
809
+ }
810
+ if (els.authorStart) {
811
+ paintActionButton(els.authorStart, {
812
+ id: "author-start",
813
+ label: "Start",
814
+ hint: "The agent will draft the requirement from this description.",
815
+ enabled: Boolean(offer && String(els.authorDescription?.value ?? "").trim() && !actionBusy),
816
+ primary: true,
817
+ hidden: !offer,
818
+ });
819
+ }
820
+ }
821
+
822
+ function renderDefineLayout() {
823
+ const askingAuthor = openQuestions()?.channel === "author";
824
+ const hasActivity = authorLines.length > 0;
825
+ const showTabs = Boolean(askingAuthor && hasActivity);
826
+ if (!askingAuthor) defineTab = "questions";
827
+ else if (!hasActivity) defineTab = "questions";
828
+ els.defineTabs?.classList.toggle("hidden", !showTabs);
829
+ if (els.defineTabs) els.defineTabs.hidden = !showTabs;
830
+ if (askingAuthor && els.defineQuestions) {
831
+ const showQuestions = !showTabs || defineTab === "questions";
832
+ els.defineQuestions.classList.toggle("hidden", !showQuestions);
833
+ els.defineQuestions.hidden = !showQuestions;
834
+ }
835
+ const activityVisible = Boolean(authoringActive() && (!askingAuthor || (showTabs && defineTab === "activity")));
836
+ els.defineActivity?.classList.toggle("hidden", !activityVisible);
837
+ if (els.defineActivity) els.defineActivity.hidden = !activityVisible;
838
+ els.defineTabQuestions?.classList.toggle("is-active", defineTab !== "activity");
839
+ els.defineTabActivity?.classList.toggle("is-active", defineTab === "activity");
840
+ els.defineTabQuestions?.setAttribute("aria-selected", defineTab !== "activity" ? "true" : "false");
841
+ els.defineTabActivity?.setAttribute("aria-selected", defineTab === "activity" ? "true" : "false");
842
+ if (askingAuthor && els.workspaceLabel) els.workspaceLabel.textContent = "Agent questions";
843
+ }
844
+
845
+ function renderImplementLayout() {
846
+ const asking = openQuestions()?.channel === "implement";
847
+ const hasActivity = implementLines.length > 0;
848
+ const showTabs = Boolean(asking && hasActivity);
849
+ if (!asking) implementTab = "questions";
850
+ else if (!hasActivity) implementTab = "questions";
851
+ els.implementTabs?.classList.toggle("hidden", !showTabs);
852
+ if (els.implementTabs) els.implementTabs.hidden = !showTabs;
853
+ const showQuestions = Boolean(asking && (!showTabs || implementTab === "questions"));
854
+ const showActivity = !asking || (showTabs && implementTab === "activity");
855
+ els.implementActivityLabel?.classList.toggle("hidden", Boolean(asking));
856
+ els.implementQuestions?.classList.toggle("hidden", !showQuestions);
857
+ if (els.implementQuestions) els.implementQuestions.hidden = !showQuestions;
858
+ els.implementActivity?.classList.toggle("hidden", !showActivity);
859
+ if (els.implementActivity) els.implementActivity.hidden = !showActivity;
860
+ els.implementTabQuestions?.classList.toggle("is-active", implementTab !== "activity");
861
+ els.implementTabActivity?.classList.toggle("is-active", implementTab === "activity");
862
+ els.implementTabQuestions?.setAttribute("aria-selected", implementTab !== "activity" ? "true" : "false");
863
+ els.implementTabActivity?.setAttribute("aria-selected", implementTab === "activity" ? "true" : "false");
864
+ }
865
+
426
866
  function renderReview() {
427
- const review = state.improveReview;
867
+ const askingDefine = openQuestions()?.channel === "improve";
868
+ const review = askingDefine ? null : state.improveReview;
428
869
  const reviewing = Boolean(review);
429
870
  const hasDiff = Boolean(review?.files?.length);
430
- els.workspace?.classList.toggle("is-review", reviewing);
431
- markdownEditor.setHidden(reviewing);
432
- if (els.save) els.save.classList.toggle("hidden", reviewing);
871
+ if (!askingDefine) {
872
+ els.workspace?.classList.toggle("is-review", reviewing);
873
+ markdownEditor.setHidden(reviewing);
874
+ if (els.save) els.save.classList.toggle("hidden", reviewing);
875
+ }
433
876
  if (!els.reviewActions || !els.reviewDiff) return;
434
877
  els.reviewActions.classList.toggle("hidden", !reviewing);
435
878
  els.reviewDiff.classList.toggle("hidden", !reviewing);
436
879
  els.reviewDiff.hidden = !reviewing;
437
- if (els.workspaceLabel) {
880
+ if (els.workspaceLabel && !askingDefine) {
438
881
  els.workspaceLabel.textContent = hasDiff ? "Improve patch" : reviewing ? "Waiting for the patch" : "requirement.md";
439
882
  }
440
883
  const lintOk = review?.lint?.ok !== false;
@@ -531,13 +974,13 @@ function actionGlyph(id, label = "") {
531
974
  `<path d="M8 2.6 9 6l3.4 1L9 8l-1 3.4L7 8 3.6 7 7 6zM12.4 10.2l.5 1.6 1.6.5-1.6.5-.5 1.6-.5-1.6-1.6-.5 1.6-.5z" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linejoin="round"/>`,
532
975
  );
533
976
  }
534
- if (id === "review-improve") {
977
+ if (id === "review-improve" || id === "answer-questions") {
535
978
  return iconSvg(
536
979
  `<path d="M2.8 8.2 6.1 11.4 13.2 4.4" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/>`,
537
980
  );
538
981
  }
539
- if (id === "reject-improve" || id === "stop-implement") {
540
- return id === "stop-implement"
982
+ if (id === "reject-improve" || id === "stop-implement" || id === "discard-questions") {
983
+ return id === "stop-implement" || id === "discard-questions"
541
984
  ? iconSvg(`<rect x="4.2" y="4.2" width="7.6" height="7.6" rx="1.2" fill="currentColor"/>`)
542
985
  : iconSvg(
543
986
  `<path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/>`,
@@ -1189,9 +1632,11 @@ function fillActivityLog(log, lines, emptyMessage, which) {
1189
1632
  if (!log) return;
1190
1633
  const rows = lines.length ? lines : [{ level: "info", message: emptyMessage }];
1191
1634
  const key = rows.map((line) => `${line.level}\0${line.message}`).join("\n");
1192
- const previous = which === "prove" ? lastProveKey : lastImplementKey;
1635
+ const previous =
1636
+ which === "prove" ? lastProveKey : which === "author" ? lastAuthorKey : lastImplementKey;
1193
1637
  if (key === previous && log.childElementCount === rows.length) return;
1194
1638
  if (which === "prove") lastProveKey = key;
1639
+ else if (which === "author") lastAuthorKey = key;
1195
1640
  else lastImplementKey = key;
1196
1641
  const pinned = log.scrollHeight - log.scrollTop - log.clientHeight < 16;
1197
1642
  const savedTop = log.scrollTop;
@@ -1214,10 +1659,12 @@ function renderActivity() {
1214
1659
  "implement",
1215
1660
  );
1216
1661
  fillActivityLog(els.qaActivity, proveLines, "No activity yet. Prove writes progress here.", "prove");
1662
+ fillActivityLog(els.defineActivity, authorLines, "No activity yet. Authoring writes progress here.", "author");
1217
1663
  }
1218
1664
 
1219
1665
  function appendActivity(line, channel) {
1220
1666
  if (channel === "prove") proveLines = [...proveLines, line].slice(-80);
1667
+ else if (channel === "author") authorLines = [...authorLines, line].slice(-80);
1221
1668
  else implementLines = [...implementLines, line].slice(-80);
1222
1669
  if (line.message) {
1223
1670
  actionNotice = line.message;
@@ -1274,6 +1721,18 @@ onEnabledClick(els.reviewAccept, () => {
1274
1721
  onEnabledClick(els.reviewReject, () => {
1275
1722
  void runNextAction("Rejecting…", "/api/improve/reject");
1276
1723
  });
1724
+ onEnabledClick(els.questionContinue, () => {
1725
+ void runStageAction("answer-questions");
1726
+ });
1727
+ onEnabledClick(els.questionStop, () => {
1728
+ void runStageAction("discard-questions");
1729
+ });
1730
+ onEnabledClick(els.questionSkipAll, () => {
1731
+ void runStageAction("skip-all-questions");
1732
+ });
1733
+ onEnabledClick(els.authorStart, () => {
1734
+ void runStageAction("author-start");
1735
+ });
1277
1736
  onEnabledClick(els.next, () => {
1278
1737
  void runStageAction(state.health?.nextAction?.id);
1279
1738
  });
@@ -1310,9 +1769,57 @@ function askIgnoreBuild() {
1310
1769
  });
1311
1770
  }
1312
1771
 
1772
+ let discardDialogResolve = null;
1773
+
1774
+ function closeDiscardDialog(value) {
1775
+ if (els.discardDialog?.open) els.discardDialog.close(value);
1776
+ }
1777
+
1778
+ function askDiscardQuestions() {
1779
+ if (!els.discardDialog) return Promise.resolve(false);
1780
+ return new Promise((resolve) => {
1781
+ discardDialogResolve = resolve;
1782
+ const onClose = () => {
1783
+ els.discardDialog.removeEventListener("close", onClose);
1784
+ const answer = els.discardDialog.returnValue;
1785
+ const done = discardDialogResolve;
1786
+ discardDialogResolve = null;
1787
+ done?.(answer === "confirm");
1788
+ };
1789
+ els.discardDialog.addEventListener("close", onClose);
1790
+ els.discardDialog.returnValue = "";
1791
+ els.discardDialog.showModal();
1792
+ });
1793
+ }
1794
+
1795
+ async function discardOpenQuestions() {
1796
+ actionNotice = "Discarding questions…";
1797
+ setActionHint(actionNotice);
1798
+ try {
1799
+ const res = await fetch("/api/agent-questions/discard", {
1800
+ method: "POST",
1801
+ headers: { "Content-Type": "application/json" },
1802
+ body: "{}",
1803
+ });
1804
+ const payload = await res.json();
1805
+ if (payload.health || payload.packId) state = { ...state, ...payload };
1806
+ applyActivities(payload);
1807
+ actionNotice = payload.error ?? payload.message ?? "Discarded the agent's questions.";
1808
+ } catch (err) {
1809
+ actionNotice = err instanceof Error ? err.message : "Could not discard questions.";
1810
+ }
1811
+ setActionHint(actionNotice);
1812
+ render();
1813
+ }
1814
+
1313
1815
  function viewForAction(id) {
1314
1816
  if (id === "check-jev") return "judgment";
1315
- if (id === "improve" || id === "review-improve" || id === "fix-spec" || id === "create") return "define";
1817
+ if (id === "improve" || id === "review-improve" || id === "fix-spec" || id === "create" || id === "author-start" || id === "stop-author") {
1818
+ return "define";
1819
+ }
1820
+ if (id === "answer-questions" || id === "discard-questions" || id === "skip-all-questions") {
1821
+ return openQuestions()?.channel === "implement" ? "implement" : "define";
1822
+ }
1316
1823
  if (id === "implement" || id === "stop-implement" || id === "ignore-build" || id === "fix-from-proof") {
1317
1824
  return state?.health?.build?.owned === false ? null : "implement";
1318
1825
  }
@@ -1332,9 +1839,61 @@ async function runStageAction(id) {
1332
1839
  }
1333
1840
  const res = await fetch("/api/create-in-place", { method: "POST" });
1334
1841
  takeState(await res.json());
1335
- focusedSection = "Business Rules";
1842
+ currentView = "define";
1336
1843
  render();
1337
- jumpToSection("Business Rules");
1844
+ return;
1845
+ }
1846
+ if (id === "author-start") {
1847
+ const description = String(els.authorDescription?.value ?? "").trim();
1848
+ if (!description) return;
1849
+ authorLines = [{ level: "info", message: "Authoring started." }];
1850
+ actionNotice = "Authoring started.";
1851
+ setActionHint(actionNotice);
1852
+ renderActivity();
1853
+ await runBackgroundAction(
1854
+ "Authoring…",
1855
+ "/api/author/start",
1856
+ { description, adapter: recordedAdapter() },
1857
+ "author",
1858
+ );
1859
+ return;
1860
+ }
1861
+ if (id === "stop-author") {
1862
+ if (openQuestions()?.channel === "author") {
1863
+ const confirmed = await askDiscardQuestions();
1864
+ if (!confirmed) return;
1865
+ }
1866
+ actionNotice = "Stopping…";
1867
+ setActionHint(actionNotice);
1868
+ try {
1869
+ const res = await fetch("/api/author/stop", {
1870
+ method: "POST",
1871
+ headers: { "Content-Type": "application/json" },
1872
+ body: "{}",
1873
+ });
1874
+ const payload = await res.json();
1875
+ if (payload.health || payload.packId) state = { ...state, ...payload };
1876
+ applyActivities(payload);
1877
+ actionNotice = payload.error ?? payload.message ?? "Owner stopped authoring.";
1878
+ } catch (err) {
1879
+ actionNotice = err instanceof Error ? err.message : "Stop failed.";
1880
+ }
1881
+ setActionHint(actionNotice);
1882
+ render();
1883
+ return;
1884
+ }
1885
+ if (id === "skip-all-questions") {
1886
+ const round = openQuestions();
1887
+ if (!round) return;
1888
+ const kind = round.channel === "author" ? "author" : round.channel === "improve" ? "improve" : "implement";
1889
+ actionNotice = "Skipping this round…";
1890
+ setActionHint(actionNotice);
1891
+ await runBackgroundAction(
1892
+ "Continuing…",
1893
+ "/api/agent-answers",
1894
+ { answers: collectedAnswers(round), skipAll: true, adapter: recordedAdapter() },
1895
+ kind,
1896
+ );
1338
1897
  return;
1339
1898
  }
1340
1899
  if (id === "fix-spec") {
@@ -1354,11 +1913,35 @@ async function runStageAction(id) {
1354
1913
  await runNextAction("Accepting…", "/api/improve/accept");
1355
1914
  return;
1356
1915
  }
1916
+ if (id === "answer-questions") {
1917
+ const round = openQuestions();
1918
+ if (!round || !questionsContinueEnabled(round)) return;
1919
+ const kind = round.channel === "author" ? "author" : round.channel === "improve" ? "improve" : "implement";
1920
+ actionNotice = "Continuing with owner answers…";
1921
+ setActionHint(actionNotice);
1922
+ await runBackgroundAction(
1923
+ "Continuing…",
1924
+ "/api/agent-answers",
1925
+ { answers: collectedAnswers(round), adapter: recordedAdapter() },
1926
+ kind,
1927
+ );
1928
+ return;
1929
+ }
1930
+ if (id === "discard-questions") {
1931
+ const confirmed = await askDiscardQuestions();
1932
+ if (!confirmed) return;
1933
+ await discardOpenQuestions();
1934
+ return;
1935
+ }
1357
1936
  if (id === "check-jev") {
1358
1937
  await runNextAction("Checking…", "/api/check-jev");
1359
1938
  return;
1360
1939
  }
1361
1940
  if (id === "stop-implement") {
1941
+ if (openQuestions()?.channel === "implement") {
1942
+ const confirmed = await askDiscardQuestions();
1943
+ if (!confirmed) return;
1944
+ }
1362
1945
  actionNotice = "Stopping…";
1363
1946
  setActionHint(actionNotice);
1364
1947
  try {
@@ -1474,17 +2057,16 @@ async function waitWhileBusy(kind) {
1474
2057
  try {
1475
2058
  const res = await fetch("/api/state");
1476
2059
  const data = await res.json();
1477
- if (data.health || data.packId || data.improveReview) state = { ...state, ...data };
2060
+ if (data.health || data.packId || data.improveReview || data.agentQuestions) state = { ...state, ...data };
1478
2061
  applyActivities(data);
1479
- if (data.improveReview?.files?.length) {
2062
+ if (data.improveReview?.files?.length || data.agentQuestions?.status === "open") {
1480
2063
  actionNotice = "";
1481
- render();
1482
- return;
1483
- }
1484
- const last = lastBusyMessage(kind === "prove" ? "prove" : "implement");
1485
- if (last && !data.improveReview) {
1486
- actionNotice = last;
1487
- setActionHint(last);
2064
+ } else {
2065
+ const last = lastBusyMessage(kind);
2066
+ if (last && !data.improveReview) {
2067
+ actionNotice = last;
2068
+ setActionHint(last);
2069
+ }
1488
2070
  }
1489
2071
  render();
1490
2072
  if (data.busy !== kind) return;
@@ -1499,11 +2081,11 @@ async function refreshActivity() {
1499
2081
  try {
1500
2082
  const res = await fetch("/api/state");
1501
2083
  const data = await res.json();
1502
- if (data.health || data.packId || data.improveReview) state = { ...state, ...data };
2084
+ if (data.health || data.packId || data.improveReview || data.agentQuestions) state = { ...state, ...data };
1503
2085
  applyActivities(data);
1504
- if (data.improveReview?.files?.length) actionNotice = "";
2086
+ if (data.improveReview?.files?.length || data.agentQuestions?.status === "open") actionNotice = "";
1505
2087
  else {
1506
- const last = lastBusyMessage(data.busy === "prove" ? "prove" : "implement");
2088
+ const last = lastBusyMessage(data.busy === "prove" ? "prove" : data.busy === "author" ? "author" : "implement");
1507
2089
  if (last) {
1508
2090
  actionNotice = last;
1509
2091
  setActionHint(last);
@@ -1540,7 +2122,8 @@ async function runBackgroundAction(busyLabel, url, body, kind) {
1540
2122
  applyActivities(payload);
1541
2123
  if (
1542
2124
  (Array.isArray(payload.implementActivity) && payload.implementActivity.length) ||
1543
- (Array.isArray(payload.proveActivity) && payload.proveActivity.length)
2125
+ (Array.isArray(payload.proveActivity) && payload.proveActivity.length) ||
2126
+ (Array.isArray(payload.authorActivity) && payload.authorActivity.length)
1544
2127
  ) {
1545
2128
  renderActivity();
1546
2129
  }
@@ -1691,6 +2274,28 @@ els.qaTabReport?.addEventListener("click", () => {
1691
2274
  qaTab = "report";
1692
2275
  renderQaLayout();
1693
2276
  });
2277
+ els.implementTabQuestions?.addEventListener("click", () => {
2278
+ implementTab = "questions";
2279
+ renderImplementLayout();
2280
+ });
2281
+ els.implementTabActivity?.addEventListener("click", () => {
2282
+ implementTab = "activity";
2283
+ renderImplementLayout();
2284
+ });
2285
+ els.defineTabQuestions?.addEventListener("click", () => {
2286
+ defineTab = "questions";
2287
+ renderDefineLayout();
2288
+ });
2289
+ els.defineTabActivity?.addEventListener("click", () => {
2290
+ defineTab = "activity";
2291
+ renderDefineLayout();
2292
+ });
2293
+ els.authorDescription?.addEventListener("input", () => {
2294
+ paintQuestionContinue();
2295
+ });
2296
+ els.discardCancel?.addEventListener("click", () => closeDiscardDialog("cancel"));
2297
+ els.discardConfirm?.addEventListener("click", () => closeDiscardDialog("confirm"));
2298
+ els.discardDialog?.addEventListener("cancel", () => closeDiscardDialog("cancel"));
1694
2299
  els.allRequirements?.addEventListener("click", () => void goHome());
1695
2300
  els.packMenu?.addEventListener("contextmenu", (event) => event.preventDefault());
1696
2301
  els.packMenuDelete?.addEventListener("click", () => openDeleteDialog());
@@ -1729,8 +2334,8 @@ els.createNew.addEventListener("click", async () => {
1729
2334
  currentView = "define";
1730
2335
  actionNotice = "";
1731
2336
  if (els.newId) els.newId.value = "";
2337
+ if (els.authorDescription) els.authorDescription.value = "";
1732
2338
  render();
1733
- jumpToSection("Business Rules");
1734
2339
  });
1735
2340
 
1736
2341
  let pollTimer = null;