Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,16 @@ export function executeRetrieve(args: Record<string, unknown>, session: Session)
// but the full text only reaches the model on a later upstream request.
// The ledger tracks it until delivered or dropped (never silently lost).
queueRetrieval(session, { ref, tokens: result.entry.tokens, chars: result.entry.chars, injection: result.injection });
recordRetrieveHit(session, ref);
const count = recordRetrieveHit(session, ref);
loggerLog("info", `[ccr] retrieve ${ref} (${result.entry.tokens} tok, ${result.entry.chars} chars)`);
// [#1430] Surface the accumulating cost at the decision point: on a repeat
// fetch of the same ref, append how many times it has come back this session
// plus the per-call size, so the model sees the bloat instead of silently
// re-pulling the full original every turn. Count + current entry size only —
// no new state; rides inside the existing tool-result field on every wire.
if (count >= REPEAT_RETRIEVE_ACK_THRESHOLD) {
return `${result.ackText}\n[${retrieveToolName(session)}: ${ref} fetched ${count}x this session — each call re-sends the full original (~${result.entry.tokens} tok); re-fetch only if you need detail you do not already have.]`;
}
return result.ackText;
}

Expand Down Expand Up @@ -360,14 +368,19 @@ export function drainPendingRetrievals(session: Session): CoreMessage[] {
// hint). In-memory only, bounded — an unbounded map would grow with every
// distinct ref across a long session. Map insertion order gives FIFO trim.
const RETRIEVE_COUNT_CAP = 512;
// [#1430] Ack-side repeat-cost note fires from the 2nd fetch onward — the first
// duplicate is where the cross-turn re-fetch loop becomes visible to the model.
const REPEAT_RETRIEVE_ACK_THRESHOLD = 2;

export function recordRetrieveHit(session: Session, ref: string): void {
export function recordRetrieveHit(session: Session, ref: string): number {
const counts = session.retrieveCountsByRef ?? new Map<string, number>();
counts.set(ref, (counts.get(ref) ?? 0) + 1);
const next = (counts.get(ref) ?? 0) + 1;
counts.set(ref, next);
while (counts.size > RETRIEVE_COUNT_CAP) {
const oldest = counts.keys().next().value;
if (oldest === undefined) break;
counts.delete(oldest);
}
session.retrieveCountsByRef = counts;
return next;
}
26 changes: 26 additions & 0 deletions tests/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ test("executeRetrieve: hit queues injection + ack, miss self-corrects, tool name
assert.equal(session.stats.retrieveMisses, 2);
});

test("#1430: repeat acp_retrieve surfaces a cumulative-cost note in the ack; first fetch stays clean", () => {
const session = getSession(`t-ccr-cost-${Math.random().toString(36).slice(2)}`);
storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 });
adoptContentStore(session, turnTwoResults(ccrConfig()).contentStore);
const [refA, refB] = Object.keys(session.contentStore!.byRef)!;
assert.ok(refA && refB && refA !== refB, "two distinct stored refs");

// First fetch: bare kernel ack, no cost note — no noise on a legitimate single fetch.
const first = executeRetrieve({ ref: refA }, session);
assert.match(first, new RegExp(`retrieved ${refA}: [\\d,]+ tok`));
assert.ok(!first.includes("this session"), "first fetch must not carry the repeat-cost note");

// Second fetch of the same ref: the note appears, citing the running count.
const second = executeRetrieve({ ref: refA }, session);
assert.match(second, /fetched 2x this session/);
assert.ok(second.includes(refA), "note names the ref");

// Third fetch: the count keeps climbing.
const third = executeRetrieve({ ref: refA }, session);
assert.match(third, /fetched 3x this session/);

// Per-ref isolation: a different ref's first fetch is still clean.
const other = executeRetrieve({ ref: refB }, session);
assert.ok(!other.includes("this session"), "a fresh ref's first fetch is unaffected by refA's repeats");
});

test("buildStoredPlaceholder renders the kernel wire format the gates assert on", () => {
const text = buildStoredPlaceholder({ ref: "m00423", kind: "shell output", tokens: 4213, head: "npm run build", command: "npm run build", retrieveToolName: RETRIEVE_TOOL_NAME });
assert.ok(text.includes("[acp-stored #m00423"));
Expand Down
Loading