diff --git a/pkgs/mcpx/docs/spec/conformance-matrix.md b/pkgs/mcpx/docs/spec/conformance-matrix.md
index b42bf2f37b..247ec90105 100644
--- a/pkgs/mcpx/docs/spec/conformance-matrix.md
+++ b/pkgs/mcpx/docs/spec/conformance-matrix.md
@@ -2,7 +2,7 @@
Generated by `internal/conformance` (`MCPX_UPDATE_MATRIX=1 go test ./internal/conformance -run TestMatrix`); do not edit.
-Generated from inputs `32a7633a66634a16` (sha256 of docs/spec/requirements.md, internal/conformance/overrides/*.json, internal/conformance/coverage*.go). `TestMatrix` regenerates this file and fails when it differs, so it describes the tree it is committed in.
+Generated from inputs `11420dd23ca5c9a9` (sha256 of docs/spec/requirements.md, internal/conformance/overrides/*.json, internal/conformance/coverage*.go). `TestMatrix` regenerates this file and fails when it differs, so it describes the tree it is committed in.
Every normative requirement of every revision (catalogue: `docs/spec/requirements.md`), for mcpx as a server and as a client. A requirement is *tested* when a named test verifies it in that revision, *n/a* when it does not bind mcpx in that role, and a *gap* when it binds mcpx and mcpx does not meet it yet, with the issue that tracks it. Run exactly the covering tests with `go run ./internal/conformance/cmd/covering`.
@@ -46,7 +46,7 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement
| 2025-06-18 | client | SHOULD NOT | 2 | 1 | 1 | 0 | 3 |
| 2025-06-18 | client | MAY | 15 | 13 | 2 | 0 | 20 |
| 2025-06-18 | client | other | 10 | 8 | 2 | 0 | 8 |
-| 2025-11-25 | server | MUST | 115 | 96 | 19 | 0 | 65 |
+| 2025-11-25 | server | MUST | 115 | 97 | 18 | 0 | 65 |
| 2025-11-25 | server | MUST NOT | 22 | 18 | 4 | 0 | 13 |
| 2025-11-25 | server | SHOULD | 78 | 61 | 17 | 0 | 81 |
| 2025-11-25 | server | SHOULD NOT | 11 | 9 | 2 | 0 | 3 |
@@ -58,7 +58,7 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement
| 2025-11-25 | client | SHOULD NOT | 5 | 4 | 1 | 0 | 9 |
| 2025-11-25 | client | MAY | 20 | 17 | 3 | 0 | 35 |
| 2025-11-25 | client | other | 13 | 11 | 2 | 0 | 8 |
-| 2026-07-28 | server | MUST | 128 | 107 | 21 | 0 | 96 |
+| 2026-07-28 | server | MUST | 128 | 108 | 20 | 0 | 96 |
| 2026-07-28 | server | MUST NOT | 44 | 41 | 3 | 0 | 20 |
| 2026-07-28 | server | SHOULD | 74 | 60 | 14 | 0 | 86 |
| 2026-07-28 | server | SHOULD NOT | 11 | 9 | 2 | 0 | 9 |
@@ -1329,7 +1329,7 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement
| [elicitation-client-32602-on-undeclared-mode](https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation#error-handling) | MUST | client | n/a: addresses the other side | tested: `mcpclient.TestElicitationDeclarations/2025-11-25/elicitation/undeclared-mode-is-32602` |
| [elicitation-server-state-bound-to-user](https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation#statefulness) | MUST | server | **gap** #257: mcpx has no authenticated user: state is bound to the request, the session id is whatever the caller sends | n/a: addresses the other side |
| [elicitation-state-not-session-id-alone](https://modelcontextprotocol.io/specification/2025-11-25/client/elicitation#statefulness) | MUST NOT | server | **gap** #257: state is bound to the request, not a user; there is no user to bind to | n/a: addresses the other side |
-| [elicitation-state-storage-protected](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | **gap** #255: pending questions and answers are readable by any local user (0755 directory, default file mode) | n/a: addresses the other side |
+| [elicitation-state-storage-protected](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | tested: `elicit.TestOpenAtCreatesOwnerOnlyStore@2025-11-25,2026-07-28`
`elicit.TestOpenAtMigratesLegacyStore@2025-11-25,2026-07-28` | n/a: addresses the other side |
| [elicitation-remote-user-id-from-authorization](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | n/a: optional feature mcpx does not implement | n/a: addresses the other side |
| [elicitation-server-must-not-self-authorize-via-url](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#url-mode-elicitation-for-oauth-flows) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2025-11-25/elicitation/elicitation-server-must-not-self-authorize-via-url` | n/a: addresses the other side |
| [elicitation-third-party-creds-not-via-client](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#understanding-the-distinction) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2025-11-25/elicitation/elicitation-third-party-creds-not-via-client` | n/a: addresses the other side |
@@ -1999,7 +1999,7 @@ Every normative requirement of every revision (catalogue: `docs/spec/requirement
| [elicitation-client-manual-retry-cancel-controls](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#example-request-sensitive-data) | SHOULD | client | n/a: addresses the other side | tested: `e2e.TestElicitationsCanBeListedAndAnsweredFromTheCommandLine@2025-11-25,2026-07-28` |
| [elicitation-no-32042-in-2026](https://modelcontextprotocol.io/specification/2026-07-28/basic/index#error-codes) | MUST NOT | both | tested: `conformance.TestClientFeaturesServer/2026-07-28/elicitation/elicitation-no-32042-in-2026` | tested: `conformance.TestClientFeaturesClient/2026-07-28/elicitation/elicitation-no-32042-in-2026` |
| [elicitation-server-stored-state-bound-to-user](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | **gap** #257: mcpx has no authenticated user: state is bound to the request, the session id is whatever the caller sends | n/a: addresses the other side |
-| [elicitation-state-storage-protected](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | **gap** #255: pending questions and answers are readable by any local user (0755 directory, default file mode) | n/a: addresses the other side |
+| [elicitation-state-storage-protected](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | tested: `elicit.TestOpenAtCreatesOwnerOnlyStore@2025-11-25,2026-07-28`
`elicit.TestOpenAtMigratesLegacyStore@2025-11-25,2026-07-28` | n/a: addresses the other side |
| [elicitation-remote-user-id-from-authorization](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#statefulness) | MUST | server | n/a: optional feature mcpx does not implement | n/a: addresses the other side |
| [elicitation-server-must-not-self-authorize-via-url](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#url-mode-elicitation-for-oauth-flows) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2026-07-28/elicitation/elicitation-server-must-not-self-authorize-via-url` | n/a: addresses the other side |
| [elicitation-third-party-creds-not-via-client](https://modelcontextprotocol.io/specification/2026-07-28/client/elicitation#understanding-the-distinction) | MUST NOT | server | tested: `conformance.TestClientFeaturesServer/2026-07-28/elicitation/elicitation-third-party-creds-not-via-client` | n/a: addresses the other side |
diff --git a/pkgs/mcpx/internal/cli/elicit.go b/pkgs/mcpx/internal/cli/elicit.go
index f9382206ae..0ffe0080e7 100644
--- a/pkgs/mcpx/internal/cli/elicit.go
+++ b/pkgs/mcpx/internal/cli/elicit.go
@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
- "github.com/dezren39/mcpx/internal/defaults"
"os"
"path/filepath"
"strings"
@@ -33,14 +32,13 @@ type ExitCoder interface {
ExitCode() int
}
-// broker opens the question store, which lives beside the log index.
+// broker opens the question store (elicit.StorePath). The legacy
+// location beside the log index -- or under a configured logging.dir --
+// is honoured as a migration source only.
func (a *App) broker() (*elicit.Broker, error) {
dir := firstNonEmpty(a.Settings().String("logging.dir"),
filepath.Join(a.Paths.State, "logs"))
- if err := os.MkdirAll(dir, defaults.PublicDirMode); err != nil {
- return nil, err
- }
- return elicit.Open(filepath.Join(dir, "elicit.db"))
+ return elicit.OpenAt(a.Paths.State, filepath.Join(dir, "elicit.db"))
}
// CmdElicit answers, lists and follows outstanding questions.
diff --git a/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go b/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go
index e8bc8ecd9a..aa081f5662 100644
--- a/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go
+++ b/pkgs/mcpx/internal/conformance/coverage_clientfeatures.go
@@ -8,6 +8,8 @@ func init() {
const decline = "elicit.TestAnsweringAfterTheProcessDiedStillWorks@2025-06-18,2025-11-25,2026-07-28"
const expire = "elicit.TestAnExpiredQuestionBecomesCancelNotDecline@2025-06-18,2025-11-25,2026-07-28"
const cliAnswer = "e2e.TestElicitationsCanBeListedAndAnsweredFromTheCommandLine@2025-11-25,2026-07-28"
+ const storeProtected = "elicit.TestOpenAtCreatesOwnerOnlyStore@2025-11-25,2026-07-28"
+ const storeMigrated = "elicit.TestOpenAtMigratesLegacyStore@2025-11-25,2026-07-28"
register(
C("elicitation-client-32602-on-undeclared-mode", "mcpclient.TestElicitationDeclarations/2025-11-25/elicitation/undeclared-mode-is-32602"),
C("elicitation-client-prepopulate-defaults", "mcpclient.TestElicitationDeclarations/2025-11-25/elicitation/client-applies-schema-defaults",
@@ -18,6 +20,7 @@ func init() {
C("elicitation-client-user-approval-controls", human),
C("elicitation-client-open-url-securely", human),
C("elicitation-client-manual-retry-cancel-controls", cliAnswer),
+ S("elicitation-state-storage-protected", storeProtected, storeMigrated),
SReq("conformance.TestClientFeaturesServer",
"sampling-tool-result-message-only-tool-results",
"sampling-tool-use-result-balance",
@@ -108,7 +111,6 @@ func init() {
Gap{ID: "sampling-both-handle-sensitive-data", Side: Client, Issue: "#255", Why: "prompts and answers are stored unredacted in state/logs/elicit.db under a 0755 directory and published on the event bus"},
Gap{ID: "sampling-both-handle-sensitive-data", Side: Server, Issue: "#255", Why: "prompts and answers are stored unredacted in state/logs/elicit.db under a 0755 directory and published on the event bus"},
Gap{ID: "sampling-messages-not-retained-between-requests", Side: Client, Issue: "#255", Why: "every CreateMessageRequest is kept in state/logs/elicit.db and never pruned"},
- Gap{ID: "elicitation-state-storage-protected", Side: Server, Issue: "#255", Why: "pending questions and answers are readable by any local user (0755 directory, default file mode)"},
Gap{ID: "sampling-client-rate-limit", Side: Client, Issue: "#257", Why: "no per-server or per-session limit on sampling requests"},
Gap{ID: "elicitation-client-rate-limit", Side: Client, Issue: "#257", Why: "no per-server limit on elicitation requests"},
Gap{ID: "sampling-client-error-on-tools-without-capability", Side: Client, Issue: "#210", Why: "a sampling request carrying tools is passed on though sampling.tools was not declared"},
diff --git a/pkgs/mcpx/internal/conformance/officialclient/main.go b/pkgs/mcpx/internal/conformance/officialclient/main.go
index 2cf15eca11..14b7dd42e3 100644
--- a/pkgs/mcpx/internal/conformance/officialclient/main.go
+++ b/pkgs/mcpx/internal/conformance/officialclient/main.go
@@ -384,12 +384,8 @@ func (m *mcpx) answerElicitations(ctx context.Context) func() {
fmt.Fprintf(os.Stderr, "-poll: %v\n", err)
return
}
- logs := filepath.Join(m.dir, "state", "logs")
- if err := os.MkdirAll(logs, 0o755); err != nil {
- fmt.Fprintf(os.Stderr, "elicit store: %v\n", err)
- return
- }
- b, err := elicit.Open(filepath.Join(logs, "elicit.db"))
+ state := filepath.Join(m.dir, "state")
+ b, err := elicit.OpenAt(state, filepath.Join(state, "logs", "elicit.db"))
if err != nil {
fmt.Fprintf(os.Stderr, "elicit store: %v\n", err)
return
diff --git a/pkgs/mcpx/internal/daemon/hooks.go b/pkgs/mcpx/internal/daemon/hooks.go
index cf8569fcb6..9a32b50619 100644
--- a/pkgs/mcpx/internal/daemon/hooks.go
+++ b/pkgs/mcpx/internal/daemon/hooks.go
@@ -284,13 +284,15 @@ func (r *Registry) rootsViaBroker(ctx context.Context, server, key string, param
return result, nil
}
-// OpenBroker opens the question store beside the log index.
+// OpenBroker opens the question store. The store lives in its own
+// owner-only directory under the state dir (elicit.StorePath); a store
+// left beside the log index by an older build is migrated on open.
func OpenBroker(paths Paths) (*elicit.Broker, error) {
dir := filepath.Join(paths.State, "logs")
if err := os.MkdirAll(dir, defaults.PublicDirMode); err != nil {
return nil, err
}
- return elicit.Open(filepath.Join(dir, "elicit.db"))
+ return elicit.OpenAt(paths.State, filepath.Join(dir, "elicit.db"))
}
// PublishLifecycle forwards a pool lifecycle event onto the bus.
diff --git a/pkgs/mcpx/internal/e2e/e2e_test.go b/pkgs/mcpx/internal/e2e/e2e_test.go
index dc525912c0..997009e5d1 100644
--- a/pkgs/mcpx/internal/e2e/e2e_test.go
+++ b/pkgs/mcpx/internal/e2e/e2e_test.go
@@ -21,6 +21,7 @@ import (
"testing"
"time"
+ "github.com/dezren39/mcpx/internal/elicit"
"github.com/dezren39/mcpx/internal/testsupport"
_ "modernc.org/sqlite"
@@ -2444,7 +2445,7 @@ func seedElicitation(t *testing.T, e *env, id, server, mode, schema string) {
t.Helper()
// Through the same pure-Go driver the store uses, so the suite needs no
// sqlite3-capable interpreter on PATH (the nix check sandbox has none).
- db, err := sql.Open("sqlite", filepath.Join(e.dir, "state", "logs", "elicit.db"))
+ db, err := sql.Open("sqlite", elicit.StorePath(filepath.Join(e.dir, "state")))
if err != nil {
t.Fatalf("seeding: %v", err)
}
diff --git a/pkgs/mcpx/internal/elicit/store.go b/pkgs/mcpx/internal/elicit/store.go
new file mode 100644
index 0000000000..021e8fe9b8
--- /dev/null
+++ b/pkgs/mcpx/internal/elicit/store.go
@@ -0,0 +1,220 @@
+package elicit
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+ "syscall"
+
+ "github.com/dezren39/mcpx/internal/defaults"
+)
+
+// storeFileMode is the mode the broker database and its SQLite sidecars
+// are held at: owner read/write only. Pending questions can carry
+// credentials and consent prompts, so the store is never group- or
+// world-readable (design #1675, Phase 1).
+const storeFileMode os.FileMode = 0o600
+
+// StorePath is the one place that knows where the broker store lives:
+// /elicit/elicit.db, in its own owner-only directory rather than
+// beside the log index. Every opener — daemon and CLI alike — resolves
+// the store through here so the two can never drift apart again.
+func StorePath(stateDir string) string {
+ return filepath.Join(stateDir, "elicit", "elicit.db")
+}
+
+// OpenAt opens the broker store for a state directory, migrating a
+// legacy store into place first when one exists.
+//
+// The legacy store lived at legacyPath (daemon: /logs/elicit.db;
+// CLI: /elicit.db — the logging directory is
+// honoured as a migration source only; the destination is always
+// StorePath). Migration preserves pending questions: files are renamed
+// into place, with a copy-then-delete fallback when a rename cannot
+// cross filesystems. If migration fails outright, the legacy store is
+// opened where it lies, tightened in place, rather than leaving the
+// broker unusable or stranding its questions.
+//
+// The store directory is created and held at defaults.DirMode (0700)
+// and the database files at 0600. Tightening is chmod-if-looser and
+// idempotent, so a daemon and a CLI opening the store at the same
+// moment cannot fail each other, and a store loosened by hand or by an
+// older build is tightened on the next open. A tightening failure is a
+// returned error: a store that cannot be made owner-only is not opened
+// at all.
+func OpenAt(stateDir, legacyPath string) (*Broker, error) {
+ dest := StorePath(stateDir)
+ dir := filepath.Dir(dest)
+ if err := os.MkdirAll(dir, defaults.DirMode); err != nil {
+ return nil, err
+ }
+ if err := tighten(dir, defaults.DirMode); err != nil {
+ return nil, err
+ }
+ // Migration and the first open of the destination are one atomic
+ // step across processes, not just within one: without a shared
+ // lock, a second opener can see the destination database file
+ // before its WAL sidecar has been moved into place, open it, and
+ // permanently lose questions that only exist in the legacy WAL
+ // (the state a daemon crash leaves behind). The lock is held only
+ // until the store is complete and opened; SQLite itself arbitrates
+ // access from then on.
+ unlock, err := lockStoreDir(dir)
+ if err != nil {
+ return nil, err
+ }
+ defer unlock()
+ return openAt(dest, legacyPath)
+}
+
+// lockStoreDir takes the store directory's cross-process lock and
+// returns the release function. The lock file lives in the owner-only
+// store directory and is itself owner-only.
+func lockStoreDir(dir string) (func(), error) {
+ f, err := os.OpenFile(filepath.Join(dir, "elicit.db.lock"), os.O_CREATE|os.O_RDWR, storeFileMode)
+ if err != nil {
+ return nil, err
+ }
+ if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
+ f.Close()
+ return nil, fmt.Errorf("locking elicitation store directory: %w", err)
+ }
+ return func() {
+ _ = syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
+ _ = f.Close()
+ }, nil
+}
+
+// openAt is OpenAt under the store lock: migrate (if needed), open,
+// tighten.
+func openAt(dest, legacyPath string) (*Broker, error) {
+ if legacyPath != "" && legacyPath != dest {
+ _, destErr := os.Stat(dest)
+ _, legacyErr := os.Stat(legacyPath)
+ if os.IsNotExist(destErr) && legacyErr == nil {
+ if err := migrateStore(legacyPath, dest); err != nil {
+ if _, derr := os.Stat(dest); derr != nil {
+ // Migration failed and no store appeared (a
+ // concurrent opener that won the rename leaves
+ // dest in place, handled by the stat above):
+ // fall back to the legacy store, tightened in
+ // place.
+ b, oerr := Open(legacyPath)
+ if oerr != nil {
+ return nil, oerr
+ }
+ if terr := tightenFiles(legacyPath); terr != nil {
+ b.Close()
+ return nil, fmt.Errorf("tightening legacy elicitation store: %w", terr)
+ }
+ return b, nil
+ }
+ }
+ }
+ }
+ dir := filepath.Dir(dest)
+ if err := os.MkdirAll(dir, defaults.DirMode); err != nil {
+ return nil, err
+ }
+ if err := tighten(dir, defaults.DirMode); err != nil {
+ return nil, err
+ }
+ b, err := Open(dest)
+ if err != nil {
+ return nil, err
+ }
+ if err := tightenFiles(dest); err != nil {
+ b.Close()
+ return nil, err
+ }
+ return b, nil
+}
+
+// migrateStore moves the legacy database (and any WAL/SHM sidecars)
+// to dest, preserving every stored question.
+func migrateStore(legacyPath, dest string) error {
+ dir := filepath.Dir(dest)
+ if err := os.MkdirAll(dir, defaults.DirMode); err != nil {
+ return err
+ }
+ if err := tighten(dir, defaults.DirMode); err != nil {
+ return err
+ }
+ for _, suffix := range []string{"", "-wal", "-shm"} {
+ src, dst := legacyPath+suffix, dest+suffix
+ if _, err := os.Stat(src); err != nil {
+ if os.IsNotExist(err) {
+ continue
+ }
+ return err
+ }
+ if err := os.Rename(src, dst); err == nil {
+ continue
+ }
+ // A rename can fail across filesystems, or because a
+ // concurrent opener already moved the file. Copy, then
+ // delete the original.
+ if err := copyFile(src, dst); err != nil {
+ if suffix == "" {
+ if _, derr := os.Stat(dest); derr == nil {
+ continue // a concurrent opener won; its copy stands
+ }
+ }
+ return err
+ }
+ _ = os.Remove(src)
+ }
+ return nil
+}
+
+func copyFile(src, dst string) error {
+ in, err := os.Open(src)
+ if err != nil {
+ return err
+ }
+ defer in.Close()
+ out, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, storeFileMode)
+ if err != nil {
+ return err
+ }
+ if _, err := io.Copy(out, in); err != nil {
+ out.Close()
+ return err
+ }
+ if err := out.Sync(); err != nil {
+ out.Close()
+ return err
+ }
+ return out.Close()
+}
+
+// tighten chmods path to want when its current mode grants anything
+// beyond want ("looser"). A mode that is already want, or stricter,
+// is left alone. A missing path is not an error: SQLite sidecars
+// exist only once the database has been written to.
+func tighten(path string, want os.FileMode) error {
+ info, err := os.Stat(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return nil
+ }
+ return err
+ }
+ if info.Mode().Perm()&^want != 0 {
+ if err := os.Chmod(path, want); err != nil {
+ return fmt.Errorf("tightening %s to %04o: %w", path, want, err)
+ }
+ }
+ return nil
+}
+
+// tightenFiles holds the database and its sidecars at storeFileMode.
+func tightenFiles(dbPath string) error {
+ for _, suffix := range []string{"", "-wal", "-shm"} {
+ if err := tighten(dbPath+suffix, storeFileMode); err != nil {
+ return err
+ }
+ }
+ return nil
+}
diff --git a/pkgs/mcpx/internal/elicit/store_test.go b/pkgs/mcpx/internal/elicit/store_test.go
new file mode 100644
index 0000000000..fbb71cb1ca
--- /dev/null
+++ b/pkgs/mcpx/internal/elicit/store_test.go
@@ -0,0 +1,233 @@
+package elicit_test
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+
+ "github.com/dezren39/mcpx/internal/elicit"
+)
+
+func perm(t *testing.T, path string) os.FileMode {
+ t.Helper()
+ info, err := os.Stat(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return info.Mode().Perm()
+}
+
+func TestOpenAtCreatesOwnerOnlyStore(t *testing.T) {
+ state := t.TempDir()
+ b, err := elicit.OpenAt(state, filepath.Join(state, "logs", "elicit.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer b.Close()
+
+ db := elicit.StorePath(state)
+ if got := perm(t, filepath.Dir(db)); got != 0o700 {
+ t.Errorf("store dir mode = %04o, want 0700", got)
+ }
+ if got := perm(t, db); got != 0o600 {
+ t.Errorf("store file mode = %04o, want 0600", got)
+ }
+ // The shared logs directory is not created, let alone tightened:
+ // it keeps its public mode and other tenants.
+ if _, err := os.Stat(filepath.Join(state, "logs")); !os.IsNotExist(err) {
+ t.Errorf("OpenAt created the logs dir; err = %v", err)
+ }
+}
+
+func TestOpenAtMigratesLegacyStore(t *testing.T) {
+ state := t.TempDir()
+ legacy := filepath.Join(state, "logs", "elicit.db")
+ if err := os.MkdirAll(filepath.Dir(legacy), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ lb, err := elicit.Open(legacy)
+ if err != nil {
+ t.Fatal(err)
+ }
+ r := ask(t, lb, elicit.Request{Session: "s1"})
+ if err := lb.Close(); err != nil {
+ t.Fatal(err)
+ }
+
+ b, err := elicit.OpenAt(state, legacy)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer b.Close()
+
+ if _, err := os.Stat(legacy); !os.IsNotExist(err) {
+ t.Errorf("legacy store still in place after migration; err = %v", err)
+ }
+ if got := perm(t, elicit.StorePath(state)); got != 0o600 {
+ t.Errorf("migrated store mode = %04o, want 0600", got)
+ }
+ pending, err := b.Pending(elicit.Filter{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ found := false
+ for _, p := range pending {
+ if p.ID == r.ID {
+ found = true
+ }
+ }
+ if !found {
+ t.Fatalf("pending question %s did not survive migration: %+v", r.ID, pending)
+ }
+ // And it is still answerable from the migrated store.
+ if err := b.Respond(elicit.Answer{
+ ID: r.ID, Action: elicit.Accept,
+ Content: json.RawMessage(`{"repo":"me/thing"}`), By: "test",
+ }); err != nil {
+ t.Fatalf("answering the migrated question: %v", err)
+ }
+}
+
+func TestOpenAtTightensLooseModes(t *testing.T) {
+ state := t.TempDir()
+ b, err := elicit.OpenAt(state, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := b.Close(); err != nil {
+ t.Fatal(err)
+ }
+ db := elicit.StorePath(state)
+ if err := os.Chmod(filepath.Dir(db), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chmod(db, 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ b2, err := elicit.OpenAt(state, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer b2.Close()
+ if got := perm(t, filepath.Dir(db)); got != 0o700 {
+ t.Errorf("store dir mode after re-open = %04o, want 0700", got)
+ }
+ if got := perm(t, db); got != 0o600 {
+ t.Errorf("store file mode after re-open = %04o, want 0600", got)
+ }
+}
+
+func TestOpenAtConcurrentOpens(t *testing.T) {
+ state := t.TempDir()
+ legacy := filepath.Join(state, "logs", "elicit.db")
+ const n = 8
+ var wg sync.WaitGroup
+ errs := make([]error, n)
+ brokers := make([]*elicit.Broker, n)
+ for i := range n {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ b, err := elicit.OpenAt(state, legacy)
+ brokers[i] = b
+ errs[i] = err
+ }()
+ }
+ wg.Wait()
+ for i := range n {
+ if errs[i] != nil {
+ t.Errorf("concurrent open %d: %v", i, errs[i])
+ }
+ if brokers[i] != nil {
+ brokers[i].Close()
+ }
+ }
+}
+
+func TestOpenAtConcurrentMigrationPreservesUncheckpointedWAL(t *testing.T) {
+ // A daemon that crashes leaves its newest questions only in the
+ // legacy store's WAL: the main database file does not have them
+ // until a checkpoint, which a crash never runs. Build that state
+ // by snapshotting a live legacy store's files before Close can
+ // checkpoint, then race first opens of the new location against
+ // the snapshot: no opener may see the destination before its WAL
+ // arrives, or the question is lost for good.
+ src := t.TempDir()
+ srcLegacy := filepath.Join(src, "logs", "elicit.db")
+ if err := os.MkdirAll(filepath.Dir(srcLegacy), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ lb, err := elicit.Open(srcLegacy)
+ if err != nil {
+ t.Fatal(err)
+ }
+ r := ask(t, lb, elicit.Request{Session: "s1"})
+ wal, err := os.ReadFile(srcLegacy + "-wal")
+ if err != nil || len(wal) == 0 {
+ t.Fatalf("live legacy store has no WAL to snapshot: err=%v len=%d", err, len(wal))
+ }
+ main, err := os.ReadFile(srcLegacy)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ state := t.TempDir()
+ legacy := filepath.Join(state, "logs", "elicit.db")
+ if err := os.MkdirAll(filepath.Dir(legacy), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(legacy, main, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(legacy+"-wal", wal, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := lb.Close(); err != nil {
+ t.Fatal(err)
+ }
+
+ const n = 8
+ var wg sync.WaitGroup
+ errs := make([]error, n)
+ brokers := make([]*elicit.Broker, n)
+ for i := range n {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ b, err := elicit.OpenAt(state, legacy)
+ brokers[i] = b
+ errs[i] = err
+ }()
+ }
+ wg.Wait()
+ for i := range n {
+ if errs[i] != nil {
+ t.Errorf("concurrent migrating open %d: %v", i, errs[i])
+ }
+ if brokers[i] != nil {
+ brokers[i].Close()
+ }
+ }
+
+ b, err := elicit.OpenAt(state, legacy)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer b.Close()
+ pending, err := b.Pending(elicit.Filter{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ found := false
+ for _, q := range pending {
+ if q.ID == r.ID {
+ found = true
+ }
+ }
+ if !found {
+ t.Fatalf("question %s that lived only in the legacy WAL did not survive concurrent migration: %+v", r.ID, pending)
+ }
+}