@united-workforce/cli 0.4.0 → 0.5.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/README.md +30 -3
- package/dist/.build-fingerprint +1 -0
- package/dist/__tests__/adapter-json-roundtrip.test.js +16 -6
- package/dist/__tests__/adapter-json-roundtrip.test.js.map +1 -1
- package/dist/__tests__/concurrency.test.d.ts +2 -0
- package/dist/__tests__/concurrency.test.d.ts.map +1 -0
- package/dist/__tests__/concurrency.test.js +196 -0
- package/dist/__tests__/concurrency.test.js.map +1 -0
- package/dist/__tests__/e2e-mock-agent.test.js +23 -7
- package/dist/__tests__/e2e-mock-agent.test.js.map +1 -1
- package/dist/__tests__/format-text-default.test.d.ts +2 -0
- package/dist/__tests__/format-text-default.test.d.ts.map +1 -0
- package/dist/__tests__/format-text-default.test.js +43 -0
- package/dist/__tests__/format-text-default.test.js.map +1 -0
- package/dist/__tests__/format-text-registry.test.d.ts +2 -0
- package/dist/__tests__/format-text-registry.test.d.ts.map +1 -0
- package/dist/__tests__/format-text-registry.test.js +158 -0
- package/dist/__tests__/format-text-registry.test.js.map +1 -0
- package/dist/__tests__/log-text-renderer.test.d.ts +2 -0
- package/dist/__tests__/log-text-renderer.test.d.ts.map +1 -0
- package/dist/__tests__/log-text-renderer.test.js +265 -0
- package/dist/__tests__/log-text-renderer.test.js.map +1 -0
- package/dist/__tests__/output-mapper-thread-list-startedat.test.d.ts +2 -0
- package/dist/__tests__/output-mapper-thread-list-startedat.test.d.ts.map +1 -0
- package/dist/__tests__/output-mapper-thread-list-startedat.test.js +102 -0
- package/dist/__tests__/output-mapper-thread-list-startedat.test.js.map +1 -0
- package/dist/__tests__/output-mapper-workflow-add.test.d.ts +2 -0
- package/dist/__tests__/output-mapper-workflow-add.test.d.ts.map +1 -0
- package/dist/__tests__/output-mapper-workflow-add.test.js +22 -0
- package/dist/__tests__/output-mapper-workflow-add.test.js.map +1 -0
- package/dist/__tests__/pid-recycling.test.js +9 -7
- package/dist/__tests__/pid-recycling.test.js.map +1 -1
- package/dist/__tests__/prompt.test.js +46 -4
- package/dist/__tests__/prompt.test.js.map +1 -1
- package/dist/__tests__/resolve-head-hash.test.js +8 -0
- package/dist/__tests__/resolve-head-hash.test.js.map +1 -1
- package/dist/__tests__/solve-issue-tea-worktree.test.js +3 -1
- package/dist/__tests__/solve-issue-tea-worktree.test.js.map +1 -1
- package/dist/__tests__/step-ask.test.js +9 -1
- package/dist/__tests__/step-ask.test.js.map +1 -1
- package/dist/__tests__/store-unified-threads.test.js +19 -17
- package/dist/__tests__/store-unified-threads.test.js.map +1 -1
- package/dist/__tests__/thread-cancel-status.test.js +19 -13
- package/dist/__tests__/thread-cancel-status.test.js.map +1 -1
- package/dist/__tests__/thread-cancel-text-renderer.test.d.ts +2 -0
- package/dist/__tests__/thread-cancel-text-renderer.test.d.ts.map +1 -0
- package/dist/__tests__/thread-cancel-text-renderer.test.js +110 -0
- package/dist/__tests__/thread-cancel-text-renderer.test.js.map +1 -0
- package/dist/__tests__/thread-list-filters.test.js +10 -8
- package/dist/__tests__/thread-list-filters.test.js.map +1 -1
- package/dist/__tests__/thread-list-template-ms-date.test.d.ts +2 -0
- package/dist/__tests__/thread-list-template-ms-date.test.d.ts.map +1 -0
- package/dist/__tests__/thread-list-template-ms-date.test.js +102 -0
- package/dist/__tests__/thread-list-template-ms-date.test.js.map +1 -0
- package/dist/__tests__/thread-list-workflow-corrupt.test.d.ts +2 -0
- package/dist/__tests__/thread-list-workflow-corrupt.test.d.ts.map +1 -0
- package/dist/__tests__/thread-list-workflow-corrupt.test.js +157 -0
- package/dist/__tests__/thread-list-workflow-corrupt.test.js.map +1 -0
- package/dist/__tests__/thread-poke.test.js +11 -1
- package/dist/__tests__/thread-poke.test.js.map +1 -1
- package/dist/__tests__/thread-read-xml-tags.test.js +10 -9
- package/dist/__tests__/thread-read-xml-tags.test.js.map +1 -1
- package/dist/__tests__/thread-resume.test.js +11 -1
- package/dist/__tests__/thread-resume.test.js.map +1 -1
- package/dist/__tests__/thread-start-cwd-cli.test.js +15 -3
- package/dist/__tests__/thread-start-cwd-cli.test.js.map +1 -1
- package/dist/__tests__/thread-stop-text-renderer.test.d.ts +2 -0
- package/dist/__tests__/thread-stop-text-renderer.test.d.ts.map +1 -0
- package/dist/__tests__/thread-stop-text-renderer.test.js +148 -0
- package/dist/__tests__/thread-stop-text-renderer.test.js.map +1 -0
- package/dist/__tests__/thread-suspend-step.test.js +5 -2
- package/dist/__tests__/thread-suspend-step.test.js.map +1 -1
- package/dist/__tests__/thread-test-helpers.d.ts +7 -0
- package/dist/__tests__/thread-test-helpers.d.ts.map +1 -1
- package/dist/__tests__/thread-test-helpers.js +13 -0
- package/dist/__tests__/thread-test-helpers.js.map +1 -1
- package/dist/__tests__/thread.test.js +11 -9
- package/dist/__tests__/thread.test.js.map +1 -1
- package/dist/__tests__/validate-semantic.test.js +56 -2
- package/dist/__tests__/validate-semantic.test.js.map +1 -1
- package/dist/__tests__/workflow-list-recursive.test.js +10 -7
- package/dist/__tests__/workflow-list-recursive.test.js.map +1 -1
- package/dist/__tests__/workflow-resolution.test.js +10 -7
- package/dist/__tests__/workflow-resolution.test.js.map +1 -1
- package/dist/__tests__/workflow-show-resolution.test.js +10 -7
- package/dist/__tests__/workflow-show-resolution.test.js.map +1 -1
- package/dist/__tests__/workflow-validate.test.js +75 -55
- package/dist/__tests__/workflow-validate.test.js.map +1 -1
- package/dist/__tests__/write-envelope.test.d.ts +2 -0
- package/dist/__tests__/write-envelope.test.d.ts.map +1 -0
- package/dist/__tests__/write-envelope.test.js +201 -0
- package/dist/__tests__/write-envelope.test.js.map +1 -0
- package/dist/cli.js +58 -35
- package/dist/cli.js.map +1 -1
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +12 -0
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/prompt.d.ts.map +1 -1
- package/dist/commands/prompt.js +42 -29
- package/dist/commands/prompt.js.map +1 -1
- package/dist/commands/setup.d.ts +9 -4
- package/dist/commands/setup.d.ts.map +1 -1
- package/dist/commands/setup.js +51 -7
- package/dist/commands/setup.js.map +1 -1
- package/dist/commands/thread.d.ts.map +1 -1
- package/dist/commands/thread.js +44 -2
- package/dist/commands/thread.js.map +1 -1
- package/dist/commands/workflow.d.ts +1 -1
- package/dist/commands/workflow.d.ts.map +1 -1
- package/dist/commands/workflow.js +2 -6
- package/dist/commands/workflow.js.map +1 -1
- package/dist/concurrency/concurrency.d.ts +34 -0
- package/dist/concurrency/concurrency.d.ts.map +1 -0
- package/dist/concurrency/concurrency.js +216 -0
- package/dist/concurrency/concurrency.js.map +1 -0
- package/dist/concurrency/index.d.ts +3 -0
- package/dist/concurrency/index.d.ts.map +1 -0
- package/dist/concurrency/index.js +2 -0
- package/dist/concurrency/index.js.map +1 -0
- package/dist/concurrency/types.d.ts +19 -0
- package/dist/concurrency/types.d.ts.map +1 -0
- package/dist/concurrency/types.js +2 -0
- package/dist/concurrency/types.js.map +1 -0
- package/dist/format.d.ts +69 -2
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +198 -1
- package/dist/format.js.map +1 -1
- package/dist/output-mappers.d.ts +122 -0
- package/dist/output-mappers.d.ts.map +1 -0
- package/dist/output-mappers.js +134 -0
- package/dist/output-mappers.js.map +1 -0
- package/dist/schemas.d.ts +4 -1
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +31 -4
- package/dist/schemas.js.map +1 -1
- package/dist/text-renderers.d.ts +30 -0
- package/dist/text-renderers.d.ts.map +1 -0
- package/dist/text-renderers.js +251 -0
- package/dist/text-renderers.js.map +1 -0
- package/dist/validate-semantic.d.ts.map +1 -1
- package/dist/validate-semantic.js +28 -11
- package/dist/validate-semantic.js.map +1 -1
- package/examples/brainstorm.yaml +130 -0
- package/examples/debate.yaml +169 -0
- package/examples/socratic-questioning.yaml +112 -0
- package/package.json +5 -4
- package/src/__tests__/adapter-json-roundtrip.test.ts +15 -6
- package/src/__tests__/concurrency.test.ts +266 -0
- package/src/__tests__/e2e-mock-agent.test.ts +45 -7
- package/src/__tests__/format-text-default.test.ts +49 -0
- package/src/__tests__/format-text-registry.test.ts +173 -0
- package/src/__tests__/log-text-renderer.test.ts +294 -0
- package/src/__tests__/output-mapper-thread-list-startedat.test.ts +124 -0
- package/src/__tests__/output-mapper-workflow-add.test.ts +24 -0
- package/src/__tests__/pid-recycling.test.ts +9 -8
- package/src/__tests__/prompt.test.ts +48 -4
- package/src/__tests__/resolve-head-hash.test.ts +7 -0
- package/src/__tests__/solve-issue-tea-worktree.test.ts +3 -1
- package/src/__tests__/step-ask.test.ts +8 -1
- package/src/__tests__/store-unified-threads.test.ts +21 -18
- package/src/__tests__/thread-cancel-status.test.ts +21 -14
- package/src/__tests__/thread-cancel-text-renderer.test.ts +125 -0
- package/src/__tests__/thread-list-filters.test.ts +9 -9
- package/src/__tests__/thread-list-template-ms-date.test.ts +110 -0
- package/src/__tests__/thread-list-workflow-corrupt.test.ts +198 -0
- package/src/__tests__/thread-poke.test.ts +10 -1
- package/src/__tests__/thread-read-xml-tags.test.ts +9 -11
- package/src/__tests__/thread-resume.test.ts +10 -1
- package/src/__tests__/thread-start-cwd-cli.test.ts +15 -3
- package/src/__tests__/thread-stop-text-renderer.test.ts +168 -0
- package/src/__tests__/thread-suspend-step.test.ts +5 -2
- package/src/__tests__/thread-test-helpers.ts +15 -1
- package/src/__tests__/thread.test.ts +10 -10
- package/src/__tests__/validate-semantic.test.ts +59 -2
- package/src/__tests__/workflow-list-recursive.test.ts +9 -9
- package/src/__tests__/workflow-resolution.test.ts +9 -8
- package/src/__tests__/workflow-show-resolution.test.ts +9 -8
- package/src/__tests__/workflow-validate.test.ts +78 -56
- package/src/__tests__/write-envelope.test.ts +257 -0
- package/src/cli.ts +92 -35
- package/src/commands/config.ts +11 -0
- package/src/commands/prompt.ts +42 -29
- package/src/commands/setup.ts +57 -7
- package/src/commands/thread.ts +48 -2
- package/src/commands/workflow.ts +3 -7
- package/src/concurrency/concurrency.ts +245 -0
- package/src/concurrency/index.ts +10 -0
- package/src/concurrency/types.ts +19 -0
- package/src/format.ts +282 -2
- package/src/output-mappers.ts +254 -0
- package/src/schemas.ts +39 -3
- package/src/text-renderers.ts +355 -0
- package/src/validate-semantic.ts +33 -12
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { unlinkSync } from "node:fs";
|
|
2
|
+
import { mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { isPidAlive } from "../background/index.js";
|
|
5
|
+
import type { AcquireSlotOptions, SlotHandle } from "./types.js";
|
|
6
|
+
|
|
7
|
+
/** Default concurrency limit when no config or flag is provided. */
|
|
8
|
+
export const DEFAULT_MAX_RUNNING = 2;
|
|
9
|
+
|
|
10
|
+
/** Default poll interval (ms) for waiting on a slot. */
|
|
11
|
+
const DEFAULT_POLL_INTERVAL_MS = 2000;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get the path to the slots directory.
|
|
15
|
+
*/
|
|
16
|
+
export function getSlotsDir(storageRoot: string): string {
|
|
17
|
+
return join(storageRoot, "slots");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Count active slot files (alive PIDs only). Stale slots are skipped but not removed.
|
|
22
|
+
*/
|
|
23
|
+
export async function countActiveSlots(storageRoot: string): Promise<number> {
|
|
24
|
+
const slotsDir = getSlotsDir(storageRoot);
|
|
25
|
+
let files: string[];
|
|
26
|
+
try {
|
|
27
|
+
files = await readdir(slotsDir);
|
|
28
|
+
} catch {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let count = 0;
|
|
33
|
+
for (const file of files) {
|
|
34
|
+
if (!file.endsWith(".slot")) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const pidStr = file.slice(0, -5);
|
|
38
|
+
const pid = Number(pidStr);
|
|
39
|
+
if (Number.isNaN(pid)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (isPidAlive(pid)) {
|
|
43
|
+
count++;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return count;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Remove slot files whose PIDs are no longer alive.
|
|
51
|
+
* Returns the number of stale slots cleaned.
|
|
52
|
+
*/
|
|
53
|
+
export async function cleanStaleSlots(storageRoot: string): Promise<number> {
|
|
54
|
+
const slotsDir = getSlotsDir(storageRoot);
|
|
55
|
+
let files: string[];
|
|
56
|
+
try {
|
|
57
|
+
files = await readdir(slotsDir);
|
|
58
|
+
} catch {
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let cleaned = 0;
|
|
63
|
+
for (const file of files) {
|
|
64
|
+
if (!file.endsWith(".slot")) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const pidStr = file.slice(0, -5);
|
|
68
|
+
const pid = Number(pidStr);
|
|
69
|
+
if (Number.isNaN(pid)) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (!isPidAlive(pid)) {
|
|
73
|
+
try {
|
|
74
|
+
await rm(join(slotsDir, file), { force: true });
|
|
75
|
+
cleaned++;
|
|
76
|
+
} catch {
|
|
77
|
+
// Ignore removal errors (race with another cleanup)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return cleaned;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Create a slot file for the current process. Returns the path to the created file.
|
|
86
|
+
*/
|
|
87
|
+
async function writeSlotFile(storageRoot: string): Promise<string> {
|
|
88
|
+
const slotsDir = getSlotsDir(storageRoot);
|
|
89
|
+
await mkdir(slotsDir, { recursive: true });
|
|
90
|
+
const slotPath = join(slotsDir, `${process.pid}.slot`);
|
|
91
|
+
await writeFile(slotPath, "", "utf8");
|
|
92
|
+
return slotPath;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Remove a slot file. Idempotent — silently ignores missing file.
|
|
97
|
+
*/
|
|
98
|
+
async function removeSlotFile(slotPath: string): Promise<void> {
|
|
99
|
+
try {
|
|
100
|
+
await rm(slotPath, { force: true });
|
|
101
|
+
} catch {
|
|
102
|
+
// Already removed or race condition — safe to ignore
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function sleep(ms: number, signal: AbortSignal | null): Promise<void> {
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
if (signal?.aborted) {
|
|
109
|
+
reject(new Error("aborted"));
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const timer = setTimeout(resolve, ms);
|
|
113
|
+
if (signal !== null) {
|
|
114
|
+
const onAbort = () => {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
reject(new Error("aborted"));
|
|
117
|
+
};
|
|
118
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Try to claim a slot. Returns the slot path on success, null if a race was
|
|
125
|
+
* detected (post-write count exceeds maxRunning → rolls back).
|
|
126
|
+
*/
|
|
127
|
+
async function tryClaimSlot(storageRoot: string, maxRunning: number): Promise<string | null> {
|
|
128
|
+
const slotPath = await writeSlotFile(storageRoot);
|
|
129
|
+
const postWriteCount = await countActiveSlots(storageRoot);
|
|
130
|
+
if (postWriteCount > maxRunning) {
|
|
131
|
+
await removeSlotFile(slotPath);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return slotPath;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function createSlotHandle(slotPath: string): SlotHandle {
|
|
138
|
+
let released = false;
|
|
139
|
+
return {
|
|
140
|
+
slotPath,
|
|
141
|
+
release: async () => {
|
|
142
|
+
if (released) return;
|
|
143
|
+
released = true;
|
|
144
|
+
await removeSlotFile(slotPath);
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type ResolvedOptions = {
|
|
150
|
+
onWaiting: ((info: string) => void) | null;
|
|
151
|
+
onAcquired: (() => void) | null;
|
|
152
|
+
pollIntervalMs: number;
|
|
153
|
+
signal: AbortSignal | null;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
function resolveOptions(options: Partial<AcquireSlotOptions>): ResolvedOptions {
|
|
157
|
+
return {
|
|
158
|
+
onWaiting: options.onWaiting ?? null,
|
|
159
|
+
onAcquired: options.onAcquired ?? null,
|
|
160
|
+
pollIntervalMs: options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS,
|
|
161
|
+
signal: options.signal ?? null,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function notifyWaiting(opts: ResolvedOptions, waited: boolean, message: string): boolean {
|
|
166
|
+
if (!waited && opts.onWaiting !== null) {
|
|
167
|
+
opts.onWaiting(message);
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
return waited;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Acquire a concurrency slot. If all slots are occupied, polls until one is available.
|
|
175
|
+
*
|
|
176
|
+
* Race protection: after writing the slot file, double-checks countActiveSlots.
|
|
177
|
+
* If the count exceeds maxRunning, rolls back (removes own slot) and retries.
|
|
178
|
+
*/
|
|
179
|
+
export async function acquireSlot(
|
|
180
|
+
storageRoot: string,
|
|
181
|
+
maxRunning: number,
|
|
182
|
+
options: Partial<AcquireSlotOptions> = {},
|
|
183
|
+
): Promise<SlotHandle> {
|
|
184
|
+
const opts = resolveOptions(options);
|
|
185
|
+
let waited = false;
|
|
186
|
+
|
|
187
|
+
while (true) {
|
|
188
|
+
await cleanStaleSlots(storageRoot);
|
|
189
|
+
|
|
190
|
+
const currentCount = await countActiveSlots(storageRoot);
|
|
191
|
+
if (currentCount >= maxRunning) {
|
|
192
|
+
waited = notifyWaiting(opts, waited, `${currentCount}/${maxRunning} running`);
|
|
193
|
+
await sleep(opts.pollIntervalMs, opts.signal);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const slotPath = await tryClaimSlot(storageRoot, maxRunning);
|
|
198
|
+
if (slotPath === null) {
|
|
199
|
+
waited = notifyWaiting(opts, waited, `race detected, retrying`);
|
|
200
|
+
await sleep(opts.pollIntervalMs, opts.signal);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (waited && opts.onAcquired !== null) {
|
|
205
|
+
opts.onAcquired();
|
|
206
|
+
}
|
|
207
|
+
return createSlotHandle(slotPath);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Alias for SlotHandle.release() — explicit function form for callers that
|
|
213
|
+
* prefer passing the handle as an argument.
|
|
214
|
+
*/
|
|
215
|
+
export async function releaseSlot(handle: SlotHandle): Promise<void> {
|
|
216
|
+
await handle.release();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Install process signal handlers that release the slot on SIGINT/SIGTERM.
|
|
221
|
+
* Returns a cleanup function that removes the handlers (call on normal exit).
|
|
222
|
+
*/
|
|
223
|
+
export function installSlotCleanup(handle: SlotHandle): () => void {
|
|
224
|
+
const cleanup = () => {
|
|
225
|
+
try {
|
|
226
|
+
unlinkSync(handle.slotPath);
|
|
227
|
+
} catch {
|
|
228
|
+
// Already removed
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const onSignal = () => {
|
|
233
|
+
cleanup();
|
|
234
|
+
process.exit(1);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
process.on("SIGINT", onSignal);
|
|
238
|
+
process.on("SIGTERM", onSignal);
|
|
239
|
+
|
|
240
|
+
// Return a function to uninstall the handlers
|
|
241
|
+
return () => {
|
|
242
|
+
process.removeListener("SIGINT", onSignal);
|
|
243
|
+
process.removeListener("SIGTERM", onSignal);
|
|
244
|
+
};
|
|
245
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Handle returned by acquireSlot; call release() to free the slot. */
|
|
2
|
+
export type SlotHandle = {
|
|
3
|
+
/** Remove the slot file. Idempotent — second call is a no-op. */
|
|
4
|
+
release: () => Promise<void>;
|
|
5
|
+
/** The slot file path (for signal-handler cleanup). */
|
|
6
|
+
slotPath: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** Options for acquireSlot polling behavior and callbacks. */
|
|
10
|
+
export type AcquireSlotOptions = {
|
|
11
|
+
/** Called when the function starts waiting (all slots occupied). */
|
|
12
|
+
onWaiting: ((info: string) => void) | null;
|
|
13
|
+
/** Called when a slot becomes available after waiting. */
|
|
14
|
+
onAcquired: (() => void) | null;
|
|
15
|
+
/** Poll interval in milliseconds (default: 2000). */
|
|
16
|
+
pollIntervalMs: number;
|
|
17
|
+
/** AbortSignal to cancel waiting. */
|
|
18
|
+
signal: AbortSignal | null;
|
|
19
|
+
};
|
package/src/format.ts
CHANGED
|
@@ -1,12 +1,292 @@
|
|
|
1
|
+
import type { Hash, Store } from "@ocas/core";
|
|
2
|
+
import type { OutputSchemaName } from "@united-workforce/protocol";
|
|
3
|
+
import { Liquid } from "liquidjs";
|
|
1
4
|
import { stringify } from "yaml";
|
|
5
|
+
import type { UwfSchemaHashes } from "./schemas.js";
|
|
6
|
+
import {
|
|
7
|
+
renderConfigGet,
|
|
8
|
+
renderConfigList,
|
|
9
|
+
renderConfigSet,
|
|
10
|
+
renderLogList,
|
|
11
|
+
renderLogShow,
|
|
12
|
+
renderStepList,
|
|
13
|
+
renderStepShow,
|
|
14
|
+
renderThreadCancel,
|
|
15
|
+
renderThreadList,
|
|
16
|
+
renderThreadShow,
|
|
17
|
+
renderThreadStart,
|
|
18
|
+
renderThreadStop,
|
|
19
|
+
renderWorkflowList,
|
|
20
|
+
renderWorkflowShow,
|
|
21
|
+
} from "./text-renderers.js";
|
|
2
22
|
|
|
3
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Five output formats supported by the uwf CLI. `text` is the default and
|
|
25
|
+
* produces a Liquid-rendered human-readable view. `json` and `yaml` wrap the
|
|
26
|
+
* payload in an ocas envelope `{ type, value }` for self-describing output.
|
|
27
|
+
* `raw-json` and `raw-yaml` emit the bare value, preserving 0.5.0 byte-for-byte
|
|
28
|
+
* output for backward-compat consumers.
|
|
29
|
+
*/
|
|
30
|
+
export type OutputFormat = "text" | "json" | "yaml" | "raw-json" | "raw-yaml";
|
|
4
31
|
|
|
5
|
-
export
|
|
32
|
+
export const SUPPORTED_FORMATS: readonly OutputFormat[] = [
|
|
33
|
+
"text",
|
|
34
|
+
"json",
|
|
35
|
+
"yaml",
|
|
36
|
+
"raw-json",
|
|
37
|
+
"raw-yaml",
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
export function isOutputFormat(v: string): v is OutputFormat {
|
|
41
|
+
return (SUPPORTED_FORMATS as readonly string[]).includes(v);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Per-command text renderer registry. Maps a fully-qualified command path
|
|
46
|
+
* (e.g. `"thread list"`, `"workflow show"`) to a function that converts
|
|
47
|
+
* the command's payload into a human-readable string.
|
|
48
|
+
*
|
|
49
|
+
* Renderers must:
|
|
50
|
+
* - Always return a `string` (never `undefined`).
|
|
51
|
+
* - Tolerate missing/null fields without throwing.
|
|
52
|
+
*
|
|
53
|
+
* The Liquid template path inside `writeEnvelope` is the primary rendering
|
|
54
|
+
* implementation. This registry exists so callers without a CAS store
|
|
55
|
+
* (tests, library consumers) can resolve `text` rendering, and so
|
|
56
|
+
* `formatOutput(data, "text", commandPath)` returns a meaningful string.
|
|
57
|
+
*/
|
|
58
|
+
export type TextRenderer = (data: unknown) => string;
|
|
59
|
+
|
|
60
|
+
export const TEXT_RENDERERS: Record<string, TextRenderer> = {
|
|
61
|
+
"thread list": renderThreadList,
|
|
62
|
+
"thread show": renderThreadShow,
|
|
63
|
+
"thread start": renderThreadStart,
|
|
64
|
+
"thread cancel": renderThreadCancel,
|
|
65
|
+
"thread stop": renderThreadStop,
|
|
66
|
+
"workflow list": renderWorkflowList,
|
|
67
|
+
"workflow show": renderWorkflowShow,
|
|
68
|
+
"step list": renderStepList,
|
|
69
|
+
"step show": renderStepShow,
|
|
70
|
+
"config list": renderConfigList,
|
|
71
|
+
"config get": renderConfigGet,
|
|
72
|
+
"config set": renderConfigSet,
|
|
73
|
+
"log list": renderLogList,
|
|
74
|
+
"log show": renderLogShow,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** Look up a registered text renderer by command path. */
|
|
78
|
+
export function getTextRenderer(commandPath: string): TextRenderer | undefined {
|
|
79
|
+
return TEXT_RENDERERS[commandPath];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Register (or override) a text renderer for a command path. */
|
|
83
|
+
export function registerTextRenderer(commandPath: string, renderer: TextRenderer): void {
|
|
84
|
+
TEXT_RENDERERS[commandPath] = renderer;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Format a payload as a string in the requested output format.
|
|
89
|
+
*
|
|
90
|
+
* For `"text"`, `formatOutput` looks up the registered renderer for
|
|
91
|
+
* `commandPath` (when provided) and falls back to a JSON serialization when
|
|
92
|
+
* no renderer is registered. The result is always a `string` — never
|
|
93
|
+
* `undefined`. For `"json"` and `"yaml"` the bare value is serialized.
|
|
94
|
+
* For `"raw-json"` and `"raw-yaml"` the output is identical to `"json"` /
|
|
95
|
+
* `"yaml"` (both modes emit the bare value; envelope wrapping happens in
|
|
96
|
+
* `writeEnvelope`).
|
|
97
|
+
*
|
|
98
|
+
* Note: this is the legacy in-process formatter used by raw output paths
|
|
99
|
+
* (`thread cancel`, `step fork`, `setup`, `log/config`) and tests. Production
|
|
100
|
+
* commands with a registered output schema go through `writeEnvelope`.
|
|
101
|
+
*/
|
|
102
|
+
export function formatOutput(data: unknown, format: OutputFormat, commandPath?: string): string {
|
|
6
103
|
switch (format) {
|
|
7
104
|
case "json":
|
|
105
|
+
case "raw-json":
|
|
8
106
|
return JSON.stringify(data);
|
|
9
107
|
case "yaml":
|
|
108
|
+
case "raw-yaml":
|
|
10
109
|
return stringify(data, { aliasDuplicateObjects: false }).trimEnd();
|
|
110
|
+
case "text": {
|
|
111
|
+
if (commandPath !== undefined) {
|
|
112
|
+
const renderer = TEXT_RENDERERS[commandPath];
|
|
113
|
+
if (renderer !== undefined) {
|
|
114
|
+
return renderer(data);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// Fallback: JSON pretty-printed so `formatOutput(_, "text")` never returns
|
|
118
|
+
// `"undefined"` (the bug from issue #327).
|
|
119
|
+
return JSON.stringify(data, null, 2);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const schemaHashCache = new Map<OutputSchemaName, Hash>();
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Resolve the CAS hash for an output schema by short name, caching the result
|
|
128
|
+
* for the lifetime of the process.
|
|
129
|
+
*/
|
|
130
|
+
export function resolveOutputSchemaHash(
|
|
131
|
+
outputs: Record<OutputSchemaName, Hash>,
|
|
132
|
+
schemaName: OutputSchemaName,
|
|
133
|
+
): Hash {
|
|
134
|
+
const cached = schemaHashCache.get(schemaName);
|
|
135
|
+
if (cached !== undefined) return cached;
|
|
136
|
+
const hash = outputs[schemaName];
|
|
137
|
+
if (hash === undefined) {
|
|
138
|
+
throw new Error(`output schema not registered: @uwf/output/${schemaName}`);
|
|
139
|
+
}
|
|
140
|
+
schemaHashCache.set(schemaName, hash);
|
|
141
|
+
return hash;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type WriteEnvelopeOptions = {
|
|
145
|
+
format: OutputFormat;
|
|
146
|
+
store: Store;
|
|
147
|
+
schemas: UwfSchemaHashes;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Wrap a CLI command payload in the chosen format and write it to stdout.
|
|
152
|
+
*
|
|
153
|
+
* - `text` → Liquid template at `@ocas/template/text/<hash>` (fallback YAML envelope)
|
|
154
|
+
* - `json` → `{"type":<hash>,"value":<payload>}` (envelope JSON)
|
|
155
|
+
* - `yaml` → envelope as multi-line YAML
|
|
156
|
+
* - `raw-json` → bare `<payload>` (legacy 0.5.0 shape)
|
|
157
|
+
* - `raw-yaml` → bare `<payload>` (legacy 0.5.0 shape)
|
|
158
|
+
*/
|
|
159
|
+
export async function writeEnvelope(
|
|
160
|
+
payload: unknown,
|
|
161
|
+
schemaName: OutputSchemaName,
|
|
162
|
+
options: WriteEnvelopeOptions,
|
|
163
|
+
): Promise<void> {
|
|
164
|
+
const { format, store, schemas } = options;
|
|
165
|
+
const schemaHash = resolveOutputSchemaHash(schemas.outputs, schemaName);
|
|
166
|
+
|
|
167
|
+
let body: string;
|
|
168
|
+
switch (format) {
|
|
169
|
+
case "json":
|
|
170
|
+
body = JSON.stringify({ type: schemaHash, value: payload });
|
|
171
|
+
break;
|
|
172
|
+
case "yaml":
|
|
173
|
+
body = stringify(
|
|
174
|
+
{ type: schemaHash, value: payload },
|
|
175
|
+
{ aliasDuplicateObjects: false },
|
|
176
|
+
).trimEnd();
|
|
177
|
+
break;
|
|
178
|
+
case "raw-json":
|
|
179
|
+
body = JSON.stringify(payload);
|
|
180
|
+
break;
|
|
181
|
+
case "raw-yaml":
|
|
182
|
+
body = stringify(payload, { aliasDuplicateObjects: false }).trimEnd();
|
|
183
|
+
break;
|
|
184
|
+
case "text":
|
|
185
|
+
body = await renderEnvelopeText(store, schemaHash, payload, schemaName);
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
process.stdout.write(`${body}\n`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let liquidEngine: Liquid | null = null;
|
|
193
|
+
|
|
194
|
+
type GraphMap = Record<string, Record<string, { role?: string }>>;
|
|
195
|
+
|
|
196
|
+
function firstRole(graph: GraphMap, current: string): string | null {
|
|
197
|
+
const transitions = graph[current];
|
|
198
|
+
if (!transitions) return null;
|
|
199
|
+
const firstKey = Object.keys(transitions)[0];
|
|
200
|
+
if (firstKey === undefined) return null;
|
|
201
|
+
const next = transitions[firstKey]?.role;
|
|
202
|
+
return typeof next === "string" ? next : null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function buildGraphPath(graph: GraphMap, start: string, limit: number): string[] {
|
|
206
|
+
const out: string[] = [start];
|
|
207
|
+
const seen = new Set<string>([start]);
|
|
208
|
+
let cur = start;
|
|
209
|
+
while (out.length < limit) {
|
|
210
|
+
const next = firstRole(graph, cur);
|
|
211
|
+
if (next === null || next === "$END") {
|
|
212
|
+
out.push("$END");
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
if (seen.has(next)) break;
|
|
216
|
+
seen.add(next);
|
|
217
|
+
out.push(next);
|
|
218
|
+
cur = next;
|
|
219
|
+
}
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function getLiquidEngine(): Liquid {
|
|
224
|
+
if (liquidEngine !== null) return liquidEngine;
|
|
225
|
+
const engine = new Liquid({ cache: false, strictFilters: false, strictVariables: false });
|
|
226
|
+
engine.registerFilter("keys", (input: unknown) =>
|
|
227
|
+
input !== null && typeof input === "object" ? Object.keys(input as object) : [],
|
|
228
|
+
);
|
|
229
|
+
engine.registerFilter("graph_path", (graph: unknown, start: unknown, max: unknown): string[] => {
|
|
230
|
+
if (graph === null || typeof graph !== "object") return [];
|
|
231
|
+
const limit = typeof max === "number" ? max : 5;
|
|
232
|
+
const startNode = typeof start === "string" ? start : "$START";
|
|
233
|
+
return buildGraphPath(graph as GraphMap, startNode, limit);
|
|
234
|
+
});
|
|
235
|
+
liquidEngine = engine;
|
|
236
|
+
return engine;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function readTemplateContent(store: Store, schemaHash: Hash): string | null {
|
|
240
|
+
const varName = `@ocas/template/text/${schemaHash}`;
|
|
241
|
+
const variable = store.var.get(varName);
|
|
242
|
+
if (variable === null) return null;
|
|
243
|
+
const node = store.cas.get(variable.value);
|
|
244
|
+
if (node === null) return null;
|
|
245
|
+
if (typeof node.payload !== "string") return null;
|
|
246
|
+
return node.payload;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function buildLiquidContext(payload: unknown, schemaHash: Hash): Record<string, unknown> {
|
|
250
|
+
const ctx: Record<string, unknown> = { payload, type: schemaHash };
|
|
251
|
+
if (payload !== null && typeof payload === "object" && !Array.isArray(payload)) {
|
|
252
|
+
for (const [k, v] of Object.entries(payload)) {
|
|
253
|
+
if (k !== "payload" && k !== "type") {
|
|
254
|
+
ctx[k] = v;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return ctx;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function renderEnvelopeText(
|
|
262
|
+
store: Store,
|
|
263
|
+
schemaHash: Hash,
|
|
264
|
+
payload: unknown,
|
|
265
|
+
schemaName: OutputSchemaName,
|
|
266
|
+
): Promise<string> {
|
|
267
|
+
const template = readTemplateContent(store, schemaHash);
|
|
268
|
+
if (template === null) {
|
|
269
|
+
process.stderr.write(
|
|
270
|
+
`warning: missing text template for @uwf/output/${schemaName} (var @ocas/template/text/${schemaHash}); falling back to YAML\n`,
|
|
271
|
+
);
|
|
272
|
+
return stringify(
|
|
273
|
+
{ type: schemaHash, value: payload },
|
|
274
|
+
{ aliasDuplicateObjects: false },
|
|
275
|
+
).trimEnd();
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const engine = getLiquidEngine();
|
|
279
|
+
const context = buildLiquidContext(payload, schemaHash);
|
|
280
|
+
const out = await engine.parseAndRender(template, context);
|
|
281
|
+
return out.replace(/\n+$/, "");
|
|
282
|
+
} catch (e) {
|
|
283
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
284
|
+
process.stderr.write(
|
|
285
|
+
`warning: failed to render text template for @uwf/output/${schemaName}: ${message}; falling back to YAML\n`,
|
|
286
|
+
);
|
|
287
|
+
return stringify(
|
|
288
|
+
{ type: schemaHash, value: payload },
|
|
289
|
+
{ aliasDuplicateObjects: false },
|
|
290
|
+
).trimEnd();
|
|
11
291
|
}
|
|
12
292
|
}
|