Skip to content

refactor(agent): detect devcontainer via workspace marker, drop DEVSY_AGENT_IN_CONTAINER#622

Merged
skevetter merged 4 commits into
mainfrom
fix/agent-in-container-detection
Jul 8, 2026
Merged

refactor(agent): detect devcontainer via workspace marker, drop DEVSY_AGENT_IN_CONTAINER#622
skevetter merged 4 commits into
mainfrom
fix/agent-in-container-detection

Conversation

@skevetter

Copy link
Copy Markdown
Contributor

Summary

Fixes the misleading DEVSY_AGENT_IN_CONTAINER=1 is set but no container indicator file found; treating as host invocation debug line that appeared on every machine-provider (e.g. ws3-ssh) up, and replaces the fragile detection behind it.

IsHostAgentInvocation selects the per-workspace state layout — host (WorkspaceDir/agent + delete-stable contents/<id>) vs. devcontainer (FindAgentHomeFolder glob). The old signal, DEVSY_AGENT_IN_CONTAINER=1, was an inline prefix set on every remote agent command — including on a bare SSH machine host, which is not a container. A filesystem probe (isLikelyContainer: /.dockerenv, /run/.containerenv, /proc/1/cgroup) was added to tell the machine host apart from the real devcontainer. That probe:

  • logged the misleading line on every machine-provider up (the machine genuinely has no container markers), and
  • is fragile on container runtimes that drop none of those markers.

Change

Detect the devcontainer via the authoritative marker DEVSY_WORKSPACE_ID, which is injected into the container through each driver's run environment (docker -e / kubernetes env) and is never set host-side. It distinguishes the devcontainer from both the user CLI and the SSH machine host directly, across all drivers — no filesystem probe.

Removes the now-unnecessary DEVSY_AGENT_IN_CONTAINER env var, the ContainerAgentEnvPrefix / AgentCommandEnvPrefix helpers and all their inline command prefixes, and container_detect.go. Net −334 lines. The explicit --agent-dir override is preserved.

Verification (end-to-end)

  • ws3-ssh machine host: host layout (contents/<id>), misleading log gone, first + second up succeed, workspace usable over SSH.
  • docker container: DEVSY_WORKSPACE_ID present inside the container, UID remap intact.
  • kubernetes dockerless: up succeeds; marker reaches the pod.
  • Unit tests, full cmd/... + pkg/agent + pkg/tunnel suites, and diff-scoped lint all pass.

@netlify

netlify Bot commented Jul 8, 2026

Copy link
Copy Markdown

Deploy Preview for devsydev canceled.

Name Link
🔨 Latest commit a3e73ce
🔍 Latest deploy log https://app.netlify.com/projects/devsydev/deploys/6a4e55b1afc2770007ca58c9

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 40d9126e-b27f-4e39-a54f-d676663d6b7d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@netlify

netlify Bot commented Jul 8, 2026

Copy link
Copy Markdown

Deploy Preview for images-devsy-sh canceled.

Name Link
🔨 Latest commit a3e73ce
🔍 Latest deploy log https://app.netlify.com/projects/images-devsy-sh/deploys/6a4e55b11a22e600073cb343

@github-actions github-actions Bot added the size/l label Jul 8, 2026
IsHostAgentInvocation decides per-workspace state layout: host
(WorkspaceDir/agent, delete-stable) vs container (FindAgentHomeFolder glob).
The layout must follow where the agent physically runs, which the
orchestrator cannot know — a machine provider's exec command is an opaque
template that may docker-exec into a container or ssh into bare metal. Only
the running agent can tell, via container filesystem markers.

The old predicate combined that runtime probe (isLikelyContainer) with an
inline DEVSY_AGENT_IN_CONTAINER=1 env prefix set by every remote agent
command builder. The env var was redundant: it was only ever set on commands
that also run in a container (probe already true), and on a bare SSH machine
it resolved to host via the probe regardless. So

  host = NOT(envPrefix AND isLikelyContainer)

is equivalent, for every real caller, to

  host = NOT(isLikelyContainer)

Drop the env var, the ContainerAgentEnvPrefix / AgentCommandEnvPrefix helpers,
and their inline prefixes across all builders; keep the runtime probe and the
explicit --agent-dir override. This also removes the misleading "env set but
no container indicator" debug line, which was a legitimate, expected state on
a bare SSH machine.

Verified on the ws3-ssh provider: up and re-up behave identically to main
(host layout preserved). Note: workspace logs on a bare SSH machine remains
broken the same way it is on main (the agent runs on the host, which the
logs guard rejects) — a pre-existing issue, unchanged by this refactor.
@skevetter
skevetter force-pushed the fix/agent-in-container-detection branch from 381c33c to 98ad5b1 Compare July 8, 2026 13:08
skevetter added 3 commits July 8, 2026 08:20
…tainer

`devsy workspace logs` failed on the ws3-ssh (bare SSH machine) provider with
"only valid inside the workspace container or machine". The command fetches
the devcontainer's logs via the docker daemon (docker logs) and reads the
per-workspace state — both available on the machine/host that hosts the
container, not only inside it. The IsHostAgentInvocation guard was therefore
too strict: on a bare SSH machine the agent runs on the host, which the guard
rejected.

Drop the guard for logs; it already reads workspace state via
ReadAgentWorkspaceInfo (host or container layout) and errors clearly when the
workspace is absent (the user's own CLI case). logs-daemon and the daemon
command keep their guards — they read agent-daemon.log, which only exists
where the daemon runs.

Verified on ws3-ssh: logs now returns the container logs; and the docker
machine-provider logs e2e still passes.
Signed-off-by: Samuel K <skevetter@pm.me>
Signed-off-by: Samuel K <skevetter@pm.me>
@skevetter
skevetter marked this pull request as ready for review July 8, 2026 14:48
@skevetter
skevetter merged commit 42ef45c into main Jul 8, 2026
105 of 107 checks passed
@skevetter
skevetter deleted the fix/agent-in-container-detection branch July 8, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant