majlis 0.7.1 → 0.7.3

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 (2) hide show
  1. package/dist/cli.js +1734 -180
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -9,6 +9,9 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
9
  var __esm = (fn, res) => function __init() {
10
10
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
11
  };
12
+ var __commonJS = (cb, mod) => function __require() {
13
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
14
+ };
12
15
  var __export = (target, all) => {
13
16
  for (var name in all)
14
17
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -31,6 +34,116 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
31
34
  ));
32
35
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
36
 
37
+ // src/output/format.ts
38
+ function bold(s) {
39
+ return `${BOLD}${s}${RESET}`;
40
+ }
41
+ function dim(s) {
42
+ return `${DIM}${s}${RESET}`;
43
+ }
44
+ function red(s) {
45
+ return `${RED}${s}${RESET}`;
46
+ }
47
+ function green(s) {
48
+ return `${GREEN}${s}${RESET}`;
49
+ }
50
+ function yellow(s) {
51
+ return `${YELLOW}${s}${RESET}`;
52
+ }
53
+ function blue(s) {
54
+ return `${BLUE}${s}${RESET}`;
55
+ }
56
+ function cyan(s) {
57
+ return `${CYAN}${s}${RESET}`;
58
+ }
59
+ function statusColor(status2) {
60
+ switch (status2) {
61
+ case "merged":
62
+ return green(status2);
63
+ case "dead_end":
64
+ return red(status2);
65
+ case "building":
66
+ case "built":
67
+ return blue(status2);
68
+ case "verifying":
69
+ case "verified":
70
+ return cyan(status2);
71
+ case "classified":
72
+ case "reframed":
73
+ return dim(status2);
74
+ default:
75
+ return yellow(status2);
76
+ }
77
+ }
78
+ function evidenceColor(level) {
79
+ switch (level) {
80
+ case "proof":
81
+ return green(level);
82
+ case "test":
83
+ return cyan(level);
84
+ case "strong_consensus":
85
+ return blue(level);
86
+ case "consensus":
87
+ return blue(level);
88
+ case "analogy":
89
+ return yellow(level);
90
+ case "judgment":
91
+ return red(level);
92
+ default:
93
+ return level;
94
+ }
95
+ }
96
+ function table(headers, rows) {
97
+ const widths = headers.map(
98
+ (h, i) => Math.max(h.length, ...rows.map((r) => stripAnsi(r[i] ?? "").length))
99
+ );
100
+ const headerLine = headers.map((h, i) => h.padEnd(widths[i])).join(" ");
101
+ const separator = widths.map((w) => "\u2500".repeat(w)).join("\u2500\u2500");
102
+ const bodyLines = rows.map(
103
+ (row) => row.map((cell, i) => {
104
+ const stripped = stripAnsi(cell);
105
+ const padding = widths[i] - stripped.length;
106
+ return cell + " ".repeat(Math.max(0, padding));
107
+ }).join(" ")
108
+ );
109
+ return [bold(headerLine), separator, ...bodyLines].join("\n");
110
+ }
111
+ function stripAnsi(s) {
112
+ return s.replace(/\x1b\[[0-9;]*m/g, "");
113
+ }
114
+ function header(title) {
115
+ console.log(`
116
+ ${bold(`[majlis] ${title}`)}
117
+ `);
118
+ }
119
+ function warn(msg) {
120
+ console.log(`${yellow("[majlis]")} ${msg}`);
121
+ }
122
+ function info(msg) {
123
+ console.log(`${cyan("[majlis]")} ${msg}`);
124
+ }
125
+ function error(msg) {
126
+ console.error(`${red("[majlis]")} ${msg}`);
127
+ }
128
+ function success(msg) {
129
+ console.log(`${green("[majlis]")} ${msg}`);
130
+ }
131
+ var useColor, RESET, BOLD, DIM, RED, GREEN, YELLOW, BLUE, CYAN;
132
+ var init_format = __esm({
133
+ "src/output/format.ts"() {
134
+ "use strict";
135
+ useColor = !process.env.NO_COLOR && process.stderr.isTTY !== false;
136
+ RESET = useColor ? "\x1B[0m" : "";
137
+ BOLD = useColor ? "\x1B[1m" : "";
138
+ DIM = useColor ? "\x1B[2m" : "";
139
+ RED = useColor ? "\x1B[31m" : "";
140
+ GREEN = useColor ? "\x1B[32m" : "";
141
+ YELLOW = useColor ? "\x1B[33m" : "";
142
+ BLUE = useColor ? "\x1B[34m" : "";
143
+ CYAN = useColor ? "\x1B[36m" : "";
144
+ }
145
+ });
146
+
34
147
  // src/shutdown.ts
35
148
  var shutdown_exports = {};
36
149
  __export(shutdown_exports, {
@@ -339,109 +452,1297 @@ var init_connection = __esm({
339
452
  }
340
453
  });
341
454
 
342
- // src/output/format.ts
343
- function bold(s) {
344
- return `${BOLD}${s}${RESET}`;
345
- }
346
- function dim(s) {
347
- return `${DIM}${s}${RESET}`;
348
- }
349
- function red(s) {
350
- return `${RED}${s}${RESET}`;
351
- }
352
- function green(s) {
353
- return `${GREEN}${s}${RESET}`;
354
- }
355
- function yellow(s) {
356
- return `${YELLOW}${s}${RESET}`;
357
- }
358
- function blue(s) {
359
- return `${BLUE}${s}${RESET}`;
360
- }
361
- function cyan(s) {
362
- return `${CYAN}${s}${RESET}`;
363
- }
364
- function statusColor(status2) {
365
- switch (status2) {
366
- case "merged":
367
- return green(status2);
368
- case "dead_end":
369
- return red(status2);
370
- case "building":
371
- case "built":
372
- return blue(status2);
373
- case "verifying":
374
- case "verified":
375
- return cyan(status2);
376
- case "classified":
377
- case "reframed":
378
- return dim(status2);
379
- default:
380
- return yellow(status2);
381
- }
455
+ // ../shared/dist/index.js
456
+ var require_dist = __commonJS({
457
+ "../shared/dist/index.js"(exports2, module2) {
458
+ "use strict";
459
+ var __create2 = Object.create;
460
+ var __defProp2 = Object.defineProperty;
461
+ var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
462
+ var __getOwnPropNames2 = Object.getOwnPropertyNames;
463
+ var __getProtoOf2 = Object.getPrototypeOf;
464
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
465
+ var __export2 = (target, all) => {
466
+ for (var name in all)
467
+ __defProp2(target, name, { get: all[name], enumerable: true });
468
+ };
469
+ var __copyProps2 = (to, from, except, desc) => {
470
+ if (from && typeof from === "object" || typeof from === "function") {
471
+ for (let key of __getOwnPropNames2(from))
472
+ if (!__hasOwnProp2.call(to, key) && key !== except)
473
+ __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
474
+ }
475
+ return to;
476
+ };
477
+ var __toESM2 = (mod, isNodeMode, target) => (target = mod != null ? __create2(__getProtoOf2(mod)) : {}, __copyProps2(
478
+ // If the importer is in node compatibility mode or this is not an ESM
479
+ // file that has been converted to a CommonJS file using a Babel-
480
+ // compatible transform (i.e. "__esModule" has not been set), then set
481
+ // "default" to the CommonJS "module.exports" for node compatibility.
482
+ isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target,
483
+ mod
484
+ ));
485
+ var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
486
+ var index_exports = {};
487
+ __export2(index_exports, {
488
+ AGENT_DEFINITIONS: () => AGENT_DEFINITIONS2,
489
+ CLAUDE_MD_SECTION: () => CLAUDE_MD_SECTION2,
490
+ DEFAULT_CONFIG: () => DEFAULT_CONFIG3,
491
+ DOC_DIRS: () => DOC_DIRS2,
492
+ DOC_TEMPLATES: () => DOC_TEMPLATES2,
493
+ HOOKS_CONFIG: () => HOOKS_CONFIG2,
494
+ SLASH_COMMANDS: () => SLASH_COMMANDS2,
495
+ SYNTHESIS_STARTERS: () => SYNTHESIS_STARTERS2,
496
+ WORKFLOW_MD: () => WORKFLOW_MD2,
497
+ claudeMdContent: () => claudeMdContent,
498
+ configTemplate: () => configTemplate,
499
+ formatValidation: () => formatValidation2,
500
+ mkdirSafe: () => mkdirSafe3,
501
+ validateProject: () => validateProject2
502
+ });
503
+ module2.exports = __toCommonJS2(index_exports);
504
+ var AGENT_DEFINITIONS2 = {
505
+ builder: `---
506
+ name: builder
507
+ model: opus
508
+ tools: [Read, Write, Edit, Bash, Glob, Grep]
509
+ ---
510
+ You are the Builder. You write code, run experiments, and make technical decisions.
511
+
512
+ Before building:
513
+ 1. Read docs/synthesis/current.md for project state \u2014 this IS ground truth. Trust it.
514
+ 2. Read the dead-ends provided in your context \u2014 these are structural constraints.
515
+ 3. Read the experiment doc for this experiment \u2014 it has your hypothesis.
516
+
517
+ The synthesis already contains the diagnosis. Do NOT re-diagnose. Do NOT run
518
+ exploratory scripts to "understand the problem." The classify/doubt/challenge
519
+ cycle already did that work. Your job is to read the synthesis, read the code
520
+ at the specific sites mentioned, and implement the fix.
521
+
522
+ Read source code at the specific locations relevant to your change. Do NOT
523
+ read the entire codebase or run diagnostic Python scripts. If the synthesis
524
+ says "lines 1921-22" then read those lines and their context. That's it.
525
+
526
+ Do NOT read raw data files (fixtures/, ground truth JSON/STL). The synthesis
527
+ has the relevant facts. Reading raw data wastes turns re-deriving what the
528
+ doubt/challenge/verify cycle already established.
529
+
530
+ ## The Rule: ONE Change, Then Document
531
+
532
+ You make ONE code change per cycle. Not two, not "one more quick fix." ONE.
533
+
534
+ The sequence:
535
+ 1. **Read synthesis + experiment doc** \u2014 3-4 turns max.
536
+ 2. **Read code at specific sites** \u2014 2-3 turns max.
537
+ 3. **Write the experiment doc FIRST** \u2014 before coding, fill in the Approach section
538
+ with what you plan to do and why. This ensures there is always a record.
539
+ 4. **Implement ONE focused change** \u2014 a single coherent edit to the codebase.
540
+ 5. **Run the benchmark ONCE** \u2014 observe the result.
541
+ 6. **Update the experiment doc** \u2014 fill in Results and Metrics with what happened.
542
+ 7. **Output the majlis-json block** \u2014 your structured decisions.
543
+ 8. **STOP.**
544
+
545
+ After the benchmark: ONLY steps 6-7-8. No investigating why it failed. No reading
546
+ stderr. No "just checking one thing." Record the numbers, write your interpretation,
547
+ output the JSON, DONE. Diagnosing failures is the critic's and adversary's job.
548
+
549
+ If your change doesn't work, document what happened and STOP. Do NOT try to fix it.
550
+ Do NOT iterate. Do NOT "try one more thing." The adversary, critic, and verifier
551
+ exist to diagnose what went wrong. The cycle comes back to you with their insights.
552
+
553
+ ## Off-limits (DO NOT modify)
554
+ - \`fixtures/\` \u2014 test data, ground truth, STL files. Read-only.
555
+ - \`scripts/benchmark.py\` \u2014 the measurement tool. Never change how you're measured.
556
+ - \`.majlis/\` \u2014 framework config. Not your concern.
557
+
558
+ ## Git Safety
559
+ NEVER use \`git stash\`, \`git checkout\`, \`git reset\`, or any git command that modifies
560
+ the working tree or index. The \`.majlis/majlis.db\` database is in the working tree \u2014
561
+ these commands will corrupt framework state. Use \`git diff\` and \`git show\` for read-only comparison.
562
+
563
+ ## Confirmed Doubts
564
+ If your context includes confirmedDoubts, these are weaknesses that the verifier has
565
+ confirmed from a previous cycle. You MUST address each one. Do not ignore them \u2014
566
+ the verifier will check again.
567
+
568
+ ## Metrics
569
+ The framework captures baseline and post-build metrics automatically. Do NOT claim
570
+ specific metric numbers unless quoting framework output. Do NOT run the benchmark
571
+ yourself unless instructed to. If you need to verify your change works, do a minimal
572
+ targeted test, not a full benchmark run.
573
+
574
+ ## During building:
575
+ - Tag EVERY decision: proof / test / strong-consensus / consensus / analogy / judgment
576
+ - When making judgment-level decisions, state: "This is judgment \u2014 reasoning without precedent"
577
+
578
+ ## CRITICAL: You MUST finish cleanly.
579
+
580
+ If you are running low on turns, STOP coding and immediately:
581
+ 1. Update the experiment doc with whatever results you have
582
+ 2. Output the <!-- majlis-json --> block
583
+
584
+ The framework CANNOT recover your work if you get truncated without structured output.
585
+ An incomplete experiment doc with honest "did not finish" notes is infinitely better
586
+ than a truncated run with no output. Budget your turns: ~8 turns for reading,
587
+ ~10 turns for coding + benchmark, ~5 turns for documentation. If you've used 35+
588
+ turns, wrap up NOW regardless of where you are.
589
+
590
+ You may NOT verify your own work or mark your own decisions as proven.
591
+ Output your decisions in structured format so they can be recorded in the database.
592
+
593
+ ## Build Verification
594
+ The framework runs a build verification command (if configured) after you finish.
595
+ If the build fails, you'll stay at 'building' with guidance explaining the error.
596
+ Make sure your changes compile/lint before you finish.
597
+
598
+ ## Abandoning a Hypothesis
599
+ If you determine through investigation that the hypothesis is mathematically
600
+ impossible, structurally incompatible with the codebase, or has already been
601
+ tried and failed as a dead-end, you may abandon the experiment instead of
602
+ writing code. This saves a full cycle and records the constraint for future
603
+ experiments. Output the abandon block instead of decisions:
604
+ \`\`\`
605
+ <!-- majlis-json
606
+ {
607
+ "abandon": { "reason": "why the hypothesis cannot work", "structural_constraint": "the specific constraint that prevents it" }
382
608
  }
383
- function evidenceColor(level) {
384
- switch (level) {
385
- case "proof":
386
- return green(level);
387
- case "test":
388
- return cyan(level);
389
- case "strong_consensus":
390
- return blue(level);
391
- case "consensus":
392
- return blue(level);
393
- case "analogy":
394
- return yellow(level);
395
- case "judgment":
396
- return red(level);
397
- default:
398
- return level;
399
- }
609
+ -->
610
+ \`\`\`
611
+ Only abandon when you have clear evidence. If you're uncertain, implement the
612
+ hypothesis and let the doubt/verify cycle evaluate it.
613
+
614
+ ## Structured Output Format
615
+ At the end of your work, include a <!-- majlis-json --> block with your decisions:
616
+ \`\`\`
617
+ <!-- majlis-json
618
+ {
619
+ "decisions": [
620
+ { "description": "...", "evidence_level": "judgment|test|proof|analogy|consensus|strong_consensus", "justification": "..." }
621
+ ]
622
+ }
623
+ -->
624
+ \`\`\``,
625
+ critic: `---
626
+ name: critic
627
+ model: opus
628
+ tools: [Read, Glob, Grep]
629
+ ---
630
+ You are the Critic. You practise constructive doubt.
631
+
632
+ You receive:
633
+ - The builder's experiment document (the artifact, not the reasoning chain)
634
+ - The current synthesis (project state)
635
+ - Dead-ends (approaches that have been tried and failed)
636
+ - The hypothesis and experiment metadata
637
+
638
+ You do NOT see the builder's reasoning chain \u2014 only their documented output.
639
+ Use the experiment doc, synthesis, and dead-ends to find weaknesses.
640
+
641
+ For each doubt:
642
+ - What specific claim, decision, or assumption you doubt
643
+ - WHY: reference a prior experiment, inconsistency, untested case, or false analogy
644
+ - Evidence level of the doubted decision
645
+ - Severity: minor / moderate / critical
646
+
647
+ Rules:
648
+ - Every doubt MUST reference evidence. "This feels wrong" is not a doubt.
649
+ - You may NOT suggest fixes. Identify problems only.
650
+ - Focus on judgment and analogy-level decisions first.
651
+ - You may NOT modify any files. Produce your doubt document as output only.
652
+ - Do NOT attempt to write files. The framework saves your output automatically.
653
+
654
+ ## Structured Output Format
655
+ <!-- majlis-json
656
+ {
657
+ "doubts": [
658
+ { "claim_doubted": "...", "evidence_level_of_claim": "judgment", "evidence_for_doubt": "...", "severity": "critical|moderate|minor" }
659
+ ]
660
+ }
661
+ -->`,
662
+ adversary: `---
663
+ name: adversary
664
+ model: opus
665
+ tools: [Read, Glob, Grep]
666
+ ---
667
+ You are the Adversary. You do NOT review code for bugs.
668
+ You reason about problem structure to CONSTRUCT pathological cases.
669
+
670
+ You receive:
671
+ - The git diff of the builder's code changes (the actual code, not prose)
672
+ - The current synthesis (project state)
673
+ - The hypothesis and experiment metadata
674
+
675
+ Study the CODE DIFF carefully \u2014 that is where the builder's assumptions are exposed.
676
+
677
+ For each approach the builder takes, ask:
678
+ - What input would make this fail?
679
+ - What boundary condition was not tested?
680
+ - What degenerate case collapses a distinction the algorithm relies on?
681
+ - What distribution shift invalidates the assumptions?
682
+ - Under what conditions do two things the builder treats as distinct become identical?
683
+
684
+ Produce constructed counterexamples with reasoning.
685
+ Do NOT suggest fixes. Do NOT modify files. Do NOT attempt to write files.
686
+ The framework saves your output automatically.
687
+
688
+ ## Structured Output Format
689
+ <!-- majlis-json
690
+ {
691
+ "challenges": [
692
+ { "description": "...", "reasoning": "..." }
693
+ ]
694
+ }
695
+ -->`,
696
+ verifier: `---
697
+ name: verifier
698
+ model: opus
699
+ tools: [Read, Glob, Grep, Bash]
700
+ ---
701
+ You are the Verifier. Perform dual verification:
702
+
703
+ You receive:
704
+ - All doubts with explicit DOUBT-{id} identifiers (use these in your doubt_resolutions)
705
+ - Challenge documents from the adversary
706
+ - Framework-captured metrics (baseline vs post-build) \u2014 this is GROUND TRUTH
707
+ - The hypothesis and experiment metadata
708
+
709
+ ## Scope Constraint (CRITICAL)
710
+
711
+ You must produce your structured output (grades + doubt resolutions) within your turn budget.
712
+ Do NOT exhaustively test every doubt and challenge \u2014 prioritize the critical ones.
713
+ For each doubt/challenge: one targeted check is enough. Confirm, dismiss, or mark inconclusive.
714
+ Reserve your final turns for writing the structured majlis-json output.
715
+
716
+ The framework saves your output automatically. Do NOT attempt to write files.
717
+
718
+ ## Metrics (GROUND TRUTH)
719
+ If framework-captured metrics are in your context, these are the canonical before/after numbers.
720
+ Do NOT trust numbers claimed by the builder \u2014 compare against the framework metrics.
721
+ If the builder claims improvement but the framework metrics show regression, flag this.
722
+
723
+ ## Git Safety (CRITICAL)
724
+
725
+ NEVER use \`git stash\`, \`git checkout\`, \`git reset\`, or any git command that modifies
726
+ the working tree or index. The \`.majlis/majlis.db\` SQLite database is in the working tree \u2014
727
+ stashing or checking out files will corrupt it and silently break the framework's state.
728
+
729
+ To compare against baseline code, use read-only git commands:
730
+ - \`git show main:path/to/file\` \u2014 read a file as it was on main
731
+ - \`git diff main -- path/to/file\` \u2014 see what changed
732
+ - \`git log --oneline main..HEAD\` \u2014 see commits on the branch
733
+
734
+ To verify baseline metrics, run the benchmark on the CURRENT code and compare with the
735
+ documented baseline in docs/synthesis/current.md. Do NOT stash changes to re-run baseline.
736
+
737
+ ## PROVENANCE CHECK:
738
+ - Can every piece of code trace to an experiment or decision?
739
+ - Is the chain unbroken from requirement -> classification -> experiment -> code?
740
+ - Flag any broken chains.
741
+
742
+ ## CONTENT CHECK:
743
+ - Does the code do what the experiment log says?
744
+ - Run at most 3-5 targeted diagnostic scripts, focused on the critical doubts/challenges.
745
+ - Do NOT run exhaustive diagnostics on every claim.
746
+
747
+ Framework-captured metrics are ground truth \u2014 if they show regression, that
748
+ alone justifies a "rejected" grade. Do not re-derive from raw fixture data.
749
+
750
+ Grade each component: sound / good / weak / rejected
751
+ Grade each doubt/challenge: confirmed / dismissed (with evidence) / inconclusive
752
+
753
+ ## Structured Output Format
754
+ IMPORTANT: For doubt_resolutions, use the DOUBT-{id} numbers from your context.
755
+ Example: if your context lists "DOUBT-7: [critical] The algorithm fails on X",
756
+ use doubt_id: 7 in your output.
757
+
758
+ <!-- majlis-json
759
+ {
760
+ "grades": [
761
+ { "component": "...", "grade": "sound|good|weak|rejected", "provenance_intact": true, "content_correct": true, "notes": "..." }
762
+ ],
763
+ "doubt_resolutions": [
764
+ { "doubt_id": 7, "resolution": "confirmed|dismissed|inconclusive" }
765
+ ]
766
+ }
767
+ -->`,
768
+ reframer: `---
769
+ name: reframer
770
+ model: opus
771
+ tools: [Read, Glob, Grep]
772
+ ---
773
+ You are the Reframer. You receive ONLY:
774
+ - The original problem statement
775
+ - The current classification document
776
+ - The synthesis and dead-end registry
777
+
778
+ You do NOT read builder code, experiments, or solutions.
779
+
780
+ Independently propose:
781
+ - How should this problem be decomposed?
782
+ - What are the natural joints?
783
+ - What analogies from other domains apply?
784
+ - What framework would a different field use?
785
+
786
+ Compare your decomposition with the existing classification.
787
+ Flag structural divergences \u2014 these are the most valuable signals.
788
+
789
+ Produce your reframe document as output. Do NOT attempt to write files.
790
+ The framework saves your output automatically.
791
+
792
+ ## Structured Output Format
793
+ <!-- majlis-json
794
+ {
795
+ "reframe": {
796
+ "decomposition": "How you decomposed the problem",
797
+ "divergences": ["List of structural divergences from current classification"],
798
+ "recommendation": "What should change based on your independent analysis"
799
+ }
800
+ }
801
+ -->`,
802
+ compressor: `---
803
+ name: compressor
804
+ model: opus
805
+ tools: [Read, Write, Edit, Glob, Grep]
806
+ ---
807
+ You are the Compressor. Hold the entire project in view and compress it.
808
+
809
+ Your taskPrompt includes a "Structured Data (CANONICAL)" section exported directly
810
+ from the SQLite database. This is the source of truth. docs/ files are agent artifacts
811
+ that may contain stale or incorrect information. Cross-reference everything against
812
+ the database export.
813
+
814
+ 1. Read the database export in your context FIRST \u2014 it has all experiments, decisions,
815
+ doubts (with resolutions), verifications (with grades), challenges, and dead-ends.
816
+ 2. Read docs/ files for narrative context, but trust the database when they conflict.
817
+ 3. Cross-reference: same question in different language? contradicting decisions?
818
+ workaround masking root cause?
819
+ 4. Update fragility map: thin coverage, weak components, untested judgment
820
+ decisions, broken provenance.
821
+ 5. Update dead-end registry: compress rejected experiments into structural constraints.
822
+ Mark each dead-end as [structural] or [procedural].
823
+ 6. REWRITE synthesis using the Write tool \u2014 shorter and denser. If it's growing,
824
+ you're accumulating, not compressing. You MUST use the Write tool to update
825
+ docs/synthesis/current.md, docs/synthesis/fragility.md, and docs/synthesis/dead-ends.md.
826
+ The framework does NOT auto-save your output for these files.
827
+ 7. Review classification: new sub-types? resolved sub-types?
828
+
829
+ You may ONLY write to these three files:
830
+ - docs/synthesis/current.md
831
+ - docs/synthesis/fragility.md
832
+ - docs/synthesis/dead-ends.md
833
+
834
+ Do NOT modify MEMORY.md, .claude/, classification/, experiments/, or any other paths.
835
+
836
+ You may NOT write code, make decisions, or run experiments.
837
+
838
+ ## Structured Output Format
839
+ <!-- majlis-json
840
+ {
841
+ "compression_report": {
842
+ "synthesis_delta": "What changed in synthesis and why",
843
+ "new_dead_ends": ["List of newly identified dead-end constraints"],
844
+ "fragility_changes": ["List of changes to the fragility map"]
845
+ }
846
+ }
847
+ -->`,
848
+ gatekeeper: `---
849
+ name: gatekeeper
850
+ model: sonnet
851
+ tools: [Read, Glob, Grep]
852
+ ---
853
+ You are the Gatekeeper. You check hypotheses before expensive build cycles.
854
+
855
+ Your job is a fast quality gate \u2014 prevent wasted Opus builds on hypotheses that
856
+ are stale, redundant with dead-ends, or too vague to produce a focused change.
857
+
858
+ ## Checks (in order)
859
+
860
+ ### 1. Stale References
861
+ Does the hypothesis reference specific functions, line numbers, or structures that
862
+ may not exist in the current code? Read the relevant files to verify.
863
+ - If references are stale, list them in stale_references.
864
+
865
+ ### 2. Dead-End Overlap
866
+ Does this hypothesis repeat an approach already ruled out by structural dead-ends?
867
+ Check each structural dead-end in your context \u2014 if the hypothesis matches the
868
+ approach or violates the structural_constraint, flag it.
869
+ - If overlapping, list the dead-end IDs in overlapping_dead_ends.
870
+
871
+ ### 3. Scope Check
872
+ Is this a single focused change? A good hypothesis names ONE function, mechanism,
873
+ or parameter to change. A bad hypothesis says "improve X and also Y and also Z."
874
+ - Flag if the hypothesis tries to do multiple things.
875
+
876
+ ## Output
877
+
878
+ gate_decision:
879
+ - **approve** \u2014 all checks pass, proceed to build
880
+ - **flag** \u2014 concerns found but not blocking (warnings only)
881
+ - **reject** \u2014 hypothesis is dead on arrival (stale refs, dead-end repeat, or too vague).
882
+ Rejected hypotheses are automatically routed to dead-end with a 'procedural' category.
883
+ This does NOT block future approaches on the same sub-type \u2014 the user can create
884
+ a new experiment with a revised hypothesis.
885
+
886
+ ## Structured Output Format
887
+ <!-- majlis-json
888
+ {
889
+ "gate_decision": "approve|reject|flag",
890
+ "reason": "Brief explanation of decision",
891
+ "stale_references": ["list of stale references found, if any"],
892
+ "overlapping_dead_ends": [0]
893
+ }
894
+ -->`,
895
+ scout: `---
896
+ name: scout
897
+ model: opus
898
+ tools: [Read, Glob, Grep, WebSearch]
899
+ ---
900
+ You are the Scout. You practise rihla \u2014 travel in search of knowledge.
901
+
902
+ Your job is to search externally for alternative approaches, contradictory evidence,
903
+ and perspectives from other fields that could inform the current experiment.
904
+
905
+ You receive:
906
+ - The current synthesis and fragility map
907
+ - Dead-ends (approaches that have been tried and failed) \u2014 search for alternatives that circumvent these
908
+ - The hypothesis and experiment metadata
909
+
910
+ For the given experiment:
911
+ 1. Describe the problem in domain-neutral terms
912
+ 2. Search for alternative approaches in other fields or frameworks
913
+ 3. Identify known limitations of the current approach from external sources
914
+ 4. Find structurally similar problems in unrelated domains
915
+ 5. Report what you find on its own terms \u2014 do not judge or filter
916
+
917
+ Rules:
918
+ - Present findings neutrally. Report each approach on its own terms.
919
+ - Note where external approaches contradict the current one \u2014 these are the most valuable signals.
920
+ - Focus on approaches that CIRCUMVENT known dead-ends \u2014 these are the most valuable.
921
+ - You may NOT modify code or make decisions. Produce your rihla document as output only.
922
+ - Do NOT attempt to write files. The framework saves your output automatically.
923
+
924
+ ## Structured Output Format
925
+ <!-- majlis-json
926
+ {
927
+ "findings": [
928
+ { "approach": "Name of alternative approach", "source": "Where you found it", "relevance": "How it applies", "contradicts_current": true }
929
+ ]
930
+ }
931
+ -->`,
932
+ cartographer: `---
933
+ name: cartographer
934
+ model: opus
935
+ tools: [Read, Write, Edit, Glob, Grep, Bash]
936
+ ---
937
+ You are the Cartographer. You map the architecture of an existing codebase.
938
+
939
+ You receive a ProjectProfile JSON (deterministic surface scan) as context.
940
+ Your job is to deeply explore the codebase and produce two synthesis documents:
941
+ - docs/synthesis/current.md \u2014 project identity, architecture, key abstractions,
942
+ entry points, test coverage, build pipeline
943
+ - docs/synthesis/fragility.md \u2014 untested areas, single points of failure,
944
+ dependency risk, tech debt
945
+
946
+ ## Your Approach
947
+
948
+ Phase 1: Orientation (turns 1-10)
949
+ - Read README, main entry point, 2-3 key imports
950
+ - Understand the project's purpose and structure
951
+
952
+ Phase 2: Architecture Mapping (turns 11-30)
953
+ - Trace module boundaries and dependency graph
954
+ - Identify data flow patterns, config patterns
955
+ - For huge codebases: focus on entry points and top 5 most-imported modules
956
+ - Map test coverage and build pipeline
957
+
958
+ Phase 3: Write Synthesis (turns 31-40)
959
+ - Write docs/synthesis/current.md with dense, actionable content
960
+ - Write docs/synthesis/fragility.md with identified weak spots
961
+
962
+ You may ONLY write to docs/synthesis/. Do NOT modify source code.
963
+
964
+ ## Structured Output Format
965
+ <!-- majlis-json
966
+ {
967
+ "architecture": {
968
+ "modules": ["list of key modules"],
969
+ "entry_points": ["main entry points"],
970
+ "key_abstractions": ["core abstractions and patterns"],
971
+ "dependency_graph": "brief description of dependency structure"
972
+ }
973
+ }
974
+ -->`,
975
+ toolsmith: `---
976
+ name: toolsmith
977
+ model: opus
978
+ tools: [Read, Write, Edit, Bash, Glob, Grep]
979
+ ---
980
+ You are the Toolsmith. You verify toolchain and create a working metrics pipeline.
981
+
982
+ You receive a ProjectProfile JSON as context with detected test/build commands.
983
+ Your job is to verify these commands actually work, then create a metrics wrapper
984
+ script that translates test output into Majlis fixtures JSON format.
985
+
986
+ ## Your Approach
987
+
988
+ Phase 1: Verify Toolchain (turns 1-10)
989
+ - Try running the detected test command
990
+ - Try the build command
991
+ - Read CI config for hints if commands fail
992
+ - Determine what actually works
993
+
994
+ Phase 2: Create Metrics Wrapper (turns 11-25)
995
+ - Create .majlis/scripts/metrics.sh that runs tests and outputs valid Majlis JSON to stdout:
996
+ {"fixtures":{"test_suite":{"total":N,"passed":N,"failed":N,"duration_ms":N}}}
997
+ - Redirect all non-JSON output to stderr
998
+ - Strategy per framework:
999
+ - jest/vitest: --json flag \u2192 parse JSON
1000
+ - pytest: --tb=no -q \u2192 parse summary line
1001
+ - go test: -json \u2192 aggregate
1002
+ - cargo test: parse "test result:" line
1003
+ - no tests: stub with {"fixtures":{"project":{"has_tests":0}}}
1004
+
1005
+ Phase 3: Output Config (turns 26-30)
1006
+ - Output structured JSON with verified commands and config
1007
+
1008
+ ## Edge Cases
1009
+ - Build fails \u2192 set build_command: null, note issue, metrics wrapper still works
1010
+ - Tests fail \u2192 wrapper still outputs valid JSON with the fail counts
1011
+ - No tests \u2192 stub wrapper
1012
+ - Huge monorepo \u2192 focus on primary workspace
1013
+
1014
+ You may ONLY write to .majlis/scripts/. Do NOT modify source code.
1015
+
1016
+ ## Structured Output Format
1017
+ <!-- majlis-json
1018
+ {
1019
+ "toolsmith": {
1020
+ "metrics_command": ".majlis/scripts/metrics.sh",
1021
+ "build_command": "npm run build",
1022
+ "test_command": "npm test",
1023
+ "test_framework": "jest",
1024
+ "pre_measure": null,
1025
+ "post_measure": null,
1026
+ "fixtures": {},
1027
+ "tracked": {},
1028
+ "verification_output": "brief summary of what worked",
1029
+ "issues": ["list of issues encountered"]
1030
+ }
1031
+ }
1032
+ -->`,
1033
+ diagnostician: `---
1034
+ name: diagnostician
1035
+ model: opus
1036
+ tools: [Read, Write, Bash, Glob, Grep, WebSearch]
1037
+ ---
1038
+ You are the Diagnostician. You perform deep project-wide analysis.
1039
+
1040
+ You have the highest turn budget of any agent. Use it for depth, not breadth.
1041
+ Your job is pure insight \u2014 you do NOT fix code, you do NOT build, you do NOT
1042
+ make decisions. You diagnose.
1043
+
1044
+ ## What You Receive
1045
+ - Full database export: every experiment, decision, doubt, challenge, verification,
1046
+ dead-end, metric, and compression across the entire project history
1047
+ - Current synthesis, fragility map, and dead-end registry
1048
+ - Full read access to the entire project codebase
1049
+ - Bash access to run tests, profiling, git archaeology, and analysis scripts
1050
+
1051
+ ## What You Can Do
1052
+ 1. **Read everything** \u2014 source code, docs, git history, test output
1053
+ 2. **Run analysis** \u2014 execute tests, profilers, git log/blame/bisect, custom scripts
1054
+ 3. **Write analysis scripts** \u2014 you may write scripts ONLY to \`.majlis/scripts/\`
1055
+ 4. **Search externally** \u2014 WebSearch for patterns, known issues, relevant techniques
1056
+
1057
+ ## What You CANNOT Do
1058
+ - Modify any project files outside \`.majlis/scripts/\`
1059
+ - Make code changes, fixes, or patches
1060
+ - Create experiments or make decisions
1061
+ - Write to docs/, src/, or any other project directory
1062
+
1063
+ ## Your Approach
1064
+
1065
+ Phase 1: Orientation (turns 1-10)
1066
+ - Read the full database export in your context
1067
+ - Read synthesis, fragility, dead-ends
1068
+ - Identify patterns: recurring failures, unresolved doubts, evidence gaps
1069
+
1070
+ Phase 2: Deep Investigation (turns 11-40)
1071
+ - Read source code at critical points identified in Phase 1
1072
+ - Run targeted tests, profiling, git archaeology
1073
+ - Write and execute analysis scripts in .majlis/scripts/
1074
+ - Cross-reference findings across experiments
1075
+
1076
+ Phase 3: Synthesis (turns 41-60)
1077
+ - Compile findings into a diagnostic report
1078
+ - Identify root causes, not symptoms
1079
+ - Rank issues by structural impact
1080
+ - Suggest investigation directions (not fixes)
1081
+
1082
+ ## Output Format
1083
+ Produce a diagnostic report as markdown. At the end, include:
1084
+
1085
+ <!-- majlis-json
1086
+ {
1087
+ "diagnosis": {
1088
+ "root_causes": ["List of identified root causes"],
1089
+ "patterns": ["Recurring patterns across experiments"],
1090
+ "evidence_gaps": ["What we don't know but should"],
1091
+ "investigation_directions": ["Suggested directions for next experiments"]
1092
+ }
1093
+ }
1094
+ -->
1095
+
1096
+ ## Safety Reminders
1097
+ - You are READ-ONLY for project code. Write ONLY to .majlis/scripts/.
1098
+ - Focus on diagnosis, not fixing. Your value is insight, not implementation.
1099
+ - Trust the database export over docs/ files when they conflict.`
1100
+ };
1101
+ var SLASH_COMMANDS2 = {
1102
+ classify: {
1103
+ description: "Classify a problem domain into canonical sub-types before building",
1104
+ body: `Run \`majlis classify "$ARGUMENTS"\` and follow its output.
1105
+ If the CLI is not installed, act as the Builder in classification mode.
1106
+ Read docs/synthesis/current.md and docs/synthesis/dead-ends.md for context.
1107
+ Enumerate and classify all canonical sub-types of: $ARGUMENTS
1108
+ Produce a classification document following docs/classification/_TEMPLATE.md.`
1109
+ },
1110
+ doubt: {
1111
+ description: "Run a constructive doubt pass on an experiment",
1112
+ body: `Run \`majlis doubt $ARGUMENTS\` to spawn the critic agent.
1113
+ If the CLI is not installed, act as the Critic directly.
1114
+ Doubt the experiment at $ARGUMENTS. Produce a doubt document
1115
+ following docs/doubts/_TEMPLATE.md.`
1116
+ },
1117
+ challenge: {
1118
+ description: "Construct adversarial test cases for an experiment",
1119
+ body: `Run \`majlis challenge $ARGUMENTS\` to spawn the adversary agent.
1120
+ If the CLI is not installed, act as the Adversary directly.
1121
+ Construct pathological inputs designed to break the approach in $ARGUMENTS.
1122
+ Produce a challenge document following docs/challenges/_TEMPLATE.md.`
1123
+ },
1124
+ verify: {
1125
+ description: "Verify correctness and provenance of an experiment",
1126
+ body: `Run \`majlis verify $ARGUMENTS\` to spawn the verifier agent.
1127
+ If the CLI is not installed, act as the Verifier directly.
1128
+ Perform dual verification (provenance + content) on $ARGUMENTS.
1129
+ Produce a verification report following docs/verification/_TEMPLATE.md.`
1130
+ },
1131
+ reframe: {
1132
+ description: "Independently reframe a problem from scratch",
1133
+ body: `Run \`majlis reframe $ARGUMENTS\` to spawn the reframer agent.
1134
+ If the CLI is not installed, act as the Reframer directly.
1135
+ You receive ONLY the problem statement and classification \u2014 NOT builder code.
1136
+ Independently decompose $ARGUMENTS and compare with existing classification.`
1137
+ },
1138
+ compress: {
1139
+ description: "Compress project state into dense synthesis",
1140
+ body: `Run \`majlis compress\` to spawn the compressor agent.
1141
+ If the CLI is not installed, act as the Compressor directly.
1142
+ Read everything. Rewrite docs/synthesis/current.md shorter and denser.
1143
+ Update fragility map and dead-end registry.`
1144
+ },
1145
+ scout: {
1146
+ description: "Search externally for alternative approaches",
1147
+ body: `Run \`majlis scout $ARGUMENTS\` to spawn the scout agent.
1148
+ If the CLI is not installed, search for alternative approaches to $ARGUMENTS.
1149
+ Look for: limitations of current approach, alternative formulations from other fields,
1150
+ structurally similar problems in unrelated domains.
1151
+ Produce a rihla document at docs/rihla/.`
1152
+ },
1153
+ audit: {
1154
+ description: "Maqasid check \u2014 is the frame right?",
1155
+ body: `Run \`majlis audit "$ARGUMENTS"\` for a purpose audit.
1156
+ If the CLI is not installed, review: original objective, current classification,
1157
+ recent failures, dead-ends. Ask: is the classification serving the objective?
1158
+ Would we decompose differently with what we now know?`
1159
+ },
1160
+ diagnose: {
1161
+ description: "Deep project-wide diagnostic analysis",
1162
+ body: `Run \`majlis diagnose $ARGUMENTS\` for deep diagnosis.
1163
+ If the CLI is not installed, perform a deep diagnostic analysis.
1164
+ Read docs/synthesis/current.md, fragility.md, dead-ends.md, and all experiments.
1165
+ Identify root causes, recurring patterns, evidence gaps, and investigation directions.
1166
+ Do NOT modify project code \u2014 analysis only.`
1167
+ },
1168
+ scan: {
1169
+ description: "Scan existing project to auto-detect config and write synthesis",
1170
+ body: `Run \`majlis scan\` to analyze the existing codebase.
1171
+ This spawns two agents in parallel:
1172
+ - Cartographer: maps architecture \u2192 docs/synthesis/current.md + fragility.md
1173
+ - Toolsmith: verifies toolchain \u2192 .majlis/scripts/metrics.sh + config.json
1174
+ Use --force to overwrite existing synthesis files.`
1175
+ },
1176
+ resync: {
1177
+ description: "Update stale synthesis after project evolved without Majlis",
1178
+ body: `Run \`majlis resync\` to bring Majlis back up to speed.
1179
+ Unlike scan (which starts from zero), resync starts from existing knowledge.
1180
+ It assesses staleness, then re-runs cartographer (always) and toolsmith (if needed)
1181
+ with the old synthesis and DB history as context.
1182
+ Use --check to see the staleness report without making changes.
1183
+ Use --force to skip active experiment checks.`
1184
+ }
1185
+ };
1186
+ var HOOKS_CONFIG2 = {
1187
+ hooks: {
1188
+ SessionStart: [
1189
+ {
1190
+ hooks: [
1191
+ {
1192
+ type: "command",
1193
+ command: "majlis status --json 2>/dev/null || true"
1194
+ }
1195
+ ]
1196
+ }
1197
+ ],
1198
+ PreToolUse: [
1199
+ {
1200
+ matcher: "Bash",
1201
+ hooks: [
1202
+ {
1203
+ type: "command",
1204
+ command: "majlis check-commit 2>/dev/null || true",
1205
+ timeout: 10
1206
+ }
1207
+ ]
1208
+ }
1209
+ ],
1210
+ SubagentStop: [
1211
+ {
1212
+ hooks: [
1213
+ {
1214
+ type: "command",
1215
+ command: "echo 'Subagent completed. Run majlis next to continue the cycle.'",
1216
+ timeout: 5
1217
+ }
1218
+ ]
1219
+ }
1220
+ ]
1221
+ }
1222
+ };
1223
+ var DOC_TEMPLATES2 = {
1224
+ "experiments/_TEMPLATE.md": `# Experiment: {{title}}
1225
+
1226
+ **Hypothesis:** {{hypothesis}}
1227
+ **Branch:** {{branch}}
1228
+ **Status:** {{status}}
1229
+ **Sub-type:** {{sub_type}}
1230
+ **Created:** {{date}}
1231
+
1232
+ ## Approach
1233
+
1234
+ [Describe the approach]
1235
+
1236
+ ## Decisions
1237
+
1238
+ - [evidence_level] Decision description \u2014 justification
1239
+
1240
+ ## Results
1241
+
1242
+ [Describe the results]
1243
+
1244
+ ## Metrics
1245
+
1246
+ | Fixture | Metric | Before | After | Delta |
1247
+ |---------|--------|--------|-------|-------|
1248
+ | | | | | |
1249
+
1250
+ <!-- majlis-json
1251
+ {
1252
+ "decisions": [],
1253
+ "grades": []
400
1254
  }
401
- function table(headers, rows) {
402
- const widths = headers.map(
403
- (h, i) => Math.max(h.length, ...rows.map((r) => stripAnsi(r[i] ?? "").length))
404
- );
405
- const headerLine = headers.map((h, i) => h.padEnd(widths[i])).join(" ");
406
- const separator = widths.map((w) => "\u2500".repeat(w)).join("\u2500\u2500");
407
- const bodyLines = rows.map(
408
- (row) => row.map((cell, i) => {
409
- const stripped = stripAnsi(cell);
410
- const padding = widths[i] - stripped.length;
411
- return cell + " ".repeat(Math.max(0, padding));
412
- }).join(" ")
413
- );
414
- return [bold(headerLine), separator, ...bodyLines].join("\n");
1255
+ -->
1256
+ `,
1257
+ "decisions/_TEMPLATE.md": `# Decision: {{title}}
1258
+
1259
+ **Evidence Level:** {{evidence_level}}
1260
+ **Experiment:** {{experiment}}
1261
+ **Date:** {{date}}
1262
+
1263
+ ## Description
1264
+
1265
+ [What was decided]
1266
+
1267
+ ## Justification
1268
+
1269
+ [Why this decision was made, referencing evidence]
1270
+
1271
+ ## Alternatives Considered
1272
+
1273
+ [What else was considered and why it was rejected]
1274
+
1275
+ <!-- majlis-json
1276
+ {
1277
+ "decisions": [
1278
+ { "description": "", "evidence_level": "", "justification": "" }
1279
+ ]
415
1280
  }
416
- function stripAnsi(s) {
417
- return s.replace(/\x1b\[[0-9;]*m/g, "");
1281
+ -->
1282
+ `,
1283
+ "classification/_TEMPLATE.md": `# Classification: {{domain}}
1284
+
1285
+ **Date:** {{date}}
1286
+
1287
+ ## Problem Domain
1288
+
1289
+ [Describe the problem domain]
1290
+
1291
+ ## Sub-Types
1292
+
1293
+ ### 1. {{sub_type_1}}
1294
+ - **Description:**
1295
+ - **Canonical form:**
1296
+ - **Known constraints:**
1297
+
1298
+ ### 2. {{sub_type_2}}
1299
+ - **Description:**
1300
+ - **Canonical form:**
1301
+ - **Known constraints:**
1302
+
1303
+ ## Relationships
1304
+
1305
+ [How sub-types relate to each other]
1306
+ `,
1307
+ "doubts/_TEMPLATE.md": `# Doubt Document \u2014 Against Experiment {{experiment}}
1308
+
1309
+ **Critic:** {{agent}}
1310
+ **Date:** {{date}}
1311
+
1312
+ ## Doubt 1: {{title}}
1313
+
1314
+ **Claim doubted:** {{claim}}
1315
+ **Evidence level of claim:** {{evidence_level}}
1316
+ **Severity:** {{severity}}
1317
+
1318
+ **Evidence for doubt:**
1319
+ [Specific evidence \u2014 a prior experiment, inconsistency, untested case, or false analogy]
1320
+
1321
+ <!-- majlis-json
1322
+ {
1323
+ "doubts": [
1324
+ { "claim_doubted": "", "evidence_level_of_claim": "", "evidence_for_doubt": "", "severity": "critical" }
1325
+ ]
418
1326
  }
419
- function header(title) {
420
- console.log(`
421
- ${bold(`[majlis] ${title}`)}
422
- `);
1327
+ -->
1328
+ `,
1329
+ "challenges/_TEMPLATE.md": `# Challenge Document \u2014 Against Experiment {{experiment}}
1330
+
1331
+ **Adversary:** {{agent}}
1332
+ **Date:** {{date}}
1333
+
1334
+ ## Challenge 1: {{title}}
1335
+
1336
+ **Constructed case:**
1337
+ [Specific input or condition designed to break the approach]
1338
+
1339
+ **Reasoning:**
1340
+ [Why this case should break the approach \u2014 what assumption does it violate?]
1341
+
1342
+ ## Challenge 2: {{title}}
1343
+
1344
+ **Constructed case:**
1345
+ [Specific input or condition]
1346
+
1347
+ **Reasoning:**
1348
+ [Why this should break]
1349
+
1350
+ <!-- majlis-json
1351
+ {
1352
+ "challenges": [
1353
+ { "description": "", "reasoning": "" }
1354
+ ]
423
1355
  }
424
- function warn(msg) {
425
- console.log(`${yellow("[majlis]")} ${msg}`);
1356
+ -->
1357
+ `,
1358
+ "verification/_TEMPLATE.md": `# Verification Report \u2014 Experiment {{experiment}}
1359
+
1360
+ **Verifier:** {{agent}}
1361
+ **Date:** {{date}}
1362
+
1363
+ ## Provenance Check (Isnad)
1364
+
1365
+ | Component | Traceable | Chain intact | Notes |
1366
+ |-----------|-----------|--------------|-------|
1367
+ | | yes/no | yes/no | |
1368
+
1369
+ ## Content Check (Matn)
1370
+
1371
+ | Component | Tests pass | Consistent | Grade | Notes |
1372
+ |-----------|-----------|------------|-------|-------|
1373
+ | | yes/no | yes/no | sound/good/weak/rejected | |
1374
+
1375
+ ## Doubt Resolution
1376
+
1377
+ | Doubt | Resolution | Evidence |
1378
+ |-------|------------|----------|
1379
+ | | confirmed/dismissed/inconclusive | |
1380
+
1381
+ <!-- majlis-json
1382
+ {
1383
+ "grades": [
1384
+ { "component": "", "grade": "sound", "provenance_intact": true, "content_correct": true, "notes": "" }
1385
+ ],
1386
+ "doubt_resolutions": [
1387
+ { "doubt_id": 0, "resolution": "confirmed" }
1388
+ ]
426
1389
  }
427
- function info(msg) {
428
- console.log(`${cyan("[majlis]")} ${msg}`);
1390
+ -->
1391
+ `,
1392
+ "reframes/_TEMPLATE.md": `# Reframe: {{domain}}
1393
+
1394
+ **Reframer:** {{agent}}
1395
+ **Date:** {{date}}
1396
+
1397
+ ## Independent Decomposition
1398
+
1399
+ [How this problem should be decomposed \u2014 without seeing the builder's approach]
1400
+
1401
+ ## Natural Joints
1402
+
1403
+ [Where does this problem naturally divide?]
1404
+
1405
+ ## Cross-Domain Analogies
1406
+
1407
+ [What analogies from other domains apply?]
1408
+
1409
+ ## Comparison with Existing Classification
1410
+
1411
+ [Structural divergences from the current classification]
1412
+
1413
+ ## Divergences (Most Valuable Signals)
1414
+
1415
+ [Where the independent decomposition differs from the builder's classification]
1416
+ `,
1417
+ "rihla/_TEMPLATE.md": `# Rihla (Scout Report): {{topic}}
1418
+
1419
+ **Date:** {{date}}
1420
+
1421
+ ## Problem (Domain-Neutral)
1422
+
1423
+ [Describe the problem in domain-neutral terms]
1424
+
1425
+ ## Alternative Approaches Found
1426
+
1427
+ ### 1. {{approach}}
1428
+ - **Source:**
1429
+ - **Description:**
1430
+ - **Applicability:**
1431
+
1432
+ ## Known Limitations of Current Approach
1433
+
1434
+ [What external sources say about where this approach fails]
1435
+
1436
+ ## Cross-Domain Analogues
1437
+
1438
+ [Structurally similar problems in unrelated domains]
1439
+ `
1440
+ };
1441
+ var DOC_DIRS2 = [
1442
+ "inbox",
1443
+ "experiments",
1444
+ "decisions",
1445
+ "classification",
1446
+ "doubts",
1447
+ "challenges",
1448
+ "verification",
1449
+ "reframes",
1450
+ "rihla",
1451
+ "synthesis",
1452
+ "diagnosis"
1453
+ ];
1454
+ var WORKFLOW_MD2 = `# Majlis Workflow \u2014 Quick Reference
1455
+
1456
+ ## The Cycle
1457
+
1458
+ \`\`\`
1459
+ 1. CLASSIFY \u2192 Taxonomy before solution (Al-Khwarizmi)
1460
+ 2. REFRAME \u2192 Independent decomposition (Al-Biruni)
1461
+ 3. GATE \u2192 Hypothesis quality check ('Ilm al-'Ilal)
1462
+ 4. BUILD \u2192 Write code with tagged decisions (Ijtihad)
1463
+ 5. CHALLENGE \u2192 Construct breaking inputs (Ibn al-Haytham)
1464
+ 6. DOUBT \u2192 Systematic challenge with evidence (Shukuk)
1465
+ 7. SCOUT \u2192 External search for alternatives (Rihla)
1466
+ 8. VERIFY \u2192 Provenance + content checks (Isnad + Matn)
1467
+ 9. RESOLVE \u2192 Route based on grades
1468
+ 10. COMPRESS \u2192 Shorter and denser (Hifz)
1469
+ \`\`\`
1470
+
1471
+ ## Resolution
1472
+ - **Sound** \u2192 Merge
1473
+ - **Good** \u2192 Merge + add gaps to fragility map
1474
+ - **Weak** \u2192 Cycle back with synthesised guidance
1475
+ - **Rejected** \u2192 Dead-end with structural constraint
1476
+
1477
+ ## Circuit Breaker
1478
+ 3+ weak/rejected on same sub-type \u2192 Maqasid Check (purpose audit)
1479
+
1480
+ ## Evidence Hierarchy
1481
+ 1. Proof \u2192 2. Test \u2192 3a. Strong Consensus \u2192 3b. Consensus \u2192 4. Analogy \u2192 5. Judgment
1482
+
1483
+ ## Commands
1484
+ | Action | Command |
1485
+ |--------|---------|
1486
+ | Initialize | \`majlis init\` |
1487
+ | Status | \`majlis status\` |
1488
+ | New experiment | \`majlis new "hypothesis"\` |
1489
+ | Baseline metrics | \`majlis baseline\` |
1490
+ | Measure metrics | \`majlis measure\` |
1491
+ | Compare metrics | \`majlis compare\` |
1492
+ | Next step | \`majlis next\` |
1493
+ | Auto cycle | \`majlis next --auto\` |
1494
+ | Autonomous | \`majlis run "goal"\` |
1495
+ | Session start | \`majlis session start "intent"\` |
1496
+ | Session end | \`majlis session end\` |
1497
+ | Compress | \`majlis compress\` |
1498
+ | Audit | \`majlis audit "objective"\` |
1499
+
1500
+ ## Experiment Flags
1501
+ | Flag | Purpose |
1502
+ |------|---------|
1503
+ | \`--sub-type TYPE\` | Classify experiment by problem sub-type |
1504
+ | \`--depends-on SLUG\` | Block building until dependency is merged |
1505
+ | \`--context FILE,FILE\` | Inject domain-specific docs into agent context |
1506
+
1507
+ Example: \`majlis new "improve fitting accuracy" --sub-type fitting --depends-on surface-construction --context docs/algorithms/fitting.md,fixtures/anatomy/part1/README.md\`
1508
+
1509
+ ## Project Readiness
1510
+
1511
+ Majlis works with zero config \u2014 agents figure things out from CLAUDE.md. But each
1512
+ config field you wire up removes a failure mode and makes cycles more autonomous.
1513
+
1514
+ ### Metrics Command
1515
+ Your \`metrics.command\` must output JSON in this format:
1516
+ \`\`\`json
1517
+ { "fixtures": { "fixture_name": { "metric_name": 123.4 } } }
1518
+ \`\`\`
1519
+ If your test harness outputs human-readable text, write a thin wrapper script that
1520
+ parses it into this format. The framework runs this command automatically before and
1521
+ after each build to capture regression data.
1522
+
1523
+ ### Fixtures and Gates
1524
+ Define your test cases in \`config.metrics.fixtures\`. Flag your regression baseline
1525
+ as a gate \u2014 regressions on gate fixtures block merge regardless of verification grades:
1526
+ \`\`\`json
1527
+ "fixtures": {
1528
+ "baseline_test": { "gate": true },
1529
+ "target_test": { "gate": false }
429
1530
  }
430
- function success(msg) {
431
- console.log(`${green("[majlis]")} ${msg}`);
1531
+ \`\`\`
1532
+
1533
+ ### Tracked Metrics
1534
+ Name the metrics you care about and set their direction:
1535
+ \`\`\`json
1536
+ "tracked": {
1537
+ "error_rate": { "direction": "lower_is_better" },
1538
+ "accuracy": { "direction": "higher_is_better" },
1539
+ "value_delta": { "direction": "closer_to_gt", "target": 0 }
432
1540
  }
433
- var RESET, BOLD, DIM, RED, GREEN, YELLOW, BLUE, CYAN;
434
- var init_format = __esm({
435
- "src/output/format.ts"() {
436
- "use strict";
437
- RESET = "\x1B[0m";
438
- BOLD = "\x1B[1m";
439
- DIM = "\x1B[2m";
440
- RED = "\x1B[31m";
441
- GREEN = "\x1B[32m";
442
- YELLOW = "\x1B[33m";
443
- BLUE = "\x1B[34m";
444
- CYAN = "\x1B[36m";
1541
+ \`\`\`
1542
+
1543
+ ### Architecture Docs
1544
+ Agents read CLAUDE.md for project context. The more specific it is about where things
1545
+ live, how to build, and how to test, the better agents perform. Include build commands,
1546
+ test commands, file layout, and key patterns.
1547
+
1548
+ Run \`majlis status\` to see which readiness checks pass and which need attention.
1549
+ `;
1550
+ var SYNTHESIS_STARTERS2 = {
1551
+ "current.md": '# Project Synthesis\n\n*No experiments yet. Run `majlis new "hypothesis"` to begin.*\n',
1552
+ "fragility.md": "# Fragility Map\n\n*No fragility recorded yet.*\n",
1553
+ "dead-ends.md": "# Dead-End Registry\n\n*No dead-ends recorded yet.*\n"
1554
+ };
1555
+ var CLAUDE_MD_SECTION2 = `
1556
+ ## Majlis Protocol
1557
+
1558
+ This project uses the Majlis Framework for structured multi-agent problem solving.
1559
+ See \`docs/workflow.md\` for the full cycle. See \`.claude/agents/\` for role definitions (source of truth in \`.majlis/agents/\`).
1560
+
1561
+ ### Evidence Hierarchy (tag every decision)
1562
+ 1. **Proof** \u2014 mathematical proof. Overturn requires error in proof.
1563
+ 2. **Test** \u2014 empirical test. Overturn requires showing test insufficiency.
1564
+ 3a. **Strong Consensus** \u2014 convergence across independent approaches.
1565
+ 3b. **Consensus** \u2014 agreement from same-model experiments.
1566
+ 4. **Analogy** \u2014 justified by similarity to prior work.
1567
+ 5. **Judgment** \u2014 independent reasoning without precedent.
1568
+
1569
+ ### Session Discipline
1570
+ - One intent per session. Declare it with \`majlis session start "intent"\`.
1571
+ - Stray thoughts \u2192 Telegram (Scribe) or docs/inbox/.
1572
+ - Every session ends with \`majlis session end\`.
1573
+
1574
+ ### Before Building
1575
+ - Read \`docs/synthesis/current.md\` for compressed project state.
1576
+ - Run \`majlis dead-ends --sub-type <relevant>\` for structural constraints.
1577
+ - Run \`majlis decisions --level judgment\` for provisional decisions to challenge.
1578
+
1579
+ ### Compression Trigger
1580
+ - Run \`majlis status\` \u2014 it will warn when compression is due.
1581
+
1582
+ ### Current State
1583
+ Run \`majlis status\` for live experiment state and cycle position.
1584
+ `;
1585
+ function claudeMdContent(name, objective) {
1586
+ return `# ${name}
1587
+
1588
+ ${objective ? `**Objective:** ${objective}
1589
+ ` : ""}## Majlis Protocol
1590
+
1591
+ This project uses the Majlis Framework for structured multi-agent problem solving.
1592
+ See \`docs/workflow.md\` for the full cycle. See \`.claude/agents/\` for role definitions (source of truth in \`.majlis/agents/\`).
1593
+
1594
+ ### Evidence Hierarchy (tag every decision)
1595
+ 1. **Proof** \u2014 mathematical proof. Overturn requires error in proof.
1596
+ 2. **Test** \u2014 empirical test. Overturn requires showing test insufficiency.
1597
+ 3a. **Strong Consensus** \u2014 convergence across independent approaches.
1598
+ 3b. **Consensus** \u2014 agreement from same-model experiments.
1599
+ 4. **Analogy** \u2014 justified by similarity to prior work.
1600
+ 5. **Judgment** \u2014 independent reasoning without precedent.
1601
+
1602
+ ### Session Discipline
1603
+ - One intent per session. Declare it with \`majlis session start "intent"\`.
1604
+ - Stray thoughts \u2192 Telegram (Scribe) or docs/inbox/.
1605
+ - Every session ends with \`majlis session end\`.
1606
+
1607
+ ### Before Building
1608
+ - Read \`docs/synthesis/current.md\` for compressed project state.
1609
+ - Run \`majlis dead-ends --sub-type <relevant>\` for structural constraints.
1610
+ - Run \`majlis decisions --level judgment\` for provisional decisions to challenge.
1611
+
1612
+ ### Compression Trigger
1613
+ - Run \`majlis status\` \u2014 it will warn when compression is due.
1614
+
1615
+ ### Current State
1616
+ Run \`majlis status\` for live experiment state and cycle position.
1617
+ `;
1618
+ }
1619
+ var DEFAULT_CONFIG3 = {
1620
+ project: {
1621
+ name: "",
1622
+ description: "",
1623
+ objective: ""
1624
+ },
1625
+ metrics: {
1626
+ command: `echo '{"fixtures":{}}'`,
1627
+ fixtures: {},
1628
+ tracked: {}
1629
+ },
1630
+ build: {
1631
+ pre_measure: null,
1632
+ post_measure: null
1633
+ },
1634
+ cycle: {
1635
+ compression_interval: 5,
1636
+ circuit_breaker_threshold: 3,
1637
+ require_doubt_before_verify: true,
1638
+ require_challenge_before_verify: false,
1639
+ auto_baseline_on_new_experiment: true
1640
+ },
1641
+ models: {
1642
+ builder: "opus",
1643
+ critic: "opus",
1644
+ adversary: "opus",
1645
+ verifier: "opus",
1646
+ reframer: "opus",
1647
+ compressor: "opus",
1648
+ gatekeeper: "sonnet",
1649
+ scout: "opus"
1650
+ }
1651
+ };
1652
+ function configTemplate(answers) {
1653
+ return JSON.stringify({
1654
+ project: {
1655
+ name: answers.name,
1656
+ description: answers.description,
1657
+ objective: answers.objective
1658
+ },
1659
+ metrics: {
1660
+ command: answers.metricsCommand,
1661
+ fixtures: {},
1662
+ tracked: {}
1663
+ },
1664
+ build: {
1665
+ pre_measure: answers.buildPre || null,
1666
+ post_measure: answers.buildPost || null
1667
+ },
1668
+ cycle: {
1669
+ compression_interval: 5,
1670
+ circuit_breaker_threshold: 3,
1671
+ require_doubt_before_verify: true,
1672
+ require_challenge_before_verify: false,
1673
+ auto_baseline_on_new_experiment: true
1674
+ },
1675
+ models: {
1676
+ builder: "opus",
1677
+ critic: "opus",
1678
+ adversary: "opus",
1679
+ verifier: "opus",
1680
+ reframer: "opus",
1681
+ compressor: "opus",
1682
+ gatekeeper: "sonnet",
1683
+ scout: "opus"
1684
+ }
1685
+ }, null, 2);
1686
+ }
1687
+ var fs23 = __toESM2(require("fs"));
1688
+ function mkdirSafe3(dir) {
1689
+ if (!fs23.existsSync(dir)) {
1690
+ fs23.mkdirSync(dir, { recursive: true });
1691
+ }
1692
+ }
1693
+ function validateProject2(checks) {
1694
+ const results = [];
1695
+ results.push(
1696
+ checks.hasGitRepo ? { label: "Git repository", status: "pass", detail: "Detected" } : { label: "Git repository", status: "fail", detail: "Not a git repo \u2014 experiment branches will not work" }
1697
+ );
1698
+ results.push(
1699
+ checks.hasObjective ? { label: "Project objective", status: "pass", detail: "Set in config" } : { label: "Project objective", status: "warn", detail: "Not set \u2014 agents lack goal context for maqasid checks" }
1700
+ );
1701
+ results.push(
1702
+ checks.hasClaudeMd ? { label: "CLAUDE.md", status: "pass", detail: "Found \u2014 agents will have project context" } : { label: "CLAUDE.md", status: "warn", detail: "Not found \u2014 agents will lack project architecture context" }
1703
+ );
1704
+ const hasCommand = checks.metricsCommand && !checks.metricsCommand.includes(`echo '{"fixtures":{}}'`);
1705
+ if (!hasCommand) {
1706
+ results.push({ label: "Metrics command", status: "warn", detail: "Using default no-op \u2014 configure metrics.command for automatic regression detection" });
1707
+ } else if (!checks.metricsCommandRunnable) {
1708
+ results.push({ label: "Metrics command", status: "warn", detail: "Set but not runnable \u2014 check the command works: " + checks.metricsCommand });
1709
+ } else {
1710
+ results.push({ label: "Metrics command", status: "pass", detail: "Set and runnable" });
1711
+ }
1712
+ const fixtureEntries = Array.isArray(checks.fixtures) ? checks.fixtures : Object.keys(checks.fixtures);
1713
+ if (fixtureEntries.length === 0) {
1714
+ results.push({ label: "Fixtures", status: "warn", detail: "None defined \u2014 consider adding fixtures with gate flags for regression protection" });
1715
+ } else {
1716
+ const gateCount = Array.isArray(checks.fixtures) ? 0 : Object.values(checks.fixtures).filter((f) => f.gate).length;
1717
+ if (gateCount === 0) {
1718
+ results.push({ label: "Fixtures", status: "warn", detail: `${fixtureEntries.length} fixture(s) but none flagged as gate \u2014 no regression protection` });
1719
+ } else {
1720
+ results.push({ label: "Fixtures", status: "pass", detail: `${fixtureEntries.length} fixture(s), ${gateCount} gate(s)` });
1721
+ }
1722
+ }
1723
+ const trackedCount = Object.keys(checks.tracked).length;
1724
+ if (trackedCount === 0) {
1725
+ results.push({ label: "Tracked metrics", status: "warn", detail: "None defined \u2014 regression detection disabled" });
1726
+ } else {
1727
+ results.push({ label: "Tracked metrics", status: "pass", detail: `${trackedCount} metric(s) tracked` });
1728
+ }
1729
+ results.push(
1730
+ checks.preMeasure ? { label: "Build command", status: "pass", detail: "Set (pre_measure)" } : { label: "Build command", status: "warn", detail: "No pre_measure \u2014 builder must know how to build from CLAUDE.md" }
1731
+ );
1732
+ results.push(
1733
+ checks.hasSynthesis ? { label: "Synthesis document", status: "pass", detail: "Found" } : { label: "Synthesis document", status: "warn", detail: "Empty \u2014 will be populated after first compression cycle" }
1734
+ );
1735
+ return results;
1736
+ }
1737
+ var _useColor = !process.env.NO_COLOR && process.stderr?.isTTY !== false;
1738
+ function formatValidation2(checks) {
1739
+ const lines = [];
1740
+ for (const c of checks) {
1741
+ const icon = c.status === "pass" ? _useColor ? "\x1B[32m\u2713\x1B[0m" : "\u2713" : c.status === "warn" ? _useColor ? "\x1B[33m\u26A0\x1B[0m" : "\u26A0" : _useColor ? "\x1B[31m\u2717\x1B[0m" : "\u2717";
1742
+ lines.push(` ${icon} ${c.label}: ${c.detail}`);
1743
+ }
1744
+ return lines.join("\n");
1745
+ }
445
1746
  }
446
1747
  });
447
1748
 
@@ -449,7 +1750,7 @@ var init_format = __esm({
449
1750
  function getExtractionSchema(role) {
450
1751
  switch (role) {
451
1752
  case "builder":
452
- return '{"decisions": [{"description": "string", "evidence_level": "proof|test|strong_consensus|consensus|analogy|judgment", "justification": "string"}]}';
1753
+ return '{"decisions": [{"description": "string", "evidence_level": "proof|test|strong_consensus|consensus|analogy|judgment", "justification": "string"}], "abandon": {"reason": "string", "structural_constraint": "string"}}';
453
1754
  case "critic":
454
1755
  return '{"doubts": [{"claim_doubted": "string", "evidence_level_of_claim": "string", "evidence_for_doubt": "string", "severity": "minor|moderate|critical"}]}';
455
1756
  case "adversary":
@@ -506,7 +1807,7 @@ async function extractStructuredData(role, markdown) {
506
1807
  const tier1 = extractMajlisJsonBlock(markdown);
507
1808
  if (tier1) {
508
1809
  const parsed = tryParseJson(tier1);
509
- if (parsed) return parsed;
1810
+ if (parsed) return { data: parsed, tier: 1 };
510
1811
  console.warn(`[majlis] Malformed JSON in <!-- majlis-json --> block for ${role}. Falling back.`);
511
1812
  } else {
512
1813
  console.warn(`[majlis] No <!-- majlis-json --> block found in ${role} output. Falling back.`);
@@ -514,15 +1815,18 @@ async function extractStructuredData(role, markdown) {
514
1815
  const tier2 = extractViaPatterns(role, markdown);
515
1816
  if (tier2 && hasData(tier2)) {
516
1817
  console.warn(`[majlis] Used regex fallback for ${role}. Review extracted data.`);
517
- return tier2;
1818
+ return { data: tier2, tier: 2 };
518
1819
  }
519
1820
  console.warn(`[majlis] Regex fallback insufficient for ${role}. Using Haiku extraction.`);
520
1821
  const tier3 = await extractViaHaiku(role, markdown);
521
- if (tier3) return tier3;
1822
+ if (tier3) {
1823
+ console.warn(`[majlis] Tier 3 (Haiku) extraction used for ${role}. Data provenance degraded.`);
1824
+ return { data: tier3, tier: 3 };
1825
+ }
522
1826
  console.error(
523
1827
  `[majlis] FAILED to extract structured data from ${role} output. State machine will continue but data is missing. Manual review required.`
524
1828
  );
525
- return null;
1829
+ return { data: null, tier: null };
526
1830
  }
527
1831
  function extractMajlisJsonBlock(markdown) {
528
1832
  const match = markdown.match(/<!--\s*majlis-json\s*\n?([\s\S]*?)-->/);
@@ -592,6 +1896,23 @@ function extractViaPatterns(role, markdown) {
592
1896
  });
593
1897
  }
594
1898
  if (doubts.length > 0) result.doubts = doubts;
1899
+ if (role === "builder") {
1900
+ const abandonPattern = /\[ABANDON\]\s*(.+?)(?:\n|$)[\s\S]*?(?:structural.?constraint|Constraint|CONSTRAINT)\s*[:=]\s*(.+?)(?:\n|$)/im;
1901
+ const abandonMatch = markdown.match(abandonPattern);
1902
+ if (abandonMatch) {
1903
+ result.abandon = {
1904
+ reason: abandonMatch[1].trim(),
1905
+ structural_constraint: abandonMatch[2].trim()
1906
+ };
1907
+ }
1908
+ const invalidMatch = markdown.match(/(?:HYPOTHESIS\s+INVALID|HYPOTHESIS\s+IMPOSSIBLE)\s*[:.\-—]\s*(.+?)(?:\n|$)/im);
1909
+ if (invalidMatch && !result.abandon) {
1910
+ result.abandon = {
1911
+ reason: invalidMatch[1].trim(),
1912
+ structural_constraint: "Extracted via regex \u2014 review original document"
1913
+ };
1914
+ }
1915
+ }
595
1916
  return result;
596
1917
  }
597
1918
  async function extractViaHaiku(role, markdown) {
@@ -631,7 +1952,7 @@ ${truncated}`;
631
1952
  }
632
1953
  }
633
1954
  function hasData(output) {
634
- return !!(output.decisions && output.decisions.length > 0 || output.grades && output.grades.length > 0 || output.doubts && output.doubts.length > 0 || output.challenges && output.challenges.length > 0 || output.findings && output.findings.length > 0 || output.guidance || output.reframe || output.compression_report || output.gate_decision || output.diagnosis);
1955
+ return !!(output.decisions && output.decisions.length > 0 || output.grades && output.grades.length > 0 || output.doubts && output.doubts.length > 0 || output.challenges && output.challenges.length > 0 || output.findings && output.findings.length > 0 || output.guidance || output.reframe || output.compression_report || output.gate_decision || output.diagnosis || output.abandon);
635
1956
  }
636
1957
  function validateForRole(role, output) {
637
1958
  const required = ROLE_REQUIRED_FIELDS[role];
@@ -846,6 +2167,8 @@ function buildPreToolUseGuards(role, cwd) {
846
2167
  const configFile = path2.resolve(cwd, ".majlis", "config.json");
847
2168
  const dbFile = path2.resolve(cwd, ".majlis", "majlis.db");
848
2169
  const settingsFile = path2.resolve(cwd, ".claude", "settings.json");
2170
+ const claudeDir = path2.resolve(cwd, ".claude");
2171
+ const agentsDir = path2.resolve(cwd, ".majlis", "agents");
849
2172
  const configGuard = async (input) => {
850
2173
  const toolInput = input.tool_input ?? {};
851
2174
  const filePath = toolInput.file_path ?? "";
@@ -854,6 +2177,9 @@ function buildPreToolUseGuards(role, cwd) {
854
2177
  if (resolved === configFile || resolved === dbFile || resolved === settingsFile) {
855
2178
  return { decision: "block", reason: `Builder may not modify framework files: ${filePath}` };
856
2179
  }
2180
+ if (isInsideDir(resolved, claudeDir) || isInsideDir(resolved, agentsDir)) {
2181
+ return { decision: "block", reason: `Builder may not modify agent definitions or framework settings: ${filePath}` };
2182
+ }
857
2183
  }
858
2184
  return {};
859
2185
  };
@@ -867,6 +2193,8 @@ function buildPreToolUseGuards(role, cwd) {
867
2193
  const configFile = path2.resolve(cwd, ".majlis", "config.json");
868
2194
  const dbFile = path2.resolve(cwd, ".majlis", "majlis.db");
869
2195
  const settingsFile = path2.resolve(cwd, ".claude", "settings.json");
2196
+ const claudeDir = path2.resolve(cwd, ".claude");
2197
+ const agentsDir = path2.resolve(cwd, ".majlis", "agents");
870
2198
  const configGuard = async (input) => {
871
2199
  const toolInput = input.tool_input ?? {};
872
2200
  const filePath = toolInput.file_path ?? "";
@@ -875,6 +2203,9 @@ function buildPreToolUseGuards(role, cwd) {
875
2203
  if (resolved === configFile || resolved === dbFile || resolved === settingsFile) {
876
2204
  return { decision: "block", reason: `Verifier may not modify framework files: ${filePath}` };
877
2205
  }
2206
+ if (isInsideDir(resolved, claudeDir) || isInsideDir(resolved, agentsDir)) {
2207
+ return { decision: "block", reason: `Verifier may not modify agent definitions or framework settings: ${filePath}` };
2208
+ }
878
2209
  }
879
2210
  return {};
880
2211
  };
@@ -947,14 +2278,17 @@ ${taskPrompt}`;
947
2278
  if (artifactPath) {
948
2279
  console.log(`[${role}] Artifact written to ${artifactPath}`);
949
2280
  }
950
- const structured = await extractStructuredData(role, markdown);
2281
+ const { data: structured, tier: extractionTier } = await extractStructuredData(role, markdown);
951
2282
  if (structured) {
952
2283
  const { valid, missing } = validateForRole(role, structured);
953
2284
  if (!valid) {
954
2285
  console.warn(`[${role}] Output missing expected fields: ${missing.join(", ")}`);
955
2286
  }
956
2287
  }
957
- return { output: markdown, structured, truncated };
2288
+ if (extractionTier === 3) {
2289
+ console.warn(`[${role}] WARNING: Structured output was reconstructed by Haiku (tier 3). Data provenance degraded.`);
2290
+ }
2291
+ return { output: markdown, structured, truncated, extractionTier };
958
2292
  }
959
2293
  async function spawnSynthesiser(context, projectRoot, opts) {
960
2294
  const root = projectRoot ?? findProjectRoot() ?? process.cwd();
@@ -982,56 +2316,7 @@ ${taskPrompt}`;
982
2316
  role: "synthesiser"
983
2317
  });
984
2318
  console.log(`[synthesiser] Complete (cost: $${costUsd.toFixed(4)})`);
985
- return { output: markdown, structured: { guidance: markdown }, truncated };
986
- }
987
- async function spawnRecovery(role, partialOutput, context, projectRoot) {
988
- const root = projectRoot ?? findProjectRoot() ?? process.cwd();
989
- const expSlug = context.experiment?.slug ?? "unknown";
990
- console.log(`[recovery] Cleaning up after truncated ${role} for ${expSlug}...`);
991
- const expDocPath = path2.join(
992
- root,
993
- "docs",
994
- "experiments",
995
- `${String(context.experiment?.id ?? 0).padStart(3, "0")}-${expSlug}.md`
996
- );
997
- const templatePath = path2.join(root, "docs", "experiments", "_TEMPLATE.md");
998
- const template = fs2.existsSync(templatePath) ? fs2.readFileSync(templatePath, "utf-8") : "";
999
- const currentDoc = fs2.existsSync(expDocPath) ? fs2.readFileSync(expDocPath, "utf-8") : "";
1000
- const prompt = `The ${role} agent was truncated (hit max turns) while working on experiment "${expSlug}".
1001
-
1002
- Here is the partial agent output (reasoning + tool calls):
1003
- <partial_output>
1004
- ${partialOutput.slice(-3e3)}
1005
- </partial_output>
1006
-
1007
- Here is the current experiment doc:
1008
- <current_doc>
1009
- ${currentDoc}
1010
- </current_doc>
1011
-
1012
- Here is the template that the experiment doc should follow:
1013
- <template>
1014
- ${template}
1015
- </template>
1016
-
1017
- Your job: Write a CLEAN experiment doc to ${expDocPath} using the Write tool.
1018
- - Keep any valid content from the current doc
1019
- - Fill in what you can infer from the partial output
1020
- - Mark incomplete sections with "[TRUNCATED \u2014 ${role} did not finish]"
1021
- - The doc MUST have the <!-- majlis-json --> block, even if decisions are empty
1022
- - Do NOT include agent reasoning or thinking \u2014 only structured experiment content
1023
- - Be concise. This is cleanup, not new work.`;
1024
- const { text: _markdown } = await runQuery({
1025
- prompt,
1026
- model: "haiku",
1027
- tools: ["Read", "Write"],
1028
- systemPrompt: `You are a Recovery Agent. You clean up experiment docs after truncated agent runs. Write clean, structured docs. Never include agent reasoning or monologue.`,
1029
- cwd: root,
1030
- maxTurns: 5,
1031
- label: "recovery",
1032
- role: "recovery"
1033
- });
1034
- console.log(`[recovery] Cleanup complete for ${expSlug}.`);
2319
+ return { output: markdown, structured: { guidance: markdown }, truncated, extractionTier: null };
1035
2320
  }
1036
2321
  async function runQuery(opts) {
1037
2322
  let truncated = false;
@@ -1071,21 +2356,21 @@ async function runQuery(opts) {
1071
2356
  const toolName = block.name ?? "tool";
1072
2357
  const input = block.input ?? {};
1073
2358
  const detail = formatToolDetail(toolName, input);
1074
- process.stderr.write(`${DIM2}[${tag}] ${CYAN2}${toolName}${RESET2}${DIM2}${detail}${RESET2}
2359
+ process.stderr.write(`${DIM}[${tag}] ${CYAN}${toolName}${RESET}${DIM}${detail}${RESET}
1075
2360
  `);
1076
2361
  }
1077
2362
  }
1078
2363
  if (hasText) {
1079
2364
  const preview = textParts[textParts.length - 1].slice(0, 120).replace(/\n/g, " ").trim();
1080
2365
  if (preview) {
1081
- process.stderr.write(`${DIM2}[${tag}] writing: ${preview}${preview.length >= 120 ? "..." : ""}${RESET2}
2366
+ process.stderr.write(`${DIM}[${tag}] writing: ${preview}${preview.length >= 120 ? "..." : ""}${RESET}
1082
2367
  `);
1083
2368
  }
1084
2369
  }
1085
2370
  } else if (message.type === "tool_progress") {
1086
2371
  const elapsed = Math.round(message.elapsed_time_seconds);
1087
2372
  if (elapsed > 0 && elapsed % 5 === 0) {
1088
- process.stderr.write(`${DIM2}[${tag}] ${message.tool_name} running (${elapsed}s)...${RESET2}
2373
+ process.stderr.write(`${DIM}[${tag}] ${message.tool_name} running (${elapsed}s)...${RESET}
1089
2374
  `);
1090
2375
  }
1091
2376
  } else if (message.type === "result") {
@@ -1170,7 +2455,7 @@ function writeArtifact(role, context, markdown, projectRoot) {
1170
2455
  fs2.writeFileSync(target, markdown);
1171
2456
  return target;
1172
2457
  }
1173
- var fs2, path2, import_claude_agent_sdk2, ROLE_MAX_TURNS, CHECKPOINT_INTERVAL, DIM2, RESET2, CYAN2;
2458
+ var fs2, path2, import_claude_agent_sdk2, ROLE_MAX_TURNS, CHECKPOINT_INTERVAL;
1174
2459
  var init_spawn = __esm({
1175
2460
  "src/agents/spawn.ts"() {
1176
2461
  "use strict";
@@ -1179,6 +2464,7 @@ var init_spawn = __esm({
1179
2464
  import_claude_agent_sdk2 = require("@anthropic-ai/claude-agent-sdk");
1180
2465
  init_parse();
1181
2466
  init_connection();
2467
+ init_format();
1182
2468
  ROLE_MAX_TURNS = {
1183
2469
  builder: 50,
1184
2470
  critic: 30,
@@ -1203,9 +2489,6 @@ var init_spawn = __esm({
1203
2489
  cartographer: 12,
1204
2490
  toolsmith: 10
1205
2491
  };
1206
- DIM2 = "\x1B[2m";
1207
- RESET2 = "\x1B[0m";
1208
- CYAN2 = "\x1B[36m";
1209
2492
  }
1210
2493
  });
1211
2494
 
@@ -1285,7 +2568,8 @@ var init_config = __esm({
1285
2568
  synthesis: 3e4,
1286
2569
  fragility: 15e3,
1287
2570
  experimentDoc: 15e3,
1288
- deadEnds: 15e3
2571
+ deadEnds: 15e3,
2572
+ experimentLineage: 15e3
1289
2573
  };
1290
2574
  }
1291
2575
  });
@@ -2009,7 +3293,7 @@ var init_init = __esm({
2009
3293
  path7 = __toESM(require("path"));
2010
3294
  init_connection();
2011
3295
  init_format();
2012
- import_shared = require("@majlis/shared");
3296
+ import_shared = __toESM(require_dist());
2013
3297
  }
2014
3298
  });
2015
3299
 
@@ -2489,13 +3773,13 @@ function addSwarmMember(db, swarmRunId, slug, worktreePath) {
2489
3773
  INSERT INTO swarm_members (swarm_run_id, experiment_slug, worktree_path) VALUES (?, ?, ?)
2490
3774
  `).run(swarmRunId, slug, worktreePath);
2491
3775
  }
2492
- function updateSwarmMember(db, swarmRunId, slug, finalStatus, overallGrade, costUsd, error) {
3776
+ function updateSwarmMember(db, swarmRunId, slug, finalStatus, overallGrade, costUsd, error2) {
2493
3777
  db.prepare(`
2494
3778
  UPDATE swarm_members SET final_status = ?, overall_grade = ?, cost_usd = ?, error = ?
2495
3779
  WHERE swarm_run_id = ? AND experiment_slug = ?
2496
- `).run(finalStatus, overallGrade, costUsd, error, swarmRunId, slug);
3780
+ `).run(finalStatus, overallGrade, costUsd, error2, swarmRunId, slug);
2497
3781
  }
2498
- function exportForCompressor(db, maxLength = 3e4) {
3782
+ function exportForCompressor(db, maxLength = 5e4) {
2499
3783
  const experiments = listAllExperiments(db);
2500
3784
  const sections = ["# Structured Data Export (from SQLite)\n"];
2501
3785
  sections.push("## Experiments");
@@ -2561,6 +3845,70 @@ function exportForCompressor(db, maxLength = 3e4) {
2561
3845
  }
2562
3846
  return full;
2563
3847
  }
3848
+ function exportExperimentLineage(db, subType, maxLength = 15e3) {
3849
+ const experiments = subType ? db.prepare(`SELECT * FROM experiments WHERE sub_type = ? ORDER BY created_at`).all(subType) : listAllExperiments(db);
3850
+ if (experiments.length === 0) return "";
3851
+ const sections = ["## Experiment Lineage (from DB \u2014 canonical, not from synthesis)\n"];
3852
+ for (const exp of experiments) {
3853
+ sections.push(`### ${exp.slug} [${exp.status}]`);
3854
+ if (exp.hypothesis) sections.push(`Hypothesis: ${exp.hypothesis}`);
3855
+ const decisions = listDecisionsByExperiment(db, exp.id);
3856
+ if (decisions.length > 0) {
3857
+ sections.push("Decisions:");
3858
+ for (const d of decisions) {
3859
+ sections.push(` - [${d.evidence_level}/${d.status}] ${d.description}`);
3860
+ }
3861
+ }
3862
+ const beforeMetrics = getMetricsByExperimentAndPhase(db, exp.id, "before");
3863
+ const afterMetrics = getMetricsByExperimentAndPhase(db, exp.id, "after");
3864
+ if (beforeMetrics.length > 0 && afterMetrics.length > 0) {
3865
+ sections.push("Metrics:");
3866
+ for (const bm of beforeMetrics) {
3867
+ const am = afterMetrics.find((a) => a.fixture === bm.fixture && a.metric_name === bm.metric_name);
3868
+ if (am) {
3869
+ const delta = am.metric_value - bm.metric_value;
3870
+ const sign = delta >= 0 ? "+" : "";
3871
+ sections.push(` - ${bm.fixture}/${bm.metric_name}: ${bm.metric_value} \u2192 ${am.metric_value} (${sign}${delta.toFixed(4)})`);
3872
+ }
3873
+ }
3874
+ }
3875
+ const doubts = getDoubtsByExperiment(db, exp.id);
3876
+ const resolved = doubts.filter((d) => d.resolution);
3877
+ if (resolved.length > 0) {
3878
+ sections.push("Doubt resolutions:");
3879
+ for (const d of resolved) {
3880
+ sections.push(` - [${d.resolution}] ${d.claim_doubted}`);
3881
+ }
3882
+ }
3883
+ const verifications = getVerificationsByExperiment(db, exp.id);
3884
+ if (verifications.length > 0) {
3885
+ sections.push("Grades:");
3886
+ for (const v of verifications) {
3887
+ sections.push(` - ${v.component}: ${v.grade}${v.notes ? ` \u2014 ${v.notes}` : ""}`);
3888
+ }
3889
+ }
3890
+ sections.push("");
3891
+ const current = sections.join("\n");
3892
+ if (current.length > maxLength - 500) {
3893
+ sections.push(`[LINEAGE TRUNCATED \u2014 ${experiments.length - experiments.indexOf(exp) - 1} experiments omitted]`);
3894
+ break;
3895
+ }
3896
+ }
3897
+ const deadEnds = subType ? listDeadEndsBySubType(db, subType) : listAllDeadEnds(db);
3898
+ if (deadEnds.length > 0) {
3899
+ sections.push("### Dead Ends (structural constraints)");
3900
+ for (const de of deadEnds) {
3901
+ sections.push(`- [${de.category ?? "structural"}] ${de.approach}: ${de.structural_constraint}`);
3902
+ }
3903
+ }
3904
+ const full = sections.join("\n");
3905
+ if (full.length > maxLength) {
3906
+ return full.slice(0, maxLength) + `
3907
+
3908
+ [LINEAGE TRUNCATED at ${maxLength} chars]`;
3909
+ }
3910
+ return full;
3911
+ }
2564
3912
  function exportForDiagnostician(db, maxLength = 6e4) {
2565
3913
  const base = exportForCompressor(db, maxLength);
2566
3914
  const sections = [base];
@@ -2767,7 +4115,7 @@ var init_status = __esm({
2767
4115
  init_connection();
2768
4116
  init_queries();
2769
4117
  init_config();
2770
- import_shared2 = require("@majlis/shared");
4118
+ import_shared2 = __toESM(require_dist());
2771
4119
  init_format();
2772
4120
  }
2773
4121
  });
@@ -3694,7 +5042,7 @@ function gitMerge(branch, cwd) {
3694
5042
  stdio: ["pipe", "pipe", "pipe"]
3695
5043
  });
3696
5044
  } catch (err) {
3697
- console.warn(`[majlis] Git merge of ${branch} failed \u2014 you may need to merge manually.`);
5045
+ warn(`Git merge of ${branch} failed \u2014 you may need to merge manually.`);
3698
5046
  }
3699
5047
  }
3700
5048
  function gitRevert(branch, cwd) {
@@ -3723,7 +5071,7 @@ function gitRevert(branch, cwd) {
3723
5071
  }
3724
5072
  }
3725
5073
  } catch {
3726
- console.warn(`[majlis] Could not switch away from ${branch} \u2014 you may need to do this manually.`);
5074
+ warn(`Could not switch away from ${branch} \u2014 you may need to do this manually.`);
3727
5075
  }
3728
5076
  }
3729
5077
  function appendToFragilityMap(projectRoot, expSlug, gaps) {
@@ -3850,9 +5198,18 @@ Output your gate_decision as "approve", "reject", or "flag" with reasoning.`
3850
5198
  const decision = result.structured?.gate_decision ?? "approve";
3851
5199
  const reason = result.structured?.reason ?? "";
3852
5200
  if (decision === "reject") {
3853
- updateExperimentStatus(db, exp.id, "gated");
3854
- warn(`Gate REJECTED for ${exp.slug}: ${reason}`);
3855
- warn(`Revise the hypothesis or run \`majlis revert\` to abandon.`);
5201
+ insertDeadEnd(
5202
+ db,
5203
+ exp.id,
5204
+ exp.hypothesis ?? exp.slug,
5205
+ reason,
5206
+ `Gate rejected: ${reason}`,
5207
+ exp.sub_type,
5208
+ "procedural"
5209
+ );
5210
+ adminTransitionAndPersist(db, exp.id, "gated", "dead_end" /* DEAD_END */, "revert");
5211
+ warn(`Gate REJECTED for ${exp.slug}: ${reason}. Dead-ended.`);
5212
+ return;
3856
5213
  } else {
3857
5214
  if (decision === "flag") {
3858
5215
  warn(`Gate flagged concerns for ${exp.slug}: ${reason}`);
@@ -3909,6 +5266,10 @@ Build the experiment: ${exp.hypothesis}` : `Build the experiment: ${exp.hypothes
3909
5266
  }
3910
5267
  taskPrompt += "\n\nNote: The framework captures metrics automatically. Do NOT claim specific numbers unless quoting framework output.";
3911
5268
  const supplementaryContext = loadExperimentContext(exp, root);
5269
+ const lineage = exportExperimentLineage(db, exp.sub_type);
5270
+ if (lineage) {
5271
+ taskPrompt += "\n\n" + lineage;
5272
+ }
3912
5273
  const result = await spawnAgent("builder", {
3913
5274
  experiment: {
3914
5275
  id: exp.id,
@@ -3927,16 +5288,123 @@ Build the experiment: ${exp.hypothesis}` : `Build the experiment: ${exp.hypothes
3927
5288
  synthesis,
3928
5289
  confirmedDoubts,
3929
5290
  supplementaryContext: supplementaryContext || void 0,
5291
+ experimentLineage: lineage || void 0,
3930
5292
  taskPrompt
3931
5293
  }, root);
3932
5294
  ingestStructuredOutput(db, exp.id, result.structured);
5295
+ if (result.structured?.abandon) {
5296
+ insertDeadEnd(
5297
+ db,
5298
+ exp.id,
5299
+ exp.hypothesis ?? exp.slug,
5300
+ result.structured.abandon.reason,
5301
+ result.structured.abandon.structural_constraint,
5302
+ exp.sub_type,
5303
+ "structural"
5304
+ );
5305
+ adminTransitionAndPersist(db, exp.id, "building", "dead_end" /* DEAD_END */, "revert");
5306
+ info(`Builder abandoned ${exp.slug}: ${result.structured.abandon.reason}`);
5307
+ return;
5308
+ }
3933
5309
  if (result.truncated && !result.structured) {
3934
5310
  warn(`Builder was truncated (hit max turns) without producing structured output.`);
3935
- await spawnRecovery("builder", result.output, {
3936
- experiment: { id: exp.id, slug: exp.slug, hypothesis: exp.hypothesis, status: "building", sub_type: exp.sub_type, builder_guidance: null }
3937
- }, root);
3938
- warn(`Experiment stays at 'building'. Run \`majlis build\` to retry or \`majlis revert\` to abandon.`);
5311
+ const recovery = await extractStructuredData("builder", result.output);
5312
+ if (recovery.data && !recovery.data.abandon) {
5313
+ info(`Recovered structured output from truncated builder (tier ${recovery.tier}).`);
5314
+ ingestStructuredOutput(db, exp.id, recovery.data);
5315
+ if (config.build?.pre_measure) {
5316
+ try {
5317
+ const [cmd, ...cmdArgs] = config.build.pre_measure.split(/\s+/);
5318
+ (0, import_node_child_process7.execFileSync)(cmd, cmdArgs, {
5319
+ cwd: root,
5320
+ encoding: "utf-8",
5321
+ timeout: 3e4,
5322
+ stdio: ["pipe", "pipe", "pipe"]
5323
+ });
5324
+ } catch (err) {
5325
+ const errMsg = err instanceof Error ? err.message : String(err);
5326
+ storeBuilderGuidance(
5327
+ db,
5328
+ exp.id,
5329
+ `Build verification failed after truncated recovery.
5330
+ Error: ${errMsg.slice(0, 500)}`
5331
+ );
5332
+ warn(`Build verification failed for ${exp.slug}. Staying at 'building'.`);
5333
+ return;
5334
+ }
5335
+ }
5336
+ if (config.metrics?.command) {
5337
+ try {
5338
+ const output = (0, import_node_child_process7.execSync)(config.metrics.command, {
5339
+ cwd: root,
5340
+ encoding: "utf-8",
5341
+ timeout: 6e4,
5342
+ stdio: ["pipe", "pipe", "pipe"]
5343
+ }).trim();
5344
+ const parsed = parseMetricsOutput(output);
5345
+ for (const m of parsed) {
5346
+ insertMetric(db, exp.id, "after", m.fixture, m.metric_name, m.metric_value);
5347
+ }
5348
+ if (parsed.length > 0) info(`Captured ${parsed.length} post-build metric(s).`);
5349
+ } catch {
5350
+ }
5351
+ }
5352
+ gitCommitBuild(exp, root);
5353
+ if (recovery.tier === 3) {
5354
+ warn(`Builder output extracted via Haiku (tier 3). Data provenance degraded.`);
5355
+ const existing = getBuilderGuidance(db, exp.id) ?? "";
5356
+ storeBuilderGuidance(
5357
+ db,
5358
+ exp.id,
5359
+ existing + "\n[PROVENANCE WARNING] Builder structured output was reconstructed by a secondary model (tier 3). Treat reported decisions with additional scrutiny."
5360
+ );
5361
+ }
5362
+ updateExperimentStatus(db, exp.id, "built");
5363
+ success(`Build complete for ${exp.slug} (recovered from truncation). Run \`majlis doubt\` or \`majlis challenge\` next.`);
5364
+ } else if (recovery.data?.abandon) {
5365
+ insertDeadEnd(
5366
+ db,
5367
+ exp.id,
5368
+ exp.hypothesis ?? exp.slug,
5369
+ recovery.data.abandon.reason,
5370
+ recovery.data.abandon.structural_constraint,
5371
+ exp.sub_type,
5372
+ "structural"
5373
+ );
5374
+ adminTransitionAndPersist(db, exp.id, "building", "dead_end" /* DEAD_END */, "revert");
5375
+ info(`Builder abandoned ${exp.slug} (recovered from truncation): ${recovery.data.abandon.reason}`);
5376
+ } else {
5377
+ const tail = result.output.slice(-2e3).trim();
5378
+ if (tail) {
5379
+ storeBuilderGuidance(
5380
+ db,
5381
+ exp.id,
5382
+ `Builder was truncated. Last ~2000 chars of output:
5383
+ ${tail}`
5384
+ );
5385
+ }
5386
+ warn(`Experiment stays at 'building'. Run \`majlis build\` to retry or \`majlis revert\` to abandon.`);
5387
+ }
3939
5388
  } else {
5389
+ if (config.build?.pre_measure) {
5390
+ try {
5391
+ const [cmd, ...cmdArgs] = config.build.pre_measure.split(/\s+/);
5392
+ (0, import_node_child_process7.execFileSync)(cmd, cmdArgs, {
5393
+ cwd: root,
5394
+ encoding: "utf-8",
5395
+ timeout: 3e4,
5396
+ stdio: ["pipe", "pipe", "pipe"]
5397
+ });
5398
+ } catch (err) {
5399
+ const errMsg = err instanceof Error ? err.message : String(err);
5400
+ const guidance = `Build verification failed after builder completion. Code may be syntactically broken or incomplete.
5401
+ Error: ${errMsg.slice(0, 500)}`;
5402
+ storeBuilderGuidance(db, exp.id, guidance);
5403
+ warn(`Build verification failed for ${exp.slug}. Staying at 'building'.`);
5404
+ warn(`Guidance stored for retry. Run \`majlis build\` to retry.`);
5405
+ return;
5406
+ }
5407
+ }
3940
5408
  if (config.metrics?.command) {
3941
5409
  try {
3942
5410
  const output = (0, import_node_child_process7.execSync)(config.metrics.command, {
@@ -3955,6 +5423,15 @@ Build the experiment: ${exp.hypothesis}` : `Build the experiment: ${exp.hypothes
3955
5423
  }
3956
5424
  }
3957
5425
  gitCommitBuild(exp, root);
5426
+ if (result.extractionTier === 3) {
5427
+ warn(`Builder output extracted via Haiku (tier 3). Data provenance degraded.`);
5428
+ const existing = getBuilderGuidance(db, exp.id) ?? "";
5429
+ storeBuilderGuidance(
5430
+ db,
5431
+ exp.id,
5432
+ existing + "\n[PROVENANCE WARNING] Builder structured output was reconstructed by a secondary model (tier 3). Treat reported decisions with additional scrutiny."
5433
+ );
5434
+ }
3958
5435
  updateExperimentStatus(db, exp.id, "built");
3959
5436
  success(`Build complete for ${exp.slug}. Run \`majlis doubt\` or \`majlis challenge\` next.`);
3960
5437
  }
@@ -4150,6 +5627,15 @@ async function doVerify(db, exp, root) {
4150
5627
  }
4151
5628
  updateExperimentStatus(db, exp.id, "verifying");
4152
5629
  const verifierSupplementaryContext = loadExperimentContext(exp, root);
5630
+ const verifierLineage = exportExperimentLineage(db, exp.sub_type);
5631
+ let verifierTaskPrompt = `Verify experiment ${exp.slug}: ${exp.hypothesis}. Check provenance and content. Test the ${doubts.length} doubt(s) and any adversarial challenges.` + metricsSection + doubtReference;
5632
+ if (verifierLineage) {
5633
+ verifierTaskPrompt += "\n\n" + verifierLineage;
5634
+ }
5635
+ const builderGuidanceForVerifier = getBuilderGuidance(db, exp.id);
5636
+ if (builderGuidanceForVerifier?.includes("[PROVENANCE WARNING]")) {
5637
+ verifierTaskPrompt += "\n\nNote: The builder's structured output was reconstructed by a secondary model (tier 3). Treat reported decisions with additional scrutiny.";
5638
+ }
4153
5639
  const result = await spawnAgent("verifier", {
4154
5640
  experiment: {
4155
5641
  id: exp.id,
@@ -4163,7 +5649,8 @@ async function doVerify(db, exp, root) {
4163
5649
  challenges,
4164
5650
  metricComparisons: metricComparisons.length > 0 ? metricComparisons : void 0,
4165
5651
  supplementaryContext: verifierSupplementaryContext || void 0,
4166
- taskPrompt: `Verify experiment ${exp.slug}: ${exp.hypothesis}. Check provenance and content. Test the ${doubts.length} doubt(s) and any adversarial challenges.` + metricsSection + doubtReference
5652
+ experimentLineage: verifierLineage || void 0,
5653
+ taskPrompt: verifierTaskPrompt
4167
5654
  }, root);
4168
5655
  ingestStructuredOutput(db, exp.id, result.structured);
4169
5656
  if (result.truncated && !result.structured) {
@@ -4325,6 +5812,7 @@ var init_cycle = __esm({
4325
5812
  init_machine();
4326
5813
  init_types2();
4327
5814
  init_spawn();
5815
+ init_parse();
4328
5816
  init_resolve();
4329
5817
  init_config();
4330
5818
  init_metrics();
@@ -5432,6 +6920,7 @@ async function swarm(args) {
5432
6920
  if (summary.bestExperiment && isMergeable(summary.bestExperiment.overallGrade)) {
5433
6921
  const best = summary.bestExperiment;
5434
6922
  info(`Best experiment: ${best.worktree.slug} (${best.overallGrade})`);
6923
+ let merged = false;
5435
6924
  try {
5436
6925
  (0, import_node_child_process10.execFileSync)(
5437
6926
  "git",
@@ -5439,9 +6928,72 @@ async function swarm(args) {
5439
6928
  { cwd: root, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
5440
6929
  );
5441
6930
  success(`Merged ${best.worktree.slug} into main.`);
6931
+ merged = true;
5442
6932
  } catch {
5443
- warn(`Git merge of ${best.worktree.slug} failed. Merge manually with:`);
5444
- info(` git merge ${best.worktree.branch} --no-ff`);
6933
+ warn(`Git merge of ${best.worktree.slug} failed (conflict). Attempting rebase...`);
6934
+ try {
6935
+ (0, import_node_child_process10.execFileSync)("git", ["merge", "--abort"], { cwd: root, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
6936
+ } catch {
6937
+ }
6938
+ try {
6939
+ (0, import_node_child_process10.execFileSync)(
6940
+ "git",
6941
+ ["rebase", "main", best.worktree.branch],
6942
+ { cwd: root, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
6943
+ );
6944
+ info(`Rebase of ${best.worktree.slug} onto main succeeded. Re-verifying gates...`);
6945
+ const config = loadConfig(root);
6946
+ let gatesHold = true;
6947
+ if (config.metrics?.command && best.experiment) {
6948
+ try {
6949
+ const output = (0, import_node_child_process10.execSync)(config.metrics.command, {
6950
+ cwd: root,
6951
+ encoding: "utf-8",
6952
+ timeout: 6e4,
6953
+ stdio: ["pipe", "pipe", "pipe"]
6954
+ }).trim();
6955
+ const parsed = parseMetricsOutput(output);
6956
+ for (const m of parsed) {
6957
+ insertMetric(db, best.experiment.id, "after", m.fixture, m.metric_name, m.metric_value);
6958
+ }
6959
+ const comparisons = compareMetrics(db, best.experiment.id, config);
6960
+ const gateViolations = checkGateViolations(comparisons);
6961
+ if (gateViolations.length > 0) {
6962
+ gatesHold = false;
6963
+ warn(`Gate violations after rebase:`);
6964
+ for (const v of gateViolations) {
6965
+ warn(` - ${v.fixture}/${v.metric}: ${v.before} \u2192 ${v.after} (delta: ${v.delta})`);
6966
+ }
6967
+ }
6968
+ } catch {
6969
+ warn("Could not re-capture metrics after rebase. Proceeding cautiously.");
6970
+ }
6971
+ }
6972
+ if (gatesHold) {
6973
+ (0, import_node_child_process10.execFileSync)("git", ["checkout", "main"], { cwd: root, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
6974
+ (0, import_node_child_process10.execFileSync)(
6975
+ "git",
6976
+ ["merge", "--ff-only", best.worktree.branch],
6977
+ { cwd: root, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
6978
+ );
6979
+ success(`Merged ${best.worktree.slug} into main (via rebase + ff).`);
6980
+ merged = true;
6981
+ } else {
6982
+ warn(`Gate violations after rebase. NOT merging ${best.worktree.slug}.`);
6983
+ info(`Manual resolution needed:`);
6984
+ info(` git checkout main && git merge ${best.worktree.branch} --no-ff`);
6985
+ }
6986
+ } catch {
6987
+ try {
6988
+ (0, import_node_child_process10.execFileSync)("git", ["rebase", "--abort"], { cwd: root, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
6989
+ } catch {
6990
+ }
6991
+ warn(`Rebase of ${best.worktree.slug} also failed. Manual merge required:`);
6992
+ info(` git merge ${best.worktree.branch} --no-ff`);
6993
+ }
6994
+ }
6995
+ if (!merged) {
6996
+ info(`${best.worktree.slug} was NOT merged automatically.`);
5445
6997
  }
5446
6998
  } else {
5447
6999
  info("No experiment achieved sound/good grade. Nothing merged.");
@@ -5594,6 +7146,7 @@ var init_swarm = __esm({
5594
7146
  init_types2();
5595
7147
  init_spawn();
5596
7148
  init_config();
7149
+ init_metrics();
5597
7150
  init_worktree();
5598
7151
  init_runner();
5599
7152
  init_aggregate();
@@ -6165,6 +7718,7 @@ var init_resync = __esm({
6165
7718
  // src/cli.ts
6166
7719
  var fs22 = __toESM(require("fs"));
6167
7720
  var path23 = __toESM(require("path"));
7721
+ init_format();
6168
7722
  var VERSION2 = JSON.parse(
6169
7723
  fs22.readFileSync(path23.join(__dirname, "..", "package.json"), "utf-8")
6170
7724
  ).version;
@@ -6175,7 +7729,7 @@ async function main() {
6175
7729
  if (sigintCount >= 2) process.exit(130);
6176
7730
  const { requestShutdown: requestShutdown2 } = (init_shutdown(), __toCommonJS(shutdown_exports));
6177
7731
  requestShutdown2();
6178
- console.error("\n\x1B[33m[majlis] Interrupt received. Finishing current step...\x1B[0m");
7732
+ warn("Interrupt received. Finishing current step...");
6179
7733
  });
6180
7734
  const args = process.argv.slice(2);
6181
7735
  if (args.includes("--version") || args.includes("-v")) {
@@ -6314,7 +7868,7 @@ async function main() {
6314
7868
  }
6315
7869
  } catch (err) {
6316
7870
  const msg = err instanceof Error ? err.message : String(err);
6317
- console.error(`\x1B[31m[majlis] Error: ${msg}\x1B[0m`);
7871
+ error(`Error: ${msg}`);
6318
7872
  process.exit(1);
6319
7873
  }
6320
7874
  }