@cleocode/playbooks 2026.4.88 → 2026.4.92
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 +207 -0
- package/package.json +3 -3
- package/dist/approval.d.ts +0 -113
- package/dist/approval.js +0 -244
- package/dist/index.d.ts +0 -29
- package/dist/index.js +0 -32
- package/dist/parser.d.ts +0 -60
- package/dist/parser.js +0 -509
- package/dist/policy.d.ts +0 -55
- package/dist/policy.js +0 -85
- package/dist/schema.d.ts +0 -374
- package/dist/schema.js +0 -34
- package/dist/state.d.ts +0 -96
- package/dist/state.js +0 -322
package/dist/state.js
DELETED
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* State layer for playbook runtime — CRUD against playbook_runs + playbook_approvals.
|
|
3
|
-
* Uses node:sqlite DatabaseSync for consistency with rest of CLEO.
|
|
4
|
-
*
|
|
5
|
-
* All JSON-shaped columns (`bindings`, `iteration_counts`) are serialized to
|
|
6
|
-
* text at the write boundary and strictly parsed on read. Parse failures throw
|
|
7
|
-
* rather than silently reset state, per the data-integrity contract of ADR-013.
|
|
8
|
-
*
|
|
9
|
-
* Multi-column updates and cross-table operations run inside a BEGIN/COMMIT
|
|
10
|
-
* transaction so partial failures cannot leave the run in a half-mutated state.
|
|
11
|
-
*
|
|
12
|
-
* @task T889 / T904 / W4-8
|
|
13
|
-
*/
|
|
14
|
-
import { randomUUID } from 'node:crypto';
|
|
15
|
-
// ---------------------------------------------------------------------------
|
|
16
|
-
// Row mappers
|
|
17
|
-
// ---------------------------------------------------------------------------
|
|
18
|
-
/**
|
|
19
|
-
* Strictly parses a JSON payload stored in a playbook column. Throws a
|
|
20
|
-
* descriptive error on malformed JSON so state corruption surfaces at the
|
|
21
|
-
* boundary rather than mutating downstream logic.
|
|
22
|
-
*/
|
|
23
|
-
function parseJsonColumn(raw, column, runId) {
|
|
24
|
-
try {
|
|
25
|
-
return JSON.parse(raw);
|
|
26
|
-
}
|
|
27
|
-
catch (err) {
|
|
28
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
29
|
-
throw new Error(`playbook state: failed to parse JSON column "${column}" for run ${runId}: ${message}`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Maps a snake_case `playbook_runs` row to the contract-shaped
|
|
34
|
-
* {@link PlaybookRun}. Performs strict JSON parsing and validates the status
|
|
35
|
-
* column against the enum.
|
|
36
|
-
*/
|
|
37
|
-
function rowToPlaybookRun(row) {
|
|
38
|
-
const bindings = parseJsonColumn(row.bindings, 'bindings', row.run_id);
|
|
39
|
-
const iterationCounts = parseJsonColumn(row.iteration_counts, 'iteration_counts', row.run_id);
|
|
40
|
-
return {
|
|
41
|
-
runId: row.run_id,
|
|
42
|
-
playbookName: row.playbook_name,
|
|
43
|
-
playbookHash: row.playbook_hash,
|
|
44
|
-
currentNode: row.current_node,
|
|
45
|
-
bindings,
|
|
46
|
-
errorContext: row.error_context,
|
|
47
|
-
status: row.status,
|
|
48
|
-
iterationCounts,
|
|
49
|
-
epicId: row.epic_id ?? undefined,
|
|
50
|
-
sessionId: row.session_id ?? undefined,
|
|
51
|
-
startedAt: row.started_at,
|
|
52
|
-
completedAt: row.completed_at ?? undefined,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Maps a snake_case `playbook_approvals` row to the contract-shaped
|
|
57
|
-
* {@link PlaybookApproval}. Converts the integer `auto_passed` column to a
|
|
58
|
-
* boolean at the boundary.
|
|
59
|
-
*/
|
|
60
|
-
function rowToPlaybookApproval(row) {
|
|
61
|
-
return {
|
|
62
|
-
approvalId: row.approval_id,
|
|
63
|
-
runId: row.run_id,
|
|
64
|
-
nodeId: row.node_id,
|
|
65
|
-
token: row.token,
|
|
66
|
-
requestedAt: row.requested_at,
|
|
67
|
-
approvedAt: row.approved_at ?? undefined,
|
|
68
|
-
approver: row.approver ?? undefined,
|
|
69
|
-
reason: row.reason ?? undefined,
|
|
70
|
-
status: row.status,
|
|
71
|
-
autoPassed: row.auto_passed === 1,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
// ---------------------------------------------------------------------------
|
|
75
|
-
// Playbook run CRUD
|
|
76
|
-
// ---------------------------------------------------------------------------
|
|
77
|
-
/**
|
|
78
|
-
* Inserts a new playbook run with a freshly generated UUID, `status='running'`,
|
|
79
|
-
* and the provided initial bindings. Returns the hydrated {@link PlaybookRun}
|
|
80
|
-
* read back from the row so callers see all server-defaulted columns
|
|
81
|
-
* (`started_at`, empty `iteration_counts`, etc.).
|
|
82
|
-
*/
|
|
83
|
-
export function createPlaybookRun(db, input) {
|
|
84
|
-
const runId = randomUUID();
|
|
85
|
-
const bindingsJson = JSON.stringify(input.initialBindings ?? {});
|
|
86
|
-
const insert = db.prepare(`INSERT INTO playbook_runs (
|
|
87
|
-
run_id, playbook_name, playbook_hash, bindings, status,
|
|
88
|
-
iteration_counts, epic_id, session_id
|
|
89
|
-
) VALUES (?, ?, ?, ?, 'running', '{}', ?, ?)`);
|
|
90
|
-
insert.run(runId, input.playbookName, input.playbookHash, bindingsJson, input.epicId ?? null, input.sessionId ?? null);
|
|
91
|
-
const row = db.prepare('SELECT * FROM playbook_runs WHERE run_id = ?').get(runId);
|
|
92
|
-
if (!row) {
|
|
93
|
-
throw new Error(`playbook state: failed to read back run ${runId} after insert`);
|
|
94
|
-
}
|
|
95
|
-
return rowToPlaybookRun(row);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Fetches a playbook run by its primary key. Returns `null` when the run
|
|
99
|
-
* does not exist. Never throws on missing rows.
|
|
100
|
-
*/
|
|
101
|
-
export function getPlaybookRun(db, runId) {
|
|
102
|
-
const row = db.prepare('SELECT * FROM playbook_runs WHERE run_id = ?').get(runId);
|
|
103
|
-
return row ? rowToPlaybookRun(row) : null;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Applies a partial patch to a playbook run inside a transaction so mixed
|
|
107
|
-
* column updates (e.g. `status` + `currentNode`) commit atomically. Returns
|
|
108
|
-
* the fully-hydrated run read back after commit.
|
|
109
|
-
*/
|
|
110
|
-
export function updatePlaybookRun(db, runId, patch) {
|
|
111
|
-
const sets = [];
|
|
112
|
-
const values = [];
|
|
113
|
-
if ('playbookName' in patch && patch.playbookName !== undefined) {
|
|
114
|
-
sets.push('playbook_name = ?');
|
|
115
|
-
values.push(patch.playbookName);
|
|
116
|
-
}
|
|
117
|
-
if ('playbookHash' in patch && patch.playbookHash !== undefined) {
|
|
118
|
-
sets.push('playbook_hash = ?');
|
|
119
|
-
values.push(patch.playbookHash);
|
|
120
|
-
}
|
|
121
|
-
if ('currentNode' in patch) {
|
|
122
|
-
sets.push('current_node = ?');
|
|
123
|
-
values.push(patch.currentNode ?? null);
|
|
124
|
-
}
|
|
125
|
-
if ('bindings' in patch && patch.bindings !== undefined) {
|
|
126
|
-
sets.push('bindings = ?');
|
|
127
|
-
values.push(JSON.stringify(patch.bindings));
|
|
128
|
-
}
|
|
129
|
-
if ('errorContext' in patch) {
|
|
130
|
-
sets.push('error_context = ?');
|
|
131
|
-
values.push(patch.errorContext ?? null);
|
|
132
|
-
}
|
|
133
|
-
if ('status' in patch && patch.status !== undefined) {
|
|
134
|
-
sets.push('status = ?');
|
|
135
|
-
values.push(patch.status);
|
|
136
|
-
}
|
|
137
|
-
if ('iterationCounts' in patch && patch.iterationCounts !== undefined) {
|
|
138
|
-
sets.push('iteration_counts = ?');
|
|
139
|
-
values.push(JSON.stringify(patch.iterationCounts));
|
|
140
|
-
}
|
|
141
|
-
if ('epicId' in patch) {
|
|
142
|
-
sets.push('epic_id = ?');
|
|
143
|
-
values.push(patch.epicId ?? null);
|
|
144
|
-
}
|
|
145
|
-
if ('sessionId' in patch) {
|
|
146
|
-
sets.push('session_id = ?');
|
|
147
|
-
values.push(patch.sessionId ?? null);
|
|
148
|
-
}
|
|
149
|
-
if ('completedAt' in patch) {
|
|
150
|
-
sets.push('completed_at = ?');
|
|
151
|
-
values.push(patch.completedAt ?? null);
|
|
152
|
-
}
|
|
153
|
-
if (sets.length === 0) {
|
|
154
|
-
const existing = getPlaybookRun(db, runId);
|
|
155
|
-
if (!existing) {
|
|
156
|
-
throw new Error(`playbook state: run ${runId} not found for update`);
|
|
157
|
-
}
|
|
158
|
-
return existing;
|
|
159
|
-
}
|
|
160
|
-
db.exec('BEGIN');
|
|
161
|
-
try {
|
|
162
|
-
const stmt = db.prepare(`UPDATE playbook_runs SET ${sets.join(', ')} WHERE run_id = ?`);
|
|
163
|
-
values.push(runId);
|
|
164
|
-
const result = stmt.run(...values);
|
|
165
|
-
if (result.changes === 0) {
|
|
166
|
-
throw new Error(`playbook state: run ${runId} not found for update`);
|
|
167
|
-
}
|
|
168
|
-
db.exec('COMMIT');
|
|
169
|
-
}
|
|
170
|
-
catch (err) {
|
|
171
|
-
db.exec('ROLLBACK');
|
|
172
|
-
throw err;
|
|
173
|
-
}
|
|
174
|
-
const row = db.prepare('SELECT * FROM playbook_runs WHERE run_id = ?').get(runId);
|
|
175
|
-
if (!row) {
|
|
176
|
-
throw new Error(`playbook state: run ${runId} disappeared after update`);
|
|
177
|
-
}
|
|
178
|
-
return rowToPlaybookRun(row);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Lists playbook runs filtered by status and/or epic. Defaults to `ORDER BY
|
|
182
|
-
* started_at DESC` so the newest runs surface first for dashboards.
|
|
183
|
-
*/
|
|
184
|
-
export function listPlaybookRuns(db, opts = {}) {
|
|
185
|
-
const clauses = [];
|
|
186
|
-
const values = [];
|
|
187
|
-
if (opts.status) {
|
|
188
|
-
clauses.push('status = ?');
|
|
189
|
-
values.push(opts.status);
|
|
190
|
-
}
|
|
191
|
-
if (opts.epicId) {
|
|
192
|
-
clauses.push('epic_id = ?');
|
|
193
|
-
values.push(opts.epicId);
|
|
194
|
-
}
|
|
195
|
-
const where = clauses.length > 0 ? `WHERE ${clauses.join(' AND ')}` : '';
|
|
196
|
-
const limitClause = typeof opts.limit === 'number' ? 'LIMIT ?' : '';
|
|
197
|
-
if (limitClause)
|
|
198
|
-
values.push(opts.limit ?? 0);
|
|
199
|
-
const sql = `SELECT * FROM playbook_runs ${where} ORDER BY started_at DESC ${limitClause}`;
|
|
200
|
-
const rows = db.prepare(sql).all(...values);
|
|
201
|
-
return rows.map(rowToPlaybookRun);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Deletes a playbook run by primary key. Returns `true` if a row was removed.
|
|
205
|
-
* CASCADE wipes all associated approvals via the foreign-key constraint on
|
|
206
|
-
* `playbook_approvals.run_id`.
|
|
207
|
-
*/
|
|
208
|
-
export function deletePlaybookRun(db, runId) {
|
|
209
|
-
const result = db.prepare('DELETE FROM playbook_runs WHERE run_id = ?').run(runId);
|
|
210
|
-
return Number(result.changes) > 0;
|
|
211
|
-
}
|
|
212
|
-
// ---------------------------------------------------------------------------
|
|
213
|
-
// Playbook approval CRUD
|
|
214
|
-
// ---------------------------------------------------------------------------
|
|
215
|
-
/**
|
|
216
|
-
* Inserts a new approval record with a freshly generated UUID, `status='pending'`,
|
|
217
|
-
* and the caller-supplied opaque token. Returns the hydrated
|
|
218
|
-
* {@link PlaybookApproval} so callers see the server-defaulted `requested_at`.
|
|
219
|
-
*/
|
|
220
|
-
export function createPlaybookApproval(db, input) {
|
|
221
|
-
const approvalId = randomUUID();
|
|
222
|
-
const autoPassed = input.autoPassed ? 1 : 0;
|
|
223
|
-
db.prepare(`INSERT INTO playbook_approvals (
|
|
224
|
-
approval_id, run_id, node_id, token, status, auto_passed
|
|
225
|
-
) VALUES (?, ?, ?, ?, 'pending', ?)`).run(approvalId, input.runId, input.nodeId, input.token, autoPassed);
|
|
226
|
-
const row = db
|
|
227
|
-
.prepare('SELECT * FROM playbook_approvals WHERE approval_id = ?')
|
|
228
|
-
.get(approvalId);
|
|
229
|
-
if (!row) {
|
|
230
|
-
throw new Error(`playbook state: failed to read back approval ${approvalId} after insert`);
|
|
231
|
-
}
|
|
232
|
-
return rowToPlaybookApproval(row);
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Fetches an approval by its opaque token. Returns `null` if no row matches.
|
|
236
|
-
* The token column carries a UNIQUE constraint so at most one row is returned.
|
|
237
|
-
*/
|
|
238
|
-
export function getPlaybookApprovalByToken(db, token) {
|
|
239
|
-
const row = db.prepare('SELECT * FROM playbook_approvals WHERE token = ?').get(token);
|
|
240
|
-
return row ? rowToPlaybookApproval(row) : null;
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Applies a partial patch to an approval record inside a transaction. Used by
|
|
244
|
-
* the approval-resume flow to transactionally set both `status` and
|
|
245
|
-
* `approved_at` when a human resolves a HITL checkpoint.
|
|
246
|
-
*/
|
|
247
|
-
export function updatePlaybookApproval(db, approvalId, patch) {
|
|
248
|
-
const sets = [];
|
|
249
|
-
const values = [];
|
|
250
|
-
if ('runId' in patch && patch.runId !== undefined) {
|
|
251
|
-
sets.push('run_id = ?');
|
|
252
|
-
values.push(patch.runId);
|
|
253
|
-
}
|
|
254
|
-
if ('nodeId' in patch && patch.nodeId !== undefined) {
|
|
255
|
-
sets.push('node_id = ?');
|
|
256
|
-
values.push(patch.nodeId);
|
|
257
|
-
}
|
|
258
|
-
if ('token' in patch && patch.token !== undefined) {
|
|
259
|
-
sets.push('token = ?');
|
|
260
|
-
values.push(patch.token);
|
|
261
|
-
}
|
|
262
|
-
if ('approvedAt' in patch) {
|
|
263
|
-
sets.push('approved_at = ?');
|
|
264
|
-
values.push(patch.approvedAt ?? null);
|
|
265
|
-
}
|
|
266
|
-
if ('approver' in patch) {
|
|
267
|
-
sets.push('approver = ?');
|
|
268
|
-
values.push(patch.approver ?? null);
|
|
269
|
-
}
|
|
270
|
-
if ('reason' in patch) {
|
|
271
|
-
sets.push('reason = ?');
|
|
272
|
-
values.push(patch.reason ?? null);
|
|
273
|
-
}
|
|
274
|
-
if ('status' in patch && patch.status !== undefined) {
|
|
275
|
-
sets.push('status = ?');
|
|
276
|
-
values.push(patch.status);
|
|
277
|
-
}
|
|
278
|
-
if ('autoPassed' in patch && patch.autoPassed !== undefined) {
|
|
279
|
-
sets.push('auto_passed = ?');
|
|
280
|
-
values.push(patch.autoPassed ? 1 : 0);
|
|
281
|
-
}
|
|
282
|
-
if (sets.length === 0) {
|
|
283
|
-
const row = db
|
|
284
|
-
.prepare('SELECT * FROM playbook_approvals WHERE approval_id = ?')
|
|
285
|
-
.get(approvalId);
|
|
286
|
-
if (!row) {
|
|
287
|
-
throw new Error(`playbook state: approval ${approvalId} not found for update`);
|
|
288
|
-
}
|
|
289
|
-
return rowToPlaybookApproval(row);
|
|
290
|
-
}
|
|
291
|
-
db.exec('BEGIN');
|
|
292
|
-
try {
|
|
293
|
-
const stmt = db.prepare(`UPDATE playbook_approvals SET ${sets.join(', ')} WHERE approval_id = ?`);
|
|
294
|
-
values.push(approvalId);
|
|
295
|
-
const result = stmt.run(...values);
|
|
296
|
-
if (result.changes === 0) {
|
|
297
|
-
throw new Error(`playbook state: approval ${approvalId} not found for update`);
|
|
298
|
-
}
|
|
299
|
-
db.exec('COMMIT');
|
|
300
|
-
}
|
|
301
|
-
catch (err) {
|
|
302
|
-
db.exec('ROLLBACK');
|
|
303
|
-
throw err;
|
|
304
|
-
}
|
|
305
|
-
const row = db
|
|
306
|
-
.prepare('SELECT * FROM playbook_approvals WHERE approval_id = ?')
|
|
307
|
-
.get(approvalId);
|
|
308
|
-
if (!row) {
|
|
309
|
-
throw new Error(`playbook state: approval ${approvalId} disappeared after update`);
|
|
310
|
-
}
|
|
311
|
-
return rowToPlaybookApproval(row);
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Lists all approvals for a given run, ordered by `requested_at ASC` so the
|
|
315
|
-
* first HITL checkpoint surfaces first.
|
|
316
|
-
*/
|
|
317
|
-
export function listPlaybookApprovals(db, runId) {
|
|
318
|
-
const rows = db
|
|
319
|
-
.prepare('SELECT * FROM playbook_approvals WHERE run_id = ? ORDER BY requested_at ASC, approval_id ASC')
|
|
320
|
-
.all(runId);
|
|
321
|
-
return rows.map(rowToPlaybookApproval);
|
|
322
|
-
}
|