Files
coder/coderd/agentapi/scripts.go
T
Bobby Ho 764e67bcff feat: expose skipped and canceled script outcomes in coderd and the Build Timeline (#30875)
feat: expose skipped and canceled script outcomes

Add `skipped` and `canceled` statuses across the Agent API, database,
SDK, and Build Timeline for startup scripts.

- Store and return both outcomes, preserving zero-duration timings,
  and reject unknown statuses. Rollbacks rewrite the new outcomes to
  `exit_failure` with exit code 255.
- Add typed legends, outcome-specific tooltips, and keyboard-accessible
  bars. Fix timeline crashes for `timed_out` and `pipes_left_open`.
- Bump the Agent API to v2.15 while agents continue using v2.14.
  Add API, migration, and Storybook coverage.

First of two PRs for PLAT-550. Agents do not emit the new statuses yet;
a follow-up after #30765 enables reporting. Shutdown scripts remain
outside the timeline, and agent health and badge rules are unchanged.

Refs: https://linear.app/codercom/issue/PLAT-550/phase-3-expose-skipped-and-canceled-script-outcomes
2026-10-16 15:55:28 +00:00

90 lines
3.0 KiB
Go

package agentapi
import (
"context"
"github.com/google/uuid"
"golang.org/x/xerrors"
agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
)
type ScriptsAPI struct {
Database database.Store
}
func (s *ScriptsAPI) ScriptCompleted(ctx context.Context, req *agentproto.WorkspaceAgentScriptCompletedRequest) (*agentproto.WorkspaceAgentScriptCompletedResponse, error) {
res := &agentproto.WorkspaceAgentScriptCompletedResponse{}
if req.GetTiming() == nil {
return nil, xerrors.New("script timing is required")
}
scriptID, err := uuid.FromBytes(req.GetTiming().GetScriptId())
if err != nil {
return nil, xerrors.Errorf("script id from bytes: %w", err)
}
scriptStart := req.GetTiming().GetStart()
if !scriptStart.IsValid() || scriptStart.AsTime().IsZero() {
return nil, xerrors.New("script start time is required and cannot be zero")
}
scriptEnd := req.GetTiming().GetEnd()
if !scriptEnd.IsValid() || scriptEnd.AsTime().IsZero() {
return nil, xerrors.New("script end time is required and cannot be zero")
}
if scriptStart.AsTime().After(scriptEnd.AsTime()) {
return nil, xerrors.New("script start time cannot be after end time")
}
var stage database.WorkspaceAgentScriptTimingStage
switch req.Timing.Stage {
case agentproto.Timing_START:
stage = database.WorkspaceAgentScriptTimingStageStart
case agentproto.Timing_STOP:
stage = database.WorkspaceAgentScriptTimingStageStop
case agentproto.Timing_CRON:
stage = database.WorkspaceAgentScriptTimingStageCron
}
var status database.WorkspaceAgentScriptTimingStatus
switch req.Timing.Status {
case agentproto.Timing_OK:
status = database.WorkspaceAgentScriptTimingStatusOk
case agentproto.Timing_EXIT_FAILURE:
status = database.WorkspaceAgentScriptTimingStatusExitFailure
case agentproto.Timing_TIMED_OUT:
status = database.WorkspaceAgentScriptTimingStatusTimedOut
case agentproto.Timing_PIPES_LEFT_OPEN:
status = database.WorkspaceAgentScriptTimingStatusPipesLeftOpen
case agentproto.Timing_SKIPPED:
status = database.WorkspaceAgentScriptTimingStatusSkipped
case agentproto.Timing_CANCELED:
status = database.WorkspaceAgentScriptTimingStatusCanceled
default:
// Without this the empty value reaches Postgres and fails with an
// enum error that does not say where it came from.
return nil, xerrors.Errorf("unknown script timing status %q: the agent may be newer than coderd", req.Timing.Status.String())
}
//nolint:gocritic // We need permissions to write to the DB here and we are in the context of the agent.
ctx = dbauthz.AsProvisionerd(ctx)
_, err = s.Database.InsertWorkspaceAgentScriptTimings(ctx, database.InsertWorkspaceAgentScriptTimingsParams{
ScriptID: scriptID,
Stage: stage,
Status: status,
StartedAt: req.Timing.Start.AsTime(),
EndedAt: req.Timing.End.AsTime(),
ExitCode: req.Timing.ExitCode,
})
if err != nil {
return nil, xerrors.Errorf("insert workspace agent script timings into database: %w", err)
}
return res, nil
}