@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,211 @@
1
+ import { DEFAULT_TIERS } from './rules.js';
2
+ import { defaultProtectedPaths, guardConfigPaths } from './paths.js';
3
+ /**
4
+ * Built-in defaults. Protection is ON for everything that can be on: a missing,
5
+ * unreadable or half-written config must never turn a gate off, so every
6
+ * fallback path lands here rather than on an empty object.
7
+ */
8
+ export const DEFAULT_CONFIG = {
9
+ rules: { ...DEFAULT_TIERS },
10
+ protectedBranches: ['master', 'main'],
11
+ protectedPaths: defaultProtectedPaths(),
12
+ guardPaths: guardConfigPaths(),
13
+ journal: { enabled: true, retainDays: 30, retainFiles: 14, allowCounters: true },
14
+ workingDirContainment: { enabled: true, spillReads: true },
15
+ };
16
+ function isPlainObject(v) {
17
+ return typeof v === 'object' && v !== null && !Array.isArray(v);
18
+ }
19
+ function stringArray(v) {
20
+ if (!Array.isArray(v))
21
+ return undefined;
22
+ const out = v.filter((x) => typeof x === 'string');
23
+ return out.length > 0 ? out : undefined;
24
+ }
25
+ /**
26
+ * A retention window must be a POSITIVE INTEGER. The journal block used to be
27
+ * spread into the defaults unvalidated, and `rotate()` multiplies/compares with
28
+ * both windows: `retainDays: "30"` makes the day cutoff `NaN` and
29
+ * `retainFiles: "14"` makes the file window compare false, so BETWEEN them a
30
+ * non-numeric value prunes EVERY archive. An invalid value falls back to the
31
+ * built-in default — the fail-safe direction for a retention window.
32
+ *
33
+ * The value is never floored: `0.5` passed the `> 0` check and `Math.floor`
34
+ * turned it into 0, which prunes every archive — the very failure this
35
+ * validation exists to prevent. A window counts days/files, so a fraction is
36
+ * simply invalid and falls back like any other bad value.
37
+ */
38
+ function positiveInt(v) {
39
+ return typeof v === 'number' && Number.isInteger(v) && v > 0 ? v : undefined;
40
+ }
41
+ /** A boolean config switch, or the built-in default when it is not a boolean. */
42
+ function booleanOr(v, fallback) {
43
+ return typeof v === 'boolean' ? v : fallback;
44
+ }
45
+ /**
46
+ * Merge the persisted `domains.guard` object onto the defaults. Only keys that
47
+ * are actually present are overlaid, and `rules` merges per rule id, so a
48
+ * partial config can neither drop the default tiers nor unset the protected
49
+ * path lists.
50
+ */
51
+ export function mergeGuardConfig(raw) {
52
+ const base = {
53
+ ...DEFAULT_CONFIG,
54
+ rules: { ...DEFAULT_CONFIG.rules },
55
+ protectedBranches: [...DEFAULT_CONFIG.protectedBranches],
56
+ protectedPaths: [...DEFAULT_CONFIG.protectedPaths],
57
+ guardPaths: [...DEFAULT_CONFIG.guardPaths],
58
+ journal: { ...DEFAULT_CONFIG.journal },
59
+ workingDirContainment: { ...DEFAULT_CONFIG.workingDirContainment },
60
+ };
61
+ if (!isPlainObject(raw))
62
+ return base;
63
+ if (isPlainObject(raw.rules)) {
64
+ for (const [rule, tier] of Object.entries(raw.rules)) {
65
+ if (typeof tier === 'string')
66
+ base.rules[rule] = tier;
67
+ }
68
+ }
69
+ base.protectedBranches = stringArray(raw.protectedBranches) ?? base.protectedBranches;
70
+ base.protectedPaths = stringArray(raw.protectedPaths) ?? base.protectedPaths;
71
+ base.guardPaths = stringArray(raw.guardPaths) ?? base.guardPaths;
72
+ if (isPlainObject(raw.journal)) {
73
+ const j = raw.journal;
74
+ base.journal = {
75
+ enabled: booleanOr(j.enabled, base.journal.enabled),
76
+ allowCounters: booleanOr(j.allowCounters, base.journal.allowCounters),
77
+ retainDays: positiveInt(j.retainDays) ?? base.journal.retainDays,
78
+ retainFiles: positiveInt(j.retainFiles) ?? base.journal.retainFiles,
79
+ };
80
+ }
81
+ if (isPlainObject(raw.workingDirContainment)) {
82
+ const c = raw.workingDirContainment;
83
+ base.workingDirContainment = {
84
+ enabled: booleanOr(c.enabled, base.workingDirContainment.enabled),
85
+ spillReads: booleanOr(c.spillReads, base.workingDirContainment.spillReads),
86
+ };
87
+ }
88
+ return base;
89
+ }
90
+ /**
91
+ * The legacy keys schema v2 replaced, and the rules each one used to gate.
92
+ * `gitProtection.enabled: false` switched the whole git family off, so all
93
+ * three git rules become `journal` (still recorded, never prompted).
94
+ */
95
+ const LEGACY_GIT_RULES = ['git.push.protected', 'git.tag.release', 'git.push.force'];
96
+ /** The four keys schema v2 replaced. They are stripped before the v2 merge. */
97
+ const LEGACY_KEYS = new Set(['gitProtection', 'publishBlocked', 'cwdContainment', 'credentialPaths']);
98
+ /**
99
+ * Translate a persisted `domains.guard` object that still carries the legacy
100
+ * `gitProtection` / `publishBlocked` / `cwdContainment` / `credentialPaths`
101
+ * keys onto schema v2.
102
+ *
103
+ * The legacy translation is applied FIRST and the remaining v2 document is
104
+ * merged on top of it: the legacy booleans are the older reading of the
105
+ * document, so an explicit v2 `rules` entry must win over the boolean that
106
+ * translated onto the same rule id. Nothing is mutated: the arrays, the rule
107
+ * table and the nested objects are fresh copies, which matters because
108
+ * `DEFAULT_CONFIG` is a module-level object shared by every caller.
109
+ *
110
+ * `migratedKeys` names the legacy keys that were actually read (a key that is
111
+ * absent, or a `gitProtection` that carries no setting at all, is not
112
+ * reported; `publishBlocked: true` / `cwdContainment: true` / an empty
113
+ * `credentialPaths` changed nothing and stay out). `apply()` journals one
114
+ * `config-legacy` note per boot from this list, so an operator can see why the
115
+ * gate changed instead of discovering it from a silent behaviour shift.
116
+ */
117
+ export function mapLegacyConfig(raw) {
118
+ if (!isPlainObject(raw))
119
+ return { config: mergeGuardConfig(undefined), migratedKeys: [] };
120
+ const migratedKeys = [];
121
+ // Stage 1 — translate the legacy keys onto the v2 shape.
122
+ const legacyRules = {};
123
+ let legacyBranches;
124
+ let legacyCredentialPaths;
125
+ const gitProtection = raw.gitProtection;
126
+ if (isPlainObject(gitProtection) && Object.keys(gitProtection).length > 0) {
127
+ migratedKeys.push('gitProtection');
128
+ if (gitProtection.enabled === false) {
129
+ for (const rule of LEGACY_GIT_RULES)
130
+ legacyRules[rule] = 'journal';
131
+ }
132
+ legacyBranches = stringArray(gitProtection.branches);
133
+ }
134
+ if (raw.publishBlocked === false) {
135
+ migratedKeys.push('publishBlocked');
136
+ legacyRules['pkg.publish'] = 'journal';
137
+ }
138
+ if (raw.cwdContainment === false) {
139
+ migratedKeys.push('cwdContainment');
140
+ legacyRules['fs.write.outside'] = 'journal';
141
+ }
142
+ const credentialPaths = stringArray(raw.credentialPaths);
143
+ if (credentialPaths) {
144
+ migratedKeys.push('credentialPaths');
145
+ legacyCredentialPaths = credentialPaths;
146
+ }
147
+ // Stage 2 — the v2 document wins key by key; `rules` merges per rule id so an
148
+ // explicit v2 tier beats the legacy boolean that produced the same id.
149
+ const v2 = {};
150
+ for (const [key, value] of Object.entries(raw)) {
151
+ if (!LEGACY_KEYS.has(key))
152
+ v2[key] = value;
153
+ }
154
+ const v2Rules = isPlainObject(v2.rules) ? v2.rules : {};
155
+ const config = mergeGuardConfig({
156
+ ...v2,
157
+ rules: { ...legacyRules, ...v2Rules },
158
+ protectedBranches: stringArray(v2.protectedBranches) ?? legacyBranches,
159
+ });
160
+ // The legacy credential paths are an ADDITION to whatever protected the
161
+ // document, never a replacement: narrowing coverage is exactly the failure
162
+ // this migration exists to prevent.
163
+ if (legacyCredentialPaths) {
164
+ config.protectedPaths = [...new Set([...config.protectedPaths, ...legacyCredentialPaths])];
165
+ }
166
+ return { config, migratedKeys };
167
+ }
168
+ /**
169
+ * Read the raw `domains.guard` value from the shared Maestro settings store. A
170
+ * missing store, an absent domain or an unreadable file all yield `undefined`:
171
+ * the caller falls back to the built-in defaults, so the guard's own
172
+ * protection may degrade in precision, never in coverage.
173
+ */
174
+ async function readGuardDomain(dshHome) {
175
+ try {
176
+ const mod = await import('../store/index.js');
177
+ const opts = dshHome === undefined ? undefined : { dshHome };
178
+ if (typeof mod.load === 'function') {
179
+ const doc = await mod.load(opts);
180
+ return doc?.domains?.guard;
181
+ }
182
+ if (typeof mod.get === 'function') {
183
+ return await mod.get('guard', opts);
184
+ }
185
+ }
186
+ catch (e) {
187
+ console.error('[dsh-maestro-guard] guard config read failed, using defaults:', e?.message);
188
+ }
189
+ return undefined;
190
+ }
191
+ /**
192
+ * The migrating read: run the persisted `domains.guard` through
193
+ * {@link mapLegacyConfig} and report which legacy keys were translated, so
194
+ * `apply()` can journal one `config-legacy` note per boot.
195
+ *
196
+ * `mapLegacyConfig` is pure and this is the only place a v1 document is
197
+ * translated at runtime — before this existed, `loadGuardConfig` called
198
+ * `mergeGuardConfig` directly and a persisted v1 document silently lost its
199
+ * `credentialPaths` and custom branch list.
200
+ */
201
+ export async function loadGuardConfigWithMigration(dshHome) {
202
+ return mapLegacyConfig(await readGuardDomain(dshHome));
203
+ }
204
+ /**
205
+ * The per-call read the guard handler performs on every tool call: a thin
206
+ * wrapper over {@link loadGuardConfigWithMigration} that returns only the
207
+ * config. No journaling happens here — that is a once-per-boot concern.
208
+ */
209
+ export async function loadGuardConfig(dshHome) {
210
+ return (await loadGuardConfigWithMigration(dshHome)).config;
211
+ }
@@ -0,0 +1,30 @@
1
+ import type { Tier } from './tiers.js';
2
+ import { type Verdict } from './rules.js';
3
+ /**
4
+ * Resolve a classified verdict to the tier actually enforced, after per-rule
5
+ * config overrides.
6
+ *
7
+ * `deny` is a structural floor: a rule that is never approvable cannot be
8
+ * downgraded by configuration, or the guard's tamper protection would be one
9
+ * settings edit away from disappearing.
10
+ *
11
+ * An entry that merely ECHOES the rule's built-in tier is not an override. The
12
+ * handler passes `cfg.rules`, which is the fully-populated table
13
+ * `DEFAULT_CONFIG` carries — every rule id appears there whether the user wrote
14
+ * one or not. Honouring those entries as overrides made every classify-level
15
+ * tier refinement unreachable: `pkg.publish` classifies a `--dry-run` as
16
+ * `journal`, but the table's `pkg.publish: ask` default then won, so production
17
+ * asked for a dry run while the corpus (asserting through `decide(v, {})`, a
18
+ * shape no production code used) reported `journal`. An entry that DIFFERS from
19
+ * `DEFAULT_TIERS` is an explicit user choice, and it raises or lowers the tier.
20
+ */
21
+ export declare function decide(v: Verdict, overrides: Partial<Record<string, Tier>>): {
22
+ tier: Tier;
23
+ ruleId: string;
24
+ };
25
+ /**
26
+ * Human-readable reason for a blocked/asked call: the rule id plus where it
27
+ * happened, then the (redacted, truncated) target. The rule id is the stable
28
+ * contract other surfaces key on; the prose after `::` is presentation only.
29
+ */
30
+ export declare function renderReason(v: Verdict, redactFn: (s: string) => string): string;
@@ -0,0 +1,38 @@
1
+ import { DEFAULT_TIERS } from './rules.js';
2
+ /**
3
+ * Resolve a classified verdict to the tier actually enforced, after per-rule
4
+ * config overrides.
5
+ *
6
+ * `deny` is a structural floor: a rule that is never approvable cannot be
7
+ * downgraded by configuration, or the guard's tamper protection would be one
8
+ * settings edit away from disappearing.
9
+ *
10
+ * An entry that merely ECHOES the rule's built-in tier is not an override. The
11
+ * handler passes `cfg.rules`, which is the fully-populated table
12
+ * `DEFAULT_CONFIG` carries — every rule id appears there whether the user wrote
13
+ * one or not. Honouring those entries as overrides made every classify-level
14
+ * tier refinement unreachable: `pkg.publish` classifies a `--dry-run` as
15
+ * `journal`, but the table's `pkg.publish: ask` default then won, so production
16
+ * asked for a dry run while the corpus (asserting through `decide(v, {})`, a
17
+ * shape no production code used) reported `journal`. An entry that DIFFERS from
18
+ * `DEFAULT_TIERS` is an explicit user choice, and it raises or lowers the tier.
19
+ */
20
+ export function decide(v, overrides) {
21
+ if (v.tier === 'deny')
22
+ return { tier: 'deny', ruleId: v.ruleId };
23
+ const override = overrides[v.ruleId];
24
+ if (override === undefined || override === DEFAULT_TIERS[v.ruleId]) {
25
+ return { tier: v.tier, ruleId: v.ruleId };
26
+ }
27
+ return { tier: override, ruleId: v.ruleId };
28
+ }
29
+ /**
30
+ * Human-readable reason for a blocked/asked call: the rule id plus where it
31
+ * happened, then the (redacted, truncated) target. The rule id is the stable
32
+ * contract other surfaces key on; the prose after `::` is presentation only.
33
+ */
34
+ export function renderReason(v, redactFn) {
35
+ const where = [v.repo, v.branch].filter(Boolean).join(':');
36
+ const head = `${v.ruleId}${where ? ` (${where})` : ''}`;
37
+ return `${head} :: ${redactFn(v.target).slice(0, 300)}`;
38
+ }
@@ -0,0 +1,6 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ export declare const name = "maestro-full-scan-tool";
3
+ export declare const inject: readonly ["tools"];
4
+ export declare function apply(ctx: Context, config?: {
5
+ rootPath?: string;
6
+ }): void;
@@ -0,0 +1,65 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ import { resolve, join } from 'node:path';
4
+ import { defineTool } from '@deepseek-ai/dsh-tools';
5
+ export const name = 'maestro-full-scan-tool';
6
+ export const inject = ['tools'];
7
+ function workspaceRootFor(c, e) {
8
+ if (c)
9
+ return c;
10
+ const cwd = e?.agent?.session?.header?.cwd;
11
+ return typeof cwd === 'string' && cwd ? cwd : process.cwd();
12
+ }
13
+ function resolveScript(root) {
14
+ // ONLY the requested root and its ancestors. The old version also walked up
15
+ // from process.cwd() as a last resort, so a call aimed at a directory without
16
+ // the script silently scanned the guard's OWN checkout and returned that
17
+ // repo's blacklist hits as if they were the caller's. Scanning a tree nobody
18
+ // asked about is worse than reporting nothing: the report looks authoritative.
19
+ let cur = resolve(root);
20
+ for (let i = 0; i < 8; i++) {
21
+ const cand = join(cur, 'scripts/enforce-rules.mjs');
22
+ if (existsSync(cand))
23
+ return cand;
24
+ const parent = resolve(cur, '..');
25
+ if (parent === cur)
26
+ break;
27
+ cur = parent;
28
+ }
29
+ return join(resolve(root), 'scripts/enforce-rules.mjs');
30
+ }
31
+ export function apply(ctx, config = {}) {
32
+ ctx.effect(() => ctx.tools.register(defineTool({
33
+ name: 'maestro_full_scan',
34
+ description: 'Run full enforce-rules scan (blacklist + protection + publish gate) and return report. No approval needed; read-only.',
35
+ parameters: { rootPath: { type: 'string', description: 'workspace root override' } },
36
+ output: { schema: { type: 'object', additionalProperties: true, properties: { ok: { type: 'boolean', required: true }, report: { type: 'string', required: true }, code: { type: 'number', required: true } } }, render: (_a, v) => [{ type: 'text', text: v.report.slice(0, 4000) }] },
37
+ async execute(args, exec) {
38
+ const root = workspaceRootFor(args?.rootPath ?? config.rootPath, exec);
39
+ const script = resolveScript(root);
40
+ if (!existsSync(script)) {
41
+ return { ok: true, report: `enforce-rules: no script found at ${script} (standalone repo, skipping)`, code: 0 };
42
+ }
43
+ const run = (extra) => {
44
+ // cwd = the scanned root: the script resolves its own targets relative to
45
+ // it, so inheriting the host's cwd made the report describe the wrong tree.
46
+ const r = spawnSync('node', [script, ...extra], { encoding: 'utf-8', timeout: 30000, cwd: resolve(root) });
47
+ return { code: r.status ?? 0, out: (r.stdout ?? '') + (r.stderr ?? '') };
48
+ };
49
+ let combined = '';
50
+ let ok = true;
51
+ const a = run([]);
52
+ combined += `$ node scripts/enforce-rules.mjs\n${a.out}\n`;
53
+ if (a.code !== 0)
54
+ ok = false;
55
+ const b = run(['--check-protection']);
56
+ combined += `$ node scripts/enforce-rules.mjs --check-protection\n${b.out}\n`;
57
+ if (b.code !== 0)
58
+ combined += `[warn] --check-protection non-zero (may need gh auth)\n`;
59
+ const c = run(['--check-publish']);
60
+ combined += `$ node scripts/enforce-rules.mjs --check-publish\n${c.out}\n`;
61
+ // do not fail on --check-publish when no tag
62
+ return { ok, report: combined.slice(0, 12000), code: ok ? 0 : 1 };
63
+ }
64
+ })));
65
+ }
@@ -0,0 +1,85 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import { PermissionPolicy } from './permission-policy.js';
3
+ import { Journal, type AskOutcome } from './journal.js';
4
+ import { type GuardConfigV2 } from './config.js';
5
+ import './validators.js';
6
+ import type { GuardToolExecution, GuardPreToolDecision } from './augment.js';
7
+ export interface GuardDeps {
8
+ journal: Journal;
9
+ policy: PermissionPolicy;
10
+ readConfig: () => Promise<GuardConfigV2>;
11
+ branchOf?: (dir: string) => string | undefined;
12
+ requestApproval: (req: {
13
+ agent?: unknown;
14
+ toolName: string;
15
+ callId?: string;
16
+ reason: string;
17
+ signal?: AbortSignal;
18
+ }) => Promise<AskOutcome | ApprovalResult>;
19
+ now?: () => number;
20
+ }
21
+ /**
22
+ * What the approval transport observed, plus why when it failed. A transport
23
+ * that only knows the outcome may return the bare {@link AskOutcome} string; one
24
+ * that caught an error returns this shape so the message reaches the journal.
25
+ */
26
+ export interface ApprovalResult {
27
+ outcome: AskOutcome;
28
+ /** The thrown message, journaled (and redacted) with the `error` outcome. */
29
+ note?: string;
30
+ }
31
+ /** The rule id of the `tools/pre-execute` contract denial (spec §8). */
32
+ export declare const CONTRACT_MISMATCH_RULE = "contract-mismatch";
33
+ /**
34
+ * The DSH `tools/pre-execute` contract the guard reads: a tool NAME and an
35
+ * arguments field. The guard accepts both spellings of each (`name`/`tool`,
36
+ * `args`/`arguments`), but a payload carrying neither is a DSH upgrade that
37
+ * renames a field — and a renamed `args` silently disables EVERY command rule,
38
+ * because `extractCommandText(undefined)` yields no command to classify.
39
+ * Spec §8: an unknown payload is a denial, never a pass.
40
+ *
41
+ * A `null` argument list counts as ABSENT, not as an empty one: `??` already
42
+ * falls through on null, so treating it as present would let a null `args`
43
+ * reach `extractCommandText` and allow a command the rules never saw.
44
+ */
45
+ export declare function contractMismatch(exec: unknown): string | undefined;
46
+ /**
47
+ * The guard pipeline: classify the call into a rule id, resolve the rule's tier,
48
+ * journal the decision, then act — `next()` for allow/journal, a deny decision
49
+ * for deny, and for ask a NATIVE approval request to DSH's `approval` service.
50
+ *
51
+ * Every branch journals before it returns, and the journal is optional in the
52
+ * failure direction only: a journal write that fails is logged and changes no
53
+ * decision (see `Journal.append`).
54
+ */
55
+ export declare function createGuardHandler(deps: GuardDeps): (exec: GuardToolExecution, next: () => Promise<GuardPreToolDecision>) => Promise<GuardPreToolDecision>;
56
+ /**
57
+ * Branch of the repo a command targets. The caller resolves the repo directory
58
+ * itself (`git -C` / `cd` handling lives in `rules.ts`); an EMPTY directory is
59
+ * the "no resolvable repo" sentinel and must never be turned into
60
+ * `git -C ""`, which would silently resolve to the session cwd and re-create
61
+ * the false positives the repo resolution exists to remove.
62
+ */
63
+ export declare function branchOf(dir: string): string | undefined;
64
+ /**
65
+ * Journal an ALREADY-COMPUTED migration result. Split out of
66
+ * {@link journalLegacyConfigMigration} so `apply()` can reuse the single
67
+ * boot-time config read for both the journal knobs and this note. Never throws.
68
+ */
69
+ export declare function journalConfigMigration(journal: Journal, migratedKeys: string[]): Promise<string[]>;
70
+ /**
71
+ * Journal the schema-v1 → v2 config migration ONCE per boot. The handler's
72
+ * per-call config read (`loadGuardConfig`) is deliberately mute, so without
73
+ * this a persisted `domains.guard` written for schema v1 would change what the
74
+ * guard gates without leaving any trace of why.
75
+ *
76
+ * Exported so the boot effect body is unit-testable. Never throws: a failed
77
+ * read or a failed journal write must not stop the guard from booting, and
78
+ * `migratedKeys.length === 0` writes nothing at all.
79
+ */
80
+ export declare function journalLegacyConfigMigration(journal: Journal, dshHome?: string): Promise<string[]>;
81
+ declare const _default: {
82
+ inject: readonly ["tools"];
83
+ apply(ctx: Context): Promise<void>;
84
+ };
85
+ export default _default;