fix(openshell): a sandbox that fails to start reports its last log lines (#1864)

This commit is contained in:
sam committed 2026-11-14 21:59:42 +11:00
1 parent a262151cb9
commit 411632548c
1 file changed
+27 -1
+27 -1
View File
@@ -165,8 +165,11 @@ func (r *Runtime) Acquire(ctx context.Context, l Launch) (*Sandbox, error) {
return nil, fmt.Errorf("create sandbox %s: %w", name, err)
}
if _, err := r.client.Sandboxes().WaitReady(ctx, r.workspace, name); err != nil {
// The sandbox's own log is the only record of why it failed, and
// removing the sandbox discards it.
tail := r.logTail(ctx, name)
r.remove(ctx, name)
return nil, fmt.Errorf("sandbox %s not ready: %w", name, err)
return nil, fmt.Errorf("sandbox %s not ready: %w%s", name, err, tail)
}
watchCtx, stop := context.WithCancel(context.WithoutCancel(ctx))
s := &Sandbox{name: name, exited: make(chan struct{}), stop: stop, done: make(chan struct{})}
@@ -175,6 +178,29 @@ func (r *Runtime) Acquire(ctx context.Context, l Launch) (*Sandbox, error) {
return s, nil
}
// logTail returns the sandbox's last log lines, prefixed for appending to an
// error, or "" when there are none or they cannot be read.
func (r *Runtime) logTail(ctx context.Context, name string) string {
res, err := r.client.Sandboxes().GetLogs(ctx, r.workspace, name, v1.WithLogLines(15))
if err != nil {
r.log.WarnContext(ctx, "read failed sandbox log", "sandbox", name, "err", err)
return ""
}
var b strings.Builder
for _, l := range res.Lines {
b.WriteString("\n")
b.WriteString(l.Source)
b.WriteString(" ")
b.WriteString(l.Level)
b.WriteString(" ")
b.WriteString(l.Message)
}
if b.Len() == 0 {
return ""
}
return "\nsandbox log:" + b.String()
}
// sandboxName is a short random name: the gateway rejects names much longer
// than 19 characters, and the instance is carried by the sandbox's label.
func sandboxName() (string, error) {