@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,229 @@
1
+ import type { Context } from '@deepseek-ai/cordis'
2
+ import { defineTool } from '@deepseek-ai/dsh-tools'
3
+ import { type Journal, type JournalEntry } from './journal.js'
4
+ import { RULE_IDS } from './rules.js'
5
+ import type { GuardConfigV2 } from './config.js'
6
+ import type { Tier } from './tiers.js'
7
+
8
+ /** How many recent decisions `maestro_guard_status` returns. */
9
+ export const RECENT_LIMIT = 20
10
+ /**
11
+ * How many journal entries `maestro_guard_stats` folds. `Journal.read` walks
12
+ * the archives newest → oldest, so this really is the last N decisions rather
13
+ * than whatever the newest archive happens to hold.
14
+ */
15
+ export const STATS_LIMIT = 1000
16
+
17
+ /**
18
+ * The rule ids a DECISION can carry — the closed `RULE_IDS` set. The journal
19
+ * also holds the guard's own bookkeeping rows (`counters`, `config-legacy`,
20
+ * `guard.migration`, `policy.deny`), whose ids are not rules: folding them into
21
+ * `byRule`/`byTier` would report decisions attributed to rule ids that exist
22
+ * nowhere in the rule table, and would invent one decision per counter flush.
23
+ */
24
+ const DECISION_RULES = new Set<string>(RULE_IDS)
25
+
26
+ export interface StatusResult {
27
+ ok: true
28
+ recent: JournalEntry[]
29
+ rules: Record<string, Tier>
30
+ journalPath: string
31
+ enabled: boolean
32
+ }
33
+
34
+ export interface StatsResult {
35
+ ok: true
36
+ byRule: Record<string, number>
37
+ byTier: Record<string, number>
38
+ byOutcome: Record<string, number>
39
+ askMs: { p50: number; p90: number; max: number }
40
+ /** Timestamp of the oldest folded entry — the start of the counted window. */
41
+ since?: string
42
+ }
43
+
44
+ export interface StatusDeps {
45
+ journal: Journal
46
+ config: () => Promise<GuardConfigV2>
47
+ }
48
+
49
+ /** Nearest-rank percentile over an ASCENDING numeric array (must be non-empty). */
50
+ function percentile(sorted: number[], q: number): number {
51
+ const rank = Math.ceil(q * sorted.length)
52
+ return sorted[Math.min(sorted.length, Math.max(1, rank)) - 1]
53
+ }
54
+
55
+ function bump(bag: Record<string, number>, key: string | undefined): void {
56
+ if (key === undefined) return
57
+ bag[key] = (bag[key] ?? 0) + 1
58
+ }
59
+
60
+ /**
61
+ * The two READ-ONLY journal tools. Every result is a fresh plain object built
62
+ * from leaf fields (never a live `Journal`), every failure is swallowed into an
63
+ * empty-but-`ok` answer, and neither tool can write, approve or re-decide
64
+ * anything: an operator can inspect the guard without being able to change it.
65
+ */
66
+ export function createStatusTools(deps: StatusDeps): { status(): Promise<StatusResult>; stats(): Promise<StatsResult> } {
67
+ return {
68
+ async status(): Promise<StatusResult> {
69
+ const cfg = await deps.config().catch(() => undefined)
70
+ return {
71
+ ok: true,
72
+ recent: await deps.journal.read(RECENT_LIMIT),
73
+ // A copy: the caller must not be able to mutate the live config table.
74
+ rules: { ...(cfg?.rules ?? {}) },
75
+ journalPath: deps.journal.path,
76
+ enabled: deps.journal.enabled,
77
+ }
78
+ },
79
+
80
+ async stats(): Promise<StatsResult> {
81
+ const entries = await deps.journal.read(STATS_LIMIT)
82
+ const byRule: Record<string, number> = {}
83
+ const byTier: Record<string, number> = {}
84
+ const byOutcome: Record<string, number> = {}
85
+ const asks: number[] = []
86
+ for (const entry of entries) {
87
+ // `read()` keeps any line that parses, and `null` parses: a non-object
88
+ // entry carries no field at all, so reading `entry.rule` off it would
89
+ // throw out of a tool that promises an empty-but-`ok` answer.
90
+ if (entry === null || typeof entry !== 'object') continue
91
+ // Only a row whose rule is one of the closed rule ids is a DECISION.
92
+ // `counters` (the periodic aggregate), `config-legacy`, `guard.migration`
93
+ // and `policy.deny` are the guard's own bookkeeping: folding them in
94
+ // invented one decision per flush and attributed several to rule ids
95
+ // that are not in the rule table.
96
+ if (typeof entry.rule === 'string' && DECISION_RULES.has(entry.rule)) {
97
+ bump(byRule, entry.rule)
98
+ bump(byTier, entry.tier)
99
+ }
100
+ bump(byOutcome, entry.outcome)
101
+ if (typeof entry.askMs === 'number' && Number.isFinite(entry.askMs)) asks.push(entry.askMs)
102
+ }
103
+ asks.sort((a, b) => a - b)
104
+ const oldest = entries[entries.length - 1]
105
+ const result: StatsResult = {
106
+ ok: true,
107
+ byRule,
108
+ byTier,
109
+ byOutcome,
110
+ askMs: asks.length === 0
111
+ ? { p50: 0, p90: 0, max: 0 }
112
+ : { p50: percentile(asks, 0.5), p90: percentile(asks, 0.9), max: asks[asks.length - 1] },
113
+ }
114
+ // `read()` keeps any line that parses, so the oldest entry in the window
115
+ // can be a foreign `{}` with no `ts`, or one whose `ts` is not a string.
116
+ // Assigning that would create an OWN `since: undefined` (or a non-string
117
+ // one), and `@deepseek-ai/dsh-tools` rejects both: it snapshots every
118
+ // successful body with `snapshotJsonValue` before schema validation, so
119
+ // an own undefined property fails as "value is not lossless JSON" instead
120
+ // of answering `ok: true`. Omit the field — `output.schema` requires only
121
+ // `ok`. (No other property here is conditionally assigned: `byRule`,
122
+ // `byTier` and `byOutcome` only ever take numeric values from `bump()`,
123
+ // and `askMs` only takes numbers.)
124
+ if (typeof oldest?.ts === 'string') result.since = oldest.ts
125
+ return result
126
+ },
127
+ }
128
+ }
129
+
130
+ function formatCounts(bag: Record<string, number>): string {
131
+ const keys = Object.keys(bag).sort()
132
+ return keys.length === 0 ? '(none)' : keys.map((k) => `${k}=${bag[k]}`).join(', ')
133
+ }
134
+
135
+ function renderStatus(v: any): string {
136
+ const lines = [
137
+ `guard status — journal ${v.journalPath} (${v.enabled ? 'enabled' : 'disabled'}), ${v.recent.length} recent decision(s)`,
138
+ ]
139
+ for (const raw of v.recent) {
140
+ // A foreign or half-written line parses to `{}` — or to a bare `null`,
141
+ // which `JSON.parse` accepts — and `read()` deliberately keeps whatever
142
+ // parses: presentation must degrade, never throw.
143
+ const e = raw !== null && typeof raw === 'object' ? raw : {}
144
+ const tier = typeof e.tier === 'string' ? e.tier.padEnd(7) : '? '
145
+ const rule = typeof e.rule === 'string' ? e.rule : '?'
146
+ const ask = typeof e.askMs === 'number' ? ` ${e.askMs}ms` : ''
147
+ const outcome = e.outcome === undefined ? '' : ` ${e.outcome}`
148
+ lines.push(`${e.ts} ${tier} ${rule} ${e.tool} ${e.target}${outcome}${ask}`)
149
+ }
150
+ lines.push(`rules: ${formatCounts(v.rules)}`)
151
+ return lines.join('\n').slice(0, 4000)
152
+ }
153
+
154
+ function renderStats(v: any): string {
155
+ const ask = v.askMs
156
+ return [
157
+ `guard stats${v.since === undefined ? '' : ` since ${v.since}`}`,
158
+ `byRule: ${formatCounts(v.byRule)}`,
159
+ `byTier: ${formatCounts(v.byTier)}`,
160
+ `byOutcome: ${formatCounts(v.byOutcome)}`,
161
+ `askMs: p50=${ask.p50} p90=${ask.p90} max=${ask.max}`,
162
+ ].join('\n').slice(0, 4000)
163
+ }
164
+
165
+ /**
166
+ * Register the read-only journal tools on the host tool registry. Each
167
+ * registration is its own reversible effect, matching `full-scan-tool.ts`;
168
+ * neither tool declares parameters, and both carry the `output: { schema,
169
+ * render }` pair the registry requires (a definition without it fails to
170
+ * register).
171
+ */
172
+ export function applyStatusTools(ctx: Context, deps: StatusDeps): void {
173
+ const tools = createStatusTools(deps)
174
+ ctx.effect(() => ctx.tools.register(defineTool({
175
+ name: 'maestro_guard_status',
176
+ description: 'Show the guard journal: the most recent decisions, the effective rule tiers, and where the journal lives. Read-only.',
177
+ parameters: {},
178
+ output: {
179
+ schema: {
180
+ type: 'object',
181
+ additionalProperties: false,
182
+ properties: {
183
+ ok: { type: 'boolean', required: true },
184
+ recent: { type: 'array', required: true },
185
+ rules: { type: 'object', additionalProperties: true, required: true },
186
+ journalPath: { type: 'string', required: true },
187
+ enabled: { type: 'boolean', required: true },
188
+ },
189
+ },
190
+ render: (_args: any, value: any) => [{ type: 'text', text: renderStatus(value) }],
191
+ },
192
+ async execute(): Promise<any> {
193
+ return tools.status()
194
+ },
195
+ })), 'guard-status-tool')
196
+
197
+ ctx.effect(() => ctx.tools.register(defineTool({
198
+ name: 'maestro_guard_stats',
199
+ 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.',
200
+ parameters: {},
201
+ output: {
202
+ schema: {
203
+ type: 'object',
204
+ additionalProperties: false,
205
+ properties: {
206
+ ok: { type: 'boolean', required: true },
207
+ byRule: { type: 'object', additionalProperties: true, required: true },
208
+ byTier: { type: 'object', additionalProperties: true, required: true },
209
+ byOutcome: { type: 'object', additionalProperties: true, required: true },
210
+ askMs: {
211
+ type: 'object',
212
+ additionalProperties: false,
213
+ required: true,
214
+ properties: {
215
+ p50: { type: 'number', required: true },
216
+ p90: { type: 'number', required: true },
217
+ max: { type: 'number', required: true },
218
+ },
219
+ },
220
+ since: { type: 'string' },
221
+ },
222
+ },
223
+ render: (_args: any, value: any) => [{ type: 'text', text: renderStats(value) }],
224
+ },
225
+ async execute(): Promise<any> {
226
+ return tools.stats()
227
+ },
228
+ })), 'guard-stats-tool')
229
+ }
@@ -0,0 +1,2 @@
1
+ export type Tier = 'allow' | 'journal' | 'ask' | 'deny'
2
+ export const TIERS: readonly Tier[] = ['allow', 'journal', 'ask', 'deny']
@@ -0,0 +1,29 @@
1
+ import { defineDomain, type DomainValidator } from '../store/index.js'
2
+
3
+ const guardValidator: DomainValidator = {
4
+ parse(v:any) {
5
+ if (v==null) return {ok:true}
6
+ if (typeof v!=='object' || Array.isArray(v)) return {ok:false, error:'guard must be object'}
7
+ if (typeof v.publishBlocked!=='undefined' && typeof v.publishBlocked!=='boolean') return {ok:false, error:'publishBlocked boolean'}
8
+ if (v.gitProtection) {
9
+ const gp=v.gitProtection
10
+ if (typeof gp.enabled!=='boolean') return {ok:false, error:'gitProtection.enabled boolean'}
11
+ if (!Array.isArray(gp.branches) || gp.branches.some((b:any)=>typeof b!=='string' || !b.trim())) return {ok:false, error:'branches string[]'}
12
+ }
13
+ if (v.credentialPaths && (!Array.isArray(v.credentialPaths) || v.credentialPaths.some((p:any)=>typeof p!=='string'))) return {ok:false, error:'credentialPaths string[]'}
14
+ if (v.cwdContainment!==undefined && typeof v.cwdContainment!=='boolean') return {ok:false, error:'cwdContainment boolean'}
15
+ return {ok:true}
16
+ }
17
+ }
18
+ try { defineDomain('guard', guardValidator) } catch {}
19
+ const guardBlacklistValidator: DomainValidator = {
20
+ parse(v:any) {
21
+ if (v==null) return {ok:true}
22
+ if (typeof v!=='object') return {ok:false, error:'guardBlacklist object'}
23
+ if (v.patterns && !Array.isArray(v.patterns)) return {ok:false, error:'patterns array'}
24
+ if (v.placeholders && typeof v.placeholders!=='object') return {ok:false, error:'placeholders object'}
25
+ return {ok:true}
26
+ }
27
+ }
28
+ try { defineDomain('guardBlacklist', guardBlacklistValidator) } catch {}
29
+ export { guardValidator, guardBlacklistValidator }