@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,90 @@
|
|
|
1
|
+
import { load } from '../store/index.js';
|
|
2
|
+
import { validateRemoteTarget } from './validation.js';
|
|
3
|
+
import { readPeerHost } from './peer-host.js';
|
|
4
|
+
/** The built-in SSH target, reached only when nothing was ever configured. */
|
|
5
|
+
export const DEFAULT_REMOTE_HOST = 'kai@ssh.ddtcorex.com';
|
|
6
|
+
/**
|
|
7
|
+
* Effective peer host from the four sources, highest first:
|
|
8
|
+
*
|
|
9
|
+
* 1. This machine's own `peer.json` — machine-local truth (see peer-host.ts).
|
|
10
|
+
* 2. The shared settings store `domains.sync.remoteHost` — UI-editable, but it
|
|
11
|
+
* travels between machines, so on the mirrored machine it can name that
|
|
12
|
+
* machine instead of its peer.
|
|
13
|
+
* 3. `REMOTE_HOST` / `REMOTE` env.
|
|
14
|
+
* 4. {@link DEFAULT_REMOTE_HOST}.
|
|
15
|
+
*
|
|
16
|
+
* The machine-local file deliberately outranks the shared store: a machine that
|
|
17
|
+
* inherited the other machine's address must be able to state its own peer
|
|
18
|
+
* without editing a file both machines share. Env stays below the store because
|
|
19
|
+
* callers that need to force a target pass it explicitly (`--remote`), which
|
|
20
|
+
* wins over this whole list.
|
|
21
|
+
*/
|
|
22
|
+
export function resolveRemoteHost(sources) {
|
|
23
|
+
const first = [sources.machineLocal ?? undefined, sources.stored, sources.env].find((candidate) => typeof candidate === 'string' && candidate.trim().length > 0);
|
|
24
|
+
return first ?? sources.fallback ?? DEFAULT_REMOTE_HOST;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Load sync config via @ddtcorex/dsh-maestro-config-lib load() -> domains.sync
|
|
28
|
+
* Peer host precedence: see {@link resolveRemoteHost}.
|
|
29
|
+
* remoteDshPath = sync.remoteDshPath || REMOTE_DSH_PATH env || '~/.dsh' (unresolved placeholder)
|
|
30
|
+
* strategy = 'merge'
|
|
31
|
+
*
|
|
32
|
+
* NOTE: remoteDshPath may be '~/.dsh' as stored placeholder. It is NOT a validated
|
|
33
|
+
* absolute path. Caller must validate via `validateRemoteTarget` / `buildSyncRequest`
|
|
34
|
+
* before constructing a SyncRequest. The absolute remote home is resolved by the
|
|
35
|
+
* transport preflight (ssh remoteHome), not by shell ~ expansion.
|
|
36
|
+
*
|
|
37
|
+
* @param opts.dshHome DSH home holding this machine's peer file; the caller's
|
|
38
|
+
* own resolved home wins over the ambient default.
|
|
39
|
+
*/
|
|
40
|
+
export async function loadSyncConfig(opts = {}) {
|
|
41
|
+
const doc = await load({ dshHome: opts.dshHome });
|
|
42
|
+
const sync = doc.domains?.sync ?? {};
|
|
43
|
+
const remoteHost = resolveRemoteHost({
|
|
44
|
+
env: process.env.REMOTE_HOST || process.env.REMOTE,
|
|
45
|
+
machineLocal: readPeerHost(opts.dshHome),
|
|
46
|
+
stored: sync.remoteHost,
|
|
47
|
+
});
|
|
48
|
+
const remoteDshPath = sync.remoteDshPath || process.env.REMOTE_DSH_PATH || '~/.dsh';
|
|
49
|
+
const strategy = sync.strategy || 'merge';
|
|
50
|
+
return { remoteHost, remoteDshPath, strategy };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Validate and convert a SyncConfig's remote fields into a RemoteTarget.
|
|
54
|
+
* Throws if host or dshRoot is not validated absolute.
|
|
55
|
+
*/
|
|
56
|
+
export function configToRemoteTarget(config) {
|
|
57
|
+
return validateRemoteTarget({ host: config.remoteHost, dshRoot: config.remoteDshPath });
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Build a validated SyncRequest. Remote is validated via validateRemoteTarget;
|
|
61
|
+
* localRoot is expected to be an absolute local path (lightly validated as absolute).
|
|
62
|
+
* Throws on invalid host/path.
|
|
63
|
+
*/
|
|
64
|
+
export function buildSyncRequest(params) {
|
|
65
|
+
const remote = validateRemoteTarget({ host: params.remoteHost, dshRoot: params.remoteDshPath });
|
|
66
|
+
// localRoot should be absolute; reuse ABSOLUTE notion but allow local home paths
|
|
67
|
+
if (!params.localRoot || typeof params.localRoot !== 'string' || !params.localRoot.startsWith('/')) {
|
|
68
|
+
throw new Error(`invalid localRoot: must be absolute path: ${JSON.stringify(params.localRoot)}`);
|
|
69
|
+
}
|
|
70
|
+
if (params.localRoot.includes('\u0000') || params.localRoot.includes('\n')) {
|
|
71
|
+
throw new Error('invalid localRoot: contains control characters');
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
direction: params.direction,
|
|
75
|
+
dryRun: params.dryRun,
|
|
76
|
+
localRoot: params.localRoot,
|
|
77
|
+
remote,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Helper: resolve effective remote target from config + env + cli overrides,
|
|
82
|
+
* but do NOT auto-expand '~'. Caller must handle unresolved '~/.dsh' via
|
|
83
|
+
* transport preflight (remoteHome) before building SyncRequest.
|
|
84
|
+
*/
|
|
85
|
+
export function resolveRemoteTargetFromConfig(config, overrides) {
|
|
86
|
+
const host = overrides?.remoteHost ?? config.remoteHost;
|
|
87
|
+
const dshRoot = overrides?.remoteDshPath ?? config.remoteDshPath;
|
|
88
|
+
const needsPreflight = dshRoot === '~/.dsh' || dshRoot.startsWith('~/');
|
|
89
|
+
return { host, dshRoot, needsPreflight };
|
|
90
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface FpEntry {
|
|
2
|
+
dev: number;
|
|
3
|
+
ino: number;
|
|
4
|
+
size: number;
|
|
5
|
+
mtimeNs: string;
|
|
6
|
+
ctimeNs: string;
|
|
7
|
+
sha256: string;
|
|
8
|
+
}
|
|
9
|
+
export interface FingerprintIndex {
|
|
10
|
+
schema: 1;
|
|
11
|
+
rootKey: string;
|
|
12
|
+
entries: Record<string, FpEntry>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Stable stat fingerprint. Prefer stats from `statSync(p, { bigint: true })`;
|
|
16
|
+
* the mtime/ctime nanoseconds are then exact. Non-bigint stats fall back to an
|
|
17
|
+
* ms-derived approximation, which still changes on any write via ctime.
|
|
18
|
+
*/
|
|
19
|
+
export declare function statFingerprint(st: any): {
|
|
20
|
+
dev: number;
|
|
21
|
+
ino: number;
|
|
22
|
+
size: number;
|
|
23
|
+
mtimeNs: string;
|
|
24
|
+
ctimeNs: string;
|
|
25
|
+
};
|
|
26
|
+
export declare function matchesStat(entry: FpEntry, st: any): boolean;
|
|
27
|
+
export declare function loadIndex(dir: string): FingerprintIndex;
|
|
28
|
+
export declare function saveIndex(dir: string, index: FingerprintIndex): void;
|
|
29
|
+
export declare function probeIndex(rel: string, index: FingerprintIndex): FpEntry | null;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/host/fingerprint.ts — persistent stat->sha256 index (a pure speed hint).
|
|
2
|
+
//
|
|
3
|
+
// The cache NEVER proves equality on its own: entry.key is trusted only when
|
|
4
|
+
// dev+ino+size+mtimeNs+ctimeNs all match the current stat, and every file a
|
|
5
|
+
// plan will write is freshly read+hashed at write time (see sync-service).
|
|
6
|
+
// mtime+size alone is never an equality proof — a same-size rewrite preserves
|
|
7
|
+
// them; ctimeNs closes that gap (ctime always changes on a write).
|
|
8
|
+
import * as fsMod from 'node:fs';
|
|
9
|
+
import * as path from 'node:path';
|
|
10
|
+
import { randomBytes } from 'node:crypto';
|
|
11
|
+
const FILE = 'index.json';
|
|
12
|
+
function toNs(v, ms) {
|
|
13
|
+
if (v !== undefined && v !== null) {
|
|
14
|
+
try {
|
|
15
|
+
return BigInt(v);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// fall through to ms-derived approximation
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return BigInt(Math.trunc((typeof ms === 'number' && Number.isFinite(ms) ? ms : 0) * 1e6));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Stable stat fingerprint. Prefer stats from `statSync(p, { bigint: true })`;
|
|
25
|
+
* the mtime/ctime nanoseconds are then exact. Non-bigint stats fall back to an
|
|
26
|
+
* ms-derived approximation, which still changes on any write via ctime.
|
|
27
|
+
*/
|
|
28
|
+
export function statFingerprint(st) {
|
|
29
|
+
return {
|
|
30
|
+
dev: Number(st.dev ?? 0),
|
|
31
|
+
ino: Number(st.ino ?? 0),
|
|
32
|
+
size: Number(st.size ?? 0),
|
|
33
|
+
mtimeNs: String(toNs(st.mtimeNs, st.mtimeMs)),
|
|
34
|
+
ctimeNs: String(toNs(st.ctimeNs, st.ctimeMs)),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function matchesStat(entry, st) {
|
|
38
|
+
const k = statFingerprint(st);
|
|
39
|
+
return entry.dev === k.dev && entry.ino === k.ino && entry.size === k.size && entry.mtimeNs === k.mtimeNs && entry.ctimeNs === k.ctimeNs;
|
|
40
|
+
}
|
|
41
|
+
export function loadIndex(dir) {
|
|
42
|
+
try {
|
|
43
|
+
const raw = JSON.parse(fsMod.readFileSync(path.join(dir, FILE), 'utf-8'));
|
|
44
|
+
if (raw?.schema !== 1 || !raw.entries || typeof raw.rootKey !== 'string')
|
|
45
|
+
return { schema: 1, rootKey: '', entries: {} };
|
|
46
|
+
return { schema: 1, rootKey: raw.rootKey, entries: raw.entries };
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return { schema: 1, rootKey: '', entries: {} };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export function saveIndex(dir, index) {
|
|
53
|
+
fsMod.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
54
|
+
const target = path.join(dir, FILE);
|
|
55
|
+
const tmp = `${target}.tmp.${randomBytes(4).toString('hex')}`;
|
|
56
|
+
fsMod.writeFileSync(tmp, JSON.stringify(index), 'utf-8');
|
|
57
|
+
try {
|
|
58
|
+
fsMod.chmodSync(tmp, 0o600);
|
|
59
|
+
}
|
|
60
|
+
catch { }
|
|
61
|
+
fsMod.renameSync(tmp, target);
|
|
62
|
+
}
|
|
63
|
+
export function probeIndex(rel, index) {
|
|
64
|
+
return index.entries[rel] ?? null;
|
|
65
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface HashOutcome {
|
|
2
|
+
path: string;
|
|
3
|
+
sha256: string;
|
|
4
|
+
size: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function hashFiles(fs: any, root: string, paths: readonly string[], opts?: {
|
|
7
|
+
concurrency?: number;
|
|
8
|
+
onFile?: (h: HashOutcome) => void;
|
|
9
|
+
}): Promise<HashOutcome[]>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/host/hashing.ts — bounded streaming SHA-256 pool with deterministic order.
|
|
2
|
+
//
|
|
3
|
+
// Reads every file in ≤1 MiB chunks through createReadStream (never whole-file
|
|
4
|
+
// buffers), hashes with a bounded worker pool, and returns outcomes sorted by
|
|
5
|
+
// path so plan/revision output stays deterministic. Concurrency is capped by
|
|
6
|
+
// the caller's default of 4 and backpressure comes from the stream consumers.
|
|
7
|
+
import { createHash } from 'node:crypto';
|
|
8
|
+
import * as path from 'node:path';
|
|
9
|
+
const CHUNK = 1024 * 1024;
|
|
10
|
+
export async function hashFiles(fs, root, paths, opts = {}) {
|
|
11
|
+
const concurrency = Math.max(1, opts.concurrency ?? 4);
|
|
12
|
+
const results = [];
|
|
13
|
+
let next = 0;
|
|
14
|
+
const worker = async () => {
|
|
15
|
+
for (;;) {
|
|
16
|
+
const idx = next++;
|
|
17
|
+
if (idx >= paths.length)
|
|
18
|
+
return;
|
|
19
|
+
const rel = paths[idx];
|
|
20
|
+
const full = path.join(root, rel);
|
|
21
|
+
const h = createHash('sha256');
|
|
22
|
+
let size = 0;
|
|
23
|
+
if (typeof fs.createReadStream === 'function') {
|
|
24
|
+
await new Promise((resolve, reject) => {
|
|
25
|
+
const rs = fs.createReadStream(full, { highWaterMark: CHUNK });
|
|
26
|
+
rs.on('data', (chunk) => {
|
|
27
|
+
h.update(chunk);
|
|
28
|
+
size += chunk.length;
|
|
29
|
+
});
|
|
30
|
+
rs.on('end', resolve);
|
|
31
|
+
rs.on('error', reject);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const data = fs.readFileSync(full);
|
|
36
|
+
h.update(data);
|
|
37
|
+
size = data.length;
|
|
38
|
+
}
|
|
39
|
+
const outcome = { path: rel, sha256: h.digest('hex'), size };
|
|
40
|
+
results.push(outcome);
|
|
41
|
+
opts.onFile?.(outcome);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const workers = Array.from({ length: Math.min(concurrency, paths.length) }, () => worker());
|
|
45
|
+
await Promise.all(workers);
|
|
46
|
+
return results.sort((a, b) => a.path.localeCompare(b.path));
|
|
47
|
+
}
|