@goodandready/dsh-cron 0.2.50 → 0.2.52
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/lib/api.js +66 -0
- package/lib/approval.js +143 -0
- package/lib/channels.js +4 -2
- package/lib/client.js +141 -90
- package/lib/cron-tool.js +24 -0
- package/lib/global-budgets.js +137 -0
- package/lib/index.js +4 -1
- package/lib/recipes-catalog.js +107 -0
- package/lib/routes.js +3 -1
- package/lib/scheduler-execution.js +132 -6
- package/lib/scheduler.js +12 -0
- package/lib/settings.js +6 -1
- package/lib/store.js +10 -1
- package/lib/task-create.js +3 -0
- package/lib/task-patch.js +7 -0
- package/lib/task-transfer.js +2 -0
- package/lib/telegram.js +1 -1
- package/lib/validators.js +192 -0
- package/package.json +1 -1
package/lib/api.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { approvalManager } from './approval.js';
|
|
2
|
+
import { sanitizeValidators } from './validators.js';
|
|
1
3
|
/**
|
|
2
4
|
* REST surface of the plugin: the /dsh-cron/tasks and /dsh-cron/action route
|
|
3
5
|
* families. Extracted from index.js and split into per-scope handlers so each
|
|
@@ -266,6 +268,7 @@ function buildTaskRecord(store, body, taskType, parsed) {
|
|
|
266
268
|
maxRetries: body.maxRetries !== undefined ? Math.max(0, Number(body.maxRetries) || 0) : (current ? (Number(current.maxRetries) || 0) : 0),
|
|
267
269
|
retryBackoffMs: body.retryBackoffMs !== undefined ? Math.max(1000, Number(body.retryBackoffMs) || 30000) : (current ? (Number(current.retryBackoffMs) || 30000) : 30000),
|
|
268
270
|
permissionPreset: !EXTERNAL_RUNTIMES.includes(taskType) && ['default', 'read-only', 'workspace-write', 'full'].includes(body.permissionPreset) ? body.permissionPreset : (current && !EXTERNAL_RUNTIMES.includes(taskType) ? (current.permissionPreset || 'default') : 'default'),
|
|
271
|
+
validators: Array.isArray(body.validators) ? sanitizeValidators(body.validators) : (current ? current.validators : undefined),
|
|
269
272
|
...mergeExecutionFields(current, body, parsed),
|
|
270
273
|
};
|
|
271
274
|
}
|
|
@@ -341,6 +344,69 @@ async function handleTaskItem({ store, scheduler, req, res, url, id, action, api
|
|
|
341
344
|
|
|
342
345
|
/** Task actions that are plain POSTs. Returns true when one matched. */
|
|
343
346
|
async function handleItemPost({ store, scheduler, req, res, url, id, action }) {
|
|
347
|
+
if (action === 'retry-delivery') {
|
|
348
|
+
const task = store.get(id);
|
|
349
|
+
if (!task) {
|
|
350
|
+
sendJson(res, 404, NOT_FOUND);
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
const body = await readBodyJson(req).catch(() => ({}));
|
|
354
|
+
const runId = body.runId || url.searchParams.get('runId');
|
|
355
|
+
const runs = store.history.get(id) || [];
|
|
356
|
+
const run = runId ? runs.find(r => r.id === runId) : runs[0];
|
|
357
|
+
if (!run) {
|
|
358
|
+
sendJson(res, 404, { ok: false, error: 'Run not found' });
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
const delivery = await scheduler.deliverNotifications(task, run);
|
|
362
|
+
run.delivery = delivery;
|
|
363
|
+
if (Array.isArray(run.stages)) {
|
|
364
|
+
const deliveryStage = run.stages.find(s => s.name === 'delivery');
|
|
365
|
+
if (deliveryStage) {
|
|
366
|
+
deliveryStage.status = delivery?.status || 'delivered';
|
|
367
|
+
deliveryStage.delivered = delivery?.delivered;
|
|
368
|
+
deliveryStage.failures = delivery?.failures;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
store.scheduleSave();
|
|
372
|
+
sendJson(res, 200, { ok: true, delivery, run });
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
if (action === 'approve' || action === 'reject') {
|
|
376
|
+
const task = store.get(id);
|
|
377
|
+
if (!task) {
|
|
378
|
+
sendJson(res, 404, NOT_FOUND);
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
const body = await readBodyJson(req).catch(() => ({}));
|
|
382
|
+
const proposalId = body.proposalId || url.searchParams.get('proposalId');
|
|
383
|
+
const proposal = proposalId
|
|
384
|
+
? approvalManager.getProposal(proposalId)
|
|
385
|
+
: approvalManager.getPendingProposalForTask(id);
|
|
386
|
+
|
|
387
|
+
if (!proposal) {
|
|
388
|
+
sendJson(res, 404, { ok: false, error: 'No pending approval proposal found for this task' });
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const decision = action === 'approve' ? 'approve' : 'reject';
|
|
393
|
+
const result = approvalManager.decide(proposal.id, decision, {
|
|
394
|
+
decidedBy: body.decidedBy || 'web-user',
|
|
395
|
+
reason: body.reason || '',
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
if (!result.ok) {
|
|
399
|
+
sendJson(res, 400, result);
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (decision === 'approve') {
|
|
404
|
+
scheduler.runTask(id, { approvedBy: body.decidedBy || 'web-user' }).catch(() => {});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
sendJson(res, 200, { ok: true, proposal: result.proposal });
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
344
410
|
if (action === 'run') {
|
|
345
411
|
if (!store.get(id)) {
|
|
346
412
|
sendJson(res, 404, NOT_FOUND);
|
package/lib/approval.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { logger } from './logger.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Human-in-the-Loop Action Approval Engine (#318).
|
|
6
|
+
* Manages action approval proposals before execution, ensuring
|
|
7
|
+
* no dangerous side effects execute without authorized human confirmation.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const APPROVAL_STATES = {
|
|
11
|
+
PENDING: 'pending',
|
|
12
|
+
APPROVED: 'approved',
|
|
13
|
+
REJECTED: 'rejected',
|
|
14
|
+
EXPIRED: 'expired',
|
|
15
|
+
CANCELLED: 'cancelled',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Clean proposal object for storage and API responses.
|
|
20
|
+
*/
|
|
21
|
+
export function sanitizeProposal(proposal) {
|
|
22
|
+
if (!proposal || typeof proposal !== 'object') return null;
|
|
23
|
+
return {
|
|
24
|
+
id: String(proposal.id || randomUUID()),
|
|
25
|
+
taskId: String(proposal.taskId || ''),
|
|
26
|
+
runId: proposal.runId ? String(proposal.runId) : null,
|
|
27
|
+
actionType: String(proposal.actionType || 'execute'),
|
|
28
|
+
summary: String(proposal.summary || ''),
|
|
29
|
+
details: proposal.details && typeof proposal.details === 'object' ? proposal.details : {},
|
|
30
|
+
requestedAt: Number(proposal.requestedAt) || Date.now(),
|
|
31
|
+
expiresAt: Number(proposal.expiresAt) || (Date.now() + 3600000), // Default 1 hour
|
|
32
|
+
state: Object.values(APPROVAL_STATES).includes(proposal.state) ? proposal.state : APPROVAL_STATES.PENDING,
|
|
33
|
+
decidedAt: proposal.decidedAt ? Number(proposal.decidedAt) : null,
|
|
34
|
+
decidedBy: proposal.decidedBy ? String(proposal.decidedBy) : null,
|
|
35
|
+
reason: proposal.reason ? String(proposal.reason) : null,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* In-memory / persistent registry for task proposals.
|
|
41
|
+
*/
|
|
42
|
+
export class ApprovalManager {
|
|
43
|
+
constructor() {
|
|
44
|
+
this.proposals = new Map();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Create a new pending approval proposal for a task.
|
|
49
|
+
*/
|
|
50
|
+
createProposal(taskId, runId, { actionType = 'execute', summary = '', details = {}, timeoutMs = 3600000 } = {}) {
|
|
51
|
+
// If an existing pending proposal exists for this task, invalidate it as superseded
|
|
52
|
+
for (const [id, prop] of this.proposals.entries()) {
|
|
53
|
+
if (prop.taskId === taskId && prop.state === APPROVAL_STATES.PENDING) {
|
|
54
|
+
prop.state = APPROVAL_STATES.CANCELLED;
|
|
55
|
+
prop.decidedAt = Date.now();
|
|
56
|
+
prop.reason = 'Superseded by newer proposal';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const proposal = sanitizeProposal({
|
|
61
|
+
id: 'appr_' + randomUUID().replace(/-/g, '').slice(0, 12),
|
|
62
|
+
taskId,
|
|
63
|
+
runId,
|
|
64
|
+
actionType,
|
|
65
|
+
summary,
|
|
66
|
+
details,
|
|
67
|
+
requestedAt: Date.now(),
|
|
68
|
+
expiresAt: Date.now() + (Number(timeoutMs) > 0 ? Number(timeoutMs) : 3600000),
|
|
69
|
+
state: APPROVAL_STATES.PENDING,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
this.proposals.set(proposal.id, proposal);
|
|
73
|
+
logger.info(`[dsh-cron] created approval proposal "${proposal.id}" for task "${taskId}": ${summary}`);
|
|
74
|
+
return proposal;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
getProposal(proposalId) {
|
|
78
|
+
const prop = this.proposals.get(proposalId);
|
|
79
|
+
if (!prop) return null;
|
|
80
|
+
// Check expiration on read
|
|
81
|
+
if (prop.state === APPROVAL_STATES.PENDING && Date.now() > prop.expiresAt) {
|
|
82
|
+
prop.state = APPROVAL_STATES.EXPIRED;
|
|
83
|
+
prop.decidedAt = Date.now();
|
|
84
|
+
prop.reason = 'Approval expired';
|
|
85
|
+
}
|
|
86
|
+
return prop;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getPendingProposalForTask(taskId) {
|
|
90
|
+
for (const prop of this.proposals.values()) {
|
|
91
|
+
if (prop.taskId === taskId && prop.state === APPROVAL_STATES.PENDING) {
|
|
92
|
+
if (Date.now() > prop.expiresAt) {
|
|
93
|
+
prop.state = APPROVAL_STATES.EXPIRED;
|
|
94
|
+
prop.decidedAt = Date.now();
|
|
95
|
+
prop.reason = 'Approval expired';
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
return prop;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
listProposals({ taskId = null, state = null } = {}) {
|
|
105
|
+
const list = [];
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
for (const prop of this.proposals.values()) {
|
|
108
|
+
if (prop.state === APPROVAL_STATES.PENDING && now > prop.expiresAt) {
|
|
109
|
+
prop.state = APPROVAL_STATES.EXPIRED;
|
|
110
|
+
prop.decidedAt = now;
|
|
111
|
+
prop.reason = 'Approval expired';
|
|
112
|
+
}
|
|
113
|
+
if (taskId && prop.taskId !== taskId) continue;
|
|
114
|
+
if (state && prop.state !== state) continue;
|
|
115
|
+
list.push({ ...prop });
|
|
116
|
+
}
|
|
117
|
+
return list.sort((a, b) => b.requestedAt - a.requestedAt);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Decide on a proposal (approve or reject) with replay and double-click protection.
|
|
122
|
+
*/
|
|
123
|
+
decide(proposalId, decision, { decidedBy = 'user', reason = '' } = {}) {
|
|
124
|
+
const prop = this.getProposal(proposalId);
|
|
125
|
+
if (!prop) {
|
|
126
|
+
return { ok: false, error: 'Proposal not found' };
|
|
127
|
+
}
|
|
128
|
+
if (prop.state !== APPROVAL_STATES.PENDING) {
|
|
129
|
+
return { ok: false, error: `Proposal already resolved with state: ${prop.state}`, proposal: prop };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const state = decision === 'approve' ? APPROVAL_STATES.APPROVED : APPROVAL_STATES.REJECTED;
|
|
133
|
+
prop.state = state;
|
|
134
|
+
prop.decidedAt = Date.now();
|
|
135
|
+
prop.decidedBy = decidedBy;
|
|
136
|
+
prop.reason = reason;
|
|
137
|
+
|
|
138
|
+
logger.info(`[dsh-cron] proposal "${proposalId}" ${state} by ${decidedBy}${reason ? ': ' + reason : ''}`);
|
|
139
|
+
return { ok: true, proposal: prop };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export const approvalManager = new ApprovalManager();
|
package/lib/channels.js
CHANGED
|
@@ -15,7 +15,7 @@ export const CHANNEL_IDS = ['telegram', 'kanban', 'discord', 'slack', 'ntfy', 'b
|
|
|
15
15
|
|
|
16
16
|
// CHANNEL_LABELS removed (#155) — localized via lib/client.js
|
|
17
17
|
|
|
18
|
-
export const isFailureStatus = (status) => status === 'error' || status === 'timeout' || status === 'missed';
|
|
18
|
+
export const isFailureStatus = (status) => status === 'error' || status === 'timeout' || status === 'missed' || status === 'validation_failed' || status === 'validator_error';
|
|
19
19
|
export const isFailed = (status) => isFailureStatus(status);
|
|
20
20
|
|
|
21
21
|
/** The message text for a channel: task template -> channel template -> global template -> built-in defaults (#233). */
|
|
@@ -362,7 +362,9 @@ const CHANNEL_HANDLERS = {
|
|
|
362
362
|
},
|
|
363
363
|
|
|
364
364
|
async tts({ task, runInfo, settings, http, signal, timeoutMs }) {
|
|
365
|
-
const
|
|
365
|
+
const defaultPort = settings._webServerPort || settings.webServerPort || (settings.webServer && settings.webServer.port) || 3080;
|
|
366
|
+
const rawBase = String(settings.ttsBaseUrl || '').trim();
|
|
367
|
+
const base = (rawBase ? rawBase : `http://127.0.0.1:${defaultPort}`).replace(/\/+$/, '');
|
|
366
368
|
const payload = { text: truncateText(messageTextFor('tts', task, runInfo, settings), 600) };
|
|
367
369
|
const data = await postJson(http, `${base}/dsh-tts/speak`, { 'Content-Type': 'application/json' }, JSON.stringify(payload), 'tts', signal, timeoutMs);
|
|
368
370
|
return { provider: data.provider || 'dsh-tts', tookMs: data.tookMs || null };
|