Skip to content

Commit 36dd5cb

Browse files
committed
fix(server): preserve native Codex goals in workspaces
1 parent 85ac1c5 commit 36dd5cb

5 files changed

Lines changed: 99 additions & 47 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8461,7 +8461,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
84618461
),
84628462
);
84638463

8464-
it.effect("settles /goal pause with a reply instead of a native turn", () =>
8464+
it.effect("settles workspace /goal pause with a reply instead of a native turn", () =>
84658465
Effect.gen(function* () {
84668466
const transcript = makeCodexReplayTranscript({
84678467
scenario: "goal-pause",
@@ -8482,7 +8482,19 @@ describe("CodexAdapterV2 post-settle continuation", () => {
84828482
],
84838483
});
84848484
const harness = yield* makeCodexReplayHarness(transcript);
8485-
yield* harness.runtime.startTurn(yield* goalTurnInput(harness, "/goal pause"));
8485+
const turnInput = yield* goalTurnInput(harness, "/goal pause");
8486+
yield* harness.runtime.startTurn({
8487+
...turnInput,
8488+
appThread: {
8489+
...turnInput.appThread,
8490+
workspace: {
8491+
filePath: "/meta/all.code-workspace",
8492+
workspaceRoot: "/meta",
8493+
folders: [{ id: "first", name: "First", path: "/meta/first" }],
8494+
repositories: [],
8495+
},
8496+
},
8497+
});
84868498
yield* harness.firstTerminal;
84878499

84888500
assert.deepEqual(

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseCodexGoalCommand, type CodexGoalCommand } from "./codexGoalCommand.ts";
12
import { revertCodexThread } from "../../provider/CodexThreadRevert.ts";
23
import { historyResponseItems } from "@t3tools/provider-core/server/handoffBudget";
34
import { makeProviderTextDeltaCoalescer } from "./ProviderTextDeltaCoalescer.ts";
@@ -890,36 +891,7 @@ const isTerminalProviderTurn = (turn: OrchestrationV2ProviderTurn): boolean =>
890891
turn.status === "failed" ||
891892
turn.status === "cancelled");
892893

893-
export type CodexGoalCommand =
894-
| { readonly type: "show" }
895-
| { readonly type: "clear" }
896-
| { readonly type: "pause" }
897-
| { readonly type: "resume" }
898-
| { readonly type: "set"; readonly objective: string };
899-
900-
/**
901-
* Parses a `/goal` message the way the Codex TUI does: `clear`, `pause` and
902-
* `resume` control the current goal, a bare `/goal` shows it, and any other
903-
* text becomes the new objective. Returns null for every other message.
904-
*/
905-
export function parseCodexGoalCommand(text: string): CodexGoalCommand | null {
906-
const match = /^\/goal(?:\s+([\s\S]*))?$/u.exec(text.trim());
907-
if (match === null) return null;
908-
const argument = (match[1] ?? "").trim();
909-
switch (argument.toLowerCase()) {
910-
case "":
911-
case "edit":
912-
return { type: "show" };
913-
case "clear":
914-
return { type: "clear" };
915-
case "pause":
916-
return { type: "pause" };
917-
case "resume":
918-
return { type: "resume" };
919-
default:
920-
return { type: "set", objective: argument };
921-
}
922-
}
894+
export { parseCodexGoalCommand, type CodexGoalCommand } from "./codexGoalCommand.ts";
923895

924896
type CodexThreadGoal = CodexSchema.V2ThreadGoalUpdatedNotification["goal"];
925897

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export type CodexGoalCommand =
2+
| { readonly type: "show" }
3+
| { readonly type: "clear" }
4+
| { readonly type: "pause" }
5+
| { readonly type: "resume" }
6+
| { readonly type: "set"; readonly objective: string };
7+
8+
/**
9+
* Parses a `/goal` message the way the Codex TUI does: `clear`, `pause` and
10+
* `resume` control the current goal, a bare `/goal` shows it, and any other
11+
* text becomes the new objective. Returns null for every other message.
12+
*/
13+
export function parseCodexGoalCommand(text: string): CodexGoalCommand | null {
14+
const match = /^\/goal(?:\s+([\s\S]*))?$/u.exec(text.trim());
15+
if (match === null) return null;
16+
const argument = (match[1] ?? "").trim();
17+
switch (argument.toLowerCase()) {
18+
case "":
19+
case "edit":
20+
return { type: "show" };
21+
case "clear":
22+
return { type: "clear" };
23+
case "pause":
24+
return { type: "pause" };
25+
case "resume":
26+
return { type: "resume" };
27+
default:
28+
return { type: "set", objective: argument };
29+
}
30+
}

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,21 @@ function makeLocalCommandHarness(input: {
452452
(input: RunExecutionService.RunExecutionServiceV2StartRootRunInput) => Effect.Effect<void>
453453
>((controls) =>
454454
input.successfulNativeTurn === true
455-
? controls.session.startTurn({
456-
appThread: controls.appThread,
457-
threadId: controls.appThread.id,
458-
runId: controls.run.id,
459-
runOrdinal: controls.run.ordinal,
460-
providerTurnOrdinal: controls.providerTurnOrdinal,
461-
attemptId: controls.attemptId,
462-
rootNodeId: controls.rootNode.id,
463-
providerThread: controls.providerThread,
464-
message: controls.message,
465-
modelSelection: controls.modelSelection,
466-
runtimePolicy: controls.runtimePolicy,
467-
})
455+
? controls.session
456+
.startTurn({
457+
appThread: controls.appThread,
458+
threadId: controls.appThread.id,
459+
runId: controls.run.id,
460+
runOrdinal: controls.run.ordinal,
461+
providerTurnOrdinal: controls.providerTurnOrdinal,
462+
attemptId: controls.attemptId,
463+
rootNodeId: controls.rootNode.id,
464+
providerThread: controls.providerThread,
465+
message: controls.message,
466+
modelSelection: controls.modelSelection,
467+
runtimePolicy: controls.runtimePolicy,
468+
})
469+
.pipe(Effect.orDie)
468470
: input.failReadsAfterRunning === true
469471
? Effect.void
470472
: Effect.die("A local command must not start a native turn."),
@@ -479,7 +481,7 @@ function makeLocalCommandHarness(input: {
479481
);
480482
const tryHandlePromptCommand = vi.fn(() =>
481483
input.logoutFailure === undefined
482-
? Effect.succeed(true)
484+
? Effect.succeed(input.successfulNativeTurn !== true)
483485
: Effect.fail(
484486
new ProviderSetupError({
485487
instanceId: oldInstanceId,
@@ -927,3 +929,32 @@ effectIt.effect("delivers frozen workspace context in an ordinary native provide
927929
expect(harness.events.some((event) => event.type === "context-handoff.updated")).toBe(false);
928930
}),
929931
);
932+
933+
for (const text of [
934+
"/goal",
935+
"/goal edit",
936+
"/goal pause",
937+
"/goal resume",
938+
"/goal clear",
939+
"/goal fix tests",
940+
]) {
941+
effectIt.effect(`keeps workspace Codex command ${text} available to the native adapter`, () =>
942+
Effect.gen(function* () {
943+
const harness = makeLocalCommandHarness({
944+
text,
945+
workspace: {
946+
filePath: "/meta/all.code-workspace",
947+
workspaceRoot: "/meta",
948+
folders: [{ id: "first", name: "First", path: "/meta/first" }],
949+
repositories: [],
950+
},
951+
successfulNativeTurn: true,
952+
});
953+
yield* harness.start;
954+
expect(harness.startTurn).toHaveBeenCalledOnce();
955+
expect(harness.startTurn.mock.calls[0]?.[0].message.text).toBe(text);
956+
expect(harness.getTurnStartHistory).not.toHaveBeenCalled();
957+
expect(harness.events.some((event) => event.type === "context-handoff.updated")).toBe(false);
958+
}),
959+
);
960+
}

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as Schema from "effect/Schema";
2525
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
2626
import * as ProjectService from "../project/ProjectService.ts";
2727
import * as ProviderAuthService from "../provider/ProviderAuthService.ts";
28+
import { parseCodexGoalCommand } from "./Adapters/codexGoalCommand.ts";
2829
import * as EventSink from "./EventSink.ts";
2930
import * as ContextHandoffService from "./ContextHandoffService.ts";
3031
import {
@@ -1222,11 +1223,17 @@ export const layer: Layer.Layer<
12221223
}),
12231224
),
12241225
);
1226+
// Native goal commands must reach Codex as the original message. Existing
1227+
// handoff and continuation delivery still takes precedence.
12251228
const deliverySession =
12261229
effectiveHandoffs.length === 0 &&
12271230
missedItems.length === 0 &&
12281231
restartNote === "" &&
1229-
!projection.thread.workspace &&
1232+
(!projection.thread.workspace ||
1233+
(session.driver === "codex" &&
1234+
message.attachments.length === 0 &&
1235+
run.restartContinuationOfRunId === undefined &&
1236+
parseCodexGoalCommand(message.text) !== null)) &&
12301237
!noteContinuation
12311238
? session
12321239
: makeDeliverySession(session, startWithHandoffs);

0 commit comments

Comments
 (0)