Skip to content

chore: pull in cherry picks for v2.24 #18674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
73ffb71
chore: remove chats experiment (#18535)
dannykopping Jun 25, 2025
cc51308
feat: allow new immutable parameters for existing workspaces (#18579)
Emyrk Jun 25, 2025
fac6263
chore: upgrade aisdk-go lib, remove vestigial code (#18577)
dannykopping Jun 25, 2025
4c938c0
refactor: remove beta label from 'select a preset' menu (#18538)
BrunoQuaresma Jun 25, 2025
19d8a40
fix: hide the preset parameter visibility switch when it has no effec…
SasSwart Jun 26, 2025
a4a5892
feat: graduate prebuilds to general availability (#18607)
SasSwart Jun 26, 2025
8e6417a
fix(agent): start devcontainers through agentcontainers package (#18471)
DanielleMaywood Jun 25, 2025
63b619c
fix(agent/agentcontainers): filter out "is test run" devcontainers (#…
mafredri Jun 25, 2025
03d1570
feat(agent/agentcontainers): add feature options as envs (#18576)
mafredri Jun 25, 2025
5be5bf3
fix(coderd/agentapi): make sub agent slugs more unique (#18581)
DanielleMaywood Jun 25, 2025
cf6d208
feat(agent/agentcontainers): add more envs to readconfig for app URL …
mafredri Jun 26, 2025
226aec1
fix!: use devcontainer ID when rebuilding a devcontainer (#18604)
DanielleMaywood Jun 26, 2025
766fc4c
fix(agent/agentcontainers): chown coder binary (#18611)
DanielleMaywood Jun 26, 2025
07d749c
fix(agent/agentcontainers): stop logging empty lines (#18605)
DanielleMaywood Jun 26, 2025
aab5059
chore: parse app status link (#18439)
code-asher Jun 26, 2025
cbc97de
fix(agent): delay containerAPI init to ensure startup scripts run bef…
mafredri Jun 27, 2025
5058d1b
feat: add task link in the workspace page when it is running a task (…
BrunoQuaresma Jun 27, 2025
b7c7d1a
refactor: move required external auth buttons to the submit side (#18…
BrunoQuaresma Jun 27, 2025
b8930ec
fix(agent): fix script filtering for devcontainers (#18635)
mafredri Jun 27, 2025
1891c6f
refactor: show the apps as soon as possible (#18625)
BrunoQuaresma Jun 27, 2025
99f3664
feat: redirect to the task page after creation (#18626)
BrunoQuaresma Jun 27, 2025
19089ad
fix: use default preset when creating a workspace for task (#18623)
BrunoQuaresma Jun 27, 2025
47f813e
fix(agent/agentcontainers): ensure proper channel closure for updateT…
mafredri Jun 27, 2025
2a68b01
fix(agent/agentcontainers): split Init into Init and Start for early …
mafredri Jun 27, 2025
2daec01
feat: make task panels resizable (#18590)
BrunoQuaresma Jun 27, 2025
9e923f9
fix: use only template version ID to create task workspace (#18642)
BrunoQuaresma Jun 27, 2025
830a9ed
chore: add beta badge to tasks (#18656)
dannykopping Jun 30, 2025
da71915
fix(agent/agentcontainers): always derive devcontainer name from work…
mafredri Jun 30, 2025
9a9dd5d
fix: handle health status when displaying task apps (#18675)
hugodutka Jun 30, 2025
4bcbc96
fix: improve reliability of app statuses (#18622)
code-asher Jun 30, 2025
2d210aa
chore: fix idle state icon when disabled (#18554)
code-asher Jun 25, 2025
459b9d9
docs: add warning about prebuilds incompatibility with certain featur…
ssncferreira Jul 1, 2025
b975443
Merge branch 'release/2.24' into cherry-picks-2.24
stirby Jul 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 92 additions & 28 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Options struct {
Execer agentexec.Execer
Devcontainers bool
DevcontainerAPIOptions []agentcontainers.Option // Enable Devcontainers for these to be effective.
Clock quartz.Clock
}

type Client interface {
Expand Down Expand Up @@ -144,6 +145,9 @@ func New(options Options) Agent {
if options.PortCacheDuration == 0 {
options.PortCacheDuration = 1 * time.Second
}
if options.Clock == nil {
options.Clock = quartz.NewReal()
}

prometheusRegistry := options.PrometheusRegistry
if prometheusRegistry == nil {
Expand All @@ -157,6 +161,7 @@ func New(options Options) Agent {
hardCtx, hardCancel := context.WithCancel(context.Background())
gracefulCtx, gracefulCancel := context.WithCancel(hardCtx)
a := &agent{
clock: options.Clock,
tailnetListenPort: options.TailnetListenPort,
reconnectingPTYTimeout: options.ReconnectingPTYTimeout,
logger: options.Logger,
Expand Down Expand Up @@ -204,6 +209,7 @@ func New(options Options) Agent {
}

type agent struct {
clock quartz.Clock
logger slog.Logger
client Client
exchangeToken func(ctx context.Context) (string, error)
Expand Down Expand Up @@ -273,7 +279,7 @@ type agent struct {

devcontainers bool
containerAPIOptions []agentcontainers.Option
containerAPI atomic.Pointer[agentcontainers.API] // Set by apiHandler.
containerAPI *agentcontainers.API
}

func (a *agent) TailnetConn() *tailnet.Conn {
Expand Down Expand Up @@ -330,6 +336,19 @@ func (a *agent) init() {
// will not report anywhere.
a.scriptRunner.RegisterMetrics(a.prometheusRegistry)

if a.devcontainers {
containerAPIOpts := []agentcontainers.Option{
agentcontainers.WithExecer(a.execer),
agentcontainers.WithCommandEnv(a.sshServer.CommandEnv),
agentcontainers.WithScriptLogger(func(logSourceID uuid.UUID) agentcontainers.ScriptLogger {
return a.logSender.GetScriptLogger(logSourceID)
}),
}
containerAPIOpts = append(containerAPIOpts, a.containerAPIOptions...)

a.containerAPI = agentcontainers.NewAPI(a.logger.Named("containers"), containerAPIOpts...)
}

a.reconnectingPTYServer = reconnectingpty.NewServer(
a.logger.Named("reconnecting-pty"),
a.sshServer,
Expand Down Expand Up @@ -1141,17 +1160,27 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
}

var (
scripts = manifest.Scripts
scriptRunnerOpts []agentscripts.InitOption
scripts = manifest.Scripts
devcontainerScripts map[uuid.UUID]codersdk.WorkspaceAgentScript
)
if a.devcontainers {
var dcScripts []codersdk.WorkspaceAgentScript
scripts, dcScripts = agentcontainers.ExtractAndInitializeDevcontainerScripts(manifest.Devcontainers, scripts)
// See ExtractAndInitializeDevcontainerScripts for motivation
// behind running dcScripts as post start scripts.
scriptRunnerOpts = append(scriptRunnerOpts, agentscripts.WithPostStartScripts(dcScripts...))
if a.containerAPI != nil {
// Init the container API with the manifest and client so that
// we can start accepting requests. The final start of the API
// happens after the startup scripts have been executed to
// ensure the presence of required tools. This means we can
// return existing devcontainers but actual container detection
// and creation will be deferred.
a.containerAPI.Init(
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
agentcontainers.WithDevcontainers(manifest.Devcontainers, manifest.Scripts),
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
)

// Since devcontainer are enabled, remove devcontainer scripts
// from the main scripts list to avoid showing an error.
scripts, devcontainerScripts = agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, scripts)
}
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted, scriptRunnerOpts...)
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted)
if err != nil {
return xerrors.Errorf("init script runner: %w", err)
}
Expand All @@ -1168,7 +1197,18 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
// finished (both start and post start). For instance, an
// autostarted devcontainer will be included in this time.
err := a.scriptRunner.Execute(a.gracefulCtx, agentscripts.ExecuteStartScripts)
err = errors.Join(err, a.scriptRunner.Execute(a.gracefulCtx, agentscripts.ExecutePostStartScripts))

if a.containerAPI != nil {
// Start the container API after the startup scripts have
// been executed to ensure that the required tools can be
// installed.
a.containerAPI.Start()
for _, dc := range manifest.Devcontainers {
cErr := a.createDevcontainer(ctx, aAPI, dc, devcontainerScripts[dc.ID])
err = errors.Join(err, cErr)
}
}

dur := time.Since(start).Seconds()
if err != nil {
a.logger.Warn(ctx, "startup script(s) failed", slog.Error(err))
Expand All @@ -1187,14 +1227,6 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
}
a.metrics.startupScriptSeconds.WithLabelValues(label).Set(dur)
a.scriptRunner.StartCron()

// If the container API is enabled, trigger an immediate refresh
// for quick sub agent injection.
if cAPI := a.containerAPI.Load(); cAPI != nil {
if err := cAPI.RefreshContainers(ctx); err != nil {
a.logger.Error(ctx, "failed to refresh containers", slog.Error(err))
}
}
})
if err != nil {
return xerrors.Errorf("track conn goroutine: %w", err)
Expand All @@ -1204,6 +1236,38 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
}
}

func (a *agent) createDevcontainer(
ctx context.Context,
aAPI proto.DRPCAgentClient26,
dc codersdk.WorkspaceAgentDevcontainer,
script codersdk.WorkspaceAgentScript,
) (err error) {
var (
exitCode = int32(0)
startTime = a.clock.Now()
status = proto.Timing_OK
)
if err = a.containerAPI.CreateDevcontainer(dc.WorkspaceFolder, dc.ConfigPath); err != nil {
exitCode = 1
status = proto.Timing_EXIT_FAILURE
}
endTime := a.clock.Now()

if _, scriptErr := aAPI.ScriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
Timing: &proto.Timing{
ScriptId: script.ID[:],
Start: timestamppb.New(startTime),
End: timestamppb.New(endTime),
ExitCode: exitCode,
Stage: proto.Timing_START,
Status: status,
},
}); scriptErr != nil {
a.logger.Warn(ctx, "reporting script completed failed", slog.Error(scriptErr))
}
return err
}

// createOrUpdateNetwork waits for the manifest to be set using manifestOK, then creates or updates
// the tailnet using the information in the manifest
func (a *agent) createOrUpdateNetwork(manifestOK, networkOK *checkpoint) func(context.Context, proto.DRPCAgentClient26) error {
Expand All @@ -1227,7 +1291,6 @@ func (a *agent) createOrUpdateNetwork(manifestOK, networkOK *checkpoint) func(co
// agent API.
network, err = a.createTailnet(
a.gracefulCtx,
aAPI,
manifest.AgentID,
manifest.DERPMap,
manifest.DERPForceWebSockets,
Expand Down Expand Up @@ -1262,9 +1325,9 @@ func (a *agent) createOrUpdateNetwork(manifestOK, networkOK *checkpoint) func(co
network.SetBlockEndpoints(manifest.DisableDirectConnections)

// Update the subagent client if the container API is available.
if cAPI := a.containerAPI.Load(); cAPI != nil {
if a.containerAPI != nil {
client := agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)
cAPI.UpdateSubAgentClient(client)
a.containerAPI.UpdateSubAgentClient(client)
}
}
return nil
Expand Down Expand Up @@ -1382,7 +1445,6 @@ func (a *agent) trackGoroutine(fn func()) error {

func (a *agent) createTailnet(
ctx context.Context,
aAPI proto.DRPCAgentClient26,
agentID uuid.UUID,
derpMap *tailcfg.DERPMap,
derpForceWebSockets, disableDirectConnections bool,
Expand Down Expand Up @@ -1515,10 +1577,7 @@ func (a *agent) createTailnet(
}()
if err = a.trackGoroutine(func() {
defer apiListener.Close()
apiHandler, closeAPIHAndler := a.apiHandler(aAPI)
defer func() {
_ = closeAPIHAndler()
}()
apiHandler := a.apiHandler()
server := &http.Server{
BaseContext: func(net.Listener) context.Context { return ctx },
Handler: apiHandler,
Expand All @@ -1532,7 +1591,6 @@ func (a *agent) createTailnet(
case <-ctx.Done():
case <-a.hardCtx.Done():
}
_ = closeAPIHAndler()
_ = server.Close()
}()

Expand Down Expand Up @@ -1871,6 +1929,12 @@ func (a *agent) Close() error {
a.logger.Error(a.hardCtx, "script runner close", slog.Error(err))
}

if a.containerAPI != nil {
if err := a.containerAPI.Close(); err != nil {
a.logger.Error(a.hardCtx, "container API close", slog.Error(err))
}
}

// Wait for the graceful shutdown to complete, but don't wait forever so
// that we don't break user expectations.
go func() {
Expand Down
Loading
Loading