GeezerVison is showing github.com — fix(claude): scope model observations to the current attempt · pingdotgg/t3code@2c8dfed
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2 packages/client-runtime/src/state
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4967,6 +4967,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 4967 | 4967 | ); | 4968 | 4968 | const restartedRun: OrchestrationV2Run = { | 4969 | 4969 | ...targetRun, | | 4970 | + observedModel: undefined, | 4970 | 4971 | providerInstanceId: input.modelSelection.instanceId, | 4971 | 4972 | modelSelection: input.modelSelection, | 4972 | 4973 | providerThreadId: restartProviderThread.id, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.test.ts +95Lines changed: 95 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1560,6 +1560,101 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { | 1560 | 1560 | assert.deepEqual(yield* store.getTurnStartHistory(threadId, [RunId.make("run:other")]), []); | 1561 | 1561 | }), | 1562 | 1562 | ); | | 1563 | + it.effect( | | 1564 | + "records root model observations without overwriting lifecycle state or another attempt", | | 1565 | + () => | | 1566 | + Effect.gen(function* () { | | 1567 | + const store = yield* ProjectionStore.ProjectionStoreV2; | | 1568 | + const threadId = yield* addRolledBackRecoveryCandidate("observed-model-attempt"); | | 1569 | + const now = yield* DateTime.now; | | 1570 | + const original = (yield* store.getThreadProjection(threadId)).runs[0]; | | 1571 | + assert(original !== undefined && original.rootNodeId !== null); | | 1572 | + const run = { | | 1573 | + ...original, | | 1574 | + activeAttemptId: RunAttemptId.make("attempt-observed-first"), | | 1575 | + status: "completed" as const, | | 1576 | + completedAt: now, | | 1577 | + }; | | 1578 | + yield* store.apply({ | | 1579 | + id: EventId.make("event-observed-completed"), | | 1580 | + type: "run.updated", | | 1581 | + threadId, | | 1582 | + runId: run.id, | | 1583 | + occurredAt: now, | | 1584 | + payload: run, | | 1585 | + }); | | 1586 | + const observation = { | | 1587 | + id: EventId.make("event-observed-first"), | | 1588 | + type: "provider-turn.updated" as const, | | 1589 | + threadId, | | 1590 | + runId: run.id, | | 1591 | + occurredAt: now, | | 1592 | + payload: { | | 1593 | + id: ProviderTurnId.make("turn-observed-first"), | | 1594 | + providerThreadId: ProviderThreadId.make("thread-observed-provider"), | | 1595 | + nodeId: original.rootNodeId, | | 1596 | + runAttemptId: run.activeAttemptId, | | 1597 | + nativeTurnRef: null, | | 1598 | + ordinal: 1, | | 1599 | + status: "running" as const, | | 1600 | + observedModel: "claude-sonnet-5-5", | | 1601 | + startedAt: now, | | 1602 | + completedAt: null, | | 1603 | + }, | | 1604 | + }; | | 1605 | + yield* store.apply(observation); | | 1606 | + const recorded = (yield* store.getThreadProjection(threadId)).runs[0]; | | 1607 | + assert.deepEqual(recorded, { ...run, observedModel: "claude-sonnet-5-5" }); | | 1608 | + | | 1609 | + yield* store.apply({ | | 1610 | + id: EventId.make("event-observed-stale-snapshot"), | | 1611 | + type: "run.updated", | | 1612 | + threadId, | | 1613 | + runId: run.id, | | 1614 | + occurredAt: now, | | 1615 | + payload: { ...run, observedModel: "stale-snapshot-model" }, | | 1616 | + }); | | 1617 | + assert.deepEqual((yield* store.getThreadProjection(threadId)).runs[0], { | | 1618 | + ...run, | | 1619 | + observedModel: "claude-sonnet-5-5", | | 1620 | + }); | | 1621 | + | | 1622 | + const restarted = { | | 1623 | + ...run, | | 1624 | + status: "starting" as const, | | 1625 | + completedAt: null, | | 1626 | + rootNodeId: NodeId.make("node-observed-second"), | | 1627 | + activeAttemptId: RunAttemptId.make("attempt-observed-second"), | | 1628 | + }; | | 1629 | + yield* store.apply({ | | 1630 | + id: EventId.make("event-observed-restart"), | | 1631 | + type: "run.updated", | | 1632 | + threadId, | | 1633 | + runId: run.id, | | 1634 | + occurredAt: now, | | 1635 | + payload: restarted, | | 1636 | + }); | | 1637 | + yield* store.apply({ ...observation, id: EventId.make("event-observed-late-first") }); | | 1638 | + assert.deepEqual((yield* store.getThreadProjection(threadId)).runs[0], restarted); | | 1639 | + | | 1640 | + yield* store.apply({ | | 1641 | + ...observation, | | 1642 | + id: EventId.make("event-observed-second"), | | 1643 | + payload: { | | 1644 | + ...observation.payload, | | 1645 | + id: ProviderTurnId.make("turn-observed-second"), | | 1646 | + ordinal: 2, | | 1647 | + nodeId: restarted.rootNodeId, | | 1648 | + runAttemptId: restarted.activeAttemptId, | | 1649 | + observedModel: "claude-opus-5-5", | | 1650 | + }, | | 1651 | + }); | | 1652 | + assert.deepEqual((yield* store.getThreadProjection(threadId)).runs[0], { | | 1653 | + ...restarted, | | 1654 | + observedModel: "claude-opus-5-5", | | 1655 | + }); | | 1656 | + }), | | 1657 | + ); | 1563 | 1658 | it.effect("preserves stored provider usage when a terminal update omits it", () => | 1564 | 1659 | Effect.gen(function* () { | 1565 | 1660 | const projectionStore = yield* ProjectionStore.ProjectionStoreV2; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.ts +42-9Lines changed: 42 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -697,7 +697,7 @@ function preserveRunRecordedFields( | 697 | 697 | if (current === undefined) return next; | 698 | 698 | return { | 699 | 699 | ...next, | 700 | | - ...(next.observedModel === undefined && current.observedModel !== undefined | | 700 | + ...(current.observedModel !== undefined && next.activeAttemptId === current.activeAttemptId | 701 | 701 | ? { observedModel: current.observedModel } | 702 | 702 | : {}), | 703 | 703 | ...(next.delegatedCompletion === undefined && current.delegatedCompletion !== undefined | @@ -881,11 +881,22 @@ export function applyToProjection( | 881 | 881 | : base.thread, | 882 | 882 | providerThreads: upsertById(base.providerThreads, event.payload), | 883 | 883 | }; | 884 | | - case "provider-turn.updated": | 885 | | - return { | 886 | | - ...base, | 887 | | - providerTurns: upsertProviderTurn(base.providerTurns, event.payload), | 888 | | - }; | | 884 | + case "provider-turn.updated": { | | 885 | + const turn = event.payload; | | 886 | + const observedModel = turn.observedModel; | | 887 | + const runs = | | 888 | + observedModel === undefined || turn.tokenUsage !== undefined | | 889 | + ? base.runs | | 890 | + : base.runs.map((run) => | | 891 | + run.rootNodeId === turn.nodeId && | | 892 | + run.activeAttemptId === turn.runAttemptId && | | 893 | + run.observedModel !== observedModel | | 894 | + ? { ...run, observedModel } | | 895 | + : run, | | 896 | + ); | | 897 | + | | 898 | + return { ...base, runs, providerTurns: upsertProviderTurn(base.providerTurns, turn) }; | | 899 | + } | 889 | 900 | case "runtime-request.updated": | 890 | 901 | return { | 891 | 902 | ...base, | @@ -1839,9 +1850,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 1839 | 1850 | OR item.payload_json LIKE '%"viewedImagePath"%' | 1840 | 1851 | OR item.payload_json LIKE '%"toolName":"%delegate_task"%')`; | 1841 | 1852 |
| 1842 | | - const keepRecordedRunField = (payload: Statement.Fragment, path: string) => sql` | | 1853 | + const keepRecordedRunField = ( | | 1854 | + payload: Statement.Fragment, | | 1855 | + path: string, | | 1856 | + condition = sql`json_type(excluded.payload_json, ${path}) IS NULL`, | | 1857 | + ) => sql` | 1843 | 1858 | CASE | 1844 | | - WHEN json_type(excluded.payload_json, ${path}) IS NULL | | 1859 | + WHEN ${condition} | 1845 | 1860 | AND json_type(orchestration_v2_projection_runs.payload_json, ${path}) IS NOT NULL | 1846 | 1861 | THEN json_set( | 1847 | 1862 | ${payload}, | @@ -1979,7 +1994,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 1979 | 1994 | completed_at = excluded.completed_at, | 1980 | 1995 | payload_json = ${keepRecordedRunField( | 1981 | 1996 | keepRecordedRunField( | 1982 | | - keepRecordedRunField(sql`excluded.payload_json`, "$.observedModel"), | | 1997 | + keepRecordedRunField( | | 1998 | + sql`excluded.payload_json`, | | 1999 | + "$.observedModel", | | 2000 | + sql`json_extract(excluded.payload_json, '$.activeAttemptId') IS | | 2001 | + json_extract(orchestration_v2_projection_runs.payload_json, '$.activeAttemptId')`, | | 2002 | + ), | 1983 | 2003 | "$.delegatedCompletion", | 1984 | 2004 | ), | 1985 | 2005 | "$.restartCancelledBackgroundWork", | @@ -2353,6 +2373,19 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 2353 | 2373 | completed_at = excluded.completed_at, | 2354 | 2374 | payload_json = excluded.payload_json | 2355 | 2375 | `; | | 2376 | + if ( | | 2377 | + event.payload.observedModel !== undefined && | | 2378 | + event.payload.tokenUsage === undefined | | 2379 | + ) { | | 2380 | + yield* sql` | | 2381 | + UPDATE orchestration_v2_projection_runs | | 2382 | + SET payload_json = json_set(payload_json, '$.observedModel', ${event.payload.observedModel}) | | 2383 | + WHERE thread_id = ${event.threadId} | | 2384 | + AND json_extract(payload_json, '$.rootNodeId') = ${event.payload.nodeId} | | 2385 | + AND json_extract(payload_json, '$.activeAttemptId') IS ${event.payload.runAttemptId} | | 2386 | + AND json_extract(payload_json, '$.observedModel') IS NOT ${event.payload.observedModel} | | 2387 | + `; | | 2388 | + } | 2356 | 2389 | break; | 2357 | 2390 | } | 2358 | 2391 | case "runtime-request.updated": { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderEventIngestor.ts +1-24Lines changed: 1 addition & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -602,30 +602,8 @@ export const layer: Layer.Layer< | 602 | 602 | })), | 603 | 603 | ]; | 604 | 604 | } | 605 | | - case "provider_turn.updated": { | 606 | | - const turn = input.event.providerTurn; | 607 | | - const updates: OrchestrationV2DomainEvent[] = []; | 608 | | - | 609 | | - // Usage frames repeat the observation; avoid a run read on every token update. | 610 | | - if (turn.observedModel !== undefined && turn.tokenUsage === undefined) { | 611 | | - const projection = yield* projections.getThreadRecords(input.threadId, ["runs"]); | 612 | | - const run = projection.runs.find( | 613 | | - (candidate) => | 614 | | - candidate.rootNodeId === turn.nodeId && | 615 | | - candidate.activeAttemptId === turn.runAttemptId, | 616 | | - ); | 617 | | - if (run !== undefined && run.observedModel !== turn.observedModel) { | 618 | | - updates.push( | 619 | | - yield* makeDomainEvent(input, { | 620 | | - type: "run.updated", | 621 | | - runId: run.id, | 622 | | - payload: { ...run, observedModel: turn.observedModel }, | 623 | | - }), | 624 | | - ); | 625 | | - } | 626 | | - } | | 605 | + case "provider_turn.updated": | 627 | 606 | return [ | 628 | | - ...updates, | 629 | 607 | ...(["completed", "interrupted", "failed", "cancelled"].includes( | 630 | 608 | input.event.providerTurn.status, | 631 | 609 | ) | @@ -642,7 +620,6 @@ export const layer: Layer.Layer< | 642 | 620 | nodeId: input.event.providerTurn.nodeId, | 643 | 621 | }), | 644 | 622 | ]; | 645 | | - } | 646 | 623 | case "node.updated": | 647 | 624 | // Adapters send an unchanged node again with each text delta. Compare with the | 648 | 625 | // stored node, not the last one sent, because the server also writes nodes. | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -242,6 +242,7 @@ function makeRestartAdapter( | 242 | 242 | strength: "strong", | 243 | 243 | }, | 244 | 244 | ordinal: input.providerTurnOrdinal, | | 245 | + observedModel: initialSelection.model, | 245 | 246 | status: "running", | 246 | 247 | startedAt: occurredAt, | 247 | 248 | completedAt: null, | @@ -343,6 +344,7 @@ it.live("retries a failed selection restart session open before completing", () | 343 | 344 | yield* worker.drain(); | 344 | 345 | const activeProjection = yield* orchestrator.getThreadProjection(threadId); | 345 | 346 | assert.isTrue(activeProjection.providerTurns.some((turn) => turn.status === "running")); | | 347 | + assert.equal(activeProjection.runs[0]?.observedModel, initialSelection.model); | 346 | 348 | const activeRunId = activeProjection.runs[0]?.id; | 347 | 349 | if (activeRunId === undefined) { | 348 | 350 | return yield* Effect.die("active restart test run is missing"); | @@ -408,6 +410,7 @@ it.live("retries a failed selection restart session open before completing", () | 408 | 410 | ); | 409 | 411 | assert.equal(projection.runs.at(-1)?.modelSelection.model, replacementSelection.model); | 410 | 412 | assert.equal(projection.runs.at(-1)?.status, "completed"); | | 413 | + assert.equal(projection.runs.at(-1)?.observedModel, undefined); | 411 | 414 | assert.isTrue(captured.failedReplacementOpen); | 412 | 415 | assert.equal(captured.closedSessionCount, 0); | 413 | 416 | assert.deepEqual( | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/orchestrationV2Projection.test.ts +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import { | 12 | 12 | NodeId, | 13 | 13 | ProviderThreadId, | 14 | 14 | ProviderTurnId, | | 15 | + RunAttemptId, | 15 | 16 | RunId, | 16 | 17 | ThreadId, | 17 | 18 | TurnItemId, | @@ -151,6 +152,16 @@ describe("applyOrchestrationV2ProjectionEvent", () => { | 151 | 152 |
| 152 | 153 | expect(next?.runs[0]?.observedModel).toBe("claude-sonnet-5-5"); | 153 | 154 | expect(next?.runs[0]?.modelSelection).toEqual(run.modelSelection); | | 155 | + | | 156 | + const restarted = applyOrchestrationV2ProjectionEvent(next, { | | 157 | + id: EventId.make("event-run-provenance-restart"), | | 158 | + type: "run.updated", | | 159 | + threadId, | | 160 | + runId, | | 161 | + occurredAt: now, | | 162 | + payload: { ...run, activeAttemptId: RunAttemptId.make("attempt-reducer-new") }, | | 163 | + }); | | 164 | + expect(restarted?.runs[0]?.observedModel).toBeUndefined(); | 154 | 165 | }); | 155 | 166 |
| 156 | 167 | it("keeps live token usage when the terminal provider turn omits it", () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/orchestrationV2Projection.ts +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -301,7 +301,8 @@ export function applyOrchestrationV2ProjectionEvent( | 301 | 301 | case "run.updated": { | 302 | 302 | const current = base.runs.find((run) => run.id === event.payload.id); | 303 | 303 | const payload = | 304 | | - event.payload.observedModel === undefined && current?.observedModel !== undefined | | 304 | + current?.observedModel !== undefined && | | 305 | + event.payload.activeAttemptId === current.activeAttemptId | 305 | 306 | ? { ...event.payload, observedModel: current.observedModel } | 306 | 307 | : event.payload; | 307 | 308 | const next = { ...base, runs: upsertEntity(base.runs, payload) }; | @@ -357,8 +358,20 @@ export function applyOrchestrationV2ProjectionEvent( | 357 | 358 | const observedModel = event.payload.observedModel ?? current?.observedModel; | 358 | 359 | const tokenUsage = event.payload.tokenUsage ?? current?.tokenUsage; | 359 | 360 |
| | 361 | + const runs = | | 362 | + event.payload.observedModel === undefined || event.payload.tokenUsage !== undefined | | 363 | + ? base.runs | | 364 | + : base.runs.map((run) => | | 365 | + run.rootNodeId === event.payload.nodeId && | | 366 | + run.activeAttemptId === event.payload.runAttemptId && | | 367 | + run.observedModel !== event.payload.observedModel | | 368 | + ? { ...run, observedModel: event.payload.observedModel } | | 369 | + : run, | | 370 | + ); | | 371 | + | 360 | 372 | return { | 361 | 373 | ...base, | | 374 | + runs, | 362 | 375 | providerTurns: upsertEntity(base.providerTurns, { | 363 | 376 | ...event.payload, | 364 | 377 | ...(observedModel === undefined ? {} : { observedModel }), | |
|