Skip to content

fix: resume uses snapshot directory instead of caller's cwd#7

Merged
aannoo merged 1 commit intoaannoo:mainfrom
bloodcarter:fix/resume-directory
Mar 8, 2026
Merged

fix: resume uses snapshot directory instead of caller's cwd#7
aannoo merged 1 commit intoaannoo:mainfrom
bloodcarter:fix/resume-directory

Conversation

@bloodcarter
Copy link
Contributor

Summary

  • hcom r <name> now starts the agent in the directory where it was originally launched, not where the resume command is run
  • Fixes database corruption when resuming from a different directory
  • Adds --dir flag for explicit directory override on resume/fork

The Bug

When resuming an agent with hcom r <name> from a different directory than the original launch, two things go wrong:

  1. Wrong working directory: The resumed agent starts in the caller's cwd instead of the directory stored in the stopped snapshot. This breaks relative paths, project-specific configs, and CLAUDE.md / .claude/settings.json resolution.
  2. Database corruption: initialize_instance_in_position_file() independently reads current_dir() and writes it to the directory field in the DB. If the agent is later stopped and resumed again, the corrupted directory propagates through snapshots.

Reproduction

cd /project/frontend && hcom 1 claude --go   # launches "luna" in /project/frontend
hcom kill luna
cd /project/backend && hcom r luna            # BUG: luna starts in /project/backend

The Fix

1. resume.rs — Use snapshot directory for resume

do_resume() now extracts the directory field from the stopped snapshot and computes effective_cwd with this priority:

Priority Condition Directory used
1 --dir /path flag The specified path (validated + canonicalized)
2 Fork mode (hcom f) Caller's current_dir() (fork = fresh start)
3 Resume with valid snapshot dir Snapshot directory (continue where you left off)
4 Resume, snapshot dir deleted Caller's current_dir() with stderr warning

2. instances.rs — Eliminate stale directory window

initialize_instance_in_position_file() now accepts an optional cwd_override parameter. When provided, it uses the override instead of calling current_dir() independently. This eliminates the window where the DB records the wrong directory during pre-registration.

3. launcher.rs — Pass correct cwd to pre-registration

The launch() function now passes working_dir (from LaunchParams.cwd) to initialize_instance_in_position_file(), ensuring the DB directory is correct from the moment the instance is created.

4. New --dir flag

Resume and fork now support --dir <path> (and --dir=<path>) to explicitly override the working directory:

hcom r luna --dir /project/frontend    # resume in specific directory
hcom f luna --dir /project/new-feature # fork into specific directory

The --dir path is validated (is_dir() check) and canonicalized to an absolute path.

Files Changed

File Change
src/commands/resume.rs Extract directory from snapshot, compute effective_cwd, add --dir flag
src/instances.rs Add cwd_override: Option<&str> parameter
src/launcher.rs Pass working_dir as cwd_override
src/commands/start.rs Pass None for new parameter (2 call sites, no behavior change)

Testing

All scenarios verified with isolated HCOM_DIR:

Scenario Result
Resume from wrong directory → uses snapshot dir PASS
--dir /tmp override PASS
--dir /nonexistent → error message PASS
Resume when original dir deleted → warns, uses cwd PASS
Fork from different dir → uses current dir PASS
Fork directory vs original directory are independent PASS

Works for all tool types (Claude, Codex, Gemini, OpenCode) — the directory logic is in the common path before tool-specific branching.

🤖 Generated with Claude Code

When resuming an agent with `hcom r <name>`, the agent now starts in
the directory where it was originally launched, not the directory where
the resume command is run. This prevents database corruption and
incorrect working directory issues.

Changes:
- resume.rs: extract `directory` from stopped snapshot, compute
  effective_cwd with priority: --dir flag > fork=cwd > resume=snapshot
  > fallback with warning
- instances.rs: add cwd_override parameter to
  initialize_instance_in_position_file() so the DB records the correct
  directory from pre-registration (no stale window)
- launcher.rs: pass working_dir as cwd_override
- New --dir flag for explicit directory override (validated and
  canonicalized)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@aannoo aannoo merged commit 1cb3dcb into aannoo:main Mar 8, 2026
1 check passed
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.

2 participants