@@ -32,6 +32,7 @@ export class NotFoundError extends Schema.TaggedError<NotFoundError>()("Shell.No
3232const EXITED_LIMIT = 25
3333export const RETENTION = Duration . days ( 7 )
3434export const DIRECTORY = "shell"
35+ const SPAN_COMMAND_LIMIT = 200
3536
3637type Info = Shell . Info
3738type CreateInput = Shell . CreateInput & {
@@ -182,7 +183,7 @@ const layer = () =>
182183 } )
183184
184185 const remove = Effect . fn ( "Shell.remove" ) ( function * ( id : Shell . ID ) {
185- yield * require ( id )
186+ yield * annotate ( ( yield * require ( id ) ) . info )
186187 yield * removeCommand ( id )
187188 } )
188189
@@ -193,15 +194,22 @@ const layer = () =>
193194 } )
194195
195196 const get = Effect . fn ( "Shell.get" ) ( function * ( id : Shell . ID ) {
196- return ( yield * require ( id ) ) . info
197+ const { info } = yield * require ( id )
198+ yield * annotate ( info )
199+ return info
197200 } )
198201
199202 const wait = Effect . fn ( "Shell.wait" ) ( function * ( id : Shell . ID ) {
200- return yield * Deferred . await ( ( yield * require ( id ) ) . done )
203+ const command = yield * require ( id )
204+ yield * annotate ( command . info )
205+ const info = yield * Deferred . await ( command . done )
206+ yield * annotate ( info )
207+ return info
201208 } )
202209
203210 const timeout = Effect . fn ( "Shell.timeout" ) ( function * ( id : Shell . ID , duration : number ) {
204211 const command = yield * require ( id )
212+ yield * annotate ( command . info )
205213 if ( command . info . status !== "running" || ! command . timeout ) return command . info
206214 yield * command . timeout ( duration )
207215 return command . info
@@ -242,11 +250,13 @@ const layer = () =>
242250 } )
243251
244252 const result = Effect . fn ( "Shell.result" ) ( function * ( started : Shell . Info ) {
253+ yield * annotate ( started )
245254 const info = yield * wait ( started . id ) . pipe (
246255 Effect . catchTag ( "Shell.NotFoundError" , ( ) =>
247256 Effect . succeed ( { ...started , status : "killed" as const , time : { ...started . time , completed : Date . now ( ) } } ) ,
248257 ) ,
249258 )
259+ yield * annotate ( info )
250260 const capture = yield * Effect . gen ( function * ( ) {
251261 const limits = Config . latest ( yield * config . entries ( ) , "tool_output" )
252262 const maxLines = limits ?. max_lines ?? ToolOutput . MAX_LINES
@@ -322,6 +332,7 @@ const layer = () =>
322332 metadata : input . metadata ?? { } ,
323333 time : { started : Date . now ( ) } ,
324334 }
335+ yield * annotate ( info )
325336
326337 // Spawn through the Environment and stream combined output to the file. The handle is scope-bound, so
327338 // the managing fiber keeps its scope open until the command terminates (it awaits `done` at the
@@ -486,6 +497,26 @@ const layer = () =>
486497 } ) ,
487498 )
488499
500+ // Spans carry only the command text, truncated; never env, stdin or output. Shells outlive the turn that started
501+ // them, so the session comes from the shell's own metadata rather than the caller's span annotations.
502+ function annotate ( info : Info ) {
503+ const sessionID = info . metadata . sessionID
504+ return Effect . annotateCurrentSpan ( {
505+ "opencode.shell.id" : info . id ,
506+ "opencode.shell.command" : spanCommand ( info . command ) ,
507+ ...( typeof sessionID === "string" ? { "session.id" : sessionID } : { } ) ,
508+ ...( info . metadata . background === true ? { "opencode.shell.background" : true } : { } ) ,
509+ ...( info . status === "running" ? { } : { "opencode.shell.status" : info . status } ) ,
510+ ...( info . exit === undefined ? { } : { "process.exit.code" : info . exit } ) ,
511+ } )
512+ }
513+
514+ // Strict OTLP JSON parsers reject a lone surrogate, which a cut through an emoji would leave.
515+ function spanCommand ( command : string ) {
516+ if ( command . length <= SPAN_COMMAND_LIMIT ) return command . toWellFormed ( )
517+ return `${ command . slice ( 0 , SPAN_COMMAND_LIMIT ) . toWellFormed ( ) } …`
518+ }
519+
489520// Moves past UTF-8 continuation bytes (at most 3) so a byte cut never splits a character.
490521function charBoundary ( bytes : Buffer , offset : number ) {
491522 const limit = Math . min ( bytes . length , offset + 3 )
0 commit comments