@skitterbyte/skitterspec-jira 2.4.0 → 2.5.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.
@@ -1,5 +1,25 @@
1
1
  'use strict'
2
2
 
3
+ const { execFileSync } = require('node:child_process')
4
+ const { primaryAnchor, fromRepoPath } = require('../../../env/project.js')
5
+
6
+ /**
7
+ * The project's prefix within its repo, read from `dir` — `''` at the root, and
8
+ * `''` whenever git cannot answer (today's behaviour). What `ticketsInRange`
9
+ * needs to read a commit's root-relative paths against the project's
10
+ * `ignorePaths`.
11
+ */
12
+ function projectPrefixOf(dir) {
13
+ const git = (args) => {
14
+ try {
15
+ return execFileSync('git', ['-C', dir, ...args], { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
16
+ } catch {
17
+ return null
18
+ }
19
+ }
20
+ return primaryAnchor(dir, git).prefix
21
+ }
22
+
3
23
  /**
4
24
  * Which tickets a release contains.
5
25
  *
@@ -99,7 +119,14 @@ function asGlobal(pattern) {
99
119
  * @param {string[]|null|undefined} paths repo-relative paths the commit changed
100
120
  * @param {string[]} ignorePaths repo-relative prefixes that are bookkeeping
101
121
  */
102
- function onlyIgnoredPaths(paths, ignorePaths) {
122
+ //
123
+ // `paths` are what `git log --name-only` prints — from the REPO ROOT — while
124
+ // `ignorePaths` are the project's, like every path in its config. For a project
125
+ // in a subfolder of its repo (`prefix`, spec feat-subfolder-project) each path
126
+ // is made project-relative first, and a path OUTSIDE the project is never
127
+ // bookkeeping: a commit that touched `Sql/` did real work. Prefix '' compares
128
+ // exactly as before.
129
+ function onlyIgnoredPaths(paths, ignorePaths, prefix = '') {
103
130
  if (!Array.isArray(paths) || !paths.length) return false
104
131
  const prefixes = (Array.isArray(ignorePaths) ? ignorePaths : [])
105
132
  .filter((p) => typeof p === 'string' && p.trim())
@@ -113,7 +140,9 @@ function onlyIgnoredPaths(paths, ignorePaths) {
113
140
  // An unreadable entry is an unknown, not an ignored one — it makes the whole
114
141
  // commit count, per the bias above.
115
142
  if (!file) return false
116
- return prefixes.some((prefix) => file === prefix || file.startsWith(`${prefix}/`))
143
+ const rel = fromRepoPath(file, prefix)
144
+ if (rel === null) return false
145
+ return prefixes.some((p) => rel === p || rel.startsWith(`${p}/`))
117
146
  })
118
147
  }
119
148
 
@@ -140,6 +169,7 @@ function onlyIgnoredPaths(paths, ignorePaths) {
140
169
  */
141
170
  function ticketsInRange(commits, options = {}) {
142
171
  const ignorePaths = (options && options.ignorePaths) || []
172
+ const prefix = (options && options.prefix) || ''
143
173
  const refPattern = options && options.refPattern
144
174
  const foreign = ((options && options.foreignPatterns) || []).map(asGlobal).filter(Boolean)
145
175
  const seen = new Map()
@@ -150,7 +180,7 @@ function ticketsInRange(commits, options = {}) {
150
180
  let sawMergeFlag = false
151
181
  for (const commit of commits || []) {
152
182
  if (commit && commit.isMerge !== undefined) sawMergeFlag = true
153
- if (onlyIgnoredPaths(commit && commit.paths, ignorePaths)) {
183
+ if (onlyIgnoredPaths(commit && commit.paths, ignorePaths, prefix)) {
154
184
  // Not counted as unreferenced either: that number exists to surface a
155
185
  // MISSED trailer, and a paperwork commit is not a gap someone should hunt.
156
186
  ignored++
@@ -222,6 +252,7 @@ function refuseRemovedVerb(out, program, verb) {
222
252
  }
223
253
 
224
254
  module.exports = {
255
+ projectPrefixOf,
225
256
  ticketsInRange,
226
257
  refuseRemovedVerb,
227
258
  nameTickets,
@@ -21,9 +21,9 @@ const {
21
21
  findSpecFolder,
22
22
  resolveSpec,
23
23
  liveWorktreePaths,
24
- resolvePrimaryCheckout,
25
24
  } = require('../../../env/resolve.js')
26
25
  const { loadEnvConfig } = require('../../../env/config.js')
26
+ const { primaryAnchor, attachProject, projectIn } = require('../../../env/project.js')
27
27
  const { readSnapshot, parseFrontmatter, lintPhases, fieldsOf, projectionOf, compareStored, comparableDescription } = require('../../sync-core')
28
28
 
29
29
  function makeSpecHelpers({ program = 'spec-sync' } = {}) {
@@ -136,9 +136,16 @@ function makeSpecHelpers({ program = 'spec-sync' } = {}) {
136
136
  }
137
137
  let primary
138
138
  let env
139
+ let prefix = ''
139
140
  try {
140
- primary = resolvePrimaryCheckout(dir, git) || dir
141
+ // The project folder in the primary checkout, with where it sits in its
142
+ // repo attached — worktrees are laid out beside the REPO, and each holds
143
+ // the project at the same prefix (feat-subfolder-project).
144
+ const anchor = primaryAnchor(dir, git)
145
+ primary = anchor.dir || dir
146
+ prefix = anchor.prefix
141
147
  env = loadEnvConfig(primary)
148
+ attachProject(env.config, anchor)
142
149
  } catch {
143
150
  return here
144
151
  }
@@ -147,7 +154,7 @@ function makeSpecHelpers({ program = 'spec-sync' } = {}) {
147
154
  // symlinks (macOS /var → /private/var) where `resolveSpec` does not.
148
155
  const real = (p) => {
149
156
  try {
150
- return fs.realpathSync(p)
157
+ return fs.realpathSync.native(p)
151
158
  } catch {
152
159
  return path.resolve(p)
153
160
  }
@@ -156,8 +163,10 @@ function makeSpecHelpers({ program = 'spec-sync' } = {}) {
156
163
  const byName = new Map(here.map((s) => [s.spec, s]))
157
164
  for (const listed of liveWorktreePaths(git)) {
158
165
  const tree = real(listed)
159
- if (tree === self) continue
160
- for (const s of listSpecs(tree, config)) {
166
+ // The project folder inside this worktree — where its specs are.
167
+ const project = real(projectIn(listed, prefix))
168
+ if (project === self) continue
169
+ for (const s of listSpecs(project, config)) {
161
170
  let own = null
162
171
  try {
163
172
  own = real(resolveSpec(s.spec, primary, env.config, { searchDirs: [tree] }).worktreePath)
@@ -198,6 +198,49 @@ function snapshotOf(projection, config) {
198
198
  }
199
199
  }
200
200
 
201
+ /**
202
+ * The `stateNames` record for a snapshot: the tracker state name each item was
203
+ * last SENT, `{ issue, subIssues: { <id>: name } }`, or null when there is
204
+ * nothing to record (no provider hook, nothing ever sent).
205
+ *
206
+ * An item this push sent a state for (`sent`) records today's resolution. Any
207
+ * other item keeps the name the previous snapshot had for it, or gets none —
208
+ * the absence `planChanges` reads as cannot-tell. Writing today's name for an
209
+ * item that was not sent would record a claim about the tracker nobody made.
210
+ *
211
+ * @param {object} projection the projection, carrying `stateName`s
212
+ * @param {object|null} previous the snapshot being replaced
213
+ * @param {{ issue?: boolean, subIssues?: Set<string> }|null} sent
214
+ */
215
+ function stateNamesRecord(projection, previous, sent) {
216
+ const p = projection || {}
217
+ const before = (previous && previous.stateNames) || {}
218
+ const beforeSubs = before.subIssues || {}
219
+ const sentSubs = (sent && sent.subIssues) || new Set()
220
+ const has = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key)
221
+ const out = { subIssues: {} }
222
+ if (sent && sent.issue && p.stateName !== undefined) out.issue = p.stateName
223
+ else if (has(before, 'issue')) out.issue = before.issue
224
+ for (const s of p.subIssues || []) {
225
+ if (!s || s.id == null) continue
226
+ const id = String(s.id)
227
+ if (sentSubs.has(id) && s.stateName !== undefined) out.subIssues[id] = s.stateName
228
+ else if (has(beforeSubs, id)) out.subIssues[id] = beforeSubs[id]
229
+ }
230
+ if (!has(out, 'issue') && !Object.keys(out.subIssues).length) return null
231
+ return out
232
+ }
233
+
234
+ // Has the state an unchanged item maps to moved under it — a state-map edit in
235
+ // the config rather than a move of the spec? Only a RECORDED name can say: an
236
+ // item with none is cannot-tell and plans nothing, so an upgrade or a config
237
+ // change never turns into a workspace-wide resend. A bucket that now sends no
238
+ // state (`null`) has nothing to resend.
239
+ function remapped(recorded, current) {
240
+ if (recorded === undefined || current === undefined || current === null) return false
241
+ return recorded !== current
242
+ }
243
+
201
244
  /**
202
245
  * Diff the local projection against the last-pushed snapshot.
203
246
  * @returns {{ issue?: object, subIssues: {create,update} }}
@@ -213,6 +256,9 @@ function planChanges(projection, snapshot, { linked = false } = {}) {
213
256
  const p = projection || {}
214
257
  const snap = snapshot || {}
215
258
  const snapS = snap.subIssues || {}
259
+ const names = snap.stateNames || {}
260
+ const recordedSub = (id) =>
261
+ names.subIssues && Object.prototype.hasOwnProperty.call(names.subIssues, id) ? names.subIssues[id] : undefined
216
262
 
217
263
  // Ids the last push minted that no phase claims any more. A phase file's stamp
218
264
  // is the only link back to its sub-issue, and it lives in frontmatter — so a
@@ -239,7 +285,15 @@ function planChanges(projection, snapshot, { linked = false } = {}) {
239
285
  subIssues.create.push({ ref: s.ref, name: s.name, goal: s.goal, state: s.state })
240
286
  }
241
287
  } else if (snapS[String(s.id)] !== subIssueHash(s)) {
242
- subIssues.update.push({ ref: s.ref, id: s.id, name: s.name, goal: s.goal, state: s.state })
288
+ const update = { ref: s.ref, id: s.id, name: s.name, goal: s.goal, state: s.state }
289
+ // No hash on record for an id the spec carries: the snapshot was lost or
290
+ // predates it, so this is a resend, not a move the spec made.
291
+ if (snapS[String(s.id)] === undefined) update.stateResend = 'unknown'
292
+ subIssues.update.push(update)
293
+ } else if (remapped(recordedSub(String(s.id)), s.stateName)) {
294
+ // STATE ONLY: the phase itself is unchanged, so its title and body stay
295
+ // as they are on the tracker.
296
+ subIssues.update.push({ ref: s.ref, id: s.id, state: s.state, stateResend: 'remap' })
243
297
  }
244
298
  }
245
299
 
@@ -276,6 +330,9 @@ function issueChanges(projection, snapshot, { linked = false } = {}) {
276
330
  // `linked` is what tells the two apart: an adopted issue has no snapshot
277
331
  // either, and its board status is a person's, not ours.
278
332
  if (!p.statePulled || !linked) out.state = p.status ?? null
333
+ // A linked issue with no split hashes is a resend: nothing says the spec
334
+ // moved, so the apply must not drag the issue out of a later state.
335
+ if (linked && out.state != null) out.stateResend = 'unknown'
279
336
  // ASSERT, NEVER CLEAR, on this path. There are no split hashes here, so
280
337
  // whether an assignee was ever pushed is unknown — and the harmless reading
281
338
  // of "unknown" is to send one we have and stay silent about one we don't.
@@ -302,6 +359,11 @@ function issueChanges(projection, snapshot, { linked = false } = {}) {
302
359
  const changed = {}
303
360
  if (fields.description !== want.description) changed.description = p.description ?? null
304
361
  if (fields.state !== want.state) changed.state = p.status ?? null
362
+ else if (!p.statePulled && remapped(snap.stateNames && snap.stateNames.issue, p.stateName)) {
363
+ // The tracker owns a pulled state, so a remap never re-asserts it there.
364
+ changed.state = p.status ?? null
365
+ changed.stateResend = 'remap'
366
+ }
305
367
 
306
368
  // ASSIGNEE — AN ABSENT SNAPSHOT KEY MEANS "NEVER PUSHED", NOT "WAS NULL".
307
369
  //
@@ -342,6 +404,7 @@ module.exports = {
342
404
  issueChanges,
343
405
  specIssueFieldHashes,
344
406
  snapshotOf,
407
+ stateNamesRecord,
345
408
  isEmptyPlan,
346
409
  hashField,
347
410
  stableStringify,
@@ -18,7 +18,7 @@
18
18
  const { normalizeLocal, phasesWithheld, phaseModeFor, ownershipOf, readSnapshot } = require('./normalize.js')
19
19
  const { descriptionMode } = require('./summary.js')
20
20
  const { fieldsOf } = require('./fields.js')
21
- const { planChanges, snapshotOf, isEmptyPlan } = require('./compare.js')
21
+ const { planChanges, snapshotOf, stateNamesRecord, isEmptyPlan } = require('./compare.js')
22
22
  const { readBase, writeBase } = require('./base.js')
23
23
  const { detectLegacyMirror } = require('./legacy.js')
24
24
 
@@ -55,9 +55,36 @@ function projectionOf(snapshotDir, config) {
55
55
  // a push sends it only as a transition — see `issueChanges`. Set only when
56
56
  // pulled, and read by name, so it can never reach a hash.
57
57
  if (ownershipOf(config, 'workflowState') === 'pull') projection.statePulled = true
58
+ attachStateNames(projection, snapshotDir, config)
58
59
  return projection
59
60
  }
60
61
 
62
+ // The tracker state NAME each bucket resolves to today, from the provider's
63
+ // `config.remote.writtenStateName(bucket, role, snapshotDir)` hook: a string,
64
+ // `null` for "this bucket sends no state", or `undefined` for cannot-tell — no
65
+ // hook, a hook that threw, or no bucket to resolve. Read by name, so it never
66
+ // reaches a hash: recorded beside them as `stateNames`, it is what lets a plan
67
+ // see a state-map change the bucket hashes are blind to.
68
+ function attachStateNames(projection, snapshotDir, config) {
69
+ const hook = config && config.remote && config.remote.writtenStateName
70
+ if (typeof hook !== 'function') return
71
+ const resolve = (bucket, role) => {
72
+ if (!bucket) return undefined
73
+ try {
74
+ const name = hook(bucket, role, snapshotDir)
75
+ return name === null || typeof name === 'string' ? name : undefined
76
+ } catch {
77
+ return undefined
78
+ }
79
+ }
80
+ const name = resolve(projection.status, 'issue')
81
+ if (name !== undefined) projection.stateName = name
82
+ projection.subIssues = projection.subIssues.map((s) => {
83
+ const sub = resolve(s && s.state, 'subIssue')
84
+ return sub === undefined ? s : { ...s, stateName: sub }
85
+ })
86
+ }
87
+
61
88
  // Does the spec carry its issue stamp? NOT `identifier`: callers fall back to the
62
89
  // folder name for an unlinked spec, so a truthy identifier proves nothing. The
63
90
  // stamp is the positive signal — it tells a create (no issue yet, so the spec's
@@ -101,9 +128,18 @@ function push({ dir, snapshotDir, identifier, config }) {
101
128
  * Record the last-pushed snapshot from the CURRENT files — call after the skill
102
129
  * has applied the plan and stamped new ids. Returns the snapshot path.
103
130
  */
104
- function recordPush({ dir, snapshotDir, identifier, config }) {
131
+ // `sent` says which items this push carried a state for — `{ issue, subIssues }`,
132
+ // the latter a set of ids. Only those get today's name recorded; anything else
133
+ // keeps the name it already had, or none. Recording today's name for an item
134
+ // that was NOT sent would claim the tracker holds it, and silence the very remap
135
+ // this record exists to catch. Without `sent` (a hand-applied MCP push) nothing
136
+ // new is recorded and what was there is kept.
137
+ function recordPush({ dir, snapshotDir, identifier, config, sent = null }) {
105
138
  const projection = projectionOf(snapshotDir, config)
106
- return writeBase(dir, identifier, config, snapshotOf(projection, config))
139
+ const snapshot = snapshotOf(projection, config)
140
+ const names = stateNamesRecord(projection, readBase(dir, identifier, config), sent)
141
+ if (names) snapshot.stateNames = names
142
+ return writeBase(dir, identifier, config, snapshot)
107
143
  }
108
144
 
109
145
  module.exports = { push, recordPush, projectionOf }