quiver-cli 0.7.0 → 1.0.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 (200) hide show
  1. package/README.md +110 -45
  2. package/bin/quiver-cli.mjs +3 -1
  3. package/dist/cli.js +733 -499
  4. package/package.json +2 -2
  5. package/template/.agents/AGENTS.md +4 -3
  6. package/template/.agents/config.json +7 -0
  7. package/template/.agents/plugins/opencode/rtk.ts +34 -0
  8. package/template/.agents/skills/agent-browser/SKILL.md +1 -0
  9. package/template/.agents/skills/apps/skybridge/SKILL.md +4 -0
  10. package/template/.agents/skills/design/impeccable/SKILL.md +36 -118
  11. package/template/.agents/skills/design/impeccable/reference/adapt.md +1 -0
  12. package/template/.agents/skills/design/impeccable/reference/adapt.native.md +58 -0
  13. package/template/.agents/skills/design/impeccable/reference/android.md +40 -0
  14. package/template/.agents/skills/design/impeccable/reference/animate.md +73 -188
  15. package/template/.agents/skills/design/impeccable/reference/audit.md +12 -9
  16. package/template/.agents/skills/design/impeccable/reference/audit.native.md +139 -0
  17. package/template/.agents/skills/design/impeccable/reference/bolder.md +19 -101
  18. package/template/.agents/skills/design/impeccable/reference/clarify.md +59 -253
  19. package/template/.agents/skills/design/impeccable/reference/colorize.md +51 -222
  20. package/template/.agents/skills/design/impeccable/reference/craft-floor.md +42 -0
  21. package/template/.agents/skills/design/impeccable/reference/craft.md +3 -121
  22. package/template/.agents/skills/design/impeccable/reference/critique.md +44 -23
  23. package/template/.agents/skills/design/impeccable/reference/degraded/asset-producer.md +90 -0
  24. package/template/.agents/skills/design/impeccable/reference/degraded/documenter.md +24 -0
  25. package/template/.agents/skills/design/impeccable/reference/degraded/finish-reviewer.md +37 -0
  26. package/template/.agents/skills/design/impeccable/reference/degraded/manual-edit-applier.md +92 -0
  27. package/template/.agents/skills/design/impeccable/reference/delight.md +47 -279
  28. package/template/.agents/skills/design/impeccable/reference/distill.md +2 -2
  29. package/template/.agents/skills/design/impeccable/reference/doctor.md +53 -0
  30. package/template/.agents/skills/design/impeccable/reference/document.md +60 -73
  31. package/template/.agents/skills/design/impeccable/reference/harden.md +1 -12
  32. package/template/.agents/skills/design/impeccable/reference/hooks.md +20 -5
  33. package/template/.agents/skills/design/impeccable/reference/init.md +72 -119
  34. package/template/.agents/skills/design/impeccable/reference/ios.md +45 -0
  35. package/template/.agents/skills/design/impeccable/reference/layout.md +54 -131
  36. package/template/.agents/skills/design/impeccable/reference/live-setup.md +102 -0
  37. package/template/.agents/skills/design/impeccable/reference/live.md +116 -511
  38. package/template/.agents/skills/design/impeccable/reference/new-work.md +105 -0
  39. package/template/.agents/skills/design/impeccable/reference/{product.md → operate.md} +6 -5
  40. package/template/.agents/skills/design/impeccable/reference/optimize.md +4 -4
  41. package/template/.agents/skills/design/impeccable/reference/overdrive.md +1 -4
  42. package/template/.agents/skills/design/impeccable/reference/polish.md +68 -212
  43. package/template/.agents/skills/design/impeccable/reference/quieter.md +3 -3
  44. package/template/.agents/skills/design/impeccable/reference/routing.md +18 -0
  45. package/template/.agents/skills/design/impeccable/reference/shape.md +38 -144
  46. package/template/.agents/skills/design/impeccable/reference/typeset.md +51 -250
  47. package/template/.agents/skills/design/impeccable/reference/visualize.md +47 -0
  48. package/template/.agents/skills/design/impeccable/scripts/command-metadata.json +1 -1
  49. package/template/.agents/skills/design/impeccable/scripts/concept-seed.mjs +558 -0
  50. package/template/.agents/skills/design/impeccable/scripts/context-signals.mjs +119 -10
  51. package/template/.agents/skills/design/impeccable/scripts/context.mjs +534 -45
  52. package/template/.agents/skills/design/impeccable/scripts/critique-storage.mjs +18 -47
  53. package/template/.agents/skills/design/impeccable/scripts/detector/browser/injected/index.mjs +96 -10
  54. package/template/.agents/skills/design/impeccable/scripts/detector/cli/main.mjs +174 -26
  55. package/template/.agents/skills/design/impeccable/scripts/detector/design-system.mjs +233 -0
  56. package/template/.agents/skills/design/impeccable/scripts/detector/detect-antipatterns-browser.js +3348 -203
  57. package/template/.agents/skills/design/impeccable/scripts/detector/engines/browser/detect-url.mjs +102 -7
  58. package/template/.agents/skills/design/impeccable/scripts/detector/engines/regex/detect-text.mjs +297 -97
  59. package/template/.agents/skills/design/impeccable/scripts/detector/engines/static-html/css-cascade.mjs +187 -16
  60. package/template/.agents/skills/design/impeccable/scripts/detector/engines/static-html/detect-html.mjs +41 -11
  61. package/template/.agents/skills/design/impeccable/scripts/detector/findings.mjs +7 -1
  62. package/template/.agents/skills/design/impeccable/scripts/detector/node/file-system.mjs +16 -2
  63. package/template/.agents/skills/design/impeccable/scripts/detector/registry/antipatterns.mjs +207 -38
  64. package/template/.agents/skills/design/impeccable/scripts/detector/rules/checks.mjs +3082 -173
  65. package/template/.agents/skills/design/impeccable/scripts/detector/shared/constants.mjs +11 -0
  66. package/template/.agents/skills/design/impeccable/scripts/detector/shared/fonts.mjs +30 -0
  67. package/template/.agents/skills/design/impeccable/scripts/doctor.mjs +336 -0
  68. package/template/.agents/skills/design/impeccable/scripts/embed-prompt.mjs +133 -0
  69. package/template/.agents/skills/design/impeccable/scripts/generate-image.mjs +240 -0
  70. package/template/.agents/skills/design/impeccable/scripts/hook-admin.mjs +98 -18
  71. package/template/.agents/skills/design/impeccable/scripts/hook-before-edit.mjs +46 -6
  72. package/template/.agents/skills/design/impeccable/scripts/hook-lib.mjs +518 -50
  73. package/template/.agents/skills/design/impeccable/scripts/hook.mjs +25 -8
  74. package/template/.agents/skills/design/impeccable/scripts/lib/artifact-schema.mjs +93 -0
  75. package/template/.agents/skills/design/impeccable/scripts/lib/composition-catalog.mjs +200 -0
  76. package/template/.agents/skills/design/impeccable/scripts/lib/concept-catalog.mjs +357 -0
  77. package/template/.agents/skills/design/impeccable/scripts/lib/impeccable-config.mjs +27 -7
  78. package/template/.agents/skills/design/impeccable/scripts/lib/impeccable-paths.mjs +17 -8
  79. package/template/.agents/skills/design/impeccable/scripts/lib/provider.mjs +5 -0
  80. package/template/.agents/skills/design/impeccable/scripts/lib/roll-selection.mjs +362 -0
  81. package/template/.agents/skills/design/impeccable/scripts/lib/staleness-deep.mjs +457 -0
  82. package/template/.agents/skills/design/impeccable/scripts/lib/staleness-notice.mjs +169 -0
  83. package/template/.agents/skills/design/impeccable/scripts/lib/staleness.mjs +457 -0
  84. package/template/.agents/skills/design/impeccable/scripts/lib/surface-briefs.mjs +151 -0
  85. package/template/.agents/skills/design/impeccable/scripts/lib/target-slug.mjs +33 -0
  86. package/template/.agents/skills/design/impeccable/scripts/lib/template-extensions.mjs +146 -0
  87. package/template/.agents/skills/design/impeccable/scripts/live/accept-css.mjs +617 -0
  88. package/template/.agents/skills/design/impeccable/scripts/live/accept-verify.mjs +60 -0
  89. package/template/.agents/skills/design/impeccable/scripts/live/browser-script-parts.mjs +7 -1
  90. package/template/.agents/skills/design/impeccable/scripts/live/completion.mjs +10 -1
  91. package/template/.agents/skills/design/impeccable/scripts/live/event-validation.mjs +67 -5
  92. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/astro.mjs +47 -0
  93. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/detect-utils.mjs +73 -0
  94. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/index.mjs +143 -0
  95. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/journal.mjs +197 -0
  96. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/nextjs.mjs +49 -0
  97. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/nuxt.mjs +161 -0
  98. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/script-src.mjs +17 -0
  99. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/static-html.mjs +26 -0
  100. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/sveltekit.mjs +71 -0
  101. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/tag-strategy.mjs +247 -0
  102. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/tanstack-start.mjs +70 -0
  103. package/template/.agents/skills/design/impeccable/scripts/live/frameworks/vite-generic.mjs +42 -0
  104. package/template/.agents/skills/design/impeccable/scripts/live/generation-preflight.mjs +149 -0
  105. package/template/.agents/skills/design/impeccable/scripts/live/instructions.mjs +142 -0
  106. package/template/.agents/skills/design/impeccable/scripts/live/poll-lanes.mjs +14 -0
  107. package/template/.agents/skills/design/impeccable/scripts/live/roots.mjs +508 -0
  108. package/template/.agents/skills/design/impeccable/scripts/live/session-store.mjs +324 -50
  109. package/template/.agents/skills/design/impeccable/scripts/live/source-lock.mjs +105 -0
  110. package/template/.agents/skills/design/impeccable/scripts/live/source-search.mjs +105 -0
  111. package/template/.agents/skills/design/impeccable/scripts/live/svelte-ast.mjs +961 -0
  112. package/template/.agents/skills/design/impeccable/scripts/live/svelte-component.mjs +588 -72
  113. package/template/.agents/skills/design/impeccable/scripts/live/sveltekit-adapter.mjs +59 -17
  114. package/template/.agents/skills/design/impeccable/scripts/live/tanstack-adapter.mjs +280 -0
  115. package/template/.agents/skills/design/impeccable/scripts/live/vocabulary.mjs +135 -0
  116. package/template/.agents/skills/design/impeccable/scripts/live-accept.mjs +210 -68
  117. package/template/.agents/skills/design/impeccable/scripts/live-browser.js +1562 -223
  118. package/template/.agents/skills/design/impeccable/scripts/live-commit-manual-edits.mjs +3 -0
  119. package/template/.agents/skills/design/impeccable/scripts/live-complete.mjs +33 -1
  120. package/template/.agents/skills/design/impeccable/scripts/live-inject.mjs +175 -255
  121. package/template/.agents/skills/design/impeccable/scripts/live-insert.mjs +26 -6
  122. package/template/.agents/skills/design/impeccable/scripts/live-manual-edit-evidence.mjs +6 -1
  123. package/template/.agents/skills/design/impeccable/scripts/live-poll.mjs +61 -16
  124. package/template/.agents/skills/design/impeccable/scripts/live-resume.mjs +39 -10
  125. package/template/.agents/skills/design/impeccable/scripts/live-server.mjs +573 -47
  126. package/template/.agents/skills/design/impeccable/scripts/live-status.mjs +17 -7
  127. package/template/.agents/skills/design/impeccable/scripts/live-wrap.mjs +124 -91
  128. package/template/.agents/skills/design/impeccable/scripts/live.mjs +88 -26
  129. package/template/.agents/skills/design/impeccable/scripts/palette.mjs +76 -81
  130. package/template/.agents/skills/design/impeccable/scripts/pin.mjs +18 -11
  131. package/template/.agents/skills/design/impeccable/scripts/serve-question.mjs +932 -0
  132. package/template/.agents/skills/design/impeccable/scripts/surface-brief.mjs +74 -0
  133. package/template/.agents/skills/design/shadcn/SKILL.md +46 -11
  134. package/template/.agents/skills/design/shadcn/cli.md +49 -16
  135. package/template/.agents/skills/design/shadcn/customization.md +14 -7
  136. package/template/.agents/skills/design/shadcn/evals/evals.json +30 -0
  137. package/template/.agents/skills/design/shadcn/mcp.md +27 -16
  138. package/template/.agents/skills/design/shadcn/registry.md +277 -0
  139. package/template/.agents/skills/design/shadcn/rules/chat.md +224 -0
  140. package/template/.agents/skills/design/shadcn/rules/composition.md +20 -2
  141. package/template/.agents/skills/design/shadcn/rules/styling.md +23 -0
  142. package/template/.agents/skills/find-skills/SKILL.md +2 -3
  143. package/template/.agents/skills/hono/SKILL.md +579 -0
  144. package/template/.agents/skills/integrations/langfuse/SKILL.md +10 -6
  145. package/template/.agents/skills/integrations/langfuse/references/ci-cd.md +41 -0
  146. package/template/.agents/skills/integrations/langfuse/references/cli.md +8 -0
  147. package/template/.agents/skills/integrations/langfuse/references/error-analysis.md +15 -27
  148. package/template/.agents/skills/integrations/langfuse/references/instrumentation.md +39 -52
  149. package/template/.agents/skills/integrations/langfuse/references/judge-calibration.md +5 -2
  150. package/template/.agents/skills/integrations/langfuse/references/prompt-engineering.md +35 -0
  151. package/template/.agents/skills/integrations/langfuse/references/prompt-migration.md +41 -196
  152. package/template/.agents/skills/integrations/langfuse/references/skill-feedback.md +3 -0
  153. package/template/.agents/skills/integrations/langfuse/references/trace-evaluator-upgrade.md +76 -0
  154. package/template/.agents/skills/integrations/langfuse/references/user-feedback.md +4 -0
  155. package/template/.agents/skills/integrations/langfuse/references/v4-project-migration.md +73 -0
  156. package/template/.agents/skills/supabase/CHANGELOG.md +71 -0
  157. package/template/.agents/skills/supabase/SKILL.md +145 -0
  158. package/template/.agents/skills/supabase/assets/feedback-issue-template.md +17 -0
  159. package/template/.agents/skills/supabase/references/skill-feedback.md +17 -0
  160. package/template/.agents/skills/supabase-postgres-best-practices/CHANGELOG.md +73 -0
  161. package/template/.agents/skills/supabase-postgres-best-practices/SKILL.md +64 -0
  162. package/template/.agents/skills/supabase-postgres-best-practices/references/_contributing.md +170 -0
  163. package/template/.agents/skills/supabase-postgres-best-practices/references/_sections.md +39 -0
  164. package/template/.agents/skills/supabase-postgres-best-practices/references/_template.md +34 -0
  165. package/template/.agents/skills/supabase-postgres-best-practices/references/advanced-full-text-search.md +55 -0
  166. package/template/.agents/skills/supabase-postgres-best-practices/references/advanced-jsonb-indexing.md +49 -0
  167. package/template/.agents/skills/supabase-postgres-best-practices/references/conn-idle-timeout.md +46 -0
  168. package/template/.agents/skills/supabase-postgres-best-practices/references/conn-limits.md +44 -0
  169. package/template/.agents/skills/supabase-postgres-best-practices/references/conn-pooling.md +41 -0
  170. package/template/.agents/skills/supabase-postgres-best-practices/references/conn-prepared-statements.md +46 -0
  171. package/template/.agents/skills/supabase-postgres-best-practices/references/data-batch-inserts.md +54 -0
  172. package/template/.agents/skills/supabase-postgres-best-practices/references/data-n-plus-one.md +53 -0
  173. package/template/.agents/skills/supabase-postgres-best-practices/references/data-pagination.md +50 -0
  174. package/template/.agents/skills/supabase-postgres-best-practices/references/data-upsert.md +50 -0
  175. package/template/.agents/skills/supabase-postgres-best-practices/references/lock-advisory.md +56 -0
  176. package/template/.agents/skills/supabase-postgres-best-practices/references/lock-deadlock-prevention.md +68 -0
  177. package/template/.agents/skills/supabase-postgres-best-practices/references/lock-short-transactions.md +50 -0
  178. package/template/.agents/skills/supabase-postgres-best-practices/references/lock-skip-locked.md +54 -0
  179. package/template/.agents/skills/supabase-postgres-best-practices/references/monitor-explain-analyze.md +45 -0
  180. package/template/.agents/skills/supabase-postgres-best-practices/references/monitor-pg-stat-statements.md +55 -0
  181. package/template/.agents/skills/supabase-postgres-best-practices/references/monitor-vacuum-analyze.md +55 -0
  182. package/template/.agents/skills/supabase-postgres-best-practices/references/query-composite-indexes.md +44 -0
  183. package/template/.agents/skills/supabase-postgres-best-practices/references/query-covering-indexes.md +40 -0
  184. package/template/.agents/skills/supabase-postgres-best-practices/references/query-index-types.md +48 -0
  185. package/template/.agents/skills/supabase-postgres-best-practices/references/query-missing-indexes.md +43 -0
  186. package/template/.agents/skills/supabase-postgres-best-practices/references/query-partial-indexes.md +45 -0
  187. package/template/.agents/skills/supabase-postgres-best-practices/references/schema-constraints.md +80 -0
  188. package/template/.agents/skills/supabase-postgres-best-practices/references/schema-data-types.md +46 -0
  189. package/template/.agents/skills/supabase-postgres-best-practices/references/schema-foreign-key-indexes.md +59 -0
  190. package/template/.agents/skills/supabase-postgres-best-practices/references/schema-lowercase-identifiers.md +55 -0
  191. package/template/.agents/skills/supabase-postgres-best-practices/references/schema-partitioning.md +55 -0
  192. package/template/.agents/skills/supabase-postgres-best-practices/references/schema-primary-keys.md +61 -0
  193. package/template/.agents/skills/supabase-postgres-best-practices/references/security-privileges.md +54 -0
  194. package/template/.agents/skills/supabase-postgres-best-practices/references/security-rls-basics.md +50 -0
  195. package/template/.agents/skills/supabase-postgres-best-practices/references/security-rls-performance.md +63 -0
  196. package/template/.agents/upstreams.json +34 -12
  197. package/template/.agents/skills/design/impeccable/reference/brand.md +0 -108
  198. package/template/.agents/skills/design/impeccable/reference/codex.md +0 -105
  199. package/template/.agents/skills/design/impeccable/reference/interaction-design.md +0 -189
  200. package/template/.agents/skills/integrations/langfuse/references/sdk-upgrade.md +0 -175
@@ -0,0 +1,362 @@
1
+ // The one implementation of world-roll selection.
2
+ //
3
+ // Two copies of this logic used to exist: this repo's concept-seed.mjs and the
4
+ // service repo's functions/api/_worldroll-core.js, whose header claimed they
5
+ // matched "exactly". They did not. The API had no breadth gate on either pool,
6
+ // no rating weighting for compositions, and dealt one composition where the
7
+ // seeder dealt three. Because the catalog never ships with the skill, every real
8
+ // user rolls through that API, so those gates reached nobody.
9
+ //
10
+ // Why generators. The two callers cannot agree on a hash: Node has a
11
+ // synchronous one, Workers only have async crypto.subtle, and concept-seed's
12
+ // local render path is deliberately synchronous so prepared eval sessions and
13
+ // tests can call it without awaiting. Rather than fork the logic or force the
14
+ // whole seeder async, the selection is written once as a generator that yields
15
+ // batches of strings to hash and resumes with their digests. runSyncSelection
16
+ // and runAsyncSelection below are the only runtime-specific code, about eight
17
+ // lines each. Both digests are the same bytes, so a roll is identical either way.
18
+ //
19
+ // Nothing here reads a file, an environment variable, or the network: callers
20
+ // pass pools in.
21
+
22
+ export const WELL_TIERS = ['graphic', 'interaction', 'atmosphere'];
23
+
24
+ // Grain: how much of the product a composition composes. Named grain rather than
25
+ // scope because scope already means direction-or-surface on every roll, and
26
+ // 'surface' is already a register value, so a scope of 'surface' would collide
27
+ // with both.
28
+ //
29
+ // This axis is framed by what the skill can be asked for, not by what the
30
+ // catalog happens to hold. A user asks for a docs site, an onboarding flow, a
31
+ // landing page, or a data table, and those are four different amounts of
32
+ // product. Register says what kind of work it is; grain says how much of it.
33
+ // Without grain, a request for a hero section can be dealt a whole-site
34
+ // navigation structure and nothing notices.
35
+ //
36
+ // Measured when this was added: 137 of 173 approved compositions were view
37
+ // grain, product grain was empty, and flow grain held one entry. That is why an
38
+ // onboarding request had nothing to draw.
39
+ export const COMPOSITION_GRAINS = [
40
+ 'product', // a whole site or app: its information architecture
41
+ 'flow', // a sequence of views with one outcome: onboarding, checkout, setup
42
+ 'view', // one page or screen
43
+ 'region', // a section inside a view: a hero, a feature grid, a table
44
+ ];
45
+
46
+ // Delivery targets a composition can survive. Mirrors the skill's platform axis
47
+ // minus 'adaptive', which is a project-level value meaning both native targets
48
+ // rather than something a single composition is authored for.
49
+ //
50
+ // A composition that leans on hover, a pointer, or a wide viewport does not
51
+ // survive a phone, and nothing in the schema could say so before this.
52
+ export const COMPOSITION_PLATFORMS = ['web', 'ios', 'android'];
53
+
54
+ // Both fields are optional and absence means eligible everywhere, so no entry
55
+ // has to be backfilled before this ships and no existing roll changes.
56
+ export function isGrain(value) {
57
+ return COMPOSITION_GRAINS.includes(value);
58
+ }
59
+
60
+ export function isPlatform(value) {
61
+ return COMPOSITION_PLATFORMS.includes(value);
62
+ }
63
+
64
+
65
+ /**
66
+ * Drives a selection generator with a synchronous hash.
67
+ * @param {Generator} generator yields string[] to hash, resumes with hex string[]
68
+ * @param {(input: string) => string} hash
69
+ */
70
+ export function runSyncSelection(generator, hash) {
71
+ let step = generator.next();
72
+ while (!step.done) step = generator.next(step.value.map(hash));
73
+ return step.value;
74
+ }
75
+
76
+ /**
77
+ * Drives a selection generator with an asynchronous hash.
78
+ * @param {Generator} generator
79
+ * @param {(input: string) => Promise<string>} hash
80
+ */
81
+ export async function runAsyncSelection(generator, hash) {
82
+ let step = generator.next();
83
+ while (!step.done) step = generator.next(await Promise.all(step.value.map(hash)));
84
+ return step.value;
85
+ }
86
+
87
+ // Ranks items by the digest of `${input}:${id}`, descending, with the id as a
88
+ // stable tiebreak. Yields every needed digest in one batch so the async driver
89
+ // can resolve them concurrently.
90
+ function* rank(items, input, idFor = item => item.id) {
91
+ const ids = items.map(idFor);
92
+ const digests = yield ids.map(id => `${input}:${id}`);
93
+ return items
94
+ .map((item, index) => ({ item, id: ids[index], score: digests[index] }))
95
+ .sort((a, b) => b.score.localeCompare(a.score) || a.id.localeCompare(b.id))
96
+ .map(entry => entry.item);
97
+ }
98
+
99
+ // Two independent exclusions, and either one is enough to hold a world back.
100
+ // Rating grades quality: a 3-star earns a second ticket, a 1-star marginal keep
101
+ // leaves the pool. Breadth says whether a world can serve an arbitrary build at
102
+ // all, so a niche world leaves however good it is, keeping its approval for
103
+ // direct briefs. Breadth was split out of rating because the only way to hold a
104
+ // narrow world back used to be calling it marginal, which made "excellent but
105
+ // narrow" unrecordable and corrupted ratings as a calibration signal.
106
+ function challengerTickets(pool) {
107
+ return pool.flatMap(concept => {
108
+ const rating = concept.review?.rating;
109
+ if (rating === 1 || concept.review?.breadth === 'niche') return [];
110
+ return rating === 3
111
+ ? [{ concept, ticket: 0 }, { concept, ticket: 1 }]
112
+ : [{ concept, ticket: 0 }];
113
+ });
114
+ }
115
+
116
+ function compositionTickets(pool) {
117
+ return pool.flatMap(composition => {
118
+ const rating = composition.review?.rating;
119
+ if (rating === 1) return [];
120
+ return rating === 3
121
+ ? [{ composition, ticket: 0 }, { composition, ticket: 1 }]
122
+ : [{ composition, ticket: 0 }];
123
+ });
124
+ }
125
+
126
+ /**
127
+ * Six challengers, two per translation tier, from an explicit approved pool.
128
+ * Drive with runSyncSelection or runAsyncSelection.
129
+ *
130
+ * @param {object} options
131
+ * @param {'direction'|'surface'} options.scope
132
+ * @param {string} options.key same key reproduces the roll
133
+ * @param {number} [options.reroll] round of the re-roll chain
134
+ * @param {number|null} [options.minRating] optional floor, skipped per tier it would empty
135
+ * @param {Array} options.concepts merged concepts with status, review, wellTier, familyId
136
+ * @returns {Generator<string[], {approved: Array, picks: Array}, string[]>}
137
+ */
138
+ // A world with no allowedModes is eligible everywhere, which is what keeps this
139
+ // additive: nothing has to be backfilled for the filter to be safe.
140
+ function modeAllows(concept, mode) {
141
+ const allowed = concept.review?.allowedModes;
142
+ if (!Array.isArray(allowed) || allowed.length === 0) return true;
143
+ return allowed.includes(mode);
144
+ }
145
+
146
+ export function* selectApprovedChallengers({ scope, key, reroll = 0, minRating = null, mode = null, concepts }) {
147
+ const approved = concepts.filter(concept => concept.status === 'approved');
148
+ // Direction chooses a durable identity, so it draws worlds; surface designs
149
+ // one page inside a committed identity, so it draws compositions. Duals serve
150
+ // both. A tier with no matching-strength approvals falls back to its full
151
+ // approved pool rather than starving the roll.
152
+ const wanted = scope === 'direction'
153
+ ? new Set(['world', 'dual'])
154
+ : new Set(['composition', 'dual']);
155
+
156
+ const approvedByTier = new Map();
157
+ for (const concept of approved) {
158
+ const tier = approvedByTier.get(concept.wellTier) || [];
159
+ tier.push(concept);
160
+ approvedByTier.set(concept.wellTier, tier);
161
+ }
162
+ if (WELL_TIERS.some(tier => !(approvedByTier.get(tier) || []).length)) {
163
+ throw new Error('concept-seed: every challenger tier needs at least one approved concept');
164
+ }
165
+
166
+ // Optional minimum-rating gate, applied per tier and skipped for any tier it
167
+ // would empty, so a thin tier degrades to its full approved pool.
168
+ if (minRating) {
169
+ for (const [tier, pool] of approvedByTier) {
170
+ const rated = pool.filter(concept => (concept.review?.rating || 0) >= minRating);
171
+ if (rated.length > 0) approvedByTier.set(tier, rated);
172
+ }
173
+ }
174
+ // Mode eligibility, per tier and skipped where it would empty a tier. Worlds
175
+ // used to be drawn with no mode awareness at all, so a build asking for an app
176
+ // UI could get six worlds that only make sense on a landing page. A world is an
177
+ // identity and identities transfer further than compositions do, so this is a
178
+ // ceiling the reviewer sets rather than a category assignment: eligible
179
+ // everywhere until someone says otherwise.
180
+ if (mode) {
181
+ for (const [tier, pool] of approvedByTier) {
182
+ const eligible = pool.filter(concept => modeAllows(concept, mode));
183
+ if (eligible.length > 0) approvedByTier.set(tier, eligible);
184
+ }
185
+ }
186
+ for (const [tier, pool] of approvedByTier) {
187
+ const matching = pool.filter(concept => wanted.has(concept.strength));
188
+ if (matching.length > 0) approvedByTier.set(tier, matching);
189
+ }
190
+
191
+ // Two challengers per tier, so every roll carries near-zero-translation
192
+ // graphic systems beside instrument languages and atmosphere worlds, with the
193
+ // second pick preferring a different family. Tier order is rolled too, to
194
+ // avoid positional bias.
195
+ function* pickRound(round, excluded) {
196
+ const salt = round === 0 ? '' : `:reroll-${round}`;
197
+ const tierOrder = (yield* rank(
198
+ WELL_TIERS.map(id => ({ id })),
199
+ `${scope}:${key}:tiers${salt}`
200
+ )).map(item => item.id);
201
+ const picks = [];
202
+ for (const [index, tier] of tierOrder.entries()) {
203
+ let pool = approvedByTier.get(tier).filter(concept => !excluded.has(concept.id));
204
+ // A tier exhausted by prior rounds falls back to reuse over starvation.
205
+ if (pool.length === 0) pool = approvedByTier.get(tier);
206
+ let tickets = challengerTickets(pool);
207
+ if (tickets.length === 0) tickets = pool.map(concept => ({ concept, ticket: 0 }));
208
+ const ranked = yield* rank(
209
+ tickets,
210
+ `${scope}:${key}:challenger-${index}${salt}`,
211
+ entry => `${entry.concept.id}#${entry.ticket}`
212
+ );
213
+ const order = [];
214
+ const seen = new Set();
215
+ for (const entry of ranked) {
216
+ if (seen.has(entry.concept.id)) continue;
217
+ seen.add(entry.concept.id);
218
+ order.push(entry.concept);
219
+ }
220
+ const first = order[0];
221
+ const second = order.find(concept => concept.familyId !== first.familyId)
222
+ || order.find(concept => concept.id !== first.id);
223
+ picks.push(...(second ? [first, second] : [first]));
224
+ }
225
+ return picks;
226
+ }
227
+
228
+ // Round n of a re-roll chain excludes everything rounds 0..n-1 drew, so the
229
+ // same base key reproduces the whole chain.
230
+ const excluded = new Set();
231
+ let picks = yield* pickRound(0, excluded);
232
+ for (let round = 1; round <= reroll; round += 1) {
233
+ for (const pick of picks) excluded.add(pick.id);
234
+ picks = yield* pickRound(round, excluded);
235
+ }
236
+ return { approved, picks };
237
+ }
238
+
239
+ function emptyMatch(grain, platform, platformExcluded = 0) {
240
+ return { grain: grain ?? null, atGrain: grain ? 0 : null, grainAvailable: grain ? 0 : null, platform: platform ?? null, platformExcluded };
241
+ }
242
+
243
+ /**
244
+ * Three identity-free composition inputs from an explicit approved pool.
245
+ * Drive with runSyncSelection or runAsyncSelection.
246
+ *
247
+ * One input was too weak a counterweight to a model's habitual page skeleton:
248
+ * it became a single optional flourish beside six identity challengers rather
249
+ * than a real search over composition. Distinct composition families are preferred
250
+ * so a roll tests materially different hierarchy, sequence, and interaction
251
+ * laws. Cross-mode fallback would make the input misleading, so an absent mode
252
+ * returns nothing rather than borrowing. Re-rolls exclude every earlier set
253
+ * until the pool runs out.
254
+ *
255
+ * @param {object} options
256
+ * @param {'direction'|'surface'} options.scope
257
+ * @param {string} options.key
258
+ * @param {number} [options.reroll]
259
+ * @param {string|null} [options.mode] surface register to stay inside
260
+ * @param {string|null} [options.grain] how much of the product is in play
261
+ * @param {string|null} [options.platform] delivery target the result has to survive
262
+ * @param {Array} options.compositions merged compositions with status, review, surface, familyId
263
+ * @param {number} [options.count]
264
+ * @returns {Generator<string[], {picks: Array, match: object}, string[]>}
265
+ */
266
+ export function* selectApprovedCompositions({ scope, key, reroll = 0, mode = null, grain = null, platform = null, compositions, count = 3 }) {
267
+ // Compositions honour the same breadth gate as worlds: one too specific to serve
268
+ // an arbitrary build stays approved for direct briefs and leaves the
269
+ // challenger pool. Falls back to the full approved set rather than returning
270
+ // nothing if every approved composition is niche.
271
+ let approved = compositions.filter(composition => composition.status === 'approved');
272
+ const broad = approved.filter(composition => composition.review?.breadth !== 'niche');
273
+ if (broad.length > 0) approved = broad;
274
+ if (approved.length === 0) return { picks: [], match: emptyMatch(grain, platform) };
275
+ if (mode) {
276
+ const matching = approved.filter(composition => composition.surface === mode);
277
+ if (matching.length === 0) return { picks: [], match: emptyMatch(grain, platform) };
278
+ approved = matching;
279
+ }
280
+ // Platform is a hard filter, unlike grain. A composition that needs hover or a
281
+ // pointer does not degrade on a phone into something slightly worse; it stops
282
+ // working, so borrowing it would be a defect rather than a stretch. Absent
283
+ // platforms means it survives anywhere.
284
+ let platformExcluded = 0;
285
+ if (platform) {
286
+ const survives = approved.filter(composition => {
287
+ const only = composition.platforms;
288
+ return !Array.isArray(only) || only.length === 0 || only.includes(platform);
289
+ });
290
+ platformExcluded = approved.length - survives.length;
291
+ // No fallback here either: dealing a hover-only composition to a phone build
292
+ // is worse than dealing nothing, and an empty deal is a visible gap.
293
+ approved = survives;
294
+ if (approved.length === 0) return { picks: [], match: emptyMatch(grain, platform, platformExcluded) };
295
+ }
296
+
297
+ const prior = new Set();
298
+ let picks = [];
299
+ for (let round = 0; round <= reroll; round += 1) {
300
+ const available = approved.filter(composition => !prior.has(composition.id));
301
+ const base = available.length >= Math.min(count, approved.length) ? available : approved;
302
+ // Rating weights the draw as it does for worlds. It matters more here
303
+ // because the per-surface pools are small, so an unweighted shuffle repeats
304
+ // a weak composition far more often. Each ticket carries its index so the rank
305
+ // sees a distinct key per ticket: ranking bare duplicates would hash
306
+ // identically and the pick loop's id-dedupe would silently discard the
307
+ // second copy, making the weighting a no-op.
308
+ let tickets = compositionTickets(base);
309
+ // A pool of nothing but 1-star keeps still has to yield compositions.
310
+ if (tickets.length === 0) tickets = base.map(composition => ({ composition, ticket: 0 }));
311
+ const ranked = (yield* rank(
312
+ tickets,
313
+ // The salt keeps the word "staging" deliberately. It is hash input, so
314
+ // renaming it would re-deal every roll anyone has ever reproduced by key.
315
+ round === 0 ? `${scope}:${key}:staging` : `${scope}:${key}:staging:reroll-${round}`,
316
+ entry => `${entry.composition.id}#${entry.ticket}`
317
+ )).map(entry => entry.composition);
318
+
319
+ // Grain is a preference, not a filter: requesting an onboarding flow deals
320
+ // flow-grain compositions first and tops up from the rest of the register
321
+ // rather than dealing fewer than three. A stable partition of an already
322
+ // deterministic ranking is still deterministic.
323
+ //
324
+ // The top-up is why match is reported. Dealing three plausible view-grain
325
+ // compositions against a flow request, with no signal that none matched, is
326
+ // the same silent-plausibility failure this whole axis exists to fix: the
327
+ // model would improvise the flow structure while believing it was handed one.
328
+ const ordered = grain
329
+ ? [...ranked.filter(composition => composition.grain === grain),
330
+ ...ranked.filter(composition => composition.grain !== grain)]
331
+ : ranked;
332
+
333
+ const families = new Set();
334
+ picks = [];
335
+ for (const composition of ordered) {
336
+ const family = composition.familyId ?? composition.id;
337
+ if (families.has(family)) continue;
338
+ picks.push(composition);
339
+ families.add(family);
340
+ if (picks.length >= count) break;
341
+ }
342
+ for (const composition of ordered) {
343
+ if (picks.length >= count) break;
344
+ if (!picks.some(pick => pick.id === composition.id)) picks.push(composition);
345
+ }
346
+ if (round < reroll) picks.forEach(composition => prior.add(composition.id));
347
+ }
348
+
349
+ const atGrain = grain ? picks.filter(composition => composition.grain === grain).length : null;
350
+ return {
351
+ picks,
352
+ match: {
353
+ grain: grain ?? null,
354
+ // How many of the dealt compositions actually sit at the requested grain.
355
+ // 0 with a grain requested means every pick is a borrowed structure.
356
+ atGrain,
357
+ grainAvailable: grain ? approved.filter(composition => composition.grain === grain).length : null,
358
+ platform: platform ?? null,
359
+ platformExcluded,
360
+ },
361
+ };
362
+ }