notesoup 0.60.0 → 0.60.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/dist/src/dream/git.js +12 -13
- package/dist/src/notes/project-namespace.js +39 -73
- package/dist/src/sqlite-lock.d.ts +2 -0
- package/dist/src/sqlite-lock.js +17 -0
- package/dist/src/tools/dream.d.ts +2 -4
- package/dist/src/tools/dream.js +15 -16
- package/dist/test/dream-tools.test.js +42 -14
- package/dist/test/notes.integration.test.js +3 -7
- package/dist/test/project-namespace.test.js +33 -88
- package/docs/notes-library.md +6 -0
- package/docs/skills.md +2 -6
- package/package.json +1 -1
- package/plugins/claude/.claude-plugin/plugin.json +1 -1
- package/plugins/claude/dist/claude/helper.js +50 -58
- package/plugins/codex/dist/codex/notesoup.js +50 -58
- package/src/dream/git.ts +11 -11
- package/src/notes/project-namespace.ts +37 -59
- package/src/sqlite-lock.ts +14 -0
- package/src/tools/dream.ts +15 -14
- package/dist/src/dream/lock.d.ts +0 -14
- package/dist/src/dream/lock.js +0 -29
- package/src/dream/lock.ts +0 -28
package/docs/notes-library.md
CHANGED
|
@@ -47,6 +47,12 @@ The library returns full data, not tool envelopes or paginated/truncated output.
|
|
|
47
47
|
|
|
48
48
|
Addresses are a relative path (a current-session note), `@project/`, `@human/`, `@self/`, `@model/`, or explicit `@projects/<slug>/`, `@sessions/<id>/`, `@agents/<slug>/` and `@models/<slug>/`. Named project and session addresses are read-only, including for rename sources and destinations. Session IDs use the same safe single-directory-component grammar as `NotesIdentity.sessionId`, not the agent/model slug grammar. Only the first character of an address is sigil-position: an unknown leading `@head` refuses, while `@` inside the path (`release@v1.md`, `@project/mail/alice@example.com.md`) is an ordinary character. Relative self/model addresses resolve to the supplied identity. Results show the current agent's and model's notes as `@self/` and `@model/`; only other agents' or models' notes carry an explicit `@agents/<slug>/` or `@models/<slug>/`. Globs accept either form. The disk layout remains `pi/session/<sessionId>/`, `project/<projectKey>/`, `human/`, `agents/<agent>/`, and `models/<model>/`. No data migration happens on library import or construction. Notes already using camelCase metadata retain their metadata; old snake_case keys are preserved as unrecognized extras, not interpreted or migrated. New session notes record the supplied project key.
|
|
49
49
|
|
|
50
|
+
### Cross-process project claims
|
|
51
|
+
|
|
52
|
+
An unchanged project marker is resolved without a lock. To allocate a notebook, adopt a remote, or update its display path, the resolver takes a SQLite writer transaction on `<home>/.project-claim.sqlite` and checks the markers again. This requires the built-in `node:sqlite` (available without a flag in Node 22.13+) and a local filesystem. The database stores no notes. Leave the file in place: the transaction, not the file's existence, is the lock. A competing writer waits up to five seconds; if the holder exits, the OS releases its lock.
|
|
53
|
+
|
|
54
|
+
A new notebook is staged under the notes home and renamed into `project/` with its marker already present (both locations must be on the same filesystem). Killing a writer before the rename can leave a hidden staging directory, but it neither claims a name nor blocks other sessions. When upgrading, stop old Notesoup processes sharing the home before running the new version; the old directory lock and SQLite do not coordinate. No notes migration is needed.
|
|
55
|
+
|
|
50
56
|
## Per-note policy
|
|
51
57
|
|
|
52
58
|
`NoteMeta` has optional `pinned?: boolean`, `budget?: number`, and `readonly?: boolean` fields. Set these in on-disk frontmatter or in write/update options. Values are validated on parse and mutation: booleans must be actual booleans; budget must be a positive safe integer. Invalid values raise `NoteError("invalid_policy", ...)` without saving. Unknown frontmatter remains intact across rewrites. As before, a leading frontmatter block in `write`'s **content** is stripped, not used to set metadata; use the options or edit the on-disk frontmatter instead.
|
package/docs/skills.md
CHANGED
|
@@ -18,10 +18,6 @@ One session dreams over one project at a time. New session notes record their pr
|
|
|
18
18
|
|
|
19
19
|
## The dream lock and Git audit
|
|
20
20
|
|
|
21
|
-
`dream_begin` takes
|
|
21
|
+
`dream_begin` takes a SQLite writer transaction on `<home>/.dream.lock.sqlite` and commits a Git baseline. Only one dream can run per notes home, even across projects; competing dreams refuse immediately. This lock is separate from project claims and does not block ordinary note reads or writes. The baseline and end commits snapshot the whole home, so they can include ordinary edits made by other sessions as well as edits made by the dream.
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
```sh
|
|
26
|
-
rm "${NOTESOUP_HOME:-$HOME/.agents/notes}/.dream.lock"
|
|
27
|
-
```
|
|
23
|
+
`dream_end` commits and releases the lock. Session shutdown releases it without a final commit. If the process dies, the OS releases the lock; completed note edits stay on disk. Leave the SQLite file in place. Stop older processes sharing this home when upgrading: the old existence lock and the transaction lock do not coordinate.
|
package/package.json
CHANGED
|
@@ -8395,8 +8395,25 @@ var MAX_NOTE_PATH_BYTES = 512;
|
|
|
8395
8395
|
|
|
8396
8396
|
// src/notes/project-namespace.ts
|
|
8397
8397
|
import { randomUUID } from "node:crypto";
|
|
8398
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync,
|
|
8398
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8399
8399
|
import { basename, dirname, join as join2, resolve as resolve2, sep } from "node:path";
|
|
8400
|
+
|
|
8401
|
+
// src/sqlite-lock.ts
|
|
8402
|
+
function trySqliteLock(path, waitMs) {
|
|
8403
|
+
const sqlite = process.getBuiltinModule?.("node:sqlite");
|
|
8404
|
+
if (!sqlite?.DatabaseSync) throw new Error("Notesoup locks require built-in node:sqlite (Node 22.13+).");
|
|
8405
|
+
const db = new sqlite.DatabaseSync(path);
|
|
8406
|
+
try {
|
|
8407
|
+
db.exec(`PRAGMA busy_timeout = ${waitMs}; BEGIN IMMEDIATE`);
|
|
8408
|
+
} catch (error) {
|
|
8409
|
+
db.close();
|
|
8410
|
+
if (error instanceof Error && "errcode" in error && error.errcode === 5) return void 0;
|
|
8411
|
+
throw error;
|
|
8412
|
+
}
|
|
8413
|
+
return () => db.close();
|
|
8414
|
+
}
|
|
8415
|
+
|
|
8416
|
+
// src/notes/project-namespace.ts
|
|
8400
8417
|
function errno(error) {
|
|
8401
8418
|
return typeof error === "object" && error !== null ? error.code : void 0;
|
|
8402
8419
|
}
|
|
@@ -8509,67 +8526,42 @@ function updateMarker(namespace, marker) {
|
|
|
8509
8526
|
}
|
|
8510
8527
|
return { ...namespace, ...marker };
|
|
8511
8528
|
}
|
|
8512
|
-
|
|
8513
|
-
|
|
8514
|
-
|
|
8515
|
-
for (; ; ) {
|
|
8516
|
-
const result = attempt();
|
|
8517
|
-
if (result !== void 0) return result;
|
|
8518
|
-
const remaining = deadline - performance.now();
|
|
8519
|
-
if (remaining <= 0) throw new Error(`Timed out waiting for project claim at ${path}`);
|
|
8520
|
-
Atomics.wait(claimSleep, 0, 0, Math.min(10, remaining));
|
|
8521
|
-
}
|
|
8529
|
+
function checkoutMarker(cwd) {
|
|
8530
|
+
const path = canonicalRoot(cwd);
|
|
8531
|
+
return { id: originRemote(path) ?? path, path };
|
|
8522
8532
|
}
|
|
8523
8533
|
function resolveProjectNamespace(cwd, home) {
|
|
8524
|
-
|
|
8525
|
-
const
|
|
8526
|
-
|
|
8527
|
-
|
|
8528
|
-
|
|
8529
|
-
|
|
8530
|
-
|
|
8531
|
-
|
|
8532
|
-
throw error;
|
|
8533
|
-
}
|
|
8534
|
-
});
|
|
8534
|
+
const expected = checkoutMarker(cwd);
|
|
8535
|
+
const current = projectNamespaces(home).find((candidate) => candidate.id === expected.id);
|
|
8536
|
+
if (current?.path === expected.path) return current;
|
|
8537
|
+
const root = projectRoot(home);
|
|
8538
|
+
mkdirSync(root, { recursive: true });
|
|
8539
|
+
const lockPath = join2(home, ".project-claim.sqlite");
|
|
8540
|
+
const release = trySqliteLock(lockPath, 5e3);
|
|
8541
|
+
if (!release) throw new Error(`Timed out waiting for project claim at ${lockPath}`);
|
|
8535
8542
|
try {
|
|
8536
|
-
|
|
8537
|
-
|
|
8538
|
-
|
|
8539
|
-
|
|
8540
|
-
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
|
|
8544
|
-
|
|
8545
|
-
|
|
8546
|
-
|
|
8547
|
-
|
|
8548
|
-
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8552
|
-
|
|
8553
|
-
|
|
8554
|
-
|
|
8555
|
-
return found;
|
|
8556
|
-
}
|
|
8557
|
-
const base = slugify(basename(root));
|
|
8558
|
-
const taken = new Set(readdirSync(markerRoot));
|
|
8559
|
-
for (let suffix = 0; ; suffix++) {
|
|
8560
|
-
const name = suffix === 0 ? base : `${base}-${suffix}`;
|
|
8561
|
-
if (taken.has(name)) continue;
|
|
8562
|
-
const dir = join2(markerRoot, name);
|
|
8563
|
-
try {
|
|
8564
|
-
mkdirSync(dir);
|
|
8565
|
-
} catch (error) {
|
|
8566
|
-
if (errno(error) !== "EEXIST") throw error;
|
|
8567
|
-
let winner = awaitClaim(join2(dir, ".project"), () => readMarker(dir));
|
|
8568
|
-
if (winner.id !== id2 && !(remote && winner.id === root)) continue;
|
|
8569
|
-
if (winner.id !== id2 || winner.path !== root) winner = updateMarker(winner, { id: id2, path: root });
|
|
8570
|
-
return winner;
|
|
8543
|
+
const marker = checkoutMarker(cwd);
|
|
8544
|
+
const { id: id2, path } = marker;
|
|
8545
|
+
const namespaces = projectNamespaces(home);
|
|
8546
|
+
const found = namespaces.find((candidate) => candidate.id === id2) ?? namespaces.find((candidate) => candidate.id === path);
|
|
8547
|
+
if (found) return found.id === id2 && found.path === path ? found : updateMarker(found, marker);
|
|
8548
|
+
const base = slugify(basename(path));
|
|
8549
|
+
for (let suffix = 0; ; suffix++) {
|
|
8550
|
+
const name = suffix === 0 ? base : `${base}-${suffix}`;
|
|
8551
|
+
const dir = join2(root, name);
|
|
8552
|
+
if (lstatSync(dir, { throwIfNoEntry: false })) continue;
|
|
8553
|
+
const staged = mkdtempSync(join2(home, ".project-pending-"));
|
|
8554
|
+
try {
|
|
8555
|
+
writeFileSync(join2(staged, ".project"), JSON.stringify(marker));
|
|
8556
|
+
renameSync(staged, dir);
|
|
8557
|
+
} catch (error) {
|
|
8558
|
+
rmSync(staged, { recursive: true, force: true });
|
|
8559
|
+
throw error;
|
|
8560
|
+
}
|
|
8561
|
+
return { name, dir, ...marker };
|
|
8571
8562
|
}
|
|
8572
|
-
|
|
8563
|
+
} finally {
|
|
8564
|
+
release();
|
|
8573
8565
|
}
|
|
8574
8566
|
}
|
|
8575
8567
|
function gitRoot(cwd) {
|
|
@@ -25370,8 +25370,25 @@ var MAX_NOTE_PATH_BYTES = 512;
|
|
|
25370
25370
|
|
|
25371
25371
|
// src/notes/project-namespace.ts
|
|
25372
25372
|
import { randomUUID } from "node:crypto";
|
|
25373
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync,
|
|
25373
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
25374
25374
|
import { basename, dirname, join as join2, resolve as resolve2, sep } from "node:path";
|
|
25375
|
+
|
|
25376
|
+
// src/sqlite-lock.ts
|
|
25377
|
+
function trySqliteLock(path, waitMs) {
|
|
25378
|
+
const sqlite = process.getBuiltinModule?.("node:sqlite");
|
|
25379
|
+
if (!sqlite?.DatabaseSync) throw new Error("Notesoup locks require built-in node:sqlite (Node 22.13+).");
|
|
25380
|
+
const db = new sqlite.DatabaseSync(path);
|
|
25381
|
+
try {
|
|
25382
|
+
db.exec(`PRAGMA busy_timeout = ${waitMs}; BEGIN IMMEDIATE`);
|
|
25383
|
+
} catch (error2) {
|
|
25384
|
+
db.close();
|
|
25385
|
+
if (error2 instanceof Error && "errcode" in error2 && error2.errcode === 5) return void 0;
|
|
25386
|
+
throw error2;
|
|
25387
|
+
}
|
|
25388
|
+
return () => db.close();
|
|
25389
|
+
}
|
|
25390
|
+
|
|
25391
|
+
// src/notes/project-namespace.ts
|
|
25375
25392
|
function errno(error2) {
|
|
25376
25393
|
return typeof error2 === "object" && error2 !== null ? error2.code : void 0;
|
|
25377
25394
|
}
|
|
@@ -25484,67 +25501,42 @@ function updateMarker(namespace, marker) {
|
|
|
25484
25501
|
}
|
|
25485
25502
|
return { ...namespace, ...marker };
|
|
25486
25503
|
}
|
|
25487
|
-
|
|
25488
|
-
|
|
25489
|
-
|
|
25490
|
-
for (; ; ) {
|
|
25491
|
-
const result = attempt();
|
|
25492
|
-
if (result !== void 0) return result;
|
|
25493
|
-
const remaining = deadline - performance.now();
|
|
25494
|
-
if (remaining <= 0) throw new Error(`Timed out waiting for project claim at ${path}`);
|
|
25495
|
-
Atomics.wait(claimSleep, 0, 0, Math.min(10, remaining));
|
|
25496
|
-
}
|
|
25504
|
+
function checkoutMarker(cwd) {
|
|
25505
|
+
const path = canonicalRoot(cwd);
|
|
25506
|
+
return { id: originRemote(path) ?? path, path };
|
|
25497
25507
|
}
|
|
25498
25508
|
function resolveProjectNamespace(cwd, home) {
|
|
25499
|
-
|
|
25500
|
-
const
|
|
25501
|
-
|
|
25502
|
-
|
|
25503
|
-
|
|
25504
|
-
|
|
25505
|
-
|
|
25506
|
-
|
|
25507
|
-
throw error2;
|
|
25508
|
-
}
|
|
25509
|
-
});
|
|
25509
|
+
const expected = checkoutMarker(cwd);
|
|
25510
|
+
const current = projectNamespaces(home).find((candidate) => candidate.id === expected.id);
|
|
25511
|
+
if (current?.path === expected.path) return current;
|
|
25512
|
+
const root = projectRoot(home);
|
|
25513
|
+
mkdirSync(root, { recursive: true });
|
|
25514
|
+
const lockPath = join2(home, ".project-claim.sqlite");
|
|
25515
|
+
const release = trySqliteLock(lockPath, 5e3);
|
|
25516
|
+
if (!release) throw new Error(`Timed out waiting for project claim at ${lockPath}`);
|
|
25510
25517
|
try {
|
|
25511
|
-
|
|
25512
|
-
|
|
25513
|
-
|
|
25514
|
-
|
|
25515
|
-
|
|
25516
|
-
|
|
25517
|
-
|
|
25518
|
-
|
|
25519
|
-
|
|
25520
|
-
|
|
25521
|
-
|
|
25522
|
-
|
|
25523
|
-
|
|
25524
|
-
|
|
25525
|
-
|
|
25526
|
-
|
|
25527
|
-
|
|
25528
|
-
|
|
25529
|
-
|
|
25530
|
-
return found;
|
|
25531
|
-
}
|
|
25532
|
-
const base = slugify2(basename(root));
|
|
25533
|
-
const taken = new Set(readdirSync(markerRoot));
|
|
25534
|
-
for (let suffix = 0; ; suffix++) {
|
|
25535
|
-
const name = suffix === 0 ? base : `${base}-${suffix}`;
|
|
25536
|
-
if (taken.has(name)) continue;
|
|
25537
|
-
const dir = join2(markerRoot, name);
|
|
25538
|
-
try {
|
|
25539
|
-
mkdirSync(dir);
|
|
25540
|
-
} catch (error2) {
|
|
25541
|
-
if (errno(error2) !== "EEXIST") throw error2;
|
|
25542
|
-
let winner = awaitClaim(join2(dir, ".project"), () => readMarker(dir));
|
|
25543
|
-
if (winner.id !== id2 && !(remote && winner.id === root)) continue;
|
|
25544
|
-
if (winner.id !== id2 || winner.path !== root) winner = updateMarker(winner, { id: id2, path: root });
|
|
25545
|
-
return winner;
|
|
25518
|
+
const marker = checkoutMarker(cwd);
|
|
25519
|
+
const { id: id2, path } = marker;
|
|
25520
|
+
const namespaces = projectNamespaces(home);
|
|
25521
|
+
const found = namespaces.find((candidate) => candidate.id === id2) ?? namespaces.find((candidate) => candidate.id === path);
|
|
25522
|
+
if (found) return found.id === id2 && found.path === path ? found : updateMarker(found, marker);
|
|
25523
|
+
const base = slugify2(basename(path));
|
|
25524
|
+
for (let suffix = 0; ; suffix++) {
|
|
25525
|
+
const name = suffix === 0 ? base : `${base}-${suffix}`;
|
|
25526
|
+
const dir = join2(root, name);
|
|
25527
|
+
if (lstatSync(dir, { throwIfNoEntry: false })) continue;
|
|
25528
|
+
const staged = mkdtempSync(join2(home, ".project-pending-"));
|
|
25529
|
+
try {
|
|
25530
|
+
writeFileSync(join2(staged, ".project"), JSON.stringify(marker));
|
|
25531
|
+
renameSync(staged, dir);
|
|
25532
|
+
} catch (error2) {
|
|
25533
|
+
rmSync(staged, { recursive: true, force: true });
|
|
25534
|
+
throw error2;
|
|
25535
|
+
}
|
|
25536
|
+
return { name, dir, ...marker };
|
|
25546
25537
|
}
|
|
25547
|
-
|
|
25538
|
+
} finally {
|
|
25539
|
+
release();
|
|
25548
25540
|
}
|
|
25549
25541
|
}
|
|
25550
25542
|
function gitRoot(cwd) {
|
package/src/dream/git.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { execFileSync, spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { errorMessage } from "../error-message.js";
|
|
5
5
|
|
|
@@ -9,18 +9,18 @@ export type AuditResult =
|
|
|
9
9
|
| { ok: false; error: string };
|
|
10
10
|
|
|
11
11
|
/** Lock runtime artifacts are not notes and must not appear in snapshots or `git status`. */
|
|
12
|
-
const RUNTIME_IGNORE = "
|
|
12
|
+
const RUNTIME_IGNORE = ["/.dream.lock*", "/.project-claim.sqlite", "/.project-claim.sqlite-*", "/.project-pending-*/"];
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/** Install root-only excludes before staging; fail the audit rather than snapshot runtime files. */
|
|
15
15
|
function ensureRuntimeIgnored(home: string): void {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
const exclude = join(home, ".git", "info", "exclude");
|
|
17
|
+
const current = existsSync(exclude) ? readFileSync(exclude, "utf8") : "";
|
|
18
|
+
const lines = new Set(current.split(/\r?\n/));
|
|
19
|
+
const missing = RUNTIME_IGNORE.filter(pattern => !lines.has(pattern));
|
|
20
|
+
if (!missing.length) return;
|
|
21
|
+
mkdirSync(dirname(exclude), { recursive: true });
|
|
22
|
+
const prefix = current.length && !current.endsWith("\n") ? "\n" : "";
|
|
23
|
+
appendFileSync(exclude, prefix + missing.join("\n") + "\n");
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/** Snapshot at begin and end; a repository without HEAD gets an empty baseline commit. */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync,
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { basename, dirname, join, resolve, sep } from "node:path";
|
|
4
|
+
import { trySqliteLock } from "../sqlite-lock.js";
|
|
4
5
|
import { slugify } from "./paths.js";
|
|
5
6
|
|
|
6
7
|
export type ProjectMarker = { id: string; path: string };
|
|
@@ -115,70 +116,47 @@ function updateMarker(namespace: ProjectNamespace, marker: ProjectMarker): Proje
|
|
|
115
116
|
return { ...namespace, ...marker };
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const deadline = performance.now() + 5_000;
|
|
122
|
-
for (;;) {
|
|
123
|
-
const result = attempt();
|
|
124
|
-
if (result !== undefined) return result;
|
|
125
|
-
const remaining = deadline - performance.now();
|
|
126
|
-
if (remaining <= 0) throw new Error(`Timed out waiting for project claim at ${path}`);
|
|
127
|
-
Atomics.wait(claimSleep, 0, 0, Math.min(10, remaining));
|
|
128
|
-
}
|
|
119
|
+
function checkoutMarker(cwd: string): ProjectMarker {
|
|
120
|
+
const path = canonicalRoot(cwd);
|
|
121
|
+
return { id: originRemote(path) ?? path, path };
|
|
129
122
|
}
|
|
130
123
|
|
|
131
124
|
/** Resolve and claim the readable notebook for one repository identity. */
|
|
132
125
|
export function resolveProjectNamespace(cwd: string, home: string): ProjectNamespace {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const lock = join(home, ".project-claim");
|
|
137
|
-
awaitClaim(lock, () => {
|
|
138
|
-
try { mkdirSync(lock); return true; } catch (error) {
|
|
139
|
-
if (errno(error) === "EEXIST") return undefined;
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
try { return claimProjectNamespace(cwd, home); } finally { rmdirSync(lock); }
|
|
144
|
-
}
|
|
126
|
+
const expected = checkoutMarker(cwd);
|
|
127
|
+
const current = projectNamespaces(home).find(candidate => candidate.id === expected.id);
|
|
128
|
+
if (current?.path === expected.path) return current;
|
|
145
129
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (errno(error) !== "EEXIST") throw error;
|
|
174
|
-
// A writer outside this protocol may have won after the snapshot.
|
|
175
|
-
// Never mistake its incomplete publication for a different identity.
|
|
176
|
-
let winner = awaitClaim(join(dir, ".project"), () => readMarker(dir));
|
|
177
|
-
if (winner.id !== id && !(remote && winner.id === root)) continue;
|
|
178
|
-
if (winner.id !== id || winner.path !== root) winner = updateMarker(winner, { id, path: root });
|
|
179
|
-
return winner;
|
|
130
|
+
const root = projectRoot(home);
|
|
131
|
+
mkdirSync(root, { recursive: true });
|
|
132
|
+
const lockPath = join(home, ".project-claim.sqlite");
|
|
133
|
+
const release = trySqliteLock(lockPath, 5000);
|
|
134
|
+
if (!release) throw new Error(`Timed out waiting for project claim at ${lockPath}`);
|
|
135
|
+
try {
|
|
136
|
+
// The checkout may have gained an origin and a peer may have already adopted it.
|
|
137
|
+
const marker = checkoutMarker(cwd);
|
|
138
|
+
const { id, path } = marker;
|
|
139
|
+
const namespaces = projectNamespaces(home);
|
|
140
|
+
const found = namespaces.find(candidate => candidate.id === id) ?? namespaces.find(candidate => candidate.id === path);
|
|
141
|
+
if (found) return found.id === id && found.path === path ? found : updateMarker(found, marker);
|
|
142
|
+
|
|
143
|
+
const base = slugify(basename(path));
|
|
144
|
+
for (let suffix = 0; ; suffix++) {
|
|
145
|
+
const name = suffix === 0 ? base : `${base}-${suffix}`;
|
|
146
|
+
const dir = join(root, name);
|
|
147
|
+
if (lstatSync(dir, { throwIfNoEntry: false })) continue; // Includes unmarked legacy names.
|
|
148
|
+
const staged = mkdtempSync(join(home, ".project-pending-"));
|
|
149
|
+
try {
|
|
150
|
+
writeFileSync(join(staged, ".project"), JSON.stringify(marker));
|
|
151
|
+
renameSync(staged, dir);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
rmSync(staged, { recursive: true, force: true });
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
return { name, dir, ...marker };
|
|
180
157
|
}
|
|
181
|
-
|
|
158
|
+
} finally {
|
|
159
|
+
release();
|
|
182
160
|
}
|
|
183
161
|
}
|
|
184
162
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The open transaction is the lock. Never unlink the database, even after a crash. */
|
|
2
|
+
export function trySqliteLock(path: string, waitMs: 0 | 5000): (() => void) | undefined {
|
|
3
|
+
const sqlite = process.getBuiltinModule?.("node:sqlite") as typeof import("node:sqlite") | undefined;
|
|
4
|
+
if (!sqlite?.DatabaseSync) throw new Error("Notesoup locks require built-in node:sqlite (Node 22.13+).");
|
|
5
|
+
const db = new sqlite.DatabaseSync(path);
|
|
6
|
+
try {
|
|
7
|
+
db.exec(`PRAGMA busy_timeout = ${waitMs}; BEGIN IMMEDIATE`);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
db.close();
|
|
10
|
+
if (error instanceof Error && "errcode" in error && error.errcode === 5) return undefined; // SQLITE_BUSY
|
|
11
|
+
throw error;
|
|
12
|
+
}
|
|
13
|
+
return () => db.close(); // Closing rolls back the empty transaction.
|
|
14
|
+
}
|
package/src/tools/dream.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
/** Dream operations: open one project's notes for consolidation, delete crumpled notes, close. */
|
|
2
|
+
import { join } from "node:path";
|
|
2
3
|
import { Type, type Static } from "typebox";
|
|
3
4
|
import { localIso } from "../notes/frontmatter.js";
|
|
4
5
|
import { createNotesStore, noteIdentity, type NoteRow, type NotesIdentity } from "../notes/index.js";
|
|
5
6
|
import { projectNamespaces } from "../notes/project-namespace.js";
|
|
6
7
|
import { gitCommit } from "../dream/git.js";
|
|
7
|
-
import {
|
|
8
|
+
import { trySqliteLock } from "../sqlite-lock.js";
|
|
8
9
|
import { failure, success, type Outcome } from "../result.js";
|
|
9
10
|
import { renderOutcome, resultSchema } from "./result.js";
|
|
10
11
|
import { failureOf, listedFile, ListedFileSchema, NoteIdentitySchema, renderListData, snapshot, SnapshotSchema } from "./notes.js";
|
|
11
12
|
|
|
12
13
|
/** The one open dream per session; the host owns where it lives. */
|
|
13
|
-
type DreamOpening = { project: string;
|
|
14
|
-
export type DreamState = { project?: string;
|
|
14
|
+
type DreamOpening = { project: string; release: () => void; generation: number };
|
|
15
|
+
export type DreamState = { project?: string; release?: () => void; pending?: DreamOpening; generation?: number };
|
|
15
16
|
|
|
16
17
|
function currentOpening(state: DreamState, opening: DreamOpening): boolean {
|
|
17
18
|
return state.pending === opening && state.generation === opening.generation;
|
|
@@ -25,19 +26,19 @@ function nextGeneration(state: DreamState): number {
|
|
|
25
26
|
|
|
26
27
|
/** Drop an open dream without an end commit, e.g. when its session shuts down. */
|
|
27
28
|
export function abandonDream(state: DreamState): void {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (state.lock && state.lock !== pending?.lock) releaseDreamLock(state.lock);
|
|
29
|
+
state.pending?.release();
|
|
30
|
+
state.release?.();
|
|
31
31
|
nextGeneration(state);
|
|
32
32
|
delete state.project;
|
|
33
|
-
delete state.
|
|
33
|
+
delete state.release;
|
|
34
34
|
delete state.pending;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/** Release an opening without disturbing a newer opening in the same state. */
|
|
38
38
|
function releaseOpening(state: DreamState, opening: DreamOpening): void {
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
if (!currentOpening(state, opening)) return;
|
|
40
|
+
opening.release();
|
|
41
|
+
delete state.pending;
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
function cancelledOpening(state: DreamState, opening: DreamOpening): Outcome<never> {
|
|
@@ -80,10 +81,10 @@ export const dreamBegin = {
|
|
|
80
81
|
if (project !== identity.projectKey && !projectNamespaces(identity.home).some(namespace => namespace.name === project)) {
|
|
81
82
|
return failure("not_found", `No project named ${JSON.stringify(project)}; notes_list with glob @projects/*/** shows the names.`);
|
|
82
83
|
}
|
|
83
|
-
const
|
|
84
|
-
if (!
|
|
85
|
-
// Publish ownership before the first await
|
|
86
|
-
const opening: DreamOpening = { project,
|
|
84
|
+
const release = trySqliteLock(join(identity.home, ".dream.lock.sqlite"), 0);
|
|
85
|
+
if (!release) return failure("already_dreaming", "Another dream is open in this notes home.");
|
|
86
|
+
// Publish ownership before the first await so shutdown can release this transaction.
|
|
87
|
+
const opening: DreamOpening = { project, release, generation: nextGeneration(state) };
|
|
87
88
|
state.pending = opening;
|
|
88
89
|
try {
|
|
89
90
|
const store = createNotesStore({ ...identity, dreaming: project });
|
|
@@ -111,7 +112,7 @@ export const dreamBegin = {
|
|
|
111
112
|
}
|
|
112
113
|
if (!currentOpening(state, opening)) return cancelledOpening(state, opening);
|
|
113
114
|
state.project = opening.project;
|
|
114
|
-
state.
|
|
115
|
+
state.release = opening.release;
|
|
115
116
|
delete state.pending;
|
|
116
117
|
return success({ project, baseline: baseline.commit, ...listed.data });
|
|
117
118
|
} catch (error) {
|
package/dist/src/dream/lock.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/** One held dream lock; only the holder that wrote the token removes it. */
|
|
2
|
-
export type DreamLock = {
|
|
3
|
-
path: string;
|
|
4
|
-
token: string;
|
|
5
|
-
};
|
|
6
|
-
/** One lock per notes home: its Git history cannot audit concurrent dreams independently. */
|
|
7
|
-
export declare function dreamLockPath(home: string): string;
|
|
8
|
-
/**
|
|
9
|
-
* Git-style exclusive existence lock: one O_CREAT|O_EXCL creation. An existing lock refuses,
|
|
10
|
-
* whatever its age; a crash-left lock is removed by hand after confirming no dream runs.
|
|
11
|
-
*/
|
|
12
|
-
export declare function acquireDreamLock(path: string): DreamLock | undefined;
|
|
13
|
-
/** Release only a lock this holder wrote; repeated release is harmless. */
|
|
14
|
-
export declare function releaseDreamLock(lock: DreamLock): void;
|
package/dist/src/dream/lock.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
/** One lock per notes home: its Git history cannot audit concurrent dreams independently. */
|
|
5
|
-
export function dreamLockPath(home) {
|
|
6
|
-
return join(home, ".dream.lock");
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Git-style exclusive existence lock: one O_CREAT|O_EXCL creation. An existing lock refuses,
|
|
10
|
-
* whatever its age; a crash-left lock is removed by hand after confirming no dream runs.
|
|
11
|
-
*/
|
|
12
|
-
export function acquireDreamLock(path) {
|
|
13
|
-
const token = `${process.pid} ${randomUUID()}`;
|
|
14
|
-
try {
|
|
15
|
-
writeFileSync(path, token, { flag: "wx" });
|
|
16
|
-
}
|
|
17
|
-
catch {
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
return { path, token };
|
|
21
|
-
}
|
|
22
|
-
/** Release only a lock this holder wrote; repeated release is harmless. */
|
|
23
|
-
export function releaseDreamLock(lock) {
|
|
24
|
-
try {
|
|
25
|
-
if (readFileSync(lock.path, "utf8") === lock.token)
|
|
26
|
-
unlinkSync(lock.path);
|
|
27
|
-
}
|
|
28
|
-
catch { /* already gone */ }
|
|
29
|
-
}
|
package/src/dream/lock.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
/** One held dream lock; only the holder that wrote the token removes it. */
|
|
6
|
-
export type DreamLock = { path: string; token: string };
|
|
7
|
-
|
|
8
|
-
/** One lock per notes home: its Git history cannot audit concurrent dreams independently. */
|
|
9
|
-
export function dreamLockPath(home: string): string {
|
|
10
|
-
return join(home, ".dream.lock");
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Git-style exclusive existence lock: one O_CREAT|O_EXCL creation. An existing lock refuses,
|
|
15
|
-
* whatever its age; a crash-left lock is removed by hand after confirming no dream runs.
|
|
16
|
-
*/
|
|
17
|
-
export function acquireDreamLock(path: string): DreamLock | undefined {
|
|
18
|
-
const token = `${process.pid} ${randomUUID()}`;
|
|
19
|
-
try { writeFileSync(path, token, { flag: "wx" }); } catch { return undefined; }
|
|
20
|
-
return { path, token };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Release only a lock this holder wrote; repeated release is harmless. */
|
|
24
|
-
export function releaseDreamLock(lock: DreamLock): void {
|
|
25
|
-
try {
|
|
26
|
-
if (readFileSync(lock.path, "utf8") === lock.token) unlinkSync(lock.path);
|
|
27
|
-
} catch { /* already gone */ }
|
|
28
|
-
}
|