Skip to content

Fix/data dir home fallback#1514

Open
Longchuanzheng wants to merge 3 commits into
chenhg5:mainfrom
Longchuanzheng:fix/data-dir-home-fallback
Open

Fix/data dir home fallback#1514
Longchuanzheng wants to merge 3 commits into
chenhg5:mainfrom
Longchuanzheng:fix/data-dir-home-fallback

Conversation

@Longchuanzheng

Copy link
Copy Markdown

Summary

Fix daemon startup/runtime path resolution under systemd and launchd by absolutizing data_dir and injecting the install-time HOME into generated service definitions. This prevents agent subprocesses that cd into work_dir from resolving .cc-connect under the project directory when HOME is missing.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing behavior to change)
  • Documentation only
  • Internal refactor / chore (no user-visible change)

Testing

Verified targeted config and daemon regressions locally:

  • GOCACHE=/tmp/cc-connect-gocache go test ./config -run 'TestLoad_DataDirAbsolute|TestLoad_DefaultsDataDir' -v
  • GOCACHE=/tmp/cc-connect-gocache go test ./daemon -run 'TestBuildPlist_IncludesHOME|TestBuildPlist_OmitsHOMEWhenEmpty|TestBuildPlist_EnvExtraHOMEDoesNotOverrideTemplateHOME' -v
  • GOCACHE=/tmp/cc-connect-gocache GOOS=linux go test -c ./daemon -o /tmp/cc-connect-daemon-linux.test
  • git diff --check

Note: Linux systemd tests were compile-verified from macOS; the Linux test binary cannot be executed on macOS.

Automated tests added in this PR

  • TestLoad_DataDirAbsoluteWhenExplicitlyRelative in config/config_test.go - asserts explicit relative data_dir is returned as an absolute path.
  • TestLoad_DataDirAbsoluteWhenHOMEEmpty in config/config_test.go - asserts empty data_dir still becomes absolute when HOME is unavailable.
  • TestBuildUnit_IncludesHOME in daemon/systemd_test.go - asserts systemd units include captured HOME.
  • TestBuildUnit_OmitsHOMEWhenEmpty in daemon/systemd_test.go - asserts systemd units omit empty HOME.
  • TestBuildUnit_EnvExtraHOMEDoesNotOverrideTemplateHOME in daemon/systemd_test.go - asserts EnvExtra["HOME"] cannot override template-owned HOME.
  • TestBuildPlist_IncludesHOME in daemon/launchd_test.go - asserts launchd plists include captured HOME.
  • TestBuildPlist_OmitsHOMEWhenEmpty in daemon/launchd_test.go - asserts launchd plists omit empty HOME.
  • TestBuildPlist_EnvExtraHOMEDoesNotOverrideTemplateHOME in daemon/launchd_test.go - asserts EnvExtra["HOME"] cannot override template-owned HOME.

For bug fixes only - regression test

  • Regression test name: TestLoad_DataDirAbsoluteWhenHOMEEmpty
  • Manual verification this test catches the regression:
    • Reverted the fix locally; the regression test failed as expected.

Critical User Journeys (CUJ) impact

  • No CUJ touched (daemon/config path resolution only)
  • A - basic conversation
  • B - session lifecycle (/new /switch /list /history etc.)
  • C - agent execution control (/mode /cancel /stop permissions)
  • D - security & permissions (allow_from admin_from banned_words rate limits)
  • E - scheduled tasks (/cron /timer)
  • F - config switching (/lang /provider /model reload)
  • G - error handling & robustness (LLM failure, ws reconnect, agent crash)
  • H - multi-platform / multi-project isolation
  • I - UI rendering correctness (cards, streaming, display modes)

Manual / user-visible behavior change

When running as a systemd/launchd service, cc-connect now preserves a usable HOME for the daemon and agent subprocesses. Users should no longer see system prompt file lookup failures caused by .cc-connect being resolved under work_dir.

Checklist (reviewer will verify)

  • go build ./... passes
  • go test ./... passes (with -race if touching concurrency)
  • AGENTS.md Pre-Commit Checklist items are satisfied
  • No new hardcoded platform/agent names in core/
  • i18n strings have all-language translations (if any new user-facing text)
  • No secrets / credentials in source

Related

  • Issue:
  • Related PR:

zhuanlan.42 added 2 commits July 8, 2026 20:58
The Claude Code agent subprocess `cd`s into the project's work_dir before
resolving the system-prompt file path. When `data_dir` is empty or set to
a relative value in config.toml, and the service manager (systemd system
units, launchd LaunchAgents bootstrapped via gui/...) does not propagate
HOME, os.UserHomeDir() fails and config.Load() would fall back to a bare
relative ".cc-connect", which the child then resolves against work_dir
instead of the supervisor's home. The user-visible failure is:

    Error: Append system prompt file not found:
      <work_dir>/.cc-connect/agent-prompts/cc-connect-system.md

Fix has three parts and applies regardless of whether HOME is /root,
/home/app, or unset:

1. config.Load unconditionally absolutizes cfg.DataDir via filepath.Abs
   after all fallbacks, so a relative value can never leak to child
   processes. Also emits a slog.Warn when the fallback path is taken.

2. daemon.Config gains a HomeDir field; Resolve() captures os.UserHomeDir
   at install time so the value survives into the service definition
   even when the service runtime lacks HOME.

3. systemd.buildUnit emits `Environment="HOME=..."`, and launchd.buildPlist
   emits `<key>HOME</key><string>...</string>`. HOME is added to
   launchd's templateOwnedEnvKeys so an EnvExtra HOME cannot override
   the captured value.

Regression tests:
- config: TestLoad_DataDirAbsoluteWhenExplicitlyRelative,
  TestLoad_DataDirAbsoluteWhenHOMEEmpty
- systemd: TestBuildUnit_IncludesHOME, TestBuildUnit_OmitsHOMEWhenEmpty
- launchd: TestBuildPlist_IncludesHOME, TestBuildPlist_OmitsHOMEWhenEmpty,
  TestBuildPlist_EnvExtraHOMEDoesNotOverrideTemplateHOME

config.example.toml gains a note recommending an absolute data_dir under
systemd/launchd.
@Longchuanzheng Longchuanzheng requested a review from chenhg5 as a code owner July 8, 2026 14:01
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