rvlib-shipkit 0.31.0 → 0.32.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 (40) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +3 -0
  3. package/package.json +1 -1
  4. package/src/cli/cmd_Bump.ts +2 -0
  5. package/src/cli/cmd_Ci.ts +3 -1
  6. package/src/cli/cmd_Pb.ts +27 -0
  7. package/src/cli/cmd_Release.ts +1 -1
  8. package/src/cli/cmd_Wiki.ts +2 -2
  9. package/src/cli.ts +49 -39
  10. package/src/collections/providers.ts +7 -2
  11. package/src/contracts/contracts-once.ts +43 -0
  12. package/src/core/canonical-scripts.ts +33 -0
  13. package/src/core/check-ctx.ts +13 -0
  14. package/src/core/check-suite.ts +121 -53
  15. package/src/core/floor.ts +82 -0
  16. package/src/core/motherhome.ts +14 -1
  17. package/src/core/run-check.ts +34 -0
  18. package/src/core/run-ci.ts +63 -2
  19. package/src/core/run-fixers.ts +6 -1
  20. package/src/ledger/toolchain-identity.ts +134 -0
  21. package/src/pb/collection-scaffold.ts +299 -0
  22. package/src/pb/pb-types.ts +233 -0
  23. package/src/pb/run-pb.ts +157 -0
  24. package/src/release/git-facts.ts +3 -2
  25. package/src/release/release-scope.ts +52 -0
  26. package/src/release/releases-share.ts +85 -0
  27. package/src/release/run-bump.ts +31 -8
  28. package/src/release/run-release.ts +8 -1
  29. package/src/release/run-releases.ts +20 -6
  30. package/src/repo-config.ts +22 -65
  31. package/src/rules/SK001-package-json-shape.ts +65 -2
  32. package/src/rules/SK002-contracts-fulfillness.ts +2 -2
  33. package/src/rules/SK004-gitignore-policy.ts +27 -3
  34. package/src/rules/SK012-config-barrel-import.ts +60 -0
  35. package/src/rules/rule-registry.ts +2 -0
  36. package/src/rules/untrack-generated.ts +117 -0
  37. package/src/scaffold/project-scaffold.ts +1 -1
  38. package/src/serve/hub-port.ts +17 -0
  39. package/src/serve/run-serve.ts +2 -5
  40. package/src/wiki/run-wiki.ts +18 -8
@@ -2,11 +2,17 @@
2
2
  // private:true, no license, capability-required script names FAIL, banned
3
3
  // aliases + missing baseline scripts WARN. Blocking, so it runs in `shipkit
4
4
  // ci` like every other rule. Pure local fs, no TTL.
5
+ //
6
+ // It also enforces the package.json half of ONE WAY IN (build/shipkit.md):
7
+ // `test`, `lint` and `typecheck` ROUTE to `shipkit ci --only <id>` and the
8
+ // tool body they used to hold sits at `<id>:raw`. That is what makes a cache
9
+ // worth having: `bun run test` is the reflex, and the reflex has to land in
10
+ // shipkit rather than beside it.
5
11
  import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'
6
12
  import { join } from 'path'
7
- import { canonicalBodies, conditionalBodies, inferBin } from '../core/canonical-scripts.ts'
13
+ import { canonicalBodies, conditionalBodies, inferBin, ROUTED_SCRIPTS, rawName, routedBody, routesToShipkit } from '../core/canonical-scripts.ts'
8
14
  import type { CheckCtx } from '../core/check-ctx.ts'
9
- import type { RepoConfig } from '../repo-config.ts'
15
+ import { resolveFloor, type RepoConfig } from '../repo-config.ts'
10
16
  import type { FixResult, Rule, RuleEnv } from './rule.ts'
11
17
 
12
18
  type Pkg = { name?: string; private?: boolean; license?: string; scripts?: Record<string, string> }
@@ -68,6 +74,23 @@ export async function evaluatePackageJson(t: CheckCtx, root: string, c: RepoConf
68
74
  const missing = required.filter((r) => !has(r.name))
69
75
  for (const m of missing) t.fail(`package.json: missing script '${m.name}' (${m.why})`)
70
76
 
77
+ // ONE WAY IN — a governed script that runs the tool directly is the whole
78
+ // reason a cache gets bypassed: an agent types `bun run test` and shipkit
79
+ // never hears about it. A slot the repo turned off has no shipkit task to
80
+ // route to, so it is not governed.
81
+ const floor = resolveFloor(c, root)
82
+ for (const id of ROUTED_SCRIPTS) {
83
+ if (routedExempt(c).has(id) || c.defaults?.[id] === false) continue
84
+ const body = scripts[id]
85
+ if (body == null) continue
86
+ if (!routesToShipkit(body)) {
87
+ t.fail(`package.json: script '${id}' runs the tool directly — route it to \`${routedBody(id, { bin: inferBin(scripts) })}\` and move the body to '${rawName(id)}', so \`bun run ${id}\` gets the cache instead of bypassing it`)
88
+ continue
89
+ }
90
+ const gap = floor.unfilled.find((u) => u.id === id)
91
+ if (gap != null) t.fail(`package.json: ${gap.reason} — \`bun run ${id}\` would measure nothing`)
92
+ }
93
+
71
94
  // `test` is asked for only once the repo HAS tests: the fixer deliberately
72
95
  // refuses to scaffold it before that (a fixer never manufactures a
73
96
  // failure), so nagging for it would be a WARN no machine may clear
@@ -96,6 +119,29 @@ function requiredScripts(root: string, _c: RepoConfig): Array<{ name: string; wh
96
119
  return required
97
120
  }
98
121
 
122
+ /** rebuild the script map with `key` inserted directly after `after`, so a
123
+ * routed pair reads as two adjacent lines instead of the tool body landing at
124
+ * the bottom of the file, far from the name it belongs to */
125
+ function insertAfter(scripts: Record<string, string>, after: string, key: string, value: string): Record<string, string> {
126
+ const out: Record<string, string> = {}
127
+ for (const [k, v] of Object.entries(scripts)) {
128
+ if (k === key) continue
129
+ out[k] = v
130
+ if (k === after) out[key] = value
131
+ }
132
+ if (out[key] == null) out[key] = value
133
+ return out
134
+ }
135
+
136
+ /** governed names this repo keeps as a direct tool call. Declared per NAME in
137
+ * repo.config, never as a list of banned binaries: the invariant is about
138
+ * which VERBS an agent reaches for, and a binary blocklist goes stale the day
139
+ * a repo changes runner. `defineRepo` rejects a name outside ROUTED_SCRIPTS,
140
+ * so a typo can never silently un-govern a script. */
141
+ function routedExempt(c: RepoConfig): Set<string> {
142
+ return new Set(c.oneWayIn?.exempt ?? [])
143
+ }
144
+
99
145
  const TEST_SKIP = new Set(['node_modules', 'dist', 'tmp', 'references', 'coverage'])
100
146
 
101
147
  /** any `*.test.ts` in the repo, so the test script is only scaffolded once it
@@ -164,6 +210,23 @@ export const packageJsonShape: Rule = {
164
210
  // none, so scaffolding this into a repo without any turns the gate red
165
211
  // on work the fixer itself did. a fixer must never manufacture a failure.
166
212
  if (pkg.scripts['test'] == null && hasTestFiles(env.root)) { pkg.scripts['test'] = 'bun test'; changes.push(`script 'test' added`) }
213
+ // ONE WAY IN, last so it also routes whatever this pass just added:
214
+ // the tool body slides to `<id>:raw` and `<id>` becomes the shipkit
215
+ // call. Both keys keep their place in the file, so the diff a human
216
+ // reads is the two lines that changed and not a reordered block
217
+ for (const id of ROUTED_SCRIPTS) {
218
+ if (routedExempt(env.config).has(id) || env.config.defaults?.[id] === false) continue
219
+ const body = pkg.scripts[id]
220
+ if (body == null || routesToShipkit(body)) continue
221
+ const raw = rawName(id)
222
+ const held = pkg.scripts[raw]
223
+ // a `<id>:raw` that already says something else is a repo's own
224
+ // answer, and overwriting it would delete a body nobody can recover
225
+ if (held != null && held !== body) { notes.push(`'${id}' left as it is — '${raw}' already holds a different body`); continue }
226
+ pkg.scripts = insertAfter(pkg.scripts, id, raw, body)
227
+ pkg.scripts[id] = routedBody(id, { bin })
228
+ changes.push(`script '${id}' routed through shipkit ('${raw}' holds the tool)`)
229
+ }
167
230
  if (changes.length > 0) {
168
231
  const indent = raw.match(/^(\s+)"/m)?.[1] ?? ' '
169
232
  writeFileSync(pkgPath, JSON.stringify(pkg, null, indent) + '\n')
@@ -6,7 +6,7 @@
6
6
  // `shipkit contracts`, the ci `contracts` task) tags with this id via
7
7
  // contracts/apply-contracts.ts.
8
8
  import { activeContracts, applyContractsResult, contractsConfigFrom } from '../contracts/apply-contracts.ts'
9
- import { evaluateContracts } from '../contracts/contracts-engine.ts'
9
+ import { evaluateContractsOnce } from '../contracts/contracts-once.ts'
10
10
  import type { Rule } from './rule.ts'
11
11
 
12
12
  export const contractsFulfillness: Rule = {
@@ -15,6 +15,6 @@ export const contractsFulfillness: Rule = {
15
15
  title: 'contracts',
16
16
  owner: 'meta/contracts.md',
17
17
  lint: async (t, env) => {
18
- applyContractsResult(t, await evaluateContracts(activeContracts(env.config), contractsConfigFrom(env.config, env.root)), contractsFulfillness)
18
+ applyContractsResult(t, await evaluateContractsOnce(activeContracts(env.config), contractsConfigFrom(env.config, env.root)), contractsFulfillness)
19
19
  },
20
20
  }
@@ -11,6 +11,7 @@
11
11
  import { existsSync, readFileSync, writeFileSync } from 'fs'
12
12
  import { join } from 'path'
13
13
  import type { RepoConfig } from '../repo-config.ts'
14
+ import { GENERATED_IGNORE_LINES, trackedGeneratedPaths, untrackGenerated } from './untrack-generated.ts'
14
15
  import type { FixResult, Rule, RuleEnv } from './rule.ts'
15
16
 
16
17
  const BEGIN = '# >>> shipkit gitignore policy (SK004) — managed block, edit outside only'
@@ -37,6 +38,10 @@ const POLICY_LINES = [
37
38
  // so every consumer repo needs both ignored before its first commit
38
39
  '.comfy-ts/',
39
40
  '*.loras.local.txt',
41
+ // shipkit's own per-run output. Ignoring is only half of it: the fixer
42
+ // also removes these from the index, because .gitignore does nothing to a
43
+ // file already tracked (untrack-generated.ts owns the pair)
44
+ ...GENERATED_IGNORE_LINES,
40
45
  ]
41
46
 
42
47
  export function expectedBlock(config: RepoConfig): string {
@@ -96,6 +101,13 @@ export const gitignorePolicy: Rule = {
96
101
  title: '.gitignore',
97
102
  owner: 'meta/contracts.md',
98
103
  lint: (t, env) => {
104
+ // a generated path still in the index is the half-applied state the
105
+ // managed block cannot fix on its own
106
+ const tracked = trackedGeneratedPaths(env.root)
107
+ if (tracked.length > 0) {
108
+ t.fail(`${tracked.join(', ')} generated on every run but TRACKED — gitignore does not apply to a tracked file, so it dirties every tree`)
109
+ t.lines.push({ kind: 'fix-fail', text: 'shipkit ci (or check --fix SK004) untracks it in one commit, the file stays on disk' })
110
+ }
99
111
  const s = blockState(env.root, env.config)
100
112
  switch (s) {
101
113
  case 'missing-file': return t.fail('.gitignore missing — the contract git policy is unenforced')
@@ -107,8 +119,20 @@ export const gitignorePolicy: Rule = {
107
119
  },
108
120
  fix: (env: RuleEnv): FixResult => {
109
121
  const before = blockState(env.root, env.config)
110
- if (before === 'current') return { changed: false, summary: 'policy block already current' }
111
- writeBlock(env.root, env.config)
112
- return { changed: true, summary: before === 'missing-file' ? '.gitignore created with the policy block' : before === 'missing-block' ? 'policy block appended (repo lines untouched)' : before === 'corrupted' ? 'policy block rebuilt (stray markers dropped, repo lines kept visible)' : 'policy block rewritten in place (repo lines untouched)' }
122
+ const blockSummary = before === 'current'
123
+ ? null
124
+ : before === 'missing-file' ? '.gitignore created with the policy block'
125
+ : before === 'missing-block' ? 'policy block appended (repo lines untouched)'
126
+ : before === 'corrupted' ? 'policy block rebuilt (stray markers dropped, repo lines kept visible)'
127
+ : 'policy block rewritten in place (repo lines untouched)'
128
+ if (blockSummary != null) writeBlock(env.root, env.config)
129
+ // the block must be written FIRST: untracking a path the ignore list
130
+ // does not yet carry leaves it staged for re-add on the next `git add`
131
+ const un = untrackGenerated(env.root, { enabled: env.config.untrackGenerated })
132
+ const parts = [blockSummary]
133
+ if (un.did === 'untracked') parts.push(`untracked ${un.paths.join(', ')} (generated, now gitignored, committed as ${un.commit})`)
134
+ if (un.did === 'refused') parts.push(`left ${trackedGeneratedPaths(env.root).join(', ')} tracked: ${un.why}`)
135
+ const summary = parts.filter((x): x is string => x != null).join(' · ')
136
+ return { changed: blockSummary != null || un.did === 'untracked', summary: summary === '' ? 'policy block already current' : summary }
113
137
  },
114
138
  }
@@ -0,0 +1,60 @@
1
+ // SK012 — `repo.config.ts` must import from the NARROW module, never from the
2
+ // package barrel.
3
+ //
4
+ // THE COST, measured on this tree: `import { defineRepo } from 'rvlib-shipkit'`
5
+ // pulls the whole toolkit, 405ms of module load. The same symbol from
6
+ // `rvlib-shipkit/src/repo-config.ts` is 7ms. Every shipkit invocation in every
7
+ // repo loads this file first, the ~1s pre-commit hook included, so the barrel
8
+ // is a fixed tax on every gate run, every hook, every hub boot.
9
+ //
10
+ // A barrel is a fine PUBLIC surface and a terrible one to import from a config
11
+ // file that exists to be read before anything else happens. This rule is
12
+ // narrow on purpose: it is about repo.config.ts, not about barrels in general.
13
+ import { existsSync, readFileSync, writeFileSync } from 'fs'
14
+ import { join } from 'path'
15
+ import type { FixResult, Rule, RuleEnv } from './rule.ts'
16
+
17
+ const CONFIG = 'repo.config.ts'
18
+ const BARREL = /(^|\n)(\s*import\s+(?:type\s+)?\{[^}]*\}\s+from\s+)['"]rvlib-shipkit['"]/
19
+ const NARROW = 'rvlib-shipkit/src/repo-config.ts'
20
+
21
+ /** the symbols a config file can legitimately want, all of which the narrow
22
+ * module already exports. A config reaching for anything else is asking the
23
+ * barrel for a reason, and the rule says so rather than rewriting it. */
24
+ const CONFIG_SYMBOLS = new Set(['defineRepo', 'RepoConfig', 'TaskDef', 'ServeConfig', 'FloorSource', 'FloorResolution', 'loadRepoConfig', 'resolveFloor', 'allTasks'])
25
+
26
+ const importedSymbols = (line: string): string[] =>
27
+ (/\{([^}]*)\}/.exec(line)?.[1] ?? '').split(',').map((s) => s.replace(/^\s*type\s+/, '').trim()).filter((s) => s !== '')
28
+
29
+ export const configBarrelImport: Rule = {
30
+ id: 'SK012',
31
+ blocking: true,
32
+ title: 'config import path',
33
+ owner: 'build/shipkit.md',
34
+ lint: (t, env) => {
35
+ const p = join(env.root, CONFIG)
36
+ if (!existsSync(p)) return void t.ok(`${CONFIG} absent — nothing to narrow`)
37
+ const src = readFileSync(p, 'utf8')
38
+ if (!BARREL.test(src)) return void t.ok(`${CONFIG} imports the narrow config module`)
39
+ const line = src.split('\n').find((l) => /from\s+['"]rvlib-shipkit['"]/.test(l)) ?? ''
40
+ const symbols = importedSymbols(line)
41
+ const strays = symbols.filter((s) => !CONFIG_SYMBOLS.has(s))
42
+ if (strays.length > 0) {
43
+ t.fail(`${CONFIG} imports ${strays.join(', ')} from the package barrel, which costs ~400ms of module load on every shipkit run`)
44
+ return void t.lines.push({ kind: 'fix-fail', text: `move ${strays.join(', ')} to their own module, then import from ${NARROW}` })
45
+ }
46
+ t.fail(`${CONFIG} imports from the package barrel (~400ms of module load on every shipkit run, the pre-commit hook included)`)
47
+ },
48
+ fix: (env: RuleEnv): FixResult => {
49
+ const p = join(env.root, CONFIG)
50
+ if (!existsSync(p)) return { changed: false, summary: `${CONFIG} absent` }
51
+ const src = readFileSync(p, 'utf8')
52
+ if (!BARREL.test(src)) return { changed: false, summary: 'already narrow' }
53
+ const line = src.split('\n').find((l) => /from\s+['"]rvlib-shipkit['"]/.test(l)) ?? ''
54
+ // only rewrite what the narrow module actually exports: silently
55
+ // repointing an import it cannot serve turns a slow gate into a broken one
56
+ if (importedSymbols(line).some((s) => !CONFIG_SYMBOLS.has(s))) return { changed: false, summary: 'imports symbols the narrow module does not export — move them by hand' }
57
+ writeFileSync(p, src.replace(BARREL, `$1$2'${NARROW}'`))
58
+ return { changed: true, summary: `${CONFIG} now imports ${NARROW} (~400ms saved per run)` }
59
+ },
60
+ }
@@ -14,6 +14,7 @@ import { reviewerAgent } from './SK008-reviewer-agent.ts'
14
14
  import { commitAiTells } from './SK009-commit-ai-tells.ts'
15
15
  import { undeclaredReleaseUnit } from './SK010-undeclared-release-unit.ts'
16
16
  import { siblingUnitRanges } from './SK011-sibling-unit-ranges.ts'
17
+ import { configBarrelImport } from './SK012-config-barrel-import.ts'
17
18
 
18
19
  export const RULES: readonly Rule[] = [
19
20
  packageJsonShape,
@@ -27,6 +28,7 @@ export const RULES: readonly Rule[] = [
27
28
  commitAiTells,
28
29
  undeclaredReleaseUnit,
29
30
  siblingUnitRanges,
31
+ configBarrelImport,
30
32
  ]
31
33
 
32
34
  // THE id shape, one place: 2-4 uppercase letters + exactly 3 digits. `SK` is
@@ -0,0 +1,117 @@
1
+ // paths shipkit GENERATES on every run, and the one operation that makes a
2
+ // declaration like that actually hold.
3
+ //
4
+ // THE CONTRADICTION THIS RESOLVES: `.gitignore` does not apply to a file that
5
+ // is already tracked. So a path shipkit ignores but leaves in the index keeps
6
+ // dirtying every tree forever, and rides into unrelated commits as a mystery
7
+ // diff. Ignoring and untracking are therefore ONE operation, never two, and
8
+ // any path declared machine-generated gets both.
9
+ //
10
+ // STATUS.md is the first customer and the reason: 141 of this repo's 625
11
+ // commits carried it, 125 of them as an unrelated rider inside someone else's
12
+ // diff, in a repo whose commits ARE the session-resume mechanism. It is
13
+ // nondeterministic by construction (minute-resolution header, task lines in
14
+ // DAG completion order, per-worktree cache stamps that move backwards), and
15
+ // nothing on this disk ever reads a past version: the hub reads it off disk,
16
+ // the TUI renders live, the tests assert the writer.
17
+ //
18
+ // WHY THIS ONE FIXER COMMITS, when shipkit otherwise never does: `git rm
19
+ // --cached` leaves a STAGED DELETION in the index, which the next agent's
20
+ // unrelated commit sweeps up as an unexplained deletion. That is strictly
21
+ // worse than the disease. The commit is a one-time migration per path, it
22
+ // goes through the hooks like any other, and it refuses in every state where
23
+ // it could surprise someone.
24
+ import { existsSync } from 'fs'
25
+ import { join } from 'path'
26
+
27
+ /** rendered into the SK004 managed block. A glob is fine HERE: ignoring is
28
+ * not destructive. */
29
+ export const GENERATED_IGNORE_LINES = [
30
+ '# shipkit writes these on every run — ignored AND untracked (untrack-generated.ts)',
31
+ 'STATUS.md',
32
+ 'STATUS.md.lock',
33
+ '.STATUS.md.tmp-*',
34
+ ]
35
+
36
+ /** the LITERAL subset an autofix may remove from the index. Never a glob and
37
+ * never derived from a pattern: `git rm --cached` driven by a wildcard is how
38
+ * a fixer eats source it was never pointed at. A future entry is added HERE,
39
+ * by hand, spelled out. */
40
+ export const GENERATED_TRACKED_PATHS = ['STATUS.md', 'STATUS.md.lock'] as const
41
+
42
+ export type UntrackOutcome =
43
+ | { did: 'nothing' }
44
+ | { did: 'refused'; why: string }
45
+ | { did: 'untracked'; paths: string[]; commit: string }
46
+
47
+ const git = (root: string, args: string[]): { code: number; out: string } => {
48
+ const p = Bun.spawnSync(['git', ...args], { cwd: root })
49
+ return { code: p.exitCode, out: `${p.stdout.toString()}${p.stderr.toString()}`.trim() }
50
+ }
51
+
52
+ /** every declared path this repo still carries in its index. ONE git spawn,
53
+ * not one per path: this runs inside the SK004 lint, which runs on every
54
+ * `shipkit check` in every repo, and a process start is ~10ms of it. */
55
+ export function trackedGeneratedPaths(root: string): string[] {
56
+ if (!existsSync(join(root, '.git'))) return []
57
+ const r = git(root, ['ls-files', '-z', '--', ...GENERATED_TRACKED_PATHS])
58
+ if (r.code !== 0) return []
59
+ const listed = new Set(r.out.split('\0').filter((l) => l !== ''))
60
+ return GENERATED_TRACKED_PATHS.filter((p) => listed.has(p))
61
+ }
62
+
63
+ /** a state where an automated commit would surprise someone, or land on the
64
+ * wrong ref. Named rather than boolean so the refusal says which one. */
65
+ function unsafeState(root: string): string | null {
66
+ const gitDir = git(root, ['rev-parse', '--git-dir'])
67
+ if (gitDir.code !== 0) return 'not a git work tree'
68
+ const d = join(root, gitDir.out)
69
+ if (existsSync(join(d, 'MERGE_HEAD'))) return 'a merge is in progress'
70
+ if (existsSync(join(d, 'rebase-merge')) || existsSync(join(d, 'rebase-apply'))) return 'a rebase is in progress'
71
+ if (git(root, ['symbolic-ref', '-q', 'HEAD']).code !== 0) return 'HEAD is detached'
72
+ if (git(root, ['rev-parse', '--verify', '-q', 'HEAD']).code !== 0) return 'the repo has no commit yet'
73
+ // the commit below stages nothing but our own removal, so ANY other staged
74
+ // change would ride along in it. A one-time migration can wait for a run
75
+ // where the index is the fixer's alone
76
+ const staged = git(root, ['diff', '--cached', '--name-only'])
77
+ if (staged.code === 0 && staged.out !== '') return `something else is already staged (${staged.out.split('\n')[0]})`
78
+ return null
79
+ }
80
+
81
+ const message = (paths: string[]): string =>
82
+ [
83
+ `chore(machine): untrack ${paths.join(', ')}`,
84
+ '',
85
+ `shipkit rewrites ${paths.length === 1 ? 'this path' : 'these paths'} on every run, so ${paths.length === 1 ? 'it was' : 'they were'} never source.`,
86
+ 'Tracked, it dirtied every tree and rode into unrelated commits as a',
87
+ 'mystery diff. Gitignore alone cannot fix that: it does not apply to a',
88
+ 'file already in the index, so the two halves land together.',
89
+ '',
90
+ 'The file stays on disk. Nothing reads a past version of it.',
91
+ ].join('\n')
92
+
93
+ /**
94
+ * ignore AND untrack, in one operation. Loud when it acts, loud when it
95
+ * refuses, silent when there is nothing to do.
96
+ *
97
+ * `frozen` is the release-shaped run: report only, touch nothing.
98
+ */
99
+ export function untrackGenerated(root: string, o: { frozen?: boolean; enabled?: boolean } = {}): UntrackOutcome {
100
+ if (o.enabled === false) return { did: 'nothing' }
101
+ if (o.frozen === true || process.env.SHIPKIT_FROZEN === '1') return { did: 'nothing' }
102
+ const paths = trackedGeneratedPaths(root)
103
+ if (paths.length === 0) return { did: 'nothing' }
104
+ const unsafe = unsafeState(root)
105
+ if (unsafe != null) return { did: 'refused', why: unsafe }
106
+
107
+ const rm = git(root, ['rm', '--cached', '--quiet', '--', ...paths])
108
+ if (rm.code !== 0) return { did: 'refused', why: `git rm --cached failed: ${rm.out.slice(0, 120)}` }
109
+ // through the hooks, never --no-verify. A rejected commit must not leave
110
+ // the staged deletion behind: that IS the failure mode this exists to stop
111
+ const commit = git(root, ['commit', '-m', message(paths)])
112
+ if (commit.code !== 0) {
113
+ git(root, ['reset', '--quiet', '--', ...paths])
114
+ return { did: 'refused', why: `the commit was rejected, index restored: ${commit.out.slice(0, 160)}` }
115
+ }
116
+ return { did: 'untracked', paths, commit: git(root, ['rev-parse', '--short', 'HEAD']).out }
117
+ }
@@ -12,7 +12,7 @@
12
12
  // none of them may name a machine-local tool or a private path.
13
13
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
14
14
  import { dirname, join } from 'path'
15
- import { defaultHubPort } from '../serve/run-serve.ts'
15
+ import { defaultHubPort } from '../serve/hub-port.ts'
16
16
 
17
17
  const FILES = join(import.meta.dir, 'files')
18
18
 
@@ -0,0 +1,17 @@
1
+ // the hub's default port, alone in its own module ON PURPOSE.
2
+ //
3
+ // It is four lines of arithmetic that several places need BEFORE any server
4
+ // exists: the repo scaffold writes it into .vscode/tasks.json, `init` prints
5
+ // it, the config resolver defaults to it. Importing it from run-serve.ts drags
6
+ // the whole hub (react, the markdown renderer, the sanitiser, every panel)
7
+ // into a caller that only wanted a number, which measured 393ms of module
8
+ // load in the scaffold alone.
9
+ //
10
+ // A port derived from the repo NAME rather than assigned means two repos open
11
+ // side by side almost never collide, and the same repo always reopens on the
12
+ // same port, which is what makes a folder-open task able to advertise a URL.
13
+ export function defaultHubPort(name: string): number {
14
+ let h = 0
15
+ for (const c of name) h = (h * 31 + c.charCodeAt(0)) >>> 0
16
+ return 4800 + (h % 180)
17
+ }
@@ -20,14 +20,11 @@ import { loadRepoConfig, type RepoConfig } from '../repo-config.ts'
20
20
  import type { GraphMount } from '../wiki/graph-mount.ts'
21
21
  import { scanMdGraph } from '../wiki/scan-md.ts'
22
22
  import { serveHub, type HubDeps } from './serve-hub.ts'
23
+ import { defaultHubPort } from './hub-port.ts'
24
+ export { defaultHubPort }
23
25
 
24
26
  export type ServeIo = { print: (l: string) => void; printErr: (l: string) => void }
25
27
 
26
- export function defaultHubPort(name: string): number {
27
- let h = 0
28
- for (const c of name) h = (h * 31 + c.charCodeAt(0)) >>> 0
29
- return 4800 + (h % 180)
30
- }
31
28
 
32
29
  // the BASE-CONTEXT wiki: the shared wiki this repo's CLAUDE.md include chain
33
30
  // points at (`@~/dev/<other-repo>/wiki/index.md`). It lives in ANOTHER repo and
@@ -6,7 +6,15 @@ import { lintMd } from './lint-md.ts'
6
6
  import { scanMdGraph } from './scan-md.ts'
7
7
  import { serveMdGraph } from './serve-graph.ts'
8
8
 
9
- export type WikiScanOpts = { root: string; index?: string; skip?: string }
9
+ export type WikiScanOpts = {
10
+ root: string
11
+ index?: string
12
+ skip?: string
13
+ /** where the verdict goes. The command passes its own stream, so the
14
+ * output is capturable: a console.log inside a command bypasses the
15
+ * stream clipanion hands it, and nothing downstream can read it */
16
+ print?: (line: string) => void
17
+ }
10
18
 
11
19
  const scanConfig = (o: WikiScanOpts): { root: string; index?: string; skipDirs?: string[] } => ({
12
20
  root: resolve(process.cwd(), o.root),
@@ -15,18 +23,19 @@ const scanConfig = (o: WikiScanOpts): { root: string; index?: string; skipDirs?:
15
23
  })
16
24
 
17
25
  export async function runWikiServe(o: WikiScanOpts & { port: number; title?: string }): Promise<number> {
26
+ const say = o.print ?? console.log
18
27
  const cfg = scanConfig(o)
19
28
  const rel = relative(process.cwd(), cfg.root)
20
29
  const title = o.title ?? (rel === '' ? basename(cfg.root) : `${basename(process.cwd())} ${rel}`)
21
30
  try {
22
31
  serveMdGraph({ port: o.port, title, load: () => scanMdGraph(cfg) })
23
- console.log(`wiki graph → http://localhost:${o.port} (open in browser, or VSCode "Simple Browser: Show")`)
32
+ say(`wiki graph → http://localhost:${o.port} (open in browser, or VSCode "Simple Browser: Show")`)
24
33
  // keep the server alive — the command must not resolve
25
34
  await new Promise(() => {})
26
35
  return 0
27
36
  } catch (e) {
28
37
  if (e instanceof Error && e.message.includes('in use')) {
29
- console.log(`wiki graph already served → http://localhost:${o.port} (another window owns it)`)
38
+ say(`wiki graph already served → http://localhost:${o.port} (another window owns it)`)
30
39
  return 0
31
40
  }
32
41
  throw e
@@ -34,17 +43,18 @@ export async function runWikiServe(o: WikiScanOpts & { port: number; title?: str
34
43
  }
35
44
 
36
45
  export function runWikiLint(o: WikiScanOpts): number {
46
+ const say = o.print ?? console.log
37
47
  const cfg = scanConfig(o)
38
48
  const r = lintMd(cfg)
39
49
  if (r.index == null) {
40
- console.log(o.index != null
50
+ say(o.index != null
41
51
  ? `FAIL entrypoint '${o.index}' not found in ${cfg.root}`
42
52
  : `FAIL no entrypoint: neither index.md nor README.md in ${cfg.root} — pass --index <entry.md>`)
43
53
  return 1
44
54
  }
45
- for (const f of r.orphans) console.log(`FAIL orphan: ${f} — unreachable from ${r.index}; link it or delete it`)
46
- for (const d of r.dangling) console.log(`FAIL dangling: ${d.from} → ${d.token} — target not found`)
47
- if (!r.ok) { console.log(`FAIL mdwiki lint: ${r.orphans.length} orphan(s), ${r.dangling.length} dangling ref(s) in ${cfg.root}`); return 1 }
48
- console.log(`ok mdwiki lint: ${r.files} files, all reachable from ${r.index}, no dangling refs`)
55
+ for (const f of r.orphans) say(`FAIL orphan: ${f} — unreachable from ${r.index}; link it or delete it`)
56
+ for (const d of r.dangling) say(`FAIL dangling: ${d.from} → ${d.token} — target not found`)
57
+ if (!r.ok) { say(`FAIL mdwiki lint: ${r.orphans.length} orphan(s), ${r.dangling.length} dangling ref(s) in ${cfg.root}`); return 1 }
58
+ say(`ok mdwiki lint: ${r.files} files, all reachable from ${r.index}, no dangling refs`)
49
59
  return 0
50
60
  }