GeezerVison is showing github.com — chore(v2): merge the V2 branch head (2c388dae9a) into the main merge · pingdotgg/t3code@b049c24
File treeExpand file treeCollapse file tree Open diff view settings thread_rollback_after_restart thread_rollback_after_stop
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/scripts/record-codex-app-server-replay-fixture.ts +82Lines changed: 82 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -102,6 +102,7 @@ const SCENARIO_NAMES = [ | 102 | 102 | "turn_interrupt", | 103 | 103 | "turn_interrupt_mid_tool", | 104 | 104 | "thread_rollback", | | 105 | + "thread_rollback_after_restart", | 105 | 106 | "thread_fork_native_continue", | 106 | 107 | "thread_fork_native_siblings", | 107 | 108 | "thread_merge_back_continue", | @@ -804,6 +805,41 @@ function scenarios(): ReadonlyArray<ReplayScenario> { | 804 | 805 | }, | 805 | 806 | ], | 806 | 807 | }, | | 808 | + { | | 809 | + name: "thread_rollback_after_restart", | | 810 | + fileName: "thread_rollback_after_restart.ndjson", | | 811 | + description: | | 812 | + "One thread completes two turns, the app-server restarts, then the thread rolls back its latest turn and starts another turn.", | | 813 | + runs: [ | | 814 | + { | | 815 | + name: "rollback-after-restart", | | 816 | + description: | | 817 | + "Two completed turns, a fresh app-server that has not loaded the thread, thread/revert before the second turn, then a post-rollback turn.", | | 818 | + steps: [ | | 819 | + { | | 820 | + type: "turn", | | 821 | + label: "first-before-rollback", | | 822 | + prompt: THREAD_ROLLBACK_FIRST_PROMPT, | | 823 | + }, | | 824 | + { | | 825 | + type: "turn", | | 826 | + label: "second-before-rollback", | | 827 | + prompt: THREAD_ROLLBACK_SECOND_PROMPT, | | 828 | + }, | | 829 | + { | | 830 | + type: "rollback", | | 831 | + label: "rollback-latest-turn", | | 832 | + numTurns: 1, | | 833 | + }, | | 834 | + { | | 835 | + type: "turn", | | 836 | + label: "post-rollback", | | 837 | + prompt: THREAD_ROLLBACK_AFTER_PROMPT, | | 838 | + }, | | 839 | + ], | | 840 | + }, | | 841 | + ], | | 842 | + }, | 807 | 843 | { | 808 | 844 | name: "thread_fork_native_continue", | 809 | 845 | fileName: "thread_fork_native_continue.ndjson", | @@ -1416,6 +1452,52 @@ function runReplaySession({ | 1416 | 1452 | return; | 1417 | 1453 | } | 1418 | 1454 |
| | 1455 | + if (scenario.name === "thread_rollback_after_restart") { | | 1456 | + const [first, second, rollback, after] = run.steps; | | 1457 | + if ( | | 1458 | + first === undefined || | | 1459 | + second === undefined || | | 1460 | + rollback?.type !== "rollback" || | | 1461 | + after === undefined || | | 1462 | + first.type === "rollback" || | | 1463 | + first.type === "fork" || | | 1464 | + second.type === "rollback" || | | 1465 | + second.type === "fork" || | | 1466 | + after.type === "rollback" || | | 1467 | + after.type === "fork" | | 1468 | + ) { | | 1469 | + throw new Error( | | 1470 | + "thread_rollback_after_restart replay recording requires turn, turn, rollback, turn.", | | 1471 | + ); | | 1472 | + } | | 1473 | + | | 1474 | + const threadId = yield* Effect.gen(function* () { | | 1475 | + const client = yield* initializeClient; | | 1476 | + const thread = yield* client.request("thread/start", threadRuntimeParams); | | 1477 | + yield* runTurnStep(client, thread.thread.id, first); | | 1478 | + yield* runTurnStep(client, thread.thread.id, second); | | 1479 | + return thread.thread.id; | | 1480 | + }).pipe(Effect.provide(makeCodexLayer({ recorder }))); | | 1481 | + | | 1482 | + yield* recorder.writeRecord({ | | 1483 | + type: "runtime_exit", | | 1484 | + status: "success", | | 1485 | + }); | | 1486 | + | | 1487 | + yield* Effect.gen(function* () { | | 1488 | + const client = yield* initializeClient; | | 1489 | + const resumeParams = { threadId, excludeTurns: true, ...threadRuntimeParams }; | | 1490 | + // Like the adapter: the fresh app-server reports the thread notLoaded, | | 1491 | + // so it is resumed before thread/revert, and the next turn resumes it again. | | 1492 | + yield* client.request("thread/read", { threadId, includeTurns: false }); | | 1493 | + yield* client.request("thread/resume", resumeParams); | | 1494 | + yield* revertCodexThread(client, threadId, rollback.numTurns); | | 1495 | + yield* client.request("thread/resume", resumeParams); | | 1496 | + yield* runTurnStep(client, threadId, after); | | 1497 | + }).pipe(Effect.provide(makeCodexLayer({ recorder }))); | | 1498 | + return; | | 1499 | + } | | 1500 | + | 1419 | 1501 | yield* Effect.gen(function* () { | 1420 | 1502 | const client = yield* initializeClient; | 1421 | 1503 | const thread = yield* client.request("thread/start", { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +40-16Lines changed: 40 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -924,22 +924,32 @@ export const resolveCodexForkBoundary = Effect.fn("CodexAdapterV2.resolveForkBou | 924 | 924 | const CodexThreadHistoryMetadata = Schema.Struct({ | 925 | 925 | thread: Schema.Struct({ | 926 | 926 | historyMode: Schema.optionalKey(Schema.Literals(["legacy", "paginated"])), | | 927 | + status: Schema.optionalKey(Schema.Struct({ type: Schema.String })), | 927 | 928 | }), | 928 | 929 | }); | 929 | 930 | const decodeCodexThreadHistoryMetadata = Schema.decodeUnknownEffect(CodexThreadHistoryMetadata); | 930 | 931 |
| 931 | | -const readCodexThreadHistoryMode = Effect.fn("CodexAdapterV2.readThreadHistoryMode")(function* ( | 932 | | - raw: Pick<CodexClient.CodexAppServerClient["Service"]["raw"], "request">, | 933 | | - threadId: string, | 934 | | -) { | 935 | | - const response = yield* raw.request("thread/read", { threadId, includeTurns: false }); | 936 | | - const metadata = yield* decodeCodexThreadHistoryMetadata(response).pipe( | 937 | | - Effect.mapError((error) => | 938 | | - CodexErrors.CodexAppServerRequestError.invalidPayload("thread/read", "decode-payload", error), | 939 | | - ), | 940 | | - ); | 941 | | - return metadata.thread.historyMode; | 942 | | -}); | | 932 | +const readCodexThreadHistoryMetadata = Effect.fn("CodexAdapterV2.readThreadHistoryMetadata")( | | 933 | + function* ( | | 934 | + raw: Pick<CodexClient.CodexAppServerClient["Service"]["raw"], "request">, | | 935 | + threadId: string, | | 936 | + ) { | | 937 | + const response = yield* raw.request("thread/read", { threadId, includeTurns: false }); | | 938 | + const metadata = yield* decodeCodexThreadHistoryMetadata(response).pipe( | | 939 | + Effect.mapError((error) => | | 940 | + CodexErrors.CodexAppServerRequestError.invalidPayload( | | 941 | + "thread/read", | | 942 | + "decode-payload", | | 943 | + error, | | 944 | + ), | | 945 | + ), | | 946 | + ); | | 947 | + return { | | 948 | + historyMode: metadata.thread.historyMode, | | 949 | + loaded: metadata.thread.status?.type !== "notLoaded", | | 950 | + }; | | 951 | + }, | | 952 | +); | 943 | 953 |
| 944 | 954 | export const resolveCodexRollbackTurnCount = Effect.fn("CodexAdapterV2.resolveRollbackTurnCount")( | 945 | 955 | function* (input: ProviderAdapterV2RollbackThreadInput) { | @@ -6086,8 +6096,8 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi | 6086 | 6096 | } | 6087 | 6097 | // Codex 0.156 can revert paginated history at a turn boundary. | 6088 | 6098 | // Legacy history no longer has a rollback endpoint. | 6089 | | - const historyMode = yield* ensureInitialized.pipe( | 6090 | | - Effect.andThen(readCodexThreadHistoryMode(client.raw, threadId)), | | 6099 | + const { historyMode, loaded } = yield* ensureInitialized.pipe( | | 6100 | + Effect.andThen(readCodexThreadHistoryMetadata(client.raw, threadId)), | 6091 | 6101 | ); | 6092 | 6102 | if (historyMode !== "paginated") { | 6093 | 6103 | return yield* new ProviderAdapterRollbackThreadError({ | @@ -6096,6 +6106,20 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi | 6096 | 6106 | cause: `Cannot roll back Codex thread ${threadId}: the thread uses legacy history, which Codex 0.156 cannot revert.`, | 6097 | 6107 | }); | 6098 | 6108 | } | | 6109 | + // `thread/revert` only acts on a thread loaded in this app-server | | 6110 | + // process. After a restart or idle release, load it the same way | | 6111 | + // the next turn would before reverting. | | 6112 | + if (!loaded) { | | 6113 | + yield* client.raw.request("thread/resume", { | | 6114 | + threadId, | | 6115 | + excludeTurns: true, | | 6116 | + ...codexThreadRuntimeParams({ | | 6117 | + threadId: threadInput.providerThread.appThreadId, | | 6118 | + modelSelection: input.modelSelection, | | 6119 | + runtimePolicy: input.runtimePolicy, | | 6120 | + }), | | 6121 | + }); | | 6122 | + } | 6099 | 6123 | const response = yield* ensureInitialized.pipe( | 6100 | 6124 | Effect.andThen(revertCodexThread(client, threadId, numTurns)), | 6101 | 6125 | ); | @@ -6163,8 +6187,8 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi | 6163 | 6187 | // Reached only when the selected source turn has no native | 6164 | 6188 | // turn reference, so the fork had to be taken at head and then | 6165 | 6189 | // trimmed with the paginated history API. | 6166 | | - const historyMode = yield* ensureInitialized.pipe( | 6167 | | - Effect.andThen(readCodexThreadHistoryMode(client.raw, response.thread.id)), | | 6190 | + const { historyMode } = yield* ensureInitialized.pipe( | | 6191 | + Effect.andThen(readCodexThreadHistoryMetadata(client.raw, response.thread.id)), | 6168 | 6192 | ); | 6169 | 6193 | if (historyMode !== "paginated") { | 6170 | 6194 | return yield* new ProviderAdapterForkThreadError({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1165,12 +1165,16 @@ export function makeCursorAdapterV2( | 1165 | 1165 | const emitToolArtifacts = Effect.fnUntraced(function* (input: { | 1166 | 1166 | readonly active: ActiveCursorToolCall; | 1167 | 1167 | readonly completed: boolean; | | 1168 | + /** Terminal status for a tool the turn ended before Cursor completed it. */ | | 1169 | + readonly unfinishedStatus?: "interrupted" | "failed" | "cancelled"; | 1168 | 1170 | }) { | 1169 | 1171 | const { active } = input; | 1170 | 1172 | const toolCall = active.toolCall; | 1171 | 1173 | const now = yield* DateTime.now; | 1172 | 1174 | const failed = input.completed && cursorToolFailed(toolCall); | 1173 | | - const status = input.completed ? (failed ? "failed" : "completed") : "running"; | | 1175 | + const status = !input.completed | | 1176 | + ? "running" | | 1177 | + : (input.unfinishedStatus ?? (failed ? "failed" : "completed")); | 1174 | 1178 | const nodeId = idAllocator.derive.nodeFromProviderItem({ | 1175 | 1179 | driver: CURSOR_PROVIDER, | 1176 | 1180 | nativeItemId: active.callId, | @@ -1968,8 +1972,14 @@ export function makeCursorAdapterV2( | 1968 | 1972 | } | 1969 | 1973 | input.context.finalized = true; | 1970 | 1974 | const completedAt = yield* DateTime.now; | | 1975 | + // Tools still here never got a tool-call-completed. A stopped or | | 1976 | + // failed turn cut them short, so they end with the turn's status. | 1971 | 1977 | for (const tool of input.context.tools.values()) { | 1972 | | - yield* emitToolArtifacts({ active: tool, completed: true }); | | 1978 | + yield* emitToolArtifacts({ | | 1979 | + active: tool, | | 1980 | + completed: true, | | 1981 | + ...(input.status === "completed" ? {} : { unfinishedStatus: input.status }), | | 1982 | + }); | 1973 | 1983 | } | 1974 | 1984 | input.context.tools.clear(); | 1975 | 1985 | // This interaction stops delivering updates at finalization. Tasks | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.ts +34-1Lines changed: 34 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,7 @@ import * as Effect from "effect/Effect"; | 16 | 16 | import * as Layer from "effect/Layer"; | 17 | 17 | import * as Schema from "effect/Schema"; | 18 | 18 |
| 19 | | -import { Agent } from "../../provider/cursorSdk.ts"; | | 19 | +import { Agent, createAgentPlatform } from "../../provider/cursorSdk.ts"; | 20 | 20 | import type { EventNdjsonLogger } from "../../provider/Layers/EventNdjsonLogger.ts"; | 21 | 21 | import { ProviderEventLoggers } from "../../provider/Layers/ProviderEventLoggers.ts"; | 22 | 22 |
| @@ -295,6 +295,36 @@ function makeCursorAgentSdkProtocolLogger(input: { | 295 | 295 | .pipe(Effect.ignore); | 296 | 296 | } | 297 | 297 |
| | 298 | +/** | | 299 | + * The Cursor SDK decides once per process whether local sandboxing works, and | | 300 | + * caches the answer the first time any run starts. Only sandboxed runs point | | 301 | + * it at its `cursorsandbox` helper first, so after an unsandboxed (Full access) | | 302 | + * run it caches "unsupported" and rejects every later sandboxed run until the | | 303 | + * server restarts. Warming a bare sandboxed executor before the first | | 304 | + * unsandboxed agent opens lets the SDK find the helper and cache the real | | 305 | + * answer. Warming is best effort: on a machine without sandbox support it | | 306 | + * fails, the SDK caches "unsupported", and sandboxed runs report that as | | 307 | + * before. | | 308 | + */ | | 309 | +let cursorSandboxSupportPrime: Promise<void> | undefined; | | 310 | + | | 311 | +function primeCursorSandboxSupport(options: AgentOptions): Promise<void> { | | 312 | + cursorSandboxSupportPrime ??= (async () => { | | 313 | + const cwd = typeof options.local?.cwd === "string" ? options.local.cwd : undefined; | | 314 | + const platform = await createAgentPlatform(cwd === undefined ? {} : { workspaceRef: cwd }); | | 315 | + const release = await platform.prewarmLocalWorkspace({ | | 316 | + ...(options.apiKey === undefined ? {} : { apiKey: options.apiKey }), | | 317 | + local: { | | 318 | + ...(cwd === undefined ? {} : { cwd }), | | 319 | + settingSources: [], | | 320 | + sandboxOptions: { enabled: true }, | | 321 | + }, | | 322 | + }); | | 323 | + await release(); | | 324 | + })().catch(() => undefined); | | 325 | + return cursorSandboxSupportPrime; | | 326 | +} | | 327 | + | 298 | 328 | /** | 299 | 329 | * Runs agents through the Cursor SDK, logging every frame to the protocol | 300 | 330 | * logger chosen for each opened agent. The live layer writes the native | @@ -306,6 +336,9 @@ export function makeCursorAgentSdkRunner( | 306 | 336 | ): CursorAgentSdkRunnerShape { | 307 | 337 | return CursorAgentSdkRunner.of({ | 308 | 338 | open: Effect.fn("CursorAgentSdkRunner.open")(function* (input) { | | 339 | + if (input.options.local?.sandboxOptions?.enabled === false) { | | 340 | + yield* Effect.promise(() => primeCursorSandboxSupport(input.options)); | | 341 | + } | 309 | 342 | const protocolLogger = protocolLoggerFor(input); | 310 | 343 | const log = (event: CursorAgentSdkProtocolLogEvent) => | 311 | 344 | protocolLogger === undefined ? Effect.void : protocolLogger(event); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/PiAdapterV2.ts +31-7Lines changed: 31 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -351,6 +351,13 @@ interface ActivePiTurn { | 351 | 351 | activeCompaction: PiCompactionState | null; | 352 | 352 | activeProviderRetry: PiProviderRetryState | null; | 353 | 353 | failure: ReturnType<typeof makeProviderFailure> | null; | | 354 | + /** Session-tree refs read just before Stop terminates Pi, when no read is possible later. */ | | 355 | + stopTreeRefs?: PiTurnTreeRefs | null; | | 356 | +} | | 357 | + | | 358 | +interface PiTurnTreeRefs { | | 359 | + readonly turnStartEntryId: string | null; | | 360 | + readonly leafId: string | null; | 354 | 361 | } | 355 | 362 |
| 356 | 363 | interface PendingPiPrompt { | @@ -1367,13 +1374,18 @@ export function makePiAdapterV2(options: PiAdapterV2Options): ProviderAdapterV2S | 1367 | 1374 | * point `fork` rolls back to); the leaf becomes the conversation head. | 1368 | 1375 | * Pure bookkeeping: failures degrade to the synthetic refs. | 1369 | 1376 | */ | 1370 | | - const captureTurnTreeRefs = Effect.fnUntraced(function* () { | | 1377 | + const captureTurnTreeRefs = Effect.fnUntraced(function* ( | | 1378 | + timeoutMs = PI_REQUEST_TIMEOUT_MS, | | 1379 | + ): Effect.fn.Return<PiTurnTreeRefs | null> { | 1371 | 1380 | const cursorWasStale = leafCursorStale; | 1372 | 1381 | const cursor = cursorWasStale ? null : lastKnownLeaf; | 1373 | | - const data = yield* request({ | 1374 | | - type: "get_entries", | 1375 | | - ...(cursor === null ? {} : { since: cursor }), | 1376 | | - }).pipe(Effect.orElseSucceed(() => undefined)); | | 1382 | + const data = yield* request( | | 1383 | + { | | 1384 | + type: "get_entries", | | 1385 | + ...(cursor === null ? {} : { since: cursor }), | | 1386 | + }, | | 1387 | + timeoutMs, | | 1388 | + ).pipe(Effect.orElseSucceed(() => undefined)); | 1377 | 1389 | if (data === undefined) { | 1378 | 1390 | // Pi may have advanced past `lastKnownLeaf` while this failed, so the | 1379 | 1391 | // cursor can no longer be trusted to bound a single turn. | @@ -1431,7 +1443,8 @@ export function makePiAdapterV2(options: PiAdapterV2Options): ProviderAdapterV2S | 1431 | 1443 | } | 1432 | 1444 | } | 1433 | 1445 | yield* cancelPendingPrompts(completedAt); | 1434 | | - const treeRefs = yield* captureTurnTreeRefs(); | | 1446 | + const treeRefs = | | 1447 | + turn.stopTreeRefs !== undefined ? turn.stopTreeRefs : yield* captureTurnTreeRefs(); | 1435 | 1448 | const tokenUsage = readUsage | 1436 | 1449 | ? yield* readTokenUsage(turn.latestCompactionAfterTokens, completedAt) | 1437 | 1450 | : undefined; | @@ -2490,7 +2503,18 @@ export function makePiAdapterV2(options: PiAdapterV2Options): ProviderAdapterV2S | 2490 | 2503 | if (interruptInput.requestRuntimeRestart === true && !turn.settleWhenIdle) { | 2491 | 2504 | yield* request({ type: "abort" }, 2_000).pipe(Effect.ignore); | 2492 | 2505 | } | 2493 | | - yield* connection.terminate; | | 2506 | + // Terminating fails every later request, so read the stopped | | 2507 | + // turn's session-tree refs first: rolling back past this turn | | 2508 | + // forks at its user entry. Holding the event permit also lets a | | 2509 | + // finalize that is already reading them finish before the kill. | | 2510 | + yield* sessionEventPermit.withPermits(1)( | | 2511 | + Effect.gen(function* () { | | 2512 | + if (threadState?.activeTurn === turn && turn.stopTreeRefs === undefined) { | | 2513 | + turn.stopTreeRefs = yield* captureTurnTreeRefs(2_000); | | 2514 | + } | | 2515 | + yield* connection.terminate; | | 2516 | + }), | | 2517 | + ); | 2494 | 2518 | return; | 2495 | 2519 | } | 2496 | 2520 | yield* request({ type: "abort" }).pipe( | |
|