Skip to content

Commit dc47cb8

Browse files
committed
fix(server): preserve native commands when marking agent messages
1 parent bedf4e8 commit dc47cb8

5 files changed

Lines changed: 74 additions & 25 deletions

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ it.effect.each(storageCases)(
275275
const layerSessions = Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
276276
get: () =>
277277
Effect.succeedSome({
278+
driver: ProviderDriverKind.make("pi"),
278279
interruptTurn: () =>
279280
Effect.sync(() => {
280281
calls.push("interrupt");
@@ -334,6 +335,26 @@ it.effect.each(storageCases)(
334335
(yield* store.getProviderControlContext(threadId, target)).message?.text,
335336
"Use the smaller fix.",
336337
);
338+
const compactText = "/compact keep the tests";
339+
yield* store.apply({
340+
...messageEvent,
341+
id: EventId.make("control:agent-compact"),
342+
payload: {
343+
...messageEvent.payload,
344+
text: compactText,
345+
createdBy: "agent",
346+
creationSource: "mcp",
347+
senderThreadId: ThreadId.make("thread:sender"),
348+
},
349+
});
350+
yield* control.steer({
351+
threadId,
352+
providerThreadId,
353+
providerTurnId,
354+
providerSessionId,
355+
messageId,
356+
});
357+
assert.equal(calls.at(-1), compactText);
337358
const wrongSession = ProviderSessionId.make("session:wrong");
338359
assert.equal(
339360
(yield* Effect.exit(
@@ -357,7 +378,7 @@ it.effect.each(storageCases)(
357378
))._tag,
358379
"Failure",
359380
);
360-
assert.lengthOf(calls, 4);
381+
assert.lengthOf(calls, 5);
361382
const turn = context.providerTurn!;
362383
yield* store.apply({
363384
id: EventId.make("control:turn-completed"),
@@ -372,6 +393,7 @@ it.effect.each(storageCases)(
372393
"Use the smaller fix.",
373394
"reply",
374395
agentText,
396+
compactText,
375397
"interrupt",
376398
]);
377399
}).pipe(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ export const layer: Layer.Layer<
381381
text: message.text,
382382
records: message.context?.records ?? [],
383383
}),
384+
loaded.session.value.driver,
384385
),
385386
attachments: message.attachments,
386387
createdBy: message.createdBy,

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,19 +1031,45 @@ it.effect(
10311031
});
10321032
const cases = [
10331033
{ text: " /compact ", attachments: [], expected: "compact" },
1034+
{
1035+
text: "/compact keep the tests",
1036+
attachments: [],
1037+
expected: "command",
1038+
driver: ProviderDriverKind.make("pi"),
1039+
},
1040+
{
1041+
text: "/compact keep the tests",
1042+
attachments: [attachment],
1043+
expected: "command",
1044+
driver: ProviderDriverKind.make("pi"),
1045+
},
1046+
{
1047+
text: "Continue the work",
1048+
attachments: [],
1049+
expected: "prompt",
1050+
driver: ProviderDriverKind.make("pi"),
1051+
},
1052+
{
1053+
text: "/review",
1054+
attachments: [],
1055+
expected: "command",
1056+
driver: ProviderDriverKind.make("opencode"),
1057+
},
10341058
{ text: "/compact", attachments: [attachment], expected: "prompt" },
10351059
{ text: "/goal finish the work", attachments: [], expected: "goal" },
10361060
{ text: "/goal finish the work", attachments: [attachment], expected: "prompt" },
10371061
{ text: "Continue the work", attachments: [], expected: "prompt" },
1038-
].flatMap((testCase) =>
1039-
(
1040-
[
1041-
{ createdBy: "user", senderThreadId: ThreadId.make("thread:sender") },
1042-
{ createdBy: "agent", senderThreadId: ThreadId.make("thread:sender") },
1043-
{ createdBy: "agent", senderThreadId: undefined },
1044-
] as const
1045-
).map((origin) => ({ ...testCase, ...origin })),
1046-
);
1062+
]
1063+
.map((testCase) => ({ driver, ...testCase }))
1064+
.flatMap((testCase) =>
1065+
(
1066+
[
1067+
{ createdBy: "user", senderThreadId: ThreadId.make("thread:sender") },
1068+
{ createdBy: "agent", senderThreadId: ThreadId.make("thread:sender") },
1069+
{ createdBy: "agent", senderThreadId: undefined },
1070+
] as const
1071+
).map((origin) => ({ ...testCase, ...origin })),
1072+
);
10471073
for (const [index, testCase] of cases.entries()) {
10481074
const prompts: Array<ProviderAdapter.ProviderAdapterV2TurnInput> = [];
10491075
const threadId = ThreadId.make(`thread:compact-routing:${index}`);
@@ -1053,7 +1079,7 @@ it.effect(
10531079
appThread: rootAppThread(threadId),
10541080
providerSessionId: ProviderSessionId.make(`session:compact-routing:${index}`),
10551081
session: {
1056-
driver,
1082+
driver: testCase.driver,
10571083
events: Stream.never,
10581084
startTurn: (input: ProviderAdapter.ProviderAdapterV2TurnInput) =>
10591085
Effect.sync(() => {
@@ -1080,7 +1106,7 @@ it.effect(
10801106
} as OrchestrationV2CheckpointScope,
10811107
providerThread: {
10821108
id: ProviderThreadId.make(`provider-thread:compact-routing:${index}`),
1083-
driver,
1109+
driver: testCase.driver,
10841110
} as OrchestrationV2ProviderThread,
10851111
attempt: { id: attemptId, providerTurnId: null } as OrchestrationV2RunAttempt,
10861112
attemptId,
@@ -1112,6 +1138,7 @@ it.effect(
11121138
if (prompt !== undefined) {
11131139
if (
11141140
testCase.expected !== "goal" &&
1141+
testCase.expected !== "command" &&
11151142
testCase.createdBy === "agent" &&
11161143
testCase.senderThreadId !== undefined
11171144
) {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { withProviderMessageOrigin } from "./providerMessageOrigin.ts";
33
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
44
import {
55
isOrchestrationV2WorkActive,
6-
parseGoalCommand,
76
CommandId,
87
type EventId,
98
type ModelSelection,
@@ -1679,9 +1678,6 @@ export const layer: Layer.Layer<
16791678
const compact =
16801679
input.message.attachments.length === 0 &&
16811680
input.message.text.trim().toLowerCase() === "/compact";
1682-
const goalCommand =
1683-
(input.session.driver === "codex" || input.session.driver === "claudeAgent") &&
1684-
parseGoalCommand(input.message) !== null;
16851681
const startTurn = compact
16861682
? (input.session.compactThread?.(turnInput) ??
16871683
Effect.fail(
@@ -1697,9 +1693,11 @@ export const layer: Layer.Layer<
16971693
...turnInput,
16981694
message: {
16991695
...turnInput.message,
1700-
text: goalCommand
1701-
? turnInput.message.text
1702-
: withProviderMessageOrigin(turnInput.message, turnInput.message.text),
1696+
text: withProviderMessageOrigin(
1697+
turnInput.message,
1698+
turnInput.message.text,
1699+
input.session.driver,
1700+
),
17031701
},
17041702
});
17051703
yield* Effect.andThen(shouldStart, startTurn).pipe(
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import type { OrchestrationV2Actor, ThreadId } from "@t3tools/contracts";
1+
import type { ProviderDriverKind } from "@t3tools/contracts";
2+
import type { ProviderAdapterV2TurnMessage } from "@t3tools/provider-core/server/ProviderAdapter";
23

3-
/** Carry durable cross-thread provenance into provider-visible text without rewriting the timeline. */
44
export function withProviderMessageOrigin(
5-
message: {
6-
readonly createdBy: OrchestrationV2Actor;
7-
readonly senderThreadId?: ThreadId | undefined;
8-
},
5+
message: Pick<ProviderAdapterV2TurnMessage, "createdBy" | "senderThreadId" | "attachments">,
96
text: string,
7+
driver: ProviderDriverKind,
108
): string {
119
if (message.createdBy !== "agent" || message.senderThreadId === undefined) return text;
10+
// Native commands need their leading slash; Pi parses commands even with attachments.
11+
if (text.trimStart().startsWith("/") && (message.attachments.length === 0 || driver === "pi"))
12+
return text;
1213
return `[Message from another thread's agent (thread ${JSON.stringify(message.senderThreadId)}). This is agent-authored coordination, not a message from the human user.]\n\n${text}`;
1314
}

0 commit comments

Comments
 (0)