Skip to content

fix(consolidation): the lock honours SURREAL_MEMORY_DIR like the rest of the data dir - #209

Merged
acidkill merged 1 commit into
acidkill:mainfrom
RobertSigmundsson:fix/consolidation-lock-honours-memory-dir
Sep 7, 2026
Merged

acidkill merged 1 commit into
acidkill:mainfrom
RobertSigmundsson:fix/consolidation-lock-honours-memory-dir

Conversation

@RobertSigmundsson

Copy link
Copy Markdown
Contributor

Summary

  • _lock_path now honours SURREAL_MEMORY_DIR, so the consolidation lock moves with the data directory like everything else in the package.
  • The lock test class gets a per-test directory and a test that asserts the redirection rather than assuming it.

Why

_lock_path derived its directory from Path.home() alone. Everything else already reads the variable — cli/config.get_default_data_dir and cli/update_check._get_cache_path both do — which made the consolidation lock the one piece of state a redirected data dir could not move. A run against a throwaway brain still created and deleted files in the operator's real ~/.surrealmemory.

Measured by asking _lock_path where it would put the file, with the variable set:

main:   SURREAL_MEMORY_DIR=/tmp/throwaway  ->  ~/.surrealmemory/consolidation-<brain>.lock
here:   SURREAL_MEMORY_DIR=/tmp/throwaway  ->  /tmp/throwaway/consolidation-<brain>.lock

A trade-off, named rather than discovered

Two processes pointed at different SURREAL_MEMORY_DIR values now take different locks and no longer exclude one another. That is the intended reading — they are operating on separate data directories — but it does mean the lock is per data directory rather than per machine. If you would rather it stayed machine-wide, this is the wrong shape and I would rather know now.

Changes

  • utils/consolidation_lock.py: read SURREAL_MEMORY_DIR, falling back to Path.home()/".surrealmemory" as before, with the trade-off recorded in the docstring.

  • tests/unit/test_multi_agent.py: the autouse fixture points SURREAL_MEMORY_DIR at a per-test directory, and test_lock_lives_in_the_configured_data_dir asserts the redirection. The failure mode is silent — the lock simply reappears in the real home — so it is worth an explicit assertion.

  • docs/reference/config.md: regenerated with scripts/gen_config_docs.py, since that file is generated from these defaults and the Docs Freshness job checks it.

A correction to that class's own docstring

The docstring says these tests race on one shared lock file under xdist, and that xdist_group pins the class to a single worker to prevent it. That was true when #74 wrote it, and is no longer true of the suite as it stands — which is worth stating carefully, because the note was not wrong, it was overtaken.

#121 later added a session-scoped $HOME redirect to conftest for unrelated reasons. That gives each worker its own home directory, and therefore its own lock file, so the race stopped being reachable and nothing updated the note. Checked directly: with $HOME redirected the lock lands under the fake home on main too, before any of this change.

What the redirect does not do is make the intent explicit. A test relying on $HOME being someone else's problem is one refactor away from writing to a real home again, which is why the fixture now says so in its own terms. xdist_group is kept as a guard against shared state being reintroduced, not as a live fix.

Test plan

  • pytest tests/unit/test_multi_agent.py — 16 passed.
  • With utils/consolidation_lock.py reverted to main and the tests kept, exactly one fails: test_lock_lives_in_the_configured_data_dir. The other fifteen pass either way, which is the point — this change is meant to move a file, not alter locking behaviour.
  • pytest tests/ -m "not stress" -n 4 — 7284 passed, 48 skipped, 1 xfailed, which is main's 7283 plus the single test added here. Two tests in tests/unit/test_dashboard_brains_scope.py fail on this branch and on main alike: they want a live database and collide with one another under -n. Both pass when that file is run on its own.
  • ruff check src/ tests/ clean; ruff format --check src/ tests/ reports 739 files already formatted.
  • mypy src/ --ignore-missing-imports — success, no issues found in 354 source files.
  • Coverage under the CI gate: 72.37%, against 72.36% on main.
  • CHANGELOG.md untouched — left to the release entry, as with fix(storage): bind datetimes in time comparisons so they select by value #191fix(memory): refresh content-derived fields on compress, restore, and refine #193.

Verified by

@RobertSigmundsson

… of the data dir

_lock_path derived its directory from Path.home() alone, so the consolidation
lock was the one piece of state a redirected data dir could not move.
Everything else in the package already honours the variable —
cli/config.get_default_data_dir and cli/update_check._get_cache_path both read
it.

Measured on SurrealDB-independent ground, by asking _lock_path where it would
put the file:

    SURREAL_MEMORY_DIR=/tmp/throwaway  ->  ~/.surrealmemory/consolidation-<brain>.lock

A run against a throwaway brain therefore created and deleted files in the
operator's real ~/.surrealmemory. With the change the same call returns
/tmp/throwaway/consolidation-<brain>.lock.

The trade-off is worth naming rather than discovering: two processes pointed
at different SURREAL_MEMORY_DIR values now take different locks and no longer
exclude one another. That is the intended reading — they are operating on
separate data directories — but the lock is now per data dir rather than per
machine.

The test class gets a per-test directory and a test that asserts the
redirection rather than assuming it, since the failure mode is silent: the
lock simply reappears in the real home.

Its docstring is brought up to date at the same time, and the history is worth
stating rather than flattening. When acidkill#74 added xdist_group, the note it left
was accurate: these tests did share one real lock file and did race on it.
acidkill#121 then added a session-scoped $HOME redirect to conftest for unrelated
reasons, which gives every worker its own home and therefore its own lock
file. The race has not been reachable since, and nothing updated the note. So
the isolation was already there — implicitly, resting on a fixture that exists
for another purpose, which is the problem this fixture addresses. xdist_group
is kept as a guard against shared state being reintroduced.

@acidkill acidkill left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed the full diff. The consolidation lock now honours SURREAL_MEMORY_DIR like every other piece of data-dir state, and the tests assert the redirection instead of assuming it.

@acidkill
acidkill merged commit cf9add5 into acidkill:main Sep 7, 2026
9 checks passed
@acidkill acidkill mentioned this pull request Sep 7, 2026
4 tasks
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