self-evolving-workflow 0.0.0-stage → 0.1.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 (173) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +90 -2
  3. package/dist/agent-memory-prompts.d.ts +13 -0
  4. package/dist/agent-memory-prompts.js +55 -0
  5. package/dist/cli.d.ts +2 -0
  6. package/dist/cli.js +6 -0
  7. package/dist/closeout.d.ts +105 -0
  8. package/dist/closeout.js +479 -0
  9. package/dist/code.d.ts +39 -0
  10. package/dist/code.js +124 -0
  11. package/dist/common.d.ts +9 -0
  12. package/dist/common.js +42 -0
  13. package/dist/context-envelope.d.ts +59 -0
  14. package/dist/context-envelope.js +23 -0
  15. package/dist/host-adapters.d.ts +34 -0
  16. package/dist/host-adapters.js +151 -0
  17. package/dist/http.d.ts +3 -0
  18. package/dist/http.js +90 -0
  19. package/dist/index.d.ts +16 -0
  20. package/dist/index.js +13 -0
  21. package/dist/knowledge-cli.d.ts +183 -0
  22. package/dist/knowledge-cli.js +654 -0
  23. package/dist/knowledge-reads.d.ts +71 -0
  24. package/dist/knowledge-reads.js +193 -0
  25. package/dist/memory.d.ts +97 -0
  26. package/dist/memory.js +250 -0
  27. package/dist/retrospective.d.ts +57 -0
  28. package/dist/retrospective.js +248 -0
  29. package/dist/search.d.ts +36 -0
  30. package/dist/search.js +112 -0
  31. package/dist/skill-addition.d.ts +25 -0
  32. package/dist/skill-addition.js +89 -0
  33. package/dist/skill-design.d.ts +96 -0
  34. package/dist/skill-design.js +226 -0
  35. package/dist/skill-replay.d.ts +55 -0
  36. package/dist/skill-replay.js +451 -0
  37. package/dist/skill-route.d.ts +30 -0
  38. package/dist/skill-route.js +75 -0
  39. package/dist/transcript-evidence.d.ts +3 -0
  40. package/dist/transcript-evidence.js +58 -0
  41. package/dist/upstream/chunker.d.ts +24 -0
  42. package/dist/upstream/chunker.js +96 -0
  43. package/dist/upstream/file-protocol.d.ts +45 -0
  44. package/dist/upstream/file-protocol.js +117 -0
  45. package/dist/upstream/frontmatter.d.ts +47 -0
  46. package/dist/upstream/frontmatter.js +86 -0
  47. package/dist/upstream/graph-search.d.ts +33 -0
  48. package/dist/upstream/graph-search.js +67 -0
  49. package/dist/upstream/l1-extraction.d.ts +26 -0
  50. package/dist/upstream/l1-extraction.js +405 -0
  51. package/dist/upstream/llm.d.ts +8 -0
  52. package/dist/upstream/llm.js +1 -0
  53. package/dist/upstream/merge.d.ts +50 -0
  54. package/dist/upstream/merge.js +153 -0
  55. package/dist/upstream/prompts.d.ts +53 -0
  56. package/dist/upstream/prompts.js +185 -0
  57. package/dist/upstream/search-utils.d.ts +34 -0
  58. package/dist/upstream/search-utils.js +55 -0
  59. package/dist/upstream/slug.d.ts +26 -0
  60. package/dist/upstream/slug.js +103 -0
  61. package/dist/upstream/template.d.ts +29 -0
  62. package/dist/upstream/template.js +80 -0
  63. package/dist/upstream/tokenize.d.ts +4 -0
  64. package/dist/upstream/tokenize.js +29 -0
  65. package/dist/wiki-change.d.ts +76 -0
  66. package/dist/wiki-change.js +299 -0
  67. package/dist/wiki-entry.d.ts +26 -0
  68. package/dist/wiki-entry.js +93 -0
  69. package/dist/wiki-ingest.d.ts +12 -0
  70. package/dist/wiki-ingest.js +126 -0
  71. package/dist/wiki-migration.d.ts +16 -0
  72. package/dist/wiki-migration.js +268 -0
  73. package/dist/wiki-tokens.d.ts +1 -0
  74. package/dist/wiki-tokens.js +46 -0
  75. package/dist/wiki.d.ts +103 -0
  76. package/dist/wiki.js +330 -0
  77. package/dist/workflow-cli.d.ts +2 -0
  78. package/dist/workflow-cli.js +258 -0
  79. package/dist/workflow-install.d.ts +36 -0
  80. package/dist/workflow-install.js +148 -0
  81. package/dist/workflow-knowledge.d.ts +16 -0
  82. package/dist/workflow-knowledge.js +24 -0
  83. package/dist/workflow-mcp.d.ts +2 -0
  84. package/dist/workflow-mcp.js +144 -0
  85. package/dist/workflow-names.d.ts +4 -0
  86. package/dist/workflow-names.js +18 -0
  87. package/dist/workflow-runtime.d.ts +622 -0
  88. package/dist/workflow-runtime.js +627 -0
  89. package/dist/workflow-skills.d.ts +48 -0
  90. package/dist/workflow-skills.js +141 -0
  91. package/package.json +76 -4
  92. package/skills/ew-odyssey/SKILL.md +237 -0
  93. package/skills/ew-odyssey/references/audit-artifacts.md +7 -0
  94. package/skills/ew-odyssey/references/odyssey-base-codex.md +3 -0
  95. package/skills/ew-odyssey/references/odyssey-base.md +278 -0
  96. package/skills/ew-odyssey/references/odyssey-debug.codex.md +305 -0
  97. package/skills/ew-odyssey/references/odyssey-debug.md +283 -0
  98. package/skills/ew-odyssey/references/odyssey-defensive.codex.md +395 -0
  99. package/skills/ew-odyssey/references/odyssey-defensive.md +369 -0
  100. package/skills/ew-odyssey/references/odyssey-improve.codex.md +270 -0
  101. package/skills/ew-odyssey/references/odyssey-improve.md +248 -0
  102. package/skills/ew-odyssey/references/odyssey-planex.codex.md +352 -0
  103. package/skills/ew-odyssey/references/odyssey-planex.md +312 -0
  104. package/skills/ew-odyssey/references/odyssey-review.codex.md +283 -0
  105. package/skills/ew-odyssey/references/odyssey-review.md +251 -0
  106. package/skills/ew-odyssey/references/odyssey-security.md +249 -0
  107. package/skills/ew-odyssey/references/odyssey-ui.codex.md +294 -0
  108. package/skills/ew-odyssey/references/odyssey-ui.md +250 -0
  109. package/skills/ew-odyssey/references/prepare/odyssey-debug.md +105 -0
  110. package/skills/ew-odyssey/references/prepare/odyssey-defensive.md +121 -0
  111. package/skills/ew-odyssey/references/prepare/odyssey-improve.md +93 -0
  112. package/skills/ew-odyssey/references/prepare/odyssey-planex.md +104 -0
  113. package/skills/ew-odyssey/references/prepare/odyssey-review.md +98 -0
  114. package/skills/ew-odyssey/references/prepare/odyssey-security.md +94 -0
  115. package/skills/ew-odyssey/references/prepare/odyssey-ui.md +87 -0
  116. package/skills/ew-orchestrate/SKILL.md +236 -0
  117. package/skills/ew-orchestrate/references/auto-policy.md +165 -0
  118. package/skills/ew-orchestrate/references/companion-route.md +52 -0
  119. package/skills/ew-orchestrate/references/ew-orchestrate.md +27 -0
  120. package/skills/ew-orchestrate/references/goal.md +53 -0
  121. package/skills/ew-orchestrate/references/knowledge-route.md +163 -0
  122. package/skills/ew-orchestrate/references/orchestrator-run-loop.md +36 -0
  123. package/skills/ew-orchestrate/references/planning.md +137 -0
  124. package/skills/ew-orchestrate/steps/ew-analyze/SKILL.md +62 -0
  125. package/skills/ew-orchestrate/steps/ew-analyze/references/boundary-grill.md +50 -0
  126. package/skills/ew-orchestrate/steps/ew-analyze/references/issue-gaps-analyze.md +59 -0
  127. package/skills/ew-orchestrate/steps/ew-analyze/references/prepare.md +42 -0
  128. package/skills/ew-orchestrate/steps/ew-analyze/references/workflow.md +401 -0
  129. package/skills/ew-orchestrate/steps/ew-brainstorm/SKILL.md +32 -0
  130. package/skills/ew-orchestrate/steps/ew-companion/SKILL.md +9 -0
  131. package/skills/ew-orchestrate/steps/ew-execute/SKILL.md +71 -0
  132. package/skills/ew-orchestrate/steps/ew-execute/references/prepare.md +69 -0
  133. package/skills/ew-orchestrate/steps/ew-execute/references/workflow.md +373 -0
  134. package/skills/ew-orchestrate/steps/ew-plan/SKILL.md +102 -0
  135. package/skills/ew-orchestrate/steps/ew-plan/references/boundary-grill.md +50 -0
  136. package/skills/ew-orchestrate/steps/ew-plan/references/prepare.md +51 -0
  137. package/skills/ew-orchestrate/steps/ew-plan/references/tdd.md +53 -0
  138. package/skills/ew-orchestrate/steps/ew-plan/references/workflow.md +278 -0
  139. package/skills/ew-orchestrate/steps/ew-review/SKILL.md +44 -0
  140. package/skills/ew-orchestrate/steps/ew-review/references/odyssey-base-codex.md +3 -0
  141. package/skills/ew-orchestrate/steps/ew-review/references/prepare.md +76 -0
  142. package/skills/ew-orchestrate/steps/ew-review/references/workflow.md +283 -0
  143. package/skills/ew-skill-design/SKILL.md +28 -0
  144. package/skills/ew-skill-design/references/package-contract.md +28 -0
  145. package/skills/ew-skill-design/references/validation.md +7 -0
  146. package/skills/team-swarm/SKILL.md +231 -0
  147. package/skills/team-swarm/references/mcp-coordination.md +10 -0
  148. package/skills/team-swarm/roles/analyst/role.md +181 -0
  149. package/skills/team-swarm/roles/ant/role.md +149 -0
  150. package/skills/team-swarm/roles/coordinator/commands/converge.md +154 -0
  151. package/skills/team-swarm/roles/coordinator/commands/init-swarm.md +134 -0
  152. package/skills/team-swarm/roles/coordinator/commands/iterate.md +202 -0
  153. package/skills/team-swarm/roles/coordinator/role.md +228 -0
  154. package/skills/team-swarm/roles/scorer/role.md +150 -0
  155. package/skills/team-swarm/scripts/aco.py +646 -0
  156. package/skills/team-swarm/scripts/pheromone.py +144 -0
  157. package/skills/team-swarm/scripts/scoring.py +111 -0
  158. package/skills/team-swarm/scripts/test_aco.py +606 -0
  159. package/skills/team-swarm/specs/ant-output-schema.md +127 -0
  160. package/skills/team-swarm/specs/convergence-criteria.md +108 -0
  161. package/skills/team-swarm/specs/pheromone-schema.md +125 -0
  162. package/skills/team-swarm/specs/swarm-config-template.json +71 -0
  163. package/skills/team-swarm/specs/swarm-protocol.md +123 -0
  164. package/skills/workflow-closeout/SKILL.md +10 -0
  165. package/skills/workflow-evolve/SKILL.md +79 -0
  166. package/skills/workflow-evolve/references/agy-gate.md +27 -0
  167. package/skills/workflow-evolve/references/auto-loop.md +20 -0
  168. package/skills/workflow-evolve/references/history-review.md +14 -0
  169. package/skills/workflow-evolve/scripts/codex-memory-replay.mjs +76 -0
  170. package/skills/workflow-evolve/scripts/codex-replay.mjs +1 -0
  171. package/skills/workflow-evolve/scripts/evolve-loop.mjs +259 -0
  172. package/skills/workflow-evolve/scripts/memory-codex-mcp.mjs +124 -0
  173. package/workflows/index.json +23 -0
@@ -0,0 +1,606 @@
1
+ """End-to-end tests for team-swarm scripts.
2
+
3
+ Runs each scenario in a clean tmp directory and asserts on outputs.
4
+ No external test framework — runnable as `python test_aco.py`.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import json
9
+ import subprocess
10
+ import sys
11
+ import tempfile
12
+ from pathlib import Path
13
+ from typing import Optional
14
+
15
+ SCRIPT_DIR = Path(__file__).parent
16
+ ACO = SCRIPT_DIR / "aco.py"
17
+
18
+ # Import modules directly for unit-level tests
19
+ sys.path.insert(0, str(SCRIPT_DIR))
20
+ from pheromone import PheromoneState, edge_key # noqa: E402
21
+ from scoring import FallbackScorer, ScriptScorer, hallucination_check, resolve_score # noqa: E402
22
+
23
+
24
+ # ---------------------------------------------------------------------------
25
+ # Helpers
26
+ # ---------------------------------------------------------------------------
27
+
28
+ PASS = 0
29
+ FAIL = 0
30
+ FAILED_NAMES = []
31
+
32
+
33
+ def run_aco(session: Path, *args, expect_exit: int = 0) -> dict:
34
+ """Invoke aco.py CLI, return parsed stdout JSON."""
35
+ cmd = [sys.executable, "-B", str(ACO), "--session", str(session), *args]
36
+ proc = subprocess.run(cmd, capture_output=True, text=True)
37
+ if proc.returncode != expect_exit:
38
+ raise AssertionError(
39
+ f"exit={proc.returncode} (expected {expect_exit})\n"
40
+ f"cmd: {' '.join(cmd)}\nstdout: {proc.stdout}\nstderr: {proc.stderr}"
41
+ )
42
+ if not proc.stdout.strip():
43
+ return {}
44
+ return json.loads(proc.stdout.strip().splitlines()[-1])
45
+
46
+
47
+ def check(name: str, cond: bool, detail: str = ""):
48
+ global PASS, FAIL
49
+ if cond:
50
+ PASS += 1
51
+ print(f" PASS {name}")
52
+ else:
53
+ FAIL += 1
54
+ FAILED_NAMES.append(name)
55
+ print(f" FAIL {name} {detail}")
56
+
57
+
58
+ def section(title: str):
59
+ print(f"\n=== {title} ===")
60
+
61
+
62
+ def write_config(session: Path, overrides: Optional[dict] = None) -> dict:
63
+ cfg = {
64
+ "swarm": {"n_ants": 3, "max_iterations": 3, "elite_keep": 2},
65
+ "aco": {"alpha": 1.0, "beta": 2.0, "rho": 0.2, "q": 1.0,
66
+ "tau_init": 1.0, "tau_min": 0.01, "tau_max": 10.0},
67
+ "task_space": {"type": "graph",
68
+ "nodes": ["a", "b", "c", "d", "e"],
69
+ "max_path_length": 3,
70
+ "start_nodes": "any",
71
+ "edges": "complete"},
72
+ "scoring": {"mode": "fallback", "self_score_discount": 0.5},
73
+ "ant_prompt": {"objective": "test", "evidence_requirements": []},
74
+ "convergence": {
75
+ "max_iterations": 3,
76
+ "stagnation": {"enabled": True, "patience": 2, "min_delta": 0.01},
77
+ "entropy_floor": {"enabled": True, "threshold": 0.1},
78
+ "target_score": {"enabled": True, "value": 0.95},
79
+ },
80
+ }
81
+ if overrides:
82
+ _deep_merge(cfg, overrides)
83
+ session.mkdir(parents=True, exist_ok=True)
84
+ (session / "swarm-config.json").write_text(json.dumps(cfg))
85
+ return cfg
86
+
87
+
88
+ def _deep_merge(base: dict, overrides: dict):
89
+ for k, v in overrides.items():
90
+ if isinstance(v, dict) and isinstance(base.get(k), dict):
91
+ _deep_merge(base[k], v)
92
+ else:
93
+ base[k] = v
94
+
95
+
96
+ def write_ant(session: Path, iteration: int, ant_idx: int,
97
+ path: list, self_score: float = 0.6, self_confidence: float = 0.7,
98
+ outputs: Optional[Path] = None) -> Path:
99
+ artifacts = outputs if outputs is not None else session / "artifacts"
100
+ artifacts.mkdir(exist_ok=True)
101
+ decisions = [
102
+ {"from": path[i], "to": path[i + 1], "rationale": "r",
103
+ "guided_by": "pheromone", "deviation_from_hint": False}
104
+ for i in range(len(path) - 1)
105
+ ]
106
+ art = {
107
+ **({"_meta": {"kind": "swarm-ant-result", "schema": "swarm-ant-result/1.0",
108
+ "role": "evidence"}} if outputs is not None else {}),
109
+ "schema_version": "1.0",
110
+ "ant_id": f"ANT-{iteration}-{ant_idx}",
111
+ "iteration": iteration,
112
+ "assignment": {"start_node": path[0], "max_path_length": 3},
113
+ "path": path,
114
+ "path_decisions": decisions,
115
+ "self_score": self_score,
116
+ "self_confidence": self_confidence,
117
+ "evidence": [f"src/{path[-1]}.ts:{ant_idx}"],
118
+ "candidate_solution": {"type": "string", "summary": f"sol-{ant_idx}",
119
+ "content": str(path)},
120
+ }
121
+ p = artifacts / f"ant-{iteration}-{ant_idx}.json"
122
+ p.write_text(json.dumps(art))
123
+ return p
124
+
125
+
126
+ # ---------------------------------------------------------------------------
127
+ # Unit tests — pheromone.py
128
+ # ---------------------------------------------------------------------------
129
+
130
+ def test_pheromone_unit():
131
+ section("pheromone.py unit")
132
+
133
+ s = PheromoneState.initialize(["a", "b", "c"], {})
134
+ check("init creates n*(n-1)/2 edges", len(s.tau) == 3, f"got {len(s.tau)}")
135
+ check("init uses default alpha=1.0", s.metadata["alpha"] == 1.0)
136
+ check("init uses default rho=0.2", s.metadata["rho"] == 0.2)
137
+ check("init all tau equal", len(set(s.tau.values())) == 1)
138
+
139
+ s.evaporate()
140
+ check("evaporate reduces by rho", abs(s.tau[edge_key("a", "b")] - 0.8) < 1e-9,
141
+ f"got {s.tau[edge_key('a', 'b')]}")
142
+
143
+ s.deposit(["a", "b", "c"], 0.5)
144
+ expected_ab = 0.8 + 0.5 * 1.0
145
+ check("deposit adds q*score per edge",
146
+ abs(s.tau[edge_key("a", "b")] - expected_ab) < 1e-9,
147
+ f"got {s.tau[edge_key('a', 'b')]}, expected {expected_ab}")
148
+
149
+ s.metadata["tau_max"] = 2.0
150
+ s.tau[edge_key("a", "b")] = 100.0
151
+ s.clip()
152
+ check("clip enforces tau_max", s.tau[edge_key("a", "b")] == 2.0)
153
+ check("clip enforces tau_min on small values",
154
+ all(v >= s.metadata["tau_min"] for v in s.tau.values()))
155
+
156
+ stats = s.stats()
157
+ check("stats has entropy field", "entropy" in stats)
158
+ check("stats entropy is positive", stats["entropy"] > 0)
159
+
160
+ probs = s.select_neighbors("a", ["a", "b", "c"])
161
+ check("select_neighbors excludes current node", "a" not in probs)
162
+ check("select_neighbors probs sum to 1",
163
+ abs(sum(probs.values()) - 1.0) < 1e-9, f"got {sum(probs.values())}")
164
+
165
+ empty = PheromoneState.initialize(["a"], {})
166
+ check("single-node init produces 0 edges", len(empty.tau) == 0)
167
+ check("empty stats handles 0-edge case", empty.stats()["entropy"] == 0.0)
168
+
169
+ s2 = PheromoneState.initialize(["a", "b", "c"], {})
170
+ with tempfile.TemporaryDirectory() as td:
171
+ p = Path(td) / "p.json"
172
+ s2.save(p)
173
+ s3 = PheromoneState.load(p)
174
+ check("save/load roundtrip preserves tau", s2.tau == s3.tau)
175
+ check("save/load preserves metadata", s2.metadata == s3.metadata)
176
+
177
+
178
+ # ---------------------------------------------------------------------------
179
+ # Unit tests — scoring.py
180
+ # ---------------------------------------------------------------------------
181
+
182
+ def test_scoring_unit():
183
+ section("scoring.py unit")
184
+
185
+ fb = FallbackScorer(discount=0.5)
186
+ artifact = {"self_score": 0.8, "self_confidence": 0.6}
187
+ expected = 0.8 * 0.6 * 0.5
188
+ check("FallbackScorer = self * conf * discount",
189
+ abs(fb.score(artifact) - expected) < 1e-9)
190
+
191
+ artifact_missing = {}
192
+ check("FallbackScorer handles missing fields",
193
+ fb.score(artifact_missing) == 0.0)
194
+
195
+ with tempfile.TemporaryDirectory() as td:
196
+ rule = Path(td) / "rule.py"
197
+ rule.write_text("def score(ant_artifact):\n return ant_artifact.get('self_score', 0) * 2\n")
198
+ ss = ScriptScorer(rule)
199
+ check("ScriptScorer loads user rule", ss.score({"self_score": 0.3}) == 0.6)
200
+ check("ScriptScorer clamps > 1.0", ss.score({"self_score": 0.9}) == 1.0)
201
+ check("ScriptScorer clamps < 0.0", ss.score({"self_score": -0.5}) == 0.0)
202
+
203
+ check("hallucination_check true at diff > 0.4",
204
+ hallucination_check(0.9, 0.4) is True)
205
+ check("hallucination_check false at diff < 0.4",
206
+ hallucination_check(0.5, 0.4) is False)
207
+
208
+ artifact_v = {"ant_id": "X", "self_score": 0.5, "self_confidence": 0.5}
209
+ score, src = resolve_score(artifact_v, {"X": 0.9}, None, fb)
210
+ check("resolve_score prefers verified", score == 0.9 and src == "verified_llm")
211
+
212
+ score, src = resolve_score(artifact_v, {}, None, fb)
213
+ check("resolve_score falls back when no verified",
214
+ src == "fallback_self")
215
+
216
+
217
+ # ---------------------------------------------------------------------------
218
+ # CLI integration — full pipeline (3 iterations, fallback scoring)
219
+ # ---------------------------------------------------------------------------
220
+
221
+ def test_full_pipeline_3_iterations():
222
+ section("aco.py — full 3-iteration pipeline (fallback scoring)")
223
+
224
+ with tempfile.TemporaryDirectory() as td:
225
+ session = Path(td)
226
+ write_config(session)
227
+
228
+ r = run_aco(session, "init")
229
+ check("init: status ok", r["status"] == "ok")
230
+ check("init: 5 nodes -> 10 edges", r["n_edges"] == 10)
231
+ check("init: pheromone file exists",
232
+ (session / "pheromone" / "current.json").exists())
233
+ check("init: task-space file exists",
234
+ (session / "task-space.json").exists())
235
+ check("init: init.json frozen",
236
+ (session / "pheromone" / "init.json").exists())
237
+
238
+ best_history = []
239
+ entropy_history = []
240
+
241
+ for k in range(1, 4):
242
+ sel = run_aco(session, "select", "--iter", str(k))
243
+ check(f"iter{k} select: 3 assignments", len(sel["assignments"]) == 3)
244
+ check(f"iter{k} select: ant_ids correct",
245
+ all(a["ant_id"] == f"ANT-{k}-{i+1}" for i, a in enumerate(sel["assignments"])))
246
+ check(f"iter{k} select: edge_preferences not empty",
247
+ all(a["edge_preferences"] for a in sel["assignments"]))
248
+
249
+ paths_by_quality = [
250
+ (["a", "b", "c"], 0.9, 0.9),
251
+ (["b", "d", "e"], 0.6, 0.7),
252
+ (["c", "e", "a"], 0.4, 0.5),
253
+ ]
254
+ for i, (path, ss, sc) in enumerate(paths_by_quality, 1):
255
+ write_ant(session, k, i, path, ss, sc)
256
+
257
+ up = run_aco(session, "update", "--iter", str(k))
258
+ check(f"iter{k} update: 3 ants processed", up["n_ants_processed"] == 3)
259
+ check(f"iter{k} update: best_score > 0", up["best_score"] > 0)
260
+ check(f"iter{k} update: stats has entropy", "entropy" in up["stats"])
261
+
262
+ best_history.append(up["best_score"])
263
+ entropy_history.append(up["stats"]["entropy"])
264
+
265
+ check(f"iter{k} history snapshot exists",
266
+ (session / "pheromone" / "history" / f"{k}.json").exists())
267
+ check(f"iter{k} trails written",
268
+ (session / "trails" / f"{k}.jsonl").exists())
269
+
270
+ check("best_score stable after iter1 (same ants each iter)",
271
+ best_history[0] == best_history[-1])
272
+ check("entropy decreases over iterations (concentration)",
273
+ entropy_history[0] >= entropy_history[-1],
274
+ f"start={entropy_history[0]:.3f} end={entropy_history[-1]:.3f}")
275
+
276
+ cv = run_aco(session, "converged")
277
+ check("converged: returns triggered_by list", isinstance(cv["triggered_by"], list))
278
+ check("converged: triggers stagnation (best unchanged)",
279
+ "stagnation" in cv["triggered_by"])
280
+ check("converged: also triggers max_iterations",
281
+ "max_iterations" in cv["triggered_by"])
282
+ check("converged: true after triggers", cv["converged"] is True)
283
+
284
+ rep = run_aco(session, "report")
285
+ check("report: has best", rep["best"] is not None)
286
+ check("report: top_k present", len(rep["top_k"]) > 0)
287
+ check("report: convergence curve len == iters",
288
+ len(rep["convergence_curve"]) == 3)
289
+ check("report: iterations_completed == 3", rep["iterations_completed"] == 3)
290
+
291
+
292
+ def test_run_scoped_outputs():
293
+ section("aco.py — v3 Run-scoped outputs and metadata")
294
+
295
+ with tempfile.TemporaryDirectory() as td:
296
+ run_dir = Path(td) / "run"
297
+ session = run_dir / "work" / "team"
298
+ write_config(session)
299
+ run_args = ("--run-dir", str(run_dir))
300
+ run_aco(session, *run_args, "init")
301
+ selected = run_aco(session, *run_args, "select", "--iter", "1")
302
+ start = selected["assignments"][0]["start_node"]
303
+ output = write_ant(session, 1, 1, [start], outputs=run_dir / "outputs")
304
+ valid = output.read_text()
305
+ changed = json.loads(valid)
306
+ changed["_meta"]["kind"] = "different-artifact"
307
+ output.write_text(json.dumps(changed))
308
+ rejected = run_aco(session, *run_args, "update", "--iter", "1", expect_exit=1)
309
+ check("v3 update rejects an invalid ant contract without recording the round",
310
+ "invalid ant artifact contract" in rejected["message"]
311
+ and not (session / "pheromone" / "updates" / "1.json").exists())
312
+ output.write_text(valid)
313
+ up = run_aco(session, *run_args, "update", "--iter", "1")
314
+ check("v3 update reads outputs/ ant", up["n_ants_processed"] == 1)
315
+ rep = run_aco(session, *run_args, "report")
316
+ check("v3 report self-describes its artifact",
317
+ rep["_meta"] == {"kind": "swarm-report", "schema": "swarm-report/1.0",
318
+ "role": "attachment"})
319
+ check("v3 report includes best solution", rep["best"]["ant_id"] == "ANT-1-1")
320
+
321
+
322
+ # ---------------------------------------------------------------------------
323
+ # CLI integration — target_score convergence
324
+ # ---------------------------------------------------------------------------
325
+
326
+ def test_target_score_convergence():
327
+ section("aco.py — target_score triggers early convergence")
328
+
329
+ with tempfile.TemporaryDirectory() as td:
330
+ session = Path(td)
331
+ write_config(session, {
332
+ "convergence": {"target_score": {"enabled": True, "value": 0.30}},
333
+ })
334
+ run_aco(session, "init")
335
+ run_aco(session, "select", "--iter", "1")
336
+ write_ant(session, 1, 1, ["a", "b"], 0.9, 0.9)
337
+ write_ant(session, 1, 2, ["b", "c"], 0.6, 0.6)
338
+ write_ant(session, 1, 3, ["c", "d"], 0.4, 0.4)
339
+ up = run_aco(session, "update", "--iter", "1")
340
+
341
+ check("best_score above target", up["best_score"] >= 0.30)
342
+ cv = run_aco(session, "converged")
343
+ check("converged after 1 iter (target hit)",
344
+ cv["converged"] is True and "target_score" in cv["triggered_by"])
345
+
346
+
347
+ # ---------------------------------------------------------------------------
348
+ # CLI integration — hallucination detection (verified scores file)
349
+ # ---------------------------------------------------------------------------
350
+
351
+ def test_hallucination_flagging():
352
+ section("aco.py — hallucination flagging with verified scores")
353
+
354
+ with tempfile.TemporaryDirectory() as td:
355
+ session = Path(td)
356
+ write_config(session)
357
+ run_aco(session, "init")
358
+ run_aco(session, "select", "--iter", "1")
359
+
360
+ write_ant(session, 1, 1, ["a", "b"], self_score=0.95, self_confidence=0.9)
361
+ write_ant(session, 1, 2, ["b", "c"], self_score=0.85, self_confidence=0.8)
362
+ write_ant(session, 1, 3, ["c", "d"], self_score=0.7, self_confidence=0.7)
363
+
364
+ scores_dir = session / "scores"
365
+ scores_dir.mkdir(exist_ok=True)
366
+ (scores_dir / "iter-1-scores.json").write_text(json.dumps({
367
+ "iteration": 1, "scorer_type": "llm",
368
+ "scores": {
369
+ "ANT-1-1": {"verified_score": 0.20, "rationale": "weak"},
370
+ "ANT-1-2": {"verified_score": 0.80, "rationale": "ok"},
371
+ "ANT-1-3": {"verified_score": 0.65, "rationale": "ok"},
372
+ },
373
+ }))
374
+
375
+ up = run_aco(session, "update", "--iter", "1")
376
+ check("ANT-1-1 flagged as hallucination (|0.95-0.20|=0.75 > 0.4)",
377
+ "ANT-1-1" in up["hallucinations_flagged"])
378
+ check("ANT-1-2 NOT flagged (|0.85-0.80|=0.05 < 0.4)",
379
+ "ANT-1-2" not in up["hallucinations_flagged"])
380
+ check("ANT-1-3 NOT flagged (|0.7-0.65|=0.05 < 0.4)",
381
+ "ANT-1-3" not in up["hallucinations_flagged"])
382
+
383
+
384
+ # ---------------------------------------------------------------------------
385
+ # CLI integration — invalid artifact handling
386
+ # ---------------------------------------------------------------------------
387
+
388
+ def test_invalid_artifacts():
389
+ section("aco.py — invalid artifacts handled gracefully")
390
+
391
+ with tempfile.TemporaryDirectory() as td:
392
+ session = Path(td)
393
+ write_config(session)
394
+ run_aco(session, "init")
395
+ run_aco(session, "select", "--iter", "1")
396
+
397
+ write_ant(session, 1, 1, ["a", "b"])
398
+
399
+ artifacts = session / "artifacts"
400
+ (artifacts / "ant-1-2.json").write_text(json.dumps({
401
+ "schema_version": "1.0", "ant_id": "ANT-1-2", "iteration": 1,
402
+ "path": ["a", "ZZZ_NOT_A_NODE"],
403
+ "path_decisions": [{"from": "a", "to": "ZZZ_NOT_A_NODE", "rationale": "x",
404
+ "guided_by": "x", "deviation_from_hint": False}],
405
+ "self_score": 0.5, "self_confidence": 0.5,
406
+ "evidence": ["x"], "candidate_solution": {"summary": "x"},
407
+ }))
408
+
409
+ (artifacts / "ant-1-3.json").write_text("{ malformed json")
410
+
411
+ up = run_aco(session, "update", "--iter", "1")
412
+ check("only 1 valid ant processed (2 rejected)",
413
+ up["n_ants_processed"] == 1, f"got {up['n_ants_processed']}")
414
+
415
+
416
+ def test_config_validation():
417
+ section("aco.py — config validation error paths")
418
+
419
+ with tempfile.TemporaryDirectory() as td:
420
+ session = Path(td)
421
+ session.mkdir(exist_ok=True)
422
+ (session / "swarm-config.json").write_text(json.dumps({"task_space": {}, "aco": {}}))
423
+ r = run_aco(session, "init", expect_exit=2)
424
+ check("missing nodes -> exit 2", r["status"] == "error")
425
+ check("error message mentions nodes",
426
+ "nodes" in r["message"] or "auto_discover" in r["message"])
427
+
428
+ with tempfile.TemporaryDirectory() as td:
429
+ session = Path(td)
430
+ session.mkdir(exist_ok=True)
431
+ r = run_aco(session, "init", expect_exit=2)
432
+ check("missing config -> exit 2", r["status"] == "error")
433
+
434
+
435
+ def test_idempotent_update():
436
+ section("aco.py — update is idempotent (re-running same iter is safe)")
437
+
438
+ with tempfile.TemporaryDirectory() as td:
439
+ session = Path(td)
440
+ write_config(session)
441
+ run_aco(session, "init")
442
+ run_aco(session, "select", "--iter", "1")
443
+ write_ant(session, 1, 1, ["a", "b"], 0.8, 0.8)
444
+ write_ant(session, 1, 2, ["b", "c"], 0.6, 0.7)
445
+ final_ant = write_ant(session, 1, 3, ["c", "d"], 0.4, 0.5)
446
+
447
+ current = session / "pheromone" / "current.json"
448
+ original = current.read_bytes()
449
+ up1 = run_aco(session, "update", "--iter", "1")
450
+ projection = [current, session / "best.json",
451
+ session / "pheromone" / "history" / "1.json",
452
+ session / "trails" / "1.jsonl"]
453
+ expected = [path.read_bytes() for path in projection]
454
+ up2 = run_aco(session, "update", "--iter", "1")
455
+ check("update replay keeps full result and projection stable",
456
+ up1 == up2 and expected == [path.read_bytes() for path in projection])
457
+ current.write_bytes(original)
458
+ projection[2].unlink()
459
+ check("report recovers committed but interrupted update",
460
+ run_aco(session, "report")["iterations_completed"] == 1
461
+ and expected == [path.read_bytes() for path in projection])
462
+ content = json.loads(final_ant.read_text())
463
+ content["self_score"] = 0.9
464
+ final_ant.write_text(json.dumps(content))
465
+ conflict = run_aco(session, "update", "--iter", "1", expect_exit=1)
466
+ check("changed retry is rejected without altering projected state",
467
+ "inputs changed" in conflict["message"]
468
+ and expected == [path.read_bytes() for path in projection])
469
+
470
+
471
+ def test_run_selection_replay():
472
+ section("aco.py — Run selection survives coordinator restart")
473
+ with tempfile.TemporaryDirectory() as td:
474
+ run_dir = Path(td) / "run"
475
+ session = run_dir / "work" / "team"
476
+ write_config(session, {"swarm": {"n_ants": 1}})
477
+ run_args = ("--run-dir", str(run_dir))
478
+ run_aco(session, *run_args, "init")
479
+ selected = run_aco(session, *run_args, "select", "--iter", "1")
480
+ selection = session / "assignments" / "1.json"
481
+ check("selection is persisted before dispatch", selection.is_file()
482
+ and json.loads(selection.read_text(encoding="utf-8")) == selected)
483
+ check("reopened selection returns original assignments",
484
+ run_aco(session, *run_args, "select", "--iter", "1") == selected)
485
+ start = selected["assignments"][0]["start_node"]
486
+ write_ant(session, 1, 1, ["b" if start == "a" else "a"], outputs=run_dir / "outputs")
487
+ mismatch = run_aco(session, *run_args, "update", "--iter", "1", expect_exit=1)
488
+ check("update rejects an ant from the wrong start", "saved assignment" in mismatch["message"])
489
+ write_ant(session, 1, 1, [start], outputs=run_dir / "outputs")
490
+ run_aco(session, *run_args, "update", "--iter", "1")
491
+ check("selection replays after a committed update",
492
+ run_aco(session, *run_args, "select", "--iter", "1") == selected)
493
+ selected["assignments"][0]["start_node"] = "b" if start == "a" else "a"
494
+ selection.write_text(json.dumps(selected), encoding="utf-8")
495
+ drift = run_aco(session, *run_args, "update", "--iter", "1", expect_exit=1)
496
+ check("replay rejects a changed assignment batch", "inputs changed" in drift["message"])
497
+ skipped = run_aco(session, *run_args, "select", "--iter", "3", expect_exit=1)
498
+ check("future iteration cannot skip controller state", "expected iteration 2" in skipped["message"])
499
+
500
+
501
+ def test_run_llm_scoring():
502
+ section("aco.py — Run requires a complete LLM score batch or an explicit fallback")
503
+ with tempfile.TemporaryDirectory() as td:
504
+ run_dir = Path(td) / "run"
505
+ session = run_dir / "work" / "team"
506
+ write_config(session, {"swarm": {"n_ants": 2}, "scoring": {"mode": "llm"}})
507
+ run_args = ("--run-dir", str(run_dir))
508
+ run_aco(session, *run_args, "init")
509
+ scores = session / "scores" / "iter-1-scores.json"
510
+ update = (*run_args, "update", "--iter", "1")
511
+ for index in (1, 2):
512
+ write_ant(session, 1, index, ["a", "b"], self_score=0.8, outputs=run_dir / "outputs")
513
+ missing = run_aco(session, *update, expect_exit=1)
514
+ check("missing LLM result requires a decision", "scores file" in missing["message"])
515
+ batch = {"iteration": 1, "scorer_type": "llm",
516
+ "scores": {"ANT-1-1": {"verified_score": 0.75, "rationale": "matches the evidence"}}}
517
+ scores.write_text(json.dumps(batch), encoding="utf-8")
518
+ partial = run_aco(session, *update, expect_exit=1)
519
+ check("partial LLM batch cannot commit controller state", "ANT-1-2" in partial["message"]
520
+ and not (session / "pheromone" / "updates" / "1.json").exists())
521
+ batch["scores"]["ANT-1-2"] = {"verified_score": True, "rationale": "invalid score type"}
522
+ scores.write_text(json.dumps(batch), encoding="utf-8")
523
+ invalid = run_aco(session, *update, expect_exit=1)
524
+ check("boolean score rejected", "invalid verified score" in invalid["message"])
525
+ batch["iteration"] = 2
526
+ batch["scores"]["ANT-1-2"]["verified_score"] = 0.65
527
+ scores.write_text(json.dumps(batch), encoding="utf-8")
528
+ wrong = run_aco(session, *update, expect_exit=1)
529
+ check("score batch from a different iteration rejected", "iteration 1" in wrong["message"])
530
+ batch["iteration"] = 1
531
+ scores.write_text(json.dumps(batch), encoding="utf-8")
532
+ scored = run_aco(session, *update)
533
+ check("verified scores used and replayed", scored["mean_score"] == 0.7
534
+ and run_aco(session, *update) == scored)
535
+ for index in (1, 2):
536
+ write_ant(session, 2, index, ["b", "c"], outputs=run_dir / "outputs")
537
+ fallback = (*run_args, "update", "--iter", "2", "--allow-score-fallback")
538
+ run_aco(session, *fallback)
539
+ receipts = [json.loads((session / "pheromone" / "updates" / f"{i}.json").read_text(encoding="utf-8"))
540
+ for i in (1, 2)]
541
+ check("receipts retain an actionable scoring choice for recovery",
542
+ [receipt["scoring"] for receipt in receipts] == ["llm", "fallback"])
543
+ report = run_aco(session, *run_args, "report")
544
+ check("both scoring sources visible in report",
545
+ {trail["source"] for trail in report["top_k"]} == {"verified_llm", "fallback_self"})
546
+ disagrees = run_aco(session, *run_args, "update", "--iter", "2", expect_exit=1)
547
+ check("replay preserves the explicit fallback decision", "inputs changed" in disagrees["message"])
548
+
549
+
550
+ def test_auto_discover_from_glob():
551
+ section("aco.py — auto_discover_from glob")
552
+
553
+ with tempfile.TemporaryDirectory() as td:
554
+ td_path = Path(td)
555
+ for name in ["alpha.txt", "beta.txt", "gamma.txt"]:
556
+ (td_path / name).write_text("data")
557
+
558
+ session = td_path / "session"
559
+ session.mkdir()
560
+ (session / "swarm-config.json").write_text(json.dumps({
561
+ "swarm": {"n_ants": 2}, "aco": {},
562
+ "task_space": {"auto_discover_from": str(td_path / "*.txt"),
563
+ "max_path_length": 2},
564
+ "scoring": {"mode": "fallback"},
565
+ "ant_prompt": {"objective": "x"},
566
+ "convergence": {"max_iterations": 1},
567
+ }))
568
+ r = run_aco(session, "init")
569
+ check("auto_discover finds 3 files", r["n_nodes"] == 3,
570
+ f"got {r['n_nodes']}")
571
+
572
+
573
+ # ---------------------------------------------------------------------------
574
+ # Main
575
+ # ---------------------------------------------------------------------------
576
+
577
+ def main():
578
+ print("=" * 60)
579
+ print("team-swarm scripts test suite")
580
+ print("=" * 60)
581
+
582
+ test_pheromone_unit()
583
+ test_scoring_unit()
584
+ test_full_pipeline_3_iterations()
585
+ test_run_scoped_outputs()
586
+ test_target_score_convergence()
587
+ test_hallucination_flagging()
588
+ test_invalid_artifacts()
589
+ test_config_validation()
590
+ test_idempotent_update()
591
+ test_run_selection_replay()
592
+ test_run_llm_scoring()
593
+ test_auto_discover_from_glob()
594
+
595
+ print("\n" + "=" * 60)
596
+ print(f"Results: {PASS} passed, {FAIL} failed")
597
+ if FAILED_NAMES:
598
+ print("\nFailed tests:")
599
+ for name in FAILED_NAMES:
600
+ print(f" - {name}")
601
+ print("=" * 60)
602
+ sys.exit(0 if FAIL == 0 else 1)
603
+
604
+
605
+ if __name__ == "__main__":
606
+ main()