@ddtcorex/dsh-maestro-core 0.15.1 → 0.16.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/README.md +7 -7
- package/assets/icon.svg +4 -0
- package/cordis.patch.yml +0 -12
- package/lib/client.js +3 -1744
- package/lib/health-poller.js +1 -1
- package/lib/self-kill-guard.d.ts +25 -8
- package/lib/self-kill-guard.js +81 -27
- package/lib/types/client/config/components/BrandMark.d.ts +3 -3
- package/lib/types/client/index.d.ts.map +1 -1
- package/locale/en.json +6 -0
- package/package.json +9 -5
- package/src/client/config/components/BrandMark.tsx +3 -3
- package/src/client/index.tsx +0 -2
- package/src/host/health-poller.ts +1 -1
- package/src/host/self-kill-guard.ts +84 -26
- package/lib/sync/backup-config.d.ts +0 -55
- package/lib/sync/backup-config.js +0 -119
- package/lib/sync/backup-service.d.ts +0 -123
- package/lib/sync/backup-service.js +0 -486
- package/lib/sync/bidirectional.d.ts +0 -39
- package/lib/sync/bidirectional.js +0 -25
- package/lib/sync/cli.d.ts +0 -38
- package/lib/sync/cli.js +0 -528
- package/lib/sync/config.d.ts +0 -59
- package/lib/sync/config.js +0 -72
- package/lib/sync/fingerprint.d.ts +0 -29
- package/lib/sync/fingerprint.js +0 -65
- package/lib/sync/hashing.d.ts +0 -22
- package/lib/sync/hashing.js +0 -77
- package/lib/sync/index.d.ts +0 -6
- package/lib/sync/index.js +0 -619
- package/lib/sync/machine-id.d.ts +0 -29
- package/lib/sync/machine-id.js +0 -66
- package/lib/sync/merge.d.ts +0 -31
- package/lib/sync/merge.js +0 -86
- package/lib/sync/peer-host.d.ts +0 -22
- package/lib/sync/peer-host.js +0 -62
- package/lib/sync/process-runner.d.ts +0 -31
- package/lib/sync/process-runner.js +0 -161
- package/lib/sync/remote-agent.d.ts +0 -41
- package/lib/sync/remote-agent.js +0 -216
- package/lib/sync/remote-cache.d.ts +0 -18
- package/lib/sync/remote-cache.js +0 -60
- package/lib/sync/remote-manifest.d.ts +0 -20
- package/lib/sync/remote-manifest.js +0 -86
- package/lib/sync/s3-object-store.d.ts +0 -38
- package/lib/sync/s3-object-store.js +0 -126
- package/lib/sync/session-plan.d.ts +0 -29
- package/lib/sync/session-plan.js +0 -213
- package/lib/sync/sigv4.d.ts +0 -20
- package/lib/sync/sigv4.js +0 -57
- package/lib/sync/snapshot.d.ts +0 -16
- package/lib/sync/snapshot.js +0 -143
- package/lib/sync/ssh-mux.d.ts +0 -12
- package/lib/sync/ssh-mux.js +0 -48
- package/lib/sync/store-root.d.ts +0 -44
- package/lib/sync/store-root.js +0 -67
- package/lib/sync/sync-plan.d.ts +0 -24
- package/lib/sync/sync-plan.js +0 -366
- package/lib/sync/sync-service.d.ts +0 -183
- package/lib/sync/sync-service.js +0 -926
- package/lib/sync/sync-types.d.ts +0 -87
- package/lib/sync/sync-types.js +0 -1
- package/lib/sync/transport.d.ts +0 -75
- package/lib/sync/transport.js +0 -196
- package/lib/sync/tunnel-restore.d.ts +0 -77
- package/lib/sync/tunnel-restore.js +0 -142
- package/lib/sync/validation.d.ts +0 -12
- package/lib/sync/validation.js +0 -96
- package/lib/types/client/sync/R2SyncPanel.d.ts +0 -6
- package/lib/types/client/sync/R2SyncPanel.d.ts.map +0 -1
- package/lib/types/client/sync/SyncPanel.d.ts +0 -12
- package/lib/types/client/sync/SyncPanel.d.ts.map +0 -1
- package/lib/types/client/sync/confirm-dialog.d.ts +0 -43
- package/lib/types/client/sync/confirm-dialog.d.ts.map +0 -1
- package/lib/types/client/sync/index.d.ts +0 -10
- package/lib/types/client/sync/index.d.ts.map +0 -1
- package/lib/types/client/sync/ui.d.ts +0 -75
- package/lib/types/client/sync/ui.d.ts.map +0 -1
- package/lib/types/client/sync/use-backup.d.ts +0 -50
- package/lib/types/client/sync/use-backup.d.ts.map +0 -1
- package/lib/types/client/sync/use-sync.d.ts +0 -88
- package/lib/types/client/sync/use-sync.d.ts.map +0 -1
- package/src/client/sync/R2SyncPanel.tsx +0 -260
- package/src/client/sync/SyncPanel.tsx +0 -422
- package/src/client/sync/confirm-dialog.tsx +0 -202
- package/src/client/sync/index.tsx +0 -286
- package/src/client/sync/ui.tsx +0 -297
- package/src/client/sync/use-backup.ts +0 -207
- package/src/client/sync/use-sync.ts +0 -502
- package/src/host/sync/backup-config.ts +0 -158
- package/src/host/sync/backup-service.ts +0 -506
- package/src/host/sync/bidirectional.ts +0 -66
- package/src/host/sync/cli.ts +0 -530
- package/src/host/sync/config.ts +0 -96
- package/src/host/sync/fingerprint.ts +0 -83
- package/src/host/sync/hashing.ts +0 -91
- package/src/host/sync/index.ts +0 -697
- package/src/host/sync/machine-id.ts +0 -81
- package/src/host/sync/merge.ts +0 -99
- package/src/host/sync/peer-host.ts +0 -65
- package/src/host/sync/process-runner.ts +0 -177
- package/src/host/sync/remote-agent.ts +0 -212
- package/src/host/sync/remote-cache.ts +0 -68
- package/src/host/sync/remote-manifest.ts +0 -91
- package/src/host/sync/s3-object-store.ts +0 -144
- package/src/host/sync/session-plan.ts +0 -237
- package/src/host/sync/sigv4.ts +0 -81
- package/src/host/sync/snapshot.ts +0 -132
- package/src/host/sync/ssh-mux.ts +0 -55
- package/src/host/sync/store-root.ts +0 -83
- package/src/host/sync/sync-plan.ts +0 -366
- package/src/host/sync/sync-service.ts +0 -960
- package/src/host/sync/sync-types.ts +0 -102
- package/src/host/sync/transport.ts +0 -256
- package/src/host/sync/tunnel-restore.ts +0 -160
- package/src/host/sync/validation.ts +0 -104
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bidirectional orchestration — one push-then-pull operation built from the
|
|
3
|
-
* existing per-direction preview/apply primitives.
|
|
4
|
-
*
|
|
5
|
-
* Each phase keeps the full safety contract (single-use preview, per-file
|
|
6
|
-
* CAS, fail closed); this module adds no new mutation route. The pull
|
|
7
|
-
* preview is minted only AFTER the push apply has landed, so it is exact.
|
|
8
|
-
* No auto-retry: the operator re-runs after reading the journal (union
|
|
9
|
-
* idempotence makes a re-run resume the unfinished phase).
|
|
10
|
-
*/
|
|
11
|
-
import type { SyncService, ApplyResult } from './sync-service.js';
|
|
12
|
-
import type { SyncScope, SyncPreview, SyncSummary, SyncFailure } from './sync-types.js';
|
|
13
|
-
|
|
14
|
-
export interface BidirectionalPreview {
|
|
15
|
-
/** Combined id — the push preview's id; the pull phase mints its own. */
|
|
16
|
-
previewId: string;
|
|
17
|
-
expiresAt: string;
|
|
18
|
-
/** Exact push plan. */
|
|
19
|
-
push: SyncPreview;
|
|
20
|
-
/** Pull plan projected BEFORE the push lands; recomputed exact at apply time. */
|
|
21
|
-
pullProjected: SyncPreview;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface BidirectionalResult {
|
|
25
|
-
ok: boolean;
|
|
26
|
-
push: ApplyResult;
|
|
27
|
-
/** Null when the push failed — pull never runs. */
|
|
28
|
-
pull: ApplyResult | null;
|
|
29
|
-
/** Final pull dry-run summary; all-skip means converged. Null when pull never ran. */
|
|
30
|
-
verification: SyncSummary | null;
|
|
31
|
-
committed: string[];
|
|
32
|
-
failures: SyncFailure[];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function runBidirectionalPreview(
|
|
36
|
-
svc: SyncService,
|
|
37
|
-
opts: { scope: SyncScope },
|
|
38
|
-
): Promise<BidirectionalPreview> {
|
|
39
|
-
const push = await svc.preview({ direction: 'push', scope: opts.scope });
|
|
40
|
-
const pullProjected = await svc.preview({ direction: 'pull', scope: opts.scope });
|
|
41
|
-
return { previewId: push.previewId, expiresAt: push.expiresAt, push, pullProjected };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export async function runBidirectionalApply(
|
|
45
|
-
svc: SyncService,
|
|
46
|
-
opts: { previewId: string; confirm: true; scope: SyncScope },
|
|
47
|
-
): Promise<BidirectionalResult> {
|
|
48
|
-
if ((opts as { confirm?: unknown }).confirm !== true) {
|
|
49
|
-
throw Object.assign(new Error('bidirectional apply requires confirm:true'), { phase: 'validate', code: 'CONFIRM_REQUIRED' });
|
|
50
|
-
}
|
|
51
|
-
const push = await svc.apply({ previewId: opts.previewId, direction: 'push', confirm: true, scope: opts.scope });
|
|
52
|
-
if (!push.ok) {
|
|
53
|
-
return { ok: false, push, pull: null, verification: null, committed: [...push.committed], failures: [...push.failures] };
|
|
54
|
-
}
|
|
55
|
-
const pullPreview = await svc.preview({ direction: 'pull', scope: opts.scope });
|
|
56
|
-
const pull = await svc.apply({ previewId: pullPreview.previewId, direction: 'pull', confirm: true, scope: opts.scope });
|
|
57
|
-
const verify = await svc.preview({ direction: 'pull', scope: opts.scope });
|
|
58
|
-
return {
|
|
59
|
-
ok: pull.ok,
|
|
60
|
-
push,
|
|
61
|
-
pull,
|
|
62
|
-
verification: verify.summary,
|
|
63
|
-
committed: [...push.committed, ...pull.committed],
|
|
64
|
-
failures: [...push.failures, ...pull.failures],
|
|
65
|
-
};
|
|
66
|
-
}
|
package/src/host/sync/cli.ts
DELETED
|
@@ -1,530 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* dsh-maestro-sync CLI — validated, preview-first wrapper for sync operations.
|
|
4
|
-
*
|
|
5
|
-
* node lib/cli.js --pull [--dry-run] -> preview only (default)
|
|
6
|
-
* node lib/cli.js --pull --apply --preview-id ID --confirm -> apply a preview
|
|
7
|
-
*
|
|
8
|
-
* No omitted boolean can apply a sync: `--apply` requires BOTH `--preview-id`
|
|
9
|
-
* and `--confirm`. `--strategy=override` is a destructive escape hatch and
|
|
10
|
-
* requires a separate `--ack-override` flag. The CLI emits one final JSON
|
|
11
|
-
* result to stdout and human progress to stderr. Executable via:
|
|
12
|
-
* node lib/cli.js --pull --dry-run
|
|
13
|
-
*/
|
|
14
|
-
import * as path from 'node:path';
|
|
15
|
-
import * as os from 'node:os';
|
|
16
|
-
import { DEFAULT_REMOTE_HOST, loadSyncConfig, type SyncConfig } from './config.js';
|
|
17
|
-
import { validateHost } from './validation.js';
|
|
18
|
-
import { SyncService } from './sync-service.js';
|
|
19
|
-
import { runBidirectionalApply, runBidirectionalPreview } from './bidirectional.js';
|
|
20
|
-
import type { SyncDirection, SyncScope } from './sync-types.js';
|
|
21
|
-
import { checkMachines, isMachineId, peerMachineId, readLocalMachineId, type MachineMode } from './machine-id.js';
|
|
22
|
-
import { restoreLocalTunnel, restoreRemoteTunnel } from './tunnel-restore.js';
|
|
23
|
-
import { clearPeerHost, writePeerHost } from './peer-host.js';
|
|
24
|
-
import { NodeProcessRunner } from './process-runner.js';
|
|
25
|
-
import { SshRsyncTransport } from './transport.js';
|
|
26
|
-
|
|
27
|
-
interface CliDeps {
|
|
28
|
-
stdout?: (s: string) => void;
|
|
29
|
-
stderr?: (s: string) => void;
|
|
30
|
-
makeService?: (cfg: SyncConfig) => Promise<SyncService>;
|
|
31
|
-
/** Hermetic seam for the machine-identity reads (defaults hit real fs/ssh). */
|
|
32
|
-
identity?: {
|
|
33
|
-
readLocal?: (dshHome: string) => Promise<string | null>;
|
|
34
|
-
readRemote?: (host: string, remoteDsh: string) => Promise<string | null>;
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface CliOpts {
|
|
39
|
-
mode: 'pull' | 'push' | 'bidirectional' | null;
|
|
40
|
-
subcommand: 'check-machines' | 'tunnel-restore' | 'set-peer-host' | null;
|
|
41
|
-
dryRun: boolean;
|
|
42
|
-
hasApplyFlag: boolean;
|
|
43
|
-
applyPreviewId?: string;
|
|
44
|
-
confirm: boolean;
|
|
45
|
-
includeSessions: boolean;
|
|
46
|
-
from?: string;
|
|
47
|
-
to?: string;
|
|
48
|
-
side?: string;
|
|
49
|
-
profile?: string;
|
|
50
|
-
peerHost?: string;
|
|
51
|
-
clearPeer?: boolean;
|
|
52
|
-
localDsh?: string;
|
|
53
|
-
remote?: string;
|
|
54
|
-
remoteDsh?: string;
|
|
55
|
-
backupRoot?: string;
|
|
56
|
-
strategy: 'merge' | 'override';
|
|
57
|
-
ackOverride: boolean;
|
|
58
|
-
help: boolean;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function printHelp(prog: string): string {
|
|
62
|
-
return `
|
|
63
|
-
dsh-maestro-sync CLI — merge memory & sessions across machines
|
|
64
|
-
|
|
65
|
-
USAGE
|
|
66
|
-
node ${prog} --pull|--push [--dry-run] preview (default, read-only)
|
|
67
|
-
node ${prog} --pull|--push --apply --preview-id ID --confirm apply a preview
|
|
68
|
-
node ${prog} --bidirectional [--dry-run] [--include-sessions] push-then-pull in one operation
|
|
69
|
-
node ${prog} --bidirectional --apply --preview-id ID --confirm apply it
|
|
70
|
-
node ${prog} check-machines [--pull|--push|--bidirectional] [--from X --to Y]
|
|
71
|
-
node ${prog} tunnel-restore --side local|remote [--profile NAME] --confirm
|
|
72
|
-
node ${prog} set-peer-host --host HOST | --clear
|
|
73
|
-
|
|
74
|
-
OPTIONS
|
|
75
|
-
--pull pull merge: remote -> local
|
|
76
|
-
--push push merge: local -> remote
|
|
77
|
-
--bidirectional push then pull, one operation (merge strategy only)
|
|
78
|
-
--include-sessions with --bidirectional: also sync sessions/ (default: memories only)
|
|
79
|
-
--from/--to <id> absolute machines, by the id each side wrote to its
|
|
80
|
-
$DSH_HOME/machine-id; identity is enforced before
|
|
81
|
-
any preview/apply (from defaults to the local
|
|
82
|
-
machine-id, to defaults to its peer)
|
|
83
|
-
--dry-run, -n preview only (default); never writes
|
|
84
|
-
--apply apply a previous preview — REQUIRES --preview-id and --confirm
|
|
85
|
-
--preview-id <id> preview id returned by --dry-run
|
|
86
|
-
--confirm explicit confirmation that the preview may be applied
|
|
87
|
-
--local-dsh <path> local DSH home (default: DSH_HOME or ~/.dsh)
|
|
88
|
-
--host <host> with set-peer-host: this machine's peer ssh target
|
|
89
|
-
--clear with set-peer-host: drop the machine-local peer
|
|
90
|
-
--remote <host> ssh remote host (default: from config/REMOTE_HOST)
|
|
91
|
-
--remote-dsh <path> remote DSH path; must be absolute (e.g. /home/user/.dsh);
|
|
92
|
-
a '~/.dsh' default is resolved to an absolute remote home
|
|
93
|
-
path by the transport preflight, never shell ~ expansion
|
|
94
|
-
--strategy <merge|override> merge (default): union dedup, no --delete.
|
|
95
|
-
override: destructive rsync --delete mirror — REQUIRES --ack-override
|
|
96
|
-
--ack-override acknowledge that --strategy=override is destructive
|
|
97
|
-
--backup-root <path> accepted for compatibility; DSH publishes .bak.<ts> beside each file
|
|
98
|
-
--side <local|remote> with tunnel-restore: which side to restore (default local)
|
|
99
|
-
--profile <name> with tunnel-restore: profile name (required for remote)
|
|
100
|
-
--help, -h this help
|
|
101
|
-
|
|
102
|
-
EXIT CODES
|
|
103
|
-
0 success (including a dry-run preview), 1 error / apply partial failure
|
|
104
|
-
`.trim();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function parseArgs(argv: string[], err: (s: string) => void): CliOpts | null {
|
|
108
|
-
const opts: CliOpts = {
|
|
109
|
-
mode: null,
|
|
110
|
-
subcommand: null,
|
|
111
|
-
dryRun: true,
|
|
112
|
-
hasApplyFlag: false,
|
|
113
|
-
confirm: false,
|
|
114
|
-
includeSessions: false,
|
|
115
|
-
strategy: 'merge',
|
|
116
|
-
ackOverride: false,
|
|
117
|
-
help: false,
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const fail = (msg: string): CliOpts | null => {
|
|
121
|
-
err(`[err] ${msg}`);
|
|
122
|
-
return null;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
// Subcommands come first (check-machines, tunnel-restore); the rest are flags.
|
|
126
|
-
const rest = [...argv];
|
|
127
|
-
if (rest.length > 0 && (rest[0] === 'check-machines' || rest[0] === 'tunnel-restore' || rest[0] === 'set-peer-host')) {
|
|
128
|
-
opts.subcommand = rest.shift() as 'check-machines' | 'tunnel-restore' | 'set-peer-host';
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
for (let i = 0; i < rest.length; i++) {
|
|
132
|
-
const a = rest[i]!;
|
|
133
|
-
if (a === '--pull' || a === '--push' || a === '--bidirectional') {
|
|
134
|
-
if (opts.mode) return fail('Specify only one of --pull, --push or --bidirectional');
|
|
135
|
-
opts.mode = a === '--pull' ? 'pull' : a === '--push' ? 'push' : 'bidirectional';
|
|
136
|
-
} else if (a === '--include-sessions') {
|
|
137
|
-
opts.includeSessions = true;
|
|
138
|
-
} else if (a === '--dry-run' || a === '-n') {
|
|
139
|
-
opts.dryRun = true;
|
|
140
|
-
} else if (a === '--apply') {
|
|
141
|
-
opts.hasApplyFlag = true;
|
|
142
|
-
opts.dryRun = false;
|
|
143
|
-
} else if (a === '--confirm') {
|
|
144
|
-
opts.confirm = true;
|
|
145
|
-
} else if (a === '--preview-id') {
|
|
146
|
-
const v = rest[++i];
|
|
147
|
-
if (!v || v.startsWith('-')) return fail('--preview-id requires a value');
|
|
148
|
-
opts.applyPreviewId = v;
|
|
149
|
-
} else if (a.startsWith('--preview-id=')) {
|
|
150
|
-
opts.applyPreviewId = a.slice('--preview-id='.length);
|
|
151
|
-
} else if (a === '--help' || a === '-h') {
|
|
152
|
-
opts.help = true;
|
|
153
|
-
} else if (a === '--local-dsh' || a === '--localDsh') {
|
|
154
|
-
const v = rest[++i];
|
|
155
|
-
if (!v || v.startsWith('-')) return fail('--local-dsh requires a path value');
|
|
156
|
-
opts.localDsh = v;
|
|
157
|
-
} else if (a === '--remote') {
|
|
158
|
-
const v = rest[++i];
|
|
159
|
-
if (!v || v.startsWith('-')) return fail('--remote requires a host value');
|
|
160
|
-
opts.remote = v;
|
|
161
|
-
} else if (a.startsWith('--remote=')) {
|
|
162
|
-
opts.remote = a.slice('--remote='.length);
|
|
163
|
-
} else if (a === '--remote-dsh' || a === '--remoteDsh') {
|
|
164
|
-
const v = rest[++i];
|
|
165
|
-
if (!v || v.startsWith('-')) return fail('--remote-dsh requires a path value');
|
|
166
|
-
opts.remoteDsh = v;
|
|
167
|
-
} else if (a.startsWith('--remote-dsh=')) {
|
|
168
|
-
opts.remoteDsh = a.slice('--remote-dsh='.length);
|
|
169
|
-
} else if (a === '--backup-root' || a === '--backupRoot') {
|
|
170
|
-
const v = rest[++i];
|
|
171
|
-
if (!v || v.startsWith('-')) return fail('--backup-root requires a path value');
|
|
172
|
-
opts.backupRoot = v;
|
|
173
|
-
} else if (a === '--strategy') {
|
|
174
|
-
const v = rest[++i];
|
|
175
|
-
if (!v || (v !== 'merge' && v !== 'override')) return fail('--strategy must be merge or override');
|
|
176
|
-
opts.strategy = v;
|
|
177
|
-
} else if (a.startsWith('--strategy=')) {
|
|
178
|
-
const v = a.slice('--strategy='.length);
|
|
179
|
-
if (v !== 'merge' && v !== 'override') return fail('--strategy must be merge or override');
|
|
180
|
-
opts.strategy = v;
|
|
181
|
-
} else if (a === '--ack-override') {
|
|
182
|
-
opts.ackOverride = true;
|
|
183
|
-
} else if (a === '--from') {
|
|
184
|
-
const v = rest[++i];
|
|
185
|
-
if (!v || v.startsWith('-')) return fail('--from requires a value (a machine id)');
|
|
186
|
-
opts.from = v;
|
|
187
|
-
} else if (a.startsWith('--from=')) {
|
|
188
|
-
opts.from = a.slice('--from='.length);
|
|
189
|
-
} else if (a === '--to') {
|
|
190
|
-
const v = rest[++i];
|
|
191
|
-
if (!v || v.startsWith('-')) return fail('--to requires a value (a machine id)');
|
|
192
|
-
opts.to = v;
|
|
193
|
-
} else if (a.startsWith('--to=')) {
|
|
194
|
-
opts.to = a.slice('--to='.length);
|
|
195
|
-
} else if (a === '--side') {
|
|
196
|
-
const v = rest[++i];
|
|
197
|
-
if (!v || v.startsWith('-')) return fail('--side requires local or remote');
|
|
198
|
-
opts.side = v;
|
|
199
|
-
} else if (a.startsWith('--side=')) {
|
|
200
|
-
opts.side = a.slice('--side='.length);
|
|
201
|
-
} else if (a === '--host') {
|
|
202
|
-
const v = rest[++i];
|
|
203
|
-
// A host, never a flag: `user@host` and bare hostnames are both legal.
|
|
204
|
-
if (!v || v.startsWith('--')) return fail('--host requires a value');
|
|
205
|
-
opts.peerHost = v;
|
|
206
|
-
} else if (a.startsWith('--host=')) {
|
|
207
|
-
opts.peerHost = a.slice('--host='.length);
|
|
208
|
-
} else if (a === '--clear') {
|
|
209
|
-
opts.clearPeer = true;
|
|
210
|
-
} else if (a === '--profile') {
|
|
211
|
-
const v = rest[++i];
|
|
212
|
-
if (!v || v.startsWith('-')) return fail('--profile requires a value');
|
|
213
|
-
opts.profile = v;
|
|
214
|
-
} else if (a.startsWith('--profile=')) {
|
|
215
|
-
opts.profile = a.slice('--profile='.length);
|
|
216
|
-
} else if (a === '--') {
|
|
217
|
-
break;
|
|
218
|
-
} else if (a.startsWith('-')) {
|
|
219
|
-
return fail(`Unknown option: ${a} (see --help)`);
|
|
220
|
-
} else {
|
|
221
|
-
return fail(`Unexpected arg: ${a} (see --help)`);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (opts.strategy === 'override' && !opts.ackOverride) {
|
|
226
|
-
return fail('--strategy=override is destructive and requires --ack-override');
|
|
227
|
-
}
|
|
228
|
-
if (opts.mode === 'bidirectional' && opts.strategy === 'override') {
|
|
229
|
-
return fail('--bidirectional requires the merge strategy (override is a one-direction destructive mirror)');
|
|
230
|
-
}
|
|
231
|
-
if (opts.from !== undefined && !isMachineId(opts.from)) return fail('--from must be a machine id (letters, digits, . _ -)');
|
|
232
|
-
if (opts.to !== undefined && !isMachineId(opts.to)) return fail('--to must be a machine id (letters, digits, . _ -)');
|
|
233
|
-
if (opts.subcommand === 'set-peer-host') {
|
|
234
|
-
if (opts.mode) return fail('set-peer-host takes no --pull/--push/--bidirectional flag');
|
|
235
|
-
if (opts.clearPeer === true && opts.peerHost !== undefined) return fail('set-peer-host takes either --host or --clear, not both');
|
|
236
|
-
if (opts.clearPeer !== true && !opts.peerHost) return fail('set-peer-host requires --host <host> or --clear');
|
|
237
|
-
}
|
|
238
|
-
if (opts.subcommand === 'tunnel-restore') {
|
|
239
|
-
if (opts.mode) return fail('tunnel-restore takes no --pull/--push/--bidirectional flag');
|
|
240
|
-
const side = opts.side ?? 'local';
|
|
241
|
-
if (side !== 'local' && side !== 'remote') return fail('--side must be local or remote');
|
|
242
|
-
if (opts.confirm !== true) return fail('tunnel-restore requires --confirm');
|
|
243
|
-
if (side === 'remote' && !opts.profile) return fail('tunnel-restore --side remote requires --profile <name>');
|
|
244
|
-
}
|
|
245
|
-
if (opts.hasApplyFlag) {
|
|
246
|
-
if (!opts.applyPreviewId) return fail('--apply requires --preview-id <id> from a previous --dry-run preview');
|
|
247
|
-
if (opts.confirm !== true) return fail('--apply requires --confirm');
|
|
248
|
-
}
|
|
249
|
-
return opts;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
async function runBidirectional(
|
|
253
|
-
svc: SyncService,
|
|
254
|
-
opts: CliOpts,
|
|
255
|
-
out: (s: string) => void,
|
|
256
|
-
err: (s: string) => void,
|
|
257
|
-
): Promise<number> {
|
|
258
|
-
const scope: SyncScope = opts.includeSessions ? 'all' : 'memory';
|
|
259
|
-
if (opts.hasApplyFlag) {
|
|
260
|
-
err(`[sync] apply bidirectional preview ${opts.applyPreviewId} (scope ${scope})`);
|
|
261
|
-
let result;
|
|
262
|
-
try {
|
|
263
|
-
result = await runBidirectionalApply(svc, { previewId: opts.applyPreviewId!, confirm: true, scope });
|
|
264
|
-
} catch (e: any) {
|
|
265
|
-
out(JSON.stringify({ ok: false, error: e?.message ?? String(e), code: e?.code, phase: e?.phase }) + '\n');
|
|
266
|
-
err(`[err] bidirectional apply failed: ${e?.message ?? String(e)}`);
|
|
267
|
-
return 1;
|
|
268
|
-
}
|
|
269
|
-
out(JSON.stringify({ ok: result.ok, push: result.push, pull: result.pull, verification: result.verification, committed: result.committed, failures: result.failures }) + '\n');
|
|
270
|
-
if (!result.ok) {
|
|
271
|
-
err(`[err] bidirectional partially failed: ${result.failures.map((f: any) => f.path ?? f.code).join(',')}`);
|
|
272
|
-
return 1;
|
|
273
|
-
}
|
|
274
|
-
err(`[sync] bidirectional applied: committed ${result.committed.length}, failures ${result.failures.length}`);
|
|
275
|
-
return 0;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// default: dry-run combined preview (read-only). The pull plan is projected:
|
|
279
|
-
// it is recomputed exact after the push lands at apply time.
|
|
280
|
-
err(`[sync] preview bidirectional (dry-run, scope ${scope})`);
|
|
281
|
-
let combined;
|
|
282
|
-
try {
|
|
283
|
-
combined = await runBidirectionalPreview(svc, { scope });
|
|
284
|
-
} catch (e: any) {
|
|
285
|
-
out(JSON.stringify({ ok: false, error: e?.message ?? String(e), code: e?.code, phase: e?.phase }) + '\n');
|
|
286
|
-
err(`[err] bidirectional preview failed: ${e?.message ?? String(e)}`);
|
|
287
|
-
return 1;
|
|
288
|
-
}
|
|
289
|
-
out(
|
|
290
|
-
JSON.stringify({
|
|
291
|
-
ok: true,
|
|
292
|
-
previewId: combined.previewId,
|
|
293
|
-
expiresAt: combined.expiresAt,
|
|
294
|
-
push: { summary: combined.push.summary, actions: combined.push.actions },
|
|
295
|
-
pullProjected: { summary: combined.pullProjected.summary, actions: combined.pullProjected.actions },
|
|
296
|
-
note: 'pull plan is projected; recomputed exact at apply time',
|
|
297
|
-
}) + '\n',
|
|
298
|
-
);
|
|
299
|
-
err(`[sync] bidirectional preview ${combined.previewId}: push merged=${combined.push.summary.merged} copied=${combined.push.summary.copied} / pull merged=${combined.pullProjected.summary.merged} copied=${combined.pullProjected.summary.copied}`);
|
|
300
|
-
return 0;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/** Resolve a `~/.dsh`-style placeholder to an absolute remote path (read-only preflight). */
|
|
304
|
-
async function resolveAbsoluteRemoteDsh(transport: SshRsyncTransport, host: string, remoteDsh: string): Promise<string> {
|
|
305
|
-
if (remoteDsh !== '~/.dsh' && !remoteDsh.startsWith('~/')) return remoteDsh;
|
|
306
|
-
const home = await transport.remoteHome({ host });
|
|
307
|
-
return home + remoteDsh.slice(1);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/** Default remote machine-id read: agent op over the existing ssh transport (no new binary). */
|
|
311
|
-
async function defaultReadRemoteId(host: string, remoteDsh: string): Promise<string | null> {
|
|
312
|
-
try {
|
|
313
|
-
const transport = new SshRsyncTransport(new NodeProcessRunner());
|
|
314
|
-
const root = await resolveAbsoluteRemoteDsh(transport, host, remoteDsh);
|
|
315
|
-
return await transport.readMachineId({ host, dshRoot: root });
|
|
316
|
-
} catch {
|
|
317
|
-
return null;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
export async function runCli(argv: string[], deps: CliDeps = {}): Promise<number> {
|
|
322
|
-
const out = deps.stdout ?? ((s: string) => process.stdout.write(s));
|
|
323
|
-
const err = deps.stderr ?? ((s: string) => process.stderr.write(s));
|
|
324
|
-
|
|
325
|
-
const opts = parseArgs(argv, err);
|
|
326
|
-
if (!opts) return 1;
|
|
327
|
-
if (opts.help) {
|
|
328
|
-
out(printHelp(path.basename(process.argv[1] || 'cli.js')) + '\n');
|
|
329
|
-
return 0;
|
|
330
|
-
}
|
|
331
|
-
if (!opts.mode && !opts.subcommand) {
|
|
332
|
-
err('[err] Missing required --pull, --push, --bidirectional, check-machines or tunnel-restore (see --help)');
|
|
333
|
-
return 1;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const resolvedLocalDsh = opts.localDsh ?? process.env.DSH_HOME ?? path.join(os.homedir(), '.dsh');
|
|
337
|
-
let cfg: SyncConfig;
|
|
338
|
-
try {
|
|
339
|
-
cfg = await loadSyncConfig({ dshHome: resolvedLocalDsh });
|
|
340
|
-
} catch {
|
|
341
|
-
cfg = { remoteHost: process.env.REMOTE_HOST || DEFAULT_REMOTE_HOST, remoteDshPath: '~/.dsh', strategy: 'merge' };
|
|
342
|
-
}
|
|
343
|
-
const resolvedRemote = opts.remote ?? cfg.remoteHost;
|
|
344
|
-
try {
|
|
345
|
-
validateHost(resolvedRemote);
|
|
346
|
-
} catch (e: any) {
|
|
347
|
-
err(`[err] invalid --remote host: ${e?.message ?? String(e)}`);
|
|
348
|
-
return 1;
|
|
349
|
-
}
|
|
350
|
-
const resolvedRemoteDsh = opts.remoteDsh ?? cfg.remoteDshPath;
|
|
351
|
-
|
|
352
|
-
// Machine identity: resolved before any service/preview/apply work.
|
|
353
|
-
const readLocal = deps.identity?.readLocal ?? ((home: string) => readLocalMachineId(undefined, home));
|
|
354
|
-
const readRemote = deps.identity?.readRemote ?? defaultReadRemoteId;
|
|
355
|
-
/**
|
|
356
|
-
* --from/--to, defaulted from the ids actually read off the two machines
|
|
357
|
-
* (`$DSH_HOME/machine-id`); an underivable peer is an error, never a guess.
|
|
358
|
-
*/
|
|
359
|
-
const resolveMachines = (
|
|
360
|
-
localId: string | null,
|
|
361
|
-
remoteId: string | null,
|
|
362
|
-
o: { from?: string; to?: string },
|
|
363
|
-
): { from: string; to: string } | { error: string } => {
|
|
364
|
-
const from = o.from ?? localId ?? undefined;
|
|
365
|
-
if (from === undefined) return { error: 'cannot read the local machine-id — pass --from <id>' };
|
|
366
|
-
const to = o.to ?? peerMachineId(from, localId, remoteId) ?? undefined;
|
|
367
|
-
if (to === undefined) return { error: 'cannot derive --to from the machine ids — pass --to <id>' };
|
|
368
|
-
return { from, to };
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
if (opts.subcommand === 'check-machines') {
|
|
372
|
-
const mode: MachineMode = opts.mode ?? 'bidirectional';
|
|
373
|
-
const localId = await readLocal(resolvedLocalDsh);
|
|
374
|
-
const remoteId = await readRemote(resolvedRemote, resolvedRemoteDsh);
|
|
375
|
-
const machines = resolveMachines(localId, remoteId, opts);
|
|
376
|
-
if ('error' in machines) {
|
|
377
|
-
err(`[err] ${machines.error}\n`);
|
|
378
|
-
return 1;
|
|
379
|
-
}
|
|
380
|
-
const { from, to } = machines;
|
|
381
|
-
const verdict = checkMachines({ mode, from, to, localId, remoteId });
|
|
382
|
-
if (!verdict.ok) {
|
|
383
|
-
out(JSON.stringify({ ok: false, code: verdict.code, error: verdict.message, localId, remoteId, from, to }) + '\n');
|
|
384
|
-
err(`[err] ${verdict.message}`);
|
|
385
|
-
return 1;
|
|
386
|
-
}
|
|
387
|
-
out(JSON.stringify({ ok: true, mode, localId, remoteId, from, to }) + '\n');
|
|
388
|
-
err(`[sync] machines ok: local=${localId ?? 'unknown'} remote=${remoteId ?? 'unknown'} from=${from} to=${to}`);
|
|
389
|
-
return 0;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (opts.subcommand === 'tunnel-restore') {
|
|
393
|
-
const side = opts.side ?? 'local';
|
|
394
|
-
if (side === 'local') {
|
|
395
|
-
err(`[sync] tunnel-restore local (profile ${opts.profile ?? 'auto'})`);
|
|
396
|
-
const r = await restoreLocalTunnel({ dshHome: resolvedLocalDsh, profileName: opts.profile });
|
|
397
|
-
out(JSON.stringify({ ok: r.ok, side, ...(r.ok ? { profile: r.profile } : { code: r.code }) }) + '\n');
|
|
398
|
-
if (!r.ok) {
|
|
399
|
-
err('[err] tunnel-restore local: no profile found');
|
|
400
|
-
return 1;
|
|
401
|
-
}
|
|
402
|
-
err(`[sync] tunnel restored from profile ${r.profile}`);
|
|
403
|
-
return 0;
|
|
404
|
-
}
|
|
405
|
-
// remote: explicit profile required (validated in parseArgs); caller ensures the agent.
|
|
406
|
-
err(`[sync] tunnel-restore remote (profile ${opts.profile})`);
|
|
407
|
-
try {
|
|
408
|
-
const runner = new NodeProcessRunner();
|
|
409
|
-
const transport = new SshRsyncTransport(runner);
|
|
410
|
-
await transport.ensureAgent({ host: resolvedRemote, dshRoot: await resolveAbsoluteRemoteDsh(transport, resolvedRemote, resolvedRemoteDsh) });
|
|
411
|
-
const r = await restoreRemoteTunnel(transport, { host: resolvedRemote, dshRoot: await resolveAbsoluteRemoteDsh(transport, resolvedRemote, resolvedRemoteDsh) }, opts.profile!);
|
|
412
|
-
if (!r.ok) {
|
|
413
|
-
out(JSON.stringify({ ok: false, side, code: r.code }) + '\n');
|
|
414
|
-
err(`[err] tunnel-restore remote: ${r.code}`);
|
|
415
|
-
return 1;
|
|
416
|
-
}
|
|
417
|
-
out(JSON.stringify({ ok: true, side, profile: r.profile, changed: r.changed, sha256: r.sha256 }) + '\n');
|
|
418
|
-
err(`[sync] tunnel restored on remote from profile ${r.profile} (changed=${r.changed})`);
|
|
419
|
-
return 0;
|
|
420
|
-
} catch (e: any) {
|
|
421
|
-
out(JSON.stringify({ ok: false, side, error: e?.message ?? String(e), code: e?.code }) + '\n');
|
|
422
|
-
err(`[err] tunnel-restore remote failed: ${e?.message ?? String(e)}`);
|
|
423
|
-
return 1;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
if (opts.subcommand === 'set-peer-host') {
|
|
428
|
-
try {
|
|
429
|
-
if (opts.clearPeer === true) {
|
|
430
|
-
const removed = clearPeerHost(resolvedLocalDsh);
|
|
431
|
-
out(JSON.stringify({ ok: true, action: 'clear', removed, dshHome: resolvedLocalDsh }) + '\n');
|
|
432
|
-
err(`[sync] machine-local peer cleared (${removed ? 'removed' : 'nothing to remove'})`);
|
|
433
|
-
} else {
|
|
434
|
-
const host = writePeerHost(resolvedLocalDsh, opts.peerHost!);
|
|
435
|
-
out(JSON.stringify({ ok: true, action: 'set', remoteHost: host, dshHome: resolvedLocalDsh }) + '\n');
|
|
436
|
-
err(`[sync] this machine's peer recorded: ${host}`);
|
|
437
|
-
}
|
|
438
|
-
return 0;
|
|
439
|
-
} catch (e: any) {
|
|
440
|
-
out(JSON.stringify({ ok: false, error: e?.message ?? String(e) }) + '\n');
|
|
441
|
-
err(`[err] set-peer-host failed: ${e?.message ?? String(e)}`);
|
|
442
|
-
return 1;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// Mode flows: enforce identity before constructing the service.
|
|
447
|
-
// (Subcommands return above, so a mode is always present here.)
|
|
448
|
-
if (!opts.mode) {
|
|
449
|
-
err('[err] Missing required --pull, --push or --bidirectional (see --help)');
|
|
450
|
-
return 1;
|
|
451
|
-
}
|
|
452
|
-
{
|
|
453
|
-
const mode: MachineMode = opts.mode;
|
|
454
|
-
const localId = await readLocal(resolvedLocalDsh);
|
|
455
|
-
const remoteId = await readRemote(resolvedRemote, resolvedRemoteDsh);
|
|
456
|
-
const machines = resolveMachines(localId, remoteId, opts);
|
|
457
|
-
if ('error' in machines) {
|
|
458
|
-
err(`[err] ${machines.error}\n`);
|
|
459
|
-
return 1;
|
|
460
|
-
}
|
|
461
|
-
const { from, to } = machines;
|
|
462
|
-
const verdict = checkMachines({ mode, from, to, localId, remoteId });
|
|
463
|
-
if (!verdict.ok) {
|
|
464
|
-
err(`[err] ${verdict.message}`);
|
|
465
|
-
return 1;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
let svc: SyncService;
|
|
470
|
-
if (deps.makeService) {
|
|
471
|
-
svc = await deps.makeService({ ...cfg, remoteHost: resolvedRemote, remoteDshPath: resolvedRemoteDsh, strategy: opts.strategy });
|
|
472
|
-
} else {
|
|
473
|
-
svc = new SyncService({ localDsh: resolvedLocalDsh, remote: resolvedRemote, remoteDsh: resolvedRemoteDsh });
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
if (opts.mode === 'bidirectional') {
|
|
477
|
-
return runBidirectional(svc, opts, out, err);
|
|
478
|
-
}
|
|
479
|
-
const direction: SyncDirection = opts.mode;
|
|
480
|
-
|
|
481
|
-
if (opts.hasApplyFlag) {
|
|
482
|
-
err(`[sync] apply ${direction} preview ${opts.applyPreviewId} (strategy ${opts.strategy})`);
|
|
483
|
-
let result;
|
|
484
|
-
try {
|
|
485
|
-
result = await svc.apply({ previewId: opts.applyPreviewId!, direction, confirm: true });
|
|
486
|
-
} catch (e: any) {
|
|
487
|
-
out(JSON.stringify({ ok: false, error: e?.message ?? String(e), code: e?.code, phase: e?.phase }) + '\n');
|
|
488
|
-
err(`[err] apply failed: ${e?.message ?? String(e)}`);
|
|
489
|
-
return 1;
|
|
490
|
-
}
|
|
491
|
-
out(JSON.stringify({ ok: result.ok, revision: result.revision, summary: result.summary, committed: result.committed, failures: result.failures }) + '\n');
|
|
492
|
-
if (!result.ok) {
|
|
493
|
-
err(`[err] apply partially failed: ${result.failures.map((f: any) => f.path).join(',')}`);
|
|
494
|
-
return 1;
|
|
495
|
-
}
|
|
496
|
-
err(`[sync] applied: committed ${result.committed.length}, failures ${result.failures.length}`);
|
|
497
|
-
return 0;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
// default: dry-run preview (read-only)
|
|
501
|
-
err(`[sync] preview ${direction} (dry-run, strategy ${opts.strategy}) localDsh=${resolvedLocalDsh} remote=${resolvedRemote} remoteDsh=${resolvedRemoteDsh}`);
|
|
502
|
-
let preview;
|
|
503
|
-
try {
|
|
504
|
-
preview = await svc.preview({ direction });
|
|
505
|
-
} catch (e: any) {
|
|
506
|
-
out(JSON.stringify({ ok: false, error: e?.message ?? String(e), code: e?.code, phase: e?.phase }) + '\n');
|
|
507
|
-
err(`[err] preview failed: ${e?.message ?? String(e)}`);
|
|
508
|
-
return 1;
|
|
509
|
-
}
|
|
510
|
-
out(JSON.stringify({ ok: true, previewId: preview.previewId, revision: preview.revision, expiresAt: preview.expiresAt, summary: preview.summary, actions: preview.actions }) + '\n');
|
|
511
|
-
err(`[sync] preview ${preview.previewId}: copied=${preview.summary.copied} merged=${preview.summary.merged} skipped=${preview.summary.skipped} conflicts=${preview.summary.conflicts} added=${preview.summary.added}`);
|
|
512
|
-
return 0;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
async function main(): Promise<void> {
|
|
516
|
-
const code = await runCli(process.argv.slice(2));
|
|
517
|
-
process.exitCode = code;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
const isMain =
|
|
521
|
-
import.meta.url === `file://${process.argv[1]}` ||
|
|
522
|
-
process.argv[1]?.endsWith('/cli.js') ||
|
|
523
|
-
process.argv[1]?.endsWith('/cli.mjs') ||
|
|
524
|
-
process.argv[1]?.endsWith('lib/cli.js');
|
|
525
|
-
if (isMain) {
|
|
526
|
-
main().catch((e) => {
|
|
527
|
-
console.error('[err] unhandled:', e?.message || String(e));
|
|
528
|
-
process.exitCode = 1;
|
|
529
|
-
});
|
|
530
|
-
}
|