@llblab/pi-state-flow 0.13.3 → 0.13.4
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/CHANGELOG.md +4 -0
- package/dist/lib/git.js +3 -9
- package/dist/lib/storage.d.ts +2 -0
- package/dist/lib/storage.js +42 -8
- package/dist/package.json +1 -1
- package/lib/git.ts +5 -8
- package/lib/storage.ts +32 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
## Unreleased
|
|
6
6
|
|
|
7
|
+
## 0.13.4: Concurrent publication hotfix
|
|
8
|
+
|
|
9
|
+
- `Publication contention`: A publisher now waits briefly for a cooperating live State Flow process to release the storage or shared Git lock, preventing transient concurrent `patch_state` calls from failing while preserving explicit errors for interrupted, malformed, reentrant, or prolonged lock ownership.
|
|
10
|
+
|
|
7
11
|
## 0.13.3: Telegram section discovery hotfix
|
|
8
12
|
|
|
9
13
|
- `Telegram section discovery`: The compiled extension now resolves the sibling pi-telegram public sections membrane from its compiled layout as well as the package export, restoring the State Flow control in the Telegram main menu without making Telegram a core dependency.
|
package/dist/lib/git.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Domain: durable Git revision reads, compare-and-swap publication, and exact-commit push retry.
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
-
import { closeSync, lstatSync, mkdirSync, mkdtempSync,
|
|
3
|
+
import { closeSync, lstatSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { dirname, relative, resolve, sep } from "node:path";
|
|
6
6
|
import { spawn, spawnSync } from "node:child_process";
|
|
@@ -10,7 +10,7 @@ import { planLegacyStorageMigration } from "./migration.js";
|
|
|
10
10
|
import { sameJson } from "./json.js";
|
|
11
11
|
import { validateTemporalState } from "./temporal.js";
|
|
12
12
|
import { createSessionRuntime, parseSessionRuntime, serializeSessionRuntime } from "./snapshot.js";
|
|
13
|
-
import { assertTemporalFileBase, loadTemporalFileRevision, planTemporalPublication, temporalFileReceipts, withStoragePublicationLock } from "./storage.js";
|
|
13
|
+
import { acquirePublicationLock, assertTemporalFileBase, loadTemporalFileRevision, planTemporalPublication, temporalFileReceipts, withStoragePublicationLock } from "./storage.js";
|
|
14
14
|
const GIT_TIMEOUT_MS = 15_000;
|
|
15
15
|
const STATE_FLOW_COMMIT_TRAILER = "State-Flow-Durable: v1";
|
|
16
16
|
function git(repositoryRoot, args, options = {}) {
|
|
@@ -72,13 +72,7 @@ function withPublicationLock(repositoryRoot, action) {
|
|
|
72
72
|
const root = assertRepositoryRoot(lockedRoot);
|
|
73
73
|
const common = resolve(root, git(root, ["rev-parse", "--git-common-dir"]).stdout.trim());
|
|
74
74
|
const path = resolve(common, "state-flow-publication.lock");
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
descriptor = openSync(path, "wx", 0o600);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
throw new Error(`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause: error });
|
|
81
|
-
}
|
|
75
|
+
const descriptor = acquirePublicationLock(path, (cause) => new Error(`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause }));
|
|
82
76
|
try {
|
|
83
77
|
writeFileSync(descriptor, `${process.pid}\n`);
|
|
84
78
|
return action(root);
|
package/dist/lib/storage.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export declare function detectGitCapability(): "git" | "files";
|
|
|
12
12
|
export declare function assertStorageDirectory(path: string): void;
|
|
13
13
|
/** Explicit creation only; existing bytes and unrelated files are never adopted or rewritten here. */
|
|
14
14
|
export declare function initializeFileStore(root: string): void;
|
|
15
|
+
/** Wait only for a cooperating live owner; interrupted or malformed locks remain explicit recovery errors. */
|
|
16
|
+
export declare function acquirePublicationLock(path: string, unavailable: (cause: unknown) => Error): number;
|
|
15
17
|
/** Git writers also acquire this lock before their common-Git-directory lock. */
|
|
16
18
|
export declare function withStoragePublicationLock<T>(repositoryRoot: string, action: (root: string) => T): T;
|
|
17
19
|
export declare function assertTemporalFileBase(expected: TemporalFileBase, current: TemporalFileBase): void;
|
package/dist/lib/storage.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Excludes: temporal algebra, Pi lifecycle, Git objects/remotes, and backend fallback policy.
|
|
3
3
|
import { spawnSync } from "node:child_process";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
|
-
import { closeSync, lstatSync, mkdirSync, openSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { dirname, relative, resolve } from "node:path";
|
|
7
7
|
import { assertOwnedFileUpdates, captureTemporalFileBases, parseScopeProvenance, parseScopeStream, restoreDurableFileBases, serializeScopeMetadata, sessionRuntimePaths, temporalScopePaths, temporalStateFileUpdates, writeOwnedFileUpdates, } from "./durable.js";
|
|
8
8
|
import { parseArtifactProvenanceRegistry } from "./artifact.js";
|
|
@@ -36,18 +36,52 @@ export function initializeFileStore(root) {
|
|
|
36
36
|
assertStorageDirectory(root);
|
|
37
37
|
mkdirSync(resolve(root), { recursive: true });
|
|
38
38
|
}
|
|
39
|
+
const PUBLICATION_LOCK_WAIT_MS = 2_000;
|
|
40
|
+
const PUBLICATION_LOCK_POLL_MS = 25;
|
|
41
|
+
const publicationLockWait = new Int32Array(new SharedArrayBuffer(4));
|
|
42
|
+
function liveForeignLockOwner(path) {
|
|
43
|
+
let owner;
|
|
44
|
+
try {
|
|
45
|
+
owner = readFileSync(path, "utf8").trim();
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
if (owner.length === 0)
|
|
51
|
+
return true;
|
|
52
|
+
if (!/^[1-9]\d*$/.test(owner))
|
|
53
|
+
return false;
|
|
54
|
+
const pid = Number(owner);
|
|
55
|
+
if (!Number.isSafeInteger(pid) || pid === process.pid)
|
|
56
|
+
return false;
|
|
57
|
+
try {
|
|
58
|
+
process.kill(pid, 0);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return error.code === "EPERM";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Wait only for a cooperating live owner; interrupted or malformed locks remain explicit recovery errors. */
|
|
66
|
+
export function acquirePublicationLock(path, unavailable) {
|
|
67
|
+
const deadline = Date.now() + PUBLICATION_LOCK_WAIT_MS;
|
|
68
|
+
while (true) {
|
|
69
|
+
try {
|
|
70
|
+
return openSync(path, "wx", 0o600);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error.code !== "EEXIST" || !liveForeignLockOwner(path) || Date.now() >= deadline)
|
|
74
|
+
throw unavailable(error);
|
|
75
|
+
Atomics.wait(publicationLockWait, 0, 0, Math.min(PUBLICATION_LOCK_POLL_MS, deadline - Date.now()));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
39
79
|
/** Git writers also acquire this lock before their common-Git-directory lock. */
|
|
40
80
|
export function withStoragePublicationLock(repositoryRoot, action) {
|
|
41
81
|
const root = resolve(repositoryRoot);
|
|
42
82
|
assertStorageDirectory(root);
|
|
43
83
|
const path = resolve(root, ".state-flow-publication.lock");
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
descriptor = openSync(path, "wx", 0o600);
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
throw new RevisionUnavailableError(`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause: error });
|
|
50
|
-
}
|
|
84
|
+
const descriptor = acquirePublicationLock(path, (cause) => new RevisionUnavailableError(`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause }));
|
|
51
85
|
try {
|
|
52
86
|
writeFileSync(descriptor, `${process.pid}\n`);
|
|
53
87
|
return action(root);
|
package/dist/package.json
CHANGED
package/lib/git.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Domain: durable Git revision reads, compare-and-swap publication, and exact-commit push retry.
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
-
import { closeSync, lstatSync, mkdirSync, mkdtempSync,
|
|
3
|
+
import { closeSync, lstatSync, mkdirSync, mkdtempSync, rmdirSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { dirname, relative, resolve, sep } from "node:path";
|
|
6
6
|
import { spawn, spawnSync } from "node:child_process";
|
|
@@ -25,7 +25,7 @@ import { planLegacyStorageMigration } from "./migration.ts";
|
|
|
25
25
|
import { canonicalJson, sameJson } from "./json.ts";
|
|
26
26
|
import { validateTemporalState, type ScopeStream, type TemporalState } from "./temporal.ts";
|
|
27
27
|
import { createSessionRuntime, parseSessionRuntime, serializeSessionRuntime, type SessionRuntime, type Snapshot } from "./snapshot.ts";
|
|
28
|
-
import { assertTemporalFileBase, loadTemporalFileRevision, planTemporalPublication, temporalFileReceipts, withStoragePublicationLock } from "./storage.ts";
|
|
28
|
+
import { acquirePublicationLock, assertTemporalFileBase, loadTemporalFileRevision, planTemporalPublication, temporalFileReceipts, withStoragePublicationLock } from "./storage.ts";
|
|
29
29
|
import type { MaterializedState, StateScope } from "./state.ts";
|
|
30
30
|
|
|
31
31
|
const GIT_TIMEOUT_MS = 15_000;
|
|
@@ -108,12 +108,9 @@ function withPublicationLock<T>(repositoryRoot: string, action: (root: string) =
|
|
|
108
108
|
const root = assertRepositoryRoot(lockedRoot);
|
|
109
109
|
const common = resolve(root, git(root, ["rev-parse", "--git-common-dir"]).stdout.trim());
|
|
110
110
|
const path = resolve(common, "state-flow-publication.lock");
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
} catch (error) {
|
|
115
|
-
throw new Error(`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause: error });
|
|
116
|
-
}
|
|
111
|
+
const descriptor = acquirePublicationLock(path, (cause) => new Error(
|
|
112
|
+
`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause },
|
|
113
|
+
));
|
|
117
114
|
try {
|
|
118
115
|
writeFileSync(descriptor, `${process.pid}\n`);
|
|
119
116
|
return action(root);
|
package/lib/storage.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Excludes: temporal algebra, Pi lifecycle, Git objects/remotes, and backend fallback policy.
|
|
3
3
|
import { spawnSync } from "node:child_process";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
|
-
import { closeSync, lstatSync, mkdirSync, openSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { dirname, relative, resolve } from "node:path";
|
|
7
7
|
import {
|
|
8
8
|
assertOwnedFileUpdates, captureTemporalFileBases, parseScopeProvenance, parseScopeStream, restoreDurableFileBases,
|
|
@@ -44,17 +44,42 @@ export function initializeFileStore(root: string): void {
|
|
|
44
44
|
mkdirSync(resolve(root), { recursive: true });
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
const PUBLICATION_LOCK_WAIT_MS = 2_000;
|
|
48
|
+
const PUBLICATION_LOCK_POLL_MS = 25;
|
|
49
|
+
const publicationLockWait = new Int32Array(new SharedArrayBuffer(4));
|
|
50
|
+
|
|
51
|
+
function liveForeignLockOwner(path: string): boolean {
|
|
52
|
+
let owner: string;
|
|
53
|
+
try { owner = readFileSync(path, "utf8").trim(); }
|
|
54
|
+
catch { return true; }
|
|
55
|
+
if (owner.length === 0) return true;
|
|
56
|
+
if (!/^[1-9]\d*$/.test(owner)) return false;
|
|
57
|
+
const pid = Number(owner);
|
|
58
|
+
if (!Number.isSafeInteger(pid) || pid === process.pid) return false;
|
|
59
|
+
try { process.kill(pid, 0); return true; }
|
|
60
|
+
catch (error) { return (error as NodeJS.ErrnoException).code === "EPERM"; }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Wait only for a cooperating live owner; interrupted or malformed locks remain explicit recovery errors. */
|
|
64
|
+
export function acquirePublicationLock(path: string, unavailable: (cause: unknown) => Error): number {
|
|
65
|
+
const deadline = Date.now() + PUBLICATION_LOCK_WAIT_MS;
|
|
66
|
+
while (true) {
|
|
67
|
+
try { return openSync(path, "wx", 0o600); }
|
|
68
|
+
catch (error) {
|
|
69
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST" || !liveForeignLockOwner(path) || Date.now() >= deadline) throw unavailable(error);
|
|
70
|
+
Atomics.wait(publicationLockWait, 0, 0, Math.min(PUBLICATION_LOCK_POLL_MS, deadline - Date.now()));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
47
75
|
/** Git writers also acquire this lock before their common-Git-directory lock. */
|
|
48
76
|
export function withStoragePublicationLock<T>(repositoryRoot: string, action: (root: string) => T): T {
|
|
49
77
|
const root = resolve(repositoryRoot);
|
|
50
78
|
assertStorageDirectory(root);
|
|
51
79
|
const path = resolve(root, ".state-flow-publication.lock");
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
} catch (error) {
|
|
56
|
-
throw new RevisionUnavailableError(`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause: error });
|
|
57
|
-
}
|
|
80
|
+
const descriptor = acquirePublicationLock(path, (cause) => new RevisionUnavailableError(
|
|
81
|
+
`State Flow publication lock is unavailable at ${path}; reconcile the active or interrupted publisher before retrying`, { cause },
|
|
82
|
+
));
|
|
58
83
|
try {
|
|
59
84
|
writeFileSync(descriptor, `${process.pid}\n`);
|
|
60
85
|
return action(root);
|