Skip to content

Commit 980fe22

Browse files
committed
fix(server): 在命令分流后标注代理消息来源
保留跨线程 /compact 的原生压缩路径,仅给实际提示词附加来源。 上下文交接继续保留已标注正文,更新委派回放的精确请求预期。 受影响检查通过 81 项测试,额外 MCP 委派回放及分流验证共 50 项通过。
1 parent 070e25a commit 980fe22

5 files changed

Lines changed: 41 additions & 47 deletions

File tree

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -772,36 +772,6 @@ function makeLocalCommandHarness(input: {
772772
};
773773
}
774774

775-
effectIt.effect.each([
776-
{ createdBy: "agent", senderThreadId: ThreadId.make("thread:sender"), marked: true },
777-
{ createdBy: "user", senderThreadId: ThreadId.make("thread:sender"), marked: false },
778-
{ createdBy: "agent", marked: false },
779-
] as const)("preserves message origin at turn start: %j", (origin) =>
780-
Effect.gen(function* () {
781-
const text = "Please merge the PR now.";
782-
const harness = makeLocalCommandHarness({
783-
text,
784-
previousTurn: { runStatus: "completed", turnStatus: "completed" },
785-
});
786-
const projection = harness.projection();
787-
const { marked, ...provenance } = origin;
788-
harness.setProjection({
789-
...projection,
790-
messages: projection.messages.map((message) => ({ ...message, ...provenance })),
791-
});
792-
yield* harness.start;
793-
const delivered = harness.startRootRun.mock.calls[0]?.[0].message.text;
794-
if (marked) {
795-
expect(delivered).toContain('thread "thread:sender"');
796-
expect(delivered).toContain("not a message from the human user");
797-
expect(delivered).toMatch(/\n\nPlease merge the PR now\.$/);
798-
} else {
799-
expect(delivered).toBe(text);
800-
}
801-
expect(harness.projection().messages.at(-1)?.text).toBe(text);
802-
}),
803-
);
804-
805775
effectIt.effect("recovers an abandoned failed turn before continuing its native conversation", () =>
806776
Effect.gen(function* () {
807777
const harness = makeLocalCommandHarness({

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import * as ProviderAuthService from "../provider/ProviderAuthService.ts";
3232
import * as ServerSettings from "../serverSettings.ts";
3333
import { makeIsManagedWorktreePath } from "../worktreesDirectory.ts";
3434
import * as EventSink from "./EventSink.ts";
35-
import { withProviderMessageOrigin } from "./providerMessageOrigin.ts";
3635
import * as ContextHandoffService from "./ContextHandoffService.ts";
3736
import {
3837
DEFAULT_HANDOFF_TOKEN_CAP,
@@ -1026,13 +1025,10 @@ export const layer: Layer.Layer<
10261025
const routableSubagents = projection.subagents.filter((subagent) =>
10271026
RunExecutionService.canRouteRelatedSubagent(subagent.status),
10281027
);
1029-
const userText = withProviderMessageOrigin(
1030-
message,
1031-
projectComposerContextForProvider({
1032-
text: message.text,
1033-
records: message.context?.records ?? [],
1034-
}),
1035-
);
1028+
const userText = projectComposerContextForProvider({
1029+
text: message.text,
1030+
records: message.context?.records ?? [],
1031+
});
10361032
// The provider turn marks cancellation work delivered; restart continuations use their own text or resume natively.
10371033
const noteContinuation = isRestartNoteContinuation(
10381034
run,
@@ -1247,7 +1243,10 @@ export const layer: Layer.Layer<
12471243
...(noteContinuation ? promptedInput : turnInput),
12481244
message: {
12491245
...turnInput.message,
1250-
text: context === "" ? userText : `${context}\n\nUser message:\n${userText}`,
1246+
text:
1247+
context === ""
1248+
? turnInput.message.text
1249+
: `${context}\n\nUser message:\n${turnInput.message.text}`,
12511250
},
12521251
}).pipe(
12531252
// Clear pending handoff markers for refused prompts so the next turn retains the native session.

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,13 @@ it.effect(
10281028
{ text: " /compact ", attachments: [], expected: "compact" },
10291029
{ text: "/compact", attachments: [attachment], expected: "prompt" },
10301030
{ text: "Continue the work", attachments: [], expected: "prompt" },
1031-
];
1031+
].flatMap((testCase) =>
1032+
[
1033+
{ createdBy: "user", senderThreadId: ThreadId.make("thread:sender") },
1034+
{ createdBy: "agent", senderThreadId: ThreadId.make("thread:sender") },
1035+
{ createdBy: "agent" },
1036+
].map((origin) => ({ ...testCase, ...origin })),
1037+
);
10321038
for (const [index, testCase] of cases.entries()) {
10331039
const threadId = ThreadId.make(`thread:compact-routing:${index}`);
10341040
const attemptId = RunAttemptId.make(`attempt:compact-routing:${index}`);
@@ -1038,12 +1044,21 @@ it.effect(
10381044
providerSessionId: ProviderSessionId.make(`session:compact-routing:${index}`),
10391045
session: {
10401046
events: Stream.never,
1041-
startTurn: () =>
1047+
startTurn: (input: ProviderAdapter.ProviderAdapterV2TurnInput) =>
10421048
Effect.sync(() => {
1049+
if (testCase.createdBy === "agent" && testCase.senderThreadId !== undefined) {
1050+
assert.include(input.message.text, 'thread "thread:sender"');
1051+
assert.include(input.message.text, "not a message from the human user");
1052+
assert.isTrue(input.message.text.endsWith(`\n\n${testCase.text}`));
1053+
} else {
1054+
assert.equal(input.message.text, testCase.text);
1055+
}
1056+
assert.deepEqual(input.message.attachments, testCase.attachments);
10431057
calls.push("prompt");
10441058
}),
1045-
compactThread: () =>
1059+
compactThread: (input: ProviderAdapter.ProviderAdapterV2TurnInput) =>
10461060
Effect.sync(() => {
1061+
assert.equal(input.message.text, testCase.text);
10471062
calls.push("compact");
10481063
}),
10491064
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime,
@@ -1070,8 +1085,11 @@ it.effect(
10701085
messageId: MessageId.make(`message:compact-routing:${index}`),
10711086
text: testCase.text,
10721087
attachments: testCase.attachments,
1073-
createdBy: "user",
1088+
createdBy: testCase.createdBy as "user" | "agent",
10741089
creationSource: "web",
1090+
...(testCase.senderThreadId === undefined
1091+
? {}
1092+
: { senderThreadId: testCase.senderThreadId }),
10751093
},
10761094
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
10771095
runtimePolicy: {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { makeAssistantStreamingFilter } from "./assistantStreaming.ts";
2+
import { withProviderMessageOrigin } from "./providerMessageOrigin.ts";
23
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
34
import {
45
isOrchestrationV2WorkActive,
@@ -1638,7 +1639,13 @@ export const layer: Layer.Layer<
16381639
cause: "This provider does not support context compaction.",
16391640
}),
16401641
))
1641-
: input.session.startTurn(turnInput);
1642+
: input.session.startTurn({
1643+
...turnInput,
1644+
message: {
1645+
...turnInput.message,
1646+
text: withProviderMessageOrigin(turnInput.message, turnInput.message.text),
1647+
},
1648+
});
16421649
yield* Effect.andThen(shouldStart, startTurn).pipe(
16431650
// Wait for acceptance so abandoned startup cannot leave a refresher
16441651
// on the already-detached ingestion fiber.

‎apps/server/src/orchestration-v2/testkit/fixtures/delegated_task_status/codex_transcript.ndjson‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{"type":"emit_inbound","label":"parent.turn/started","frame":{"method":"turn/started","params":{"threadId":"019f1000-0000-7000-8000-000000000001","turn":{"id":"019f1000-0000-7000-8000-000000000002","items":[],"status":"inProgress","error":null,"startedAt":1776739467,"completedAt":null,"durationMs":null}}}}
1212
{"type":"expect_outbound","label":"child.thread/start","frame":{"id":4,"method":"thread/start","params":{"config":{"tools.update_plan.enabled":true,"mcp_optional_startup_grace_ms":10000}}}}
1313
{"type":"emit_inbound","label":"child.thread/start","frame":{"id":4,"result":{"thread":{"id":"019f1000-0000-7000-8000-000000000003","forkedFromId":null,"preview":"","ephemeral":false,"modelProvider":"openai","createdAt":1776739468,"updatedAt":1776739468,"status":{"type":"idle"},"path":"/home/replay-user/.codex/sessions/delegated-task-status-child.jsonl","cwd":"/home/replay-user/t3code","cliVersion":"0.120.0","source":"vscode","agentNickname":null,"agentRole":null,"gitInfo":null,"name":null,"turns":[]},"model":"gpt-5.4","modelProvider":"openai","serviceTier":"fast","cwd":"/home/replay-user/t3code","approvalPolicy":"on-request","approvalsReviewer":"user","sandbox":{"type":"workspaceWrite","writableRoots":[],"readOnlyAccess":{"type":"fullAccess"},"networkAccess":false,"excludeTmpdirEnvVar":false,"excludeSlashTmp":false},"reasoningEffort":"xhigh"}}}
14-
{"type":"expect_outbound","label":"child.original.turn/start","frame":{"id":5,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"Inspect the delegated API boundary and return the result.","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"019f1000-0000-7000-8000-000000000003","approvalsReviewer":"user","summary":"detailed"}}}
14+
{"type":"expect_outbound","label":"child.original.turn/start","frame":{"id":5,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"[Message from another thread's agent (thread \"thread:mcp-orchestrator-parent\"). This is agent-authored coordination, not a message from the human user.]\n\nInspect the delegated API boundary and return the result.","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"019f1000-0000-7000-8000-000000000003","approvalsReviewer":"user","summary":"detailed"}}}
1515
{"type":"emit_inbound","label":"child.thread/started","frame":{"method":"thread/started","params":{"thread":{"id":"019f1000-0000-7000-8000-000000000003","forkedFromId":null,"preview":"","ephemeral":false,"modelProvider":"openai","createdAt":1776739468,"updatedAt":1776739468,"status":{"type":"idle"},"path":"/home/replay-user/.codex/sessions/delegated-task-status-child.jsonl","cwd":"/home/replay-user/t3code","cliVersion":"0.120.0","source":"vscode","agentNickname":null,"agentRole":null,"gitInfo":null,"name":null,"turns":[]}}}}
1616
{"type":"emit_inbound","label":"child.original.turn/start","frame":{"id":5,"result":{"turn":{"id":"019f1000-0000-7000-8000-000000000004","items":[],"status":"inProgress","error":null,"startedAt":null,"completedAt":null,"durationMs":null}}}}
1717
{"type":"emit_inbound","label":"child.original.thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","status":{"type":"active","activeFlags":[]}}}}
@@ -21,15 +21,15 @@
2121
{"type":"emit_inbound","label":"child.original.item/completed","frame":{"method":"item/completed","params":{"item":{"type":"agentMessage","id":"msg_delegated_task_status_original","text":"Delegated API boundary inspected.","phase":"final_answer","memoryCitation":null},"threadId":"019f1000-0000-7000-8000-000000000003","turnId":"019f1000-0000-7000-8000-000000000004"}}}
2222
{"type":"emit_inbound","label":"child.original.thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","status":{"type":"idle"}}}}
2323
{"type":"emit_inbound","label":"child.original.turn/completed","frame":{"method":"turn/completed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","turn":{"id":"019f1000-0000-7000-8000-000000000004","items":[],"status":"completed","error":null,"startedAt":1776739468,"completedAt":1776739469,"durationMs":1000}}}}
24-
{"type":"expect_outbound","label":"child.running.turn/start","frame":{"id":6,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"Remain active until the parent cancels this delegated task.","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"019f1000-0000-7000-8000-000000000003","approvalsReviewer":"user","summary":"detailed"}}}
24+
{"type":"expect_outbound","label":"child.running.turn/start","frame":{"id":6,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"[Message from another thread's agent (thread \"thread:mcp-orchestrator-parent\"). This is agent-authored coordination, not a message from the human user.]\n\nRemain active until the parent cancels this delegated task.","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"019f1000-0000-7000-8000-000000000003","approvalsReviewer":"user","summary":"detailed"}}}
2525
{"type":"emit_inbound","label":"child.running.turn/start","frame":{"id":6,"result":{"turn":{"id":"019f1000-0000-7000-8000-000000000005","items":[],"status":"inProgress","error":null,"startedAt":null,"completedAt":null,"durationMs":null}}}}
2626
{"type":"emit_inbound","label":"child.running.thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","status":{"type":"active","activeFlags":[]}}}}
2727
{"type":"emit_inbound","label":"child.running.turn/started","frame":{"method":"turn/started","params":{"threadId":"019f1000-0000-7000-8000-000000000003","turn":{"id":"019f1000-0000-7000-8000-000000000005","items":[],"status":"inProgress","error":null,"startedAt":1776739470,"completedAt":null,"durationMs":null}}}}
2828
{"type":"expect_outbound","label":"child.running.turn/interrupt","frame":{"id":7,"method":"turn/interrupt","params":{"threadId":"019f1000-0000-7000-8000-000000000003","turnId":"019f1000-0000-7000-8000-000000000005"}}}
2929
{"type":"emit_inbound","label":"child.running.turn/interrupt","frame":{"id":7,"result":{}}}
3030
{"type":"emit_inbound","label":"child.running.thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","status":{"type":"idle"}}}}
3131
{"type":"emit_inbound","label":"child.running.turn/completed","frame":{"method":"turn/completed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","turn":{"id":"019f1000-0000-7000-8000-000000000005","items":[],"status":"interrupted","error":null,"startedAt":1776739470,"completedAt":1776739471,"durationMs":1000}}}}
32-
{"type":"expect_outbound","label":"child.queued.turn/start","frame":{"id":8,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"Complete the queued follow-up and return the final result.","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"019f1000-0000-7000-8000-000000000003","approvalsReviewer":"user","summary":"detailed"}}}
32+
{"type":"expect_outbound","label":"child.queued.turn/start","frame":{"id":8,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"[Message from another thread's agent (thread \"thread:mcp-orchestrator-parent\"). This is agent-authored coordination, not a message from the human user.]\n\nComplete the queued follow-up and return the final result.","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"019f1000-0000-7000-8000-000000000003","approvalsReviewer":"user","summary":"detailed"}}}
3333
{"type":"emit_inbound","label":"child.queued.turn/start","frame":{"id":8,"result":{"turn":{"id":"019f1000-0000-7000-8000-000000000006","items":[],"status":"inProgress","error":null,"startedAt":null,"completedAt":null,"durationMs":null}}}}
3434
{"type":"emit_inbound","label":"child.queued.thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"019f1000-0000-7000-8000-000000000003","status":{"type":"active","activeFlags":[]}}}}
3535
{"type":"emit_inbound","label":"child.queued.turn/started","frame":{"method":"turn/started","params":{"threadId":"019f1000-0000-7000-8000-000000000003","turn":{"id":"019f1000-0000-7000-8000-000000000006","items":[],"status":"inProgress","error":null,"startedAt":1776739472,"completedAt":null,"durationMs":null}}}}

0 commit comments

Comments
 (0)