@event4u/agent-config 2.12.0 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/.agent-src/commands/council/analysis.md +142 -0
  2. package/.agent-src/commands/council/debate.md +129 -0
  3. package/.agent-src/commands/council/default.md +8 -0
  4. package/.agent-src/commands/council/design.md +16 -12
  5. package/.agent-src/commands/council/optimize.md +16 -15
  6. package/.agent-src/commands/council/pr.md +12 -12
  7. package/.agent-src/commands/council.md +48 -2
  8. package/.agent-src/commands/memory/learn-low-impact.md +143 -0
  9. package/.agent-src/personas/advisors/contrarian.md +95 -0
  10. package/.agent-src/personas/advisors/executor.md +99 -0
  11. package/.agent-src/personas/advisors/expansionist.md +98 -0
  12. package/.agent-src/personas/advisors/first-principles.md +98 -0
  13. package/.agent-src/personas/advisors/outsider.md +102 -0
  14. package/.agent-src/rules/ask-when-uncertain.md +10 -6
  15. package/.agent-src/rules/copilot-routing.md +19 -0
  16. package/.agent-src/rules/devcontainer-routing.md +20 -0
  17. package/.agent-src/rules/external-reference-deep-dive.md +1 -1
  18. package/.agent-src/rules/fast-path-marker-visibility.md +38 -0
  19. package/.agent-src/rules/laravel-routing.md +20 -0
  20. package/.agent-src/rules/low-impact-corpus-privacy-floor.md +74 -0
  21. package/.agent-src/rules/symfony-routing.md +20 -0
  22. package/.agent-src/skills/ai-council/SKILL.md +388 -10
  23. package/.agent-src/skills/copilot-config/SKILL.md +1 -1
  24. package/.agent-src/skills/devcontainer/SKILL.md +1 -1
  25. package/.agent-src/skills/laravel/SKILL.md +1 -1
  26. package/.agent-src/skills/project-analysis-core/SKILL.md +1 -1
  27. package/.agent-src/skills/project-analyzer/SKILL.md +1 -1
  28. package/.agent-src/skills/symfony-workflow/SKILL.md +1 -1
  29. package/.agent-src/skills/universal-project-analysis/SKILL.md +1 -1
  30. package/.agent-src/templates/agents/agent-project-settings.example.yml +1 -1
  31. package/.claude-plugin/marketplace.json +4 -1
  32. package/AGENTS.md +1 -1
  33. package/CHANGELOG.md +346 -124
  34. package/CONTRIBUTING.md +5 -0
  35. package/README.md +6 -6
  36. package/config/agent-settings.template.yml +5 -93
  37. package/config/gitignore-block.txt +6 -0
  38. package/docs/architecture/multi-tool-projection.md +53 -0
  39. package/docs/architecture/{compression.md → source-projection.md} +21 -3
  40. package/docs/architecture.md +15 -15
  41. package/docs/archive/CHANGELOG-pre-2.11.0.md +141 -0
  42. package/docs/catalog.md +25 -12
  43. package/docs/contracts/adr-architectural-consensus-mechanism.md +68 -0
  44. package/docs/contracts/adr-level-6-productization.md +7 -9
  45. package/docs/contracts/ai-council-config.md +658 -0
  46. package/docs/contracts/command-clusters.md +58 -2
  47. package/docs/contracts/command-surface-tiers.md +3 -2
  48. package/docs/contracts/cost-profile-defaults.md +5 -0
  49. package/docs/contracts/decision-engine-gates.md +5 -0
  50. package/docs/contracts/decision-trace-v1.md +2 -2
  51. package/docs/contracts/file-ownership-matrix.json +1735 -72
  52. package/docs/contracts/installed-tools-lockfile.md +2 -1
  53. package/docs/contracts/low-impact-corpus-format.md +95 -0
  54. package/docs/contracts/mcp-beta-criteria.md +6 -5
  55. package/docs/contracts/mcp-cloud-scope.md +5 -4
  56. package/docs/contracts/multi-tool-projection-fidelity.md +115 -0
  57. package/docs/contracts/release-trunk-sync.md +4 -3
  58. package/docs/contracts/tier-3-contrib-plugin.md +5 -6
  59. package/docs/getting-started.md +2 -2
  60. package/docs/guidelines/agent-infra/installed-tools-manifest.md +2 -1
  61. package/docs/installation.md +32 -0
  62. package/package.json +1 -1
  63. package/scripts/_archive/README.md +59 -0
  64. package/scripts/_cli/cmd_doctor.py +134 -0
  65. package/scripts/ai_council/_default_prices.py +10 -1
  66. package/scripts/ai_council/advisors.py +148 -0
  67. package/scripts/ai_council/airgap.py +165 -0
  68. package/scripts/ai_council/cli_hints.py +123 -0
  69. package/scripts/ai_council/clients.py +959 -5
  70. package/scripts/ai_council/compile_corpus.py +178 -0
  71. package/scripts/ai_council/confidence_gate.py +156 -0
  72. package/scripts/ai_council/config.py +1364 -0
  73. package/scripts/ai_council/consensus.py +329 -0
  74. package/scripts/ai_council/events_log.py +137 -0
  75. package/scripts/ai_council/learn_low_impact_preview.py +252 -0
  76. package/scripts/ai_council/low_impact.py +714 -0
  77. package/scripts/ai_council/low_impact_corpus.py +466 -0
  78. package/scripts/ai_council/low_impact_intake.py +163 -0
  79. package/scripts/ai_council/modes.py +6 -1
  80. package/scripts/ai_council/necessity.py +782 -0
  81. package/scripts/ai_council/orchestrator.py +872 -20
  82. package/scripts/ai_council/probation_gate.py +152 -0
  83. package/scripts/ai_council/prompts.py +335 -0
  84. package/scripts/ai_council/redact_low_impact_entry.py +155 -0
  85. package/scripts/ai_council/replay.py +155 -0
  86. package/scripts/ai_council/session.py +19 -1
  87. package/scripts/ai_council/shadow_dispatch.py +235 -0
  88. package/scripts/ai_council/solo_dispatch.py +226 -0
  89. package/scripts/audit_cloud_compatibility.py +74 -0
  90. package/scripts/audit_command_surface.py +363 -0
  91. package/scripts/check_compressed_paths.py +6 -1
  92. package/scripts/check_council_layout.py +11 -0
  93. package/scripts/ci_time_ratio.py +168 -0
  94. package/scripts/council_cli.py +2005 -30
  95. package/scripts/install.sh +12 -0
  96. package/scripts/measure_projection_bytes.py +159 -0
  97. package/scripts/measure_roadmap_trajectory.py +112 -0
  98. package/scripts/probe_projection_fidelity.py +202 -0
  99. package/scripts/score_skill_selection.py +198 -0
  100. package/scripts/skill_collision_clusters.py +162 -0
  101. /package/scripts/{_backfill_skill_domains.py → _archive/_backfill_skill_domains.py} +0 -0
  102. /package/scripts/{_bootstrap_tier_frontmatter.py → _archive/_bootstrap_tier_frontmatter.py} +0 -0
  103. /package/scripts/{_p43_bodies.py → _archive/_p43_bodies.py} +0 -0
  104. /package/scripts/{_p43_compress.py → _archive/_p43_compress.py} +0 -0
  105. /package/scripts/{_p4_migrate.py → _archive/_p4_migrate.py} +0 -0
  106. /package/scripts/{_phase2_shim_helper.py → _archive/_phase2_shim_helper.py} +0 -0
  107. /package/scripts/{_pilot_council_question.py → _archive/_pilot_council_question.py} +0 -0
@@ -0,0 +1,1364 @@
1
+ """Council configuration loader — single source of truth.
2
+
3
+ Reads ``agents/.ai-council.yml`` per the contract in
4
+ ``docs/contracts/ai-council-config.md``. Replaces the fragmented
5
+ ``.agent-settings.yml`` ``ai_council`` block (Phase 0 migration).
6
+
7
+ Validation contract (8 rules, all enforced at load time):
8
+
9
+ 1. ``enabled`` is a bool.
10
+ 2. ``defaults.mode`` ∈ {``api``, ``manual``, ``cli``}; per-member mode
11
+ same set. Semantics: ``api`` = SDK call against a stored key
12
+ (billable); ``manual`` = copy & paste — human transports prompt +
13
+ reply between the agent and an external chat surface (free);
14
+ ``cli`` = shell out to a locally-installed CLI under subscription
15
+ auth (free for first-party CLIs, billable for community wrappers).
16
+ 3. ``members.<name>`` keys are restricted to the known provider set.
17
+ 4. ``cost_budget.*`` numeric fields are >= 0.
18
+ 5. Enabled members carry a non-empty ``model`` and ``api_key_ref``
19
+ when their effective mode is ``api``. CLI-mode members do NOT
20
+ require ``api_key_ref`` (subscription auth is provided by the CLI
21
+ binary itself).
22
+ 6. ``api_key_ref`` starts with ``file:`` or ``env:`` — raw keys are
23
+ refused even if syntactically plausible.
24
+ 7. Resolved ``file:`` key paths must have mode 0o600 (delegated to
25
+ :func:`resolve_api_key`; runs at use-time, not parse-time).
26
+ 8. ``binary:`` is only valid when the member's effective mode is
27
+ ``cli``; ``cli_call_budget.max_calls_per_day.<provider>`` keys
28
+ must be valid providers.
29
+ """
30
+
31
+ from __future__ import annotations
32
+
33
+ import os
34
+ import stat
35
+ from dataclasses import dataclass, field
36
+ from pathlib import Path
37
+ from typing import Any
38
+
39
+ import yaml
40
+
41
+ from scripts._lib import user_global_paths
42
+
43
+ _VALID_PROVIDERS = frozenset({"anthropic", "openai", "gemini", "xai", "perplexity"})
44
+ _VALID_MODES = frozenset({"api", "manual", "cli"})
45
+
46
+ #: Prefixes that signal "this is a raw API key" so we refuse it loudly
47
+ #: even when the user accidentally inlined it in ``api_key_ref``.
48
+ _RAW_KEY_PREFIXES = ("sk-", "sk-ant-", "ya29.", "AIza", "xai-", "pplx-", "gsk_")
49
+
50
+
51
+ class CouncilConfigError(RuntimeError):
52
+ """Raised when ``agents/.ai-council.yml`` violates the schema."""
53
+
54
+
55
+ @dataclass(frozen=True)
56
+ class DefaultsConfig:
57
+ mode: str = "api"
58
+ member_mode: str = "cli"
59
+ min_rounds: int = 2
60
+ deep_min_rounds: int = 3
61
+ max_output_tokens: int = 0
62
+ session_retention_days: int = 7
63
+ debate_max_rounds: int = 4
64
+
65
+
66
+ @dataclass(frozen=True)
67
+ class CostBudgetConfig:
68
+ max_input_tokens: int = 500_000
69
+ max_output_tokens: int = 200_000
70
+ max_calls: int = 50
71
+ max_total_usd: float = 20.0
72
+
73
+
74
+ @dataclass(frozen=True)
75
+ class MemberConfig:
76
+ name: str
77
+ enabled: bool
78
+ model: str
79
+ api_key_ref: str | None
80
+ mode: str | None = None
81
+ binary: str | None = None
82
+ model_ladder: tuple[str, ...] = ()
83
+ participate_low_impact: bool = False
84
+
85
+
86
+ @dataclass(frozen=True)
87
+ class AdvisorConfig:
88
+ """Replace-mode advisor binding (Phase 6).
89
+
90
+ `member` names the provider whose plain call is replaced by this
91
+ advisor-persona call. `persona` is the path to the advisor persona
92
+ file (resolved relative to the package root). `model` is an
93
+ optional override of the bound member's plain model.
94
+ """
95
+
96
+ name: str
97
+ enabled: bool
98
+ member: str
99
+ persona: str
100
+ model: str | None = None
101
+
102
+
103
+ @dataclass(frozen=True)
104
+ class ConsensusScoringConfig:
105
+ """Consensus-scoring round settings (Phase 4 / F3).
106
+
107
+ Only the `analysis` lens activates the scoring round today. Other
108
+ lenses see this as inert config. Thresholds are inclusive on the
109
+ `strong` side (> strong → strong bucket) and exclusive on the
110
+ `minority` side (≤ minority → minority bucket); the middle bucket
111
+ is `(minority, strong]`. Defaults mirror the roadmap (0.7 / 0.4).
112
+ """
113
+
114
+ enabled: bool = False
115
+ strong_threshold: float = 0.7
116
+ minority_threshold: float = 0.4
117
+ lenses: tuple[str, ...] = ("analysis",)
118
+
119
+
120
+ _VALID_NECESSITY_MODES = frozenset({"off", "educate", "block", "warn-only"})
121
+ _VALID_DISCLOSURE_MODES = frozenset({"always", "above_threshold", "off"})
122
+
123
+
124
+ @dataclass(frozen=True)
125
+ class NecessityClassifierConfig:
126
+ """Council-necessity classifier toggle (Phase 6).
127
+
128
+ ``mode`` controls the **agent** invocation path; ``user_explicit_mode``
129
+ controls the **user-explicit** invocation path (step-8 D2 tier split).
130
+
131
+ Valid modes ∈ ``{"off", "educate", "block", "warn-only"}``:
132
+
133
+ - ``off`` — legacy behaviour: classifier never runs, every request
134
+ proceeds to deliberation.
135
+ - ``educate`` — agent-initiated `unnecessary` skips silently;
136
+ user-explicit `unnecessary` emits the educate paragraph and
137
+ requires ``--proceed-anyway`` (CLI) or a numbered-options
138
+ confirmation (agent surface) before firing members.
139
+ - ``block`` — power-user opt-in: `unnecessary` skips silently
140
+ regardless of override flag.
141
+ - ``warn-only`` — classifier verdict annotated in stdout but
142
+ **never** skips. Default for ``user_explicit_mode`` (step-8 D2).
143
+
144
+ Default ``mode`` (agent path) = ``educate``;
145
+ default ``user_explicit_mode`` = ``warn-only``. Reconciles
146
+ "Council always active when called" with "skip trivial agent-side
147
+ requests".
148
+
149
+ Per-lens overrides live in :class:`CouncilConfig.lens_overrides`;
150
+ this dataclass carries only the global default.
151
+ """
152
+
153
+ enabled: bool = True
154
+ mode: str = "educate"
155
+ user_explicit_mode: str = "warn-only"
156
+
157
+
158
+ @dataclass(frozen=True)
159
+ class ModelDowngradeConfig:
160
+ """Model-size downgrade-suggestion toggle (Phase 7).
161
+
162
+ When ``enabled`` is true, the dispatcher runs the size-fit
163
+ classifier (:func:`scripts.ai_council.necessity.classify_size_fit`)
164
+ before deliberation. ``fit=False`` triggers a single
165
+ numbered-options prompt offering the suggested cheaper model.
166
+
167
+ ``auto_apply`` toggles agent-initiated invocations to silently
168
+ accept the suggested model (logged in ``session.md`` as a
169
+ ``downgraded`` event). User-explicit invocations always see the
170
+ prompt regardless. Default global ON per the roadmap spec; per-lens
171
+ override lives in :class:`LensOverridesConfig`.
172
+ """
173
+
174
+ enabled: bool = True
175
+ auto_apply: bool = False
176
+
177
+
178
+ @dataclass(frozen=True)
179
+ class CostDisclosureConfig:
180
+ """Pre-flight cost-disclosure toggle (Phase 8).
181
+
182
+ ``mode`` ∈ ``{"always", "above_threshold", "off"}``:
183
+
184
+ - ``always`` — always emit the disclosure block before deliberation.
185
+ - ``above_threshold`` — emit only when the expected USD spend exceeds
186
+ ``threshold_usd``. Useful for cheap lenses (analysis / default)
187
+ where the prompt would be friction.
188
+ - ``off`` — power-user opt-out, never emit. The hard refusal cap
189
+ still fires regardless (cost ceilings are not optional).
190
+
191
+ ``show_per_member`` toggles whether the per-member breakdown is
192
+ surfaced or just the rolled-up totals.
193
+ """
194
+
195
+ mode: str = "always"
196
+ threshold_usd: float = 1.00
197
+ show_per_member: bool = True
198
+
199
+
200
+ @dataclass(frozen=True)
201
+ class DebateConfig:
202
+ """Debate cost-visibility + hard refusal cap (Phase 8).
203
+
204
+ ``max_cost_usd`` is the unconditional refusal cap — when the
205
+ pre-flight ``high_usd`` estimate exceeds this value, the dispatcher
206
+ refuses to start the debate (no prompt, exit non-zero). Mirrored
207
+ mid-run: running spend > cap aborts cleanly after the current round.
208
+
209
+ ``cost_disclosure`` controls the pre-flight disclosure block. Both
210
+ are independent — disclosure off + cap on still refuses over-budget
211
+ debates; cap=0 disables the refusal but disclosure still fires.
212
+ """
213
+
214
+ max_cost_usd: float = 5.00
215
+ cost_disclosure: CostDisclosureConfig = field(
216
+ default_factory=CostDisclosureConfig,
217
+ )
218
+
219
+
220
+ @dataclass(frozen=True)
221
+ class DecisionReplayConfig:
222
+ """Decision-replay artefact toggle (Phase 9).
223
+
224
+ ``enabled`` controls whether ``decision-replay.md`` is written
225
+ alongside the session JSON. ``include_member_arguments`` toggles
226
+ the redacted view: when ``False`` the artefact emits consensus +
227
+ dissent COUNT only — no per-member arguments — for sharing without
228
+ leaking which model framed which point.
229
+ """
230
+
231
+ enabled: bool = True
232
+ include_member_arguments: bool = True
233
+
234
+
235
+ @dataclass(frozen=True)
236
+ class DecisionResolutionEntry:
237
+ """Routing entry for one impact class (Phase 10).
238
+
239
+ Attributes:
240
+ mode: One of ``agent`` / ``council`` / ``user``. ``high_impact``
241
+ and ``user_required`` are LOCKED to ``user`` at parse-time
242
+ — overrides are rejected with a CouncilConfigError.
243
+ confidence_threshold: When the classifier's confidence is BELOW
244
+ this value, the entry's ``mode`` is upgraded by one rung
245
+ (agent → council, council → user). ``user`` mode ignores
246
+ the threshold. Default ``0.6``.
247
+ """
248
+
249
+ mode: str = "user"
250
+ confidence_threshold: float = 0.6
251
+
252
+
253
+ @dataclass(frozen=True)
254
+ class FuzzyMatchConfig:
255
+ """Opt-in fuzzy matching for the corpus-aware classifier (step-9 P5).
256
+
257
+ Lives under ``decision_resolution.fast_path.fuzzy_match`` in the
258
+ YAML. When ``enabled`` is ``False`` the classifier uses
259
+ exact-after-normalisation matching only (Phase 12 default).
260
+
261
+ Attributes:
262
+ enabled: Master switch. Default ``False`` — exact match only.
263
+ threshold: ``difflib.SequenceMatcher.ratio()`` cutoff in the
264
+ range ``(0.0, 1.0]``. Default ``0.92``. Two safety vetoes
265
+ apply on top of the ratio test (Iron Law preserved):
266
+ high-impact trigger tokens in the query short-circuit to
267
+ the base verdict, and anti-example similarity at or above
268
+ the validated similarity rejects the match.
269
+ """
270
+
271
+ enabled: bool = False
272
+ threshold: float = 0.92
273
+
274
+
275
+ @dataclass(frozen=True)
276
+ class LowImpactFastPathConfig:
277
+ """Hard caps for the lightweight-QA fast-path (Phase 11).
278
+
279
+ Lives under ``decision_resolution.fast_path`` in the YAML. Used by
280
+ :mod:`scripts.ai_council.low_impact` to assemble the fast-path
281
+ ``CostBudget`` when a ``low_impact`` question routes to ``council``.
282
+
283
+ Attributes:
284
+ max_members: Upper bound on opted-in members invoked per
285
+ resolution. Default ``2``. ``1`` skips quick-consensus
286
+ and returns the single responder's answer; ``2`` runs the
287
+ agreement / disagreement check.
288
+ max_rounds: Locked to ``1`` — the fast-path strips debate.
289
+ Surfaced for documentation; the loader rejects any other
290
+ value with a hard schema error.
291
+ max_tokens: Combined input+output token budget per resolution.
292
+ Default ``2500``. Passed through to ``CostBudget``.
293
+ max_cost_usd: USD cap per resolution. Default ``0.05``.
294
+ fuzzy_match: Opt-in fuzzy corpus matching (step-9 P5). Off by
295
+ default — exact-after-normalisation matching only.
296
+ """
297
+
298
+ max_members: int = 2
299
+ max_rounds: int = 1
300
+ max_tokens: int = 2500
301
+ max_cost_usd: float = 0.05
302
+ fuzzy_match: FuzzyMatchConfig = field(default_factory=FuzzyMatchConfig)
303
+
304
+
305
+ @dataclass(frozen=True)
306
+ class DecisionResolutionConfig:
307
+ """Impact-class → routing map (Phase 10).
308
+
309
+ Keyed by impact class (``trivial`` / ``low_impact`` / ``medium_impact``
310
+ / ``high_impact`` / ``user_required``). The Iron Law rule for
311
+ ``high_impact`` and ``user_required`` is enforced by
312
+ :func:`_build_decision_resolution` — those classes MUST route to
313
+ ``user`` and any other value is a hard schema error.
314
+
315
+ ``fast_path`` carries the Phase 11 hard caps for the lightweight-QA
316
+ resolver. Only consulted when a ``low_impact`` question routes to
317
+ ``council``.
318
+ """
319
+
320
+ enabled: bool = True
321
+ classes: dict[str, DecisionResolutionEntry] = field(default_factory=dict)
322
+ fast_path: LowImpactFastPathConfig = field(
323
+ default_factory=LowImpactFastPathConfig,
324
+ )
325
+
326
+
327
+ @dataclass(frozen=True)
328
+ class LensOverridesConfig:
329
+ """Per-lens overrides keyed by lens name (Phase 6+).
330
+
331
+ Carries necessity-classifier mode overrides (Phase 6),
332
+ model-downgrade overrides (Phase 7), cost-disclosure overrides
333
+ (Phase 8), and decision-replay overrides (Phase 9).
334
+ """
335
+
336
+ necessity_classifier_mode: dict[str, str] = field(default_factory=dict)
337
+ necessity_classifier_user_explicit_mode: dict[str, str] = field(
338
+ default_factory=dict,
339
+ )
340
+ model_downgrade: dict[str, ModelDowngradeConfig] = field(default_factory=dict)
341
+ cost_disclosure: dict[str, CostDisclosureConfig] = field(default_factory=dict)
342
+ decision_replay: dict[str, DecisionReplayConfig] = field(default_factory=dict)
343
+
344
+
345
+ @dataclass(frozen=True)
346
+ class RoutingConfig:
347
+ """Solo-member dispatch fallback chain (step-9 P8/P9 · U2).
348
+
349
+ ``solo_member_fallback_chain`` is the ordered list of provider names
350
+ consulted when the dispatcher resolves a single-member invocation
351
+ (CLI ``--single`` flag or ``low_impact.dispatch: single``). The
352
+ first chain entry whose member is enabled AND has cached-valid auth
353
+ wins. An empty tuple disables solo dispatch — any caller that
354
+ requests it falls back to the full council with a WARN log.
355
+
356
+ ``auth_check_timeout_seconds`` bounds the lazy auth probe per
357
+ member (default ``3``). Loader rejects values outside ``[1, 30]``
358
+ so a misconfigured CLI cannot stall the dispatcher.
359
+ """
360
+
361
+ solo_member_fallback_chain: tuple[str, ...] = ()
362
+ auth_check_timeout_seconds: int = 3
363
+
364
+
365
+ @dataclass(frozen=True)
366
+ class LowImpactConfig:
367
+ """Low-impact dispatch + shadow-mode config (step-9 P8/P10 · U3).
368
+
369
+ ``dispatch`` switches the low-impact resolution shape:
370
+
371
+ - ``full`` (default) — the existing fast-path quick-consensus over
372
+ opted-in members (caps from ``decision_resolution.fast_path``).
373
+ - ``single`` — solo-member dispatch via
374
+ :class:`RoutingConfig.solo_member_fallback_chain`. The loader
375
+ rejects ``single`` when the chain is empty or contains only
376
+ disabled members, so a misconfigured opt-in cannot silently
377
+ escalate to the full council on every call.
378
+
379
+ ``shadow_sample_rate`` is the per-decision sampling probability
380
+ for shadow-mode logging (Phase 10). At ``0.0`` shadow is off; at
381
+ ``1.0`` every solo decision is also dispatched to the full
382
+ council and the verdicts are compared in
383
+ ``agents/council-shadow-log.jsonl``. Must be in ``[0.0, 1.0]``.
384
+
385
+ ``solo_confidence_floor`` is the auto-escalation threshold for
386
+ solo-member responses (step-9 P13). When the dispatcher extracts
387
+ a confidence signal below the floor — or detects a split /
388
+ refusal response — the decision escalates to the full council
389
+ on this invocation, independent of shadow sampling. Default
390
+ ``0.7``; must be in ``[0.0, 1.0]``. The floor is irrelevant when
391
+ ``dispatch=full`` (no solo step to gate).
392
+ """
393
+
394
+ dispatch: str = "full"
395
+ shadow_sample_rate: float = 0.1
396
+ solo_confidence_floor: float = 0.7
397
+
398
+
399
+ @dataclass(frozen=True)
400
+ class CliCallBudgetConfig:
401
+ """Per-day call-count guard for ``mode: cli`` members (Phase 0).
402
+
403
+ The standard ``cost_budget`` gate skips CLI calls because they are
404
+ ``billable=False`` — but provider subscriptions still carry their
405
+ own quotas (Claude Pro 5h windows, ChatGPT Plus message caps,
406
+ Gemini free-tier per-day limits). Users opt into a per-provider
407
+ cap; default unset = unlimited from this loader's perspective.
408
+
409
+ Counter state persists under
410
+ ``~/.event4u/agent-config/cli-calls.json`` with daily UTC reset
411
+ (wired in Phase 1).
412
+
413
+ ``warn_at`` is the fractional threshold (0.0–1.0) at which the
414
+ pre-run quota summary in :func:`council_cli.cmd_run` flips its
415
+ prefix to ``⚠️`` and surfaces a ``council:quota · WARN`` line
416
+ (step-8 P1). Default ``0.8`` is the standard ops-monitoring 80 %
417
+ threshold (step-8 D4). Providers without a configured cap are
418
+ omitted from the summary regardless.
419
+ """
420
+
421
+ max_calls_per_day: dict[str, int] = field(default_factory=dict)
422
+ warn_at: float = 0.8
423
+
424
+
425
+ @dataclass(frozen=True)
426
+ class CouncilConfig:
427
+ enabled: bool
428
+ defaults: DefaultsConfig
429
+ cost_budget: CostBudgetConfig
430
+ members: dict[str, MemberConfig]
431
+ advisors: dict[str, AdvisorConfig] = field(default_factory=dict)
432
+ consensus_scoring: ConsensusScoringConfig = field(
433
+ default_factory=ConsensusScoringConfig,
434
+ )
435
+ cli_call_budget: CliCallBudgetConfig = field(
436
+ default_factory=CliCallBudgetConfig,
437
+ )
438
+ necessity_classifier: NecessityClassifierConfig = field(
439
+ default_factory=NecessityClassifierConfig,
440
+ )
441
+ model_downgrade: ModelDowngradeConfig = field(
442
+ default_factory=ModelDowngradeConfig,
443
+ )
444
+ debate: DebateConfig = field(default_factory=DebateConfig)
445
+ decision_replay: DecisionReplayConfig = field(
446
+ default_factory=DecisionReplayConfig,
447
+ )
448
+ decision_resolution: DecisionResolutionConfig = field(
449
+ default_factory=DecisionResolutionConfig,
450
+ )
451
+ routing: RoutingConfig = field(default_factory=RoutingConfig)
452
+ low_impact: LowImpactConfig = field(default_factory=LowImpactConfig)
453
+ lens_overrides: LensOverridesConfig = field(
454
+ default_factory=LensOverridesConfig,
455
+ )
456
+ source_path: Path | None = None
457
+
458
+
459
+ def load_council_config(path: Path) -> CouncilConfig:
460
+ """Load and validate the council YAML at ``path``."""
461
+ if not path.exists():
462
+ raise CouncilConfigError(
463
+ f"Council config not found at {path}. "
464
+ f"Create it per docs/contracts/ai-council-config.md."
465
+ )
466
+ try:
467
+ raw = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
468
+ except yaml.YAMLError as exc:
469
+ raise CouncilConfigError(f"{path}: invalid YAML — {exc}") from exc
470
+ if not isinstance(raw, dict):
471
+ raise CouncilConfigError(f"{path}: top-level must be a mapping.")
472
+ return _build_config(raw, source_path=path)
473
+
474
+
475
+ def _build_config(raw: dict[str, Any], *, source_path: Path) -> CouncilConfig:
476
+ enabled = raw.get("enabled", False)
477
+ if not isinstance(enabled, bool):
478
+ raise CouncilConfigError("`enabled` must be a bool.")
479
+
480
+ defaults = _build_defaults(raw.get("defaults") or {})
481
+ cost_budget = _build_cost_budget(raw.get("cost_budget") or {})
482
+
483
+ members_raw = raw.get("members") or {}
484
+ if not isinstance(members_raw, dict):
485
+ raise CouncilConfigError("`members` must be a mapping.")
486
+ members: dict[str, MemberConfig] = {}
487
+ for name, cfg in members_raw.items():
488
+ members[name] = _build_member(name, cfg or {}, default_mode=defaults.mode)
489
+
490
+ advisors_raw = raw.get("advisors") or {}
491
+ if not isinstance(advisors_raw, dict):
492
+ raise CouncilConfigError("`advisors` must be a mapping.")
493
+ advisors: dict[str, AdvisorConfig] = {}
494
+ for adv_name, adv_cfg in advisors_raw.items():
495
+ advisors[adv_name] = _build_advisor(adv_name, adv_cfg or {})
496
+
497
+ # Cross-validate enabled advisors against the members block. An
498
+ # advisor referencing a missing or disabled member is a hard error
499
+ # — never a silent skip — so a typo never costs the user money on
500
+ # an unintended call plan.
501
+ for adv in advisors.values():
502
+ if not adv.enabled:
503
+ continue
504
+ bound = members.get(adv.member)
505
+ if bound is None:
506
+ raise CouncilConfigError(
507
+ f"advisors.{adv.name}.member={adv.member!r}: no such "
508
+ f"member in the `members` block."
509
+ )
510
+ if not bound.enabled:
511
+ raise CouncilConfigError(
512
+ f"advisors.{adv.name}.member={adv.member!r}: member "
513
+ f"exists but is disabled. Enable the member or disable "
514
+ f"the advisor."
515
+ )
516
+
517
+ consensus = _build_consensus_scoring(raw.get("consensus_scoring") or {})
518
+ cli_call_budget = _build_cli_call_budget(raw.get("cli_call_budget") or {})
519
+ necessity_classifier = _build_necessity_classifier(
520
+ raw.get("necessity_classifier") or {},
521
+ )
522
+ model_downgrade = _build_model_downgrade(raw.get("model_downgrade") or {})
523
+ debate = _build_debate(raw.get("debate") or {})
524
+ decision_replay = _build_decision_replay(
525
+ raw.get("decision_replay") or {}, path="decision_replay",
526
+ )
527
+ decision_resolution = _build_decision_resolution(
528
+ raw.get("decision_resolution") or {},
529
+ )
530
+ routing = _build_routing(raw.get("routing") or {}, members=members)
531
+ low_impact = _build_low_impact(
532
+ raw.get("low_impact") or {},
533
+ members=members,
534
+ routing=routing,
535
+ )
536
+ _reject_top_level_locked_dispatch(raw)
537
+ lens_overrides = _build_lens_overrides(raw.get("lenses") or {})
538
+
539
+ return CouncilConfig(
540
+ enabled=enabled,
541
+ defaults=defaults,
542
+ cost_budget=cost_budget,
543
+ members=members,
544
+ advisors=advisors,
545
+ consensus_scoring=consensus,
546
+ cli_call_budget=cli_call_budget,
547
+ necessity_classifier=necessity_classifier,
548
+ model_downgrade=model_downgrade,
549
+ debate=debate,
550
+ decision_replay=decision_replay,
551
+ decision_resolution=decision_resolution,
552
+ routing=routing,
553
+ low_impact=low_impact,
554
+ lens_overrides=lens_overrides,
555
+ source_path=source_path,
556
+ )
557
+
558
+
559
+ def _build_necessity_classifier(d: dict[str, Any]) -> NecessityClassifierConfig:
560
+ if not isinstance(d, dict):
561
+ raise CouncilConfigError("`necessity_classifier` must be a mapping.")
562
+ enabled = d.get("enabled", True)
563
+ if not isinstance(enabled, bool):
564
+ raise CouncilConfigError("`necessity_classifier.enabled` must be a bool.")
565
+ mode = d.get("mode", "educate")
566
+ if mode not in _VALID_NECESSITY_MODES:
567
+ raise CouncilConfigError(
568
+ f"necessity_classifier.mode={mode!r} not in "
569
+ f"{sorted(_VALID_NECESSITY_MODES)}."
570
+ )
571
+ user_explicit_mode = d.get("user_explicit_mode", "warn-only")
572
+ if user_explicit_mode not in _VALID_NECESSITY_MODES:
573
+ raise CouncilConfigError(
574
+ f"necessity_classifier.user_explicit_mode="
575
+ f"{user_explicit_mode!r} not in "
576
+ f"{sorted(_VALID_NECESSITY_MODES)}."
577
+ )
578
+ return NecessityClassifierConfig(
579
+ enabled=bool(enabled),
580
+ mode=mode,
581
+ user_explicit_mode=user_explicit_mode,
582
+ )
583
+
584
+
585
+ def _build_model_downgrade(d: dict[str, Any]) -> ModelDowngradeConfig:
586
+ if not isinstance(d, dict):
587
+ raise CouncilConfigError("`model_downgrade` must be a mapping.")
588
+ enabled = d.get("enabled", True)
589
+ if not isinstance(enabled, bool):
590
+ raise CouncilConfigError("`model_downgrade.enabled` must be a bool.")
591
+ auto_apply = d.get("auto_apply", False)
592
+ if not isinstance(auto_apply, bool):
593
+ raise CouncilConfigError("`model_downgrade.auto_apply` must be a bool.")
594
+ return ModelDowngradeConfig(enabled=bool(enabled), auto_apply=bool(auto_apply))
595
+
596
+
597
+ def _build_cost_disclosure(
598
+ d: dict[str, Any], *, path: str,
599
+ ) -> CostDisclosureConfig:
600
+ if not isinstance(d, dict):
601
+ raise CouncilConfigError(f"`{path}` must be a mapping.")
602
+ mode = d.get("mode", "always")
603
+ if mode not in _VALID_DISCLOSURE_MODES:
604
+ raise CouncilConfigError(
605
+ f"{path}.mode={mode!r} not in {sorted(_VALID_DISCLOSURE_MODES)}."
606
+ )
607
+ threshold = float(d.get("threshold_usd", 1.00))
608
+ if threshold < 0:
609
+ raise CouncilConfigError(
610
+ f"{path}.threshold_usd must be >= 0 (got {threshold!r})."
611
+ )
612
+ show_per_member = d.get("show_per_member", True)
613
+ if not isinstance(show_per_member, bool):
614
+ raise CouncilConfigError(
615
+ f"`{path}.show_per_member` must be a bool."
616
+ )
617
+ return CostDisclosureConfig(
618
+ mode=mode,
619
+ threshold_usd=threshold,
620
+ show_per_member=bool(show_per_member),
621
+ )
622
+
623
+
624
+ def _build_debate(d: dict[str, Any]) -> DebateConfig:
625
+ if not isinstance(d, dict):
626
+ raise CouncilConfigError("`debate` must be a mapping.")
627
+ cap = float(d.get("max_cost_usd", 5.00))
628
+ if cap < 0:
629
+ raise CouncilConfigError(
630
+ f"debate.max_cost_usd must be >= 0 (got {cap!r}; "
631
+ f"use 0 to disable the cap)."
632
+ )
633
+ disclosure_raw = d.get("cost_disclosure") or {}
634
+ disclosure = _build_cost_disclosure(
635
+ disclosure_raw, path="debate.cost_disclosure",
636
+ )
637
+ return DebateConfig(max_cost_usd=cap, cost_disclosure=disclosure)
638
+
639
+
640
+ def _build_decision_replay(
641
+ d: dict[str, Any], *, path: str,
642
+ ) -> DecisionReplayConfig:
643
+ if not isinstance(d, dict):
644
+ raise CouncilConfigError(f"`{path}` must be a mapping.")
645
+ enabled = d.get("enabled", True)
646
+ if not isinstance(enabled, bool):
647
+ raise CouncilConfigError(f"`{path}.enabled` must be a bool.")
648
+ include_args = d.get("include_member_arguments", True)
649
+ if not isinstance(include_args, bool):
650
+ raise CouncilConfigError(
651
+ f"`{path}.include_member_arguments` must be a bool."
652
+ )
653
+ return DecisionReplayConfig(
654
+ enabled=bool(enabled),
655
+ include_member_arguments=bool(include_args),
656
+ )
657
+
658
+
659
+ _VALID_RESOLUTION_MODES = frozenset({"agent", "council", "user"})
660
+ _IMPACT_CLASSES = (
661
+ "trivial", "low_impact", "medium_impact", "high_impact", "user_required",
662
+ )
663
+ _LOCKED_IMPACT_CLASSES = frozenset({"high_impact", "user_required"})
664
+
665
+ _DEFAULT_RESOLUTION_MODES: dict[str, str] = {
666
+ "trivial": "agent",
667
+ "low_impact": "agent",
668
+ "medium_impact": "council",
669
+ "high_impact": "user",
670
+ "user_required": "user",
671
+ }
672
+
673
+
674
+ def _build_decision_resolution(
675
+ d: dict[str, Any],
676
+ ) -> DecisionResolutionConfig:
677
+ """Build the impact-class → routing map (Phase 10).
678
+
679
+ Enforces the Iron Law: ``high_impact`` and ``user_required`` MUST
680
+ route to ``user``. Any other value is a hard schema error — no
681
+ silent fall-back, no override path.
682
+ """
683
+ if not isinstance(d, dict):
684
+ raise CouncilConfigError("`decision_resolution` must be a mapping.")
685
+ enabled = d.get("enabled", True)
686
+ if not isinstance(enabled, bool):
687
+ raise CouncilConfigError(
688
+ "`decision_resolution.enabled` must be a bool."
689
+ )
690
+ classes_raw = d.get("classes") or {}
691
+ if not isinstance(classes_raw, dict):
692
+ raise CouncilConfigError(
693
+ "`decision_resolution.classes` must be a mapping."
694
+ )
695
+ classes: dict[str, DecisionResolutionEntry] = {}
696
+ for cls in _IMPACT_CLASSES:
697
+ entry_raw = classes_raw.get(cls) or {}
698
+ if not isinstance(entry_raw, dict):
699
+ raise CouncilConfigError(
700
+ f"`decision_resolution.classes.{cls}` must be a mapping."
701
+ )
702
+ mode = entry_raw.get("mode", _DEFAULT_RESOLUTION_MODES[cls])
703
+ if mode not in _VALID_RESOLUTION_MODES:
704
+ raise CouncilConfigError(
705
+ f"decision_resolution.classes.{cls}.mode={mode!r} not in "
706
+ f"{sorted(_VALID_RESOLUTION_MODES)}."
707
+ )
708
+ # Iron Law: high_impact + user_required are locked to user.
709
+ if cls in _LOCKED_IMPACT_CLASSES and mode != "user":
710
+ raise CouncilConfigError(
711
+ f"decision_resolution.classes.{cls}.mode={mode!r}: "
712
+ f"class `{cls}` is LOCKED to `user` (Iron Law) — "
713
+ f"high-impact and user-required decisions never bypass "
714
+ f"the user."
715
+ )
716
+ # Iron Law: `dispatch` is not configurable for locked classes
717
+ # (step-9 P8/P11 · U3). Any nested `dispatch` key — including
718
+ # smuggled-in YAML anchor merges — is a hard schema error.
719
+ if cls in _LOCKED_IMPACT_CLASSES and "dispatch" in entry_raw:
720
+ raise CouncilConfigError(
721
+ f"decision_resolution.classes.{cls}.dispatch="
722
+ f"{entry_raw['dispatch']!r}: dispatch is not "
723
+ f"configurable for high-impact / user-required "
724
+ f"decisions — always full council."
725
+ )
726
+ threshold = float(entry_raw.get("confidence_threshold", 0.6))
727
+ if not 0.0 <= threshold <= 1.0:
728
+ raise CouncilConfigError(
729
+ f"decision_resolution.classes.{cls}.confidence_threshold "
730
+ f"must be in [0.0, 1.0] (got {threshold!r})."
731
+ )
732
+ classes[cls] = DecisionResolutionEntry(
733
+ mode=mode,
734
+ confidence_threshold=threshold,
735
+ )
736
+ fast_path_raw = d.get("fast_path") or {}
737
+ if not isinstance(fast_path_raw, dict):
738
+ raise CouncilConfigError(
739
+ "`decision_resolution.fast_path` must be a mapping."
740
+ )
741
+ fast_path = _build_fast_path(fast_path_raw)
742
+ return DecisionResolutionConfig(
743
+ enabled=bool(enabled),
744
+ classes=classes,
745
+ fast_path=fast_path,
746
+ )
747
+
748
+
749
+ def _build_fast_path(d: dict[str, Any]) -> LowImpactFastPathConfig:
750
+ """Parse `decision_resolution.fast_path` into a frozen config.
751
+
752
+ Hard caps for the lightweight-QA resolver (Phase 11). ``max_rounds``
753
+ is locked to ``1`` — any other value is a hard schema error. The
754
+ other fields are positive numbers within sane bounds; out-of-range
755
+ values are rejected to prevent silent token / cost blow-ups.
756
+ """
757
+ max_members = d.get("max_members", 2)
758
+ if not isinstance(max_members, int) or isinstance(max_members, bool):
759
+ raise CouncilConfigError(
760
+ "decision_resolution.fast_path.max_members must be an int "
761
+ f"(got {type(max_members).__name__})."
762
+ )
763
+ if max_members < 1 or max_members > 2:
764
+ raise CouncilConfigError(
765
+ "decision_resolution.fast_path.max_members must be 1 or 2 "
766
+ f"(got {max_members}). Fast-path is by design a 1-2 member "
767
+ "lookup — wider fan-out belongs in the standard council path."
768
+ )
769
+ max_rounds = d.get("max_rounds", 1)
770
+ if max_rounds != 1:
771
+ raise CouncilConfigError(
772
+ "decision_resolution.fast_path.max_rounds is LOCKED to 1 "
773
+ f"(got {max_rounds!r}). Multi-round fast-paths defeat the "
774
+ "purpose — escalate to standard council instead."
775
+ )
776
+ max_tokens = d.get("max_tokens", 2500)
777
+ if (
778
+ not isinstance(max_tokens, int)
779
+ or isinstance(max_tokens, bool)
780
+ or max_tokens <= 0
781
+ ):
782
+ raise CouncilConfigError(
783
+ "decision_resolution.fast_path.max_tokens must be a positive "
784
+ f"int (got {max_tokens!r})."
785
+ )
786
+ max_cost_raw = d.get("max_cost_usd", 0.05)
787
+ if isinstance(max_cost_raw, bool) or not isinstance(
788
+ max_cost_raw, (int, float)
789
+ ):
790
+ raise CouncilConfigError(
791
+ "decision_resolution.fast_path.max_cost_usd must be a "
792
+ f"number (got {type(max_cost_raw).__name__})."
793
+ )
794
+ max_cost = float(max_cost_raw)
795
+ if max_cost <= 0.0:
796
+ raise CouncilConfigError(
797
+ "decision_resolution.fast_path.max_cost_usd must be > 0 "
798
+ f"(got {max_cost!r})."
799
+ )
800
+ fuzzy_match = _build_fuzzy_match(d.get("fuzzy_match") or {})
801
+ return LowImpactFastPathConfig(
802
+ max_members=max_members,
803
+ max_rounds=1,
804
+ max_tokens=max_tokens,
805
+ max_cost_usd=max_cost,
806
+ fuzzy_match=fuzzy_match,
807
+ )
808
+
809
+
810
+ def _build_fuzzy_match(d: dict[str, Any]) -> FuzzyMatchConfig:
811
+ """Parse ``decision_resolution.fast_path.fuzzy_match`` (step-9 P5).
812
+
813
+ Opt-in only: defaults to ``enabled=False, threshold=0.92``. A
814
+ threshold outside ``(0.0, 1.0]`` is a hard schema error — sub-0.5
815
+ matches are noise; ``0.0`` would auto-match anything; ``> 1.0`` is
816
+ impossible by definition of the ratio.
817
+ """
818
+ if not isinstance(d, dict):
819
+ raise CouncilConfigError(
820
+ "decision_resolution.fast_path.fuzzy_match must be a mapping."
821
+ )
822
+ enabled = d.get("enabled", False)
823
+ if not isinstance(enabled, bool):
824
+ raise CouncilConfigError(
825
+ "decision_resolution.fast_path.fuzzy_match.enabled must be a bool "
826
+ f"(got {type(enabled).__name__})."
827
+ )
828
+ threshold_raw = d.get("threshold", 0.92)
829
+ if isinstance(threshold_raw, bool) or not isinstance(
830
+ threshold_raw, (int, float)
831
+ ):
832
+ raise CouncilConfigError(
833
+ "decision_resolution.fast_path.fuzzy_match.threshold must be a "
834
+ f"number (got {type(threshold_raw).__name__})."
835
+ )
836
+ threshold = float(threshold_raw)
837
+ if not (0.0 < threshold <= 1.0):
838
+ raise CouncilConfigError(
839
+ "decision_resolution.fast_path.fuzzy_match.threshold must be in "
840
+ f"(0.0, 1.0] (got {threshold!r})."
841
+ )
842
+ return FuzzyMatchConfig(enabled=enabled, threshold=threshold)
843
+
844
+
845
+
846
+ def _build_lens_overrides(d: dict[str, Any]) -> LensOverridesConfig:
847
+ if not isinstance(d, dict):
848
+ raise CouncilConfigError("`lenses` must be a mapping.")
849
+ nc_overrides: dict[str, str] = {}
850
+ nc_user_overrides: dict[str, str] = {}
851
+ md_overrides: dict[str, ModelDowngradeConfig] = {}
852
+ cd_overrides: dict[str, CostDisclosureConfig] = {}
853
+ dr_overrides: dict[str, DecisionReplayConfig] = {}
854
+ for lens_name, lens_cfg in d.items():
855
+ if not isinstance(lens_cfg, dict):
856
+ raise CouncilConfigError(
857
+ f"`lenses.{lens_name}` must be a mapping."
858
+ )
859
+ nc_block = lens_cfg.get("necessity_classifier")
860
+ if nc_block is not None:
861
+ if not isinstance(nc_block, dict):
862
+ raise CouncilConfigError(
863
+ f"`lenses.{lens_name}.necessity_classifier` must be a mapping."
864
+ )
865
+ mode = nc_block.get("mode")
866
+ if mode is not None:
867
+ if mode not in _VALID_NECESSITY_MODES:
868
+ raise CouncilConfigError(
869
+ f"lenses.{lens_name}.necessity_classifier.mode={mode!r} "
870
+ f"not in {sorted(_VALID_NECESSITY_MODES)}."
871
+ )
872
+ nc_overrides[lens_name] = mode
873
+ user_mode = nc_block.get("user_explicit_mode")
874
+ if user_mode is not None:
875
+ if user_mode not in _VALID_NECESSITY_MODES:
876
+ raise CouncilConfigError(
877
+ f"lenses.{lens_name}.necessity_classifier."
878
+ f"user_explicit_mode={user_mode!r} "
879
+ f"not in {sorted(_VALID_NECESSITY_MODES)}."
880
+ )
881
+ nc_user_overrides[lens_name] = user_mode
882
+ md_block = lens_cfg.get("model_downgrade")
883
+ if md_block is not None:
884
+ if not isinstance(md_block, dict):
885
+ raise CouncilConfigError(
886
+ f"`lenses.{lens_name}.model_downgrade` must be a mapping."
887
+ )
888
+ md_enabled = md_block.get("enabled", True)
889
+ if not isinstance(md_enabled, bool):
890
+ raise CouncilConfigError(
891
+ f"`lenses.{lens_name}.model_downgrade.enabled` must be a bool."
892
+ )
893
+ md_auto = md_block.get("auto_apply", False)
894
+ if not isinstance(md_auto, bool):
895
+ raise CouncilConfigError(
896
+ f"`lenses.{lens_name}.model_downgrade.auto_apply` must be a bool."
897
+ )
898
+ md_overrides[lens_name] = ModelDowngradeConfig(
899
+ enabled=bool(md_enabled),
900
+ auto_apply=bool(md_auto),
901
+ )
902
+ cd_block = lens_cfg.get("cost_disclosure")
903
+ if cd_block is not None:
904
+ cd_overrides[lens_name] = _build_cost_disclosure(
905
+ cd_block, path=f"lenses.{lens_name}.cost_disclosure",
906
+ )
907
+ dr_block = lens_cfg.get("decision_replay")
908
+ if dr_block is not None:
909
+ dr_overrides[lens_name] = _build_decision_replay(
910
+ dr_block, path=f"lenses.{lens_name}.decision_replay",
911
+ )
912
+ return LensOverridesConfig(
913
+ necessity_classifier_mode=nc_overrides,
914
+ necessity_classifier_user_explicit_mode=nc_user_overrides,
915
+ model_downgrade=md_overrides,
916
+ cost_disclosure=cd_overrides,
917
+ decision_replay=dr_overrides,
918
+ )
919
+
920
+
921
+ def _build_consensus_scoring(d: dict[str, Any]) -> ConsensusScoringConfig:
922
+ if not isinstance(d, dict):
923
+ raise CouncilConfigError("`consensus_scoring` must be a mapping.")
924
+ strong = float(d.get("strong_threshold", 0.7))
925
+ minority = float(d.get("minority_threshold", 0.4))
926
+ if not 0.0 <= minority <= strong <= 1.0:
927
+ raise CouncilConfigError(
928
+ f"consensus_scoring thresholds broken: require "
929
+ f"0 <= minority ({minority}) <= strong ({strong}) <= 1."
930
+ )
931
+ lenses_raw = d.get("lenses", ["analysis"])
932
+ if not isinstance(lenses_raw, list) or not all(isinstance(x, str) for x in lenses_raw):
933
+ raise CouncilConfigError("`consensus_scoring.lenses` must be a list of strings.")
934
+ return ConsensusScoringConfig(
935
+ enabled=bool(d.get("enabled", False)),
936
+ strong_threshold=strong,
937
+ minority_threshold=minority,
938
+ lenses=tuple(lenses_raw),
939
+ )
940
+
941
+
942
+ _VALID_MEMBER_MODES = frozenset({"cli", "api"})
943
+
944
+
945
+ def _build_defaults(d: dict[str, Any]) -> DefaultsConfig:
946
+ if not isinstance(d, dict):
947
+ raise CouncilConfigError("`defaults` must be a mapping.")
948
+ mode = d.get("mode", "api")
949
+ if mode not in _VALID_MODES:
950
+ raise CouncilConfigError(
951
+ f"defaults.mode={mode!r} not in {sorted(_VALID_MODES)}."
952
+ )
953
+ # `member_mode` (step-9 P8 · U1) — global preference for solo /
954
+ # CLI-mode invocations. Narrower set than `defaults.mode` because
955
+ # `manual` makes no sense as a per-member dispatch default.
956
+ member_mode = d.get("member_mode", "cli")
957
+ if member_mode not in _VALID_MEMBER_MODES:
958
+ raise CouncilConfigError(
959
+ f"defaults.member_mode={member_mode!r} not in "
960
+ f"{sorted(_VALID_MEMBER_MODES)}."
961
+ )
962
+ return DefaultsConfig(
963
+ mode=mode,
964
+ member_mode=member_mode,
965
+ min_rounds=int(d.get("min_rounds", 2)),
966
+ deep_min_rounds=int(d.get("deep_min_rounds", 3)),
967
+ max_output_tokens=int(d.get("max_output_tokens", 0)),
968
+ session_retention_days=int(d.get("session_retention_days", 7)),
969
+ debate_max_rounds=int(d.get("debate_max_rounds", 4)),
970
+ )
971
+
972
+
973
+ _VALID_DISPATCH_MODES = frozenset({"full", "single"})
974
+
975
+
976
+ def _build_routing(
977
+ d: dict[str, Any],
978
+ *,
979
+ members: dict[str, MemberConfig],
980
+ ) -> RoutingConfig:
981
+ """Parse ``routing`` block (step-9 P8 · U2).
982
+
983
+ Validates the solo-member fallback chain shape:
984
+
985
+ - Must be a list of strings.
986
+ - Each entry must be a configured member name.
987
+ - Duplicate entries are rejected — a chain is an order, not a set.
988
+
989
+ Auth-check timeout must be in ``[1, 30]`` seconds. Empty chain
990
+ is legal; it just disables solo dispatch.
991
+ """
992
+ if not isinstance(d, dict):
993
+ raise CouncilConfigError("`routing` must be a mapping.")
994
+ chain_raw = d.get("solo_member_fallback_chain", [])
995
+ if not isinstance(chain_raw, list):
996
+ raise CouncilConfigError(
997
+ "`routing.solo_member_fallback_chain` must be a list "
998
+ f"(got {type(chain_raw).__name__})."
999
+ )
1000
+ chain: list[str] = []
1001
+ seen: set[str] = set()
1002
+ for idx, entry in enumerate(chain_raw):
1003
+ if not isinstance(entry, str) or not entry.strip():
1004
+ raise CouncilConfigError(
1005
+ f"routing.solo_member_fallback_chain[{idx}]: each "
1006
+ f"entry must be a non-empty string (got {entry!r})."
1007
+ )
1008
+ if entry in seen:
1009
+ raise CouncilConfigError(
1010
+ f"routing.solo_member_fallback_chain[{idx}]: "
1011
+ f"duplicate entry {entry!r} — chain order must be "
1012
+ f"unique."
1013
+ )
1014
+ if entry not in members:
1015
+ raise CouncilConfigError(
1016
+ f"routing.solo_member_fallback_chain[{idx}]={entry!r}: "
1017
+ f"no such member in the `members` block."
1018
+ )
1019
+ seen.add(entry)
1020
+ chain.append(entry)
1021
+ timeout_raw = d.get("auth_check_timeout_seconds", 3)
1022
+ if (
1023
+ not isinstance(timeout_raw, int)
1024
+ or isinstance(timeout_raw, bool)
1025
+ or not 1 <= timeout_raw <= 30
1026
+ ):
1027
+ raise CouncilConfigError(
1028
+ "routing.auth_check_timeout_seconds must be an int in "
1029
+ f"[1, 30] (got {timeout_raw!r})."
1030
+ )
1031
+ return RoutingConfig(
1032
+ solo_member_fallback_chain=tuple(chain),
1033
+ auth_check_timeout_seconds=timeout_raw,
1034
+ )
1035
+
1036
+
1037
+ def _build_low_impact(
1038
+ d: dict[str, Any],
1039
+ *,
1040
+ members: dict[str, MemberConfig],
1041
+ routing: RoutingConfig,
1042
+ ) -> LowImpactConfig:
1043
+ """Parse ``low_impact`` block (step-9 P8 · U3).
1044
+
1045
+ ``dispatch: single`` is only legal when the routing fallback
1046
+ chain has at least one enabled member — otherwise the loader
1047
+ rejects with a clear error. A misconfigured opt-in must not
1048
+ silently escalate to the full council on every call.
1049
+ """
1050
+ if not isinstance(d, dict):
1051
+ raise CouncilConfigError("`low_impact` must be a mapping.")
1052
+ # Iron Law: `dispatch` is the only place where dispatch shape
1053
+ # is configurable. Reject any `dispatch` key on the locked
1054
+ # impact classes — covered separately at top-level by
1055
+ # `_reject_top_level_locked_dispatch`.
1056
+ dispatch = d.get("dispatch", "full")
1057
+ if dispatch not in _VALID_DISPATCH_MODES:
1058
+ raise CouncilConfigError(
1059
+ f"low_impact.dispatch={dispatch!r} not in "
1060
+ f"{sorted(_VALID_DISPATCH_MODES)}."
1061
+ )
1062
+ if dispatch == "single":
1063
+ enabled_in_chain = [
1064
+ name
1065
+ for name in routing.solo_member_fallback_chain
1066
+ if members.get(name) is not None and members[name].enabled
1067
+ ]
1068
+ if not enabled_in_chain:
1069
+ raise CouncilConfigError(
1070
+ "low_impact.dispatch='single' requires at least one "
1071
+ "enabled member in routing.solo_member_fallback_chain "
1072
+ f"(chain={list(routing.solo_member_fallback_chain)!r}). "
1073
+ "Enable a chain member or set dispatch back to 'full'."
1074
+ )
1075
+ shadow_raw = d.get("shadow_sample_rate", 0.1)
1076
+ if isinstance(shadow_raw, bool) or not isinstance(shadow_raw, (int, float)):
1077
+ raise CouncilConfigError(
1078
+ "low_impact.shadow_sample_rate must be a number "
1079
+ f"(got {type(shadow_raw).__name__})."
1080
+ )
1081
+ shadow = float(shadow_raw)
1082
+ if not 0.0 <= shadow <= 1.0:
1083
+ raise CouncilConfigError(
1084
+ "low_impact.shadow_sample_rate must be in [0.0, 1.0] "
1085
+ f"(got {shadow!r})."
1086
+ )
1087
+ floor_raw = d.get("solo_confidence_floor", 0.7)
1088
+ if isinstance(floor_raw, bool) or not isinstance(floor_raw, (int, float)):
1089
+ raise CouncilConfigError(
1090
+ "low_impact.solo_confidence_floor must be a number "
1091
+ f"(got {type(floor_raw).__name__})."
1092
+ )
1093
+ floor = float(floor_raw)
1094
+ if not 0.0 <= floor <= 1.0:
1095
+ raise CouncilConfigError(
1096
+ "low_impact.solo_confidence_floor must be in [0.0, 1.0] "
1097
+ f"(got {floor!r})."
1098
+ )
1099
+ return LowImpactConfig(
1100
+ dispatch=dispatch,
1101
+ shadow_sample_rate=shadow,
1102
+ solo_confidence_floor=floor,
1103
+ )
1104
+
1105
+
1106
+ def _reject_top_level_locked_dispatch(raw: dict[str, Any]) -> None:
1107
+ """Iron Law (step-9 P8/P11/P13 · U3) — reject locked-class dispatch keys.
1108
+
1109
+ Some authors will write `high_impact.dispatch: single` at the top
1110
+ level (mirroring `low_impact.dispatch`) rather than nested under
1111
+ `decision_resolution.classes`. Catch that shape too so the Iron
1112
+ Law cannot be bypassed by surface choice. Also covers
1113
+ `solo_confidence_floor` — meaningless on locked classes (they
1114
+ never dispatch solo) and a confusing knob to leave reachable.
1115
+ """
1116
+ for cls in _LOCKED_IMPACT_CLASSES:
1117
+ block = raw.get(cls)
1118
+ if not isinstance(block, dict):
1119
+ continue
1120
+ if "dispatch" in block:
1121
+ raise CouncilConfigError(
1122
+ f"{cls}.dispatch={block['dispatch']!r}: dispatch is "
1123
+ f"not configurable for high-impact / user-required "
1124
+ f"decisions — always full council."
1125
+ )
1126
+ if "solo_confidence_floor" in block:
1127
+ raise CouncilConfigError(
1128
+ f"{cls}.solo_confidence_floor="
1129
+ f"{block['solo_confidence_floor']!r}: irrelevant on "
1130
+ f"high-impact / user-required classes — they never "
1131
+ f"dispatch solo. Set on `low_impact` instead."
1132
+ )
1133
+
1134
+
1135
+ def _build_cost_budget(d: dict[str, Any]) -> CostBudgetConfig:
1136
+ if not isinstance(d, dict):
1137
+ raise CouncilConfigError("`cost_budget` must be a mapping.")
1138
+ cb = CostBudgetConfig(
1139
+ max_input_tokens=int(d.get("max_input_tokens", 500_000)),
1140
+ max_output_tokens=int(d.get("max_output_tokens", 200_000)),
1141
+ max_calls=int(d.get("max_calls", 50)),
1142
+ max_total_usd=float(d.get("max_total_usd", 20.0)),
1143
+ )
1144
+ for fname in ("max_input_tokens", "max_output_tokens", "max_calls", "max_total_usd"):
1145
+ val = getattr(cb, fname)
1146
+ if val < 0:
1147
+ raise CouncilConfigError(
1148
+ f"cost_budget.{fname} must be >= 0 (got {val!r})."
1149
+ )
1150
+ return cb
1151
+
1152
+
1153
+ def _build_member(
1154
+ name: str,
1155
+ cfg: dict[str, Any],
1156
+ *,
1157
+ default_mode: str = "api",
1158
+ ) -> MemberConfig:
1159
+ if name not in _VALID_PROVIDERS:
1160
+ raise CouncilConfigError(
1161
+ f"members.{name}: unknown provider; valid: {sorted(_VALID_PROVIDERS)}."
1162
+ )
1163
+ member_enabled = bool(cfg.get("enabled", False))
1164
+ model = cfg.get("model") or ""
1165
+ api_key_ref = cfg.get("api_key_ref")
1166
+ member_mode = cfg.get("mode")
1167
+ if member_mode is not None and member_mode not in _VALID_MODES:
1168
+ raise CouncilConfigError(
1169
+ f"members.{name}.mode={member_mode!r} not in {sorted(_VALID_MODES)}."
1170
+ )
1171
+ effective_mode = member_mode or default_mode
1172
+ binary = cfg.get("binary")
1173
+ if binary is not None:
1174
+ if not isinstance(binary, str) or not binary.strip():
1175
+ raise CouncilConfigError(
1176
+ f"members.{name}.binary must be a non-empty string when set."
1177
+ )
1178
+ if effective_mode != "cli":
1179
+ raise CouncilConfigError(
1180
+ f"members.{name}.binary is only valid when the member's "
1181
+ f"effective mode is 'cli' (got {effective_mode!r}). Set "
1182
+ f"`mode: cli` on the member or `defaults.mode: cli` to use "
1183
+ f"this field."
1184
+ )
1185
+ if member_enabled:
1186
+ if not model:
1187
+ raise CouncilConfigError(
1188
+ f"members.{name}: enabled members require a non-empty `model`."
1189
+ )
1190
+ # CLI-mode members authenticate via the subscription bound to
1191
+ # the local CLI binary; api_key_ref is not required for them.
1192
+ # Manual mode is human-transported and also key-free. Only
1193
+ # api-mode members must supply an api_key_ref.
1194
+ if effective_mode == "api" and not api_key_ref:
1195
+ raise CouncilConfigError(
1196
+ f"members.{name}: enabled api-mode members require an `api_key_ref`."
1197
+ )
1198
+ if api_key_ref is not None:
1199
+ _validate_api_key_ref(f"members.{name}", api_key_ref)
1200
+ ladder_raw = cfg.get("model_ladder") or ()
1201
+ if not isinstance(ladder_raw, (list, tuple)):
1202
+ raise CouncilConfigError(
1203
+ f"members.{name}.model_ladder must be a list (got "
1204
+ f"{type(ladder_raw).__name__})."
1205
+ )
1206
+ ladder: tuple[str, ...] = ()
1207
+ if ladder_raw:
1208
+ entries: list[str] = []
1209
+ for entry in ladder_raw:
1210
+ if not isinstance(entry, str) or not entry.strip():
1211
+ raise CouncilConfigError(
1212
+ f"members.{name}.model_ladder entries must be non-empty "
1213
+ f"strings (got {entry!r})."
1214
+ )
1215
+ entries.append(entry)
1216
+ if member_enabled and model and model not in entries:
1217
+ raise CouncilConfigError(
1218
+ f"members.{name}.model_ladder must include the active "
1219
+ f"`model` ({model!r}); got {entries!r}."
1220
+ )
1221
+ ladder = tuple(entries)
1222
+ participate_raw = cfg.get("participate_low_impact", False)
1223
+ if not isinstance(participate_raw, bool):
1224
+ raise CouncilConfigError(
1225
+ f"members.{name}.participate_low_impact must be a bool "
1226
+ f"(got {type(participate_raw).__name__})."
1227
+ )
1228
+ return MemberConfig(
1229
+ name=name,
1230
+ enabled=member_enabled,
1231
+ model=model,
1232
+ api_key_ref=api_key_ref,
1233
+ mode=member_mode,
1234
+ binary=binary,
1235
+ model_ladder=ladder,
1236
+ participate_low_impact=participate_raw,
1237
+ )
1238
+
1239
+
1240
+ def _build_cli_call_budget(d: dict[str, Any]) -> CliCallBudgetConfig:
1241
+ if not isinstance(d, dict):
1242
+ raise CouncilConfigError("`cli_call_budget` must be a mapping.")
1243
+ raw_caps = d.get("max_calls_per_day") or {}
1244
+ if not isinstance(raw_caps, dict):
1245
+ raise CouncilConfigError(
1246
+ "`cli_call_budget.max_calls_per_day` must be a mapping."
1247
+ )
1248
+ caps: dict[str, int] = {}
1249
+ for provider, value in raw_caps.items():
1250
+ if provider not in _VALID_PROVIDERS:
1251
+ raise CouncilConfigError(
1252
+ f"cli_call_budget.max_calls_per_day.{provider}: unknown "
1253
+ f"provider; valid: {sorted(_VALID_PROVIDERS)}."
1254
+ )
1255
+ if not isinstance(value, int) or isinstance(value, bool) or value < 0:
1256
+ raise CouncilConfigError(
1257
+ f"cli_call_budget.max_calls_per_day.{provider} must be a "
1258
+ f"non-negative integer (got {value!r})."
1259
+ )
1260
+ caps[provider] = value
1261
+ warn_at_raw = d.get("warn_at", 0.8)
1262
+ if isinstance(warn_at_raw, bool) or not isinstance(warn_at_raw, (int, float)):
1263
+ raise CouncilConfigError(
1264
+ f"cli_call_budget.warn_at must be a number in [0.0, 1.0] "
1265
+ f"(got {warn_at_raw!r})."
1266
+ )
1267
+ warn_at = float(warn_at_raw)
1268
+ if not 0.0 <= warn_at <= 1.0:
1269
+ raise CouncilConfigError(
1270
+ f"cli_call_budget.warn_at must be in [0.0, 1.0] (got {warn_at})."
1271
+ )
1272
+ return CliCallBudgetConfig(max_calls_per_day=caps, warn_at=warn_at)
1273
+
1274
+
1275
+ def _build_advisor(name: str, cfg: dict[str, Any]) -> AdvisorConfig:
1276
+ if not isinstance(cfg, dict):
1277
+ raise CouncilConfigError(f"advisors.{name}: must be a mapping.")
1278
+ member = cfg.get("member")
1279
+ if member not in _VALID_PROVIDERS:
1280
+ raise CouncilConfigError(
1281
+ f"advisors.{name}.member={member!r} not a valid provider; "
1282
+ f"valid: {sorted(_VALID_PROVIDERS)}."
1283
+ )
1284
+ # `persona` may be set explicitly; otherwise default to the
1285
+ # convention path so the YAML stays terse.
1286
+ persona = cfg.get("persona") or f"personas/advisors/{name}.md"
1287
+ model = cfg.get("model")
1288
+ if model is not None and not isinstance(model, str):
1289
+ raise CouncilConfigError(
1290
+ f"advisors.{name}.model must be a string when set."
1291
+ )
1292
+ return AdvisorConfig(
1293
+ name=name,
1294
+ enabled=bool(cfg.get("enabled", False)),
1295
+ member=member,
1296
+ persona=persona,
1297
+ model=model,
1298
+ )
1299
+
1300
+
1301
+ def _validate_api_key_ref(scope: str, ref: Any) -> None:
1302
+ if not isinstance(ref, str) or not ref:
1303
+ raise CouncilConfigError(f"{scope}.api_key_ref must be a non-empty string.")
1304
+ if any(ref.startswith(prefix) for prefix in _RAW_KEY_PREFIXES):
1305
+ raise CouncilConfigError(
1306
+ f"{scope}.api_key_ref looks like a raw API key. "
1307
+ f"Use `file:<path>` (0600) or `env:<VAR>` — never inline secrets."
1308
+ )
1309
+ if ref.startswith("file:"):
1310
+ if not ref[len("file:"):].strip():
1311
+ raise CouncilConfigError(f"{scope}.api_key_ref `file:` ref missing path.")
1312
+ return
1313
+ if ref.startswith("env:"):
1314
+ if not ref[len("env:"):].strip():
1315
+ raise CouncilConfigError(f"{scope}.api_key_ref `env:` ref missing variable name.")
1316
+ return
1317
+ raise CouncilConfigError(
1318
+ f"{scope}.api_key_ref must start with `file:` or `env:` (got {ref!r})."
1319
+ )
1320
+
1321
+
1322
+ def resolve_api_key(ref: str, *, scope: str = "api_key_ref") -> str:
1323
+ """Resolve ``file:<path>`` or ``env:<VAR>`` to the raw key string.
1324
+
1325
+ ``file:`` — relative paths resolve under the user-global namespace
1326
+ (``~/.event4u/agent-config/`` today, with the pre-2.4
1327
+ ``~/.config/agent-config/`` tree read as a fallback). Mode must be
1328
+ 0o600. ``env:`` — reads from ``os.environ``; empty/missing is a
1329
+ hard error. Never echoes the value.
1330
+ """
1331
+ _validate_api_key_ref(scope, ref)
1332
+ if ref.startswith("env:"):
1333
+ var = ref[len("env:"):].strip()
1334
+ if not var:
1335
+ raise CouncilConfigError(f"{scope}: `env:` ref missing variable name.")
1336
+ value = os.environ.get(var, "").strip()
1337
+ if not value:
1338
+ raise CouncilConfigError(f"{scope}: env var {var!r} is unset or empty.")
1339
+ return value
1340
+ spec = ref[len("file:"):].strip()
1341
+ if not spec:
1342
+ raise CouncilConfigError(f"{scope}: `file:` ref missing path.")
1343
+ path = Path(spec).expanduser()
1344
+ if not path.is_absolute():
1345
+ found = user_global_paths.resolve_with_fallback(spec)
1346
+ if found is None:
1347
+ target = user_global_paths.write_target(spec)
1348
+ raise CouncilConfigError(
1349
+ f"{scope}: key file not found at {target} (or legacy fallback)."
1350
+ )
1351
+ path = found
1352
+ if not path.exists():
1353
+ raise CouncilConfigError(f"{scope}: key file does not exist at {path}.")
1354
+ mode = stat.S_IMODE(path.stat().st_mode)
1355
+ if mode != 0o600:
1356
+ raise CouncilConfigError(
1357
+ f"{scope}: unsafe permissions on {path}: got {oct(mode)}, expected 0o600. "
1358
+ f"Fix: chmod 600 {path}"
1359
+ )
1360
+ value = path.read_text(encoding="utf-8").strip()
1361
+ if not value:
1362
+ raise CouncilConfigError(f"{scope}: key file at {path} is empty.")
1363
+ return value
1364
+