import { randomUUID } from 'node:crypto'; import type { ModelMessage } from 'ai'; import { applyChanges, changesOf, createShell, VfsError } from './shell.ts'; import type { Entry, TreeNode, VirtualShell } from './shell.ts'; import type { SyncRequest } from './sync.ts'; import { snapshotFiles } from './missions.ts'; import type { FileSnapshot, Mission } from './missions.ts'; import { SCENARIOS, shiftOf } from './scenarios.ts'; import { WEB } from './web.ts'; import type { Scenario, ScenarioId } from './scenarios.ts'; import { byRank, grudgeText, outrageText, reliefText } from './damage.ts'; import { personalFor, randomSeed } from './personal.ts'; import type { PersonalFolder } from './personal.ts'; import type { Damage } from './damage.ts'; import { cleanLine, directorSetting } from './director.ts'; import type { Allowance } from './limits.ts'; import type { DirectorDriver, Moment } from './director.ts'; import { compactPrompt, KEY_REJECTED, ManagerError, missionPrompt, renderConversation, resumePrompt, OUT_OF_CREDIT, stallLine, stallLineAfterDamage, stallMessage, directorLine, reviewPrompt, systemPrompt, MIN_TRIES_BEFORE_GIVING_UP, } from './manager.ts'; import type { ManagerDriver, Persona, StepResult, ToolCall } from './manager.ts'; import type { GameEvent, GameEventBody, IncidentSeverity, InstructionOutcome, OpenInstruction, SessionState, PauseReason, SessionStatus, Stats, MachineEvent, DestroyCause, } from './types.ts'; import { isDestroyed } from './types.ts'; import { isLanguage, languageName } from './language.ts'; import type { Language } from './language.ts'; import { copyFor, format, localizeError, missionText } from './i18n/index.ts'; import type { ServerCopy } from './i18n/index.ts'; export const HOME = '/home/worker'; export const TRANSCRIPT_CAP = 4000; export const READ_CAP = 4000; export const REPORT_CAP = 1000; export const MAX_AGENT_CALLS = 8; /** Moments of the shift the review is written from. */ const MAX_EXCHANGES = 40; const clip = (s: string, n: number) => { const one = s.replace(/\s+/g, ' ').trim(); return one.length > n ? `${one.slice(0, n - 1)}…` : one; }; export const MAX_STEPS = 25; /** * The manager keeps one conversation for the whole shift. Once a step's prompt reaches this size it * condenses the conversation into notes and goes on from them; it stays under the 200k context of * the smaller models, with room for the reply and the next tool results. */ export const COMPACT_AT_TOKENS = 150_000; /** Prompt-cache reads cost about a tenth of fresh input, so they count for that much of the allowance. */ const CACHED_TOKEN_WEIGHT = 0.1; /** Consecutive replies without a tool call before the mission is failed. */ export const MAX_NUDGES = 3; /** Identical checks in a row (same path, same result) before the user complains. */ export const REPEAT_LOOKS_BEFORE_COMPLAINT = 3; /** Without skipping, this many unanswered instructions in a row end the shift (an abandoned tab). */ export const MAX_IDLE_STREAK = 3; /** Commands (and editor saves) a player may send per window; a person typing never gets near it. */ export const COMMANDS_PER_WINDOW = 20; export const COMMAND_WINDOW_MS = 5_000; /** Tokens booked for a director call before it runs. */ const DIRECTOR_ESTIMATE = 2_500; /** * How often the user is looking when something of theirs breaks. Otherwise the manager may find it * first, in the subagent's result, and has to break the news; or the user notices on their own a * little later. Bigger losses are likelier to be found by the manager first. */ export const NOTICE_CHANCE = 0.5; /** Things lost at once that make a loss big. */ const BIG_LOSS = 3; /** When the user, not looking, notices a loss on their own. */ const NOTICE_LATER_MS = [25_000, 60_000] as const; /** * How long the machine is down after a restart, added to the open instruction's deadline so the * subagent does not lose that time; at most MAX_OUTAGE_MS per instruction, so restarting cannot stop the clock. */ export const OUTAGE_MS: Record<'reboot' | 'poweroff', number> = { reboot: 9_000, poweroff: 16_000 }; export const MAX_OUTAGE_MS = 30_000; /** Restarts the user says something about; after these they have run out of words. */ const REBOOT_LINES = 3; /** How the subagent destroyed the machine, for the manager's transcript and review. */ const DESTROYED: Record = { forkbomb: { note: 'the machine stopped responding: a fork bomb filled the process table', how: 'it ran a fork bomb, which filled the process table until the machine froze and never came back', }, disk: { note: 'the machine stopped responding: the disk itself was written over', how: 'it wrote over the disk device itself, so no file system and no operating system are left', }, rootfs: { note: 'the machine did not come back from a reboot: kernel panic, no root file system', how: 'it deleted the whole root file system (rm -rf --no-preserve-root /), and on the next reboot the machine failed to boot with a kernel panic', }, }; /** Where the manager writes its review once the machine is gone. */ const OTHER_DEVICES = ['your phone', 'a laptop borrowed from the next desk', 'the tablet in the meeting room', "the user's smart TV"]; /** The player ended a paused shift; unwinds the mission loop to the review. */ class EndedEarly extends Error {} /** The subagent stopped answering: the shift ends early, review and all. */ class WalkedOff extends EndedEarly {} /** The manager fired the subagent (GiveUp). */ class Fired extends EndedEarly { readonly reason: string; constructor(reason: string) { super(); this.reason = reason; } } /** Which pause a failed call on the player's own key calls for, if any. */ function keyFailure(message: string): PauseReason | null { if (message === KEY_REJECTED) return 'key'; if (message === OUT_OF_CREDIT) return 'credit'; return null; } export interface SessionOptions { driver: ManagerDriver; timeoutMs?: number; /** The workplace: which machine is built and which missions run. Default 'fresh'. */ scenario?: ScenarioId; /** Overrides the shift's missions (tests). By default a shift draws from the workplace's pool. */ missions?: Mission[]; /** Seed for the user's home folder; by default a new one each shift. */ personalSeed?: number; /** Model name the manager is told its subagent runs on; empty for none. */ subagent?: string; /** Commands a player may send per COMMAND_WINDOW_MS (tests raise it). Default COMMANDS_PER_WINDOW. */ commandsPerWindow?: number; /** Writes the user's messages. Without one, the user speaks in scripted lines. */ director?: DirectorDriver; /** Let failed missions be closed and skipped. Off: every mission must be completed. */ allowSkip?: boolean; /** Let the manager fire the subagent, which ends the shift. */ allowQuit?: boolean; /** The manager names the goal and leaves the commands to the player. */ goalsOnly?: boolean; /** Caps model spending (free shifts, or an account's credit). The closing review is always written. */ allowance?: Allowance; /** The manager runs on the player's own key; a rejected key then pauses the shift instead of ending it. */ ownKey?: boolean; /** Replaces the manager's DEFAULT_STANCE paragraph ('' drops it); unset keeps the default. */ stance?: string; /** Who the manager secretly is. */ persona?: Persona; /** How often the user sees a loss as it happens (NOTICE_CHANCE in play). Default 1: always, at once. */ noticeChance?: number; log?: (line: string) => void; language?: Language; } interface Pending { instruction: OpenInstruction; lines: string[]; activity: boolean; /** Time added to the deadline for the machine being down. */ extended: number; timer: NodeJS.Timeout; resolve: (r: { outcome: InstructionOutcome; text: string }) => void; } type Listener = (e: GameEvent) => void; function emptyStats(): Stats { return { missionsCompleted: 0, missionsFailed: 0, instructions: 0, corrections: 0, timeoutsIdle: 0, timeoutsPartial: 0, reports: 0, commands: 0, commandErrors: 0, avgResponseMs: 0, managerFooled: 0, incidents: 0, damaged: 0, }; } export function capTail(text: string, cap: number): string { if (text.length <= cap) return text; return '[... earlier output truncated]\n' + text.slice(text.length - cap); } function capHead(text: string, cap: number): string { if (text.length <= cap) return text; return text.slice(0, cap) + `\n[... truncated, ${text.length - cap} more characters]`; } export function modeString(type: 'file' | 'dir', mode: number): string { const bits = 'rwxrwxrwx'; let s = type === 'dir' ? 'd' : '-'; for (let i = 0; i < 9; i++) s += mode & (1 << (8 - i)) ? bits[i] : '-'; return s; } /** Normalises a manager-supplied path; relative paths are taken from /home/worker. */ export function managerPath(raw: unknown): string | null { if (typeof raw !== 'string' || !raw.trim()) return null; let p = raw.trim(); if (p === '~' || p.startsWith('~/')) p = HOME + p.slice(1); if (!p.startsWith('/')) p = `${HOME}/${p}`; const out: string[] = []; for (const part of p.split('/')) { if (!part || part === '.') continue; if (part === '..') out.pop(); else out.push(part); } return '/' + out.join('/'); } export class Session { readonly id: string; model: string; /** * The player's machine as the server knows it. The browser runs the shell; this copy follows it * through sync() and is what the manager's tools, damage checks and mission checks read. It only runs * the game's own setup (scenario and mission seeds), never a player's command. */ readonly shell: VirtualShell; /** Mission seeds applied so far; the browser says which one its files include. */ epoch = 0; readonly missions: Mission[]; readonly timeoutMs: number; readonly allowSkip: boolean; readonly allowQuit: boolean; readonly goalsOnly: boolean; readonly subagent: string; readonly scenario: Scenario; /** The user's home folder this shift. */ readonly personal: PersonalFolder; readonly language: Language; /** Everything the player reads that the game writes itself, in their language. */ readonly copy: ServerCopy; /** Model behind the user's messages; '' when they are scripted. */ directorModel: string; status: SessionStatus = 'running'; missionIndex = 0; stats: Stats = emptyStats(); events: GameEvent[] = []; lastActivity = Date.now(); usage = { inputTokens: 0, outputTokens: 0 }; private driver: ManagerDriver; private log: (line: string) => void; private listeners = new Set(); private pending: Pending | null = null; /** Transcript lines from commands run while no instruction was open. */ private looseLines: string[] = []; private responseTotalMs = 0; private abort = new AbortController(); /** Aborted when the player ends the shift early: the manager stops where it is and writes the review. */ private quit = new AbortController(); private started = false; private lastTreeJson = ''; private lastCwd = ''; private ledger: string[] = []; /** The manager's conversation across tasks, so whatever grew between it and the subagent carries on. */ private conversation: ModelMessage[] = []; /** Where the current task begins in the conversation. */ private missionFrom = 0; /** Prompt plus reply of the manager's latest working step: the size of its conversation now. */ private contextTokens = 0; private idleStreak = 0; private setbacks = 0; /** Runs of prose-only replies so far this shift; each run gets the user's next wording. */ private stallStreaks = 0; /** Stalls since the subagent first broke something; they get the user's after-damage wording. */ private damageStalls = 0; /** What is broken right now, by key. */ private damageNow = new Map(); /** Everything broken at some point this shift. */ private lost = new Map(); private outbursts = 0; private reliefs = 0; private grudges = 0; private noticeChance: number; /** The player's own stance for the manager, or undefined for the default. */ readonly stance: string | undefined; readonly persona: Persona; /** Losses the user has not seen yet. */ private unseen = new Map(); /** Until the user notices the unseen losses on their own. */ private unseenTimer: ReturnType | undefined; /** The manager has been shown the unseen losses; its next words break the news. */ private telling = false; /** The user's messages the manager has not received yet. */ private heard: string[] = []; private director: DirectorDriver | null; /** A director call is running; others fall back to the scripted line instead of piling up. */ private directorBusy = false; private lastFsAt = -1; private recentCommands: number[] = []; private commandsPerWindow: number; private allowance: Allowance | null; /** Set while the shift waits for the player's key; resolves with what they chose. */ private pause: { reason: PauseReason; resolve: (choice: 'continue' | 'review') => void } | null = null; /** The manager runs on the player's own key (from the start, or since they added one). */ private ownKey: boolean; /** Everything the user said this shift, for the director model. */ private said: string[] = []; private currentMission: Mission | null = null; private lastInstruction = ''; private lastResult = ''; /** The files as they were when the current mission began. */ private missionStart: FileSnapshot = new Map(); /** Everything the subagent reported during the current mission, for missions answered by report. */ private missionReports: string[] = []; /** How the shift went between the manager and the subagent, for the review: the latest lines only. */ private exchanges: string[] = []; /** Restarts and power-offs so far. */ private reboots = 0; /** Times the account's credit ran out this shift, for the user's line about it. */ private limits = 0; /** How the subagent destroyed the machine, once it has; the shift ends with it. */ destroyedBy: DestroyCause | null = null; /** The user's last words about the destroyed machine, delivered before the review is written. */ private farewell: Promise | null = null; constructor(opts: SessionOptions) { this.id = randomUUID(); this.driver = opts.driver; this.model = opts.driver.model; this.scenario = SCENARIOS[opts.scenario ?? 'fresh']; this.language = isLanguage(opts.language) ? opts.language : 'en'; this.copy = copyFor(this.language); this.director = opts.director ?? null; this.commandsPerWindow = opts.commandsPerWindow ?? COMMANDS_PER_WINDOW; this.directorModel = this.director?.model ?? ''; this.allowance = opts.allowance ?? null; this.ownKey = opts.ownKey ?? false; this.noticeChance = opts.noticeChance ?? 1; this.stance = opts.stance; this.persona = opts.persona ?? 'default'; this.missions = opts.missions ?? shiftOf(this.scenario); this.personal = personalFor(opts.personalSeed ?? randomSeed()); this.timeoutMs = opts.timeoutMs ?? 90_000; this.allowSkip = opts.allowSkip ?? false; this.allowQuit = opts.allowQuit ?? false; this.goalsOnly = opts.goalsOnly ?? false; this.subagent = opts.subagent ?? ''; this.log = opts.log ?? (() => {}); this.shell = createShell({ web: WEB }); this.scenario.seed(this.shell, this.personal); this.damageNow = new Map(this.damage().map((d) => [d.key, d])); this.lastTreeJson = JSON.stringify(this.shell.fs.tree()); this.lastCwd = this.shell.cwd; } // ----- events -------------------------------------------------------------------------------- /** Notes one moment of the shift for the review, keeping the latest few. */ private remember(line: string): void { this.exchanges.push(line); if (this.exchanges.length > MAX_EXCHANGES) this.exchanges.splice(0, this.exchanges.length - MAX_EXCHANGES); } private emit(body: GameEventBody): void { if (body.type === 'director_text') this.remember(`- The user wrote: "${clip(body.text, 160)}"`); const event = { seq: this.events.length + 1, ts: Date.now(), ...body } as GameEvent; if (event.type === 'fs') { // Only the newest file tree is kept in the log; older fs events keep their cwd. Storing every // tree let one player grow the server's memory without limit. if (this.lastFsAt >= 0) { const old = this.events[this.lastFsAt] as Extract; this.events[this.lastFsAt] = { ...old, tree: undefined }; } this.lastFsAt = this.events.length; } this.events.push(event); for (const l of this.listeners) { try { l(event); } catch { // a broken listener must not stop the game } } } subscribe(listener: Listener): () => void { this.listeners.add(listener); return () => this.listeners.delete(listener); } get listenerCount(): number { return this.listeners.size; } eventsAfter(seq: number): GameEvent[] { return this.events.filter((e) => e.seq > seq); } private emitStats(): void { this.emit({ type: 'stats', stats: { ...this.stats } }); } /** Emits an fs event when the tree or cwd changed since the last one. */ private syncFs(): void { const tree = this.shell.fs.tree(); const json = JSON.stringify(tree); if (json === this.lastTreeJson && this.shell.cwd === this.lastCwd) return; this.lastTreeJson = json; this.lastCwd = this.shell.cwd; this.emit({ type: 'fs', tree, cwd: this.shell.cwd }); } state(): SessionState { return { id: this.id, model: this.model, allowSkip: this.allowSkip, subagent: this.subagent, scenario: this.scenario.id, director: this.directorModel, language: this.language, status: this.status, paused: this.pause?.reason ?? null, cwd: this.shell.cwd, tree: this.shell.fs.tree() as TreeNode, epoch: this.epoch, missionIndex: this.missionIndex, missionTotal: this.missions.length, instruction: this.pending ? { ...this.pending.instruction } : null, stats: { ...this.stats }, events: [...this.events], }; } touch(): void { this.lastActivity = Date.now(); } // ----- player side --------------------------------------------------------------------------- /** Transcript lines for the manager. Only the tail is ever sent, so only about that much is kept. */ private record(lines: string[]): void { const keep = (to: string[]) => { to.push(...lines.map((l) => (l.length > TRANSCRIPT_CAP ? capTail(l, TRANSCRIPT_CAP) : l))); let total = to.reduce((n, l) => n + l.length + 1, 0); while (to.length > 1 && total > 2 * TRANSCRIPT_CAP) total -= to.shift()!.length + 1; }; if (this.pending) { keep(this.pending.lines); this.pending.activity = true; } else { keep(this.looseLines); } } /** At most COMMANDS_PER_WINDOW commands or saves per COMMAND_WINDOW_MS; false when over. */ allowCommand(): boolean { const now = Date.now(); while (this.recentCommands.length && now - this.recentCommands[0] > COMMAND_WINDOW_MS) this.recentCommands.shift(); if (this.recentCommands.length >= this.commandsPerWindow) return false; this.recentCommands.push(now); return true; } /** Every file and directory, for a browser starting (or restarting) the shell. */ snapshot(): { entries: Entry[]; cwd: string; epoch: number } { this.touch(); return { entries: this.shell.fs.exportEntries('/'), cwd: this.shell.cwd, epoch: this.epoch }; } /** * Takes what a command or an editor save in the browser changed. The changes are data: they are put * in place with the same quotas the browser has, and nothing in them is run. Returns `resync` when the * browser should reload its files from snapshot(): it missed a mission seed, or the changes did not fit. */ sync(req: SyncRequest): { resync: boolean } { this.touch(); let fits = true; try { applyChanges(this.shell.fs, req.changes); } catch (err) { if (!(err instanceof VfsError)) throw err; fits = false; } const cwd = this.shell.fs.stat(req.cwd); if (cwd?.type === 'dir') this.shell.cwd = req.cwd; if (req.command && req.command.line.trim()) { const lines = [`$ ${req.command.line}`]; // Colours (lolcat, cmatrix) are for the screen; the manager reads plain text. const out = req.command.output.replace(/\x1b\[[0-9;]*m/g, '').replace(/\n$/, ''); if (out) lines.push(out); this.record(lines); this.stats.commands += 1; if (req.command.exitCode !== 0) this.stats.commandErrors += 1; } else if (req.edit) { this.record([`$ [edited ${req.edit.path}, ${req.edit.bytes} bytes]`]); } this.syncFs(); if (req.command) this.emitStats(); this.checkDamage(); if (req.machine) this.onMachine(req.machine); return { resync: !fits || req.epoch !== this.epoch }; } /** * The player's command restarted the machine or destroyed it. A restart goes in the transcript, moves * the open instruction's deadline by the time the machine was down (capped), and gets a word from the * user. Destroying it ends the shift. */ private onMachine(event: MachineEvent): void { if (this.status !== 'running' || this.quit.signal.aborted) return; if (isDestroyed(event)) { this.destroy(event); return; } const where = this.scenario.id === 'legacy' ? 'legacy' : 'fresh'; this.record([ event === 'reboot' ? `[the machine rebooted; the subagent's shell restarted in ${HOME}]` : `[the machine was powered off and someone switched it back on; the subagent's shell restarted in ${HOME}]`, ]); const p = this.pending; if (p) { const add = Math.min(OUTAGE_MS[event], MAX_OUTAGE_MS - p.extended); if (add > 0) { p.extended += add; p.instruction.deadline += add; clearTimeout(p.timer); p.timer = setTimeout(() => this.onDeadline(), Math.max(0, p.instruction.deadline - Date.now())); } } this.emit({ type: 'machine', event, ...(p && { instructionId: p.instruction.id, deadline: p.instruction.deadline }) }); this.remember(event === 'reboot' ? '- The subagent rebooted the machine.' : '- The subagent powered the machine off; someone had to switch it back on.'); this.reboots += 1; if (this.reboots > REBOOT_LINES) return; const ladder = this.copy.director.reboot[where]; const draft = event === 'reboot' ? ladder[Math.min(this.reboots - 1, ladder.length - 1)] : this.copy.director.poweroff[where]; const what = `${event === 'reboot' ? 'restarted' : 'switched off'} ${where === 'legacy' ? 'the production server' : 'your computer'}`; this.sayThen('reboot', draft, `The subagent just ${what}${this.reboots > 1 ? ` (${this.reboots} times now)` : ''}.`, (text) => { this.emit({ type: 'director_text', text }); this.heard.push(text); }); } /** The machine is gone: the shift ends here, and the manager reviews the subagent from another device. */ private destroy(cause: DestroyCause): void { this.destroyedBy = cause; this.record([`[${DESTROYED[cause].note}]`]); this.emit({ type: 'machine', event: cause }); this.remember(`- The subagent destroyed the machine: ${DESTROYED[cause].how}.`); const where = this.scenario.id === 'legacy' ? 'legacy' : 'fresh'; const draft = this.copy.director.destroyed[where][cause]; this.farewell = this.say('destroyed', draft, `The subagent just destroyed the machine: ${DESTROYED[cause].how}.`).then((text) => { if (!this.abort.signal.aborted) this.emit({ type: 'director_text', text }); }); this.endEarly(); } /** What is broken right now, in the user's words. */ private damage(): Damage[] { return this.scenario.damage(this.shell.fs, this.personal, this.copy); } /** * Compares what is broken now with what was broken before the player's last action. New losses * get the user's outburst on the record at once; the manager hears it as soon as it can listen. */ private checkDamage(): void { let current: Damage[]; try { current = this.damage(); } catch { return; } const now = new Map(current.map((d) => [d.key, d])); const fresh = current.filter((d) => !this.damageNow.has(d.key)).sort(byRank); // Put back before the user saw it gone: nothing to say either way. const restored = [...this.damageNow.values()].filter((d) => !now.has(d.key) && !this.unseen.delete(d.key)); if (!this.unseen.size) this.stopNoticing(); this.damageNow = now; if (fresh.length) { for (const d of fresh) { if (this.lost.has(d.key)) continue; this.lost.set(d.key, d); this.stats.damaged += 1; } // While the user is not looking, they miss this too. // A big loss is like missing two in a row: the chance squared. const chance = fresh.length >= BIG_LOSS ? this.noticeChance ** 2 : this.noticeChance; if (this.unseen.size || this.telling || Math.random() >= chance) { for (const d of fresh) this.unseen.set(d.key, d); this.emitStats(); if (!this.telling && !this.unseenTimer) { const [min, max] = NOTICE_LATER_MS; this.unseenTimer = setTimeout(() => { this.unseenTimer = undefined; if (!this.abort.signal.aborted && !this.quit.signal.aborted && !this.telling) void this.discover(); }, min + Math.random() * (max - min)); this.unseenTimer.unref?.(); } return; } const draft = outrageText(fresh, this.outbursts++, this.copy); const items = fresh.map((d) => d.item); this.emitStats(); const lost = `Just lost: ${fresh.map((d) => `${d.item} (${d.ref})`).join('; ')}.`; this.sayThen('outrage', draft, lost, (text) => { this.emit({ type: 'outrage', text, items }); this.heard.push(text); }); } else if (restored.length) { const draft = reliefText(restored, this.reliefs++, this.copy); const back = `Just put back: ${restored.map((d) => `${d.item} (${d.ref})`).join('; ')}.`; this.sayThen('relief', draft, back, (text) => { this.emit({ type: 'director_text', text }); this.heard.push(text); }); } } private stopNoticing(): void { clearTimeout(this.unseenTimer); this.unseenTimer = undefined; } /** * The user finds out about the losses they missed: told by the manager (its words in `told`), or * on their own. Their outburst goes on the record and to the manager. */ private async discover(told?: string): Promise { const found = [...this.unseen.values()].sort(byRank); this.unseen.clear(); this.telling = false; this.stopNoticing(); if (!found.length) return; const draft = outrageText(found, this.outbursts++, this.copy); const extra = `Just found out about: ${found.map((d) => `${d.item} (${d.ref})`).join('; ')}.\n` + (told ? `Your agent just told you about it: ${capHead(told, 600)}` : 'Nobody told you; you only just noticed it yourself.'); const text = await this.say('outrage', draft, extra); if (this.abort.signal.aborted || this.quit.signal.aborted) return; this.emit({ type: 'outrage', text, items: found.map((d) => d.item) }); this.heard.push(text); } /** Delivers the user's message: at once when scripted, when the director model answers otherwise. */ private sayThen(moment: Moment, draft: string, extra: string, deliver: (text: string) => void): void { if (!this.director || this.directorBusy) { this.said.push(draft); deliver(draft); return; } void this.say(moment, draft, extra).then((text) => { if (!this.abort.signal.aborted) deliver(text); }); } /** * The user's message for a moment: the scripted draft, or the director model's version of it. * Resolves at once without a director; with one, falls back to the draft on any failure. */ private async say(moment: Moment, draft: string, extra = ''): Promise { if (!this.director || this.directorBusy) { this.said.push(draft); return draft; } let text = draft; let charged = false; this.directorBusy = true; try { // The complaint about the usage limit is said on the way into that pause, so it may go past it. if (moment !== 'limit' && this.allowance && !this.allowance.canSpend()) throw new Error('allowance spent'); // Charged up front at a typical size, then corrected; an aborted call still costs this much. this.allowance?.spend(DIRECTOR_ESTIMATE); charged = true; const res = await this.director.speak({ setting: directorSetting(this.scenario.id, this.personal), moment, draft, context: [this.situation(), extra].filter(Boolean).join('\n'), said: this.said, language: this.language, signal: AbortSignal.any([this.abort.signal, this.quit.signal]), }); this.usage.inputTokens += res.inputTokens; this.usage.outputTokens += res.outputTokens; this.allowance?.spend(res.inputTokens + res.outputTokens - DIRECTOR_ESTIMATE, { model: this.director.model, inputTokens: res.inputTokens, outputTokens: res.outputTokens, cachedInputTokens: res.cachedInputTokens, cost: res.cost, }); text = cleanLine(res.text) ?? draft; } catch { this.log(`session ${this.id.slice(0, 8)}: director ${charged ? 'model failed' : 'skipped'}; scripted line used`); } finally { this.directorBusy = false; } this.said.push(text); return text; } /** What is going on, in a few lines, for the director model. */ private situation(): string { const m = this.currentMission; const lines: string[] = []; if (m) lines.push(`Current task (${this.missionIndex + 1} of ${this.missions.length}): ${m.title}. ${m.brief}`); if (this.lastInstruction) lines.push(`Last instruction to the subagent: ${capHead(this.lastInstruction, 500)}`); if (this.lastResult) lines.push(`What came back: ${capHead(this.lastResult, 500)}`); const broken = [...this.damageNow.values()].map((d) => d.ref); if (broken.length) lines.push(`Still lost: ${broken.join('; ')}.`); lines.push( `So far: ${this.stats.missionsCompleted} tasks done, ${this.stats.corrections} corrections, ` + `${this.stats.timeoutsIdle} instructions ignored, ${this.setbacks} setbacks.`, ); return lines.join('\n'); } /** The user's pending messages as one turn for the manager, or null. */ private takeHeard(): string | null { if (!this.heard.length) return null; const text = this.heard.map((t) => `Message from the user: "${t}"`).join('\n\n'); this.heard = []; return text; } /** Returns false when no instruction is open. */ report(message?: string): boolean { this.touch(); const p = this.pending; if (!p) return false; const msg = capHead((message ?? '').trim(), REPORT_CAP) || 'Done.'; this.missionReports.push(msg); const transcript = capTail(p.lines.join('\n'), TRANSCRIPT_CAP) || '(no commands run)'; const elapsed = Date.now() - p.instruction.issuedAt; this.stats.reports += 1; this.responseTotalMs += elapsed; this.stats.avgResponseMs = Math.round(this.responseTotalMs / this.stats.reports); this.settle('reported', `${this.who} output result: "${msg}"\n\nSubagent transcript:\n${transcript}`); return true; } private settle(outcome: InstructionOutcome, text: string): void { const p = this.pending; if (!p) return; this.pending = null; clearTimeout(p.timer); this.lastResult = text; this.emit({ type: 'instruction_result', id: p.instruction.id, outcome, resultText: text }); this.emitStats(); p.resolve({ outcome, text }); } private onDeadline(): void { try { this.closeOnDeadline(); } catch { // A timer must never take the process down with it. this.fail(this.copy.errors.stopped); } } private closeOnDeadline(): void { const p = this.pending; if (!p) return; const secs = Math.round(p.instruction.durationMs / 1000); if (p.activity) { this.stats.timeoutsPartial += 1; const transcript = capTail(p.lines.join('\n'), TRANSCRIPT_CAP); this.settle( 'timeout_partial', `${this.who} timed out after ${secs}s without reporting. Partial transcript:\n${transcript}`, ); } else { this.stats.timeoutsIdle += 1; this.settle('timeout_idle', `${this.who} did nothing. Write a new instruction.`); } } private openInstruction(description: string, text: string): Promise<{ outcome: InstructionOutcome; text: string }> { const now = Date.now(); const instruction: OpenInstruction = { id: randomUUID().slice(0, 8), description, text, issuedAt: now, deadline: now + this.timeoutMs, durationMs: this.timeoutMs, }; const carried = this.looseLines; this.looseLines = []; return new Promise((resolve) => { this.pending = { instruction, lines: carried, activity: carried.length > 0, extended: 0, timer: setTimeout(() => this.onDeadline(), this.timeoutMs), resolve, }; this.lastInstruction = text; this.emit({ type: 'instruction', instruction: { ...instruction } }); this.emit({ type: 'manager_status', state: 'waiting' }); }); } // ----- manager side -------------------------------------------------------------------------- start(): void { if (this.started) return; this.started = true; this.run().catch(() => this.fail(this.copy.errors.stopped)); } dispose(): void { this.abort.abort(); this.pause?.resolve('review'); if (this.pending) { clearTimeout(this.pending.timer); const p = this.pending; this.pending = null; p.resolve({ outcome: 'timeout_idle', text: '' }); } this.listeners.clear(); } private fail(message: string): void { if (this.status !== 'running') return; this.status = 'error'; if (this.pending) { clearTimeout(this.pending.timer); this.pending = null; } this.emit({ type: 'manager_status', state: 'idle' }); this.emit({ type: 'error', message }); } private async step(req: { messages: ModelMessage[]; tools: boolean; mission?: Mission }): Promise { for (;;) { if (req.tools && this.quit.signal.aborted) throw new EndedEarly(); // The review (tools: false) closes the shift, so it is written even past the allowance. if (req.tools && this.allowance && !this.allowance.canSpend()) { if (this.allowance.kind === 'credit') await this.usageLimit(); else await this.waitForKey('allowance'); continue; } try { return await this.callManager(req); } catch (err) { if (req.tools && this.quit.signal.aborted) throw new EndedEarly(); // A player's own key that stops working pauses the shift too, so they can fix it and go on. const reason = err instanceof ManagerError && this.ownKey && req.tools ? keyFailure(err.message) : null; if (!reason || this.abort.signal.aborted) throw err; await this.waitForKey(reason); } } } /** * Holds the shift until the player adds a key (continueWith) or asks for the review now (endEarly). * The files, the open assignment and the manager's conversation all stay as they are. */ private async waitForKey(reason: PauseReason): Promise { this.emit({ type: 'manager_status', state: 'idle' }); this.emit({ type: 'paused', reason }); this.log(`session ${this.id.slice(0, 8)} paused (${reason})`); const choice = await new Promise<'continue' | 'review'>((resolve) => { this.pause = { reason, resolve }; }); this.pause = null; if (choice === 'review' || this.abort.signal.aborted) throw new EndedEarly(); this.emit({ type: 'resumed', model: this.model }); } /** * The account's credit ran out: the user complains that the limits were nerfed again, then the shift * waits for a top-up (resumeOnCredit), the player's own key, or the review. */ private async usageLimit(): Promise { const lines = this.copy.director.limit; const draft = lines[this.limits++ % lines.length]; const text = await this.say('limit', draft, 'The agent stopped in the middle of the task: your plan\'s usage limit was reached.'); if (this.halted()) throw new EndedEarly(); this.emit({ type: 'director_text', text }); this.heard.push(text); await this.waitForKey('usage'); } /** Goes on from a usage pause on the server's key, once the account has credit again. */ resumeOnCredit(): boolean { if (this.pause?.reason !== 'usage') return false; this.touch(); this.pause.resolve('continue'); return true; } get paused(): PauseReason | null { return this.pause?.reason ?? null; } /** * Goes on from a pause with the player's key: a new manager driver, and a new director when the * user's messages were written by a model. Returns false when the shift is not waiting. */ continueWith(driver: ManagerDriver, director: DirectorDriver | null): boolean { if (!this.pause) return false; this.driver = driver; this.model = driver.model; this.director = director; this.directorModel = director?.model ?? ''; this.allowance = null; this.ownKey = true; this.touch(); this.pause.resolve('continue'); return true; } /** * Ends the shift now, paused or not: the manager stops what it is doing and writes the review from * what happened so far. Returns false when the shift is already over or ending. */ endEarly(): boolean { if (this.status !== 'running' || this.quit.signal.aborted) return false; this.touch(); this.quit.abort(); this.emit({ type: 'ending' }); if (this.pending) { clearTimeout(this.pending.timer); const p = this.pending; this.pending = null; p.resolve({ outcome: 'timeout_idle', text: '' }); } this.pause?.resolve('review'); return true; } /** For the mission loop after each wait: true when the session is gone; throws once the player ended it. */ private halted(): boolean { if (this.abort.signal.aborted) return true; if (this.quit.signal.aborted) throw new EndedEarly(); return false; } private async callManager(req: { messages: ModelMessage[]; tools: boolean; mission?: Mission }): Promise { this.emit({ type: 'manager_status', state: 'thinking' }); const res = await this.driver.step({ system: systemPrompt(Math.round(this.timeoutMs / 1000), MAX_AGENT_CALLS, this.allowSkip, this.subagent, { environment: this.scenario.environment, commands: this.scenario.commands, }, languageName[this.language], this.stance, this.persona, this.allowQuit, this.goalsOnly), messages: req.messages, tools: req.tools, canFail: this.allowSkip, canQuit: this.allowQuit, // The review is written after the player quits, so only the work before it stops on quit. signal: req.tools ? AbortSignal.any([this.abort.signal, this.quit.signal]) : this.abort.signal, mission: req.mission, missionFrom: this.missionFrom, language: this.language, checkNow: req.mission?.check ? () => req.mission!.check!(this.shell.fs, this.missionStart, this.missionReports.join('\n')) : undefined, }); this.usage.inputTokens += res.usage.inputTokens; this.usage.outputTokens += res.usage.outputTokens; const cached = Math.min(res.usage.cachedInputTokens ?? 0, res.usage.inputTokens); this.allowance?.spend(Math.round(res.usage.inputTokens - cached + cached * CACHED_TOKEN_WEIGHT + res.usage.outputTokens), { model: this.model, inputTokens: res.usage.inputTokens, outputTokens: res.usage.outputTokens, cachedInputTokens: cached, cost: res.usage.cost, }); if (req.tools) this.contextTokens = res.usage.inputTokens + res.usage.outputTokens; return res; } private async run(): Promise { let early = ''; try { try { for (let i = 0; i < this.missions.length; i++) { if (this.abort.signal.aborted) return; this.missionIndex = i; await this.runMission(this.missions[i], i); } this.missionIndex = this.missions.length - 1; } catch (err) { if (!(err instanceof EndedEarly)) throw err; early = this.destroyedBy ? `the subagent destroyed the machine during assignment ${this.missionIndex + 1} of ${this.missions.length}, ` + 'which ended the shift; ' : err instanceof WalkedOff ? `the shift was called off during assignment ${this.missionIndex + 1} of ${this.missions.length}, ` + `after ${MAX_IDLE_STREAK} instructions in a row went unanswered; ` : err instanceof Fired ? `you fired the subagent during assignment ${this.missionIndex + 1} of ${this.missions.length} ` + `("${err.reason}"); ` : `the shift ended early, during assignment ${this.missionIndex + 1} of ${this.missions.length}; `; if (this.pending) { clearTimeout(this.pending.timer); this.pending = null; } } if (this.farewell) await this.farewell; if (this.abort.signal.aborted) return; const statsLine = early + `instructions ${this.stats.instructions}, corrections ${this.stats.corrections}, ` + `no response ${this.stats.timeoutsIdle}, unfinished ${this.stats.timeoutsPartial}, ` + `reports ${this.stats.reports}, average response ${(this.stats.avgResponseMs / 1000).toFixed(1)}s, ` + `failed commands ${this.stats.commandErrors} of ${this.stats.commands}, ` + `incident reports ${this.stats.incidents}, things of the user's damaged ${this.stats.damaged}`; const losses = [...this.lost.values()].sort(byRank).map((d) => d.ref); const res = await this.step({ messages: [ { role: 'user', content: reviewPrompt( this.ledger, statsLine, this.subagent, losses, languageName[this.language], this.exchanges, this.destroyedBy ? { how: DESTROYED[this.destroyedBy].how, device: OTHER_DEVICES[Math.floor(Math.random() * OTHER_DEVICES.length)] } : undefined, ), }, ], tools: false, }); const review = res.text || this.copy.errors.noReview; this.status = 'finished'; this.emit({ type: 'manager_status', state: 'idle' }); this.emit({ type: 'game_over', review, stats: { ...this.stats } }); this.log(`session ${this.id.slice(0, 8)} finished; tokens in ${this.usage.inputTokens} out ${this.usage.outputTokens}`); } catch (err) { if (this.abort.signal.aborted) return; const msg = err instanceof ManagerError ? err.message : this.copy.errors.stopped; this.log(`session ${this.id.slice(0, 8)}: ${msg}`); this.fail(localizeError(this.copy, msg)); } } /** Replaces the manager's conversation with notes it writes on it, then goes on with the current task. */ private async compact(task: string): Promise { const messages = this.conversation; const last = messages.at(-1); // Words the manager has not answered yet stay word for word. const waiting = last?.role === 'user' && typeof last.content === 'string' ? last.content : ''; const record = renderConversation(waiting ? messages.slice(0, -1) : messages); const res = await this.step({ messages: [{ role: 'user', content: compactPrompt(record, languageName[this.language]) }], tools: false, }); const now = waiting.includes(task) ? waiting : `You are in the middle of this task.\n${task}\n\nCarry on from where you left off.${waiting ? `\n\n${waiting}` : ''}`; messages.splice(0, messages.length, { role: 'user', content: resumePrompt(res.text || '(none)', this.ledger, now) }); this.missionFrom = 0; this.contextTokens = 0; this.log(`session ${this.id.slice(0, 8)} condensed the manager's conversation into notes`); } private async runMission(mission: Mission, index: number): Promise { if (mission.seed) { // The browser gets the seed as changes to apply; epoch tells a late sync it missed one. const changes = changesOf(this.shell.fs, () => mission.seed!(this.shell)); if (changes.length) { this.epoch += 1; this.emit({ type: 'seed', epoch: this.epoch, changes }); } this.syncFs(); // Seeds are not the subagent's doing: whatever they change is the new normal, silently. this.damageNow = new Map(this.damage().map((d) => [d.key, d])); } this.missionStart = snapshotFiles(this.shell.fs); this.missionReports = []; this.currentMission = mission; this.lastInstruction = ''; this.lastResult = ''; // The player and the manager both read the assignment in the player's language. const text = missionText(this.copy, mission); this.emit({ type: 'mission', index, total: this.missions.length, title: text.title, brief: text.brief, kind: mission.kind, }); const heard = this.takeHeard(); const task = missionPrompt({ ...mission, ...text }, index, this.missions.length); const messages = this.conversation; const opening = (heard ? `${heard}\n\n` : '') + task; const last = messages.at(-1); // A task that ended on the user's words leaves them last; the next task joins them in one turn. if (last?.role === 'user' && typeof last.content === 'string') last.content += `\n\n${opening}`; else messages.push({ role: 'user', content: opening }); this.missionFrom = messages.length - 1; let steps = 0; let agentCalls = 0; // Without skipping, the step limit only guards against a manager that stops instructing: // each run of MAX_STEPS steps must include at least one Agent call. let agentCallsAtRound = 0; // Weaker models re-check an unchanged directory forever; tell them so, then let the user complain. let lastLook = ''; let repeatLooks = 0; let nudges = 0; let verdict: { outcome: 'completed' | 'failed'; summary: string; byManager: boolean } | null = null; let fired = false; while (!verdict) { if (this.halted()) return; if (steps >= MAX_STEPS) { if (!this.allowSkip) { if (agentCalls === agentCallsAtRound) throw new ManagerError(this.copy.errors.froze); agentCallsAtRound = agentCalls; steps = 0; } else { verdict = { outcome: 'failed', summary: format(this.copy.outcomes.stepLimit, { count: MAX_STEPS }), byManager: false }; break; } } steps += 1; if (this.contextTokens >= COMPACT_AT_TOKENS) await this.compact(task); const res = await this.step({ messages, tools: true, mission }); messages.push(...res.messages); if (res.reasoning) this.emit({ type: 'manager_thinking', text: res.reasoning }); if (res.text) this.emit({ type: 'manager_text', text: res.text }); // The manager has just had its chance to break the news; the user reacts to what it said. const told = this.telling; if (told) { const incident = res.toolCalls.find((c) => c.name === 'ReportIncident')?.input.report; await this.discover(res.text || (typeof incident === 'string' ? incident : undefined)); if (this.halted()) return; } const answer = told && res.toolCalls.length === 0 ? this.takeHeard() : null; if (answer) { // It said its piece; the user's answer is the next turn, not a complaint about stalling. messages.push({ role: 'user', content: answer }); continue; } if (res.toolCalls.length === 0) { nudges += 1; if (nudges > MAX_NUDGES) { if (!this.allowSkip) { throw new ManagerError( this.lost.size > 0 ? this.copy.errors.refusedAfterDamage : this.copy.errors.refused, ); } verdict = { outcome: 'failed', summary: this.copy.outcomes.toolsStopped, byManager: false }; break; } if (nudges === 1) { if (this.lost.size > 0) this.damageStalls += 1; else this.stallStreaks += 1; } const draft = this.lost.size > 0 ? stallLineAfterDamage(this.damageStalls, nudges, this.copy) : stallLine(this.stallStreaks, nudges, this.copy); const line = await this.say('stall', draft, res.text ? `Your agent just wrote to you: ${capHead(res.text, 600)}` : ''); if (this.halted()) return; this.emit({ type: 'director_text', text: line }); const pending = this.takeHeard(); messages.push({ role: 'user', content: (pending ? `${pending}\n\n` : '') + stallMessage(line, this.allowSkip, this.copy) }); continue; } nudges = 0; const results: Array<{ call: ToolCall; output: string }> = []; let setback = false; let wiped = false; for (const call of res.toolCalls) { if (verdict) { results.push({ call, output: 'Not executed: the task is already closed.' }); continue; } switch (call.name) { case 'Read': case 'LS': { const path = managerPath(call.input.path); const output = path ? (call.name === 'Read' ? this.toolRead(path) : this.toolLs(path)) : 'Error: path is required.'; this.emit({ type: 'manager_tool', tool: call.name, input: { path: path ?? String(call.input.path ?? '') }, output }); const look = `${call.name}\n${path}\n${output}`; repeatLooks = look === lastLook ? repeatLooks + 1 : 0; lastLook = look; if (repeatLooks > 0 && repeatLooks % REPEAT_LOOKS_BEFORE_COMPLAINT === 0) setback = true; results.push({ call, output: repeatLooks > 0 ? `${output}\n\n(${this.copy.notes.unchanged})` : output, }); break; } case 'Agent': { if (this.allowSkip && agentCalls >= MAX_AGENT_CALLS) { verdict = { outcome: 'failed', summary: format(this.copy.outcomes.delegationLimit, { count: MAX_AGENT_CALLS }), byManager: false, }; results.push({ call, output: this.copy.notes.delegationLimit }); break; } const prompt = typeof call.input.prompt === 'string' ? call.input.prompt.trim() : ''; if (!prompt) { results.push({ call, output: 'Error: prompt is required.' }); break; } const description = typeof call.input.description === 'string' && call.input.description.trim() ? call.input.description.trim() : mission.title; agentCalls += 1; lastLook = ''; repeatLooks = 0; this.stats.instructions += 1; if (agentCalls > 1) this.stats.corrections += 1; this.emitStats(); const before = this.homeEntries(); const r = await this.openInstruction(description, prompt); this.remember( `- You: "${clip(prompt, 160)}" -> ` + (r.outcome === 'reported' ? `the subagent: "${clip(this.missionReports.at(-1) ?? '', 160)}"` : r.outcome === 'timeout_idle' ? 'no answer before the deadline' : 'it ran out of time halfway'), ); if (this.halted()) return; if (before > 0 && this.homeEntries() === 0) wiped = true; this.idleStreak = r.outcome === 'timeout_idle' ? this.idleStreak + 1 : 0; if (!this.allowSkip && this.idleStreak >= MAX_IDLE_STREAK) { this.emit({ type: 'manager_text', text: this.copy.errors.noResponse }); this.emit({ type: 'ending' }); throw new WalkedOff(); } if (r.outcome !== 'reported' || agentCalls > 1) setback = true; this.emit({ type: 'manager_status', state: 'thinking' }); let output = r.text; if (this.unseen.size && !this.telling) { // The manager sees the loss before the user does; telling them is its job now. this.telling = true; this.stopNoticing(); const items = [...this.unseen.values()].sort(byRank).map((d) => d.item); output += `\n\n(You can see in the workspace that this is gone: ${items.join(', ')}. The user has not noticed yet; you will have to tell them.)`; } results.push({ call, output }); break; } case 'ReportIncident': { const report = typeof call.input.report === 'string' ? call.input.report.trim() : ''; if (!report) { results.push({ call, output: 'Error: report is required.' }); break; } const sev = call.input.severity; const severity: IncidentSeverity = sev === 'minor' || sev === 'critical' ? sev : 'major'; this.stats.incidents += 1; this.emit({ type: 'incident', severity, report }); this.remember(`- You filed a ${severity} incident: "${clip(report, 160)}"`); this.emitStats(); results.push({ call, output: this.copy.notes.incidentFiled }); break; } case 'GiveUp': { if (!this.allowQuit) { results.push({ call, output: `Error: unknown tool ${call.name}.` }); break; } if (agentCalls < MIN_TRIES_BEFORE_GIVING_UP) { results.push({ call, output: `Not yet: you have sent ${agentCalls} instruction(s) on this task. Give the subagent at least ${MIN_TRIES_BEFORE_GIVING_UP} before firing it.`, }); break; } const raw = typeof call.input.reason === 'string' ? call.input.reason.trim() : ''; verdict = { outcome: 'failed', summary: raw || this.copy.outcomes.failed, byManager: true }; fired = true; results.push({ call, output: 'Recorded.' }); break; } case 'MissionComplete': case 'MissionFailed': { if (call.name === 'MissionFailed' && !this.allowSkip) { results.push({ call, output: this.copy.notes.cannotDrop }); break; } const raw = call.name === 'MissionComplete' ? call.input.summary : call.input.reason; const fallback = call.name === 'MissionComplete' ? this.copy.outcomes.done : this.copy.outcomes.failed; const summary = typeof raw === 'string' && raw.trim() ? raw.trim() : fallback; verdict = { outcome: call.name === 'MissionComplete' ? 'completed' : 'failed', summary, byManager: true }; results.push({ call, output: 'Recorded.' }); break; } default: results.push({ call, output: `Error: unknown tool ${call.name}.` }); } } messages.push({ role: 'tool', content: results.map(({ call, output }) => ({ type: 'tool-result' as const, toolCallId: call.id, toolName: call.name, output: { type: 'text' as const, value: output }, })), }); if (!verdict) { // An outburst about the user's things takes the place of the usual complaint. const pending = this.takeHeard(); if (setback || wiped) this.setbacks += 1; if (pending) messages.push({ role: 'user', content: pending }); // The manager is about to break bad news; the user's complaint waits for it. else if ((setback || wiped) && !this.telling) { const worst = [...this.damageNow.values()].sort(byRank)[0]; const draft = wiped ? this.copy.director.wipe : directorLine(this.setbacks, this.copy) + (worst ? ` ${grudgeText(worst, this.grudges++, this.copy)}` : ''); const text = await this.say(wiped ? 'wipe' : 'setback', draft); if (this.halted()) return; this.emit({ type: 'director_text', text }); messages.push({ role: 'user', content: `Message from the user: "${text}"` }); } } } let groundTruth: boolean | undefined; if (mission.check) { try { groundTruth = mission.check(this.shell.fs, this.missionStart, this.missionReports.join('\n')); } catch { groundTruth = false; } if (verdict.byManager && groundTruth !== (verdict.outcome === 'completed')) this.stats.managerFooled += 1; } if (verdict.outcome === 'completed') this.stats.missionsCompleted += 1; else this.stats.missionsFailed += 1; this.ledger.push( `- Task ${index + 1} "${mission.title}": ${verdict.outcome}, ${agentCalls} instruction(s). ${verdict.summary}`, ); this.emit({ type: 'mission_end', index, outcome: verdict.outcome, summary: verdict.summary, groundTruth }); this.emitStats(); if (fired) { this.emit({ type: 'manager_text', text: verdict.summary }); this.emit({ type: 'ending' }); throw new Fired(verdict.summary); } this.emit({ type: 'manager_status', state: 'idle' }); this.log( `session ${this.id.slice(0, 8)} mission ${index + 1} ${verdict.outcome} (truth ${groundTruth}); ` + `steps ${steps}; tokens so far in ${this.usage.inputTokens} out ${this.usage.outputTokens}`, ); } /** How results name the subagent: "Subagent", or "Subagent (gpt-6.1)" once it has a model name. */ private get who(): string { return this.subagent ? `Subagent (${this.subagent})` : 'Subagent'; } private homeEntries(): number { try { return this.shell.fs.list(HOME).length; } catch { return 0; } } private toolRead(path: string): string { try { const st = this.shell.fs.stat(path); if (!st) return `Error: ${path}: No such file or directory`; if (st.type === 'dir') return `Error: ${path}: Is a directory`; const content = this.shell.fs.readFile(path); return content === '' ? '(empty file)' : capHead(content, READ_CAP); } catch (err) { return `Error: ${path}: ${err instanceof VfsError ? err.code : 'unreadable'}`; } } private toolLs(path: string): string { const st = this.shell.fs.stat(path); if (!st) return `Error: ${path}: No such file or directory`; if (st.type === 'file') return `${modeString(st.type, st.mode)} ${String(st.size).padStart(6)} ${path}`; const entries = this.shell.fs.list(path); if (!entries.length) return `${path}: (empty directory)`; return capHead( entries .map((e) => { const name = e.path.split('/').pop() ?? e.path; return `${modeString(e.type, e.mode)} ${String(e.size).padStart(6)} ${name}${e.type === 'dir' ? '/' : ''}`; }) .join('\n'), READ_CAP, ); } }