Skip to content

Commit eb7f45e

Browse files
Radu028mmazurclaude
committed
fix(acp): agents get T3's runtime notes once per session, not on every message
The ACP adapter appended the runtime_info and pull request linking block to every non-slash prompt. ACP agents keep their own history, so the block piled up, one copy per user message. Remember per session the block last delivered and resend it only when its text changes (a model switch), after compaction, or in a new or replacement session. Delivery is recorded after a successful session/prompt, next to the existing instruction state. Replay fixtures expect the block until a prompt in the session succeeds, matching the adapter. Fixes #24185 Co-authored-by: Mariusz Mazur <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6d450a4 commit eb7f45e

3 files changed

Lines changed: 71 additions & 18 deletions

File tree

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ function assertReinjectedInstructions(
416416
assert.include(text, "## T3 Code browser");
417417
assert.include(text, "T3 Code orchestration");
418418
assert.include(text, `<user_request>\n${request}\n</user_request>`);
419+
assert.include(blocks[1]?.text ?? "", "<runtime_info>");
419420
}
420421

421422
describe("AcpAdapterV2", () => {
@@ -959,6 +960,7 @@ describe("AcpAdapterV2", () => {
959960
ordinal: number,
960961
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
961962
messageText: string,
963+
turnModelSelection: ModelSelection = modelSelection,
962964
) {
963965
yield* runtime.startTurn(
964966
makeTurnInput({
@@ -969,6 +971,7 @@ describe("AcpAdapterV2", () => {
969971
now,
970972
ordinal,
971973
messageText,
974+
modelSelection: turnModelSelection,
972975
}),
973976
);
974977
yield* runtime.events.pipe(
@@ -1006,15 +1009,15 @@ describe("AcpAdapterV2", () => {
10061009
assert.include(firstDefault.prompt, "T3 Code interaction mode: Default");
10071010
assert.include(firstDefault.prompt, "## T3 Code browser");
10081011
assert.include(firstDefault.prompt, "T3 Code orchestration");
1012+
assert.include(firstDefault.prompt, "<runtime_info>");
10091013
assert.notInclude(
10101014
firstDefault.methods,
10111015
"session/set_config_option",
10121016
"Build should preserve the agent's advertised mode default",
10131017
);
1014-
assert.include(
1015-
(yield* runTurn(3, defaultPolicy, "Second default request.")).prompt,
1016-
"Second default request.",
1017-
);
1018+
const secondDefault = yield* runTurn(3, defaultPolicy, "Second default request.");
1019+
assert.equal(secondDefault.prompt, "Second default request.");
1020+
assert.equal(secondDefault.textBlockCount, 1);
10181021

10191022
const planPolicy = policy("plan");
10201023
const firstPlan = yield* runTurn(4, planPolicy, "Plan this change.");
@@ -1031,6 +1034,12 @@ describe("AcpAdapterV2", () => {
10311034
"session/set_config_option",
10321035
"Build should restore the native mode that T3 temporarily replaced for Plan",
10331036
);
1037+
const switchedModel = yield* runTurn(7, defaultPolicy, "Switch models.", {
1038+
instanceId,
1039+
model: "composer-2",
1040+
});
1041+
assert.equal(switchedModel.textBlockCount, 2);
1042+
assert.include(switchedModel.prompt, "as composer-2");
10341043
}).pipe(Effect.provide(layerTest), Effect.scoped),
10351044
);
10361045

@@ -1054,7 +1063,7 @@ describe("AcpAdapterV2", () => {
10541063
yield* runTurn("Continue after the second compaction."),
10551064
"Continue after the second compaction.",
10561065
);
1057-
assert.equal((yield* runTurn("Finish work."))[0]?.text, "Finish work.");
1066+
assert.deepEqual(yield* runTurn("Finish work."), [{ type: "text", text: "Finish work." }]);
10581067
}).pipe(Effect.provide(layerTest), Effect.scoped),
10591068
);
10601069

@@ -1069,14 +1078,13 @@ describe("AcpAdapterV2", () => {
10691078
assert.deepEqual(yield* runTurn("/compact", "failed"), [
10701079
{ type: "text", text: "/compact" },
10711080
]);
1072-
assert.equal(
1073-
(yield* runTurn("Continue after failure."))[0]?.text,
1074-
"Continue after failure.",
1075-
);
1081+
assert.deepEqual(yield* runTurn("Continue after failure."), [
1082+
{ type: "text", text: "Continue after failure." },
1083+
]);
10761084
assert.deepEqual(yield* runTurn("/compact"), [{ type: "text", text: "/compact" }]);
10771085
assert.deepEqual(yield* runTurn("/repro-skill"), [{ type: "text", text: "/repro-skill" }]);
10781086
assertReinjectedInstructions(yield* runTurn("Continue work."), "Continue work.");
1079-
assert.equal((yield* runTurn("Finish work."))[0]?.text, "Finish work.");
1087+
assert.deepEqual(yield* runTurn("Finish work."), [{ type: "text", text: "Finish work." }]);
10801088
}).pipe(Effect.provide(layerTest), Effect.scoped),
10811089
);
10821090

@@ -3285,9 +3293,11 @@ describe("AcpAdapterV2", () => {
32853293
browserToolsAvailable: false,
32863294
});
32873295
const runtimeInputs: Array<AcpAdapterV2RuntimeInput> = [];
3296+
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
32883297
const makeRuntime = makeMockRuntime({
32893298
childProcessSpawner,
32903299
mockAgentPath,
3300+
protocolEvents,
32913301
environment: { T3_ACP_PROMPT_DELAY_MS: "100" },
32923302
});
32933303
const adapter = yield* makeAcpAdapterV2({
@@ -3419,6 +3429,15 @@ describe("AcpAdapterV2", () => {
34193429
}
34203430
}
34213431
assert.equal(secondStatus, "completed");
3432+
const prompts = Array.from(yield* Queue.takeAll(protocolEvents))
3433+
.filter(
3434+
(event) =>
3435+
event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt",
3436+
)
3437+
.map(rawProtocolPromptTextBlocks);
3438+
assert.lengthOf(prompts, 2);
3439+
// The mock agent reuses its session id, so only the rollback reset resends these notes.
3440+
assert.include(prompts[1]?.at(-1)?.text ?? "", "<runtime_info>");
34223441
}).pipe(Effect.provide(layerTest), Effect.scoped),
34233442
);
34243443

‎apps/server/src/orchestration-v2/testkit/ReplayRuntimeInstructions.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,28 @@ export function materializeReplayTranscriptRuntimeInstructions(
1818
: undefined;
1919
if (harness === undefined) return transcript;
2020
const instructions = buildRuntimeInstructions({ harness, model: runtime.model });
21+
// The ACP adapter resends these instructions until a prompt in that session succeeds.
22+
const instructedSessionIds = new Set<unknown>();
23+
let promptSessionId: unknown;
2124

2225
return {
2326
...transcript,
2427
entries: transcript.entries.map((entry) => {
28+
if (entry.type === "emit_inbound") {
29+
const frame = entry.frame;
30+
if (
31+
typeof frame === "object" &&
32+
frame !== null &&
33+
"kind" in frame &&
34+
frame.kind === "response" &&
35+
"method" in frame &&
36+
frame.method === "session/prompt" &&
37+
"result" in frame
38+
) {
39+
instructedSessionIds.add(promptSessionId);
40+
}
41+
return entry;
42+
}
2543
if (entry.type !== "expect_outbound") return entry;
2644
const frame = entry.frame;
2745
if (typeof frame !== "object" || frame === null) return entry;
@@ -45,6 +63,8 @@ export function materializeReplayTranscriptRuntimeInstructions(
4563
"prompt" in frame.params &&
4664
Array.isArray(frame.params.prompt)
4765
) {
66+
promptSessionId = "sessionId" in frame.params ? frame.params.sessionId : undefined;
67+
if (instructedSessionIds.has(promptSessionId)) return entry;
4868
const prompt: ReadonlyArray<unknown> = frame.params.prompt;
4969
const lastPart = prompt.at(-1);
5070
if (

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
15941594
const activeSelection = yield* Ref.make<ModelSelection | null>(null);
15951595
const activeInteractionMode = yield* Ref.make<ProviderInteractionMode | null>(null);
15961596
const promptInstructionStates = yield* Ref.make(new Map<string, T3AcpInstructionState>());
1597+
const promptRuntimeInstructions = yield* Ref.make(new Map<string, string>());
15971598
// Unique markers keep delayed prompt completions from clearing later compactions.
15981599
const compactionMarkers = yield* Ref.make(new Map<string, symbol>());
15991600
const runtimeRestartRequired = yield* Ref.make(false);
@@ -6574,6 +6575,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
65746575
yield* Ref.set(activeSelection, null);
65756576
yield* Ref.set(activeInteractionMode, null);
65766577
yield* Ref.set(promptInstructionStates, new Map());
6578+
yield* Ref.set(promptRuntimeInstructions, new Map());
65776579
yield* Ref.set(compactionMarkers, new Map());
65786580
yield* Ref.set(providerTurns, new Map());
65796581
yield* Ref.set(snapshot, {
@@ -7279,18 +7281,22 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
72797281
detail: "ACP turn requires non-empty text or attachments",
72807282
});
72817283
}
7282-
if (!isNativeSlashCommand) {
7283-
prompt.push({
7284-
type: "text",
7285-
text: buildRuntimeInstructions({
7286-
harness: flavor.runtimeHarness ?? driver,
7287-
model: turnInput.modelSelection.model,
7288-
}),
7289-
});
7284+
const runtimeInstructions = buildRuntimeInstructions({
7285+
harness: flavor.runtimeHarness ?? driver,
7286+
model: turnInput.modelSelection.model,
7287+
});
7288+
// The agent keeps its own history, so resend only after compaction or when the text changes.
7289+
const sendRuntimeInstructions =
7290+
!isNativeSlashCommand &&
7291+
(compactionMarker !== undefined ||
7292+
(yield* Ref.get(promptRuntimeInstructions)).get(sessionId) !== runtimeInstructions);
7293+
if (sendRuntimeInstructions) {
7294+
prompt.push({ type: "text", text: runtimeInstructions });
72907295
}
72917296
return {
72927297
prompt,
72937298
instructionState: text === messageText ? undefined : instructionState,
7299+
runtimeInstructions: sendRuntimeInstructions ? runtimeInstructions : undefined,
72947300
compactionMarker,
72957301
};
72967302
});
@@ -7603,6 +7609,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
76037609
return updated;
76047610
}),
76057611
),
7612+
Effect.tap(() =>
7613+
Ref.update(promptRuntimeInstructions, (current) => {
7614+
if (promptParts?.runtimeInstructions === undefined) return current;
7615+
const updated = new Map(current);
7616+
updated.set(requestedSessionId, promptParts.runtimeInstructions);
7617+
return updated;
7618+
}),
7619+
),
76067620
Effect.tap(() =>
76077621
Ref.update(compactionMarkers, (current) => {
76087622
if (promptParts?.instructionState === undefined) return current;

0 commit comments

Comments
 (0)