@goodandready/dsh-cron 0.3.43 → 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 +13 -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 +75 -888
  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,195 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { gzipSync, gunzipSync } from 'node:zlib';
4
+ import { logger } from '../logger.js';
5
+ import { bestEffort } from '../best-effort.js';
6
+
7
+ /**
8
+ * Append rotated overflow history entries into tasks-history-archive.json (#134, #379, GH #17).
9
+ */
10
+ export function archiveHistoryRuns(store, id, overflowRuns) {
11
+ if (!Array.isArray(overflowRuns) || overflowRuns.length === 0) return;
12
+ try {
13
+ const dir = path.dirname(store.filePath);
14
+ const archivePath = path.join(dir, 'tasks-history-archive.json');
15
+ let archive = {};
16
+ if (fs.existsSync(archivePath)) {
17
+ archive = bestEffort('parse-history-archive', () => JSON.parse(fs.readFileSync(archivePath, 'utf-8'))) || {};
18
+ }
19
+ if (!Array.isArray(archive[id])) archive[id] = [];
20
+
21
+ // Strip full raw diff payload from archived overflow runs to prevent runaway JSON growth (#379, GH #17)
22
+ const sanitizedOverflow = overflowRuns.map((r) => {
23
+ if (!r || !r.gitDiff) return r;
24
+ const { diff, ...gitDiffMeta } = r.gitDiff;
25
+ return {
26
+ ...r,
27
+ gitDiff: {
28
+ ...gitDiffMeta,
29
+ diffExcerpt: diff ? diff.slice(0, 500) : '',
30
+ hasChanges: Boolean(gitDiffMeta.hasChanges),
31
+ archived: true,
32
+ },
33
+ };
34
+ });
35
+
36
+ archive[id].push(...sanitizedOverflow);
37
+ if (archive[id].length > 1000) {
38
+ archive[id] = archive[id].slice(-1000);
39
+ }
40
+
41
+ // Enforce total archive size budget (max 15MB) across tasks (#379, GH #17)
42
+ const MAX_ARCHIVE_BYTES = 15 * 1024 * 1024;
43
+ let serialized = JSON.stringify(archive);
44
+ if (serialized.length > MAX_ARCHIVE_BYTES) {
45
+ for (const taskId of Object.keys(archive)) {
46
+ if (Array.isArray(archive[taskId]) && archive[taskId].length > 100) {
47
+ archive[taskId] = archive[taskId].slice(-100);
48
+ }
49
+ }
50
+ serialized = JSON.stringify(archive);
51
+ }
52
+
53
+ // Write atomically with tmp file using compact JSON (avoiding mega-lines and null, 2 serialization bloat)
54
+ const rand = Math.random().toString(36).slice(2, 8);
55
+ const tmp = `${archivePath}.tmp.${process.pid}_${Date.now()}_${rand}`;
56
+ fs.writeFileSync(tmp, serialized, { encoding: 'utf-8', mode: 0o600 });
57
+ bestEffort('chmod-tmp', () => fs.chmodSync(tmp, 0o600));
58
+ fs.renameSync(tmp, archivePath);
59
+ bestEffort('chmod-archive', () => fs.chmodSync(archivePath, 0o600));
60
+
61
+ // Partition runs into monthly buckets for compressed archives (#391)
62
+ const monthlyBuckets = new Map();
63
+ for (const run of sanitizedOverflow) {
64
+ const at = run.at || run.timestamp || run.startedAt || Date.now();
65
+ const date = new Date(at);
66
+ const monthKey = !isNaN(date.getTime())
67
+ ? `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, '0')}`
68
+ : new Date().toISOString().slice(0, 7);
69
+ if (!monthlyBuckets.has(monthKey)) {
70
+ monthlyBuckets.set(monthKey, []);
71
+ }
72
+ monthlyBuckets.get(monthKey).push(run);
73
+ }
74
+
75
+ for (const [monthKey, runsForMonth] of monthlyBuckets.entries()) {
76
+ const gzPath = path.join(dir, `tasks-history-${monthKey}.json.gz`);
77
+ let monthArchive = {};
78
+ if (fs.existsSync(gzPath)) {
79
+ try {
80
+ const rawGz = fs.readFileSync(gzPath);
81
+ const decompressed = gunzipSync(rawGz).toString('utf-8');
82
+ monthArchive = JSON.parse(decompressed) || {};
83
+ } catch (gzReadErr) {
84
+ logger.warn(`[dsh-cron] failed to read existing gzip archive for ${monthKey}:`, gzReadErr.message);
85
+ monthArchive = {};
86
+ }
87
+ }
88
+ if (!Array.isArray(monthArchive[id])) {
89
+ monthArchive[id] = [];
90
+ }
91
+ monthArchive[id].push(...runsForMonth);
92
+ const compressed = gzipSync(Buffer.from(JSON.stringify(monthArchive), 'utf-8'), { level: 9 });
93
+ const randGz = Math.random().toString(36).slice(2, 8);
94
+ const tmpGz = `${gzPath}.tmp.${process.pid}_${Date.now()}_${randGz}`;
95
+ fs.writeFileSync(tmpGz, compressed, { mode: 0o600 });
96
+ bestEffort('chmod-tmp-gz', () => fs.chmodSync(tmpGz, 0o600));
97
+ fs.renameSync(tmpGz, gzPath);
98
+ bestEffort('chmod-archive-gz', () => fs.chmodSync(gzPath, 0o600));
99
+ }
100
+
101
+ pruneOldMonthlyArchives(dir, 6);
102
+ } catch (archiveErr) {
103
+ logger.error('[dsh-cron] history archive error:', archiveErr.message);
104
+ }
105
+ }
106
+
107
+ export function listMonthlyArchives(store) {
108
+ try {
109
+ const dir = path.dirname(store.filePath);
110
+ if (!fs.existsSync(dir)) return [];
111
+ const files = fs.readdirSync(dir);
112
+ const archives = [];
113
+ for (const file of files) {
114
+ const match = file.match(/^tasks-history-(\d{4}-\d{2})\.json\.gz$/);
115
+ if (match) {
116
+ const month = match[1];
117
+ const fullPath = path.join(dir, file);
118
+ try {
119
+ const stat = fs.statSync(fullPath);
120
+ archives.push({
121
+ month,
122
+ filename: file,
123
+ sizeBytes: stat.size,
124
+ mtime: stat.mtime.toISOString(),
125
+ path: fullPath,
126
+ });
127
+ } catch (_err) {
128
+ // ignore unreadable stat
129
+ }
130
+ }
131
+ }
132
+ return archives.sort((a, b) => b.month.localeCompare(a.month));
133
+ } catch (err) {
134
+ logger.error('[dsh-cron] listMonthlyArchives error:', err.message);
135
+ return [];
136
+ }
137
+ }
138
+
139
+ export function getMonthlyArchive(store, month) {
140
+ if (!month || !/^\d{4}-\d{2}$/.test(month)) return null;
141
+ try {
142
+ const dir = path.dirname(store.filePath);
143
+ const gzPath = path.join(dir, `tasks-history-${month}.json.gz`);
144
+ if (!fs.existsSync(gzPath)) return null;
145
+ const rawGz = fs.readFileSync(gzPath);
146
+ const decompressed = gunzipSync(rawGz).toString('utf-8');
147
+ return JSON.parse(decompressed);
148
+ } catch (err) {
149
+ logger.error(`[dsh-cron] getMonthlyArchive error for ${month}:`, err.message);
150
+ return null;
151
+ }
152
+ }
153
+
154
+ export function pruneOldMonthlyArchives(dir, maxMonths = 6) {
155
+ try {
156
+ if (!fs.existsSync(dir) || maxMonths < 0) return;
157
+ const files = fs.readdirSync(dir);
158
+ const monthlyFiles = [];
159
+ for (const file of files) {
160
+ const match = file.match(/^tasks-history-(\d{4}-\d{2})\.json\.gz$/);
161
+ if (match) {
162
+ monthlyFiles.push({ month: match[1], file, fullPath: path.join(dir, file) });
163
+ }
164
+ }
165
+ monthlyFiles.sort((a, b) => b.month.localeCompare(a.month));
166
+ if (monthlyFiles.length > maxMonths) {
167
+ const toDelete = monthlyFiles.slice(maxMonths);
168
+ for (const item of toDelete) {
169
+ bestEffort('delete-old-archive', () => fs.unlinkSync(item.fullPath));
170
+ logger.info(`[dsh-cron] pruned old monthly history archive: ${item.file}`);
171
+ }
172
+ }
173
+ } catch (err) {
174
+ bestEffort('log-prune-err', () => logger.debug?.(`[dsh-cron] prune old monthly archives error: ${err.message}`));
175
+ }
176
+ }
177
+
178
+ export function getArchivedRuns(store, id, { limit = 50, offset = 0, search = '' } = {}) {
179
+ try {
180
+ const dir = path.dirname(store.filePath);
181
+ const archivePath = path.join(dir, 'tasks-history-archive.json');
182
+ if (!fs.existsSync(archivePath)) return { runs: [], total: 0 };
183
+ const archive = JSON.parse(fs.readFileSync(archivePath, 'utf-8')) || {};
184
+ 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))) : [];
185
+ if (search && search.trim()) {
186
+ const q = search.toLowerCase();
187
+ runs = runs.filter(r => (r.output && r.output.toLowerCase().includes(q)) || (r.error && r.error.toLowerCase().includes(q)));
188
+ }
189
+ const total = runs.length;
190
+ const paged = runs.slice(offset, offset + limit);
191
+ return { runs: paged, total };
192
+ } catch (err) {
193
+ return { runs: [], total: 0, error: err.message };
194
+ }
195
+ }
@@ -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
+ }