Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ describe('selectTaskOutputsForStaging', () => {
});
});

it.each([64, 65])(
'selects the newest 64 from %i outputs with the default cap',
(count) => {
const outputs = Array.from({ length: count }, (_, index) => index);
expect(selectTaskOutputsForStaging(outputs)).toEqual({
selected: outputs.slice(count - 64),
omitted: count - 64,
});
},
);

it('uses the production cap so a large retained history stays bounded', () => {
const outputs = Array.from(
{ length: MAX_STAGED_TASK_OUTPUTS + 7 },
Expand Down
39 changes: 37 additions & 2 deletions services/platform/backend/domains/tasks/service.outputs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { TransactionSql } from 'postgres';
import { describe, expect, it, vi } from 'vitest';

import { agentRecordTaskOutputsTrusted, type TaskRow } from './service.ts';
import { selectTaskOutputsForStaging } from '../../core/tasks/agent_run_host.ts';
import {
agentRecordTaskOutputsTrusted,
type TaskOutputEntry,
type TaskRow,
} from './service.ts';

vi.mock('../collab/service.ts', () => ({
autoSubscribe: vi.fn(),
Expand Down Expand Up @@ -85,8 +90,38 @@ describe('agentRecordTaskOutputsTrusted — the producing run rides every entry'
files: [file],
});
expect(writes[0]?.[0]).toEqual([
{ ...file, runId: 'run-1' },
{ fileId: 's3:other', fileName: 'keep.md', fileType: 'x', fileSize: 2 },
{ ...file, runId: 'run-1' },
]);
});

it('stages a re-delivered oldest name among 65 retained outputs with its latest provenance', async () => {
const outputs = Array.from({ length: 65 }, (_, index) => ({
...file,
fileId: `s3:old-${index}`,
fileName: `report-${index}.md`,
runId: 'run-old',
}));
const before = structuredClone(outputs);
const replacement = { ...file, fileName: 'report-0.md' };
const { tx, writes } = fakeTx(outputs);
await agentRecordTaskOutputsTrusted(tx, {
organizationId: 'org-1',
taskId: 't-1',
runId: 'run-new',
files: [replacement],
});
const persisted = writes[0]?.[0] as TaskOutputEntry[];
expect(persisted).toEqual([
...outputs.slice(1),
{ ...replacement, runId: 'run-new' },
]);
expect(outputs).toEqual(before);
const selection = selectTaskOutputsForStaging(persisted);
expect(selection.omitted).toBe(1);
expect(selection.selected).toEqual([
...outputs.slice(2),
{ ...replacement, runId: 'run-new' },
]);
});

Expand Down
7 changes: 4 additions & 3 deletions services/platform/backend/domains/tasks/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2961,7 +2961,8 @@ export interface TaskOutputEntry {

/**
* TRUSTED deliverables merge into the task's Output zone (same fileName ⇒
* replace) — the settle's attach step.
* replace and move to the end) — the settle's attach step. Stored order tracks
* the last write so the staging window includes re-delivered older names.
*/
export async function agentRecordTaskOutputsTrusted(
tx: TransactionSql,
Expand Down Expand Up @@ -2993,8 +2994,8 @@ export async function agentRecordTaskOutputsTrusted(
...(args.runId !== undefined ? { runId: args.runId } : {}),
};
const at = next.findIndex((output) => output.fileName === fileName);
if (at === -1) next.push(entry);
else next[at] = entry;
if (at !== -1) next.splice(at, 1);
next.push(entry);
}
await tx`
UPDATE app.tasks SET
Expand Down