@yahyahammad/venastine 0.2.0 → 0.3.8

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 (129) hide show
  1. package/.env.example +19 -14
  2. package/CONFIG_ARCHITECTURE.md +2002 -0
  3. package/NOTICE +1 -1
  4. package/README.md +233 -94
  5. package/agents/builtin/build.md +73 -0
  6. package/agents/builtin/general.md +80 -0
  7. package/agents/builtin/plan.md +23 -5
  8. package/agents/builtin/test.md +65 -0
  9. package/agents/builtin/writer.md +67 -0
  10. package/agents/manager.py +261 -195
  11. package/agents/subagent_tool.py +277 -43
  12. package/agents/tui_commands.py +7 -12
  13. package/bin/venastine.mjs +528 -382
  14. package/bin/venastine.py +71 -0
  15. package/cli_commands.py +835 -0
  16. package/config.py +95 -1198
  17. package/config.yaml +1090 -0
  18. package/config_edit.py +1301 -0
  19. package/config_schema.py +966 -0
  20. package/config_update.py +749 -0
  21. package/core/agent_activity.py +158 -20
  22. package/core/ansi.py +452 -0
  23. package/core/approval.py +27 -5
  24. package/core/call_view.py +364 -0
  25. package/core/cancel.py +121 -0
  26. package/core/chatgpt_auth.py +747 -0
  27. package/core/claims_view.py +107 -0
  28. package/core/client.py +1238 -82
  29. package/core/commands.py +454 -0
  30. package/core/compaction.py +830 -66
  31. package/core/config_loader.py +516 -97
  32. package/core/connect.py +686 -0
  33. package/core/events.py +68 -6
  34. package/core/file_mentions.py +280 -0
  35. package/core/interaction.py +87 -4
  36. package/core/line_pattern.py +128 -0
  37. package/core/loop.py +1516 -440
  38. package/core/memory.py +120 -8
  39. package/core/pipeline_models.py +23 -9
  40. package/core/provider_errors.py +234 -0
  41. package/core/reasoning/confidence_scoring.py +70 -54
  42. package/core/reasoning/json_retry.py +19 -1
  43. package/core/reasoning/orchestrator.py +232 -53
  44. package/core/reasoning/output_writer.py +90 -31
  45. package/core/reasoning/payload_validation.py +133 -7
  46. package/core/reasoning/pipeline_storage.py +427 -361
  47. package/core/reasoning/review.py +659 -615
  48. package/core/reasoning/scholar.py +38 -14
  49. package/core/reasoning/source_corpus.py +110 -4
  50. package/core/replay.py +158 -26
  51. package/core/session.py +36 -12
  52. package/core/session_view.py +259 -0
  53. package/core/session_wake.py +259 -0
  54. package/core/shell_sessions.py +1553 -0
  55. package/core/workspace_trust.py +1 -1
  56. package/credentials.py +308 -180
  57. package/database.py +82 -1
  58. package/{PRIVACY.md → docs/PRIVACY.md} +19 -7
  59. package/docs/SECURITY.md +108 -0
  60. package/docs/THIRD_PARTY_NOTICES.md +148 -0
  61. package/env_secrets.py +118 -77
  62. package/json_store.py +236 -16
  63. package/logging_setup.py +121 -11
  64. package/main.py +1671 -199
  65. package/mcp_client/client.py +257 -23
  66. package/mcp_client/config.py +484 -379
  67. package/mcp_client/registration.py +163 -2
  68. package/memories/manager.py +15 -0
  69. package/memories/tui_commands.py +7 -12
  70. package/package.json +234 -4
  71. package/project_init/config_files.py +4 -0
  72. package/project_init/generator.py +201 -37
  73. package/project_init/manifest.py +418 -406
  74. package/project_init/tui_commands.py +7 -4
  75. package/prompts/revalidate.md +1 -1
  76. package/prompts/revise.md +17 -15
  77. package/prompts/system_prompts.py +64 -10
  78. package/providers.json.example +94 -92
  79. package/pyproject.toml +51 -2
  80. package/requirements.txt +252 -29
  81. package/safety/policy_enforcement.py +353 -17
  82. package/security/askpass.py +419 -0
  83. package/security/capability.py +37 -7
  84. package/security/confine.py +484 -0
  85. package/security/hosted.py +182 -0
  86. package/security/permissions.py +54 -7
  87. package/security/posture.py +124 -12
  88. package/security/protected_paths.py +198 -6
  89. package/security/sandbox.py +3262 -139
  90. package/security/secrets.py +855 -0
  91. package/skills/tui_commands.py +3 -4
  92. package/storage.py +368 -35
  93. package/tools/base.py +465 -305
  94. package/tools/builtin/_math_common.py +306 -16
  95. package/tools/builtin/_net_common.py +267 -61
  96. package/tools/builtin/arxiv.py +681 -223
  97. package/tools/builtin/ask_user.py +231 -213
  98. package/tools/builtin/discrete_math.py +35 -4
  99. package/tools/builtin/fetch_url.py +239 -21
  100. package/tools/builtin/file_ops.py +341 -33
  101. package/tools/builtin/geometry.py +184 -148
  102. package/tools/builtin/get_time.py +2 -2
  103. package/tools/builtin/linear_algebra.py +66 -23
  104. package/tools/builtin/load_skill.py +10 -0
  105. package/tools/builtin/logic.py +171 -12
  106. package/tools/builtin/pin.py +46 -12
  107. package/tools/builtin/probability_stats.py +224 -228
  108. package/tools/builtin/project_docs.py +80 -21
  109. package/tools/builtin/remember.py +21 -4
  110. package/tools/builtin/shell.py +586 -67
  111. package/tools/builtin/shell_sessions.py +613 -0
  112. package/tools/builtin/symbolic_math.py +80 -10
  113. package/tools/builtin/todo.py +5 -3
  114. package/tools/builtin/web_search.py +329 -151
  115. package/tools/context.py +23 -0
  116. package/tools/isolation.py +387 -314
  117. package/tools/registry.py +347 -30
  118. package/tui/app.py +4645 -438
  119. package/tui/app.tcss +185 -2
  120. package/tui/commands.py +10 -154
  121. package/tui/diffs.py +286 -265
  122. package/tui/hosted_driver.py +102 -0
  123. package/tui/markdown.py +60 -10
  124. package/tui/meters.py +103 -7
  125. package/tui/screens.py +346 -92
  126. package/tui/themes.py +59 -19
  127. package/tui/widgets.py +1575 -111
  128. package/SECURITY.md +0 -88
  129. package/THIRD_PARTY_NOTICES.md +0 -100
@@ -6,8 +6,9 @@ for factual claims) -> 3c -> 4 (all claims) -> Pass 5 (code) -> D1 -> the
6
6
  6a/6b/D2 retry loop -> 6c (template) -> merge -> final synthesis.
7
7
 
8
8
  SCOPE OF THIS VERSION: core sequential pipeline with optional ensemble
9
- mode (ROADMAP §10: N-candidate Pass 1 generation + cross-candidate
10
- consistency check feeding Pass 5's scoring). No filesystem output yet
9
+ mode (ROADMAP §10: N-candidate Pass 1 generation + a cross-candidate
10
+ consistency check, whose contested claims D1 routes to the 6a/6b loop,
11
+ ROADMAP_v3 §90). No filesystem output yet
11
12
  (returns a PipelineRun object; the /output/<run_id>/ file-writing system
12
13
  is a separate, later piece of work).
13
14
 
@@ -79,7 +80,7 @@ from core import pipeline_models
79
80
  from core.config_loader import effective_confidence, effective_source_scoring
80
81
  from core.loop import RunAgentLoop, advertisement_facts
81
82
  from core.reasoning.base import Claim, PipelineRun, resolve_by_id
82
- from core.reasoning.confidence_scoring import run_confidence_tiering
83
+ from core.reasoning.confidence_scoring import is_contested, run_confidence_tiering
83
84
  from core.reasoning.events import PipelineEvent
84
85
  from core.reasoning.json_retry import parse_json_response as _parse_json_response
85
86
  from core.reasoning.json_retry import retry_until_json
@@ -88,7 +89,7 @@ from core.reasoning.pipeline_storage import create_pipeline_run, update_pipeline
88
89
  from core.reasoning.scholar import ScholarLookup
89
90
  from core.reasoning.source_corpus import SourceCorpus
90
91
  from core.reasoning.source_scoring import make_scorer, score_grounding_sources
91
- from safety.policy_enforcement import redact_secrets
92
+ from safety.policy_enforcement import redact_diagnostic_text
92
93
  from tools.registry import registry
93
94
 
94
95
  logger = logging.getLogger(__name__)
@@ -163,7 +164,7 @@ class _Progress:
163
164
  _ACTIVITY_CHARS = 2000
164
165
 
165
166
  # §26's _param_digest, and its two truncation constants, MOVED to
166
- # safety/policy_enforcement.py in §27 -- beside redact_secrets, because
167
+ # safety/policy_enforcement.py in §27 -- beside the redactors, because
167
168
  # §27's replay renderer is its second caller and the redact-before-truncate
168
169
  # ordering must not exist twice. Imported under its own name rather than
169
170
  # aliased back to the old private one: an alias would leave two spellings
@@ -208,6 +209,17 @@ def _translate(pass_id: str, stream, corpus=None):
208
209
  anything shows no activity, which is the
209
210
  shape §26 built pass_activity to cure. Flip
210
211
  it here if that turns up in a real run.
212
+ retract batch 113. A retraction tells a surface
213
+ that DREW model text to take it back, and
214
+ this one never drew any -- `chars` is the
215
+ only thing that escaped. Left monotonic
216
+ deliberately: the total measures what the
217
+ pass produced, it is the panel's liveness
218
+ figure rather than a bill, and a running
219
+ total that jumped backwards mid-pass
220
+ would read as the defect instead of the
221
+ honesty. The TUI's own output-char meter
222
+ is left alone for the same reason.
211
223
  permission_request §25's ApprovalProvider already carries this
212
224
  to the shell and blocks on the answer. A
213
225
  second rendering of a question already on
@@ -287,7 +299,8 @@ def _translate(pass_id: str, stream, corpus=None):
287
299
  # boundary -- "nothing leaves this function unredacted" --
288
300
  # beats depending on a guarantee made two layers away that
289
301
  # a later refactor could quietly drop.
290
- text=redact_secrets(str(result.get("error"))) if failed else None)
302
+ text=(redact_diagnostic_text(str(result.get("error")))
303
+ if failed else None))
291
304
 
292
305
 
293
306
  def _check_not_truncated(pass_id: str, response, trace: list[str] | None):
@@ -366,10 +379,10 @@ def _ensemble_roster(ensemble_mode: bool) -> list[dict]:
366
379
  roster = config.ENSEMBLE_MODELS
367
380
  if not roster:
368
381
  raise ValueError(
369
- "Ensemble mode is on but config.ENSEMBLE_MODELS is empty. "
382
+ "Ensemble mode is on but ensemble_models in config.yaml is empty. "
370
383
  "Diversity comes from different models now, not from a raised "
371
384
  "temperature (ROADMAP §10 revisit), so the roster is required. "
372
- "Set it in config.py to a list of at least two distinct "
385
+ "Set it in config.yaml to a list of at least two distinct "
373
386
  '{"provider_name": ..., "model": ...} entries, or disable '
374
387
  "ensemble_mode. There is deliberately no settings.json key for "
375
388
  "the roster -- see E2."
@@ -378,7 +391,7 @@ def _ensemble_roster(ensemble_mode: bool) -> list[dict]:
378
391
  for entry in roster:
379
392
  if not isinstance(entry, dict) or not entry.get("provider_name") or not entry.get("model"):
380
393
  raise ValueError(
381
- f"Malformed config.ENSEMBLE_MODELS entry {entry!r}: every "
394
+ f"Malformed ensemble_models entry in config.yaml {entry!r}: every "
382
395
  f'entry needs both "provider_name" and "model".'
383
396
  )
384
397
 
@@ -392,7 +405,7 @@ def _ensemble_roster(ensemble_mode: bool) -> list[dict]:
392
405
  if len(distinct) < 2:
393
406
  raise ValueError(
394
407
  f"Ensemble mode needs at least two DISTINCT models; "
395
- f"config.ENSEMBLE_MODELS resolves to {len(distinct)} "
408
+ f"ensemble_models in config.yaml resolves to {len(distinct)} "
396
409
  f"({sorted(distinct)}). One model cannot disagree with itself, so "
397
410
  f"every claim would score maximal consistency and Pass 5 would "
398
411
  f"read that as confidence."
@@ -418,7 +431,7 @@ def _pass_failed_event(pass_id: str, exc: BaseException) -> PipelineEvent:
418
431
  def _run_pass(pass_id: str, pass_input: str, model: str, provider_name: str,
419
432
  temperature: float | None = None, authorization=None,
420
433
  trace: list[str] | None = None, pass_threads: list | None = None,
421
- effort: str | None = None, corpus=None):
434
+ effort: str | None = None, corpus=None, activity=None):
422
435
  """One LLM-backed pass. Every actual model call in this file goes
423
436
  through this single function.
424
437
 
@@ -441,6 +454,7 @@ def _run_pass(pass_id: str, pass_input: str, model: str, provider_name: str,
441
454
  response = yield from _translate(pass_id, RunAgentLoop.stream_deep_research_mode(
442
455
  pass_input=pass_input, model=model, pass_id=pass_id, provider_name=provider_name,
443
456
  temperature=temperature, authorization=authorization, effort=effort,
457
+ activity=activity,
444
458
  ), corpus=corpus)
445
459
  _record_granted_calls(pass_id, response, authorization)
446
460
  _record_pass_thread(pass_id, response, pass_threads)
@@ -514,6 +528,7 @@ def _run_pass_with_json_retry(
514
528
  ensemble: bool = False,
515
529
  effort: str | None = None,
516
530
  corpus=None,
531
+ activity=None,
517
532
  ):
518
533
  """Runs one JSON-emitting pass and recovers from malformed JSON.
519
534
 
@@ -542,8 +557,9 @@ def _run_pass_with_json_retry(
542
557
  reviewer shares retry_until_json's contract; re-parsing a short string
543
558
  is cheaper than moving twenty seams in the batch that adds the check.
544
559
 
545
- `claim_ids` is the set the APPLY step will resolve against -- every
546
- claim for 3a/3b/5/6c, the currently-flagged batch for 6a -- because
560
+ `claim_ids` is the set the APPLY step will resolve against -- the
561
+ factual claims for 3a/3b (batch 220), every claim for 4, the
562
+ currently-flagged batch for 6a/6b -- because
547
563
  validating against a wider set would accept an entry that then lands
548
564
  on nothing.
549
565
 
@@ -575,6 +591,7 @@ def _run_pass_with_json_retry(
575
591
  response = yield from _translate(pass_id, RunAgentLoop.stream_deep_research_mode(
576
592
  pass_input=pass_input, model=model, pass_id=pass_id, provider_name=provider_name,
577
593
  temperature=temperature, authorization=authorization, effort=effort,
594
+ activity=activity,
578
595
  ), corpus=corpus)
579
596
  _record_granted_calls(pass_id, response, authorization)
580
597
  # §27: the retries re-enter THIS thread (continue_conversation), so one
@@ -605,6 +622,12 @@ def _run_pass_with_json_retry(
605
622
  authorization=authorization,
606
623
  effort=effort,
607
624
  on_response=lambda r: _record_granted_calls(pass_id, r, authorization),
625
+ # §47's sink, at the hop batch 73 missed (batch 75). A
626
+ # corrective attempt reaches the model through
627
+ # continue_conversation, which drains its own loop, so a
628
+ # subagent spawned while a pass is being corrected is visible
629
+ # through this channel or not at all.
630
+ activity=activity,
608
631
  # The retry re-enters THIS pass's thread. No separate pass
609
632
  # ceiling any more (#4): omitting the kwarg lets the wrapper
610
633
  # resolve the configured spend cap, exactly as the pass itself
@@ -662,7 +685,8 @@ def _apply_grounding(claims: list[Claim], grounding_entries: list[dict]) -> int:
662
685
 
663
686
 
664
687
  def _ground_and_score(run: PipelineRun, grounding_entries: list[dict], corpus,
665
- scorer=None, knobs=None, scholar=None) -> int:
688
+ scorer=None, knobs=None, scholar=None,
689
+ claims=None) -> int:
666
690
  """Apply Pass 3a/6b's grounding, then everything §45 derives from it.
667
691
 
668
692
  THE ONE SEAM BOTH GROUNDING SITES GO THROUGH. There are two -- Pass 3a
@@ -682,13 +706,33 @@ def _ground_and_score(run: PipelineRun, grounding_entries: list[dict], corpus,
682
706
  enough that a run dying in a later pass still carries the evidence it
683
707
  had already gathered -- the failure path being where an unattended
684
708
  run's record matters most.
709
+
710
+ `claims` NARROWS BOTH HALVES, and defaults to the whole run. Pass 3a
711
+ passes the factual claims since batch 220 (#247), the only ones it is
712
+ shown: the default let an entry naming a speculative claim mark it
713
+ grounded. §66 (A26) passes the 6a/6b loop's current batch, and it has to
714
+ reach the SCORING as well as the apply: `score_grounding_sources`
715
+ "rewrite[s] EVERY claim's grounding_sources with computed scores" by
716
+ default, so a 6b round recomputed the sources of claims D1 had already
717
+ finished with, while the re-tiering beside it was deliberately scoped
718
+ to the batch. Driven with the corpus grown by that round -- which is
719
+ what a grounding round does -- a claim the payload never named had its
720
+ `quote_verified` rewritten from None to True. That one moves no score
721
+ by design (§45 slice A), but the embedding and scholar phases run over
722
+ the same set and do move one, and the claim they would move is not
723
+ re-tiered.
724
+
725
+ A PARAMETER THAT EXISTED AND WAS NEVER PASSED. `score_grounding_sources`
726
+ has had `claims` since §45; the one production caller omitted it, so
727
+ the narrowing it was built for was available and unused.
685
728
  """
686
- applied = _apply_grounding(run.claims, grounding_entries)
729
+ targets = run.claims if claims is None else claims
730
+ applied = _apply_grounding(targets, grounding_entries)
687
731
  if corpus is not None:
688
732
  run.source_documents = corpus.artifact_entries()
689
733
  knobs = knobs or {}
690
734
  score_grounding_sources(
691
- run, corpus=corpus, scorer=scorer, scholar=scholar,
735
+ run, claims=claims, corpus=corpus, scorer=scorer, scholar=scholar,
692
736
  overrides=knobs.get("domain_overrides"),
693
737
  **{k: knobs[k] for k in ("window_chars", "max_windows",
694
738
  "claim_min_chars",
@@ -724,6 +768,50 @@ def _apply_critic(claims: list[Claim], critic_entries: list[dict]) -> int:
724
768
  return len(applied)
725
769
 
726
770
 
771
+ def _note_entries_outside_the_batch(run: PipelineRun, batch: list[Claim],
772
+ *entry_lists, pass_id: str, scope: str,
773
+ asked: str, why: str) -> int:
774
+ """Say that a pass answered about a claim it was not asked about.
775
+
776
+ §66 (A26). The apply is scoped to the batch, so such an entry lands on
777
+ nothing -- and `review.py` settled the policy for exactly this shape:
778
+ a finding it cannot apply is DROPPED AND TRACED, because dropping in
779
+ silence is indistinguishable from applying. 6b's own checkpoint line
780
+ reports how many claims came out clean, which is an effect and
781
+ therefore cannot report this.
782
+
783
+ FOUR PASSES SINCE BATCH 220. 6a made the same
784
+ drop twenty lines above 6b and said nothing (#248), and 3a and 3b,
785
+ shown only the factual claims, applied to every claim (#247): a 3a
786
+ entry could mark a speculative claim grounded, which gate D0 says cannot
787
+ happen, and a 3b severity on one moved its tier -- measured, MEDIUM to
788
+ LOW at 0.5 and to UNVERIFIED at 0.8, which then cost it 6a/6b rounds.
789
+ Each caller says its own `scope`, what it `asked` about and `why` an
790
+ answer outside it must not land.
791
+
792
+ The id cross-check does not cover it and is not meant to: B5 makes a
793
+ TOTAL mismatch the error and a partial one deliberately not, since a
794
+ model that gets nine of ten right must not cost a run nine passes.
795
+ Measured -- a payload naming the batch PLUS one extra id passes
796
+ `claim_ids=[c.id for c in flagged]` unchanged, which is why the apply
797
+ is where this had to be fixed and the trace is what makes the drop
798
+ visible.
799
+ """
800
+ known = {c.id for c in batch}
801
+ outside = sorted({entry.get("claim_id") for entries in entry_lists
802
+ for entry in entries if isinstance(entry, dict)}
803
+ - known - {None})
804
+ if outside:
805
+ run.log(
806
+ f"{pass_id}: {len(outside)} claim id(s) in the response name a "
807
+ f"claim outside {scope} "
808
+ f"({', '.join(repr(i) for i in outside[:3])}) -- dropped. "
809
+ f"{pass_id.removeprefix('Pass ')} was asked about {len(known)} "
810
+ f"{asked}; {why}"
811
+ )
812
+ return len(outside)
813
+
814
+
727
815
  def _apply_assumption_flags(claims: list[Claim], assumptions: dict) -> int:
728
816
  by_id = resolve_by_id(claims)
729
817
  applied = set()
@@ -760,7 +848,8 @@ def _synthesis_input(run: PipelineRun, directives: list | None = None) -> str:
760
848
 
761
849
  def _review_stage(run: PipelineRun, progress: _Progress, model: str,
762
850
  provider_name: str, authorization, review,
763
- enabled: bool = False, effort: str | None = None):
851
+ enabled: bool = False, effort: str | None = None,
852
+ activity=None):
764
853
  """ROADMAP_v2 §20. Opt-in review of the finished run, with every
765
854
  correction consented to individually.
766
855
 
@@ -796,7 +885,8 @@ def _review_stage(run: PipelineRun, progress: _Progress, model: str,
796
885
  from core.reasoning import review as review_module
797
886
 
798
887
  findings, thread_id = review_module.run_review(
799
- run, model, provider_name, authorization, effort=effort)
888
+ run, model, provider_name, authorization, effort=effort,
889
+ activity=activity)
800
890
  run.log(f"Review: {len(findings)} finding(s) raised.")
801
891
  # The findings' CONTENT is durable from this checkpoint on (review
802
892
  # r1-2): a kill during the consent walk used to leave only the count,
@@ -811,7 +901,10 @@ def _review_stage(run: PipelineRun, progress: _Progress, model: str,
811
901
 
812
902
  decisions = review_module.walk_consent(
813
903
  findings, review, run, model=model, provider_name=provider_name,
814
- thread_id=thread_id, authorization=authorization, effort=effort)
904
+ thread_id=thread_id, authorization=authorization, effort=effort,
905
+ # §47's sink, for the refinement calls the walk can make. Every
906
+ # hop is a place it can be dropped, and this one was.
907
+ activity=activity)
815
908
  run.subagent_reviews = decisions
816
909
  yield from progress.checkpoint()
817
910
 
@@ -831,6 +924,7 @@ def _review_stage(run: PipelineRun, progress: _Progress, model: str,
831
924
  trace=run.trace,
832
925
  pass_threads=run.pass_threads,
833
926
  effort=effort,
927
+ activity=activity,
834
928
  )
835
929
  except Exception as e:
836
930
  # CONTAINED, not re-raised. A provider error on this one call
@@ -911,6 +1005,28 @@ def _apply_fallback(claim: Claim) -> None:
911
1005
  claim.final_text = claim.revision_text or claim.text
912
1006
 
913
1007
 
1008
+ def _revision_request(claim: Claim) -> dict:
1009
+ """One claim's entry in Pass 6a's batch: the claim and the feedback
1010
+ that put it there.
1011
+
1012
+ `disagreement_feedback` only on a contested claim (ROADMAP_v3 §90),
1013
+ because D1 may have flagged it for that alone, and 6a cannot address a
1014
+ reason it is not given. `revise.md` says what to do with one."""
1015
+ entry = {
1016
+ "claim": vars(claim),
1017
+ "critic_feedback": {"fallacies": claim.fallacies, "contradictions": claim.contradictions},
1018
+ "grounding_feedback": claim.grounding_sources,
1019
+ "assumption_flags": claim.assumption_flags,
1020
+ }
1021
+ if is_contested(claim):
1022
+ entry["disagreement_feedback"] = {
1023
+ # Deduplicated, as the consistency score it came from is (B8).
1024
+ "asserted_by": len(set(claim.asserted_by_candidates)),
1025
+ "of": claim.score_breakdown["consistency_denominator"],
1026
+ }
1027
+ return entry
1028
+
1029
+
914
1030
  # ============================================================================
915
1031
  # ---- The pipeline ------------------------------------------------------
916
1032
  # ============================================================================
@@ -945,6 +1061,11 @@ def stream_deep_research_pipeline(
945
1061
  review=None,
946
1062
  subagent_review: bool | None = None,
947
1063
  effort: str | None = None,
1064
+ # §47. Threaded like `authorization` beside it, and for the same
1065
+ # reason: both are run-scoped values the layers between here and
1066
+ # the model call only forward. Batch 59 stopped at
1067
+ # stream_deep_research_mode's signature; this is the rest of it.
1068
+ activity=None,
948
1069
  ):
949
1070
  """The pipeline, as a generator of PipelineEvents (§22). The terminal
950
1071
  `run_complete` event carries the finished PipelineRun.
@@ -991,8 +1112,8 @@ def stream_deep_research_pipeline(
991
1112
  if ensemble_n is not None:
992
1113
  logger.warning(
993
1114
  "Ignoring ensemble_n=%s: the number of candidates is "
994
- "len(config.ENSEMBLE_MODELS) now (ROADMAP §10 revisit, E3). "
995
- "Change the roster in config.py to change N.",
1115
+ "len(ensemble_models) now (ROADMAP §10 revisit, E3). "
1116
+ "Change the roster in config.yaml to change N.",
996
1117
  ensemble_n,
997
1118
  )
998
1119
 
@@ -1065,7 +1186,7 @@ def stream_deep_research_pipeline(
1065
1186
 
1066
1187
  try:
1067
1188
  # --- Pass 0: preliminary plan ---
1068
- run.plan = _parse_json_response((yield from _run_pass_with_json_retry("Pass 0", user_query, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, effort=effort)))
1189
+ run.plan = _parse_json_response((yield from _run_pass_with_json_retry("Pass 0", user_query, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, effort=effort, activity=activity)))
1069
1190
  yield from progress.checkpoint(f"Pass 0: plan produced ({len(run.plan.get('key_entities_or_subjects', []))} key entities anticipated).")
1070
1191
 
1071
1192
  # --- Pass 1: initial generation (ensemble mode: N candidates) ---
@@ -1088,7 +1209,8 @@ def stream_deep_research_pipeline(
1088
1209
  candidates.append((yield from _run_pass(
1089
1210
  "Pass 1", pass1_input, entry["model"], entry["provider_name"],
1090
1211
  authorization=authorization, trace=run.trace,
1091
- pass_threads=run.pass_threads, effort=effort)))
1212
+ pass_threads=run.pass_threads, effort=effort,
1213
+ activity=activity)))
1092
1214
  except Exception as e:
1093
1215
  # NAMED, TRACED, SKIPPED (E7) -- §20's containment rule.
1094
1216
  # An optional generation strategy must not flip a
@@ -1135,8 +1257,9 @@ def stream_deep_research_pipeline(
1135
1257
  else:
1136
1258
  # DEGRADE TO THE SINGLE-CANDIDATE PATH (E7), rather than
1137
1259
  # scoring one candidate as an ensemble of one -- which would
1138
- # give every claim consistency 1.0 and a zero penalty while
1139
- # reporting that agreement as if it had been corroborated.
1260
+ # give every claim consistency 1.0, so none could ever be
1261
+ # contested, while reporting that agreement as if it had
1262
+ # been corroborated.
1140
1263
  # ensemble_n=0 is not a degraded formula, it is the correct
1141
1264
  # one when there is nothing to compare against.
1142
1265
  effective_n = 0
@@ -1144,8 +1267,8 @@ def stream_deep_research_pipeline(
1144
1267
  run.raw_response = candidates[0]
1145
1268
  pass2_input = f"Response to extract claims from:\n{run.raw_response}"
1146
1269
  run.log("Pass 1: only one ensemble candidate survived — "
1147
- "scoring WITHOUT a disagreement penalty, since one "
1148
- "candidate cannot disagree with itself.")
1270
+ "no claim can be contested, since one candidate "
1271
+ "cannot disagree with itself.")
1149
1272
  else:
1150
1273
  raise RuntimeError(
1151
1274
  "Pass 1: every ensemble candidate failed; there is no "
@@ -1153,13 +1276,13 @@ def stream_deep_research_pipeline(
1153
1276
  "each provider's error."
1154
1277
  )
1155
1278
  else:
1156
- run.raw_response = yield from _run_pass("Pass 1", pass1_input, model, provider_name, authorization=authorization, corpus=corpus, trace=run.trace, pass_threads=run.pass_threads, effort=effort)
1279
+ run.raw_response = yield from _run_pass("Pass 1", pass1_input, model, provider_name, authorization=authorization, corpus=corpus, trace=run.trace, pass_threads=run.pass_threads, effort=effort, activity=activity)
1157
1280
  pass2_input = f"Response to extract claims from:\n{run.raw_response}"
1158
1281
  run.log("Pass 1: initial generation complete.")
1159
1282
  yield from progress.checkpoint()
1160
1283
 
1161
1284
  # --- Pass 2: claim extraction & classification ---
1162
- claims_json = _parse_json_response((yield from _run_pass_with_json_retry("Pass 2", pass2_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, ensemble=effective_n >= 2, effort=effort)))
1285
+ claims_json = _parse_json_response((yield from _run_pass_with_json_retry("Pass 2", pass2_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, ensemble=effective_n >= 2, effort=effort, activity=activity)))
1163
1286
  run.claims = [_claim_from_json(c) for c in claims_json]
1164
1287
  for claim in run.claims:
1165
1288
  yield PipelineEvent(kind="claim_extracted", claim_id=claim.id,
@@ -1183,10 +1306,20 @@ def stream_deep_research_pipeline(
1183
1306
  f"Deduplicated entities to research (search each ONCE, map results back "
1184
1307
  f"to every claim referencing it):\n{json.dumps(unique_entities)}"
1185
1308
  )
1186
- grounding_json = _parse_json_response((yield from _run_pass_with_json_retry("Pass 3a", pass3a_input, critic_model, critic_provider, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in run.claims], effort=effort)))
1309
+ # SCOPED TO `factual_claims`, like 6b to its batch (batch 220,
1310
+ # #247): 3a is shown the factual claims and nothing else, so an
1311
+ # entry naming another claim is dropped and traced rather than
1312
+ # marking a claim grounded that gate D0 routed past grounding.
1313
+ grounding_json = _parse_json_response((yield from _run_pass_with_json_retry("Pass 3a", pass3a_input, critic_model, critic_provider, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in factual_claims], effort=effort, activity=activity)))
1187
1314
  grounded = _ground_and_score(run, grounding_json, corpus, scorer,
1188
- scoring_knobs, scholar)
1315
+ scoring_knobs, scholar,
1316
+ claims=factual_claims)
1189
1317
  yield from progress.checkpoint(f"Pass 3a: grounded {grounded} of {len(factual_claims)} factual claim(s), across {len(unique_entities)} deduplicated entities.")
1318
+ _note_entries_outside_the_batch(
1319
+ run, factual_claims, grounding_json, pass_id="Pass 3a",
1320
+ scope="the factual claims", asked="factual claim(s)",
1321
+ why="Gate D0 routes every other claim past 3a/3b, so grounding "
1322
+ "one would record a check gate D0 says never ran.")
1190
1323
 
1191
1324
  # E13/#77. Two or more survivors: the claims were extracted
1192
1325
  # from the UNION of the candidates, so the critic sees every
@@ -1208,9 +1341,18 @@ def stream_deep_research_pipeline(
1208
1341
  f"Raw response:\n{run.raw_response}\n\n"
1209
1342
  f"Factual claims with grounding:\n{json.dumps([vars(c) for c in factual_claims])}"
1210
1343
  )
1211
- critic_json = _parse_json_response((yield from _run_pass_with_json_retry("Pass 3b", pass3b_input, critic_model, critic_provider, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in run.claims], effort=effort)))
1212
- critiqued = _apply_critic(run.claims, critic_json)
1344
+ critic_json = _parse_json_response((yield from _run_pass_with_json_retry("Pass 3b", pass3b_input, critic_model, critic_provider, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in factual_claims], effort=effort, activity=activity)))
1345
+ # The same scope as 3a, for a stronger reason: a non-factual
1346
+ # claim's score IS its capped critic component, so a severity
1347
+ # landing on one moved its tier (measured, batch 220).
1348
+ critiqued = _apply_critic(factual_claims, critic_json)
1213
1349
  yield from progress.checkpoint(f"Pass 3b: critique applied to {critiqued} of {len(factual_claims)} factual claim(s).")
1350
+ _note_entries_outside_the_batch(
1351
+ run, factual_claims, critic_json, pass_id="Pass 3b",
1352
+ scope="the factual claims", asked="factual claim(s)",
1353
+ why="Gate D0 routes every other claim past 3a/3b, and a "
1354
+ "severity on one would lower the score Pass 5 gives "
1355
+ "it.")
1214
1356
  else:
1215
1357
  # A stage, not a silence. "3a/3b did not run" is a fact about
1216
1358
  # the run's shape -- it means nothing was factual -- and a
@@ -1221,7 +1363,7 @@ def stream_deep_research_pipeline(
1221
1363
 
1222
1364
  # --- Pass 3c: completeness, independent of Pass 1's raw_response ---
1223
1365
  pass3c_input = f"Original query:\n{user_query}\n\nPreliminary plan:\n{json.dumps(run.plan)}"
1224
- run.completeness = _parse_json_response((yield from _run_pass_with_json_retry("Pass 3c", pass3c_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, effort=effort)))
1366
+ run.completeness = _parse_json_response((yield from _run_pass_with_json_retry("Pass 3c", pass3c_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, effort=effort, activity=activity)))
1225
1367
  yield from progress.checkpoint(
1226
1368
  f"Pass 3c: coverage_score={run.completeness.get('coverage_score')}, "
1227
1369
  f"{len(run.completeness.get('gaps', []))} gap(s) identified."
@@ -1233,7 +1375,7 @@ def stream_deep_research_pipeline(
1233
1375
  f"All claims:\n{json.dumps([vars(c) for c in run.claims])}\n\n"
1234
1376
  f"Completeness findings:\n{json.dumps(run.completeness)}"
1235
1377
  )
1236
- run.assumptions = _parse_json_response((yield from _run_pass_with_json_retry("Pass 4", pass4_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in run.claims], effort=effort)))
1378
+ run.assumptions = _parse_json_response((yield from _run_pass_with_json_retry("Pass 4", pass4_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in run.claims], effort=effort, activity=activity)))
1237
1379
  flagged_count = _apply_assumption_flags(run.claims, run.assumptions)
1238
1380
  yield from progress.checkpoint(f"Pass 4: assumption audit complete, {flagged_count} of {len(run.claims)} claim(s) flagged.")
1239
1381
 
@@ -1246,22 +1388,27 @@ def stream_deep_research_pipeline(
1246
1388
  yield from progress.checkpoint("Pass 5: confidence tiers assigned (pure code, zero LLM calls).")
1247
1389
 
1248
1390
  # --- D1 + the 6a/6b/D2 retry loop ---
1249
- flagged = [c for c in run.claims if c.confidence_tier in FLAGGED_TIERS]
1391
+ # ROADMAP_v3 §90 (A85). D1 also takes the claims most ensemble
1392
+ # candidates did not assert, whatever they scored: disagreement is
1393
+ # a reason to check a claim, and the loop is where claims are
1394
+ # checked. ENTRY ONLY -- `still_flagged` below reads the tier alone.
1395
+ # Who asserted a claim does not change when 6a rewrites it, so a
1396
+ # loop that kept a contested claim for being contested would hold
1397
+ # it to D2's cap and publish it UNVERIFIED, which is the defect the
1398
+ # E8 penalty had.
1399
+ flagged = [c for c in run.claims
1400
+ if c.confidence_tier in FLAGGED_TIERS or is_contested(c)]
1401
+ contested = sum(1 for c in flagged if is_contested(c))
1250
1402
  yield from _stage("D1")
1251
- yield from progress.checkpoint(f"D1: {len(flagged)} claim(s) flagged for revision, {len(run.claims) - len(flagged)} already clean.")
1403
+ yield from progress.checkpoint(
1404
+ f"D1: {len(flagged)} claim(s) flagged for revision"
1405
+ + (f" ({contested} contested by the ensemble)" if contested else "")
1406
+ + f", {len(run.claims) - len(flagged)} already clean.")
1252
1407
 
1253
1408
  while flagged:
1254
1409
  # --- Pass 6a: ONE batched call across every currently-flagged claim ---
1255
- pass6a_input = json.dumps([
1256
- {
1257
- "claim": vars(c),
1258
- "critic_feedback": {"fallacies": c.fallacies, "contradictions": c.contradictions},
1259
- "grounding_feedback": c.grounding_sources,
1260
- "assumption_flags": c.assumption_flags,
1261
- }
1262
- for c in flagged
1263
- ])
1264
- revisions = _parse_json_response((yield from _run_pass_with_json_retry("Pass 6a", pass6a_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in flagged], effort=effort)))
1410
+ pass6a_input = json.dumps([_revision_request(c) for c in flagged])
1411
+ revisions = _parse_json_response((yield from _run_pass_with_json_retry("Pass 6a", pass6a_input, model, provider_name, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in flagged], effort=effort, activity=activity)))
1265
1412
 
1266
1413
  # COUNT THE ROUND, NOT THE REVISION. The increment used to live
1267
1414
  # inside the loop below, so only a claim Pass 6a NAMED counted --
@@ -1299,13 +1446,44 @@ def stream_deep_research_pipeline(
1299
1446
  yield PipelineEvent(kind="retry", claim_id=claim.id,
1300
1447
  attempt=claim.retry_count)
1301
1448
  yield from progress.checkpoint(f"Pass 6a: revised {revised} of {len(flagged)} claim(s) in this retry round.")
1449
+ # The count above shows that something was dropped, not why
1450
+ # (#248). The comment on the loop bound is about the BOUND,
1451
+ # which the round counter settled; a reader of the trace still
1452
+ # needs to tell an omission from a misdirected revision.
1453
+ _note_entries_outside_the_batch(
1454
+ run, flagged, revisions, pass_id="Pass 6a",
1455
+ scope="this round's batch", asked="flagged claim(s)",
1456
+ why="a revision to a claim D1 had already finished with "
1457
+ "would replace its text without re-validating it.")
1302
1458
 
1303
1459
  # --- Pass 6b: re-validate the revised subset only (batched, reuses Pass 5's code) ---
1304
1460
  pass6b_input = json.dumps([vars(c) for c in flagged])
1305
- revalidation = _parse_json_response((yield from _run_pass_with_json_retry("Pass 6b", pass6b_input, critic_model, critic_provider, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in run.claims], effort=effort)))
1306
- _ground_and_score(run, revalidation.get("grounding", []), corpus,
1307
- scorer, scoring_knobs, scholar)
1308
- _apply_critic(run.claims, revalidation.get("critic", []))
1461
+ # SCOPED TO `flagged`, ALL THREE OF THEM -- the ids validated
1462
+ # against, the claims the entries are applied to, and the
1463
+ # claims whose sources are rescored (§66 A26). 6a was already
1464
+ # resolved within the batch, with the reason written beside it
1465
+ # ("a revision naming a claim outside it would land on one
1466
+ # already finished"); 6b validated against every id in the run
1467
+ # and applied to every claim in it, while the re-tiering below
1468
+ # stayed narrow. So an entry naming a clean claim changed its
1469
+ # grounding and left its tier alone: driven, a HIGH claim
1470
+ # ended up `grounding_status='ungrounded'` with `sources=[]`
1471
+ # beside a breakdown still recording `source_count: 1` -- which
1472
+ # breaks B10's property that the breakdown alone lets a reader
1473
+ # recompute the score.
1474
+ revalidation = _parse_json_response((yield from _run_pass_with_json_retry("Pass 6b", pass6b_input, critic_model, critic_provider, run.trace, authorization=authorization, corpus=corpus, pass_threads=run.pass_threads, claim_ids=[c.id for c in flagged], effort=effort, activity=activity)))
1475
+ grounding_entries = revalidation.get("grounding", [])
1476
+ critic_entries = revalidation.get("critic", [])
1477
+ _ground_and_score(run, grounding_entries, corpus,
1478
+ scorer, scoring_knobs, scholar, claims=flagged)
1479
+ _apply_critic(flagged, critic_entries)
1480
+ _note_entries_outside_the_batch(
1481
+ run, flagged, grounding_entries, critic_entries,
1482
+ pass_id="Pass 6b", scope="this round's batch",
1483
+ asked="flagged claim(s)",
1484
+ why="applying an answer to a claim D1 had already finished "
1485
+ "with would change its grounding without recomputing "
1486
+ "its tier.")
1309
1487
  # 6b's own checkpoint below already reports an EFFECT -- how
1310
1488
  # many claims came out clean -- rather than what it was asked
1311
1489
  # about, so it needs no count from these two.
@@ -1352,13 +1530,14 @@ def stream_deep_research_pipeline(
1352
1530
  yield from progress.checkpoint(f"Merge: final claim set assembled -- {len(run.claims)} claim(s), {len(run.coverage_gaps)} coverage gap(s).")
1353
1531
 
1354
1532
  # --- Final synthesis ---
1355
- run.final_report = yield from _run_pass("Final synthesis", _synthesis_input(run), model, provider_name, authorization=authorization, corpus=corpus, trace=run.trace, pass_threads=run.pass_threads, effort=effort)
1533
+ run.final_report = yield from _run_pass("Final synthesis", _synthesis_input(run), model, provider_name, authorization=authorization, corpus=corpus, trace=run.trace, pass_threads=run.pass_threads, effort=effort, activity=activity)
1356
1534
  yield from progress.checkpoint("Final synthesis complete.")
1357
1535
 
1358
1536
  # --- §20: review, consent, correct ---
1359
1537
  yield from _review_stage(run, progress, model, provider_name,
1360
1538
  authorization, review,
1361
- enabled=subagent_review, effort=effort)
1539
+ enabled=subagent_review, effort=effort,
1540
+ activity=activity)
1362
1541
 
1363
1542
  update_pipeline_run(run.run_id, run, status="complete")
1364
1543
  yield PipelineEvent(kind="run_complete", run=run)