@ddtcorex/dsh-maestro-core 0.11.2 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +18 -1
  2. package/cordis.patch.yml +4 -4
  3. package/lib/client.js +5 -29
  4. package/lib/guard/config.d.ts +3 -43
  5. package/lib/guard/config.js +3 -95
  6. package/lib/guard/index.d.ts +0 -17
  7. package/lib/guard/index.js +4 -54
  8. package/lib/guard/journal.js +11 -1
  9. package/lib/guard/paths.d.ts +0 -6
  10. package/lib/guard/paths.js +0 -12
  11. package/lib/guard/rules.js +1 -3
  12. package/lib/health-poller.d.ts +0 -2
  13. package/lib/health-poller.js +0 -4
  14. package/lib/notifier.d.ts +0 -8
  15. package/lib/notifier.js +0 -19
  16. package/lib/paths.d.ts +2 -3
  17. package/lib/paths.js +3 -8
  18. package/lib/plugin.d.ts +0 -1
  19. package/lib/plugin.js +37 -29
  20. package/lib/preset.js +1 -1
  21. package/lib/restart-exec.d.ts +0 -1
  22. package/lib/restart-exec.js +0 -1
  23. package/lib/restart-guards.d.ts +1 -1
  24. package/lib/restart-guards.js +3 -33
  25. package/lib/self-kill-guard.d.ts +0 -2
  26. package/lib/self-kill-guard.js +0 -4
  27. package/lib/supervisor.d.ts +0 -1
  28. package/lib/supervisor.js +0 -9
  29. package/lib/sync/cli.js +0 -8
  30. package/lib/sync/config.d.ts +1 -19
  31. package/lib/sync/config.js +0 -18
  32. package/lib/sync/index.js +2 -17
  33. package/lib/sync/merge.d.ts +0 -2
  34. package/lib/sync/merge.js +0 -2
  35. package/lib/sync/remote-agent.d.ts +0 -1
  36. package/lib/sync/remote-agent.js +0 -1
  37. package/lib/sync/snapshot.d.ts +0 -5
  38. package/lib/sync/snapshot.js +0 -33
  39. package/lib/sync/sync-service.d.ts +0 -20
  40. package/lib/sync/sync-service.js +0 -13
  41. package/lib/sync/transport.d.ts +1 -1
  42. package/lib/sync/transport.js +0 -4
  43. package/lib/types/client/config/MaestroSettings.d.ts.map +1 -1
  44. package/lib/types/client/config/components/BrandMark.d.ts +5 -2
  45. package/lib/types/client/config/components/BrandMark.d.ts.map +1 -1
  46. package/lib/types/client/config/guard-view.d.ts +7 -10
  47. package/lib/types/client/config/guard-view.d.ts.map +1 -1
  48. package/lib/types/client/sync/ui.d.ts +0 -4
  49. package/lib/types/client/sync/ui.d.ts.map +1 -1
  50. package/package.json +5 -2
  51. package/skills/dsh-safe-restart/SKILL.md +1 -1
  52. package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +6 -5
  53. package/src/client/config/MaestroSettings.tsx +4 -9
  54. package/src/client/config/components/BrandMark.tsx +5 -2
  55. package/src/client/config/guard-view.ts +10 -37
  56. package/src/client/sync/ui.tsx +1 -17
  57. package/src/host/guard/config.ts +4 -107
  58. package/src/host/guard/index.ts +4 -53
  59. package/src/host/guard/journal.ts +9 -1
  60. package/src/host/guard/paths.ts +0 -11
  61. package/src/host/guard/rules.ts +1 -3
  62. package/src/host/health-poller.ts +0 -5
  63. package/src/host/notifier.ts +0 -26
  64. package/src/host/paths.ts +3 -7
  65. package/src/host/plugin.ts +34 -28
  66. package/src/host/preset.ts +1 -1
  67. package/src/host/restart-exec.ts +0 -2
  68. package/src/host/restart-guards.ts +3 -27
  69. package/src/host/self-kill-guard.ts +0 -5
  70. package/src/host/supervisor.ts +0 -8
  71. package/src/host/sync/backup-config.ts +0 -1
  72. package/src/host/sync/cli.ts +0 -9
  73. package/src/host/sync/config.ts +1 -24
  74. package/src/host/sync/index.ts +1 -41
  75. package/src/host/sync/merge.ts +0 -3
  76. package/src/host/sync/remote-agent.ts +0 -1
  77. package/src/host/sync/snapshot.ts +0 -30
  78. package/src/host/sync/ssh-mux.ts +0 -1
  79. package/src/host/sync/sync-service.ts +2 -32
  80. package/src/host/sync/transport.ts +2 -7
  81. package/lib/guard/migrate.d.ts +0 -15
  82. package/lib/guard/migrate.js +0 -42
  83. package/lib/sync/session-merge.d.ts +0 -18
  84. package/lib/sync/session-merge.js +0 -191
  85. package/scripts/sync/bench-preview.mjs +0 -43
  86. package/scripts/sync/cdp-sync-probe.mjs +0 -200
  87. package/scripts/sync/cdp-sync-verify.mjs +0 -169
  88. package/src/host/guard/migrate.ts +0 -41
  89. package/src/host/sync/session-merge.ts +0 -220
@@ -1,220 +0,0 @@
1
- /**
2
- * Session Zstandard line union. DSH session artifacts require the first
3
- * checksummed Zstandard frame to contain exactly their JSONL header line.
4
- * Binary-safe: .jsonl.zstd is staged and merged only through validated Zstd artifact API.
5
- * Zstd bytes remain Buffer and are never converted to UTF-8 string; only decompressed
6
- * plaintext is stringified for line union. The standalone checksummed header frame is preserved.
7
- */
8
- import { copyFile, open, readFile, rename, unlink } from 'node:fs/promises';
9
- import { existsSync } from 'node:fs';
10
- import { randomBytes } from 'node:crypto';
11
- import { dirname } from 'node:path';
12
- import { constants, zstdCompress, zstdDecompress } from 'node:zlib';
13
- import { promisify } from 'node:util';
14
-
15
- const ZSTD_MAGIC = 0xFD2FB528;
16
- const compressZstd = promisify(zstdCompress);
17
- const decompressZstd = promisify(zstdDecompress);
18
- const ZSTD_CHECKSUM_OPTIONS = {
19
- params: { [constants.ZSTD_c_checksumFlag]: 1 },
20
- };
21
-
22
- interface ZstdFrameRange {
23
- start: number;
24
- end: number;
25
- }
26
-
27
- interface SessionArtifact {
28
- header: string;
29
- text: string;
30
- }
31
-
32
- /**
33
- * Union JSONL lines (or any line-delimited text) by exact line equality.
34
- * Preserves local order then appends remote-only lines.
35
- * Ensures trailing newline when result is non-empty.
36
- */
37
- export function mergeZstdLines(
38
- localText: string,
39
- remoteText: string,
40
- ): { merged: string; added: number } {
41
- const localLines = String(localText ?? '')
42
- .split('\n')
43
- .filter((line) => line.length > 0);
44
- const remoteLines = String(remoteText ?? '')
45
- .split('\n')
46
- .filter((line) => line.length > 0);
47
-
48
- const seen = new Set<string>(localLines);
49
- const mergedArr = [...localLines];
50
- let added = 0;
51
-
52
- for (const line of remoteLines) {
53
- if (!seen.has(line)) {
54
- seen.add(line);
55
- mergedArr.push(line);
56
- added++;
57
- }
58
- }
59
-
60
- const merged = mergedArr.length > 0 ? mergedArr.join('\n') + '\n' : '';
61
- return { merged, added };
62
- }
63
-
64
- /** Locate complete normal Zstandard frames in a concatenated DSH artifact. */
65
- function scanZstdFrames(source: Buffer): ZstdFrameRange[] {
66
- const frames: ZstdFrameRange[] = [];
67
- let offset = 0;
68
-
69
- while (offset < source.length) {
70
- const start = offset;
71
- if (source.length - offset < 5 || source.readUInt32LE(offset) !== ZSTD_MAGIC) {
72
- throw new Error('corrupt Zstandard session artifact');
73
- }
74
- offset += 4;
75
- const descriptor = source.readUInt8(offset++);
76
- if ((descriptor & 0x18) !== 0) throw new Error('corrupt Zstandard frame descriptor');
77
-
78
- const contentSizeFlag = descriptor >>> 6;
79
- const singleSegment = (descriptor & 0x20) !== 0;
80
- const dictionaryFlag = descriptor & 0x03;
81
- const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag;
82
- const contentSizeBytes = contentSizeFlag === 0
83
- ? (singleSegment ? 1 : 0)
84
- : 1 << contentSizeFlag;
85
- const frameHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes;
86
- if (source.length - offset < frameHeaderBytes) throw new Error('incomplete Zstandard frame header');
87
- offset += frameHeaderBytes;
88
-
89
- for (;;) {
90
- if (source.length - offset < 3) throw new Error('incomplete Zstandard frame block');
91
- const blockHeader = source.readUIntLE(offset, 3);
92
- offset += 3;
93
- const lastBlock = (blockHeader & 1) !== 0;
94
- const blockType = (blockHeader >>> 1) & 0x03;
95
- const blockSize = blockHeader >>> 3;
96
- if (blockType === 3) throw new Error('corrupt Zstandard frame block');
97
- const payloadBytes = blockType === 1 ? 1 : blockSize;
98
- if (source.length - offset < payloadBytes) throw new Error('incomplete Zstandard frame payload');
99
- offset += payloadBytes;
100
- if (lastBlock) break;
101
- }
102
-
103
- if ((descriptor & 0x04) !== 0) {
104
- if (source.length - offset < 4) throw new Error('incomplete Zstandard frame checksum');
105
- offset += 4;
106
- }
107
- frames.push({ start, end: offset });
108
- }
109
-
110
- return frames;
111
- }
112
-
113
- function parseSessionHeader(header: Buffer, label: string): string {
114
- if (header.length === 0 || header.indexOf(0x0A) !== header.length - 1) {
115
- throw new Error(`${label} session artifact has no standalone header frame`);
116
- }
117
-
118
- const line = header.subarray(0, -1).toString('utf8');
119
- let value: unknown;
120
- try {
121
- value = JSON.parse(line);
122
- } catch {
123
- throw new Error(`${label} session artifact has an invalid session header`);
124
- }
125
-
126
- const record = value as Record<string, unknown> | null;
127
- const hasValidInteger = (field: 'createdAt' | 'delegationDepth') => {
128
- const value = record?.[field];
129
- return typeof value === 'number'
130
- && Number.isSafeInteger(value)
131
- && value >= 0
132
- && !Object.is(value, -0);
133
- };
134
- if (
135
- record === null
136
- || typeof record !== 'object'
137
- || record.type !== 'session'
138
- || typeof record.version !== 'number'
139
- || typeof record.id !== 'string'
140
- || !hasValidInteger('createdAt')
141
- || !hasValidInteger('delegationDepth')
142
- || (record.origin !== undefined && record.origin !== 'subagent')
143
- || (record.agentPreset !== undefined && typeof record.agentPreset !== 'string')
144
- ) {
145
- throw new Error(`${label} session artifact has an invalid session header`);
146
- }
147
- return line;
148
- }
149
-
150
- async function readSessionArtifact(filePath: string, label: string): Promise<SessionArtifact> {
151
- if (!existsSync(filePath)) throw new Error(`${label} session artifact does not exist`);
152
-
153
- const source = await readFile(filePath);
154
- const frames = scanZstdFrames(source);
155
- if (frames.length === 0) throw new Error(`${label} session artifact has no Zstandard frames`);
156
-
157
- const headerBytes = await decompressZstd(source.subarray(frames[0]!.start, frames[0]!.end));
158
- const header = parseSessionHeader(headerBytes, label);
159
- const plaintext = await Promise.all(
160
- frames.map(async ({ start, end }) => decompressZstd(source.subarray(start, end))),
161
- );
162
- return { header, text: Buffer.concat(plaintext).toString('utf8') };
163
- }
164
-
165
- async function writeAtomically(filePath: string, content: Buffer): Promise<void> {
166
- const temporaryPath = `${filePath}.${randomBytes(12).toString('hex')}.tmp`;
167
- const handle = await open(temporaryPath, 'wx', 0o600);
168
- try {
169
- await handle.writeFile(content);
170
- await handle.sync();
171
- } finally {
172
- await handle.close();
173
- }
174
-
175
- try {
176
- await rename(temporaryPath, filePath);
177
- } catch (error) {
178
- await unlink(temporaryPath).catch(() => undefined);
179
- throw error;
180
- }
181
-
182
- const directory = await open(dirname(filePath), 'r');
183
- try {
184
- await directory.sync();
185
- } finally {
186
- await directory.close();
187
- }
188
- }
189
-
190
- /**
191
- * Merge two valid DSH Zstandard session artifacts into the local artifact.
192
- * The original local bytes are copied to a timestamped backup before the
193
- * atomic publish. Invalid, missing, plaintext, or mismatched input never
194
- * replaces the local file.
195
- */
196
- export async function mergeZstdFiles(
197
- localPath: string,
198
- remotePath: string,
199
- ): Promise<{ added: number }> {
200
- const local = await readSessionArtifact(localPath, 'local');
201
- const remote = await readSessionArtifact(remotePath, 'remote');
202
- if (local.header !== remote.header) {
203
- throw new Error('cannot merge session artifacts with different headers');
204
- }
205
-
206
- const { merged, added } = mergeZstdLines(local.text, remote.text);
207
- if (added === 0) return { added: 0 };
208
- if (!merged.startsWith(local.header + '\n')) {
209
- throw new Error('merged session artifact lost its header');
210
- }
211
-
212
- const body = merged.slice(local.header.length + 1);
213
- const frames = [await compressZstd(local.header + '\n', ZSTD_CHECKSUM_OPTIONS)];
214
- if (body.length > 0) frames.push(await compressZstd(body, ZSTD_CHECKSUM_OPTIONS));
215
- const backupPath = `${localPath}.bak.${Date.now()}.${randomBytes(6).toString('hex')}`;
216
- await copyFile(localPath, backupPath);
217
- await writeAtomically(localPath, Buffer.concat(frames));
218
-
219
- return { added };
220
- }