diff --git a/core/src/background/agent-loop.ts b/core/src/background/agent-loop.ts index 91b4df7..06cfa8e 100644 --- a/core/src/background/agent-loop.ts +++ b/core/src/background/agent-loop.ts @@ -1528,7 +1528,7 @@ export async function runTask(task: AgentTask, deps: AgentDeps): Promise row.key)); - collectionQueue = applyBatch(c, batch, read, moved).queue; + collectionQueue = applyBatch(c, batch, read, moved); if (batch.kind === 'rows') { // Full rows are saved and shown to the user; the model gets a compact view so a // page of long cards does not crowd the history and vanish on compaction. diff --git a/core/src/background/bridge-collection.ts b/core/src/background/bridge-collection.ts index 0192941..cd6a6f7 100644 --- a/core/src/background/bridge-collection.ts +++ b/core/src/background/bridge-collection.ts @@ -11,7 +11,7 @@ import type { ToolCall } from '@/lib/types'; const STORE_KEY = 'bridgeCollections'; const MAX_STORED = 20; /** Well inside the 30 s every bridge path allows, leaving room for one slow last step. */ -export const FOLLOW_BUDGET_MS = 12_000; +const FOLLOW_BUDGET_MS = 12_000; type Mode = 'rows' | 'chunk'; interface Stored { collection: Collection; mode: Mode; tabId: number; selector?: string; updatedAt: number } diff --git a/core/src/lib/collection.test.ts b/core/src/lib/collection.test.ts index 4094ef2..5b2f1c0 100644 --- a/core/src/lib/collection.test.ts +++ b/core/src/lib/collection.test.ts @@ -167,12 +167,12 @@ describe('target and model view', () => { it('raises a target that was set to the page size', () => { const c = fresh(); const page = { kind:'rows' as const, source:'search', range:[1,25] as [number,number], claimed: 5981, rows: rows(1, 25), nextRef: '@e4' }; - expect(applyBatch(c, page, {name:'extract',arguments:{refs:'rows',target:25}}).queue).toEqual([]); + expect(applyBatch(c, page, {name:'extract',arguments:{refs:'rows',target:25}})).toEqual([]); expect(c.target).toBe(25); - const next = applyBatch(c, page, {name:'extract',arguments:{refs:'rows',target:50}}, false).queue; + const next = applyBatch(c, page, {name:'extract',arguments:{refs:'rows',target:50}}, false); expect(c.target).toBe(50); expect(next.map(x => x.name)).toEqual(['click', 'extract']); - expect(applyBatch(c, page, {name:'extract',arguments:{refs:'rows',target:10}}, false).queue.length).toBe(2); + expect(applyBatch(c, page, {name:'extract',arguments:{refs:'rows',target:10}}, false).length).toBe(2); expect(c.target).toBe(50); }); it('gives the model one short line per new row', () => { diff --git a/core/src/lib/collection.ts b/core/src/lib/collection.ts index 582f6b7..49246c1 100644 --- a/core/src/lib/collection.ts +++ b/core/src/lib/collection.ts @@ -246,7 +246,7 @@ export function collectionFollowUp(c: Collection, batch: PageRows | TextChunk, r } /** Merges one capture into the collection and returns the steps that continue it. */ -export function applyBatch(c: Collection, batch: PageRows | TextChunk, read: ToolCall, moved = true): { queue: ToolCall[]; advanced: boolean } { +export function applyBatch(c: Collection, batch: PageRows | TextChunk, read: ToolCall, moved = true): ToolCall[] { // A later, larger target wins: the first read may have passed the page size by mistake. const requested = Number(read.arguments.target); if (batch.kind === 'rows' && Number.isSafeInteger(requested) && requested > (c.target ?? 0)) c.target = requested; @@ -255,27 +255,28 @@ export function applyBatch(c: Collection, batch: PageRows | TextChunk, read: Too const advanced = batch.kind === 'rows' ? c.rows.length > previous : batch.text.length > 0 && (batch.offset === 0 || batch.offset + batch.text.length > previous); - return { queue: collectionFollowUp(c, batch, read, advanced, moved), advanced }; + return collectionFollowUp(c, batch, read, advanced, moved); } /** What the model sees for a rows capture: the new rows, each cut to one short line. */ -export function compactRows(batch: PageRows, added: CollectedRow[], priorKeys: ReadonlySet, width = 200): Record { +export function compactRows(batch: PageRows, added: CollectedRow[], priorKeys: ReadonlySet): Record { const duplicates = batch.rows.filter(row => priorKeys.has(row.key)).length; return { kind: 'rows', source: batch.source, ...(batch.range ? { range: batch.range } : {}), ...(batch.claimed ? { claimed: batch.claimed } : {}), - newRows: added.map(row => row.text.replace(/\s+/g, ' ').trim().slice(0, width)), + newRows: added.map(row => row.text.replace(/\s+/g, ' ').trim().slice(0, 200)), alreadySaved: duplicates, beyondTarget: batch.rows.length - added.length - duplicates, - note: 'Full rows are saved and shown to the user under Collected data.', }; } const isRead = (call: ToolCall) => call.name === 'extract' && (call.arguments.refs === 'rows' || call.arguments.refs === 'chunk'); -export interface FollowOutcome { steps: number; timedOut: boolean; error?: string } +interface FollowOutcome { steps: number; timedOut: boolean; error?: string } +/** A runaway guard; the caller's deadline is what normally ends a call. */ +const MAX_FOLLOW_STEPS = 200; /** * Runs a collection to its end (target met, no more pages, stall, or chunk total reached) @@ -286,15 +287,14 @@ export async function followCollection( c: Collection, read: ToolCall, run: (call: ToolCall) => Promise<{ ok: boolean; extracted?: unknown; error?: string }>, - opts: { deadline: number; now?: () => number; maxSteps?: number }, + opts: { deadline: number; now?: () => number }, ): Promise { const now = opts.now ?? Date.now; - const maxSteps = opts.maxSteps ?? 200; let queue: ToolCall[] = [read]; let steps = 0; let moved = false; while (queue.length) { - if (now() >= opts.deadline || steps >= maxSteps) return { steps, timedOut: true }; + if (now() >= opts.deadline || steps >= MAX_FOLLOW_STEPS) return { steps, timedOut: true }; let call = queue.shift()!; if (call.arguments.refs === 'chunk') { const prior = c.documents.at(-1); @@ -305,7 +305,7 @@ export async function followCollection( if (!result.ok) return { steps, timedOut: false, error: result.error ?? `${call.name} failed` }; if (!isRead(call)) { moved = true; continue; } try { - queue = applyBatch(c, result.extracted as PageRows | TextChunk, read, moved).queue; + queue = applyBatch(c, result.extracted as PageRows | TextChunk, read, moved); moved = false; } catch (err) { return { steps, timedOut: false, error: err instanceof Error ? err.message : String(err) };