@goodandready/dsh-cron 0.3.42 → 0.3.45

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
@@ -4,65 +4,58 @@ import { ArtifactManager } from './artifacts.js';
4
4
  import { logger } from '../logger.js';
5
5
  import { bestEffort } from '../best-effort.js';
6
6
  import fs from 'node:fs';
7
- import { gzipSync, gunzipSync } from 'node:zlib';
8
- import { pipeline } from 'node:stream/promises';
9
7
  import path from 'node:path';
10
- import { getDshDefaultTelegramCredentials } from '../telegram.js';
11
8
 
12
- /**
13
- * Where plugin data lives.
14
- *
15
- * DSH_DATA_DIR wins when the harness sets it. Otherwise the profile home
16
- * (DSH_HOME) is authoritative: falling back to ~/.dsh would make an isolated
17
- * profile — a test contour, a second profile — write its tasks into another
18
- * home's data directory, which is how an isolated test cycle once leaked a
19
- * task into the wrong profile.
20
- */
21
-
22
- export const CURRENT_SCHEMA_VERSION = 2;
23
-
24
- export function runStoreMigrations(data, fromVersion = 1, toVersion = CURRENT_SCHEMA_VERSION) {
25
- if (!data || typeof data !== 'object') return data;
26
- let current = { ...data };
27
-
28
- // Migration from version 1 to 2 (Issue #390 & #393)
29
- if (fromVersion < 2 && toVersion >= 2) {
30
- if (Array.isArray(current.tasks)) {
31
- current.tasks = current.tasks.map((task) => {
32
- if (!task || typeof task !== 'object') return task;
33
- return {
34
- ...task,
35
- maxMemoryMb: task.maxMemoryMb !== undefined ? Number(task.maxMemoryMb) : 0,
36
- executionTimeoutMs: task.executionTimeoutMs !== undefined
37
- ? Number(task.executionTimeoutMs)
38
- : (Number(task.timeoutSeconds) > 0 ? Number(task.timeoutSeconds) * 1000 : 900000),
39
- maxLogBytesPerRun: task.maxLogBytesPerRun !== undefined ? Number(task.maxLogBytesPerRun) : (5 * 1024 * 1024),
40
- cpus: task.cpus !== undefined ? Number(task.cpus) : 0,
41
- maxArchiveMonths: task.maxArchiveMonths !== undefined ? Number(task.maxArchiveMonths) : 6,
42
- };
43
- });
44
- }
45
- current.schemaVersion = 2;
46
- }
47
-
48
- return current;
49
- }
50
-
51
- export function getDefaultStorePath() {
52
- const base = process.env.DSH_DATA_DIR
53
- || (process.env.DSH_HOME
54
- ? path.join(process.env.DSH_HOME, 'data')
55
- : path.join(process.env.HOME || '.', '.dsh', 'data'));
56
- return path.join(base, 'cron', 'tasks.json');
57
- }
58
-
59
- /** Mask a secret for transport to the browser, keeping a readable hint. */
60
- function maskSecretValue(value) {
61
- const s = String(value == null ? '' : value);
62
- if (!s) return '';
63
- if (s.length <= 8) return '••••••••';
64
- return s.slice(0, 4) + '••••••••' + s.slice(-3);
65
- }
9
+ import {
10
+ CURRENT_SCHEMA_VERSION,
11
+ runStoreMigrations,
12
+ getDefaultStorePath,
13
+ } from './store-migration.js';
14
+
15
+ import { initStore } from './store-loader.js';
16
+
17
+ import {
18
+ isAccessOrIoError,
19
+ cleanOrphanedTmpFiles,
20
+ scheduleSave,
21
+ flushSync,
22
+ createSnapshot,
23
+ waitForDiskVersion,
24
+ notifyDiskVersion,
25
+ saveAsync,
26
+ writeStoreStream,
27
+ save,
28
+ } from './store-io.js';
29
+
30
+ import {
31
+ archiveHistoryRuns,
32
+ listMonthlyArchives,
33
+ getMonthlyArchive,
34
+ pruneOldMonthlyArchives,
35
+ getArchivedRuns,
36
+ } from './store-archive.js';
37
+
38
+ import {
39
+ maskSecretValue,
40
+ FORBIDDEN_SETTING_KEYS,
41
+ MASKED_SETTING_KEYS,
42
+ getSettings,
43
+ getClientSettings,
44
+ saveSettings,
45
+ } from './store-settings.js';
46
+
47
+ import {
48
+ getTaskStats,
49
+ getAggregatedStats,
50
+ } from './store-stats.js';
51
+
52
+ export {
53
+ CURRENT_SCHEMA_VERSION,
54
+ runStoreMigrations,
55
+ getDefaultStorePath,
56
+ isAccessOrIoError,
57
+ maskSecretValue,
58
+ };
66
59
 
67
60
  export class TaskStore {
68
61
  constructor(filePath) {
@@ -82,442 +75,89 @@ export class TaskStore {
82
75
  this._saveVersion = 0;
83
76
  this._diskVersion = 0;
84
77
  this._lastWrittenVersion = 0;
78
+ this._readOnly = false;
79
+ this._loadFailed = false;
80
+ this._accessErrorCode = null;
81
+ this._loadError = null;
85
82
  this.init();
86
83
  }
87
84
 
88
85
  init() {
89
- const dir = path.dirname(this.filePath);
90
- if (!fs.existsSync(dir)) {
91
- fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
86
+ initStore(this);
87
+ }
88
+
89
+ _enterReadOnlyMode(err, operation = 'read', filePath = this.filePath) {
90
+ this._readOnly = true;
91
+ this._loadFailed = true;
92
+ this._loadError = err;
93
+ if (isAccessOrIoError(err)) {
94
+ this._accessErrorCode = err.code;
95
+ logger.error(`[dsh-cron] store access error (operation: ${operation}, code: ${err.code}): cannot access "${filePath}". Check file permissions and ownership.`);
96
+ } else {
97
+ this._accessErrorCode = null;
98
+ logger.error(`[dsh-cron] store read error (operation: ${operation}): cannot load tasks from "${filePath}": ${err?.message || err}. Entering read-only recovery mode to protect existing store.`);
92
99
  }
93
- bestEffort('chmod-dir', () => fs.chmodSync(dir, 0o700));
94
- this._cleanOrphanedTmpFiles(dir);
95
- const bakPath = `${this.filePath}.bak`;
100
+ }
96
101
 
97
- const loadData = (raw) => {
98
- let data = JSON.parse(raw);
99
- const storedVersion = Number(data.schemaVersion) || 1;
100
- if (storedVersion < CURRENT_SCHEMA_VERSION) {
101
- // Pre-migration backup (#390)
102
- try {
103
- const preBakPath = `${this.filePath}.bak-v${storedVersion}-${Date.now()}`;
104
- fs.writeFileSync(preBakPath, raw, { encoding: 'utf-8', mode: 0o600 });
105
- bestEffort('chmod-pre-bak', () => fs.chmodSync(preBakPath, 0o600));
106
- logger.info(`[dsh-cron] pre-migration backup saved to ${preBakPath}`);
107
- } catch (bakErr) {
108
- logger.warn('[dsh-cron] failed to write pre-migration backup:', bakErr.message);
109
- }
110
- data = runStoreMigrations(data, storedVersion, CURRENT_SCHEMA_VERSION);
111
- this._needsMigrationFlush = true;
112
- }
113
- this.tasks.clear();
114
- this.history.clear();
115
- if (Array.isArray(data.tasks)) {
116
- const now = Date.now();
117
- const cutoff24h = now - 86400000;
118
- for (const t of data.tasks) {
119
- if (Array.isArray(t.costLedger) && t.costLedger.length > 0) {
120
- t.costLedger = t.costLedger.filter(e => e && typeof e.at === 'number' && e.at >= cutoff24h);
121
- } else {
122
- // Seed from active history if present (#221)
123
- const hist = (data.history && Array.isArray(data.history[t.id])) ? data.history[t.id] : [];
124
- t.costLedger = hist
125
- .filter(r => r && typeof r.at === 'number' && r.at >= cutoff24h)
126
- .map(r => ({
127
- at: r.at,
128
- costUsd: Number(r.costUsd) || 0,
129
- tokens: (r.usage?.inputTokens || 0) + (r.usage?.outputTokens || 0) + (r.usage?.cacheReadTokens || 0),
130
- }));
131
- }
132
- this.tasks.set(t.id, t);
133
- }
134
- }
135
- if (data.history && typeof data.history === 'object') {
136
- for (const [k, v] of Object.entries(data.history)) {
137
- this.history.set(k, Array.isArray(v) ? v : []);
138
- }
139
- }
140
- if (data.settings && typeof data.settings === 'object') {
141
- this.settings = data.settings;
142
- }
143
- if (data.chainCheckpoints && typeof data.chainCheckpoints === 'object') {
144
- this.chainCheckpoints = data.chainCheckpoints;
145
- }
146
- if (data.artifacts && typeof data.artifacts === 'object') {
147
- this.artifacts = data.artifacts;
148
- }
149
- };
102
+ isReadOnly() {
103
+ return Boolean(this._readOnly);
104
+ }
150
105
 
151
- if (fs.existsSync(this.filePath)) {
152
- try {
153
- bestEffort('chmod-store', () => fs.chmodSync(this.filePath, 0o600));
154
- const raw = fs.readFileSync(this.filePath, 'utf-8');
155
- loadData(raw);
156
- if (!fs.existsSync(bakPath)) {
157
- bestEffort('backup-initial', () => {
158
- fs.copyFileSync(this.filePath, bakPath);
159
- fs.chmodSync(bakPath, 0o600);
160
- });
161
- } else {
162
- bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
163
- }
164
- } catch (err) {
165
- logger.error('[dsh-cron] store read error (corrupted file):', err.message);
166
- try {
167
- const corruptedPath = `${this.filePath}.corrupted.${Date.now()}`;
168
- fs.copyFileSync(this.filePath, corruptedPath);
169
- bestEffort('chmod-corrupted', () => fs.chmodSync(corruptedPath, 0o600));
170
- logger.warn(`[dsh-cron] corrupted store backed up to ${corruptedPath}`);
171
- } catch (snapErr) {
172
- logger.error('[dsh-cron] failed to snapshot corrupted store:', snapErr.message);
173
- }
174
- if (fs.existsSync(bakPath)) {
175
- try {
176
- bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
177
- const rawBak = fs.readFileSync(bakPath, 'utf-8');
178
- loadData(rawBak);
179
- logger.warn(`[dsh-cron] successfully recovered tasks store from ${bakPath}`);
180
- } catch (bakErr) {
181
- logger.error('[dsh-cron] failed to recover tasks store from .bak:', bakErr.message);
182
- }
183
- }
184
- }
185
- } else if (fs.existsSync(bakPath)) {
186
- try {
187
- bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
188
- const rawBak = fs.readFileSync(bakPath, 'utf-8');
189
- loadData(rawBak);
190
- bestEffort('restore-from-bak', () => {
191
- fs.copyFileSync(bakPath, this.filePath);
192
- fs.chmodSync(this.filePath, 0o600);
193
- });
194
- logger.warn(`[dsh-cron] restored missing tasks store from ${bakPath}`);
195
- } catch (bakErr) {
196
- logger.error('[dsh-cron] failed to restore tasks store from .bak:', bakErr.message);
197
- }
198
- }
199
- if (this._needsMigrationFlush) {
200
- this._needsMigrationFlush = false;
201
- this.scheduleSave(100);
202
- }
203
- bestEffort('prune-old-archives', () => this._pruneOldMonthlyArchives(dir, 6));
106
+ hasLoadFailed() {
107
+ return Boolean(this._loadFailed);
108
+ }
109
+
110
+ isDegraded() {
111
+ return Boolean(this._readOnly || this._loadFailed);
112
+ }
113
+
114
+ getAccessErrorCode() {
115
+ return this._accessErrorCode;
116
+ }
117
+
118
+ getLoadError() {
119
+ return this._loadError;
120
+ }
121
+
122
+ reload() {
123
+ this._readOnly = false;
124
+ this._loadFailed = false;
125
+ this._accessErrorCode = null;
126
+ this._loadError = null;
127
+ this.init();
128
+ return !this._readOnly && !this._loadFailed;
204
129
  }
205
130
 
206
- /**
207
- * Schedule a debounced save using the asynchronous write queue to avoid
208
- * blocking the event loop on large task stores (#244).
209
- */
210
131
  scheduleSave(delay = 50) {
211
- if (this._saveTimer) return;
212
- this._saveTimer = setTimeout(() => {
213
- this._saveTimer = null;
214
- this.saveAsync().catch((err) => {
215
- logger.error('[dsh-cron] debounced save failed:', err.message);
216
- });
217
- }, delay);
218
- if (typeof this._saveTimer.unref === 'function') {
219
- this._saveTimer.unref();
220
- }
132
+ return scheduleSave(this, delay);
221
133
  }
222
134
 
223
- /**
224
- * Synchronously flush any pending debounced save to disk.
225
- */
226
135
  flushSync() {
227
- if (this._saveTimer) {
228
- clearTimeout(this._saveTimer);
229
- this._saveTimer = null;
230
- }
231
- this.save();
136
+ return flushSync(this);
232
137
  }
233
138
 
234
- /**
235
- * Asynchronously serialize and write tasks store to disk using fs.promises queue (#244).
236
- * Safe mode 0600 on files and 0700 on dir (#222).
237
- */
238
- /**
239
- * Create an isolated deep snapshot of tasks, history, and settings (#301).
240
- * Prevents concurrent in-memory mutations (e.g. recordRun during streamed save)
241
- * from corrupting serialized JSON state or causing duplicate / skipped run records.
242
- */
243
139
  _createSnapshot() {
244
- const tasksSnapshot = Array.from(this.tasks.values()).map((t) => ({
245
- ...t,
246
- costLedger: Array.isArray(t.costLedger) ? t.costLedger.slice() : t.costLedger,
247
- }));
248
- const historySnapshot = {};
249
- for (const [id, runs] of this.history.entries()) {
250
- historySnapshot[id] = Array.isArray(runs) ? runs.slice() : runs;
251
- }
252
- return {
253
- version: 1,
254
- schemaVersion: CURRENT_SCHEMA_VERSION,
255
- updatedAt: new Date().toISOString(),
256
- tasks: tasksSnapshot,
257
- history: historySnapshot,
258
- settings: this.settings ? { ...this.settings } : {},
259
- chainCheckpoints: this.chainCheckpoints ? { ...this.chainCheckpoints } : {},
260
- artifacts: this.artifacts ? { ...this.artifacts } : {},
261
- };
140
+ return createSnapshot(this);
262
141
  }
263
142
 
264
- /**
265
- * Asynchronously serialize and write tasks store to disk using fs.promises queue (#244, #301, #303).
266
- * Safe mode 0600 on files and 0700 on dir (#222).
267
- */
268
- /**
269
- * Wait until diskVersion or lastSyncVersion is at least the target version (#372).
270
- */
271
143
  _waitForDiskVersion(version) {
272
- if ((this._diskVersion || 0) >= version || (this._lastSyncVersion || 0) >= version) {
273
- return Promise.resolve();
274
- }
275
- return new Promise((resolve, reject) => {
276
- if (!this._diskWaiters) this._diskWaiters = [];
277
- this._diskWaiters.push({ version, resolve, reject });
278
- });
144
+ return waitForDiskVersion(this, version);
279
145
  }
280
146
 
281
147
  _notifyDiskVersion(writtenVersion, err = null) {
282
- if (!this._diskWaiters || this._diskWaiters.length === 0) return;
283
- const remaining = [];
284
- for (const waiter of this._diskWaiters) {
285
- if (err) {
286
- waiter.reject(err);
287
- } else if ((this._diskVersion || 0) >= waiter.version || (this._lastSyncVersion || 0) >= waiter.version) {
288
- waiter.resolve();
289
- } else {
290
- remaining.push(waiter);
291
- }
292
- }
293
- this._diskWaiters = remaining;
148
+ return notifyDiskVersion(this, writtenVersion, err);
294
149
  }
295
150
 
296
151
  async saveAsync() {
297
- if (this._saveTimer) {
298
- clearTimeout(this._saveTimer);
299
- this._saveTimer = null;
300
- }
301
- const version = ++this._saveVersion;
302
- const data = this._createSnapshot();
303
-
304
- const writeOp = async () => {
305
- if ((this._diskVersion || 0) >= version || (this._lastSyncVersion || 0) >= version) {
306
- this._notifyDiskVersion(version);
307
- return;
308
- }
309
- const dir = path.dirname(this.filePath);
310
- await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
311
- bestEffort('chmod-dir', () => fs.chmodSync(dir, 0o700));
312
-
313
- const rand = Math.random().toString(36).slice(2, 8);
314
- const tmp = `${this.filePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
315
- this._currentAsyncTmp = tmp;
316
-
317
- // Streamed chunked serialization with fine-grained event loop yields (#244).
318
- // Eliminates continuous CPU stalls on large stores (~21MB) by batching tasks and history runs
319
- // with setImmediate yields every 10 tasks and 15 runs, guaranteeing max event loop lag < 10ms.
320
- try {
321
- await this._writeStoreStream(tmp, data, version);
322
- bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
323
- } catch (writeErr) {
324
- await fs.promises.unlink(tmp).catch(() => {});
325
- if (this._currentAsyncTmp === tmp) this._currentAsyncTmp = null;
326
- this._notifyDiskVersion(null, writeErr);
327
- throw writeErr;
328
- }
329
-
330
- if ((this._diskVersion || 0) >= version || (this._lastSyncVersion || 0) >= version || this._saveVersion > version) {
331
- await fs.promises.unlink(tmp).catch(() => {});
332
- if (this._currentAsyncTmp === tmp) this._currentAsyncTmp = null;
333
- // Do not notify waiters yet: a newer queued saveOp will write to disk and notify (#372)
334
- return;
335
- }
336
-
337
- try {
338
- await fs.promises.rename(tmp, this.filePath);
339
- bestEffort('chmod-store', () => fs.chmodSync(this.filePath, 0o600));
340
- this._currentAsyncTmp = null;
341
- this._diskVersion = Math.max(this._diskVersion || 0, version);
342
- this._lastWrittenVersion = this._diskVersion;
343
- this._notifyDiskVersion(this._diskVersion);
344
- } catch (err) {
345
- this._currentAsyncTmp = null;
346
- // If tmp was unlinked by newer sync save, rename will fail with ENOENT - safely ignore only in that case (#244)
347
- if (err.code === 'ENOENT' && (this._lastSyncVersion || 0) >= version) {
348
- this._notifyDiskVersion(version);
349
- return;
350
- }
351
- this._notifyDiskVersion(null, err);
352
- throw err;
353
- }
354
-
355
- const now = Date.now();
356
- if (now - this._lastBackupTime > 60000) {
357
- this._lastBackupTime = now;
358
- try {
359
- const bakPath = `${this.filePath}.bak`;
360
- await fs.promises.copyFile(this.filePath, bakPath);
361
- bestEffort('chmod-bak', () => fs.chmodSync(bakPath, 0o600));
362
- } catch (bakErr) {
363
- logger.warn('[dsh-cron] async backup copy failed:', bakErr.message);
364
- }
365
- }
366
- };
367
-
368
- this._savePromise = this._savePromise.then(writeOp, writeOp);
369
- await this._savePromise;
370
- await this._waitForDiskVersion(version);
152
+ return saveAsync(this);
371
153
  }
372
154
 
373
- /**
374
- * Stream data incrementally to disk with fine-grained setImmediate yields (#244).
375
- * Prevents blocking the V8 single thread on heavy serialization (e.g. 21MB / 10k runs).
376
- */
377
- /**
378
- * Stream data incrementally to disk with fine-grained setImmediate yields (#244, #303).
379
- * Prevents blocking the V8 single thread on heavy serialization (e.g. 21MB / 10k runs).
380
- * Uses canonical Node stream/promises pipeline for error-handling and backpressure hygiene (#303).
381
- */
382
155
  async _writeStoreStream(filePath, data, version) {
383
- const isCanceled = () => {
384
- return (
385
- (this._diskVersion || 0) >= version ||
386
- (this._lastSyncVersion || 0) >= version ||
387
- this._saveVersion > version
388
- );
389
- };
390
-
391
- if (isCanceled()) {
392
- return;
393
- }
394
-
395
- const ac = new AbortController();
396
- let ws;
397
- try {
398
- ws = fs.createWriteStream(filePath, { encoding: 'utf-8', mode: 0o600 });
399
- } catch (createErr) {
400
- if (isCanceled()) return;
401
- throw createErr;
402
- }
403
-
404
- async function* generateChunks() {
405
- await new Promise((r) => setImmediate(r));
406
- if (isCanceled()) {
407
- ac.abort();
408
- return;
409
- }
410
-
411
- yield '{"version":' + JSON.stringify(data.version || 1);
412
- yield ',"schemaVersion":' + JSON.stringify(data.schemaVersion || CURRENT_SCHEMA_VERSION);
413
- yield ',"updatedAt":' + JSON.stringify(data.updatedAt || new Date().toISOString());
414
- yield ',"settings":' + JSON.stringify(data.settings || {});
415
-
416
- yield ',"tasks":[';
417
- const tasks = data.tasks || [];
418
- for (let i = 0; i < tasks.length; i++) {
419
- if (isCanceled()) {
420
- ac.abort();
421
- return;
422
- }
423
- if (i > 0) yield ',';
424
- yield JSON.stringify(tasks[i]);
425
- if ((i + 1) % 10 === 0) {
426
- await new Promise((r) => setImmediate(r));
427
- }
428
- }
429
- yield ']';
430
-
431
- yield ',"history":{';
432
- const historyEntries = Object.entries(data.history || {});
433
- for (let i = 0; i < historyEntries.length; i++) {
434
- if (isCanceled()) {
435
- ac.abort();
436
- return;
437
- }
438
- if (i > 0) yield ',';
439
- const [taskId, runs] = historyEntries[i];
440
- yield JSON.stringify(taskId) + ':[';
441
- if (Array.isArray(runs)) {
442
- for (let j = 0; j < runs.length; j++) {
443
- if (isCanceled()) {
444
- ac.abort();
445
- return;
446
- }
447
- if (j > 0) yield ',';
448
- yield JSON.stringify(runs[j]);
449
- if ((j + 1) % 15 === 0) {
450
- await new Promise((r) => setImmediate(r));
451
- }
452
- }
453
- } else {
454
- yield JSON.stringify(runs);
455
- }
456
- yield ']';
457
- await new Promise((r) => setImmediate(r));
458
- }
459
- yield '}';
460
- yield ',"chainCheckpoints":' + JSON.stringify(data.chainCheckpoints || {});
461
- yield ',"artifacts":' + JSON.stringify(data.artifacts || {});
462
- yield '}';
463
- }
464
-
465
- try {
466
- await pipeline(generateChunks(), ws, { signal: ac.signal });
467
- } catch (err) {
468
- if (ac.signal.aborted || isCanceled()) {
469
- return;
470
- }
471
- throw err;
472
- }
156
+ return writeStoreStream(this, filePath, data, version);
473
157
  }
474
158
 
475
159
  save() {
476
- if (this._saveTimer) {
477
- clearTimeout(this._saveTimer);
478
- this._saveTimer = null;
479
- }
480
- const version = ++this._saveVersion;
481
- this._lastSyncVersion = version;
482
-
483
- // Immediately cancel and unlink any in-flight async tmp file so async rename cannot touch disk (#244)
484
- if (this._currentAsyncTmp) {
485
- const inFlightTmp = this._currentAsyncTmp;
486
- this._currentAsyncTmp = null;
487
- bestEffort('unlink-async-tmp', () => fs.unlinkSync(inFlightTmp));
488
- }
489
- try {
490
- const dir = path.dirname(this.filePath);
491
- if (!fs.existsSync(dir)) {
492
- fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
493
- }
494
- bestEffort('chmod-dir', () => fs.chmodSync(dir, 0o700));
495
- const data = this._createSnapshot();
496
- // Unique tmp file per save call with process PID and random token to eliminate file collision
497
- const rand = Math.random().toString(36).slice(2, 8);
498
- const tmp = `${this.filePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
499
- fs.writeFileSync(tmp, JSON.stringify(data, null, 2), { encoding: 'utf-8', mode: 0o600 });
500
- bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
501
- fs.renameSync(tmp, this.filePath);
502
- bestEffort('chmod-store', () => fs.chmodSync(this.filePath, 0o600));
503
- this._diskVersion = Math.max(this._diskVersion || 0, version);
504
- this._lastWrittenVersion = this._diskVersion;
505
- this._notifyDiskVersion(this._diskVersion);
506
-
507
- // Throttled backup (.bak): avoid expensive copyFileSync on every micro-save
508
- const now = Date.now();
509
- if (now - this._lastBackupTime > 60000) {
510
- this._lastBackupTime = now;
511
- bestEffort('backup-on-save', () => {
512
- const bakPath = `${this.filePath}.bak`;
513
- fs.copyFileSync(this.filePath, bakPath);
514
- fs.chmodSync(bakPath, 0o600);
515
- });
516
- }
517
- } catch (err) {
518
- logger.error('[dsh-cron] store save error:', err.message);
519
- throw err;
520
- }
160
+ return save(this);
521
161
  }
522
162
 
523
163
  getRun(runId) {
@@ -547,119 +187,23 @@ export class TaskStore {
547
187
  }
548
188
 
549
189
  getSettings() {
550
- const defaults = getDshDefaultTelegramCredentials();
551
- const base = { ...this.settings };
552
- base.botToken = base.botToken !== undefined ? base.botToken : (defaults.botToken || '');
553
- base.botTokenRef = base.botTokenRef || '';
554
- base.chatId = base.chatId !== undefined ? base.chatId : (defaults.chatId || '');
555
- base.notifyTelegram = Boolean(base.notifyTelegram);
556
- base.onlyOnFailure = Boolean(base.onlyOnFailure);
557
- base.kanbanBaseUrl = base.kanbanBaseUrl || 'http://127.0.0.1:3000';
558
- base.hasDefaultCredentials = Boolean(defaults.botToken && defaults.chatId);
559
- base.llmActionsEnabled = Boolean(base.llmActionsEnabled);
560
- base.showChatHeaderButton = base.showChatHeaderButton !== undefined ? Boolean(base.showChatHeaderButton) : true;
561
- base.showChatHeaderActions = base.showChatHeaderActions !== undefined ? Boolean(base.showChatHeaderActions) : true;
562
- return base;
190
+ return getSettings(this);
563
191
  }
564
192
 
565
193
  getClientSettings() {
566
- const raw = this.getSettings();
567
- const isCustom = Boolean(this.settings.botToken);
568
- const hasToken = Boolean(raw.botToken);
569
- const masked = hasToken ? (raw.botToken.length > 8 ? raw.botToken.slice(0, 4) + '••••••••' + raw.botToken.slice(-3) : '••••••••') : '';
570
- const { botToken, ...rest } = raw;
571
- const out = {
572
- ...rest,
573
- botToken: masked,
574
- botTokenMasked: masked,
575
- hasBotToken: hasToken,
576
- hasCustomBotToken: isCustom,
577
- };
578
- // Webhook URLs and the Bark device key carry an embedded secret, so they
579
- // are never sent to the browser in clear text either.
580
- for (const key of TaskStore.MASKED_SETTING_KEYS) {
581
- if (key === 'botToken') continue;
582
- out[key] = maskSecretValue(raw[key]);
583
- }
584
- return out;
194
+ return getClientSettings(this);
585
195
  }
586
196
 
587
- /**
588
- * Raw secret values that must not be stored in plugin settings; the
589
- * corresponding *Ref credential names are the supported way to configure
590
- * these channels.
591
- */
592
197
  static get FORBIDDEN_SETTING_KEYS() {
593
- return ['giteaToken', 'ntfyToken', 'pushplusToken', 'discordToken', 'slackToken'];
198
+ return FORBIDDEN_SETTING_KEYS;
594
199
  }
595
200
 
596
- /**
597
- * Settings that hold a secret and are therefore masked on read: tokens and
598
- * webhook URLs (the URL embeds the auth token), plus the Bark device key.
599
- * These are stored in the plugin settings file; the credential-reference
600
- * fields (botTokenRef, ntfyTokenRef, …) take precedence over them.
601
- */
602
201
  static get MASKED_SETTING_KEYS() {
603
- return ['botToken', 'discordWebhookUrl', 'slackWebhookUrl', 'barkKey', 'apiToken', 'telegramWebhookSecret'];
202
+ return MASKED_SETTING_KEYS;
604
203
  }
605
204
 
606
205
  saveSettings(newSettings = {}) {
607
- const patch = { ...newSettings };
608
-
609
- for (const key of TaskStore.FORBIDDEN_SETTING_KEYS) {
610
- if (key in patch) {
611
- logger.warn(`[dsh-cron] refusing to store raw secret "${key}" in settings — use a credential reference instead`);
612
- delete patch[key];
613
- }
614
- }
615
-
616
- let botTokenToSave = this.settings.botToken || '';
617
- if (patch.botToken !== undefined) {
618
- const val = String(patch.botToken).trim();
619
- // If client sends masked token or empty string while custom token existed, don't overwrite with dots
620
- if (val && !val.includes('••') && !val.includes('****')) {
621
- botTokenToSave = val;
622
- } else if (val === '') {
623
- botTokenToSave = '';
624
- }
625
- delete patch.botToken;
626
- }
627
-
628
- // A masked value echoed back by the UI must never overwrite the real one.
629
- for (const key of TaskStore.MASKED_SETTING_KEYS) {
630
- if (key === 'botToken') continue;
631
- const val = patch[key];
632
- if (typeof val === 'string' && (val.includes('••') || val.includes('****'))) {
633
- delete patch[key];
634
- }
635
- }
636
-
637
- for (const [key, value] of Object.entries(patch)) {
638
- if (value === undefined) delete patch[key];
639
- else if (typeof value === 'string') patch[key] = value.trim();
640
- }
641
-
642
- const prevSettings = this.settings;
643
- this.settings = {
644
- ...this.settings,
645
- ...patch,
646
- botToken: botTokenToSave,
647
- kanbanBaseUrl: patch.kanbanBaseUrl !== undefined
648
- ? (patch.kanbanBaseUrl || 'http://127.0.0.1:3000')
649
- : (this.settings.kanbanBaseUrl || 'http://127.0.0.1:3000'),
650
- notifyTelegram: patch.notifyTelegram !== undefined ? Boolean(patch.notifyTelegram) : Boolean(this.settings.notifyTelegram),
651
- onlyOnFailure: patch.onlyOnFailure !== undefined ? Boolean(patch.onlyOnFailure) : Boolean(this.settings.onlyOnFailure),
652
- llmActionsEnabled: patch.llmActionsEnabled !== undefined ? Boolean(patch.llmActionsEnabled) : Boolean(this.settings.llmActionsEnabled),
653
- showChatHeaderButton: patch.showChatHeaderButton !== undefined ? Boolean(patch.showChatHeaderButton) : (this.settings.showChatHeaderButton !== undefined ? Boolean(this.settings.showChatHeaderButton) : true),
654
- showChatHeaderActions: patch.showChatHeaderActions !== undefined ? Boolean(patch.showChatHeaderActions) : (this.settings.showChatHeaderActions !== undefined ? Boolean(this.settings.showChatHeaderActions) : true),
655
- };
656
- try {
657
- this.save();
658
- } catch (err) {
659
- this.settings = prevSettings;
660
- throw err;
661
- }
662
- return this.getSettings();
206
+ return saveSettings(this, newSettings);
663
207
  }
664
208
 
665
209
  list(filter = {}) {
@@ -683,6 +227,9 @@ export class TaskStore {
683
227
  }
684
228
 
685
229
  set(task) {
230
+ if (this._readOnly || this._loadFailed) {
231
+ throw new Error(`Store write blocked: read-only recovery mode (${this.filePath})`);
232
+ }
686
233
  const now = Date.now();
687
234
  const id = task.id || ('cron_' + Math.random().toString(36).slice(2, 9));
688
235
  const existing = this.tasks.get(id);
@@ -766,6 +313,9 @@ export class TaskStore {
766
313
  recordHeartbeat(id) {
767
314
  const task = this.tasks.get(id);
768
315
  if (!task) return null;
316
+ if (this._readOnly || this._loadFailed) {
317
+ return null;
318
+ }
769
319
  const now = Date.now();
770
320
  const prevPing = task.lastPingAt;
771
321
  const prevAlerted = task.heartbeatAlerted;
@@ -797,6 +347,9 @@ export class TaskStore {
797
347
  }
798
348
 
799
349
  delete(id) {
350
+ if (this._readOnly || this._loadFailed) {
351
+ throw new Error(`Store write blocked: read-only recovery mode (${this.filePath})`);
352
+ }
800
353
  const existing = this.tasks.get(id);
801
354
  if (!existing) return false;
802
355
  const existingHistory = this.history.get(id);
@@ -909,196 +462,27 @@ export class TaskStore {
909
462
  * Remove orphaned temporary write files older than 10 minutes (#379, GH #17).
910
463
  */
911
464
  _cleanOrphanedTmpFiles(dir) {
912
- try {
913
- if (!fs.existsSync(dir)) return;
914
- const entries = fs.readdirSync(dir);
915
- const now = Date.now();
916
- const tenMinutesMs = 10 * 60 * 1000;
917
- for (const entry of entries) {
918
- if (entry.includes('.tmp.')) {
919
- const fullPath = path.join(dir, entry);
920
- bestEffort('clean-tmp-file', () => {
921
- const stat = fs.statSync(fullPath);
922
- if (stat.isFile() && (now - stat.mtimeMs > tenMinutesMs)) {
923
- fs.unlinkSync(fullPath);
924
- }
925
- });
926
- }
927
- }
928
- } catch (dirErr) {
929
- bestEffort('log-clean-tmp-dir-error', () => logger.debug?.(`[dsh-cron] clean orphaned tmp dir error: ${dirErr?.message}`));
930
- }
465
+ return cleanOrphanedTmpFiles(dir);
931
466
  }
932
467
 
933
- /**
934
- * Append rotated overflow history entries into tasks-history-archive.json (#134, #379, GH #17).
935
- */
936
468
  _archiveHistoryRuns(id, overflowRuns) {
937
- if (!Array.isArray(overflowRuns) || overflowRuns.length === 0) return;
938
- try {
939
- const dir = path.dirname(this.filePath);
940
- const archivePath = path.join(dir, 'tasks-history-archive.json');
941
- let archive = {};
942
- if (fs.existsSync(archivePath)) {
943
- archive = bestEffort('parse-history-archive', () => JSON.parse(fs.readFileSync(archivePath, 'utf-8'))) || {};
944
- }
945
- if (!Array.isArray(archive[id])) archive[id] = [];
946
-
947
- // Strip full raw diff payload from archived overflow runs to prevent runaway JSON growth (#379, GH #17)
948
- const sanitizedOverflow = overflowRuns.map((r) => {
949
- if (!r || !r.gitDiff) return r;
950
- const { diff, ...gitDiffMeta } = r.gitDiff;
951
- return {
952
- ...r,
953
- gitDiff: {
954
- ...gitDiffMeta,
955
- diffExcerpt: diff ? diff.slice(0, 500) : '',
956
- hasChanges: Boolean(gitDiffMeta.hasChanges),
957
- archived: true,
958
- },
959
- };
960
- });
961
-
962
- archive[id].push(...sanitizedOverflow);
963
- if (archive[id].length > 1000) {
964
- archive[id] = archive[id].slice(-1000);
965
- }
966
-
967
- // Enforce total archive size budget (max 15MB) across tasks (#379, GH #17)
968
- const MAX_ARCHIVE_BYTES = 15 * 1024 * 1024;
969
- let serialized = JSON.stringify(archive);
970
- if (serialized.length > MAX_ARCHIVE_BYTES) {
971
- for (const taskId of Object.keys(archive)) {
972
- if (Array.isArray(archive[taskId]) && archive[taskId].length > 100) {
973
- archive[taskId] = archive[taskId].slice(-100);
974
- }
975
- }
976
- serialized = JSON.stringify(archive);
977
- }
978
-
979
- // Write atomically with tmp file using compact JSON (avoiding mega-lines and null, 2 serialization bloat)
980
- const rand = Math.random().toString(36).slice(2, 8);
981
- const tmp = `${archivePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
982
- fs.writeFileSync(tmp, serialized, { encoding: 'utf-8', mode: 0o600 });
983
- bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
984
- fs.renameSync(tmp, archivePath);
985
- bestEffort('chmod-archive', () => fs.chmodSync(archivePath, 0o600));
986
-
987
- // Partition runs into monthly buckets for compressed archives (#391)
988
- const monthlyBuckets = new Map();
989
- for (const run of sanitizedOverflow) {
990
- const at = run.at || run.timestamp || run.startedAt || Date.now();
991
- const date = new Date(at);
992
- const monthKey = !isNaN(date.getTime())
993
- ? `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, '0')}`
994
- : new Date().toISOString().slice(0, 7);
995
- if (!monthlyBuckets.has(monthKey)) {
996
- monthlyBuckets.set(monthKey, []);
997
- }
998
- monthlyBuckets.get(monthKey).push(run);
999
- }
1000
-
1001
- for (const [monthKey, runsForMonth] of monthlyBuckets.entries()) {
1002
- const gzPath = path.join(dir, `tasks-history-${monthKey}.json.gz`);
1003
- let monthArchive = {};
1004
- if (fs.existsSync(gzPath)) {
1005
- try {
1006
- const rawGz = fs.readFileSync(gzPath);
1007
- const decompressed = gunzipSync(rawGz).toString('utf-8');
1008
- monthArchive = JSON.parse(decompressed) || {};
1009
- } catch (gzReadErr) {
1010
- logger.warn(`[dsh-cron] failed to read existing gzip archive for ${monthKey}:`, gzReadErr.message);
1011
- monthArchive = {};
1012
- }
1013
- }
1014
- if (!Array.isArray(monthArchive[id])) {
1015
- monthArchive[id] = [];
1016
- }
1017
- monthArchive[id].push(...runsForMonth);
1018
- const compressed = gzipSync(Buffer.from(JSON.stringify(monthArchive), 'utf-8'), { level: 9 });
1019
- const randGz = Math.random().toString(36).slice(2, 8);
1020
- const tmpGz = `${gzPath}.tmp.${process.pid}_${Date.now()}_${randGz}`;
1021
- fs.writeFileSync(tmpGz, compressed, { mode: 0o600 });
1022
- bestEffort('chmod-tmp-gz', () => fs.chmodSync(tmpGz, 0o600));
1023
- fs.renameSync(tmpGz, gzPath);
1024
- bestEffort('chmod-archive-gz', () => fs.chmodSync(gzPath, 0o600));
1025
- }
1026
-
1027
- this._pruneOldMonthlyArchives(dir, 6);
1028
- } catch (archiveErr) {
1029
- logger.error('[dsh-cron] history archive error:', archiveErr.message);
1030
- }
469
+ return archiveHistoryRuns(this, id, overflowRuns);
1031
470
  }
1032
471
 
1033
472
  listMonthlyArchives() {
1034
- try {
1035
- const dir = path.dirname(this.filePath);
1036
- if (!fs.existsSync(dir)) return [];
1037
- const files = fs.readdirSync(dir);
1038
- const archives = [];
1039
- for (const file of files) {
1040
- const match = file.match(/^tasks-history-(\d{4}-\d{2})\.json\.gz$/);
1041
- if (match) {
1042
- const month = match[1];
1043
- const fullPath = path.join(dir, file);
1044
- try {
1045
- const stat = fs.statSync(fullPath);
1046
- archives.push({
1047
- month,
1048
- filename: file,
1049
- sizeBytes: stat.size,
1050
- mtime: stat.mtime.toISOString(),
1051
- path: fullPath,
1052
- });
1053
- } catch (_err) {
1054
- // ignore unreadable stat
1055
- }
1056
- }
1057
- }
1058
- return archives.sort((a, b) => b.month.localeCompare(a.month));
1059
- } catch (err) {
1060
- logger.error('[dsh-cron] listMonthlyArchives error:', err.message);
1061
- return [];
1062
- }
473
+ return listMonthlyArchives(this);
1063
474
  }
1064
475
 
1065
476
  getMonthlyArchive(month) {
1066
- if (!month || !/^\d{4}-\d{2}$/.test(month)) return null;
1067
- try {
1068
- const dir = path.dirname(this.filePath);
1069
- const gzPath = path.join(dir, `tasks-history-${month}.json.gz`);
1070
- if (!fs.existsSync(gzPath)) return null;
1071
- const rawGz = fs.readFileSync(gzPath);
1072
- const decompressed = gunzipSync(rawGz).toString('utf-8');
1073
- return JSON.parse(decompressed);
1074
- } catch (err) {
1075
- logger.error(`[dsh-cron] getMonthlyArchive error for ${month}:`, err.message);
1076
- return null;
1077
- }
477
+ return getMonthlyArchive(this, month);
1078
478
  }
1079
479
 
1080
480
  _pruneOldMonthlyArchives(dir, maxMonths = 6) {
1081
- try {
1082
- if (!fs.existsSync(dir) || maxMonths < 0) return;
1083
- const files = fs.readdirSync(dir);
1084
- const monthlyFiles = [];
1085
- for (const file of files) {
1086
- const match = file.match(/^tasks-history-(\d{4}-\d{2})\.json\.gz$/);
1087
- if (match) {
1088
- monthlyFiles.push({ month: match[1], file, fullPath: path.join(dir, file) });
1089
- }
1090
- }
1091
- monthlyFiles.sort((a, b) => b.month.localeCompare(a.month));
1092
- if (monthlyFiles.length > maxMonths) {
1093
- const toDelete = monthlyFiles.slice(maxMonths);
1094
- for (const item of toDelete) {
1095
- bestEffort('delete-old-archive', () => fs.unlinkSync(item.fullPath));
1096
- logger.info(`[dsh-cron] pruned old monthly history archive: ${item.file}`);
1097
- }
1098
- }
1099
- } catch (err) {
1100
- bestEffort('log-prune-err', () => logger.debug?.(`[dsh-cron] prune old monthly archives error: ${err.message}`));
1101
- }
481
+ return pruneOldMonthlyArchives(dir, maxMonths);
482
+ }
483
+
484
+ getArchivedRuns(id, options = {}) {
485
+ return getArchivedRuns(this, id, options);
1102
486
  }
1103
487
 
1104
488
  getHistory(id, limit = 20) {
@@ -1106,78 +490,11 @@ export class TaskStore {
1106
490
  return runs.slice(0, limit);
1107
491
  }
1108
492
 
1109
- getArchivedRuns(id, { limit = 50, offset = 0, search = '' } = {}) {
1110
- try {
1111
- const dir = path.dirname(this.filePath);
1112
- const archivePath = path.join(dir, 'tasks-history-archive.json');
1113
- if (!fs.existsSync(archivePath)) return { runs: [], total: 0 };
1114
- const archive = JSON.parse(fs.readFileSync(archivePath, 'utf-8')) || {};
1115
- let runs = Array.isArray(archive[id]) ? [...archive[id]].sort((a, b) => ((b.at || b.timestamp || b.startedAt || 0) - (a.at || a.timestamp || a.startedAt || 0))) : [];
1116
- if (search && search.trim()) {
1117
- const q = search.toLowerCase();
1118
- runs = runs.filter(r => (r.output && r.output.toLowerCase().includes(q)) || (r.error && r.error.toLowerCase().includes(q)));
1119
- }
1120
- const total = runs.length;
1121
- const paged = runs.slice(offset, offset + limit);
1122
- return { runs: paged, total };
1123
- } catch (err) {
1124
- return { runs: [], total: 0, error: err.message };
1125
- }
1126
- }
1127
-
1128
493
  getTaskStats(id) {
1129
- const runs = this.history.get(id) || [];
1130
- const total = runs.length;
1131
- let success = 0;
1132
- let error = 0;
1133
- let timeout = 0;
1134
- const recentDurations = [];
1135
-
1136
- for (let i = 0; i < runs.length; i++) {
1137
- const r = runs[i];
1138
- if (r.status === 'success') success++;
1139
- else if (r.status === 'error') error++;
1140
- else if (r.status === 'timeout') timeout++;
1141
-
1142
- if (recentDurations.length < 10 && r.durationMs !== undefined) {
1143
- recentDurations.push({
1144
- at: r.at,
1145
- durationMs: r.durationMs,
1146
- status: r.status,
1147
- });
1148
- }
1149
- }
1150
-
1151
- const successRate = total > 0 ? Number((success / total).toFixed(2)) : 1.0;
1152
- return {
1153
- totalRuns: total,
1154
- successRuns: success,
1155
- errorRuns: error,
1156
- timeoutRuns: timeout,
1157
- successRate,
1158
- recentDurations,
1159
- };
494
+ return getTaskStats(this, id);
1160
495
  }
1161
496
 
1162
497
  getAggregatedStats() {
1163
- let totalRuns = 0;
1164
- let totalTokens = 0;
1165
- let totalCostUsd = 0;
1166
- let activeTasks = 0;
1167
-
1168
- for (const t of this.tasks.values()) {
1169
- if (t.status === 'active') activeTasks++;
1170
- totalTokens += (t.totalTokens || 0);
1171
- totalCostUsd += (t.totalCostUsd || 0);
1172
- const h = this.history.get(t.id) || [];
1173
- totalRuns += h.length;
1174
- }
1175
-
1176
- return {
1177
- activeTasks,
1178
- totalRuns,
1179
- totalTokens,
1180
- totalCostUsd: Number(totalCostUsd.toFixed(4)),
1181
- };
498
+ return getAggregatedStats(this);
1182
499
  }
1183
500
  }