@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,54 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import { type Journal, type JournalEntry } from './journal.js';
3
+ import type { GuardConfigV2 } from './config.js';
4
+ import type { Tier } from './tiers.js';
5
+ /** How many recent decisions `maestro_guard_status` returns. */
6
+ export declare const RECENT_LIMIT = 20;
7
+ /**
8
+ * How many journal entries `maestro_guard_stats` folds. `Journal.read` walks
9
+ * the archives newest → oldest, so this really is the last N decisions rather
10
+ * than whatever the newest archive happens to hold.
11
+ */
12
+ export declare const STATS_LIMIT = 1000;
13
+ export interface StatusResult {
14
+ ok: true;
15
+ recent: JournalEntry[];
16
+ rules: Record<string, Tier>;
17
+ journalPath: string;
18
+ enabled: boolean;
19
+ }
20
+ export interface StatsResult {
21
+ ok: true;
22
+ byRule: Record<string, number>;
23
+ byTier: Record<string, number>;
24
+ byOutcome: Record<string, number>;
25
+ askMs: {
26
+ p50: number;
27
+ p90: number;
28
+ max: number;
29
+ };
30
+ /** Timestamp of the oldest folded entry — the start of the counted window. */
31
+ since?: string;
32
+ }
33
+ export interface StatusDeps {
34
+ journal: Journal;
35
+ config: () => Promise<GuardConfigV2>;
36
+ }
37
+ /**
38
+ * The two READ-ONLY journal tools. Every result is a fresh plain object built
39
+ * from leaf fields (never a live `Journal`), every failure is swallowed into an
40
+ * empty-but-`ok` answer, and neither tool can write, approve or re-decide
41
+ * anything: an operator can inspect the guard without being able to change it.
42
+ */
43
+ export declare function createStatusTools(deps: StatusDeps): {
44
+ status(): Promise<StatusResult>;
45
+ stats(): Promise<StatsResult>;
46
+ };
47
+ /**
48
+ * Register the read-only journal tools on the host tool registry. Each
49
+ * registration is its own reversible effect, matching `full-scan-tool.ts`;
50
+ * neither tool declares parameters, and both carry the `output: { schema,
51
+ * render }` pair the registry requires (a definition without it fails to
52
+ * register).
53
+ */
54
+ export declare function applyStatusTools(ctx: Context, deps: StatusDeps): void;
@@ -0,0 +1,195 @@
1
+ import { defineTool } from '@deepseek-ai/dsh-tools';
2
+ import { RULE_IDS } from './rules.js';
3
+ /** How many recent decisions `maestro_guard_status` returns. */
4
+ export const RECENT_LIMIT = 20;
5
+ /**
6
+ * How many journal entries `maestro_guard_stats` folds. `Journal.read` walks
7
+ * the archives newest → oldest, so this really is the last N decisions rather
8
+ * than whatever the newest archive happens to hold.
9
+ */
10
+ export const STATS_LIMIT = 1000;
11
+ /**
12
+ * The rule ids a DECISION can carry — the closed `RULE_IDS` set. The journal
13
+ * also holds the guard's own bookkeeping rows (`counters`, `config-legacy`,
14
+ * `guard.migration`, `policy.deny`), whose ids are not rules: folding them into
15
+ * `byRule`/`byTier` would report decisions attributed to rule ids that exist
16
+ * nowhere in the rule table, and would invent one decision per counter flush.
17
+ */
18
+ const DECISION_RULES = new Set(RULE_IDS);
19
+ /** Nearest-rank percentile over an ASCENDING numeric array (must be non-empty). */
20
+ function percentile(sorted, q) {
21
+ const rank = Math.ceil(q * sorted.length);
22
+ return sorted[Math.min(sorted.length, Math.max(1, rank)) - 1];
23
+ }
24
+ function bump(bag, key) {
25
+ if (key === undefined)
26
+ return;
27
+ bag[key] = (bag[key] ?? 0) + 1;
28
+ }
29
+ /**
30
+ * The two READ-ONLY journal tools. Every result is a fresh plain object built
31
+ * from leaf fields (never a live `Journal`), every failure is swallowed into an
32
+ * empty-but-`ok` answer, and neither tool can write, approve or re-decide
33
+ * anything: an operator can inspect the guard without being able to change it.
34
+ */
35
+ export function createStatusTools(deps) {
36
+ return {
37
+ async status() {
38
+ const cfg = await deps.config().catch(() => undefined);
39
+ return {
40
+ ok: true,
41
+ recent: await deps.journal.read(RECENT_LIMIT),
42
+ // A copy: the caller must not be able to mutate the live config table.
43
+ rules: { ...(cfg?.rules ?? {}) },
44
+ journalPath: deps.journal.path,
45
+ enabled: deps.journal.enabled,
46
+ };
47
+ },
48
+ async stats() {
49
+ const entries = await deps.journal.read(STATS_LIMIT);
50
+ const byRule = {};
51
+ const byTier = {};
52
+ const byOutcome = {};
53
+ const asks = [];
54
+ for (const entry of entries) {
55
+ // `read()` keeps any line that parses, and `null` parses: a non-object
56
+ // entry carries no field at all, so reading `entry.rule` off it would
57
+ // throw out of a tool that promises an empty-but-`ok` answer.
58
+ if (entry === null || typeof entry !== 'object')
59
+ continue;
60
+ // Only a row whose rule is one of the closed rule ids is a DECISION.
61
+ // `counters` (the periodic aggregate), `config-legacy`, `guard.migration`
62
+ // and `policy.deny` are the guard's own bookkeeping: folding them in
63
+ // invented one decision per flush and attributed several to rule ids
64
+ // that are not in the rule table.
65
+ if (typeof entry.rule === 'string' && DECISION_RULES.has(entry.rule)) {
66
+ bump(byRule, entry.rule);
67
+ bump(byTier, entry.tier);
68
+ }
69
+ bump(byOutcome, entry.outcome);
70
+ if (typeof entry.askMs === 'number' && Number.isFinite(entry.askMs))
71
+ asks.push(entry.askMs);
72
+ }
73
+ asks.sort((a, b) => a - b);
74
+ const oldest = entries[entries.length - 1];
75
+ const result = {
76
+ ok: true,
77
+ byRule,
78
+ byTier,
79
+ byOutcome,
80
+ askMs: asks.length === 0
81
+ ? { p50: 0, p90: 0, max: 0 }
82
+ : { p50: percentile(asks, 0.5), p90: percentile(asks, 0.9), max: asks[asks.length - 1] },
83
+ };
84
+ // `read()` keeps any line that parses, so the oldest entry in the window
85
+ // can be a foreign `{}` with no `ts`, or one whose `ts` is not a string.
86
+ // Assigning that would create an OWN `since: undefined` (or a non-string
87
+ // one), and `@deepseek-ai/dsh-tools` rejects both: it snapshots every
88
+ // successful body with `snapshotJsonValue` before schema validation, so
89
+ // an own undefined property fails as "value is not lossless JSON" instead
90
+ // of answering `ok: true`. Omit the field — `output.schema` requires only
91
+ // `ok`. (No other property here is conditionally assigned: `byRule`,
92
+ // `byTier` and `byOutcome` only ever take numeric values from `bump()`,
93
+ // and `askMs` only takes numbers.)
94
+ if (typeof oldest?.ts === 'string')
95
+ result.since = oldest.ts;
96
+ return result;
97
+ },
98
+ };
99
+ }
100
+ function formatCounts(bag) {
101
+ const keys = Object.keys(bag).sort();
102
+ return keys.length === 0 ? '(none)' : keys.map((k) => `${k}=${bag[k]}`).join(', ');
103
+ }
104
+ function renderStatus(v) {
105
+ const lines = [
106
+ `guard status — journal ${v.journalPath} (${v.enabled ? 'enabled' : 'disabled'}), ${v.recent.length} recent decision(s)`,
107
+ ];
108
+ for (const raw of v.recent) {
109
+ // A foreign or half-written line parses to `{}` — or to a bare `null`,
110
+ // which `JSON.parse` accepts — and `read()` deliberately keeps whatever
111
+ // parses: presentation must degrade, never throw.
112
+ const e = raw !== null && typeof raw === 'object' ? raw : {};
113
+ const tier = typeof e.tier === 'string' ? e.tier.padEnd(7) : '? ';
114
+ const rule = typeof e.rule === 'string' ? e.rule : '?';
115
+ const ask = typeof e.askMs === 'number' ? ` ${e.askMs}ms` : '';
116
+ const outcome = e.outcome === undefined ? '' : ` ${e.outcome}`;
117
+ lines.push(`${e.ts} ${tier} ${rule} ${e.tool} ${e.target}${outcome}${ask}`);
118
+ }
119
+ lines.push(`rules: ${formatCounts(v.rules)}`);
120
+ return lines.join('\n').slice(0, 4000);
121
+ }
122
+ function renderStats(v) {
123
+ const ask = v.askMs;
124
+ return [
125
+ `guard stats${v.since === undefined ? '' : ` since ${v.since}`}`,
126
+ `byRule: ${formatCounts(v.byRule)}`,
127
+ `byTier: ${formatCounts(v.byTier)}`,
128
+ `byOutcome: ${formatCounts(v.byOutcome)}`,
129
+ `askMs: p50=${ask.p50} p90=${ask.p90} max=${ask.max}`,
130
+ ].join('\n').slice(0, 4000);
131
+ }
132
+ /**
133
+ * Register the read-only journal tools on the host tool registry. Each
134
+ * registration is its own reversible effect, matching `full-scan-tool.ts`;
135
+ * neither tool declares parameters, and both carry the `output: { schema,
136
+ * render }` pair the registry requires (a definition without it fails to
137
+ * register).
138
+ */
139
+ export function applyStatusTools(ctx, deps) {
140
+ const tools = createStatusTools(deps);
141
+ ctx.effect(() => ctx.tools.register(defineTool({
142
+ name: 'maestro_guard_status',
143
+ description: 'Show the guard journal: the most recent decisions, the effective rule tiers, and where the journal lives. Read-only.',
144
+ parameters: {},
145
+ output: {
146
+ schema: {
147
+ type: 'object',
148
+ additionalProperties: false,
149
+ properties: {
150
+ ok: { type: 'boolean', required: true },
151
+ recent: { type: 'array', required: true },
152
+ rules: { type: 'object', additionalProperties: true, required: true },
153
+ journalPath: { type: 'string', required: true },
154
+ enabled: { type: 'boolean', required: true },
155
+ },
156
+ },
157
+ render: (_args, value) => [{ type: 'text', text: renderStatus(value) }],
158
+ },
159
+ async execute() {
160
+ return tools.status();
161
+ },
162
+ })), 'guard-status-tool');
163
+ ctx.effect(() => ctx.tools.register(defineTool({
164
+ name: 'maestro_guard_stats',
165
+ description: 'Aggregate the guard journal by rule, tier and outcome, with the ask-approval latency percentiles. Read-only. byRule/byTier count rule decisions only (the guard\'s own bookkeeping rows are excluded); byOutcome counts every row.',
166
+ parameters: {},
167
+ output: {
168
+ schema: {
169
+ type: 'object',
170
+ additionalProperties: false,
171
+ properties: {
172
+ ok: { type: 'boolean', required: true },
173
+ byRule: { type: 'object', additionalProperties: true, required: true },
174
+ byTier: { type: 'object', additionalProperties: true, required: true },
175
+ byOutcome: { type: 'object', additionalProperties: true, required: true },
176
+ askMs: {
177
+ type: 'object',
178
+ additionalProperties: false,
179
+ required: true,
180
+ properties: {
181
+ p50: { type: 'number', required: true },
182
+ p90: { type: 'number', required: true },
183
+ max: { type: 'number', required: true },
184
+ },
185
+ },
186
+ since: { type: 'string' },
187
+ },
188
+ },
189
+ render: (_args, value) => [{ type: 'text', text: renderStats(value) }],
190
+ },
191
+ async execute() {
192
+ return tools.stats();
193
+ },
194
+ })), 'guard-stats-tool');
195
+ }
@@ -0,0 +1,2 @@
1
+ export type Tier = 'allow' | 'journal' | 'ask' | 'deny';
2
+ export declare const TIERS: readonly Tier[];
@@ -0,0 +1 @@
1
+ export const TIERS = ['allow', 'journal', 'ask', 'deny'];
@@ -0,0 +1,4 @@
1
+ import { type DomainValidator } from '../store/index.js';
2
+ declare const guardValidator: DomainValidator;
3
+ declare const guardBlacklistValidator: DomainValidator;
4
+ export { guardValidator, guardBlacklistValidator };
@@ -0,0 +1,45 @@
1
+ import { defineDomain } from '../store/index.js';
2
+ const guardValidator = {
3
+ parse(v) {
4
+ if (v == null)
5
+ return { ok: true };
6
+ if (typeof v !== 'object' || Array.isArray(v))
7
+ return { ok: false, error: 'guard must be object' };
8
+ if (typeof v.publishBlocked !== 'undefined' && typeof v.publishBlocked !== 'boolean')
9
+ return { ok: false, error: 'publishBlocked boolean' };
10
+ if (v.gitProtection) {
11
+ const gp = v.gitProtection;
12
+ if (typeof gp.enabled !== 'boolean')
13
+ return { ok: false, error: 'gitProtection.enabled boolean' };
14
+ if (!Array.isArray(gp.branches) || gp.branches.some((b) => typeof b !== 'string' || !b.trim()))
15
+ return { ok: false, error: 'branches string[]' };
16
+ }
17
+ if (v.credentialPaths && (!Array.isArray(v.credentialPaths) || v.credentialPaths.some((p) => typeof p !== 'string')))
18
+ return { ok: false, error: 'credentialPaths string[]' };
19
+ if (v.cwdContainment !== undefined && typeof v.cwdContainment !== 'boolean')
20
+ return { ok: false, error: 'cwdContainment boolean' };
21
+ return { ok: true };
22
+ }
23
+ };
24
+ try {
25
+ defineDomain('guard', guardValidator);
26
+ }
27
+ catch { }
28
+ const guardBlacklistValidator = {
29
+ parse(v) {
30
+ if (v == null)
31
+ return { ok: true };
32
+ if (typeof v !== 'object')
33
+ return { ok: false, error: 'guardBlacklist object' };
34
+ if (v.patterns && !Array.isArray(v.patterns))
35
+ return { ok: false, error: 'patterns array' };
36
+ if (v.placeholders && typeof v.placeholders !== 'object')
37
+ return { ok: false, error: 'placeholders object' };
38
+ return { ok: true };
39
+ }
40
+ };
41
+ try {
42
+ defineDomain('guardBlacklist', guardBlacklistValidator);
43
+ }
44
+ catch { }
45
+ export { guardValidator, guardBlacklistValidator };
@@ -0,0 +1,90 @@
1
+ export interface HealthState {
2
+ up: boolean;
3
+ httpCode?: number;
4
+ error?: string;
5
+ degraded?: boolean;
6
+ logTail?: string;
7
+ /** Boot verdict for the unit start this poll observed (see bootFreshness). */
8
+ bootPhase?: BootFreshness;
9
+ }
10
+ /**
11
+ * Wall-clock epoch ms of the current dsh-web unit start, or undefined when
12
+ * systemd does not manage the unit (portable host) or the lookup is disabled.
13
+ * `ActiveEnterTimestampMonotonic` is deliberately gone: a monotonic reading
14
+ * cannot be compared with a timestamp-less append-only log.
15
+ */
16
+ export declare function getActiveEnterMs(): number | undefined;
17
+ /** @deprecated kept for callers; identical to getActiveEnterMs(). */
18
+ export declare function getActiveEnterWallMs(): number | undefined;
19
+ export interface PollHealthOpts {
20
+ fetch?: () => Promise<{
21
+ status: number;
22
+ text: () => Promise<string>;
23
+ }>;
24
+ psAlive?: () => Promise<boolean>;
25
+ logTail?: () => Promise<string>;
26
+ url?: string;
27
+ timeoutMs?: number;
28
+ /** injectable for tests — overrides the systemctl lookup */
29
+ getActiveEnterMs?: () => number | undefined;
30
+ /** Wall-clock epoch ms of the current dsh-web unit start (see bootFreshness). */
31
+ activeEnterAtMs?: number;
32
+ /** Boot budget in ms; a younger boot without its own success marker is 'booting'. */
33
+ bootGraceMs?: number;
34
+ }
35
+ export type BootFreshness = 'unknown' | 'booting' | 'settled';
36
+ /**
37
+ * Decide whether the current unit start can already be judged.
38
+ *
39
+ * `activeEnterAtMs` is WALL-CLOCK epoch ms from
40
+ * `systemctl --user show -p ActiveEnterTimestamp dsh-web.service`.
41
+ * Deliberately NOT `ActiveEnterTimestampMonotonic`: a monotonic reading can
42
+ * never be compared against an append-only log that carries no timestamps, and
43
+ * that mismatch is why the previous scan filter silently fell through and let
44
+ * the previous boot's crash text be read as this boot's (incident 2026-09-13).
45
+ *
46
+ * - 'unknown' — no boot anchor (systemd absent, lookup disabled, clock skew):
47
+ * behave exactly as before the fix; never suppress anything.
48
+ * - 'booting' — the unit started less than `bootGraceMs` ago and has not yet
49
+ * proven itself by actually serving a request: weak failures are
50
+ * suppressed and log lines are inconclusive.
51
+ * - 'settled' — the boot proved itself, or the grace window expired: judge
52
+ * normally.
53
+ */
54
+ export declare function bootFreshness(opts: {
55
+ activeEnterAtMs?: number;
56
+ now: number;
57
+ bootGraceMs: number;
58
+ /**
59
+ * Did THIS poll receive a serving response (200/401)?
60
+ *
61
+ * Deliberately not "the log shows `dsh web: http`": that line is printed the
62
+ * moment the raw webserver binds, seconds into a boot that can take ~90s to
63
+ * finish loading the plugin tree. Crediting it as proof ended the grace while
64
+ * the proxy in front was still coming up, so probes that hung during the rest
65
+ * of the boot were judged as crashes — the 2026-09-13 restart loop.
66
+ */
67
+ probeSucceeded: boolean;
68
+ }): BootFreshness;
69
+ /**
70
+ * Classify a fetch failure by what it says about the process.
71
+ *
72
+ * 'refused' — nothing is listening, so the process is gone: a strong down
73
+ * signal that must never be masked by a boot grace.
74
+ * 'timeout' — something may be alive but slow: weak, only meaningful once the
75
+ * boot grace expired.
76
+ * 'other' — anything we cannot attribute.
77
+ *
78
+ * Walks the `cause` chain because undici surfaces a refused connection as
79
+ * `TypeError: fetch failed` with the real `ECONNREFUSED` on `cause`.
80
+ */
81
+ export declare function classifyFetchFailure(err: unknown): 'refused' | 'timeout' | 'other';
82
+ export declare const SUCCESS_MARKER = "dsh web: http";
83
+ /** Index of the last boot-boundary line in the tail, or -1 when it predates the tail. */
84
+ export declare function lastBootBoundaryIndex(lines: string[]): number;
85
+ /** Index of the last "dsh web: http" success marker (`lowerLines` must be lower-cased). */
86
+ export declare function lastSuccessMarkerIndex(lowerLines: string[]): number;
87
+ /** Wall-clock ms parsed from a boot-boundary line, or undefined when unparseable. */
88
+ export declare function parseBootBoundaryMs(line: string): number | undefined;
89
+ export declare function pollHealth(opts?: PollHealthOpts): Promise<HealthState>;
90
+ export declare function collectLogTail(): Promise<string>;