@llblab/pi-state-flow 0.13.2 → 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 +8 -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/lib/telegram.d.ts +2 -0
- package/dist/lib/telegram.js +8 -5
- package/dist/package.json +1 -1
- package/lib/git.ts +5 -8
- package/lib/storage.ts +32 -7
- package/lib/telegram.ts +9 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,14 @@
|
|
|
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
|
+
|
|
11
|
+
## 0.13.3: Telegram section discovery hotfix
|
|
12
|
+
|
|
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.
|
|
14
|
+
|
|
7
15
|
## 0.13.2: Benign compaction readiness
|
|
8
16
|
|
|
9
17
|
- `Benign compaction readiness`: Automatic State Flow compaction now requires enough persisted message history as well as high total context usage, so a short session enlarged only by its system prompt remains a quiet non-action instead of surfacing Pi's native `Nothing to compact` failure.
|
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/lib/telegram.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const STATE_FLOW_TELEGRAM_ID = "@llblab/pi-state-flow";
|
|
2
|
+
/** Resolve the package export or the compiled sibling-extension layout used in local development. */
|
|
3
|
+
export declare function stateFlowTelegramSectionSpecifiers(moduleUrl?: string): string[];
|
|
2
4
|
export interface StateFlowTelegramSnapshot {
|
|
3
5
|
enabled: boolean;
|
|
4
6
|
step: number;
|
package/dist/lib/telegram.js
CHANGED
|
@@ -11,10 +11,13 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
11
11
|
return path;
|
|
12
12
|
};
|
|
13
13
|
export const STATE_FLOW_TELEGRAM_ID = "@llblab/pi-state-flow";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
/** Resolve the package export or the compiled sibling-extension layout used in local development. */
|
|
15
|
+
export function stateFlowTelegramSectionSpecifiers(moduleUrl = import.meta.url) {
|
|
16
|
+
return [
|
|
17
|
+
"@llblab/pi-telegram/sections",
|
|
18
|
+
new URL("../../../pi-telegram/dist/api/sections.js", moduleUrl).href,
|
|
19
|
+
];
|
|
20
|
+
}
|
|
18
21
|
/** Main-menu section label doubles as the live status value: the spiral identity is constant, the value is not. */
|
|
19
22
|
export function formatStateFlowSectionLabel(snapshot) {
|
|
20
23
|
return `🌀 State Flow: ${stateFlowLabelValue(snapshot)}`;
|
|
@@ -168,7 +171,7 @@ async function importTelegramModule(specifiers, guard) {
|
|
|
168
171
|
}
|
|
169
172
|
/** Default loader; injectable so tests and embedded hosts can control transport presence. */
|
|
170
173
|
export async function loadStateFlowTelegramModules() {
|
|
171
|
-
const sections = await importTelegramModule(
|
|
174
|
+
const sections = await importTelegramModule(stateFlowTelegramSectionSpecifiers(), (module) => typeof module?.registerTelegramSection === "function");
|
|
172
175
|
return { ...(sections === undefined ? {} : { sections }) };
|
|
173
176
|
}
|
|
174
177
|
export function createStateFlowTelegramAdapter(options) {
|
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);
|
package/lib/telegram.ts
CHANGED
|
@@ -4,10 +4,14 @@
|
|
|
4
4
|
// pi-telegram is absent or its registry is not ready, registration fails open and retries.
|
|
5
5
|
|
|
6
6
|
export const STATE_FLOW_TELEGRAM_ID = "@llblab/pi-state-flow";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
|
|
8
|
+
/** Resolve the package export or the compiled sibling-extension layout used in local development. */
|
|
9
|
+
export function stateFlowTelegramSectionSpecifiers(moduleUrl = import.meta.url): string[] {
|
|
10
|
+
return [
|
|
11
|
+
"@llblab/pi-telegram/sections",
|
|
12
|
+
new URL("../../../pi-telegram/dist/api/sections.js", moduleUrl).href,
|
|
13
|
+
];
|
|
14
|
+
}
|
|
11
15
|
|
|
12
16
|
export interface StateFlowTelegramSnapshot {
|
|
13
17
|
enabled: boolean;
|
|
@@ -259,7 +263,7 @@ async function importTelegramModule<TModule>(
|
|
|
259
263
|
/** Default loader; injectable so tests and embedded hosts can control transport presence. */
|
|
260
264
|
export async function loadStateFlowTelegramModules(): Promise<StateFlowTelegramModules> {
|
|
261
265
|
const sections = await importTelegramModule<StateFlowTelegramSectionModule>(
|
|
262
|
-
|
|
266
|
+
stateFlowTelegramSectionSpecifiers(),
|
|
263
267
|
(module): module is StateFlowTelegramSectionModule =>
|
|
264
268
|
typeof (module as StateFlowTelegramSectionModule | undefined)?.registerTelegramSection === "function",
|
|
265
269
|
);
|