@amaranthic/hernex 0.0.0-stage → 0.3.0
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 +261 -0
- package/LICENSE +21 -0
- package/README.md +66 -2
- package/dist/ai.d.ts +11 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +11 -0
- package/dist/ai.js.map +1 -0
- package/dist/checkpoints/FileCheckpointStore.d.ts +95 -0
- package/dist/checkpoints/FileCheckpointStore.d.ts.map +1 -0
- package/dist/checkpoints/FileCheckpointStore.js +502 -0
- package/dist/checkpoints/FileCheckpointStore.js.map +1 -0
- package/dist/checkpoints/index.d.ts +7 -0
- package/dist/checkpoints/index.d.ts.map +1 -0
- package/dist/checkpoints/index.js +7 -0
- package/dist/checkpoints/index.js.map +1 -0
- package/dist/checkpoints/types.d.ts +157 -0
- package/dist/checkpoints/types.d.ts.map +1 -0
- package/dist/checkpoints/types.js +6 -0
- package/dist/checkpoints/types.js.map +1 -0
- package/dist/engine/AgentEngine.d.ts +156 -0
- package/dist/engine/AgentEngine.d.ts.map +1 -0
- package/dist/engine/AgentEngine.js +290 -0
- package/dist/engine/AgentEngine.js.map +1 -0
- package/dist/engine/engineConfig.d.ts +138 -0
- package/dist/engine/engineConfig.d.ts.map +1 -0
- package/dist/engine/engineConfig.js +6 -0
- package/dist/engine/engineConfig.js.map +1 -0
- package/dist/engine/eventQueue.d.ts +42 -0
- package/dist/engine/eventQueue.d.ts.map +1 -0
- package/dist/engine/eventQueue.js +93 -0
- package/dist/engine/eventQueue.js.map +1 -0
- package/dist/engine/index.d.ts +7 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +6 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/runTurn.d.ts +17 -0
- package/dist/engine/runTurn.d.ts.map +1 -0
- package/dist/engine/runTurn.js +718 -0
- package/dist/engine/runTurn.js.map +1 -0
- package/dist/errors.d.ts +194 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +219 -0
- package/dist/errors.js.map +1 -0
- package/dist/events/checkpointEvents.d.ts +20 -0
- package/dist/events/checkpointEvents.d.ts.map +1 -0
- package/dist/events/checkpointEvents.js +6 -0
- package/dist/events/checkpointEvents.js.map +1 -0
- package/dist/events/errorEvents.d.ts +23 -0
- package/dist/events/errorEvents.d.ts.map +1 -0
- package/dist/events/errorEvents.js +6 -0
- package/dist/events/errorEvents.js.map +1 -0
- package/dist/events/index.d.ts +11 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +6 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/stepEvents.d.ts +68 -0
- package/dist/events/stepEvents.d.ts.map +1 -0
- package/dist/events/stepEvents.js +6 -0
- package/dist/events/stepEvents.js.map +1 -0
- package/dist/events/streamEvents.d.ts +41 -0
- package/dist/events/streamEvents.d.ts.map +1 -0
- package/dist/events/streamEvents.js +6 -0
- package/dist/events/streamEvents.js.map +1 -0
- package/dist/events/toolEvents.d.ts +64 -0
- package/dist/events/toolEvents.d.ts.map +1 -0
- package/dist/events/toolEvents.js +6 -0
- package/dist/events/toolEvents.js.map +1 -0
- package/dist/events/types.d.ts +44 -0
- package/dist/events/types.d.ts.map +1 -0
- package/dist/events/types.js +25 -0
- package/dist/events/types.js.map +1 -0
- package/dist/hooks/composeAgentHooks.d.ts +14 -0
- package/dist/hooks/composeAgentHooks.d.ts.map +1 -0
- package/dist/hooks/composeAgentHooks.js +154 -0
- package/dist/hooks/composeAgentHooks.js.map +1 -0
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +6 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/types.d.ts +133 -0
- package/dist/hooks/types.d.ts.map +1 -0
- package/dist/hooks/types.js +6 -0
- package/dist/hooks/types.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/model/ProviderRegistry.d.ts +62 -0
- package/dist/model/ProviderRegistry.d.ts.map +1 -0
- package/dist/model/ProviderRegistry.js +114 -0
- package/dist/model/ProviderRegistry.js.map +1 -0
- package/dist/model/providers/alibaba.d.ts +11 -0
- package/dist/model/providers/alibaba.d.ts.map +1 -0
- package/dist/model/providers/alibaba.js +16 -0
- package/dist/model/providers/alibaba.js.map +1 -0
- package/dist/model/providers/anthropic.d.ts +11 -0
- package/dist/model/providers/anthropic.d.ts.map +1 -0
- package/dist/model/providers/anthropic.js +16 -0
- package/dist/model/providers/anthropic.js.map +1 -0
- package/dist/model/providers/cohere.d.ts +11 -0
- package/dist/model/providers/cohere.d.ts.map +1 -0
- package/dist/model/providers/cohere.js +16 -0
- package/dist/model/providers/cohere.js.map +1 -0
- package/dist/model/providers/compatible.d.ts +11 -0
- package/dist/model/providers/compatible.d.ts.map +1 -0
- package/dist/model/providers/compatible.js +17 -0
- package/dist/model/providers/compatible.js.map +1 -0
- package/dist/model/providers/deepseek.d.ts +11 -0
- package/dist/model/providers/deepseek.d.ts.map +1 -0
- package/dist/model/providers/deepseek.js +16 -0
- package/dist/model/providers/deepseek.js.map +1 -0
- package/dist/model/providers/google.d.ts +11 -0
- package/dist/model/providers/google.d.ts.map +1 -0
- package/dist/model/providers/google.js +16 -0
- package/dist/model/providers/google.js.map +1 -0
- package/dist/model/providers/groq.d.ts +11 -0
- package/dist/model/providers/groq.d.ts.map +1 -0
- package/dist/model/providers/groq.js +16 -0
- package/dist/model/providers/groq.js.map +1 -0
- package/dist/model/providers/mistral.d.ts +11 -0
- package/dist/model/providers/mistral.d.ts.map +1 -0
- package/dist/model/providers/mistral.js +16 -0
- package/dist/model/providers/mistral.js.map +1 -0
- package/dist/model/providers/openai.d.ts +11 -0
- package/dist/model/providers/openai.d.ts.map +1 -0
- package/dist/model/providers/openai.js +16 -0
- package/dist/model/providers/openai.js.map +1 -0
- package/dist/model/providers/openrouter.d.ts +14 -0
- package/dist/model/providers/openrouter.d.ts.map +1 -0
- package/dist/model/providers/openrouter.js +20 -0
- package/dist/model/providers/openrouter.js.map +1 -0
- package/dist/model/providers/togetherai.d.ts +11 -0
- package/dist/model/providers/togetherai.d.ts.map +1 -0
- package/dist/model/providers/togetherai.js +16 -0
- package/dist/model/providers/togetherai.js.map +1 -0
- package/dist/model/providers/vercelGateway.d.ts +14 -0
- package/dist/model/providers/vercelGateway.d.ts.map +1 -0
- package/dist/model/providers/vercelGateway.js +19 -0
- package/dist/model/providers/vercelGateway.js.map +1 -0
- package/dist/model/providers/xai.d.ts +11 -0
- package/dist/model/providers/xai.d.ts.map +1 -0
- package/dist/model/providers/xai.js +16 -0
- package/dist/model/providers/xai.js.map +1 -0
- package/dist/model/resolveModel.d.ts +45 -0
- package/dist/model/resolveModel.d.ts.map +1 -0
- package/dist/model/resolveModel.js +109 -0
- package/dist/model/resolveModel.js.map +1 -0
- package/dist/model/types.d.ts +98 -0
- package/dist/model/types.d.ts.map +1 -0
- package/dist/model/types.js +6 -0
- package/dist/model/types.js.map +1 -0
- package/dist/runtime.d.ts +74 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +41 -0
- package/dist/runtime.js.map +1 -0
- package/dist/storage/atomicFile.d.ts +28 -0
- package/dist/storage/atomicFile.d.ts.map +1 -0
- package/dist/storage/atomicFile.js +82 -0
- package/dist/storage/atomicFile.js.map +1 -0
- package/dist/storage/homeDirectory.d.ts +21 -0
- package/dist/storage/homeDirectory.d.ts.map +1 -0
- package/dist/storage/homeDirectory.js +36 -0
- package/dist/storage/homeDirectory.js.map +1 -0
- package/dist/storage/index.d.ts +11 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +11 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/jsonl.d.ts +55 -0
- package/dist/storage/jsonl.d.ts.map +1 -0
- package/dist/storage/jsonl.js +142 -0
- package/dist/storage/jsonl.js.map +1 -0
- package/dist/storage/lock.d.ts +70 -0
- package/dist/storage/lock.d.ts.map +1 -0
- package/dist/storage/lock.js +274 -0
- package/dist/storage/lock.js.map +1 -0
- package/dist/storage/valueCodec.d.ts +74 -0
- package/dist/storage/valueCodec.d.ts.map +1 -0
- package/dist/storage/valueCodec.js +165 -0
- package/dist/storage/valueCodec.js.map +1 -0
- package/dist/storage/workspaceLock.d.ts +38 -0
- package/dist/storage/workspaceLock.d.ts.map +1 -0
- package/dist/storage/workspaceLock.js +77 -0
- package/dist/storage/workspaceLock.js.map +1 -0
- package/dist/tasks/TaskManager.d.ts +155 -0
- package/dist/tasks/TaskManager.d.ts.map +1 -0
- package/dist/tasks/TaskManager.js +810 -0
- package/dist/tasks/TaskManager.js.map +1 -0
- package/dist/tasks/index.d.ts +7 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +6 -0
- package/dist/tasks/index.js.map +1 -0
- package/dist/tasks/types.d.ts +166 -0
- package/dist/tasks/types.d.ts.map +1 -0
- package/dist/tasks/types.js +8 -0
- package/dist/tasks/types.js.map +1 -0
- package/dist/threads/FileThreadStore.d.ts +118 -0
- package/dist/threads/FileThreadStore.d.ts.map +1 -0
- package/dist/threads/FileThreadStore.js +324 -0
- package/dist/threads/FileThreadStore.js.map +1 -0
- package/dist/threads/index.d.ts +9 -0
- package/dist/threads/index.d.ts.map +1 -0
- package/dist/threads/index.js +9 -0
- package/dist/threads/index.js.map +1 -0
- package/dist/threads/transcriptMigration.d.ts +52 -0
- package/dist/threads/transcriptMigration.d.ts.map +1 -0
- package/dist/threads/transcriptMigration.js +100 -0
- package/dist/threads/transcriptMigration.js.map +1 -0
- package/dist/threads/transcriptProjection.d.ts +21 -0
- package/dist/threads/transcriptProjection.d.ts.map +1 -0
- package/dist/threads/transcriptProjection.js +123 -0
- package/dist/threads/transcriptProjection.js.map +1 -0
- package/dist/threads/types.d.ts +250 -0
- package/dist/threads/types.d.ts.map +1 -0
- package/dist/threads/types.js +6 -0
- package/dist/threads/types.js.map +1 -0
- package/dist/tools/AgentTool.d.ts +38 -0
- package/dist/tools/AgentTool.d.ts.map +1 -0
- package/dist/tools/AgentTool.js +6 -0
- package/dist/tools/AgentTool.js.map +1 -0
- package/dist/tools/BashTool.d.ts +61 -0
- package/dist/tools/BashTool.d.ts.map +1 -0
- package/dist/tools/BashTool.js +158 -0
- package/dist/tools/BashTool.js.map +1 -0
- package/dist/tools/EditFileTool.d.ts +49 -0
- package/dist/tools/EditFileTool.d.ts.map +1 -0
- package/dist/tools/EditFileTool.js +119 -0
- package/dist/tools/EditFileTool.js.map +1 -0
- package/dist/tools/GlobTool.d.ts +48 -0
- package/dist/tools/GlobTool.d.ts.map +1 -0
- package/dist/tools/GlobTool.js +117 -0
- package/dist/tools/GlobTool.js.map +1 -0
- package/dist/tools/GrepTool.d.ts +76 -0
- package/dist/tools/GrepTool.d.ts.map +1 -0
- package/dist/tools/GrepTool.js +276 -0
- package/dist/tools/GrepTool.js.map +1 -0
- package/dist/tools/ReadFileTool.d.ts +51 -0
- package/dist/tools/ReadFileTool.d.ts.map +1 -0
- package/dist/tools/ReadFileTool.js +153 -0
- package/dist/tools/ReadFileTool.js.map +1 -0
- package/dist/tools/TaskManagerTool.d.ts +102 -0
- package/dist/tools/TaskManagerTool.d.ts.map +1 -0
- package/dist/tools/TaskManagerTool.js +132 -0
- package/dist/tools/TaskManagerTool.js.map +1 -0
- package/dist/tools/ToolExecutionContext.d.ts +61 -0
- package/dist/tools/ToolExecutionContext.d.ts.map +1 -0
- package/dist/tools/ToolExecutionContext.js +8 -0
- package/dist/tools/ToolExecutionContext.js.map +1 -0
- package/dist/tools/ToolRegistry.d.ts +82 -0
- package/dist/tools/ToolRegistry.d.ts.map +1 -0
- package/dist/tools/ToolRegistry.js +116 -0
- package/dist/tools/ToolRegistry.js.map +1 -0
- package/dist/tools/WriteFileTool.d.ts +46 -0
- package/dist/tools/WriteFileTool.d.ts.map +1 -0
- package/dist/tools/WriteFileTool.js +92 -0
- package/dist/tools/WriteFileTool.js.map +1 -0
- package/dist/tools/index.d.ts +17 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +17 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/internal/bashEnvironment.d.ts +20 -0
- package/dist/tools/internal/bashEnvironment.d.ts.map +1 -0
- package/dist/tools/internal/bashEnvironment.js +65 -0
- package/dist/tools/internal/bashEnvironment.js.map +1 -0
- package/dist/tools/internal/pathGuard.d.ts +27 -0
- package/dist/tools/internal/pathGuard.d.ts.map +1 -0
- package/dist/tools/internal/pathGuard.js +99 -0
- package/dist/tools/internal/pathGuard.js.map +1 -0
- package/dist/tools/prompts.d.ts +33 -0
- package/dist/tools/prompts.d.ts.map +1 -0
- package/dist/tools/prompts.js +48 -0
- package/dist/tools/prompts.js.map +1 -0
- package/package.json +65 -4
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Resolves home directory configuration for Hernex state storage.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
/**
|
|
8
|
+
* Resolves the authoritative home directory for Hernex threads and checkpoints.
|
|
9
|
+
*
|
|
10
|
+
* Precedence:
|
|
11
|
+
* 1. Explicit `options.homeDirectory`
|
|
12
|
+
* 2. `process.env.HERNEX_HOME`
|
|
13
|
+
* 3. Default fallback: `path.join(os.homedir(), '.greg')`
|
|
14
|
+
*
|
|
15
|
+
* @param options - Optional configuration parameters.
|
|
16
|
+
* @returns Fully normalized absolute directory path.
|
|
17
|
+
*/
|
|
18
|
+
export function resolveHomeDirectory(options) {
|
|
19
|
+
let resolved;
|
|
20
|
+
if (options?.homeDirectory && options.homeDirectory.trim().length > 0) {
|
|
21
|
+
resolved = options.homeDirectory.trim();
|
|
22
|
+
}
|
|
23
|
+
else if (process.env.HERNEX_HOME &&
|
|
24
|
+
process.env.HERNEX_HOME.trim().length > 0) {
|
|
25
|
+
resolved = process.env.HERNEX_HOME.trim();
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
resolved = path.join(os.homedir(), ".greg");
|
|
29
|
+
}
|
|
30
|
+
// Expand leading tilde
|
|
31
|
+
if (resolved === "~" || resolved.startsWith("~" + path.sep)) {
|
|
32
|
+
resolved = path.join(os.homedir(), resolved.slice(1));
|
|
33
|
+
}
|
|
34
|
+
return path.resolve(resolved);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=homeDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"homeDirectory.js","sources":["../../src/storage/homeDirectory.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAO7B;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAqC;IAErC,IAAI,QAAgB,CAAC;IAErB,IAAI,OAAO,EAAE,aAAa,IAAI,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtE,QAAQ,GAAG,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;IAC1C,CAAC;SAAM,IACL,OAAO,CAAC,GAAG,CAAC,WAAW;QACvB,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EACzC,CAAC;QACD,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;IAC5C,CAAC;SAAM,CAAC;QACN,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;IAED,uBAAuB;IACvB,IAAI,QAAQ,KAAK,GAAG,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5D,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Storage primitives export barrel for Hernex.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
export * from "./homeDirectory.js";
|
|
6
|
+
export * from "./atomicFile.js";
|
|
7
|
+
export * from "./lock.js";
|
|
8
|
+
export * from "./jsonl.js";
|
|
9
|
+
export * from "./valueCodec.js";
|
|
10
|
+
export * from "./workspaceLock.js";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Storage primitives export barrel for Hernex.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
export * from "./homeDirectory.js";
|
|
6
|
+
export * from "./atomicFile.js";
|
|
7
|
+
export * from "./lock.js";
|
|
8
|
+
export * from "./jsonl.js";
|
|
9
|
+
export * from "./valueCodec.js";
|
|
10
|
+
export * from "./workspaceLock.js";
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Robust append-only JSONL parser and appender with torn-tail recovery and tagged value codec.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import { type EncodedValue } from "./valueCodec.js";
|
|
6
|
+
/**
|
|
7
|
+
* Versioned envelope wrapper used to store encoded values in JSONL streams.
|
|
8
|
+
*/
|
|
9
|
+
export interface JsonlEnvelope {
|
|
10
|
+
/** Format schema version. */
|
|
11
|
+
readonly version: 1;
|
|
12
|
+
/** Encoded value payload. */
|
|
13
|
+
readonly value: EncodedValue;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Type guard for checking if a parsed JSON object is a JsonlEnvelope.
|
|
17
|
+
*
|
|
18
|
+
* @param value - Value to inspect.
|
|
19
|
+
* @returns True if value conforms to JsonlEnvelope shape.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isJsonlEnvelope(value: unknown): value is JsonlEnvelope;
|
|
22
|
+
/**
|
|
23
|
+
* Appends a record to a JSONL file with trailing newline using a versioned tagged envelope.
|
|
24
|
+
*
|
|
25
|
+
* Before appending, if the file contains a torn non-newline-terminated final line,
|
|
26
|
+
* it is verified and repaired under the caller's lock before appending. Truncate
|
|
27
|
+
* failures propagate fail-closed.
|
|
28
|
+
*
|
|
29
|
+
* @param filePath - The target JSONL file.
|
|
30
|
+
* @param record - Record or value to serialize and append.
|
|
31
|
+
* @throws {TypeError} If record contains non-serializable values (e.g. functions, cycles).
|
|
32
|
+
* @throws {Error} If torn-tail repair or write fails.
|
|
33
|
+
*/
|
|
34
|
+
export declare function appendJsonl<T>(filePath: string, record: T): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Writes an entire array of records to a JSONL file atomically using versioned tagged envelopes.
|
|
37
|
+
*
|
|
38
|
+
* @param filePath - The target JSONL file.
|
|
39
|
+
* @param records - Array of records to serialize and write.
|
|
40
|
+
* @throws {TypeError} If any record contains non-serializable values (e.g. functions, cycles).
|
|
41
|
+
* @throws {Error} If writing or temporary file replacement fails.
|
|
42
|
+
*/
|
|
43
|
+
export declare function writeJsonl<T>(filePath: string, records: readonly T[]): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Reads all records from a JSONL file, recovering only from torn non-newline-terminated final lines.
|
|
46
|
+
*
|
|
47
|
+
* Malformed middle lines or newline-terminated malformed lines remain fatal and throw.
|
|
48
|
+
* Truncate failures propagate immediately. Legacy untagged raw JSON lines are parsed as fallback.
|
|
49
|
+
*
|
|
50
|
+
* @param filePath - The JSONL file to read.
|
|
51
|
+
* @returns Array of parsed records.
|
|
52
|
+
* @throws {Error} When encountering corrupted middle or newline-terminated records.
|
|
53
|
+
*/
|
|
54
|
+
export declare function readJsonl<T>(filePath: string): Promise<T[]>;
|
|
55
|
+
//# sourceMappingURL=jsonl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonl.d.ts","sources":["../../src/storage/jsonl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,OAAO,EAA4B,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE9E;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,6BAA6B;IAC7B,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,6BAA6B;IAC7B,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;CAC9B;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAWtE;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,CAAC,GACR,OAAO,CAAC,IAAI,CAAC,CA6Cf;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,CAAC,EAChC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,SAAS,CAAC,EAAE,GACpB,OAAO,CAAC,IAAI,CAAC,CAUf;AAED;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CA+CjE"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Robust append-only JSONL parser and appender with torn-tail recovery and tagged value codec.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { appendFile, mkdir, readFile, truncate } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { atomicWriteFile } from "./atomicFile.js";
|
|
9
|
+
import { decodeValue, encodeValue } from "./valueCodec.js";
|
|
10
|
+
/**
|
|
11
|
+
* Type guard for checking if a parsed JSON object is a JsonlEnvelope.
|
|
12
|
+
*
|
|
13
|
+
* @param value - Value to inspect.
|
|
14
|
+
* @returns True if value conforms to JsonlEnvelope shape.
|
|
15
|
+
*/
|
|
16
|
+
export function isJsonlEnvelope(value) {
|
|
17
|
+
return (typeof value === "object" &&
|
|
18
|
+
value !== null &&
|
|
19
|
+
value.version === 1 &&
|
|
20
|
+
"value" in value &&
|
|
21
|
+
typeof value.value === "object" &&
|
|
22
|
+
value.value !== null &&
|
|
23
|
+
"type" in
|
|
24
|
+
value.value);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Appends a record to a JSONL file with trailing newline using a versioned tagged envelope.
|
|
28
|
+
*
|
|
29
|
+
* Before appending, if the file contains a torn non-newline-terminated final line,
|
|
30
|
+
* it is verified and repaired under the caller's lock before appending. Truncate
|
|
31
|
+
* failures propagate fail-closed.
|
|
32
|
+
*
|
|
33
|
+
* @param filePath - The target JSONL file.
|
|
34
|
+
* @param record - Record or value to serialize and append.
|
|
35
|
+
* @throws {TypeError} If record contains non-serializable values (e.g. functions, cycles).
|
|
36
|
+
* @throws {Error} If torn-tail repair or write fails.
|
|
37
|
+
*/
|
|
38
|
+
export async function appendJsonl(filePath, record) {
|
|
39
|
+
const absolutePath = path.resolve(filePath);
|
|
40
|
+
const parentDir = path.dirname(absolutePath);
|
|
41
|
+
if (!existsSync(parentDir)) {
|
|
42
|
+
await mkdir(parentDir, { recursive: true });
|
|
43
|
+
}
|
|
44
|
+
if (existsSync(absolutePath)) {
|
|
45
|
+
const existingContent = await readFile(absolutePath, "utf-8");
|
|
46
|
+
if (existingContent.length > 0 && !existingContent.endsWith("\n")) {
|
|
47
|
+
const lastNewlineIndex = existingContent.lastIndexOf("\n");
|
|
48
|
+
const tornLine = lastNewlineIndex === -1
|
|
49
|
+
? existingContent
|
|
50
|
+
: existingContent.slice(lastNewlineIndex + 1);
|
|
51
|
+
let isMalformed = false;
|
|
52
|
+
try {
|
|
53
|
+
JSON.parse(tornLine);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
isMalformed = true;
|
|
57
|
+
}
|
|
58
|
+
if (isMalformed) {
|
|
59
|
+
const validBytes = lastNewlineIndex === -1
|
|
60
|
+
? 0
|
|
61
|
+
: Buffer.byteLength(existingContent.slice(0, lastNewlineIndex + 1), "utf-8");
|
|
62
|
+
await truncate(absolutePath, validBytes);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
await appendFile(absolutePath, "\n", "utf-8");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const envelope = {
|
|
70
|
+
version: 1,
|
|
71
|
+
value: encodeValue(record),
|
|
72
|
+
};
|
|
73
|
+
const line = JSON.stringify(envelope) + "\n";
|
|
74
|
+
await appendFile(absolutePath, line, "utf-8");
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Writes an entire array of records to a JSONL file atomically using versioned tagged envelopes.
|
|
78
|
+
*
|
|
79
|
+
* @param filePath - The target JSONL file.
|
|
80
|
+
* @param records - Array of records to serialize and write.
|
|
81
|
+
* @throws {TypeError} If any record contains non-serializable values (e.g. functions, cycles).
|
|
82
|
+
* @throws {Error} If writing or temporary file replacement fails.
|
|
83
|
+
*/
|
|
84
|
+
export async function writeJsonl(filePath, records) {
|
|
85
|
+
const lines = records.map((record) => {
|
|
86
|
+
const envelope = {
|
|
87
|
+
version: 1,
|
|
88
|
+
value: encodeValue(record),
|
|
89
|
+
};
|
|
90
|
+
return JSON.stringify(envelope) + "\n";
|
|
91
|
+
});
|
|
92
|
+
const buffer = Buffer.from(lines.join(""), "utf-8");
|
|
93
|
+
await atomicWriteFile(filePath, buffer);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Reads all records from a JSONL file, recovering only from torn non-newline-terminated final lines.
|
|
97
|
+
*
|
|
98
|
+
* Malformed middle lines or newline-terminated malformed lines remain fatal and throw.
|
|
99
|
+
* Truncate failures propagate immediately. Legacy untagged raw JSON lines are parsed as fallback.
|
|
100
|
+
*
|
|
101
|
+
* @param filePath - The JSONL file to read.
|
|
102
|
+
* @returns Array of parsed records.
|
|
103
|
+
* @throws {Error} When encountering corrupted middle or newline-terminated records.
|
|
104
|
+
*/
|
|
105
|
+
export async function readJsonl(filePath) {
|
|
106
|
+
const absolutePath = path.resolve(filePath);
|
|
107
|
+
if (!existsSync(absolutePath)) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
const content = await readFile(absolutePath, "utf-8");
|
|
111
|
+
if (!content.trim()) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
const lines = content.split("\n");
|
|
115
|
+
const records = [];
|
|
116
|
+
for (let i = 0; i < lines.length; i++) {
|
|
117
|
+
const rawLine = lines[i];
|
|
118
|
+
if (rawLine.trim().length === 0) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
const parsed = JSON.parse(rawLine);
|
|
123
|
+
if (isJsonlEnvelope(parsed)) {
|
|
124
|
+
records.push(decodeValue(parsed.value));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
records.push(parsed);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (parseError) {
|
|
131
|
+
const isFinalNonTerminatedLine = i === lines.length - 1 && !content.endsWith("\n");
|
|
132
|
+
if (isFinalNonTerminatedLine) {
|
|
133
|
+
const validBytes = Buffer.byteLength(lines.slice(0, i).join("\n") + (i > 0 ? "\n" : ""), "utf-8");
|
|
134
|
+
await truncate(absolutePath, validBytes);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
throw new Error(`Corrupt JSONL entry at line ${i + 1} in "${filePath}": ${parseError.message}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return records;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=jsonl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonl.js","sources":["../../src/storage/jsonl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAqB,MAAM,iBAAiB,CAAC;AAY9E;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAAiC,CAAC,OAAO,KAAK,CAAC;QAChD,OAAO,IAAI,KAAK;QAChB,OAAQ,KAAiC,CAAC,KAAK,KAAK,QAAQ;QAC3D,KAAiC,CAAC,KAA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AAC,CAAC,CAAC,EAAE,CAAC,EAClD,OAAO,CACR,CAAC;gBACF,MAAM,QAAQ,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;gBACzC,MAAM;YACR,CAAC;YAED,MAAM,IAAI,KAAK,CACb,+BAA+B,CAAC,GAAG,CAAC,QAAQ,QAAQ,MAAO,UAAoB,CAAC,OAAO,EAAE,CAC1F,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview File-based exclusive locking for multi-process and multi-worker coordination.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Payload written to the exclusive lock file for ownership validation.
|
|
7
|
+
*/
|
|
8
|
+
export interface LockPayload {
|
|
9
|
+
/** Process identifier of the lock owner. */
|
|
10
|
+
readonly pid: number;
|
|
11
|
+
/** Unique cryptographically random token verifying ownership. */
|
|
12
|
+
readonly token: string;
|
|
13
|
+
/** Epoch timestamp in milliseconds when the lock was created. */
|
|
14
|
+
readonly createdAt: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Options for acquiring an exclusive file lock.
|
|
18
|
+
*/
|
|
19
|
+
export interface FileLockOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Maximum duration in milliseconds to wait before timing out.
|
|
22
|
+
* Defaults to 10,000ms.
|
|
23
|
+
*/
|
|
24
|
+
readonly timeoutMs?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Polling retry interval in milliseconds when contention (`EEXIST`) occurs.
|
|
27
|
+
* Defaults to 25ms.
|
|
28
|
+
*/
|
|
29
|
+
readonly retryIntervalMs?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Maximum age of a lock in milliseconds before it is considered stale, even if process
|
|
32
|
+
* liveness cannot be disproven.
|
|
33
|
+
*
|
|
34
|
+
* Note on distributed/network filesystems:
|
|
35
|
+
* Process liveness checks (`process.kill(pid, 0)`) are strictly local to the current host kernel.
|
|
36
|
+
* Clustered or shared network file systems (NFS, SMB, CIFS) span multiple hosts that do not share
|
|
37
|
+
* process identifiers; in multi-host topologies, configure `staleMs` to recover abandoned locks.
|
|
38
|
+
*/
|
|
39
|
+
readonly staleMs?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Checks whether a process with the given PID is running on the local host.
|
|
43
|
+
*
|
|
44
|
+
* Uses `process.kill(pid, 0)` which performs permission and existence checks without
|
|
45
|
+
* sending an actual signal.
|
|
46
|
+
*
|
|
47
|
+
* @param pid - Local operating system process identifier to probe.
|
|
48
|
+
* @returns `true` if the process exists and is active; `false` if `ESRCH` (process not found).
|
|
49
|
+
*/
|
|
50
|
+
export declare function isProcessAlive(pid: number): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Executes an operation while holding an exclusive file lock.
|
|
53
|
+
*
|
|
54
|
+
* Contention (`EEXIST`) is retried until `timeoutMs` expires. All non-contention
|
|
55
|
+
* errors (such as `EACCES`, `ENOSPC`, or `EIO`) fail immediately without retry.
|
|
56
|
+
* If an existing lock is held by a terminated local process or its age exceeds `staleMs`,
|
|
57
|
+
* it is safely reclaimed via an atomic staging and compare-and-swap sequence without
|
|
58
|
+
* displacing live owners.
|
|
59
|
+
* Ownership is verified on release using a cryptographic token. If both the inner
|
|
60
|
+
* operation and lock release encounter errors, they are preserved via `AggregateError`.
|
|
61
|
+
*
|
|
62
|
+
* @param lockPath - Path to the lock file.
|
|
63
|
+
* @param operation - Async function to run under the lock.
|
|
64
|
+
* @param optionsOrTimeout - Optional lock options or timeout in milliseconds (defaults to 10000ms).
|
|
65
|
+
* @returns Result of the executed operation.
|
|
66
|
+
* @throws {HernexLockError} When acquisition times out or token validation fails.
|
|
67
|
+
* @throws {AggregateError} When both the operation and lock release throw.
|
|
68
|
+
*/
|
|
69
|
+
export declare function withFileLock<T>(lockPath: string, operation: () => Promise<T>, optionsOrTimeout?: number | FileLockOptions): Promise<T>;
|
|
70
|
+
//# sourceMappingURL=lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.d.ts","sources":["../../src/storage/lock.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAgBH;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,4CAA4C;IAC5C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,iEAAiE;IACjE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,iEAAiE;IACjE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC;;;;;;;;OAQG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAenD;AA8ED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAClC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC3B,gBAAgB,CAAC,EAAE,MAAM,GAAG,eAAe,GAC1C,OAAO,CAAC,CAAC,CAAC,CAoLZ"}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview File-based exclusive locking for multi-process and multi-worker coordination.
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { mkdir, open, readFile, rename, stat, unlink, writeFile, } from "node:fs/promises";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { HernexLockError } from "../errors.js";
|
|
10
|
+
/**
|
|
11
|
+
* Checks whether a process with the given PID is running on the local host.
|
|
12
|
+
*
|
|
13
|
+
* Uses `process.kill(pid, 0)` which performs permission and existence checks without
|
|
14
|
+
* sending an actual signal.
|
|
15
|
+
*
|
|
16
|
+
* @param pid - Local operating system process identifier to probe.
|
|
17
|
+
* @returns `true` if the process exists and is active; `false` if `ESRCH` (process not found).
|
|
18
|
+
*/
|
|
19
|
+
export function isProcessAlive(pid) {
|
|
20
|
+
if (typeof pid !== "number" || pid <= 0 || !Number.isInteger(pid)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
process.kill(pid, 0);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
const code = error?.code;
|
|
29
|
+
if (code === "ESRCH") {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
// EPERM indicates the process exists on this host but is owned by another user
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Inspects an existing lock file to determine if it is stale or abandoned.
|
|
38
|
+
*
|
|
39
|
+
* @param lockPath - Absolute path to the lock file.
|
|
40
|
+
* @param staleMs - Optional stale threshold in milliseconds.
|
|
41
|
+
* @returns Lock inspection result.
|
|
42
|
+
*/
|
|
43
|
+
async function inspectLock(lockPath, staleMs) {
|
|
44
|
+
let content;
|
|
45
|
+
try {
|
|
46
|
+
content = await readFile(lockPath, "utf-8");
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error?.code === "ENOENT") {
|
|
50
|
+
return { isStale: false };
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
if (!content || content.trim() === "") {
|
|
55
|
+
try {
|
|
56
|
+
const stats = await stat(lockPath);
|
|
57
|
+
if (Date.now() - stats.mtimeMs > (staleMs ?? 1000)) {
|
|
58
|
+
return { isStale: true, reason: "empty_or_abandoned" };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return { isStale: false };
|
|
63
|
+
}
|
|
64
|
+
return { isStale: false };
|
|
65
|
+
}
|
|
66
|
+
let parsed;
|
|
67
|
+
try {
|
|
68
|
+
parsed = JSON.parse(content);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
try {
|
|
72
|
+
const stats = await stat(lockPath);
|
|
73
|
+
if (Date.now() - stats.mtimeMs > (staleMs ?? 1000)) {
|
|
74
|
+
return { isStale: true, reason: "corrupted_payload" };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return { isStale: false };
|
|
79
|
+
}
|
|
80
|
+
return { isStale: false };
|
|
81
|
+
}
|
|
82
|
+
if (typeof parsed?.pid !== "number" ||
|
|
83
|
+
typeof parsed?.token !== "string" ||
|
|
84
|
+
typeof parsed?.createdAt !== "number") {
|
|
85
|
+
return { isStale: true, reason: "malformed_fields" };
|
|
86
|
+
}
|
|
87
|
+
if (staleMs !== undefined && Date.now() - parsed.createdAt > staleMs) {
|
|
88
|
+
return { isStale: true, payload: parsed, reason: "stale_ms_exceeded" };
|
|
89
|
+
}
|
|
90
|
+
if (!isProcessAlive(parsed.pid)) {
|
|
91
|
+
return { isStale: true, payload: parsed, reason: "process_terminated" };
|
|
92
|
+
}
|
|
93
|
+
return { isStale: false, payload: parsed };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Executes an operation while holding an exclusive file lock.
|
|
97
|
+
*
|
|
98
|
+
* Contention (`EEXIST`) is retried until `timeoutMs` expires. All non-contention
|
|
99
|
+
* errors (such as `EACCES`, `ENOSPC`, or `EIO`) fail immediately without retry.
|
|
100
|
+
* If an existing lock is held by a terminated local process or its age exceeds `staleMs`,
|
|
101
|
+
* it is safely reclaimed via an atomic staging and compare-and-swap sequence without
|
|
102
|
+
* displacing live owners.
|
|
103
|
+
* Ownership is verified on release using a cryptographic token. If both the inner
|
|
104
|
+
* operation and lock release encounter errors, they are preserved via `AggregateError`.
|
|
105
|
+
*
|
|
106
|
+
* @param lockPath - Path to the lock file.
|
|
107
|
+
* @param operation - Async function to run under the lock.
|
|
108
|
+
* @param optionsOrTimeout - Optional lock options or timeout in milliseconds (defaults to 10000ms).
|
|
109
|
+
* @returns Result of the executed operation.
|
|
110
|
+
* @throws {HernexLockError} When acquisition times out or token validation fails.
|
|
111
|
+
* @throws {AggregateError} When both the operation and lock release throw.
|
|
112
|
+
*/
|
|
113
|
+
export async function withFileLock(lockPath, operation, optionsOrTimeout) {
|
|
114
|
+
const timeoutMs = typeof optionsOrTimeout === "number"
|
|
115
|
+
? optionsOrTimeout
|
|
116
|
+
: (optionsOrTimeout?.timeoutMs ?? 10000);
|
|
117
|
+
const retryIntervalMs = typeof optionsOrTimeout === "object" && optionsOrTimeout !== null
|
|
118
|
+
? (optionsOrTimeout.retryIntervalMs ?? 25)
|
|
119
|
+
: 25;
|
|
120
|
+
const staleMs = typeof optionsOrTimeout === "object" && optionsOrTimeout !== null
|
|
121
|
+
? optionsOrTimeout.staleMs
|
|
122
|
+
: undefined;
|
|
123
|
+
const absoluteLock = path.resolve(lockPath);
|
|
124
|
+
const parentDir = path.dirname(absoluteLock);
|
|
125
|
+
if (!existsSync(parentDir)) {
|
|
126
|
+
await mkdir(parentDir, { recursive: true });
|
|
127
|
+
}
|
|
128
|
+
const token = randomUUID();
|
|
129
|
+
const payload = {
|
|
130
|
+
pid: process.pid,
|
|
131
|
+
token,
|
|
132
|
+
createdAt: Date.now(),
|
|
133
|
+
};
|
|
134
|
+
const startTime = Date.now();
|
|
135
|
+
let acquired = false;
|
|
136
|
+
while (!acquired) {
|
|
137
|
+
try {
|
|
138
|
+
const handle = await open(absoluteLock, "wx");
|
|
139
|
+
try {
|
|
140
|
+
await handle.writeFile(JSON.stringify(payload), "utf-8");
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
await handle.close();
|
|
144
|
+
}
|
|
145
|
+
acquired = true;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
const errno = error?.code;
|
|
149
|
+
if (errno !== "EEXIST") {
|
|
150
|
+
// Clean up partial lock file if open succeeded before writeFile threw
|
|
151
|
+
try {
|
|
152
|
+
await unlink(absoluteLock);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// Ignore unlink error on write failure cleanup
|
|
156
|
+
}
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
// Check if existing lock is stale and can be safely reclaimed
|
|
160
|
+
const inspection = await inspectLock(absoluteLock, staleMs);
|
|
161
|
+
if (inspection.isStale) {
|
|
162
|
+
const reclaimLockPath = `${absoluteLock}.reclaim`;
|
|
163
|
+
let reclaimHandle = null;
|
|
164
|
+
try {
|
|
165
|
+
reclaimHandle = await open(reclaimLockPath, "wx");
|
|
166
|
+
}
|
|
167
|
+
catch (reclaimErr) {
|
|
168
|
+
if (reclaimErr?.code === "EEXIST") {
|
|
169
|
+
try {
|
|
170
|
+
const reclaimStats = await stat(reclaimLockPath);
|
|
171
|
+
if (Date.now() - reclaimStats.mtimeMs > 5000) {
|
|
172
|
+
await unlink(reclaimLockPath);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// Ignore stats/unlink errors on secondary contention
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (reclaimHandle !== null) {
|
|
181
|
+
try {
|
|
182
|
+
await reclaimHandle.close();
|
|
183
|
+
// Re-verify under arbitration lock to avoid displacing newly active owners
|
|
184
|
+
const recheck = await inspectLock(absoluteLock, staleMs);
|
|
185
|
+
if (recheck.isStale) {
|
|
186
|
+
const stagingPath = `${absoluteLock}.staging.${process.pid}.${token}.${Date.now()}`;
|
|
187
|
+
await writeFile(stagingPath, JSON.stringify(payload), "utf-8");
|
|
188
|
+
try {
|
|
189
|
+
await rename(stagingPath, absoluteLock);
|
|
190
|
+
acquired = true;
|
|
191
|
+
}
|
|
192
|
+
catch (renameErr) {
|
|
193
|
+
try {
|
|
194
|
+
await unlink(stagingPath);
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
// Ignore staging cleanup error
|
|
198
|
+
}
|
|
199
|
+
throw renameErr;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
try {
|
|
205
|
+
await unlink(reclaimLockPath);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// Ignore reclaim lock unlink error
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (acquired) {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
if (Date.now() - startTime >= timeoutMs) {
|
|
217
|
+
throw new HernexLockError(`Failed to acquire lock at "${absoluteLock}" within ${timeoutMs}ms.`, absoluteLock);
|
|
218
|
+
}
|
|
219
|
+
await new Promise((resolve) => setTimeout(resolve, retryIntervalMs));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
let operationError = null;
|
|
223
|
+
let operationResult;
|
|
224
|
+
try {
|
|
225
|
+
operationResult = await operation();
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
operationError = error;
|
|
229
|
+
}
|
|
230
|
+
let releaseError = null;
|
|
231
|
+
try {
|
|
232
|
+
let content = null;
|
|
233
|
+
try {
|
|
234
|
+
content = await readFile(absoluteLock, "utf-8");
|
|
235
|
+
}
|
|
236
|
+
catch (readError) {
|
|
237
|
+
if (readError?.code !== "ENOENT") {
|
|
238
|
+
throw readError;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (content !== null) {
|
|
242
|
+
let parsed = null;
|
|
243
|
+
try {
|
|
244
|
+
parsed = JSON.parse(content);
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
throw new HernexLockError(`Corrupted lock file at "${absoluteLock}": invalid JSON content.`, absoluteLock);
|
|
248
|
+
}
|
|
249
|
+
if (parsed?.token === token) {
|
|
250
|
+
await unlink(absoluteLock);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
throw new HernexLockError(`Lock token mismatch at "${absoluteLock}": expected "${token}", found "${parsed?.token ?? "unknown"}".`, absoluteLock);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
throw new HernexLockError(`Lock file at "${absoluteLock}" was unexpectedly removed before release.`, absoluteLock);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
releaseError = error;
|
|
262
|
+
}
|
|
263
|
+
if (operationError && releaseError) {
|
|
264
|
+
throw new AggregateError([operationError, releaseError], `Operation failed and lock release also failed at "${absoluteLock}".`);
|
|
265
|
+
}
|
|
266
|
+
if (operationError) {
|
|
267
|
+
throw operationError;
|
|
268
|
+
}
|
|
269
|
+
if (releaseError) {
|
|
270
|
+
throw releaseError;
|
|
271
|
+
}
|
|
272
|
+
return operationResult;
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.js","sources":["../../src/storage/lock.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,MAAM,EACN,SAAS,GACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAwC/C;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SA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