@@ -6,6 +6,7 @@ import type { ComputerFile, ComputerFiles, FileReader, ToolResult, ToolSpec } fr
66import { demonstrationReplySchema , type DemonstrationCommand } from "@openwork/types/browser-demonstration"
77import { Desktop , type DesktopView } from "./desktop.js"
88import { DESKTOP_TOOL } from "./desktop-tool.js"
9+ import { DesktopTiming , timedProvider } from "./desktop-timing.js"
910
1011/** One provider-neutral Linux computer per conversation. Jobs own idle policy and files; adapters own transport. */
1112export type ComputerOptions = {
@@ -54,6 +55,8 @@ export class Computers {
5455 private readonly pausing = new Map < string , Promise < void > > ( )
5556 /** Saved file ids already copied into each session's VM by this process. */
5657 private readonly copied = new Map < string , Set < string > > ( )
58+ /** The desktop request being timed for each VM, by its slug. */
59+ private readonly timings = new Map < string , DesktopTiming > ( )
5760
5861 private readonly snapshot : string
5962 private readonly files ?: ComputerFiles
@@ -71,7 +74,7 @@ export class Computers {
7174 * `readFile` turns a file into what the model can see (images, PDFs, text), the same way tool results are read.
7275 */
7376 constructor ( private readonly options : ComputerOptions , deps : { files ?: ComputerFiles ; readFile : FileReader } ) {
74- this . provider = options . provider
77+ this . provider = timedProvider ( options . provider , key => this . timings . get ( key ) )
7578 this . blocks = computerBlocks ( this . provider )
7679 this . snapshot = this . provider . currentImage ( ) ?. id ?? "configured image"
7780 this . files = deps . files
@@ -86,14 +89,17 @@ export class Computers {
8689 const desktop = this . desktop
8790 if ( ! desktop ) throw new Error ( "Desktop access is not configured." )
8891 signal ?. throwIfAborted ( )
89- const box = await this . vm ( sessionId )
92+ const timing = this . track ( sessionId , "tool" )
93+ const box = await this . vm ( sessionId ) . catch ( ( error : unknown ) => { this . finish ( timing , "failed" ) ; throw error } )
9094 let stopping : Promise < unknown > | undefined
9195 const stop = ( ) => {
9296 stopping ??= desktop . call ( box , "/stop" , { } )
9397 void stopping . catch ( ( ) => { } )
9498 }
9599 try {
96- await desktop . start ( box )
100+ await desktop . start ( box ) . catch ( ( error : unknown ) => { this . finish ( timing , "failed" ) ; throw error } )
101+ // A tool call that found the desktop ready is not worth a log line; one that woke or started it is.
102+ this . finish ( timing , "ok" , ! timing . prepared )
97103 signal ?. addEventListener ( "abort" , stop , { once : true } )
98104 if ( signal ?. aborted ) { stop ( ) ; signal . throwIfAborted ( ) }
99105 const program = parsed . operation === "computer_run"
@@ -118,12 +124,22 @@ export class Computers {
118124
119125 async desktopViewer ( sessionId : string , input : { view : DesktopView ; control : boolean ; origin : string } ) {
120126 if ( ! this . desktop ) throw new Error ( "Desktop access is not configured." )
121- const pending = this . vms . get ( sessionId )
122- const existing = pending ? await this . provider . inspect ( await pending ) : await this . provider . find ( { idempotencyKey : this . slug ( sessionId ) , labels : this . spec ( sessionId ) . labels } )
123- if ( existing ?. state === "stopped" || existing ?. state === "stopping" ) return { state : "paused" as const }
124- const box = await this . vm ( sessionId )
125- try { return { state : "connected" as const , ...await this . desktop . viewer ( box , input ) } }
126- finally { this . release ( sessionId ) }
127+ const timing = this . track ( sessionId , "viewer" )
128+ let outcome = "failed"
129+ try {
130+ const pending = this . vms . get ( sessionId )
131+ const existing = pending ? await this . provider . inspect ( await pending ) : await this . provider . find ( { idempotencyKey : this . slug ( sessionId ) , labels : this . spec ( sessionId ) . labels } )
132+ if ( existing ?. state === "stopped" || existing ?. state === "stopping" ) {
133+ outcome = "paused"
134+ return { state : "paused" as const }
135+ }
136+ const box = await this . vm ( sessionId )
137+ try {
138+ const viewer = { state : "connected" as const , ...await this . desktop . viewer ( box , input ) }
139+ outcome = "ok"
140+ return viewer
141+ } finally { this . release ( sessionId ) }
142+ } finally { this . finish ( timing , outcome ) }
127143 }
128144
129145 async browserDemonstration ( sessionId : string , command : DemonstrationCommand ) {
@@ -141,6 +157,16 @@ export class Computers {
141157 }
142158
143159 async desktopAction ( sessionId : string , action : "continue" | "pause" | "resume" | "rebuild" ) {
160+ const timing = this . track ( sessionId , action )
161+ let outcome = "failed"
162+ try {
163+ const result = await this . changeDesktop ( sessionId , action )
164+ outcome = "ok"
165+ return result
166+ } finally { this . finish ( timing , outcome ) }
167+ }
168+
169+ private async changeDesktop ( sessionId : string , action : "continue" | "pause" | "resume" | "rebuild" ) {
144170 if ( ! this . desktop ) throw new Error ( "Desktop access is not configured." )
145171 if ( action === "rebuild" ) {
146172 // A fresh VM from the configured snapshot, without waking the old one first. Kept Files survive; the VM's own disk does not.
@@ -284,6 +310,20 @@ export class Computers {
284310 this . pauseTimers . delete ( sessionId )
285311 }
286312
313+ /** Times one desktop request; the first one open for a VM gets its steps, a concurrent one only its total. */
314+ private track ( sessionId : string , operation : string ) {
315+ const timing = new DesktopTiming ( sessionId , operation )
316+ const slug = this . slug ( sessionId )
317+ if ( ! this . timings . has ( slug ) ) this . timings . set ( slug , timing )
318+ return timing
319+ }
320+
321+ private finish ( timing : DesktopTiming , outcome : string , quiet = false ) {
322+ const slug = this . slug ( timing . sessionId )
323+ if ( this . timings . get ( slug ) === timing ) this . timings . delete ( slug )
324+ if ( ! quiet ) timing . log ( outcome )
325+ }
326+
287327 /** Deletes the conversation's VM, if it has one. */
288328 async delete ( sessionId : string ) {
289329 this . cancelPause ( sessionId )
0 commit comments