@@ -37,7 +37,7 @@ import { refreshCachedBalance } from "./ai-gateway-billing/cloudflare/connection
3737import { SharingManager , SharingCaller , CollaboratorRecord , ShareKeyRecord } from "./sharing" ;
3838import { AutoApprovalDrainer } from "./auto-approval" ;
3939import { collectSlashCommands , invokeSlashCommand } from "./slash-commands" ;
40- import { createWorkshopLogger , obsContext } from "./observability" ;
40+ import { createWorkshopLogger , obsContext , traced } from "./observability" ;
4141import type { ChatGatewayRpcTarget , SubmitExternalMessageResult } from "@gadgets/workshop-shared/external-message-gateway" ;
4242import {
4343 assertChatAttachmentSupportedByProvider ,
@@ -1376,6 +1376,7 @@ class OverseerImpl implements AgentHooks {
13761376
13771377 // Run the whole migration in one transaction so that a mid-migration error can't leave the
13781378 // workspace half-migrated.
1379+ let startedAt = Date . now ( ) ;
13791380 this . ctx . storage . transactionSync ( ( ) => {
13801381 // Version 0 -> 1: the workspace predates multi-gadget support. If it has any gadget content
13811382 // (code beyond the initial empty snapshot, or named bindings), register that content as the
@@ -1455,6 +1456,10 @@ class OverseerImpl implements AgentHooks {
14551456
14561457 this . storage . version . put ( 1 ) ;
14571458 } ) ;
1459+
1460+ this . logger . info ( "migrated workspace storage" , {
1461+ event : "storage.migration.completed" , durationMs : Date . now ( ) - startedAt ,
1462+ } ) ;
14581463 }
14591464
14601465 // Allocate a workpiece ID from the shared counter. (The counter is named `nextGatekeeperId`
@@ -2003,18 +2008,24 @@ class OverseerImpl implements AgentHooks {
20032008 this . #snapshotMetrics. logSize += update . length ;
20042009 if ( this . #snapshotMetrics. logSize >
20052010 Math . max ( this . #snapshotMetrics. snapshotSize , MIN_SNAPSHOT_THRESHOLD ) ) {
2006- let { ydoc} = this . buildYDoc ( "current" ) ;
2007- let snapshotUpdate = Y . encodeStateAsUpdateV2 ( ydoc ) ;
2008- this . storage . snapshots . put ( {
2009- version,
2010- timestamp,
2011- update : snapshotUpdate
2011+ let logBytes = this . #snapshotMetrics. logSize ;
2012+ let startedAt = Date . now ( ) ;
2013+ traced ( "code.snapshot.rebuild" , ( span ) => {
2014+ let { ydoc} = this . buildYDoc ( "current" ) ;
2015+ let snapshotUpdate = Y . encodeStateAsUpdateV2 ( ydoc ) ;
2016+ this . storage . snapshots . put ( { version, timestamp, update : snapshotUpdate } ) ;
2017+ span . setAttribute ( "gadgetId" , this . ctx . id . toString ( ) ) ;
2018+ span . setAttribute ( "size" , snapshotUpdate . length ) ;
2019+ span . setAttribute ( "logBytes" , logBytes ) ;
2020+ this . #snapshotMetrics = {
2021+ snapshotSize : snapshotUpdate . length ,
2022+ logSize : 0 ,
2023+ } ;
2024+ this . logger . info ( "rebuilt code snapshot" , {
2025+ event : "code.snapshot.rebuilt" , durationMs : Date . now ( ) - startedAt ,
2026+ size : snapshotUpdate . length , logBytes, sequence : version ,
2027+ } ) ;
20122028 } ) ;
2013-
2014- this . #snapshotMetrics = {
2015- snapshotSize : snapshotUpdate . length ,
2016- logSize : 0 ,
2017- } ;
20182029 }
20192030 }
20202031
@@ -3857,8 +3868,8 @@ class OverseerImpl implements AgentHooks {
38573868 gadgetId : this . ctx . id . toString ( ) ,
38583869 chatId,
38593870 modelId : aiModel . profile . id ,
3860- } , ( ) => this . #runAgentTurnWithContext(
3861- chatId , aiModel , initiator , callbackInitiated , liveChat ) ) ;
3871+ } , ( ) => traced ( "agent.run" , ( ) => this . #runAgentTurnWithContext(
3872+ chatId , aiModel , initiator , callbackInitiated , liveChat ) ) ) ;
38623873 }
38633874
38643875 async #runAgentTurnWithContext( chatId : number , aiModel : UserAiModelRecord ,
@@ -7311,6 +7322,7 @@ class OverseerClientInterface extends RpcTarget implements Overseer {
73117322 if ( ! this . isOwner ) {
73127323 throw new Error ( "Only the workspace owner can delete it." ) ;
73137324 }
7325+ let startedAt = Date . now ( ) ;
73147326
73157327 this . impl . recordGadgetAnalytics ( {
73167328 event_name : "gadget_deleted" ,
@@ -7337,6 +7349,10 @@ class OverseerClientInterface extends RpcTarget implements Overseer {
73377349 this . impl . scheduleRevocationRestart ( ) ;
73387350 this . impl . ownerId = undefined ;
73397351 } ) ;
7352+
7353+ this . impl . logger . info ( "deleted workspace" , {
7354+ event : "workspace.delete.completed" , durationMs : Date . now ( ) - startedAt ,
7355+ } ) ;
73407356 }
73417357
73427358 async subscribeToCode ( subscriber : RpcStub < CodeSubscriber > , fromVersion : number = 0 )
@@ -8390,6 +8406,7 @@ class OverseerClientInterface extends RpcTarget implements Overseer {
83908406 }
83918407
83928408 async deleteChat ( chatId : number ) : Promise < void > {
8409+ let startedAt = Date . now ( ) ;
83938410 let response = this . impl . storage . gadgetResponseDeliveries . undeliveredByChatId . get ( chatId ) ;
83948411 if ( response ?. status === "waiting" ) {
83958412 this . impl . deliverExternalMessageResponse ( response , "The chat was deleted before the agent responded." ) ;
@@ -8459,6 +8476,10 @@ class OverseerClientInterface extends RpcTarget implements Overseer {
84598476
84608477 // Clean up all in-memory live state for this chat.
84618478 this . impl . destroyLiveChat ( chatId ) ;
8479+
8480+ this . impl . logger . info ( "deleted chat" , {
8481+ event : "chat.delete.completed" , chatId, durationMs : Date . now ( ) - startedAt ,
8482+ } ) ;
84628483 }
84638484
84648485 async stopAgent ( chatId : number ) : Promise < void > {
0 commit comments