Skip to content

Commit a5fea22

Browse files
committed
refactor(agents): remove fallback persistence casts
1 parent 6e4fbd9 commit a5fea22

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎src/agents/agent-command.live-model-switch.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,11 @@ describe("agentCommand – LiveSessionModelSwitchError retry", () => {
766766
});
767767

768768
it("suppresses duplicate user persistence only after the current turn has flushed", async () => {
769-
const attemptCalls: Array<Record<string, unknown>> = [];
769+
type AttemptCall = {
770+
onUserMessagePersisted?: () => void;
771+
suppressPromptPersistenceOnRetry?: boolean;
772+
};
773+
const attemptCalls: AttemptCall[] = [];
770774
state.runWithModelFallbackMock.mockImplementation(async (params: FallbackRunnerParams) => {
771775
const first = await params.run(params.provider, params.model);
772776
const result = await params.run(params.provider, params.model);
@@ -777,13 +781,9 @@ describe("agentCommand – LiveSessionModelSwitchError retry", () => {
777781
attempts: [first],
778782
};
779783
});
780-
state.runAgentAttemptMock.mockImplementation(async (params: unknown) => {
781-
const attemptParams = params as Record<string, unknown>;
784+
state.runAgentAttemptMock.mockImplementation(async (attemptParams: AttemptCall) => {
782785
attemptCalls.push(attemptParams);
783-
const persisted = attemptParams.onUserMessagePersisted;
784-
if (typeof persisted === "function") {
785-
persisted();
786-
}
786+
attemptParams.onUserMessagePersisted?.();
787787
return makeSuccessResult("openai", "gpt-5.4");
788788
});
789789

‎src/agents/session-tool-result-guard.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,9 @@ describe("installSessionToolResultGuard", () => {
500500

501501
it("suppresses only the next persisted user message when requested", () => {
502502
const sm = SessionManager.inMemory();
503-
installSessionToolResultGuard(
504-
sm,
505-
{
506-
suppressNextUserMessagePersistence: true,
507-
} as never,
508-
);
503+
installSessionToolResultGuard(sm, {
504+
suppressNextUserMessagePersistence: true,
505+
});
509506

510507
sm.appendMessage(
511508
asAppendMessage({

‎src/agents/session-tool-result-guard.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ function resolveMaxToolResultChars(opts?: { maxToolResultChars?: number }): numb
4444
return Math.max(1, opts?.maxToolResultChars ?? DEFAULT_MAX_LIVE_TOOL_RESULT_CHARS);
4545
}
4646

47+
type UserAgentMessage = Extract<AgentMessage, { role: "user" }>;
48+
49+
function isUserAgentMessage(message: AgentMessage): message is UserAgentMessage {
50+
return message.role === "user";
51+
}
52+
4753
// `details` is runtime/UI metadata, not model-visible tool output. Keep the
4854
// session JSONL useful for debugging without letting metadata blobs dominate
4955
// disk, replay repair, transcript broadcasts, or future tooling that reads raw
@@ -455,10 +461,7 @@ export function installSessionToolResultGuard(
455461
if (!finalMessage) {
456462
return undefined;
457463
}
458-
if (
459-
(finalMessage as { role?: unknown }).role === "user" &&
460-
suppressNextUserMessagePersistence
461-
) {
464+
if (isUserAgentMessage(finalMessage) && suppressNextUserMessagePersistence) {
462465
suppressNextUserMessagePersistence = false;
463466
return undefined;
464467
}
@@ -479,8 +482,8 @@ export function installSessionToolResultGuard(
479482
if (toolCalls.length > 0) {
480483
pendingState.trackToolCalls(toolCalls);
481484
}
482-
if ((finalMessage as { role?: unknown }).role === "user") {
483-
void opts?.onUserMessagePersisted?.(finalMessage as Extract<AgentMessage, { role: "user" }>);
485+
if (isUserAgentMessage(finalMessage)) {
486+
void opts?.onUserMessagePersisted?.(finalMessage);
484487
}
485488

486489
return result;

0 commit comments

Comments
 (0)