@bitkyc08/opencodex 2.99.0-preview.20261108 → 2.100.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (258) hide show
  1. package/AGENTS_INSTALL.md +19 -1
  2. package/README.md +37 -33
  3. package/SPONSORS.md +5 -1
  4. package/bin/ocx.mjs +1 -1
  5. package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
  6. package/gui/dist/assets/App-CitvEefy.js +59 -0
  7. package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
  8. package/gui/dist/assets/{brand-mark-DpiBmFGI.js → brand-mark-M92UnxX2.js} +1 -1
  9. package/gui/dist/assets/index-BP47do6-.js +94 -0
  10. package/gui/dist/assets/index-DmDooSAG.css +2 -0
  11. package/gui/dist/assets/{segment-thumb-features-JcszQ18T.js → segment-thumb-features-CcxZBnxv.js} +1 -1
  12. package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
  13. package/gui/dist/classic/assets/{Tray-5v86r9Az.js → Tray-D8jfUXpQ.js} +1 -1
  14. package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
  15. package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
  16. package/gui/dist/classic/index.html +1 -1
  17. package/gui/dist/index.html +2 -2
  18. package/native/ocx-child/Cargo.lock +215 -0
  19. package/native/ocx-child/Cargo.toml +31 -0
  20. package/native/ocx-child/src/cli.rs +1901 -0
  21. package/native/ocx-child/src/codex_home.rs +429 -0
  22. package/native/ocx-child/src/codex_settings.rs +231 -0
  23. package/native/ocx-child/src/daemon.rs +363 -0
  24. package/native/ocx-child/src/http_wire.rs +619 -0
  25. package/native/ocx-child/src/journal.rs +977 -0
  26. package/native/ocx-child/src/lib.rs +22 -0
  27. package/native/ocx-child/src/main.rs +9 -0
  28. package/native/ocx-child/src/parity.rs +102 -0
  29. package/native/ocx-child/src/platform.rs +1487 -0
  30. package/native/ocx-child/src/relay.rs +1288 -0
  31. package/native/ocx-child/src/relay_auth.rs +166 -0
  32. package/native/ocx-child/src/routes.rs +236 -0
  33. package/native/ocx-child/src/state.rs +190 -0
  34. package/native/ocx-child/src/test_hooks.rs +58 -0
  35. package/native/ocx-child/src/toml_surgeon.rs +1156 -0
  36. package/package.json +8 -2
  37. package/src/adapters/anthropic.ts +28 -2
  38. package/src/adapters/claude-cli/adapter.ts +5 -25
  39. package/src/adapters/claude-cli/instruction-images.ts +31 -0
  40. package/src/adapters/claude-cli/stable-replay.ts +42 -14
  41. package/src/adapters/claude-cli/usage-admission.ts +53 -64
  42. package/src/adapters/coding-agent/protocol.ts +1 -1
  43. package/src/adapters/devin/live-models.ts +3 -0
  44. package/src/adapters/google-http.ts +3 -0
  45. package/src/adapters/google-vertex-key-project.ts +10 -2
  46. package/src/adapters/google.ts +40 -8
  47. package/src/adapters/kiro/usage.ts +3 -13
  48. package/src/adapters/openai-responses/passthrough.ts +5 -2
  49. package/src/adapters/openai-responses/request-strips.ts +32 -0
  50. package/src/adapters/openai-responses/tool-schema.ts +9 -4
  51. package/src/chat/inbound.ts +1 -1
  52. package/src/claude/auth-detect.ts +26 -8
  53. package/src/claude/inbound-content-options.ts +18 -3
  54. package/src/claude/inbound.ts +5 -2
  55. package/src/cli/bundled-bun-diagnostic.ts +91 -0
  56. package/src/cli/capabilities-agents-routing.ts +2 -2
  57. package/src/cli/capabilities-observe-system.ts +60 -0
  58. package/src/cli/combo-input.ts +6 -0
  59. package/src/cli/combo-result.ts +1 -1
  60. package/src/cli/combo.ts +4 -0
  61. package/src/cli/config-command.ts +16 -2
  62. package/src/cli/connect.ts +12 -10
  63. package/src/cli/dispatch.ts +4 -0
  64. package/src/cli/doctor.ts +11 -0
  65. package/src/cli/help.ts +1 -0
  66. package/src/cli/init.ts +4 -0
  67. package/src/cli/link-enrollment.ts +152 -25
  68. package/src/cli/link-local-control.ts +49 -0
  69. package/src/cli/link.ts +37 -10
  70. package/src/cli/provider-lifecycle-runtime.ts +4 -2
  71. package/src/cli/registry.ts +13 -0
  72. package/src/cli/star-prompt.ts +2 -0
  73. package/src/cli/status.ts +1 -1
  74. package/src/cli/telemetry-profile.ts +206 -0
  75. package/src/cli/telemetry.ts +260 -0
  76. package/src/client/hub-relay.ts +26 -0
  77. package/src/client/machine-api.ts +3 -0
  78. package/src/clients/config-export/t3code.ts +44 -5
  79. package/src/clients/omo-role-models.ts +1 -1
  80. package/src/codex/catalog/bundled.ts +44 -2
  81. package/src/codex/runtime.ts +12 -0
  82. package/src/codex/shim-restore-lock.ts +67 -30
  83. package/src/combos/types.ts +21 -1
  84. package/src/config/atomic-write.ts +24 -1
  85. package/src/config/diagnostics.ts +15 -0
  86. package/src/config/live-reconcile.ts +17 -0
  87. package/src/config/load-degrade.ts +16 -0
  88. package/src/config/provider-validation.ts +29 -0
  89. package/src/config/schema/config-schema.ts +24 -0
  90. package/src/config/schema/leaf-validators.ts +17 -0
  91. package/src/config/telemetry-carry.ts +30 -0
  92. package/src/config.ts +13 -1
  93. package/src/generated/compatibility-version.json +405 -153
  94. package/src/generated/model-metadata.ts +3 -3
  95. package/src/integrations/aside-profiles.ts +7 -1
  96. package/src/integrations/owned-refresh.ts +9 -2
  97. package/src/lab/artifacts/secure-fs.ts +12 -11
  98. package/src/lab/fabric/scratch.ts +11 -9
  99. package/src/lab/public/community.ts +2 -3
  100. package/src/lab/public/file-safety.ts +18 -20
  101. package/src/lab/public/mutation-lock.ts +109 -337
  102. package/src/lab/public/private-file.ts +4 -3
  103. package/src/lib/bun-runtime-preflight.ts +1 -1
  104. package/src/lib/codex-catalog-read-only.ts +122 -0
  105. package/src/lib/config-ownership.ts +49 -18
  106. package/src/lib/errors.ts +11 -0
  107. package/src/lib/gcp-adc.ts +167 -48
  108. package/src/lib/jsonc.ts +4 -2
  109. package/src/lib/local-control-socket.ts +88 -0
  110. package/src/lib/privacy.ts +15 -0
  111. package/src/lib/provider-client-headers.ts +3 -0
  112. package/src/lib/token-estimate.ts +13 -2
  113. package/src/link/child-binary-source.ts +228 -0
  114. package/src/link/ephemeral-child.ts +45 -0
  115. package/src/link/remote-plant.ts +306 -0
  116. package/src/link/remote-windows.ts +59 -5
  117. package/src/link/ssh-argv.ts +71 -8
  118. package/src/link/status-projection.ts +2 -0
  119. package/src/link/store.ts +5 -1
  120. package/src/oauth/claude-keychain-read.ts +165 -0
  121. package/src/oauth/local-token-detect.ts +14 -17
  122. package/src/protocols/envelope.ts +20 -2
  123. package/src/providers/api-key-selection.ts +8 -2
  124. package/src/providers/claude-cli-usage-state.ts +93 -0
  125. package/src/providers/github-copilot-auto.ts +25 -5
  126. package/src/providers/meta-muse-plan-refusal.ts +94 -0
  127. package/src/providers/model-rename-fields.ts +3 -0
  128. package/src/providers/quota/vendor-probes-key.ts +23 -4
  129. package/src/providers/quota/vendor-probes-oauth.ts +59 -0
  130. package/src/providers/quota/workbuddy.ts +2 -2
  131. package/src/providers/quota.ts +25 -18
  132. package/src/providers/registry/entries-core.ts +7 -4
  133. package/src/providers/registry/entries-extended.ts +39 -7
  134. package/src/providers/registry/model-seeds.ts +10 -2
  135. package/src/providers/resolved-model-policy.ts +4 -0
  136. package/src/providers/routed-config.ts +30 -3
  137. package/src/providers/stale-no-vision-seed-migration.ts +7 -0
  138. package/src/providers/vertex-environment-authority.ts +50 -0
  139. package/src/providers/workbuddy-credit-balance.ts +38 -6
  140. package/src/quota-pool/assemble.ts +14 -6
  141. package/src/router.ts +1 -0
  142. package/src/server/adapter-resolve.ts +11 -6
  143. package/src/server/audio-dictation.ts +21 -7
  144. package/src/server/auth-cors.ts +19 -0
  145. package/src/server/background-lifecycle.ts +71 -0
  146. package/src/server/chat-completions.ts +2 -0
  147. package/src/server/claude-messages.ts +30 -3
  148. package/src/server/index/live-sideband.ts +15 -1
  149. package/src/server/index/local-control-listener.ts +515 -0
  150. package/src/server/index/optional-listeners.ts +21 -1
  151. package/src/server/index/serve-options.ts +4 -3
  152. package/src/server/index/websocket-handler.ts +2 -0
  153. package/src/server/index.ts +3 -0
  154. package/src/server/management/combo-routes.ts +6 -1
  155. package/src/server/management/context.ts +1 -0
  156. package/src/server/management/link-child-preflight.ts +74 -0
  157. package/src/server/management/link-enrollment.ts +190 -25
  158. package/src/server/management/link-route-state.ts +6 -1
  159. package/src/server/management/link-routes.ts +27 -9
  160. package/src/server/management/model-rows.ts +4 -0
  161. package/src/server/management/oauth-account-routes.ts +9 -4
  162. package/src/server/management/provider-overwrite-carry.ts +9 -2
  163. package/src/server/management/provider-routes.ts +14 -0
  164. package/src/server/management/provider-thinking-display-patch.ts +54 -0
  165. package/src/server/management/route-registry.ts +7 -0
  166. package/src/server/management/session-routes.ts +4 -1
  167. package/src/server/management/telemetry-routes.ts +37 -0
  168. package/src/server/management/usage-aggregate-cache.ts +290 -67
  169. package/src/server/management-api.ts +11 -2
  170. package/src/server/messages-native-preflight.ts +166 -0
  171. package/src/server/messages-native.ts +143 -43
  172. package/src/server/messages-shared.ts +14 -0
  173. package/src/server/proxy-liveness.ts +99 -6
  174. package/src/server/request-log-conversation.ts +18 -2
  175. package/src/server/request-log.ts +7 -2
  176. package/src/server/responses/adapter-continuation.ts +1 -0
  177. package/src/server/responses/adapter-dispatch.ts +11 -28
  178. package/src/server/responses/adapter-failure-message.ts +68 -0
  179. package/src/server/responses/combo-stream-preflight.ts +14 -2
  180. package/src/server/responses/core-auth.ts +26 -5
  181. package/src/server/responses/core-codex-account.ts +8 -1
  182. package/src/server/responses/core-combo-failure.ts +6 -1
  183. package/src/server/responses/core-combo-native.ts +150 -37
  184. package/src/server/responses/core-combo.ts +15 -9
  185. package/src/server/responses/core-normalize.ts +1 -0
  186. package/src/server/responses/core-options.ts +7 -1
  187. package/src/server/responses/core-pipeline.ts +21 -1
  188. package/src/server/responses/fetch-helpers.ts +5 -0
  189. package/src/server/responses/opengateway-completion-guard.ts +7 -3
  190. package/src/server/responses/passthrough-delivery.ts +8 -1
  191. package/src/server/responses/passthrough-error.ts +22 -0
  192. package/src/server/responses/request-prepare.ts +7 -1
  193. package/src/server/responses/request-transport.ts +28 -5
  194. package/src/server/responses/run-turn-execution.ts +13 -0
  195. package/src/server/sse-frame-buffer.ts +5 -0
  196. package/src/server/ws-bridge.ts +10 -0
  197. package/src/service/launchd.ts +78 -4
  198. package/src/telemetry/aggregate.ts +348 -0
  199. package/src/telemetry/consent.ts +128 -0
  200. package/src/telemetry/contract.ts +403 -0
  201. package/src/telemetry/disclosure.ts +19 -0
  202. package/src/telemetry/install-id.ts +226 -0
  203. package/src/telemetry/payload.ts +129 -0
  204. package/src/telemetry/profile/client.ts +150 -0
  205. package/src/telemetry/profile/contract.ts +288 -0
  206. package/src/telemetry/profile/disclosure.ts +14 -0
  207. package/src/telemetry/profile/paths.ts +13 -0
  208. package/src/telemetry/profile/routes.ts +99 -0
  209. package/src/telemetry/profile/service.ts +234 -0
  210. package/src/telemetry/profile/state.ts +189 -0
  211. package/src/telemetry/profile/sync.ts +276 -0
  212. package/src/telemetry/routes.ts +183 -0
  213. package/src/telemetry/scheduler.ts +386 -0
  214. package/src/telemetry/sender.ts +97 -0
  215. package/src/telemetry/state.ts +334 -0
  216. package/src/types/combo-jev-effort-mode.ts +11 -0
  217. package/src/types/config.ts +3 -0
  218. package/src/types/provider.ts +11 -1
  219. package/src/types/request.ts +2 -0
  220. package/src/types/telemetry.ts +15 -0
  221. package/src/types/wire.ts +10 -0
  222. package/src/types.ts +5 -0
  223. package/src/update/index.ts +7 -1
  224. package/src/update/minisign.d.mts +17 -0
  225. package/src/update/minisign.mjs +88 -0
  226. package/src/update/signed-supply.d.mts +59 -0
  227. package/src/update/signed-supply.mjs +289 -0
  228. package/src/update/trust-root.json +38 -0
  229. package/src/update/update-bounded-json.d.mts +4 -0
  230. package/src/update/update-bounded-json.mjs +113 -0
  231. package/src/update/update-bytes.d.mts +2 -0
  232. package/src/update/update-bytes.mjs +23 -0
  233. package/src/update/update-candidate-id.d.mts +2 -0
  234. package/src/update/update-candidate-id.mjs +23 -0
  235. package/src/update/update-checkpoint-codec.d.mts +33 -0
  236. package/src/update/update-checkpoint-codec.mjs +217 -0
  237. package/src/update/update-contracts.d.mts +24 -0
  238. package/src/update/update-contracts.mjs +207 -0
  239. package/src/update/update-signature-threshold.d.mts +12 -0
  240. package/src/update/update-signature-threshold.mjs +47 -0
  241. package/src/update/update-signed-version.d.mts +5 -0
  242. package/src/update/update-signed-version.mjs +25 -0
  243. package/src/update/update-trust-anchor.d.mts +2 -0
  244. package/src/update/update-trust-anchor.mjs +2 -0
  245. package/src/usage/cache-diagnostic.ts +195 -50
  246. package/src/usage/expected-prices.ts +20 -2
  247. package/src/usage/filter-index.ts +1469 -0
  248. package/src/usage/ledger-scanner.ts +245 -4
  249. package/src/usage/source-filter.ts +10 -2
  250. package/src/usage/summary.ts +16 -0
  251. package/src/web-search/parse.ts +244 -38
  252. package/gui/dist/assets/App-CWz0areF.js +0 -59
  253. package/gui/dist/assets/Tray-BosDoskm.js +0 -1
  254. package/gui/dist/assets/index-CAxjGWkm.js +0 -94
  255. package/gui/dist/assets/index-bnUWypyz.css +0 -2
  256. package/gui/dist/classic/assets/App-BOEsFmiN.js +0 -58
  257. package/gui/dist/classic/assets/index-ClZ_zZ77.js +0 -94
  258. package/gui/dist/classic/assets/quota-summary-xwvDtvu7.js +0 -1
@@ -27,6 +27,7 @@ interface OutputTextBlock {
27
27
  annotations?: AnnotationLike[];
28
28
  }
29
29
  interface OutputItem {
30
+ id?: string;
30
31
  type?: string;
31
32
  content?: OutputTextBlock[];
32
33
  }
@@ -34,53 +35,214 @@ interface OutputItem {
34
35
  * Completed output text and citations. `parts` holds each output_text block's text keyed by
35
36
  * `output_index:content_index`, and `incomplete` marks skipped malformed entries.
36
37
  */
37
- type CompletedOutput = WebSearchResult & { incomplete: boolean; parts: Map<string, string> };
38
+ type CompletedOutput = WebSearchResult & {
39
+ incomplete: boolean;
40
+ parts: Map<string, string>;
41
+ slots: Map<string, { itemId?: string; outputIndex: number; contentIndex: number; singleContent: boolean }>;
42
+ };
38
43
  /** Streamed text for one `output_text` content part. */
39
44
  interface StreamedPart {
45
+ key: string;
46
+ order: number;
40
47
  delta: string;
41
48
  done: string | null;
49
+ itemId?: string;
50
+ outputIndex?: number;
51
+ contentIndex?: number;
52
+ promotedFromContent?: boolean;
42
53
  }
43
54
 
44
55
  /** The streamed part for events that carry no valid `output_index`/`content_index`. */
45
56
  const UNINDEXED_PART = "unindexed";
46
57
 
58
+ // Identity buckets allow a unique compatible match without scanning prior parts on every frame.
59
+ type PartLookup = Map<string, Set<StreamedPart>>;
60
+
61
+ function partBucket(itemId: string, contentIndex: number | undefined, exact: boolean, unindexed: boolean, output?: number | null): string {
62
+ return JSON.stringify([itemId, contentIndex ?? null, exact, unindexed, output === undefined ? "any" : output]);
63
+ }
64
+
65
+ function indexPart(lookup: PartLookup, part: StreamedPart, add: boolean): void {
66
+ const positionBuckets = part.outputIndex !== undefined
67
+ ? [["output", part.outputIndex], ["output-content", part.outputIndex, part.contentIndex ?? null]]
68
+ : [["unpositioned-content", part.contentIndex ?? null]];
69
+ for (const positionBucket of positionBuckets) {
70
+ const key = JSON.stringify(positionBucket);
71
+ let bucket = lookup.get(key);
72
+ if (add) {
73
+ if (!bucket) { bucket = new Set(); lookup.set(key, bucket); }
74
+ bucket.add(part);
75
+ } else if (bucket) {
76
+ bucket.delete(part);
77
+ if (!bucket.size) lookup.delete(key);
78
+ }
79
+ }
80
+ if (!part.itemId) return;
81
+ for (const exact of [false, true]) {
82
+ for (const unindexed of isIndexedPartKey(part.key) ? [false] : [false, true]) {
83
+ for (const output of [undefined, part.outputIndex ?? null]) {
84
+ const key = partBucket(part.itemId, exact ? part.contentIndex : undefined, exact, unindexed, output);
85
+ let bucket = lookup.get(key);
86
+ if (add) {
87
+ if (!bucket) { bucket = new Set(); lookup.set(key, bucket); }
88
+ bucket.add(part);
89
+ } else if (bucket) {
90
+ bucket.delete(part);
91
+ if (!bucket.size) lookup.delete(key);
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
97
+
98
+ function identifiedPart(lookup: PartLookup, itemId: string, contentIndex: number | undefined, unindexed: boolean, outputIndex?: number): StreamedPart | undefined {
99
+ const outputs = outputIndex === undefined ? [undefined] : [outputIndex, null];
100
+ const buckets = outputs.flatMap(output => contentIndex === undefined
101
+ ? [lookup.get(partBucket(itemId, undefined, false, unindexed, output))]
102
+ : [lookup.get(partBucket(itemId, contentIndex, true, unindexed, output)), lookup.get(partBucket(itemId, undefined, true, unindexed, output))]);
103
+ if (buckets.reduce((count, bucket) => count + (bucket?.size ?? 0), 0) !== 1) return undefined;
104
+ return buckets.find(bucket => bucket?.size)?.values().next().value;
105
+ }
106
+
47
107
  /**
48
- * Key a streamed text event by its content part. An event without valid indexes joins the only
49
- * indexed part when exactly one exists. The first indexed event identifies any preceding
50
- * unindexed accumulator; once multiple indexed parts exist, unindexed text stays separate.
108
+ * Match explicit item identity before applying the legacy single-part fallback. Indexed parts
109
+ * retain their numeric keys so incomplete completed outputs can fill individual missing parts.
51
110
  */
52
- function streamedPartKey(data: Record<string, unknown>, parts: Map<string, StreamedPart>): string {
111
+ function streamedPartKey(data: Record<string, unknown>, parts: Map<string, StreamedPart>, lookup: PartLookup, nextOrder: number): string {
53
112
  const output = data.output_index;
54
113
  const content = data.content_index;
55
- if (Number.isInteger(output) && Number.isInteger(content)) {
56
- const key = `${output}:${content}`;
57
- if (parts.size === 1 && parts.has(UNINDEXED_PART)) {
58
- parts.set(key, parts.get(UNINDEXED_PART)!);
59
- parts.delete(UNINDEXED_PART);
114
+ const outputIndex = Number.isInteger(output) ? output as number : undefined;
115
+ const contentIndex = Number.isInteger(content) ? content as number : undefined;
116
+ const itemId = typeof data.item_id === "string" && data.item_id ? data.item_id : undefined;
117
+ const indexedKey = outputIndex !== undefined && contentIndex !== undefined ? `${outputIndex}:${contentIndex}` : undefined;
118
+ const outputOnly = outputIndex === undefined ? undefined : parts.get(`output:${outputIndex}`);
119
+ const compatible = (part: StreamedPart): boolean =>
120
+ (!itemId || !part.itemId || itemId === part.itemId)
121
+ && (outputIndex === undefined || part.outputIndex === undefined || outputIndex === part.outputIndex)
122
+ && (contentIndex === undefined || part.contentIndex === undefined || contentIndex === part.contentIndex);
123
+ const indexedPart = indexedKey ? parts.get(indexedKey) : undefined;
124
+ let match = itemId ? identifiedPart(lookup, itemId, contentIndex, !!indexedKey, outputIndex) : undefined;
125
+ if (!match && itemId && indexedPart?.itemId === itemId
126
+ && identifiedPart(lookup, itemId, contentIndex, false, outputIndex) === indexedPart) match = indexedPart;
127
+ if (match && !compatible(match)) match = undefined;
128
+ const outputBucket = outputIndex === undefined ? undefined : lookup.get(JSON.stringify(["output", outputIndex]));
129
+ const outputContentBucket = outputIndex === undefined || contentIndex === undefined ? undefined
130
+ : lookup.get(JSON.stringify(["output-content", outputIndex, contentIndex]));
131
+ const outputUnknownContentBucket = outputIndex === undefined ? undefined
132
+ : lookup.get(JSON.stringify(["output-content", outputIndex, null]));
133
+ const outputCandidates = contentIndex === undefined ? outputBucket?.size ?? 0
134
+ : (outputContentBucket?.size ?? 0) + (outputUnknownContentBucket?.size ?? 0);
135
+ // A linking snapshot can identify an anonymous position even when either frame omitted the
136
+ // content index. Only inspect the buckets when their total proves there are at most two parts.
137
+ if (data.type === "response.output_text.done" && itemId && match
138
+ && outputCandidates === (match.outputIndex === undefined ? 1 : 2)) {
139
+ const buckets = contentIndex === undefined ? [outputBucket] : [outputContentBucket, outputUnknownContentBucket];
140
+ const anonymous = buckets.flatMap(bucket => bucket ? [...bucket] : [])
141
+ .find(part => part !== match && !part.itemId && compatible(part));
142
+ if (anonymous) {
143
+ indexPart(lookup, match, false);
144
+ parts.delete(match.key);
145
+ return anonymous.key;
146
+ }
147
+ }
148
+ if (indexedPart && compatible(indexedPart)) {
149
+ if (match && match !== indexedPart) return match.key;
150
+ if ((itemId && indexedPart.itemId === itemId) || outputCandidates === 1) return indexedKey!;
151
+ }
152
+ if (outputCandidates > 1 && (indexedPart || outputOnly) && !match) return `conflict:${nextOrder}`;
153
+ if (!match && outputOnly && compatible(outputOnly) && outputCandidates === 1) match = outputOnly;
154
+ if (!match && outputIndex !== undefined && contentIndex === undefined) {
155
+ const only = outputBucket?.size === 1 ? outputBucket.values().next().value : undefined;
156
+ if (only && compatible(only)) match = only;
157
+ }
158
+ if (!match && !itemId && indexedKey && !indexedPart && outputCandidates === 0) {
159
+ const bucket = lookup.get(JSON.stringify(["unpositioned-content", contentIndex]));
160
+ const unknown = lookup.get(JSON.stringify(["unpositioned-content", null]));
161
+ if ((bucket?.size ?? 0) + (unknown?.size ?? 0) === 1) {
162
+ const candidate = (bucket ?? unknown)?.values().next().value;
163
+ if (candidate?.contentIndex !== undefined || candidate?.itemId || parts.size === 1) match = candidate;
164
+ }
165
+ }
166
+ // A content-only part can acquire its item or output identity after other parts arrived.
167
+ const contentOnly = contentIndex === undefined ? undefined : parts.get(`content:${contentIndex}`);
168
+ if (!match && (itemId || !indexedKey) && contentOnly && compatible(contentOnly)) match = contentOnly;
169
+ if (!match && parts.size === 1) {
170
+ const only = parts.values().next().value;
171
+ if (only && compatible(only) && (!indexedKey || !isIndexedPartKey(only.key))
172
+ && (!itemId || !only.itemId)) match = only;
173
+ }
174
+ if (match) {
175
+ const mergedOutput = outputIndex ?? match.outputIndex;
176
+ const mergedContent = contentIndex ?? match.contentIndex;
177
+ const mergedKey = mergedOutput === undefined ? undefined : mergedContent === undefined
178
+ ? `output:${mergedOutput}` : `${mergedOutput}:${mergedContent}`;
179
+ if (mergedKey && !parts.has(mergedKey) && match.key !== mergedKey) {
180
+ indexPart(lookup, match, false);
181
+ parts.delete(match.key);
182
+ if (match.key.startsWith("content:")) match.promotedFromContent = true;
183
+ match.key = mergedKey;
184
+ parts.set(mergedKey, match);
185
+ indexPart(lookup, match, true);
186
+ return mergedKey;
60
187
  }
61
- return key;
188
+ return match.key;
62
189
  }
63
- const indexed = [...parts.keys()].filter(key => key !== UNINDEXED_PART);
64
- return indexed.length === 1 ? indexed[0]! : UNINDEXED_PART;
190
+ if (indexedKey && !indexedPart) return indexedKey;
191
+ const key = outputIndex !== undefined && !outputOnly ? `output:${outputIndex}`
192
+ : itemId ? `item:${JSON.stringify([itemId, contentIndex ?? null])}`
193
+ : contentIndex !== undefined ? `content:${contentIndex}` : UNINDEXED_PART;
194
+ const existing = parts.get(key);
195
+ return !existing || compatible(existing) ? key : `conflict:${nextOrder}`;
65
196
  }
66
197
 
67
- /** Order `output_index:content_index` keys numerically. */
68
- function comparePartKeys(left: string, right: string): number {
69
- const [leftOutput = 0, leftContent = 0] = left.split(":").map(Number);
70
- const [rightOutput = 0, rightContent = 0] = right.split(":").map(Number);
71
- return leftOutput - rightOutput || leftContent - rightContent;
198
+ function isIndexedPartKey(key: string): boolean {
199
+ return key !== UNINDEXED_PART && !key.startsWith("item:") && !key.startsWith("content:") && !key.startsWith("output:") && !key.startsWith("conflict:");
200
+ }
201
+
202
+ /** Order known output indexes numerically; an unknown content index follows known siblings. */
203
+ function comparePartKeys(left: string, right: string, streamed: Map<string, StreamedPart>): number {
204
+ const position = (key: string): [number, number] => {
205
+ const part = streamed.get(key);
206
+ if (part?.outputIndex !== undefined) return [part.outputIndex, part.contentIndex ?? Infinity];
207
+ return key.split(":").map(Number) as [number, number];
208
+ };
209
+ const [leftOutput, leftContent] = position(left);
210
+ const [rightOutput, rightContent] = position(right);
211
+ return leftOutput - rightOutput || leftContent - rightContent
212
+ || (streamed.get(left)?.order ?? -1) - (streamed.get(right)?.order ?? -1);
72
213
  }
73
214
 
74
215
  /** One part's streamed text: its done text when present, otherwise its deltas. */
75
216
  function streamedPartText(part: StreamedPart): string {
76
- return part.done?.trim() ? part.done : part.delta;
217
+ return part.done ?? part.delta;
77
218
  }
78
219
 
79
- /** Join indexed parts in index order, followed by any unindexed text. */
220
+ /** Keep earlier content-only siblings before a part promoted to an indexed identity. */
221
+ function orderedPartKeys(completed: Iterable<string>, streamed: Map<string, StreamedPart>): string[] {
222
+ const ordered = (key: string): boolean => isIndexedPartKey(key) || streamed.get(key)?.outputIndex !== undefined;
223
+ const indexed = [...new Set([...completed, ...streamed.keys()].filter(ordered))]
224
+ .sort((left, right) => comparePartKeys(left, right, streamed));
225
+ const contentOnly = [...streamed].filter(([key]) => key.startsWith("content:") && !ordered(key))
226
+ .sort((a, b) => a[1].order - b[1].order);
227
+ const keys: string[] = [];
228
+ let next = 0;
229
+ for (const key of indexed) {
230
+ const part = streamed.get(key);
231
+ if (part?.promotedFromContent) {
232
+ while (next < contentOnly.length && contentOnly[next]![1].order < part.order) keys.push(contentOnly[next++]![0]);
233
+ }
234
+ keys.push(key);
235
+ }
236
+ const emitted = new Set(contentOnly.slice(0, next).map(([key]) => key));
237
+ const remaining = [...streamed].filter(([key]) => !ordered(key) && !emitted.has(key));
238
+ remaining.sort((a, b) => a[1].order - b[1].order);
239
+ keys.push(...remaining.map(([key]) => key));
240
+ return keys;
241
+ }
242
+
243
+ /** Join parts with known output indexes in order, then other parts in arrival order. */
80
244
  function assembleStreamedText(parts: Map<string, StreamedPart>): string {
81
- const keys = [...parts.keys()].filter(key => key !== UNINDEXED_PART).sort(comparePartKeys);
82
- if (parts.has(UNINDEXED_PART)) keys.push(UNINDEXED_PART);
83
- return keys.map(key => streamedPartText(parts.get(key)!)).join("");
245
+ return orderedPartKeys([], parts).map(key => streamedPartText(parts.get(key)!)).join("");
84
246
  }
85
247
 
86
248
  /**
@@ -88,7 +250,7 @@ function assembleStreamedText(parts: Map<string, StreamedPart>): string {
88
250
  * completed output has it, and otherwise takes that part's streamed text.
89
251
  */
90
252
  function mergeIncompleteOutput(completed: Map<string, string>, streamed: Map<string, StreamedPart>): string {
91
- const keys = [...new Set([...completed.keys(), ...streamed.keys()])].sort(comparePartKeys);
253
+ const keys = orderedPartKeys(completed.keys(), streamed);
92
254
  return keys.map(key => {
93
255
  const text = completed.get(key);
94
256
  if (text?.trim()) return text;
@@ -97,6 +259,38 @@ function mergeIncompleteOutput(completed: Map<string, string>, streamed: Map<str
97
259
  }).join("");
98
260
  }
99
261
 
262
+ /** Match completed slots to unique compatible streamed parts before filling missing output. */
263
+ function reconcileCompletedParts(final: CompletedOutput, streamed: Map<string, StreamedPart>, lookup: PartLookup): Map<string, StreamedPart> {
264
+ const reconciled = new Map(streamed);
265
+ const matched = new Set<StreamedPart>();
266
+ for (const [key, identity] of final.slots) {
267
+ const existing = reconciled.get(key);
268
+ let part = identity.itemId ? identifiedPart(lookup, identity.itemId, identity.contentIndex, true) : undefined;
269
+ if (existing) {
270
+ const atContent = lookup.get(JSON.stringify(["output-content", identity.outputIndex, identity.contentIndex]));
271
+ const atUnknownContent = lookup.get(JSON.stringify(["output-content", identity.outputIndex, null]));
272
+ const ambiguous = !identity.itemId && (atContent?.size ?? 0) + (atUnknownContent?.size ?? 0) > 1;
273
+ const different = identity.itemId && existing.itemId && identity.itemId !== existing.itemId;
274
+ const otherIdentified = identity.itemId && !existing.itemId && part && part !== existing;
275
+ if (!ambiguous && !different && !otherIdentified) continue;
276
+ // Preserve a conflicting or ambiguous stream beside the completed slot.
277
+ reconciled.delete(key);
278
+ reconciled.set(`conflict:completed:${existing.order}`, existing);
279
+ }
280
+ if (!part && identity.singleContent) {
281
+ const bucket = lookup.get(JSON.stringify(["output", identity.outputIndex]));
282
+ const candidate = bucket?.size === 1 ? bucket.values().next().value : undefined;
283
+ if (candidate?.key.startsWith("output:") && (!identity.itemId || !candidate.itemId || identity.itemId === candidate.itemId)) part = candidate;
284
+ }
285
+ if (part?.outputIndex !== undefined && part.outputIndex !== identity.outputIndex) continue;
286
+ if (!part || matched.has(part) || (part.contentIndex === undefined && !identity.singleContent)) continue;
287
+ matched.add(part);
288
+ reconciled.delete(part.key);
289
+ reconciled.set(key, { ...part, key });
290
+ }
291
+ return reconciled;
292
+ }
293
+
100
294
  // Keep this compatibility export for non-Responses sidecar executors and bounded error bodies.
101
295
  export const MAX_SIDECAR_RESPONSE_BYTES = 64 * 1024;
102
296
  // Responses SSE can spend far more wire bytes on JSON framing than on useful model output. Keep a
@@ -315,6 +509,7 @@ function fromOutputArray(output: OutputItem[], seen: Set<string>): CompletedOutp
315
509
  let text = "";
316
510
  let incomplete = false;
317
511
  const parts = new Map<string, string>();
512
+ const slots: CompletedOutput["slots"] = new Map();
318
513
  const sources: WebSearchSource[] = [];
319
514
  for (const [outputIndex, item] of output.entries()) {
320
515
  if (!item || typeof item !== "object") { incomplete = true; continue; }
@@ -323,14 +518,17 @@ function fromOutputArray(output: OutputItem[], seen: Set<string>): CompletedOutp
323
518
  for (const [contentIndex, block] of item.content.entries()) {
324
519
  if (!block || typeof block !== "object") { incomplete = true; continue; }
325
520
  if (block.type === "output_text") {
521
+ const key = `${outputIndex}:${contentIndex}`;
522
+ slots.set(key, { itemId: typeof item.id === "string" && item.id ? item.id : undefined,
523
+ outputIndex, contentIndex, singleContent: item.content.length === 1 });
326
524
  if (typeof block.text !== "string") { incomplete = true; continue; }
327
525
  text += block.text;
328
- parts.set(`${outputIndex}:${contentIndex}`, block.text);
526
+ parts.set(key, block.text);
329
527
  for (const ann of Array.isArray(block.annotations) ? block.annotations : []) collectAnnotation(ann, sources, seen);
330
528
  }
331
529
  }
332
530
  }
333
- return { text, sources, incomplete, parts };
531
+ return { text, sources, incomplete, parts, slots };
334
532
  }
335
533
 
336
534
  export function cancelReaderWithoutWaiting(
@@ -358,6 +556,8 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
358
556
  let buffer = "";
359
557
  let responseBytes = 0;
360
558
  const seen = new Set<string>();
559
+ const partLookup: PartLookup = new Map();
560
+ let nextPartOrder = 0;
361
561
  // Holder object — fields are mutated inside the closure, so they can't live as narrowed locals.
362
562
  const acc: {
363
563
  parts: Map<string, StreamedPart>;
@@ -379,9 +579,14 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
379
579
 
380
580
  /** The accumulator for the content part that `data` belongs to. */
381
581
  const streamedPart = (data: Record<string, unknown>): StreamedPart => {
382
- const key = streamedPartKey(data, acc.parts);
582
+ const key = streamedPartKey(data, acc.parts, partLookup, nextPartOrder);
383
583
  let part = acc.parts.get(key);
384
- if (!part) { part = { delta: "", done: null }; acc.parts.set(key, part); }
584
+ if (!part) { part = { key, order: nextPartOrder++, delta: "", done: null }; acc.parts.set(key, part); }
585
+ indexPart(partLookup, part, false);
586
+ if (typeof data.item_id === "string" && data.item_id) part.itemId = data.item_id;
587
+ if (Number.isInteger(data.output_index)) part.outputIndex = data.output_index as number;
588
+ if (Number.isInteger(data.content_index)) part.contentIndex = data.content_index as number;
589
+ indexPart(partLookup, part, true);
385
590
  return part;
386
591
  };
387
592
 
@@ -396,7 +601,7 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
396
601
  };
397
602
 
398
603
  const handle = (payload: string): void => {
399
- if (!payload) return;
604
+ if (!payload || acc.terminalEvent) return;
400
605
  if (payload === "[DONE]") {
401
606
  acc.terminalEvent = true;
402
607
  return;
@@ -424,15 +629,12 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
424
629
  if (acceptDecodedChars(data.delta.length)) streamedPart(data).delta += data.delta;
425
630
  } else if (type === "response.output_text.done" && typeof data.text === "string") {
426
631
  // The `done` event carries the full, authoritative text for one content part.
427
- if (acceptDecodedChars(data.text.length)) {
428
- const part = streamedPart(data);
429
- part.done = (part.done ?? "") + data.text;
430
- }
632
+ if (acceptDecodedChars(data.text.length)) streamedPart(data).done = data.text;
431
633
  } else if (type === "response.completed" || type === "response.done") {
432
634
  acc.terminalEvent = true;
433
635
  const resp = data.response as { output?: OutputItem[] } | undefined;
434
636
  if (Array.isArray(resp?.output)) {
435
- const final = fromOutputArray(resp.output, seen);
637
+ const final = fromOutputArray(resp.output, new Set<string>());
436
638
  if (acceptDecodedChars(final.text.length)) acc.final = final;
437
639
  }
438
640
  } else if (type === "response.failed" || type === "response.incomplete" || type === "error") {
@@ -500,17 +702,21 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
500
702
 
501
703
  // Prefer the authoritative completed output[], then the done text, then accumulated deltas.
502
704
  // Each streamed content part contributes its done text, or its deltas when the done event never
503
- // arrived. A completed output that skipped malformed entries may be partial: indexed streams fill
504
- // only the parts it lacks, while an unindexed stream replaces it as a whole.
705
+ // arrived. A completed output that skipped malformed entries may be partial: identified parts
706
+ // can be matched by item ID, and indexed streams fill only the parts that output lacks.
505
707
  const streamedText = assembleStreamedText(acc.parts);
506
708
  const completedText = acc.final?.text.trim() ? acc.final.text : "";
507
709
  let text = completedText || streamedText;
508
710
  if (acc.final?.incomplete) {
509
- const repaired = acc.parts.has(UNINDEXED_PART) ? streamedText : mergeIncompleteOutput(acc.final.parts, acc.parts);
711
+ const reconciled = reconcileCompletedParts(acc.final, acc.parts, partLookup);
712
+ const repaired = [...reconciled].some(([key, part]) => isIndexedPartKey(key) || part.outputIndex !== undefined)
713
+ ? mergeIncompleteOutput(acc.final.parts, reconciled) : streamedText;
510
714
  if (repaired.trim()) text = repaired;
511
715
  }
512
716
  // Merge sources from the final output[] and the streaming annotation events.
513
- const sources = safeWebSearchSources(acc.final?.sources ?? []);
717
+ const finalSources = acc.final?.sources.map(source => source.title !== undefined ? source
718
+ : acc.streamSources.find(streamed => streamed.url === source.url) ?? source);
719
+ const sources = safeWebSearchSources(finalSources ?? []);
514
720
  for (const s of acc.streamSources) {
515
721
  appendSafeWebSearchSource(sources, s);
516
722
  }