Skip to content

Commit 0591ffc

Browse files
fix(miner): route the governor/prediction/plan local stores through openLocalStoreDb (#6667)
governor-ledger.js, prediction-ledger.js, and plan-store.js each hand-rolled their own mkdir(0o700)/DatabaseSync/chmod(0o600)/busy_timeout open, bypassing local-store.js's openLocalStoreDb — so unlike the package's other local stores their handles were never registered with registerCleanupResource, and a SIGINT/SIGTERM/uncaught-exception mid-run left them half-written instead of closed cleanly (#4826). Route all three through openLocalStoreDb, which centralizes that boilerplate (including the ':memory:' no-file case) and the crash-safe cleanup registration. Closes #6595.
1 parent b4458ca commit 0591ffc

3 files changed

Lines changed: 11 additions & 24 deletions

File tree

packages/loopover-miner/lib/governor-ledger.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { chmodSync, mkdirSync } from "node:fs";
21
import { homedir } from "node:os";
3-
import { dirname, join } from "node:path";
4-
import { DatabaseSync } from "node:sqlite";
2+
import { join } from "node:path";
53
import { normalizeGovernorLedgerEvent } from "@loopover/engine";
4+
import { openLocalStoreDb } from "./local-store.js";
65
import { applySchemaMigrations } from "./schema-version.js";
76
import {
87
GOVERNOR_LEDGER_PURGE_SPEC,
@@ -96,10 +95,7 @@ function rowToDecision(row) {
9695
*/
9796
export function initGovernorLedger(dbPath = resolveGovernorLedgerDbPath()) {
9897
const resolvedPath = normalizeDbPath(dbPath);
99-
mkdirSync(dirname(resolvedPath), { recursive: true, mode: 0o700 });
100-
const db = new DatabaseSync(resolvedPath);
101-
chmodSync(resolvedPath, 0o600);
102-
db.exec("PRAGMA busy_timeout = 5000");
98+
const db = openLocalStoreDb(resolvedPath);
10399
db.exec(`
104100
CREATE TABLE IF NOT EXISTS governor_events (
105101
id INTEGER PRIMARY KEY AUTOINCREMENT,

packages/loopover-miner/lib/plan-store.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { chmodSync, mkdirSync } from "node:fs";
21
import { homedir } from "node:os";
3-
import { dirname, join } from "node:path";
4-
import { DatabaseSync } from "node:sqlite";
2+
import { join } from "node:path";
3+
import { openLocalStoreDb } from "./local-store.js";
54
import { applySchemaMigrations } from "./schema-version.js";
65

76
// Local SQLite persistence for the stateless MCP plan DAG (#2318). `loopover_build_plan`/`plan_status`/
@@ -150,13 +149,9 @@ function rowToRecord(row) {
150149
*/
151150
export function openPlanStore(dbPath = resolvePlanStoreDbPath()) {
152151
const resolvedPath = normalizeDbPath(dbPath);
153-
// The store is a persistent local file; the special in-memory path (':memory:') has no file to create or chmod.
154-
if (resolvedPath !== ":memory:") {
155-
mkdirSync(dirname(resolvedPath), { recursive: true, mode: 0o700 });
156-
}
157-
const db = new DatabaseSync(resolvedPath);
158-
if (resolvedPath !== ":memory:") chmodSync(resolvedPath, 0o600);
159-
db.exec("PRAGMA busy_timeout = 5000");
152+
// openLocalStoreDb centralizes the mkdir(0o700)/chmod(0o600)/busy_timeout + crash-safe cleanup registration and
153+
// treats ':memory:' as a no-file special case, so this store no longer hand-rolls that boilerplate (#4826).
154+
const db = openLocalStoreDb(resolvedPath);
160155
db.exec(`
161156
CREATE TABLE IF NOT EXISTS miner_plans (
162157
plan_id TEXT PRIMARY KEY,

packages/loopover-miner/lib/prediction-ledger.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { chmodSync, mkdirSync } from "node:fs";
21
import { homedir } from "node:os";
3-
import { dirname, join } from "node:path";
4-
import { DatabaseSync } from "node:sqlite";
2+
import { join } from "node:path";
3+
import { openLocalStoreDb } from "./local-store.js";
54
import {
65
PREDICTION_LEDGER_PURGE_SPEC,
76
PREDICTION_LEDGER_RETENTION_SPEC,
@@ -135,10 +134,7 @@ function rowToEntry(row) {
135134
*/
136135
export function initPredictionLedger(dbPath = resolvePredictionLedgerDbPath()) {
137136
const resolvedPath = normalizeDbPath(dbPath);
138-
mkdirSync(dirname(resolvedPath), { recursive: true, mode: 0o700 });
139-
const db = new DatabaseSync(resolvedPath);
140-
chmodSync(resolvedPath, 0o600);
141-
db.exec("PRAGMA busy_timeout = 5000");
137+
const db = openLocalStoreDb(resolvedPath);
142138
db.exec(`
143139
CREATE TABLE IF NOT EXISTS predictions (
144140
id INTEGER PRIMARY KEY AUTOINCREMENT,

0 commit comments

Comments
 (0)