@afokapu/atdd-flow 0.3.13 → 0.3.14
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/package.json +1 -1
- package/src/core.ts +3 -2
- package/src/threads.ts +9 -4
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { basename, dirname, join, resolve } from "node:path";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
|
|
5
6
|
|
|
6
7
|
export type Role = { address: string; branch: string; agent?: string; purpose?: string; worktree?: string; base?: string };
|
|
7
8
|
export type Group = { role?: string; members?: string[] };
|
|
@@ -129,8 +130,8 @@ export const required = <T>(value: T | undefined, label: string) => {
|
|
|
129
130
|
return value;
|
|
130
131
|
};
|
|
131
132
|
export const yaml = {
|
|
132
|
-
parse: <T>(text: string) =>
|
|
133
|
-
print: (value: unknown) =>
|
|
133
|
+
parse: <T>(text: string) => parseYaml(text) as T,
|
|
134
|
+
print: (value: unknown) => stringifyYaml(value, { collectionStyle: "flow", flowCollectionPadding: false }),
|
|
134
135
|
};
|
|
135
136
|
|
|
136
137
|
export function addressParts(address: string) {
|
package/src/threads.ts
CHANGED
|
@@ -84,12 +84,16 @@ function inboxDirectory(root: string, address: string) {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
const pause = (milliseconds: number) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
87
|
+
const inboxLockStaleMs = 30_000;
|
|
88
|
+
const inboxLockAcquireTimeoutMs = 5_000;
|
|
89
|
+
const inboxLockRetryMs = 5;
|
|
87
90
|
|
|
88
91
|
/** mkdir is atomic across CLI processes; stale locks are recoverable after a crashed writer. */
|
|
89
92
|
async function withInboxLock<T>(directory: string, action: () => Promise<T>) {
|
|
90
93
|
await mkdir(directory, { recursive: true });
|
|
91
94
|
const lock = join(directory, "queue.lock");
|
|
92
|
-
|
|
95
|
+
const deadline = Date.now() + inboxLockAcquireTimeoutMs;
|
|
96
|
+
while (Date.now() < deadline) {
|
|
93
97
|
try {
|
|
94
98
|
await mkdir(lock, { recursive: false });
|
|
95
99
|
try { return await action(); }
|
|
@@ -97,9 +101,10 @@ async function withInboxLock<T>(directory: string, action: () => Promise<T>) {
|
|
|
97
101
|
} catch (error) {
|
|
98
102
|
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
99
103
|
try {
|
|
100
|
-
if (Date.now() - (await stat(lock)).mtimeMs >
|
|
101
|
-
|
|
102
|
-
|
|
104
|
+
if (Date.now() - (await stat(lock)).mtimeMs > inboxLockStaleMs) await rm(lock, { recursive: true, force: true });
|
|
105
|
+
} catch { /* A competing release or recovery is retried below. */ }
|
|
106
|
+
const remaining = deadline - Date.now();
|
|
107
|
+
if (remaining > 0) await pause(Math.min(inboxLockRetryMs, remaining));
|
|
103
108
|
}
|
|
104
109
|
}
|
|
105
110
|
throw new Error(`Timed out waiting for inbox queue lock ${directory}`);
|