@ddtcorex/dsh-maestro-core 0.10.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/LICENSE +21 -0
- package/README.md +209 -0
- package/cordis.patch.yml +43 -0
- package/lib/bin.d.ts +2 -0
- package/lib/bin.js +3 -0
- package/lib/boot-guard.d.ts +33 -0
- package/lib/boot-guard.js +68 -0
- package/lib/boot-lock.d.ts +56 -0
- package/lib/boot-lock.js +155 -0
- package/lib/cli.d.ts +31 -0
- package/lib/cli.js +336 -0
- package/lib/client.js +2901 -0
- package/lib/config/index.d.ts +34 -0
- package/lib/config/index.js +62 -0
- package/lib/config/service.d.ts +18 -0
- package/lib/config/service.js +28 -0
- package/lib/config.d.ts +1 -0
- package/lib/config.js +19 -0
- package/lib/daemon-freshness.d.ts +48 -0
- package/lib/daemon-freshness.js +122 -0
- package/lib/debug-agent.d.ts +17 -0
- package/lib/debug-agent.js +144 -0
- package/lib/dsh-session.d.ts +26 -0
- package/lib/dsh-session.js +45 -0
- package/lib/guard/config.d.ts +82 -0
- package/lib/guard/config.js +211 -0
- package/lib/guard/decide.d.ts +30 -0
- package/lib/guard/decide.js +38 -0
- package/lib/guard/full-scan-tool.d.ts +6 -0
- package/lib/guard/full-scan-tool.js +65 -0
- package/lib/guard/index.d.ts +85 -0
- package/lib/guard/index.js +254 -0
- package/lib/guard/journal.d.ts +146 -0
- package/lib/guard/journal.js +398 -0
- package/lib/guard/migrate.d.ts +15 -0
- package/lib/guard/migrate.js +42 -0
- package/lib/guard/parse.d.ts +193 -0
- package/lib/guard/parse.js +1103 -0
- package/lib/guard/paths.d.ts +104 -0
- package/lib/guard/paths.js +254 -0
- package/lib/guard/permission-policy.d.ts +10 -0
- package/lib/guard/permission-policy.js +21 -0
- package/lib/guard/redact.d.ts +25 -0
- package/lib/guard/redact.js +52 -0
- package/lib/guard/rules.d.ts +65 -0
- package/lib/guard/rules.js +832 -0
- package/lib/guard/status-tool.d.ts +54 -0
- package/lib/guard/status-tool.js +195 -0
- package/lib/guard/tiers.d.ts +2 -0
- package/lib/guard/tiers.js +1 -0
- package/lib/guard/validators.d.ts +4 -0
- package/lib/guard/validators.js +45 -0
- package/lib/health-poller.d.ts +90 -0
- package/lib/health-poller.js +430 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/intents.d.ts +32 -0
- package/lib/intents.js +49 -0
- package/lib/notifier.d.ts +12 -0
- package/lib/notifier.js +62 -0
- package/lib/paths.d.ts +13 -0
- package/lib/paths.js +70 -0
- package/lib/plugin.d.ts +250 -0
- package/lib/plugin.js +1126 -0
- package/lib/preset.d.ts +73 -0
- package/lib/preset.js +158 -0
- package/lib/report.d.ts +11 -0
- package/lib/report.js +58 -0
- package/lib/restart-exec.d.ts +35 -0
- package/lib/restart-exec.js +60 -0
- package/lib/restart-guards.d.ts +39 -0
- package/lib/restart-guards.js +150 -0
- package/lib/restart-tool.d.ts +124 -0
- package/lib/restart-tool.js +539 -0
- package/lib/restart-web.d.ts +26 -0
- package/lib/restart-web.js +58 -0
- package/lib/resume-log.d.ts +24 -0
- package/lib/resume-log.js +18 -0
- package/lib/resume-tools.d.ts +131 -0
- package/lib/resume-tools.js +337 -0
- package/lib/resume.d.ts +33 -0
- package/lib/resume.js +221 -0
- package/lib/scan.d.ts +20 -0
- package/lib/scan.js +71 -0
- package/lib/self-kill-guard.d.ts +81 -0
- package/lib/self-kill-guard.js +208 -0
- package/lib/session-health.d.ts +89 -0
- package/lib/session-health.js +274 -0
- package/lib/session-log-file.d.ts +22 -0
- package/lib/session-log-file.js +74 -0
- package/lib/skill-provider.d.ts +6 -0
- package/lib/skill-provider.js +73 -0
- package/lib/snapshot.d.ts +59 -0
- package/lib/snapshot.js +358 -0
- package/lib/source.d.ts +19 -0
- package/lib/source.js +19 -0
- package/lib/store/index.d.ts +42 -0
- package/lib/store/index.js +340 -0
- package/lib/store/legacy.d.ts +17 -0
- package/lib/store/legacy.js +85 -0
- package/lib/supervisor.d.ts +119 -0
- package/lib/supervisor.js +741 -0
- package/lib/sync/backup-config.d.ts +55 -0
- package/lib/sync/backup-config.js +119 -0
- package/lib/sync/backup-service.d.ts +110 -0
- package/lib/sync/backup-service.js +440 -0
- package/lib/sync/bidirectional.d.ts +39 -0
- package/lib/sync/bidirectional.js +25 -0
- package/lib/sync/cli.d.ts +38 -0
- package/lib/sync/cli.js +536 -0
- package/lib/sync/config.d.ts +77 -0
- package/lib/sync/config.js +90 -0
- package/lib/sync/fingerprint.d.ts +29 -0
- package/lib/sync/fingerprint.js +65 -0
- package/lib/sync/hashing.d.ts +9 -0
- package/lib/sync/hashing.js +47 -0
- package/lib/sync/index.d.ts +6 -0
- package/lib/sync/index.js +634 -0
- package/lib/sync/machine-id.d.ts +29 -0
- package/lib/sync/machine-id.js +66 -0
- package/lib/sync/merge.d.ts +33 -0
- package/lib/sync/merge.js +88 -0
- package/lib/sync/peer-host.d.ts +22 -0
- package/lib/sync/peer-host.js +62 -0
- package/lib/sync/process-runner.d.ts +31 -0
- package/lib/sync/process-runner.js +161 -0
- package/lib/sync/remote-agent.d.ts +42 -0
- package/lib/sync/remote-agent.js +217 -0
- package/lib/sync/remote-cache.d.ts +18 -0
- package/lib/sync/remote-cache.js +60 -0
- package/lib/sync/remote-manifest.d.ts +8 -0
- package/lib/sync/remote-manifest.js +64 -0
- package/lib/sync/s3-object-store.d.ts +38 -0
- package/lib/sync/s3-object-store.js +126 -0
- package/lib/sync/session-merge.d.ts +18 -0
- package/lib/sync/session-merge.js +191 -0
- package/lib/sync/session-plan.d.ts +29 -0
- package/lib/sync/session-plan.js +213 -0
- package/lib/sync/sigv4.d.ts +20 -0
- package/lib/sync/sigv4.js +57 -0
- package/lib/sync/snapshot.d.ts +21 -0
- package/lib/sync/snapshot.js +176 -0
- package/lib/sync/ssh-mux.d.ts +12 -0
- package/lib/sync/ssh-mux.js +48 -0
- package/lib/sync/sync-plan.d.ts +24 -0
- package/lib/sync/sync-plan.js +366 -0
- package/lib/sync/sync-service.d.ts +177 -0
- package/lib/sync/sync-service.js +819 -0
- package/lib/sync/sync-types.d.ts +87 -0
- package/lib/sync/sync-types.js +1 -0
- package/lib/sync/transport.d.ts +75 -0
- package/lib/sync/transport.js +200 -0
- package/lib/sync/tunnel-restore.d.ts +77 -0
- package/lib/sync/tunnel-restore.js +142 -0
- package/lib/sync/validation.d.ts +10 -0
- package/lib/sync/validation.js +83 -0
- package/lib/tool-view.d.ts +62 -0
- package/lib/tool-view.js +70 -0
- package/lib/types/client/auto-reload.d.ts +8 -0
- package/lib/types/client/auto-reload.d.ts.map +1 -0
- package/lib/types/client/config/MaestroSettings.d.ts +25 -0
- package/lib/types/client/config/MaestroSettings.d.ts.map +1 -0
- package/lib/types/client/config/components/BrandMark.d.ts +50 -0
- package/lib/types/client/config/components/BrandMark.d.ts.map +1 -0
- package/lib/types/client/config/guard-view.d.ts +83 -0
- package/lib/types/client/config/guard-view.d.ts.map +1 -0
- package/lib/types/client/config/index.d.ts +20 -0
- package/lib/types/client/config/index.d.ts.map +1 -0
- package/lib/types/client/config/maestro-mark.d.ts +29 -0
- package/lib/types/client/config/maestro-mark.d.ts.map +1 -0
- package/lib/types/client/config/settings-nav-icon.d.ts +21 -0
- package/lib/types/client/config/settings-nav-icon.d.ts.map +1 -0
- package/lib/types/client/index.d.ts +16 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts +6 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/SyncPanel.d.ts +12 -0
- package/lib/types/client/sync/SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/confirm-dialog.d.ts +43 -0
- package/lib/types/client/sync/confirm-dialog.d.ts.map +1 -0
- package/lib/types/client/sync/index.d.ts +10 -0
- package/lib/types/client/sync/index.d.ts.map +1 -0
- package/lib/types/client/sync/ui.d.ts +79 -0
- package/lib/types/client/sync/ui.d.ts.map +1 -0
- package/lib/types/client/sync/use-backup.d.ts +50 -0
- package/lib/types/client/sync/use-backup.d.ts.map +1 -0
- package/lib/types/client/sync/use-sync.d.ts +88 -0
- package/lib/types/client/sync/use-sync.d.ts.map +1 -0
- package/package.json +89 -0
- package/skills/dsh-safe-restart/SKILL.md +206 -0
- package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +468 -0
- package/src/client/auto-reload.ts +104 -0
- package/src/client/config/MaestroSettings.tsx +892 -0
- package/src/client/config/components/BrandMark.tsx +94 -0
- package/src/client/config/guard-view.ts +187 -0
- package/src/client/config/index.tsx +109 -0
- package/src/client/config/maestro-mark.ts +37 -0
- package/src/client/config/settings-nav-icon.ts +68 -0
- package/src/client/index.tsx +33 -0
- package/src/client/sync/R2SyncPanel.tsx +260 -0
- package/src/client/sync/SyncPanel.tsx +422 -0
- package/src/client/sync/confirm-dialog.tsx +202 -0
- package/src/client/sync/index.tsx +336 -0
- package/src/client/sync/ui.tsx +313 -0
- package/src/client/sync/use-backup.ts +207 -0
- package/src/client/sync/use-sync.ts +502 -0
- package/src/host/bin.ts +3 -0
- package/src/host/boot-guard.ts +76 -0
- package/src/host/boot-lock.ts +155 -0
- package/src/host/cli.ts +288 -0
- package/src/host/config/index.ts +78 -0
- package/src/host/config/service.ts +40 -0
- package/src/host/config.ts +18 -0
- package/src/host/daemon-freshness.ts +143 -0
- package/src/host/debug-agent.ts +145 -0
- package/src/host/dsh-session.ts +56 -0
- package/src/host/dsh-skill.d.ts +44 -0
- package/src/host/guard/augment.d.ts +33 -0
- package/src/host/guard/config.ts +244 -0
- package/src/host/guard/decide.ts +40 -0
- package/src/host/guard/full-scan-tool.ts +61 -0
- package/src/host/guard/index.ts +281 -0
- package/src/host/guard/journal.ts +440 -0
- package/src/host/guard/migrate.ts +41 -0
- package/src/host/guard/parse.ts +1150 -0
- package/src/host/guard/paths.ts +239 -0
- package/src/host/guard/permission-policy.ts +15 -0
- package/src/host/guard/redact.ts +64 -0
- package/src/host/guard/rules.ts +891 -0
- package/src/host/guard/status-tool.ts +229 -0
- package/src/host/guard/tiers.ts +2 -0
- package/src/host/guard/validators.ts +29 -0
- package/src/host/health-poller.ts +446 -0
- package/src/host/index.ts +7 -0
- package/src/host/intents.ts +67 -0
- package/src/host/notifier.ts +71 -0
- package/src/host/paths.ts +70 -0
- package/src/host/plugin.ts +1097 -0
- package/src/host/preset.ts +170 -0
- package/src/host/report.ts +71 -0
- package/src/host/restart-exec.ts +81 -0
- package/src/host/restart-guards.ts +150 -0
- package/src/host/restart-tool.ts +483 -0
- package/src/host/restart-web.ts +72 -0
- package/src/host/resume-log.ts +39 -0
- package/src/host/resume-tools.ts +385 -0
- package/src/host/resume.ts +204 -0
- package/src/host/scan.ts +61 -0
- package/src/host/self-kill-guard.ts +216 -0
- package/src/host/session-health.ts +236 -0
- package/src/host/session-log-file.ts +77 -0
- package/src/host/skill-provider.ts +69 -0
- package/src/host/snapshot.ts +337 -0
- package/src/host/source.ts +19 -0
- package/src/host/store/index.ts +348 -0
- package/src/host/store/legacy.ts +96 -0
- package/src/host/supervisor.ts +720 -0
- package/src/host/sync/backup-config.ts +159 -0
- package/src/host/sync/backup-service.ts +453 -0
- package/src/host/sync/bidirectional.ts +66 -0
- package/src/host/sync/cli.ts +539 -0
- package/src/host/sync/config.ts +119 -0
- package/src/host/sync/fingerprint.ts +83 -0
- package/src/host/sync/hashing.ts +58 -0
- package/src/host/sync/index.ts +737 -0
- package/src/host/sync/machine-id.ts +81 -0
- package/src/host/sync/merge.ts +102 -0
- package/src/host/sync/peer-host.ts +65 -0
- package/src/host/sync/process-runner.ts +177 -0
- package/src/host/sync/remote-agent.ts +213 -0
- package/src/host/sync/remote-cache.ts +68 -0
- package/src/host/sync/remote-manifest.ts +70 -0
- package/src/host/sync/s3-object-store.ts +144 -0
- package/src/host/sync/session-merge.ts +220 -0
- package/src/host/sync/session-plan.ts +237 -0
- package/src/host/sync/sigv4.ts +81 -0
- package/src/host/sync/snapshot.ts +162 -0
- package/src/host/sync/ssh-mux.ts +56 -0
- package/src/host/sync/sync-plan.ts +366 -0
- package/src/host/sync/sync-service.ts +855 -0
- package/src/host/sync/sync-types.ts +102 -0
- package/src/host/sync/transport.ts +261 -0
- package/src/host/sync/tunnel-restore.ts +160 -0
- package/src/host/sync/validation.ts +91 -0
- package/src/host/tool-view.ts +98 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import * as fs from 'node:fs';
|
|
4
|
+
import type { FileSnapshot, PlannedAction, SyncPlan, SyncPreview, SyncSummary, SyncDirection, SessionCounts } from './sync-types.js';
|
|
5
|
+
import { mergeDelimited } from './merge.js';
|
|
6
|
+
import { isSameSession, mergeSessionBuffers } from './session-plan.js';
|
|
7
|
+
|
|
8
|
+
export const PREVIEW_TTL_MS = 60_000;
|
|
9
|
+
export const MAX_PREVIEWS = 50;
|
|
10
|
+
|
|
11
|
+
const previews = new Map<string, SyncPreview>();
|
|
12
|
+
const previewDirections = new Map<string, SyncDirection>();
|
|
13
|
+
|
|
14
|
+
function previewFile(id: string, dir: string): string {
|
|
15
|
+
return path.join(dir, `${id}.json`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Read a persisted preview record (null when missing/corrupt). */
|
|
19
|
+
function readPersisted(id: string, dir: string): { preview: SyncPreview; direction?: SyncDirection } | null {
|
|
20
|
+
try {
|
|
21
|
+
const parsed = JSON.parse(fs.readFileSync(previewFile(id, dir), 'utf-8')) as { preview?: SyncPreview; direction?: SyncDirection };
|
|
22
|
+
if (!parsed || typeof parsed !== 'object' || typeof parsed.preview?.previewId !== 'string') return null;
|
|
23
|
+
return { preview: parsed.preview, direction: parsed.direction };
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pruneExpired(): void {
|
|
30
|
+
const now = Date.now();
|
|
31
|
+
for (const [id, p] of previews.entries()) {
|
|
32
|
+
if (new Date(p.expiresAt).getTime() <= now) {
|
|
33
|
+
previews.delete(id);
|
|
34
|
+
previewDirections.delete(id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (previews.size > MAX_PREVIEWS) {
|
|
38
|
+
const entries = [...previews.entries()].sort((a, b) => new Date(a[1].expiresAt).getTime() - new Date(b[1].expiresAt).getTime());
|
|
39
|
+
const toDelete = previews.size - MAX_PREVIEWS;
|
|
40
|
+
for (let i = 0; i < toDelete; i++) {
|
|
41
|
+
const delId = entries[i]![0];
|
|
42
|
+
previews.delete(delId);
|
|
43
|
+
previewDirections.delete(delId);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Persist a preview to disk so a later process (a separate CLI apply) can
|
|
50
|
+
* consume it. The file mirrors the in-memory record; the TTL is enforced on
|
|
51
|
+
* read. Sidecar only — a plain operational folder under DSH_HOME, never part
|
|
52
|
+
* of the eligible sync data.
|
|
53
|
+
*/
|
|
54
|
+
export function persistPreview(preview: SyncPreview, direction?: SyncDirection, dir?: string): string {
|
|
55
|
+
if (!dir) return '';
|
|
56
|
+
try {
|
|
57
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
let files: string[] = [];
|
|
60
|
+
try {
|
|
61
|
+
files = fs.readdirSync(dir).filter((f) => f.endsWith('.json'));
|
|
62
|
+
} catch {
|
|
63
|
+
files = [];
|
|
64
|
+
}
|
|
65
|
+
// purge expired records, then cap at MAX_PREVIEWS (oldest first)
|
|
66
|
+
const keep: string[] = [];
|
|
67
|
+
for (const f of files) {
|
|
68
|
+
if (f === `${preview.previewId}.json`) continue;
|
|
69
|
+
const record = readPersisted(f.slice(0, -5), dir);
|
|
70
|
+
if (record && new Date(record.preview.expiresAt).getTime() > now) keep.push(f);
|
|
71
|
+
else {
|
|
72
|
+
try {
|
|
73
|
+
fs.unlinkSync(path.join(dir, f));
|
|
74
|
+
} catch {}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
keep.sort();
|
|
78
|
+
while (keep.length >= MAX_PREVIEWS) {
|
|
79
|
+
const old = keep.shift()!;
|
|
80
|
+
try {
|
|
81
|
+
fs.unlinkSync(path.join(dir, old));
|
|
82
|
+
} catch {}
|
|
83
|
+
}
|
|
84
|
+
const target = previewFile(preview.previewId, dir);
|
|
85
|
+
const tmp = target + `.tmp.${randomBytes(4).toString('hex')}`;
|
|
86
|
+
fs.writeFileSync(tmp, JSON.stringify({ preview, direction }), 'utf-8');
|
|
87
|
+
fs.renameSync(tmp, target);
|
|
88
|
+
try {
|
|
89
|
+
fs.chmodSync(target, 0o600);
|
|
90
|
+
} catch {}
|
|
91
|
+
} catch {
|
|
92
|
+
// persistence is best-effort; the in-memory record still works in-process
|
|
93
|
+
}
|
|
94
|
+
return previewFile(preview.previewId, dir);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Delete every persisted preview file under dir (cleanup helper). */
|
|
98
|
+
export function clearPreviewStore(dir: string): void {
|
|
99
|
+
if (!dir) return;
|
|
100
|
+
try {
|
|
101
|
+
for (const f of fs.readdirSync(dir)) {
|
|
102
|
+
if (f.endsWith('.json')) {
|
|
103
|
+
try {
|
|
104
|
+
fs.unlinkSync(path.join(dir, f));
|
|
105
|
+
} catch {}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} catch {}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function storePreview(preview: SyncPreview, direction?: SyncDirection, dir?: string): void {
|
|
112
|
+
pruneExpired();
|
|
113
|
+
if (previews.size >= MAX_PREVIEWS) {
|
|
114
|
+
let oldestId: string | null = null;
|
|
115
|
+
let oldestTime = Infinity;
|
|
116
|
+
for (const [id, p] of previews.entries()) {
|
|
117
|
+
const t = new Date(p.expiresAt).getTime();
|
|
118
|
+
if (t < oldestTime) {
|
|
119
|
+
oldestTime = t;
|
|
120
|
+
oldestId = id;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (oldestId) {
|
|
124
|
+
previews.delete(oldestId);
|
|
125
|
+
previewDirections.delete(oldestId);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
previews.set(preview.previewId, preview);
|
|
129
|
+
if (direction) previewDirections.set(preview.previewId, direction);
|
|
130
|
+
if (dir) persistPreview(preview, direction, dir);
|
|
131
|
+
const ttl = Math.max(0, new Date(preview.expiresAt).getTime() - Date.now());
|
|
132
|
+
setTimeout(() => {
|
|
133
|
+
previews.delete(preview.previewId);
|
|
134
|
+
previewDirections.delete(preview.previewId);
|
|
135
|
+
}, ttl).unref?.();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function getPreview(id: string, dir?: string): SyncPreview | undefined {
|
|
139
|
+
const cached = previews.get(id);
|
|
140
|
+
if (cached) {
|
|
141
|
+
if (new Date(cached.expiresAt).getTime() <= Date.now()) {
|
|
142
|
+
previews.delete(id);
|
|
143
|
+
previewDirections.delete(id);
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
return cached;
|
|
147
|
+
}
|
|
148
|
+
if (!dir) return undefined;
|
|
149
|
+
const record = readPersisted(id, dir);
|
|
150
|
+
if (!record) return undefined;
|
|
151
|
+
const p = record.preview;
|
|
152
|
+
if (new Date(p.expiresAt).getTime() <= Date.now()) {
|
|
153
|
+
try {
|
|
154
|
+
fs.unlinkSync(previewFile(id, dir));
|
|
155
|
+
} catch {}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
// hydrate into memory for single-process idempotence
|
|
159
|
+
previews.set(p.previewId, p);
|
|
160
|
+
if (record.direction) previewDirections.set(p.previewId, record.direction);
|
|
161
|
+
return p;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function getPreviewDirection(id: string): SyncDirection | undefined {
|
|
165
|
+
return previewDirections.get(id);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function deletePreview(id: string, dir?: string): void {
|
|
169
|
+
previews.delete(id);
|
|
170
|
+
previewDirections.delete(id);
|
|
171
|
+
if (dir) {
|
|
172
|
+
try {
|
|
173
|
+
fs.unlinkSync(previewFile(id, dir));
|
|
174
|
+
} catch {}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function clearPreviews(): void {
|
|
179
|
+
previews.clear();
|
|
180
|
+
previewDirections.clear();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function revisionFrom(snapshots: FileSnapshot[], direction: SyncDirection): string {
|
|
184
|
+
const h = createHash('sha256');
|
|
185
|
+
h.update(direction);
|
|
186
|
+
const sorted = [...snapshots].sort((a, b) => a.path.localeCompare(b.path));
|
|
187
|
+
for (const s of sorted) {
|
|
188
|
+
h.update(s.path + ':' + s.sha256 + ':' + String(s.size) + ':' + s.kind);
|
|
189
|
+
}
|
|
190
|
+
return h.digest('hex').slice(0, 16);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function unionJsonlAdded(localBuf: Buffer, remoteBuf: Buffer): number {
|
|
194
|
+
const localLines = localBuf.toString('utf-8').split('\n').filter((l) => l.length > 0);
|
|
195
|
+
const remoteLines = remoteBuf.toString('utf-8').split('\n').filter((l) => l.length > 0);
|
|
196
|
+
const seen = new Set(localLines);
|
|
197
|
+
let added = 0;
|
|
198
|
+
for (const line of remoteLines) {
|
|
199
|
+
if (!seen.has(line)) {
|
|
200
|
+
seen.add(line);
|
|
201
|
+
added++;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return added;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function buildPlan(
|
|
208
|
+
localSnapshots: FileSnapshot[],
|
|
209
|
+
remoteSnapshots: FileSnapshot[],
|
|
210
|
+
direction: SyncDirection,
|
|
211
|
+
localContents: Map<string, Buffer>,
|
|
212
|
+
remoteContents: Map<string, Buffer>,
|
|
213
|
+
opts: { countOnlySessions?: boolean } = {},
|
|
214
|
+
): Promise<SyncPlan> {
|
|
215
|
+
const localMap = new Map(localSnapshots.map((s) => [s.path, s]));
|
|
216
|
+
const remoteMap = new Map(remoteSnapshots.map((s) => [s.path, s]));
|
|
217
|
+
const allPaths = new Set<string>([...localMap.keys(), ...remoteMap.keys()]);
|
|
218
|
+
|
|
219
|
+
const actions: PlannedAction[] = [];
|
|
220
|
+
let copied = 0;
|
|
221
|
+
let merged = 0;
|
|
222
|
+
let skipped = 0;
|
|
223
|
+
let conflicts = 0;
|
|
224
|
+
let added = 0;
|
|
225
|
+
|
|
226
|
+
// Count-only sessions: preview reports added/updated/deleted/identical from
|
|
227
|
+
// path+checksum without staging content. These files never produce action
|
|
228
|
+
// rows — the UI shows the counts instead of per-file rows. Apply is untouched:
|
|
229
|
+
// it re-inventories with full content and builds a real merge plan.
|
|
230
|
+
let sessionCounts: SessionCounts = { added: 0, updated: 0, deleted: 0, identical: 0 };
|
|
231
|
+
const isSession = (p: string) => p.endsWith('.zstd');
|
|
232
|
+
|
|
233
|
+
const sortedPaths = [...allPaths].sort((a, b) => a.localeCompare(b));
|
|
234
|
+
|
|
235
|
+
for (const p of sortedPaths) {
|
|
236
|
+
const ls = localMap.get(p);
|
|
237
|
+
const rs = remoteMap.get(p);
|
|
238
|
+
const target: 'local' | 'remote' = direction === 'pull' ? 'local' : 'remote';
|
|
239
|
+
|
|
240
|
+
if (opts.countOnlySessions && isSession(p)) {
|
|
241
|
+
if (!ls && rs) {
|
|
242
|
+
if (direction === 'pull') sessionCounts.added++;
|
|
243
|
+
else sessionCounts.deleted++;
|
|
244
|
+
} else if (ls && !rs) {
|
|
245
|
+
if (direction === 'pull') sessionCounts.deleted++;
|
|
246
|
+
else sessionCounts.added++;
|
|
247
|
+
} else if (ls && rs) {
|
|
248
|
+
if (ls.sha256 === rs.sha256) sessionCounts.identical++;
|
|
249
|
+
else sessionCounts.updated++;
|
|
250
|
+
}
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (ls && !rs) {
|
|
255
|
+
if (direction === 'push') {
|
|
256
|
+
actions.push({ path: p, action: 'copy', target: 'remote', added: 0, reason: 'local only', expectedTargetSha256: undefined });
|
|
257
|
+
copied++;
|
|
258
|
+
} else {
|
|
259
|
+
actions.push({ path: p, action: 'skip', target: 'local', added: 0, reason: 'local only, pull skips' });
|
|
260
|
+
skipped++;
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (!ls && rs) {
|
|
265
|
+
if (direction === 'pull') {
|
|
266
|
+
actions.push({ path: p, action: 'copy', target: 'local', added: 0, reason: 'remote only' });
|
|
267
|
+
copied++;
|
|
268
|
+
} else {
|
|
269
|
+
actions.push({ path: p, action: 'skip', target: 'remote', added: 0, reason: 'remote only, push skips' });
|
|
270
|
+
skipped++;
|
|
271
|
+
}
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (ls && rs) {
|
|
275
|
+
if (ls.sha256 === rs.sha256) {
|
|
276
|
+
actions.push({ path: p, action: 'skip', target, added: 0, reason: 'identical bytes', expectedTargetSha256: ls.sha256 });
|
|
277
|
+
skipped++;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
const localBuf = localContents.get(p);
|
|
281
|
+
const remoteBuf = remoteContents.get(p);
|
|
282
|
+
if (!localBuf || !remoteBuf) {
|
|
283
|
+
actions.push({ path: p, action: 'conflict', target, added: 0, reason: 'missing content for merge', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
284
|
+
conflicts++;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
if (p.endsWith('.md')) {
|
|
290
|
+
const localStr = localBuf.toString('utf-8');
|
|
291
|
+
const remoteStr = remoteBuf.toString('utf-8');
|
|
292
|
+
const { added: addedCount } = direction === 'pull' ? mergeDelimited(localStr, remoteStr) : mergeDelimited(remoteStr, localStr);
|
|
293
|
+
if (addedCount === 0) {
|
|
294
|
+
// same delimited entries, different bytes/order — nothing to add
|
|
295
|
+
actions.push({ path: p, action: 'skip', target, added: 0, reason: 'content-equal, no new entries', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
296
|
+
skipped++;
|
|
297
|
+
} else {
|
|
298
|
+
actions.push({ path: p, action: 'merge', target, added: addedCount, reason: 'content differs, merge', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
299
|
+
merged++;
|
|
300
|
+
added += addedCount;
|
|
301
|
+
}
|
|
302
|
+
} else if (p.endsWith('.jsonl')) {
|
|
303
|
+
const addedCount = direction === 'pull' ? unionJsonlAdded(localBuf, remoteBuf) : unionJsonlAdded(remoteBuf, localBuf);
|
|
304
|
+
if (addedCount === 0) {
|
|
305
|
+
actions.push({ path: p, action: 'skip', target, added: 0, reason: 'content-equal, no new lines', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
306
|
+
skipped++;
|
|
307
|
+
} else {
|
|
308
|
+
actions.push({ path: p, action: 'merge', target, added: addedCount, reason: 'content differs, merge', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
309
|
+
merged++;
|
|
310
|
+
added += addedCount;
|
|
311
|
+
}
|
|
312
|
+
} else if (p.endsWith('.zstd')) {
|
|
313
|
+
// Session handling is Buffer/path-only and preserves DSH standalone checksummed Zstd header frame.
|
|
314
|
+
// Never convert Zstd bytes to UTF-8 string; use validated Zstd artifact API via session-plan.
|
|
315
|
+
try {
|
|
316
|
+
if (!isSameSession(localBuf, remoteBuf)) {
|
|
317
|
+
actions.push({ path: p, action: 'conflict', target, added: 0, reason: 'session header mismatch', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
318
|
+
conflicts++;
|
|
319
|
+
} else {
|
|
320
|
+
const { added: addedCount } = direction === 'pull' ? mergeSessionBuffers(localBuf, remoteBuf) : mergeSessionBuffers(remoteBuf, localBuf);
|
|
321
|
+
if (addedCount === 0) {
|
|
322
|
+
// identical session lines under a (possibly different) zstd frame layout
|
|
323
|
+
actions.push({ path: p, action: 'skip', target, added: 0, reason: 'content-equal, no new entries', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
324
|
+
skipped++;
|
|
325
|
+
} else {
|
|
326
|
+
actions.push({ path: p, action: 'merge', target, added: addedCount, reason: 'session differs, merge', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
327
|
+
merged++;
|
|
328
|
+
added += addedCount;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
} catch (e: any) {
|
|
332
|
+
actions.push({ path: p, action: 'conflict', target, added: 0, reason: e?.message ?? 'session merge failed', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
333
|
+
conflicts++;
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
actions.push({ path: p, action: 'conflict', target, added: 0, reason: 'unknown kind', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
337
|
+
conflicts++;
|
|
338
|
+
}
|
|
339
|
+
} catch (e: any) {
|
|
340
|
+
actions.push({ path: p, action: 'conflict', target, added: 0, reason: e?.message ?? 'merge failed', expectedTargetSha256: target === 'local' ? ls.sha256 : rs.sha256 });
|
|
341
|
+
conflicts++;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const revision = revisionFrom([...localSnapshots, ...remoteSnapshots], direction);
|
|
347
|
+
const summary: SyncSummary = { copied, merged, skipped, conflicts, added };
|
|
348
|
+
actions.sort((a, b) => a.path.localeCompare(b.path));
|
|
349
|
+
return { revision, actions, summary, sessionCounts: opts.countOnlySessions ? sessionCounts : undefined };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export async function buildPreview(
|
|
353
|
+
localSnapshots: FileSnapshot[],
|
|
354
|
+
remoteSnapshots: FileSnapshot[],
|
|
355
|
+
direction: SyncDirection,
|
|
356
|
+
localContents: Map<string, Buffer>,
|
|
357
|
+
remoteContents: Map<string, Buffer>,
|
|
358
|
+
opts: { countOnlySessions?: boolean } = {},
|
|
359
|
+
): Promise<SyncPreview> {
|
|
360
|
+
const plan = await buildPlan(localSnapshots, remoteSnapshots, direction, localContents, remoteContents, opts);
|
|
361
|
+
const previewId = randomBytes(16).toString('hex');
|
|
362
|
+
const expiresAt = new Date(Date.now() + PREVIEW_TTL_MS).toISOString();
|
|
363
|
+
const preview: SyncPreview = { ...plan, previewId, expiresAt };
|
|
364
|
+
storePreview(preview, direction);
|
|
365
|
+
return preview;
|
|
366
|
+
}
|