@skitterbyte/skitterspec-github 1.3.0 → 2.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.
@@ -14,7 +14,16 @@
14
14
  const fs = require('node:fs')
15
15
  const path = require('node:path')
16
16
 
17
- const { BUCKETS, findSpecFolder } = require('../../../env/resolve.js')
17
+ const { execFileSync } = require('node:child_process')
18
+
19
+ const {
20
+ BUCKETS,
21
+ findSpecFolder,
22
+ resolveSpec,
23
+ liveWorktreePaths,
24
+ resolvePrimaryCheckout,
25
+ } = require('../../../env/resolve.js')
26
+ const { loadEnvConfig } = require('../../../env/config.js')
18
27
  const { readSnapshot, parseFrontmatter, lintPhases, fieldsOf, projectionOf, compareStored } = require('../../sync-core')
19
28
 
20
29
  function makeSpecHelpers({ program = 'spec-sync' } = {}) {
@@ -45,22 +54,32 @@ function makeSpecHelpers({ program = 'spec-sync' } = {}) {
45
54
  // Read a spec's stamped identifier without throwing: an unlinked spec, an
46
55
  // unreadable file and a spec with no frontmatter all read as `null`.
47
56
  function linkedIdentifier(overviewPath, config) {
48
- const field = fieldsOf(config).identifier
57
+ return linkedFields(overviewPath, config).identifier
58
+ }
59
+
60
+ // The stamped identifier AND url, each `null` when absent. The url is only
61
+ // ever what a push stamped — never built from the identifier, because a url
62
+ // constructed here would be a guess about a tracker's address scheme.
63
+ function linkedFields(overviewPath, config) {
64
+ const fields = fieldsOf(config)
49
65
  let raw
50
66
  try {
51
67
  raw = fs.readFileSync(overviewPath, 'utf-8')
52
68
  } catch {
53
- return null
69
+ return { identifier: null, url: null }
54
70
  }
55
71
  const { data } = parseFrontmatter(raw)
56
- return data[field] ? String(data[field]) : null
72
+ return {
73
+ identifier: data[fields.identifier] ? String(data[fields.identifier]) : null,
74
+ url: fields.url && data[fields.url] ? String(data[fields.url]) : null,
75
+ }
57
76
  }
58
77
 
59
78
  // Every spec under specs/<bucket>/, paired with the tracker issue it is
60
79
  // linked to (`null` when it has never been pushed). Folder specs read
61
80
  // `snapshot.overviewFile`; legacy bare `<name>.md` specs read the file
62
81
  // itself. Sorted for stable output.
63
- function listSpecs(dir, config) {
82
+ function listSpecs(dir, config, options = {}) {
64
83
  const overviewFile = (config.snapshot && config.snapshot.overviewFile) || '00-overview.md'
65
84
  const specs = []
66
85
  for (const bucket of BUCKETS) {
@@ -83,12 +102,75 @@ function makeSpecHelpers({ program = 'spec-sync' } = {}) {
83
102
  } else {
84
103
  continue
85
104
  }
86
- specs.push({ spec: name, bucket, identifier: linkedIdentifier(overviewPath, config) })
105
+ const linked = linkedFields(overviewPath, config)
106
+ const row = { spec: name, bucket, identifier: linked.identifier }
107
+ // Opt-in, so every listing that never asked for a url keeps its shape.
108
+ if (options.urls) row.url = linked.url
109
+ specs.push(row)
87
110
  }
88
111
  }
89
112
  return specs.sort((a, b) => a.spec.localeCompare(b.spec))
90
113
  }
91
114
 
115
+ // `listSpecs`, but each spec's BUCKET read from where that spec actually
116
+ // lives. `/spec-start` moves a spec to `in-progress/` on its own branch, so
117
+ // on the base branch an in-flight spec still reads `backlog` — and a spec
118
+ // authored in its own worktree and not yet landed is not here at all. Each
119
+ // live worktree is read too, and a spec found in ITS OWN worktree (the path
120
+ // `resolveSpec` names for it) wins over every other copy. `source` says
121
+ // which: `here`, `worktree`, or `other-tree` for a spec only some other
122
+ // worktree carries.
123
+ //
124
+ // WHAT WOULD FOOL THIS: a worktree left behind by a declined teardown is
125
+ // still live, so a finished spec can read from a stale tree. And with no
126
+ // `env.config.json` there is no worktree layout to resolve, so this is
127
+ // exactly `listSpecs` — the current tree's answer, as before.
128
+ function listSpecsLive(dir, config) {
129
+ const here = listSpecs(dir, config).map((s) => ({ ...s, source: 'here' }))
130
+ const git = (args) => {
131
+ try {
132
+ return execFileSync('git', ['-C', dir, ...args], { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
133
+ } catch {
134
+ return null
135
+ }
136
+ }
137
+ let primary
138
+ let env
139
+ try {
140
+ primary = resolvePrimaryCheckout(dir, git) || dir
141
+ env = loadEnvConfig(primary)
142
+ } catch {
143
+ return here
144
+ }
145
+ if (!env.present) return here
146
+ // Compared as REAL paths: git reports a worktree resolved through
147
+ // symlinks (macOS /var → /private/var) where `resolveSpec` does not.
148
+ const real = (p) => {
149
+ try {
150
+ return fs.realpathSync(p)
151
+ } catch {
152
+ return path.resolve(p)
153
+ }
154
+ }
155
+ const self = real(dir)
156
+ const byName = new Map(here.map((s) => [s.spec, s]))
157
+ for (const listed of liveWorktreePaths(git)) {
158
+ const tree = real(listed)
159
+ if (tree === self) continue
160
+ for (const s of listSpecs(tree, config)) {
161
+ let own = null
162
+ try {
163
+ own = real(resolveSpec(s.spec, primary, env.config, { searchDirs: [tree] }).worktreePath)
164
+ } catch {
165
+ own = null
166
+ }
167
+ if (own === tree) byName.set(s.spec, { ...s, source: 'worktree' })
168
+ else if (!byName.has(s.spec)) byName.set(s.spec, { ...s, source: 'other-tree' })
169
+ }
170
+ }
171
+ return [...byName.values()].sort((a, b) => a.spec.localeCompare(b.spec))
172
+ }
173
+
92
174
  // Resolve a spec argument to its folder, or null with a message on stdout.
93
175
  // EVERY failure path here must print and EVERY caller must return a non-zero
94
176
  // code: a push skill checks $? before it applies a plan, so a resolve failure
@@ -160,7 +242,7 @@ function makeSpecHelpers({ program = 'spec-sync' } = {}) {
160
242
  return lines
161
243
  }
162
244
 
163
- return { resolveSnapshotDir, specIdentifier, linkedIdentifier, listSpecs, resolveOrExit, warningLines, warnToErr, verifyLines }
245
+ return { resolveSnapshotDir, specIdentifier, linkedIdentifier, listSpecs, listSpecsLive, resolveOrExit, warningLines, warnToErr, verifyLines }
164
246
  }
165
247
 
166
248
  module.exports = { makeSpecHelpers }
@@ -13,7 +13,7 @@
13
13
  * stored matches what was sent — it merges nothing (see `src/verify.js`).
14
14
  */
15
15
 
16
- const { ownsField, normalizeLocal, LIFECYCLE_BUCKETS, lintPhases, readSnapshot, parseFrontmatter, remoteWorkflowState, titleFromText, validateStates, stateSuggestions, stageForState, remoteStage, phaseModeFor, LADDER_ORIGIN_BUCKET } = require('./src/normalize.js')
16
+ const { ownsField, normalizeLocal, LIFECYCLE_BUCKETS, lintPhases, readSnapshot, parseFrontmatter, remoteWorkflowState, remoteStateReading, stateNamesFor, titleFromText, validateStates, stateSuggestions, phaseModeFor } = require('./src/normalize.js')
17
17
  const { planChanges, snapshotOf, isEmptyPlan, hashField, stableStringify, remoteDescriptionEdited, remoteSubIssueEdited } = require('./src/compare.js')
18
18
  const { readBase, writeBase } = require('./src/base.js')
19
19
  const { push, recordPush, projectionOf } = require('./src/push.js')
@@ -41,12 +41,11 @@ module.exports = {
41
41
  push,
42
42
  recordPush,
43
43
  remoteWorkflowState,
44
+ remoteStateReading,
45
+ stateNamesFor,
44
46
  titleFromText,
45
47
  validateStates,
46
48
  stateSuggestions,
47
- stageForState,
48
- remoteStage,
49
- LADDER_ORIGIN_BUCKET,
50
49
  hashField,
51
50
  stableStringify,
52
51
  readBase,
@@ -58,12 +58,12 @@ function specIssueHash(p) {
58
58
 
59
59
  // Per-field hashes of the same two values.
60
60
  //
61
- // They are hashed SEPARATELY because they have different owners once a spec is
62
- // finished. The repo owns the description forever, but the workflow state is
63
- // handed off: a deploy pipeline (see `release.stages`) moves the issue past
64
- // `complete`, and welding the two meant any prose edit re-emitted the state and
65
- // dragged the issue back. Diffing them apart is what lets a push touch the
66
- // description without re-asserting a state someone else now owns.
61
+ // They are hashed SEPARATELY because skitterspec tells the tracker about its
62
+ // own transitions and nothing else. The description is resent whenever it
63
+ // changes; the state only when the spec itself moves. Welding the two meant any
64
+ // prose edit re-emitted the state and dragged back an issue someone had moved
65
+ // on the tracker — which is theirs to do. Diffing them apart is what lets a
66
+ // push touch the description without re-asserting a state.
67
67
  function specIssueFieldHashes(p, config) {
68
68
  const hashes = {
69
69
  description: hashField(p.description ?? null),
@@ -751,39 +751,27 @@ function phasesWithheld(snapshotDir, config) {
751
751
 
752
752
  // --- remote projection ------------------------------------------------------
753
753
 
754
- // The lifecycle bucket a deployment ladder descends FROM. A spec is handed over
755
- // to the deploy pipeline once it is finished, so every rung sits downstream of
756
- // `complete` — a ticket on "On Test" is a completed spec that has been deployed,
757
- // not a spec that moved somewhere else.
758
- const LADDER_ORIGIN_BUCKET = 'complete'
759
-
760
- // The declared ladder rung with this state name, or null.
761
- function stageForState(state, config) {
762
- if (state == null) return null
763
- const want = String(state).toLowerCase().trim()
764
- const stages = (config && config.release && config.release.stages) || []
765
- if (!Array.isArray(stages)) return null
766
- return stages.find((s) => s && typeof s.state === 'string' && s.state.toLowerCase().trim() === want) || null
754
+ // Every tracker state NAME that means this bucket: the one push writes
755
+ // (`states[bucket]`) first, then its read-only aliases (`stateAliases[bucket]`,
756
+ // from the array form of `states`). Readers use the whole list; writers only
757
+ // ever use `states[bucket]`, so an alias can never be written.
758
+ function stateNamesFor(bucket, config) {
759
+ const primary = config && config.states && config.states[bucket]
760
+ const aliases = (config && config.stateAliases && config.stateAliases[bucket]) || []
761
+ return [primary, ...(Array.isArray(aliases) ? aliases : [])].filter((n) => typeof n === 'string' && n)
767
762
  }
768
763
 
769
764
  // Map a remote workflow-state name back to the local lifecycle bucket (the
770
765
  // vocabulary `spec_status` uses) via config.states, so local and remote
771
766
  // workflowState hash equal when semantically equal. Falls back to a lowercased
772
767
  // raw value when the state isn't one of the configured names.
773
- //
774
- // A DECLARED DEPLOYMENT STAGE reads as `complete`, deliberately. Without this
775
- // the fallback lowercases it — "On Test" becomes "on test", which equals no
776
- // bucket — and every deployed spec reports as drifted forever, for the whole
777
- // time it sits in the pipeline. The project told us these states are downstream
778
- // of a finished spec, so they are not a disagreement about where the spec is.
779
768
  function bucketForState(state, config) {
780
769
  if (state == null) return null
781
770
  const states = (config && config.states) || {}
782
771
  const want = String(state).toLowerCase().trim()
783
- for (const [bucket, name] of Object.entries(states)) {
784
- if (typeof name === 'string' && name.toLowerCase().trim() === want) return bucket
772
+ for (const bucket of Object.keys(states)) {
773
+ if (stateNamesFor(bucket, config).some((name) => name.toLowerCase().trim() === want)) return bucket
785
774
  }
786
- if (stageForState(state, config)) return LADDER_ORIGIN_BUCKET
787
775
  return want
788
776
  }
789
777
 
@@ -818,11 +806,20 @@ function remoteWorkflowState(issue, config) {
818
806
  return name != null ? bucketForState(name, config) : null
819
807
  }
820
808
 
821
- // The declared ladder rung a remote issue is currently sitting on, or null.
822
- // Pairs with `remoteWorkflowState`: that says which bucket the issue maps to,
823
- // this says whether it got there by being deployed. Read-only.
824
- function remoteStage(issue, config) {
825
- return stageForState(remoteStateName(issue || {}, config), config)
809
+ // What the drift report needs to know about a remote issue's state: its name,
810
+ // the bucket it maps to, and whether it maps to one at all. Null when the
811
+ // payload carried no state — a cannot-tell, never an answer.
812
+ //
813
+ // AN UNMAPPED STATE IS NOT A DISAGREEMENT. A tracker grows states skitterspec
814
+ // never names ("Blocked", "Next Sprint"), and a ticket a person moved into one
815
+ // is not wrong about the spec — it is somewhere push does not manage, and push
816
+ // will not move it until the spec itself moves. `mapped` is what lets a report
817
+ // say that rather than accuse it.
818
+ function remoteStateReading(issue, config) {
819
+ const name = remoteStateName(issue || {}, config)
820
+ if (name == null) return null
821
+ const bucket = bucketForState(name, config)
822
+ return { name: String(name), bucket, mapped: LIFECYCLE_BUCKETS.includes(bucket) }
826
823
  }
827
824
 
828
825
  // A Linear issue title is plain text, so markdown emphasis is noise there — and
@@ -897,24 +894,16 @@ function titleFromText(text, max = 100) {
897
894
  return stripTitleMarkup(title)
898
895
  }
899
896
 
900
- // Every state NAME the config points at: the lifecycle bucket map, plus the
901
- // project's deployment ladder when it declares one. Both reach Linear the same
902
- // way and fail the same way, so both are checked by the same lookup.
897
+ // Every state NAME the config points at: the lifecycle bucket map.
903
898
  function configuredStateNames(config) {
904
- const names = Object.values((config && config.states) || {}).filter((v) => typeof v === 'string')
905
- const stages = (config && config.release && config.release.stages) || []
906
- for (const stage of Array.isArray(stages) ? stages : []) {
907
- if (stage && typeof stage.state === 'string') names.push(stage.state)
908
- }
909
- return names
899
+ // The WRITTEN names only. An alias is never written, so one the workspace
900
+ // lacks cannot strand a push; `stateSuggestions` still names it in reports.
901
+ return Object.values((config && config.states) || {}).filter((v) => typeof v === 'string')
910
902
  }
911
903
 
912
904
  // Which configured state NAMES are absent from the live workspace. The skill
913
905
  // fetches the workspace's project-status names over MCP and passes them here;
914
906
  // a non-empty result means a typo/rename that Linear would silently no-op.
915
- //
916
- // A project with no `release.stages` contributes nothing here, so the ladder
917
- // cannot make this accuse a config that was fine before it existed.
918
907
  function validateStates(config, workspaceStates) {
919
908
  const configured = configuredStateNames(config)
920
909
  const have = new Set((workspaceStates || []).map((s) => String(s).toLowerCase().trim()))
@@ -953,21 +942,6 @@ function stateSuggestions(config, workspaceStates) {
953
942
  const suggestion = names.find((n) => words.includes(n.toLowerCase().trim())) || null
954
943
  out.push({ bucket, configured, suggestion, label: `states.${bucket}` })
955
944
  }
956
- // Deployment-ladder rungs, reported the same way. No suggestion is offered:
957
- // BUCKET_WORDS describes lifecycle vocabulary, and a stage name is the
958
- // project's own — guessing "On Test" meant "In Progress" would be worse than
959
- // saying nothing.
960
- const stages = (config && config.release && config.release.stages) || []
961
- for (const stage of Array.isArray(stages) ? stages : []) {
962
- if (!stage || typeof stage.state !== 'string') continue
963
- if (have.has(stage.state.toLowerCase().trim())) continue
964
- out.push({
965
- bucket: null,
966
- configured: stage.state,
967
- suggestion: null,
968
- label: `release.stages[${stage.key}]`,
969
- })
970
- }
971
945
  return out
972
946
  }
973
947
 
@@ -991,8 +965,7 @@ module.exports = {
991
965
  canonicalRemoteStatus,
992
966
  bucketForState,
993
967
  LIFECYCLE_BUCKETS,
994
- stageForState,
995
- remoteStage,
996
968
  remoteWorkflowState,
997
- LADDER_ORIGIN_BUCKET,
969
+ remoteStateReading,
970
+ stateNamesFor,
998
971
  }