Skip to content

fix(fs-watcher): load agentmemory env file - #319

Open
EvanWangZ wants to merge 2 commits into
rohitg00:mainfrom
EvanWangZ:fix/fs-watcher-env-file
Open

EvanWangZ wants to merge 2 commits into
rohitg00:mainfrom
EvanWangZ:fix/fs-watcher-env-file

Conversation

@EvanWangZ

@EvanWangZ EvanWangZ commented May 13, 2026 •

Copy link
Copy Markdown

Summary

  • load ~/.agentmemory/.env and $XDG_CONFIG_HOME/agentmemory/.env in @agentmemory/fs-watcher
  • keep explicit process environment values higher priority than file values
  • document the fallback and add tests for file loading and env override behavior

This addresses the filesystem watcher part of #298, where a watcher running under a process manager/container could fall back to localhost even though the shared server URL lives in agentmemory's documented env file.

Conflict check

  • Current open PRs do not touch integrations/filesystem-watcher/* or test/fs-watcher.test.ts.

Tests

  • npm test -- test/fs-watcher.test.ts
  • node --check integrations/filesystem-watcher/watcher.mjs
  • git diff --check
  • npm run build
  • npm test

Summary by CodeRabbit

  • Documentation

    • Clarified how configuration is loaded from .env files for the filesystem watcher.
  • New Features

    • Filesystem watcher can load configuration from .env files in user config locations, with explicit process environment variables taking precedence.
  • Tests

    • Added tests covering .env parsing, quoting/comments handling, XDG_HOME support, and environment-variable precedence.

Review Change Stack

Signed-off-by: Zavian <36817799+Zavianx@users.noreply.github.com>
@vercel

vercel Bot commented May 13, 2026

Copy link
Copy Markdown

@Zavianx is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 41950be1-89d3-4423-bee4-bca786a9e2b7

📥 Commits

Reviewing files that changed from the base of the PR and between b8ca80a and 1a1f409.

📒 Files selected for processing (2)
  • integrations/filesystem-watcher/README.md
  • test/fs-watcher.test.ts
✅ Files skipped from review due to trivial changes (1)
  • integrations/filesystem-watcher/README.md

📝 Walkthrough

Walkthrough

Adds support for loading watcher configuration from ~/.agentmemory/.env and $XDG_CONFIG_HOME/agentmemory/.env; implements parsing/loading helpers, merges file-sourced values with process env in configFromEnv (process env wins), expands tests, and updates README.

Changes

Environment file configuration loading

Layer / File(s) Summary
Env-file parsing and loading helpers
integrations/filesystem-watcher/watcher.mjs
parseEnvFile parses key/value pairs (comments, blank lines, basic quoting), loadAgentMemoryEnv best-effort loads .env from ~/.agentmemory/.env and $XDG_CONFIG_HOME/agentmemory/.env. fs import reformatted.
ConfigFromEnv env merging
integrations/filesystem-watcher/watcher.mjs
configFromEnv merges file-loaded env values with the provided env (file first, explicit env overrides) and derives roots, baseUrl, secret, project, sessionId, ignorePatterns, and allowBinary.
Test coverage for env file loading
test/fs-watcher.test.ts
Adds Vitest cases to verify loading from ~/.agentmemory/.env, parsing edge cases (quoting, inline comments, open quotes, empty/malformed lines), XDG_CONFIG_HOME precedence, and explicit AGENTMEMORY_* overrides. Temporary HOME/XDG dirs are used and cleaned up.
Documentation of env file loading
integrations/filesystem-watcher/README.md
README documents the .env loading order and that process environment variables override file values.

Possibly related PRs

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 In burrows of code I softly comb,
I read .env from home and XDG home,
I stitch their keys with process light,
so watcher wakes configured right,
and hops along the filesystem loam.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(fs-watcher): load agentmemory env file' accurately summarizes the main change: enabling the filesystem watcher to load environment configuration from agentmemory env files, which directly addresses the PR's primary objective.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@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 (3)
integrations/filesystem-watcher/watcher.mjs (3)

61-80: ⚡ Quick win

Document the precedence between config file locations.

When both ~/.agentmemory/.env and $XDG_CONFIG_HOME/agentmemory/.env exist, the implementation gives precedence to the XDG location (line 73's Object.assign merges HOME first, then XDG, so XDG values override). This follows the typical XDG pattern (XDG as primary, ~/.appname as legacy), but it's not documented in the README.

Users might expect their home directory config to be most specific, or may not be aware that values can be overridden by the XDG location.

Verify the intended precedence order and update the README to clarify which file takes precedence when both exist. Consider adding this to the configuration table:

The watcher loads configuration from these locations in order (later sources override earlier):
1. `~/.agentmemory/.env`
2. `$XDG_CONFIG_HOME/agentmemory/.env`
3. Process environment variables
🤖 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 `@integrations/filesystem-watcher/watcher.mjs` around lines 61 - 80, The
loadAgentMemoryEnv function builds candidates with HOME first then XDG and
merges with Object.assign so XDG values currently override HOME; verify this is
the intended precedence and update the README configuration section to clearly
document the load/override order (e.g., list ~/.agentmemory/.env then
$XDG_CONFIG_HOME/agentmemory/.env, with later sources overriding earlier, and
that process env overrides both), referencing loadAgentMemoryEnv and the
candidates/Object.assign behavior so users know XDG takes precedence when both
files exist.

38-59: ⚡ Quick win

Consider adding test coverage for env file edge cases.

The parseEnvFile implementation handles quotes, inline comments ( # pattern), and malformed lines gracefully, but the test file only exercises basic KEY=value pairs. Consider adding tests for:

  • Quoted values: KEY="value with spaces"
  • Inline comments: KEY=value # comment
  • Missing closing quotes: KEY="incomplete
  • Empty values: KEY=
🤖 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 `@integrations/filesystem-watcher/watcher.mjs` around lines 38 - 59, Add unit
tests covering edge cases for parseEnvFile: create tests that call parseEnvFile
with content including quoted values (e.g., KEY="value with spaces") and assert
the value preserves spaces without quotes; inline comments (e.g., KEY=value #
comment) and assert the comment is stripped; a missing closing quote (e.g.,
KEY="incomplete) and assert the parser returns the remainder after the opening
quote as the value; and empty values (e.g., KEY=) and assert the value is an
empty string. Target the parseEnvFile function in your test suite and include
assertions for each of these scenarios to match the existing parsing behavior.

61-80: ⚡ Quick win

Add test coverage for XDG_CONFIG_HOME loading.

The test suite validates loading from ~/.agentmemory/.env (lines 195-220 in test file) but doesn't cover $XDG_CONFIG_HOME/agentmemory/.env. Consider adding a test case that:

  • Sets both HOME and XDG_CONFIG_HOME
  • Places different values in both locations
  • Verifies XDG values take precedence per the implementation's Object.assign order
🤖 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 `@integrations/filesystem-watcher/watcher.mjs` around lines 61 - 80, Add a unit
test for loadAgentMemoryEnv that sets both HOME and XDG_CONFIG_HOME environment
variables, creates two temporary env files (~/.agentmemory/.env and
$XDG_CONFIG_HOME/agentmemory/.env) with different key values, invokes
loadAgentMemoryEnv (or the module that calls it) and asserts the returned object
contains the values from the XDG file overriding the HOME file per Object.assign
order; reference the loadAgentMemoryEnv function and the candidates array when
locating where to call it, and ensure cleanup of temp files and restored env
vars after the test.
🤖 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 `@integrations/filesystem-watcher/watcher.mjs`:
- Around line 61-80: The loadAgentMemoryEnv function builds candidates with HOME
first then XDG and merges with Object.assign so XDG values currently override
HOME; verify this is the intended precedence and update the README configuration
section to clearly document the load/override order (e.g., list
~/.agentmemory/.env then $XDG_CONFIG_HOME/agentmemory/.env, with later sources
overriding earlier, and that process env overrides both), referencing
loadAgentMemoryEnv and the candidates/Object.assign behavior so users know XDG
takes precedence when both files exist.
- Around line 38-59: Add unit tests covering edge cases for parseEnvFile: create
tests that call parseEnvFile with content including quoted values (e.g.,
KEY="value with spaces") and assert the value preserves spaces without quotes;
inline comments (e.g., KEY=value # comment) and assert the comment is stripped;
a missing closing quote (e.g., KEY="incomplete) and assert the parser returns
the remainder after the opening quote as the value; and empty values (e.g.,
KEY=) and assert the value is an empty string. Target the parseEnvFile function
in your test suite and include assertions for each of these scenarios to match
the existing parsing behavior.
- Around line 61-80: Add a unit test for loadAgentMemoryEnv that sets both HOME
and XDG_CONFIG_HOME environment variables, creates two temporary env files
(~/.agentmemory/.env and $XDG_CONFIG_HOME/agentmemory/.env) with different key
values, invokes loadAgentMemoryEnv (or the module that calls it) and asserts the
returned object contains the values from the XDG file overriding the HOME file
per Object.assign order; reference the loadAgentMemoryEnv function and the
candidates array when locating where to call it, and ensure cleanup of temp
files and restored env vars after the test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fb56847d-9954-4ff6-ae15-f3bd47b125d0

📥 Commits

Reviewing files that changed from the base of the PR and between 25dddc4 and b8ca80a.

📒 Files selected for processing (3)
  • integrations/filesystem-watcher/README.md
  • integrations/filesystem-watcher/watcher.mjs
  • test/fs-watcher.test.ts

Signed-off-by: Zavian <36817799+Zavianx@users.noreply.github.com>
possiblyneal pushed a commit to possiblyneal/agentmemory-sqlite that referenced this pull request Oct 4, 2026
The watcher built its config from process.env alone, so settings in the
env file were ignored. bin.mjs now merges the file under process.env,
which wins, matching the daemon. Fixes the diagnosis in rohitg00#319.

Generated-By: Claude Sonnet 5.5

This branch has not been deployed

No deployments
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