Skip to content

feat: register scripts as sync units for lifecycle tracking#26459

Draft
SasSwart wants to merge 1 commit into
mainfrom
sas/script-units
Draft

feat: register scripts as sync units for lifecycle tracking#26459
SasSwart wants to merge 1 commit into
mainfrom
sas/script-units

Conversation

@SasSwart

Copy link
Copy Markdown
Contributor

The script runner now registers each workspace agent script as a sync unit during initialization, making all scripts immediately visible via coder exp sync list. As the script runner executes each script, its unit status transitions through the lifecycle: pending, started, completed.

Problem

The agent/unit.Manager (sync unit system) and agent/agentscripts.Runner (script runner) were independent. Scripts run by the agent were invisible to coder exp sync list, and there was no way to observe script lifecycle through the dependency coordination system.

Changes

Shared unit.Manager (composition root)

Previously, unit.NewManager() was created inside agentsocket.NewServer(), hidden from the rest of the agent. This PR lifts it out so the agent creates a single unit.Manager and passes it to both the script runner and the socket server.

  • agent/agentsocket/server.go: NewServer takes *unit.Manager as an explicit required parameter instead of creating its own.
  • agent/agent.go: Creates a shared unit.NewManager() in init(), passes it to both agentscripts.New() and agentsocket.NewServer().

Script lifecycle tracking

  • agent/agentscripts/agentscripts.go: Options gains a UnitManager field. Init() registers each script using its DisplayName as the unit ID. run() marks the unit as started before execution and completed after (regardless of success/failure).
  • Unit name uses DisplayName for now. Uniqueness enforcement is deferred to a follow-up.
  • All unit manager calls are nil-guarded, so the runner works without a manager (backwards compatible).

Tests

  • Updated all NewServer call sites in tests to pass unit.NewManager().
  • Added TestScriptUnitsRegistered: verifies scripts with display names are registered as pending units during Init, and scripts without display names are skipped.
  • Added TestScriptUnitsLifecycle: verifies the full pending, started, completed transition through Execute.
Mini RFC / Design notes

Unit name

Each script's DisplayName is used as the unit name (unit.ID). This is user-friendly but not guaranteed unique. Uniqueness will be addressed in a follow-up.

Status lifecycle per script

Init()                → Register(displayName)           → pending
run() before start    → UpdateStatus(_, StatusStarted)  → started
run() after complete  → UpdateStatus(_, StatusComplete) → completed

For cron scripts that run multiple times, the cycle repeats: completed → started → completed → ...

What this does NOT change

  • No proto/DRPC changes; the existing SyncList RPC from PR feat: add coder exp sync list command #26443 surfaces the units as-is.
  • No new CLI commands.
  • No uniqueness enforcement on DisplayName (follow-up).
  • No dependency edges between scripts (follow-up).

Generated by Coder Agents on behalf of @SasSwart

The script runner now registers each script as a sync unit during
Init() so all scripts are immediately visible via 'coder exp sync list'.
As each script executes, its unit status transitions from pending to
started, then to completed.

Changes:
- agent/agentsocket/server.go: NewServer takes *unit.Manager as a
  required parameter instead of creating its own internally.
- agent/agentscripts/agentscripts.go: Options gains a UnitManager field.
  Init() registers scripts using DisplayName as the unit ID.
  run() sets unit status to started before execution and completed after.
- agent/agent.go: Creates a shared unit.Manager and passes it to both
  the script runner and the socket server.
- Tests: Updated NewServer call sites; added TestScriptUnitsRegistered
  and TestScriptUnitsLifecycle.
@SasSwart SasSwart changed the title feat(agent): register scripts as sync units for lifecycle tracking feat: register scripts as sync units for lifecycle tracking Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant