Skip to content

fix(app): honor DEEPCHAT_E2E_USER_DATA_DIR before startup#1995

Merged
zerob13 merged 1 commit into
ThinkInAIXYZ:devfrom
Dorence:fix/e2e-user-data-dir
Jul 18, 2026
Merged

fix(app): honor DEEPCHAT_E2E_USER_DATA_DIR before startup#1995
zerob13 merged 1 commit into
ThinkInAIXYZ:devfrom
Dorence:fix/e2e-user-data-dir

Conversation

@Dorence

@Dorence Dorence commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Description

I want to launch Deepchat with a dedicated user profile in Agent sandbox, but fail. I use DEEPCHAT_E2E_USER_DATA_DIR to isolate development profile from the installed application. However, two early calls to app.getPath('userData'), before the app starts, resolve to the installed profile path.

  1. logs/main.log from src/shared/logger.ts
18:24:24.675 › Main process starting, checking for deeplink...
18:24:24.677 › electron-log.transports.file: Can't write to /Users/***/Library/Application Support/DeepChat/logs/main.log Error: EPERM: operation not permitted, open '/Users/***/Library/Application Support/DeepChat/logs/main.log'
    at Object.openSync (node:fs:561:18)
    at Object.func [as openSync] (node:electron/js2c/node_init:2:2625)
    at Object.writeFileSync (node:fs:2428:35)
    ...
18:24:24.683 › Startup arguments received { argc: 2 }
18:24:24.683 › No startup deeplink detected during initialization
sandbox initialization failed: Operation not permitted
  1. provider-db/meta.json from src/main/provider/providerDbLoader.ts
18:28:51.381 › main: Application startup
sandbox initialization failed: Operation not permitted
sandbox initialization failed: Operation not permitted
Failed to initialize sandbox.Failed to initialize sandbox.18:28:51.543 › DatabaseInitializer: Starting database initialization
Sandbox Error: hit restricted
  Not allow operate files: /Users/***/Library/Application Support/DeepChat/provider-db/meta.json, ...

Risks

  • ElectronStore<ToolPolicySettings> in src/main/plugin/toolPolicyStore.ts is also a suspicious early path resolver. (But DeepChat can run in Agent sandbox after those two fixes, so I don't fix this one.)

Summary by CodeRabbit

  • Documentation

    • Added documentation describing profile-specific data directories, path constraints, logging, caching, and validation scenarios.
  • Bug Fixes

    • Ensured logs and provider cache files use the selected profile directory.
    • Whitespace-only profile directory settings now fall back to the default location.
  • Tests

    • Added coverage verifying provider cache files are created in the configured profile directory.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change documents profile-scoped Electron user-data behavior and applies the trimmed DEEPCHAT_E2E_USER_DATA_DIR override to provider DB caching, logging, E2E fixture resolution, and related validation.

Changes

Profile-scoped user data

Layer / File(s) Summary
Profile path resolution and validation
docs/architecture/user-data-profile-path/spec.md, src/main/provider/providerDbLoader.ts, src/shared/logger.ts, test/e2e/fixtures/electronApp.ts, test/main/provider/providerDbLoader.test.ts
The architecture spec defines default and explicit profile paths, whitespace handling, startup constraints, and validation steps. Logging and provider caching use the trimmed override with Electron fallback, while fixtures and tests verify the selected directory behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: zerob13

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main change: honoring DEEPCHAT_E2E_USER_DATA_DIR before startup.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
test/main/provider/providerDbLoader.test.ts (1)

109-123: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Ensure environment variables are restored if assertions fail.

If an expect assertion fails, the test will throw an error and the cleanup code (lines 117-121) will not execute, potentially polluting the environment for subsequent tests in this file.

Since you are using Vitest, consider using vi.stubEnv which safely handles environment variable mocking and can be easily cleaned up, or wrap the assertions in a try...finally block.

♻️ Proposed refactor using `vi.stubEnv`
-  it('resolves DEEPCHAT_E2E_USER_DATA_DIR as provider-db base directory', async () => {
-    const oldDir = process.env.DEEPCHAT_E2E_USER_DATA_DIR
+  it('resolves DEEPCHAT_E2E_USER_DATA_DIR as provider-db base directory', async () => {
     const e2eUserDataRoot = path.join(tempRoot, 'e2e-user-data')
-    process.env.DEEPCHAT_E2E_USER_DATA_DIR = e2eUserDataRoot
+    vi.stubEnv('DEEPCHAT_E2E_USER_DATA_DIR', e2eUserDataRoot)
     const ProviderDbLoader = await importLoader()
     new ProviderDbLoader()
     expect(fs.existsSync(path.join(e2eUserDataRoot, 'provider-db'))).toBe(true)
     expect(fs.existsSync(getCacheDir())).toBe(false)
-    if (oldDir === undefined) {
-      delete process.env.DEEPCHAT_E2E_USER_DATA_DIR
-    } else {
-      process.env.DEEPCHAT_E2E_USER_DATA_DIR = oldDir
-    }
+    vi.unstubAllEnvs()
   })
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/main/provider/providerDbLoader.test.ts` around lines 109 - 123, Update
the test “resolves DEEPCHAT_E2E_USER_DATA_DIR as provider-db base directory” to
guarantee environment cleanup when assertions fail. Use Vitest’s vi.stubEnv with
the appropriate restoration mechanism, or move the existing environment
restoration into a finally block surrounding the setup and assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@test/main/provider/providerDbLoader.test.ts`:
- Around line 109-123: Update the test “resolves DEEPCHAT_E2E_USER_DATA_DIR as
provider-db base directory” to guarantee environment cleanup when assertions
fail. Use Vitest’s vi.stubEnv with the appropriate restoration mechanism, or
move the existing environment restoration into a finally block surrounding the
setup and assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ff86753d-9575-4337-b7cd-b8170d8f225d

📥 Commits

Reviewing files that changed from the base of the PR and between e84428b and 79c4bce.

📒 Files selected for processing (5)
  • docs/architecture/user-data-profile-path/spec.md
  • src/main/provider/providerDbLoader.ts
  • src/shared/logger.ts
  • test/e2e/fixtures/electronApp.ts
  • test/main/provider/providerDbLoader.test.ts

@zerob13
zerob13 merged commit 500fb3c into ThinkInAIXYZ:dev Jul 18, 2026
4 checks 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