fix(sprites): DestroySandbox passes terminal StopReason so cleanup actually runs

shouldRunExecutorCleanup gates Sprites teardown on a small set of
terminal reasons (task/session archived/deleted). DestroySandbox was
calling StopInstance with an empty StopReason, which never matched
that set — so the sandbox was never actually destroyed, only the
local execution state was cleaned up. The sprite kept running
upstream until it idled out.

Pass StopReasonTaskDeleted explicitly. While here, lift the four
terminal-reason literals into named constants so callers don't have
to keep them in sync with shouldRunExecutorCleanup's switch.
This commit is contained in:
José Almeida
2026-05-14 00:05:16 +01:00
parent 107d8ba87e
commit 0c486cedde
2 changed files with 18 additions and 1 deletions
@@ -129,9 +129,20 @@ func (r *SpritesExecutor) runTerminalCleanupScript(ctx context.Context, sprite *
zap.String("reason", instance.StopReason))
}
// Terminal stop reasons that trigger destructive executor cleanup
// (sandbox teardown, container removal, per-instance session-dir removal).
// Anything outside this set is treated as a "preserve" stop — see
// shouldRunExecutorCleanup.
const (
StopReasonTaskArchived = "task archived"
StopReasonTaskDeleted = "task deleted"
StopReasonSessionArchived = "session archived"
StopReasonSessionDeleted = "session deleted"
)
func shouldRunExecutorCleanup(reason string) bool {
switch strings.ToLower(strings.TrimSpace(reason)) {
case "task archived", "task deleted", "session archived", "session deleted":
case StopReasonTaskArchived, StopReasonTaskDeleted, StopReasonSessionArchived, StopReasonSessionDeleted:
return true
default:
return false
@@ -110,9 +110,15 @@ func (m *Manager) DestroySandbox(ctx context.Context, sandboxID, executionID str
if !ok {
return fmt.Errorf("sprites backend has unexpected type %T", backend)
}
// StopInstance only runs sandbox cleanup when StopReason matches a
// terminal reason (see shouldRunExecutorCleanup). DestroySandbox IS the
// destruction path, so set StopReasonTaskDeleted explicitly — the
// empty StopReason this previously sent was silently treated as a
// preserve-on-stop signal and the sandbox was never destroyed.
instance := &ExecutorInstance{
InstanceID: executionID,
Metadata: map[string]interface{}{"sprite_name": sandboxID},
StopReason: StopReasonTaskDeleted,
}
return spritesExec.StopInstance(ctx, instance, true)
}