GeezerVison is showing github.com — fix(server): reject oversized goal objectives before dispatch (#23033) · pingdotgg/t3code@42ca7c0
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2 effect-codex-app-server/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +102Lines changed: 102 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { historyResponseItems } from "@t3tools/provider-core/server/handoffBudget"; | | 2 | +import { makeProviderFailure } from "@t3tools/provider-core/server/failure"; | 2 | 3 | import { | 3 | 4 | makeProviderTextDeltaCoalescer, | 4 | 5 | type ProviderTextDeltaUpdate, | @@ -8184,6 +8185,107 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 8184 | 8185 | ), | 8185 | 8186 | ); | 8186 | 8187 |
| | 8188 | + it.effect("rejects an oversized /goal objective before any goal request", () => | | 8189 | + Effect.gen(function* () { | | 8190 | + const oversized = "x".repeat(4001); | | 8191 | + const requests: string[] = []; | | 8192 | + const harness = yield* makeCodexReplayHarness( | | 8193 | + makeCodexReplayTranscript({ scenario: "goal-oversized", entries: sessionStart }), | | 8194 | + () => Effect.void, | | 8195 | + (method) => | | 8196 | + Effect.sync(() => { | | 8197 | + requests.push(method); | | 8198 | + }), | | 8199 | + ); | | 8200 | + const start = yield* harness.runtime | | 8201 | + .startTurn(yield* goalTurnInput(harness, `/goal ${oversized}`)) | | 8202 | + .pipe(Effect.result); | | 8203 | + | | 8204 | + assert.deepEqual( | | 8205 | + requests.filter((method) => method.startsWith("thread/goal/") || method === "turn/start"), | | 8206 | + [], | | 8207 | + ); | | 8208 | + assert.equal(start._tag, "Failure"); | | 8209 | + if (start._tag === "Failure") { | | 8210 | + const { message } = makeProviderFailure({ cause: start.failure }); | | 8211 | + assert.match(message, /4,?001/u); | | 8212 | + assert.match(message, /4,?000/u); | | 8213 | + } | | 8214 | + }).pipe( | | 8215 | + Effect.scoped, | | 8216 | + Effect.provide( | | 8217 | + Layer.mergeAll( | | 8218 | + IdAllocator.layer, | | 8219 | + McpProviderSessions.layer, | | 8220 | + NodeServices.layer, | | 8221 | + NodeCrypto.layer, | | 8222 | + ), | | 8223 | + ), | | 8224 | + ), | | 8225 | + ); | | 8226 | + | | 8227 | + it.effect("sends a /goal objective at the 4000 character limit unchanged", () => | | 8228 | + Effect.gen(function* () { | | 8229 | + const atLimit = "y".repeat(4000); | | 8230 | + const goal = (status: string) => ({ ...codexGoal(status, 0), objective: atLimit }); | | 8231 | + const transcript = makeCodexReplayTranscript({ | | 8232 | + scenario: "goal-at-limit", | | 8233 | + entries: [ | | 8234 | + ...sessionStart, | | 8235 | + ...request(3, "thread/goal/get", { threadId: nativeThreadId }, { goal: null }), | | 8236 | + ...request( | | 8237 | + 4, | | 8238 | + "thread/goal/set", | | 8239 | + { threadId: nativeThreadId, objective: atLimit, status: "paused" }, | | 8240 | + { goal: goal("paused") }, | | 8241 | + ), | | 8242 | + ...withReplayRequestId( | | 8243 | + codexReplayPreamble({ | | 8244 | + nativeThreadId, | | 8245 | + nativeTurnId: "goal-turn-limit", | | 8246 | + prompt: atLimit, | | 8247 | + sentPrompt: `Start working toward this goal: ${atLimit}`, | | 8248 | + }).slice(5), | | 8249 | + 5, | | 8250 | + ), | | 8251 | + ...request( | | 8252 | + 6, | | 8253 | + "thread/goal/set", | | 8254 | + { threadId: nativeThreadId, status: "active" }, | | 8255 | + { goal: goal("active") }, | | 8256 | + ), | | 8257 | + notification("goal complete", "thread/goal/updated", { | | 8258 | + threadId: nativeThreadId, | | 8259 | + turnId: "goal-turn-limit", | | 8260 | + goal: goal("complete"), | | 8261 | + }), | | 8262 | + notification("turn done", "turn/completed", { | | 8263 | + threadId: nativeThreadId, | | 8264 | + turn: makeCodexReplayTurn({ id: "goal-turn-limit", status: "completed" }), | | 8265 | + }), | | 8266 | + ], | | 8267 | + }); | | 8268 | + const harness = yield* makeCodexReplayHarness(transcript); | | 8269 | + yield* harness.runtime.startTurn(yield* goalTurnInput(harness, `/goal ${atLimit}`)); | | 8270 | + yield* harness.firstTerminal; | | 8271 | + | | 8272 | + assert.deepEqual( | | 8273 | + harness.terminalEvents().map((event) => event.status), | | 8274 | + ["completed"], | | 8275 | + ); | | 8276 | + }).pipe( | | 8277 | + Effect.scoped, | | 8278 | + Effect.provide( | | 8279 | + Layer.mergeAll( | | 8280 | + IdAllocator.layer, | | 8281 | + McpProviderSessions.layer, | | 8282 | + NodeServices.layer, | | 8283 | + NodeCrypto.layer, | | 8284 | + ), | | 8285 | + ), | | 8286 | + ), | | 8287 | + ); | | 8288 | + | 8187 | 8289 | it.effect("settles a run held for the next goal turn when Stop arrives in between", () => | 8188 | 8290 | Effect.gen(function* () { | 8189 | 8291 | const prompt = "Keep going"; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -900,6 +900,28 @@ const isTerminalProviderTurn = (turn: OrchestrationV2ProviderTurn): boolean => | 900 | 900 | turn.status === "failed" || | 901 | 901 | turn.status === "cancelled"); | 902 | 902 |
| | 903 | +class CodexGoalObjectiveTooLongError extends Schema.TaggedError<CodexGoalObjectiveTooLongError>()( | | 904 | + "CodexGoalObjectiveTooLongError", | | 905 | + { length: Schema.Number, limit: Schema.Number }, | | 906 | +) { | | 907 | + override get message() { | | 908 | + return `The goal objective is ${this.length} characters; Codex supports at most ${this.limit}. Shorten it and send /goal again. The current goal was not changed.`; | | 909 | + } | | 910 | +} | | 911 | + | | 912 | +function validateCodexGoalObjective(objective: string) { | | 913 | + let length = 0; | | 914 | + for (const _ of objective) length++; | | 915 | + return length > CodexSchema.MAX_THREAD_GOAL_OBJECTIVE_CHARS | | 916 | + ? Effect.fail( | | 917 | + new CodexGoalObjectiveTooLongError({ | | 918 | + length, | | 919 | + limit: CodexSchema.MAX_THREAD_GOAL_OBJECTIVE_CHARS, | | 920 | + }), | | 921 | + ) | | 922 | + : Effect.void; | | 923 | +} | | 924 | + | 903 | 925 | type CodexThreadGoal = CodexSchema.V2ThreadGoalUpdatedNotification["goal"]; | 904 | 926 |
| 905 | 927 | const CODEX_GOAL_STATUSES = { | @@ -6801,6 +6823,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 6801 | 6823 | turnInput: ProviderAdapter.ProviderAdapterV2TurnInput, | 6802 | 6824 | command: OrchestrationV2GoalCommand, | 6803 | 6825 | ) { | | 6826 | + if (command.type === "set") yield* validateCodexGoalObjective(command.objective); | 6804 | 6827 | const threadId = yield* getNativeThreadId(turnInput.providerThread); | 6805 | 6828 | rootProviderThreads.set(threadId, turnInput.providerThread); | 6806 | 6829 | const readGoal = client | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/RunExecutionService.test.ts +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ import * as Layer from "effect/Layer"; | 36 | 36 | import * as Option from "effect/Option"; | 37 | 37 | import * as Ref from "effect/Ref"; | 38 | 38 | import * as Stream from "effect/Stream"; | | 39 | +import { CodexAppServerRequestError } from "effect-codex-app-server/errors"; | 39 | 40 | import { SqlError, classifySqliteError } from "effect/sql/SqlError"; | 40 | 41 |
| 41 | 42 | import { toPersistenceSqlError } from "../persistence/Errors.ts"; | @@ -2686,6 +2687,35 @@ it.effect("refreshes pull requests after a provider stream exits with an error", | 2686 | 2687 | }), | 2687 | 2688 | ); | 2688 | 2689 |
| | 2690 | +it.effect("records a provider's input rejection at turn start in the terminal error", () => | | 2691 | + Effect.gen(function* () { | | 2692 | + const rejection = "goal objective must be at most 4000 characters"; | | 2693 | + const { written, observed } = yield* captureRootRunTermination({ | | 2694 | + key: "start-failure:input-rejected", | | 2695 | + shouldFinalizeRun: () => Effect.succeed(true), | | 2696 | + events: () => Stream.never, | | 2697 | + afterStart: (closed) => Deferred.succeed(closed, undefined), | | 2698 | + startTurn: (turnInput) => | | 2699 | + Effect.fail( | | 2700 | + new ProviderAdapter.ProviderAdapterTurnStartError({ | | 2701 | + driver, | | 2702 | + threadId: turnInput.threadId, | | 2703 | + providerThreadId: turnInput.providerThread.id, | | 2704 | + runId: turnInput.runId, | | 2705 | + cause: new CodexAppServerRequestError({ | | 2706 | + code: -32600, | | 2707 | + errorMessage: rejection, | | 2708 | + method: "thread/goal/set", | | 2709 | + }), | | 2710 | + }), | | 2711 | + ), | | 2712 | + }); | | 2713 | + assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]); | | 2714 | + const error = written.find((item) => item.type === "error"); | | 2715 | + assert.include(error?.failure.message ?? "", rejection); | | 2716 | + }), | | 2717 | +); | | 2718 | + | 2689 | 2719 | it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as const)( | 2690 | 2720 | "stops the live provider and background work before settling an ingestion %s", | 2691 | 2721 | (failureMode) => | |
Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | export * from "./_generated/schema.gen.ts"; | 2 | 2 | export * from "./_generated/meta.gen.ts"; | 3 | 3 | export * from "./_generated/namespaces.gen.ts"; | | 4 | + | | 5 | +// Mirrors codex_protocol::protocol::MAX_THREAD_GOAL_OBJECTIVE_CHARS, which the | | 6 | +// generated schema does not carry. Codex counts Unicode scalar values (Rust | | 7 | +// `chars()`), not UTF-16 code units. | | 8 | +export const MAX_THREAD_GOAL_OBJECTIVE_CHARS = 4_000; |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-core/src/server/failure.test.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -146,6 +146,16 @@ it("keeps unrecognized reasons and provider text out of category messages", () = | 146 | 146 | { _tag: "ProviderAdapterProtocolError", reason: "private-config" }, | 147 | 147 | { _tag: "ProviderAdapterProtocolError", reason: "constructor" }, | 148 | 148 | { _tag: "constructor", cause: "private-config" }, | | 149 | + { | | 150 | + _tag: "CodexAppServerRequestError", | | 151 | + method: "thread/goal/set", | | 152 | + message: "thread not found: private-thread", | | 153 | + }, | | 154 | + { | | 155 | + _tag: "CodexAppServerRequestError", | | 156 | + method: "turn/start", | | 157 | + message: "goal objective must be at most 4000 characters", | | 158 | + }, | 149 | 159 | ]) { | 150 | 160 | assert.equal( | 151 | 161 | makeProviderFailure({ cause: { _tag: "ProviderAdapterTurnStartError", cause } }).message, | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-core/src/server/failure.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,6 +61,10 @@ const PROVIDER_FAILURE_CATEGORIES: Partial< | 61 | 61 | }, | 62 | 62 | }; | 63 | 63 |
| | 64 | +// codex_protocol::protocol::validate_thread_goal_objective | | 65 | +const CODEX_GOAL_VALIDATION_MESSAGE = | | 66 | + /^goal objective must (?:not be empty|be at most \d{1,6} characters)$/u; | | 67 | + | 64 | 68 | const PROVIDER_FAILURE_REASONS: Record<ProviderAdapterFailureReason, string> = { | 65 | 69 | previous_turn_active: "the previous turn is still running", | 66 | 70 | attachment_unreadable: "an attachment could not be read", | @@ -108,8 +112,22 @@ function curatedCauseMessages(cause: unknown): { | 108 | 112 | case "ClaudeSettingsLaunchArgError": | 109 | 113 | case "ClaudeMcpRegistrationTimeoutError": | 110 | 114 | case "CursorModelUnavailableError": | | 115 | + case "CodexGoalObjectiveTooLongError": | 111 | 116 | case "ProviderSetupError": | 112 | 117 | return { known: stringField(cause, "message"), category: categoryMessage() }; | | 118 | + case "CodexAppServerRequestError": { | | 119 | + // Only Codex's fixed-shape goal validation text is actionable and | | 120 | + // safe to show; other request error text stays hidden. | | 121 | + const rejection = stringField(cause, "message"); | | 122 | + if ( | | 123 | + (cause as Record<string, unknown>).method === "thread/goal/set" && | | 124 | + rejection !== undefined && | | 125 | + CODEX_GOAL_VALIDATION_MESSAGE.test(rejection) | | 126 | + ) { | | 127 | + return { known: `Codex rejected the goal: ${rejection}.`, category: categoryMessage() }; | | 128 | + } | | 129 | + break; | | 130 | + } | 113 | 131 | case "AcpProcessExitedError": { | 114 | 132 | const exit = `provider process exited${processExitStatus(cause)}`; | 115 | 133 | const stage = category === undefined ? `The ${exit}` : `${category.summary}: the ${exit}`; | |
|