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
16 changes: 16 additions & 0 deletions src/agent_env/cli/task/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def _format_server_config_changes(changes: list[dict]) -> str:
return "\n".join(lines)


def _trajectory_turn_label(pr, turn: int | None) -> str:
"""Which turn a listed trajectory is, since the list skips turns that stored none: "turn k of n", with the wake on
an always_on step, whose turns count its nudges too, so turn k needn't be wake k; "last available" for a response
url the per-turn list lacks."""
if turn is None:
return "last available"
label = f"turn {turn} of {len(pr.target_agent_per_turn_trajectory_object_urls or [])}"
record = next(iter((pr.target_agent_per_turn_trajectories or [])[turn - 1:turn]), None)
if record is None or record.get("wake") is None:
return label
return f"{label} ({'nudge after wake' if record.get('nudge') else 'wake'} {record['wake']})"


def _format_prompt_response(pr) -> str:
indented_response = _indent(pr.response, " ")
lines = [
Expand All @@ -117,6 +130,9 @@ def _format_prompt_response(pr) -> str:
]
if pr.agent_trajectory_object_url:
lines.append(f" agent_trajectory_object_url: {pr.agent_trajectory_object_url}")
if len(trajectories := pr.distinct_trajectory_object_urls()) > 1:
lines.append(f" trajectory_object_urls ({len(trajectories)}, in turn order):")
lines.extend(f" - {_trajectory_turn_label(pr, turn)}: {url}" for turn, url in trajectories)
if pr.agent_trajectory_object_prefix:
lines.append(f" agent_trajectory_object_prefix: {pr.agent_trajectory_object_prefix}")
if pr.agent_trajectory_file_path:
Expand Down
263 changes: 170 additions & 93 deletions src/agent_env/explorer/ui/src/components/task-instance-viewer.tsx

Large diffs are not rendered by default.

224 changes: 224 additions & 0 deletions src/agent_env/explorer/ui/src/lib/bar-downloads.smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
* directory. The package has no Jest config and `test` is a no-op, so there is
* no runner a `.test.ts` file would be picked up by.
*/
import { isDeepStrictEqual } from 'node:util';

import {
MAX_INLINE_BAR_DOWNLOADS,
barDownloadFileCount,
buildBarDownloads,
shouldCollapseBarDownloads,
} from './bar-downloads';
Expand Down Expand Up @@ -156,6 +159,227 @@ function main(): void {
);
}

// A single trajectory keeps its plain entry: no turn in the label, no turn picker.
{
const [only] = buildBarDownloads(
[{ agent_trajectory_s3_uri: 'obj://t/one', target_agent_per_turn_trajectory_s3_uris: ['obj://t/one'], model: 'm' }],
);
assert(
isDeepStrictEqual(only, {
key: 'obj://t/one',
label: 'Trajectory',
title: 'Download Trajectory (m)',
objectUrl: 'obj://t/one',
}),
'a one-turn response is the same entry as a response with no per-turn list',
);
}

// An always_on step: one entry for the run. Its button downloads the last wake, the picker every wake.
{
const downloads = buildBarDownloads(
[
{
agent_trajectory_s3_uri: 'obj://w/3',
target_agent_per_turn_trajectory_s3_uris: ['obj://w/1', null, 'obj://w/3'],
step_id: 'week',
model: 'm',
},
],
);
assert(downloads.length === 1, 'an always_on run of several wakes is one entry');
const [run] = downloads;
assert(
run?.label === 'Trajectory (turn 3 of 3)' && run.objectUrl === 'obj://w/3',
'its button downloads the last wake, its label naming that turn',
);
assert(run?.title === 'Download Trajectory, turn 3 of 3 (m)', 'the tooltip names the turn and the model');
assert(
run?.turnPicker?.turns.map(t => `${t.label}=${t.objectUrl}`).join('|') ===
'turn 1 of 3=obj://w/1|turn 3 of 3=obj://w/3',
'the turn picker lists every wake that stored a trajectory, in wake order',
);
assert(run?.turnPicker?.label === 'Trajectory (3 turns)', 'the picker counts the run\'s turns');
}

// An always_on run whose turns name their wake: its per-turn list holds wake 1's nudge beside the wakes, so its
// second turn is that nudge, not wake 2. Each wake is labelled by its wake, the nudge as a nudge.
{
const records = [
{ format: 'shots/1', atif: null, wake: 1, nudge: false },
{ format: 'shots/1', atif: null, wake: 1, nudge: true },
{ format: 'shots/1', atif: null, wake: 2, nudge: false },
{ format: 'shots/1', atif: null, wake: 3, nudge: false },
];
const [run] = buildBarDownloads(
[
{
agent_trajectory_s3_uri: 'obj://w/3',
target_agent_per_turn_trajectory_s3_uris: ['obj://w/1', 'obj://w/1n', 'obj://w/2', 'obj://w/3'],
target_agent_per_turn_trajectories: records,
step_id: 'week',
model: 'm',
},
],
);
assert(
run?.label === 'Trajectory (wake 3 of 3)' && run.title === 'Download Trajectory, wake 3 of 3 (m)',
'an always_on run\'s button names its last wake, not its fourth turn',
);
assert(
run?.turnPicker?.turns.map(t => `${t.label}=${t.objectUrl}`).join('|') ===
'wake 1 of 3=obj://w/1|nudge after wake 1=obj://w/1n|wake 2 of 3=obj://w/2|wake 3 of 3=obj://w/3',
'the picker labels each wake by its wake and the nudge as a nudge, so turn 2 is not read as wake 2',
);
assert(run?.turnPicker?.label === 'Trajectory (3 wakes)', 'the picker counts the run\'s wakes, not its turns');

const [nudgedLast] = buildBarDownloads(
[
{
agent_trajectory_s3_uri: 'obj://w/1n',
target_agent_per_turn_trajectory_s3_uris: ['obj://w/1', 'obj://w/1n'],
target_agent_per_turn_trajectories: records.slice(0, 2),
step_id: 'week',
},
],
);
assert(
nudgedLast?.label === 'Trajectory (nudge after wake 1)',
'a run whose last turn was a nudge names it as one',
);
}

// A multi-turn prompt_agent step (a user-sim conversation of 20 turns) beside a single-turn step: two entries, not 21.
{
const turns = Array.from({ length: 20 }, (_, i) => `obj://chat/${i + 1}`);
const downloads = buildBarDownloads(
[
{ agent_trajectory_object_url: 'obj://t/setup', step_id: 'setup' },
{
agent_trajectory_object_url: turns[19],
target_agent_per_turn_trajectory_object_urls: turns,
step_id: 'converse',
},
],
);
assert(
downloads.map(d => d.label).join('|') === 'setup|converse (turn 20 of 20)',
'one entry per prompt response, the conversation labelled by its last turn',
);
assert(downloads[0]?.turnPicker === undefined, 'the single-turn step has no turn picker');
assert(
isDeepStrictEqual(downloads[1]?.turnPicker?.turns.map(t => t.objectUrl), turns) &&
downloads[1]?.turnPicker?.turns[0]?.label === 'turn 1 of 20' &&
downloads[1]?.turnPicker?.turns[0]?.title === 'Download converse, turn 1 of 20',
'the conversation\'s picker offers every turn, labelled "turn k of n"',
);
assert(!shouldCollapseBarDownloads(downloads.length), 'two entries stay inline');
}

// A Claude Code atif/1 run's turns share one document: one plain entry, while a step of distinct turns gets a picker.
{
const downloads = buildBarDownloads(
[
{
agent_trajectory_s3_uri: 'obj://atif/doc',
target_agent_per_turn_trajectory_s3_uris: ['obj://atif/doc', 'obj://atif/doc', 'obj://atif/doc'],
step_id: 'copied',
},
{
agent_trajectory_object_url: 'obj://w/2',
target_agent_per_turn_trajectory_object_urls: ['obj://w/1', 'obj://w/2'],
step_id: 'week',
},
],
);
assert(
downloads.map(d => `${d.label}:${d.turnPicker ? d.turnPicker.turns.length : 0}`).join('|') ===
'copied:0|week (turn 2 of 2):2',
'turns sharing a document give one entry and no picker; distinct turns give a picker',
);
assert(new Set(downloads.map(d => d.key)).size === downloads.length, 'keys stay unique');
}

// An atif/1 run whose second turn's copy failed, so that turn kept its native trajectory: per-turn [A, B, A]. The
// button downloads the response's own trajectory, A, which the last turn stored, not the middle turn's B.
{
const [run] = buildBarDownloads(
[
{
agent_trajectory_s3_uri: 'obj://atif/doc',
target_agent_per_turn_trajectory_s3_uris: ['obj://atif/doc', 'obj://native/2', 'obj://atif/doc'],
step_id: 'copied',
model: 'm',
},
],
);
assert(
run?.objectUrl === 'obj://atif/doc' && run.key === 'obj://atif/doc',
'a run of [A, B, A] downloads A, the response\'s own trajectory',
);
assert(
run?.label === 'Trajectory (turn 3 of 3)' && run.title === 'Download Trajectory, turn 3 of 3 (m)',
'its label names the last turn that stored A',
);
assert(
run?.turnPicker?.turns.map(t => `${t.label}=${t.objectUrl}`).join('|') ===
'turn 2 of 3=obj://native/2|turn 3 of 3=obj://atif/doc',
'its picker lists every distinct trajectory, A at the same turn as the button names it',
);
}

// A response that records only its per-turn list downloads the last turn that stored one.
{
const [run] = buildBarDownloads(
[{ target_agent_per_turn_trajectory_object_urls: ['obj://w/1', 'obj://w/2', null] }],
);
assert(
run?.objectUrl === 'obj://w/2' && run.label === 'Trajectory (turn 2 of 3)',
'with no agent_trajectory url, the button falls back to the run\'s last trajectory',
);
}

// A response url the per-turn list lacks can't be placed in a turn, so it isn't called the last turn's.
{
const [run] = buildBarDownloads(
[
{
agent_trajectory_s3_uri: 'obj://x/final',
target_agent_per_turn_trajectory_s3_uris: ['obj://x/1', 'obj://x/2'],
},
],
);
assert(
run?.objectUrl === 'obj://x/final' && run.label === 'Trajectory (last available)',
'a url outside the per-turn list is labelled "last available"',
);
}

// The collapsed bar's button counts files, not responses: a response with a turn picker is one row of several files.
{
const downloads = buildBarDownloads(
[
{ agent_trajectory_object_url: 'obj://a', step_id: 'a' },
{ agent_trajectory_object_url: 'obj://b', step_id: 'b' },
{
agent_trajectory_object_url: 'obj://c/7',
target_agent_per_turn_trajectory_object_urls: Array.from({ length: 7 }, (_, i) => `obj://c/${i + 1}`),
step_id: 'c',
},
{
agent_trajectory_object_url: 'obj://atif/doc',
target_agent_per_turn_trajectory_object_urls: ['obj://atif/doc', 'obj://atif/doc'],
step_id: 'd',
},
],
);
assert(
downloads.length === 4 && barDownloadFileCount(downloads) === 10,
'four responses, one of seven turns and one of a shared document, are ten files',
);
assert(barDownloadFileCount([]) === 0, 'no downloads, no files');
}

if (failures > 0) {
console.error(`\n${failures} assertion(s) failed`);
process.exit(1);
Expand Down
97 changes: 83 additions & 14 deletions src/agent_env/explorer/ui/src/lib/bar-downloads.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/** Download entries for the task-instance tab bar. Every prompt_agent step with `trajectory_output_prefix`
* uploads its own trajectory; the bar exposes one per trajectory (the last/solver step is usually most
* uploads its own trajectory; the bar exposes one entry per prompt response (the last/solver step is usually most
* interesting). Past MAX_INLINE_BAR_DOWNLOADS the caller collapses them into a dropdown. Built here (not inline) so the row and dropdown share entries/order and the labels are testable. */

/** How many downloads may sit inline in the bar before they collapse. */
export const MAX_INLINE_BAR_DOWNLOADS = 2;

import { type TrajectoryUrlKeys, trajectoryUrl } from './trajectory-url';
import {
type RunTrajectory,
type TrajectoryUrlKeys,
runTrajectoryUrls,
trajectoryUrl,
turnLabel,
} from './trajectory-url';

/** The subset of a prompt-response the bar needs to label a trajectory. */
export interface BarDownloadTrajectory extends TrajectoryUrlKeys {
Expand All @@ -14,7 +20,8 @@ export interface BarDownloadTrajectory extends TrajectoryUrlKeys {
model?: string;
}

export interface BarDownload {
/** One file the bar can download. */
export interface BarDownloadFile {
/** Stable React key. */
key: string;
label: string;
Expand All @@ -23,25 +30,87 @@ export interface BarDownload {
objectUrl: string;
}

/** Build the bar's download list from a run's prompt responses.
* Trajectories are labelled by step_id, else prompt_id, else "Trajectory N"; a single trajectory gets the bare
* "Trajectory". Entries with no URI are dropped. */
/** The turn picker of a response whose turns stored distinct trajectories. */
export interface BarDownloadTurnPicker {
/** The picker's row in the collapsed dropdown, e.g. "week (7 turns)". */
label: string;
/** The picker's tooltip. */
title: string;
/** Every turn's trajectory, in turn order, each labelled "turn k of n". */
turns: BarDownloadFile[];
}

/** One prompt response's entry. Its own file is the response's trajectory, the last available one when it has
* several. */
export interface BarDownload extends BarDownloadFile {
/** Set only when the response's turns stored several distinct trajectories. */
turnPicker?: BarDownloadTurnPicker;
}

/** The picker's count: an always_on run's wakes, else its turns. Several runs means the per-turn list named at least
* one, so the first run has the counts. */
function turnCount(run: RunTrajectory): string {
const [n, noun] = run.wakes !== undefined ? [run.wakes, 'wake'] : [run.turns, 'turn'];
return `${n} ${noun}${n === 1 ? '' : 's'}`;
}

/** The trajectory a response's own download gets: its `agent_trajectory` url (the last available one), as `runs`
* places and labels it, so the button and the picker name it alike; with no such url, the run's last trajectory.
* `runs` always holds that url: it appends it, with no turn, when the per-turn list lacks it. */
function mainTrajectory(pr: TrajectoryUrlKeys, runs: RunTrajectory[]): RunTrajectory {
const url = trajectoryUrl(pr);
return runs.find(run => run.url === url) ?? runs[runs.length - 1]!;
}

/** How many files the bar offers: a response with a turn picker is one entry of several. */
export function barDownloadFileCount(downloads: BarDownload[]): number {
return downloads.reduce((n, download) => n + (download.turnPicker?.turns.length ?? 1), 0);
}

/** Build the bar's download list from a run's prompt responses: one entry per response that has a trajectory.
* Trajectories are labelled by step_id, else prompt_id, else "Trajectory N"; a single response gets the bare
* "Trajectory". A response whose turns stored several distinct trajectories (a multi-turn prompt_agent step, an
* always_on step's wakes) downloads its last available trajectory, its label naming that turn, e.g. "Trajectory
* (turn 7 of 7)", or for an always_on step "Trajectory (wake 7 of 7)", and carries a turn picker with every turn's. Turns that share one document (an atif/1 run) are one
* trajectory, so that response gets a plain entry. Responses with no URI are dropped. */
export function buildBarDownloads(
promptResponses: BarDownloadTrajectory[],
): BarDownload[] {
const withTrajectory = promptResponses.filter(pr => trajectoryUrl(pr));
const withTrajectory = promptResponses
.map(pr => ({ pr, runs: runTrajectoryUrls(pr) }))
.filter(({ runs }) => runs.length > 0);
const single = withTrajectory.length === 1;

return withTrajectory.map((pr, i) => {
const label = single
return withTrajectory.map(({ pr, runs }, i) => {
const name = single
? 'Trajectory'
: pr.step_id || pr.prompt_id || `Trajectory ${i + 1}`;
const objectUrl = trajectoryUrl(pr) as string;
const model = pr.model ? ` (${pr.model})` : '';
const main = mainTrajectory(pr, runs);
if (runs.length === 1) {
return {
key: main.url,
label: name,
title: `Download ${name}${model}`,
objectUrl: main.url,
};
}
const turns = runs.map(run => ({
key: run.url,
label: turnLabel(run),
title: `Download ${name}, ${turnLabel(run)}${model}`,
objectUrl: run.url,
}));
return {
key: objectUrl,
label,
title: pr.model ? `Download ${label} (${pr.model})` : `Download ${label}`,
objectUrl,
key: main.url,
label: `${name} (${turnLabel(main)})`,
title: `Download ${name}, ${turnLabel(main)}${model}`,
objectUrl: main.url,
turnPicker: {
label: `${name} (${turnCount(runs[0]!)})`,
title: `Download one turn of ${name}`,
turns,
},
};
});
}
Expand Down
Loading
Loading