Skip to content

Commit 46989c1

Browse files
committed
fix local codex review comment:
- [P0] Respect sessionInputCallback redactions — packages/agents-core/src/runImplementation.ts:1986-1992 If a session input callback intentionally omits or redacts the new turn inputs (e.g. it returns only the prior history to avoid persisting sensitive data), we should not write the raw inputs to the session. However, when appended.length === 0 this code falls back to newInputSnapshot, so we still persist the original turn items even though the callback removed them. This leaks exactly the data the developer tried to drop, defeating the purpose of the callback. Please respect the callback’s decision by persisting nothing (or only the items it returned) when no new items were appended.
1 parent 72ac1dc commit 46989c1

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

packages/agents-core/src/run.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
493493
// When the server tracks conversation state we only send the new turn inputs;
494494
// previous messages are recovered via conversationId/previousResponseId.
495495
includeHistoryInPreparedInput: !serverManagesConversation,
496+
preserveDroppedNewItems: serverManagesConversation,
496497
},
497498
);
498499
if (serverManagesConversation && session) {

packages/agents-core/src/runImplementation.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,12 @@ export async function prepareInputItemsWithSession(
19081908
* (e.g. server-managed conversations) to avoid sending duplicated messages each turn.
19091909
*/
19101910
includeHistoryInPreparedInput?: boolean;
1911+
/**
1912+
* When true, ensures new turn inputs are still provided to the model even if the session input
1913+
* callback drops them from persistence (used for server-managed conversations that redact
1914+
* writes).
1915+
*/
1916+
preserveDroppedNewItems?: boolean;
19111917
},
19121918
): Promise<PreparedInputWithSessionResult> {
19131919
if (!session) {
@@ -1919,6 +1925,7 @@ export async function prepareInputItemsWithSession(
19191925

19201926
const includeHistoryInPreparedInput =
19211927
options?.includeHistoryInPreparedInput ?? true;
1928+
const preserveDroppedNewItems = options?.preserveDroppedNewItems ?? false;
19221929

19231930
const history = await session.getItems();
19241931
const newInputItems = Array.isArray(input)
@@ -1985,13 +1992,20 @@ export async function prepareInputItemsWithSession(
19851992
appended.push(item);
19861993
}
19871994

1995+
// Preserve redacted inputs for model delivery when requested (e.g. server-managed histories).
1996+
const preparedItems = includeHistoryInPreparedInput
1997+
? combined
1998+
: appended.length > 0
1999+
? appended
2000+
: preserveDroppedNewItems
2001+
? newInputSnapshot
2002+
: [];
2003+
19882004
return {
1989-
preparedInput: includeHistoryInPreparedInput
1990-
? combined
1991-
: appended.length > 0
1992-
? appended
1993-
: newInputSnapshot,
1994-
sessionItems: appended.length > 0 ? appended : newInputSnapshot,
2005+
preparedInput: preparedItems,
2006+
// Respect callbacks that intentionally drop the latest inputs (e.g. to redact sensitive
2007+
// values) by persisting only the items they kept in the combined array.
2008+
sessionItems: appended,
19952009
};
19962010
}
19972011

packages/agents-core/test/runImplementation.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ describe('prepareInputItemsWithSession', () => {
552552
expect(sessionItems[0]).toBe(newItem);
553553
});
554554

555-
it('preserves the latest input when callbacks omit new items entirely', async () => {
555+
it('respects callbacks that intentionally drop new inputs', async () => {
556556
const historyItem: AgentInputItem = {
557557
type: 'message',
558558
role: 'user',
@@ -574,13 +574,12 @@ describe('prepareInputItemsWithSession', () => {
574574
{ includeHistoryInPreparedInput: false },
575575
);
576576

577-
expect(result.preparedInput).toEqual([newItem]);
577+
expect(result.preparedInput).toEqual([]);
578578
const sessionItems = result.sessionItems;
579579
if (!sessionItems) {
580580
throw new Error('Expected sessionItems to be defined.');
581581
}
582-
expect(sessionItems).toEqual([newItem]);
583-
expect(sessionItems[0]).toBe(newItem);
582+
expect(sessionItems).toEqual([]);
584583
});
585584

586585
it('persists appended copies when callbacks mutate history in place', async () => {

0 commit comments

Comments
 (0)