Skip to content

Implement agentsd v0.1 - #1

Closed
Hunt4Bugs wants to merge 4 commits into
mainfrom
v0.1-impl
Closed

Hunt4Bugs wants to merge 4 commits into
mainfrom
v0.1-impl

Conversation

@Hunt4Bugs

Copy link
Copy Markdown
Owner

Summary

Implements the agentsd v0.1 spec in Go as a single binary: a daemon that supervises agent runs, and the CLI that talks to it over a Unix-socket /v1 API. The spec now lives at docs/spec.md. Its Amendments section records every deviation from the draft.

Core

  • XDG resolution works the same on macOS and Linux. State and runtime dirs are 0700. A socket path that is too long falls back to /tmp/agentsd-$UID.
  • Config and manifests are parsed as strict TOML, and errors report file, line, and key. Unknown keys and reserved keychain:/op:// secret references are errors.
  • Adapters for claude-code, codex, and exec. Argv templates can be overridden in config, and runs get a minimal environment containing only env.pass, env.set, and the AGENTSD_* variables.
  • The supervisor handles timeouts, stopping (SIGTERM, then SIGKILL after a grace period, signalled to the whole process group), per-agent and global concurrency caps, marking runs lost after a daemon crash, and graceful shutdown.
  • Each run stores run.json, events.jsonl, and timestamped stdout/stderr logs. Following a run replays its history and then streams live events with no gaps.
  • The socket accepts only peers with the same UID (SO_PEERCRED on Linux, LOCAL_PEERCRED on macOS).

Declarative, service, and diagnostics

  • plan/apply keep a managed.toml ledger. apply never deletes anything it didn't create, or any non-empty directory, and it registers agents with the daemon.
  • service install|uninstall|start|stop|restart|status manages a launchd agent or a systemd user unit, with --env-file support. It only controls a service that was loaded from its own service file.
  • doctor implements every §10 check. uninstall --purge-state never touches config or the AHS roots.

Distribution

  • install.sh: POSIX sh, 149 lines, wrapped in main(). It verifies the checksum, and the cosign signature when cosign is installed. It replaces the binary with an atomic rename.
  • GoReleaser builds four static targets with completions, signs the checksums with cosign keyless (a .sigstore.json bundle), and publishes a Homebrew cask.
  • CI runs on macOS and Linux: unit tests with -race, the e2e acceptance suite, the install.sh tests, golangci-lint, shellcheck, and a GoReleaser dry run. The release workflow is gated on CI passing.

Deviations from the draft spec (see docs/spec.md → Amendments)

  • [runtime] → [runtime_options]. The draft's examples were invalid TOML: runtime was defined as both a string and a table.
  • Registration model. The daemon runs only agents that apply has recorded in managed.toml.
  • Policy scan scope. It walks the AHS roots plus the run's working directory, because scanning only the allowed folders could never find anything. It is capped at 200k entries or 10s and labelled best-effort.
  • Homebrew uses a cask instead of a formula, because GoReleaser has deprecated formulas. The cask clears the quarantine flag after install.
  • Signing uses checksums.txt.sigstore.json instead of checksums.txt.sig.

Test plan

  • make test: vet plus unit tests with -race (macOS)
  • make e2e: seven acceptance tests against an isolated HOME: lifecycle and idempotent plan, lost on restart, policy violation, doctor detections, --json and exit codes everywhere, no writes outside allowed locations, and a purge that keeps config and roots (macOS)
  • make install-test: happy path, reinstall, checksum-mismatch abort, truncated script does nothing
  • golangci-lint: 0 issues. goreleaser release --snapshot builds all four targets and the cask.
  • GOOS=linux vet and build (amd64 and arm64)
  • CI on ubuntu-latest. The Linux tests have only run in CI; Docker wasn't available locally.
  • shellcheck (runs in CI; not installed locally)
  • Manual agentsd service install on macOS (launchd) and Linux (systemd)
  • Before the first tag: create Hunt4Bugs/homebrew-tap and the HOMEBREW_TAP_TOKEN secret

🤖 Generated with Claude Code

Hunt4Bugs and others added 2 commits September 28, 2026 15:40
Single Go binary: a daemon supervising agent runs over a Unix-socket
/v1 API, and the CLI that drives it (spec §4, §10).

- XDG-native layout identical on macOS and Linux, with socket-length
  fallback and 0700 state/runtime dirs
- Strict TOML config and manifests with file:line:key diagnostics
- claude-code, codex, and exec adapters with overridable argv templates
  and a minimal run environment
- Run store (run.json, events.jsonl, timestamped logs), supervisor with
  timeouts, stop grace, concurrency caps, and lost-run recovery
- Advisory policy: cwd precondition plus best-effort post-run scan
- plan/apply with a managed.toml ledger; agents register on apply
- launchd/systemd user service management, doctor, uninstall
- install.sh, GoReleaser (cask + cosign keyless), CI and release
  workflows gated on the §15 acceptance suite

docs/spec.md carries the spec with an Amendments section recording
deviations (e.g. [runtime] -> [runtime_options], which was invalid TOML).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Linux stamps mtimes from a coarse kernel clock that can lag time.Now()
by a few milliseconds, so files written right after launch could get an
mtime before the window start and escape the scan (TestScan failed on
ubuntu-latest). Probe the filesystem clock by touching a file in the
state dir just before exec and use its mtime as the window start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Hunt4Bugs Hunt4Bugs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of the v0.1 implementation. The two I'd fix before merging are the shutdown/launch race in supervisor.go and agentsd run exiting early when a follower gets cut off. The rest are smaller correctness issues plus a few efficiency and cleanup notes. Each is in its own thread below.

Comment thread internal/supervisor/supervisor.go Resolved
Comment thread internal/cli/runs.go Resolved
Comment thread internal/adapter/adapter.go Resolved
Comment thread internal/runstore/events.go Resolved
Comment thread internal/supervisor/supervisor.go Resolved
Comment thread internal/supervisor/supervisor.go Resolved
Comment thread internal/supervisor/sink.go Resolved
Comment thread internal/env/env.go Resolved
Comment thread internal/plan/plan.go Resolved
Comment thread internal/daemon/daemon.go Resolved
- Supervisor: start the process under the lock and refuse if a stop or
  shutdown already arrived, so a run mid-launch can no longer be
  orphaned; Stop now works on a pending run. Snapshot runs under the
  lock (data race). Reap leftover process-group members after the main
  process exits and record group_reaped on run.exited.
- Streams: every sink item gets a strictly increasing nanosecond
  timestamp and a seq, so replay order matches emission order. Replay
  reads files up to offsets recorded under the lock instead of holding
  it. Followers that fall behind get stream.truncated and the client
  resumes with after=<seq>; complete streams end with stream.end, so
  `agentsd run` no longer exits early on a cut-off follower.
- Adapter: prompts starting with "-" go to claude/codex on stdin, never
  argv, so they cannot be parsed as flags.
- plan: remove nested apply-created dirs in a single apply.
- Agent listings find each agent's last run in one pass.
- Cleanup: cmp.Or instead of two firstNonEmpty helpers; drop filter.go.

Spec amendments 13-16 document the behavior changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Hunt4Bugs Hunt4Bugs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 4690125 and 9597192. All 10 points from the earlier review are addressed, and go vet plus go test -race ./... pass locally. The fixes introduced two new issues, in the threads below. The first is a correctness bug. The second is a smaller lock-contention cost.

Comment thread internal/supervisor/supervisor.go Resolved
Comment thread internal/supervisor/supervisor.go Resolved
- wait() now marks the proc exited and stops the timeout/kill timers
  before reaping leftover group members. onTimeout, Stop and Shutdown
  skip an exited proc, so a timeout or stop landing during the reap can
  no longer record a clean exit as timed_out or stopped.
- cmd.Start() runs outside the supervisor lock. A stop or shutdown that
  arrives while the process is starting sets stopReq (with its grace),
  which launch applies to the new process group as soon as Start
  returns.

Tests: TestCleanExitDuringReapKeepsStatus (fails on the previous
ordering) and TestStopDuringStartSignalsNewProcess.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hunt4Bugs Hunt4Bugs closed this Nov 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