Skip to content

feat(agent): register scripts as sync units for lifecycle tracking#27153

Draft
SasSwart wants to merge 1 commit into
sync-timeline-watchfrom
sas/script-units-on-timeline
Draft

feat(agent): register scripts as sync units for lifecycle tracking#27153
SasSwart wants to merge 1 commit into
sync-timeline-watchfrom
sas/script-units-on-timeline

Conversation

@SasSwart

@SasSwart SasSwart commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Revives #26459 (closed as stale) and rebases it onto the sync-timeline-watch branch (#27152), which adds coder exp sync timeline --watch. Together, agent scripts now appear in the timeline: each script is registered as a sync unit and its registered → started → completed lifecycle is recorded in the unit event log, so coder exp sync timeline (and --watch) shows a live history of all coder scripts and when they ran.

Stacked on: #27152#27150#27149. Base is sync-timeline-watch, not main. Rebasing onto the watch branch lets the --watch flag be used when testing script lifecycle live.

Changes

  • agent/agentscripts/agentscripts.go: Options gains a UnitManager field. Init() registers each script as a unit keyed by DisplayName (scripts with an empty display name are skipped; duplicate names are logged and skipped). run() marks the unit started before execution and completed afterward, regardless of success or failure. All calls are nil-guarded, so the runner still works without a manager.
  • agent/agent.go: creates a single shared unit.Manager in init() and passes it to both the script runner (UnitManager) and the socket server.

Rebase note

The original #26459 changed agentsocket.NewServer to take *unit.Manager as a required positional parameter. The timeline stack already introduces the same composition root via a WithUnitManager functional option, so this revival drops #26459's NewServer signature change (and its test call-site edits) and instead wires the shared manager through agentsocket.WithUnitManager(a.unitManager). Net diff is 3 files, +168, and it touches only agent/ files (disjoint from the watch branch's cli/ changes), so the rebase onto sync-timeline-watch was conflict-free.

Testing

  • go build ./agent/...
  • go test ./agent/agentscripts/ ./agent/agentsocket/ (incl. new TestScriptUnitsRegistered and TestScriptUnitsLifecycle)
  • go test ./cli/ -run TestSync (timeline/status/watch goldens unchanged)
Original PR #26459 design notes

Unit name: each script's DisplayName is used as the unit name (unit.ID). User-friendly but not guaranteed unique; uniqueness enforcement was deferred to 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 → ...


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'
and appear in 'coder exp sync timeline'. As each script executes, its
unit status transitions from pending to started, then to completed.

Changes:
- 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 (via WithUnitManager).
- Tests: added TestScriptUnitsRegistered and TestScriptUnitsLifecycle.

Rebased onto the sync-unit-timeline branch: the socket server already
exposes a WithUnitManager option there, so this revives PR #26459
without re-introducing its NewServer signature change.
@SasSwart SasSwart force-pushed the sas/script-units-on-timeline branch from 9b52d47 to 5981e99 Compare July 10, 2026 10:23
@SasSwart SasSwart changed the base branch from sync-unit-timeline to sync-timeline-watch July 10, 2026 10:23
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