@goodandready/dsh-cron 0.3.39 → 0.3.41
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/log-streamer.js +0 -1
- package/lib/routes/api-collection.js +1 -1
- package/lib/routes/api-item.js +2 -2
- package/lib/routes/routes.js +7 -1
- package/lib/runtimes/runner.js +66 -26
- package/lib/runtimes/runtimes.js +19 -6
- package/lib/scheduler/scheduler-preflight.js +2 -1
- package/package.json +1 -1
package/lib/log-streamer.js
CHANGED
|
@@ -38,7 +38,7 @@ export async function handleTaskCollection({ store, scheduler, collection, req,
|
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
40
|
if (req.method === 'POST') {
|
|
41
|
-
await createOrUpdateTask({ store, scheduler, req, res, apiToken, ctx
|
|
41
|
+
await createOrUpdateTask({ store, scheduler, req, res, apiToken, ctx });
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
sendJson(res, 405, NOT_ALLOWED);
|
package/lib/routes/api-item.js
CHANGED
|
@@ -21,12 +21,12 @@ export async function handleTaskItem({ store, scheduler, req, res, url, id, acti
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
if (req.method === 'GET') {
|
|
24
|
-
|
|
24
|
+
if (rejectCrossOrigin(req, res, { apiToken, ctx })) return;
|
|
25
|
+
// Both /dsh-cron/tasks/:id/stream and /dsh-cron/tasks/:id/logs/stream (#392, #424)
|
|
25
26
|
if (action === 'stream' || (action === 'logs' && subAction === 'stream')) {
|
|
26
27
|
logStreamer.handleSseRequest(id, req, res);
|
|
27
28
|
return;
|
|
28
29
|
}
|
|
29
|
-
if (rejectCrossOrigin(req, res, { apiToken, ctx })) return;
|
|
30
30
|
if (action === 'archive') {
|
|
31
31
|
const limit = parseInt(url.searchParams.get('limit') || '50', 10);
|
|
32
32
|
const offset = parseInt(url.searchParams.get('offset') || '0', 10);
|
package/lib/routes/routes.js
CHANGED
|
@@ -33,6 +33,9 @@ export function registerRoutes(ctx, { store, scheduler, recommendations }) {
|
|
|
33
33
|
res.end('GET only');
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
|
+
const settings = store && typeof store.getSettings === 'function' ? store.getSettings() : {};
|
|
37
|
+
const apiToken = settings.apiToken;
|
|
38
|
+
if (rejectCrossOrigin(req, res, { apiToken, ctx })) return;
|
|
36
39
|
res.writeHead(200, {
|
|
37
40
|
'content-type': 'application/json; charset=utf-8',
|
|
38
41
|
'cache-control': 'no-store',
|
|
@@ -52,6 +55,9 @@ export function registerRoutes(ctx, { store, scheduler, recommendations }) {
|
|
|
52
55
|
res.end('GET only');
|
|
53
56
|
return;
|
|
54
57
|
}
|
|
58
|
+
const settings = store && typeof store.getSettings === 'function' ? store.getSettings() : {};
|
|
59
|
+
const apiToken = settings.apiToken;
|
|
60
|
+
if (rejectCrossOrigin(req, res, { apiToken, ctx })) return;
|
|
55
61
|
const url = new URL(req.url, 'http://127.0.0.1');
|
|
56
62
|
const month = url.pathname.replace(/^\/dsh-cron\/history\/archive\//, '').replace(/\/$/, '');
|
|
57
63
|
if (!/^\d{4}-\d{2}$/.test(month)) {
|
|
@@ -67,7 +73,7 @@ export function registerRoutes(ctx, { store, scheduler, recommendations }) {
|
|
|
67
73
|
}
|
|
68
74
|
res.writeHead(200, {
|
|
69
75
|
'content-type': 'application/json; charset=utf-8',
|
|
70
|
-
'cache-control': '
|
|
76
|
+
'cache-control': 'no-store',
|
|
71
77
|
});
|
|
72
78
|
res.end(JSON.stringify({ ok: true, month, archive }));
|
|
73
79
|
}
|
package/lib/runtimes/runner.js
CHANGED
|
@@ -460,8 +460,8 @@ export class SessionRunner {
|
|
|
460
460
|
|
|
461
461
|
const targetSessionId = resolveTargetSessionId(task.targetSessionId, task.targetSessionReset);
|
|
462
462
|
|
|
463
|
-
const mintSessionId = () => {
|
|
464
|
-
if (targetSessionId) {
|
|
463
|
+
const mintSessionId = ({ fresh = false } = {}) => {
|
|
464
|
+
if (targetSessionId && !fresh) {
|
|
465
465
|
if (sessionModule && typeof sessionModule.SessionId === 'function') {
|
|
466
466
|
return sessionModule.SessionId(targetSessionId);
|
|
467
467
|
}
|
|
@@ -572,35 +572,52 @@ export class SessionRunner {
|
|
|
572
572
|
}
|
|
573
573
|
} : {};
|
|
574
574
|
|
|
575
|
-
// If targetSessionId is set, attempt to resume the existing session (#143, #346, GH #9)
|
|
575
|
+
// If targetSessionId is set, attempt to borrow or resume the existing session (#143, #346, GH #9, GH #23)
|
|
576
576
|
let resumeFailure = null;
|
|
577
577
|
const agents = this._getAgentsService();
|
|
578
|
-
if (prep.targetSessionId && agents
|
|
578
|
+
if (prep.targetSessionId && agents) {
|
|
579
|
+
let liveAgent = null;
|
|
579
580
|
try {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
});
|
|
581
|
+
if (typeof agents.get === 'function') {
|
|
582
|
+
liveAgent = agents.get(prep.targetSessionId);
|
|
583
|
+
}
|
|
584
|
+
} catch (_getErr) {
|
|
585
|
+
liveAgent = null;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (liveAgent) {
|
|
589
|
+
handle = { agent: liveAgent, __borrowedLive: true };
|
|
590
590
|
isResumed = true;
|
|
591
|
-
logger.info(`[dsh-cron]
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
591
|
+
logger.info(`[dsh-cron] borrowed active live session: ${prep.targetSessionId}`);
|
|
592
|
+
} else if (typeof agents.resume === 'function') {
|
|
593
|
+
try {
|
|
594
|
+
handle = await agents.resume({
|
|
595
|
+
sessionId: prep.targetSessionId,
|
|
596
|
+
resumeSessionId: prep.targetSessionId,
|
|
597
|
+
meta: {
|
|
598
|
+
cwd: prep.cwd,
|
|
599
|
+
...(preset ? { agentPreset: preset.id } : {})
|
|
600
|
+
},
|
|
601
|
+
agentOptions: prep.provider && prep.model ? { provider: prep.provider, model: prep.model } : undefined,
|
|
602
|
+
...presetSetup,
|
|
603
|
+
});
|
|
604
|
+
isResumed = true;
|
|
605
|
+
logger.info(`[dsh-cron] resumed existing persistent session: ${prep.targetSessionId}`);
|
|
606
|
+
} catch (resumeErr) {
|
|
607
|
+
resumeFailure = resumeErr;
|
|
608
|
+
logger.warn(`[dsh-cron] could not resume session ${prep.targetSessionId} (${resumeErr.message}), fallback creating new session`);
|
|
609
|
+
handle = null;
|
|
610
|
+
}
|
|
596
611
|
}
|
|
597
612
|
}
|
|
598
613
|
|
|
599
614
|
// Create new session if not resumed
|
|
600
615
|
if (!handle) {
|
|
616
|
+
const isLockedOrColliding = resumeFailure && /already (?:active|owned|exists)|write handle|locked|collision/i.test(resumeFailure.message || '');
|
|
617
|
+
const targetId = prep.mintSessionId({ fresh: isLockedOrColliding });
|
|
601
618
|
try {
|
|
602
619
|
handle = await agents.create({
|
|
603
|
-
sessionId:
|
|
620
|
+
sessionId: targetId,
|
|
604
621
|
meta: {
|
|
605
622
|
cwd: prep.cwd,
|
|
606
623
|
ephemeral: !prep.targetSessionId,
|
|
@@ -611,10 +628,32 @@ export class SessionRunner {
|
|
|
611
628
|
...presetSetup,
|
|
612
629
|
});
|
|
613
630
|
} catch (createErr) {
|
|
614
|
-
if (
|
|
615
|
-
|
|
631
|
+
if (prep.targetSessionId && targetId === prep.targetSessionId) {
|
|
632
|
+
logger.warn(`[dsh-cron] failed to create session with targetSessionId ${prep.targetSessionId} (${createErr.message}), retrying with fresh session ID`);
|
|
633
|
+
try {
|
|
634
|
+
handle = await agents.create({
|
|
635
|
+
sessionId: prep.mintSessionId({ fresh: true }),
|
|
636
|
+
meta: {
|
|
637
|
+
cwd: prep.cwd,
|
|
638
|
+
ephemeral: !prep.targetSessionId,
|
|
639
|
+
internal: !prep.targetSessionId,
|
|
640
|
+
...(preset ? { agentPreset: preset.id } : {})
|
|
641
|
+
},
|
|
642
|
+
agentOptions: prep.provider && prep.model ? { provider: prep.provider, model: prep.model } : undefined,
|
|
643
|
+
...presetSetup,
|
|
644
|
+
});
|
|
645
|
+
} catch (retryErr) {
|
|
646
|
+
if (resumeFailure) {
|
|
647
|
+
throw new Error(`could not resume session "${prep.targetSessionId}" (${resumeFailure.message}): ${retryErr.message}`);
|
|
648
|
+
}
|
|
649
|
+
throw retryErr;
|
|
650
|
+
}
|
|
651
|
+
} else {
|
|
652
|
+
if (resumeFailure) {
|
|
653
|
+
throw new Error(`could not resume session "${prep.targetSessionId}" (${resumeFailure.message}): ${createErr.message}`);
|
|
654
|
+
}
|
|
655
|
+
throw createErr;
|
|
616
656
|
}
|
|
617
|
-
throw createErr;
|
|
618
657
|
}
|
|
619
658
|
}
|
|
620
659
|
|
|
@@ -771,9 +810,6 @@ export class SessionRunner {
|
|
|
771
810
|
if (handle?.agent) {
|
|
772
811
|
bestEffort('approval-set-policy-agent', () => approval.setPolicy(handle.agent, 'never'), logger);
|
|
773
812
|
}
|
|
774
|
-
if (handle?.agent?.session) {
|
|
775
|
-
bestEffort('approval-set-policy-session', () => approval.setPolicy(handle.agent.session, 'never'), logger);
|
|
776
|
-
}
|
|
777
813
|
}
|
|
778
814
|
} catch (appErr) {
|
|
779
815
|
logger.warn('[dsh-cron] approval setPolicy not applied:', appErr.message);
|
|
@@ -912,6 +948,10 @@ export class SessionRunner {
|
|
|
912
948
|
|
|
913
949
|
/** Dispose the session, archive it best-effort, drop the worktree (#30, #29). */
|
|
914
950
|
async _disposeAgentRun(task, prep, handle) {
|
|
951
|
+
if (handle?.__borrowedLive) {
|
|
952
|
+
logger.info(`[dsh-cron] skipping disposal of borrowed live session: ${prep.targetSessionId}`);
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
915
955
|
let archivedSessionId = null;
|
|
916
956
|
archivedSessionId = bestEffort('get-archived-session-id', () => {
|
|
917
957
|
return (handle && handle.agent && handle.agent.session && handle.agent.session.id) || null;
|
package/lib/runtimes/runtimes.js
CHANGED
|
@@ -90,6 +90,24 @@ export function buildPythonInvocation(task, cwd) {
|
|
|
90
90
|
return { file: interpreter, args: ['-c', String(task.prompt || '')] };
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/** #427: Validate hostnames against cloud provider metadata and link-local IP addresses. */
|
|
94
|
+
export function isCloudMetadataHost(hostname) {
|
|
95
|
+
if (!hostname || typeof hostname !== 'string') return false;
|
|
96
|
+
const h = hostname.toLowerCase().replace(/^\[|\]$/g, '');
|
|
97
|
+
if (
|
|
98
|
+
h === '169.254.169.254' ||
|
|
99
|
+
h.startsWith('169.254.') ||
|
|
100
|
+
h === '100.100.100.200' ||
|
|
101
|
+
h === 'fd00:ec2::254' ||
|
|
102
|
+
h === 'metadata.google.internal' ||
|
|
103
|
+
h === 'metadata.azure.com' ||
|
|
104
|
+
h.endsWith('.internal.cloudapp.net')
|
|
105
|
+
) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
93
111
|
/** #7: build the HTTP trigger request description. */
|
|
94
112
|
export function buildHttpRequest(task) {
|
|
95
113
|
const raw = String(task.httpUrl || task.prompt || '').trim();
|
|
@@ -101,12 +119,7 @@ export function buildHttpRequest(task) {
|
|
|
101
119
|
throw new Error(`Invalid HTTP URL: ${raw}`);
|
|
102
120
|
}
|
|
103
121
|
const hostname = url.hostname.toLowerCase();
|
|
104
|
-
if (
|
|
105
|
-
hostname === '169.254.169.254' ||
|
|
106
|
-
hostname === 'metadata.google.internal' ||
|
|
107
|
-
hostname === 'metadata.azure.com' ||
|
|
108
|
-
hostname.endsWith('.internal.cloudapp.net')
|
|
109
|
-
) {
|
|
122
|
+
if (isCloudMetadataHost(hostname)) {
|
|
110
123
|
throw new Error(`Access to cloud metadata endpoint "${hostname}" is restricted`);
|
|
111
124
|
}
|
|
112
125
|
let headers = {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isCloudMetadataHost } from '../runtimes/runtimes.js';
|
|
1
2
|
import { collectDigestMetrics, formatDigestReport } from '../digest.js';
|
|
2
3
|
import { exec } from 'node:child_process';
|
|
3
4
|
import fs from 'node:fs';
|
|
@@ -42,7 +43,7 @@ export async function executePreflight(task) {
|
|
|
42
43
|
try {
|
|
43
44
|
const u = new URL(target);
|
|
44
45
|
const host = u.hostname.toLowerCase();
|
|
45
|
-
if (host
|
|
46
|
+
if (isCloudMetadataHost(host)) {
|
|
46
47
|
return { ok: false, reason: `HTTP check to cloud metadata endpoint "${host}" is restricted` };
|
|
47
48
|
}
|
|
48
49
|
} catch (_e) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-cron",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.41",
|
|
4
4
|
"description": "Background automation runner for DSH: isolated agent runs, script/HTTP/SSH/Docker runtimes, cost guard, notifications, heartbeats.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|