Skip to content
Open
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
2 changes: 1 addition & 1 deletion core/src/background/agent-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ export async function runTask(task: AgentTask, deps: AgentDeps): Promise<AgentTa
? { ...toolCall, arguments: { ...toolCall.arguments, target: asked } } : toolCall;
const before = c.rows.length;
const priorKeys = new Set(c.rows.map(row => 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.
Expand Down
2 changes: 1 addition & 1 deletion core/src/background/bridge-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions core/src/lib/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
20 changes: 10 additions & 10 deletions core/src/lib/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>, width = 200): Record<string, unknown> {
export function compactRows(batch: PageRows, added: CollectedRow[], priorKeys: ReadonlySet<string>): Record<string, unknown> {
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)
Expand All @@ -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<FollowOutcome> {
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);
Expand All @@ -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) };
Expand Down
Loading