GeezerVison is showing github.com — fix(server): continue a provider session that already holds its histo… · patroza/t3code@5284632
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts +123Lines changed: 123 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import type { ProviderAdapterV2HistoricalContext } from "./ProviderAdapter.ts"; | 2 | 2 | import { assert, describe, it } from "@effect/vitest"; | 3 | 3 | import { | 4 | 4 | ContextHandoffId, | | 5 | + MessageId, | 5 | 6 | PROVIDER_SEND_TURN_MAX_ATTACHMENTS, | 6 | 7 | ProviderDriverKind, | 7 | 8 | ProviderInstanceId, | @@ -17,7 +18,10 @@ import * as DateTime from "effect/DateTime"; | 17 | 18 | import * as Effect from "effect/Effect"; | 18 | 19 | import * as Schema from "effect/Schema"; | 19 | 20 | import { | | 21 | + completedCompactionRunOrdinal, | 20 | 22 | contextUsageForHandoff, | | 23 | + countsTowardNativeContextEstimate, | | 24 | + handoffAlreadyInNativeSession, | 21 | 25 | handoffBudget, | 22 | 26 | historyCost, | 23 | 27 | historyResponseItems, | @@ -412,6 +416,125 @@ describe("handoff budget", () => { | 412 | 416 | }); | 413 | 417 | }); | 414 | 418 |
| | 419 | +describe("native session already holds the history", () => { | | 420 | + const compactMessageId = MessageId.make("message:compact"); | | 421 | + const providerThreadId = ProviderThreadId.make("provider-thread:grok"); | | 422 | + const delivered = { | | 423 | + ...handoff, | | 424 | + strategy: "manual_context" as const, | | 425 | + fromProviderThreadIds: [], | | 426 | + toProviderThreadId: providerThreadId, | | 427 | + coveredRunOrdinals: { from: 1, to: 1 }, | | 428 | + delivery: { | | 429 | + nativeThreadId: "native:grok", | | 430 | + status: "inline" as const, | | 431 | + itemIds: [], | | 432 | + }, | | 433 | + }; | | 434 | + | | 435 | + it("keeps a completed compact and ignores a later copy of a delivered import", () => { | | 436 | + const ordinal = completedCompactionRunOrdinal({ | | 437 | + providerThreadId, | | 438 | + compactUserMessageIds: new Set([compactMessageId]), | | 439 | + runs: [ | | 440 | + { | | 441 | + ordinal: 5, | | 442 | + status: "failed", | | 443 | + providerThreadId, | | 444 | + userMessageId: MessageId.make("message:failed"), | | 445 | + }, | | 446 | + { | | 447 | + ordinal: 6, | | 448 | + status: "completed", | | 449 | + providerThreadId, | | 450 | + userMessageId: compactMessageId, | | 451 | + }, | | 452 | + { | | 453 | + ordinal: 7, | | 454 | + status: "completed", | | 455 | + providerThreadId: ProviderThreadId.make("provider-thread:other"), | | 456 | + userMessageId: compactMessageId, | | 457 | + }, | | 458 | + ], | | 459 | + }); | | 460 | + assert.equal(ordinal, 6); | | 461 | + assert.isFalse( | | 462 | + countsTowardNativeContextEstimate({ | | 463 | + compactionRunOrdinal: ordinal, | | 464 | + itemRunOrdinal: undefined, | | 465 | + }), | | 466 | + ); | | 467 | + assert.isFalse( | | 468 | + countsTowardNativeContextEstimate({ compactionRunOrdinal: ordinal, itemRunOrdinal: 6 }), | | 469 | + ); | | 470 | + assert.isTrue( | | 471 | + countsTowardNativeContextEstimate({ compactionRunOrdinal: ordinal, itemRunOrdinal: 8 }), | | 472 | + ); | | 473 | + assert.isTrue( | | 474 | + countsTowardNativeContextEstimate({ | | 475 | + compactionRunOrdinal: undefined, | | 476 | + itemRunOrdinal: undefined, | | 477 | + }), | | 478 | + ); | | 479 | + assert.equal( | | 480 | + handoffBudget({ | | 481 | + tokenCap: 16_000, | | 482 | + userText: "update to latest again", | | 483 | + attachments: [], | | 484 | + providerThread, | | 485 | + nativeContextEstimate: 795_175, | | 486 | + }), | | 487 | + 0, | | 488 | + ); | | 489 | + assert.isAbove( | | 490 | + handoffBudget({ | | 491 | + tokenCap: 16_000, | | 492 | + userText: "update to latest again", | | 493 | + attachments: [], | | 494 | + providerThread, | | 495 | + nativeContextEstimate: 0, | | 496 | + modelContextWindow: 256_000, | | 497 | + }), | | 498 | + 0, | | 499 | + ); | | 500 | + | | 501 | + const duplicate = { ...delivered, delivery: undefined }; | | 502 | + assert.isTrue( | | 503 | + handoffAlreadyInNativeSession({ | | 504 | + handoff: duplicate, | | 505 | + nativeThreadId: "native:grok", | | 506 | + sameNativeThread: true, | | 507 | + delivered: [delivered], | | 508 | + }), | | 509 | + ); | | 510 | + // `/compact` defers an undelivered handoff for the next ordinary turn. | | 511 | + assert.isFalse( | | 512 | + handoffAlreadyInNativeSession({ | | 513 | + handoff: { ...delivered, coveredRunOrdinals: { from: 1, to: 6 } }, | | 514 | + nativeThreadId: "native:grok", | | 515 | + sameNativeThread: true, | | 516 | + delivered: [], | | 517 | + }), | | 518 | + ); | | 519 | + assert.isFalse( | | 520 | + handoffAlreadyInNativeSession({ | | 521 | + handoff: { ...handoff, coveredRunOrdinals: { from: 1, to: 8 } }, | | 522 | + nativeThreadId: "native:grok", | | 523 | + sameNativeThread: true, | | 524 | + delivered: [delivered], | | 525 | + }), | | 526 | + ); | | 527 | + assert.isFalse( | | 528 | + handoffAlreadyInNativeSession({ | | 529 | + handoff: duplicate, | | 530 | + nativeThreadId: "native:replacement", | | 531 | + sameNativeThread: false, | | 532 | + delivered: [delivered], | | 533 | + }), | | 534 | + ); | | 535 | + }); | | 536 | +}); | | 537 | + | 415 | 538 | describe("handoff delivery", () => { | 416 | 539 | it.effect.each([ | 417 | 540 | { native: true, label: "injected" }, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ContextHandoffBudget.ts +85Lines changed: 85 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,9 @@ import type { | 6 | 6 | OrchestrationV2ContextHandoff, | 7 | 7 | OrchestrationV2HistoricalMessage, | 8 | 8 | OrchestrationV2ProviderThread, | | 9 | + OrchestrationV2Run, | 9 | 10 | OrchestrationV2TurnItem, | | 11 | + ProviderThreadId, | 10 | 12 | } from "@t3tools/contracts"; | 11 | 13 |
| 12 | 14 | import * as Config from "effect/Config"; | @@ -91,6 +93,89 @@ export function contextUsageForHandoff(input: { | 91 | 93 | }; | 92 | 94 | } | 93 | 95 |
| | 96 | +/** | | 97 | + * Latest completed native `/compact` on this provider thread. Its summary | | 98 | + * replaces the earlier transcript, so later handoffs must not replay it. | | 99 | + */ | | 100 | +export function completedCompactionRunOrdinal(input: { | | 101 | + readonly runs: ReadonlyArray< | | 102 | + Pick<OrchestrationV2Run, "ordinal" | "status" | "providerThreadId" | "userMessageId"> | | 103 | + >; | | 104 | + readonly providerThreadId: ProviderThreadId; | | 105 | + readonly compactUserMessageIds: ReadonlySet<OrchestrationV2Run["userMessageId"]>; | | 106 | +}): number | undefined { | | 107 | + let ordinal: number | undefined; | | 108 | + for (const run of input.runs) { | | 109 | + if ( | | 110 | + run.status === "completed" && | | 111 | + run.providerThreadId === input.providerThreadId && | | 112 | + input.compactUserMessageIds.has(run.userMessageId) && | | 113 | + (ordinal === undefined || run.ordinal > ordinal) | | 114 | + ) { | | 115 | + ordinal = run.ordinal; | | 116 | + } | | 117 | + } | | 118 | + return ordinal; | | 119 | +} | | 120 | + | | 121 | +/** | | 122 | + * A completed compact folds imported rows and earlier runs into the native | | 123 | + * summary. Counting those bytes as still-resident occupancy refuses a | | 124 | + * follow-up the provider session has room for. | | 125 | + */ | | 126 | +export function countsTowardNativeContextEstimate(input: { | | 127 | + readonly compactionRunOrdinal: number | undefined; | | 128 | + readonly itemRunOrdinal: number | undefined; | | 129 | +}): boolean { | | 130 | + if (input.compactionRunOrdinal === undefined) return true; | | 131 | + if (input.itemRunOrdinal === undefined) return false; | | 132 | + return input.itemRunOrdinal > input.compactionRunOrdinal; | | 133 | +} | | 134 | + | | 135 | +function sameProviderThreadSet( | | 136 | + left: ReadonlyArray<ProviderThreadId>, | | 137 | + right: ReadonlyArray<ProviderThreadId>, | | 138 | +): boolean { | | 139 | + if (left.length !== right.length) return false; | | 140 | + const seen = new Set<ProviderThreadId>(left); | | 141 | + return right.every((id) => seen.has(id)); | | 142 | +} | | 143 | + | | 144 | +/** | | 145 | + * The native session already holds this history when an earlier handoff of | | 146 | + * the same strategy and source set was delivered into it. A `/compact` that | | 147 | + * cannot take an inline transcript leaves its handoff undelivered on purpose; | | 148 | + * the next ordinary turn still sends that one. | | 149 | + */ | | 150 | +export function handoffAlreadyInNativeSession(input: { | | 151 | + readonly handoff: Pick< | | 152 | + OrchestrationV2ContextHandoff, | | 153 | + "strategy" | "coveredRunOrdinals" | "fromProviderThreadIds" | | 154 | + >; | | 155 | + readonly nativeThreadId: string | null | undefined; | | 156 | + readonly sameNativeThread: boolean; | | 157 | + readonly delivered: ReadonlyArray< | | 158 | + Pick< | | 159 | + OrchestrationV2ContextHandoff, | | 160 | + "strategy" | "coveredRunOrdinals" | "fromProviderThreadIds" | "delivery" | | 161 | + > | | 162 | + >; | | 163 | +}): boolean { | | 164 | + if (!input.sameNativeThread || input.nativeThreadId == null) return false; | | 165 | + return input.delivered.some((delivered) => { | | 166 | + const delivery = delivered.delivery; | | 167 | + return ( | | 168 | + delivery !== undefined && | | 169 | + delivery.nativeThreadId === input.nativeThreadId && | | 170 | + delivery.status !== "pending" && | | 171 | + delivered.strategy === input.handoff.strategy && | | 172 | + sameProviderThreadSet(delivered.fromProviderThreadIds, input.handoff.fromProviderThreadIds) && | | 173 | + delivered.coveredRunOrdinals.from <= input.handoff.coveredRunOrdinals.from && | | 174 | + delivered.coveredRunOrdinals.to >= input.handoff.coveredRunOrdinals.to | | 175 | + ); | | 176 | + }); | | 177 | +} | | 178 | + | 94 | 179 | export function attachmentTokenAllowance(attachments: ReadonlyArray<ChatAttachment>): number { | 95 | 180 | // Encoded image bytes are not model tokens. Without dimensions/detail metadata, | 96 | 181 | // reserve 8k tokens per image, above typical resized Codex/Claude image costs. | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.migration.test.ts +50-1Lines changed: 50 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { assert, it } from "@effect/vitest"; | 2 | 2 | import * as Schema from "effect/Schema"; | 3 | | -import { ContextHandoffId, OrchestrationV2Command, ThreadId } from "@t3tools/contracts"; | | 3 | +import { | | 4 | + ContextHandoffId, | | 5 | + OrchestrationV2Command, | | 6 | + ProviderThreadId, | | 7 | + ThreadId, | | 8 | +} from "@t3tools/contracts"; | 4 | 9 |
| 5 | 10 | import { | 6 | 11 | appendContextHandoffId, | 7 | 12 | canReplayCommandReceipt, | | 13 | + legacyImportAlreadyInNativeThread, | 8 | 14 | shouldPrepareLegacyImportHandoff, | 9 | 15 | } from "./Orchestrator.ts"; | 10 | 16 |
| @@ -37,6 +43,49 @@ it("reissues imported context until a V2 run completes", () => { | 37 | 43 | legacyImportItemCount: 0, | 38 | 44 | }), | 39 | 45 | ); | | 46 | + assert.isFalse( | | 47 | + shouldPrepareLegacyImportHandoff({ | | 48 | + historyOrigin: "v1_import", | | 49 | + hasCompletedRun: false, | | 50 | + legacyImportItemCount: 2, | | 51 | + alreadyInNativeThread: true, | | 52 | + }), | | 53 | + ); | | 54 | +}); | | 55 | + | | 56 | +it("treats an inlined v1 import as already in that native session", () => { | | 57 | + const providerThreadId = ProviderThreadId.make("provider-thread:grok"); | | 58 | + const delivered = { | | 59 | + strategy: "manual_context" as const, | | 60 | + fromProviderThreadIds: [], | | 61 | + toProviderThreadId: providerThreadId, | | 62 | + delivery: { | | 63 | + nativeThreadId: "native-1", | | 64 | + status: "inline" as const, | | 65 | + itemIds: [], | | 66 | + }, | | 67 | + }; | | 68 | + assert.isTrue( | | 69 | + legacyImportAlreadyInNativeThread({ | | 70 | + providerThreadId, | | 71 | + nativeThreadId: "native-1", | | 72 | + handoffs: [delivered], | | 73 | + }), | | 74 | + ); | | 75 | + assert.isFalse( | | 76 | + legacyImportAlreadyInNativeThread({ | | 77 | + providerThreadId, | | 78 | + nativeThreadId: "native-1", | | 79 | + handoffs: [{ ...delivered, delivery: { ...delivered.delivery, status: "pending" } }], | | 80 | + }), | | 81 | + ); | | 82 | + assert.isFalse( | | 83 | + legacyImportAlreadyInNativeThread({ | | 84 | + providerThreadId, | | 85 | + nativeThreadId: undefined, | | 86 | + handoffs: [delivered], | | 87 | + }), | | 88 | + ); | 40 | 89 | }); | 41 | 90 |
| 42 | 91 | it("records a reissued legacy handoff on an existing provider thread", () => { | |
|