Report the source position of every suspension

Merged
SA
2mo ago
+1532-140

Every suspension now carries position, a SourceRange (filename plus UTF-8 byte offsets start/end) locating the suspending call, name lookup or await, across monty, the pool, proto, WIT and the Python and JS bindings.

I made what might look like an odd decision to return byte offsets, not line and column for three reason:

  1. It should make everything more efficient - less to store, less to calculate, less to send over the wire.
  2. if we have the source we can calculate the line and column on demand (rather than paying the price every time), if we don’t have the code, knowing the line and column isn’t very useful
  3. this scenario is different to application code - in application code systems (e.g. o11y) should know the file position, but not have the code. that’s not really as true for sandbox code. So it’s less clear we should use the same otel attributes as normal code, also we could calculate and cache line/column in telemetry later if we need to

Also fixes instruction_ip being left on the next instruction after a builtin runs Python synchronously (e.g. time.sleep(Index())).


Summary by cubic

Every suspension now carries position, a SourceRange (filename plus UTF-8 start/end byte offsets, end exclusive) locating the suspending expression: the function or OS call, the name lookup, or the await the main task is blocked on. The range flows through monty, monty-pool, proto, WIT, and the Python and JS snapshot APIs.

  • Positions come from the existing location table, so a suspension copies its range instead of rescanning or re-resolving source. filename follows traceback naming (<python-input-N> per feed, <string> inside eval/exec) and is capped at 256 bytes on a character boundary, so a compromised worker cannot force large copies; hosts slice the encoded source. A builtin that runs Python synchronously (for example time.sleep(Index()) calling __index__) restores the outer instruction pointer on return, so the following suspension reports the outer instruction's position. Binding tests put a multi-byte character before the suspending expression, catching a binding that counts characters instead of bytes.
  • Suspension spans carry the position under the sandbox.code.* namespace; metrics omit positions because they are high-cardinality. The run span reports sandbox.execution.code.attribute and sandbox.execution.language; host code.* location attributes stay out of print records.
  • A cancelled or unavailable main task reports SourceRange::unknown() instead of panicking.

Migration

  • PROTOCOL_VERSION remains 5 because position is additive; missing positions decode as an unknown range.
  • DUMP_VERSION is now 13 because positions are part of suspended state.