dodo-mcp 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/globalConfig.js +17 -0
- package/dist/config/globalConfig.js.map +1 -1
- package/dist/server/aiAdmin.js +20 -5
- package/dist/server/aiAdmin.js.map +1 -1
- package/dist/server/bootstrap.js +2 -1
- package/dist/server/bootstrap.js.map +1 -1
- package/dist/server/configUi/ui/recovery.js +415 -20
- package/dist/server/configUi/ui/recoveryMaintenance.js +2 -2
- package/dist/server/configUi/workbench.css +50 -1
- package/dist/server/configUi/workbench.js +193 -0
- package/dist/server/installationRuntime.js +1 -0
- package/dist/server/installationRuntime.js.map +1 -1
- package/dist/server/ipcDispatch.js +52 -3
- package/dist/server/ipcDispatch.js.map +1 -1
- package/dist/services/changes/planner.js +11 -2
- package/dist/services/changes/planner.js.map +1 -1
- package/dist/services/git/gitService.js +239 -0
- package/dist/services/git/gitService.js.map +1 -1
- package/dist/services/recovery/contracts.js +6 -0
- package/dist/services/recovery/contracts.js.map +1 -1
- package/dist/services/recovery/maintenance.js +8 -2
- package/dist/services/recovery/maintenance.js.map +1 -1
- package/dist/services/recovery/recoveryService.js +299 -48
- package/dist/services/recovery/recoveryService.js.map +1 -1
- package/dist/tools/context.js +11 -3
- package/dist/tools/context.js.map +1 -1
- package/dist/workspace/ignores.js +32 -6
- package/dist/workspace/ignores.js.map +1 -1
- package/docs/RELEASE_1.4.0.md +25 -0
- package/docs/RELEASE_NOTES.md +7 -0
- package/package.json +2 -1
- package/schemas/global-config.schema.json +20 -0
- package/scripts/setup-multimodal.mjs +0 -0
|
@@ -17,7 +17,7 @@ import { digestOf } from '../../util/hash.js';
|
|
|
17
17
|
import { loadGlobalConfig } from '../../config/globalConfig.js';
|
|
18
18
|
import { RecoveryPolicySchema } from './contracts.js';
|
|
19
19
|
import { RecoveryStorage, rootIdentity } from './storage.js';
|
|
20
|
-
const OMIT_DIR = new Set(['.git', 'node_modules', 'dist', 'build', 'out', '.cache', 'coverage', 'target', '.venv', 'venv', '__pycache__', 'uploads', 'logs', 'volumes']);
|
|
20
|
+
const OMIT_DIR = new Set(['.git', 'node_modules', '.next', '.nuxt', '.svelte-kit', '.turbo', '.output', '.vercel', '.parcel-cache', 'dist', 'build', 'out', '.cache', 'coverage', 'target', '.venv', 'venv', '__pycache__', 'uploads', 'logs', 'volumes', 'tmp', 'temp']);
|
|
21
21
|
const DATA_EXTENSION = /\.(?:db|sqlite|sqlite3|db-wal|db-shm|log|dump|bak)$/i;
|
|
22
22
|
// Bounded high-confidence screening, NOT a guarantee that every credential is detectable.
|
|
23
23
|
const SECRET = /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----|\b(?:gh[pousr]_[a-zA-Z0-9]{30,}|github_pat_[a-zA-Z0-9_]{40,}|sk-[a-zA-Z0-9_-]{32,}|AKIA[0-9A-Z]{16})\b/;
|
|
@@ -90,6 +90,11 @@ export class RecoveryService {
|
|
|
90
90
|
identity;
|
|
91
91
|
state = 'INITIALIZING';
|
|
92
92
|
errorCode = null;
|
|
93
|
+
errorDetail = null;
|
|
94
|
+
blockedPath = null;
|
|
95
|
+
skippedSecretFiles = new Set();
|
|
96
|
+
lastWarning = null;
|
|
97
|
+
lastGitCheckpoint = null;
|
|
93
98
|
baseline;
|
|
94
99
|
latest;
|
|
95
100
|
lastTrigger = null;
|
|
@@ -134,10 +139,25 @@ export class RecoveryService {
|
|
|
134
139
|
throw new DodoError('PATH_DENIED', 'path is outside allowed recovery source scope');
|
|
135
140
|
this.s.wfs.resolve(rel, { allowMissing: true });
|
|
136
141
|
}
|
|
142
|
+
effectiveMode(policy = this.policy()) {
|
|
143
|
+
if (!policy.enabled || policy.mode === 'off')
|
|
144
|
+
return 'off';
|
|
145
|
+
if (policy.mode === 'git')
|
|
146
|
+
return 'git';
|
|
147
|
+
if (policy.mode === 'snapshot')
|
|
148
|
+
return 'snapshot';
|
|
149
|
+
return this.s.git.hasGitMarker() ? 'git' : 'snapshot';
|
|
150
|
+
}
|
|
151
|
+
recordSkippedSecret(rel) {
|
|
152
|
+
if (this.skippedSecretFiles.size < 50)
|
|
153
|
+
this.skippedSecretFiles.add(rel);
|
|
154
|
+
this.lastWarning = `ข้ามไฟล์ที่มีเนื้อหาคล้ายคีย์ความลับอัตโนมัติ (${this.skippedSecretFiles.size} ไฟล์): ${[...this.skippedSecretFiles].slice(0, 5).join(', ')}`;
|
|
155
|
+
}
|
|
137
156
|
async currentEntries(targets, extraPaths = []) {
|
|
138
|
-
|
|
157
|
+
const policy = this.policy();
|
|
158
|
+
return this.deadline.run(this.deadline.getStore() ?? Date.now() + policy.scanMs, async () => {
|
|
139
159
|
const read = async () => {
|
|
140
|
-
const inventory = await this.inventory(
|
|
160
|
+
const inventory = await this.inventory(policy, targets);
|
|
141
161
|
const seen = new Set(inventory.files.map(f => f.entry.path));
|
|
142
162
|
for (const p of extraPaths)
|
|
143
163
|
if (!seen.has(p)) {
|
|
@@ -149,14 +169,28 @@ export class RecoveryService {
|
|
|
149
169
|
}
|
|
150
170
|
const r = this.s.wfs.resolve(p, { allowMissing: true });
|
|
151
171
|
if (r.stat?.isFile())
|
|
152
|
-
inventory.files.push(...(await this.inventory(
|
|
172
|
+
inventory.files.push(...(await this.inventory(policy, [p])).files);
|
|
153
173
|
}
|
|
154
|
-
if (inventory.files.length >
|
|
174
|
+
if (inventory.files.length > policy.maxEntries)
|
|
155
175
|
throw new DodoError('RESOURCE_LIMIT', 'source scan exceeds entry budget');
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
176
|
+
const kept = [];
|
|
177
|
+
for (const f of inventory.files) {
|
|
178
|
+
if (f.entry.kind === 'file') {
|
|
179
|
+
try {
|
|
180
|
+
f.entry.hash = await this.readFile(f, undefined, policy);
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
if (e instanceof DodoError && e.code === 'SECRET_PATH_DENIED' && policy.skipSecretContentFiles) {
|
|
184
|
+
this.recordSkippedSecret(f.entry.path);
|
|
185
|
+
this.blockedPath = null;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
throw e;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
kept.push(f);
|
|
192
|
+
}
|
|
193
|
+
return kept.sort((a, b) => a.entry.path.localeCompare(b.entry.path));
|
|
160
194
|
};
|
|
161
195
|
const first = await read(), second = await read();
|
|
162
196
|
if (digestOf(first) !== digestOf(second))
|
|
@@ -168,18 +202,44 @@ export class RecoveryService {
|
|
|
168
202
|
const r = this.s.store.db.prepare('SELECT payload FROM recovery_policies WHERE workspace_id=?').get(this.s.workspaceId);
|
|
169
203
|
return RecoveryPolicySchema.parse(r ? JSON.parse(r.payload) : {});
|
|
170
204
|
}
|
|
205
|
+
normalizePolicyPaths(raw) {
|
|
206
|
+
const cleanList = (items, allowSecretNames) => {
|
|
207
|
+
const out = [];
|
|
208
|
+
for (const p of items) {
|
|
209
|
+
const candidate = p.trim().replace(/\\/g, '/').replace(/^\.\/+/, '').replace(/\/+$/, '');
|
|
210
|
+
if (!candidate || candidate === '.')
|
|
211
|
+
throw new DodoError('INVALID_INPUT', 'data roots and exclusions must be canonical relative paths');
|
|
212
|
+
const rel = this.s.wfs.normalizeRel(candidate);
|
|
213
|
+
if (rel === '.' || rel !== candidate || /[?*\[\]]/.test(rel))
|
|
214
|
+
throw new DodoError('INVALID_INPUT', 'data roots and exclusions must be canonical relative paths without wildcards');
|
|
215
|
+
if (!allowSecretNames) {
|
|
216
|
+
try {
|
|
217
|
+
this.s.wfs.resolve(rel, { allowMissing: true });
|
|
218
|
+
}
|
|
219
|
+
catch (e) {
|
|
220
|
+
if (!(e instanceof DodoError && e.code === 'SECRET_PATH_DENIED'))
|
|
221
|
+
throw e;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
out.push(rel);
|
|
225
|
+
}
|
|
226
|
+
return [...new Set(out)].sort();
|
|
227
|
+
};
|
|
228
|
+
return {
|
|
229
|
+
...raw,
|
|
230
|
+
enabled: raw.mode === 'off' ? false : raw.enabled,
|
|
231
|
+
excludePaths: cleanList(raw.excludePaths, true),
|
|
232
|
+
dataRoots: cleanList(raw.dataRoots, false),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
171
235
|
/** Private owner service only. Repository configuration never reaches this method. */
|
|
172
236
|
async configure(input, confirmed, revalidate) {
|
|
173
237
|
if (!confirmed)
|
|
174
238
|
throw new DodoError('INVALID_INPUT', 'confirm the Recovery policy change');
|
|
175
|
-
const
|
|
239
|
+
const parsed = RecoveryPolicySchema.parse(input);
|
|
176
240
|
if (!this.project())
|
|
177
241
|
throw new DodoError('NOT_FOUND', 'register this project first');
|
|
178
|
-
|
|
179
|
-
const rel = this.s.wfs.normalizeRel(p);
|
|
180
|
-
if (rel === '.' || rel !== p)
|
|
181
|
-
throw new DodoError('INVALID_INPUT', 'data roots must be canonical relative subdirectories');
|
|
182
|
-
}
|
|
242
|
+
const policy = this.normalizePolicyPaths(parsed);
|
|
183
243
|
if (this.s.jobs.runningCount() || this.s.mutations?.busy)
|
|
184
244
|
throw new DodoError('CONFLICT', 'wait for active mutations and jobs before changing Recovery policy');
|
|
185
245
|
await this.initialization;
|
|
@@ -196,6 +256,10 @@ export class RecoveryService {
|
|
|
196
256
|
this.activated = false;
|
|
197
257
|
this.state = policy.enabled ? 'INITIALIZING' : 'DISABLED_BY_OWNER';
|
|
198
258
|
this.errorCode = null;
|
|
259
|
+
this.errorDetail = null;
|
|
260
|
+
this.blockedPath = null;
|
|
261
|
+
this.lastWarning = null;
|
|
262
|
+
this.skippedSecretFiles.clear();
|
|
199
263
|
this.activate();
|
|
200
264
|
return this.status();
|
|
201
265
|
}
|
|
@@ -204,20 +268,107 @@ export class RecoveryService {
|
|
|
204
268
|
this.s.mutations.assertCanStart();
|
|
205
269
|
if (this.s.jobs.runningCount())
|
|
206
270
|
throw new DodoError('CONFLICT', 'wait for running jobs before changing Recovery policy');
|
|
207
|
-
|
|
271
|
+
const normalized = this.normalizePolicyPaths(policy);
|
|
272
|
+
this.s.store.db.prepare('INSERT INTO recovery_policies VALUES (?,?) ON CONFLICT(workspace_id) DO UPDATE SET payload=excluded.payload').run(this.s.workspaceId, JSON.stringify(normalized));
|
|
273
|
+
if (this.state === 'BLOCKED' || this.errorCode) {
|
|
274
|
+
this.errorCode = null;
|
|
275
|
+
this.errorDetail = null;
|
|
276
|
+
this.blockedPath = null;
|
|
277
|
+
this.lastWarning = null;
|
|
278
|
+
this.skippedSecretFiles.clear();
|
|
279
|
+
this.baseline = undefined;
|
|
280
|
+
this.activated = false;
|
|
281
|
+
this.state = normalized.enabled ? 'INITIALIZING' : 'DISABLED_BY_OWNER';
|
|
282
|
+
this.activate();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async resetBlockedState(revalidate) {
|
|
286
|
+
revalidate?.();
|
|
287
|
+
this.assertProject();
|
|
288
|
+
this.s.store.db.prepare("UPDATE recovery_snapshots SET state='INCOMPLETE' WHERE workspace_id=? AND state='PREPARING'").run(this.s.workspaceId);
|
|
289
|
+
this.s.store.db.prepare('DELETE FROM recovery_reservations WHERE workspace_id=?').run(this.s.workspaceId);
|
|
290
|
+
this.errorCode = null;
|
|
291
|
+
this.errorDetail = null;
|
|
292
|
+
this.blockedPath = null;
|
|
293
|
+
this.lastWarning = null;
|
|
294
|
+
this.skippedSecretFiles.clear();
|
|
295
|
+
this.baseline = undefined;
|
|
296
|
+
this.activated = false;
|
|
297
|
+
const policy = this.policy();
|
|
298
|
+
if (!policy.enabled || policy.mode === 'off') {
|
|
299
|
+
this.state = 'DISABLED_BY_OWNER';
|
|
300
|
+
return this.status();
|
|
301
|
+
}
|
|
302
|
+
this.state = 'INITIALIZING';
|
|
303
|
+
try {
|
|
304
|
+
await this.checkpoint('owner-checkpoint', 'local-config-owner');
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
/* status reflects current state */
|
|
308
|
+
}
|
|
309
|
+
return this.status();
|
|
310
|
+
}
|
|
311
|
+
async gitRecoveryStatus(limit = 20) {
|
|
312
|
+
this.assertProject();
|
|
313
|
+
const git = await this.s.git.gitRecoveryStatus(limit);
|
|
314
|
+
return { ...git, effectiveMode: this.effectiveMode(), lastGitCheckpoint: this.lastGitCheckpoint };
|
|
315
|
+
}
|
|
316
|
+
async createGitCheckpoint(trigger = 'owner-checkpoint', actor = 'local-config-owner') {
|
|
317
|
+
this.assertProject();
|
|
318
|
+
const policy = this.policy();
|
|
319
|
+
const cp = await this.s.git.createRecoveryCheckpoint({
|
|
320
|
+
trigger,
|
|
321
|
+
actor,
|
|
322
|
+
indexStagingDir: path.join(this.storage.directory, 'staging'),
|
|
323
|
+
excludePaths: [...policy.excludePaths, ...policy.dataRoots],
|
|
324
|
+
});
|
|
325
|
+
this.lastGitCheckpoint = { id: cp.id, ref: cp.ref, commitSha: cp.commitSha, createdAt: cp.createdAt, trigger };
|
|
326
|
+
this.lastCheckedAt = cp.createdAt;
|
|
327
|
+
this.lastTrigger = trigger;
|
|
328
|
+
this.state = 'READY';
|
|
329
|
+
this.errorCode = null;
|
|
330
|
+
this.errorDetail = null;
|
|
331
|
+
this.blockedPath = null;
|
|
332
|
+
this.s.store.audit({ principal: actor, workspaceId: this.s.workspaceId, tool: 'recovery.git.checkpoint', result: 'ready', inputDigest: digestOf({ id: cp.id, trigger }) });
|
|
333
|
+
return cp;
|
|
334
|
+
}
|
|
335
|
+
async previewGitRestore(opts) {
|
|
336
|
+
this.assertProject();
|
|
337
|
+
return await this.s.git.previewRecoveryRestore(opts);
|
|
338
|
+
}
|
|
339
|
+
async applyGitRestore(opts, revalidate) {
|
|
340
|
+
return this.s.mutations.run(async () => {
|
|
341
|
+
revalidate?.();
|
|
342
|
+
this.assertProject();
|
|
343
|
+
const policy = this.policy();
|
|
344
|
+
const res = await this.s.git.applyRecoveryRestore({
|
|
345
|
+
...opts,
|
|
346
|
+
indexStagingDir: path.join(this.storage.directory, 'staging'),
|
|
347
|
+
excludePaths: [...policy.excludePaths, ...policy.dataRoots],
|
|
348
|
+
});
|
|
349
|
+
this.s.store.db.prepare('DELETE FROM recovery_drift WHERE workspace_id=?').run(this.s.workspaceId);
|
|
350
|
+
this.baseline = undefined;
|
|
351
|
+
this.lastCheckedAt = Date.now();
|
|
352
|
+
this.errorCode = null;
|
|
353
|
+
this.errorDetail = null;
|
|
354
|
+
this.blockedPath = null;
|
|
355
|
+
this.s.store.audit({ principal: 'local-config-owner', workspaceId: this.s.workspaceId, tool: 'recovery.git.restore', result: 'restored', inputDigest: digestOf({ ref: opts.ref, paths: opts.paths ?? [] }) });
|
|
356
|
+
return res;
|
|
357
|
+
});
|
|
208
358
|
}
|
|
209
359
|
protectedCheckpointIds() { return [this.baseline, this.latest]; }
|
|
210
360
|
summary() {
|
|
211
|
-
const { enabled, state, registered, awaitingActivation, sourceOnly, lastCheckedAt, errorCode, progress, projectBytes } = this.status();
|
|
361
|
+
const { enabled, state, effectiveMode, hasGitRepo, registered, awaitingActivation, sourceOnly, lastCheckedAt, errorCode, errorDetail, blockedPath, skippedSecretFiles, lastWarning, lastGitCheckpoint, progress, projectBytes } = this.status();
|
|
212
362
|
const drift = this.drift.status();
|
|
213
|
-
return { enabled, state, registered, awaitingActivation, sourceOnly, lastCheckedAt, errorCode, progress, projectBytes, drift: { state: drift.state, changedCount: drift.changedCount, critical: drift.critical, errorCode: drift.errorCode }, git: this.git.status(), verification: this.evidence.summary() };
|
|
363
|
+
return { enabled, state, effectiveMode, hasGitRepo, registered, awaitingActivation, sourceOnly, lastCheckedAt, errorCode, errorDetail, blockedPath, skippedSecretFiles, lastWarning, lastGitCheckpoint, progress, projectBytes, drift: { state: drift.state, changedCount: drift.changedCount, critical: drift.critical, errorCode: drift.errorCode }, git: this.git.status(), verification: this.evidence.summary() };
|
|
214
364
|
}
|
|
215
365
|
status() {
|
|
216
366
|
const registered = Boolean(this.s.store.db.prepare('SELECT 1 FROM project_registry WHERE workspace_id=? AND removed_at IS NULL').get(this.s.workspaceId));
|
|
217
367
|
const policy = this.policy();
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
368
|
+
const effectiveMode = this.effectiveMode(policy);
|
|
369
|
+
return { enabled: policy.enabled, state: policy.enabled && policy.mode !== 'off' ? this.state : 'DISABLED_BY_OWNER', effectiveMode, hasGitRepo: this.s.git.hasGitMarker(), registered, awaitingActivation: registered && !this.activated,
|
|
370
|
+
sourceOnly: true, lastTrigger: this.lastTrigger, counts: { ...this.counts }, lastCheckpointId: this.latest ?? this.lastGitCheckpoint?.id ?? null, baselineId: this.baseline ?? this.lastGitCheckpoint?.id ?? null, integrity: this.lastCheckedAt ? 'verified_at_capture' : 'not_verified', lastCheckedAt: this.lastCheckedAt,
|
|
371
|
+
errorCode: this.errorCode, errorDetail: this.errorDetail, blockedPath: this.blockedPath, skippedSecretFiles: [...this.skippedSecretFiles], lastWarning: this.lastWarning, lastGitCheckpoint: this.lastGitCheckpoint, progress: { ...this.progress }, drift: this.drift.status(), git: this.git.status(), verification: this.evidence.summary(), ...this.storage.usageFor(this.s.workspaceId), policy };
|
|
221
372
|
}
|
|
222
373
|
activate() {
|
|
223
374
|
if (this.closed || this.activated)
|
|
@@ -227,7 +378,7 @@ export class RecoveryService {
|
|
|
227
378
|
return;
|
|
228
379
|
}
|
|
229
380
|
catch (e) {
|
|
230
|
-
this.fail(e);
|
|
381
|
+
this.fail(e, true);
|
|
231
382
|
return;
|
|
232
383
|
}
|
|
233
384
|
this.activated = true;
|
|
@@ -238,13 +389,18 @@ export class RecoveryService {
|
|
|
238
389
|
this.configVault.reconcile();
|
|
239
390
|
this.reconciled = true;
|
|
240
391
|
}
|
|
241
|
-
|
|
392
|
+
const policy = this.policy();
|
|
393
|
+
if (!policy.enabled || policy.mode === 'off') {
|
|
242
394
|
this.state = 'DISABLED_BY_OWNER';
|
|
243
395
|
return;
|
|
244
396
|
}
|
|
245
397
|
this.initialization = (async () => {
|
|
246
398
|
// The same queue protects activation from direct, agent and scheduled writers.
|
|
247
399
|
await this.s.mutations.run(async () => {
|
|
400
|
+
if (policy.mode === 'git') {
|
|
401
|
+
await this.createGitCheckpoint('activation', 'system:activation');
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
248
404
|
const prior = this.s.store.db.prepare("SELECT id FROM recovery_snapshots WHERE workspace_id=? AND state='READY' ORDER BY created_at DESC LIMIT 1").get(this.s.workspaceId);
|
|
249
405
|
if (prior) {
|
|
250
406
|
const m = await this.storage.readVerified(prior.id, this.s.workspaceId);
|
|
@@ -259,7 +415,22 @@ export class RecoveryService {
|
|
|
259
415
|
});
|
|
260
416
|
})().catch(e => { this.fail(e); });
|
|
261
417
|
}
|
|
262
|
-
fail(error
|
|
418
|
+
fail(error, forceBlock = false) {
|
|
419
|
+
const policy = this.policy();
|
|
420
|
+
this.errorCode = error instanceof DodoError ? error.code : 'BACKUP_IO_ERROR';
|
|
421
|
+
this.errorDetail = error instanceof Error ? error.message : String(error);
|
|
422
|
+
if (error instanceof DodoError && error.detail && typeof error.detail === 'object' && 'path' in error.detail && typeof error.detail.path === 'string') {
|
|
423
|
+
this.blockedPath = error.detail.path;
|
|
424
|
+
}
|
|
425
|
+
const criticalIdentity = error instanceof DodoError && ((error.code === 'PATH_DENIED' && /original registered project|private-state overlap|previous root identity/.test(error.message)) || error.code === 'RECOVERY_REQUIRED');
|
|
426
|
+
if (forceBlock || criticalIdentity || policy.strictEnforcement || policy.gitRequired || Boolean(policy.gitDirectory)) {
|
|
427
|
+
this.state = 'BLOCKED';
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
this.lastWarning = `${this.errorCode}: ${this.errorDetail}${this.blockedPath ? ` (${this.blockedPath})` : ''}`;
|
|
431
|
+
this.state = 'READY';
|
|
432
|
+
}
|
|
433
|
+
}
|
|
263
434
|
async checkpoint(trigger, actor, targets) {
|
|
264
435
|
if (this.closed)
|
|
265
436
|
throw new DodoError('CONFLICT', 'Recovery runtime is closing');
|
|
@@ -272,10 +443,33 @@ export class RecoveryService {
|
|
|
272
443
|
this.configVault.reconcile();
|
|
273
444
|
this.reconciled = true;
|
|
274
445
|
}
|
|
275
|
-
|
|
446
|
+
const policy = this.policy();
|
|
447
|
+
if (!policy.enabled || policy.mode === 'off') {
|
|
276
448
|
this.state = 'DISABLED_BY_OWNER';
|
|
277
449
|
return undefined;
|
|
278
450
|
}
|
|
451
|
+
if (policy.mode === 'git') {
|
|
452
|
+
return this.s.mutations.run(async () => {
|
|
453
|
+
try {
|
|
454
|
+
const cp = await this.createGitCheckpoint(trigger, actor);
|
|
455
|
+
this.activated = true;
|
|
456
|
+
this.authority.getStore()?.();
|
|
457
|
+
await this.reviewedRestore.getStore()?.();
|
|
458
|
+
return cp.id;
|
|
459
|
+
}
|
|
460
|
+
catch (e) {
|
|
461
|
+
if (e instanceof DodoError && ['FORBIDDEN', 'AUTH_REQUIRED'].includes(e.code))
|
|
462
|
+
throw e;
|
|
463
|
+
this.fail(e);
|
|
464
|
+
if (!policy.strictEnforcement && (trigger === 'before-write' || trigger === 'before-exec' || trigger === 'activation')) {
|
|
465
|
+
this.authority.getStore()?.();
|
|
466
|
+
await this.reviewedRestore.getStore()?.();
|
|
467
|
+
return this.lastGitCheckpoint?.id;
|
|
468
|
+
}
|
|
469
|
+
throw new DodoError('RECOVERY_REQUIRED', 'Git recovery checkpoint is blocked; no source mutation was started', { detail: { reason: this.errorCode, path: this.blockedPath }, recovery: 'inspect Recovery in project settings or switch mode' });
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
}
|
|
279
473
|
// Initialization may itself be waiting behind this call's mutation ticket.
|
|
280
474
|
// Capture here within that ticket; never await a queued activation from a writer.
|
|
281
475
|
return this.s.mutations.run(async () => {
|
|
@@ -291,14 +485,18 @@ export class RecoveryService {
|
|
|
291
485
|
await this.capture('activation', 'system:activation');
|
|
292
486
|
}
|
|
293
487
|
await this.storage.readVerified(this.baseline, this.s.workspaceId);
|
|
294
|
-
if (!this.reviewedRestore.getStore() && (trigger === 'before-write' || trigger === 'before-exec'))
|
|
488
|
+
if (policy.checkDrift && !this.reviewedRestore.getStore() && (trigger === 'before-write' || trigger === 'before-exec')) {
|
|
295
489
|
try {
|
|
296
|
-
await this.deadline.run(Date.now() +
|
|
490
|
+
await this.deadline.run(Date.now() + policy.scanMs, () => this.drift.guard(targets));
|
|
297
491
|
}
|
|
298
492
|
catch (e) {
|
|
299
|
-
|
|
300
|
-
|
|
493
|
+
if (policy.strictEnforcement) {
|
|
494
|
+
guardRejected = e instanceof DodoError && ['FILE_CHANGED', 'CONFLICT'].includes(e.code);
|
|
495
|
+
throw e;
|
|
496
|
+
}
|
|
497
|
+
this.lastWarning = e instanceof Error ? e.message : String(e);
|
|
301
498
|
}
|
|
499
|
+
}
|
|
302
500
|
const id = await this.capture(trigger, actor, targets);
|
|
303
501
|
this.activated = true;
|
|
304
502
|
this.startScanner();
|
|
@@ -315,8 +513,15 @@ export class RecoveryService {
|
|
|
315
513
|
catch (e) {
|
|
316
514
|
if (guardRejected || e instanceof DodoError && (['FORBIDDEN', 'AUTH_REQUIRED'].includes(e.code)))
|
|
317
515
|
throw e;
|
|
318
|
-
|
|
319
|
-
|
|
516
|
+
const criticalIdentity = e instanceof DodoError && e.code === 'PATH_DENIED' && /original registered project|private-state overlap|previous root identity/.test(e.message);
|
|
517
|
+
const mustBlock = criticalIdentity || policy.strictEnforcement || policy.gitRequired || Boolean(policy.gitDirectory) || trigger === 'verified-checkpoint';
|
|
518
|
+
this.fail(e, mustBlock);
|
|
519
|
+
if (!mustBlock && (trigger === 'before-write' || trigger === 'before-exec' || trigger === 'activation')) {
|
|
520
|
+
this.authority.getStore()?.();
|
|
521
|
+
await this.reviewedRestore.getStore()?.();
|
|
522
|
+
return this.latest ?? this.lastGitCheckpoint?.id;
|
|
523
|
+
}
|
|
524
|
+
throw new DodoError('RECOVERY_REQUIRED', 'source backup is blocked; no source mutation was started', { detail: { reason: this.errorCode, path: this.blockedPath }, recovery: 'inspect Recovery in project settings, fix storage/path/integrity problems and retry' });
|
|
320
525
|
}
|
|
321
526
|
});
|
|
322
527
|
}
|
|
@@ -340,8 +545,10 @@ export class RecoveryService {
|
|
|
340
545
|
if (visited % 100 === 0)
|
|
341
546
|
await tick();
|
|
342
547
|
if (this.excluded(rel, false, policy)) {
|
|
343
|
-
if (target)
|
|
344
|
-
|
|
548
|
+
if (target && (this.s.wfs.ignores.isSecret(rel) || this.s.wfs.ignores.isProtected(rel) || policy.strictEnforcement)) {
|
|
549
|
+
this.blockedPath = rel;
|
|
550
|
+
throw new DodoError('PATH_DENIED', `mutation target is excluded from source Recovery: ${rel}`, { detail: { path: rel } });
|
|
551
|
+
}
|
|
345
552
|
excluded++;
|
|
346
553
|
return;
|
|
347
554
|
}
|
|
@@ -352,8 +559,10 @@ export class RecoveryService {
|
|
|
352
559
|
return;
|
|
353
560
|
}
|
|
354
561
|
if (this.excluded(rel, st.isDirectory(), policy)) {
|
|
355
|
-
if (target)
|
|
356
|
-
|
|
562
|
+
if (target && (this.s.wfs.ignores.isSecret(rel) || this.s.wfs.ignores.isProtected(rel) || policy.strictEnforcement)) {
|
|
563
|
+
this.blockedPath = rel;
|
|
564
|
+
throw new DodoError('PATH_DENIED', `mutation target is excluded from source Recovery: ${rel}`, { detail: { path: rel } });
|
|
565
|
+
}
|
|
357
566
|
excluded++;
|
|
358
567
|
return;
|
|
359
568
|
}
|
|
@@ -371,8 +580,15 @@ export class RecoveryService {
|
|
|
371
580
|
}
|
|
372
581
|
else {
|
|
373
582
|
this.s.wfs.assertRegularFileForDirectAccess(r);
|
|
374
|
-
if (st.size > policy.fileBytes)
|
|
375
|
-
|
|
583
|
+
if (st.size > policy.fileBytes) {
|
|
584
|
+
if (!policy.strictEnforcement && !target) {
|
|
585
|
+
this.lastWarning = `ข้ามไฟล์ขนาดใหญ่เกินขีดจำกัด (${rel})`;
|
|
586
|
+
excluded++;
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
this.blockedPath = rel;
|
|
590
|
+
throw new DodoError('RESOURCE_LIMIT', `source file exceeds Recovery file budget: ${rel}`, { detail: { path: rel } });
|
|
591
|
+
}
|
|
376
592
|
files.push({ entry: { path: rel, kind: 'file', hash: null, bytes: st.size, mode: st.mode & 0o777 }, signature: this.signature(st) });
|
|
377
593
|
}
|
|
378
594
|
};
|
|
@@ -390,7 +606,7 @@ export class RecoveryService {
|
|
|
390
606
|
const distinct = new Map(files.map(f => [f.entry.path, f]));
|
|
391
607
|
return { files: [...distinct.values()].sort((a, b) => a.entry.path.localeCompare(b.entry.path)), excluded };
|
|
392
608
|
}
|
|
393
|
-
async readFile(file, stage) {
|
|
609
|
+
async readFile(file, stage, policy = this.policy()) {
|
|
394
610
|
const r = this.s.wfs.resolve(file.entry.path);
|
|
395
611
|
this.s.wfs.assertRegularFileForDirectAccess(r);
|
|
396
612
|
if (!r.stat || this.signature(r.stat) !== file.signature)
|
|
@@ -412,10 +628,15 @@ export class RecoveryService {
|
|
|
412
628
|
bytes += bytesRead;
|
|
413
629
|
if (bytes > file.entry.bytes)
|
|
414
630
|
throw new DodoError('FILE_CHANGED', 'source grew during backup');
|
|
415
|
-
const chunk = buf.subarray(0, bytesRead)
|
|
416
|
-
if (
|
|
417
|
-
|
|
418
|
-
|
|
631
|
+
const chunk = buf.subarray(0, bytesRead);
|
|
632
|
+
if (policy.scanContentSecrets) {
|
|
633
|
+
const text = tail + chunk.toString('utf8');
|
|
634
|
+
if (SECRET.test(text)) {
|
|
635
|
+
this.blockedPath = file.entry.path;
|
|
636
|
+
throw new DodoError('SECRET_PATH_DENIED', `credential-like content found during source backup: ${file.entry.path}`, { detail: { path: file.entry.path } });
|
|
637
|
+
}
|
|
638
|
+
tail = text.slice(-1024);
|
|
639
|
+
}
|
|
419
640
|
hash.update(chunk);
|
|
420
641
|
if (output) {
|
|
421
642
|
let offset = 0;
|
|
@@ -451,9 +672,27 @@ export class RecoveryService {
|
|
|
451
672
|
const inventory = await this.inventory(policy, targets);
|
|
452
673
|
// Hash before reserving: unchanged objects cost logical references, not duplicate disk bytes.
|
|
453
674
|
const expectedHashes = new Map();
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
675
|
+
const keptFiles = [];
|
|
676
|
+
const skippedInPass = new Set();
|
|
677
|
+
for (const f of inventory.files) {
|
|
678
|
+
if (f.entry.kind === 'file') {
|
|
679
|
+
try {
|
|
680
|
+
expectedHashes.set(f.entry.path, await this.readFile(f, undefined, policy));
|
|
681
|
+
}
|
|
682
|
+
catch (e) {
|
|
683
|
+
if (e instanceof DodoError && e.code === 'SECRET_PATH_DENIED' && policy.skipSecretContentFiles) {
|
|
684
|
+
this.recordSkippedSecret(f.entry.path);
|
|
685
|
+
this.blockedPath = null;
|
|
686
|
+
skippedInPass.add(f.entry.path);
|
|
687
|
+
inventory.excluded++;
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
throw e;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
keptFiles.push(f);
|
|
694
|
+
}
|
|
695
|
+
inventory.files = keptFiles;
|
|
457
696
|
this.storage.prune(this.s.workspaceId, policy);
|
|
458
697
|
this.storage.collectOrphans();
|
|
459
698
|
this.s.store.db.transaction(() => {
|
|
@@ -470,7 +709,7 @@ export class RecoveryService {
|
|
|
470
709
|
// freshly verifies every distinct object before recording READY.
|
|
471
710
|
const duplicate = published.has(expectedHashes.get(entry.path)) || this.storage.hasObject(expectedHashes.get(entry.path));
|
|
472
711
|
staging = duplicate ? undefined : this.storage.stagePath();
|
|
473
|
-
entry.hash = await this.readFile(f, staging);
|
|
712
|
+
entry.hash = await this.readFile(f, staging, policy);
|
|
474
713
|
if (entry.hash !== expectedHashes.get(entry.path))
|
|
475
714
|
throw new DodoError('FILE_CHANGED', 'source changed after space reservation');
|
|
476
715
|
if (staging)
|
|
@@ -484,12 +723,21 @@ export class RecoveryService {
|
|
|
484
723
|
}
|
|
485
724
|
// Rescan paths AND reread hashes. A watcher or old checkpoint alone is not freshness proof.
|
|
486
725
|
const after = await this.inventory(policy, targets);
|
|
726
|
+
if (skippedInPass.size > 0) {
|
|
727
|
+
after.files = after.files.filter(af => {
|
|
728
|
+
if (skippedInPass.has(af.entry.path)) {
|
|
729
|
+
after.excluded++;
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
return true;
|
|
733
|
+
});
|
|
734
|
+
}
|
|
487
735
|
if (digestOf(after) !== digestOf(inventory))
|
|
488
736
|
throw new DodoError('FILE_CHANGED', 'source inventory changed during backup');
|
|
489
737
|
const entryHashes = new Map(entries.map(e => [e.path, e.hash]));
|
|
490
738
|
for (const f of after.files)
|
|
491
739
|
if (f.entry.kind === 'file') {
|
|
492
|
-
const hash = await this.readFile(f);
|
|
740
|
+
const hash = await this.readFile(f, undefined, policy);
|
|
493
741
|
if (hash !== entryHashes.get(f.entry.path))
|
|
494
742
|
throw new DodoError('FILE_CHANGED', 'source content changed during backup');
|
|
495
743
|
}
|
|
@@ -512,6 +760,8 @@ export class RecoveryService {
|
|
|
512
760
|
this.lastCheckedAt = Date.now();
|
|
513
761
|
this.state = 'READY';
|
|
514
762
|
this.errorCode = null;
|
|
763
|
+
this.errorDetail = null;
|
|
764
|
+
this.blockedPath = null;
|
|
515
765
|
this.s.store.audit({ principal: actor, workspaceId: this.s.workspaceId, tool: 'recovery.checkpoint', result: 'ready', inputDigest: digestOf({ id, trigger }) });
|
|
516
766
|
this.storage.prune(this.s.workspaceId, policy);
|
|
517
767
|
this.git.prune();
|
|
@@ -519,7 +769,8 @@ export class RecoveryService {
|
|
|
519
769
|
}
|
|
520
770
|
catch (e) {
|
|
521
771
|
this.s.store.db.prepare("UPDATE recovery_snapshots SET state='INCOMPLETE' WHERE id=? AND state='PREPARING'").run(id);
|
|
522
|
-
|
|
772
|
+
const mustBlock = (e instanceof DodoError && e.code === 'PATH_DENIED' && /original registered project|private-state overlap|previous root identity/.test(e.message)) || policy.strictEnforcement || policy.gitRequired || Boolean(policy.gitDirectory) || trigger === 'verified-checkpoint';
|
|
773
|
+
this.fail(e, mustBlock);
|
|
523
774
|
throw e;
|
|
524
775
|
}
|
|
525
776
|
finally {
|