@yahyahammad/venastine 0.1.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 (130) hide show
  1. package/.env.example +19 -14
  2. package/CONFIG_ARCHITECTURE.md +2002 -0
  3. package/NOTICE +1 -1
  4. package/README.md +249 -93
  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 +280 -23
  12. package/agents/tui_commands.py +31 -21
  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 +256 -0
  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 +1249 -82
  29. package/core/commands.py +454 -0
  30. package/core/compaction.py +1735 -959
  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 +2566 -1353
  38. package/core/memory.py +482 -370
  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 +315 -167
  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 +1672 -198
  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 +21 -14
  70. package/package.json +303 -73
  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 +149 -100
  80. package/requirements.txt +252 -29
  81. package/safety/policy_enforcement.py +1082 -682
  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 +1338 -1005
  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 +374 -29
  118. package/tui/app.py +5281 -397
  119. package/tui/app.tcss +290 -9
  120. package/tui/commands.py +26 -61
  121. package/tui/diffs.py +286 -265
  122. package/tui/history.py +128 -0
  123. package/tui/hosted_driver.py +102 -0
  124. package/tui/markdown.py +1329 -0
  125. package/tui/meters.py +364 -0
  126. package/tui/screens.py +346 -92
  127. package/tui/themes.py +599 -366
  128. package/tui/widgets.py +2673 -116
  129. package/SECURITY.md +0 -88
  130. package/THIRD_PARTY_NOTICES.md +0 -100
package/core/client.py CHANGED
@@ -1,14 +1,20 @@
1
- from dataclasses import dataclass, field
2
- from typing import Optional
3
- from uuid import UUID, uuid4
1
+ import base64
2
+ import binascii
4
3
  import json
5
4
  import logging
5
+ import math
6
6
  import os
7
+ from dataclasses import dataclass, field
8
+ from typing import Optional
9
+ from uuid import UUID, uuid4
7
10
 
8
- import credentials
9
- from credentials import load_provider_data, no_providers_message, \
10
- unknown_provider_message
11
11
  import config
12
+ import credentials
13
+ from credentials import (
14
+ load_provider_data,
15
+ no_providers_message,
16
+ unknown_provider_message,
17
+ )
12
18
 
13
19
  # ROADMAP_v2 §33 W7 (#135). The three provider SDKs are NOT imported here.
14
20
  #
@@ -58,13 +64,26 @@ def api_initialization(provider_name: str):
58
64
 
59
65
  entry = provider_data[provider_name]
60
66
 
61
- if entry.get("is_v1_compatible", False):
67
+ # ROADMAP_v3 §83. Routed by NAME, before the flag: the plan is not
68
+ # v1-compatible and must never fall to the Chat Completions branch.
69
+ if provider_name == "CHATGPT":
70
+ client = _chatgpt_client()
71
+ elif entry.get("is_v1_compatible", False):
62
72
  from openai import OpenAI
63
73
  client = OpenAI(base_url=entry["API_URL"], api_key=entry["API_KEY"])
64
74
  elif provider_name == "GOOGLE":
65
75
  from google import genai
66
76
  client = genai.Client(api_key=entry["API_KEY"])
67
77
  elif provider_name == "ANTHROPIC":
78
+ # Batch 234. Refused HERE, before a client exists, because the SDK
79
+ # no longer says what is wrong: 2.x sends the key as `Bearer <key>`,
80
+ # httpx2 refuses `Bearer ` as an illegal header before any request,
81
+ # and the SDK wraps that in APIConnectionError -- which
82
+ # provider_errors retries as a network blip. 0.116.0 refused the
83
+ # empty key itself, so no configuration that worked stops working.
84
+ # Nothing is cached, so adding the key takes effect on the next call.
85
+ if not entry.get("API_KEY"):
86
+ raise ValueError(credentials.empty_key_refusal(provider_name))
68
87
  from anthropic import Anthropic
69
88
  client = Anthropic(api_key=entry["API_KEY"])
70
89
  # Add further proprietary API formats here
@@ -75,6 +94,69 @@ def api_initialization(provider_name: str):
75
94
  return client
76
95
 
77
96
 
97
+ def _chatgpt_client():
98
+ """The openai SDK, set up for the ChatGPT plan (ROADMAP_v3 §83, CG21).
99
+
100
+ `api_key` is a FUNCTION, `chatgpt_auth.access_token`: the SDK calls it
101
+ before every request (measured 25 of 25 in slice 2a), so the hourly
102
+ refresh needs no other plumbing and a cached client never holds a
103
+ stale token. `max_retries=0`, so CG10 decides every retry and the SDK
104
+ decides none: its own would resend a usage-limit 429.
105
+
106
+ The address is `chatgpt_auth.RESOURCE` and never the entry's `API_URL`.
107
+ The token is minted for that resource alone, and a hand-edited entry
108
+ must not be able to send it anywhere else.
109
+
110
+ Refused on a hosted deployment (CG1), as the sign-in already is, so a
111
+ `providers.json` carried onto one cannot reach the plan either.
112
+ """
113
+ from security import hosted
114
+
115
+ if hosted.is_hosted():
116
+ raise ValueError(
117
+ "The ChatGPT plan answers on a local install only (ROADMAP_v3 "
118
+ "§83, CG1): OpenAI offers it to open-source apps running on your "
119
+ "own machine.")
120
+ from openai import OpenAI
121
+
122
+ from core import chatgpt_auth
123
+
124
+ return OpenAI(base_url=chatgpt_auth.RESOURCE,
125
+ api_key=chatgpt_auth.access_token, max_retries=0)
126
+
127
+
128
+ def forget_client(provider_name: str) -> None:
129
+ """Drop the cached client for `provider_name`, so the next call builds a
130
+ new one from what `providers.json` (or the session) says NOW.
131
+
132
+ `/connect` is the caller (ROADMAP_v3 §79, HD5): the cache above is keyed
133
+ by provider name for the life of the process, so a key added or replaced
134
+ mid-session was otherwise ignored until a restart, with every call still
135
+ going out under the old one.
136
+ """
137
+ _client_cache.pop(provider_name, None)
138
+
139
+
140
+ def accepts_images(provider_name: str) -> bool:
141
+ """ROADMAP_v3 §81 (IM4). Whether an image in a tool result is DELIVERED
142
+ to this provider, as opposed to replaced by a note saying it was not.
143
+
144
+ Opt-in per provider through `accepts_images` in providers.json, on
145
+ `echoes_reasoning`'s terms: whether a provider takes an image depends on
146
+ the endpoint and often the model, cannot be discovered, and must not be
147
+ guessed, so it lives beside the endpoint and defaults to the
148
+ conservative answer. The test is `is True`, never truthiness:
149
+ `bool("false")` is True, and a quoted flag is a hand edit away (A29).
150
+
151
+ GOOGLE is honoured like the others since batch 173 (IM9). Until then it
152
+ was never delivered to and a flag on it was warned about: the old pin's
153
+ FunctionResponse had no field an image could go in, and the wire that
154
+ does work -- a harness turn after the batch -- had not been measured.
155
+ """
156
+ entry = load_provider_data().get(provider_name, {})
157
+ return isinstance(entry, dict) and entry.get("accepts_images") is True
158
+
159
+
78
160
  # ============================================================================
79
161
  # ---- Normalized response shape ---------------------------------------------
80
162
  # ============================================================================
@@ -140,8 +222,19 @@ class ModelResponse:
140
222
  # and mid-turn compaction shrinks clamped away. This is the figure
141
223
  # anything reasoning about SIZE reads; the spend meter must never be
142
224
  # read that way again (TECHNICAL_DEBT item 9, closed batch 27).
225
+ # turn_output_tokens: what the MODEL WROTE this turn -- output tokens
226
+ # and nothing else, summed across every step. Batch 61, and it is a
227
+ # THIRD instrument rather than a convenience: the two above are a
228
+ # SPEND meter and a SIZE meter, and a generation RATE can be derived
229
+ # from neither. turn_billed_tokens counts the prompt again on every
230
+ # step; turn_new_tokens adds the positive input deltas, so a
231
+ # tool-using turn's tool results land in it and it overstates what was
232
+ # generated. Dividing either by a duration produces a number that
233
+ # looks like tok/s and is not, which is exactly the misreading
234
+ # TECHNICAL_DEBT item 9 was closed for.
143
235
  turn_billed_tokens: Optional[int] = None
144
236
  turn_new_tokens: Optional[int] = None
237
+ turn_output_tokens: Optional[int] = None
145
238
  # Batch 44 (§44). The turn's reasoning, as the PROVIDER'S OWN BLOCKS
146
239
  # rather than as the text §38 streams for display:
147
240
  #
@@ -173,9 +266,10 @@ def _tool_arguments(raw_args: str, tool_name: str,
173
266
  """(input, parse_error) for one provider `arguments` string.
174
267
 
175
268
  The ONLY place a wire string is parsed as JSON. Google and Anthropic
176
- hand back dicts, so this is the OpenAI-compatible branch's alone --
177
- which is the branch a local model server takes, and truncated or
178
- malformed tool arguments are likeliest there.
269
+ hand back dicts, so this is the OpenAI-compatible branch's -- which is
270
+ the branch a local model server takes, and truncated or malformed tool
271
+ arguments are likeliest there -- and, since ROADMAP_v3 §83, CHATGPT's,
272
+ whose Responses function calls carry `arguments` as a string too.
179
273
 
180
274
  §33 W1 (#37): a failure is RETURNED, not raised. `dispatch()` turning
181
275
  handler exceptions into {"error": ...} is the whole reason a raising
@@ -249,10 +343,234 @@ class StreamToken:
249
343
  final_response: Optional[ModelResponse] = None
250
344
 
251
345
 
346
+ # ============================================================================
347
+ # ---- Token usage, which every branch reports and no branch can trust -------
348
+ # ============================================================================
349
+
350
+ def _token_count(usage_obj, field: str) -> Optional[int]:
351
+ """One token count off a provider's usage object, or None for "this
352
+ provider did not report that number".
353
+
354
+ `_record_window`'s rule applied to the other nullable figure these SDKs
355
+ carry -- "present" is not "usable". A bool is an int in Python and is
356
+ not a token count; a negative count is not one either.
357
+
358
+ WHY THIS IS NOT A `getattr(obj, field, 0)` DEFAULT, which is the
359
+ softened form D21's own note forbids: the attribute EXISTS on all three
360
+ pinned SDKs and holds None, so the default never fires and the None
361
+ travels into `core/loop.py`'s `usage_in + usage_out`. Measured per SDK
362
+ (batch 147), because the three reasons are different and only one of
363
+ them is visible in a type annotation. test_sdk_conformance.py holds each
364
+ on the pin; the versions below are batch 234's:
365
+
366
+ google-genai 2.32.0 every field of (as on 1.0.0 before it)
367
+ GenerateContentResponseUsageMetadata is
368
+ Optional[int] = None, and a bare instance is
369
+ TRUTHY -- so `if usage_meta:` passes and then
370
+ every read is None.
371
+ anthropic 2.1.0 Usage.input_tokens and CompletionUsage.
372
+ openai 3.36.0 prompt_tokens are annotated `int` and REQUIRED,
373
+ which says what the server promises rather than
374
+ what the object holds: both SDKs parse wire JSON
375
+ through pydantic's construct(), which skips
376
+ validation. `construct_type({"output_tokens":
377
+ 7}, Usage)` yields input_tokens=None, and
378
+ anthropic's own stream accumulator guards
379
+ `input_tokens` with `is not None` against that
380
+ same `int` annotation -- while assigning
381
+ `output_tokens` unconditionally.
382
+
383
+ So an `int` annotation is not a runtime guarantee anywhere here, which
384
+ is why ONE reader serves all three branches rather than the Google one
385
+ that the defect was found in.
386
+ """
387
+ if usage_obj is None:
388
+ return None
389
+ value = getattr(usage_obj, field, None)
390
+ if isinstance(value, bool) or not isinstance(value, int):
391
+ return None
392
+ return value if value >= 0 else None
393
+
394
+
395
+ def _stream_usage(provider_name: str, counts: dict,
396
+ supports_stream_usage: bool) -> dict:
397
+ """D21's usage-or-raise for all three streaming branches, and the one
398
+ place that turns "not reported" into a number.
399
+
400
+ RETURNS A TOTAL SHAPE: both values are ints, always. That is what makes
401
+ a belt in `core/loop.py` unnecessary rather than merely unfashionable --
402
+ the producer is where this belongs (AGENTS.md), and a consumer-side
403
+ guard would leave the next consumer to write its own.
404
+
405
+ D21 says a stream must not silently degrade token accounting, and the
406
+ guard that enforced it compared `input == 0 and output == 0`. A
407
+ PARTIAL report defeats that comparison without reporting anything
408
+ usable: `None != 0`, and once coerced, an unreported output count is
409
+ indistinguishable from a genuine zero. §64's A20 widens the rule to
410
+ match what D21 says rather than what it happened to compare: under the
411
+ flag, a count that is not a number raises, because the flag is the
412
+ per-provider statement that this provider DOES report usage and a
413
+ missing number is that promise broken.
414
+
415
+ Two messages, deliberately. "Completed without usage data" is kept
416
+ verbatim for the case it has always named -- nothing reported at all,
417
+ or an explicit zero for both -- so the existing guards keep their
418
+ meaning. The partial case gets its own line naming which count is
419
+ missing, because that is the one a reader cannot diagnose from the
420
+ other.
421
+
422
+ With the flag FALSE the coercion is all that happens, and 0 is the
423
+ right answer there: the flag's whole purpose is to let a provider that
424
+ genuinely omits usage run anyway.
425
+ """
426
+ reported_in = counts["input_tokens"]
427
+ reported_out = counts["output_tokens"]
428
+ if supports_stream_usage:
429
+ if (reported_in is None and reported_out is None) or (
430
+ reported_in == 0 and reported_out == 0):
431
+ raise RuntimeError(
432
+ f"Streaming call to {provider_name} completed without usage "
433
+ f"data despite supports_stream_usage=True. The stream may "
434
+ f"have been interrupted before the final usage chunk."
435
+ )
436
+ missing = [name for name, value in
437
+ (("input_tokens", reported_in),
438
+ ("output_tokens", reported_out)) if value is None]
439
+ if missing:
440
+ raise RuntimeError(
441
+ f"Streaming call to {provider_name} reported "
442
+ f"{' and '.join(missing)} as absent or not a number despite "
443
+ f"supports_stream_usage=True, so this step's token "
444
+ f"accounting would be short by it."
445
+ )
446
+ return {"input_tokens": reported_in or 0,
447
+ "output_tokens": reported_out or 0}
448
+
449
+
252
450
  # ============================================================================
253
451
  # ---- Tool schema translation ------------------------------------------------
254
452
  # ============================================================================
255
453
 
454
+ #: ROADMAP_v3 §82 (GS4). The `Schema` fields the Gemini API is known to take:
455
+ #: the 22 google-genai 1.0.0 had, which batch 132 and §9 verified on the
456
+ #: wire. The SDK serves Vertex AI as well, and 2.29.0 added three fields for
457
+ #: it -- `additional_properties`, which the Gemini API refused when measured
458
+ #: in batch 172 (400, `Unknown name "additional_properties"`), `defs` and
459
+ #: `ref`. One refused key fails every GOOGLE call, because the declarations
460
+ #: travel together, so a new SDK field waits to be measured.
461
+ _GOOGLE_SCHEMA_VERIFIED = frozenset({
462
+ "any_of", "default", "description", "enum", "example", "format",
463
+ "items", "max_items", "max_length", "max_properties", "maximum",
464
+ "min_items", "min_length", "min_properties", "minimum",
465
+ "nullable", "pattern", "properties", "property_ordering",
466
+ "required", "title", "type",
467
+ })
468
+
469
+
470
+ def _google_schema_keys(genai_types) -> set:
471
+ """Every key the pinned `types.Schema` will accept AND the Gemini API is
472
+ known to take, read off the SDK.
473
+
474
+ Field names plus their camelCase aliases, because `Schema` sets
475
+ `populate_by_name` and JSON Schema spells things `anyOf`/`maxItems`
476
+ while the model declares `any_of`/`max_items`. Read off the SDK so that
477
+ a field the installed version lacks is never sent -- and read at CALL
478
+ time, not import time, because the root `conftest.py` puts a fake
479
+ `google.genai` in `sys.modules` before collection and an import-time
480
+ constant would bind the fake's fields for the whole test session.
481
+
482
+ It used to take EVERY field, on the reasoning that a version which adds
483
+ one should carry it with no change here. The 2.29.0 pin disproved that
484
+ (§82): a field on the type is not a field the Gemini API accepts. So the
485
+ SDK's set is narrowed to `_GOOGLE_SCHEMA_VERIFIED`.
486
+ """
487
+ keys = set()
488
+ # NOT `field`: this module imports dataclasses.field at the top, and
489
+ # shadowing it here is what ruff F402 flagged in batch 141 -- found only
490
+ # then, because the lint job is not on main and so has never run.
491
+ for name, model_field in genai_types.Schema.model_fields.items():
492
+ if name not in _GOOGLE_SCHEMA_VERIFIED:
493
+ continue
494
+ keys.add(name)
495
+ alias = getattr(model_field, "alias", None)
496
+ if alias:
497
+ keys.add(alias)
498
+ return keys
499
+
500
+
501
+ def _reduce_for_google(node, accepted: set):
502
+ """JSON Schema -> what the pinned `types.Schema` can actually carry.
503
+
504
+ `Schema.model_config` is `extra="forbid"`, so a key it has no field for
505
+ is a `ValidationError` rather than something ignored. The translation
506
+ used to hand `input_schema` over untouched, and because it is one list
507
+ comprehension over every schema, ONE refusal took the whole advertised
508
+ set with it: no model call on GOOGLE could be built at all. This
509
+ repository's own `probability_stats` was the entry that did it --
510
+ pydantic renders its `Optional[dict[str, float]]` as
511
+ `{"anyOf": [{"additionalProperties": {...}, "type": "object"},
512
+ {"type": "null"}]}`, and `additionalProperties` is not a field.
513
+
514
+ THE REDUCTION IS HERE AND NOT IN THE TOOL, because the schemas are
515
+ generated by `model_json_schema()`: fixing the one tool leaves the next
516
+ `Optional[dict]` to find this again, which is the shape D24 settled for
517
+ permissions. Two rules:
518
+
519
+ * drop any key `Schema` has no field or alias for;
520
+ * flatten `anyOf: [X, {"type": "null"}]` to X plus `nullable`, which
521
+ is how this SDK spells an optional value. Dropping the null member
522
+ is not cosmetic -- the pinned SDK coerces `{"type": "null"}` with a
523
+ `UserWarning: null is not a valid Type` and carries on, so without
524
+ the flattening a nullable field reaches the wire describing a type
525
+ that does not exist.
526
+
527
+ `docs/ARCHITECTURE.md` 4.7 said the raw dict "auto-converts to
528
+ `types.Schema`". It does not; it validates, and refuses.
529
+
530
+ Since batch 172 (§82) the pin's `Schema` HAS `additional_properties`
531
+ (2.29.0 then, 2.32.0 since batch 234, the same 25 fields) -- and the
532
+ Gemini API refuses it, so it is still dropped: `accepted` is the
533
+ verified set, not the SDK's.
534
+ """
535
+ if isinstance(node, list):
536
+ return [_reduce_for_google(item, accepted) for item in node]
537
+ if not isinstance(node, dict):
538
+ return node
539
+
540
+ members = node.get("anyOf")
541
+ if isinstance(members, list):
542
+ live = [m for m in members
543
+ if not (isinstance(m, dict) and m.get("type") == "null")]
544
+ nullable = len(live) != len(members)
545
+ siblings = {k: v for k, v in node.items() if k != "anyOf"}
546
+ if len(live) == 1:
547
+ # The Optional[X] case: X itself, carrying anything that sat
548
+ # beside the union (a description, a default) and the flag.
549
+ merged = dict(_reduce_for_google(live[0], accepted))
550
+ merged.update(_reduce_for_google(siblings, accepted))
551
+ if nullable:
552
+ merged["nullable"] = True
553
+ return merged
554
+ out = _reduce_for_google(siblings, accepted)
555
+ out["anyOf"] = [_reduce_for_google(m, accepted) for m in live]
556
+ if nullable:
557
+ out["nullable"] = True
558
+ return out
559
+
560
+ out = {}
561
+ for key, value in node.items():
562
+ if key not in accepted:
563
+ continue
564
+ if key == "properties" and isinstance(value, dict):
565
+ out[key] = {name: _reduce_for_google(sub, accepted)
566
+ for name, sub in value.items()}
567
+ elif key in ("items", "anyOf"):
568
+ out[key] = _reduce_for_google(value, accepted)
569
+ else:
570
+ out[key] = value
571
+ return out
572
+
573
+
256
574
  def _tools_for_provider(provider_name: str, tool_schemas: list[dict]):
257
575
  """
258
576
  tool_schemas arrive in Anthropic's native shape (name/description/
@@ -267,17 +585,34 @@ def _tools_for_provider(provider_name: str, tool_schemas: list[dict]):
267
585
 
268
586
  if provider_name == "GOOGLE":
269
587
  from google.genai import types as genai_types
588
+ accepted = _google_schema_keys(genai_types)
270
589
  return [genai_types.Tool(
271
590
  function_declarations=[
272
591
  genai_types.FunctionDeclaration(
273
592
  name=s["name"],
274
593
  description=s["description"],
275
- parameters=s["input_schema"],
594
+ parameters=_reduce_for_google(s["input_schema"], accepted),
276
595
  )
277
596
  for s in tool_schemas
278
597
  ]
279
598
  )]
280
599
 
600
+ if provider_name == "CHATGPT":
601
+ # ROADMAP_v3 §83. The Responses API's FLAT function shape, and no
602
+ # `strict`. Slice 2a measured it: the registry's fifteen schemas,
603
+ # none of which meets strict mode's rules, completed a call without
604
+ # the key -- and cost fewer input tokens than with `strict: false`.
605
+ # Never a hosted tool (CG3): only what the harness itself runs.
606
+ return [
607
+ {
608
+ "type": "function",
609
+ "name": s["name"],
610
+ "description": s["description"],
611
+ "parameters": s["input_schema"],
612
+ }
613
+ for s in tool_schemas
614
+ ]
615
+
281
616
  # OpenAI-compatible providers
282
617
  return [
283
618
  {
@@ -360,16 +695,220 @@ def _echoed_blocks(provider_name: str, model: Optional[str],
360
695
  return [b for b in record.get("blocks") or [] if isinstance(b, dict)]
361
696
 
362
697
 
698
+ def _chatgpt_reasoning(model: Optional[str], msg: dict) -> list:
699
+ """The reasoning items a CHATGPT turn goes back with (ROADMAP_v3 §83,
700
+ CG18, CG23): exactly as the SDK parsed them, `None` fields left out,
701
+ and only to the model that wrote them -- §44's pair, through
702
+ `_echoed_blocks`.
703
+
704
+ EVERY turn this model wrote, the ones that ended in calls included
705
+ (CG23, the owner's choice). Slice 2a measured the item accepted under
706
+ `store: false`, with its id and without, before a user message; inside a
707
+ tool loop it was never sent, because the model did not reason before a
708
+ trivial call. The item type is the same and so is the list it sits in,
709
+ and the tool loop is where a reasoning model most needs what it already
710
+ worked out. A refusal would fail that step with a 400 naming the input,
711
+ which is the owner's live check to see."""
712
+ return [b for b in _echoed_blocks("CHATGPT", model, msg)
713
+ if b.get("type") == "reasoning"]
714
+
715
+
716
+ #: ROADMAP_v3 §82 (GS3). Google's stand-in for a thought signature, for a
717
+ #: function call the target model did not write. Gemini 3 refuses a request
718
+ #: whose current turn holds an unsigned call, and a thread that switched
719
+ #: provider or model mid-turn has exactly that. Measured accepted on
720
+ #: gemini-3.1-flash-lite (batch 172); a real signature is always preferred.
721
+ _GOOGLE_FOREIGN_SIGNATURE = b"skip_thought_signature_validator"
722
+
723
+
724
+ def _google_signature_block(call_id: str, signature: Optional[bytes]) -> dict:
725
+ """One function call's thought signature, as §44's record holds it
726
+ (§82, GS2). One block per call, INCLUDING a call that carried none --
727
+ Gemini 3 signs only the first of a parallel batch, and a model that
728
+ does not think signs nothing -- so "this model wrote the turn and
729
+ attached nothing" stays distinguishable from "nothing is known". Base64
730
+ in the record, because the record is JSON; and no `text` or `thinking`
731
+ key, so replay renders nothing for it."""
732
+ return {"type": "thought_signature", "call_id": call_id,
733
+ "signature": (base64.b64encode(signature).decode("ascii")
734
+ if signature else None)}
735
+
736
+
737
+ def _google_signatures(model: Optional[str], msg: dict) -> Optional[dict]:
738
+ """{call_id: signature bytes or None} for a turn THIS model wrote, or
739
+ None when it wrote nothing here (§82, GS2/GS3).
740
+
741
+ The gate is §44's pair, read through `_echoed_blocks`: a signature goes
742
+ back unchanged to the model that produced it and to nothing else. A
743
+ stored value that will not decode costs that call its signature and
744
+ gets the placeholder instead, `_decode_thinking`'s rule: an unreadable
745
+ entry costs the entry, never the request."""
746
+ blocks = [b for b in _echoed_blocks("GOOGLE", model, msg)
747
+ if b.get("type") == "thought_signature"]
748
+ if not blocks:
749
+ return None
750
+ signatures: dict = {}
751
+ for block in blocks:
752
+ value = block.get("signature")
753
+ if value is None:
754
+ signatures[block.get("call_id")] = None
755
+ continue
756
+ try:
757
+ signatures[block.get("call_id")] = base64.b64decode(
758
+ value, validate=True)
759
+ except (binascii.Error, TypeError, ValueError):
760
+ signatures[block.get("call_id")] = _GOOGLE_FOREIGN_SIGNATURE
761
+ return signatures
762
+
763
+
764
+ #: ROADMAP_v3 §81 (IM10). The image types GOOGLE is sent: Gemini's documented
765
+ #: list (PNG, JPEG, WEBP, HEIC, HEIF) met with what `view_image` attaches.
766
+ #: GIF is left out, the owner's choice. Two Gemini 3 models read one correctly
767
+ #: when measured, but it is not documented, and an image the endpoint refused
768
+ #: would stay in the thread and fail every request after it.
769
+ _GOOGLE_IMAGE_TYPES = frozenset({"image/png", "image/jpeg", "image/webp"})
770
+
771
+ #: What a tool message says on the two wires that deliver images after the
772
+ #: batch, so the model meets the images knowing whose they are (IM4, IM9).
773
+ _IMAGES_FOLLOW = ("[The attached image(s) follow these tool results, in a "
774
+ "harness message.]")
775
+
776
+
777
+ def _image_note(images: list,
778
+ reason: str = "this provider is not set up to receive images"
779
+ ) -> str:
780
+ """What a tool result's images become on a wire that does not receive
781
+ them (ROADMAP_v3 §81, IM5): a sentence saying so, never the bytes and
782
+ never silence. One rule for every way to get here -- a provider not
783
+ marked `accepts_images`, a thread that switched to one after the image
784
+ was attached, and since batch 173 an image GOOGLE is not sent (IM10). A
785
+ model told nothing would answer from a result that reads as complete,
786
+ which is A32's lesson about MCP's placeholder."""
787
+ kinds = ", ".join(sorted({image["media_type"] for image in images}))
788
+ return (f"[{len(images)} image(s) attached to this result ({kinds}) were "
789
+ f"not sent: {reason}.]")
790
+
791
+
792
+ def _with_image_note(msg: dict) -> str:
793
+ """A tool row's text, with the IM5 note appended when it carries images
794
+ this wire will not receive."""
795
+ images = msg.get("images")
796
+ if not images:
797
+ return msg["content"]
798
+ return f"{msg['content']}\n{_image_note(images)}"
799
+
800
+
801
+ def _openai_image_turn(pending: list) -> dict:
802
+ """The user-role message that carries a tool batch's images on the
803
+ OpenAI-compatible wire (IM4).
804
+
805
+ A tool message there holds TEXT PARTS ONLY -- the installed SDK types its
806
+ content as text parts, and the API rejects an image part in it -- so an
807
+ image a tool returned can only travel in a user message. It goes AFTER
808
+ the whole batch: every tool message must directly follow the assistant
809
+ turn that called it, so one placed between two of them would break the
810
+ pairing. Built here and never persisted, like the compaction summary
811
+ (M8): the archive keeps the tool row with its images, and each request
812
+ derives this message from it again. It says it is the harness speaking,
813
+ so the model does not read the images as something the user sent.
814
+ """
815
+ parts: list = []
816
+ for tool_call_id, images in pending:
817
+ parts.append({"type": "text", "text": (
818
+ f"[Harness: the image(s) attached to the result of tool call "
819
+ f"{tool_call_id}. A tool message on this API carries text only, "
820
+ f"so they are delivered here.]")})
821
+ for image in images:
822
+ parts.append({"type": "image_url", "image_url": {
823
+ "url": f"data:{image['media_type']};base64,{image['data']}"}})
824
+ return {"role": "user", "content": parts}
825
+
826
+
827
+ def _google_tool_images(msg: dict, images: bool) -> tuple:
828
+ """(result text, [(media_type, bytes)]) for one tool row on GOOGLE
829
+ (ROADMAP_v3 §81, IM9/IM10).
830
+
831
+ The bytes go out DECODED: `Blob.data` is bytes on the pinned type, and
832
+ a base64 string handed to it would be sent as the text of the base64.
833
+ Each image that is not sent says why in the result: the provider is not
834
+ marked (IM5), the type is not one Google is sent (IM10), or the stored
835
+ copy will not decode -- `_decode_thinking`'s rule, so one unreadable
836
+ image costs that image, never the request."""
837
+ attached = msg.get("images") or []
838
+ if not attached:
839
+ return msg["content"], []
840
+ if not images:
841
+ return _with_image_note(msg), []
842
+ sent, refused, unreadable = [], [], []
843
+ for image in attached:
844
+ if image["media_type"] not in _GOOGLE_IMAGE_TYPES:
845
+ refused.append(image)
846
+ continue
847
+ try:
848
+ sent.append((image["media_type"],
849
+ base64.b64decode(image["data"], validate=True)))
850
+ except (binascii.Error, TypeError, ValueError):
851
+ unreadable.append(image)
852
+ text = msg["content"]
853
+ if refused:
854
+ kinds = ", ".join(sorted({image["media_type"] for image in refused}))
855
+ text += "\n" + _image_note(refused, f"this provider is not sent {kinds}")
856
+ if unreadable:
857
+ text += "\n" + _image_note(unreadable,
858
+ "the stored copy could not be read")
859
+ if sent:
860
+ text += "\n" + _IMAGES_FOLLOW
861
+ return text, sent
862
+
863
+
864
+ def _google_image_turn(pending: list, genai_types):
865
+ """The user-role Content that carries a tool batch's images on GOOGLE
866
+ (ROADMAP_v3 §81, IM9).
867
+
868
+ A FunctionResponse cannot carry them on the models a new account can
869
+ call: Gemini 2.5 answers "multimodal function responses are not
870
+ supported", and only Gemini 3 takes `FunctionResponse.parts`. A harness
871
+ turn after the WHOLE batch can, measured on two Gemini 3 models, and it
872
+ is the shape Google's own ADK uses for an artifact a tool loads. After
873
+ the batch, so the function responses still answer the call turn
874
+ directly; derived per request and never persisted, `_openai_image_turn`'s
875
+ rule; and it says it is the harness speaking."""
876
+ parts: list = []
877
+ for tool_call_id, images in pending:
878
+ parts.append(genai_types.Part(text=(
879
+ f"[Harness: the image(s) attached to the result of tool call "
880
+ f"{tool_call_id}. A function response on this API carries no "
881
+ f"image, so they are delivered here.]")))
882
+ for media_type, data in images:
883
+ parts.append(genai_types.Part(inline_data=genai_types.Blob(
884
+ mime_type=media_type, data=data)))
885
+ return genai_types.Content(role="user", parts=parts)
886
+
887
+
363
888
  def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
364
889
  model: Optional[str] = None,
365
- echo_reasoning: bool = False) -> list:
890
+ echo_reasoning: bool = False,
891
+ images: bool = False) -> list:
366
892
  """The wire shape for one provider.
367
893
 
368
894
  `model` and `echo_reasoning` are §44's: they decide whether a stored
369
895
  thinking record may travel. Both DEFAULT to the pre-§44 behaviour --
370
896
  no model means no echo, and the OpenAI-compatible side is opt-in per
371
897
  provider -- so a caller that does not pass them gets exactly the
372
- translation this function produced before.
898
+ translation this function produced before. On GOOGLE `model` also
899
+ decides whether a function call's thought signature goes back (§82),
900
+ and the echo is not opt-in there: Gemini 3 refuses a turn without one.
901
+
902
+ `images` is ROADMAP_v3 §81's (IM4): whether a tool row's images are
903
+ delivered to this provider, or replaced by `_image_note`. It defaults
904
+ to the note, so a caller that does not pass it sends no bytes. GOOGLE
905
+ reads it too since batch 173 (IM9); it ignored it while that wire was
906
+ deferred.
907
+
908
+ CHATGPT (ROADMAP_v3 §83) gets the Responses API's `input` items, each
909
+ shape measured on the plan route in slice 2a. Its reasoning goes back
910
+ on §44's pair rule like Anthropic's, and is not opt-in: OpenAI's own
911
+ stateless pattern hands it back (CG18, CG23).
373
912
  """
374
913
  # One rule, above the split (#13, #36). Note this can leave two
375
914
  # consecutive user turns -- which is the shape Google's branch has
@@ -379,6 +918,41 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
379
918
  neutral_messages = [m for m in neutral_messages
380
919
  if not _is_empty_assistant_turn(m)]
381
920
 
921
+ if provider_name == "CHATGPT":
922
+ # Slice 2a, item by item: an earlier reply as a plain assistant
923
+ # message, two user turns in a row, and a call WITHOUT its `fc_`
924
+ # item id -- under any call id, another provider's included, which
925
+ # is what a thread holds after a /model switch. So nothing beyond
926
+ # the neutral {id, name, input} has to be kept for a call.
927
+ translated: list = []
928
+ for msg in neutral_messages:
929
+ role = msg["role"]
930
+ if role == "user":
931
+ translated.append({"role": "user", "content": msg["content"]})
932
+ elif role == "assistant":
933
+ # The reasoning FIRST: the order the model emitted it in.
934
+ translated.extend(_chatgpt_reasoning(model, msg))
935
+ if msg.get("text"):
936
+ translated.append(
937
+ {"role": "assistant", "content": msg["text"]})
938
+ for tc in msg.get("tool_calls", []):
939
+ translated.append({
940
+ "type": "function_call", "call_id": tc["id"],
941
+ "name": tc["name"],
942
+ "arguments": json.dumps(tc["input"]),
943
+ })
944
+ elif role == "tool":
945
+ # CG7: no image is delivered on this wire. One swatch was
946
+ # misread in slice 0, and delivering one is a measurement and
947
+ # a translation still to come, so a result's images become
948
+ # IM5's note whatever `accepts_images` says.
949
+ translated.append({
950
+ "type": "function_call_output",
951
+ "call_id": msg["tool_call_id"],
952
+ "output": _with_image_note(msg),
953
+ })
954
+ return translated
955
+
382
956
  if provider_name == "GOOGLE":
383
957
  from google.genai import types as genai_types
384
958
  # Build a {tool_call_id: function_name} lookup from assistant
@@ -391,9 +965,18 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
391
965
  call_id_to_name[tc["id"]] = tc["name"]
392
966
 
393
967
  translated = []
968
+ # §81 (IM9). (tool_call_id, [(media_type, bytes)]) for the tool
969
+ # batch being translated, sent as one harness turn when the batch
970
+ # ends -- see _google_image_turn.
971
+ pending_images: list = []
394
972
  for msg in neutral_messages:
395
973
  role = msg["role"]
396
974
 
975
+ if role != "tool" and pending_images:
976
+ translated.append(
977
+ _google_image_turn(pending_images, genai_types))
978
+ pending_images = []
979
+
397
980
  if role == "user":
398
981
  translated.append(genai_types.Content(
399
982
  role="user",
@@ -404,13 +987,21 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
404
987
  parts = []
405
988
  if msg.get("text"):
406
989
  parts.append(genai_types.Part(text=msg["text"]))
407
- for tc in msg.get("tool_calls", []):
990
+ # §82 (GS2/GS3). Each call goes back with exactly what this
991
+ # model attached to it. A turn it did not write gets the
992
+ # placeholder on its FIRST call -- the one Gemini 3 checks --
993
+ # and nothing on the rest, which is how it signs its own.
994
+ own = _google_signatures(model, msg)
995
+ for index, tc in enumerate(msg.get("tool_calls", [])):
996
+ fallback = _GOOGLE_FOREIGN_SIGNATURE if index == 0 else None
408
997
  parts.append(genai_types.Part(
409
998
  function_call=genai_types.FunctionCall(
410
999
  name=tc["name"],
411
1000
  args=tc["input"],
412
1001
  id=tc["id"],
413
- )
1002
+ ),
1003
+ thought_signature=(fallback if own is None
1004
+ else own.get(tc["id"], fallback)),
414
1005
  ))
415
1006
  # Google rejects Content with empty parts. The turn that
416
1007
  # produced them is now dropped above the branch split, so
@@ -425,14 +1016,21 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
425
1016
  f"Tool result references tool_call_id={msg['tool_call_id']!r} "
426
1017
  f"but no assistant message in the history made that call"
427
1018
  )
1019
+ # §81 (IM9/IM10). The images wait for the harness turn after
1020
+ # the batch; the result says they follow, or why not.
1021
+ result, sent = _google_tool_images(msg, images)
1022
+ if sent:
1023
+ pending_images.append((msg["tool_call_id"], sent))
428
1024
  part = genai_types.Part(
429
1025
  function_response=genai_types.FunctionResponse(
430
1026
  name=name,
431
- response={"result": msg["content"]},
1027
+ response={"result": result},
432
1028
  )
433
1029
  )
434
1030
  # Batch consecutive tool results into a single user
435
1031
  # Content — Google requires strict user/model alternation.
1032
+ # The harness image turn comes only after the batch, so it
1033
+ # never sits between two of them.
436
1034
  if (translated
437
1035
  and translated[-1].role == "user"
438
1036
  and all(p.function_response is not None for p in translated[-1].parts)):
@@ -440,6 +1038,8 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
440
1038
  else:
441
1039
  translated.append(genai_types.Content(role="user", parts=[part]))
442
1040
 
1041
+ if pending_images:
1042
+ translated.append(_google_image_turn(pending_images, genai_types))
443
1043
  return translated
444
1044
 
445
1045
  if provider_name == "ANTHROPIC":
@@ -470,10 +1070,30 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
470
1070
  # blocks, not one message per result -- merge consecutive
471
1071
  # tool-role entries into the same message rather than
472
1072
  # appending a new one each time.
1073
+ #
1074
+ # §81 (IM4). An image travels INSIDE its tool_result here --
1075
+ # the one wire of the three whose tool result takes an image
1076
+ # block -- after the text, as a content array. The text
1077
+ # block is omitted when empty, because the API rejects an
1078
+ # empty text block.
1079
+ content = msg["content"]
1080
+ if msg.get("images"):
1081
+ if images:
1082
+ content = (
1083
+ [{"type": "text", "text": msg["content"]}]
1084
+ if msg["content"] else [])
1085
+ content += [{
1086
+ "type": "image",
1087
+ "source": {"type": "base64",
1088
+ "media_type": image["media_type"],
1089
+ "data": image["data"]},
1090
+ } for image in msg["images"]]
1091
+ else:
1092
+ content = _with_image_note(msg)
473
1093
  block = {
474
1094
  "type": "tool_result",
475
1095
  "tool_use_id": msg["tool_call_id"],
476
- "content": msg["content"],
1096
+ "content": content,
477
1097
  }
478
1098
  if translated and translated[-1]["role"] == "user" and _is_tool_result_message(translated[-1]):
479
1099
  translated[-1]["content"].append(block)
@@ -484,9 +1104,16 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
484
1104
 
485
1105
  # OpenAI-compatible: close to 1:1, each tool result is its own message
486
1106
  translated = []
1107
+ # §81 (IM4). (tool_call_id, images) for the tool batch being translated,
1108
+ # sent as one user message when the batch ends -- see _openai_image_turn.
1109
+ pending_images: list = []
487
1110
  for msg in neutral_messages:
488
1111
  role = msg["role"]
489
1112
 
1113
+ if role != "tool" and pending_images:
1114
+ translated.append(_openai_image_turn(pending_images))
1115
+ pending_images = []
1116
+
490
1117
  if role == "user":
491
1118
  translated.append({"role": "user", "content": msg["content"]})
492
1119
 
@@ -518,10 +1145,20 @@ def _messages_for_provider(provider_name: str, neutral_messages: list[dict],
518
1145
  translated.append(entry)
519
1146
 
520
1147
  elif role == "tool":
1148
+ content = msg["content"]
1149
+ if msg.get("images"):
1150
+ if images:
1151
+ pending_images.append(
1152
+ (msg["tool_call_id"], msg["images"]))
1153
+ content = f"{content}\n{_IMAGES_FOLLOW}"
1154
+ else:
1155
+ content = _with_image_note(msg)
521
1156
  translated.append({
522
- "role": "tool", "tool_call_id": msg["tool_call_id"], "content": msg["content"],
1157
+ "role": "tool", "tool_call_id": msg["tool_call_id"], "content": content,
523
1158
  })
524
1159
 
1160
+ if pending_images:
1161
+ translated.append(_openai_image_turn(pending_images))
525
1162
  return translated
526
1163
 
527
1164
 
@@ -560,8 +1197,62 @@ _context_window_cache: dict[tuple[str, str], Optional[int]] = {}
560
1197
  _context_window_failures: dict[tuple[str, str], int] = {}
561
1198
  MAX_CONTEXT_WINDOW_QUERY_ATTEMPTS = 3
562
1199
 
1200
+ #: ROADMAP_v3 §83 (CG22). The plan route's whole effort vocabulary, read off
1201
+ #: slice 0's `invalid_value` refusal of `ultra`. The model list names a level
1202
+ #: the route refuses (`ultra`), so a level reaches /effort only when the list
1203
+ #: names it AND this does. Slice 2a found the rule exact on both models whose
1204
+ #: refusal lists their levels.
1205
+ CHATGPT_EFFORT_VOCABULARY = ("none", "minimal", "low", "medium", "high",
1206
+ "xhigh", "max")
1207
+ #: CG24, the owner's choice. Six models accepted `none` in slice 2a and two
1208
+ #: refused it; no list entry names it, and a table of the six would go stale
1209
+ #: with a roster that changed between two runs a day apart.
1210
+ CHATGPT_NEVER_OFFERED = frozenset({"none"})
1211
+
1212
+
1213
+ def _chatgpt_listing(client) -> list:
1214
+ """The plan's model list, every entry (ROADMAP_v3 §83).
1215
+
1216
+ `client.get("/models", cast_to=object)`, because the plan's list is
1217
+ `{"models": [...]}` keyed by `slug`, which the SDK's `models.list()` --
1218
+ built for `{"data": [...]}` -- cannot read; slice 2a read it this way.
1219
+ Raises when the list cannot be read, which each caller counts as a
1220
+ failed query.
1221
+ """
1222
+ body = client.get("/models", cast_to=object)
1223
+ entries = body.get("models") if isinstance(body, dict) else None
1224
+ if not isinstance(entries, list):
1225
+ raise ValueError(
1226
+ "the ChatGPT plan's model list carried no `models` array")
1227
+ return [entry for entry in entries if isinstance(entry, dict)]
1228
+
1229
+
1230
+ def _chatgpt_entry(entries: list, model: str) -> Optional[dict]:
1231
+ """`model`'s entry, or None when the list does not name it. Any entry
1232
+ counts, a `visibility: hide` one included: a hidden model works when
1233
+ named, and its levels and window are its own."""
1234
+ return next((e for e in entries if e.get("slug") == model), None)
563
1235
 
564
- def _sampling_kwargs(provider_name: str, model: str, temperature) -> dict:
1236
+
1237
+ def _chatgpt_unlisted(model: str, entries: list) -> None:
1238
+ """Say that the plan does not list `model`, and what it does list.
1239
+
1240
+ Batch 189, the owner's live check: a typo (`gpt-6-luba`) fell through to
1241
+ the static table's warning, which advised adding a config entry and
1242
+ named `ultra` -- a level the CHATGPT filter removes after that warning
1243
+ is written. On the plan an unlisted name is likeliest misspelt, and the
1244
+ list that would show it was already in hand.
1245
+ """
1246
+ listed = [e.get("slug") for e in entries
1247
+ if e.get("visibility") == "list" and e.get("slug")]
1248
+ logger.warning(
1249
+ "%r is not in your ChatGPT plan's model list, so the next turn will "
1250
+ "probably be refused. The plan lists: %s.", model,
1251
+ ", ".join(listed) or "no models")
1252
+
1253
+
1254
+ def _sampling_kwargs(provider_name: str, model: str, temperature,
1255
+ effort: Optional[str] = None) -> dict:
565
1256
  """Sampling parameters for this call, or {} when the model rejects them.
566
1257
 
567
1258
  Current Anthropic models removed temperature/top_p/top_k outright (any
@@ -570,6 +1261,24 @@ def _sampling_kwargs(provider_name: str, model: str, temperature) -> dict:
570
1261
  ROADMAP §10's ensemble mode came to be built, documented as working, and
571
1262
  incapable of running against this harness's own default model.
572
1263
 
1264
+ TWO MORE, MEASURED FROM THE PROVIDERS' OWN DOCUMENTATION (ROADMAP_v3
1265
+ §87, #6). OpenAI's rule for its reasoning models is "When reasoning
1266
+ effort is not `none`, remove `temperature`, `top_p`, and
1267
+ `top_logprobs`", and a model that reasons refuses one with a 400 -- so
1268
+ `effort` decides it on OPENAI, the value `_thinking_for_provider` is
1269
+ about to send, with no model list to go stale. And Google's Gemini 3.6
1270
+ Flash and 3.5 Flash-Lite IGNORE a temperature, as Google says every
1271
+ later Gemini will: no error, no effect, which is the one failure here
1272
+ worse than a 400. Those are named in the set below like the rest.
1273
+
1274
+ ON ANTHROPIC, EVERY MODEL, SINCE BATCH 234 (A82, amended). anthropic 2.x
1275
+ sends API version 2026-10-21, which refuses `temperature`, `top_p` and
1276
+ `top_k` with a 400 on every model, even at the default; and 1.0 took
1277
+ them out of `messages.stream()`, so passing one is a TypeError before
1278
+ anything is sent (measured). The set's Claude names stay for Claude
1279
+ reached through an OpenAI-compatible gateway, where they are still the
1280
+ only thing that can drop it.
1281
+
573
1282
  Dropping the parameter is logged at WARNING rather than done silently.
574
1283
 
575
1284
  THIS FUNCTION NOW HAS NO CALLER THAT DEPENDS ON IT, and the warning is
@@ -589,6 +1298,27 @@ def _sampling_kwargs(provider_name: str, model: str, temperature) -> dict:
589
1298
  """
590
1299
  if temperature is None:
591
1300
  return {}
1301
+ if provider_name == "CHATGPT":
1302
+ # ROADMAP_v3 §83: the plan route answers a temperature with 400
1303
+ # "Unsupported parameter" (slice 0), on every model.
1304
+ logger.warning(
1305
+ "Dropping temperature=%s: the ChatGPT plan's route refuses "
1306
+ "sampling parameters.", temperature)
1307
+ return {}
1308
+ if provider_name == "ANTHROPIC":
1309
+ logger.warning(
1310
+ "Dropping temperature=%s: Anthropic's API version 2026-10-21 "
1311
+ "refuses sampling parameters on every model.", temperature)
1312
+ return {}
1313
+ if provider_name == "OPENAI" and effort and effort != "none":
1314
+ # §87. Sent no effort at all, a reasoning model still reasons at
1315
+ # its own default and still refuses: that stays the loud 400 an
1316
+ # unlisted model has always been, rather than a guess here.
1317
+ logger.warning(
1318
+ "Dropping temperature=%s: OpenAI refuses sampling parameters "
1319
+ "while a model reasons (reasoning effort %r).", temperature,
1320
+ effort)
1321
+ return {}
592
1322
  if model in config.MODELS_REJECTING_SAMPLING_PARAMS:
593
1323
  logger.warning(
594
1324
  "Dropping temperature=%s: model %r rejects sampling parameters. "
@@ -658,6 +1388,35 @@ def effort_levels_for_model(client, provider_name: str, model: str) -> list[str]
658
1388
  return _effort_levels(client, provider_name, model)[0]
659
1389
 
660
1390
 
1391
+ def effort_choice(levels, requested: str, current: Optional[str],
1392
+ model: str) -> tuple:
1393
+ """`/effort`'s decision in either shell: `(level, sentence, refused)`.
1394
+
1395
+ `levels` is what `effort_levels_for_model` answered for the session's
1396
+ model, `requested` the command's argument and `current` the level in
1397
+ force. The level comes back unchanged for a report or a refusal, None
1398
+ for `auto`, and the requested one when the model accepts it.
1399
+
1400
+ ONE COPY (ROADMAP_v3 §89): the TUI answers on a worker's message and
1401
+ the CLI inline, and what each may accept is the same decision. Moved
1402
+ out of the TUI's `_apply_effort_command`.
1403
+ """
1404
+ if not levels:
1405
+ return current, f"{model} exposes no reasoning-effort control.", False
1406
+ available = ", ".join(levels)
1407
+ if not requested:
1408
+ return (current,
1409
+ f"Effort: {current or 'auto'}. Available: {available} "
1410
+ f"(or 'auto').", False)
1411
+ if requested == "auto":
1412
+ return None, "Effort set to auto.", False
1413
+ if requested in levels:
1414
+ return requested, f"Effort set to {requested}.", False
1415
+ return (current,
1416
+ f"{model} does not support effort {requested!r}. "
1417
+ f"Available: {available}.", True)
1418
+
1419
+
661
1420
  def _effort_levels(client, provider_name: str, model: str) -> tuple:
662
1421
  """(levels, authoritative).
663
1422
 
@@ -726,6 +1485,30 @@ def _effort_levels(client, provider_name: str, model: str) -> tuple:
726
1485
  "Could not read effort capabilities for %r from the Anthropic "
727
1486
  "Models API (%s); falling back to the static table.", model, e,
728
1487
  )
1488
+ if provider_name == "CHATGPT":
1489
+ try:
1490
+ entries = _chatgpt_listing(client)
1491
+ except Exception as e:
1492
+ query_failed = True
1493
+ logger.warning(
1494
+ "Could not read effort levels for %r from the ChatGPT plan's "
1495
+ "model list (%s); falling back to the static table.", model, e)
1496
+ else:
1497
+ entry = _chatgpt_entry(entries, model)
1498
+ if entry is not None:
1499
+ # ONE read, TWO readings, as Anthropic's retrieve: the entry
1500
+ # carries the window too.
1501
+ _record_window(provider_name, model,
1502
+ entry.get("context_window"))
1503
+ levels = [lv.get("effort") if isinstance(lv, dict) else lv
1504
+ for lv in entry.get("supported_reasoning_levels")
1505
+ or []]
1506
+ else:
1507
+ # The list answered and does not name the model: said in
1508
+ # the plan's own terms, and the default set is filtered
1509
+ # below like any other, without the static table's advice.
1510
+ _chatgpt_unlisted(model, entries)
1511
+ levels = list(config.DEFAULT_EFFORT_LEVELS)
729
1512
 
730
1513
  if levels is None:
731
1514
  levels = config.MODEL_EFFORT_LEVELS.get(model)
@@ -737,6 +1520,12 @@ def _effort_levels(client, provider_name: str, model: str) -> tuple:
737
1520
  )
738
1521
  levels = list(config.DEFAULT_EFFORT_LEVELS)
739
1522
 
1523
+ if provider_name == "CHATGPT":
1524
+ # CG22 and CG24, applied to the static table's answer as well: its
1525
+ # default set ends in `ultra`, which the route refuses.
1526
+ levels = [lv for lv in levels if lv in CHATGPT_EFFORT_VOCABULARY
1527
+ and lv not in CHATGPT_NEVER_OFFERED]
1528
+
740
1529
  if query_failed:
741
1530
  # Do NOT cache a failure-derived guess. Caching it made one
742
1531
  # transient error permanently suppress the query that is this
@@ -754,8 +1543,8 @@ def _effort_levels(client, provider_name: str, model: str) -> tuple:
754
1543
  # rather than one adapter each, because openai.types.Model is declared
755
1544
  # `extra="allow"` -- a field the SDK does not model survives on the parsed
756
1545
  # object and reaches `.model_extra`. Verified against the pinned openai
757
- # 2.45.0 in test_sdk_conformance.py, because that permissiveness is the
758
- # whole reason this is one function.
1546
+ # (2.45.0 when written, 3.36.0 since batch 234) in test_sdk_conformance.py,
1547
+ # because that permissiveness is the whole reason this is one function.
759
1548
  #
760
1549
  # context_window Groq
761
1550
  # context_length Together, OpenRouter, Cohere (native /v1 only)
@@ -898,6 +1687,10 @@ def context_window_for(client, provider_name: str, model: str) -> tuple:
898
1687
  makes no call at all.
899
1688
  GOOGLE models.get(...).input_token_limit, one field on the
900
1689
  pinned google-genai type.
1690
+ CHATGPT the plan's model list, `context_window` on the model's
1691
+ entry (ROADMAP_v3 §83) -- not the larger
1692
+ `max_context_window`. Usually a cache hit too: _effort_levels
1693
+ records it off the same read.
901
1694
  v1-compat ONE alias sniff over model_extra, not thirteen adapters,
902
1695
  because openai.types.Model allows extra fields -- over
903
1696
  whichever of models.retrieve() and models.list() answers
@@ -927,6 +1720,11 @@ def context_window_for(client, provider_name: str, model: str) -> tuple:
927
1720
  return _record_window(
928
1721
  provider_name, model,
929
1722
  getattr(info, "input_token_limit", None)), True
1723
+ if provider_name == "CHATGPT":
1724
+ entry = _chatgpt_entry(_chatgpt_listing(client), model)
1725
+ return _record_window(
1726
+ provider_name, model,
1727
+ entry.get("context_window") if entry else None), True
930
1728
  info = _model_info(client, model)
931
1729
  if info is _NOT_LISTED:
932
1730
  # Asked and answered by a listing that did not name it.
@@ -960,16 +1758,16 @@ _google_budget_support: Optional[bool] = None
960
1758
  def _google_supports_thinking_budget() -> bool:
961
1759
  """Whether the INSTALLED google-genai can express a thinking budget.
962
1760
 
963
- Checked rather than assumed, and the check earns its keep: the pinned
964
- google-genai (1.0.0) ships a ThinkingConfig whose only field is
965
- `include_thoughts` -- `thinking_budget` arrived in a later release. So
966
- the obvious implementation, ThinkingConfig(thinking_budget=N), raises a
967
- TypeError against the version this repo actually pins.
1761
+ Checked rather than assumed, and the check earned its keep: the old pin,
1762
+ google-genai 1.0.0, shipped a ThinkingConfig whose only field was
1763
+ `include_thoughts`, so ThinkingConfig(thinking_budget=N) raised a
1764
+ TypeError and reasoning effort was unavailable on Google.
968
1765
 
969
- Rather than bump the pin (which would put §9's verified Google
970
- implementation back in play for a feature Google users can do without),
971
- reasoning effort is simply unavailable on Google until the pin moves.
972
- D22's rule, applied: check the dependency's real shape, don't assume it.
1766
+ The pin moved in batch 172 (ROADMAP_v3 §82, GS1) -- for thought
1767
+ signatures, not for effort -- and 2.29.0 carries the field, so this now
1768
+ answers True and Google effort is on. The check stays: it is still the
1769
+ dependency's real shape that decides, D22's rule, and a downgrade would
1770
+ turn effort off again rather than send a field the SDK cannot carry.
973
1771
  """
974
1772
  from google.genai import types as genai_types
975
1773
 
@@ -1011,7 +1809,21 @@ def _thinking_for_provider(provider_name: str, effort: Optional[str]) -> dict:
1011
1809
  Returned keys are merged into the provider's own kwargs by the caller;
1012
1810
  the Google entry is nested inside GenerateContentConfig rather than sent
1013
1811
  top-level, matching how that provider takes every other setting.
1812
+
1813
+ CHATGPT is the one provider that sends something with no effort set
1814
+ (ROADMAP_v3 §83, CG18). Every request asks for a reasoning summary and
1815
+ for the encrypted reasoning: both were accepted with and without an
1816
+ effort in slice 2a, the encrypted reasoning arrives unasked anyway, and
1817
+ asking says what this code relies on rather than leaning on a default.
1818
+ "auto" leaves the summary to the server, which wrote one on one of the
1819
+ two requests that asked.
1014
1820
  """
1821
+ if provider_name == "CHATGPT":
1822
+ reasoning = {"summary": "auto"}
1823
+ if effort:
1824
+ reasoning["effort"] = effort
1825
+ return {"reasoning": reasoning,
1826
+ "include": ["reasoning.encrypted_content"]}
1015
1827
  if not effort:
1016
1828
  return {}
1017
1829
  if provider_name == "ANTHROPIC":
@@ -1041,6 +1853,281 @@ def _thinking_for_provider(provider_name: str, effort: Optional[str]) -> dict:
1041
1853
  return {"reasoning_effort": effort}
1042
1854
 
1043
1855
 
1856
+ # ============================================================================
1857
+ # ---- The ChatGPT plan's Responses path (ROADMAP_v3 §83, slice 2) -----------
1858
+ # ============================================================================
1859
+ #
1860
+ # The one route the plan answers on is the Responses API (CG3), carried by the
1861
+ # openai SDK (CG21). Every shape below was measured on the owner's account in
1862
+ # slices 0 and 2a, except CG23's reasoning inside a tool loop, which is said
1863
+ # where it is sent (_chatgpt_reasoning).
1864
+
1865
+ class ChatGPTPlanError(RuntimeError):
1866
+ """A plan request OpenAI refused for a reason another attempt cannot fix
1867
+ (CG10). The message is the person's to read.
1868
+
1869
+ ITS OWN TYPE, WITH NO `status_code`, and that is what makes it permanent
1870
+ to `core/provider_errors.py`, which reads a failure by its class names
1871
+ and its `status_code` and `body` and finds none of them here. So the
1872
+ loop's generic 429 retry never sees a usage limit, which CG10 says is
1873
+ never retried.
1874
+ """
1875
+
1876
+
1877
+ class ChatGPTUsageLimit(ChatGPTPlanError):
1878
+ """The plan's usage limit. Its message LEADS with Manage usage (CG8)."""
1879
+
1880
+
1881
+ class ChatGPTNotEligible(ChatGPTPlanError):
1882
+ """This account cannot share its plan, or its sign-in was kept without
1883
+ the plan scope (CG6)."""
1884
+
1885
+
1886
+ class ChatGPTUnsupported(ChatGPTPlanError):
1887
+ """A field the plan does not support. The message names it, and the
1888
+ same request is not sent again (CG10)."""
1889
+
1890
+
1891
+ #: §83's adopted default: an ineligible account is told the whole story once
1892
+ #: per process and briefly after that.
1893
+ _ineligible_explained = False
1894
+
1895
+
1896
+ def _wire_dict(obj) -> dict:
1897
+ """An SDK object as the plain dict the wire spelled, `None` fields left
1898
+ out: what goes into §44's record and back unchanged, the shape slice 2a
1899
+ handed back."""
1900
+ if isinstance(obj, dict):
1901
+ return {k: v for k, v in obj.items() if v is not None}
1902
+ dump = getattr(obj, "model_dump", None)
1903
+ if callable(dump):
1904
+ return dump(exclude_none=True, mode="json")
1905
+ return {}
1906
+
1907
+
1908
+ def _chatgpt_fields(body) -> tuple:
1909
+ """(code, param, message) off an error body, whichever of OpenAI's two
1910
+ shapes it came in: the standard error object, or `{"detail": ...}`."""
1911
+ error = body.get("error") if isinstance(body, dict) and isinstance(
1912
+ body.get("error"), dict) else body
1913
+ if not isinstance(error, dict):
1914
+ return None, None, None
1915
+ return (error.get("code"), error.get("param"),
1916
+ error.get("message") or error.get("detail"))
1917
+
1918
+
1919
+ def _chatgpt_refusal(body) -> Optional[Exception]:
1920
+ """CG10's exception for a refusal its code names, or None.
1921
+
1922
+ None hands the failure back to `core/provider_errors.py`, as for every
1923
+ other provider: a 503, another 429 or a dropped connection is retried
1924
+ with the loop's bounded backoff, and anything else fails the turn.
1925
+ """
1926
+ global _ineligible_explained
1927
+ from core import chatgpt_auth
1928
+
1929
+ code, param, message = _chatgpt_fields(body)
1930
+ said = (f" OpenAI said: {' '.join(str(message).split())[:300]}"
1931
+ if message else "")
1932
+ if code == "subscription_sharing_usage_limit_exceeded":
1933
+ return ChatGPTUsageLimit(
1934
+ f"Manage usage: {chatgpt_auth.USAGE_URL} -- this ChatGPT plan "
1935
+ f"has reached its usage limit, so the request was not sent again. "
1936
+ f"Your next message tries once more.{said}")
1937
+ if code == "subscription_sharing_user_not_eligible":
1938
+ if _ineligible_explained:
1939
+ return ChatGPTNotEligible(
1940
+ "This ChatGPT account cannot share its plan (said above).")
1941
+ _ineligible_explained = True
1942
+ return ChatGPTNotEligible(
1943
+ f"This ChatGPT account cannot share its plan with an app, so "
1944
+ f"nothing was sent again and no new sign-in was asked for. Switch "
1945
+ f"with /model, or sign out with /disconnect chatgpt.{said}")
1946
+ if code == "subscription_sharing_unsupported_capability":
1947
+ return ChatGPTUnsupported(
1948
+ f"The ChatGPT plan does not support "
1949
+ f"{param or 'a field this request sent'}, so the request was not "
1950
+ f"sent again.{said}")
1951
+ return None
1952
+
1953
+
1954
+ def _chatgpt_open(client, request: dict):
1955
+ """The stream, opened with CG10's one forced refresh on a 401.
1956
+
1957
+ The SDK called `chatgpt_auth.access_token` for the token it sent and
1958
+ holds that token as `client.api_key`. `replacing=` refreshes only while
1959
+ the file still holds it, so two requests refused together spend one
1960
+ refresh between them. A refresh refused as unusable raises `SignedOut`
1961
+ with the session cleared; a second 401 after a good refresh says to sign
1962
+ in again and keeps the tokens, because only an unusable refresh clears
1963
+ them (CG10). A refresh that could not be made at all is a
1964
+ `ConnectionError`, which the loop retries like any dropped connection.
1965
+
1966
+ THE ONE RESEND THIS FILE MAKES, and it is not a retry in AGENTS.md's
1967
+ sense, which this file does not own: nothing was wrong with the request
1968
+ or the weather, the credential was stale, and the second attempt is made
1969
+ with a different one. Every retry for a failure the provider might not
1970
+ repeat is still `core/loop.py`'s, asked of `core/provider_errors.py`.
1971
+ """
1972
+ from core import chatgpt_auth
1973
+
1974
+ for attempt in (1, 2):
1975
+ try:
1976
+ return client.responses.create(**request)
1977
+ except chatgpt_auth.Transient as exc:
1978
+ raise ConnectionError(str(exc)) from exc
1979
+ except chatgpt_auth.ChatGPTAuthError:
1980
+ raise
1981
+ except Exception as exc:
1982
+ refusal = _chatgpt_refusal(getattr(exc, "body", None))
1983
+ if refusal is not None:
1984
+ raise refusal from exc
1985
+ if getattr(exc, "status_code", None) != 401:
1986
+ raise
1987
+ if attempt == 2:
1988
+ raise ChatGPTPlanError(
1989
+ "OpenAI refused the ChatGPT plan's sign-in again after a "
1990
+ "refresh. /connect chatgpt signs in again; the stored "
1991
+ "tokens are kept.") from exc
1992
+ try:
1993
+ chatgpt_auth.access_token(
1994
+ replacing=getattr(client, "api_key", None) or None)
1995
+ except chatgpt_auth.Transient as again:
1996
+ raise ConnectionError(str(again)) from again
1997
+ raise AssertionError("unreachable: the second attempt returns or raises")
1998
+
1999
+
2000
+ def _chatgpt_stream_failure(source) -> Exception:
2001
+ """An `error` event or a `response.failed` as an exception. Neither has
2002
+ been seen: nothing failed in slice 2a.
2003
+
2004
+ The openai SDK raises only on an event whose top-level `error` key is an
2005
+ object, which neither of these has, so they arrive as events and this
2006
+ reads them. CG10's type when the code names one; otherwise the rule
2007
+ `core/provider_errors.py` applies to a stream that failed after it
2008
+ opened, on every provider.
2009
+ """
2010
+ from core import provider_errors
2011
+
2012
+ body = _wire_dict(source)
2013
+ refusal = _chatgpt_refusal(body)
2014
+ if refusal is not None:
2015
+ return refusal
2016
+ code, _, message = _chatgpt_fields(body)
2017
+ text = (f"The ChatGPT plan's response failed ({code or 'no code'}): "
2018
+ f"{message or 'no message'}")
2019
+ if provider_errors.open_stream_error_is_transient(body):
2020
+ return ConnectionError(text)
2021
+ return ChatGPTPlanError(text)
2022
+
2023
+
2024
+ def _chatgpt_stream(client, model: str, items: list, system_prompt: str,
2025
+ tools, reasoning: dict, supports_stream_usage: bool):
2026
+ """The CHATGPT branch of `call_model_stream`.
2027
+
2028
+ DISPATCH IS ON `event.type` ALONE. The SDK parses an event it does not
2029
+ know as whichever class its union tries first: slice 2a's
2030
+ `response.metadata` came out as a `ResponseAudioDeltaEvent` whose `type`
2031
+ still said `response.metadata`. A class check would have read it as
2032
+ audio; the string is the one thing that is right.
2033
+
2034
+ Never sent: `temperature` (refused, slice 0), `previous_response_id`
2035
+ (the whole history goes in `input`), `service_tier`, a hosted tool, and
2036
+ the Codex instructions the model list carries (CG3).
2037
+ """
2038
+ from core import chatgpt_auth
2039
+
2040
+ state = chatgpt_auth.status()
2041
+ if state.signed_in and not state.plan_usage:
2042
+ raise ChatGPTNotEligible(
2043
+ "This ChatGPT sign-in did not share the plan: OpenAI did not "
2044
+ "grant chatgpt.tokens.use.direct, so nothing is sent on it (CG6). "
2045
+ "/connect chatgpt signs in again.")
2046
+ request = dict(model=model, instructions=system_prompt, input=items,
2047
+ store=False, stream=True,
2048
+ max_output_tokens=config.MAX_TOKENS)
2049
+ if tools:
2050
+ request["tools"] = tools
2051
+ request.update(reasoning)
2052
+
2053
+ stream = _chatgpt_open(client, request)
2054
+ text_parts: list = []
2055
+ pending_calls: list = []
2056
+ reasoning_items: list = []
2057
+ # None rather than 0, for the Google branch's reason (D21).
2058
+ usage = {"input_tokens": None, "output_tokens": None}
2059
+ finish_reason = None
2060
+ summary_started = False
2061
+ try:
2062
+ for event in stream:
2063
+ kind = getattr(event, "type", None)
2064
+ if kind == "response.output_text.delta":
2065
+ delta = getattr(event, "delta", None)
2066
+ if isinstance(delta, str) and delta:
2067
+ text_parts.append(delta)
2068
+ yield StreamToken(text_delta=delta)
2069
+ elif kind == "response.reasoning_summary_part.added":
2070
+ # §38: a summary in several parts reads as paragraphs.
2071
+ if summary_started:
2072
+ yield StreamToken(thinking_delta="\n\n")
2073
+ summary_started = True
2074
+ elif kind == "response.reasoning_summary_text.delta":
2075
+ delta = getattr(event, "delta", None)
2076
+ if isinstance(delta, str) and delta:
2077
+ yield StreamToken(thinking_delta=delta)
2078
+ elif kind == "response.output_item.done":
2079
+ item = getattr(event, "item", None)
2080
+ item_type = getattr(item, "type", None)
2081
+ if item_type == "function_call":
2082
+ pending_calls.append(item)
2083
+ elif item_type == "reasoning":
2084
+ reasoning_items.append(_wire_dict(item))
2085
+ elif kind in ("response.completed", "response.incomplete"):
2086
+ response = getattr(event, "response", None)
2087
+ usage_obj = getattr(response, "usage", None)
2088
+ for key in ("input_tokens", "output_tokens"):
2089
+ reported = _token_count(usage_obj, key)
2090
+ if reported is not None:
2091
+ usage[key] = reported
2092
+ details = getattr(response, "incomplete_details", None)
2093
+ if getattr(details, "reason", None) == "max_output_tokens":
2094
+ finish_reason = "length"
2095
+ elif kind == "response.failed":
2096
+ raise _chatgpt_stream_failure(
2097
+ getattr(getattr(event, "response", None), "error", None))
2098
+ elif kind == "error":
2099
+ raise _chatgpt_stream_failure(event)
2100
+ except Exception as exc:
2101
+ # An error the SDK raised mid-stream may still carry a code CG10
2102
+ # names; anything else, ours included, goes up as it is.
2103
+ refusal = _chatgpt_refusal(getattr(exc, "body", None))
2104
+ if refusal is None:
2105
+ raise
2106
+ raise refusal from exc
2107
+ finally:
2108
+ close = getattr(stream, "close", None)
2109
+ if callable(close):
2110
+ close()
2111
+
2112
+ usage = _stream_usage("CHATGPT", usage, supports_stream_usage)
2113
+ calls = []
2114
+ for item in pending_calls:
2115
+ name = getattr(item, "name", None) or ""
2116
+ parsed, parse_error = _tool_arguments(
2117
+ getattr(item, "arguments", None) or "", name, finish_reason)
2118
+ if parse_error is not None:
2119
+ logger.warning(
2120
+ "%s: %s (finish_reason=%r)", name, parse_error, finish_reason)
2121
+ calls.append(ToolCallRequest(
2122
+ # The id rule the other branches keep (batch 76).
2123
+ id=getattr(item, "call_id", None) or str(uuid4()),
2124
+ name=name, input=parsed, parse_error=parse_error))
2125
+ yield StreamToken(final_response=ModelResponse(
2126
+ text="".join(text_parts), tool_calls=calls, usage=usage,
2127
+ thinking=_thinking_record("CHATGPT", model, reasoning_items),
2128
+ ))
2129
+
2130
+
1044
2131
  # ============================================================================
1045
2132
  # ---- Streaming call + helpers -----------------------------------------------
1046
2133
  # ============================================================================
@@ -1061,10 +2148,11 @@ def call_model_stream(
1061
2148
  final_response for the terminal ModelResponse with accumulated tool
1062
2149
  calls and usage.
1063
2150
 
1064
- Three provider implementations — Anthropic typed events, Google
2151
+ Four provider implementations — Anthropic typed events, Google
1065
2152
  chunked candidates, OpenAI-compatible index-accumulated tool-call
1066
- fragments — which must agree with each other about the ModelResponse
1067
- they produce for the same inputs.
2153
+ fragments, and since ROADMAP_v3 §83 the ChatGPT plan's Responses events
2154
+ (_chatgpt_stream) — which must agree with each other about the
2155
+ ModelResponse they produce for the same inputs.
1068
2156
 
1069
2157
  There used to be a non-streaming `call_model()` beside this, and this
1070
2158
  docstring used to promise the two were identical. They were not: it
@@ -1098,10 +2186,20 @@ def call_model_stream(
1098
2186
  )
1099
2187
 
1100
2188
  translated_messages = _messages_for_provider(
1101
- provider_name, messages, model, echoes_reasoning)
1102
- sampling = _sampling_kwargs(provider_name, model, temperature)
2189
+ provider_name, messages, model, echoes_reasoning,
2190
+ images=accepts_images(provider_name))
2191
+ sampling = _sampling_kwargs(provider_name, model, temperature, effort)
1103
2192
  thinking = _thinking_for_provider(provider_name, effort)
1104
2193
 
2194
+ if provider_name == "CHATGPT":
2195
+ # ROADMAP_v3 §83. `sampling` is always {} here: _sampling_kwargs
2196
+ # drops a temperature for this provider, which the route refuses.
2197
+ yield from _chatgpt_stream(
2198
+ client, model, translated_messages, system_prompt,
2199
+ _tools_for_provider(provider_name, tool_schemas), thinking,
2200
+ supports_stream_usage)
2201
+ return
2202
+
1105
2203
  if provider_name == "ANTHROPIC":
1106
2204
  stream_kwargs = dict(
1107
2205
  model=model,
@@ -1125,8 +2223,8 @@ def call_model_stream(
1125
2223
  # ThinkingEvent(type="thinking", thinking=<delta>) -- which is
1126
2224
  # why this reads two attribute names rather than unpacking
1127
2225
  # content_block_delta by hand. Verified against the pinned
1128
- # anthropic==0.116.0 rather than assumed (D22), and pinned in
1129
- # tests/test_sdk_conformance.py.
2226
+ # anthropic (0.116.0, and 2.1.0 since batch 234) rather than
2227
+ # assumed (D22), and pinned in tests/test_sdk_conformance.py.
1130
2228
  #
1131
2229
  # Every other event kind is ignored on purpose: `text` is the
1132
2230
  # accumulated answer read off get_final_message() below, not
@@ -1158,31 +2256,31 @@ def call_model_stream(
1158
2256
  ]
1159
2257
  usage_obj = getattr(final_msg, "usage", None)
1160
2258
  usage = {
1161
- "input_tokens": getattr(usage_obj, "input_tokens", 0) if usage_obj else 0,
1162
- "output_tokens": getattr(usage_obj, "output_tokens", 0) if usage_obj else 0,
2259
+ "input_tokens": _token_count(usage_obj, "input_tokens"),
2260
+ "output_tokens": _token_count(usage_obj, "output_tokens"),
1163
2261
  }
1164
2262
 
1165
2263
  # D21 parity with the other two branches (audit #40). This branch
1166
2264
  # used to be exempt on the reasoning that get_final_message() always
1167
2265
  # carries usage -- which is very likely right for anthropic==0.116.0,
1168
2266
  # and is exactly the kind of assumption D21 exists because nobody can
1169
- # verify from inside. The getattr defaults above are the softened
1170
- # form D21's own note forbids, so on the DEFAULT provider an SDK
1171
- # field rename would have zeroed the budget silently, disabling both
1172
- # the budget stop condition and §21's compaction trigger. That is the
1173
- # mcp v1->v2 failure mode, and #35 is this file's live example of it.
2267
+ # verify from inside. The getattr defaults that used to be above were
2268
+ # the softened form D21's own note forbids, so on the DEFAULT
2269
+ # provider an SDK field rename would have zeroed the budget silently,
2270
+ # disabling both the budget stop condition and §21's compaction
2271
+ # trigger. That is the mcp v1->v2 failure mode, and #35 is this
2272
+ # file's live example of it.
1174
2273
  #
1175
2274
  # Honouring the flag rather than always raising keeps it meaning ONE
1176
2275
  # thing on all three providers, which is the coherence #40 is about;
1177
2276
  # providers.json.example ships it true for ANTHROPIC, so the default
1178
2277
  # posture is the checked one, and a proxy that genuinely omits usage
1179
2278
  # can still opt out instead of being unable to run.
1180
- if supports_stream_usage and usage["input_tokens"] == 0 and usage["output_tokens"] == 0:
1181
- raise RuntimeError(
1182
- f"Streaming call to {provider_name} completed without usage "
1183
- f"data despite supports_stream_usage=True. The stream may "
1184
- f"have been interrupted before the final usage chunk."
1185
- )
2279
+ #
2280
+ # Batch 147: the rule itself, and the coercion that makes `usage`
2281
+ # two ints, are in `_stream_usage` -- one copy for three branches
2282
+ # that carried three.
2283
+ usage = _stream_usage(provider_name, usage, supports_stream_usage)
1186
2284
 
1187
2285
  yield StreamToken(final_response=ModelResponse(
1188
2286
  text=text, tool_calls=calls, usage=usage,
@@ -1202,7 +2300,15 @@ def call_model_stream(
1202
2300
 
1203
2301
  text_parts = []
1204
2302
  calls = []
1205
- usage = {"input_tokens": 0, "output_tokens": 0}
2303
+ # §82 (GS2). One block per function call, read off the part that
2304
+ # carried the call: Gemini 3 refuses the next request unless each
2305
+ # call goes back with the signature it came with.
2306
+ signatures = []
2307
+ # None rather than 0: "not reported" has to stay distinguishable
2308
+ # from "reported as zero" until D21 has looked at it, or the
2309
+ # partial-report case the widened rule exists for arrives here
2310
+ # already coerced and indistinguishable (batch 147).
2311
+ usage = {"input_tokens": None, "output_tokens": None}
1206
2312
 
1207
2313
  for chunk in client.models.generate_content_stream(
1208
2314
  model=model,
@@ -1211,34 +2317,54 @@ def call_model_stream(
1211
2317
  ):
1212
2318
  candidate = chunk.candidates[0] if chunk.candidates else None
1213
2319
  content = candidate.content if candidate else None
1214
- parts = content.parts if content else []
2320
+ # `or []`: Content.parts is Optional on the pinned type, and a
2321
+ # chunk can carry a content with no parts at all.
2322
+ parts = (content.parts if content else None) or []
1215
2323
  for p in parts:
1216
2324
  if p.text is not None:
1217
2325
  text_parts.append(p.text)
1218
2326
  yield StreamToken(text_delta=p.text)
1219
2327
  if p.function_call is not None:
1220
2328
  fc = p.function_call
2329
+ call_id = fc.id or str(uuid4())
1221
2330
  calls.append(ToolCallRequest(
1222
- id=fc.id or str(uuid4()),
2331
+ id=call_id,
1223
2332
  name=fc.name,
1224
2333
  input=fc.args or {},
1225
2334
  ))
2335
+ # getattr: the field is on every pinned Part
2336
+ # (test_sdk_conformance asks the real one), but not on
2337
+ # the bare namespaces the stream tests build parts from.
2338
+ signatures.append(_google_signature_block(
2339
+ call_id, getattr(p, "thought_signature", None)))
2340
+ # PER FIELD, and only when the field was reported. The
2341
+ # guard used to be `if usage_meta:` -- which passes for a bare
2342
+ # instance, because every field is Optional and the object is
2343
+ # truthy anyway -- and then overwrote both counts with
2344
+ # whatever it found, None included. A chunk that carries
2345
+ # usage_metadata without a count now leaves an earlier chunk's
2346
+ # figure alone: reported beats unreported whatever the order,
2347
+ # which is the only rule that does not depend on which chunk
2348
+ # the provider chose to put the totals on.
1226
2349
  usage_meta = getattr(chunk, "usage_metadata", None)
1227
- if usage_meta:
1228
- usage["input_tokens"] = getattr(usage_meta, "prompt_token_count", 0)
1229
- usage["output_tokens"] = getattr(usage_meta, "candidates_token_count", 0)
1230
-
1231
- # D21 parity with the OpenAI branch: raise if marked as supporting
1232
- # stream usage but the stream completed with zero usage.
1233
- if supports_stream_usage and usage["input_tokens"] == 0 and usage["output_tokens"] == 0:
1234
- raise RuntimeError(
1235
- f"Streaming call to {provider_name} completed without usage "
1236
- f"data despite supports_stream_usage=True. The stream may "
1237
- f"have been interrupted before the final usage chunk."
1238
- )
2350
+ if usage_meta is not None:
2351
+ for key, attribute in (
2352
+ ("input_tokens", "prompt_token_count"),
2353
+ ("output_tokens", "candidates_token_count")):
2354
+ reported = _token_count(usage_meta, attribute)
2355
+ if reported is not None:
2356
+ usage[key] = reported
2357
+
2358
+ # D21, in the one place all three branches read it (batch 147).
2359
+ # This is the branch the nullable-field defect was found in: the
2360
+ # pinned google-genai declares every usage field Optional[int], so
2361
+ # `usage` could leave here holding None and the raise could not
2362
+ # fire -- `None != 0`.
2363
+ usage = _stream_usage(provider_name, usage, supports_stream_usage)
1239
2364
 
1240
2365
  yield StreamToken(final_response=ModelResponse(
1241
2366
  text="".join(text_parts), tool_calls=calls, usage=usage,
2367
+ thinking=_thinking_record(provider_name, model, signatures),
1242
2368
  ))
1243
2369
  return
1244
2370
 
@@ -1259,7 +2385,9 @@ def call_model_stream(
1259
2385
  text_parts = []
1260
2386
  reasoning_parts = []
1261
2387
  tool_fragments: dict[int, dict] = {}
1262
- usage = {"input_tokens": 0, "output_tokens": 0}
2388
+ # None, for the reason the Google branch states: D21 cannot tell an
2389
+ # unreported count from a zero one once it has been coerced.
2390
+ usage = {"input_tokens": None, "output_tokens": None}
1263
2391
 
1264
2392
  finish_reason = None
1265
2393
  for chunk in client.chat.completions.create(**openai_kwargs):
@@ -1315,20 +2443,23 @@ def call_model_stream(
1315
2443
  tool_fragments[idx]["arguments_parts"].append(
1316
2444
  tc_delta.function.arguments
1317
2445
  )
2446
+ # Per field and only when reported -- the Google branch's shape,
2447
+ # for its reason, and because `CompletionUsage` is parsed by the
2448
+ # SDK's construct() and so can hold None against an `int`
2449
+ # annotation (batch 147).
1318
2450
  usage_obj = getattr(chunk, "usage", None)
1319
- if usage_obj:
1320
- usage["input_tokens"] = getattr(usage_obj, "prompt_tokens", 0)
1321
- usage["output_tokens"] = getattr(usage_obj, "completion_tokens", 0)
2451
+ if usage_obj is not None:
2452
+ for key, attribute in (("input_tokens", "prompt_tokens"),
2453
+ ("output_tokens", "completion_tokens")):
2454
+ reported = _token_count(usage_obj, attribute)
2455
+ if reported is not None:
2456
+ usage[key] = reported
1322
2457
 
1323
2458
  # D21: if the provider is marked as supporting stream usage but we
1324
- # got zero usage, the stream was likely interrupted before the final
1325
- # usage chunk arrived. Raise rather than silently reporting zero.
1326
- if supports_stream_usage and usage["input_tokens"] == 0 and usage["output_tokens"] == 0:
1327
- raise RuntimeError(
1328
- f"Streaming call to {provider_name} completed without usage "
1329
- f"data despite supports_stream_usage=True. The stream may "
1330
- f"have been interrupted before the final usage chunk."
1331
- )
2459
+ # got no usable usage, the stream was likely interrupted before the
2460
+ # final usage chunk arrived. Raise rather than silently reporting zero.
2461
+ # One copy for three branches since batch 147.
2462
+ usage = _stream_usage(provider_name, usage, supports_stream_usage)
1332
2463
 
1333
2464
  calls = []
1334
2465
  for idx in sorted(tool_fragments):
@@ -1340,7 +2471,21 @@ def call_model_stream(
1340
2471
  logger.warning(
1341
2472
  "%s: %s (finish_reason=%r)", name, parse_error, finish_reason)
1342
2473
  calls.append(ToolCallRequest(
1343
- id=frag["id"], name=name, input=parsed, parse_error=parse_error,
2474
+ # `or str(uuid4())` for the Google branch's reason, which this
2475
+ # one was missing (batch 76). The fragment's id starts as ""
2476
+ # and is filled only if some delta carries one, so a provider
2477
+ # that streams parallel calls without ids hands back two
2478
+ # requests that are EQUAL on the field everything downstream
2479
+ # pairs by: the batch's own bookkeeping, `_spawn_threads` in
2480
+ # the TUI, and `parent_call_id` in storage. The generated id
2481
+ # is what goes back on the wire too, on BOTH halves of the
2482
+ # pair -- `add_assistant_message` persists the call and
2483
+ # `add_tool_result` answers it, so the two agree by
2484
+ # construction, which is all M4/D20 pairing needs. That is
2485
+ # also why "" was not the safer answer: a provider that
2486
+ # validates `tool_call_id` at all rejects an empty one.
2487
+ id=frag["id"] or str(uuid4()),
2488
+ name=name, input=parsed, parse_error=parse_error,
1344
2489
  ))
1345
2490
 
1346
2491
  # ONE block, not one per delta. There is no block structure on this
@@ -1416,13 +2561,15 @@ class EmbeddingResult:
1416
2561
  # ANTHROPIC IS NOT AN OVERSIGHT. There is no Anthropic embeddings API; the
1417
2562
  # recommended route is a third-party embedder, which this harness reaches
1418
2563
  # as its own provider entry. OPENROUTER proxies chat completions only.
2564
+ # CHATGPT is the plan, which answers on the Responses API and nothing else
2565
+ # (ROADMAP_v3 §83, CG3).
1419
2566
  #
1420
2567
  # INCOMPLETE ON PURPOSE, and safe when incomplete -- the same posture as
1421
2568
  # MODELS_REJECTING_SAMPLING_PARAMS. An unlisted provider that turns out to
1422
2569
  # have no endpoint fails loudly on the probe `/embedder` runs at selection
1423
2570
  # time, which is a measurement rather than a guess and is why there is no
1424
2571
  # `supports_embeddings` table to keep in step with the world.
1425
- PROVIDERS_WITHOUT_EMBEDDINGS = frozenset({"ANTHROPIC", "OPENROUTER"})
2572
+ PROVIDERS_WITHOUT_EMBEDDINGS = frozenset({"ANTHROPIC", "OPENROUTER", "CHATGPT"})
1426
2573
 
1427
2574
 
1428
2575
  def embed_texts(provider_name: str, model: str, texts,
@@ -1525,18 +2672,31 @@ def _embed_google(client, model, items, task_type):
1525
2672
 
1526
2673
 
1527
2674
  def _check_vectors(provider_name, model, vectors, expected) -> None:
1528
- """Three ways a successful response is still unusable.
2675
+ """Four ways a successful response is still unusable.
1529
2676
 
1530
- All three produce a WRONG NUMBER rather than an error downstream: a
1531
- short batch pairs texts with the wrong vectors, a ragged batch makes
2677
+ The first three produce a WRONG NUMBER rather than an error downstream:
2678
+ a short batch pairs texts with the wrong vectors, a ragged batch makes
1532
2679
  cosine compare different spaces, and an empty vector makes every
1533
2680
  cosine zero -- which reads as "no source supports this claim" and is
1534
2681
  indistinguishable from a real finding.
2682
+
2683
+ The fourth (batch 219, #218) is a component that is not a finite
2684
+ number. A string or None raised TypeError in `l2_normalize`, outside
2685
+ every handler, and flipped a run that had already grounded and scored
2686
+ to `status='failed'`; a NaN or an infinity raised nothing and scored
2687
+ NaN, the wrong-number class again. As an EmbeddingError it takes the
2688
+ path every other unusable response takes: the scorer gives up for the
2689
+ run and the stage falls back, with the cause in the trace.
1535
2690
  """
1536
2691
  if len(vectors) != expected:
1537
2692
  raise EmbeddingError(
1538
2693
  f"{provider_name} returned {len(vectors)} embedding(s) for "
1539
2694
  f"{expected} input(s) with model {model!r}.")
2695
+ if not all(isinstance(v, (list, tuple))
2696
+ and all(_is_finite_number(c) for c in v) for v in vectors):
2697
+ raise EmbeddingError(
2698
+ f"{provider_name} returned an embedding whose components are not "
2699
+ f"all finite numbers for model {model!r}.")
1540
2700
  dimensions = {len(v) for v in vectors}
1541
2701
  if len(dimensions) > 1:
1542
2702
  raise EmbeddingError(
@@ -1546,3 +2706,10 @@ def _check_vectors(provider_name, model, vectors, expected) -> None:
1546
2706
  raise EmbeddingError(
1547
2707
  f"{provider_name} returned empty embeddings for model "
1548
2708
  f"{model!r}.")
2709
+
2710
+
2711
+ def _is_finite_number(component) -> bool:
2712
+ """An int or a float, not a bool, and not NaN or an infinity."""
2713
+ return (isinstance(component, (int, float))
2714
+ and not isinstance(component, bool)
2715
+ and math.isfinite(component))