Skip to content

fix(agent): default process working directory to agent dir or $HOME#23224

Merged
mafredri merged 1 commit intomainfrom
mafredri/fix-process-default-workdir
Mar 18, 2026
Merged

fix(agent): default process working directory to agent dir or $HOME#23224
mafredri merged 1 commit intomainfrom
mafredri/fix-process-default-workdir

Conversation

@mafredri
Copy link
Member

@mafredri mafredri commented Mar 18, 2026

Processes started via the agent process API inherited the agent's own working directory (/tmp/coder.xxx) when no WorkDir was specified. SSH sessions already use a fallback chain in agent/agentssh/agentssh.go:898-910: configured agent directory > $HOME. The process manager had no equivalent.

This wires the same manifest directory closure into agentproc.NewAPI so the resolved working directory priority is:

  1. Explicit req.WorkDir from the request
  2. Agent's configured directory (from manifest, validated with os.Stat)
  3. $HOME

The resolved directory is recorded on the process struct so ProcessInfo.WorkDir and pathStore git notifications reflect where the process actually ran, not just what was in the request.

🤖 This PR was created with the help of Coder Agents, and has been reviewed by a human. 🏂🏻

@mafredri mafredri marked this pull request as ready for review March 18, 2026 14:33
@mafredri mafredri requested a review from kylecarbs March 18, 2026 14:33
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

Ahh, much nicer! Good find

@mafredri
Copy link
Member Author

Actually, going to pipe through configured working directory from coder agent.

@mafredri mafredri force-pushed the mafredri/fix-process-default-workdir branch from cea4797 to 642aecf Compare March 18, 2026 14:43
@mafredri mafredri changed the title fix(agent): default process working directory to $HOME fix(agent): default process working directory to agent dir or $HOME Mar 18, 2026
@mafredri mafredri force-pushed the mafredri/fix-process-default-workdir branch 2 times, most recently from 7163a9e to 31cc520 Compare March 18, 2026 14:55
clock: quartz.NewReal(),
procs: make(map[string]*process),
updateEnv: updateEnv,
workingDir: workingDir,
Copy link
Member

Choose a reason for hiding this comment

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

How often is this likely to change during an agent lifecycle?
Could this just be a string?

Copy link
Member Author

Choose a reason for hiding this comment

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

It needs to be a function. The processAPI is created once in agent.init() (line 388), but the manifest directory can change across reconnects since run() calls a.manifest.Swap() (line 1262) each time. A plain string would capture whatever value the manifest had at init time (empty, since the manifest has not been fetched yet).

The closure reads a.manifest.Load().Directory at process-start time, picking up the latest value. This is the same pattern SSH uses for WorkingDirectory at line 334.

The nil guard on the manifest (if m := a.manifest.Load(); m != nil) is the one difference from SSH, which dereferences without checking. The guard is there because processAPI is created in init() before run() populates the manifest. In practice the HTTP routes are unreachable until the manifest is loaded (the tailnet listener starts after manifestOK), but the guard costs nothing.

🤖 I traced the init path so you don't have to.

@mafredri mafredri force-pushed the mafredri/fix-process-default-workdir branch 2 times, most recently from 71cff44 to e699e4c Compare March 18, 2026 15:40
@mafredri mafredri enabled auto-merge (squash) March 18, 2026 15:43
@mafredri mafredri force-pushed the mafredri/fix-process-default-workdir branch from e699e4c to 687bca8 Compare March 18, 2026 15:51
Processes started via the agent process API inherited the agent's
own working directory (/tmp/coder.xxx) when no WorkDir was
specified. SSH sessions already use a fallback chain: configured
agent directory > $HOME. This wires the same manifest directory
closure into the process manager so the priority is now:

  explicit req.WorkDir > agent configured dir > $HOME

The resolved directory is recorded on the process struct so
ProcessInfo.WorkDir and pathStore notifications reflect where
the process actually ran.
@mafredri mafredri force-pushed the mafredri/fix-process-default-workdir branch from 687bca8 to 400f726 Compare March 18, 2026 16:27
@mafredri mafredri merged commit 119030d into main Mar 18, 2026
41 of 43 checks passed
@mafredri mafredri deleted the mafredri/fix-process-default-workdir branch March 18, 2026 16:46
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2026
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.

3 participants