@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.
Files changed (285) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +209 -0
  3. package/cordis.patch.yml +43 -0
  4. package/lib/bin.d.ts +2 -0
  5. package/lib/bin.js +3 -0
  6. package/lib/boot-guard.d.ts +33 -0
  7. package/lib/boot-guard.js +68 -0
  8. package/lib/boot-lock.d.ts +56 -0
  9. package/lib/boot-lock.js +155 -0
  10. package/lib/cli.d.ts +31 -0
  11. package/lib/cli.js +336 -0
  12. package/lib/client.js +2901 -0
  13. package/lib/config/index.d.ts +34 -0
  14. package/lib/config/index.js +62 -0
  15. package/lib/config/service.d.ts +18 -0
  16. package/lib/config/service.js +28 -0
  17. package/lib/config.d.ts +1 -0
  18. package/lib/config.js +19 -0
  19. package/lib/daemon-freshness.d.ts +48 -0
  20. package/lib/daemon-freshness.js +122 -0
  21. package/lib/debug-agent.d.ts +17 -0
  22. package/lib/debug-agent.js +144 -0
  23. package/lib/dsh-session.d.ts +26 -0
  24. package/lib/dsh-session.js +45 -0
  25. package/lib/guard/config.d.ts +82 -0
  26. package/lib/guard/config.js +211 -0
  27. package/lib/guard/decide.d.ts +30 -0
  28. package/lib/guard/decide.js +38 -0
  29. package/lib/guard/full-scan-tool.d.ts +6 -0
  30. package/lib/guard/full-scan-tool.js +65 -0
  31. package/lib/guard/index.d.ts +85 -0
  32. package/lib/guard/index.js +254 -0
  33. package/lib/guard/journal.d.ts +146 -0
  34. package/lib/guard/journal.js +398 -0
  35. package/lib/guard/migrate.d.ts +15 -0
  36. package/lib/guard/migrate.js +42 -0
  37. package/lib/guard/parse.d.ts +193 -0
  38. package/lib/guard/parse.js +1103 -0
  39. package/lib/guard/paths.d.ts +104 -0
  40. package/lib/guard/paths.js +254 -0
  41. package/lib/guard/permission-policy.d.ts +10 -0
  42. package/lib/guard/permission-policy.js +21 -0
  43. package/lib/guard/redact.d.ts +25 -0
  44. package/lib/guard/redact.js +52 -0
  45. package/lib/guard/rules.d.ts +65 -0
  46. package/lib/guard/rules.js +832 -0
  47. package/lib/guard/status-tool.d.ts +54 -0
  48. package/lib/guard/status-tool.js +195 -0
  49. package/lib/guard/tiers.d.ts +2 -0
  50. package/lib/guard/tiers.js +1 -0
  51. package/lib/guard/validators.d.ts +4 -0
  52. package/lib/guard/validators.js +45 -0
  53. package/lib/health-poller.d.ts +90 -0
  54. package/lib/health-poller.js +430 -0
  55. package/lib/index.d.ts +7 -0
  56. package/lib/index.js +7 -0
  57. package/lib/intents.d.ts +32 -0
  58. package/lib/intents.js +49 -0
  59. package/lib/notifier.d.ts +12 -0
  60. package/lib/notifier.js +62 -0
  61. package/lib/paths.d.ts +13 -0
  62. package/lib/paths.js +70 -0
  63. package/lib/plugin.d.ts +250 -0
  64. package/lib/plugin.js +1126 -0
  65. package/lib/preset.d.ts +73 -0
  66. package/lib/preset.js +158 -0
  67. package/lib/report.d.ts +11 -0
  68. package/lib/report.js +58 -0
  69. package/lib/restart-exec.d.ts +35 -0
  70. package/lib/restart-exec.js +60 -0
  71. package/lib/restart-guards.d.ts +39 -0
  72. package/lib/restart-guards.js +150 -0
  73. package/lib/restart-tool.d.ts +124 -0
  74. package/lib/restart-tool.js +539 -0
  75. package/lib/restart-web.d.ts +26 -0
  76. package/lib/restart-web.js +58 -0
  77. package/lib/resume-log.d.ts +24 -0
  78. package/lib/resume-log.js +18 -0
  79. package/lib/resume-tools.d.ts +131 -0
  80. package/lib/resume-tools.js +337 -0
  81. package/lib/resume.d.ts +33 -0
  82. package/lib/resume.js +221 -0
  83. package/lib/scan.d.ts +20 -0
  84. package/lib/scan.js +71 -0
  85. package/lib/self-kill-guard.d.ts +81 -0
  86. package/lib/self-kill-guard.js +208 -0
  87. package/lib/session-health.d.ts +89 -0
  88. package/lib/session-health.js +274 -0
  89. package/lib/session-log-file.d.ts +22 -0
  90. package/lib/session-log-file.js +74 -0
  91. package/lib/skill-provider.d.ts +6 -0
  92. package/lib/skill-provider.js +73 -0
  93. package/lib/snapshot.d.ts +59 -0
  94. package/lib/snapshot.js +358 -0
  95. package/lib/source.d.ts +19 -0
  96. package/lib/source.js +19 -0
  97. package/lib/store/index.d.ts +42 -0
  98. package/lib/store/index.js +340 -0
  99. package/lib/store/legacy.d.ts +17 -0
  100. package/lib/store/legacy.js +85 -0
  101. package/lib/supervisor.d.ts +119 -0
  102. package/lib/supervisor.js +741 -0
  103. package/lib/sync/backup-config.d.ts +55 -0
  104. package/lib/sync/backup-config.js +119 -0
  105. package/lib/sync/backup-service.d.ts +110 -0
  106. package/lib/sync/backup-service.js +440 -0
  107. package/lib/sync/bidirectional.d.ts +39 -0
  108. package/lib/sync/bidirectional.js +25 -0
  109. package/lib/sync/cli.d.ts +38 -0
  110. package/lib/sync/cli.js +536 -0
  111. package/lib/sync/config.d.ts +77 -0
  112. package/lib/sync/config.js +90 -0
  113. package/lib/sync/fingerprint.d.ts +29 -0
  114. package/lib/sync/fingerprint.js +65 -0
  115. package/lib/sync/hashing.d.ts +9 -0
  116. package/lib/sync/hashing.js +47 -0
  117. package/lib/sync/index.d.ts +6 -0
  118. package/lib/sync/index.js +634 -0
  119. package/lib/sync/machine-id.d.ts +29 -0
  120. package/lib/sync/machine-id.js +66 -0
  121. package/lib/sync/merge.d.ts +33 -0
  122. package/lib/sync/merge.js +88 -0
  123. package/lib/sync/peer-host.d.ts +22 -0
  124. package/lib/sync/peer-host.js +62 -0
  125. package/lib/sync/process-runner.d.ts +31 -0
  126. package/lib/sync/process-runner.js +161 -0
  127. package/lib/sync/remote-agent.d.ts +42 -0
  128. package/lib/sync/remote-agent.js +217 -0
  129. package/lib/sync/remote-cache.d.ts +18 -0
  130. package/lib/sync/remote-cache.js +60 -0
  131. package/lib/sync/remote-manifest.d.ts +8 -0
  132. package/lib/sync/remote-manifest.js +64 -0
  133. package/lib/sync/s3-object-store.d.ts +38 -0
  134. package/lib/sync/s3-object-store.js +126 -0
  135. package/lib/sync/session-merge.d.ts +18 -0
  136. package/lib/sync/session-merge.js +191 -0
  137. package/lib/sync/session-plan.d.ts +29 -0
  138. package/lib/sync/session-plan.js +213 -0
  139. package/lib/sync/sigv4.d.ts +20 -0
  140. package/lib/sync/sigv4.js +57 -0
  141. package/lib/sync/snapshot.d.ts +21 -0
  142. package/lib/sync/snapshot.js +176 -0
  143. package/lib/sync/ssh-mux.d.ts +12 -0
  144. package/lib/sync/ssh-mux.js +48 -0
  145. package/lib/sync/sync-plan.d.ts +24 -0
  146. package/lib/sync/sync-plan.js +366 -0
  147. package/lib/sync/sync-service.d.ts +177 -0
  148. package/lib/sync/sync-service.js +819 -0
  149. package/lib/sync/sync-types.d.ts +87 -0
  150. package/lib/sync/sync-types.js +1 -0
  151. package/lib/sync/transport.d.ts +75 -0
  152. package/lib/sync/transport.js +200 -0
  153. package/lib/sync/tunnel-restore.d.ts +77 -0
  154. package/lib/sync/tunnel-restore.js +142 -0
  155. package/lib/sync/validation.d.ts +10 -0
  156. package/lib/sync/validation.js +83 -0
  157. package/lib/tool-view.d.ts +62 -0
  158. package/lib/tool-view.js +70 -0
  159. package/lib/types/client/auto-reload.d.ts +8 -0
  160. package/lib/types/client/auto-reload.d.ts.map +1 -0
  161. package/lib/types/client/config/MaestroSettings.d.ts +25 -0
  162. package/lib/types/client/config/MaestroSettings.d.ts.map +1 -0
  163. package/lib/types/client/config/components/BrandMark.d.ts +50 -0
  164. package/lib/types/client/config/components/BrandMark.d.ts.map +1 -0
  165. package/lib/types/client/config/guard-view.d.ts +83 -0
  166. package/lib/types/client/config/guard-view.d.ts.map +1 -0
  167. package/lib/types/client/config/index.d.ts +20 -0
  168. package/lib/types/client/config/index.d.ts.map +1 -0
  169. package/lib/types/client/config/maestro-mark.d.ts +29 -0
  170. package/lib/types/client/config/maestro-mark.d.ts.map +1 -0
  171. package/lib/types/client/config/settings-nav-icon.d.ts +21 -0
  172. package/lib/types/client/config/settings-nav-icon.d.ts.map +1 -0
  173. package/lib/types/client/index.d.ts +16 -0
  174. package/lib/types/client/index.d.ts.map +1 -0
  175. package/lib/types/client/sync/R2SyncPanel.d.ts +6 -0
  176. package/lib/types/client/sync/R2SyncPanel.d.ts.map +1 -0
  177. package/lib/types/client/sync/SyncPanel.d.ts +12 -0
  178. package/lib/types/client/sync/SyncPanel.d.ts.map +1 -0
  179. package/lib/types/client/sync/confirm-dialog.d.ts +43 -0
  180. package/lib/types/client/sync/confirm-dialog.d.ts.map +1 -0
  181. package/lib/types/client/sync/index.d.ts +10 -0
  182. package/lib/types/client/sync/index.d.ts.map +1 -0
  183. package/lib/types/client/sync/ui.d.ts +79 -0
  184. package/lib/types/client/sync/ui.d.ts.map +1 -0
  185. package/lib/types/client/sync/use-backup.d.ts +50 -0
  186. package/lib/types/client/sync/use-backup.d.ts.map +1 -0
  187. package/lib/types/client/sync/use-sync.d.ts +88 -0
  188. package/lib/types/client/sync/use-sync.d.ts.map +1 -0
  189. package/package.json +89 -0
  190. package/skills/dsh-safe-restart/SKILL.md +206 -0
  191. package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +468 -0
  192. package/src/client/auto-reload.ts +104 -0
  193. package/src/client/config/MaestroSettings.tsx +892 -0
  194. package/src/client/config/components/BrandMark.tsx +94 -0
  195. package/src/client/config/guard-view.ts +187 -0
  196. package/src/client/config/index.tsx +109 -0
  197. package/src/client/config/maestro-mark.ts +37 -0
  198. package/src/client/config/settings-nav-icon.ts +68 -0
  199. package/src/client/index.tsx +33 -0
  200. package/src/client/sync/R2SyncPanel.tsx +260 -0
  201. package/src/client/sync/SyncPanel.tsx +422 -0
  202. package/src/client/sync/confirm-dialog.tsx +202 -0
  203. package/src/client/sync/index.tsx +336 -0
  204. package/src/client/sync/ui.tsx +313 -0
  205. package/src/client/sync/use-backup.ts +207 -0
  206. package/src/client/sync/use-sync.ts +502 -0
  207. package/src/host/bin.ts +3 -0
  208. package/src/host/boot-guard.ts +76 -0
  209. package/src/host/boot-lock.ts +155 -0
  210. package/src/host/cli.ts +288 -0
  211. package/src/host/config/index.ts +78 -0
  212. package/src/host/config/service.ts +40 -0
  213. package/src/host/config.ts +18 -0
  214. package/src/host/daemon-freshness.ts +143 -0
  215. package/src/host/debug-agent.ts +145 -0
  216. package/src/host/dsh-session.ts +56 -0
  217. package/src/host/dsh-skill.d.ts +44 -0
  218. package/src/host/guard/augment.d.ts +33 -0
  219. package/src/host/guard/config.ts +244 -0
  220. package/src/host/guard/decide.ts +40 -0
  221. package/src/host/guard/full-scan-tool.ts +61 -0
  222. package/src/host/guard/index.ts +281 -0
  223. package/src/host/guard/journal.ts +440 -0
  224. package/src/host/guard/migrate.ts +41 -0
  225. package/src/host/guard/parse.ts +1150 -0
  226. package/src/host/guard/paths.ts +239 -0
  227. package/src/host/guard/permission-policy.ts +15 -0
  228. package/src/host/guard/redact.ts +64 -0
  229. package/src/host/guard/rules.ts +891 -0
  230. package/src/host/guard/status-tool.ts +229 -0
  231. package/src/host/guard/tiers.ts +2 -0
  232. package/src/host/guard/validators.ts +29 -0
  233. package/src/host/health-poller.ts +446 -0
  234. package/src/host/index.ts +7 -0
  235. package/src/host/intents.ts +67 -0
  236. package/src/host/notifier.ts +71 -0
  237. package/src/host/paths.ts +70 -0
  238. package/src/host/plugin.ts +1097 -0
  239. package/src/host/preset.ts +170 -0
  240. package/src/host/report.ts +71 -0
  241. package/src/host/restart-exec.ts +81 -0
  242. package/src/host/restart-guards.ts +150 -0
  243. package/src/host/restart-tool.ts +483 -0
  244. package/src/host/restart-web.ts +72 -0
  245. package/src/host/resume-log.ts +39 -0
  246. package/src/host/resume-tools.ts +385 -0
  247. package/src/host/resume.ts +204 -0
  248. package/src/host/scan.ts +61 -0
  249. package/src/host/self-kill-guard.ts +216 -0
  250. package/src/host/session-health.ts +236 -0
  251. package/src/host/session-log-file.ts +77 -0
  252. package/src/host/skill-provider.ts +69 -0
  253. package/src/host/snapshot.ts +337 -0
  254. package/src/host/source.ts +19 -0
  255. package/src/host/store/index.ts +348 -0
  256. package/src/host/store/legacy.ts +96 -0
  257. package/src/host/supervisor.ts +720 -0
  258. package/src/host/sync/backup-config.ts +159 -0
  259. package/src/host/sync/backup-service.ts +453 -0
  260. package/src/host/sync/bidirectional.ts +66 -0
  261. package/src/host/sync/cli.ts +539 -0
  262. package/src/host/sync/config.ts +119 -0
  263. package/src/host/sync/fingerprint.ts +83 -0
  264. package/src/host/sync/hashing.ts +58 -0
  265. package/src/host/sync/index.ts +737 -0
  266. package/src/host/sync/machine-id.ts +81 -0
  267. package/src/host/sync/merge.ts +102 -0
  268. package/src/host/sync/peer-host.ts +65 -0
  269. package/src/host/sync/process-runner.ts +177 -0
  270. package/src/host/sync/remote-agent.ts +213 -0
  271. package/src/host/sync/remote-cache.ts +68 -0
  272. package/src/host/sync/remote-manifest.ts +70 -0
  273. package/src/host/sync/s3-object-store.ts +144 -0
  274. package/src/host/sync/session-merge.ts +220 -0
  275. package/src/host/sync/session-plan.ts +237 -0
  276. package/src/host/sync/sigv4.ts +81 -0
  277. package/src/host/sync/snapshot.ts +162 -0
  278. package/src/host/sync/ssh-mux.ts +56 -0
  279. package/src/host/sync/sync-plan.ts +366 -0
  280. package/src/host/sync/sync-service.ts +855 -0
  281. package/src/host/sync/sync-types.ts +102 -0
  282. package/src/host/sync/transport.ts +261 -0
  283. package/src/host/sync/tunnel-restore.ts +160 -0
  284. package/src/host/sync/validation.ts +91 -0
  285. package/src/host/tool-view.ts +98 -0
@@ -0,0 +1,358 @@
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+ import * as crypto from 'node:crypto';
4
+ /**
5
+ * DSH-home entries the last-known-good snapshot deliberately never copies.
6
+ *
7
+ * The LKG exists to recover a boot that fails while loading the plugin tree, so
8
+ * it holds boot **configuration**: `profiles/` (the plugin tree with its
9
+ * lockfile, `cordis.patch.yml` and sidecars), the per-plugin config directories
10
+ * and the settings documents. Everything below is runtime **data** — it is
11
+ * written continuously while `dsh web` runs, so restoring a snapshot of it can
12
+ * only lose newer state, and some of it is hostile to a bulk copy:
13
+ *
14
+ * - `sessions/` — append-only transcripts. Restoring a stale copy over live
15
+ * sessions drops every turn recorded after the snapshot, i.e. the recovery
16
+ * path would lose the log it is supposed to protect.
17
+ * - `attachments/` — content-addressed blobs stored mode `0400`. That read-only
18
+ * bit is exactly what made the 2026-09-13 rollback abort with
19
+ * `EACCES: Permission denied '.../attachments/v1/objects/f8'`.
20
+ * - `plugins-src/` — plugin source cache, re-fetched on demand (~400 MB host).
21
+ * - `.supervisor/` — the LKG root itself lives inside the DSH home, so copying
22
+ * it would recurse into every retained snapshot.
23
+ *
24
+ * This list is data, not a heuristic: the copy loop and the restore loop both
25
+ * consult `isLkgExcluded()`, and the table test pins the rule.
26
+ */
27
+ export const LKG_EXCLUDED_ENTRIES = [
28
+ 'sessions',
29
+ 'attachments',
30
+ 'plugins-src',
31
+ '.supervisor',
32
+ ];
33
+ /**
34
+ * True when a DSH-home-relative path must never enter (or leave) the LKG.
35
+ *
36
+ * The named entries match the first path segment; `*.log` matches by basename
37
+ * anywhere, because append-only logs are data at any depth and a restored stale
38
+ * `dsh-web.log` would poison the boot-boundary scan.
39
+ */
40
+ export function isLkgExcluded(relPath) {
41
+ const normalized = relPath.replace(/\\/g, '/').replace(/^\.\//, '').replace(/^\/+/, '');
42
+ const segments = normalized.split('/').filter(s => s.length > 0 && s !== '.');
43
+ if (!segments.length)
44
+ return false;
45
+ if (LKG_EXCLUDED_ENTRIES.includes(segments[0]))
46
+ return true;
47
+ return segments[segments.length - 1].endsWith('.log');
48
+ }
49
+ function sha256File(filePath) {
50
+ const data = fs.readFileSync(filePath);
51
+ return crypto.createHash('sha256').update(data).digest('hex');
52
+ }
53
+ function walkFiles(dir, base = dir) {
54
+ const out = [];
55
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
56
+ const full = path.join(dir, entry.name);
57
+ if (entry.isDirectory())
58
+ out.push(...walkFiles(full, base));
59
+ else if (entry.isFile())
60
+ out.push(path.relative(base, full));
61
+ }
62
+ return out;
63
+ }
64
+ /** Human-readable, bounded reason for a per-entry copy failure. */
65
+ export function failureReason(e) {
66
+ const err = e;
67
+ const code = typeof err?.code === 'string' && err.code ? `${err.code}: ` : '';
68
+ const message = typeof err?.message === 'string' ? err.message : String(e);
69
+ return `${code}${message}`.slice(0, 300);
70
+ }
71
+ /**
72
+ * Copy one snapshot entry, collecting — never throwing — per-entry failures
73
+ * (D2). `fs.cpSync` aborts the whole snapshot on the first unreadable object,
74
+ * which is precisely how one 0400 attachment file stopped the rollback that
75
+ * exists to rescue a broken boot.
76
+ */
77
+ function copyEntry(state, src, dest, rel) {
78
+ if (isLkgExcluded(rel))
79
+ return;
80
+ let st;
81
+ try {
82
+ st = fs.lstatSync(src);
83
+ }
84
+ catch (e) {
85
+ state.skipped.push({ path: rel, reason: failureReason(e) });
86
+ return;
87
+ }
88
+ if (st.isDirectory()) {
89
+ let names;
90
+ try {
91
+ // Directory modes are replicated only in their permission-to-traverse
92
+ // sense: the snapshot itself must stay readable/removable even when the
93
+ // source directory is not (a 0000 source dir must not produce a snapshot
94
+ // that nothing — including retention — can delete).
95
+ fs.mkdirSync(dest, { recursive: true, mode: (st.mode & 0o7777) | 0o700 });
96
+ names = fs.readdirSync(src);
97
+ }
98
+ catch (e) {
99
+ state.skipped.push({ path: rel, reason: failureReason(e) });
100
+ return;
101
+ }
102
+ for (const name of names) {
103
+ copyEntry(state, path.join(src, name), path.join(dest, name), rel ? `${rel}/${name}` : name);
104
+ }
105
+ return;
106
+ }
107
+ if (st.isSymbolicLink()) {
108
+ try {
109
+ const link = fs.readlinkSync(src);
110
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
111
+ try {
112
+ fs.unlinkSync(dest);
113
+ }
114
+ catch { }
115
+ fs.symlinkSync(link, dest);
116
+ state.copied++;
117
+ }
118
+ catch (e) {
119
+ state.skipped.push({ path: rel, reason: failureReason(e) });
120
+ }
121
+ return;
122
+ }
123
+ if (st.isFile()) {
124
+ try {
125
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
126
+ state.copyFile(src, dest, st.mode & 0o7777);
127
+ state.copied++;
128
+ }
129
+ catch (e) {
130
+ state.skipped.push({ path: rel, reason: failureReason(e) });
131
+ }
132
+ }
133
+ // Sockets/FIFOs/devices are not boot configuration: ignored on purpose.
134
+ }
135
+ export async function writeLKG(dshHome, lkgRoot, deps = {}) {
136
+ // Dedupe: skip snapshot if current state identical to latest LKG (prevents 5-min unconditional growth)
137
+ try {
138
+ if (await isDuplicateLKG(dshHome, lkgRoot)) {
139
+ const entries = fs.readdirSync(lkgRoot).filter((n) => {
140
+ try {
141
+ return fs.statSync(path.join(lkgRoot, n)).isDirectory();
142
+ }
143
+ catch {
144
+ return false;
145
+ }
146
+ }).sort();
147
+ const latest = entries[entries.length - 1];
148
+ const manifestPath = path.join(lkgRoot, latest, 'manifest.json');
149
+ const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
150
+ return { ts: latest, files: manifest.files?.length ?? 0, skipped: [] };
151
+ }
152
+ }
153
+ catch { }
154
+ const ts = new Date().toISOString().replace(/[:.]/g, '-');
155
+ const dest = path.join(lkgRoot, ts);
156
+ try {
157
+ fs.mkdirSync(dest, { recursive: true });
158
+ }
159
+ catch (e) {
160
+ // No snapshot is better than an exception thrown into the rollback path.
161
+ return { ts, files: 0, skipped: [{ path: lkgRoot, reason: failureReason(e) }] };
162
+ }
163
+ const state = {
164
+ dshHome,
165
+ copyFile: deps.copyFile ?? ((src, dst, mode) => {
166
+ fs.copyFileSync(src, dst);
167
+ try {
168
+ fs.chmodSync(dst, mode);
169
+ }
170
+ catch { }
171
+ }),
172
+ copied: 0,
173
+ skipped: [],
174
+ };
175
+ // Copy only the boot configuration (D1), one entry at a time so a single
176
+ // unreadable file is recorded and skipped instead of aborting the snapshot (D2).
177
+ let topLevel = [];
178
+ try {
179
+ if (fs.existsSync(dshHome))
180
+ topLevel = fs.readdirSync(dshHome);
181
+ }
182
+ catch (e) {
183
+ state.skipped.push({ path: '.', reason: failureReason(e) });
184
+ }
185
+ for (const entry of topLevel) {
186
+ copyEntry(state, path.join(dshHome, entry), path.join(dest, entry), entry);
187
+ }
188
+ let fileList = [];
189
+ try {
190
+ fileList = fs.existsSync(dest) ? walkFiles(dest) : [];
191
+ }
192
+ catch (e) {
193
+ state.skipped.push({ path: 'manifest', reason: failureReason(e) });
194
+ }
195
+ const manifest = {
196
+ ts,
197
+ files: fileList
198
+ .filter(f => f !== 'manifest.json')
199
+ .map(f => ({ path: f, sha256: sha256File(path.join(dest, f)) })),
200
+ };
201
+ try {
202
+ fs.writeFileSync(path.join(dest, 'manifest.json'), JSON.stringify(manifest, null, 2));
203
+ }
204
+ catch (e) {
205
+ state.skipped.push({ path: 'manifest.json', reason: failureReason(e) });
206
+ }
207
+ // Retention: keep only 3 most recent, plus age (7d) and size (5GB) caps — prevents unbounded 40GB+ growth
208
+ await rotateLKG(lkgRoot, 3).catch(() => { });
209
+ await pruneByAge(lkgRoot, 7 * 24 * 60 * 60 * 1000).catch(() => { });
210
+ await pruneBySize(lkgRoot, 5 * 1024 * 1024 * 1024).catch(() => { });
211
+ return { ts, files: state.copied, skipped: state.skipped };
212
+ }
213
+ export async function pruneByAge(root, maxAgeMs) {
214
+ if (!fs.existsSync(root))
215
+ return;
216
+ const now = Date.now();
217
+ const entries = fs.readdirSync(root).filter((n) => {
218
+ try {
219
+ return fs.statSync(path.join(root, n)).isDirectory();
220
+ }
221
+ catch {
222
+ return false;
223
+ }
224
+ });
225
+ for (const name of entries) {
226
+ try {
227
+ const tsStr = name.replace(/^(\d{4})-(\d{2})-(\d{2})T(\d{2})-(\d{2})-(\d{2})-(\d+)Z$/, '$1-$2-$3T$4:$5:$6.$7Z');
228
+ const ts = Date.parse(tsStr);
229
+ if (!isNaN(ts) && now - ts > maxAgeMs) {
230
+ fs.rmSync(path.join(root, name), { recursive: true, force: true });
231
+ }
232
+ }
233
+ catch { }
234
+ }
235
+ }
236
+ export async function pruneBySize(root, maxBytes) {
237
+ if (!fs.existsSync(root))
238
+ return;
239
+ const entries = fs.readdirSync(root).filter((n) => {
240
+ try {
241
+ return fs.statSync(path.join(root, n)).isDirectory();
242
+ }
243
+ catch {
244
+ return false;
245
+ }
246
+ }).sort();
247
+ let total = 0;
248
+ const sizes = [];
249
+ for (const name of entries) {
250
+ try {
251
+ const p = path.join(root, name);
252
+ let size = 0;
253
+ for (const f of walkFiles(p)) {
254
+ try {
255
+ size += fs.statSync(path.join(p, f)).size;
256
+ }
257
+ catch { }
258
+ }
259
+ sizes.push({ name, size });
260
+ total += size;
261
+ }
262
+ catch { }
263
+ }
264
+ for (const { name, size } of sizes) {
265
+ if (total <= maxBytes)
266
+ break;
267
+ try {
268
+ fs.rmSync(path.join(root, name), { recursive: true, force: true });
269
+ total -= size;
270
+ }
271
+ catch { }
272
+ }
273
+ }
274
+ export async function isDuplicateLKG(dshHome, lkgRoot) {
275
+ // Lightweight dedupe: if latest snapshot is <5 minutes old, skip (prevents 5-min unconditional growth)
276
+ // Full hash check is too heavy (would read 500MB+ each tick) and caused status timeouts
277
+ if (!fs.existsSync(lkgRoot))
278
+ return false;
279
+ const entries = fs.readdirSync(lkgRoot).filter((n) => {
280
+ try {
281
+ return fs.statSync(path.join(lkgRoot, n)).isDirectory();
282
+ }
283
+ catch {
284
+ return false;
285
+ }
286
+ }).sort();
287
+ if (!entries.length)
288
+ return false;
289
+ const latestName = entries[entries.length - 1];
290
+ try {
291
+ const tsStr = latestName.replace(/^(\d{4})-(\d{2})-(\d{2})T(\d{2})-(\d{2})-(\d{2})-(\d+)Z$/, '$1-$2-$3T$4:$5:$6.$7Z');
292
+ const ts = Date.parse(tsStr);
293
+ if (!isNaN(ts) && Date.now() - ts < 5 * 60 * 1000) {
294
+ // If latest is recent and DSH home hasn't changed in mtime, consider duplicate
295
+ // Quick check: compare latest snapshot's mtime vs DSH home's newest file mtime
296
+ const latestPath = path.join(lkgRoot, latestName);
297
+ const latestMtime = fs.statSync(latestPath).mtimeMs;
298
+ let newestFileMtime = 0;
299
+ if (fs.existsSync(dshHome)) {
300
+ for (const entry of fs.readdirSync(dshHome)) {
301
+ // Same scope as the copy loop: runtime data changes constantly and
302
+ // must not defeat the dedupe for the configuration being snapshotted.
303
+ if (isLkgExcluded(entry))
304
+ continue;
305
+ try {
306
+ const s = fs.statSync(path.join(dshHome, entry));
307
+ if (s.mtimeMs > newestFileMtime)
308
+ newestFileMtime = s.mtimeMs;
309
+ }
310
+ catch { }
311
+ }
312
+ }
313
+ if (newestFileMtime > 0 && newestFileMtime < latestMtime)
314
+ return true;
315
+ }
316
+ }
317
+ catch { }
318
+ return false;
319
+ }
320
+ export async function verifyLKG(lkgPath) {
321
+ const manifestPath = path.join(lkgPath, 'manifest.json');
322
+ if (!fs.existsSync(manifestPath))
323
+ return false;
324
+ try {
325
+ const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
326
+ for (const entry of manifest.files) {
327
+ const filePath = path.join(lkgPath, entry.path);
328
+ if (!fs.existsSync(filePath))
329
+ return false;
330
+ const hash = sha256File(filePath);
331
+ if (hash !== entry.sha256)
332
+ return false;
333
+ }
334
+ return true;
335
+ }
336
+ catch {
337
+ return false;
338
+ }
339
+ }
340
+ export async function rotateLKG(lkgRoot, keep = 3) {
341
+ if (!fs.existsSync(lkgRoot))
342
+ return;
343
+ const entries = fs.readdirSync(lkgRoot).filter((n) => {
344
+ try {
345
+ return fs.statSync(path.join(lkgRoot, n)).isDirectory();
346
+ }
347
+ catch {
348
+ return false;
349
+ }
350
+ }).sort();
351
+ const toDelete = entries.slice(0, Math.max(0, entries.length - keep));
352
+ for (const name of toDelete) {
353
+ fs.rmSync(path.join(lkgRoot, name), { recursive: true, force: true });
354
+ }
355
+ }
356
+ export async function writeFailed(dshHome, failedRoot) {
357
+ return writeLKG(dshHome, failedRoot);
358
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The producer-owned source kind this plugin stamps on every message it injects
3
+ * into a session.
4
+ *
5
+ * Session format v4 requires the producer to name itself (`plugin:<package>`)
6
+ * and rejects the retired bare `kind: 'plugin'` outright. `kind: 'user'` is
7
+ * admissible but false: these turns are machine-initiated, and consumers
8
+ * classify a turn by this kind — dsh-maestro-memory's write guard documents
9
+ * that plugin-initiated turns carry no per-turn write duty and can only honour
10
+ * that when the producer says who it is.
11
+ *
12
+ * Scope, so the claim stays honest: this covers the two `agent.followup`
13
+ * injections in `plugin.ts` and `resume-tools.ts`. The third delivery path —
14
+ * `promptOwnedSession` through `sessionController.prompt` — cannot carry a kind
15
+ * because the controller hardcodes `'user'`; that gap is documented at the call
16
+ * site and tracked as a follow-up in
17
+ * `docs/specs/2026-09-28-cross-repo-upstream-v4-lessons-design.md`.
18
+ */
19
+ export declare const SUPERVISOR_SOURCE_KIND = "plugin:@ddtcorex/dsh-maestro-core";
package/lib/source.js ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The producer-owned source kind this plugin stamps on every message it injects
3
+ * into a session.
4
+ *
5
+ * Session format v4 requires the producer to name itself (`plugin:<package>`)
6
+ * and rejects the retired bare `kind: 'plugin'` outright. `kind: 'user'` is
7
+ * admissible but false: these turns are machine-initiated, and consumers
8
+ * classify a turn by this kind — dsh-maestro-memory's write guard documents
9
+ * that plugin-initiated turns carry no per-turn write duty and can only honour
10
+ * that when the producer says who it is.
11
+ *
12
+ * Scope, so the claim stays honest: this covers the two `agent.followup`
13
+ * injections in `plugin.ts` and `resume-tools.ts`. The third delivery path —
14
+ * `promptOwnedSession` through `sessionController.prompt` — cannot carry a kind
15
+ * because the controller hardcodes `'user'`; that gap is documented at the call
16
+ * site and tracked as a follow-up in
17
+ * `docs/specs/2026-09-28-cross-repo-upstream-v4-lessons-design.md`.
18
+ */
19
+ export const SUPERVISOR_SOURCE_KIND = 'plugin:@ddtcorex/dsh-maestro-core';
@@ -0,0 +1,42 @@
1
+ export interface SettingsDoc {
2
+ version: 1;
3
+ domains: Record<string, unknown>;
4
+ }
5
+ /** Minimal structural validator a domain owner supplies (schemastery/zod adapters fit). */
6
+ export interface DomainValidator {
7
+ parse(value: unknown): {
8
+ ok: true;
9
+ value?: unknown;
10
+ } | {
11
+ ok: false;
12
+ error: string;
13
+ };
14
+ }
15
+ /** Register a validator for a domain you own. Writes to it are validated on every set(). */
16
+ export declare function defineDomain(name: string, validator: DomainValidator): void;
17
+ /** Fire callbacks registered through this instance after a successful set(). */
18
+ export declare function onChange(cb: (domain: string) => void): () => void;
19
+ /** Test seam: drop the memoized document and listeners (schemas are kept). */
20
+ export declare function resetForTests(): void;
21
+ export declare function load(opts?: {
22
+ dshHome?: string;
23
+ }): Promise<SettingsDoc>;
24
+ export declare function get(domain: string, opts?: {
25
+ dshHome?: string;
26
+ }): Promise<unknown>;
27
+ export declare function set(domain: string, patch: object, opts?: {
28
+ dshHome?: string;
29
+ }): Promise<void>;
30
+ /**
31
+ * Delete one TOP-LEVEL key of a domain — the delete operation `set()` cannot
32
+ * express (a merge patch has no delete signal: `null` persists as null).
33
+ * Dotted paths are rejected to keep the semantics exactly one key. No-op
34
+ * (`false`, no write, no callbacks) when the domain is absent/not an object
35
+ * or the key is absent. Otherwise the resulting domain is validated like a
36
+ * `set()` before the atomic write; `true` means the key is gone.
37
+ */
38
+ export declare function unset(domain: string, key: string, opts?: {
39
+ dshHome?: string;
40
+ }): Promise<boolean>;
41
+ /** Names of domains registered via defineDomain (schema owners). */
42
+ export declare function definedDomains(): string[];