@clearideas/agent-runtime-store-local 0.6.3 → 0.7.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/dist/artifact-store.file.js +4 -3
- package/dist/file-retry.d.ts +13 -0
- package/dist/file-retry.js +39 -0
- package/dist/run-store.file.d.ts +1 -0
- package/dist/run-store.file.js +25 -4
- package/dist/run-store.memory.d.ts +6 -0
- package/dist/run-store.memory.js +19 -1
- package/dist/workspace.directory.js +3 -2
- package/package.json +3 -3
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, open, readFile,
|
|
2
|
+
import { mkdir, open, readFile, unlink } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
5
|
import { safePathComponent } from "./path-component.js";
|
|
6
|
+
import { readTextWithRetry, renameWithRetry } from "./file-retry.js";
|
|
6
7
|
const writeAtomically = async (filePath, data) => {
|
|
7
8
|
await mkdir(path.dirname(filePath), { recursive: true });
|
|
8
9
|
const temporaryPath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.tmp`);
|
|
@@ -13,7 +14,7 @@ const writeAtomically = async (filePath, data) => {
|
|
|
13
14
|
await handle.sync();
|
|
14
15
|
await handle.close();
|
|
15
16
|
handle = undefined;
|
|
16
|
-
await
|
|
17
|
+
await renameWithRetry(temporaryPath, filePath);
|
|
17
18
|
}
|
|
18
19
|
catch (error) {
|
|
19
20
|
await handle?.close().catch(() => undefined);
|
|
@@ -53,7 +54,7 @@ export class FileArtifactStore {
|
|
|
53
54
|
return ref;
|
|
54
55
|
}
|
|
55
56
|
async get(ref) {
|
|
56
|
-
const stored = JSON.parse(await
|
|
57
|
+
const stored = JSON.parse(await readTextWithRetry(this.#metadataPath(ref.id)));
|
|
57
58
|
if (stored.ref.id !== ref.id)
|
|
58
59
|
throw new Error(`Artifact metadata id mismatch: ${ref.id}`);
|
|
59
60
|
if (stored.ref.name !== ref.name ||
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface FileRetryOptions {
|
|
2
|
+
/** Defaults to process.platform; tests override it. */
|
|
3
|
+
platform?: NodeJS.Platform;
|
|
4
|
+
/** Total time to keep retrying before the last error is thrown. */
|
|
5
|
+
maxWaitMs?: number;
|
|
6
|
+
sleep?: (delayMs: number) => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
/** Run a file operation, retrying transient Windows sharing errors. */
|
|
9
|
+
export declare const withFileRetry: <T>(operation: () => Promise<T>, options?: FileRetryOptions) => Promise<T>;
|
|
10
|
+
/** Atomically replace `to` with `from`, tolerating brief Windows file locks. */
|
|
11
|
+
export declare const renameWithRetry: (from: string, to: string, options?: FileRetryOptions) => Promise<void>;
|
|
12
|
+
/** Read a UTF-8 file, tolerating brief Windows file locks. */
|
|
13
|
+
export declare const readTextWithRetry: (file: string, options?: FileRetryOptions) => Promise<string>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Windows reports a file as busy while any process holds it open: antivirus
|
|
3
|
+
* scanners, search indexers, and sync clients such as OneDrive open files as
|
|
4
|
+
* soon as they change. Replacing or reading such a file then fails with
|
|
5
|
+
* EPERM, EACCES, or EBUSY for a few milliseconds. These helpers retry those
|
|
6
|
+
* transient errors with a short backoff, as graceful-fs and npm do. On other
|
|
7
|
+
* platforms the same codes are real permission errors and are not retried.
|
|
8
|
+
*/
|
|
9
|
+
import { readFile, rename } from "node:fs/promises";
|
|
10
|
+
const TRANSIENT_WINDOWS_CODES = new Set(["EPERM", "EACCES", "EBUSY"]);
|
|
11
|
+
const defaultSleep = (delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
12
|
+
/** Run a file operation, retrying transient Windows sharing errors. */
|
|
13
|
+
export const withFileRetry = async (operation, options = {}) => {
|
|
14
|
+
const platform = options.platform ?? process.platform;
|
|
15
|
+
const maxWaitMs = options.maxWaitMs ?? 10_000;
|
|
16
|
+
const sleep = options.sleep ?? defaultSleep;
|
|
17
|
+
let waited = 0;
|
|
18
|
+
let delayMs = 10;
|
|
19
|
+
while (true) {
|
|
20
|
+
try {
|
|
21
|
+
return await operation();
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
const code = error?.code;
|
|
25
|
+
if (platform !== "win32" ||
|
|
26
|
+
!code ||
|
|
27
|
+
!TRANSIENT_WINDOWS_CODES.has(code) ||
|
|
28
|
+
waited >= maxWaitMs)
|
|
29
|
+
throw error;
|
|
30
|
+
await sleep(delayMs);
|
|
31
|
+
waited += delayMs;
|
|
32
|
+
delayMs = Math.min(delayMs * 2, 250);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/** Atomically replace `to` with `from`, tolerating brief Windows file locks. */
|
|
37
|
+
export const renameWithRetry = (from, to, options) => withFileRetry(() => rename(from, to), options);
|
|
38
|
+
/** Read a UTF-8 file, tolerating brief Windows file locks. */
|
|
39
|
+
export const readTextWithRetry = (file, options) => withFileRetry(() => readFile(file, "utf8"), options);
|
package/dist/run-store.file.d.ts
CHANGED
|
@@ -16,5 +16,6 @@ export declare class FileRunStore implements RunStore {
|
|
|
16
16
|
suspendRun(runId: string, suspendedAt: string, expectedAttempt: number): Promise<void>;
|
|
17
17
|
completeRun(record: CompletedRunRecord): Promise<void>;
|
|
18
18
|
failRun(runId: string, error: RunError, failedAt: string, expectedAttempt: number): Promise<void>;
|
|
19
|
+
interruptRun(runId: string, error: RunError, interruptedAt: string, expectedAttempt: number): Promise<void>;
|
|
19
20
|
cancelRun(runId: string, cancelledAt: string, expectedAttempt: number): Promise<void>;
|
|
20
21
|
}
|
package/dist/run-store.file.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { access, mkdir, open,
|
|
2
|
+
import { access, mkdir, open, unlink } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import { readTextWithRetry, renameWithRetry } from "./file-retry.js";
|
|
4
5
|
import { safePathComponent } from "./path-component.js";
|
|
5
6
|
const RUN_FILE = "run.json";
|
|
6
7
|
const CHECKPOINT_FILE = "checkpoint.json";
|
|
@@ -17,7 +18,7 @@ const pathExists = async (filePath) => {
|
|
|
17
18
|
};
|
|
18
19
|
const readJson = async (filePath) => {
|
|
19
20
|
try {
|
|
20
|
-
return JSON.parse(await
|
|
21
|
+
return JSON.parse(await readTextWithRetry(filePath));
|
|
21
22
|
}
|
|
22
23
|
catch (error) {
|
|
23
24
|
if (error?.code === "ENOENT")
|
|
@@ -39,7 +40,7 @@ const writeJsonAtomically = async (filePath, value) => {
|
|
|
39
40
|
await handle.sync();
|
|
40
41
|
await handle.close();
|
|
41
42
|
handle = undefined;
|
|
42
|
-
await
|
|
43
|
+
await renameWithRetry(temporaryPath, filePath);
|
|
43
44
|
}
|
|
44
45
|
catch (error) {
|
|
45
46
|
await handle?.close().catch(() => undefined);
|
|
@@ -181,6 +182,25 @@ export class FileRunStore {
|
|
|
181
182
|
await writeJsonAtomically(this.#runFile(runId), failedRecord);
|
|
182
183
|
});
|
|
183
184
|
}
|
|
185
|
+
async interruptRun(runId, error, interruptedAt, expectedAttempt) {
|
|
186
|
+
return this.#withRunLock(runId, async () => {
|
|
187
|
+
const existing = await this.#requireRun(runId);
|
|
188
|
+
if (existing.status !== "created" && existing.status !== "running") {
|
|
189
|
+
throw new Error(`Cannot interrupt ${existing.status} run ${runId}`);
|
|
190
|
+
}
|
|
191
|
+
if ((existing.attempt ?? 1) !== expectedAttempt) {
|
|
192
|
+
throw new Error(`Run ${runId} is owned by another attempt`);
|
|
193
|
+
}
|
|
194
|
+
const interrupted = {
|
|
195
|
+
...existing,
|
|
196
|
+
status: "interrupted",
|
|
197
|
+
updatedAt: interruptedAt,
|
|
198
|
+
error,
|
|
199
|
+
interruptedAt,
|
|
200
|
+
};
|
|
201
|
+
await writeJsonAtomically(this.#runFile(runId), interrupted);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
184
204
|
async cancelRun(runId, cancelledAt, expectedAttempt) {
|
|
185
205
|
return this.#withRunLock(runId, async () => {
|
|
186
206
|
const existing = await this.#requireRun(runId);
|
|
@@ -188,7 +208,8 @@ export class FileRunStore {
|
|
|
188
208
|
return;
|
|
189
209
|
if (existing.status !== "created" &&
|
|
190
210
|
existing.status !== "running" &&
|
|
191
|
-
existing.status !== "suspended"
|
|
211
|
+
existing.status !== "suspended" &&
|
|
212
|
+
existing.status !== "interrupted") {
|
|
192
213
|
throw new Error(`Cannot cancel ${existing.status} run ${runId}`);
|
|
193
214
|
}
|
|
194
215
|
if ((existing.attempt ?? 1) !== expectedAttempt) {
|
|
@@ -5,6 +5,11 @@ export type FailedRunRecord = RunRecord & {
|
|
|
5
5
|
error: RunError;
|
|
6
6
|
failedAt: string;
|
|
7
7
|
};
|
|
8
|
+
export type InterruptedRunRecord = RunRecord & {
|
|
9
|
+
status: "interrupted";
|
|
10
|
+
error: RunError;
|
|
11
|
+
interruptedAt: string;
|
|
12
|
+
};
|
|
8
13
|
/** In-process persistence for tests, embedded use, and short-lived local runs. */
|
|
9
14
|
export declare class MemoryRunStore implements RunStore {
|
|
10
15
|
#private;
|
|
@@ -16,5 +21,6 @@ export declare class MemoryRunStore implements RunStore {
|
|
|
16
21
|
suspendRun(runId: string, suspendedAt: string, expectedAttempt: number): Promise<void>;
|
|
17
22
|
completeRun(record: CompletedRunRecord): Promise<void>;
|
|
18
23
|
failRun(runId: string, error: RunError, failedAt: string, expectedAttempt: number): Promise<void>;
|
|
24
|
+
interruptRun(runId: string, error: RunError, interruptedAt: string, expectedAttempt: number): Promise<void>;
|
|
19
25
|
cancelRun(runId: string, cancelledAt: string, expectedAttempt: number): Promise<void>;
|
|
20
26
|
}
|
package/dist/run-store.memory.js
CHANGED
|
@@ -114,13 +114,31 @@ export class MemoryRunStore {
|
|
|
114
114
|
};
|
|
115
115
|
this.#runs.set(runId, failedRecord);
|
|
116
116
|
}
|
|
117
|
+
async interruptRun(runId, error, interruptedAt, expectedAttempt) {
|
|
118
|
+
const existing = this.#requireRun(runId);
|
|
119
|
+
if (existing.status !== "created" && existing.status !== "running") {
|
|
120
|
+
throw new Error(`Cannot interrupt ${existing.status} run ${runId}`);
|
|
121
|
+
}
|
|
122
|
+
if ((existing.attempt ?? 1) !== expectedAttempt) {
|
|
123
|
+
throw new Error(`Run ${runId} is owned by another attempt`);
|
|
124
|
+
}
|
|
125
|
+
const interrupted = {
|
|
126
|
+
...existing,
|
|
127
|
+
status: "interrupted",
|
|
128
|
+
updatedAt: interruptedAt,
|
|
129
|
+
error: clone(error),
|
|
130
|
+
interruptedAt,
|
|
131
|
+
};
|
|
132
|
+
this.#runs.set(runId, interrupted);
|
|
133
|
+
}
|
|
117
134
|
async cancelRun(runId, cancelledAt, expectedAttempt) {
|
|
118
135
|
const existing = this.#requireRun(runId);
|
|
119
136
|
if (existing.status === "cancelled")
|
|
120
137
|
return;
|
|
121
138
|
if (existing.status !== "created" &&
|
|
122
139
|
existing.status !== "running" &&
|
|
123
|
-
existing.status !== "suspended"
|
|
140
|
+
existing.status !== "suspended" &&
|
|
141
|
+
existing.status !== "interrupted") {
|
|
124
142
|
throw new Error(`Cannot cancel ${existing.status} run ${runId}`);
|
|
125
143
|
}
|
|
126
144
|
if ((existing.attempt ?? 1) !== expectedAttempt) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { lstat, mkdir, open, readdir, readFile,
|
|
2
|
+
import { lstat, mkdir, open, readdir, readFile, unlink, } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { assertWorkspacePath, isSafeWorkspacePath, } from "@clearideas/agent-runtime-core";
|
|
5
5
|
import { DEFAULT_WORKSPACE_MAX_FILE_BYTES, DEFAULT_WORKSPACE_MAX_FILES, DEFAULT_WORKSPACE_MAX_TOTAL_BYTES, WorkspacePathFilter, compareNames, positiveInteger, throwIfAborted, } from "./workspace-common.js";
|
|
6
|
+
import { renameWithRetry } from "./file-retry.js";
|
|
6
7
|
const DEFAULT_MAX_SCAN_ENTRIES = 200_000;
|
|
7
8
|
const isMissing = (error) => error?.code === "ENOENT";
|
|
8
9
|
const writeAtomically = async (filePath, data, mode) => {
|
|
@@ -14,7 +15,7 @@ const writeAtomically = async (filePath, data, mode) => {
|
|
|
14
15
|
await handle.sync();
|
|
15
16
|
await handle.close();
|
|
16
17
|
handle = undefined;
|
|
17
|
-
await
|
|
18
|
+
await renameWithRetry(temporaryPath, filePath);
|
|
18
19
|
}
|
|
19
20
|
catch (error) {
|
|
20
21
|
await handle?.close().catch(() => undefined);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clearideas/agent-runtime-store-local",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Memory, file, console, and JSONL adapters for Clear Ideas Agent Runtime.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Clear Ideas Incorporated",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"postpack": "node ../../scripts/cleanup-package-license.mjs"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@clearideas/agent-runtime-contracts": "0.
|
|
40
|
-
"@clearideas/agent-runtime-core": "0.
|
|
39
|
+
"@clearideas/agent-runtime-contracts": "0.7.0",
|
|
40
|
+
"@clearideas/agent-runtime-core": "0.7.0",
|
|
41
41
|
"yaml": "^2.9.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|