8000 fix(server): say once that a lost data directory needs a restart by lexfrei · Pull Request #321 · aenix-io/aeman · GitHub
[go: up one dir, main page]

Skip to content

fix(server): say once that a lost data directory needs a restart - #321

Merged
kvaps merged 2 commits into
mainfrom
fix/gitstore-lost-store-signal
Nov 22, 2026
Merged

kvaps merged 2 commits into
mainfrom
fix/gitstore-lost-store-signal

Conversation

@lexfrei
@lexfrei lexfrei commented Nov 15, 2026 •
Copy link
Copy Markdown
Contributor

When a running process loses its data directory, it now says once that it needs a restart, refuses every write at the door, and answers 503 on its health endpoints so a probe can restart it. It also stops writing into a directory another process may own.

Before this, the operator got no clear signal. The sync loop logged the same warning every 15s until restart, and /api/healthz still said ok. Now the first check that finds the condition logs one ERROR naming the domain and "restart required", and the state is kept until restart. The sync tick is skipped with no warning, and pushes, commits, maintenance and deepening are refused.

Writes are refused before they are applied. A write under /api/v1 gets 503 with a new problem code, storeLost, and an MCP tool call gets a tool error with the same reason. Before, the write was accepted with a 2xx and rolled back later in the drain, so an MCP agent was told "ok" for a change that was lost. The MCP refusal covers read tools too: an agent sees no banner, and the board it would read no longer syncs. REST reads keep working, so the board stays readable in the browser.

/api/healthz and the /healthz of the aeman mcp --listen daemon answer degraded with a storeLost field, and with HTTP 503. A restart is the only cure for this state, and a liveness probe acts on the status code. An unpushed backlog stays 200, since restarting would not make the remote accept the push. aeman service status reads the 503 body, so it reports the daemon as degraded rather than silent. Both endpoints answer without a login, so the reason names no path; the path is in the ERROR line.

The process does not exit on its own. The Helm chart's liveness probe reads /api/healthz and restarts the pod. A process without a supervisor keeps explaining why it refuses instead of vanishing. The trade-off is the unit aeman service install writes: it restarts only a process that exits, so a daemon in this state is restarted by hand. docs/api.md says so.

The board's banner used to read any degraded as "saved locally but not pushed", which was false here. With storeLost it now says the data directory is lost and changes are refused until a restart.

On a board of several repositories this covers every domain. The sync stops for all of them, so a write to a surviving domain would otherwise sit unpushed until the restart. A commit made before the state latched is pushed after the restart.

The second problem is a race around the lock. The data-directory lock lives inside the directory it guards, so removing the directory removes the claim. A second serve or mcp can then start, make a fresh lock file, clone into the same path and take the lock. The 8000 first process sees a HEAD again and writes into the newcomer's clone, and a test reproduces exactly that. The holder now compares the file it holds with the file at <data>/lock (os.SameFile). A different file, no file, or a data path that is now a file counts as a lost directory. A stat that fails without proving it (EACCES) does not.

On Windows the check is off. The lock file is opened without sharing, so the directory cannot be removed while the lock is held. That follows from the share-mode-0 open in datalock_windows.go and was not run on Windows. Renaming the data directory away while the lock is held is not covered. The lock stays inside the data directory, because that directory may be a mount root.

Every commit, fetch and deepen refuses through one place. The check sits in gitstore.Repo.CheckStore behind a new optional SetStoreGuard, which also covers writes that skip the queue (team create, synchronous delete, ghost sweep). Push and repack do not go through CheckStore, so they check before they start. The sync tick checks before it looks at any board, so a daemon no client has used yet still notices. It costs a few stat calls.

Rows G90 and G91 are in the behavior matrix. docs/api.md documents storeLost, the 503 answers, the MCP tool error and the limit of the lock, and api/openapi.yaml names the new problem code.

Testing: go test ./... passes, go test -race passes on internal/server, pkg/gitstore and cmd/aeman, golangci-lint reports nothing, and in web/ npm run typecheck and npm test pass. Each new test failed before its fix, and removing each guard makes a named test fail.

Follow-ups, not in this PR:

  • The chart's readiness probe reads the same /api/healthz, so in this state the pod leaves the Service endpoints at once and the new banner is rarely seen on a Helm install before liveness restarts the pod. A separate readiness path is a chart change of its own.
  • docs/design/git-backend.md still says a second serve or mcp on the same directory is refused at start. That was already untrue before this PR: removing the directory with its lock file lets a second process start. It needs one sentence pointing at G91.
  • gitSync.storeLost stats the lock file twice per check, once directly and once through each repository's guard. This is harmless but redundant.
  • With --sync-interval 0 writes are still refused, but the ERROR line appears only when health, maintenance or a request first checks the store.
  • TestALostDomainStopsWritesToTheOthers checks only that the head did not move. It could also assert that the refused write's error says "restart required".
  • Without --data the clones live in the user cache directory, and a cache cleaner that removes the lock file now stops the process until restart. That is the intended answer to a lost claim, but the docs could recommend an explicit --data for a long-running service.

@lexfrei
lexfrei marked this pull request as ready for review November 16, 2026 09:13
@lexfrei
lexfrei commented Nov 16, 2026 •
Copy link
Copy Markdown
Contributor Author

@kvaps this one is ready for review. #320 is merged, so I rebased it onto main. A running process that lost its data directory now logs one ERROR saying a restart is required, reports degraded health without exposing the path, and stops writing, also when a second process took over the lock. Opus reviewed the current commit twice with no findings, and CI is green.

@lexfrei
lexfrei changed the base branch from fix/gitstore-lost-clone to main November 16, 2026 09:22
@lexfrei
lexfrei force-pushed the fix/gitstore-lost-store-signal branch 3 times, most recently from 45c2398 to b110f70 Compare 8000 November 16, 2026 09:36
A clone removed under a running process is refused with ErrStoreLost,
but the sync loop kept retrying it and logging the same warning every
tick, and both health endpoints still said ok. The operator had no
clear signal that only a restart recovers.

Worse, the data-directory lock lives inside the directory it guards.
Removing the directory removes the claim: a second serve or mcp starts,
makes a fresh lock file, clones into the same path, and the first
process sees a HEAD again and writes into the newcomer's clone.

A lock file that is no longer the file this process holds, or gone, is
now the same condition as a clone without HEAD. gitstore.Repo gains an
optional store guard that CheckStore consults, and the server wires the
lock into it, so commit, fetch and deepen refuse, including writes that
bypass the write queue. The push and the repack do not pass through
CheckStore and check before they start; the sync tick checks before it
looks at any board. A stat that fails without proving the loss (EACCES)
is not treated as one. On Windows the lock is held without sharing, so
the directory cannot be removed while held and the check is off.

The first hit logs one ERROR naming the domain or the lock file and
"restart required", the state is kept until restart, the tick stops
warning, and /api/healthz and the mcp daemon's /healthz report degraded
with storeLost carrying a fixed reason. Both answer without a login, so
the reason names no path; the path is in the ERROR line. The latched
state is part of every repository's guard: the sync stops for every
domain of the board, so a write to a surviving domain is refused too
instead of sitting unpushed until the restart.

A write that arrives while the state holds is refused at the request
door, before the cache takes it: a 2xx that the drain later rolls back
tells the caller, and an MCP agent above all, that a change was kept
when it was not. /api/v1 answers 503 with the new storeLost problem
code, and an MCP tool call returns a tool error with the same reason.
The MCP refusal covers reads as well: an agent sees no banner, and the
board it would read no longer syncs.

Both health endpoints answer 503 in this state, with the same body. It
is the one degraded state a restart cures, and a liveness probe acts on
the status code; an unpushed backlog stays 200, since restarting would
not make the remote accept the push. aeman service status still reads
the 503 body. The process does not exit: a probe restarts a pod, and a
process without a supervisor keeps explaining why it refuses instead of
vanishing.

The board's banner read any degraded health as "saved locally but not
pushed". With storeLost it now says the data directory is lost and that
changes are refused until a restart.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>

# Conflicts:
#	docs/api.md
#	docs/design/behavior-matrix.md
#	pkg/gitstore/lostclone_test.go
@kvaps
kvaps merged commit 57c9718 into main Nov 22, 2026
2 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

0