Skip to content

Commit cd17a5f

Browse files
committed
fix(server): 原生命令正文保留跨线程消息来源
- 在原生目标正文和 Pi 压缩指令中保留发送线程及 agent 来源标记。 - 保持命令前缀、目标控制操作及人工消息原文不变。 - 扩展现有启动与引导回归,变更范围检查和 158 项测试通过。
1 parent 0804165 commit cd17a5f

4 files changed

Lines changed: 57 additions & 16 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,12 @@ it.effect.each(
376376
messageId,
377377
});
378378
const deliveredCommandText = calls.at(-1);
379-
if (commandDriver === "pi") assert.equal(deliveredCommandText, commandText);
380-
else {
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 {
381385
assert.include(deliveredCommandText, "not a message from the human user");
382386
assert.isTrue(deliveredCommandText?.endsWith(`\n\n${commandText}`));
383387
}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3+
parseGoalCommand,
34
CheckpointScopeId,
45
ChatAttachmentId,
56
ChatFileAttachment,
@@ -1083,6 +1084,18 @@ it.effect(
10831084
},
10841085
{ text: "/compact", attachments: [attachment], expected: "prompt" },
10851086
{ text: "/goal finish the work", attachments: [], expected: "goal" },
1087+
{ text: " /goal finish the work ", attachments: [], expected: "goal" },
1088+
{
1089+
text: "/goal finish the work",
1090+
attachments: [],
1091+
expected: "goal",
1092+
driver: ProviderDriverKind.make("claudeAgent"),
1093+
},
1094+
...["/goal", "/goal edit", "/goal clear", "/goal pause", "/goal resume"].map((text) => ({
1095+
text,
1096+
attachments: [],
1097+
expected: "control",
1098+
})),
10861099
{ text: "/goal finish the work", attachments: [attachment], expected: "prompt" },
10871100
{ text: "Continue the work", attachments: [], expected: "prompt" },
10881101
]
@@ -1163,14 +1176,23 @@ it.effect(
11631176
const prompt = prompts[0];
11641177
if (prompt !== undefined) {
11651178
if (
1166-
testCase.expected !== "goal" &&
1167-
testCase.expected !== "command" &&
1179+
testCase.expected !== "control" &&
11681180
testCase.createdBy === "agent" &&
11691181
testCase.senderThreadId !== undefined
11701182
) {
11711183
assert.include(prompt.message.text, 'thread "thread:sender"');
11721184
assert.include(prompt.message.text, "not a message from the human user");
1173-
assert.isTrue(prompt.message.text.endsWith(`\n\n${testCase.text}`));
1185+
if (testCase.expected === "goal") {
1186+
const goal = parseGoalCommand(prompt.message);
1187+
assert.equal(goal?.type, "set");
1188+
if (goal?.type === "set")
1189+
assert.isTrue(goal.objective.endsWith("\n\nfinish the work"));
1190+
} else if (testCase.expected === "command") {
1191+
assert.isTrue(prompt.message.text.startsWith("/compact [Message from"));
1192+
assert.isTrue(prompt.message.text.endsWith("\n\nkeep the tests"));
1193+
} else {
1194+
assert.isTrue(prompt.message.text.endsWith(`\n\n${testCase.text}`));
1195+
}
11741196
} else {
11751197
assert.equal(prompt.message.text, testCase.text);
11761198
}

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,8 +1730,9 @@ export const layer: Layer.Layer<
17301730
input.message.attachments.length === 0 &&
17311731
input.message.text.trim().toLowerCase() === "/compact";
17321732
const goalCommand =
1733-
(input.session.driver === "codex" || input.session.driver === "claudeAgent") &&
1734-
parseGoalCommand(input.message) !== null;
1733+
input.session.driver === "codex" || input.session.driver === "claudeAgent"
1734+
? parseGoalCommand(input.message)
1735+
: null;
17351736
const startTurn = compact
17361737
? (input.session.compactThread?.(turnInput) ??
17371738
Effect.fail(
@@ -1747,13 +1748,22 @@ export const layer: Layer.Layer<
17471748
...turnInput,
17481749
message: {
17491750
...turnInput.message,
1750-
text: goalCommand
1751-
? turnInput.message.text
1752-
: withProviderMessageOrigin(
1753-
turnInput.message,
1754-
turnInput.message.text,
1755-
input.session.driver,
1756-
),
1751+
text:
1752+
goalCommand?.type === "set" &&
1753+
turnInput.message.createdBy === "agent" &&
1754+
turnInput.message.senderThreadId !== undefined
1755+
? `/goal ${withProviderMessageOrigin(
1756+
turnInput.message,
1757+
goalCommand.objective,
1758+
input.session.driver,
1759+
)}`
1760+
: goalCommand !== null
1761+
? turnInput.message.text
1762+
: withProviderMessageOrigin(
1763+
turnInput.message,
1764+
turnInput.message.text,
1765+
input.session.driver,
1766+
),
17571767
},
17581768
});
17591769
yield* Effect.andThen(shouldStart, startTurn).pipe(

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export function withProviderMessageOrigin(
77
driver: ProviderDriverKind,
88
): string {
99
if (message.createdBy !== "agent" || message.senderThreadId === undefined) return text;
10-
if (driver === "pi" && /^\/compact(?:\s|$)/u.test(text.trim())) return text;
11-
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}`;
10+
const heading = `[Message from another thread's agent (thread ${JSON.stringify(message.senderThreadId)}). This is agent-authored coordination, not a message from the human user.]`;
11+
const compact = driver === "pi" ? /^\/compact(?:\s+([\s\S]*))?$/u.exec(text.trim()) : null;
12+
if (compact !== null) {
13+
const instructions = compact[1]?.trim();
14+
return instructions ? `/compact ${heading}\n\n${instructions}` : text;
15+
}
16+
return `${heading}\n\n${text}`;
1217
}

0 commit comments

Comments
 (0)