@facthouse/mcp 0.35.0 → 0.35.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -13
- package/dist/db/connection.d.ts +2 -0
- package/dist/db/connection.d.ts.map +1 -1
- package/dist/db/connection.js +27 -0
- package/dist/db/connection.js.map +1 -1
- package/dist/db/consolidation-lock.d.ts +60 -4
- package/dist/db/consolidation-lock.d.ts.map +1 -1
- package/dist/db/consolidation-lock.js +230 -49
- package/dist/db/consolidation-lock.js.map +1 -1
- package/dist/db/pg-backend.d.ts.map +1 -1
- package/dist/db/pg-backend.js +22 -2
- package/dist/db/pg-backend.js.map +1 -1
- package/dist/db/postgres-schema.d.ts.map +1 -1
- package/dist/db/postgres-schema.js +8 -1
- package/dist/db/postgres-schema.js.map +1 -1
- package/dist/db/postgres.d.ts +8 -0
- package/dist/db/postgres.d.ts.map +1 -1
- package/dist/db/postgres.js +13 -1
- package/dist/db/postgres.js.map +1 -1
- package/dist/db/schema-version.d.ts +1 -1
- package/dist/db/schema-version.js +1 -1
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/db/schema.js +25 -0
- package/dist/db/schema.js.map +1 -1
- package/dist/intelligence/consolidate.d.ts.map +1 -1
- package/dist/intelligence/consolidate.js +64 -3
- package/dist/intelligence/consolidate.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,8 @@ FTS5 (words) and optional embeddings (meaning) are indexes of **K**. They are no
|
|
|
49
49
|
|
|
50
50
|
Two speeds. **Copy** tails named transcripts into Data. **Extract** turns new transcript lines into self-contained facts (D→I). **Integrate** fits them into what the store already knows: domains, entities, duplicates, contradictions, the graph (I→K). `consolidate` is the umbrella: copy, extract, and integrate together. Extract is capped at 50 lines per run, so a first backfill is never spent on the lot; when extract runs, it takes the oldest 50 lines. Consolidation does not invent a sentence nobody said.
|
|
51
51
|
|
|
52
|
+
One consolidation writes the store at a time. While the run is alive it refreshes a lease every 30 seconds, including during a long model call, so a long extract is not treated as abandoned. A second `consolidate` skips with “Another consolidation is in progress” until that lease is free. If this run loses the lease, it stops at the next extract batch, leaves marks at the last finished batch, and does not start integrate.
|
|
53
|
+
|
|
52
54
|
A hook cannot call MCP tools — those exist only on the assistant’s connection — and it must not wait for a model pass. So it does not invoke `consolidate`. It runs `facthouse notify …`, which tells the **already-running** server that a moment happened and returns at once. `consolidate` is the pipeline verb (MCP tool or CLI); the caller waits. `notify compaction` is that verb asked of the live server, asynchronously. `notify threshold` is a different policy (extract only, if due).
|
|
53
55
|
|
|
54
56
|
**Automatic**
|
|
@@ -137,17 +139,17 @@ These CLI commands work in bash, zsh, and PowerShell. Quote @facthouse/mcp in Po
|
|
|
137
139
|
|
|
138
140
|
<!-- x-release-please-start-version -->
|
|
139
141
|
```bash
|
|
140
|
-
npm install -g @facthouse/mcp@0.35.
|
|
142
|
+
npm install -g @facthouse/mcp@0.35.1
|
|
141
143
|
facthouse init --yes
|
|
142
144
|
```
|
|
143
145
|
|
|
144
146
|
If npm install -g fails because a command named mcp already exists, remove that leftover command and retry.
|
|
145
147
|
|
|
146
148
|
```bash
|
|
147
|
-
npx -y -p "@facthouse/mcp@0.35.
|
|
148
|
-
npx -y -p "@facthouse/mcp@0.35.
|
|
149
|
-
npx -y -p "@facthouse/mcp@0.35.
|
|
150
|
-
npx -y -p "@facthouse/mcp@0.35.
|
|
149
|
+
npx -y -p "@facthouse/mcp@0.35.1" -- facthouse init --yes
|
|
150
|
+
npx -y -p "@facthouse/mcp@0.35.1" -- facthouse settings --json
|
|
151
|
+
npx -y -p "@facthouse/mcp@0.35.1" -- facthouse stats
|
|
152
|
+
npx -y -p "@facthouse/mcp@0.35.1" -- facthouse inspect
|
|
151
153
|
```
|
|
152
154
|
<!-- x-release-please-end -->
|
|
153
155
|
|
|
@@ -301,12 +303,12 @@ A non-default data directory prints a distinct MCP server name so two stores can
|
|
|
301
303
|
"mcpServers": {
|
|
302
304
|
"facthouse-personal": {
|
|
303
305
|
"command": "npx",
|
|
304
|
-
"args": ["-y", "@facthouse/mcp@0.35.
|
|
306
|
+
"args": ["-y", "@facthouse/mcp@0.35.1"],
|
|
305
307
|
"env": { "FACTHOUSE_DATA": "C:\\Users\\alex\\.facthouse-personal" }
|
|
306
308
|
},
|
|
307
309
|
"facthouse-work": {
|
|
308
310
|
"command": "npx",
|
|
309
|
-
"args": ["-y", "@facthouse/mcp@0.35.
|
|
311
|
+
"args": ["-y", "@facthouse/mcp@0.35.1"],
|
|
310
312
|
"env": { "FACTHOUSE_DATA": "C:\\Users\\alex\\.facthouse-work" }
|
|
311
313
|
}
|
|
312
314
|
}
|
|
@@ -320,7 +322,7 @@ Point each store's `sources.cwd` (or hook `--data`) at that store only. Two dire
|
|
|
320
322
|
|
|
321
323
|
SQLite is the default and needs no extra software. To use Postgres instead, set `storage.provider` to `"postgres"` in that store's `config.json`, or `FACTHOUSE_STORAGE=postgres` on the MCP entry, and set `FACTHOUSE_POSTGRES_URL` to a `postgres://` (or `postgresql://`) URL. The password belongs in the environment, not in `config.json`. If the URL is missing or the server cannot be reached, Facthouse stops; it does not create a SQLite file.
|
|
322
324
|
|
|
323
|
-
The data directory is still the memory: `config.json` and the scheduler socket live there. Tables live at the URL. Two memories need two directories and two databases.
|
|
325
|
+
The data directory is still the memory: `config.json` and the scheduler socket live there. Tables live at the URL. Two memories need two directories and two databases. On Postgres the consolidation lease is also a session advisory lock held on a connection kept open for the whole run. When that connection drops, Postgres releases the lock. A stale lease row can be taken only by a run that holds the lock.
|
|
324
326
|
|
|
325
327
|
Init does not ask which engine to use. `facthouse init --yes` still writes sqlite.
|
|
326
328
|
|
|
@@ -332,7 +334,7 @@ Example — placeholders only; do not put a real password in a committed file:
|
|
|
332
334
|
"mcpServers": {
|
|
333
335
|
"facthouse": {
|
|
334
336
|
"command": "npx",
|
|
335
|
-
"args": ["-y", "@facthouse/mcp@0.35.
|
|
337
|
+
"args": ["-y", "@facthouse/mcp@0.35.1"],
|
|
336
338
|
"env": {
|
|
337
339
|
"FACTHOUSE_DATA": "C:\\Users\\alex\\.facthouse-work",
|
|
338
340
|
"FACTHOUSE_STORAGE": "postgres",
|
|
@@ -378,7 +380,7 @@ To skip the wizard (record only — no transcript copy), paste this. The server
|
|
|
378
380
|
"mcpServers": {
|
|
379
381
|
"facthouse": {
|
|
380
382
|
"command": "npx",
|
|
381
|
-
"args": ["-y", "@facthouse/mcp@0.35.
|
|
383
|
+
"args": ["-y", "@facthouse/mcp@0.35.1"]
|
|
382
384
|
}
|
|
383
385
|
}
|
|
384
386
|
}
|
|
@@ -403,7 +405,7 @@ PreCompact `notify compaction` is the useful one: when the client is about to co
|
|
|
403
405
|
"hooks": [
|
|
404
406
|
{
|
|
405
407
|
"type": "command",
|
|
406
|
-
"command": "npx -y -p @facthouse/mcp@0.35.
|
|
408
|
+
"command": "npx -y -p @facthouse/mcp@0.35.1 -- facthouse notify compaction --data /absolute/path/to/the-same-store"
|
|
407
409
|
}
|
|
408
410
|
]
|
|
409
411
|
}
|
|
@@ -492,7 +494,7 @@ Throwaway store, not the capture path for a real Claude Code or Cursor home. The
|
|
|
492
494
|
<!-- x-release-please-start-version -->
|
|
493
495
|
```bash
|
|
494
496
|
export FACTHOUSE_DATA=/tmp/facthouse-demo
|
|
495
|
-
om() { npx -y -p "@facthouse/mcp@0.35.
|
|
497
|
+
om() { npx -y -p "@facthouse/mcp@0.35.1" -- facthouse "$@"; }
|
|
496
498
|
|
|
497
499
|
om init --yes
|
|
498
500
|
|
|
@@ -507,7 +509,7 @@ om stats
|
|
|
507
509
|
|
|
508
510
|
```powershell
|
|
509
511
|
$env:FACTHOUSE_DATA = Join-Path $env:TEMP "facthouse-demo"
|
|
510
|
-
function om { npx -y -p "@facthouse/mcp@0.35.
|
|
512
|
+
function om { npx -y -p "@facthouse/mcp@0.35.1" -- facthouse @args }
|
|
511
513
|
om init --yes
|
|
512
514
|
om record --role user --content "I prefer dark mode in every editor, and I never want telemetry enabled."
|
|
513
515
|
om record --role user --content "I am allergic to shellfish, so avoid seafood restaurants when booking anything."
|
package/dist/db/connection.d.ts
CHANGED
|
@@ -64,6 +64,8 @@ export interface Db {
|
|
|
64
64
|
export declare function openDatabase(dbPath: string): Db;
|
|
65
65
|
/** Close the database connection. Safe to call multiple times. */
|
|
66
66
|
export declare function closeDatabase(db: Db): Promise<void>;
|
|
67
|
+
export declare function onDatabaseClose(db: object, hook: () => Promise<void>): void;
|
|
68
|
+
export declare function runCloseHooks(db: object): Promise<void>;
|
|
67
69
|
/**
|
|
68
70
|
* Read a scalar pragma (e.g. `user_version`, `data_version`).
|
|
69
71
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/db/connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,OAAO,KAAK,EAAE,aAAa,EAAiB,MAAM,aAAa,CAAC;AAEhE;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,aAAa,CAAC;AAErC,oEAAoE;AACpE,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,GAAG,MAAM,CAAC;CAClC;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,GAAG,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAC/C,GAAG,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC7C,GAAG,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;CAChD;AAED,0CAA0C;AAC1C,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,WAAW,EAAE;IACjB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,IAAI,CAAC,GAA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,CAAC,IAAI,CAAC,CAAC;IACvB;;;OAGG;IACH,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAClD;AAgDD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,EAAE,CAQ/C;AAED,kEAAkE;AAClE,wBAAsB,aAAa,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAEzD;AAQD,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAI3E;AAED,wBAAsB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAW7D;AAMD;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAMtE;AAED;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE1E;AAwBD;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,EAAE,EAAE,EAAE,EACN,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,CAAC,CAEZ"}
|
package/dist/db/connection.js
CHANGED
|
@@ -49,6 +49,7 @@ class SqliteDb {
|
|
|
49
49
|
return new SqliteStatement(this.raw.prepare(sql));
|
|
50
50
|
}
|
|
51
51
|
async close() {
|
|
52
|
+
await runCloseHooks(this);
|
|
52
53
|
try {
|
|
53
54
|
this.raw.close();
|
|
54
55
|
}
|
|
@@ -78,6 +79,32 @@ export function openDatabase(dbPath) {
|
|
|
78
79
|
export async function closeDatabase(db) {
|
|
79
80
|
await db.close();
|
|
80
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Run when this handle closes, before the underlying connection drops.
|
|
84
|
+
* Used so a session advisory lock is released with the store that holds it.
|
|
85
|
+
*/
|
|
86
|
+
const closeHooks = new WeakMap();
|
|
87
|
+
export function onDatabaseClose(db, hook) {
|
|
88
|
+
const list = closeHooks.get(db);
|
|
89
|
+
if (list)
|
|
90
|
+
list.push(hook);
|
|
91
|
+
else
|
|
92
|
+
closeHooks.set(db, [hook]);
|
|
93
|
+
}
|
|
94
|
+
export async function runCloseHooks(db) {
|
|
95
|
+
const list = closeHooks.get(db);
|
|
96
|
+
closeHooks.delete(db);
|
|
97
|
+
if (!list)
|
|
98
|
+
return;
|
|
99
|
+
for (const hook of list) {
|
|
100
|
+
try {
|
|
101
|
+
await hook();
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Closing still has to finish. A hook must not strand the connection.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
81
108
|
// ---------------------------------------------------------------------------
|
|
82
109
|
// Pragmas (SQLite-only)
|
|
83
110
|
// ---------------------------------------------------------------------------
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/db/connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AA8C3C,MAAM,eAAe;IACU;IAA7B,YAA6B,IAAmB;QAAnB,SAAI,GAAJ,IAAI,CAAe;IAAG,CAAC;IAEpD,KAAK,CAAC,GAAG,CAAC,GAAG,MAAkB;QAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;QACxC,OAAO;YACL,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;YAC/B,eAAe,E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C,WAAW,SAAS,EAAE,CAAC,CAAC;YACxC,CAAC;iBAAM,CAAC;gBACN,MAAM,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,mEAAmE;YACnE,sEAAsE;QACxE,CAAC;QACD,MAAM,GAAG,CAAC;IACZ,CAAC;YAAS,CAAC;QACT,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IACzB,CAAC;AACH,CAAC"}
|
|
@@ -1,15 +1,71 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* One consolidation writes the store at a time.
|
|
3
|
+
*
|
|
4
|
+
* The lease is a single row. `started_at` is when this run took it.
|
|
5
|
+
* `heartbeat_at` is the last time the holder showed it was still alive.
|
|
6
|
+
* A holder refreshes that timestamp on a timer, including across a long
|
|
7
|
+
* model call, and again at each extract batch boundary. Another run may
|
|
8
|
+
* take the row only when the lease is older than STALE_LOCK_MS, and only
|
|
9
|
+
* by a compare-and-swap on the holder and the lease timestamp.
|
|
10
|
+
*
|
|
11
|
+
* Postgres also takes a session advisory lock on a connection kept open
|
|
12
|
+
* for the whole run. Session locks do not survive a pool, so this is not
|
|
13
|
+
* a query on the shared client. The lock disappears when that connection
|
|
14
|
+
* drops. SQLite, and an in-process Postgres with only one session, use
|
|
15
|
+
* the lease alone.
|
|
4
16
|
*/
|
|
5
17
|
import type { Db } from "./connection.js";
|
|
6
18
|
export interface LockState {
|
|
7
19
|
holder: string;
|
|
20
|
+
/** When this run took the lease. Heartbeats do not move it. */
|
|
8
21
|
started_at: string;
|
|
22
|
+
/**
|
|
23
|
+
* Last heartbeat. Null on a row written before heartbeats existed;
|
|
24
|
+
* the lease is then `started_at`.
|
|
25
|
+
*/
|
|
26
|
+
heartbeat_at: string | null;
|
|
9
27
|
}
|
|
10
|
-
|
|
28
|
+
export interface LockHeartbeat {
|
|
29
|
+
/** Stop the timer. Does not release the lease. */
|
|
30
|
+
stop(): void;
|
|
31
|
+
/** True once a refresh saw that this holder no longer owns the row. */
|
|
32
|
+
lost(): boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* How often the holder refreshes the lease while the run is alive.
|
|
36
|
+
* Timer-driven, so a single long model call still counts as alive.
|
|
37
|
+
*/
|
|
38
|
+
export declare const LOCK_HEARTBEAT_MS = 30000;
|
|
39
|
+
/**
|
|
40
|
+
* A lease older than this may be taken. Four heartbeats: one missed
|
|
41
|
+
* refresh is not a dead run.
|
|
42
|
+
*/
|
|
43
|
+
export declare const STALE_LOCK_MS: number;
|
|
44
|
+
/**
|
|
45
|
+
* `pg_try_advisory_lock` / `pg_advisory_unlock` key. One consolidation
|
|
46
|
+
* per database. Fits in a JS safe integer so the driver does not round it.
|
|
47
|
+
*/
|
|
48
|
+
export declare const CONSOLIDATION_ADVISORY_LOCK_KEY = 7483921654032211;
|
|
49
|
+
/** Skip reason when a refresh finds the row is no longer ours. */
|
|
50
|
+
export declare const LOCK_LOST_REASON = "Lost the consolidation lock and stopped.";
|
|
51
|
+
/**
|
|
52
|
+
* Refresh the lease every LOCK_HEARTBEAT_MS until `stop`, `releaseLock`,
|
|
53
|
+
* or the database closes. The timer is unref'd so it cannot keep the
|
|
54
|
+
* process alive after the run has finished.
|
|
55
|
+
*/
|
|
56
|
+
export declare function startLockHeartbeat(db: Db, holder: string): LockHeartbeat;
|
|
57
|
+
/**
|
|
58
|
+
* Self-fence. `UPDATE ... WHERE id = 1 AND holder = ?`.
|
|
59
|
+
* Zero rows means this run lost the lease and must not write again.
|
|
60
|
+
*/
|
|
61
|
+
export declare function refreshLock(db: Db, holder: string): Promise<boolean>;
|
|
62
|
+
/** Try to acquire the lock. Returns true if this holder owns the lease. */
|
|
11
63
|
export declare function acquireLock(db: Db, holder: string): Promise<boolean>;
|
|
12
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Release the lease if we still hold it, stop the heartbeat, and drop
|
|
66
|
+
* the advisory connection. A holder mismatch deletes nothing — a run
|
|
67
|
+
* that already lost the row must not remove the new holder's lease.
|
|
68
|
+
*/
|
|
13
69
|
export declare function releaseLock(db: Db, holder: string): Promise<boolean>;
|
|
14
70
|
/** Check if a lock exists and return its state. */
|
|
15
71
|
export declare function getLockState(db: Db): Promise<LockState | null>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consolidation-lock.d.ts","sourceRoot":"","sources":["../../src/db/consolidation-lock.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"consolidation-lock.d.ts","sourceRoot":"","sources":["../../src/db/consolidation-lock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAQ1C,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAED,MAAM,WAAW,aAAa;IAC5B,kDAAkD;IAClD,IAAI,IAAI,IAAI,CAAC;IACb,uEAAuE;IACvE,IAAI,IAAI,OAAO,CAAC;CACjB;AAMD;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAS,CAAC;AAExC;;;GAGG;AACH,eAAO,MAAM,aAAa,QAAgB,CAAC;AAE3C;;;GAGG;AACH,eAAO,MAAM,+BAA+B,mBAAwB,CAAC;AAErE,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,6CAA6C,CAAC;AAkD3E;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,CA+BxE;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAS1E;AAqHD,2EAA2E;AAC3E,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAW1E;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAU1E;AAED,mDAAmD;AACnD,wBAAsB,YAAY,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAQpE"}
|
|
@@ -1,66 +1,247 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* One consolidation writes the store at a time.
|
|
3
|
+
*
|
|
4
|
+
* The lease is a single row. `started_at` is when this run took it.
|
|
5
|
+
* `heartbeat_at` is the last time the holder showed it was still alive.
|
|
6
|
+
* A holder refreshes that timestamp on a timer, including across a long
|
|
7
|
+
* model call, and again at each extract batch boundary. Another run may
|
|
8
|
+
* take the row only when the lease is older than STALE_LOCK_MS, and only
|
|
9
|
+
* by a compare-and-swap on the holder and the lease timestamp.
|
|
10
|
+
*
|
|
11
|
+
* Postgres also takes a session advisory lock on a connection kept open
|
|
12
|
+
* for the whole run. Session locks do not survive a pool, so this is not
|
|
13
|
+
* a query on the shared client. The lock disappears when that connection
|
|
14
|
+
* drops. SQLite, and an in-process Postgres with only one session, use
|
|
15
|
+
* the lease alone.
|
|
4
16
|
*/
|
|
5
|
-
import { withTransaction } from "./connection.js";
|
|
17
|
+
import { onDatabaseClose, withTransaction } from "./connection.js";
|
|
18
|
+
import { postgresSessionOpener } from "./postgres.js";
|
|
6
19
|
// ---------------------------------------------------------------------------
|
|
7
20
|
// Constants
|
|
8
21
|
// ---------------------------------------------------------------------------
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
22
|
+
/**
|
|
23
|
+
* How often the holder refreshes the lease while the run is alive.
|
|
24
|
+
* Timer-driven, so a single long model call still counts as alive.
|
|
25
|
+
*/
|
|
26
|
+
export const LOCK_HEARTBEAT_MS = 30_000;
|
|
27
|
+
/**
|
|
28
|
+
* A lease older than this may be taken. Four heartbeats: one missed
|
|
29
|
+
* refresh is not a dead run.
|
|
30
|
+
*/
|
|
31
|
+
export const STALE_LOCK_MS = 2 * 60 * 1000;
|
|
32
|
+
/**
|
|
33
|
+
* `pg_try_advisory_lock` / `pg_advisory_unlock` key. One consolidation
|
|
34
|
+
* per database. Fits in a JS safe integer so the driver does not round it.
|
|
35
|
+
*/
|
|
36
|
+
export const CONSOLIDATION_ADVISORY_LOCK_KEY = 7_483_921_654_032_211;
|
|
37
|
+
/** Skip reason when a refresh finds the row is no longer ours. */
|
|
38
|
+
export const LOCK_LOST_REASON = "Lost the consolidation lock and stopped.";
|
|
39
|
+
const beats = new WeakMap();
|
|
40
|
+
const advisoryByDb = new WeakMap();
|
|
41
|
+
const closeWatch = new WeakSet();
|
|
42
|
+
function beatsFor(db) {
|
|
43
|
+
let map = beats.get(db);
|
|
44
|
+
if (!map) {
|
|
45
|
+
map = new Map();
|
|
46
|
+
beats.set(db, map);
|
|
47
|
+
}
|
|
48
|
+
return map;
|
|
49
|
+
}
|
|
50
|
+
function stopBeat(db, holder) {
|
|
51
|
+
const map = beats.get(db);
|
|
52
|
+
const beat = map?.get(holder);
|
|
53
|
+
if (!beat)
|
|
54
|
+
return;
|
|
55
|
+
clearInterval(beat.timer);
|
|
56
|
+
map.delete(holder);
|
|
57
|
+
}
|
|
58
|
+
function watchClose(db) {
|
|
59
|
+
if (closeWatch.has(db))
|
|
60
|
+
return;
|
|
61
|
+
closeWatch.add(db);
|
|
62
|
+
onDatabaseClose(db, async () => {
|
|
63
|
+
const map = beats.get(db);
|
|
64
|
+
if (map) {
|
|
65
|
+
for (const holder of [...map.keys()])
|
|
66
|
+
stopBeat(db, holder);
|
|
49
67
|
}
|
|
50
|
-
|
|
68
|
+
const hold = advisoryByDb.get(db);
|
|
69
|
+
if (hold)
|
|
70
|
+
await releaseAdvisory(db, hold.holder);
|
|
51
71
|
});
|
|
52
72
|
}
|
|
53
|
-
/**
|
|
54
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Refresh the lease every LOCK_HEARTBEAT_MS until `stop`, `releaseLock`,
|
|
75
|
+
* or the database closes. The timer is unref'd so it cannot keep the
|
|
76
|
+
* process alive after the run has finished.
|
|
77
|
+
*/
|
|
78
|
+
export function startLockHeartbeat(db, holder) {
|
|
79
|
+
watchClose(db);
|
|
80
|
+
stopBeat(db, holder);
|
|
81
|
+
const beat = {
|
|
82
|
+
timer: setInterval(() => {
|
|
83
|
+
void refreshLock(db, holder).then((ok) => {
|
|
84
|
+
if (!beats.get(db)?.get(holder))
|
|
85
|
+
return;
|
|
86
|
+
if (!ok) {
|
|
87
|
+
beat.lost = true;
|
|
88
|
+
stopBeat(db, holder);
|
|
89
|
+
}
|
|
90
|
+
}, () => {
|
|
91
|
+
// A closed database or a dropped connection. The next ownership
|
|
92
|
+
// check reads the row and stops the run if the lease is gone.
|
|
93
|
+
});
|
|
94
|
+
}, LOCK_HEARTBEAT_MS),
|
|
95
|
+
lost: false,
|
|
96
|
+
};
|
|
97
|
+
if (typeof beat.timer.unref === "function")
|
|
98
|
+
beat.timer.unref();
|
|
99
|
+
beatsFor(db).set(holder, beat);
|
|
100
|
+
return {
|
|
101
|
+
stop() {
|
|
102
|
+
stopBeat(db, holder);
|
|
103
|
+
},
|
|
104
|
+
lost() {
|
|
105
|
+
return beat.lost;
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Self-fence. `UPDATE ... WHERE id = 1 AND holder = ?`.
|
|
111
|
+
* Zero rows means this run lost the lease and must not write again.
|
|
112
|
+
*/
|
|
113
|
+
export async function refreshLock(db, holder) {
|
|
114
|
+
const now = new Date().toISOString();
|
|
55
115
|
const result = await db
|
|
56
|
-
.prepare(`
|
|
57
|
-
|
|
116
|
+
.prepare(`UPDATE consolidation_lock SET heartbeat_at = ?
|
|
117
|
+
WHERE id = 1 AND holder = ?`)
|
|
118
|
+
.run(now, holder);
|
|
58
119
|
return result.changes > 0;
|
|
59
120
|
}
|
|
121
|
+
function isPgTrue(value) {
|
|
122
|
+
return value === true || value === 1 || value === "t" || value === "true";
|
|
123
|
+
}
|
|
124
|
+
async function releaseAdvisory(db, holder) {
|
|
125
|
+
const hold = advisoryByDb.get(db);
|
|
126
|
+
if (!hold || hold.holder !== holder)
|
|
127
|
+
return;
|
|
128
|
+
advisoryByDb.delete(db);
|
|
129
|
+
try {
|
|
130
|
+
await hold.session.query(`SELECT pg_advisory_unlock($1::bigint)`, [
|
|
131
|
+
CONSOLIDATION_ADVISORY_LOCK_KEY,
|
|
132
|
+
]);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// Dropping the session releases a session lock either way.
|
|
136
|
+
}
|
|
137
|
+
await hold.session.close().catch(() => undefined);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Take the advisory lock on a dedicated session before the row is touched.
|
|
141
|
+
* `acquired` means this call opened that session and must release it if
|
|
142
|
+
* the row lease is not ours. `held` means this process already has it.
|
|
143
|
+
*/
|
|
144
|
+
async function holdAdvisory(db, holder) {
|
|
145
|
+
const existing = advisoryByDb.get(db);
|
|
146
|
+
if (existing)
|
|
147
|
+
return existing.holder === holder ? "held" : "busy";
|
|
148
|
+
const open = postgresSessionOpener(db);
|
|
149
|
+
if (!open)
|
|
150
|
+
return "na";
|
|
151
|
+
const session = await open();
|
|
152
|
+
try {
|
|
153
|
+
const result = await session.query(`SELECT pg_try_advisory_lock($1::bigint) AS locked`, [CONSOLIDATION_ADVISORY_LOCK_KEY]);
|
|
154
|
+
if (!isPgTrue(result.rows[0]?.locked)) {
|
|
155
|
+
await session.close().catch(() => undefined);
|
|
156
|
+
return "busy";
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
await session.close().catch(() => undefined);
|
|
161
|
+
throw err;
|
|
162
|
+
}
|
|
163
|
+
advisoryByDb.set(db, { holder, session });
|
|
164
|
+
watchClose(db);
|
|
165
|
+
return "acquired";
|
|
166
|
+
}
|
|
167
|
+
function leaseAt(row) {
|
|
168
|
+
return row.heartbeat_at ?? row.started_at;
|
|
169
|
+
}
|
|
170
|
+
async function writeLease(db, holder) {
|
|
171
|
+
const now = new Date().toISOString();
|
|
172
|
+
const inserted = await db
|
|
173
|
+
.prepare(`INSERT OR IGNORE INTO consolidation_lock
|
|
174
|
+
(id, holder, started_at, heartbeat_at)
|
|
175
|
+
VALUES (1, ?, ?, ?)`)
|
|
176
|
+
.run(holder, now, now);
|
|
177
|
+
if (inserted.changes > 0)
|
|
178
|
+
return true;
|
|
179
|
+
const existing = (await db
|
|
180
|
+
.prepare(`SELECT holder, started_at, heartbeat_at
|
|
181
|
+
FROM consolidation_lock WHERE id = 1`)
|
|
182
|
+
.get());
|
|
183
|
+
if (!existing)
|
|
184
|
+
return false;
|
|
185
|
+
if (existing.holder === holder) {
|
|
186
|
+
const refreshed = await db
|
|
187
|
+
.prepare(`UPDATE consolidation_lock SET heartbeat_at = ?
|
|
188
|
+
WHERE id = 1 AND holder = ?`)
|
|
189
|
+
.run(now, holder);
|
|
190
|
+
return refreshed.changes > 0;
|
|
191
|
+
}
|
|
192
|
+
const stamp = leaseAt(existing);
|
|
193
|
+
const elapsed = Date.parse(now) - Date.parse(stamp);
|
|
194
|
+
if (!(elapsed > STALE_LOCK_MS))
|
|
195
|
+
return false;
|
|
196
|
+
// Optimistic compare-and-swap: the row we read is still the row we take.
|
|
197
|
+
const taken = await db
|
|
198
|
+
.prepare(`UPDATE consolidation_lock
|
|
199
|
+
SET holder = ?, started_at = ?, heartbeat_at = ?
|
|
200
|
+
WHERE id = 1
|
|
201
|
+
AND holder = ?
|
|
202
|
+
AND COALESCE(heartbeat_at, started_at) = ?`)
|
|
203
|
+
.run(holder, now, now, existing.holder, stamp);
|
|
204
|
+
return taken.changes > 0;
|
|
205
|
+
}
|
|
206
|
+
/** Try to acquire the lock. Returns true if this holder owns the lease. */
|
|
207
|
+
export async function acquireLock(db, holder) {
|
|
208
|
+
const advisory = await holdAdvisory(db, holder);
|
|
209
|
+
if (advisory === "busy")
|
|
210
|
+
return false;
|
|
211
|
+
try {
|
|
212
|
+
const ok = await withTransaction(db, () => writeLease(db, holder));
|
|
213
|
+
if (!ok && advisory === "acquired")
|
|
214
|
+
await releaseAdvisory(db, holder);
|
|
215
|
+
return ok;
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
if (advisory === "acquired")
|
|
219
|
+
await releaseAdvisory(db, holder);
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Release the lease if we still hold it, stop the heartbeat, and drop
|
|
225
|
+
* the advisory connection. A holder mismatch deletes nothing — a run
|
|
226
|
+
* that already lost the row must not remove the new holder's lease.
|
|
227
|
+
*/
|
|
228
|
+
export async function releaseLock(db, holder) {
|
|
229
|
+
stopBeat(db, holder);
|
|
230
|
+
try {
|
|
231
|
+
const result = await db
|
|
232
|
+
.prepare(`DELETE FROM consolidation_lock WHERE id = 1 AND holder = ?`)
|
|
233
|
+
.run(holder);
|
|
234
|
+
return result.changes > 0;
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
await releaseAdvisory(db, holder);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
60
240
|
/** Check if a lock exists and return its state. */
|
|
61
241
|
export async function getLockState(db) {
|
|
62
242
|
const row = (await db
|
|
63
|
-
.prepare(`SELECT holder, started_at
|
|
243
|
+
.prepare(`SELECT holder, started_at, heartbeat_at
|
|
244
|
+
FROM consolidation_lock WHERE id = 1`)
|
|
64
245
|
.get());
|
|
65
246
|
return row ?? null;
|
|
66
247
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consolidation-lock.js","sourceRoot":"","sources":["../../src/db/consolidation-lock.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"consolidation-lock.js","sourceRoot":"","sources":["../../src/db/consolidation-lock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAyBtD,8EAA8E;AAC9E,YAAY;AACZ,8EAA8E;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC;AAExC;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAE3C;;;GAGG;AACH,MAAM,CAAC,MAAM,+BAA+B,GAAG,qBAAqB,CAAC;AAErE,kEAAkE;AAClE,MAAM,CAAC,MAAM,gBAAgB,GAAG,0CAA0C,CAAC;AAgB3E,MAAM,KAAK,GAAG,IAAI,OAAO,EAA6B,CAAC;AACvD,MAAM,YAAY,GAAG,IAAI,OAAO,EAAwB,CAAC;AACzD,MAAM,UAAU,GAAG,IAAI,OAAO,EAAU,CAAC;AAEzC,SAAS,QAAQ,CAAC,EAAU;IAC1B,IAAI,GAAG,GAA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,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,EAAM,EAAE,MAAc;IACtD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IACrB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,EAAE;aACpB,OAAO,CAAC,4DAA4D,CAAC;aACrE,GAAG,CAAC,MAAM,CAAC,CAAC;QACf,OAAO,MAAM,CAAC,OAAO,GAAG,CAAC,CAAC;IAC5B,CAAC;YAAS,CAAC;QACT,MAAM,eAAe,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IACpC,CAAC;AACH,CAAC;AAED,mDAAmD;AACnD,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,EAAM;IACvC,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE;SAClB,OAAO,CACN;4CACsC,CACvC;SACA,GAAG,EAAE,CAA0B,CAAC;IACnC,OAAO,GAAG,IAAI,IAAI,CAAC;AACrB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-backend.d.ts","sourceRoot":"","sources":["../../src/db/pg-backend.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"pg-backend.d.ts","sourceRoot":"","sources":["../../src/db/pg-backend.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAsD1C;;;GAGG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,CAY9D"}
|
package/dist/db/pg-backend.js
CHANGED
|
@@ -11,8 +11,28 @@ import { attachPostgres } from "./postgres.js";
|
|
|
11
11
|
const CONNECT_TIMEOUT_MS = 10_000;
|
|
12
12
|
class PgClientBackend {
|
|
13
13
|
client;
|
|
14
|
-
|
|
14
|
+
url;
|
|
15
|
+
constructor(client, url) {
|
|
15
16
|
this.client = client;
|
|
17
|
+
this.url = url;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A new TCP session. Advisory locks are per session; a pooled or shared
|
|
21
|
+
* client would drop them at the next checkout.
|
|
22
|
+
*/
|
|
23
|
+
async openSession() {
|
|
24
|
+
const client = new Client({
|
|
25
|
+
connectionString: this.url,
|
|
26
|
+
connectionTimeoutMillis: CONNECT_TIMEOUT_MS,
|
|
27
|
+
});
|
|
28
|
+
try {
|
|
29
|
+
await client.connect();
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
await client.end().catch(() => undefined);
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
return new PgClientBackend(client, this.url);
|
|
16
36
|
}
|
|
17
37
|
async query(sql, params) {
|
|
18
38
|
// No parameters → simple query protocol, which can run the multi-statement
|
|
@@ -48,6 +68,6 @@ export async function connectPostgres(url) {
|
|
|
48
68
|
await client.end().catch(() => undefined);
|
|
49
69
|
throw err;
|
|
50
70
|
}
|
|
51
|
-
return attachPostgres(new PgClientBackend(client));
|
|
71
|
+
return attachPostgres(new PgClientBackend(client, url));
|
|
52
72
|
}
|
|
53
73
|
//# sourceMappingURL=pg-backend.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-backend.js","sourceRoot":"","sources":["../../src/db/pg-backend.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAwB,MAAM,eAAe,CAAC;AAGrE,+EAA+E;AAC/E,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,MAAM,eAAe;
|
|
1
|
+
{"version":3,"file":"pg-backend.js","sourceRoot":"","sources":["../../src/db/pg-backend.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,M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