@skitterbyte/skitterspec-github 0.0.0-stage → 1.0.1

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 (112) hide show
  1. package/MIGRATION.md +1126 -0
  2. package/README.md +53 -2
  3. package/assets/claude-md-section.md +85 -0
  4. package/assets/commands/allow-main.md +33 -0
  5. package/assets/commands/spec-connect.md +13 -0
  6. package/assets/commands/spec-live.md +14 -0
  7. package/assets/commands/spec-remote-review.md +37 -0
  8. package/assets/commands/spec-skip.md +24 -0
  9. package/assets/core/SETUP.md +112 -0
  10. package/assets/core/env.config.json.example +56 -0
  11. package/assets/core/env.config.md +557 -0
  12. package/assets/core/gating.config.json.example +4 -0
  13. package/assets/core/gating.config.md +81 -0
  14. package/assets/core/github.config.json.example +18 -0
  15. package/assets/core/github.config.md +66 -0
  16. package/assets/hooks/main-guard.cjs +162 -0
  17. package/assets/hooks/review-gate.cjs +146 -0
  18. package/assets/review/page.html +3384 -0
  19. package/assets/rules/commit-trailers.md +103 -0
  20. package/assets/rules/negative-checks.md +69 -0
  21. package/assets/rules/offered-unblocks.md +100 -0
  22. package/assets/rules/spec-planning.md +692 -0
  23. package/assets/rules/spec-reports.md +557 -0
  24. package/assets/skills/no-spec/SKILL.md +246 -0
  25. package/assets/skills/spec/SKILL.md +718 -0
  26. package/assets/skills/spec-bug/SKILL.md +646 -0
  27. package/assets/skills/spec-cancel/SKILL.md +263 -0
  28. package/assets/skills/spec-claim/SKILL.md +122 -0
  29. package/assets/skills/spec-complete/SKILL.md +450 -0
  30. package/assets/skills/spec-diff/SKILL.md +870 -0
  31. package/assets/skills/spec-github-setup/SKILL.md +236 -0
  32. package/assets/skills/spec-hotfix/SKILL.md +668 -0
  33. package/assets/skills/spec-init/SKILL.md +178 -0
  34. package/assets/skills/spec-list/SKILL.md +172 -0
  35. package/assets/skills/spec-next/SKILL.md +685 -0
  36. package/assets/skills/spec-push/SKILL.md +307 -0
  37. package/assets/skills/spec-review/SKILL.md +258 -0
  38. package/assets/skills/spec-reviewed/SKILL.md +284 -0
  39. package/assets/skills/spec-start/SKILL.md +554 -0
  40. package/assets/skills/spec-status/SKILL.md +112 -0
  41. package/assets/skills/spec-sync/SKILL.md +226 -0
  42. package/assets/skills/spec-to-main/SKILL.md +139 -0
  43. package/bin/skitterspec-github.js +61 -0
  44. package/package.json +41 -4
  45. package/src/cli.js +5907 -0
  46. package/src/deprecate.js +138 -0
  47. package/src/env/bootstrap.js +188 -0
  48. package/src/env/building.js +143 -0
  49. package/src/env/classify.js +176 -0
  50. package/src/env/commitcmd.js +108 -0
  51. package/src/env/config.js +753 -0
  52. package/src/env/dev.js +63 -0
  53. package/src/env/hooks.js +226 -0
  54. package/src/env/hotfix.js +411 -0
  55. package/src/env/hotfixland.js +302 -0
  56. package/src/env/integrate.js +126 -0
  57. package/src/env/live.js +504 -0
  58. package/src/env/mainguard.js +299 -0
  59. package/src/env/provision.js +466 -0
  60. package/src/env/proxy.js +225 -0
  61. package/src/env/prune.js +119 -0
  62. package/src/env/registry.js +159 -0
  63. package/src/env/render.js +17 -0
  64. package/src/env/resolve.js +716 -0
  65. package/src/env/review.js +2577 -0
  66. package/src/env/reviewers/coderabbit.js +228 -0
  67. package/src/env/reviewers.js +517 -0
  68. package/src/env/serve.js +1044 -0
  69. package/src/env/supervise.js +157 -0
  70. package/src/env/teardown.js +248 -0
  71. package/src/env/trust.js +87 -0
  72. package/src/gating.js +155 -0
  73. package/src/init.js +1150 -0
  74. package/src/lines-diff.js +114 -0
  75. package/src/prompts.js +129 -0
  76. package/src/vendor/github/api.js +800 -0
  77. package/src/vendor/github/cli-sync.js +1530 -0
  78. package/src/vendor/github/commands.js +44 -0
  79. package/src/vendor/github/config.js +414 -0
  80. package/src/vendor/github/credentials.js +109 -0
  81. package/src/vendor/github/dialect.js +62 -0
  82. package/src/vendor/github/identity.js +38 -0
  83. package/src/vendor/github/mcp.js +85 -0
  84. package/src/vendor/provider-kit/index.js +41 -0
  85. package/src/vendor/provider-kit/src/adapter.js +23 -0
  86. package/src/vendor/provider-kit/src/apply.js +207 -0
  87. package/src/vendor/provider-kit/src/args.js +68 -0
  88. package/src/vendor/provider-kit/src/assignment.js +277 -0
  89. package/src/vendor/provider-kit/src/config.js +198 -0
  90. package/src/vendor/provider-kit/src/credentials.js +404 -0
  91. package/src/vendor/provider-kit/src/doctor.js +35 -0
  92. package/src/vendor/provider-kit/src/http.js +63 -0
  93. package/src/vendor/provider-kit/src/identity.js +93 -0
  94. package/src/vendor/provider-kit/src/md-compare.js +39 -0
  95. package/src/vendor/provider-kit/src/md-tables.js +94 -0
  96. package/src/vendor/provider-kit/src/prompt.js +50 -0
  97. package/src/vendor/provider-kit/src/released.js +269 -0
  98. package/src/vendor/provider-kit/src/releases.js +380 -0
  99. package/src/vendor/provider-kit/src/specs.js +166 -0
  100. package/src/vendor/provider-kit/src/transport.js +22 -0
  101. package/src/vendor/sync-core/index.js +93 -0
  102. package/src/vendor/sync-core/src/base.js +81 -0
  103. package/src/vendor/sync-core/src/compare.js +338 -0
  104. package/src/vendor/sync-core/src/dialect.js +51 -0
  105. package/src/vendor/sync-core/src/fields.js +36 -0
  106. package/src/vendor/sync-core/src/legacy.js +100 -0
  107. package/src/vendor/sync-core/src/normalize.js +998 -0
  108. package/src/vendor/sync-core/src/push.js +85 -0
  109. package/src/vendor/sync-core/src/retarget.js +278 -0
  110. package/src/vendor/sync-core/src/task-block.js +308 -0
  111. package/src/vendor/sync-core/src/verify.js +67 -0
  112. package/src/vendor/sync-core/src/write.js +191 -0
@@ -0,0 +1,85 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * The MCP transport: resolve the connected GitHub MCP server's tool names at
5
+ * runtime, so a skill knows which tools to call where the engine cannot.
6
+ *
7
+ * Tool names are DISCOVERED, never hardcoded as a single truth. GitHub's
8
+ * official server (`https://api.githubcopilot.com/mcp/`) names them one way
9
+ * today — `issue_read`, `issue_write`, `sub_issue_write`, `add_issue_comment`,
10
+ * `list_issues`, `search_issues`, `projects_get`, `projects_list`,
11
+ * `projects_write`, `get_me` — and a host may prefix them
12
+ * (`mcp__github__issue_read`). Each matcher is an ordered regex list; the first
13
+ * advertised tool that matches wins.
14
+ *
15
+ * WHAT THIS SERVER CANNOT DO, and so what degrades: it has no release WRITE
16
+ * tool (only `get_latest_release` / `get_release_by_tag`), so a release cannot
17
+ * be created or completed over MCP at all — those commands exit 1 and say so.
18
+ *
19
+ * The engine itself never CALLS an MCP tool — the skill does, and hands
20
+ * results to the engine as files (the `--workspace-states` / `--stored` /
21
+ * `--text` idiom every provider uses). This module exists so a skill can ask
22
+ * which tools to call and so `doctor` can report a usable server.
23
+ */
24
+
25
+ const tool = (name) => new RegExp(`(^|__)${name}$`, 'i')
26
+
27
+ const MATCHERS = {
28
+ issueRead: [tool('issue_read'), tool('get_issue')],
29
+ issueWrite: [tool('issue_write'), tool('create_issue'), tool('update_issue')],
30
+ subIssueWrite: [tool('sub_issue_write'), tool('add_sub_issue')],
31
+ issueSearch: [tool('search_issues'), tool('list_issues')],
32
+ commentAdd: [tool('add_issue_comment')],
33
+ projectRead: [tool('projects_get'), tool('projects_list')],
34
+ projectWrite: [tool('projects_write')],
35
+ viewerRead: [tool('get_me')],
36
+ }
37
+
38
+ // The minimum a server must offer for the MCP transport to be worth using at
39
+ // all: reading an issue and writing one. Everything else degrades per command.
40
+ const REQUIRED = ['issueRead', 'issueWrite']
41
+
42
+ // Ops the official server cannot express at all — named so a refusal can say
43
+ // so rather than imply a missing connection.
44
+ const IMPOSSIBLE_OVER_MCP = Object.freeze(['release create', 'release complete'])
45
+
46
+ /**
47
+ * Match the advertised tool list against the matchers.
48
+ * @param {string[]} toolNames what the connected server advertises
49
+ * @returns {{ ok: true, tools: Record<string,string> } | { ok: false, error, tools }}
50
+ */
51
+ function discoverGithub(toolNames) {
52
+ const names = (toolNames || []).filter((n) => typeof n === 'string')
53
+ const tools = {}
54
+ for (const [op, patterns] of Object.entries(MATCHERS)) {
55
+ for (const pattern of patterns) {
56
+ const hit = names.find((n) => pattern.test(n))
57
+ if (hit) {
58
+ tools[op] = hit
59
+ break
60
+ }
61
+ }
62
+ }
63
+ const missing = REQUIRED.filter((op) => !tools[op])
64
+ if (missing.length) {
65
+ return {
66
+ ok: false,
67
+ tools,
68
+ error:
69
+ `the connected MCP server is missing ${missing.join(' and ')} — ` +
70
+ `advertised ${names.length} tool(s), none matched. Is GitHub's MCP server connected?`,
71
+ }
72
+ }
73
+ return { ok: true, tools }
74
+ }
75
+
76
+ // The one-paragraph instructions an MCP-transport command hands the skill.
77
+ const APPLY_OVER_MCP =
78
+ 'Apply the plan with the GitHub MCP tools: issue_write to create/update each\n' +
79
+ ' issue (labels from github.labels), sub_issue_write to link each phase under\n' +
80
+ ' the spec issue, projects_write to add every issue to the board and set its\n' +
81
+ ' Status option, issue_write again to close terminal-bucket issues\n' +
82
+ ' (completed / not_planned). Then stamp the returned #numbers with\n' +
83
+ ' `spec-sync stamp` and record with `spec-sync record`.'
84
+
85
+ module.exports = { discoverGithub, MATCHERS, REQUIRED, IMPOSSIBLE_OVER_MCP, APPLY_OVER_MCP }
@@ -0,0 +1,41 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * Provider-neutral CLI plumbing shared by skitterspec ticketing providers.
5
+ *
6
+ * sync-core is the projection/plan/snapshot ENGINE — pure, no CLI, no network.
7
+ * This package is everything a provider\'s CLI builds around that engine and
8
+ * that a second provider would otherwise duplicate: config merging, the
9
+ * namespaced credentials store, identity resolution, the doctor report
10
+ * framework, released-range scanning, transport resolution, rate-limit-aware
11
+ * HTTP retry, spec resolution/listing, terminal secret input, and a flag
12
+ * parser with the unknown-flag refusal. Nothing in here names a tracker.
13
+ */
14
+
15
+ const config = require('./src/config.js')
16
+ const credentials = require('./src/credentials.js')
17
+ const identity = require('./src/identity.js')
18
+ const doctor = require('./src/doctor.js')
19
+ const released = require('./src/released.js')
20
+ const transport = require('./src/transport.js')
21
+ const http = require('./src/http.js')
22
+ const specs = require('./src/specs.js')
23
+ const prompt = require('./src/prompt.js')
24
+ const args = require('./src/args.js')
25
+ const adapter = require('./src/adapter.js')
26
+ const apply = require('./src/apply.js')
27
+
28
+ module.exports = {
29
+ ...config,
30
+ ...credentials,
31
+ ...identity,
32
+ ...doctor,
33
+ ...released,
34
+ ...transport,
35
+ ...http,
36
+ ...specs,
37
+ ...prompt,
38
+ ...args,
39
+ ...adapter,
40
+ ...apply,
41
+ }
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * The transport op-contract check: a provider's API adapter may ADD ops beyond
5
+ * its MCP adapter, but must never be missing one — consumers feature-detect the
6
+ * extras and assume the base set. Run it in the provider's own tests against
7
+ * both adapters, so the two transports cannot drift apart silently.
8
+ */
9
+
10
+ // The function-valued op names an adapter exposes.
11
+ function adapterOps(adapter) {
12
+ return Object.keys(adapter || {})
13
+ .filter((k) => typeof adapter[k] === 'function')
14
+ .sort()
15
+ }
16
+
17
+ // Which of `required` are absent from `adapter` — [] is the passing answer.
18
+ function missingOps(required, adapter) {
19
+ const have = new Set(adapterOps(adapter))
20
+ return [].concat(required).filter((op) => !have.has(op))
21
+ }
22
+
23
+ module.exports = { adapterOps, missingOps }
@@ -0,0 +1,207 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * The apply ORCHESTRATION — the ordering, stamping and accounting shared by
5
+ * every provider's `spec-sync apply`, with the tracker calls themselves left
6
+ * to provider `ops` (deferred to this shape until two providers existed to
7
+ * validate it: one folds workflow state into its create/update payloads, the
8
+ * other must move status by a separate transition call — so state application
9
+ * lives inside the ops, and the skeleton owns only what is identical).
10
+ *
11
+ * What the skeleton owns, in order:
12
+ *
13
+ * 1. resolve every workflow state BEFORE the first write, so a bad
14
+ * `config.states` value cannot strand the spec mid-apply;
15
+ * 2. the twin-mint guard: an EXACT-title issue held by no spec is a positive
16
+ * signal the issue already exists — refuse to mint a second (a fuzzy
17
+ * match must never count: being wrong here blocks a legitimate push);
18
+ * 3. mint-or-update the spec issue, stamping the identifier the moment the
19
+ * create lands — an interrupt after that point must not mint a twin;
20
+ * 4. sub-issue creates, each STAMPED AS IT LANDS, then updates (keyed by
21
+ * ref, never by id — the read-back matches by ref);
22
+ * 5. read back what the tracker stored and run the same check `verify` runs;
23
+ * 6. record the snapshot from the now-stamped files, so the next push is
24
+ * empty.
25
+ *
26
+ * Orphan accounting rides the whole way: `created` is what the tracker holds,
27
+ * `stamped` what the repo recorded, and the one case a re-run makes WORSE is a
28
+ * create whose stamp could not be written — the identifiers travel out on the
29
+ * error so the caller can name them.
30
+ *
31
+ * @param {object} ops the provider's tracker calls:
32
+ * - `prevalidateStates(buckets)` throw early on a bad state name
33
+ * - `mintIssue({ title, issue })` → `{ id, identifier, url }`
34
+ * - `updateIssue(existing, issue)` apply `plan.issue` to a live issue
35
+ * - `mintSubIssue(parentId, sub)` → `{ id, identifier }`
36
+ * - `updateSubIssue(sub)` apply a sub-issue update
37
+ * - `remoteEditedLine(identifier)` the someone-else-edited warning
38
+ * - `twinAdvice(twin, title, specName)` the refusal message for a twin
39
+ */
40
+
41
+ const path = require('node:path')
42
+
43
+ const {
44
+ readSnapshot,
45
+ readBase,
46
+ projectionOf,
47
+ recordPush,
48
+ writeFrontmatter,
49
+ stampSubIssueId,
50
+ fieldsOf,
51
+ remoteDescriptionEdited,
52
+ remoteSubIssueEdited,
53
+ listPhaseFiles,
54
+ } = require('../../sync-core')
55
+ const { makeSpecHelpers } = require('./specs.js')
56
+
57
+ /**
58
+ * The outer accounting wrapper: run the inner apply, and on ANY throw attach
59
+ * what had been stamped versus created by the time it happened.
60
+ */
61
+ async function applySpecPlan(args) {
62
+ const progress = {}
63
+ try {
64
+ return await applyInner({ ...args, progress })
65
+ } catch (error) {
66
+ const r = progress.result
67
+ if (error && typeof error === 'object' && error.stamped === undefined) {
68
+ error.stamped = r ? (r.issue ? 1 : 0) + Object.keys(r.subIssues || {}).length : 0
69
+ error.created = progress.created || []
70
+ error.orphans = error.created.slice(error.stamped)
71
+ }
72
+ throw error
73
+ }
74
+ }
75
+
76
+ async function applyInner({ dir, config, snapshotDir, plan, adapter, ops, progress, forceNew = false, program = 'spec-sync' }) {
77
+ const fields = fieldsOf(config)
78
+ const helpers = makeSpecHelpers({ program })
79
+ const overviewFile = (config.snapshot && config.snapshot.overviewFile) || '00-overview.md'
80
+ const identifier = helpers.linkedIdentifier(path.join(snapshotDir, overviewFile), config)
81
+ const lines = []
82
+ const result = { issue: null, subIssues: {} }
83
+ // Handed out immediately, so a throw from anywhere below still reports what
84
+ // had been stamped by the time it happened.
85
+ if (progress) progress.result = result
86
+
87
+ // 1. Every state resolved BEFORE the first write.
88
+ const wanted = new Set()
89
+ if (plan.issue && plan.issue.state) wanted.add(plan.issue.state)
90
+ for (const s of (plan.subIssues && plan.subIssues.create) || []) if (s.state) wanted.add(s.state)
91
+ for (const s of (plan.subIssues && plan.subIssues.update) || []) if (s.state) wanted.add(s.state)
92
+ ops.prevalidateStates([...wanted])
93
+
94
+ let parentId = null
95
+ if (!identifier) {
96
+ // 2. REFUSE TO MINT A TWIN. An EXACT match held by no spec is a positive
97
+ // signal that the issue already exists; a near-match must never count —
98
+ // being wrong here blocks a legitimate push, and a renamed spec is
99
+ // answered by adopting the issue, not by fuzz.
100
+ if (!forceNew && adapter.searchIssues) {
101
+ const title = String(plan.title || readSnapshot(snapshotDir, config).title || '').trim()
102
+ if (title) {
103
+ let found = []
104
+ try {
105
+ found = (await ops.searchTwin(title)) || []
106
+ } catch {
107
+ // A search we could not run is not evidence of an orphan. Carry on
108
+ // and mint — the cannot-tell case goes to the harmless branch, which
109
+ // here is the one that does the work the caller asked for.
110
+ found = []
111
+ }
112
+ const claimed = new Set(helpers.listSpecs(dir, config).map((sp) => sp.identifier).filter(Boolean))
113
+ const twin = found.find(
114
+ (i) => i && i.identifier && !claimed.has(i.identifier) && String(i.title || '').trim() === title,
115
+ )
116
+ if (twin) throw new Error(ops.twinAdvice(twin, title, path.basename(snapshotDir)))
117
+ }
118
+ }
119
+ // 3. Mint, then stamp IMMEDIATELY: noted before it is stamped, so a stamp
120
+ // that cannot be written can still name the identifier it strands.
121
+ const created = await ops.mintIssue({ title: readSnapshot(snapshotDir, config).title, issue: plan.issue })
122
+ parentId = created.id
123
+ if (progress) (progress.created = progress.created || []).push(created.identifier)
124
+ writeFrontmatter(snapshotDir, config, {
125
+ [fields.identifier]: created.identifier,
126
+ [fields.url]: created.url,
127
+ })
128
+ result.issue = { id: created.id, identifier: created.identifier, url: created.url }
129
+ lines.push(` issue created: ${created.identifier}`)
130
+ } else {
131
+ const existing = await adapter.readIssue(identifier)
132
+ if (!existing || !existing.id) throw new Error(ops.missingIssueMessage(identifier))
133
+ parentId = existing.id
134
+ result.issue = { id: existing.id, identifier: existing.identifier, url: existing.url }
135
+
136
+ // SOMEONE ELSE EDITED THE DESCRIPTION SINCE THE LAST PUSH, and this write
137
+ // replaces it. A WARNING AND NEVER A REFUSAL: one-way sync means the repo
138
+ // wins; what was wrong was winning silently.
139
+ if (remoteDescriptionEdited(readBase(dir, identifier, config), existing, config) === true) {
140
+ lines.push(ops.remoteEditedLine(identifier))
141
+ }
142
+ if (plan.issue) {
143
+ await ops.updateIssue(existing, plan.issue)
144
+ lines.push(` issue updated: ${identifier}`)
145
+ }
146
+ }
147
+
148
+ // 4. Sub-issue creates — each stamped as it lands, for the same reason.
149
+ for (const sub of (plan.subIssues && plan.subIssues.create) || []) {
150
+ const created = await ops.mintSubIssue(parentId, sub)
151
+ if (progress) (progress.created = progress.created || []).push(created.identifier)
152
+ const file = resolvePhaseFileIn(snapshotDir, sub.ref)
153
+ if (!file) throw new Error(`no phase file for ref ${sub.ref}`)
154
+ stampSubIssueId(snapshotDir, file, created.identifier, config)
155
+ result.subIssues[sub.ref] = created.identifier
156
+ lines.push(` sub-issue created: ${sub.ref} → ${created.identifier}`)
157
+ }
158
+
159
+ // 5. Sub-issue updates — keyed by REF, never by id: the read-back matches
160
+ // sub-issues to phases by ref, and a plan written before updates carried a
161
+ // ref still resolves — by id, off the projection.
162
+ const refById = new Map()
163
+ for (const s of projectionOf(snapshotDir, config).subIssues || []) {
164
+ if (s.id != null) refById.set(String(s.id), s.ref)
165
+ }
166
+ // A phase body edited on the tracker gets the same warning as the spec
167
+ // issue. The read is spent only where the snapshot recorded something to
168
+ // compare against, so an old snapshot costs no extra calls.
169
+ const base = readBase(dir, identifier, config)
170
+ for (const sub of (plan.subIssues && plan.subIssues.update) || []) {
171
+ const streams = base && base.subIssueStreams
172
+ if (sub.goal != null && streams && streams[String(sub.id)]) {
173
+ const before = await adapter.readIssue(sub.id)
174
+ if (remoteSubIssueEdited(base, sub.id, before, config) === true) lines.push(ops.remoteEditedLine(sub.id))
175
+ }
176
+ await ops.updateSubIssue(sub)
177
+ result.subIssues[sub.ref || refById.get(String(sub.id)) || sub.id] = sub.id
178
+ lines.push(` sub-issue updated: ${sub.id}`)
179
+ }
180
+
181
+ // 6. Read back what the tracker stored and run the SAME check verify runs.
182
+ const stored = { issue: undefined, subIssues: {} }
183
+ if (result.issue) {
184
+ const back = await adapter.readIssue(result.issue.id)
185
+ if (back && typeof back.description === 'string') stored.issue = back.description
186
+ }
187
+ for (const [ref, id] of Object.entries(result.subIssues)) {
188
+ const back = await adapter.readIssue(id)
189
+ if (back && typeof back.description === 'string') stored.subIssues[ref] = back.description
190
+ }
191
+ const verify = helpers.verifyLines(snapshotDir, config, stored, result.issue ? result.issue.identifier : identifier)
192
+ lines.push(...verify.map((l) => ` ${l}`))
193
+ const lost = verify.some((l) => l.includes('!!') || l.includes('??'))
194
+
195
+ // 7. Record the snapshot from the now-stamped files, so the next push is empty.
196
+ const file = recordPush({ dir, snapshotDir, identifier: helpers.specIdentifier(snapshotDir, config), config })
197
+ lines.push(` snapshot: ${path.relative(dir, file)}`)
198
+ return { result, lines, lost }
199
+ }
200
+
201
+ // The phase file a sub-issue ref names — the ref is the phase-file basename.
202
+ function resolvePhaseFileIn(snapshotDir, ref) {
203
+ const want = String(ref).replace(/\.md$/, '')
204
+ return listPhaseFiles(snapshotDir).find((f) => f.replace(/\.md$/, '') === want) || null
205
+ }
206
+
207
+ module.exports = { applySpecPlan }
@@ -0,0 +1,68 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * A table-driven flag parser for a provider CLI, carrying the one discipline
5
+ * the first provider learned the hard way: an UNKNOWN `--flag` is a refusal,
6
+ * never a silent positional. (A renamed flag once parsed as a no-op — the
7
+ * repair invocation ran the wrong command, ignored the flag, and exited 0.)
8
+ *
9
+ * The table maps `--flag` → spec:
10
+ * - `{ kind: 'boolean' }` sets `flags[name] = true`
11
+ * - `{ kind: 'value' }` takes the next token verbatim
12
+ * - `{ kind: 'string' }` takes the next token, trimmed ('' kept)
13
+ * - `{ kind: 'number' }` Number(next token)
14
+ * - `{ kind: 'path' }` path.resolve(next token)
15
+ * - `{ kind: 'list' }` repeatable; pushes trimmed tokens
16
+ * - `{ kind: 'custom', take, handle }` `handle(flags, value, context)`;
17
+ * `take: false` consumes no token
18
+ * - any spec may set `name` (defaults to the camelCased flag)
19
+ *
20
+ * A spec name never starts with `--`, so the refusal cannot swallow a real
21
+ * argument. `renamed` maps an old flag to advice lines appended to the refusal,
22
+ * so a rename hands off instead of leaving the caller to guess.
23
+ */
24
+
25
+ const path = require('node:path')
26
+
27
+ const camel = (flag) => flag.replace(/^--/, '').replace(/-([a-z])/g, (_, c) => c.toUpperCase())
28
+
29
+ function parseFlags(args, table, { defaults = {}, renamed = {}, context = {} } = {}) {
30
+ const flags = { ...defaults }
31
+ const positional = []
32
+ const unknown = []
33
+ for (let i = 0; i < args.length; i++) {
34
+ const token = args[i]
35
+ const spec = table[token]
36
+ if (!spec) {
37
+ if (String(token).startsWith('--')) unknown.push(token)
38
+ else positional.push(token)
39
+ continue
40
+ }
41
+ const name = spec.name || camel(token)
42
+ const kind = spec.kind || 'boolean'
43
+ if (kind === 'boolean') flags[name] = true
44
+ else if (kind === 'value') flags[name] = args[++i]
45
+ else if (kind === 'string') flags[name] = String(args[++i] || '').trim()
46
+ else if (kind === 'number') flags[name] = Number(args[++i])
47
+ else if (kind === 'path') flags[name] = path.resolve(args[++i])
48
+ else if (kind === 'list') (flags[name] = flags[name] || []).push(String(args[++i] || '').trim())
49
+ else if (kind === 'custom') {
50
+ const value = spec.take === false ? undefined : args[++i]
51
+ spec.handle(flags, value, context)
52
+ } else throw new Error(`parseFlags: unknown kind "${kind}" for ${token}`)
53
+ }
54
+ return { flags, positional, unknown }
55
+ }
56
+
57
+ // The refusal, phrased once. Returns the lines to print (the caller owns the
58
+ // stream and the exit code).
59
+ function unknownFlagLines(program, unknown, renamed = {}) {
60
+ const lines = [`${program}: unknown flag ${unknown.join(', ')}`]
61
+ for (const flag of unknown) {
62
+ if (renamed[flag]) for (const advice of [].concat(renamed[flag])) lines.push(` ${advice}`)
63
+ }
64
+ lines.push(` run \`${program}\` for the full usage.`)
65
+ return lines
66
+ }
67
+
68
+ module.exports = { parseFlags, unknownFlagLines }