Skip to content

feat(agent): populate subagent ID for terraform-defined devcontainers#21942

Merged
DanielleMaywood merged 17 commits intomainfrom
danielle/devcontainer-resources/agent
Feb 6, 2026
Merged

feat(agent): populate subagent ID for terraform-defined devcontainers#21942
DanielleMaywood merged 17 commits intomainfrom
danielle/devcontainer-resources/agent

Conversation

@DanielleMaywood
Copy link
Contributor

@DanielleMaywood DanielleMaywood commented Feb 5, 2026

Closes coder/internal#1243

Completes the final piece of the puzzle. Support the pre-creation flow from the agent side.

@DanielleMaywood DanielleMaywood force-pushed the danielle/devcontainer-resources/agent branch 4 times, most recently from db0350c to c128b81 Compare February 5, 2026 13:22
Update the manifest API in coderd to populate the subagent_id field for
devcontainers, enabling the agent to receive devcontainer resource
definitions with pre-assigned IDs and use them when creating sub-agents.

This allows Terraform-defined devcontainers to:
- Have stable subagent IDs across workspace rebuilds
- Prevent accidental deletion/recreation of managed subagents
- Prevent UI-initiated rebuilds of terraform-managed containers
@DanielleMaywood DanielleMaywood force-pushed the danielle/devcontainer-resources/agent branch from c128b81 to bca517e Compare February 5, 2026 13:35
@DanielleMaywood DanielleMaywood marked this pull request as ready for review February 5, 2026 13:53
@DanielleMaywood DanielleMaywood requested review from johnstcn and mafredri and removed request for mafredri February 5, 2026 13:53
@coder-tasks
Copy link
Contributor

coder-tasks bot commented Feb 5, 2026

Documentation Check

Updates Needed

  • docs/admin/integrations/devcontainers/integration.md - Document the new Terraform-defined devcontainer feature with pre-assigned subagent IDs

Details

This PR introduces an important new capability for the coder_devcontainer resource: Terraform-defined devcontainers with pre-assigned subagent IDs. This feature needs to be documented in the existing Dev Containers integration guide.

What should be documented:

  1. New Capability: Explain that coder_devcontainer resources now create devcontainers with stable, pre-assigned subagent IDs that persist across workspace rebuilds

  2. Behavioral Differences: Document the distinction between:

    • Dynamically-created devcontainers (discovered via project discovery): Can be rebuilt from the UI, IDs may change
    • Terraform-defined devcontainers (via coder_devcontainer resource): Cannot be rebuilt from UI, stable IDs, managed by template updates
  3. UI Limitations: Explain that Terraform-defined devcontainers show a disabled rebuild button with a tooltip explaining they must be updated via template changes

  4. Benefits:

    • Stable subagent IDs across workspace rebuilds
    • Prevents accidental deletion/recreation of managed subagents
    • Prevents UI-initiated rebuilds that could conflict with Terraform state

Suggested location: Add a new section in docs/admin/integrations/devcontainers/integration.md after the "Configure Automatic Dev Container Startup" section, perhaps titled "Terraform-Managed Dev Containers" or "Stable Subagent IDs"

Example content structure:

## Terraform-Managed Dev Containers

Dev Containers defined using the `coder_devcontainer` resource are managed by Terraform 
and have stable subagent IDs that persist across workspace rebuilds...

### Benefits
- Stable subagent IDs prevent disruption during workspace updates
- Resources attached to subagents remain consistent
- Template changes are the single source of truth

### UI Behavior
Terraform-managed dev containers cannot be rebuilt from the Coder UI. Users will see a 
disabled rebuild button with a tooltip...

Automated review via Coder Tasks

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Partial review, will post more comments later.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Nice work! I don't need to review again.

})
}

func TestSubAgent_EqualConfig(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

It feels unfortunate to need to define two separate equality methods for two very similar structs.

api.mu.Unlock()
httpapi.Write(ctx, w, http.StatusConflict, codersdk.Response{
Message: "Cannot rebuild Terraform-defined devcontainer",
Detail: fmt.Sprintf("Devcontainer %q has resources defined in Terraform and cannot be rebuilt from the UI. Update the workspace template to modify this devcontainer.", dc.Name),
Copy link
Member

Choose a reason for hiding this comment

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

Why do we want to block recreation of a devcontainer (docker container) here? Perhaps it's justified but it could use an explanation.

Ultimately the agent will just be restarted, unmodified. (Or perhaps display apps updated.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that's a good point, silly me 🤦‍♀️

I'll get a test added to ensure rebuilding works as we expect then (restart unmodified other than picking up the new container).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 25120cc

- Allow re-building terraform managed dev containers
- Add a test to confirm we upsert, not delete & create
- Update the frontend to reflect this change
- Update the frontend to indicate a dev container is terraform managed
@coder-tasks
Copy link
Contributor

coder-tasks bot commented Feb 6, 2026

Documentation Check

Previous Feedback

Partially addressed - The implementation has been refined (terraform-managed devcontainers can now be rebuilt via upsert), but documentation is still needed.

Updates Needed

  • docs/admin/integrations/devcontainers/integration.md - Document the Terraform-defined devcontainer feature with stable subagent IDs

Details

The latest commits refined the implementation:

  • Terraform-managed devcontainers now support rebuilding (via upsert, not delete/recreate)
  • The UI now shows "(terraform)" indicator for managed devcontainers
  • Stable subagent IDs persist across rebuilds

Documentation should cover:

  1. Stable Subagent IDs: When using coder_devcontainer resource, subagents get pre-assigned IDs that persist across workspace rebuilds

  2. Behavioral Differences:

    • Project Discovery: Dynamically discovered devcontainers, can be rebuilt from UI, IDs may change
    • Terraform-Defined (coder_devcontainer resource): Stable IDs, can be rebuilt (config updates via upsert), UI shows "(terraform)" label
  3. Benefits:

    • Stable subagent IDs enable attaching Terraform resources to specific devcontainers
    • Prevents accidental ID changes during workspace updates
    • Config changes (apps, display settings) update without deleting the agent

Suggested location: Add a new section in docs/admin/integrations/devcontainers/integration.md after "Configure Automatic Dev Container Startup" (line 73), perhaps titled "Terraform-Managed Dev Containers" or "Stable Subagent IDs"


Automated review via Coder Tasks

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

A few more minor things, and checking whether or not display apps should be updated on re-create. I think they should, since we support updating, but reading the code/tests it doesn't seem like we do?

@coder-tasks
Copy link
Contributor

coder-tasks bot commented Feb 6, 2026

Documentation Check

Updates Needed

  • docs/admin/integrations/devcontainers/integration.md - Document Terraform-managed devcontainers with stable subagent IDs

Details

This PR introduces an important new capability for the coder_devcontainer resource: stable, pre-assigned subagent IDs for Terraform-defined devcontainers. This feature needs to be documented in the existing Dev Containers integration guide.

What should be documented:

  1. Stable Subagent IDs: When using coder_devcontainer resource, subagents get pre-assigned IDs that persist across workspace rebuilds. This enables attaching Terraform resources (like coder_app, coder_script) to specific devcontainer subagents.

  2. Behavioral Differences: Document the distinction between:

    • Project Discovery: Dynamically discovered devcontainers, can be rebuilt from UI, IDs may change between rebuilds
    • Terraform-Managed (via coder_devcontainer resource): Stable IDs that never change, can be rebuilt (config updates via upsert), UI shows "(terraform agent)" label
  3. Benefits:

    • Stable subagent IDs enable attaching Terraform resources to specific devcontainers
    • Prevents accidental ID changes during workspace updates
    • Config changes (apps, display settings) update without deleting the agent
    • Terraform remains the single source of truth for managed devcontainers
  4. UI Indicators: The UI now shows "dev container (terraform agent)" for Terraform-managed devcontainers with a tooltip explaining limited configurability.

Suggested location: Add a new section in docs/admin/integrations/devcontainers/integration.md after the "Configure Automatic Dev Container Startup" section (around line 73), perhaps titled:

Terraform-Managed Dev Containers with Stable IDs

Example content structure:

## Terraform-Managed Dev Containers with Stable IDs

Dev Containers defined using the `coder_devcontainer` resource have stable subagent IDs 
that persist across workspace rebuilds. This differs from dynamically discovered Dev Containers, 
which may receive new IDs when rebuilt.

### Benefits

- **Attach Terraform resources**: Stable IDs allow you to attach `coder_app`, `coder_script`, 
  and other Terraform resources directly to devcontainer subagents
- **Predictable behavior**: Subagent IDs never change during workspace rebuilds or template updates
- **Config updates without disruption**: Changes to apps and display settings update the existing 
  agent without deletion

### Usage

The subagent ID is automatically assigned and managed by Terraform when you create a 
`coder_devcontainer` resource. You can reference it in other resources...

Note: This addresses the limitation mentioned in lines 150-162 of the current documentation, where it states that Terraform resources cannot be attached to dev container sub-agents because they're "created dynamically after workspace provisioning." With this PR, Terraform-managed devcontainers now have stable IDs that CAN have resources attached.


Automated review via Coder Tasks

@DanielleMaywood DanielleMaywood merged commit 6ccd20d into main Feb 6, 2026
29 of 32 checks passed
@DanielleMaywood DanielleMaywood deleted the danielle/devcontainer-resources/agent branch February 6, 2026 15:52
@github-actions github-actions bot locked and limited conversation to collaborators Feb 6, 2026
@matifali
Copy link
Member

@DanielleMaywood, any plan to put this in dogfood? It would help to capture any unforeseen issues before the release.

@matifali matifali linked an issue Feb 18, 2026 that may be closed by this pull request
2 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agent: Handle terraform-defined devcontainer sub-agent creation Tasks should run within devcontainers

4 participants