Skip to content

Commit 6ca4404

Browse files
committed
fix: preserve final prompt read policy
1 parent 98c87a4 commit 6ca4404

9 files changed

Lines changed: 79 additions & 9 deletions

‎src/agents/embedded-agent-runner/run/attempt-execution-settle.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,11 @@ function createFixture() {
173173
agentSession: {
174174
activeSession,
175175
clientToolCallSlots: [],
176+
coreReadAuthorized: true,
176177
getCodeModeReconciliationCandidate: vi.fn(() => false),
177178
hasDeliveredSourceReply: vi.fn(() => true),
178179
hookRunner,
180+
setCodeModeReconciliationReadAuthorized: vi.fn(),
179181
setActiveSessionSystemPrompt: vi.fn(),
180182
settingsManager: { getCompactionReserveTokens: vi.fn(() => 1_000) },
181183
},

‎src/agents/embedded-agent-runner/run/attempt-prompt-phase.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import type { submitEmbeddedAttemptPrompt } from "./attempt-prompt-submit.js";
6464
type PromptPhaseInput = Parameters<typeof runEmbeddedAttemptPromptPhase>[0];
6565
type PromptPhaseState = ReturnType<PromptPhaseInput["lifecycle"]["readState"]>;
6666
type AssemblyCall = {
67+
applyPromptBuildToolsAllow: (toolsAllow: string[] | undefined) => string[];
6768
setLeasedSteering: (lease: { leaseId: string; runIds: string[] }) => void;
6869
};
6970
type PromptPreflightCall = Parameters<typeof prepareEmbeddedAttemptPromptPreflight>[0];
@@ -107,6 +108,7 @@ function createFixture() {
107108
prePromptMessageCount = count;
108109
});
109110
const setPromptCacheChangesForTurn = vi.fn();
111+
const setCodeModeReconciliationReadAuthorized = vi.fn();
110112
const setFinalPromptText = vi.fn();
111113
const markBeforeAgentRunBlocked = vi.fn();
112114
const markYieldAborted = vi.fn(() => {
@@ -119,6 +121,7 @@ function createFixture() {
119121
mocks.preparePromptAssembly.mockImplementation(async (input: AssemblyCall) => {
120122
order.push("assembly");
121123
const lease = { leaseId: "lease-1", runIds: ["run-1"] };
124+
input.applyPromptBuildToolsAllow(undefined);
122125
input.setLeasedSteering(lease);
123126
return {
124127
hookCtx: {},
@@ -237,6 +240,14 @@ function createFixture() {
237240
transport: "sse",
238241
uncompactedEffectiveTools: [],
239242
},
243+
toolPolicy: {
244+
baseline: { activeToolNames: ["read"], catalogEntries: [] },
245+
effectiveTools: [{ name: "read" }],
246+
uncompactedEffectiveTools: [{ name: "read" }],
247+
tools: [{ name: "read" }],
248+
codeModeControlsEnabled: false,
249+
coreReadAuthorized: true,
250+
},
240251
preflight: {
241252
contextEngineAssemblySucceeded: false,
242253
contextEnginePromptAuthority: "assembled",
@@ -258,6 +269,7 @@ function createFixture() {
258269
setPrePromptMessageCount,
259270
setCurrentUserTimestampOverride: vi.fn(),
260271
setPromptCacheChangesForTurn,
272+
setCodeModeReconciliationReadAuthorized,
261273
setFinalPromptText,
262274
markBeforeAgentRunBlocked,
263275
markYieldAborted,
@@ -275,13 +287,21 @@ function createFixture() {
275287
setFinalPromptText,
276288
setPrePromptMessageCount,
277289
setPromptCacheChangesForTurn,
290+
setCodeModeReconciliationReadAuthorized,
278291
state,
279292
yieldState,
280293
};
281294
}
282295

283296
beforeEach(() => {
284297
vi.clearAllMocks();
298+
mocks.applyPromptToolsAllow.mockReturnValue({
299+
activeToolNames: ["read"],
300+
coreReadAuthorized: true,
301+
effectiveTools: [{ name: "read" }],
302+
uncompactedEffectiveTools: [{ name: "read" }],
303+
tools: [{ name: "read" }],
304+
});
285305
});
286306

287307
describe("runEmbeddedAttemptPromptPhase", () => {
@@ -308,6 +328,7 @@ describe("runEmbeddedAttemptPromptPhase", () => {
308328
]);
309329
expect(fixture.setPrePromptMessageCount).toHaveBeenCalledWith(2);
310330
expect(fixture.setPromptCacheChangesForTurn).toHaveBeenCalledWith([]);
331+
expect(fixture.setCodeModeReconciliationReadAuthorized).toHaveBeenCalledWith(true);
311332
expect(fixture.setFinalPromptText).toHaveBeenCalledWith("hello");
312333
expect(mocks.preparePromptExecution).toHaveBeenCalledWith(
313334
expect.objectContaining({
@@ -335,6 +356,21 @@ describe("runEmbeddedAttemptPromptPhase", () => {
335356
expect(mocks.releasePendingSteering).not.toHaveBeenCalled();
336357
});
337358

359+
it("records a final prompt policy that removes core read", async () => {
360+
const fixture = createFixture();
361+
mocks.applyPromptToolsAllow.mockReturnValueOnce({
362+
activeToolNames: [],
363+
coreReadAuthorized: false,
364+
effectiveTools: [],
365+
uncompactedEffectiveTools: [],
366+
tools: [],
367+
});
368+
369+
await runEmbeddedAttemptPromptPhase(fixture.input);
370+
371+
expect(fixture.setCodeModeReconciliationReadAuthorized).toHaveBeenCalledWith(false);
372+
});
373+
338374
it("skips before_agent_run for settled-turn finalization", async () => {
339375
const fixture = createFixture();
340376
fixture.input.attempt.operation = "settled-tool-finalization";

‎src/agents/embedded-agent-runner/run/attempt-prompt-phase.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export async function runEmbeddedAttemptPromptPhase(input: {
122122
tools: Array<{ name: string }>;
123123
toolSearchCatalogRef?: Parameters<typeof applyPromptBuildToolsAllow>[0]["catalogRef"];
124124
codeModeControlsEnabled: boolean;
125+
coreReadAuthorized: boolean;
125126
forceToolNames?: readonly string[];
126127
};
127128
preflight: PromptPreflightPhaseInput;
@@ -137,6 +138,7 @@ export async function runEmbeddedAttemptPromptPhase(input: {
137138
setPromptCacheChangesForTurn: (
138139
changes: PromptAssemblyResult["promptCacheChangesForTurn"],
139140
) => void;
141+
setCodeModeReconciliationReadAuthorized: (value: boolean) => void;
140142
setFinalPromptText: (prompt: string) => void;
141143
markBeforeAgentRunBlocked: (outcome: BeforeAgentRunOutcome) => void;
142144
markYieldAborted: () => void;
@@ -217,8 +219,10 @@ export async function runEmbeddedAttemptPromptPhase(input: {
217219
tools: input.toolPolicy.tools,
218220
catalogRef: input.toolPolicy.toolSearchCatalogRef,
219221
codeModeControlsEnabled: input.toolPolicy.codeModeControlsEnabled,
222+
coreReadAuthorized: input.toolPolicy.coreReadAuthorized,
220223
forceToolNames: input.toolPolicy.forceToolNames,
221224
});
225+
input.lifecycle.setCodeModeReconciliationReadAuthorized(promptToolSurface.coreReadAuthorized);
222226
return promptToolSurface.activeToolNames;
223227
},
224228
setLeasedSteering: (lease) => {

‎src/agents/embedded-agent-runner/run/attempt-prompt-support.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ export function applyPromptBuildToolsAllow<
7676
tools: TTool[];
7777
catalogRef?: ToolSearchCatalogRef;
7878
codeModeControlsEnabled: boolean;
79+
coreReadAuthorized: boolean;
7980
forceToolNames?: readonly string[];
8081
}): {
8182
activeToolNames: string[];
83+
coreReadAuthorized: boolean;
8284
effectiveTools: TEffectiveTool[];
8385
uncompactedEffectiveTools: TUncompactedTool[];
8486
tools: TTool[];
@@ -122,6 +124,9 @@ export function applyPromptBuildToolsAllow<
122124

123125
return {
124126
activeToolNames,
127+
coreReadAuthorized:
128+
params.coreReadAuthorized &&
129+
allowedUncompactedTools.some((tool) => normalizeToolPolicyName(tool.name) === "read"),
125130
effectiveTools: promptPolicy.tools,
126131
uncompactedEffectiveTools: allowedUncompactedTools,
127132
tools: allowedTools,

‎src/agents/embedded-agent-runner/run/attempt-prompt-tool-policy.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ describe("applyPromptBuildToolsAllow", () => {
7878
tools: [{ name: "read" }, { name: "write" }, { name: "message" }],
7979
catalogRef,
8080
codeModeControlsEnabled: false,
81+
coreReadAuthorized: true,
8182
});
8283

8384
expect(result.activeToolNames).toEqual([]);
85+
expect(result.coreReadAuthorized).toBe(false);
8486
expect(result.effectiveTools).toEqual([]);
8587
expect(result.uncompactedEffectiveTools).toEqual([]);
8688
expect(result.tools).toEqual([]);
@@ -107,6 +109,7 @@ describe("applyPromptBuildToolsAllow", () => {
107109
uncompactedEffectiveTools: [{ name: "message" }, { name: "read" }],
108110
tools: [{ name: "message" }, { name: "read" }],
109111
codeModeControlsEnabled: false,
112+
coreReadAuthorized: true,
110113
});
111114

112115
expect(result.activeToolNames).toEqual(["message"]);
@@ -143,9 +146,11 @@ describe("applyPromptBuildToolsAllow", () => {
143146
tools: [{ name: "read" }, { name: "write" }, { name: "message" }],
144147
catalogRef,
145148
codeModeControlsEnabled: false,
149+
coreReadAuthorized: true,
146150
});
147151

148152
expect(result.activeToolNames).toEqual(["tool_search"]);
153+
expect(result.coreReadAuthorized).toBe(true);
149154
expect(result.effectiveTools).toEqual([{ name: "tool_search" }]);
150155
expect(result.uncompactedEffectiveTools).toEqual([{ name: "read" }]);
151156
expect(result.tools).toEqual([{ name: "read" }]);
@@ -164,9 +169,11 @@ describe("applyPromptBuildToolsAllow", () => {
164169
uncompactedEffectiveTools: [{ name: "read" }],
165170
tools: [{ name: "read" }],
166171
codeModeControlsEnabled: false,
172+
coreReadAuthorized: true,
167173
});
168174

169175
expect(result.activeToolNames).toEqual([]);
176+
expect(result.coreReadAuthorized).toBe(false);
170177
expect(result.effectiveTools).toEqual([]);
171178
expect(result.uncompactedEffectiveTools).toEqual([]);
172179
expect(result.tools).toEqual([]);
@@ -198,6 +205,7 @@ describe("applyPromptBuildToolsAllow", () => {
198205
tools: [pluginTool],
199206
catalogRef,
200207
codeModeControlsEnabled: false,
208+
coreReadAuthorized: false,
201209
});
202210

203211
expect(result.activeToolNames).toEqual(["tool_search"]);
@@ -225,6 +233,7 @@ describe("applyPromptBuildToolsAllow", () => {
225233
tools: [{ name: "read" }, { name: "write" }],
226234
catalogRef,
227235
codeModeControlsEnabled: false,
236+
coreReadAuthorized: true,
228237
};
229238

230239
applyPromptBuildToolsAllow({ ...params, toolsAllow: ["read"] });

‎src/agents/embedded-agent-runner/run/attempt-session-prepare.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export async function prepareEmbeddedAttemptAgentSession(input: {
223223
setActiveSessionSystemPrompt(input.initialSystemPrompt);
224224
let didDeliverSourceReplyViaMessageTool = false;
225225
let codeModeReconciliationCandidate = false;
226+
let codeModeReconciliationReadAuthorized = false;
226227
const markSourceReplyDelivered = () => {
227228
didDeliverSourceReplyViaMessageTool = true;
228229
};
@@ -234,13 +235,11 @@ export async function prepareEmbeddedAttemptAgentSession(input: {
234235
if (input.clientToolPreparation.codeModeControlsEnabledForRun) {
235236
installCodeModeRepairHook({
236237
agent: activeSession.agent,
237-
...(clientToolRuntime.coreReadAuthorized
238-
? {
239-
onReconciliationCandidate: () => {
240-
codeModeReconciliationCandidate = true;
241-
},
242-
}
243-
: {}),
238+
onReconciliationCandidate: () => {
239+
if (codeModeReconciliationReadAuthorized) {
240+
codeModeReconciliationCandidate = true;
241+
}
242+
},
244243
});
245244
}
246245
input.markStage("agent-session");
@@ -253,6 +252,9 @@ export async function prepareEmbeddedAttemptAgentSession(input: {
253252
hasDeliveredSourceReply: () => didDeliverSourceReplyViaMessageTool,
254253
hookRunner,
255254
markSourceReplyDelivered,
255+
setCodeModeReconciliationReadAuthorized: (value: boolean) => {
256+
codeModeReconciliationReadAuthorized = clientToolRuntime.coreReadAuthorized && value;
257+
},
256258
setActiveSessionSystemPrompt,
257259
settingsManager,
258260
};

‎src/agents/embedded-agent-runner/run/attempt-session.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ describe("prepareEmbeddedAttemptAgentSession", () => {
248248
fixture.onDeliveredSourceReply();
249249
expect(result.hasDeliveredSourceReply()).toBe(true);
250250
expect(result.getCodeModeReconciliationCandidate()).toBe(false);
251+
result.setCodeModeReconciliationReadAuthorized(true);
251252
fixture.markCodeModeReconciliationCandidate();
252253
expect(result.getCodeModeReconciliationCandidate()).toBe(true);
253254
});
@@ -261,14 +262,19 @@ describe("prepareEmbeddedAttemptAgentSession", () => {
261262
expect(fixture.events).not.toContain("install-code-mode-repair");
262263
});
263264

264-
it("withholds reconciliation when the effective core tools exclude read", async () => {
265-
const fixture = createInput({ coreReadAllowed: false });
265+
it.each([
266+
["the effective core tools exclude read", false, true],
267+
["the final prompt policy removes read", true, false],
268+
])("withholds reconciliation when %s", async (_label, coreReadAllowed, finalReadAllowed) => {
269+
const fixture = createInput({ coreReadAllowed });
266270

267271
const result = await prepareEmbeddedAttemptAgentSession(fixture.input);
268272

269273
expect(hoisted.installCodeModeRepairHook).toHaveBeenCalledWith({
270274
agent: fixture.activeSession.agent,
275+
onReconciliationCandidate: expect.any(Function),
271276
});
277+
result.setCodeModeReconciliationReadAuthorized(finalReadAllowed);
272278
fixture.markCodeModeReconciliationCandidate();
273279
expect(result.getCodeModeReconciliationCandidate()).toBe(false);
274280
});

‎src/agents/embedded-agent-runner/run/attempt-settle.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ export async function runEmbeddedAttemptSettledPhase(
129129
agentSession: {
130130
activeSession,
131131
clientToolCallSlots,
132+
coreReadAuthorized,
132133
getCodeModeReconciliationCandidate,
133134
hasDeliveredSourceReply,
134135
hookRunner,
136+
setCodeModeReconciliationReadAuthorized,
135137
setActiveSessionSystemPrompt,
136138
settingsManager,
137139
},
@@ -280,6 +282,7 @@ export async function runEmbeddedAttemptSettledPhase(
280282
uncompactedEffectiveTools,
281283
tools,
282284
codeModeControlsEnabled: toolBase.codeModeControlsEnabledForRun,
285+
coreReadAuthorized,
283286
toolSearchCatalogRef: toolBase.toolSearchCatalogRef,
284287
forceToolNames: [
285288
...(toolBase.forceDirectMessageTool ? ["message"] : []),
@@ -327,6 +330,7 @@ export async function runEmbeddedAttemptSettledPhase(
327330
setPromptCacheChangesForTurn: (changes) => {
328331
promptCacheChangesForTurn = changes;
329332
},
333+
setCodeModeReconciliationReadAuthorized,
330334
setFinalPromptText: (prompt) => {
331335
finalPromptText = prompt;
332336
},

‎src/agents/embedded-agent-runner/run/attempt-stream-finalize.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ function createFixture(overrides: FixtureOverrides = {}) {
122122
agentSession: {
123123
activeSession,
124124
clientToolCallSlots: [],
125+
coreReadAuthorized: true,
125126
getCodeModeReconciliationCandidate: vi.fn(() => false),
126127
hasDeliveredSourceReply: vi.fn(() => false),
127128
hookRunner: {},
129+
setCodeModeReconciliationReadAuthorized: vi.fn(),
128130
setActiveSessionSystemPrompt: vi.fn(),
129131
settingsManager: { getCompactionReserveTokens: vi.fn(() => 1_000) },
130132
},

0 commit comments

Comments
 (0)