yadflow 4.0.0-next.3 → 4.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.
Files changed (128) hide show
  1. package/CHANGELOG.md +699 -0
  2. package/README.md +85 -22
  3. package/bin/commands.mjs +3 -1
  4. package/bin/yad.mjs +183 -29
  5. package/cli/capture.mjs +4 -2
  6. package/cli/checkpoint.mjs +10 -10
  7. package/cli/claims.mjs +4 -4
  8. package/cli/commit.mjs +48 -12
  9. package/cli/docs.mjs +15 -13
  10. package/cli/doctor.mjs +557 -141
  11. package/cli/epic-state.mjs +41 -18
  12. package/cli/errors.mjs +3 -2
  13. package/cli/find-product.mjs +197 -0
  14. package/cli/fold.mjs +4 -4
  15. package/cli/gate-local.mjs +13 -13
  16. package/cli/gate.mjs +111 -103
  17. package/cli/history.mjs +13 -13
  18. package/cli/hook.mjs +201 -30
  19. package/cli/index-command.mjs +6 -6
  20. package/cli/lib.mjs +91 -29
  21. package/cli/manifest.mjs +171 -104
  22. package/cli/migrate.mjs +208 -17
  23. package/cli/mode.mjs +22 -22
  24. package/cli/next.mjs +6 -6
  25. package/cli/openpr.mjs +32 -24
  26. package/cli/people.mjs +22 -10
  27. package/cli/plan.mjs +718 -45
  28. package/cli/platform.mjs +13 -7
  29. package/cli/product-index.mjs +4 -4
  30. package/cli/{hubcommit.mjs → productcommit.mjs} +11 -7
  31. package/cli/protection.mjs +19 -9
  32. package/cli/reconcile.mjs +98 -23
  33. package/cli/repo-publish.mjs +22 -16
  34. package/cli/repo.mjs +128 -33
  35. package/cli/report.mjs +4 -2
  36. package/cli/review.mjs +26 -19
  37. package/cli/setup.mjs +105 -36
  38. package/cli/ship.mjs +1 -1
  39. package/cli/skip.mjs +4 -4
  40. package/cli/tidy.mjs +7 -7
  41. package/cli/update-commit.mjs +22 -19
  42. package/cli/workspace.mjs +495 -0
  43. package/package.json +7 -4
  44. package/skills/sdlc/config.yaml +14 -13
  45. package/skills/sdlc/module-help.csv +8 -8
  46. package/skills/yad-analysis/SKILL.md +6 -4
  47. package/skills/yad-architecture/SKILL.md +4 -3
  48. package/skills/yad-backfill/SKILL.md +11 -5
  49. package/skills/yad-backfill/references/backfill.md +33 -3
  50. package/skills/yad-backfill/templates/checks/backfill-check.sh +200 -17
  51. package/skills/yad-change/SKILL.md +1 -1
  52. package/skills/yad-checks/SKILL.md +48 -27
  53. package/skills/yad-checks/references/check-gates.md +213 -53
  54. package/skills/yad-checks/templates/checks/commit-message.sh +39 -13
  55. package/skills/yad-checks/templates/checks/contract-check.sh +472 -23
  56. package/skills/yad-checks/templates/checks/epic-open.sh +398 -8
  57. package/skills/yad-checks/templates/checks/ledger-guard.sh +75 -25
  58. package/skills/yad-checks/templates/checks/lineage-check.sh +391 -7
  59. package/skills/yad-checks/templates/checks/product-checkout.sh +141 -0
  60. package/skills/yad-checks/templates/checks/reconcile-debt-check.sh +399 -8
  61. package/skills/yad-checks/templates/checks/risk-map-check.sh +35 -3
  62. package/skills/yad-checks/templates/checks/spec-link.sh +36 -3
  63. package/skills/yad-checks/templates/checks/verified-commits.sh +26 -2
  64. package/skills/yad-checks/templates/github/yad-checks.yml +33 -13
  65. package/skills/yad-checks/templates/github/{yad-hub-checks.yml → yad-product-checks.yml} +14 -10
  66. package/skills/yad-checks/templates/github/yad-update-guard.yml +1 -1
  67. package/skills/yad-checks/templates/github/yad-verified-commits.yml +1 -1
  68. package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +12 -0
  69. package/skills/yad-checks/templates/gitlab/{yad-hub-checks.gitlab-ci.yml → yad-product-checks.gitlab-ci.yml} +16 -16
  70. package/skills/yad-checks/templates/gitlab/yad-verified-commits.gitlab-ci.yml +2 -2
  71. package/skills/yad-checks/templates/hooks/ledger-guard-cursor.mjs +130 -0
  72. package/skills/yad-checks/templates/hooks/ledger-guard.mjs +117 -0
  73. package/skills/yad-checks/templates/hooks/yad-capture.mjs +117 -0
  74. package/skills/yad-commit/SKILL.md +18 -7
  75. package/skills/yad-connect-design/SKILL.md +1 -1
  76. package/skills/yad-connect-design/references/design-registry.md +1 -1
  77. package/skills/yad-connect-docs/SKILL.md +16 -11
  78. package/skills/yad-connect-docs/references/docs-registry.md +8 -8
  79. package/skills/yad-connect-learning/SKILL.md +1 -1
  80. package/skills/yad-connect-learning/references/learning-registry.md +1 -1
  81. package/skills/yad-connect-repos/SKILL.md +31 -8
  82. package/skills/yad-connect-repos/references/code-context.md +1 -1
  83. package/skills/yad-connect-repos/references/{hub-config.md → product-config.md} +19 -11
  84. package/skills/yad-connect-repos/references/repos-registry.md +1 -1
  85. package/skills/yad-connect-testing/SKILL.md +1 -1
  86. package/skills/yad-connect-testing/references/testing-registry.md +1 -1
  87. package/skills/yad-discovery/SKILL.md +2 -2
  88. package/skills/yad-discovery/references/discovery-schema.md +1 -1
  89. package/skills/yad-docs/SKILL.md +1 -1
  90. package/skills/yad-docs/references/data-mapping.md +1 -1
  91. package/skills/yad-docs/templates/app/package-lock.json +8 -8
  92. package/skills/yad-docs/templates/app/package.json +1 -1
  93. package/skills/yad-docs/templates/app/src/components/Canvas/FlowCanvas.tsx +2 -2
  94. package/skills/yad-docs-overview/references/pipeline-model.md +3 -3
  95. package/skills/yad-engineer-review/SKILL.md +2 -2
  96. package/skills/yad-engineer-review/references/ship-and-record.md +3 -3
  97. package/skills/yad-epic/SKILL.md +6 -4
  98. package/skills/yad-epic/references/state-schema.md +13 -12
  99. package/skills/yad-open-pr/SKILL.md +1 -1
  100. package/skills/yad-pair-review/SKILL.md +5 -5
  101. package/skills/yad-pair-review/references/session-state.md +2 -2
  102. package/skills/yad-pr-template/SKILL.md +14 -12
  103. package/skills/yad-pr-template/templates/checks/pr-template.sh +18 -16
  104. package/skills/yad-pr-template/templates/checks/pr-title.sh +15 -13
  105. package/skills/yad-pr-template/templates/{hub → product}/github/pull_request_template.md +2 -2
  106. package/skills/yad-pr-template/templates/{hub → product}/gitlab/merge_request_templates/Default.md +2 -2
  107. package/skills/{yad-hub-bridge → yad-product-bridge}/SKILL.md +15 -15
  108. package/skills/{yad-hub-bridge → yad-product-bridge}/references/bridge.md +15 -10
  109. package/skills/{yad-hub-bridge → yad-product-bridge}/references/login-roster.md +2 -2
  110. package/skills/{yad-hub-bridge/templates/checks/hub-route.sh → yad-product-bridge/templates/checks/product-route.sh} +2 -2
  111. package/skills/{yad-hub-bridge → yad-product-bridge}/templates/github/yad-gate-sync.yml +29 -16
  112. package/skills/{yad-hub-bridge → yad-product-bridge}/templates/gitlab/gitlab-ci.include-root.yml +1 -1
  113. package/skills/{yad-hub-bridge → yad-product-bridge}/templates/gitlab/yad-gate-sync.gitlab-ci.yml +5 -3
  114. package/skills/yad-report/SKILL.md +1 -1
  115. package/skills/yad-review-companion/SKILL.md +3 -3
  116. package/skills/yad-review-gate/SKILL.md +22 -23
  117. package/skills/yad-review-gate/references/gating.md +13 -13
  118. package/skills/yad-run/SKILL.md +2 -2
  119. package/skills/yad-run/references/run-loop.md +2 -2
  120. package/skills/yad-spec/SKILL.md +2 -1
  121. package/skills/yad-spec/references/spec-handoff.md +19 -10
  122. package/skills/yad-stories/SKILL.md +2 -2
  123. package/skills/yad-sync-repos/SKILL.md +19 -3
  124. package/skills/yad-test-cases/SKILL.md +2 -2
  125. package/skills/yad-ui/SKILL.md +2 -2
  126. package/skills/yad-checks/templates/hooks/ledger-guard-cursor.sh +0 -91
  127. package/skills/yad-checks/templates/hooks/ledger-guard.sh +0 -100
  128. package/skills/yad-checks/templates/hooks/yad-capture.sh +0 -53
@@ -11,11 +11,35 @@
11
11
  # Fails CLOSED on an unresolvable base.
12
12
  set -euo pipefail
13
13
 
14
+ # --- shared Product settings file (byte-identical across the gates; they are standalone by design, so
15
+ # --- it is duplicated, not sourced) ---
16
+ # The Product's settings live under two names until v5: `.sdlc/product.json`, read first from 4.0, and
17
+ # `.sdlc/hub.json`, which an older yadflow wrote and its gates read. The environment may name the file
18
+ # instead: SDLC_PRODUCT_CONFIG, or the older SDLC_HUB_CONFIG. Two names that say different things are
19
+ # never settled by picking one — the CLI refuses the same way (YAD-STATE-008) — so the gate FAILS and
20
+ # names both. Said on stderr and returned as a failure: this runs inside `$(...)`, where an `exit` would
21
+ # only leave the subshell. `cmp -s` is byte for byte, like the CLI's own comparison.
22
+ product_config() {
23
+ if [ -n "${SDLC_PRODUCT_CONFIG:-}" ] && [ -n "${SDLC_HUB_CONFIG:-}" ] \
24
+ && [ "$SDLC_PRODUCT_CONFIG" != "$SDLC_HUB_CONFIG" ] && ! cmp -s "$SDLC_PRODUCT_CONFIG" "$SDLC_HUB_CONFIG"; then
25
+ echo "FAIL [product-settings]: SDLC_PRODUCT_CONFIG (${SDLC_PRODUCT_CONFIG}) and SDLC_HUB_CONFIG (${SDLC_HUB_CONFIG}) name files that say different things. Set only SDLC_PRODUCT_CONFIG." >&2
26
+ return 1
27
+ fi
28
+ if [ -n "${SDLC_PRODUCT_CONFIG:-}" ]; then printf '%s' "$SDLC_PRODUCT_CONFIG"; return 0; fi
29
+ if [ -n "${SDLC_HUB_CONFIG:-}" ]; then printf '%s' "$SDLC_HUB_CONFIG"; return 0; fi
30
+ if [ -f .sdlc/product.json ] && [ -f .sdlc/hub.json ] && ! cmp -s .sdlc/product.json .sdlc/hub.json; then
31
+ echo "FAIL [product-settings]: .sdlc/product.json and .sdlc/hub.json say different things — they are one file under two names until v5. Run \`yad migrate\` in the Product to choose the copy to keep, and commit both." >&2
32
+ return 1
33
+ fi
34
+ if [ -f .sdlc/product.json ]; then printf '%s' .sdlc/product.json; else printf '%s' .sdlc/hub.json; fi
35
+ }
36
+ PRODUCT_CONFIG="$(product_config)" || exit 1
37
+
14
38
  # --- shared base resolution (byte-identical across the gates; they are standalone by design, so it
15
39
  # --- is duplicated, not sourced) ---
16
40
  # With no explicit base, RESOLVE the trunk instead of assuming a hardcoded `origin/main` — on a repo
17
41
  # whose trunk is `develop`/`master` that guess either fails closed or, where a stale `main` still
18
- # exists, silently diffs the WRONG range (issue #161). Mirrors the CLI's own order (cli/hubcommit.mjs,
42
+ # exists, silently diffs the WRONG range (issue #161). Mirrors the CLI's own order (cli/productcommit.mjs,
19
43
  # cli/repo.mjs): the CONFIGURED default_branch first, then the remote's published default
20
44
  # (origin/HEAD), then origin/main. Each candidate must actually resolve before it is used, so a
21
45
  # DANGLING origin/HEAD (trunk renamed, the old remote-tracking ref pruned) falls through to the next
@@ -24,7 +48,7 @@ set -euo pipefail
24
48
  # command substitution in an assignment aborts the script.
25
49
  resolve_base() {
26
50
  # tr first: a key and its value may legally sit on separate lines, which a per-line match misses.
27
- _cfg="$(tr -d '\n' < "${SDLC_HUB_CONFIG:-.sdlc/hub.json}" 2>/dev/null | sed -nE 's/.*"default_branch"[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p')" || _cfg=""
51
+ _cfg="$({ tr -d '\n' < "$PRODUCT_CONFIG"; } 2>/dev/null | sed -nE 's/.*"default_branch"[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p')" || _cfg=""
28
52
  _head="$(git symbolic-ref --short --quiet refs/remotes/origin/HEAD 2>/dev/null)" || _head=""
29
53
  for _c in "origin/${_cfg}" "${_head}" origin/main; do
30
54
  case "$_c" in ''|origin/) continue ;; esac
@@ -47,8 +71,9 @@ EXEMPT='ci|chore|build|test'
47
71
  # --- reconcile-debt-check; the gates are deliberately standalone, so it is duplicated, not sourced) ---
48
72
  # Read one frontmatter value from the FIRST --- … --- block only. awk bounds to the first block (stops
49
73
  # at the first closing fence), so a body `---` or an absent key can never leak a body line. Plain
50
- # scalars only; trailing spaces/CR are stripped so they never become part of a path.
51
- fm_val() { awk -v k="$1" 'NR==1 && /^---$/ {f=1; next} f && /^---$/ {exit} f && index($0, k":")==1 {sub("^" k ":[ \t]*", ""); print; exit}' "$2" 2>/dev/null | tr -d '\r' | sed -E 's/[[:space:]]+$//'; }
74
+ # scalars only; trailing spaces/CR are stripped so they never become part of a path. The CR goes BEFORE
75
+ # the fences are matched: a CRLF file's `---\r` is no fence, and every key in it read as empty (E117 r2).
76
+ fm_val() { awk -v k="$1" '{ sub(/\r$/, "") } NR==1 && /^---$/ {f=1; next} f && /^---$/ {exit} f && index($0, k":")==1 {sub("^" k ":[ \t]*", ""); print; exit}' "$2" 2>/dev/null | tr -d '\r' | sed -E 's/[[:space:]]+$//'; }
52
77
 
53
78
  # Same, for a link.md field. yad-spec writes link.md WITH frontmatter, but code repos still carry
54
79
  # pre-frontmatter ones that contract-check used to read with a whole-file scan — so fall back to that
@@ -60,6 +85,349 @@ link_val() {
60
85
  printf '%s' "$_v"
61
86
  }
62
87
 
88
+ # E117. WHERE the Product is read from must not be something the PR under check chose. link.md is a file
89
+ # the PR's own author writes, so two things are read around it:
90
+ # - `product-repo` comes from the BASE. Where the Product lives is a fact about the whole repo, not
91
+ # about one story, so: link.md as it stands on the base when that has a value; else a sibling
92
+ # `specs/<story>/link.md` on the base — one of the same epic first, and the first whose value reaches
93
+ # a folder here, so one stale link.md cannot hand every new story a deferral; else — the repo's
94
+ # first spec — this PR's value. Otherwise a PR could point it at a path that does not exist —
95
+ # "not reachable", a deferral, a PASS — or at a folder of its own. A value that differs is printed
96
+ # and counts once it merges. Only `product-repo` comes from the base: `contract-lock` is read as the
97
+ # PR leaves it — a re-spec PR updates the pin, and must.
98
+ # - a Product this repo KEEPS (a monorepo: the Product and the code in one git repo) is read as it
99
+ # stands on the base, never as the PR leaves it. In order: (1) the path's TEXT names a folder whose
100
+ # `.sdlc/product.json` or `hub.json` the base tracks (every Product commits one) — asked of the base before the disk,
101
+ # which is the PR's: a PR that deleted or moved that folder read as untracked, and deferred; (2) the
102
+ # path is walked on disk: a tracked symlink, submodule or file on the way is refused (what is behind
103
+ # it is the PR's to choose), and so is a tracked folder that is not a kept Product, so one force-added
104
+ # file cannot switch the gates to a partial Product; the walk is repeated from where the path really
105
+ # lands (an alias like /proc/self/cwd means another folder to each process); an untracked folder
106
+ # that is there is CI's checkout, and is read — unless it is inside the repo's own git folder,
107
+ # which branch names shape (review 7); (3) only when the path reaches NOTHING, a Product the base keeps that holds
108
+ # this story's epic is the one meant. "Inside the code repo" is NOT the test: CI can only check a
109
+ # second repo out INSIDE the workspace, and an untracked checkout there is not the PR's.
110
+ #
111
+ # E120. Before any of that, the repo's own record: `.sdlc/product-link.json`, written by `yad check --fix`
112
+ # / `yad update` and merged on its own. Its `path` (from the repo root; where CI checks the Product out)
113
+ # is read from the BASE and wins over every link.md — so no gate takes where the Product lives from the
114
+ # PR. A record the PR adds or changes counts once it merges (a note says so). A link.md whose own
115
+ # product-repo reaches a DIFFERENT folder here gets a note; one that reaches nothing (a path on its
116
+ # author's machine) is passed over in silence. When the record's path reaches NOTHING — a developer's
117
+ # own run, where nobody cloned into it — the base's link.md order below is used if IT reaches a folder
118
+ # (still the base's, never the PR's); otherwise the record stands, and the gate defers (review 1).
119
+ # With no record on the base, the order above is kept.
120
+ # Sets product_rel, prod (the folder to read), prod_note and prod_fail (text; the caller prints them).
121
+ _yad_tmp=""
122
+ _arch_key=""
123
+ _arch_dir=""
124
+ yad_tmp() { [ -n "$_yad_tmp" ] || { _yad_tmp="$(mktemp -d)"; trap 'rm -rf "$_yad_tmp"' EXIT; }; }
125
+ # `product-repo` as link.md $1 has it on the base (empty when it is not there or names none).
126
+ base_product_rel() {
127
+ git cat-file -e "${BASE}:$1" 2>/dev/null || return 0
128
+ yad_tmp
129
+ git show "${BASE}:$1" > "$_yad_tmp/base-link.md" 2>/dev/null || : > "$_yad_tmp/base-link.md"
130
+ link_val product-repo "$_yad_tmp/base-link.md"
131
+ }
132
+ # The `path` of `.sdlc/product-link.json` at $1 (a commit), or empty when there is none or it names none.
133
+ record_path() {
134
+ git cat-file -e "$1:.sdlc/product-link.json" 2>/dev/null || return 0
135
+ git show "$1:.sdlc/product-link.json" 2>/dev/null | tr -d '\n\r' | sed -nE 's/.*"path"[[:space:]]*:[[:space:]]*"([^"\\]*)".*/\1/p' || true
136
+ }
137
+ product_for() {
138
+ _link="specs/$1/link.md"
139
+ prod=""; prod_note=""; prod_fail=""
140
+ yad_tmp # here, in this shell: a $(…) below would set it, and its EXIT trap, in a subshell only
141
+ product_rel="$(link_val product-repo "$_link")"
142
+ _from="$_link"
143
+ _rec="$(record_path "$BASE")"
144
+ _rec_head="$(record_path HEAD)"
145
+ if [ "$_rec_head" != "$_rec" ]; then
146
+ if [ -n "$_rec" ]; then _w="changes in this PR"; else _w="is new in this PR"; fi
147
+ prod_note=".sdlc/product-link.json ${_w} — where the Product lives is read from ${BASE}, not from this PR; the new one counts once it merges."
148
+ fi
149
+ _base_rel="$(base_product_rel "$_link")"
150
+ if [ -z "$_base_rel" ]; then
151
+ # A sibling's value. Every link.md sits at specs/<story>/, so a relative value means the same there.
152
+ _sibs="$(git ls-tree -r -z --name-only "$BASE" -- specs 2>/dev/null | tr '\n\0' '?\n' | grep -E '^specs/[^/]+/link\.md$' || true)"
153
+ _first=""; _first_from=""
154
+ for _pass in same other; do
155
+ while IFS= read -r _sib; do
156
+ [ -n "$_sib" ] && [ "$_sib" != "$_link" ] || continue
157
+ _s="${_sib#specs/}"; _s="${_s%/link.md}"
158
+ story_ok "$_s" || continue
159
+ if [ "$(story_epic "$_s")" = "$(story_epic "$1")" ]; then [ "$_pass" = same ] || continue; else [ "$_pass" = other ] || continue; fi
160
+ _v="$(base_product_rel "$_sib")"
161
+ [ -n "$_v" ] || continue
162
+ if [ -z "$_first" ]; then _first="$_v"; _first_from="$_sib"; fi
163
+ if [ -d "$(resolve_product "$_v" "$1")" ]; then _base_rel="$_v"; _from="$_sib"; break 2; fi
164
+ done <<SIBLINGS
165
+ $_sibs
166
+ SIBLINGS
167
+ done
168
+ if [ -z "$_base_rel" ]; then _base_rel="$_first"; _from="$_first_from"; fi
169
+ fi
170
+ _use_rec=""
171
+ if [ -n "$_rec" ]; then
172
+ # Relative to the repo root; link.md values are relative to specs/<story>/, so join it that way.
173
+ case "$_rec" in /*) _rv="$_rec" ;; *) _rv="../../$_rec" ;; esac
174
+ if [ -d "$(resolve_product "$_rv" "$1")" ] || [ -z "$_base_rel" ] || [ ! -d "$(resolve_product "$_base_rel" "$1")" ]; then
175
+ _use_rec=1; _base_rel="$_rv"; _from=".sdlc/product-link.json"
176
+ fi
177
+ fi
178
+ if [ -n "$_use_rec" ]; then
179
+ # The record wins. Say so only when this link.md's own value reaches a different folder here.
180
+ _mine="$(resolve_product "$product_rel" "$1")"
181
+ _theirs="$(resolve_product "$_base_rel" "$1")"
182
+ if [ -n "$product_rel" ] && [ -d "$_mine" ] && ! [ "$_mine" -ef "$_theirs" ]; then
183
+ prod_note="${prod_note:+$prod_note
184
+ }${_link} names product-repo '${product_rel}', but .sdlc/product-link.json on ${BASE} says '${_rec}' — the record is read."
185
+ fi
186
+ product_rel="$_base_rel"
187
+ elif [ -n "$_base_rel" ] && [ "$_base_rel" != "$product_rel" ]; then
188
+ prod_note="${prod_note:+$prod_note
189
+ }${_link} names product-repo '${product_rel}', but ${_from} on ${BASE} says '${_base_rel}' — the Product is read from the base value; a new one counts once it merges."
190
+ product_rel="$_base_rel"
191
+ fi
192
+ # A Product this repo keeps, asked of the base first (see above). Both joins resolve_product may pick.
193
+ case "$product_rel" in
194
+ '') _cands="" ;;
195
+ /*|'~'*|'$'*) _cands="$product_rel" ;;
196
+ *) _cands="specs/$1/$product_rel
197
+ $product_rel" ;;
198
+ esac
199
+ while IFS= read -r _cand; do
200
+ [ -n "$_cand" ] || continue
201
+ _lr="$(lex_rel "$_cand")"
202
+ [ -n "$_lr" ] || continue
203
+ [ "$_lr" != . ] || _lr=""
204
+ if git cat-file -e "${BASE}:${_lr:+$_lr/}.sdlc/product.json" 2>/dev/null || git cat-file -e "${BASE}:${_lr:+$_lr/}.sdlc/hub.json" 2>/dev/null; then base_product "$_lr"; return 0; fi
205
+ done <<CANDS
206
+ $_cands
207
+ CANDS
208
+ if [ -n "$product_rel" ]; then
209
+ prod="$(resolve_product "$product_rel" "$1")"
210
+ ! tracked_verdict "$(product_tracked "$prod")" || return 0
211
+ if [ -d "$prod" ]; then
212
+ # Walked once more, from where it really lands, resolved in ONE step from this shell (review 5).
213
+ # The walk above goes one part at a time in a subshell, and a Linux magic link such as
214
+ # /proc/self/cwd means a different folder to each process: there it pointed at the walk's own
215
+ # subshell and found nothing, while this shell read the PR's own copy of a kept Product through it.
216
+ # `./` before a relative path, so a value starting with `-` is not read by `cd` as an option
217
+ # (review 6); and a folder that is there but cannot be entered is refused, not read unchecked.
218
+ case "$prod" in /*) _pc="$prod" ;; *) _pc="./$prod" ;; esac
219
+ _phys="$(cd -P "$_pc" 2>/dev/null && pwd -P)" || _phys=""
220
+ if [ -z "$_phys" ]; then
221
+ prod_fail="product-repo resolves to '${prod}', a folder the gate cannot enter to check where it really is — so it is not read."
222
+ return 0
223
+ fi
224
+ # Not inside this repo's own git folder (review 7). git never lists `.git/` as tracked, yet a PR
225
+ # shapes it: a branch named `epics/EP-x/y` makes the folders `.git/refs/…/epics/EP-x/` when CI
226
+ # fetches, and that read as an untracked checkout holding an open epic with no lock.
227
+ # Compared as FOLDERS (`-ef`: the same one on disk), not as spellings: on a disk that ignores case
228
+ # (macOS, Windows) `.GIT/refs` is `.git/refs`, and `pwd -P` keeps the case it was typed in (review 8).
229
+ for _gd in "$(git rev-parse --absolute-git-dir 2>/dev/null)" "$(git rev-parse --git-common-dir 2>/dev/null)"; do
230
+ [ -n "$_gd" ] && [ -d "$_gd" ] || continue
231
+ _p="$_phys"
232
+ while :; do
233
+ if [ "$_p" -ef "$_gd" ]; then
234
+ prod_fail="product-repo resolves to '${prod}', inside this repo's own git folder — what is there is git's, shaped by branch names, not a Product."
235
+ return 0
236
+ fi
237
+ case "$_p" in /|'') break ;; esac
238
+ _p="${_p%/*}"; [ -n "$_p" ] || _p=/
239
+ done
240
+ done
241
+ ! tracked_verdict "$(product_tracked "$_phys")" || return 0
242
+ # An untracked folder that is there is the Product CI checked out: a PR cannot make one. Read it.
243
+ return 0
244
+ fi
245
+ fi
246
+ # product-repo reaches NOTHING. Then a Product the base keeps that holds this story's epic is the one
247
+ # meant (E117 review 3): a monorepo value spelled past the repo's own folder name, another machine's
248
+ # absolute path, or a first spec pointing nowhere cannot be folded to it by text, and a PR that deleted
249
+ # or moved it leaves no disk to walk. Only when nothing is reached (review 4): a kept Product used to
250
+ # win over the real checkout, so a merged test fixture shaped like a Product was read instead of it.
251
+ # And only one holding the epic, so such a fixture is not read for stories it knows nothing about.
252
+ # (In a two-repo setup whose CI checks nothing out, a fixture that DOES hold the epic is read where
253
+ # the gate used to defer — no weaker than the deferral it replaces.)
254
+ kept_products
255
+ _epic_of="$(story_epic "$1")"
256
+ _kept_here=""
257
+ while IFS= read -r _k; do
258
+ [ -n "$_k" ] || continue
259
+ _kd="$_k"; [ "$_kd" != . ] || _kd=""
260
+ if [ "$(git cat-file -t "${BASE}:${_kd:+$_kd/}epics/${_epic_of}" 2>/dev/null)" = tree ]; then _kept_here="${_kept_here}${_k}
261
+ "; fi
262
+ done <<KEPT
263
+ $_kept
264
+ KEPT
265
+ [ -n "$_kept_here" ] || return 0
266
+ if [ -n "$product_rel" ]; then _what="product-repo '${product_rel}' reaches nothing"; else _what="link.md names no product-repo"; fi
267
+ if [ "$(printf '%s' "$_kept_here" | grep -c .)" -gt 1 ]; then
268
+ prod_fail="${_what}, and ${BASE} keeps more than one Product holding ${_epic_of} ($(printf '%s' "$_kept_here" | tr '\n' ' ' | sed -E 's/ +$//')) — the gate cannot tell which is meant."
269
+ return 0
270
+ fi
271
+ _k="$(printf '%s' "$_kept_here" | head -1)"
272
+ prod_note="${prod_note:+$prod_note
273
+ }${_what} here; the Product this repo keeps at '${_k}' on ${BASE} holds ${_epic_of}, so that one is read."
274
+ product_rel="$_k"
275
+ [ "$_k" != . ] || _k=""
276
+ base_product "$_k"
277
+ return 0
278
+ }
279
+
280
+ # Act on product_tracked's answer $1: a tracked entry on the way is refused; a tracked folder is read from
281
+ # the base when the base keeps a Product there, and refused otherwise. Returns 1 when there was nothing to
282
+ # act on (the path is not the repo's), so the caller goes on.
283
+ tracked_verdict() {
284
+ case "$1" in
285
+ entry:*)
286
+ prod_fail="product-repo reaches the Product through '${1#*:}', a symlink, submodule or file this repo tracks — what is behind it is this PR's to choose. Point product-repo at the Product itself."
287
+ return 0 ;;
288
+ tree:*)
289
+ _r="${1#*:}"
290
+ if git cat-file -e "${BASE}:${_r:+$_r/}.sdlc/product.json" 2>/dev/null || git cat-file -e "${BASE}:${_r:+$_r/}.sdlc/hub.json" 2>/dev/null; then base_product "$_r"; return 0; fi
291
+ prod_fail="product-repo resolves to '${_r:-.}', which holds files this repo tracks, but ${BASE} does not track '${_r:+$_r/}.sdlc/product.json' (or the older hub.json) there, so it is not a Product kept in this repo. Untrack those files (git rm -r --cached '${_r:-.}') or point product-repo at the Product."
292
+ return 0 ;;
293
+ esac
294
+ return 1
295
+ }
296
+
297
+ # Every Product the base keeps: a tracked `.sdlc/product.json` or `.sdlc/hub.json` (both, until v5) with
298
+ # an `epics/` folder beside it (a code repo may track one of its own; only a Product has epics). One line
299
+ # each, `.` for the root, the two names counted once; read once.
300
+ _kept=""
301
+ _kept_read=""
302
+ kept_products() {
303
+ [ -z "$_kept_read" ] || return 0
304
+ _kept_read=1
305
+ while IFS= read -r _h; do
306
+ [ -n "$_h" ] || continue
307
+ _d="${_h%.sdlc/*.json}"; _d="${_d%/}"
308
+ case "
309
+ ${_kept}" in *"
310
+ ${_d:-.}
311
+ "*) continue ;; esac
312
+ if [ "$(git cat-file -t "${BASE}:${_d:+$_d/}epics" 2>/dev/null)" = tree ]; then _kept="${_kept}${_d:-.}
313
+ "; fi
314
+ done <<KEPT
315
+ $(git ls-tree -r -z --name-only "$BASE" 2>/dev/null | tr '\n\0' '?\n' | grep -E '(^|/)\.sdlc/(product|hub)\.json$' || true)
316
+ KEPT
317
+ }
318
+
319
+ # Read the Product this repo keeps at $1 (from the repo root; "" is the root) as it stands on the base:
320
+ # its `epics/` written out under the temp folder. Sets prod, or prod_fail.
321
+ base_product() {
322
+ _r="$1"; _e="${_r:+$_r/}epics"
323
+ if [ "$(git cat-file -t "${BASE}:${_e}" 2>/dev/null)" != tree ]; then
324
+ prod_fail="product-repo resolves to '${_r:-.}', a Product this repo keeps, and ${BASE} has no '${_e}' folder — there is nothing on the base to read."
325
+ return 0
326
+ fi
327
+ # (awk reads to the end rather than stopping at the first hit: stopping left `tr` writing into a
328
+ # closed pipe on a large epics/, and under pipefail the gate died with no message — review 6.)
329
+ # A symlink or submodule inside it is refused by name, as under specs/ (E115): a link written out
330
+ # still points where it pointed — an absolute one into this PR's working tree — and a submodule comes
331
+ # out as an empty folder, which reads as an epic with no stories.
332
+ _bad="$(git ls-tree -r -z "${BASE}:${_e}" 2>/dev/null | tr '\n\0' '?\n' | awk '!f && ($1 == "120000" || $1 == "160000") { sub(/^[^\t]*\t/, ""); print; f = 1 }')"
333
+ if [ -n "$_bad" ]; then
334
+ prod_fail="'${_e}/${_bad}' on ${BASE} is a symlink or a submodule, so the Product this repo keeps cannot be read from the base. Replace it with the files themselves."
335
+ return 0
336
+ fi
337
+ # Keyed as "x<path>": the repo root is the path "", which an empty key would take as done.
338
+ if [ "$_arch_key" != "x$_r" ]; then
339
+ _arch_dir="$(mktemp -d "$_yad_tmp/product.XXXXXX")"
340
+ # A throwaway index holding the base's epics/, written out with the WORK TREE pointed at the empty
341
+ # temp folder. Not `git archive`: it drops whatever the base's .gitattributes marks `export-ignore`.
342
+ # And not with the real work tree: `git checkout-index` takes attributes from the work tree, which
343
+ # is the PR's — a `.gitattributes` of `*.md text eol=crlf` or `working-tree-encoding=UTF-16` in the
344
+ # PR wrote the base's files unreadable, and every gate passed. Pointed here, it finds none but the
345
+ # base's own.
346
+ # The index's own `.gitattributes` entries are dropped before it is written out, for the same reason
347
+ # (review 3): one merged into the base's epics/ applied `working-tree-encoding=UTF-16` to every file.
348
+ if ! { mkdir -p "${_arch_dir}/${_e}" &&
349
+ GIT_INDEX_FILE="${_arch_dir}.idx" git read-tree "${BASE}:${_e}" &&
350
+ GIT_INDEX_FILE="${_arch_dir}.idx" git ls-files -z -- ':(glob)**/.gitattributes' |
351
+ GIT_INDEX_FILE="${_arch_dir}.idx" xargs -0 git update-index --force-remove -- &&
352
+ GIT_WORK_TREE="${_arch_dir}/${_e}" GIT_INDEX_FILE="${_arch_dir}.idx" git checkout-index -a -f; } >/dev/null 2>&1; then
353
+ prod_fail="product-repo resolves to '${_r:-.}', a Product this repo keeps, and '${_e}' could not be read from ${BASE}."
354
+ return 0
355
+ fi
356
+ _arch_key="x$_r"
357
+ fi
358
+ prod="${_arch_dir}${_r:+/$_r}"
359
+ prod_note="${prod_note:+$prod_note
360
+ }the Product at '${_r:-.}' is kept in this repo, so it is read as it stands on ${BASE}, not as this PR leaves it."
361
+ return 0
362
+ }
363
+
364
+ # The path $1 from the repo root, `.` and `..` folded by its text alone — no disk. Prints `.` for the
365
+ # root and nothing when it leaves the repo. An absolute path counts when it starts with the repo's path
366
+ # as the shell spells it or as the disk does (`pwd`, `pwd -P`; the gates run from the root).
367
+ lex_rel() (
368
+ _p="$1"
369
+ case "$_p" in
370
+ /*)
371
+ _in=""
372
+ for _top in "$(pwd)" "$(pwd -P)"; do
373
+ case "$_p/" in "$_top"/*) _p="${_p#"$_top"}"; _in=1; break ;; esac
374
+ done
375
+ [ -n "$_in" ] || exit 0 ;;
376
+ esac
377
+ _out=""
378
+ set -f; IFS=/
379
+ for _c in $_p; do
380
+ case "$_c" in
381
+ ''|.) ;;
382
+ ..) [ -n "$_out" ] || exit 0
383
+ case "$_out" in */*) _out="${_out%/*}" ;; *) _out="" ;; esac ;;
384
+ *) _out="${_out:+$_out/}$_c" ;;
385
+ esac
386
+ done
387
+ printf '%s' "${_out:-.}"
388
+ )
389
+
390
+ # Is the path $1 — as the shell would walk it, `..` and links included — something this repo tracks?
391
+ # Prints `entry:<path>` when a part of the way is a tracked entry itself (a symlink, a submodule, a
392
+ # file), `tree:<path>` when it ends in a folder with tracked files at or under it, and nothing when
393
+ # it is the repo's to neither. <path> is from the repo root, as the disk spells it (`pwd -P`). The case
394
+ # is folded where git folds it (core.ignorecase — a Mac checkout), and in ASCII only.
395
+ product_tracked() (
396
+ _top="$(git rev-parse --show-toplevel 2>/dev/null)" && _top="$(cd -P "$_top" 2>/dev/null && pwd -P)" || exit 0
397
+ _ps='literal'; [ "$(git config --bool core.ignorecase 2>/dev/null)" = true ] && _ps='literal,icase'
398
+ _in() { case "$1/" in "$_top"/*) _r="${1#"$_top"}"; _r="${_r#/}"; return 0 ;; esac; return 1; }
399
+ _hit() { git -C "$_top" ls-files -z -- ":(${_ps})$1" 2>/dev/null | tr '\n\0' '?\n'; }
400
+ case "$1" in /*) cd / ;; esac
401
+ _rest="$1"
402
+ while [ -n "$_rest" ]; do
403
+ _c="${_rest%%/*}"
404
+ case "$_rest" in */*) _rest="${_rest#*/}" ;; *) _rest="" ;; esac
405
+ case "$_c" in ''|.) continue ;; esac
406
+ if [ "$_c" != .. ] && _in "$(pwd -P)/$_c" && [ -n "$_r" ] &&
407
+ _hit "$_r" | awk -v p="$_r" -v i="$_ps" '{ a = $0; b = p; if (i ~ /icase/) { a = tolower(a); b = tolower(b) } } a == b { f = 1 } END { exit !f }'; then
408
+ printf 'entry:%s' "$_r"; exit 0
409
+ fi
410
+ cd -P "./$_c" 2>/dev/null || exit 0 # ./ — a part named `-p` is not an option
411
+ done
412
+ _in "$(pwd -P)" || exit 0
413
+ if [ -n "$(_hit "${_r:-.}" | head -1)" ]; then printf 'tree:%s' "$_r"; fi
414
+ exit 0
415
+ )
416
+
417
+ # E118. A story's epic is its ID's prefix (EP-<slug>-S0N -> EP-<slug>): yad-spec always writes link.md's
418
+ # `epic:` so, and contract-check never read the field. The field is the PR's to write, so a gate that took
419
+ # it as it stands gated whichever epic the PR named — a story of a SEALED epic could name an open one and
420
+ # pass epic-open, or step off a thread frozen for hotfix debt. The gates that read `epic:` require it to
421
+ # agree with the story ID.
422
+ story_epic() { printf '%s' "$1" | sed -E 's/-S[0-9]+$//'; }
423
+
424
+ # One story ID shape for every gate (E117 review 1): EP-<slug>-S<n>, what yad-stories writes. Anything
425
+ # else is refused by name — a Task trailer of `EP-x-S01/.-T1` read `specs/EP-x-S01/./link.md`, a path
426
+ # the base read never found, and its `epic:` of `EP-x-S01/.` agreed with story_epic. The whole string
427
+ # must match (bash `=~`, not a line-wise grep), and the letters are spelled out so no locale widens them.
428
+ _story_re='^EP-[abcdefghijklmnopqrstuvwxyz0123456789-]+-S[0123456789]+$'
429
+ story_ok() { [[ $1 =~ $_story_re ]]; }
430
+
63
431
  # Resolve link.md's `product-repo` to a path in THIS checkout. An ABSOLUTE value is used as-is. A
64
432
  # RELATIVE value is written relative to the link.md's own directory (specs/<story>/) — the canonical
65
433
  # form — but contract-check historically read it from the repo root, so a link.md authored against that
@@ -102,7 +470,7 @@ while IFS= read -r sha; do
102
470
  [ -z "$sha" ] && continue
103
471
  short="$(git log -1 --format=%h "$sha")"
104
472
  subject="$(git log -1 --format=%s "$sha")"
105
- task="$(git log -1 --format='%(trailers:key=Task,valueonly)' "$sha" | sed '/^$/d' | head -1)"
473
+ task="$(git log -1 --format='%(trailers:key=Task,valueonly)' "$sha" | awk '!f && length($0) { print; f = 1 }')"
106
474
  # The type exemption waives the REQUIREMENT for an owning epic, not the VALIDITY of one that is
107
475
  # claimed — same rule spec-link applies. Exempting on the subject alone would let `chore(x): …` plus
108
476
  # a Task trailer pointing at a SEALED epic add behaviour to it, which is exactly what this gate exists
@@ -116,19 +484,41 @@ while IFS= read -r sha; do
116
484
  continue
117
485
  fi
118
486
  story="$(printf '%s' "$task" | sed -E 's/-T[0-9]+$//')"
487
+ if ! story_ok "$story"; then
488
+ echo "FAIL [epic-open]: ${short} ${task} — '${story}' is not a story ID (expected EP-<slug>-S<n>, the slug in lowercase letters, digits and dashes)."
489
+ rc=1
490
+ continue
491
+ fi
119
492
  link="specs/${story}/link.md"
120
493
  [ -f "$link" ] || { echo "note [epic-open]: ${short} ${task} — link.md missing (spec-link will FAIL)."; continue; }
121
- product_rel="$(link_val product-repo "$link")"
122
494
  epic="$(link_val epic "$link")"
123
495
  # A malformed link.md (empty product-repo, or an epic that is not a real EP-<slug>) must FAIL, not
124
496
  # slip through as "not reachable" — an empty epic would collapse ep_dir to <product>/epics/ (a real
125
497
  # dir) and pass the seal check as if the epic were open.
126
- if [ -z "$product_rel" ] || ! printf '%s' "$epic" | grep -qE '^EP-[a-z0-9-]+$'; then
498
+ if ! printf '%s' "$epic" | grep -qE '^EP-[a-z0-9-]+$'; then
499
+ echo "FAIL [epic-open]: ${short} ${task} — link.md has no valid product-repo/epic metadata."
500
+ rc=1
501
+ continue
502
+ fi
503
+ if [ "$epic" != "$(story_epic "$story")" ]; then
504
+ echo "FAIL [epic-open]: ${short} ${task} — ${link} says epic: ${epic:-<none>}, but ${story} is a story of $(story_epic "$story")."
505
+ echo " A story's epic is the prefix of its ID; the gate will not read another. Fix epic: in ${link}."
506
+ rc=1
507
+ continue
508
+ fi
509
+ product_for "$story"
510
+ if [ -n "$prod_note" ]; then printf '%s\n' "$prod_note" | sed "s/^/note [epic-open]: ${short} /"; fi
511
+ # The empty-product-repo half of the malformed check, on the value product_for settled on (E117).
512
+ if [ -z "$product_rel" ]; then
127
513
  echo "FAIL [epic-open]: ${short} ${task} — link.md has no valid product-repo/epic metadata."
128
514
  rc=1
129
515
  continue
130
516
  fi
131
- prod="$(resolve_product "$product_rel" "$story")"
517
+ if [ -n "$prod_fail" ]; then
518
+ echo "FAIL [epic-open]: ${short} ${task} — ${prod_fail}"
519
+ rc=1
520
+ continue
521
+ fi
132
522
  ep_dir="${prod}/epics/${epic}"
133
523
  if [ ! -d "$prod" ]; then
134
524
  echo "PASS [epic-open]: ${short} ${task} -> ${epic} (product repo not reachable — seal check deferred)."