@goodandready/dsh-cron 0.3.42 → 0.3.44

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.
Files changed (42) hide show
  1. package/lib/client.js +998 -3987
  2. package/lib/llm-ask.js +2 -1
  3. package/lib/routes/api-actions.js +36 -6
  4. package/lib/routes/api-item.js +1 -1
  5. package/lib/routes/api-webhook.js +2 -4
  6. package/lib/runtimes/runner-external.js +2 -3
  7. package/lib/runtimes/runner-session.js +385 -0
  8. package/lib/runtimes/runner-turn.js +255 -0
  9. package/lib/runtimes/runner.js +43 -616
  10. package/lib/scheduler/scheduler-execution.js +4 -1
  11. package/lib/scheduler/scheduler-lifecycle.js +65 -0
  12. package/lib/scheduler/scheduler-parser.js +261 -0
  13. package/lib/scheduler/scheduler.js +24 -319
  14. package/lib/store/store-archive.js +195 -0
  15. package/lib/store/store-io.js +331 -0
  16. package/lib/store/store-loader.js +165 -0
  17. package/lib/store/store-migration.js +38 -0
  18. package/lib/store/store-settings.js +126 -0
  19. package/lib/store/store-stats.js +55 -0
  20. package/lib/store/store.js +127 -810
  21. package/package.json +8 -6
  22. package/lib/client-src/00-open.js +0 -63
  23. package/lib/client-src/10-locales.js +0 -540
  24. package/lib/client-src/20-styles.js +0 -64
  25. package/lib/client-src/30-utils.js +0 -372
  26. package/lib/client-src/40-delivery-settings.js +0 -96
  27. package/lib/client-src/45-dag-graph-view.js +0 -287
  28. package/lib/client-src/46-live-logs-modal.js +0 -108
  29. package/lib/client-src/47-analytics-digest-modal.js +0 -166
  30. package/lib/client-src/48-timeline-heatmap-view.js +0 -176
  31. package/lib/client-src/50-cron-screen-state.js +0 -457
  32. package/lib/client-src/52-cron-screen-actions.js +0 -330
  33. package/lib/client-src/53-cron-screen-modal-actions.js +0 -677
  34. package/lib/client-src/54-cron-screen-analytics-actions.js +0 -68
  35. package/lib/client-src/55-cron-screen-view.js +0 -378
  36. package/lib/client-src/60-modal-task-form-head.js +0 -581
  37. package/lib/client-src/62-modal-task-form-tail.js +0 -563
  38. package/lib/client-src/65-modal-dialogs.js +0 -350
  39. package/lib/client-src/70-settings-card.js +0 -453
  40. package/lib/client-src/75-quick-action-bar.js +0 -146
  41. package/lib/client-src/80-sidebar-jobs.js +0 -200
  42. package/lib/client-src/90-close.js +0 -219
@@ -0,0 +1,331 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { pipeline } from 'node:stream/promises';
4
+ import { logger } from '../logger.js';
5
+ import { bestEffort } from '../best-effort.js';
6
+ import { CURRENT_SCHEMA_VERSION } from './store-migration.js';
7
+
8
+ export function isAccessOrIoError(err) {
9
+ if (!err) return false;
10
+ const codes = ['EACCES', 'EPERM', 'EIO', 'EBUSY', 'EROFS', 'EMFILE', 'ENFILE'];
11
+ return Boolean(err.code && codes.includes(err.code));
12
+ }
13
+
14
+ export function cleanOrphanedTmpFiles(dir) {
15
+ try {
16
+ if (!fs.existsSync(dir)) return;
17
+ const now = Date.now();
18
+ const files = fs.readdirSync(dir);
19
+ for (const f of files) {
20
+ if (f.startsWith('tasks.json.tmp.') || f.startsWith('tasks-history-archive.json.tmp.')) {
21
+ const full = path.join(dir, f);
22
+ bestEffort('clean-tmp-file', () => {
23
+ const stat = fs.statSync(full);
24
+ if (now - stat.mtimeMs > 10 * 60 * 1000) {
25
+ fs.unlinkSync(full);
26
+ logger.info(`[dsh-cron] cleaned orphaned temporary store file: ${f}`);
27
+ }
28
+ });
29
+ }
30
+ }
31
+ } catch (err) {
32
+ bestEffort('log-clean-err', () => logger.debug?.(`[dsh-cron] clean tmp files error: ${err.message}`));
33
+ }
34
+ }
35
+
36
+ export function scheduleSave(store, delay = 50) {
37
+ if (store._readOnly || store._loadFailed) {
38
+ return;
39
+ }
40
+ if (store._saveTimer) return;
41
+ store._saveTimer = setTimeout(() => {
42
+ store._saveTimer = null;
43
+ store.saveAsync().catch((err) => {
44
+ logger.error('[dsh-cron] debounced save failed:', err.message);
45
+ });
46
+ }, delay);
47
+ if (typeof store._saveTimer.unref === 'function') {
48
+ store._saveTimer.unref();
49
+ }
50
+ }
51
+
52
+ export function flushSync(store) {
53
+ if (store._saveTimer) {
54
+ clearTimeout(store._saveTimer);
55
+ store._saveTimer = null;
56
+ }
57
+ if (store._readOnly || store._loadFailed) {
58
+ return;
59
+ }
60
+ store.save();
61
+ }
62
+
63
+ export function createSnapshot(store) {
64
+ const tasksSnapshot = Array.from(store.tasks.values()).map((t) => ({
65
+ ...t,
66
+ costLedger: Array.isArray(t.costLedger) ? t.costLedger.slice() : t.costLedger,
67
+ }));
68
+ const historySnapshot = {};
69
+ for (const [id, runs] of store.history.entries()) {
70
+ historySnapshot[id] = Array.isArray(runs) ? runs.slice() : runs;
71
+ }
72
+ return {
73
+ version: 1,
74
+ schemaVersion: CURRENT_SCHEMA_VERSION,
75
+ updatedAt: new Date().toISOString(),
76
+ tasks: tasksSnapshot,
77
+ history: historySnapshot,
78
+ settings: store.settings ? { ...store.settings } : {},
79
+ chainCheckpoints: store.chainCheckpoints ? { ...store.chainCheckpoints } : {},
80
+ artifacts: store.artifacts ? { ...store.artifacts } : {},
81
+ };
82
+ }
83
+
84
+ export function waitForDiskVersion(store, version) {
85
+ if ((store._diskVersion || 0) >= version || (store._lastSyncVersion || 0) >= version) {
86
+ return Promise.resolve();
87
+ }
88
+ return new Promise((resolve, reject) => {
89
+ if (!store._diskWaiters) store._diskWaiters = [];
90
+ store._diskWaiters.push({ version, resolve, reject });
91
+ });
92
+ }
93
+
94
+ export function notifyDiskVersion(store, writtenVersion, err = null) {
95
+ if (!store._diskWaiters || store._diskWaiters.length === 0) return;
96
+ const remaining = [];
97
+ for (const waiter of store._diskWaiters) {
98
+ if (err) {
99
+ waiter.reject(err);
100
+ } else if ((store._diskVersion || 0) >= waiter.version || (store._lastSyncVersion || 0) >= waiter.version) {
101
+ waiter.resolve();
102
+ } else {
103
+ remaining.push(waiter);
104
+ }
105
+ }
106
+ store._diskWaiters = remaining;
107
+ }
108
+
109
+ export async function saveAsync(store) {
110
+ if (store._readOnly || store._loadFailed) {
111
+ const reason = store._accessErrorCode
112
+ ? `access error (${store._accessErrorCode})`
113
+ : (store._loadError ? store._loadError.message : 'unrecovered read failure');
114
+ logger.error(`[dsh-cron] saveAsync blocked: store is in read-only recovery mode due to ${reason} on "${store.filePath}". File on disk was preserved.`);
115
+ return Promise.reject(new Error(`Store write blocked: read-only recovery mode (${store.filePath})`));
116
+ }
117
+ if (store._saveTimer) {
118
+ clearTimeout(store._saveTimer);
119
+ store._saveTimer = null;
120
+ }
121
+ const version = ++store._saveVersion;
122
+ const data = store._createSnapshot();
123
+
124
+ const writeOp = async () => {
125
+ if ((store._diskVersion || 0) >= version || (store._lastSyncVersion || 0) >= version) {
126
+ store._notifyDiskVersion(version);
127
+ return;
128
+ }
129
+ const dir = path.dirname(store.filePath);
130
+ await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
131
+ bestEffort('chmod-dir', () => fs.chmodSync(dir, 0o700));
132
+
133
+ const rand = Math.random().toString(36).slice(2, 8);
134
+ const tmp = `${store.filePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
135
+ store._currentAsyncTmp = tmp;
136
+
137
+ try {
138
+ await store._writeStoreStream(tmp, data, version);
139
+ bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
140
+ } catch (writeErr) {
141
+ await bestEffort('store-unlink-tmp', () => fs.promises.unlink(tmp), store?.logger || logger);
142
+ if (store._currentAsyncTmp === tmp) store._currentAsyncTmp = null;
143
+ store._notifyDiskVersion(null, writeErr);
144
+ throw writeErr;
145
+ }
146
+
147
+ if ((store._diskVersion || 0) >= version || (store._lastSyncVersion || 0) >= version || store._saveVersion > version) {
148
+ await bestEffort('store-unlink-stale-tmp', () => fs.promises.unlink(tmp), store?.logger || logger);
149
+ if (store._currentAsyncTmp === tmp) store._currentAsyncTmp = null;
150
+ return;
151
+ }
152
+
153
+ try {
154
+ await fs.promises.rename(tmp, store.filePath);
155
+ bestEffort('chmod-store', () => fs.chmodSync(store.filePath, 0o600));
156
+ store._currentAsyncTmp = null;
157
+ store._diskVersion = Math.max(store._diskVersion || 0, version);
158
+ store._lastWrittenVersion = store._diskVersion;
159
+ store._notifyDiskVersion(store._diskVersion);
160
+ } catch (err) {
161
+ store._currentAsyncTmp = null;
162
+ if (err.code === 'ENOENT' && (store._lastSyncVersion || 0) >= version) {
163
+ store._notifyDiskVersion(version);
164
+ return;
165
+ }
166
+ store._notifyDiskVersion(null, err);
167
+ throw err;
168
+ }
169
+
170
+ const now = Date.now();
171
+ if (now - store._lastBackupTime > 60000) {
172
+ store._lastBackupTime = now;
173
+ try {
174
+ const bakPath = `${store.filePath}.bak`;
175
+ await fs.promises.copyFile(store.filePath, bakPath);
176
+ bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
177
+ } catch (bakErr) {
178
+ logger.warn('[dsh-cron] async backup copy failed:', bakErr.message);
179
+ }
180
+ }
181
+ };
182
+
183
+ store._savePromise = store._savePromise.then(writeOp, writeOp);
184
+ await store._savePromise;
185
+ await store._waitForDiskVersion(version);
186
+ }
187
+
188
+ export async function writeStoreStream(store, filePath, data, version) {
189
+ const isCanceled = () => {
190
+ return (
191
+ (store._diskVersion || 0) >= version ||
192
+ (store._lastSyncVersion || 0) >= version ||
193
+ store._saveVersion > version
194
+ );
195
+ };
196
+
197
+ if (isCanceled()) {
198
+ return;
199
+ }
200
+
201
+ const ac = new AbortController();
202
+ let ws;
203
+ try {
204
+ ws = fs.createWriteStream(filePath, { encoding: 'utf-8', mode: 0o600 });
205
+ } catch (createErr) {
206
+ if (isCanceled()) return;
207
+ throw createErr;
208
+ }
209
+
210
+ async function* generateChunks() {
211
+ await new Promise((r) => setImmediate(r));
212
+ if (isCanceled()) {
213
+ ac.abort();
214
+ return;
215
+ }
216
+
217
+ yield '{"version":' + JSON.stringify(data.version || 1);
218
+ yield ',"schemaVersion":' + JSON.stringify(data.schemaVersion || CURRENT_SCHEMA_VERSION);
219
+ yield ',"updatedAt":' + JSON.stringify(data.updatedAt || new Date().toISOString());
220
+ yield ',"settings":' + JSON.stringify(data.settings || {});
221
+
222
+ yield ',"tasks":[';
223
+ const tasks = data.tasks || [];
224
+ for (let i = 0; i < tasks.length; i++) {
225
+ if (isCanceled()) {
226
+ ac.abort();
227
+ return;
228
+ }
229
+ if (i > 0) yield ',';
230
+ yield JSON.stringify(tasks[i]);
231
+ if ((i + 1) % 10 === 0) {
232
+ await new Promise((r) => setImmediate(r));
233
+ }
234
+ }
235
+ yield ']';
236
+
237
+ yield ',"history":{';
238
+ const historyEntries = Object.entries(data.history || {});
239
+ for (let i = 0; i < historyEntries.length; i++) {
240
+ if (isCanceled()) {
241
+ ac.abort();
242
+ return;
243
+ }
244
+ if (i > 0) yield ',';
245
+ const [taskId, runs] = historyEntries[i];
246
+ yield JSON.stringify(taskId) + ':[';
247
+ if (Array.isArray(runs)) {
248
+ for (let j = 0; j < runs.length; j++) {
249
+ if (isCanceled()) {
250
+ ac.abort();
251
+ return;
252
+ }
253
+ if (j > 0) yield ',';
254
+ yield JSON.stringify(runs[j]);
255
+ if ((j + 1) % 15 === 0) {
256
+ await new Promise((r) => setImmediate(r));
257
+ }
258
+ }
259
+ } else {
260
+ yield JSON.stringify(runs);
261
+ }
262
+ yield ']';
263
+ await new Promise((r) => setImmediate(r));
264
+ }
265
+ yield '}';
266
+ yield ',"chainCheckpoints":' + JSON.stringify(data.chainCheckpoints || {});
267
+ yield ',"artifacts":' + JSON.stringify(data.artifacts || {});
268
+ yield '}';
269
+ }
270
+
271
+ try {
272
+ await pipeline(generateChunks(), ws, { signal: ac.signal });
273
+ } catch (err) {
274
+ if (ac.signal.aborted || isCanceled()) {
275
+ return;
276
+ }
277
+ throw err;
278
+ }
279
+ }
280
+
281
+ export function save(store) {
282
+ if (store._readOnly || store._loadFailed) {
283
+ const reason = store._accessErrorCode
284
+ ? `access error (${store._accessErrorCode})`
285
+ : (store._loadError ? store._loadError.message : 'unrecovered read failure');
286
+ logger.error(`[dsh-cron] save blocked: store is in read-only recovery mode due to ${reason} on "${store.filePath}". File on disk was preserved.`);
287
+ throw new Error(`Store write blocked: read-only recovery mode (${store.filePath})`);
288
+ }
289
+ if (store._saveTimer) {
290
+ clearTimeout(store._saveTimer);
291
+ store._saveTimer = null;
292
+ }
293
+ const version = ++store._saveVersion;
294
+ store._lastSyncVersion = version;
295
+
296
+ if (store._currentAsyncTmp) {
297
+ const inFlightTmp = store._currentAsyncTmp;
298
+ store._currentAsyncTmp = null;
299
+ bestEffort('unlink-async-tmp', () => fs.unlinkSync(inFlightTmp));
300
+ }
301
+ try {
302
+ const dir = path.dirname(store.filePath);
303
+ if (!fs.existsSync(dir)) {
304
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
305
+ }
306
+ bestEffort('chmod-dir', () => fs.chmodSync(dir, 0o700));
307
+ const data = store._createSnapshot();
308
+ const rand = Math.random().toString(36).slice(2, 8);
309
+ const tmp = `${store.filePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
310
+ fs.writeFileSync(tmp, JSON.stringify(data, null, 2), { encoding: 'utf-8', mode: 0o600 });
311
+ bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
312
+ fs.renameSync(tmp, store.filePath);
313
+ bestEffort('chmod-store', () => fs.chmodSync(store.filePath, 0o600));
314
+ store._diskVersion = Math.max(store._diskVersion || 0, version);
315
+ store._lastWrittenVersion = store._diskVersion;
316
+ store._notifyDiskVersion(store._diskVersion);
317
+
318
+ const now = Date.now();
319
+ if (now - store._lastBackupTime > 60000) {
320
+ store._lastBackupTime = now;
321
+ bestEffort('backup-on-save', () => {
322
+ const bakPath = `${store.filePath}.bak`;
323
+ fs.copyFileSync(store.filePath, bakPath);
324
+ fs.chmodSync(bakPath, 0o600);
325
+ });
326
+ }
327
+ } catch (err) {
328
+ logger.error('[dsh-cron] store save error:', err.message);
329
+ throw err;
330
+ }
331
+ }
@@ -0,0 +1,165 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { logger } from '../logger.js';
4
+ import { bestEffort } from '../best-effort.js';
5
+ import { CURRENT_SCHEMA_VERSION, runStoreMigrations } from './store-migration.js';
6
+ import { isAccessOrIoError, cleanOrphanedTmpFiles } from './store-io.js';
7
+ import { pruneOldMonthlyArchives } from './store-archive.js';
8
+
9
+ export function loadStoreData(store, raw) {
10
+ let data = JSON.parse(raw);
11
+ const storedVersion = Number(data.schemaVersion) || 1;
12
+ if (storedVersion < CURRENT_SCHEMA_VERSION) {
13
+ // Pre-migration backup (#390)
14
+ try {
15
+ const preBakPath = `${store.filePath}.bak-v${storedVersion}-${Date.now()}`;
16
+ fs.writeFileSync(preBakPath, raw, { encoding: 'utf-8', mode: 0o600 });
17
+ bestEffort('chmod-pre-bak', () => fs.chmodSync(preBakPath, 0o600));
18
+ logger.info(`[dsh-cron] pre-migration backup saved to ${preBakPath}`);
19
+ } catch (bakErr) {
20
+ logger.warn('[dsh-cron] failed to write pre-migration backup:', bakErr.message);
21
+ }
22
+ data = runStoreMigrations(data, storedVersion, CURRENT_SCHEMA_VERSION);
23
+ store._needsMigrationFlush = true;
24
+ }
25
+ store.tasks.clear();
26
+ store.history.clear();
27
+ if (Array.isArray(data.tasks)) {
28
+ const now = Date.now();
29
+ const cutoff24h = now - 86400000;
30
+ for (const t of data.tasks) {
31
+ if (Array.isArray(t.costLedger) && t.costLedger.length > 0) {
32
+ t.costLedger = t.costLedger.filter(e => e && typeof e.at === 'number' && e.at >= cutoff24h);
33
+ } else {
34
+ // Seed from active history if present (#221)
35
+ const hist = (data.history && Array.isArray(data.history[t.id])) ? data.history[t.id] : [];
36
+ t.costLedger = hist
37
+ .filter(r => r && typeof r.at === 'number' && r.at >= cutoff24h)
38
+ .map(r => ({
39
+ at: r.at,
40
+ costUsd: Number(r.costUsd) || 0,
41
+ tokens: (r.usage?.inputTokens || 0) + (r.usage?.outputTokens || 0) + (r.usage?.cacheReadTokens || 0),
42
+ }));
43
+ }
44
+ store.tasks.set(t.id, t);
45
+ }
46
+ }
47
+ if (data.history && typeof data.history === 'object') {
48
+ for (const [k, v] of Object.entries(data.history)) {
49
+ store.history.set(k, Array.isArray(v) ? v : []);
50
+ }
51
+ }
52
+ if (data.settings && typeof data.settings === 'object') {
53
+ store.settings = data.settings;
54
+ }
55
+ if (data.chainCheckpoints && typeof data.chainCheckpoints === 'object') {
56
+ store.chainCheckpoints = data.chainCheckpoints;
57
+ }
58
+ if (data.artifacts && typeof data.artifacts === 'object') {
59
+ store.artifacts = data.artifacts;
60
+ }
61
+ }
62
+
63
+ export function initStore(store) {
64
+ const dir = path.dirname(store.filePath);
65
+ if (!fs.existsSync(dir)) {
66
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
67
+ }
68
+ bestEffort('chmod-dir', () => fs.chmodSync(dir, 0o700));
69
+ cleanOrphanedTmpFiles(dir);
70
+ const bakPath = `${store.filePath}.bak`;
71
+
72
+ if (fs.existsSync(store.filePath)) {
73
+ try {
74
+ bestEffort('chmod-store', () => fs.chmodSync(store.filePath, 0o600));
75
+ const raw = fs.readFileSync(store.filePath, 'utf-8');
76
+ loadStoreData(store, raw);
77
+ store._readOnly = false;
78
+ store._loadFailed = false;
79
+ store._accessErrorCode = null;
80
+ store._loadError = null;
81
+
82
+ if (!fs.existsSync(bakPath)) {
83
+ bestEffort('backup-initial', () => {
84
+ fs.copyFileSync(store.filePath, bakPath);
85
+ fs.chmodSync(bakPath, 0o600);
86
+ });
87
+ } else {
88
+ bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
89
+ }
90
+ } catch (err) {
91
+ if (isAccessOrIoError(err)) {
92
+ // File exists on disk but is inaccessible due to OS permissions/IO error (#435)
93
+ store._enterReadOnlyMode(err, 'read', store.filePath);
94
+ } else {
95
+ // Genuine corruption or invalid data structure
96
+ logger.error(`[dsh-cron] store read error (corrupted file): ${err.message}`);
97
+ try {
98
+ const corruptedPath = `${store.filePath}.corrupted.${Date.now()}`;
99
+ fs.copyFileSync(store.filePath, corruptedPath);
100
+ bestEffort('chmod-corrupted', () => fs.chmodSync(corruptedPath, 0o600));
101
+ logger.warn(`[dsh-cron] corrupted store backed up to ${corruptedPath}`);
102
+ } catch (snapErr) {
103
+ logger.error('[dsh-cron] failed to snapshot corrupted store:', snapErr.message);
104
+ }
105
+ if (fs.existsSync(bakPath)) {
106
+ try {
107
+ bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
108
+ const rawBak = fs.readFileSync(bakPath, 'utf-8');
109
+ loadStoreData(store, rawBak);
110
+ store._readOnly = false;
111
+ store._loadFailed = false;
112
+ store._accessErrorCode = null;
113
+ store._loadError = null;
114
+ logger.warn(`[dsh-cron] successfully recovered tasks store from ${bakPath}`);
115
+ } catch (bakErr) {
116
+ if (isAccessOrIoError(bakErr)) {
117
+ store._enterReadOnlyMode(bakErr, 'read', bakPath);
118
+ } else {
119
+ logger.error('[dsh-cron] failed to recover tasks store from .bak:', bakErr.message);
120
+ store._enterReadOnlyMode(bakErr, 'parse', bakPath);
121
+ }
122
+ }
123
+ } else {
124
+ store._enterReadOnlyMode(err, 'parse', store.filePath);
125
+ }
126
+ }
127
+ }
128
+ } else if (fs.existsSync(bakPath)) {
129
+ try {
130
+ bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
131
+ const rawBak = fs.readFileSync(bakPath, 'utf-8');
132
+ loadStoreData(store, rawBak);
133
+ store._readOnly = false;
134
+ store._loadFailed = false;
135
+ store._accessErrorCode = null;
136
+ store._loadError = null;
137
+ bestEffort('restore-from-bak', () => {
138
+ fs.copyFileSync(bakPath, store.filePath);
139
+ fs.chmodSync(store.filePath, 0o600);
140
+ });
141
+ logger.warn(`[dsh-cron] restored missing tasks store from ${bakPath}`);
142
+ } catch (bakErr) {
143
+ if (isAccessOrIoError(bakErr)) {
144
+ store._enterReadOnlyMode(bakErr, 'read', bakPath);
145
+ } else {
146
+ logger.error('[dsh-cron] failed to restore tasks store from .bak:', bakErr.message);
147
+ store._enterReadOnlyMode(bakErr, 'parse', bakPath);
148
+ }
149
+ }
150
+ } else {
151
+ // Clean initial start: neither primary nor backup exists yet (#435)
152
+ store._readOnly = false;
153
+ store._loadFailed = false;
154
+ store._accessErrorCode = null;
155
+ store._loadError = null;
156
+ }
157
+
158
+ if (store._needsMigrationFlush && !store._readOnly && !store._loadFailed) {
159
+ store._needsMigrationFlush = false;
160
+ store.scheduleSave(100);
161
+ }
162
+ if (!store._readOnly && !store._loadFailed) {
163
+ bestEffort('prune-old-archives', () => pruneOldMonthlyArchives(dir, 6));
164
+ }
165
+ }
@@ -0,0 +1,38 @@
1
+ import path from 'node:path';
2
+
3
+ export const CURRENT_SCHEMA_VERSION = 2;
4
+
5
+ export function runStoreMigrations(data, fromVersion = 1, toVersion = CURRENT_SCHEMA_VERSION) {
6
+ if (!data || typeof data !== 'object') return data;
7
+ let current = { ...data };
8
+
9
+ // Migration from version 1 to 2 (Issue #390 & #393)
10
+ if (fromVersion < 2 && toVersion >= 2) {
11
+ if (Array.isArray(current.tasks)) {
12
+ current.tasks = current.tasks.map((task) => {
13
+ if (!task || typeof task !== 'object') return task;
14
+ return {
15
+ ...task,
16
+ maxMemoryMb: task.maxMemoryMb !== undefined ? Number(task.maxMemoryMb) : 0,
17
+ executionTimeoutMs: task.executionTimeoutMs !== undefined
18
+ ? Number(task.executionTimeoutMs)
19
+ : (Number(task.timeoutSeconds) > 0 ? Number(task.timeoutSeconds) * 1000 : 900000),
20
+ maxLogBytesPerRun: task.maxLogBytesPerRun !== undefined ? Number(task.maxLogBytesPerRun) : (5 * 1024 * 1024),
21
+ cpus: task.cpus !== undefined ? Number(task.cpus) : 0,
22
+ maxArchiveMonths: task.maxArchiveMonths !== undefined ? Number(task.maxArchiveMonths) : 6,
23
+ };
24
+ });
25
+ }
26
+ current.schemaVersion = 2;
27
+ }
28
+
29
+ return current;
30
+ }
31
+
32
+ export function getDefaultStorePath() {
33
+ const base = process.env.DSH_DATA_DIR
34
+ || (process.env.DSH_HOME
35
+ ? path.join(process.env.DSH_HOME, 'data')
36
+ : path.join(process.env.HOME || '.', '.dsh', 'data'));
37
+ return path.join(base, 'cron', 'tasks.json');
38
+ }
@@ -0,0 +1,126 @@
1
+ import { logger } from '../logger.js';
2
+ import { getDshDefaultTelegramCredentials } from '../telegram.js';
3
+
4
+ /** Mask a secret for transport to the browser, keeping a readable hint. */
5
+ export function maskSecretValue(value) {
6
+ const s = String(value == null ? '' : value);
7
+ if (!s) return '';
8
+ if (s.length <= 8) return '••••••••';
9
+ return s.slice(0, 4) + '••••••••' + s.slice(-3);
10
+ }
11
+
12
+ /**
13
+ * Raw secret values that must not be stored in plugin settings; the
14
+ * corresponding *Ref credential names are the supported way to configure
15
+ * these channels.
16
+ */
17
+ export const FORBIDDEN_SETTING_KEYS = ['giteaToken', 'ntfyToken', 'pushplusToken', 'discordToken', 'slackToken'];
18
+
19
+ /**
20
+ * Settings that hold a secret and are therefore masked on read: tokens and
21
+ * webhook URLs (the URL embeds the auth token), plus the Bark device key.
22
+ * These are stored in the plugin settings file; the credential-reference
23
+ * fields (botTokenRef, ntfyTokenRef, …) take precedence over them.
24
+ */
25
+ export const MASKED_SETTING_KEYS = ['botToken', 'discordWebhookUrl', 'slackWebhookUrl', 'barkKey', 'apiToken', 'telegramWebhookSecret'];
26
+
27
+ export function getSettings(store) {
28
+ const defaults = getDshDefaultTelegramCredentials();
29
+ const base = { ...store.settings };
30
+ base.botToken = base.botToken !== undefined ? base.botToken : (defaults.botToken || '');
31
+ base.botTokenRef = base.botTokenRef || '';
32
+ base.chatId = base.chatId !== undefined ? base.chatId : (defaults.chatId || '');
33
+ base.notifyTelegram = Boolean(base.notifyTelegram);
34
+ base.onlyOnFailure = Boolean(base.onlyOnFailure);
35
+ base.kanbanBaseUrl = base.kanbanBaseUrl || 'http://127.0.0.1:3000';
36
+ base.hasDefaultCredentials = Boolean(defaults.botToken && defaults.chatId);
37
+ base.llmActionsEnabled = Boolean(base.llmActionsEnabled);
38
+ base.showChatHeaderButton = base.showChatHeaderButton !== undefined ? Boolean(base.showChatHeaderButton) : true;
39
+ base.showChatHeaderActions = base.showChatHeaderActions !== undefined ? Boolean(base.showChatHeaderActions) : true;
40
+ return base;
41
+ }
42
+
43
+ export function getClientSettings(store) {
44
+ const raw = getSettings(store);
45
+ const isCustom = Boolean(store.settings.botToken);
46
+ const hasToken = Boolean(raw.botToken);
47
+ const masked = hasToken ? (raw.botToken.length > 8 ? raw.botToken.slice(0, 4) + '••••••••' + raw.botToken.slice(-3) : '••••••••') : '';
48
+ const { botToken, ...rest } = raw;
49
+ const out = {
50
+ ...rest,
51
+ botToken: masked,
52
+ botTokenMasked: masked,
53
+ hasBotToken: hasToken,
54
+ hasCustomBotToken: isCustom,
55
+ };
56
+ // Webhook URLs and the Bark device key carry an embedded secret, so they
57
+ // are never sent to the browser in clear text either.
58
+ for (const key of MASKED_SETTING_KEYS) {
59
+ if (key === 'botToken') continue;
60
+ out[key] = maskSecretValue(raw[key]);
61
+ }
62
+ return out;
63
+ }
64
+
65
+ export function saveSettings(store, newSettings = {}) {
66
+ const patch = { ...newSettings };
67
+
68
+ for (const key of FORBIDDEN_SETTING_KEYS) {
69
+ if (key in patch) {
70
+ logger.warn(`[dsh-cron] refusing to store raw secret "${key}" in settings — use a credential reference instead`);
71
+ delete patch[key];
72
+ }
73
+ }
74
+
75
+ let botTokenToSave = store.settings.botToken || '';
76
+ if (patch.botToken !== undefined) {
77
+ const val = String(patch.botToken).trim();
78
+ // If client sends masked token or empty string while custom token existed, don't overwrite with dots
79
+ if (val && !val.includes('••') && !val.includes('****')) {
80
+ botTokenToSave = val;
81
+ } else if (val === '') {
82
+ botTokenToSave = '';
83
+ }
84
+ delete patch.botToken;
85
+ }
86
+
87
+ // A masked value echoed back by the UI must never overwrite the real one.
88
+ for (const key of MASKED_SETTING_KEYS) {
89
+ if (key === 'botToken') continue;
90
+ const val = patch[key];
91
+ if (typeof val === 'string' && (val.includes('••') || val.includes('****'))) {
92
+ delete patch[key];
93
+ }
94
+ }
95
+
96
+ for (const [key, value] of Object.entries(patch)) {
97
+ if (value === undefined) delete patch[key];
98
+ else if (typeof value === 'string') patch[key] = value.trim();
99
+ }
100
+
101
+ const prevSettings = store.settings;
102
+ store.settings = {
103
+ ...store.settings,
104
+ ...patch,
105
+ botToken: botTokenToSave,
106
+ kanbanBaseUrl: patch.kanbanBaseUrl !== undefined
107
+ ? (patch.kanbanBaseUrl || 'http://127.0.0.1:3000')
108
+ : (store.settings.kanbanBaseUrl || 'http://127.0.0.1:3000'),
109
+ notifyTelegram: patch.notifyTelegram !== undefined ? Boolean(patch.notifyTelegram) : Boolean(store.settings.notifyTelegram),
110
+ onlyOnFailure: patch.onlyOnFailure !== undefined ? Boolean(patch.onlyOnFailure) : Boolean(store.settings.onlyOnFailure),
111
+ llmActionsEnabled: patch.llmActionsEnabled !== undefined ? Boolean(patch.llmActionsEnabled) : Boolean(store.settings.llmActionsEnabled),
112
+ showChatHeaderButton: patch.showChatHeaderButton !== undefined ? Boolean(patch.showChatHeaderButton) : (store.settings.showChatHeaderButton !== undefined ? Boolean(store.settings.showChatHeaderButton) : true),
113
+ showChatHeaderActions: patch.showChatHeaderActions !== undefined ? Boolean(patch.showChatHeaderActions) : (store.settings.showChatHeaderActions !== undefined ? Boolean(store.settings.showChatHeaderActions) : true),
114
+ };
115
+ if (store._readOnly || store._loadFailed) {
116
+ logger.warn(`[dsh-cron] store is in read-only recovery mode; updated in-memory settings but skipped writing to "${store.filePath}".`);
117
+ return getSettings(store);
118
+ }
119
+ try {
120
+ store.save();
121
+ } catch (err) {
122
+ store.settings = prevSettings;
123
+ throw err;
124
+ }
125
+ return getSettings(store);
126
+ }