Skip to content

Commit fdd0618

Browse files
fix(server): keep native goal routing capability-based for agent messages
Share one native-goal predicate between run execution and turn start so cross-thread agent goals on capability-declaring drivers such as Antigravity keep `/goal` first, and only read session capabilities for cross-thread agent messages. Cut the routing tests to explicit cases. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1c83b69 commit fdd0618

5 files changed

Lines changed: 81 additions & 203 deletions

File tree

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

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,9 @@ it.effect.each(storageCases)(
214214
assert.isUndefined((yield* store.getRunningTurnContext(threadId)).providerTurn);
215215
}).pipe(Effect.provide(storeLayer)),
216216
);
217-
it.effect.each(
218-
storageCases.flatMap((storageCase) => [
219-
{
220-
...storageCase,
221-
commandDriver: ProviderDriverKind.make("pi"),
222-
commandText: "/compact keep the tests",
223-
},
224-
{ ...storageCase, commandDriver: ProviderDriverKind.make("opencode"), commandText: "/review" },
225-
{ ...storageCase, commandDriver: driver, commandText: "/goal finish the work" },
226-
]),
227-
)(
228-
"$storage: controls and replies read only their exact durable targets ($commandDriver)",
229-
({ storage, storeLayer, commandDriver, commandText }) =>
217+
it.effect.each(storageCases)(
218+
"$storage: controls and replies read only their exact durable targets",
219+
({ storage, storeLayer }) =>
230220
Effect.gen(function* () {
231221
const store = yield* ProjectionStore.ProjectionStoreV2;
232222
const now = yield* DateTime.now;
@@ -285,7 +275,7 @@ it.effect.each(
285275
const layerSessions = Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
286276
get: () =>
287277
Effect.succeedSome({
288-
driver: commandDriver,
278+
driver,
289279
interruptTurn: () =>
290280
Effect.sync(() => {
291281
calls.push("interrupt");
@@ -345,46 +335,6 @@ it.effect.each(
345335
(yield* store.getProviderControlContext(threadId, target)).message?.text,
346336
"Use the smaller fix.",
347337
);
348-
yield* store.apply({
349-
...messageEvent,
350-
id: EventId.make("control:agent-compact"),
351-
payload: {
352-
...messageEvent.payload,
353-
text: commandText,
354-
attachments:
355-
commandDriver === "codex"
356-
? []
357-
: [
358-
{
359-
type: "file",
360-
id: "command-attachment",
361-
name: "notes.txt",
362-
mimeType: "text/plain",
363-
sizeBytes: 1,
364-
},
365-
],
366-
createdBy: "agent",
367-
creationSource: "mcp",
368-
senderThreadId: ThreadId.make("thread:sender"),
369-
},
370-
});
371-
yield* control.steer({
372-
threadId,
373-
providerThreadId,
374-
providerTurnId,
375-
providerSessionId,
376-
messageId,
377-
});
378-
const deliveredCommandText = calls.at(-1);
379-
if (commandDriver === "pi") {
380-
assert.isTrue(deliveredCommandText?.startsWith("/compact [Message from"));
381-
assert.include(deliveredCommandText, 'thread "thread:sender"');
382-
assert.include(deliveredCommandText, "not a message from the human user");
383-
assert.isTrue(deliveredCommandText?.endsWith("\n\nkeep the tests"));
384-
} else {
385-
assert.include(deliveredCommandText, "not a message from the human user");
386-
assert.isTrue(deliveredCommandText?.endsWith(`\n\n${commandText}`));
387-
}
388338
const wrongSession = ProviderSessionId.make("session:wrong");
389339
assert.equal(
390340
(yield* Effect.exit(
@@ -408,7 +358,7 @@ it.effect.each(
408358
))._tag,
409359
"Failure",
410360
);
411-
assert.lengthOf(calls, 5);
361+
assert.lengthOf(calls, 4);
412362
const turn = context.providerTurn!;
413363
yield* store.apply({
414364
id: EventId.make("control:turn-completed"),
@@ -423,7 +373,6 @@ it.effect.each(
423373
"Use the smaller fix.",
424374
"reply",
425375
agentText,
426-
deliveredCommandText,
427376
"interrupt",
428377
]);
429378
}).pipe(

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
makeProviderFailure,
6262
} from "@t3tools/provider-core/server/failure";
6363
import * as RunExecutionService from "./RunExecutionService.ts";
64+
import { isCrossThreadAgentMessage } from "./providerMessageOrigin.ts";
6465
import * as RuntimePolicy from "./RuntimePolicy.ts";
6566
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
6667
import {
@@ -870,10 +871,7 @@ export const layer: Layer.Layer<
870871
records: message.context?.records ?? [],
871872
});
872873
// Native goal commands go verbatim; handoff history waits a turn.
873-
const goalDriver =
874-
session.driver === "codex" ||
875-
session.driver === "claudeAgent" ||
876-
session.providerSession.capabilities.turns.supportsNativeGoals === true;
874+
const goalDriver = RunExecutionService.acceptsNativeGoalCommands(session);
877875
const goalCommand = goalDriver && parseGoalCommand(message) !== null;
878876
// The provider turn marks cancellation work delivered; restart continuations use their own text or resume natively.
879877
const noteContinuation = isRestartNoteContinuation(
@@ -1756,11 +1754,9 @@ export const layer: Layer.Layer<
17561754
.filter((part) => part !== "")
17571755
.join("\n\n");
17581756
const { restartContinuationOfRunId: _resumedRunId, ...promptedInput } = turnInput;
1759-
const messageHeading =
1760-
turnInput.message.createdBy === "agent" &&
1761-
turnInput.message.senderThreadId !== undefined
1762-
? "Agent message:"
1763-
: "User message:";
1757+
const messageHeading = isCrossThreadAgentMessage(turnInput.message)
1758+
? "Agent message:"
1759+
: "User message:";
17641760
yield* start({
17651761
...(noteContinuation ? promptedInput : turnInput),
17661762
message: {

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

Lines changed: 39 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3-
parseGoalCommand,
43
CheckpointScopeId,
54
ChatAttachmentId,
65
ChatFileAttachment,
@@ -1043,7 +1042,7 @@ it.effect("fails the run when its ownership check cannot be read before calling
10431042
}),
10441043
);
10451044

1046-
it.effect("preserves native compaction and goal command routing when labeling agent messages", () =>
1045+
it.effect("routes compaction and goal commands and marks cross-thread agent messages", () =>
10471046
Effect.gen(function* () {
10481047
const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
10491048
const calls: Array<string> = [];
@@ -1054,113 +1053,67 @@ it.effect("preserves native compaction and goal command routing when labeling ag
10541053
mimeType: "text/plain",
10551054
sizeBytes: 1,
10561055
});
1057-
const cases = [
1058-
{ text: " /compact ", attachments: [], expected: "compact" },
1056+
const origin =
1057+
'[Message from another thread\'s agent (thread "thread:sender").' +
1058+
" This is agent-authored coordination, not a message from the human user.]";
1059+
const agent = { createdBy: "agent", senderThreadId: ThreadId.make("thread:sender") } as const;
1060+
const cases: ReadonlyArray<{
1061+
readonly text: string;
1062+
readonly expected: string;
1063+
readonly attachments?: ReadonlyArray<ChatFileAttachment>;
1064+
readonly createdBy?: "user" | "agent";
1065+
readonly senderThreadId?: ThreadId;
1066+
readonly driver?: ProviderDriverKind;
1067+
readonly nativeGoals?: boolean;
1068+
}> = [
1069+
{ text: " /compact ", expected: "compact" },
1070+
{ text: "/compact", attachments: [attachment], expected: "/compact" },
1071+
{ text: "Continue the work", expected: "Continue the work" },
1072+
{ createdBy: "agent", text: "Continue the work", expected: "Continue the work" },
1073+
{ ...agent, text: "Continue the work", expected: `${origin}\n\nContinue the work` },
10591074
{
1060-
text: "/workspace/src/main.ts is the file to update.",
1061-
attachments: [],
1062-
expected: "prompt",
1063-
},
1064-
{
1065-
text: "/compact keep the tests",
1066-
attachments: [],
1067-
expected: "command",
1075+
...agent,
10681076
driver: ProviderDriverKind.make("pi"),
1069-
},
1070-
{
10711077
text: "/compact keep the tests",
1072-
attachments: [attachment],
1073-
expected: "command",
1074-
driver: ProviderDriverKind.make("pi"),
1075-
},
1076-
{
1077-
text: "Continue the work",
1078-
attachments: [],
1079-
expected: "prompt",
1080-
driver: ProviderDriverKind.make("pi"),
1081-
},
1082-
{
1083-
text: "/review",
1084-
attachments: [attachment],
1085-
expected: "prompt",
1086-
driver: ProviderDriverKind.make("opencode"),
1078+
expected: `/compact ${origin}\n\nkeep the tests`,
10871079
},
1080+
{ ...agent, text: "/goal finish", expected: `/goal ${origin}\n\nfinish` },
10881081
{
1089-
text: "/README.md is the file to update.",
1090-
attachments: [],
1091-
expected: "prompt",
1092-
driver: ProviderDriverKind.make("opencode"),
1093-
},
1094-
{ text: "/compact", attachments: [attachment], expected: "prompt" },
1095-
{ text: "/goal finish the work", attachments: [], expected: "goal" },
1096-
{ text: " /goal finish the work ", attachments: [], expected: "goal" },
1097-
{
1098-
text: "/goal finish the work",
1099-
attachments: [],
1100-
expected: "goal",
1101-
driver: ProviderDriverKind.make("claudeAgent"),
1102-
},
1103-
{
1104-
text: "/goal finish the work",
1105-
attachments: [],
1106-
expected: "goal",
1107-
driver: ProviderDriverKind.make("opencode"),
1108-
nativeGoals: true,
1109-
},
1110-
{
1111-
text: "/goal clear",
1112-
attachments: [],
1113-
expected: "control",
1114-
driver: ProviderDriverKind.make("opencode"),
1082+
...agent,
1083+
driver: ProviderDriverKind.make("antigravity"),
11151084
nativeGoals: true,
1085+
text: "/goal finish",
1086+
expected: `/goal ${origin}\n\nfinish`,
11161087
},
11171088
{
1118-
text: "/goal finish the work",
1119-
attachments: [],
1120-
expected: "prompt",
1089+
...agent,
11211090
driver: ProviderDriverKind.make("opencode"),
11221091
nativeGoals: false,
1092+
text: "/goal finish",
1093+
expected: `${origin}\n\n/goal finish`,
11231094
},
1124-
...["/goal", "/goal edit", "/goal clear", "/goal pause", "/goal resume"].map((text) => ({
1125-
text,
1126-
attachments: [],
1127-
expected: "control",
1128-
})),
1129-
{ text: "/goal finish the work", attachments: [attachment], expected: "prompt" },
1130-
{ text: "Continue the work", attachments: [], expected: "prompt" },
1131-
]
1132-
.map((testCase) => ({ driver, ...testCase }))
1133-
.flatMap((testCase) =>
1134-
(
1135-
[
1136-
{ createdBy: "user", senderThreadId: ThreadId.make("thread:sender") },
1137-
{ createdBy: "agent", senderThreadId: ThreadId.make("thread:sender") },
1138-
{ createdBy: "agent", senderThreadId: undefined },
1139-
] as const
1140-
).map((origin) => ({ ...testCase, ...origin })),
1141-
);
1095+
{ ...agent, text: "/goal clear", expected: "/goal clear" },
1096+
];
11421097
for (const [index, testCase] of cases.entries()) {
1143-
const prompts: Array<ProviderAdapter.ProviderAdapterV2TurnInput> = [];
11441098
const threadId = ThreadId.make(`thread:compact-routing:${index}`);
11451099
const attemptId = RunAttemptId.make(`attempt:compact-routing:${index}`);
1100+
const caseDriver = testCase.driver ?? driver;
11461101
yield* runExecution.startRootRun({
11471102
commandId: CommandId.make(`command:compact-routing:${index}`),
11481103
appThread: rootAppThread(threadId),
11491104
providerSessionId: ProviderSessionId.make(`session:compact-routing:${index}`),
11501105
session: {
1151-
driver: testCase.driver,
1106+
driver: caseDriver,
11521107
providerSession: {
11531108
capabilities: { turns: { supportsNativeGoals: testCase.nativeGoals } },
11541109
},
11551110
events: Stream.never,
11561111
startTurn: (input: ProviderAdapter.ProviderAdapterV2TurnInput) =>
11571112
Effect.sync(() => {
1158-
prompts.push(input);
1159-
calls.push("prompt");
1113+
calls.push(input.message.text);
11601114
}),
1161-
compactThread: (input: ProviderAdapter.ProviderAdapterV2TurnInput) =>
1115+
compactThread: () =>
11621116
Effect.sync(() => {
1163-
assert.equal(input.message.text, testCase.text);
11641117
calls.push("compact");
11651118
}),
11661119
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime,
@@ -1178,16 +1131,16 @@ it.effect("preserves native compaction and goal command routing when labeling ag
11781131
} as OrchestrationV2CheckpointScope,
11791132
providerThread: {
11801133
id: ProviderThreadId.make(`provider-thread:compact-routing:${index}`),
1181-
driver: testCase.driver,
1134+
driver: caseDriver,
11821135
} as OrchestrationV2ProviderThread,
11831136
attempt: { id: attemptId, providerTurnId: null } as OrchestrationV2RunAttempt,
11841137
attemptId,
11851138
providerTurnOrdinal: 1,
11861139
message: {
11871140
messageId: MessageId.make(`message:compact-routing:${index}`),
11881141
text: testCase.text,
1189-
attachments: testCase.attachments,
1190-
createdBy: testCase.createdBy,
1142+
attachments: testCase.attachments ?? [],
1143+
createdBy: testCase.createdBy ?? "user",
11911144
creationSource: "web",
11921145
...(testCase.senderThreadId === undefined
11931146
? {}
@@ -1206,34 +1159,10 @@ it.effect("preserves native compaction and goal command routing when labeling ag
12061159
},
12071160
},
12081161
});
1209-
const prompt = prompts[0];
1210-
if (prompt !== undefined) {
1211-
if (
1212-
testCase.expected !== "control" &&
1213-
testCase.createdBy === "agent" &&
1214-
testCase.senderThreadId !== undefined
1215-
) {
1216-
assert.include(prompt.message.text, 'thread "thread:sender"');
1217-
assert.include(prompt.message.text, "not a message from the human user");
1218-
if (testCase.expected === "goal") {
1219-
const goal = parseGoalCommand(prompt.message);
1220-
assert.equal(goal?.type, "set");
1221-
if (goal?.type === "set") assert.isTrue(goal.objective.endsWith("\n\nfinish the work"));
1222-
} else if (testCase.expected === "command") {
1223-
assert.isTrue(prompt.message.text.startsWith("/compact [Message from"));
1224-
assert.isTrue(prompt.message.text.endsWith("\n\nkeep the tests"));
1225-
} else {
1226-
assert.isTrue(prompt.message.text.endsWith(`\n\n${testCase.text}`));
1227-
}
1228-
} else {
1229-
assert.equal(prompt.message.text, testCase.text);
1230-
}
1231-
assert.deepEqual(prompt.message.attachments, testCase.attachments);
1232-
}
12331162
}
12341163
assert.deepEqual(
12351164
calls,
1236-
cases.map((testCase) => (testCase.expected === "compact" ? "compact" : "prompt")),
1165+
cases.map((testCase) => testCase.expected),
12371166
);
12381167
}).pipe(Effect.provide(layerRunExecutionTest)),
12391168
);

0 commit comments

Comments
 (0)