Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: This PR was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!
Why
Closing a GoatDB instance was fragile. The
readyboolean could not represent closing, closed, or failed states. Callers could keep using a closed database, and operations failed deep inside code with unclear errors.Teardown ran on every
close()call. Concurrent calls ran multiple teardown sequences in parallel. A failed teardown was reported as success, and dirty items could be lost. A stalled sync peer blockedclose()until the network timeout fired.What changed
State machine replaces the ready boolean
GoatDB.stateis now an explicit state machine:bootingreadyclosingclosedfailedisOpenderives from state and reports whether the instance accepts work.state === 'ready'replacesreadyas the readiness check. Each transition emits aLifecycleChangedevent.Public operations fail closed
Public data operations reject with
serviceUnavailablewhen the instance is not open. Guards run at the API boundary inGoatDBandManagedItem:count(),keys(),getTrustPool(),logout()set(),delete(), and mutator setters on itemsErrors surface at the boundary instead of inside a repository.
close() is single-flight and idempotent
close()returns the same promise for every concurrent call. A single teardown runs. Repeated calls are safe.Failed teardown is retryable
A failed
close()moves the instance tofailedand rejects. A laterclose()retries the remaining teardown. On success, the instance settles atclosed. No partial teardown is reported as success.Sync aborts on close
SyncSchedulercarries anAbortController.close()aborts the in-flight batch-sync fetch, so teardown does not wait on a stalled peer. Abort is cancellation, not a peer fault, so it is not logged as a sync error.RepoClientsuppressesSerializeErrorlogs while closed.Dirty items persist during close
Teardown commits dirty items through
_commitForClose()and_acquireRepoForClose(). These internal paths bypass the public guards during theclosingstate. If a commit fails, the item stays dirty for a retry, and the trust pool is preserved.React hook maps lifecycle events
useDBReady()subscribes toLifecycleChangedthroughuseSyncExternalStore. It mapsclosedandfailedstates toerror, andreadyto a ready signal.Tests
New coverage exercises state transitions, fail-closed guards, single-flight close, retryable failed teardown, sync abort, and React hook behavior. Failure injection helpers simulate commit and file-close failures.
Impact
readymigrate tostate === 'ready'.serviceUnavailableinstead of failing silently.close()is deterministic: one teardown, no leaks, no hangs on stalled peers.Fixes #94