@isaacriehm/cairn-core 0.18.2 → 0.19.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.
@@ -66,3 +66,27 @@ export declare function findDuplicateClusters(args: {
66
66
  thresholdFloor?: number;
67
67
  thresholdDefinite?: number;
68
68
  }): DedupResult;
69
+ export interface AcceptedDuplicate {
70
+ dup: boolean;
71
+ /** Matched accepted DEC id, present only when `dup`. */
72
+ matchId?: string;
73
+ /** Matched accepted DEC title, present only when `dup`. */
74
+ matchTitle?: string;
75
+ /** Title-Jaccard similarity of the match, present only when `dup`. */
76
+ similarity?: number;
77
+ }
78
+ /**
79
+ * Is a candidate decision a near-duplicate of one ALREADY accepted in the
80
+ * ledger? This is the dedup half of the auto-accept verify gate: a fresh
81
+ * DEC that merely restates an accepted one should NOT silently re-land —
82
+ * it falls back to an `_inbox/` draft for human eyes instead.
83
+ *
84
+ * Title-token Jaccard against the accepted (non-superseded) ledger, at the
85
+ * same `definite` threshold the inbox clusterer uses. Title-vs-title keeps
86
+ * it symmetric (the ledger only carries titles); deterministic, no LLM.
87
+ */
88
+ export declare function isDuplicateOfAccepted(args: {
89
+ repoRoot: string;
90
+ title: string;
91
+ threshold?: number;
92
+ }): AcceptedDuplicate;
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
21
21
  import { join } from "node:path";
22
- import { decisionsDir } from "@isaacriehm/cairn-state";
22
+ import { buildDecisionsLedger, decisionsDir } from "@isaacriehm/cairn-state";
23
23
  import { jaccard, tokenize } from "../text/jaccard.js";
24
24
  /** Default char window of body to fold into the token bag. */
25
25
  const BODY_CHAR_WINDOW = 500;
@@ -214,4 +214,45 @@ export function findDuplicateClusters(args) {
214
214
  thresholdDefinite,
215
215
  };
216
216
  }
217
+ /**
218
+ * Is a candidate decision a near-duplicate of one ALREADY accepted in the
219
+ * ledger? This is the dedup half of the auto-accept verify gate: a fresh
220
+ * DEC that merely restates an accepted one should NOT silently re-land —
221
+ * it falls back to an `_inbox/` draft for human eyes instead.
222
+ *
223
+ * Title-token Jaccard against the accepted (non-superseded) ledger, at the
224
+ * same `definite` threshold the inbox clusterer uses. Title-vs-title keeps
225
+ * it symmetric (the ledger only carries titles); deterministic, no LLM.
226
+ */
227
+ export function isDuplicateOfAccepted(args) {
228
+ const threshold = args.threshold ?? DEFAULT_THRESHOLD_DEFINITE;
229
+ const candidate = tokenize(args.title);
230
+ if (candidate.size === 0)
231
+ return { dup: false };
232
+ let entries;
233
+ try {
234
+ entries = buildDecisionsLedger({ repoRoot: args.repoRoot });
235
+ }
236
+ catch {
237
+ return { dup: false };
238
+ }
239
+ let best = { sim: 0, id: "", title: "" };
240
+ for (const e of entries) {
241
+ const t = tokenize(e.title);
242
+ if (t.size === 0)
243
+ continue;
244
+ const sim = jaccard(candidate, t);
245
+ if (sim > best.sim)
246
+ best = { sim, id: e.id, title: e.title };
247
+ }
248
+ if (best.sim >= threshold) {
249
+ return {
250
+ dup: true,
251
+ matchId: best.id,
252
+ matchTitle: best.title,
253
+ similarity: Math.round(best.sim * 100) / 100,
254
+ };
255
+ }
256
+ return { dup: false };
257
+ }
217
258
  //# sourceMappingURL=dedup.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dedup.js","sourceRoot":"","sources":["../../src/attention/dedup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEvD,8DAA8D;AAC9D,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAc7B,SAAS,uBAAuB,CAAC,GAAW;IAI1C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AC1B,OAAO;YACL,GAAG,EAAE,IAAI;YACT,OAAO,EAAE,IAAI,CAAC,EAAE;YAChB,UAAU,EAAE,IAAI,CAAC,KAAK;YACtB,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG;SAC7C,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AACxB,CAAC"}
@@ -1,36 +1,42 @@
1
1
  /**
2
- * Deterministic component-dir detection for adoption (Phase 4-seed).
2
+ * LLM-driven, convention-agnostic component-layout detection.
3
3
  *
4
- * No LLM. Probes conventional component directories that actually
5
- * exist on disk and proposes a `components:` block for
6
- * `.cairn/config.yaml`. Returns `null` when the project has no
7
- * recognizable component layout (non-UI repos stay untouched).
4
+ * Cairn adoption ALWAYS runs inside an LLM coding agent, so detection
5
+ * leans on a model rather than a hardcoded convention list — there is no
6
+ * `src/components` / `packages/*` assumption baked in. A Sonnet call reads
7
+ * the repo's structural digest (per-directory file-extension histogram,
8
+ * the dirs that hold a `package.json`, and any workspace-manifest files)
9
+ * and returns the `components:` config: which workspaces carry reusable
10
+ * UI, where their component dirs live, the extensions in play, and a
11
+ * taxonomy that fits THAT workspace. A non-UI repo (a backend with no
12
+ * components) returns null and is left untouched.
8
13
  *
9
- * Isolation invariant (port invariant 3): a monorepo workspace is
10
- * NEVER guessed as `shared`. We omit the flag entirely — it normalizes
11
- * to isolated and leave the opt-in to the operator (manual config
12
- * edit or the future annotate step).
14
+ * Only mechanical, repo-agnostic facts stay deterministic: the file walk
15
+ * and the universal build-output exclude list. Everything that requires
16
+ * understanding "what is this directory for" is the model's job — naming,
17
+ * monorepo tooling, framework, and taxonomy are all inferred, never
18
+ * assumed.
19
+ *
20
+ * Isolation invariant (port invariant 3): a workspace is NEVER emitted as
21
+ * `shared`. The flag is omitted (it normalizes to isolated); the operator
22
+ * opts in afterward (the skill asks).
13
23
  */
14
24
  import { type ComponentsConfig } from "@isaacriehm/cairn-state";
15
- import type { DetectionResult } from "./types.js";
16
25
  /**
17
- * Probe `repoRoot` for a component layout and return a `components:`
18
- * config block (raw yaml shape), or `null` when nothing is found.
19
- *
20
- * - 2+ monorepo packages with component dirs `workspaces` form.
21
- * - Exactly one package, or root-level dirs flat single-app form.
22
- *
23
- * `detection` is reserved for future stack-aware tuning; extension
24
- * detection is presence-driven so the result holds even when stack
25
- * signatures are absent (e.g. a Vue repo with no tsconfig).
26
+ * Detect the repo's component layout via the model and return a
27
+ * `components:` config block (raw yaml shape), or `null` when the repo
28
+ * carries no reusable UI components. Retries the model call once before
29
+ * giving up. There is no deterministic fallback by design: detection only
30
+ * ever runs inside an LLM coding agent, so "no model" means adoption is
31
+ * not happening at all.
26
32
  */
27
- export declare function detectComponentsConfig(repoRoot: string, _detection: DetectionResult): ComponentsConfig | null;
33
+ export declare function detectComponentsConfig(repoRoot: string): Promise<ComponentsConfig | null>;
28
34
  export type EnsureComponentsStatus =
29
35
  /** No `.cairn/config.yaml` — the repo isn't adopted; run `/cairn-adopt` first. */
30
36
  "not-adopted"
31
37
  /** A `components:` block already exists — left untouched (idempotent). */
32
38
  | "exists"
33
- /** No recognizable component layout on disk — nothing written (non-UI repo). */
39
+ /** No reusable UI components on disk — nothing written (non-UI repo). */
34
40
  | "none"
35
41
  /** A `components:` block was detected and merged into the config. */
36
42
  | "written";
@@ -43,9 +49,9 @@ export interface EnsureComponentsConfigResult {
43
49
  }
44
50
  /**
45
51
  * Backfill a `components:` block into an already-adopted repo's
46
- * `.cairn/config.yaml`. The same deterministic FS probe adoption Phase
47
- * 4-seed runs, but applied to a config that already exists — so it MERGES
48
- * the key in (preserving every other key) rather than writing a fresh file.
52
+ * `.cairn/config.yaml`. Runs the same LLM detection adoption Phase 4-seed
53
+ * runs, but MERGES the key into a config that already exists (preserving
54
+ * every other key) rather than writing a fresh file.
49
55
  *
50
56
  * Idempotent: a repo that already carries a `components:` block is left
51
57
  * untouched ("exists"). The standalone backfill path
@@ -53,7 +59,7 @@ export interface EnsureComponentsConfigResult {
53
59
  * is the only caller; adoption keeps using `detectComponentsConfig`
54
60
  * directly inside 4-seed.
55
61
  *
56
- * Isolation invariant (port invariant 3): monorepo workspaces are never
57
- * guessed as `shared` — the operator opts in afterward (the skill asks).
62
+ * Isolation invariant (port invariant 3): workspaces are never emitted as
63
+ * `shared` — the operator opts in afterward (the skill asks).
58
64
  */
59
- export declare function ensureComponentsConfig(repoRoot: string): EnsureComponentsConfigResult;
65
+ export declare function ensureComponentsConfig(repoRoot: string): Promise<EnsureComponentsConfigResult>;
@@ -1,153 +1,255 @@
1
1
  /**
2
- * Deterministic component-dir detection for adoption (Phase 4-seed).
2
+ * LLM-driven, convention-agnostic component-layout detection.
3
3
  *
4
- * No LLM. Probes conventional component directories that actually
5
- * exist on disk and proposes a `components:` block for
6
- * `.cairn/config.yaml`. Returns `null` when the project has no
7
- * recognizable component layout (non-UI repos stay untouched).
4
+ * Cairn adoption ALWAYS runs inside an LLM coding agent, so detection
5
+ * leans on a model rather than a hardcoded convention list — there is no
6
+ * `src/components` / `packages/*` assumption baked in. A Sonnet call reads
7
+ * the repo's structural digest (per-directory file-extension histogram,
8
+ * the dirs that hold a `package.json`, and any workspace-manifest files)
9
+ * and returns the `components:` config: which workspaces carry reusable
10
+ * UI, where their component dirs live, the extensions in play, and a
11
+ * taxonomy that fits THAT workspace. A non-UI repo (a backend with no
12
+ * components) returns null and is left untouched.
8
13
  *
9
- * Isolation invariant (port invariant 3): a monorepo workspace is
10
- * NEVER guessed as `shared`. We omit the flag entirely — it normalizes
11
- * to isolated and leave the opt-in to the operator (manual config
12
- * edit or the future annotate step).
14
+ * Only mechanical, repo-agnostic facts stay deterministic: the file walk
15
+ * and the universal build-output exclude list. Everything that requires
16
+ * understanding "what is this directory for" is the model's job — naming,
17
+ * monorepo tooling, framework, and taxonomy are all inferred, never
18
+ * assumed.
19
+ *
20
+ * Isolation invariant (port invariant 3): a workspace is NEVER emitted as
21
+ * `shared`. The flag is omitted (it normalizes to isolated); the operator
22
+ * opts in afterward (the skill asks).
13
23
  */
14
- import { existsSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
15
- import { extname, join } from "node:path";
24
+ import { existsSync, readFileSync, writeFileSync } from "node:fs";
25
+ import { join } from "node:path";
16
26
  import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
17
- import { DEFAULT_CATEGORIES, DEFAULT_EXCLUDE, DEFAULT_EXTENSIONS, walkFs, } from "@isaacriehm/cairn-state";
18
- import { detectAll } from "./detect.js";
19
- /** Conventional component-dir suffixes, probed in order under a root or package. */
20
- const CONVENTIONAL_DIRS = [
21
- "src/components",
22
- "src/features",
23
- "app/components",
24
- "src/app/components",
25
- "components",
27
+ import { z } from "zod";
28
+ import { DEFAULT_EXCLUDE, walkFs, } from "@isaacriehm/cairn-state";
29
+ import { runClaude } from "../claude/index.js";
30
+ import { logger } from "../logger.js";
31
+ const log = logger("init.detect-components");
32
+ const TIMEOUT_MS = 120_000;
33
+ /** Cap the directory histogram so a huge repo can't blow the prompt. */
34
+ const MAX_DIGEST_DIRS = 600;
35
+ const MAX_MANIFEST_CHARS = 2_000;
36
+ /** Workspace-tooling manifests, read verbatim as grouping signal. */
37
+ const WORKSPACE_MANIFESTS = [
38
+ "pnpm-workspace.yaml",
39
+ "lerna.json",
40
+ "nx.json",
41
+ "turbo.json",
42
+ "rush.json",
26
43
  ];
27
- /** Monorepo package parents to scan for nested component dirs. */
28
- const MONOREPO_PARENTS = ["packages", "apps"];
29
- /** Extensions we sniff for beyond the React default. */
30
- const SVELTE_EXT = ".svelte";
31
- const VUE_EXT = ".vue";
32
44
  /**
33
- * Detect the extension set to scan. Defaults to the React profile
34
- * (`.tsx`/`.jsx`); appends `.vue`/`.svelte` only when such files
35
- * actually exist under the candidate dirs (framework-agnostic, but
36
- * presence-driven so we never invent extensions a repo doesn't use).
45
+ * Walk the repo once and build an agnostic structural digest: a
46
+ * per-directory extension histogram plus the set of dirs that carry a
47
+ * `package.json`. No convention names are consulted this is raw shape.
37
48
  */
38
- function detectExtensions(repoRoot, dirs) {
39
- let hasVue = false;
40
- let hasSvelte = false;
41
- let hasReact = false;
42
- const skipDirs = new Set([...DEFAULT_EXCLUDE]);
43
- for (const dir of dirs) {
44
- const abs = join(repoRoot, dir);
45
- if (!existsSync(abs))
46
- continue;
47
- walkFs({
48
- dir: abs,
49
- repoRoot,
50
- skipDirs,
51
- onFile: (_rel, fileAbs) => {
52
- const e = extname(fileAbs);
53
- if (e === VUE_EXT)
54
- hasVue = true;
55
- else if (e === SVELTE_EXT)
56
- hasSvelte = true;
57
- else if (e === ".tsx" || e === ".jsx")
58
- hasReact = true;
59
- },
60
- });
49
+ function buildRepoDigest(repoRoot) {
50
+ const skip = new Set([...DEFAULT_EXCLUDE]);
51
+ const perDir = new Map();
52
+ const packageRoots = [];
53
+ walkFs({
54
+ dir: repoRoot,
55
+ repoRoot,
56
+ skipDirs: skip,
57
+ onFile: (rel, _abs, entry) => {
58
+ const slash = rel.lastIndexOf("/");
59
+ const dir = slash === -1 ? "." : rel.slice(0, slash);
60
+ if (entry.name === "package.json")
61
+ packageRoots.push(dir);
62
+ const dot = rel.lastIndexOf(".");
63
+ const ext = dot > slash ? rel.slice(dot) : "(noext)";
64
+ let m = perDir.get(dir);
65
+ if (m === undefined) {
66
+ m = new Map();
67
+ perDir.set(dir, m);
68
+ }
69
+ m.set(ext, (m.get(ext) ?? 0) + 1);
70
+ },
71
+ });
72
+ const dirs = [...perDir.entries()].sort((a, b) => a[0].localeCompare(b[0]));
73
+ const lines = [];
74
+ for (const [dir, exts] of dirs) {
75
+ const parts = [...exts.entries()]
76
+ .sort((a, b) => b[1] - a[1])
77
+ .map(([e, c]) => `${c}${e}`);
78
+ lines.push(`${dir}: ${parts.join(" ")}`);
79
+ if (lines.length >= MAX_DIGEST_DIRS) {
80
+ lines.push(`… (${dirs.length - MAX_DIGEST_DIRS} more dirs truncated)`);
81
+ break;
82
+ }
61
83
  }
62
- const exts = [];
63
- // Keep the React pair as the baseline unless the repo is purely
64
- // Vue/Svelte — most TS UI repos still carry .tsx alongside framework files.
65
- if (hasReact || (!hasVue && !hasSvelte))
66
- exts.push(...DEFAULT_EXTENSIONS);
67
- if (hasVue)
68
- exts.push(VUE_EXT);
69
- if (hasSvelte)
70
- exts.push(SVELTE_EXT);
71
- return exts;
84
+ return { histogram: lines.join("\n"), packageRoots: packageRoots.sort() };
72
85
  }
73
- /** Conventional dirs (relative to `base`) that exist on disk. */
74
- function existingDirs(repoRoot, base) {
75
- return CONVENTIONAL_DIRS.map((suffix) => base.length > 0 ? `${base}/${suffix}` : suffix).filter((rel) => existsSync(join(repoRoot, rel)));
76
- }
77
- /** Scan `packages/*` + `apps/*` for sub-packages with component dirs. */
78
- function probeMonorepo(repoRoot) {
79
- const exclude = new Set([...DEFAULT_EXCLUDE]);
80
- const found = [];
81
- const usedNames = new Set();
82
- for (const parent of MONOREPO_PARENTS) {
83
- const parentAbs = join(repoRoot, parent);
84
- if (!existsSync(parentAbs))
86
+ function readWorkspaceManifests(repoRoot) {
87
+ const out = [];
88
+ for (const name of WORKSPACE_MANIFESTS) {
89
+ const p = join(repoRoot, name);
90
+ if (!existsSync(p))
85
91
  continue;
86
- let subs;
87
92
  try {
88
- subs = readdirSync(parentAbs, { withFileTypes: true });
93
+ out.push(`${name}:\n${readFileSync(p, "utf8").slice(0, MAX_MANIFEST_CHARS)}`);
89
94
  }
90
95
  catch {
91
- continue;
96
+ /* unreadable → skip */
92
97
  }
93
- for (const d of subs.sort((a, b) => a.name.localeCompare(b.name))) {
94
- if (!d.isDirectory() || exclude.has(d.name) || d.name.startsWith(".")) {
95
- continue;
96
- }
97
- const dirs = existingDirs(repoRoot, `${parent}/${d.name}`);
98
- if (dirs.length === 0)
99
- continue;
100
- // Disambiguate a name shared by packages/<x> and apps/<x>.
101
- let name = d.name;
102
- if (usedNames.has(name))
103
- name = `${parent}-${d.name}`;
104
- usedNames.add(name);
105
- found.push({ name, dirs });
98
+ }
99
+ const rootPkg = join(repoRoot, "package.json");
100
+ if (existsSync(rootPkg)) {
101
+ try {
102
+ out.push(`package.json (root):\n${readFileSync(rootPkg, "utf8").slice(0, MAX_MANIFEST_CHARS)}`);
103
+ }
104
+ catch {
105
+ /* skip */
106
106
  }
107
107
  }
108
- return found;
108
+ return out.join("\n\n");
109
109
  }
110
- /**
111
- * Probe `repoRoot` for a component layout and return a `components:`
112
- * config block (raw yaml shape), or `null` when nothing is found.
113
- *
114
- * - 2+ monorepo packages with component dirs → `workspaces` form.
115
- * - Exactly one package, or root-level dirs flat single-app form.
116
- *
117
- * `detection` is reserved for future stack-aware tuning; extension
118
- * detection is presence-driven so the result holds even when stack
119
- * signatures are absent (e.g. a Vue repo with no tsconfig).
120
- */
121
- export function detectComponentsConfig(repoRoot, _detection) {
122
- const workspaces = probeMonorepo(repoRoot);
123
- if (workspaces.length >= 2) {
124
- const allDirs = workspaces.flatMap((w) => w.dirs);
125
- const config = {
126
- extensions: detectExtensions(repoRoot, allDirs),
127
- categories: [...DEFAULT_CATEGORIES],
128
- exclude: [...DEFAULT_EXCLUDE],
129
- workspaces: Object.fromEntries(workspaces.map((w) => [w.name, { componentDirs: w.dirs }])),
130
- };
131
- return config;
110
+ const SYSTEM_PROMPT = `You map a repository's reusable-UI-component layout for a component registry. You receive the repo's workspace-manifest files, the directories that contain a package.json, and a per-directory file-extension histogram.
111
+
112
+ Return STRICT JSON matching the schema. No prose, no markdown.
113
+
114
+ Definitions:
115
+ - A "component dir" is a directory whose primary contents are REUSABLE UI components (buttons, cards, modals, layout, navigation, domain widgets). It is NOT a route/page dir, NOT tests, NOT stories, NOT backend/service/data/model code, NOT email templates.
116
+ - A "workspace" is an independently-scoped package. A monorepo has 2+ (each typically rooted at a package.json); a single app has one. Infer workspaces from the manifests / package roots / structure.
117
+
118
+ Hard rules be convention-agnostic:
119
+ - Do NOT assume any naming. Component dirs are NOT necessarily named "components"; workspaces are NOT necessarily under "packages/" or "apps/". Decide from the actual extension histogram and package roots, wherever they sit (top-level dirs, nested, anywhere).
120
+ - componentDirs are repo-relative POSIX paths that appear in the histogram.
121
+ - Include a workspace ONLY if it actually contains reusable UI components. A backend-only or data-only package (e.g. mostly .ts services with no component dir, or only email-template files) is OMITTED ENTIRELY.
122
+ - extensions: the component file extensions actually present in that workspace's component dirs (e.g. ".tsx", ".jsx", ".vue", ".svelte", ".astro").
123
+ - categories: a SHORT taxonomy (5-12 lowercase kebab-case tags) derived from what THIS workspace actually is — a marketing site leans ["layout","navigation","marketing","forms","media","feedback"], an app shell leans ["layout","navigation","shell","domain","forms","overlay","feedback","data-display"]. Do not copy a fixed list; fit the workspace.
124
+ - name: "" for a single-app repo; for monorepo workspaces use the package's directory name (the last path segment of its root).
125
+ - If the repo has NO reusable UI components at all, return {"is_ui_repo": false, "monorepo": false, "workspaces": []}.`;
126
+ const OUTPUT_SCHEMA = {
127
+ type: "object",
128
+ required: ["is_ui_repo", "monorepo", "workspaces"],
129
+ properties: {
130
+ is_ui_repo: { type: "boolean" },
131
+ monorepo: { type: "boolean" },
132
+ workspaces: {
133
+ type: "array",
134
+ items: {
135
+ type: "object",
136
+ required: ["name", "componentDirs", "extensions", "categories"],
137
+ properties: {
138
+ name: { type: "string" },
139
+ componentDirs: { type: "array", items: { type: "string" } },
140
+ extensions: { type: "array", items: { type: "string" } },
141
+ categories: { type: "array", items: { type: "string" } },
142
+ },
143
+ },
144
+ },
145
+ },
146
+ };
147
+ const ResultSchema = z.object({
148
+ is_ui_repo: z.boolean(),
149
+ monorepo: z.boolean(),
150
+ workspaces: z.array(z.object({
151
+ name: z.string(),
152
+ componentDirs: z.array(z.string()),
153
+ extensions: z.array(z.string()),
154
+ categories: z.array(z.string()),
155
+ })),
156
+ });
157
+ function buildPrompt(repoRoot) {
158
+ const digest = buildRepoDigest(repoRoot);
159
+ const manifests = readWorkspaceManifests(repoRoot);
160
+ return [
161
+ "WORKSPACE MANIFESTS:",
162
+ manifests.length > 0 ? manifests : "(none)",
163
+ "",
164
+ "DIRS CONTAINING A package.json (workspace boundaries):",
165
+ digest.packageRoots.length > 0 ? digest.packageRoots.join("\n") : "(none)",
166
+ "",
167
+ "DIRECTORY EXTENSION HISTOGRAM (path: <count><ext> …):",
168
+ digest.histogram.length > 0 ? digest.histogram : "(no source files found)",
169
+ "",
170
+ "Return the component-layout JSON.",
171
+ ].join("\n");
172
+ }
173
+ async function attempt(repoRoot, prompt) {
174
+ let result;
175
+ try {
176
+ result = await runClaude({
177
+ tier: "sonnet",
178
+ prompt,
179
+ system: SYSTEM_PROMPT,
180
+ jsonSchema: OUTPUT_SCHEMA,
181
+ timeoutMs: TIMEOUT_MS,
182
+ repoRoot,
183
+ cacheable: true,
184
+ isolateAmbientContext: true,
185
+ purpose: "init.detect-components",
186
+ });
187
+ }
188
+ catch (err) {
189
+ log.warn({ error: err instanceof Error ? err.message : String(err) }, "detect-components: runClaude failed");
190
+ return null;
191
+ }
192
+ const parsed = ResultSchema.safeParse(result.parsed);
193
+ if (!parsed.success) {
194
+ log.warn({ error: parsed.error.message }, "detect-components: response failed schema");
195
+ return null;
132
196
  }
133
- // Single-app: a lone monorepo package's dirs, else root-level dirs.
134
- const dirs = workspaces.length === 1
135
- ? workspaces[0].dirs
136
- : existingDirs(repoRoot, "");
137
- if (dirs.length === 0)
197
+ return parsed.data;
198
+ }
199
+ function toConfig(parsed) {
200
+ if (!parsed.is_ui_repo)
201
+ return null;
202
+ const ws = parsed.workspaces.filter((w) => w.componentDirs.length > 0);
203
+ if (ws.length === 0)
138
204
  return null;
205
+ const exclude = [...DEFAULT_EXCLUDE];
206
+ // Multi-workspace → monorepo `workspaces` form. A lone workspace (even
207
+ // if the model flagged the repo a monorepo) collapses to the flat form.
208
+ if (ws.length >= 2) {
209
+ const usedNames = new Set();
210
+ const workspaces = {};
211
+ for (const w of ws) {
212
+ let name = w.name.trim().length > 0 ? w.name.trim() : "app";
213
+ let n = 2;
214
+ while (usedNames.has(name))
215
+ name = `${w.name || "app"}-${n++}`;
216
+ usedNames.add(name);
217
+ workspaces[name] = {
218
+ componentDirs: w.componentDirs,
219
+ extensions: w.extensions,
220
+ categories: w.categories,
221
+ };
222
+ }
223
+ return { exclude, workspaces };
224
+ }
225
+ const single = ws[0];
139
226
  return {
140
- componentDirs: dirs,
141
- extensions: detectExtensions(repoRoot, dirs),
142
- categories: [...DEFAULT_CATEGORIES],
143
- exclude: [...DEFAULT_EXCLUDE],
227
+ componentDirs: single.componentDirs,
228
+ extensions: single.extensions,
229
+ categories: single.categories,
230
+ exclude,
144
231
  };
145
232
  }
233
+ /**
234
+ * Detect the repo's component layout via the model and return a
235
+ * `components:` config block (raw yaml shape), or `null` when the repo
236
+ * carries no reusable UI components. Retries the model call once before
237
+ * giving up. There is no deterministic fallback by design: detection only
238
+ * ever runs inside an LLM coding agent, so "no model" means adoption is
239
+ * not happening at all.
240
+ */
241
+ export async function detectComponentsConfig(repoRoot) {
242
+ const prompt = buildPrompt(repoRoot);
243
+ const out = (await attempt(repoRoot, prompt)) ?? (await attempt(repoRoot, prompt));
244
+ if (out === null)
245
+ return null;
246
+ return toConfig(out);
247
+ }
146
248
  /**
147
249
  * Backfill a `components:` block into an already-adopted repo's
148
- * `.cairn/config.yaml`. The same deterministic FS probe adoption Phase
149
- * 4-seed runs, but applied to a config that already exists — so it MERGES
150
- * the key in (preserving every other key) rather than writing a fresh file.
250
+ * `.cairn/config.yaml`. Runs the same LLM detection adoption Phase 4-seed
251
+ * runs, but MERGES the key into a config that already exists (preserving
252
+ * every other key) rather than writing a fresh file.
151
253
  *
152
254
  * Idempotent: a repo that already carries a `components:` block is left
153
255
  * untouched ("exists"). The standalone backfill path
@@ -155,10 +257,10 @@ export function detectComponentsConfig(repoRoot, _detection) {
155
257
  * is the only caller; adoption keeps using `detectComponentsConfig`
156
258
  * directly inside 4-seed.
157
259
  *
158
- * Isolation invariant (port invariant 3): monorepo workspaces are never
159
- * guessed as `shared` — the operator opts in afterward (the skill asks).
260
+ * Isolation invariant (port invariant 3): workspaces are never emitted as
261
+ * `shared` — the operator opts in afterward (the skill asks).
160
262
  */
161
- export function ensureComponentsConfig(repoRoot) {
263
+ export async function ensureComponentsConfig(repoRoot) {
162
264
  const configPath = join(repoRoot, ".cairn", "config.yaml");
163
265
  if (!existsSync(configPath)) {
164
266
  return { status: "not-adopted", monorepo: false };
@@ -168,8 +270,7 @@ export function ensureComponentsConfig(repoRoot) {
168
270
  if (parsed["components"] !== undefined && parsed["components"] !== null) {
169
271
  return { status: "exists", monorepo: false };
170
272
  }
171
- const detection = detectAll({ repoRoot });
172
- const components = detectComponentsConfig(repoRoot, detection);
273
+ const components = await detectComponentsConfig(repoRoot);
173
274
  if (components === null) {
174
275
  return { status: "none", monorepo: false };
175
276
  }