geneseed 5.0.1 → 5.0.4

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 (185) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/README.md +4 -4
  3. package/SHIPPED.md +2 -2
  4. package/adapters/claude-code/README.md +11 -5
  5. package/adapters/openclaude/README.md +3 -1
  6. package/adapters/opencode/HOW-OPENCODE-LOADS.md +3 -0
  7. package/adapters/opencode/plugins/geneseed-context.js +12 -1
  8. package/adapters/opencode/plugins/geneseed-guard.js +594 -73
  9. package/adapters/opencode/plugins/geneseed-learn.js +1 -1
  10. package/adapters/opencode/plugins/geneseed-workflow.js +1 -1
  11. package/adapters/opencode/workflows/_runtime.js +1 -1
  12. package/bin/geneseed-cli.mjs +3 -0
  13. package/bin/geneseed-hook.mjs +31 -9
  14. package/docs/PRD.md +1 -1
  15. package/docs/README.md +4 -1
  16. package/docs/concepts/enforcement-table.md +23 -4
  17. package/docs/concepts/exclusions.md +48 -2
  18. package/docs/concepts/factories.md +151 -0
  19. package/docs/concepts/foreman-mode.md +19 -4
  20. package/docs/concepts/hook-gates.md +1 -1
  21. package/docs/concepts/hooks.md +53 -12
  22. package/docs/concepts/host-matrix.md +5 -3
  23. package/docs/concepts/hosts.md +1 -1
  24. package/docs/concepts/kits.md +18 -0
  25. package/docs/concepts/loop-gates.md +3 -0
  26. package/docs/concepts/loops.md +1 -0
  27. package/docs/concepts/rules.md +1 -1
  28. package/docs/concepts/shared-tier.md +1 -1
  29. package/docs/concepts/skills.md +1 -1
  30. package/docs/extending.md +8 -2
  31. package/docs/guides/autostart.md +29 -3
  32. package/docs/guides/choose-your-setup.md +1 -1
  33. package/docs/guides/migrate.md +1 -1
  34. package/docs/guides/run-a-factory.md +88 -0
  35. package/docs/guides/set-up-ownership.md +1 -0
  36. package/docs/guides/uninstall.md +1 -1
  37. package/docs/reference/factory-actions.md +22 -0
  38. package/docs/reference/loop-actions.md +1 -1
  39. package/docs/reference/plugin-activity.md +1 -1
  40. package/docs/reference/plugin-context.md +1 -1
  41. package/docs/reference/plugin-guard.md +24 -5
  42. package/docs/reference/troubleshoot-doctor.md +2 -2
  43. package/docs/understand/a-day.md +9 -5
  44. package/docs/web-ui.md +101 -17
  45. package/harness.config.json +1 -1
  46. package/js/README.md +21 -3
  47. package/js/build/args.mjs +14 -2
  48. package/js/build/emit-claude.mjs +9 -1
  49. package/js/build/emit-opencode.mjs +2 -1
  50. package/js/build/source.mjs +28 -3
  51. package/js/cli-table.json +287 -3
  52. package/js/factory/cli.mjs +153 -0
  53. package/js/factory/graph.mjs +245 -0
  54. package/js/factory/registry.mjs +71 -0
  55. package/js/factory/state.mjs +429 -0
  56. package/js/hosts/activity.mjs +3 -3
  57. package/js/hosts/geneseed-dir.mjs +2 -2
  58. package/js/hosts/gitref.mjs +21 -0
  59. package/js/hosts/hooks-context.mjs +71 -15
  60. package/js/hosts/hooks-done.mjs +3 -1
  61. package/js/hosts/hooks-learn.mjs +1 -1
  62. package/js/hosts/hooks-prims.mjs +271 -1
  63. package/js/hosts/hooks.mjs +409 -151
  64. package/js/hosts/hosts.mjs +22 -0
  65. package/js/hosts/installs.mjs +9 -5
  66. package/js/hosts/settings.mjs +128 -27
  67. package/js/hosts/shim.mjs +155 -7
  68. package/js/hosts/store-watch.mjs +36 -11
  69. package/js/inspect/checks-authoring.mjs +11 -1
  70. package/js/inspect/cost.mjs +91 -5
  71. package/js/inspect/diff.mjs +2 -1
  72. package/js/inspect/doctor.mjs +2 -1
  73. package/js/inspect/inventory.mjs +2 -1
  74. package/js/inspect/validate.mjs +1 -1
  75. package/js/loop/catalog.mjs +24 -12
  76. package/js/loop/cli.mjs +21 -15
  77. package/js/loop/graph.mjs +13 -4
  78. package/js/loop/registry.mjs +65 -28
  79. package/js/loop/state.mjs +18 -8
  80. package/js/maintain/migrate.mjs +2 -2
  81. package/js/maintain/uninstall.mjs +5 -1
  82. package/js/web/actions.mjs +17 -1
  83. package/js/web/api.mjs +52 -11
  84. package/js/web/catalog.mjs +38 -2
  85. package/js/web/daemon.mjs +136 -6
  86. package/js/web/handler.mjs +14 -5
  87. package/js/web/routes.mjs +12 -4
  88. package/js/web/server.mjs +4 -1
  89. package/package.json +1 -1
  90. package/registry.json +35 -0
  91. package/src/AGENT.md.tmpl +18 -24
  92. package/src/agents/_self-improvement.md +7 -6
  93. package/src/agents/developer.md +3 -2
  94. package/src/agents/docker-expert.md +1 -0
  95. package/src/agents/memory-curator.md +3 -2
  96. package/src/bricks/docs-update.md +5 -0
  97. package/src/bricks/holdout-write.md +23 -0
  98. package/src/bricks/holdout.md +25 -0
  99. package/src/bricks/intake.md +18 -0
  100. package/src/bricks/merge-gate.md +22 -0
  101. package/src/bricks/pr-open.md +21 -0
  102. package/src/bricks/retro.md +17 -0
  103. package/src/bricks/test.md +4 -1
  104. package/src/bricks/triage.md +19 -0
  105. package/src/doctrines/comms.md +29 -41
  106. package/src/doctrines/craft.md +3 -10
  107. package/src/doctrines/ops.md +4 -15
  108. package/src/doctrines/process.md +24 -19
  109. package/src/doctrines/rigor.md +1 -5
  110. package/src/factories/analysis-to-build.json +34 -0
  111. package/src/factories/bug-report-to-fix.json +32 -0
  112. package/src/factories/issue-to-pr.json +49 -0
  113. package/src/kits/ci-pipelines/rules.md +10 -0
  114. package/src/kits/ci-pipelines.json +14 -0
  115. package/src/kits/docker/rules.md +11 -0
  116. package/src/kits/docker.json +12 -0
  117. package/src/memory/README.md +5 -4
  118. package/src/modes/foreman.md +23 -10
  119. package/src/notebook/README.md +1 -1
  120. package/src/skills/_checkpoints.md +1 -0
  121. package/src/skills/_self-improvement.md +12 -9
  122. package/src/skills/adhd.md +1 -1
  123. package/src/skills/compose-ops.md +58 -0
  124. package/src/skills/factory-forge.md +97 -0
  125. package/src/skills/factory.md +126 -0
  126. package/src/skills/gitlab-ci.md +51 -0
  127. package/src/skills/jenkins-pipeline.md +56 -0
  128. package/src/skills/parallel-agents.md +1 -1
  129. package/src/skills/pipeline.md +2 -2
  130. package/src/skills/rule.md +24 -18
  131. package/src/skills/skill-forge.md +24 -6
  132. package/src/skills/workflow.md +2 -1
  133. package/themes/_TEMPLATE.json +9 -4
  134. package/themes/biker.json +9 -4
  135. package/themes/commentator.json +9 -4
  136. package/themes/cyberpunk.json +9 -4
  137. package/themes/gamer.json +9 -4
  138. package/themes/imperial.json +9 -4
  139. package/themes/joker.json +9 -4
  140. package/themes/marvin.json +9 -4
  141. package/themes/mean.json +9 -4
  142. package/themes/military.json +9 -4
  143. package/themes/neutral.json +9 -4
  144. package/themes/pirate.json +9 -4
  145. package/themes/sports.json +9 -4
  146. package/themes/verstappen.json +9 -4
  147. package/themes/wizard.json +9 -4
  148. package/web/dist/assets/ActiveLoops-BTHIaRhn.js +1 -0
  149. package/web/dist/assets/Activity-BaoxwVFH.js +1 -0
  150. package/web/dist/assets/ActivityDetail-C_bFD0rW.js +1 -0
  151. package/web/dist/assets/{Bonsai-C1iBAt4v.js → Bonsai-B01lLf0m.js} +1 -1
  152. package/web/dist/assets/{CopyButton-DJ3a4N88.js → CopyButton-Bc66KPyA.js} +1 -1
  153. package/web/dist/assets/Dashboard-X7GZzGJp.js +2 -0
  154. package/web/dist/assets/{Docs-Kffdg5uV.js → Docs-LRYVnGLG.js} +3 -3
  155. package/web/dist/assets/Factories-ARenKBiP.js +1 -0
  156. package/web/dist/assets/{GreenhouseView-k56_cBSF.js → GreenhouseView-CfWR2-sX.js} +1 -1
  157. package/web/dist/assets/{Installs-DtEKLC9h.js → Installs-nfFHnovH.js} +3 -3
  158. package/web/dist/assets/{JournalView-RQnk5l7I.js → JournalView-lpOHcHKK.js} +1 -1
  159. package/web/dist/assets/Kits-Bw5EQl_i.js +1 -0
  160. package/web/dist/assets/Laws-CnZJ0wjZ.js +4 -0
  161. package/web/dist/assets/LibRows-Hqvf1beK.js +1 -0
  162. package/web/dist/assets/Library-JaSsp6Yr.js +3 -0
  163. package/web/dist/assets/Loops-ZaHC5MVl.js +2 -0
  164. package/web/dist/assets/{OperatorHudView-CM5jNPwL.js → OperatorHudView-CggG0_sK.js} +1 -1
  165. package/web/dist/assets/Personal-BObvlb70.js +2 -0
  166. package/web/dist/assets/Settings-BFfPMPc9.js +1 -0
  167. package/web/dist/assets/Tabs-B594Pad_.js +1 -0
  168. package/web/dist/assets/api-DLDVY3K-.js +1 -0
  169. package/web/dist/assets/index-DsLmKHw0.js +6 -0
  170. package/web/dist/assets/index-DvPjphz0.css +1 -0
  171. package/web/dist/index.html +3 -3
  172. package/web/dist/assets/Activity-Ci4PZco3.js +0 -1
  173. package/web/dist/assets/ActivityDetail-C8hroCND.js +0 -1
  174. package/web/dist/assets/Dashboard-C_On4KXw.js +0 -2
  175. package/web/dist/assets/Kits-CXkEBpnf.js +0 -1
  176. package/web/dist/assets/Laws-DhhIVqmh.js +0 -3
  177. package/web/dist/assets/Library-CAUiEoJo.js +0 -3
  178. package/web/dist/assets/Loops-LKD8mF4E.js +0 -2
  179. package/web/dist/assets/Personal-B6Uoly3C.js +0 -2
  180. package/web/dist/assets/RailCats-DQxxYBEP.js +0 -1
  181. package/web/dist/assets/Settings-BTe6-wda.js +0 -1
  182. package/web/dist/assets/Tabs-CbdDWTp_.js +0 -1
  183. package/web/dist/assets/api-D_r0Jtla.js +0 -1
  184. package/web/dist/assets/index-HfGmYXab.js +0 -3
  185. package/web/dist/assets/index-ao3M-Fhm.css +0 -1
package/js/cli-table.json CHANGED
@@ -339,6 +339,36 @@
339
339
  "is_flag": true,
340
340
  "type": null,
341
341
  "hidden": false
342
+ },
343
+ {
344
+ "names": [
345
+ "--no-external-gate"
346
+ ],
347
+ "dest": "no_external_gate",
348
+ "metavar": null,
349
+ "help": "switch External Gate's hook off for this install: protected checks and the gates' own configuration stop asking, on every host (the doctrine's text still binds unless excluded; OpenCode's own permission.edit keys stay); written to the carrier as 'External gate: off' and replayed by rebuilds",
350
+ "choices": null,
351
+ "default": false,
352
+ "required": false,
353
+ "nargs": "0",
354
+ "is_flag": true,
355
+ "type": null,
356
+ "hidden": false
357
+ },
358
+ {
359
+ "names": [
360
+ "--no-test-note"
361
+ ],
362
+ "dest": "no_test_note",
363
+ "metavar": null,
364
+ "help": "switch the weakened-test note off for this install, at edit time and at turn end (Cure the Cause's text still binds; the done checks still run); written to the carrier as 'Test note: off' and replayed by rebuilds",
365
+ "choices": null,
366
+ "default": false,
367
+ "required": false,
368
+ "nargs": "0",
369
+ "is_flag": true,
370
+ "type": null,
371
+ "hidden": false
342
372
  }
343
373
  ],
344
374
  "mutex": []
@@ -633,6 +663,157 @@
633
663
  "options": [],
634
664
  "mutex": []
635
665
  },
666
+ {
667
+ "name": "factory",
668
+ "help": "run a factory: carry one work item through a graph of bricks and whole loops to an open PR (check/list/init/next/record/decide)",
669
+ "description": "",
670
+ "positionals": [
671
+ {
672
+ "names": [],
673
+ "dest": "action",
674
+ "metavar": null,
675
+ "help": "",
676
+ "choices": [
677
+ "check",
678
+ "list",
679
+ "init",
680
+ "next",
681
+ "record",
682
+ "decide"
683
+ ],
684
+ "default": null,
685
+ "required": true,
686
+ "nargs": null,
687
+ "is_flag": false,
688
+ "type": null,
689
+ "hidden": false
690
+ }
691
+ ],
692
+ "options": [
693
+ {
694
+ "names": ["--factory"],
695
+ "dest": "factory",
696
+ "metavar": null,
697
+ "help": "a factory name or a .json factory file (check, init)",
698
+ "choices": null,
699
+ "default": null,
700
+ "required": false,
701
+ "nargs": null,
702
+ "is_flag": false,
703
+ "type": null,
704
+ "hidden": false
705
+ },
706
+ {
707
+ "names": ["--title"],
708
+ "dest": "title",
709
+ "metavar": null,
710
+ "help": "the run's title, written into FACTORY.md",
711
+ "choices": null,
712
+ "default": null,
713
+ "required": false,
714
+ "nargs": null,
715
+ "is_flag": false,
716
+ "type": null,
717
+ "hidden": false
718
+ },
719
+ {
720
+ "names": ["--requirement"],
721
+ "dest": "requirement",
722
+ "metavar": null,
723
+ "help": "the work item the run exists to carry to a PR",
724
+ "choices": null,
725
+ "default": null,
726
+ "required": false,
727
+ "nargs": null,
728
+ "is_flag": false,
729
+ "type": null,
730
+ "hidden": false
731
+ },
732
+ {
733
+ "names": ["--input"],
734
+ "dest": "input",
735
+ "metavar": null,
736
+ "help": "where the work item came from: an issue URL, a file path or pasted text (init)",
737
+ "choices": null,
738
+ "default": null,
739
+ "required": false,
740
+ "nargs": null,
741
+ "is_flag": false,
742
+ "type": null,
743
+ "hidden": false
744
+ },
745
+ {
746
+ "names": ["--outcome"],
747
+ "dest": "outcome",
748
+ "metavar": null,
749
+ "help": "the stage's reported outcome, one of its declared outcomes",
750
+ "choices": null,
751
+ "default": null,
752
+ "required": false,
753
+ "nargs": null,
754
+ "is_flag": false,
755
+ "type": null,
756
+ "hidden": false
757
+ },
758
+ {
759
+ "names": ["--spec"],
760
+ "dest": "spec",
761
+ "metavar": null,
762
+ "help": "the spec a stage wrote; its ordered tasks replay the next loop stage once each (record)",
763
+ "choices": null,
764
+ "default": null,
765
+ "required": false,
766
+ "nargs": null,
767
+ "is_flag": false,
768
+ "type": null,
769
+ "hidden": false
770
+ },
771
+ {
772
+ "names": ["--carry"],
773
+ "dest": "carry",
774
+ "metavar": null,
775
+ "help": "the reason the next stage must hear, such as why CI failed; a re-entered loop starts with it as a note (record)",
776
+ "choices": null,
777
+ "default": null,
778
+ "required": false,
779
+ "nargs": null,
780
+ "is_flag": false,
781
+ "type": null,
782
+ "hidden": false
783
+ },
784
+ {
785
+ "names": ["--verdict"],
786
+ "dest": "verdict",
787
+ "metavar": null,
788
+ "help": "the user's answer to a held gate",
789
+ "choices": [
790
+ "ok",
791
+ "no",
792
+ "amend"
793
+ ],
794
+ "default": null,
795
+ "required": false,
796
+ "nargs": null,
797
+ "is_flag": false,
798
+ "type": null,
799
+ "hidden": false
800
+ },
801
+ {
802
+ "names": ["--note"],
803
+ "dest": "note",
804
+ "metavar": null,
805
+ "help": "a note attached to a decide verdict; an amend hands it to the re-run stage",
806
+ "choices": null,
807
+ "default": null,
808
+ "required": false,
809
+ "nargs": null,
810
+ "is_flag": false,
811
+ "type": null,
812
+ "hidden": false
813
+ }
814
+ ],
815
+ "mutex": []
816
+ },
636
817
  {
637
818
  "name": "git-gate",
638
819
  "help": "PreToolUse hook: ASK before a history-discarding git act (Deletion Is Deliberate), a recursive scan of a whole filesystem (Commands Must Return) and every git commit/push (Consent Before Push backstop)",
@@ -698,6 +879,21 @@
698
879
  "is_flag": true,
699
880
  "type": null,
700
881
  "hidden": false
882
+ },
883
+ {
884
+ "names": [
885
+ "--no-external-gate"
886
+ ],
887
+ "dest": "no_external_gate",
888
+ "metavar": null,
889
+ "help": "skip the External Gate protected-check and gate-configuration asks (set by the emit when the install was built --no-external-gate); the deletion, root-scan, store and consent asks still run",
890
+ "choices": null,
891
+ "default": false,
892
+ "required": false,
893
+ "nargs": "0",
894
+ "is_flag": true,
895
+ "type": null,
896
+ "hidden": false
701
897
  }
702
898
  ],
703
899
  "mutex": []
@@ -774,6 +970,21 @@
774
970
  "is_flag": true,
775
971
  "type": null,
776
972
  "hidden": false
973
+ },
974
+ {
975
+ "names": [
976
+ "--no-test-note"
977
+ ],
978
+ "dest": "no_test_note",
979
+ "metavar": null,
980
+ "help": "skip the weakened-test note (set by the emit when the install was built --no-test-note); the done checks and learning still run",
981
+ "choices": null,
982
+ "default": false,
983
+ "required": false,
984
+ "nargs": "0",
985
+ "is_flag": true,
986
+ "type": null,
987
+ "hidden": false
777
988
  }
778
989
  ],
779
990
  "mutex": []
@@ -1021,7 +1232,7 @@
1021
1232
  "names": ["--note"],
1022
1233
  "dest": "note",
1023
1234
  "metavar": null,
1024
- "help": "a note attached to a record or a decide verdict, written into LOOP.md so it survives the unit",
1235
+ "help": "a note attached to a record or a decide verdict, written into LOOP.md so it survives the unit; on init, the note the loop starts with",
1025
1236
  "choices": null,
1026
1237
  "default": null,
1027
1238
  "required": false,
@@ -1034,7 +1245,20 @@
1034
1245
  "names": ["--note-file"],
1035
1246
  "dest": "note_file",
1036
1247
  "metavar": null,
1037
- "help": "record's note read from this file instead of the command line (CRLF folded, trimmed) — for a multi-line note or one that mentions git commit/push",
1248
+ "help": "record's or init's note read from this file instead of the command line (CRLF folded, trimmed) — for a multi-line note or one that mentions git commit/push",
1249
+ "choices": null,
1250
+ "default": null,
1251
+ "required": false,
1252
+ "nargs": null,
1253
+ "is_flag": false,
1254
+ "type": null,
1255
+ "hidden": false
1256
+ },
1257
+ {
1258
+ "names": ["--factory"],
1259
+ "dest": "factory",
1260
+ "metavar": null,
1261
+ "help": "on init, the slug of the factory run this loop is a stage of, stored on the loop's registry row",
1038
1262
  "choices": null,
1039
1263
  "default": null,
1040
1264
  "required": false,
@@ -1219,7 +1443,7 @@
1219
1443
  },
1220
1444
  {
1221
1445
  "name": "rule-gate",
1222
- "help": "PreToolUse hook: force an ASK before any write to user-rules.md or memory (Persist Insight backstop), and stop the first write of a session once in an application repo with no ## Ownership section in its root AGENTS.md (the ownership gate)",
1446
+ "help": "PreToolUse hook: force an ASK before any write to user-rules.md or the rule draft (Persist Insight backstop; memory is written freely), and stop the first write of a session once in an application repo with no ## Ownership section in its root AGENTS.md (the ownership gate)",
1223
1447
  "description": "",
1224
1448
  "positionals": [],
1225
1449
  "options": [
@@ -1282,6 +1506,36 @@
1282
1506
  "is_flag": true,
1283
1507
  "type": null,
1284
1508
  "hidden": false
1509
+ },
1510
+ {
1511
+ "names": [
1512
+ "--no-external-gate"
1513
+ ],
1514
+ "dest": "no_external_gate",
1515
+ "metavar": null,
1516
+ "help": "skip the External Gate protected-check and gate-configuration asks (set by the emit when the install was built --no-external-gate); every other check still runs",
1517
+ "choices": null,
1518
+ "default": false,
1519
+ "required": false,
1520
+ "nargs": "0",
1521
+ "is_flag": true,
1522
+ "type": null,
1523
+ "hidden": false
1524
+ },
1525
+ {
1526
+ "names": [
1527
+ "--no-test-note"
1528
+ ],
1529
+ "dest": "no_test_note",
1530
+ "metavar": null,
1531
+ "help": "skip the weakened-test note (set by the emit when the install was built --no-test-note); every other check still runs",
1532
+ "choices": null,
1533
+ "default": false,
1534
+ "required": false,
1535
+ "nargs": "0",
1536
+ "is_flag": true,
1537
+ "type": null,
1538
+ "hidden": false
1285
1539
  }
1286
1540
  ],
1287
1541
  "mutex": []
@@ -1567,6 +1821,36 @@
1567
1821
  "is_flag": true,
1568
1822
  "type": null,
1569
1823
  "hidden": false
1824
+ },
1825
+ {
1826
+ "names": [
1827
+ "--no-external-gate"
1828
+ ],
1829
+ "dest": "no_external_gate",
1830
+ "metavar": null,
1831
+ "help": "skip the External Gate protected-check and gate-configuration asks (set by the emit when the install was built --no-external-gate); every other check still runs",
1832
+ "choices": null,
1833
+ "default": false,
1834
+ "required": false,
1835
+ "nargs": "0",
1836
+ "is_flag": true,
1837
+ "type": null,
1838
+ "hidden": false
1839
+ },
1840
+ {
1841
+ "names": [
1842
+ "--no-test-note"
1843
+ ],
1844
+ "dest": "no_test_note",
1845
+ "metavar": null,
1846
+ "help": "skip the weakened-test note (set by the emit when the install was built --no-test-note); every other check still runs",
1847
+ "choices": null,
1848
+ "default": false,
1849
+ "required": false,
1850
+ "nargs": "0",
1851
+ "is_flag": true,
1852
+ "type": null,
1853
+ "hidden": false
1570
1854
  }
1571
1855
  ],
1572
1856
  "mutex": []
@@ -0,0 +1,153 @@
1
+ /**
2
+ * `geneseed factory <action>` — the factory engine's only face, built as `geneseed loop` is
3
+ * (`js/loop/cli.mjs`): one JSON object per action, exit 1 on any refusal, no spawn. git and
4
+ * `gh`/`glab` are the model's job; the porcelain reaches `record` on stdin, and a TTY stdin reads
5
+ * as `null` ("none given"), never as a clean tree.
6
+ *
7
+ * FACTORY.md LIVES AT THE GIT ROOT, beside LOOP.md, for the same reason LOOP.md does: the loop
8
+ * engine filters both out of every diff by their root-relative name (`ENGINE_FILES`). So the root
9
+ * is found by walking up to `.git`, never taken from the caller. The same root answers R3: the
10
+ * status in the LOOP.md there (`loopStatus`) goes to the engine, which refuses while it is neither
11
+ * `done` nor `stopped`, holds a loop stage's outcome to it (R2: stopped is fail, done is pass), and
12
+ * refuses `next` on a loop stage while any LOOP.md is left. A loop that ends on `$stop` leaves its
13
+ * LOOP.md in place, so without that read R2 could never be recorded. A LOOP.md that does not parse
14
+ * counts as running: refusing is the safe side, and `loop` itself refuses it too. Its graph name
15
+ * (`loopTemplate`) and the HEAD commit (`headCommit`, read off disk) go along: `next` on a loop
16
+ * stage stores the launch in FACTORY.md with the time now, so `next` writes, and `record` checks
17
+ * the loop against it — with the loop registry's last `loop init` time for this root
18
+ * (`loopInitAt`, a read of `loops.json`, no write), which a `pass` with no LOOP.md left needs.
19
+ *
20
+ * THE PORCELAIN IS TRUSTED AS PIPED. A TTY stdin is `null` and the engine refuses it after a
21
+ * stage outside a loop (J5); an empty pipe (or a null device) reads as a clean tree, and nothing
22
+ * here can tell it from one. The Rite pipes `git status --porcelain --untracked-files=all`, so a
23
+ * new spec reaches the engine as its own path, never folded into its directory.
24
+ *
25
+ * `--spec` IS READ HERE, not in the state module: the engine stays pure, and the CLI is the one
26
+ * layer that may read files. The path is stored root-relative with forward slashes, the form a
27
+ * porcelain line carries it in, and a path outside the root is refused before it is read.
28
+ */
29
+ import { readFileSync } from 'node:fs';
30
+ import path from 'node:path';
31
+ import { readText, isFile, printOut } from '../lib/fs.mjs';
32
+ import { gitRootOf, headCommit, currentBranch } from '../hosts/gitref.mjs';
33
+ import { loadCatalog } from '../loop/catalog.mjs';
34
+ import { LOOP_FILE, parseLoopFile } from '../loop/state.mjs';
35
+ import { loopInitAt } from '../loop/registry.mjs';
36
+ import { recordFactory } from './registry.mjs';
37
+ import { checkFactory, factoryCatalogProblems } from './graph.mjs';
38
+ import {
39
+ FACTORY_FILE, initFactory, nextStage, recordStage, decideFactory, writeFactoryFile, parseFactoryFile,
40
+ validateFactoryState, countSpecTasks,
41
+ } from './state.mjs';
42
+
43
+ const emit = (obj) => { printOut(`${JSON.stringify(obj)}\n`); return obj.error || obj.ok === false ? 1 : 0; };
44
+ const stdin = () => (process.stdin.isTTY ? null : readFileSync(0, 'utf8'));
45
+ const byName = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
46
+
47
+ /** R3: the LOOP.md at `root` — its status (`running` when it does not parse) and graph name; nulls without one. */
48
+ function loopFileOf(root) {
49
+ const file = path.join(root, LOOP_FILE);
50
+ if (!isFile(file)) return { loopStatus: null, loopTemplate: null };
51
+ try {
52
+ const loop = parseLoopFile(readText(file));
53
+ return { loopStatus: String(loop?.status ?? 'running'), loopTemplate: loop?.graph?.name ?? null };
54
+ } catch { return { loopStatus: 'running', loopTemplate: null }; }
55
+ }
56
+
57
+ function requireRoot() {
58
+ const root = gitRootOf(process.cwd());
59
+ if (!root) throw new Error('not inside a git repository');
60
+ return root;
61
+ }
62
+
63
+ /**
64
+ * R4: an unreadable FACTORY.md throws in `parseFactoryFile` or `validateFactoryState`, before `fn`
65
+ * runs or anything is written.
66
+ */
67
+ function withState(fn) {
68
+ const root = requireRoot();
69
+ const file = path.join(root, FACTORY_FILE);
70
+ if (!isFile(file)) throw new Error(`${file} does not exist — run \`geneseed factory init\` first`);
71
+ const state = parseFactoryFile(readText(file));
72
+ const catalog = loadCatalog({ projectRoot: root });
73
+ validateFactoryState(state, catalog);
74
+ const result = fn(state, catalog, { root, head: headCommit(root), ...loopFileOf(root) });
75
+ writeFactoryFile(file, state);
76
+ return result;
77
+ }
78
+
79
+ function resolveFactory(spec, catalog) {
80
+ if (spec.endsWith('.json')) return JSON.parse(readText(path.resolve(spec)));
81
+ const f = catalog.factories.get(spec);
82
+ if (!f) throw new Error(`no factory named ${JSON.stringify(spec)}`);
83
+ const { origin, ...factory } = f;
84
+ return factory;
85
+ }
86
+
87
+ const ACTIONS = {
88
+ check(args) {
89
+ const root = gitRootOf(process.cwd()) ?? process.cwd();
90
+ const catalog = loadCatalog({ projectRoot: root });
91
+ if (args.factory) {
92
+ const problems = checkFactory(resolveFactory(args.factory, catalog), catalog);
93
+ return { ok: !problems.length, problems };
94
+ }
95
+ const problems = factoryCatalogProblems(catalog);
96
+ return { ok: !problems.length, problems, factories: [...catalog.factories.keys()].sort(byName) };
97
+ },
98
+ list() {
99
+ const root = gitRootOf(process.cwd()) ?? process.cwd();
100
+ const { factories } = loadCatalog({ projectRoot: root });
101
+ return {
102
+ factories: [...factories.values()].sort((a, b) => byName(a.name, b.name)).map((f) => ({
103
+ name: f.name, description: f.description ?? '', category: f.category ?? null, origin: f.origin,
104
+ })),
105
+ };
106
+ },
107
+ init(args) {
108
+ const root = requireRoot();
109
+ const file = path.join(root, FACTORY_FILE);
110
+ if (isFile(file)) throw new Error(`${file} already exists — a factory is already running here`);
111
+ if (!args.title || !args.requirement || !args.factory) throw new Error('init needs --title, --requirement and --factory');
112
+ const catalog = loadCatalog({ projectRoot: root });
113
+ const factory = resolveFactory(args.factory, catalog);
114
+ const problems = checkFactory(factory, catalog);
115
+ if (problems.length) return { error: 'the factory does not pass factory check', problems };
116
+ const state = initFactory({ title: args.title, requirement: args.requirement, factory, input: args.input ?? null });
117
+ writeFactoryFile(file, state);
118
+ // Best-effort, as `loop init`'s `recordLoop`: the registry is how the console finds the run.
119
+ recordFactory({ root, branch: currentBranch(root), factory: factory.name, title: args.title });
120
+ return { file, factory: factory.name, stage: state.stage, status: state.status };
121
+ },
122
+ next: () => withState((s, c, { loopStatus, head }) => nextStage(s, c, { loopStatus, head, now: new Date().toISOString() })),
123
+ record(args) {
124
+ if (!args.outcome) throw new Error('record needs --outcome');
125
+ const porcelain = stdin();
126
+ return withState((s, c, { root, loopStatus, loopTemplate, head }) => {
127
+ let specPath = null; let taskCount = null;
128
+ if (args.spec) {
129
+ const abs = path.resolve(root, args.spec);
130
+ const rel = path.relative(root, abs);
131
+ if (!rel || rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) throw new Error(`--spec ${args.spec} is outside the worktree`);
132
+ let text;
133
+ try { text = readText(abs); } catch (e) { throw new Error(`cannot read --spec ${args.spec}: ${e.message}`, { cause: e }); }
134
+ specPath = rel.replaceAll('\\', '/');
135
+ taskCount = countSpecTasks(text);
136
+ }
137
+ return recordStage(s, c, args.outcome, {
138
+ porcelain: porcelain === null ? null : porcelain.replace(/\r\n/g, '\n').trimEnd(),
139
+ specPath, taskCount, carry: args.carry ?? null, loopStatus, loopTemplate, head,
140
+ loopInitAt: loopInitAt(root),
141
+ });
142
+ });
143
+ },
144
+ decide: (args) => withState((s, c) => decideFactory(s, c, args.verdict, args.note ?? '')),
145
+ };
146
+
147
+ export function cmdFactory(args) {
148
+ try {
149
+ return emit(ACTIONS[args.action](args));
150
+ } catch (e) {
151
+ return emit({ error: e.message });
152
+ }
153
+ }