Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/loopover-miner/lib/deny-hook-synthesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// this module is now a thin wrapper that re-exports those pure helpers and keeps the local SQLite store for
// refresh + maintainer review before any synthesized rule takes effect. Approved rules merge with
// {@link DEFAULT_DENY_RULES}; unapproved proposals never block tool calls. No behavior change.
import { chmodSync, mkdirSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import {
aggregateBlockerHistory,
Expand All @@ -25,6 +24,7 @@ import type { DenyRuleProposal, SynthesisConfig } from "@loopover/engine";
import { DEFAULT_FORGE_CONFIG } from "./forge-config.js";
import type { DenyRule } from "./deny-hooks.js";
import { DENY_HOOK_SYNTHESIS_PURGE_SPEC, purgeStoreByRepo } from "./store-maintenance.js";
import { openLocalStoreDb } from "./local-store.js";

// Re-export the pure synthesis helpers from the engine so this module's public API is unchanged after #5667
// moved derivation/audit into @loopover/engine. Only the SQLite store below (and its forge/db-path helpers) is
Expand Down Expand Up @@ -162,10 +162,9 @@ function ensureDenyRuleProposalsForgeScope(db: DatabaseSync): void {
*/
export function initDenyHookSynthesisStore(dbPath: string = resolveDenyHookSynthesisDbPath()): DenyHookSynthesisStore {
const resolvedPath = normalizeDbPath(dbPath);
mkdirSync(dirname(resolvedPath), { recursive: true, mode: 0o700 });
const db = new DatabaseSync(resolvedPath);
chmodSync(resolvedPath, 0o600);
db.exec("PRAGMA busy_timeout = 5000");
// openLocalStoreDb centralizes the mkdir(0o700)/chmod(0o600)/busy_timeout + crash-safe cleanup registration
// (#8319) -- so a SIGINT/SIGTERM/uncaught-exception mid-refresh doesn't leave this file half-written.
const db = openLocalStoreDb(resolvedPath);
db.exec(`
CREATE TABLE IF NOT EXISTS deny_rule_proposals (
repo_full_name TEXT NOT NULL,
Expand Down
10 changes: 6 additions & 4 deletions packages/loopover-miner/lib/laptop-init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { accessSync, chmodSync, constants, existsSync, mkdirSync } from "node:fs";
import { accessSync, constants, existsSync } from "node:fs";
import { homedir } from "node:os";
import { delimiter, join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import { applySchemaMigrations } from "./schema-version.js";
import { reportCliFailure } from "./cli-error.js";
import { resolveGitHubToken } from "./github-token-resolution.js";
import { openLocalStoreDb } from "./local-store.js";

const githubApiBaseUrl = "https://api.github.com";
const githubApiVersion = "2022-11-28";
Expand Down Expand Up @@ -52,9 +53,11 @@ export function resolveLaptopStateDbPath(env: Record<string, string | undefined>
export function initLaptopState(env: Record<string, string | undefined> = process.env): LaptopInitResult {
const stateDir = resolveMinerStateDir(env);
const dbPath = resolveLaptopStateDbPath(env);
mkdirSync(stateDir, { recursive: true, mode: 0o700 });
const created = !existsSync(dbPath);
const db = new DatabaseSync(dbPath);
// openLocalStoreDb centralizes the mkdir(0o700)/chmod(0o600)/busy_timeout + crash-safe cleanup registration
// (#8319) -- this was previously the one store in the package with no busy-timeout and no crash-safety
// registration at all, so a SIGINT/SIGTERM/uncaught-exception mid-bootstrap could leave the file half-written.
const db = openLocalStoreDb(dbPath);
db.exec(`
CREATE TABLE IF NOT EXISTS laptop_meta (
key TEXT PRIMARY KEY,
Expand All @@ -67,7 +70,6 @@ export function initLaptopState(env: Record<string, string | undefined> = proces
db.prepare("INSERT INTO laptop_meta (key, value) VALUES ('initialized_at', ?)")
.run(new Date().toISOString());
}
chmodSync(dbPath, 0o600);
db.close();
return { stateDir, dbPath, created };
}
Expand Down
12 changes: 5 additions & 7 deletions packages/loopover-miner/lib/orb-export.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { chmodSync, mkdirSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import { join } from "node:path";
import { createHash, createHmac } from "node:crypto";
import { generateAnonSecret, hmacAnonymize as engineHmacAnonymize } from "@loopover/engine";
import { readPrOutcomes } from "./pr-outcome.js";
import type { NormalizedPrOutcomePayload, PrOutcomeLedgerReader } from "./pr-outcome.js";
import { initEventLedger } from "./event-ledger.js";
import { argsWantJson, describeCliError, reportCliFailure } from "./cli-error.js";
import { openLocalStoreDb } from "./local-store.js";

// Optional anonymized Orb telemetry export (#4277, network send wired in #5681). The self-host Orb collector
// (src/selfhost/orb-collector.ts, #1255) is ALWAYS-ON for a maintainer's own instance; a miner runs on a
Expand Down Expand Up @@ -125,10 +124,9 @@ export function buildAnonymizedOrbBatch(
*/
export function openOrbExportStore(dbPath: string = resolveOrbExportDbPath()): OrbExportStore {
const resolvedPath = normalizeDbPath(dbPath);
mkdirSync(dirname(resolvedPath), { recursive: true, mode: 0o700 });
const db = new DatabaseSync(resolvedPath);
chmodSync(resolvedPath, 0o600);
db.exec("PRAGMA busy_timeout = 5000");
// openLocalStoreDb centralizes the mkdir(0o700)/chmod(0o600)/busy_timeout + crash-safe cleanup registration
// (#8319) -- so a SIGINT/SIGTERM/uncaught-exception mid-export doesn't leave this file half-written.
const db = openLocalStoreDb(resolvedPath);
db.exec(`CREATE TABLE IF NOT EXISTS orb_export_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)`);

const getStatement = db.prepare("SELECT value FROM orb_export_meta WHERE key = ?");
Expand Down
11 changes: 11 additions & 0 deletions test/unit/miner-deny-hook-synthesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { DenyRuleProposal } from "../../packages/loopover-engine/src/miner/
// #7525: normalizeRepoFullName is defined in the engine and re-exported unchanged by the miner-lib module
// above; import it from the engine source directly so the guard's src branches are the ones exercised.
import { normalizeRepoFullName } from "../../packages/loopover-engine/src/miner/deny-hook-synthesis";
import { cleanupResourceCount, resetProcessLifecycleForTesting } from "../../packages/loopover-miner/lib/process-lifecycle.js";

const tempDirs: string[] = [];
const stores: Array<{ close(): void }> = [];
Expand Down Expand Up @@ -163,6 +164,16 @@ describe("initDenyHookSynthesisStore() (#4522)", () => {
expect(() => initDenyHookSynthesisStore(" ")).toThrow("invalid_deny_hook_synthesis_db_path");
});

it("registers the store for crash-safe cleanup via openLocalStoreDb, and unregisters it on close (#8319)", () => {
resetProcessLifecycleForTesting();
expect(cleanupResourceCount()).toBe(0);
const store = tempStore();
expect(cleanupResourceCount()).toBe(1);
stores.splice(stores.indexOf(store), 1);
store.close();
expect(cleanupResourceCount()).toBe(0);
});

it("skips the forge-scope migration on a second open of an already-migrated file", () => {
const dir = mkdtempSync(join(tmpdir(), "miner-deny-hook-synthesis-remigrate-"));
tempDirs.push(dir);
Expand Down
11 changes: 11 additions & 0 deletions test/unit/miner-laptop-init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
resolveLaptopStateDbPath,
runInit,
} from "../../packages/loopover-miner/lib/laptop-init.js";
import { cleanupResourceCount, resetProcessLifecycleForTesting } from "../../packages/loopover-miner/lib/process-lifecycle.js";

const roots: string[] = [];

Expand Down Expand Up @@ -83,6 +84,16 @@ describe("loopover-miner laptop init (#2329)", () => {
expect(readFileSync(join(first.stateDir, "marker.txt"), "utf8")).toBe("keep-me");
});

it("opens its store via openLocalStoreDb, registering and unregistering it for crash-safe cleanup within the call (#8319)", () => {
resetProcessLifecycleForTesting();
expect(cleanupResourceCount()).toBe(0);
const root = tempRoot();
initLaptopState({ LOOPOVER_MINER_CONFIG_DIR: join(root, "state") });
// initLaptopState closes its own handle internally before returning (it exposes no db handle), so a
// leftover registration here would mean the register→unregister cycle didn't complete cleanly.
expect(cleanupResourceCount()).toBe(0);
});

it("runInit prints human text (0) and machine JSON with --json", async () => {
const root = tempRoot();
const env = { LOOPOVER_MINER_CONFIG_DIR: join(root, "state") };
Expand Down
10 changes: 10 additions & 0 deletions test/unit/miner-orb-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
sendAmsExportBatch,
} from "../../packages/loopover-miner/lib/orb-export.js";
import type { OrbExportOutcome, OrbExportRow } from "../../packages/loopover-miner/lib/orb-export.js";
import { cleanupResourceCount, resetProcessLifecycleForTesting } from "../../packages/loopover-miner/lib/process-lifecycle.js";

let dir: string;
function storePath() {
Expand Down Expand Up @@ -68,6 +69,15 @@ describe("orb-export store (#4277)", () => {
expect(store.getCursor()).toBe("2026-01-02T00:00:00Z");
store.close();
});

it("registers the store for crash-safe cleanup via openLocalStoreDb, and unregisters it on close (#8319)", () => {
resetProcessLifecycleForTesting();
expect(cleanupResourceCount()).toBe(0);
const store = openOrbExportStore(storePath());
expect(cleanupResourceCount()).toBe(1);
store.close();
expect(cleanupResourceCount()).toBe(0);
});
});

describe("hmacAnonymize", () => {
Expand Down