@erclx/canon 5.61.0 → 5.62.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 (39) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +1 -1
  3. package/claude/.claude-plugin/plugin.json +1 -1
  4. package/claude/skills/canon-cli/SKILL.md +2 -1
  5. package/claude/skills/design-identity/references/card-route.md +3 -3
  6. package/claude/skills/target-update/SKILL.md +9 -1
  7. package/claude/skills/ux-audit/SKILL.md +1 -1
  8. package/docs/agents/install-and-sync.md +45 -11
  9. package/docs/index.md +1 -1
  10. package/docs/target/migrations.md +6 -8
  11. package/docs/target/sync.md +20 -1
  12. package/docs/workflow/ai-workflow.md +1 -1
  13. package/docs/workflow/index.md +3 -3
  14. package/docs/workflow/visual-design-workflow.md +58 -32
  15. package/governance/detect.toml +8 -0
  16. package/governance/ledger/ui/a11y.md +8 -6
  17. package/governance/ledger/ui/keyboard.md +20 -21
  18. package/governance/ledger/ui/pointer.md +24 -0
  19. package/governance/ledger/ui/surface-capture.md +12 -12
  20. package/governance/ledger/ui/ux-completeness.md +11 -23
  21. package/governance/ledger/ui/ux-controls.md +26 -0
  22. package/governance/rules/ui/a11y.md +2 -0
  23. package/governance/rules/ui/keyboard.md +4 -7
  24. package/governance/rules/ui/pointer.md +23 -0
  25. package/governance/rules/ui/surface-capture.md +5 -5
  26. package/governance/rules/ui/ux-completeness.md +2 -10
  27. package/governance/rules/ui/ux-controls.md +25 -0
  28. package/governance/splits.toml +15 -0
  29. package/package.json +1 -1
  30. package/src/commands/sync.ts +3 -1
  31. package/src/commands/tooling.ts +329 -10
  32. package/src/gov/adapter.ts +53 -4
  33. package/src/gov/splits.ts +36 -0
  34. package/src/sync/check.ts +9 -1
  35. package/src/sync/stamp.ts +135 -11
  36. package/src/tooling/disposition.ts +167 -0
  37. package/src/tooling/inject.ts +17 -2
  38. package/src/tooling/scan.ts +86 -7
  39. package/src/tooling/stamp.ts +51 -7
package/src/sync/stamp.ts CHANGED
@@ -12,7 +12,8 @@ import { readInstalled } from '@/version/installed'
12
12
  /**
13
13
  * Domains the stamp can record. Governance attributes file by file through
14
14
  * the sync engine. Tooling runs its own inject and manifest machinery, so it
15
- * records the stack chain it resolved instead and carries no file hashes.
15
+ * records the stack chain it resolved, and hashes only the configs a target
16
+ * matched, which is what lets a later write tell a stale copy from an edit.
16
17
  *
17
18
  * A stamp written before the standards or snippets install channel closed
18
19
  * still carries a `standards` or `snippets` record, and one written before the
@@ -34,6 +35,23 @@ export interface StampSource {
34
35
  /** Target-relative posix path to content hash, for one domain. */
35
36
  export type DomainHashes = Readonly<Record<string, string>>
36
37
 
38
+ export const DISPOSITIONS = ['declined', 'customized'] as const
39
+
40
+ export type Disposition = (typeof DISPOSITIONS)[number]
41
+
42
+ /**
43
+ * What a project decided about one golden file. `base` is the golden hash a
44
+ * customized file was taken from, so a later scan can tell the copy moved.
45
+ */
46
+ export interface DispositionEntry {
47
+ readonly disposition: Disposition
48
+ readonly base?: string
49
+ readonly reason?: string
50
+ }
51
+
52
+ /** Golden posix path to the project's decision about it, for one domain. */
53
+ export type DomainDispositions = Readonly<Record<string, DispositionEntry>>
54
+
37
55
  /**
38
56
  * One domain's record. The anchor sits here rather than at the top level
39
57
  * because domains sync independently, and a shared anchor would let a gov sync
@@ -61,6 +79,12 @@ export interface DomainStamp {
61
79
  * ancestors internally and a reader needs only the leaf to ask it again.
62
80
  */
63
81
  readonly chain?: readonly string[]
82
+ /**
83
+ * Golden files the project declined or customized. Validated apart from the
84
+ * record, so a malformed map reads as empty rather than failing the stamp
85
+ * and costing every other domain its record.
86
+ */
87
+ readonly dispositions?: DomainDispositions
64
88
  }
65
89
 
66
90
  export interface Stamp {
@@ -199,7 +223,7 @@ function withoutRetired(stamp: Stamp): Stamp {
199
223
  const domains: Partial<Record<StampDomain, DomainStamp>> = {}
200
224
  for (const domain of STAMP_DOMAINS) {
201
225
  const record = stamp.domains[domain]
202
- if (record !== undefined) domains[domain] = record
226
+ if (record !== undefined) domains[domain] = withValidDispositions(record)
203
227
  }
204
228
 
205
229
  const covers = Array.isArray(stamp.covers)
@@ -245,6 +269,14 @@ export function stampedChain(
245
269
  return stamp?.domains[domain]?.chain ?? []
246
270
  }
247
271
 
272
+ /** The golden files a domain's project declined or customized. */
273
+ export function stampedDispositions(
274
+ stamp: Stamp | undefined,
275
+ domain: StampDomain,
276
+ ): DomainDispositions {
277
+ return stamp?.domains[domain]?.dispositions ?? {}
278
+ }
279
+
248
280
  /**
249
281
  * Replaces one domain's file hashes and leaves the others, including that
250
282
  * domain's own chain, untouched. Domains install and sync independently but
@@ -261,24 +293,61 @@ export async function writeStamp(
261
293
  }
262
294
 
263
295
  /**
264
- * Records the stack chain an install resolved and leaves that domain's file
265
- * hashes untouched. Tooling calls this with no files ever recorded, since
266
- * `src/tooling/` never runs the sync engine and the chain is its whole record.
267
- * Governance calls it alongside `writeStamp`, since it records both.
296
+ * Records the stack chain an install resolved, and leaves that domain's file
297
+ * hashes untouched unless `files` is passed. Tooling passes the golden hash of
298
+ * each config the target matches, since it never runs the sync engine.
299
+ * Governance calls it alongside `writeStamp`, since that records its files.
268
300
  */
269
301
  export async function writeChainStamp(
270
302
  target: string,
271
303
  source: StampSource,
272
304
  chain: readonly string[],
273
305
  now: Date,
306
+ files?: DomainHashes,
307
+ ): Promise<void> {
308
+ await putDomain(
309
+ target,
310
+ source,
311
+ {
312
+ chain: [...chain],
313
+ ...(files === undefined ? {} : { files: sortKeys(files) }),
314
+ },
315
+ now,
316
+ )
317
+ }
318
+
319
+ /**
320
+ * Replaces one domain's dispositions and leaves the rest of its record as it
321
+ * was, sync anchor included, since declining a file is not a sync. A domain
322
+ * with no record yet gets one through the ordinary write.
323
+ */
324
+ export async function writeDispositions(
325
+ target: string,
326
+ source: StampSource,
327
+ dispositions: DomainDispositions,
328
+ now: Date,
274
329
  ): Promise<void> {
275
- await putDomain(target, source, { chain: [...chain] }, now)
330
+ const previous = readStamp(target)
331
+ const previousRecord = previous?.domains[source.domain]
332
+ if (previousRecord === undefined) {
333
+ await putDomain(target, source, { dispositions }, now)
334
+ return
335
+ }
336
+
337
+ const { dispositions: _replaced, ...rest } = previousRecord
338
+ await persist(
339
+ target,
340
+ previous,
341
+ source.domain,
342
+ { ...rest, ...dispositionsField(dispositions) },
343
+ now,
344
+ )
276
345
  }
277
346
 
278
347
  async function putDomain(
279
348
  target: string,
280
349
  source: StampSource,
281
- payload: Partial<Pick<DomainStamp, 'files' | 'chain'>>,
350
+ payload: Partial<Pick<DomainStamp, 'files' | 'chain' | 'dispositions'>>,
282
351
  now: Date,
283
352
  ): Promise<void> {
284
353
  const previous = readStamp(target)
@@ -292,11 +361,24 @@ async function putDomain(
292
361
  syncedAt: now.toISOString(),
293
362
  files: payload.files ?? previousRecord?.files ?? {},
294
363
  ...resolveChainField(payload.chain, previousRecord?.chain),
364
+ ...dispositionsField(
365
+ payload.dispositions ?? previousRecord?.dispositions ?? {},
366
+ ),
295
367
  }
296
368
 
369
+ await persist(target, previous, source.domain, record, now)
370
+ }
371
+
372
+ async function persist(
373
+ target: string,
374
+ previous: Stamp | undefined,
375
+ domain: StampDomain,
376
+ record: DomainStamp,
377
+ now: Date,
378
+ ): Promise<void> {
297
379
  const domains = sortDomains({
298
380
  ...previous?.domains,
299
- [source.domain]: record,
381
+ [domain]: record,
300
382
  })
301
383
 
302
384
  const stamp: Stamp = {
@@ -434,10 +516,52 @@ function resolveChainField(
434
516
  return resolved === undefined ? {} : { chain: resolved }
435
517
  }
436
518
 
519
+ /** An empty map is left out of the file, as an unrecorded chain is. */
520
+ function dispositionsField(
521
+ dispositions: DomainDispositions,
522
+ ): Pick<DomainStamp, 'dispositions'> {
523
+ return Object.keys(dispositions).length === 0
524
+ ? {}
525
+ : { dispositions: sortKeys(dispositions) }
526
+ }
527
+
528
+ /**
529
+ * Keeps each well-formed entry and drops the rest, so one bad entry costs only
530
+ * itself, and a map that is not an object costs only the map.
531
+ */
532
+ function withValidDispositions(record: DomainStamp): DomainStamp {
533
+ const { dispositions, ...rest } = record
534
+ const raw: unknown = dispositions
535
+ if (raw === undefined) return record
536
+ if (!isRecord(raw)) return rest
537
+
538
+ return {
539
+ ...rest,
540
+ ...dispositionsField(
541
+ Object.fromEntries(
542
+ Object.entries(raw).filter(([, entry]) => isDispositionEntry(entry)),
543
+ ) as DomainDispositions,
544
+ ),
545
+ }
546
+ }
547
+
548
+ function isDispositionEntry(value: unknown): value is DispositionEntry {
549
+ if (!isRecord(value)) return false
550
+ if (!(DISPOSITIONS as readonly unknown[]).includes(value.disposition)) {
551
+ return false
552
+ }
553
+ if (value.base !== undefined && typeof value.base !== 'string') return false
554
+ return value.reason === undefined || typeof value.reason === 'string'
555
+ }
556
+
437
557
  /** Deterministic key order keeps a re-sync diff empty and a merge conflict local. */
438
- function sortKeys(hashes: DomainHashes): DomainHashes {
558
+ function sortKeys<T>(
559
+ entries: Readonly<Record<string, T>>,
560
+ ): Readonly<Record<string, T>> {
439
561
  return Object.fromEntries(
440
- Object.entries(hashes).sort(([left], [right]) => left.localeCompare(right)),
562
+ Object.entries(entries).sort(([left], [right]) =>
563
+ left.localeCompare(right),
564
+ ),
441
565
  )
442
566
  }
443
567
 
@@ -0,0 +1,167 @@
1
+ import { join, posix } from 'node:path'
2
+ import {
3
+ type DomainDispositions,
4
+ hashFile,
5
+ readStamp,
6
+ stampedDispositions,
7
+ } from '@/sync/stamp'
8
+ import { configFiles, type Manifest, seedFiles } from '@/tooling/manifest'
9
+
10
+ /** A golden file as the chain ships it: the stack that wins, and that copy's hash. */
11
+ export interface GoldenPath {
12
+ readonly rel: string
13
+ readonly stack: string
14
+ readonly kind: 'config' | 'seed'
15
+ readonly hash: string
16
+ }
17
+
18
+ export type Transition =
19
+ | { readonly ok: true; readonly dispositions: DomainDispositions }
20
+ | { readonly ok: false; readonly reason: string; readonly message: string }
21
+
22
+ export function readDispositions(target: string): DomainDispositions {
23
+ return stampedDispositions(readStamp(target), 'tooling')
24
+ }
25
+
26
+ /**
27
+ * The posix key a target-relative path is recorded under, or undefined for a
28
+ * path that leaves the target, since no golden file lives there.
29
+ */
30
+ export function normalizeGoldenPath(input: string): string | undefined {
31
+ const rel = posix.normalize(input.split('\\').join('/'))
32
+ if (rel === '.' || rel === '' || posix.isAbsolute(rel)) return undefined
33
+ if (rel === '..' || rel.startsWith('../')) return undefined
34
+ return rel
35
+ }
36
+
37
+ /**
38
+ * Resolves a path the way `scan` and the injectors do: nearest stack first,
39
+ * and a config shadows a seed at the same path. The hash is of the copy
40
+ * `injectConfigs` writes last, which is the nearest stack's.
41
+ */
42
+ export function resolveGolden(
43
+ chain: readonly Manifest[],
44
+ rel: string,
45
+ ): GoldenPath | undefined {
46
+ for (const manifest of chain) {
47
+ if (configFiles(manifest).includes(rel)) {
48
+ return golden(manifest, rel, 'config', manifest.configsDir)
49
+ }
50
+ }
51
+ for (const manifest of chain) {
52
+ if (seedFiles(manifest).includes(rel)) {
53
+ return golden(manifest, rel, 'seed', manifest.seedsDir)
54
+ }
55
+ }
56
+ return undefined
57
+ }
58
+
59
+ function golden(
60
+ manifest: Manifest,
61
+ rel: string,
62
+ kind: GoldenPath['kind'],
63
+ dir: string,
64
+ ): GoldenPath {
65
+ return { rel, stack: manifest.name, kind, hash: hashFile(join(dir, rel)) }
66
+ }
67
+
68
+ export function decline(
69
+ dispositions: DomainDispositions,
70
+ path: GoldenPath | undefined,
71
+ reason?: string,
72
+ ): Transition {
73
+ if (path === undefined) return NOT_GOLDEN
74
+ const { rel } = path
75
+ const held = refuseHeld(dispositions, rel)
76
+ if (held !== undefined) return held
77
+
78
+ return {
79
+ ok: true,
80
+ dispositions: {
81
+ ...dispositions,
82
+ [rel]: {
83
+ disposition: 'declined',
84
+ ...(reason === undefined ? {} : { reason }),
85
+ },
86
+ },
87
+ }
88
+ }
89
+
90
+ /**
91
+ * A seed is copy-once, so the target already owns it and a customization
92
+ * would record nothing a sync does not already respect.
93
+ */
94
+ export function customize(
95
+ dispositions: DomainDispositions,
96
+ path: GoldenPath | undefined,
97
+ present: boolean,
98
+ ): Transition {
99
+ if (path === undefined) return NOT_GOLDEN
100
+ const { rel, kind, hash } = path
101
+ const held = refuseHeld(dispositions, rel)
102
+ if (held !== undefined) return held
103
+
104
+ if (kind === 'seed') {
105
+ return {
106
+ ok: false,
107
+ reason: 'seed-not-customizable',
108
+ message: `${rel} is a seed, which sync never overwrites. Decline it instead to keep sync from restoring it.`,
109
+ }
110
+ }
111
+ if (!present) {
112
+ return {
113
+ ok: false,
114
+ reason: 'missing-file',
115
+ message: `${rel} is not in the target, so there is no copy to keep. Decline it instead.`,
116
+ }
117
+ }
118
+
119
+ return {
120
+ ok: true,
121
+ dispositions: {
122
+ ...dispositions,
123
+ [rel]: { disposition: 'customized', base: hash },
124
+ },
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Lifts by key alone, so a path the chain stopped shipping can still be
130
+ * cleared from the record.
131
+ */
132
+ export function lift(
133
+ dispositions: DomainDispositions,
134
+ rel: string,
135
+ ): Transition {
136
+ if (dispositions[rel] === undefined) {
137
+ return {
138
+ ok: false,
139
+ reason: 'not-held',
140
+ message: `${rel} is neither declined nor customized.`,
141
+ }
142
+ }
143
+
144
+ const { [rel]: _lifted, ...rest } = dispositions
145
+ return { ok: true, dispositions: rest }
146
+ }
147
+
148
+ const NOT_GOLDEN: Transition = {
149
+ ok: false,
150
+ reason: 'not-golden',
151
+ message:
152
+ 'No stack in the recorded chain ships that path. Pass it relative to the target, as `canon tooling diff` lists it.',
153
+ }
154
+
155
+ function refuseHeld(
156
+ dispositions: DomainDispositions,
157
+ rel: string,
158
+ ): Transition | undefined {
159
+ const held = dispositions[rel]
160
+ if (held === undefined) return undefined
161
+
162
+ return {
163
+ ok: false,
164
+ reason: 'already-held',
165
+ message: `${rel} is already ${held.disposition}. Run \`canon tooling lift ${rel}\` first to change it.`,
166
+ }
167
+ }
@@ -2,6 +2,7 @@ import { existsSync } from 'node:fs'
2
2
  import { copyFile, mkdir, readFile, writeFile } from 'node:fs/promises'
3
3
  import { dirname, join, relative } from 'node:path'
4
4
  import { copyPreservingMode } from '@/sync/copy'
5
+ import { readDispositions } from '@/tooling/disposition'
5
6
  import { type InstallResult, installDevDependencies } from '@/tooling/install'
6
7
  import { rewritesOnInstall, stripSeedMarker } from '@/tooling/seed-marker'
7
8
  import { resolveSurfacePath } from '@/roots/surface'
@@ -24,16 +25,27 @@ import {
24
25
  import { isSubfolderTarget, isWithheldInSubfolder } from '@/tooling/subfolder'
25
26
  import { logAdd, logRemove, logStep, logWarn } from '@/cli/ui'
26
27
 
28
+ /**
29
+ * Skips every path the project declined or customized, which is what makes
30
+ * the record hold for `sync --write` and `inject` alike. `hold` names the
31
+ * further paths a caller decided this write must leave, which `sync` derives
32
+ * from the scan and `inject`, writing without one, never passes.
33
+ */
27
34
  export async function injectConfigs(
28
35
  chain: readonly Manifest[],
29
36
  target: string,
37
+ hold: ReadonlySet<string> = new Set(),
30
38
  ): Promise<string[]> {
31
39
  const applied: string[] = []
32
40
  const isSubfolder = isSubfolderTarget(target)
41
+ const held = readDispositions(target)
33
42
 
34
43
  for (const manifest of ancestorsFirst(chain)) {
35
44
  const files = configFiles(manifest).filter(
36
- (rel) => !(isSubfolder && isWithheldInSubfolder(rel)),
45
+ (rel) =>
46
+ held[rel] === undefined &&
47
+ !hold.has(rel) &&
48
+ !(isSubfolder && isWithheldInSubfolder(rel)),
37
49
  )
38
50
  if (files.length === 0) continue
39
51
 
@@ -83,11 +95,14 @@ export async function injectSeeds(
83
95
  const applied: string[] = []
84
96
  const isSubfolder = isSubfolderTarget(target)
85
97
  const shadowed = configPaths(chain)
98
+ const held = readDispositions(target)
86
99
 
87
100
  for (const manifest of ancestorsFirst(chain)) {
88
101
  const files = seedFiles(manifest).filter(
89
102
  (rel) =>
90
- !shadowed.has(rel) && !(isSubfolder && isWithheldInSubfolder(rel)),
103
+ !shadowed.has(rel) &&
104
+ held[rel] === undefined &&
105
+ !(isSubfolder && isWithheldInSubfolder(rel)),
91
106
  )
92
107
  if (files.length === 0) continue
93
108
 
@@ -1,6 +1,13 @@
1
1
  import { existsSync, readFileSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
3
  import { resolveSurfacePath } from '@/roots/surface'
4
+ import {
5
+ type Disposition,
6
+ hashFile,
7
+ readStamp,
8
+ stampedDispositions,
9
+ stampedHashes,
10
+ } from '@/sync/stamp'
4
11
  import { mergeSections } from '@/tooling/gitignore'
5
12
  import {
6
13
  ancestorsFirst,
@@ -19,10 +26,14 @@ import {
19
26
  } from '@/tooling/package'
20
27
  import { isSubfolderTarget, isWithheldInSubfolder } from '@/tooling/subfolder'
21
28
 
29
+ export type DriftCause = 'stale' | 'edited' | 'unattributed'
30
+
31
+ /** `cause` is set on a drifted config alone. */
22
32
  export interface ConfigState {
23
33
  readonly rel: string
24
34
  readonly stack: string
25
35
  readonly state: 'new' | 'drifted' | 'matching'
36
+ readonly cause?: DriftCause
26
37
  }
27
38
 
28
39
  export interface SeedState {
@@ -41,9 +52,24 @@ export interface WithheldState {
41
52
  readonly present: boolean
42
53
  }
43
54
 
55
+ /**
56
+ * A golden file the project declined or customized, which every write skips.
57
+ * `moved` is set on a customized config alone, true once the golden copy no
58
+ * longer hashes to the one the project took.
59
+ */
60
+ export interface DispositionState {
61
+ readonly rel: string
62
+ readonly stack: string
63
+ readonly kind: 'config' | 'seed'
64
+ readonly disposition: Disposition
65
+ readonly present: boolean
66
+ readonly moved?: boolean
67
+ }
68
+
44
69
  export interface ScanResult {
45
70
  readonly configs: readonly ConfigState[]
46
71
  readonly withheld: readonly WithheldState[]
72
+ readonly dispositions: readonly DispositionState[]
47
73
  readonly seeds: readonly SeedState[]
48
74
  readonly scripts: readonly ScriptState[]
49
75
  readonly deps: readonly DepState[]
@@ -53,6 +79,17 @@ export interface ScanResult {
53
79
  readonly totalChanges: number
54
80
  }
55
81
 
82
+ /**
83
+ * Why a drifted config differs, which decides whether a write may replace it.
84
+ * A copy still at the hash the last write recorded is `stale`, since canon
85
+ * moved and the project did not. A copy off it is `edited`, and a copy with no
86
+ * recorded hash is `unattributed`, since nothing says who changed it.
87
+ */
88
+ function driftCause(recorded: string | undefined, dest: string): DriftCause {
89
+ if (recorded === undefined) return 'unattributed'
90
+ return hashFile(dest) === recorded ? 'stale' : 'edited'
91
+ }
92
+
56
93
  function isIdentical(a: string, b: string): boolean {
57
94
  if (!existsSync(b)) return false
58
95
  return readFileSync(a).equals(readFileSync(b))
@@ -72,11 +109,19 @@ function isIdentical(a: string, b: string): boolean {
72
109
  * GitHub reads only at the root moves to `withheld` and counts as no change.
73
110
  * The scope is read here so every caller of `scan` agrees without passing a
74
111
  * flag.
112
+ *
113
+ * A path the project declined or customized moves to `dispositions` ahead of
114
+ * the subfolder test, so it is listed once. The record is read here for the
115
+ * same reason as the scope.
75
116
  */
76
117
  export function scan(chain: readonly Manifest[], target: string): ScanResult {
77
118
  const isSubfolder = isSubfolderTarget(target)
119
+ const stamp = readStamp(target)
120
+ const held = stampedDispositions(stamp, 'tooling')
121
+ const recorded = stampedHashes(stamp, 'tooling')
78
122
  const configs: ConfigState[] = []
79
123
  const withheld: WithheldState[] = []
124
+ const dispositions: DispositionState[] = []
80
125
  const seenConfigs = new Set<string>()
81
126
 
82
127
  for (const manifest of chain) {
@@ -87,18 +132,38 @@ export function scan(chain: readonly Manifest[], target: string): ScanResult {
87
132
  const source = join(manifest.configsDir, rel)
88
133
  const dest = join(target, rel)
89
134
 
135
+ const entry = held[rel]
136
+ if (entry !== undefined) {
137
+ dispositions.push({
138
+ rel,
139
+ stack: manifest.name,
140
+ kind: 'config',
141
+ disposition: entry.disposition,
142
+ present: existsSync(dest),
143
+ ...(entry.disposition === 'customized'
144
+ ? { moved: hashFile(source) !== entry.base }
145
+ : {}),
146
+ })
147
+ continue
148
+ }
149
+
90
150
  if (isSubfolder && isWithheldInSubfolder(rel)) {
91
151
  withheld.push({ rel, stack: manifest.name, present: existsSync(dest) })
92
152
  continue
93
153
  }
94
154
 
95
- const state = !existsSync(dest)
96
- ? 'new'
97
- : isIdentical(source, dest)
98
- ? 'matching'
99
- : 'drifted'
100
-
101
- configs.push({ rel, stack: manifest.name, state })
155
+ if (!existsSync(dest)) {
156
+ configs.push({ rel, stack: manifest.name, state: 'new' })
157
+ } else if (isIdentical(source, dest)) {
158
+ configs.push({ rel, stack: manifest.name, state: 'matching' })
159
+ } else {
160
+ configs.push({
161
+ rel,
162
+ stack: manifest.name,
163
+ state: 'drifted',
164
+ cause: driftCause(recorded[rel], dest),
165
+ })
166
+ }
102
167
  }
103
168
  }
104
169
 
@@ -110,6 +175,18 @@ export function scan(chain: readonly Manifest[], target: string): ScanResult {
110
175
  if (seenSeeds.has(rel)) continue
111
176
  seenSeeds.add(rel)
112
177
 
178
+ const entry = held[rel]
179
+ if (entry !== undefined) {
180
+ dispositions.push({
181
+ rel,
182
+ stack: manifest.name,
183
+ kind: 'seed',
184
+ disposition: entry.disposition,
185
+ present: existsSync(resolveSurfacePath(target, rel)),
186
+ })
187
+ continue
188
+ }
189
+
113
190
  if (isSubfolder && isWithheldInSubfolder(rel)) {
114
191
  withheld.push({
115
192
  rel,
@@ -144,6 +221,7 @@ export function scan(chain: readonly Manifest[], target: string): ScanResult {
144
221
  const totalChanges =
145
222
  (newManifest ? 1 : 0) +
146
223
  configs.filter((entry) => entry.state !== 'matching').length +
224
+ dispositions.filter((entry) => entry.moved === true).length +
147
225
  seeds.filter((entry) => entry.state === 'missing').length +
148
226
  scripts.filter((entry) => entry.state !== 'matching').length +
149
227
  deps.filter((entry) => entry.state === 'missing').length +
@@ -152,6 +230,7 @@ export function scan(chain: readonly Manifest[], target: string): ScanResult {
152
230
  return {
153
231
  configs,
154
232
  withheld,
233
+ dispositions,
155
234
  seeds,
156
235
  scripts,
157
236
  deps,
@@ -1,7 +1,19 @@
1
1
  import { existsSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
- import { readStamp, stampedChain, writeChainStamp } from '@/sync/stamp'
4
- import { isWholeStack, loadManifest, type Manifest } from '@/tooling/manifest'
3
+ import {
4
+ type DomainHashes,
5
+ hashFile,
6
+ readStamp,
7
+ stampedChain,
8
+ stampedHashes,
9
+ writeChainStamp,
10
+ } from '@/sync/stamp'
11
+ import {
12
+ configFiles,
13
+ isWholeStack,
14
+ loadManifest,
15
+ type Manifest,
16
+ } from '@/tooling/manifest'
5
17
  import { readPackage } from '@/tooling/package'
6
18
 
7
19
  /**
@@ -32,15 +44,47 @@ export async function recordToolingChain(
32
44
  ): Promise<string[] | undefined> {
33
45
  if (isWorkspaceRoot(target)) return undefined
34
46
 
35
- const merged = mergeAddOns(
36
- toolkitRoot,
37
- stampedChain(readStamp(target), 'tooling'),
38
- chain,
47
+ const stamp = readStamp(target)
48
+ const merged = mergeAddOns(toolkitRoot, stampedChain(stamp, 'tooling'), chain)
49
+ await writeChainStamp(
50
+ target,
51
+ { domain: 'tooling', toolkitRoot },
52
+ merged,
53
+ now,
54
+ matchedHashes(chain, target, stampedHashes(stamp, 'tooling')),
39
55
  )
40
- await writeChainStamp(target, { domain: 'tooling', toolkitRoot }, merged, now)
41
56
  return merged
42
57
  }
43
58
 
59
+ /**
60
+ * The golden hash of every config the target now matches, over the hashes an
61
+ * earlier run recorded. A config the target holds a different copy of keeps
62
+ * its previous hash, so a held edit still reads as edited on the next run
63
+ * rather than as a stale copy the write may replace.
64
+ */
65
+ function matchedHashes(
66
+ chain: readonly Manifest[],
67
+ target: string,
68
+ previous: DomainHashes,
69
+ ): DomainHashes {
70
+ const hashes: Record<string, string> = { ...previous }
71
+ const seen = new Set<string>()
72
+
73
+ for (const manifest of chain) {
74
+ for (const rel of configFiles(manifest)) {
75
+ if (seen.has(rel)) continue
76
+ seen.add(rel)
77
+
78
+ const dest = join(target, rel)
79
+ if (!existsSync(dest)) continue
80
+ const golden = hashFile(join(manifest.configsDir, rel))
81
+ if (hashFile(dest) === golden) hashes[rel] = golden
82
+ }
83
+ }
84
+
85
+ return hashes
86
+ }
87
+
44
88
  /**
45
89
  * An add-on stands alone beside a core chain, so syncing one must not displace
46
90
  * the chain it layers onto. Base extends nothing and still heads a core chain.