@kirrosh/zond 0.21.0 → 0.23.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 (256) hide show
  1. package/CHANGELOG.md +758 -3
  2. package/README.md +78 -15
  3. package/package.json +17 -10
  4. package/src/cli/argv.ts +122 -0
  5. package/src/cli/commands/add-api.ts +134 -0
  6. package/src/cli/commands/api/annotate/idempotency.ts +59 -0
  7. package/src/cli/commands/api/annotate/index.ts +525 -0
  8. package/src/cli/commands/api/annotate/lifecycle.ts +74 -0
  9. package/src/cli/commands/api/annotate/overlay.ts +206 -0
  10. package/src/cli/commands/api/annotate/pagination.ts +60 -0
  11. package/src/cli/commands/api/annotate/prompts.ts +183 -0
  12. package/src/cli/commands/api/annotate/readback.ts +58 -0
  13. package/src/cli/commands/api/annotate/resources.ts +91 -0
  14. package/src/cli/commands/api/annotate/seed-bodies.ts +61 -0
  15. package/src/cli/commands/audit.ts +480 -0
  16. package/src/cli/commands/bootstrap.ts +710 -0
  17. package/src/cli/commands/catalog.ts +35 -0
  18. package/src/cli/commands/check.ts +348 -0
  19. package/src/cli/commands/checks.ts +756 -0
  20. package/src/cli/commands/ci-init.ts +55 -6
  21. package/src/cli/commands/clean.ts +212 -0
  22. package/src/cli/commands/cleanup.ts +262 -0
  23. package/src/cli/commands/completions.ts +192 -0
  24. package/src/cli/commands/coverage.ts +605 -132
  25. package/src/cli/commands/db.ts +180 -8
  26. package/src/cli/commands/describe.ts +37 -2
  27. package/src/cli/commands/discover.ts +1236 -0
  28. package/src/cli/commands/doctor.ts +607 -0
  29. package/src/cli/commands/fixtures.ts +402 -0
  30. package/src/cli/commands/generate.ts +420 -47
  31. package/src/cli/commands/init/agents-md.ts +61 -0
  32. package/src/cli/commands/init/bootstrap.ts +108 -0
  33. package/src/cli/commands/init/index.ts +244 -0
  34. package/src/cli/commands/init/skills.ts +98 -0
  35. package/src/cli/commands/init/templates/agents.md +77 -0
  36. package/src/cli/commands/init/templates/markdown.d.ts +4 -0
  37. package/src/cli/commands/init/templates/skills/zond-checks.md +397 -0
  38. package/src/cli/commands/init/templates/skills/zond-triage.md +210 -0
  39. package/src/cli/commands/init/templates/skills/zond.md +651 -0
  40. package/src/cli/commands/init/templates/zond-config.yml +14 -0
  41. package/src/cli/commands/prepare-fixtures.ts +135 -0
  42. package/src/cli/commands/probe/mass-assignment.ts +503 -0
  43. package/src/cli/commands/probe/security.ts +454 -0
  44. package/src/cli/commands/probe/static.ts +255 -0
  45. package/src/cli/commands/probe/webhooks.ts +161 -0
  46. package/src/cli/commands/probe.ts +459 -0
  47. package/src/cli/commands/reference.ts +87 -0
  48. package/src/cli/commands/refresh-api.ts +169 -0
  49. package/src/cli/commands/remove-api.ts +150 -0
  50. package/src/cli/commands/report-bundle.ts +318 -0
  51. package/src/cli/commands/report.ts +241 -0
  52. package/src/cli/commands/request.ts +379 -4
  53. package/src/cli/commands/run.ts +911 -33
  54. package/src/cli/commands/session.ts +244 -0
  55. package/src/cli/commands/use.ts +74 -0
  56. package/src/cli/index.ts +36 -607
  57. package/src/cli/json-envelope.ts +112 -3
  58. package/src/cli/json-schemas.ts +263 -0
  59. package/src/cli/program.ts +218 -0
  60. package/src/cli/resolve.ts +105 -0
  61. package/src/cli/status-filter.ts +124 -0
  62. package/src/cli/util/api-context.ts +85 -0
  63. package/src/cli/version.ts +8 -0
  64. package/src/core/anti-fp/bootstrap.ts +34 -0
  65. package/src/core/anti-fp/index.ts +33 -0
  66. package/src/core/anti-fp/registry.ts +44 -0
  67. package/src/core/anti-fp/rules/baseline-echo.ts +74 -0
  68. package/src/core/anti-fp/rules/schemathesis/body_negation_becomes_valid.ts +52 -0
  69. package/src/core/anti-fp/rules/schemathesis/coverage_phase_boundary_positive.ts +38 -0
  70. package/src/core/anti-fp/rules/schemathesis/has_unverifiable_mutations.ts +35 -0
  71. package/src/core/anti-fp/rules/schemathesis/index.ts +24 -0
  72. package/src/core/anti-fp/rules/schemathesis/string_type_mutation_becomes_valid.ts +53 -0
  73. package/src/core/anti-fp/rules/subscription-gated/index.ts +11 -0
  74. package/src/core/anti-fp/rules/subscription-gated/paid-plan-403.ts +75 -0
  75. package/src/core/anti-fp/types.ts +68 -0
  76. package/src/core/checks/checks/_crud-helpers.ts +133 -0
  77. package/src/core/checks/checks/_negative_mutator.ts +133 -0
  78. package/src/core/checks/checks/_readback-helpers.ts +133 -0
  79. package/src/core/checks/checks/content_type_conformance.ts +39 -0
  80. package/src/core/checks/checks/cross_call_references.ts +134 -0
  81. package/src/core/checks/checks/ensure_resource_availability.ts +62 -0
  82. package/src/core/checks/checks/idempotency_replay.ts +246 -0
  83. package/src/core/checks/checks/ignored_auth.ts +211 -0
  84. package/src/core/checks/checks/index.ts +65 -0
  85. package/src/core/checks/checks/lifecycle_transitions.ts +273 -0
  86. package/src/core/checks/checks/missing_required_header.ts +40 -0
  87. package/src/core/checks/checks/negative_data_rejection.ts +45 -0
  88. package/src/core/checks/checks/not_a_server_error.ts +27 -0
  89. package/src/core/checks/checks/open_cors_on_sensitive.ts +131 -0
  90. package/src/core/checks/checks/pagination_invariants.ts +238 -0
  91. package/src/core/checks/checks/positive_data_acceptance.ts +36 -0
  92. package/src/core/checks/checks/rate_limit_headers_absent.ts +77 -0
  93. package/src/core/checks/checks/response_headers_conformance.ts +74 -0
  94. package/src/core/checks/checks/response_schema_conformance.ts +30 -0
  95. package/src/core/checks/checks/status_code_conformance.ts +61 -0
  96. package/src/core/checks/checks/unsupported_method.ts +63 -0
  97. package/src/core/checks/checks/use_after_free.ts +78 -0
  98. package/src/core/checks/index.ts +30 -0
  99. package/src/core/checks/mode.ts +79 -0
  100. package/src/core/checks/recommended-action.ts +64 -0
  101. package/src/core/checks/registry.ts +78 -0
  102. package/src/core/checks/runner.ts +874 -0
  103. package/src/core/checks/sarif.ts +230 -0
  104. package/src/core/checks/stateful.ts +121 -0
  105. package/src/core/checks/types.ts +189 -0
  106. package/src/core/classifier/recommended-action.ts +222 -0
  107. package/src/core/context/current.ts +51 -0
  108. package/src/core/context/session.ts +78 -0
  109. package/src/core/coverage/loader.ts +185 -0
  110. package/src/core/coverage/reasons.ts +300 -0
  111. package/src/core/diagnostics/db-analysis.ts +161 -12
  112. package/src/core/diagnostics/failure-class.ts +120 -0
  113. package/src/core/diagnostics/failure-hints.ts +212 -9
  114. package/src/core/diagnostics/spec-pointer.ts +99 -0
  115. package/src/core/diagnostics/suggested-fixes.ts +156 -0
  116. package/src/core/exporter/case-study/index.ts +270 -0
  117. package/src/core/exporter/curl.ts +40 -0
  118. package/src/core/exporter/exporter.ts +48 -0
  119. package/src/core/exporter/html-report/escape.ts +24 -0
  120. package/src/core/exporter/html-report/index.ts +479 -0
  121. package/src/core/exporter/html-report/script.ts +100 -0
  122. package/src/core/exporter/html-report/styles.ts +408 -0
  123. package/src/core/generator/chunker.ts +53 -15
  124. package/src/core/generator/coverage-phase.ts +0 -0
  125. package/src/core/generator/create-body.ts +89 -0
  126. package/src/core/generator/data-factory.ts +490 -33
  127. package/src/core/generator/describe.ts +1 -1
  128. package/src/core/generator/fixtures-builder.ts +325 -0
  129. package/src/core/generator/index.ts +7 -5
  130. package/src/core/generator/openapi-reader.ts +55 -3
  131. package/src/core/generator/path-param-disambig.ts +114 -0
  132. package/src/core/generator/resources-builder.ts +648 -0
  133. package/src/core/generator/schema-utils.ts +11 -3
  134. package/src/core/generator/serializer.ts +114 -15
  135. package/src/core/generator/suite-generator.ts +484 -77
  136. package/src/core/generator/types.ts +8 -0
  137. package/src/core/identity/identity-file.ts +129 -0
  138. package/src/core/lint/affects.ts +28 -0
  139. package/src/core/lint/config.ts +96 -0
  140. package/src/core/lint/format.ts +42 -0
  141. package/src/core/lint/index.ts +94 -0
  142. package/src/core/lint/reporter.ts +128 -0
  143. package/src/core/lint/rules/consistency.ts +158 -0
  144. package/src/core/lint/rules/heuristics.ts +97 -0
  145. package/src/core/lint/rules/strictness.ts +109 -0
  146. package/src/core/lint/types.ts +96 -0
  147. package/src/core/lint/walker.ts +248 -0
  148. package/src/core/meta/meta-store.ts +6 -73
  149. package/src/core/output/README.md +91 -0
  150. package/src/core/output/index.ts +13 -0
  151. package/src/core/output/run.ts +126 -0
  152. package/src/core/output/types.ts +129 -0
  153. package/src/core/parser/env-interpolation.ts +104 -0
  154. package/src/core/parser/filter.ts +57 -0
  155. package/src/core/parser/schema.ts +132 -5
  156. package/src/core/parser/types.ts +29 -2
  157. package/src/core/parser/variables.ts +0 -0
  158. package/src/core/parser/yaml-parser.ts +108 -13
  159. package/src/core/probe/bootstrap.ts +34 -0
  160. package/src/core/probe/dry-run-envelope.ts +57 -0
  161. package/src/core/probe/mass-assignment-probe-class.ts +198 -0
  162. package/src/core/probe/mass-assignment-probe.ts +1122 -0
  163. package/src/core/probe/mass-assignment-template.ts +212 -0
  164. package/src/core/probe/method-probe.ts +164 -0
  165. package/src/core/probe/method-shared.ts +69 -0
  166. package/src/core/probe/negative-probe.ts +691 -0
  167. package/src/core/probe/orphan-tracker.ts +188 -0
  168. package/src/core/probe/path-discovery.ts +440 -0
  169. package/src/core/probe/probe-harness.ts +120 -0
  170. package/src/core/probe/registry.ts +89 -0
  171. package/src/core/probe/runner.ts +136 -0
  172. package/src/core/probe/security-probe-class.ts +201 -0
  173. package/src/core/probe/security-probe.ts +1453 -0
  174. package/src/core/probe/shared.ts +505 -0
  175. package/src/core/probe/static-probe-class.ts +125 -0
  176. package/src/core/probe/types.ts +165 -0
  177. package/src/core/probe/verdict-aggregator.ts +33 -0
  178. package/src/core/probe/webhooks-probe.ts +284 -0
  179. package/src/core/reporter/console.ts +69 -4
  180. package/src/core/reporter/index.ts +2 -3
  181. package/src/core/reporter/json.ts +15 -2
  182. package/src/core/reporter/junit.ts +27 -12
  183. package/src/core/reporter/ndjson.ts +37 -0
  184. package/src/core/reporter/types.ts +3 -0
  185. package/src/core/runner/assertions.ts +62 -2
  186. package/src/core/runner/async-pool.ts +108 -0
  187. package/src/core/runner/auth-path.ts +8 -0
  188. package/src/core/runner/ci-context.ts +72 -0
  189. package/src/core/runner/executor.ts +391 -52
  190. package/src/core/runner/form-encode.ts +51 -0
  191. package/src/core/runner/http-client.ts +115 -7
  192. package/src/core/runner/learn-drift.ts +293 -0
  193. package/src/core/runner/preflight-vars.ts +149 -0
  194. package/src/core/runner/progress-tracker.ts +73 -0
  195. package/src/core/runner/rate-limiter.ts +203 -0
  196. package/src/core/runner/run-kind.ts +39 -0
  197. package/src/core/runner/schema-validator.ts +312 -0
  198. package/src/core/runner/send-request.ts +153 -20
  199. package/src/core/runner/types.ts +38 -0
  200. package/src/core/secrets/registry.ts +164 -0
  201. package/src/core/secrets/secrets-file.ts +115 -0
  202. package/src/core/selectors/operation-filter.ts +144 -0
  203. package/src/core/setup-api.ts +419 -17
  204. package/src/core/severity/category.ts +94 -0
  205. package/src/core/severity/index.ts +121 -0
  206. package/src/core/spec/layers.ts +154 -0
  207. package/src/core/util/format-eta.ts +21 -0
  208. package/src/core/utils.ts +5 -1
  209. package/src/core/workspace/config.ts +129 -0
  210. package/src/core/workspace/manifest.ts +283 -0
  211. package/src/core/workspace/output-rotation.ts +62 -0
  212. package/src/core/workspace/root.ts +94 -0
  213. package/src/core/workspace/triage-path.ts +87 -0
  214. package/src/db/lint-runs.ts +47 -0
  215. package/src/db/migrate.ts +126 -0
  216. package/src/db/migrations/0001_run_kind.sql +25 -0
  217. package/src/db/migrations/sql.d.ts +4 -0
  218. package/src/db/queries/collections.ts +133 -0
  219. package/src/db/queries/coverage.ts +9 -0
  220. package/src/db/queries/dashboard.ts +59 -0
  221. package/src/db/queries/results.ts +128 -0
  222. package/src/db/queries/runs.ts +235 -0
  223. package/src/db/queries/sessions.ts +42 -0
  224. package/src/db/queries/settings.ts +28 -0
  225. package/src/db/queries/types.ts +172 -0
  226. package/src/db/queries.ts +72 -802
  227. package/src/db/schema.ts +179 -48
  228. package/src/cli/commands/export.ts +0 -144
  229. package/src/cli/commands/guide.ts +0 -127
  230. package/src/cli/commands/init.ts +0 -57
  231. package/src/cli/commands/serve.ts +0 -81
  232. package/src/cli/commands/sync.ts +0 -269
  233. package/src/cli/commands/update.ts +0 -189
  234. package/src/cli/commands/validate.ts +0 -34
  235. package/src/core/exporter/postman.ts +0 -963
  236. package/src/core/generator/guide-builder.ts +0 -253
  237. package/src/core/meta/types.ts +0 -21
  238. package/src/core/parser/index.ts +0 -21
  239. package/src/core/runner/execute-run.ts +0 -132
  240. package/src/core/runner/index.ts +0 -12
  241. package/src/core/sync/spec-differ.ts +0 -38
  242. package/src/web/data/collection-state.ts +0 -362
  243. package/src/web/routes/api.ts +0 -314
  244. package/src/web/routes/dashboard.ts +0 -350
  245. package/src/web/routes/runs.ts +0 -64
  246. package/src/web/schemas.ts +0 -121
  247. package/src/web/server.ts +0 -134
  248. package/src/web/static/htmx.min.cjs +0 -1
  249. package/src/web/static/style.css +0 -1148
  250. package/src/web/views/endpoints-tab.ts +0 -174
  251. package/src/web/views/explorer-tab.ts +0 -402
  252. package/src/web/views/health-strip.ts +0 -92
  253. package/src/web/views/layout.ts +0 -48
  254. package/src/web/views/results.ts +0 -210
  255. package/src/web/views/runs-tab.ts +0 -126
  256. package/src/web/views/suites-tab.ts +0 -181
@@ -0,0 +1,283 @@
1
+ /**
2
+ * `.zond/manifest.json` — auto-generated file tracking (TASK-156, m-9).
3
+ *
4
+ * Every command that writes files into the workspace appends an entry
5
+ * here so `zond clean` can later remove only what zond produced, leaving
6
+ * user edits intact (sha256 mismatch → manually-edited → skipped).
7
+ */
8
+
9
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, statSync } from "node:fs";
10
+ import { createHash } from "node:crypto";
11
+ import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
12
+
13
+ const MANIFEST_VERSION = 1;
14
+ const MANIFEST_RELPATH = ".zond/manifest.json";
15
+
16
+ export type ManifestCategory =
17
+ | "spec"
18
+ | "catalog"
19
+ | "resources"
20
+ | "fixtures"
21
+ | "env"
22
+ | "tests"
23
+ | "probes"
24
+ | "other";
25
+
26
+ export interface ManifestEntry {
27
+ /** Workspace-relative POSIX path. */
28
+ path: string;
29
+ /** sha256 of the file contents at write-time. */
30
+ sha256: string;
31
+ /** Command that emitted the file (e.g. "zond generate", "zond probe-validation --emit"). */
32
+ by: string;
33
+ /** ISO 8601 timestamp. */
34
+ ts: string;
35
+ /** API name when applicable (used by `zond clean --api <name>`). */
36
+ api?: string;
37
+ /** Logical category for `zond clean --probes` etc. */
38
+ category?: ManifestCategory;
39
+ }
40
+
41
+ export interface Manifest {
42
+ version: number;
43
+ generated: ManifestEntry[];
44
+ }
45
+
46
+ function getManifestPath(workspaceRoot: string): string {
47
+ return join(workspaceRoot, MANIFEST_RELPATH);
48
+ }
49
+
50
+ export function loadManifest(workspaceRoot: string): Manifest {
51
+ const p = getManifestPath(workspaceRoot);
52
+ if (!existsSync(p)) return { version: MANIFEST_VERSION, generated: [] };
53
+ try {
54
+ const raw = readFileSync(p, "utf-8");
55
+ const parsed = JSON.parse(raw);
56
+ if (!parsed || typeof parsed !== "object" || !Array.isArray(parsed.generated)) {
57
+ return { version: MANIFEST_VERSION, generated: [] };
58
+ }
59
+ return {
60
+ version: typeof parsed.version === "number" ? parsed.version : MANIFEST_VERSION,
61
+ generated: parsed.generated as ManifestEntry[],
62
+ };
63
+ } catch {
64
+ return { version: MANIFEST_VERSION, generated: [] };
65
+ }
66
+ }
67
+
68
+ function saveManifest(workspaceRoot: string, manifest: Manifest): void {
69
+ const p = getManifestPath(workspaceRoot);
70
+ mkdirSync(dirname(p), { recursive: true });
71
+ writeFileSync(p, JSON.stringify(manifest, null, 2) + "\n", "utf-8");
72
+ }
73
+
74
+ /** Workspace-relative POSIX path. */
75
+ export function toWorkspacePath(workspaceRoot: string, filePath: string): string {
76
+ const abs = isAbsolute(filePath) ? filePath : resolve(filePath);
77
+ let rel = relative(workspaceRoot, abs);
78
+ if (sep === "\\") rel = rel.split(sep).join("/");
79
+ return rel;
80
+ }
81
+
82
+ export function sha256OfFile(filePath: string): string | null {
83
+ try {
84
+ const buf = readFileSync(filePath);
85
+ return createHash("sha256").update(buf).digest("hex");
86
+ } catch {
87
+ return null;
88
+ }
89
+ }
90
+
91
+ function sha256OfString(content: string): string {
92
+ return createHash("sha256").update(content).digest("hex");
93
+ }
94
+
95
+ export interface RecordInput {
96
+ /** Absolute or workspace-relative path of the file just written. */
97
+ path: string;
98
+ by: string;
99
+ api?: string;
100
+ category?: ManifestCategory;
101
+ /** Pre-computed sha256; if absent, the file is read from disk. */
102
+ sha256?: string;
103
+ }
104
+
105
+ /**
106
+ * Append (or replace) entries in the manifest. Existing entries with the
107
+ * same `path` are replaced so re-running a generator updates the hash
108
+ * instead of accumulating duplicates.
109
+ */
110
+ export function recordGeneratedFiles(workspaceRoot: string, entries: RecordInput[]): void {
111
+ if (entries.length === 0) return;
112
+ const ts = new Date().toISOString();
113
+ const accepted: ManifestEntry[] = [];
114
+
115
+ for (const e of entries) {
116
+ const abs = isAbsolute(e.path) ? e.path : resolve(workspaceRoot, e.path);
117
+ const rel = toWorkspacePath(workspaceRoot, abs);
118
+ // Refuse to record paths that escape the workspace — happens when tests
119
+ // run setupApi from a tmp dir but findWorkspaceRoot walks up to the host
120
+ // project root. Without this guard, a `../../../tmp/...` entry pollutes
121
+ // the host workspace's manifest.
122
+ if (rel.startsWith("..") || isAbsolute(rel)) continue;
123
+ const sha = e.sha256 ?? sha256OfFile(abs);
124
+ if (!sha) continue; // file vanished; skip
125
+ accepted.push({
126
+ path: rel,
127
+ sha256: sha,
128
+ by: e.by,
129
+ ts,
130
+ api: e.api,
131
+ category: e.category,
132
+ });
133
+ }
134
+
135
+ // Don't touch disk when nothing applied to this workspace — keeps tests
136
+ // (which run from /tmp but inherit the host's workspace root) from
137
+ // creating an empty `.zond/manifest.json` in the dev repo.
138
+ if (accepted.length === 0) return;
139
+
140
+ const manifest = loadManifest(workspaceRoot);
141
+ const byPath = new Map<string, ManifestEntry>();
142
+ for (const e of manifest.generated) byPath.set(e.path, e);
143
+ for (const e of accepted) byPath.set(e.path, e);
144
+
145
+ manifest.version = MANIFEST_VERSION;
146
+ manifest.generated = [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path));
147
+ saveManifest(workspaceRoot, manifest);
148
+ }
149
+
150
+ export function recordGeneratedFile(workspaceRoot: string, entry: RecordInput): void {
151
+ recordGeneratedFiles(workspaceRoot, [entry]);
152
+ }
153
+
154
+ export interface CleanFilter {
155
+ api?: string;
156
+ category?: ManifestCategory;
157
+ /** When true, include all entries regardless of api/category. */
158
+ all?: boolean;
159
+ }
160
+
161
+ export interface SelectEntriesResult {
162
+ selected: ManifestEntry[];
163
+ /** Entries that match `api` filter but were excluded because the user
164
+ * did not explicitly opt into the `probes` category. Surfaced so the
165
+ * CLI can hint at how to reach them (TASK-258). */
166
+ probesPreserved: ManifestEntry[];
167
+ }
168
+
169
+ export function selectEntries(manifest: Manifest, filter: CleanFilter): ManifestEntry[] {
170
+ return selectEntriesEx(manifest, filter).selected;
171
+ }
172
+
173
+ export function selectEntriesEx(manifest: Manifest, filter: CleanFilter): SelectEntriesResult {
174
+ const selected: ManifestEntry[] = [];
175
+ const probesPreserved: ManifestEntry[] = [];
176
+ // TASK-258: when scoping by --api alone (no explicit --probes / --all),
177
+ // probes/ belongs to a separate pipeline (zond probe-validation/-methods)
178
+ // and re-generating it costs ~30s on a 200-endpoint spec. Treat it as
179
+ // out-of-scope and surface a hint instead of silently nuking suites.
180
+ const protectProbes = !!filter.api && filter.category !== "probes" && !filter.all;
181
+ for (const e of manifest.generated) {
182
+ // spec.json is the source-of-truth snapshot downloaded from the network.
183
+ // It is never auto-deleted — removal requires manual action or re-adding the API.
184
+ if (e.category === "spec") continue;
185
+ if (filter.all) {
186
+ selected.push(e);
187
+ continue;
188
+ }
189
+ if (filter.api) {
190
+ const matchesApi = e.api === filter.api ||
191
+ e.path.startsWith(`apis/${filter.api}/`);
192
+ if (!matchesApi) continue;
193
+ }
194
+ if (filter.category && e.category !== filter.category) continue;
195
+ if (protectProbes && e.category === "probes") {
196
+ probesPreserved.push(e);
197
+ continue;
198
+ }
199
+ selected.push(e);
200
+ }
201
+ return { selected, probesPreserved };
202
+ }
203
+
204
+ export type CleanVerdict = "delete" | "modified" | "missing";
205
+
206
+ export interface CleanItem {
207
+ entry: ManifestEntry;
208
+ absPath: string;
209
+ verdict: CleanVerdict;
210
+ /** Current sha256 if file exists. */
211
+ currentSha256?: string;
212
+ }
213
+
214
+ export function inspectEntries(workspaceRoot: string, entries: ManifestEntry[]): CleanItem[] {
215
+ const items: CleanItem[] = [];
216
+ for (const entry of entries) {
217
+ const abs = resolve(workspaceRoot, entry.path);
218
+ if (!existsSync(abs)) {
219
+ items.push({ entry, absPath: abs, verdict: "missing" });
220
+ continue;
221
+ }
222
+ const cur = sha256OfFile(abs);
223
+ if (cur && cur !== entry.sha256) {
224
+ items.push({ entry, absPath: abs, verdict: "modified", currentSha256: cur });
225
+ } else {
226
+ items.push({ entry, absPath: abs, verdict: "delete", currentSha256: cur ?? undefined });
227
+ }
228
+ }
229
+ return items;
230
+ }
231
+
232
+ /**
233
+ * Drop entries from the manifest by absolute or workspace-relative path.
234
+ */
235
+ export function removeManifestEntries(workspaceRoot: string, paths: string[]): void {
236
+ if (paths.length === 0) return;
237
+ const manifest = loadManifest(workspaceRoot);
238
+ const drop = new Set(paths.map((p) => toWorkspacePath(workspaceRoot, p)));
239
+ manifest.generated = manifest.generated.filter((e) => !drop.has(e.path));
240
+ saveManifest(workspaceRoot, manifest);
241
+ }
242
+
243
+ /** True when the workspace has a manifest file. */
244
+ export function hasManifest(workspaceRoot: string): boolean {
245
+ return existsSync(getManifestPath(workspaceRoot));
246
+ }
247
+
248
+ /**
249
+ * Header comment prepended to every YAML/MD file zond writes, so a human
250
+ * opening the file sees who wrote it and how to regenerate it. Pair with
251
+ * `recordGeneratedFile` for the machine-readable audit trail.
252
+ */
253
+ export function autoGenHeader(by: string, regenerate?: string): string {
254
+ const lines = [
255
+ `# Auto-generated by ${by}.`,
256
+ `# ⚠️ Edits will be overwritten on regenerate. Drop from .zond/manifest.json (or rename) to keep changes.`,
257
+ ];
258
+ if (regenerate) lines.push(`# Regenerate: ${regenerate}`);
259
+ return lines.join("\n") + "\n";
260
+ }
261
+
262
+ /**
263
+ * Best-effort: derive the API name from a path like `apis/<name>/tests`.
264
+ * Returns undefined for non-conventional layouts so manifest entries stay
265
+ * un-tagged rather than mis-tagged.
266
+ */
267
+ export function inferApiName(outputDir: string): string | undefined {
268
+ const norm = outputDir.replace(/\\/g, "/");
269
+ const m = norm.match(/(?:^|\/)apis\/([^/]+)(?:\/|$)/);
270
+ return m?.[1];
271
+ }
272
+
273
+ /** Cheap sanity check used by zond doctor — true when path lives in workspace. */
274
+ function isWithinWorkspace(workspaceRoot: string, candidate: string): boolean {
275
+ const abs = isAbsolute(candidate) ? candidate : resolve(workspaceRoot, candidate);
276
+ try {
277
+ statSync(abs);
278
+ } catch {
279
+ return false;
280
+ }
281
+ const rel = relative(workspaceRoot, abs);
282
+ return !rel.startsWith("..") && !isAbsolute(rel);
283
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Auto-rotate `--output <path>` targets so a second `zond report ...` (or
3
+ * `zond probe-* --output ...`) does not silently clobber the previous
4
+ * artifact (TASK-162, m-9 P6).
5
+ *
6
+ * Strategy: when `path` already exists, rename it to `<basename>-vN<ext>`
7
+ * with the smallest free N (≥ 2) and return that rotation info so the
8
+ * caller can print it. The `--overwrite` flag short-circuits to no-op so
9
+ * users keep the previous behaviour when they explicitly ask for it.
10
+ */
11
+
12
+ import { existsSync, renameSync } from "node:fs";
13
+ import { basename, dirname, extname, join } from "node:path";
14
+
15
+ export interface RotationResult {
16
+ /** The path that was renamed (the old artifact). undefined when no rotation happened. */
17
+ rotatedFrom?: string;
18
+ /** Where the old artifact moved to. undefined when no rotation happened. */
19
+ rotatedTo?: string;
20
+ /** True when caller asked for `--overwrite` (or the target didn't exist). */
21
+ overwrite: boolean;
22
+ }
23
+
24
+ export interface RotateOptions {
25
+ /** When true, skip rotation entirely (overwrite-in-place). */
26
+ overwrite?: boolean;
27
+ /** Optional callback for the human-facing notice; defaults to stderr. */
28
+ notice?: (msg: string) => void;
29
+ }
30
+
31
+ /**
32
+ * Rename `targetPath` to `<base>-vN<ext>` if it exists and `--overwrite`
33
+ * is not set. Returns rotation info; the caller is responsible for
34
+ * actually writing the new artifact at `targetPath`.
35
+ */
36
+ export function rotateOutputTarget(targetPath: string, opts: RotateOptions = {}): RotationResult {
37
+ if (opts.overwrite) return { overwrite: true };
38
+ if (!existsSync(targetPath)) return { overwrite: false };
39
+
40
+ const dir = dirname(targetPath);
41
+ const ext = extname(targetPath);
42
+ const stem = basename(targetPath, ext);
43
+ // Strip an existing `-vN` suffix from the stem so successive rotations
44
+ // produce `digest-v2.md`, `digest-v3.md` rather than
45
+ // `digest-v2-v2.md` etc.
46
+ const stemBare = stem.replace(/-v\d+$/, "");
47
+
48
+ let n = 2;
49
+ while (n < 1000) {
50
+ const candidate = join(dir, `${stemBare}-v${n}${ext}`);
51
+ if (!existsSync(candidate)) {
52
+ renameSync(targetPath, candidate);
53
+ const notice = opts.notice ?? ((m: string) => process.stderr.write(m + "\n"));
54
+ notice(`Previous artifact moved to ${candidate}`);
55
+ return { rotatedFrom: targetPath, rotatedTo: candidate, overwrite: false };
56
+ }
57
+ n++;
58
+ }
59
+ // Pathological: 1000 versions exist. Fall back to overwrite to avoid
60
+ // an infinite-loop UX failure.
61
+ return { overwrite: true };
62
+ }
@@ -0,0 +1,94 @@
1
+ import { existsSync, statSync } from "node:fs";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { homedir } from "node:os";
4
+
5
+ /**
6
+ * Files / directories that mark a workspace root. Order matters — earlier
7
+ * markers win when more than one is present in the same directory.
8
+ *
9
+ * zond.config.yml — explicit project config (T12)
10
+ * .zond/ — `zond init --here` subdir convention (T19)
11
+ * zond.db — flat layout from `zond init`
12
+ * apis/ — flat layout (collections directory)
13
+ */
14
+ export const WORKSPACE_MARKERS = ["zond.config.yml", ".zond", "zond.db", "apis"] as const;
15
+ export type WorkspaceMarker = (typeof WORKSPACE_MARKERS)[number];
16
+
17
+ export interface WorkspaceInfo {
18
+ /** Absolute path to the workspace root. */
19
+ root: string;
20
+ /** Marker that triggered detection, or "" when fallback (cwd) was used. */
21
+ marker: WorkspaceMarker | "";
22
+ /** True when no marker was found and we fell back to `cwd`. */
23
+ fromFallback: boolean;
24
+ }
25
+
26
+ let warned = false;
27
+
28
+ function hasMarker(dir: string): WorkspaceMarker | null {
29
+ for (const m of WORKSPACE_MARKERS) {
30
+ const p = join(dir, m);
31
+ if (!existsSync(p)) continue;
32
+ // .zond and apis must be directories; zond.config.yml and zond.db must be files
33
+ try {
34
+ const st = statSync(p);
35
+ if (m === ".zond" || m === "apis") {
36
+ if (st.isDirectory()) return m;
37
+ } else if (st.isFile()) {
38
+ return m;
39
+ }
40
+ } catch {
41
+ /* race / permissions — treat as no marker */
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+
47
+ /**
48
+ * Walk-up from `cwd` (default `process.cwd()`) to the nearest workspace
49
+ * marker. The walk stops at `os.homedir()` to avoid accidentally picking up
50
+ * `~/apis` or `~/zond.db` when the user runs zond from somewhere unrelated.
51
+ *
52
+ * When no marker is found, returns `{ root: cwd, fromFallback: true }` and
53
+ * prints a one-time stderr warning so the user knows zond is operating in
54
+ * cwd-mode.
55
+ */
56
+ export function findWorkspaceRoot(cwd?: string): WorkspaceInfo {
57
+ const start = resolve(cwd ?? process.cwd());
58
+ const stop = resolve(homedir());
59
+
60
+ let dir = start;
61
+ // Walk strictly while above (or equal to) HOME's length, but include HOME
62
+ // itself as a candidate only when start is inside HOME. If start is outside
63
+ // HOME (e.g. /tmp), walk all the way to "/".
64
+ const insideHome = start === stop || start.startsWith(stop + "/") || start.startsWith(stop + "\\");
65
+
66
+ while (true) {
67
+ const marker = hasMarker(dir);
68
+ if (marker) return { root: dir, marker, fromFallback: false };
69
+
70
+ const parent = dirname(dir);
71
+ if (parent === dir) break; // filesystem root
72
+ if (insideHome && dir === stop) break; // do not climb past HOME
73
+ dir = parent;
74
+ }
75
+
76
+ if (!warned) {
77
+ warned = true;
78
+ process.stderr.write(
79
+ `[zond] no workspace marker found from ${start}; using cwd. ` +
80
+ `Run 'zond init' or create zond.config.yml to anchor the workspace.\n`,
81
+ );
82
+ }
83
+ return { root: start, marker: "", fromFallback: true };
84
+ }
85
+
86
+ /** Resolve `relative` against the workspace root (auto-detected from `cwd`). */
87
+ export function resolveWorkspacePath(relative: string, cwd?: string): string {
88
+ return resolve(findWorkspaceRoot(cwd).root, relative);
89
+ }
90
+
91
+ /** Test helper: reset the one-shot warning latch. */
92
+ export function _resetWorkspaceWarning(): void {
93
+ warned = false;
94
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Default `triage/` path for `--output` (TASK-163, m-9 P7).
3
+ *
4
+ * Rule: when the user runs a report-emitting command without `--output`,
5
+ * we drop the artifact into:
6
+ *
7
+ * <workspace>/triage/<api|"adhoc">/<run-id>/<command>-<timestamp>.<ext>
8
+ *
9
+ * If they pass `--output some-filename.md` (no slash), that filename is
10
+ * used as the basename inside the same directory. An `--output` that
11
+ * includes a directory component is honoured verbatim.
12
+ */
13
+
14
+ import { existsSync, mkdirSync } from "node:fs";
15
+ import { dirname, isAbsolute, join, resolve } from "node:path";
16
+ import { findWorkspaceRoot } from "./root.ts";
17
+
18
+ export interface TriageOpts {
19
+ /** Logical command name — used in the auto-filename. */
20
+ command: string;
21
+ /** Run id (or undefined for ad-hoc artifacts). */
22
+ runId?: number | null;
23
+ /** API/collection name; falls back to `"adhoc"` when not known. */
24
+ api?: string | null;
25
+ /** Default extension (md / html / json). Without a leading dot. */
26
+ ext: string;
27
+ /** What the user typed in --output (may be undefined). */
28
+ userOutput?: string;
29
+ /** Optional explicit timestamp for tests. */
30
+ now?: Date;
31
+ }
32
+
33
+ export interface ResolvedTriagePath {
34
+ /** Absolute path to write. */
35
+ absolute: string;
36
+ /** Workspace-relative path for prettier console output. */
37
+ relative: string;
38
+ /** True when we landed under triage/ vs. honoured the user path. */
39
+ underTriage: boolean;
40
+ }
41
+
42
+ function pad(n: number): string {
43
+ return n < 10 ? `0${n}` : `${n}`;
44
+ }
45
+
46
+ function timestamp(d: Date): string {
47
+ return `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}`;
48
+ }
49
+
50
+ export function resolveTriageOutput(opts: TriageOpts): ResolvedTriagePath {
51
+ const ws = findWorkspaceRoot();
52
+ const root = ws.root;
53
+ const ext = opts.ext.replace(/^\./, "");
54
+ const ts = timestamp(opts.now ?? new Date());
55
+ const apiSlug = opts.api ?? "adhoc";
56
+ const runSlug = opts.runId != null ? `run-${opts.runId}` : "adhoc";
57
+
58
+ // 1) User passed a path with a directory component → honour verbatim.
59
+ if (opts.userOutput && /[\\/]/.test(opts.userOutput)) {
60
+ const abs = isAbsolute(opts.userOutput) ? opts.userOutput : resolve(opts.userOutput);
61
+ mkdirSync(dirname(abs), { recursive: true });
62
+ return {
63
+ absolute: abs,
64
+ relative: relPath(abs, root),
65
+ underTriage: false,
66
+ };
67
+ }
68
+
69
+ // 2) Default location.
70
+ const dir = join(root, "triage", apiSlug, runSlug);
71
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
72
+ const basename = opts.userOutput ?? `${opts.command}-${ts}.${ext}`;
73
+ const abs = join(dir, basename);
74
+ return {
75
+ absolute: abs,
76
+ relative: relPath(abs, root),
77
+ underTriage: true,
78
+ };
79
+ }
80
+
81
+ function relPath(abs: string, root: string): string {
82
+ if (abs.startsWith(root)) {
83
+ const r = abs.slice(root.length).replace(/^[\\/]+/, "");
84
+ return r.replace(/\\/g, "/");
85
+ }
86
+ return abs;
87
+ }
@@ -0,0 +1,47 @@
1
+ import type { Database } from "bun:sqlite";
2
+ import type { Issue, LintConfig, LintStats } from "../core/lint/index.ts";
3
+
4
+ export interface LintRunRow {
5
+ id: number;
6
+ spec_path: string;
7
+ started_at: string;
8
+ finished_at: string | null;
9
+ total: number;
10
+ high_count: number;
11
+ medium_count: number;
12
+ low_count: number;
13
+ endpoint_count: number;
14
+ }
15
+
16
+ export function createLintRun(db: Database, specPath: string): number {
17
+ const startedAt = new Date().toISOString();
18
+ const stmt = db.prepare(
19
+ "INSERT INTO lint_runs (spec_path, started_at) VALUES (?, ?)",
20
+ );
21
+ const info = stmt.run(specPath, startedAt) as { lastInsertRowid: number | bigint };
22
+ return Number(info.lastInsertRowid);
23
+ }
24
+
25
+ export function finalizeLintRun(
26
+ db: Database,
27
+ id: number,
28
+ issues: Issue[],
29
+ stats: LintStats,
30
+ config: LintConfig,
31
+ ): void {
32
+ db.prepare(
33
+ `UPDATE lint_runs SET
34
+ finished_at = ?,
35
+ total = ?, high_count = ?, medium_count = ?, low_count = ?,
36
+ endpoint_count = ?,
37
+ config_json = ?, issues_json = ?
38
+ WHERE id = ?`,
39
+ ).run(
40
+ new Date().toISOString(),
41
+ stats.total, stats.high, stats.medium, stats.low,
42
+ stats.endpoints,
43
+ JSON.stringify({ rules: config.rules, heuristics: config.heuristics, ignore_paths: config.ignore_paths }),
44
+ JSON.stringify(issues),
45
+ id,
46
+ );
47
+ }