From 2a48ea046bc411dab8aea2a5fe1c7d03109464fc Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Thu, 2 Apr 2026 01:00:56 +0200 Subject: [PATCH] fix: invert FindAndOpenSQLiteStore existence check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The condition was backwards — it exited with "database already exists" when the file was found, and tried to open it when it did not exist. Co-Authored-By: Claude Sonnet 4.6 --- src/service/node_service_impl.go | 7 ++----- src/store/graph_store_sqlite.go | 10 ++++------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/service/node_service_impl.go b/src/service/node_service_impl.go index cbd4901..1b67198 100644 --- a/src/service/node_service_impl.go +++ b/src/service/node_service_impl.go @@ -78,7 +78,7 @@ type permContext struct { levels map[string]int } -func (pc *permContext) level(nodeID string) int { return pc.levels[nodeID] } +func (pc *permContext) level(nodeID string) int { return pc.levels[nodeID] } func (pc *permContext) canRead(nodeID string) bool { return pc.levels[nodeID] >= permRead } func (pc *permContext) canCreateRel(nodeID string) bool { return pc.levels[nodeID] >= permCreateRel } func (pc *permContext) canWrite(nodeID string) bool { return pc.levels[nodeID] >= permWrite } @@ -118,10 +118,7 @@ func (s *nodeServiceImpl) getPermContext() (*permContext, error) { } for relType, pLevel := range permRelLevels { for _, tgt := range node.Relations[string(relType)] { - eff := curr.level - if pLevel < eff { - eff = pLevel - } + eff := min(curr.level, pLevel) if eff > pc.levels[tgt] { queue = append(queue, entry{tgt, eff}) } diff --git a/src/store/graph_store_sqlite.go b/src/store/graph_store_sqlite.go index 8c78f23..36c0bd7 100644 --- a/src/store/graph_store_sqlite.go +++ b/src/store/graph_store_sqlite.go @@ -60,7 +60,7 @@ func InitSQLiteStore(path string) error { // FindAndOpenSQLiteStore opens the SQLite database. If the AX_DB_PATH environment // variable is set, it uses that path directly. Otherwise, it walks up from the -// current working directory to find an .ax.db file. +// current working directory to find an .ax/ax.db file. func FindAndOpenSQLiteStore() (GraphStore, error) { if dbpath := os.Getenv("AX_DB_PATH"); dbpath != "" { return NewSQLiteStore(dbpath) @@ -68,13 +68,11 @@ func FindAndOpenSQLiteStore() (GraphStore, error) { dataRoot, err := FindDataRoot(".local", "share") if err != nil { - fmt.Fprintln(os.Stderr, "failed to find data dir:", err) - os.Exit(1) + return nil, fmt.Errorf("failed to find data dir: %w", err) } dbPath := filepath.Join(dataRoot, "ax.db") - if _, err := os.Stat(dbPath); err == nil { - fmt.Fprintln(os.Stderr, "database already exists:", dbPath) - os.Exit(1) + if _, err := os.Stat(dbPath); err != nil { + return nil, fmt.Errorf("no database found at %s: run 'ax init' first", dbPath) } return NewSQLiteStore(dbPath) }