Skip to content

Fix agent init: interpolate init_script at plan time via heredoc#44

Merged
MathurAditya724 merged 26 commits into
mainfrom
fix-agent-init-heredoc
May 6, 2026
Merged

Fix agent init: interpolate init_script at plan time via heredoc#44
MathurAditya724 merged 26 commits into
mainfrom
fix-agent-init-heredoc

Conversation

@MathurAditya724
Copy link
Copy Markdown
Member

Changes

  • Fix agent connectivity — Replace the CODER_AGENT_INIT_SCRIPT env var approach with direct Terraform interpolation of coder_agent.main.init_script into container args using a heredoc. The script is a literal string in the pod spec with no runtime shell expansion — same pattern as Sentry's GCE startup-script. Drop CODER_AGENT_TOKEN and CODER_AGENT_INIT_SCRIPT env vars (both embedded in the init script by the Coder provider). OpenCode is started via coder_script after the agent connects.

  • Add linux_amd64 provider hashes — Fixes the .terraform.lock.hcl was modified during init warning on every workspace provision.

Replace the CODER_AGENT_INIT_SCRIPT env var approach with direct
Terraform interpolation of coder_agent.main.init_script into the
container args using a heredoc. Matches the pattern used by Sentry's
GCE startup-script — the script is a literal string in the pod spec,
never subject to runtime shell expansion.

Drop CODER_AGENT_TOKEN and CODER_AGENT_INIT_SCRIPT env vars (both
are embedded in the init script by the Coder provider).
Fixes the 'provider hashes are missing for the current platform
(linux_amd64)' warning that appeared on every workspace provision.
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 6, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
opencode-email-worker e24c005 Commit Preview URL

Branch Preview URL
May 06 2026, 11:06 PM

The agent init script requires these to connect back to the Coder
control plane. CODER_AGENT_URL was missing entirely, causing:
  error: create agent client: CODER_AGENT_URL must be set
…cript

The Coder bootstrap script uses ACCESS_URL and AUTH_TYPE as shell
variables to set CODER_AGENT_URL and download the agent binary.
When the Coder server's access URL isn't configured, these resolve
to empty strings and the agent fails with 'CODER_AGENT_URL must be set'.

Set them explicitly as container env vars using data.coder_workspace.me.access_url.
The Coder provider substitutes ACCESS_URL as a literal value into the
init script at plan time. When the server's access URL config is empty,
the script has literal empty strings for BINARY_URL and CODER_AGENT_URL.

After writing the init script to a file via heredoc, sed-patch both
lines to inject the correct URL from the CODER_AGENT_URL container
env var (set from data.coder_workspace.me.access_url).
data.coder_workspace.me.access_url returns empty because the Coder
server doesn't have access_url configured. Hardcode the URL until
the server config is fixed.
printenv dumps the raw init script to a file without any shell
expansion. Then sed patches the baked-in empty CODER_AGENT_URL
and BINARY_URL with the correct server URL before exec'ing.
The Coder provider bakes an empty ACCESS_URL into init_script because
the server's access URL isn't configured. Every approach to patch the
script fails because the substitution is literal (not a shell var
reference). Since the image already has the coder binary, just exec
it directly with CODER_AGENT_URL, CODER_AGENT_AUTH, and
CODER_AGENT_TOKEN set as container env vars.
The Coder provider bakes empty ACCESS_URL into init_script. Use
Terraform's replace() to fix BINARY_URL and CODER_AGENT_URL in the
script string at plan time, before it's written to the pod spec.
No shell quoting or sed needed.
The Terraform state confirms coder_agent.main.init_script already has
the correct BINARY_URL and CODER_AGENT_URL baked in by the provider.
The replace() calls and extra env vars were unnecessary. Drop them and
use the simple heredoc pattern that matches Sentry's templates.
The heredoc approach fails on Kubernetes because args are serialized
as JSON — newlines become literal \n escape sequences and the shell
never sees the line breaks that heredoc syntax requires.

Pass the init script as an env var instead and use printenv to write
it to a file, which preserves newlines correctly. This is the
Kubernetes-specific adaptation of the Sentry GCE pattern.
Wrap the gh auth setup-git and git identity configuration block
with set +e / set -e so failures in that section (e.g. gh spawning
git in a non-repo cwd, network errors, jq parse failures) are
logged as warnings instead of aborting the script before exec "$@".

The critical setup (volume ownership, mkdir, git init) still runs
under set -e. Only the best-effort identity block is relaxed.
… of direct /usr/bin/coder agent

Remove the command override that bypassed docker-entrypoint.sh entirely.
The image ENTRYPOINT (tini -> docker-entrypoint.sh) now runs first for
volume ownership, git init, and gh/git identity setup, then exec's into
the Coder init script delivered via the CODER_AGENT_INIT_SCRIPT env var.
… opencode serve

The container command (sh -c init_script) bypasses docker-entrypoint.sh
entirely, so OpenCode never gets the environment it needs: no git init,
no .opencode session dir, no PVC chown. The coder_script now replicates
the critical entrypoint setup before starting opencode serve.

Also rename opentower display_name from 'Opentower Webhooks' to 'OpenTower'.
@MathurAditya724 MathurAditya724 marked this pull request as ready for review May 6, 2026 23:27
@MathurAditya724 MathurAditya724 merged commit b051b4d into main May 6, 2026
3 checks passed
@MathurAditya724 MathurAditya724 deleted the fix-agent-init-heredoc branch May 6, 2026 23:27
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