@pecot/workflow-mcp 0.0.0-stage → 6.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 (287) hide show
  1. package/.claude-plugin/plugin.json +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +184 -2
  4. package/bin/pecot-workflow-mcp.mjs +10 -0
  5. package/cli/dist/cli.d.ts +26 -0
  6. package/cli/dist/cli.js +227 -0
  7. package/cli/dist/commands/board-add.d.ts +3 -0
  8. package/cli/dist/commands/board-add.js +59 -0
  9. package/cli/dist/commands/board-judgment.d.ts +60 -0
  10. package/cli/dist/commands/board-judgment.js +144 -0
  11. package/cli/dist/commands/board-move.d.ts +3 -0
  12. package/cli/dist/commands/board-move.js +57 -0
  13. package/cli/dist/commands/board-readiness.d.ts +34 -0
  14. package/cli/dist/commands/board-readiness.js +108 -0
  15. package/cli/dist/commands/board-show.d.ts +3 -0
  16. package/cli/dist/commands/board-show.js +72 -0
  17. package/cli/dist/commands/board-sync.d.ts +3 -0
  18. package/cli/dist/commands/board-sync.js +111 -0
  19. package/cli/dist/commands/board-view.d.ts +62 -0
  20. package/cli/dist/commands/board-view.js +191 -0
  21. package/cli/dist/commands/board.d.ts +3 -0
  22. package/cli/dist/commands/board.js +12 -0
  23. package/cli/dist/commands/branch.d.ts +3 -0
  24. package/cli/dist/commands/branch.js +100 -0
  25. package/cli/dist/commands/cell-table.d.ts +3 -0
  26. package/cli/dist/commands/cell-table.js +495 -0
  27. package/cli/dist/commands/command-binding.d.ts +26 -0
  28. package/cli/dist/commands/command-binding.js +270 -0
  29. package/cli/dist/commands/command-definition.d.ts +150 -0
  30. package/cli/dist/commands/command-definition.js +105 -0
  31. package/cli/dist/commands/command-help.d.ts +8 -0
  32. package/cli/dist/commands/command-help.js +173 -0
  33. package/cli/dist/commands/command-hint.d.ts +5 -0
  34. package/cli/dist/commands/command-hint.js +13 -0
  35. package/cli/dist/commands/command-stop.d.ts +21 -0
  36. package/cli/dist/commands/command-stop.js +31 -0
  37. package/cli/dist/commands/directory-option.d.ts +7 -0
  38. package/cli/dist/commands/directory-option.js +48 -0
  39. package/cli/dist/commands/failure-kind.d.ts +3 -0
  40. package/cli/dist/commands/failure-kind.js +34 -0
  41. package/cli/dist/commands/failure.d.ts +19 -0
  42. package/cli/dist/commands/failure.js +59 -0
  43. package/cli/dist/commands/fuzzy.d.ts +7 -0
  44. package/cli/dist/commands/fuzzy.js +79 -0
  45. package/cli/dist/commands/github-client.d.ts +16 -0
  46. package/cli/dist/commands/github-client.js +33 -0
  47. package/cli/dist/commands/global-options.d.ts +25 -0
  48. package/cli/dist/commands/global-options.js +83 -0
  49. package/cli/dist/commands/help-layout.d.ts +40 -0
  50. package/cli/dist/commands/help-layout.js +117 -0
  51. package/cli/dist/commands/help.d.ts +8 -0
  52. package/cli/dist/commands/help.js +50 -0
  53. package/cli/dist/commands/input-schema.d.ts +14 -0
  54. package/cli/dist/commands/input-schema.js +56 -0
  55. package/cli/dist/commands/issue-board.d.ts +71 -0
  56. package/cli/dist/commands/issue-board.js +122 -0
  57. package/cli/dist/commands/issue-comment.d.ts +3 -0
  58. package/cli/dist/commands/issue-comment.js +215 -0
  59. package/cli/dist/commands/issue-create.d.ts +3 -0
  60. package/cli/dist/commands/issue-create.js +786 -0
  61. package/cli/dist/commands/issue-dependency.d.ts +3 -0
  62. package/cli/dist/commands/issue-dependency.js +282 -0
  63. package/cli/dist/commands/issue-readiness.d.ts +94 -0
  64. package/cli/dist/commands/issue-readiness.js +218 -0
  65. package/cli/dist/commands/issue-reopen.d.ts +3 -0
  66. package/cli/dist/commands/issue-reopen.js +115 -0
  67. package/cli/dist/commands/issue-shared.d.ts +172 -0
  68. package/cli/dist/commands/issue-shared.js +426 -0
  69. package/cli/dist/commands/issue-show.d.ts +3 -0
  70. package/cli/dist/commands/issue-show.js +169 -0
  71. package/cli/dist/commands/issue.d.ts +3 -0
  72. package/cli/dist/commands/issue.js +13 -0
  73. package/cli/dist/commands/names.d.ts +42 -0
  74. package/cli/dist/commands/names.js +78 -0
  75. package/cli/dist/commands/output.d.ts +90 -0
  76. package/cli/dist/commands/output.js +194 -0
  77. package/cli/dist/commands/pr-waiting.d.ts +20 -0
  78. package/cli/dist/commands/pr-waiting.js +106 -0
  79. package/cli/dist/commands/pr.d.ts +22 -0
  80. package/cli/dist/commands/pr.js +852 -0
  81. package/cli/dist/commands/project-sync.d.ts +92 -0
  82. package/cli/dist/commands/project-sync.js +282 -0
  83. package/cli/dist/commands/project.d.ts +6 -0
  84. package/cli/dist/commands/project.js +714 -0
  85. package/cli/dist/commands/registry.d.ts +6 -0
  86. package/cli/dist/commands/registry.js +30 -0
  87. package/cli/dist/commands/release.d.ts +10 -0
  88. package/cli/dist/commands/release.js +353 -0
  89. package/cli/dist/commands/renamed-commands.d.ts +26 -0
  90. package/cli/dist/commands/renamed-commands.js +53 -0
  91. package/cli/dist/commands/repository-display.d.ts +3 -0
  92. package/cli/dist/commands/repository-display.js +7 -0
  93. package/cli/dist/commands/repository-match.d.ts +14 -0
  94. package/cli/dist/commands/repository-match.js +34 -0
  95. package/cli/dist/commands/rich-panel.d.ts +22 -0
  96. package/cli/dist/commands/rich-panel.js +295 -0
  97. package/cli/dist/commands/session-finish.d.ts +3 -0
  98. package/cli/dist/commands/session-finish.js +267 -0
  99. package/cli/dist/commands/session-start.d.ts +3 -0
  100. package/cli/dist/commands/session-start.js +236 -0
  101. package/cli/dist/commands/session.d.ts +6 -0
  102. package/cli/dist/commands/session.js +214 -0
  103. package/cli/dist/commands/tracker-sync-dependabot.d.ts +7 -0
  104. package/cli/dist/commands/tracker-sync-dependabot.js +156 -0
  105. package/cli/dist/commands/tracker-sync-security.d.ts +7 -0
  106. package/cli/dist/commands/tracker-sync-security.js +153 -0
  107. package/cli/dist/commands/tracker-sync.d.ts +65 -0
  108. package/cli/dist/commands/tracker-sync.js +50 -0
  109. package/cli/dist/commands/tracker.d.ts +3 -0
  110. package/cli/dist/commands/tracker.js +18 -0
  111. package/cli/dist/commands/usage-error.d.ts +51 -0
  112. package/cli/dist/commands/usage-error.js +78 -0
  113. package/cli/dist/commands/value-types.d.ts +46 -0
  114. package/cli/dist/commands/value-types.js +94 -0
  115. package/cli/dist/commands/work-target.d.ts +22 -0
  116. package/cli/dist/commands/work-target.js +82 -0
  117. package/cli/dist/commands/worktree.d.ts +3 -0
  118. package/cli/dist/commands/worktree.js +339 -0
  119. package/cli/dist/domain/api-error-detail.d.ts +10 -0
  120. package/cli/dist/domain/api-error-detail.js +25 -0
  121. package/cli/dist/domain/batch-plan.d.ts +73 -0
  122. package/cli/dist/domain/batch-plan.js +114 -0
  123. package/cli/dist/domain/blocked-by-change.d.ts +99 -0
  124. package/cli/dist/domain/blocked-by-change.js +101 -0
  125. package/cli/dist/domain/board-move.d.ts +90 -0
  126. package/cli/dist/domain/board-move.js +92 -0
  127. package/cli/dist/domain/branch-name.d.ts +5 -0
  128. package/cli/dist/domain/branch-name.js +10 -0
  129. package/cli/dist/domain/changelog.d.ts +43 -0
  130. package/cli/dist/domain/changelog.js +107 -0
  131. package/cli/dist/domain/command-hint.d.ts +85 -0
  132. package/cli/dist/domain/command-hint.js +148 -0
  133. package/cli/dist/domain/config.d.ts +219 -0
  134. package/cli/dist/domain/config.js +310 -0
  135. package/cli/dist/domain/dependabot-pull-requests.d.ts +82 -0
  136. package/cli/dist/domain/dependabot-pull-requests.js +338 -0
  137. package/cli/dist/domain/dependabot-source.d.ts +51 -0
  138. package/cli/dist/domain/dependabot-source.js +4 -0
  139. package/cli/dist/domain/dependabot.d.ts +47 -0
  140. package/cli/dist/domain/dependabot.js +88 -0
  141. package/cli/dist/domain/errors.d.ts +24 -0
  142. package/cli/dist/domain/errors.js +36 -0
  143. package/cli/dist/domain/field-options.d.ts +31 -0
  144. package/cli/dist/domain/field-options.js +44 -0
  145. package/cli/dist/domain/format.d.ts +94 -0
  146. package/cli/dist/domain/format.js +234 -0
  147. package/cli/dist/domain/frontmatter.d.ts +137 -0
  148. package/cli/dist/domain/frontmatter.js +290 -0
  149. package/cli/dist/domain/git-runner.d.ts +10 -0
  150. package/cli/dist/domain/git-runner.js +4 -0
  151. package/cli/dist/domain/git-url.d.ts +17 -0
  152. package/cli/dist/domain/git-url.js +30 -0
  153. package/cli/dist/domain/issue-kind.d.ts +16 -0
  154. package/cli/dist/domain/issue-kind.js +16 -0
  155. package/cli/dist/domain/issue-order.d.ts +55 -0
  156. package/cli/dist/domain/issue-order.js +65 -0
  157. package/cli/dist/domain/issue-readout.d.ts +143 -0
  158. package/cli/dist/domain/issue-readout.js +4 -0
  159. package/cli/dist/domain/json-null.d.ts +2 -0
  160. package/cli/dist/domain/json-null.js +5 -0
  161. package/cli/dist/domain/json.d.ts +21 -0
  162. package/cli/dist/domain/json.js +47 -0
  163. package/cli/dist/domain/prerequisites.d.ts +32 -0
  164. package/cli/dist/domain/prerequisites.js +192 -0
  165. package/cli/dist/domain/pull-request.d.ts +201 -0
  166. package/cli/dist/domain/pull-request.js +281 -0
  167. package/cli/dist/domain/python-format.d.ts +105 -0
  168. package/cli/dist/domain/python-format.js +304 -0
  169. package/cli/dist/domain/readiness.d.ts +199 -0
  170. package/cli/dist/domain/readiness.js +391 -0
  171. package/cli/dist/domain/reference-parser.d.ts +51 -0
  172. package/cli/dist/domain/reference-parser.js +123 -0
  173. package/cli/dist/domain/release.d.ts +53 -0
  174. package/cli/dist/domain/release.js +65 -0
  175. package/cli/dist/domain/repository-policy.d.ts +38 -0
  176. package/cli/dist/domain/repository-policy.js +66 -0
  177. package/cli/dist/domain/repository-resolver.d.ts +57 -0
  178. package/cli/dist/domain/repository-resolver.js +122 -0
  179. package/cli/dist/domain/sections.d.ts +69 -0
  180. package/cli/dist/domain/sections.js +514 -0
  181. package/cli/dist/domain/security-alerts-source.d.ts +29 -0
  182. package/cli/dist/domain/security-alerts-source.js +4 -0
  183. package/cli/dist/domain/security-alerts.d.ts +52 -0
  184. package/cli/dist/domain/security-alerts.js +107 -0
  185. package/cli/dist/domain/security-tracker.d.ts +115 -0
  186. package/cli/dist/domain/security-tracker.js +353 -0
  187. package/cli/dist/domain/state.d.ts +59 -0
  188. package/cli/dist/domain/state.js +95 -0
  189. package/cli/dist/domain/tracker-body.d.ts +70 -0
  190. package/cli/dist/domain/tracker-body.js +65 -0
  191. package/cli/dist/domain/work-start.d.ts +34 -0
  192. package/cli/dist/domain/work-start.js +49 -0
  193. package/cli/dist/domain/yaml-document.d.ts +5 -0
  194. package/cli/dist/domain/yaml-document.js +206 -0
  195. package/cli/dist/infra/authored-notes.d.ts +37 -0
  196. package/cli/dist/infra/authored-notes.js +72 -0
  197. package/cli/dist/infra/changelog-file.d.ts +19 -0
  198. package/cli/dist/infra/changelog-file.js +43 -0
  199. package/cli/dist/infra/child-stdin.d.ts +4 -0
  200. package/cli/dist/infra/child-stdin.js +8 -0
  201. package/cli/dist/infra/config-store.d.ts +53 -0
  202. package/cli/dist/infra/config-store.js +134 -0
  203. package/cli/dist/infra/diagnostics.d.ts +9 -0
  204. package/cli/dist/infra/diagnostics.js +2 -0
  205. package/cli/dist/infra/file-kind.d.ts +9 -0
  206. package/cli/dist/infra/file-kind.js +26 -0
  207. package/cli/dist/infra/frontmatter-file.d.ts +14 -0
  208. package/cli/dist/infra/frontmatter-file.js +30 -0
  209. package/cli/dist/infra/git.d.ts +86 -0
  210. package/cli/dist/infra/git.js +145 -0
  211. package/cli/dist/infra/github/auth.d.ts +60 -0
  212. package/cli/dist/infra/github/auth.js +158 -0
  213. package/cli/dist/infra/github/client.d.ts +178 -0
  214. package/cli/dist/infra/github/client.js +579 -0
  215. package/cli/dist/infra/github/dependabot-source.d.ts +13 -0
  216. package/cli/dist/infra/github/dependabot-source.js +33 -0
  217. package/cli/dist/infra/github/errors.d.ts +65 -0
  218. package/cli/dist/infra/github/errors.js +64 -0
  219. package/cli/dist/infra/github/index.d.ts +13 -0
  220. package/cli/dist/infra/github/index.js +19 -0
  221. package/cli/dist/infra/github/issues.d.ts +165 -0
  222. package/cli/dist/infra/github/issues.js +428 -0
  223. package/cli/dist/infra/github/payload.d.ts +44 -0
  224. package/cli/dist/infra/github/payload.js +80 -0
  225. package/cli/dist/infra/github/projects.d.ts +143 -0
  226. package/cli/dist/infra/github/projects.js +384 -0
  227. package/cli/dist/infra/github/pulls.d.ts +104 -0
  228. package/cli/dist/infra/github/pulls.js +217 -0
  229. package/cli/dist/infra/github/queries.d.ts +44 -0
  230. package/cli/dist/infra/github/queries.js +1031 -0
  231. package/cli/dist/infra/github/readout.d.ts +22 -0
  232. package/cli/dist/infra/github/readout.js +167 -0
  233. package/cli/dist/infra/github/repos.d.ts +86 -0
  234. package/cli/dist/infra/github/repos.js +168 -0
  235. package/cli/dist/infra/github/rest-path.d.ts +14 -0
  236. package/cli/dist/infra/github/rest-path.js +31 -0
  237. package/cli/dist/infra/github/security-alerts-source.d.ts +15 -0
  238. package/cli/dist/infra/github/security-alerts-source.js +73 -0
  239. package/cli/dist/infra/gitignore-spec.d.ts +36 -0
  240. package/cli/dist/infra/gitignore-spec.js +235 -0
  241. package/cli/dist/infra/nx-scope.d.ts +51 -0
  242. package/cli/dist/infra/nx-scope.js +182 -0
  243. package/cli/dist/infra/os-error.d.ts +24 -0
  244. package/cli/dist/infra/os-error.js +38 -0
  245. package/cli/dist/infra/paths.d.ts +88 -0
  246. package/cli/dist/infra/paths.js +234 -0
  247. package/cli/dist/infra/state-store.d.ts +54 -0
  248. package/cli/dist/infra/state-store.js +130 -0
  249. package/cli/dist/infra/template-store.d.ts +15 -0
  250. package/cli/dist/infra/template-store.js +34 -0
  251. package/cli/dist/infra/text-file.d.ts +12 -0
  252. package/cli/dist/infra/text-file.js +33 -0
  253. package/cli/dist/infra/worktree-copy.d.ts +24 -0
  254. package/cli/dist/infra/worktree-copy.js +95 -0
  255. package/cli/dist/infra/worktree-links.d.ts +142 -0
  256. package/cli/dist/infra/worktree-links.js +440 -0
  257. package/cli/dist/infra/worktree-setup.d.ts +21 -0
  258. package/cli/dist/infra/worktree-setup.js +41 -0
  259. package/cli/dist/infra/worktree.d.ts +185 -0
  260. package/cli/dist/infra/worktree.js +584 -0
  261. package/cli/dist/mcp/result.d.ts +61 -0
  262. package/cli/dist/mcp/result.js +96 -0
  263. package/cli/dist/mcp/serve.d.ts +51 -0
  264. package/cli/dist/mcp/serve.js +183 -0
  265. package/cli/dist/mcp/server.d.ts +21 -0
  266. package/cli/dist/mcp/server.js +142 -0
  267. package/cli/dist/mcp/session-directory.d.ts +26 -0
  268. package/cli/dist/mcp/session-directory.js +84 -0
  269. package/cli/dist/package.json +1 -0
  270. package/cli/dist/version.d.ts +17 -0
  271. package/cli/dist/version.js +42 -0
  272. package/cli/templates/comment/merge-diff.md +34 -0
  273. package/cli/templates/comment/plan.md +195 -0
  274. package/cli/templates/comment/precede-notify.md +33 -0
  275. package/cli/templates/comment/review-findings.md +42 -0
  276. package/cli/templates/comment/review-resolution.md +48 -0
  277. package/cli/templates/issue/bug.md +63 -0
  278. package/cli/templates/issue/epic.md +54 -0
  279. package/cli/templates/issue/task-implement.md +74 -0
  280. package/cli/templates/issue/task.md +48 -0
  281. package/cli/templates/issue/tracker.md +48 -0
  282. package/cli/templates/pr/create.md +123 -0
  283. package/cli/templates/pr/release.md +56 -0
  284. package/cli/templates/workflow/pecot-board-sync.yml +84 -0
  285. package/hooks/lib/core/pecot-gitignore-ensure.sh +48 -0
  286. package/npm-shrinkwrap.json +83 -0
  287. package/package.json +22 -4
@@ -0,0 +1,295 @@
1
+ // 端末向けの枠付き表示。Python の CLI が使っていた表示ライブラリ(rich 15)の折り返し規則を写す。
2
+ //
3
+ // 規則は次のとおり。
4
+ // - 単語は、前の空白・語・後ろの空白の並び。語が行に収まらなければ次の行へ送る。
5
+ // - 1 行より長い語は、幅の単位で折る。
6
+ // - 幅は端末のセル数で数える。幅の表は rich のもの(cell-table.ts)。
7
+ // - 異体字セレクタ U+FE0F は、直前の文字を 2 セルにする場合がある。ゼロ幅接合子 U+200D は、次の文字を数えない。
8
+ import { splitCodePoints } from '../domain/python-format.js';
9
+ import { CELL_WIDTHS, NARROW_TO_WIDE } from './cell-table.js';
10
+ const ZERO_WIDTH_JOINER = '‍';
11
+ const VARIATION_SELECTOR_16 = '️';
12
+ const WHITESPACE = /^\s$/u;
13
+ const NARROW_TO_WIDE_SET = new Set(NARROW_TO_WIDE);
14
+ const LAST_RANGE_END = Math.max(...CELL_WIDTHS.map(([, end]) => end));
15
+ /**
16
+ 先頭のコードポイント。空文字列は 0
17
+ */
18
+ function codePointOf(character) {
19
+ /* v8 ignore next -- 呼び出し側は 1 文字以上を渡す */
20
+ return character.codePointAt(0) ?? 0;
21
+ }
22
+ /**
23
+ 表の middle 番目の行。二分探索が範囲内の添字だけを渡す
24
+ */
25
+ function tableEntry(index) {
26
+ /* v8 ignore next -- 二分探索は範囲内の添字だけを渡す */
27
+ return CELL_WIDTHS.at(index) ?? [0, 0, 1];
28
+ }
29
+ /**
30
+ index 番目の文字。範囲外は空文字列
31
+ */
32
+ function characterAt(characters, index) {
33
+ /* v8 ignore next -- 呼び出し側は範囲内の添字を渡す */
34
+ return characters.at(index) ?? '';
35
+ }
36
+ function characterCells(character) {
37
+ const code = codePointOf(character);
38
+ if ((code > 0 && code < 32) || (code >= 0x7F && code < 0xA0))
39
+ return 0;
40
+ if (code > LAST_RANGE_END)
41
+ return 1;
42
+ let low = 0;
43
+ let high = CELL_WIDTHS.length - 1;
44
+ while (low <= high) {
45
+ const middle = (low + high) >> 1;
46
+ const [start, end, width] = tableEntry(middle);
47
+ if (code < start)
48
+ high = middle - 1;
49
+ else if (code > end)
50
+ low = middle + 1;
51
+ else
52
+ return width;
53
+ }
54
+ return 1;
55
+ }
56
+ function isSpecial(character) {
57
+ return character === ZERO_WIDTH_JOINER || character === VARIATION_SELECTOR_16;
58
+ }
59
+ function cellLengthOf(characters) {
60
+ if (characters.every(character => !isSpecial(character))) {
61
+ return characters.reduce((total, character) => total + characterCells(character), 0);
62
+ }
63
+ let total = 0;
64
+ let lastMeasured;
65
+ for (let index = 0; index < characters.length; index += 1) {
66
+ const character = characterAt(characters, index);
67
+ if (character === ZERO_WIDTH_JOINER) {
68
+ index += 1;
69
+ }
70
+ else if (character === VARIATION_SELECTOR_16) {
71
+ if (lastMeasured !== undefined) {
72
+ total += NARROW_TO_WIDE_SET.has(codePointOf(lastMeasured)) ? 1 : 0;
73
+ lastMeasured = undefined;
74
+ }
75
+ }
76
+ else {
77
+ const cells = characterCells(character);
78
+ if (cells > 0) {
79
+ lastMeasured = character;
80
+ total += cells;
81
+ }
82
+ }
83
+ }
84
+ return total;
85
+ }
86
+ /**
87
+ 端末のセル数
88
+ */
89
+ export function cellLength(text) {
90
+ return cellLengthOf(splitCodePoints(text));
91
+ }
92
+ /**
93
+ 文字を字素ごとの範囲に分ける。ゼロ幅の文字は直前の字素に付ける
94
+ */
95
+ function splitGraphemes(characters) {
96
+ const spans = [];
97
+ let lastMeasured;
98
+ let index = 0;
99
+ while (index < characters.length) {
100
+ const character = characterAt(characters, index);
101
+ const previous = spans.at(-1);
102
+ if (isSpecial(character)) {
103
+ if (previous === undefined) {
104
+ spans.push({ start: index, end: index + 1, cells: 0 });
105
+ index += 1;
106
+ }
107
+ else if (character === ZERO_WIDTH_JOINER) {
108
+ index += index < characters.length - 1 ? 2 : 1;
109
+ spans[spans.length - 1] = { ...previous, end: index };
110
+ }
111
+ else {
112
+ index += 1;
113
+ let cells = previous.cells;
114
+ if (lastMeasured !== undefined && NARROW_TO_WIDE_SET.has(codePointOf(lastMeasured))) {
115
+ lastMeasured = undefined;
116
+ cells += 1;
117
+ }
118
+ spans[spans.length - 1] = { ...previous, end: index, cells };
119
+ }
120
+ continue;
121
+ }
122
+ const cells = characterCells(character);
123
+ if (cells > 0) {
124
+ lastMeasured = character;
125
+ spans.push({ start: index, end: index + 1, cells });
126
+ }
127
+ else if (previous === undefined) {
128
+ spans.push({ start: index, end: index + 1, cells: 0 });
129
+ }
130
+ else {
131
+ spans[spans.length - 1] = { ...previous, end: index + 1 };
132
+ }
133
+ index += 1;
134
+ }
135
+ return spans;
136
+ }
137
+ /**
138
+ 行を幅の単位で折る。幅に収まらない字素は次の行へ送る
139
+ */
140
+ function chopCells(characters, width) {
141
+ const lines = [];
142
+ let lineSize = 0;
143
+ let lineOffset = 0;
144
+ for (const { start, cells } of splitGraphemes(characters)) {
145
+ if (lineSize + cells > width) {
146
+ lines.push(characters.slice(lineOffset, start));
147
+ lineOffset = start;
148
+ lineSize = 0;
149
+ }
150
+ lineSize += cells;
151
+ }
152
+ if (lineSize > 0)
153
+ lines.push(characters.slice(lineOffset));
154
+ return lines;
155
+ }
156
+ /**
157
+ 先頭から cells セル分を残す。字素の途中で切れるときは、半端な 1 セルを空白にする
158
+ */
159
+ function keepCells(characters, cells) {
160
+ if (cells <= 0)
161
+ return [];
162
+ const spans = splitGraphemes(characters);
163
+ let left = 0;
164
+ for (const span of spans) {
165
+ if (left + span.cells > cells) {
166
+ const rest = cells - left;
167
+ return [...characters.slice(0, span.start), ...Array.from({ length: rest }, () => ' ')];
168
+ }
169
+ left += span.cells;
170
+ if (left === cells)
171
+ return characters.slice(0, span.end);
172
+ }
173
+ return characters;
174
+ }
175
+ function truncateCells(characters, width) {
176
+ return cellLengthOf(characters) <= width ? characters : keepCells(characters, width);
177
+ }
178
+ function isSpaceAt(characters, index) {
179
+ return WHITESPACE.test(characterAt(characters, index));
180
+ }
181
+ function nextWord(characters, start) {
182
+ let index = start;
183
+ while (index < characters.length && isSpaceAt(characters, index))
184
+ index += 1;
185
+ if (index >= characters.length)
186
+ return undefined;
187
+ while (index < characters.length && !isSpaceAt(characters, index))
188
+ index += 1;
189
+ const bodyEnd = index;
190
+ while (index < characters.length && isSpaceAt(characters, index))
191
+ index += 1;
192
+ return { start, end: index, bodyEnd };
193
+ }
194
+ /**
195
+ 折り返す位置(文字の添字)を返す。長すぎる語は幅の単位で折る
196
+ */
197
+ function divideLine(characters, width) {
198
+ const breaks = [];
199
+ let cellOffset = 0;
200
+ let word = nextWord(characters, 0);
201
+ while (word !== undefined) {
202
+ const { start, end, bodyEnd } = word;
203
+ const wordLength = cellLengthOf(characters.slice(start, bodyEnd));
204
+ const wholeWord = characters.slice(start, end);
205
+ if (width - cellOffset >= wordLength) {
206
+ cellOffset += cellLengthOf(wholeWord);
207
+ }
208
+ else if (wordLength > width) {
209
+ const pieces = chopCells(wholeWord, width);
210
+ let position = start;
211
+ for (const [pieceIndex, piece] of pieces.entries()) {
212
+ if (position > 0)
213
+ breaks.push(position);
214
+ if (pieceIndex === pieces.length - 1)
215
+ cellOffset = cellLengthOf(piece);
216
+ else
217
+ position += piece.length;
218
+ }
219
+ }
220
+ else if (cellOffset > 0) {
221
+ breaks.push(start);
222
+ cellOffset = cellLengthOf(wholeWord);
223
+ }
224
+ word = nextWord(characters, end);
225
+ }
226
+ return breaks;
227
+ }
228
+ /**
229
+ 行がコードポイント数で幅を超えるとき、超えた分だけ末尾の空白を落とす
230
+ */
231
+ function stripExcessWhitespace(characters, width) {
232
+ if (characters.length <= width)
233
+ return characters;
234
+ let whitespace = 0;
235
+ while (whitespace < characters.length && isSpaceAt(characters, characters.length - 1 - whitespace))
236
+ whitespace += 1;
237
+ return characters.slice(0, characters.length - Math.min(whitespace, characters.length - width));
238
+ }
239
+ /**
240
+ 幅に収まるように折り返した行を返す。短い行を空白で埋めはしない。
241
+ 入力の改行はそのまま行の区切りにする。長すぎる語は幅の単位で折る
242
+ */
243
+ export function wrapText(text, width) {
244
+ return text.split('\n').flatMap((line) => {
245
+ const characters = splitCodePoints(line);
246
+ const offsets = [0, ...divideLine(characters, width), characters.length];
247
+ return offsets.slice(0, -1).map((from, index) => {
248
+ const piece = characters.slice(from, offsets.at(index + 1));
249
+ return truncateCells(stripExcessWhitespace(piece, width), width).join('');
250
+ });
251
+ });
252
+ }
253
+ /**
254
+ 行を幅のセル数まで空白で埋める
255
+ */
256
+ export function padCells(line, width) {
257
+ return `${line}${' '.repeat(Math.max(0, width - cellLength(line)))}`;
258
+ }
259
+ const MIN_WIDTH = 10;
260
+ const DEFAULT_WIDTH = 80;
261
+ function isDigits(value) {
262
+ return value !== undefined && /^\d+$/u.test(value);
263
+ }
264
+ /**
265
+ 枠の幅。typer と rich に合わせた順で決める。TERMINAL_WIDTH、COLUMNS、端末の幅、無ければ 80
266
+ */
267
+ export function outputWidth(environment, terminalColumns) {
268
+ const terminalWidth = environment['TERMINAL_WIDTH'];
269
+ const columns = environment['COLUMNS'];
270
+ let width = DEFAULT_WIDTH;
271
+ if (isDigits(terminalWidth) && Number(terminalWidth) > 0)
272
+ width = Number(terminalWidth);
273
+ else if (isDigits(columns) && Number(columns) > 0)
274
+ width = Number(columns);
275
+ else if (terminalColumns !== undefined && terminalColumns > 0)
276
+ width = terminalColumns;
277
+ return Math.max(MIN_WIDTH, width);
278
+ }
279
+ /**
280
+ 枠付きの表示。左上にタイトルを置く。lines は枠の内側の幅に収まっている行。末尾は改行で終わる
281
+ */
282
+ function framePanel(title, lines, width) {
283
+ const inner = width - 4;
284
+ const titleText = ` ${title} `;
285
+ const top = `╭─${titleText}${'─'.repeat(Math.max(0, width - 3 - cellLength(titleText)))}╮`;
286
+ const body = lines.map(line => `│ ${padCells(line, inner)} │`);
287
+ return `${[top, ...body, `╰${'─'.repeat(width - 2)}╯`].join('\n')}\n`;
288
+ }
289
+ /**
290
+ 枠付きの表示。本文は幅に合わせて折り返す。末尾は改行で終わる
291
+ */
292
+ export function renderPanel(title, body, width) {
293
+ return framePanel(title, wrapText(body, width - 4), width);
294
+ }
295
+ //# sourceMappingURL=rich-panel.js.map
@@ -0,0 +1,3 @@
1
+ import type { CommandDefinition } from './command-definition.js';
2
+ export declare const SESSION_FINISH_COMMANDS: readonly CommandDefinition[];
3
+ //# sourceMappingURL=session-finish.d.ts.map
@@ -0,0 +1,267 @@
1
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types -- GitHubClient は内部の状態を持つ */
2
+ // session finish。cli/commands/pr.py の pr_cleanup の移植。
3
+ import { z } from 'zod';
4
+ import { describeApiError } from '../domain/api-error-detail.js';
5
+ import { extractIssueNumberFromBranch } from '../domain/branch-name.js';
6
+ import { getDevelopBranch } from '../domain/config.js';
7
+ import { isEpicIssue } from '../domain/issue-kind.js';
8
+ import { extractClosingIssueNumbers } from '../domain/pull-request.js';
9
+ import { FLAG_MERGE_SLASH_PENDING, FLAG_PECOT_ACTIVE, FLAG_PECOT_BASE, FLAG_PECOT_QUICK_MODE, FLAG_PECOT_SESSION_TITLE, FLAG_WIP_BRANCH_LINKED } from '../domain/state.js';
10
+ import { gitCurrentBranch, GitCommandError, runGit } from '../infra/git.js';
11
+ import { GitHubAPIError, GitHubNotFoundError, isClosedByAnotherRequest, isMissingReferenceError } from '../infra/github/errors.js';
12
+ import { isFileNotFound } from '../infra/os-error.js';
13
+ import { consumeFlag, removeFlag } from '../infra/state-store.js';
14
+ import { getCurrentWorktreeName, isWorktreeEnvironment } from '../infra/worktree.js';
15
+ import { requireActiveConfigPlain, stopWith } from './command-stop.js';
16
+ import { kindOfFailure } from './failure.js';
17
+ import { createGitHubClient } from './github-client.js';
18
+ import { createOutput } from './output.js';
19
+ import { defineCommand } from './command-definition.js';
20
+ import { settle } from './tracker-sync.js';
21
+ import { isPositive, parseReference, PR_REFERENCE_DESCRIPTION, primaryRemote, reasonOf } from './pr.js';
22
+ async function fetchPullRequestForCleanup(config, location, out) {
23
+ try {
24
+ const client = createGitHubClient(out, config.host);
25
+ return { client, pr: await client.getPullRequestSlim(location.owner, location.repository, location.number) };
26
+ }
27
+ catch (error) {
28
+ if (error instanceof GitHubNotFoundError)
29
+ return stopWith(out, `PR not found: ${location.owner}/${location.repository}/${String(location.number)}`, 'github-not-found');
30
+ if (error instanceof GitHubAPIError)
31
+ return stopWith(out, error.message, kindOfFailure(error));
32
+ throw error;
33
+ }
34
+ }
35
+ function collectClosingIssueNumbers(pr) {
36
+ const numbers = new Set(pr.closing_issues.map(issue => issue.number));
37
+ for (const number of extractClosingIssueNumbers(pr.body))
38
+ numbers.add(number);
39
+ const branchNumber = extractIssueNumberFromBranch(pr.head_ref);
40
+ if (isPositive(branchNumber))
41
+ numbers.add(branchNumber);
42
+ return numbers;
43
+ }
44
+ // Epic 保護。Issue Type が Epic か、sub-issue を持つ Issue
45
+ async function findEpicProtectedNumbers(client, location, issueNumbers, out) {
46
+ const protectedNumbers = new Set();
47
+ const ordered = [...issueNumbers].toSorted((a, b) => a - b);
48
+ for (const number of ordered) {
49
+ try {
50
+ if (isEpicIssue(await client.getIssueKind(location.owner, location.repository, number)))
51
+ protectedNumbers.add(number);
52
+ }
53
+ catch (error) {
54
+ if (error instanceof GitHubNotFoundError)
55
+ continue;
56
+ if (!(error instanceof GitHubAPIError))
57
+ throw error;
58
+ out.warning(`kind check of Issue #${String(number)} failed: ${error.message}; not protected`);
59
+ }
60
+ }
61
+ return protectedNumbers;
62
+ }
63
+ async function closeRelatedIssues(context, client, location, closing, epicProtected) {
64
+ const { out, isDryRun } = context;
65
+ const protectedOrdered = [...epicProtected].toSorted((a, b) => a - b);
66
+ const closingOrdered = [...closing].toSorted((a, b) => a - b);
67
+ for (const number of protectedOrdered)
68
+ settle(out, { verb: 'skip', target: `closing Issue #${String(number)} (Epic)` });
69
+ for (const number of closingOrdered) {
70
+ const operation = { verb: 'close', target: `Issue #${String(number)}` };
71
+ out.planned(operation);
72
+ if (isDryRun)
73
+ continue;
74
+ try {
75
+ await client.closeIssue(location.owner, location.repository, number);
76
+ out.done(operation);
77
+ }
78
+ catch (error) {
79
+ if (error instanceof GitHubNotFoundError) {
80
+ out.warning(`Issue #${String(number)} not found; skipped`);
81
+ }
82
+ else if (error instanceof GitHubAPIError) {
83
+ if (isClosedByAnotherRequest(error))
84
+ out.done({ verb: 'unchanged', target: `Issue #${String(number)} (already closed)` });
85
+ else
86
+ out.warning(`closing Issue #${String(number)} failed: ${describeApiError(error)}`);
87
+ }
88
+ else {
89
+ throw error;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ // head ブランチを消し、続けて wip ブランチを消す。head が無くても wip は試す
95
+ async function deleteMergedBranch(context, client, location, headBranch) {
96
+ const { out, isDryRun } = context;
97
+ const operation = { verb: 'delete', target: `remote branch ${headBranch}` };
98
+ out.planned(operation);
99
+ if (isDryRun)
100
+ return;
101
+ try {
102
+ await client.deleteBranch(location.owner, location.repository, headBranch);
103
+ out.done(operation);
104
+ }
105
+ catch (error) {
106
+ if (!(error instanceof GitHubAPIError))
107
+ throw error;
108
+ if (!isMissingReferenceError(error)) {
109
+ out.warning(`deleting remote branch ${headBranch} failed: ${describeApiError(error)}`);
110
+ return;
111
+ }
112
+ out.done({ verb: 'unchanged', target: `remote branch ${headBranch} (already deleted)` });
113
+ }
114
+ const wipBranch = `wip/${headBranch}`;
115
+ try {
116
+ await client.deleteBranch(location.owner, location.repository, wipBranch);
117
+ out.done({ verb: 'delete', target: `remote branch ${wipBranch}` });
118
+ }
119
+ catch (error) {
120
+ if (!(error instanceof GitHubAPIError))
121
+ throw error;
122
+ if (!isMissingReferenceError(error))
123
+ out.warning(`deleting remote branch ${wipBranch} failed: ${describeApiError(error)}`);
124
+ }
125
+ }
126
+ // 手元と PR の repository が同じなら主の remote 名。違えば警告を出して undefined
127
+ function sameRepositoryRemote(cwd, location, out) {
128
+ const skipped = 'skipped local cleanup';
129
+ try {
130
+ const found = primaryRemote(cwd);
131
+ if (found === undefined) {
132
+ out.warning(`cannot read the remote URL; ${skipped}`);
133
+ return undefined;
134
+ }
135
+ if (found.location === undefined) {
136
+ out.warning(`cannot read the repository from the remote URL; ${skipped}`);
137
+ return undefined;
138
+ }
139
+ if (found.location.owner !== location.owner || found.location.repository !== location.repository) {
140
+ out.warning(`current repository (${found.location.owner}/${found.location.repository}) differs from the PR's (${location.owner}/${location.repository}); ${skipped}`);
141
+ return undefined;
142
+ }
143
+ return found.name;
144
+ }
145
+ catch {
146
+ out.warning(`cannot determine the repository; ${skipped}`);
147
+ return undefined;
148
+ }
149
+ }
150
+ // worktree では worktree 名、そうでなければ develop ブランチ
151
+ function resolveCheckoutTarget(cwd, config, location, out) {
152
+ try {
153
+ if (isWorktreeEnvironment(cwd))
154
+ return getCurrentWorktreeName(cwd) ?? 'main';
155
+ const developBranch = getDevelopBranch(config, location.owner, location.repository);
156
+ if (developBranch !== undefined && developBranch !== '')
157
+ return developBranch;
158
+ out.warning('develop_branch is not set; skipped local cleanup');
159
+ return undefined;
160
+ }
161
+ catch {
162
+ out.warning('cannot determine the worktree; skipped local cleanup');
163
+ return undefined;
164
+ }
165
+ }
166
+ function didRunLocalStep(context, operation, cwd, arguments_, failure) {
167
+ const { out, isDryRun } = context;
168
+ out.planned(operation);
169
+ if (isDryRun)
170
+ return true;
171
+ try {
172
+ runGit(arguments_, { cwd, check: true });
173
+ }
174
+ catch (error) {
175
+ if (!(error instanceof GitCommandError) && !isFileNotFound(error))
176
+ throw error;
177
+ out.warning(`${failure} failed: ${reasonOf(error)}`);
178
+ return false;
179
+ }
180
+ out.done(operation);
181
+ return true;
182
+ }
183
+ function localCleanupAfterMerge(context, headBranch, config, location) {
184
+ const { out } = context;
185
+ const cwd = process.cwd();
186
+ const remote = sameRepositoryRemote(cwd, location, out);
187
+ if (remote === undefined)
188
+ return;
189
+ const currentBranch = gitCurrentBranch(cwd);
190
+ const checkoutTarget = resolveCheckoutTarget(cwd, config, location, out);
191
+ if (checkoutTarget === undefined)
192
+ return;
193
+ const developBranch = getDevelopBranch(config, location.owner, location.repository);
194
+ if (developBranch === undefined || developBranch === '') {
195
+ out.warning('develop_branch is not set; skipped local cleanup');
196
+ return;
197
+ }
198
+ if (currentBranch === checkoutTarget) {
199
+ settle(out, { verb: 'unchanged', target: `current branch ${checkoutTarget}` });
200
+ }
201
+ else if (!didRunLocalStep(context, { verb: 'set', target: `current branch to ${checkoutTarget}` }, cwd, ['checkout', checkoutTarget], `checking out ${checkoutTarget}`)) {
202
+ // checkout が失敗したら、pull と branch -D は別のブランチに当たるので止める
203
+ return;
204
+ }
205
+ didRunLocalStep(context, { verb: 'update', target: `${checkoutTarget} from ${remote}/${developBranch}` }, cwd, ['pull', remote, developBranch], `pulling ${developBranch}`);
206
+ if (headBranch === checkoutTarget) {
207
+ settle(out, { verb: 'skip', target: `deleting local branch ${headBranch} (it is the checkout target)` });
208
+ }
209
+ else {
210
+ didRunLocalStep(context, { verb: 'delete', target: `local branch ${headBranch}` }, cwd, ['branch', '-D', headBranch], `deleting local branch ${headBranch}`);
211
+ }
212
+ }
213
+ function removeActiveFlags(out) {
214
+ try {
215
+ for (const flag of [FLAG_PECOT_ACTIVE, FLAG_PECOT_BASE, FLAG_PECOT_SESSION_TITLE, FLAG_PECOT_QUICK_MODE, FLAG_WIP_BRANCH_LINKED])
216
+ removeFlag(flag);
217
+ }
218
+ catch (error) {
219
+ out.warning(`removing flag files failed: ${reasonOf(error)}`);
220
+ }
221
+ }
222
+ const FinishInputSchema = z.strictObject({
223
+ pr_ref: z.string().describe(PR_REFERENCE_DESCRIPTION),
224
+ dry_run: z.boolean().default(false).describe('Dry-run (do not actually clean up)'),
225
+ delete_branch: z.boolean().default(false).describe('Delete the branch'),
226
+ skip_local_cleanup: z.boolean().default(false).describe('Skip the local branch switch and deletion'),
227
+ });
228
+ async function runFinish(input, io) {
229
+ const out = createOutput(input.dry_run, io);
230
+ const context = { out, isDryRun: input.dry_run };
231
+ const config = requireActiveConfigPlain(out);
232
+ const reference = parseReference(input.pr_ref, input.repo, out);
233
+ const location = { owner: reference.owner, repository: reference.repository };
234
+ const { client, pr } = await fetchPullRequestForCleanup(config, reference, out);
235
+ if (pr.state !== 'MERGED')
236
+ stopWith(out, `PR #${String(reference.number)} is not merged yet (state: ${pr.state}); merge it with gh pr merge first`, 'pr-state');
237
+ const closing = collectClosingIssueNumbers(pr);
238
+ const epicProtected = await findEpicProtectedNumbers(client, location, closing, out);
239
+ for (const number of epicProtected)
240
+ closing.delete(number);
241
+ out.count('Issues to close', closing.size);
242
+ await closeRelatedIssues(context, client, location, closing, epicProtected);
243
+ if (input.delete_branch)
244
+ await deleteMergedBranch(context, client, location, pr.head_ref);
245
+ if (input.skip_local_cleanup)
246
+ return;
247
+ localCleanupAfterMerge(context, pr.head_ref, config, location);
248
+ if (input.dry_run) {
249
+ return;
250
+ }
251
+ removeActiveFlags(out);
252
+ consumeFlag(FLAG_MERGE_SLASH_PENDING);
253
+ }
254
+ const sessionFinishCommand = defineCommand({
255
+ path: ['session', 'finish'],
256
+ description: 'Clean up after a merged PR (close related Issues, delete branches, local cleanup)',
257
+ input: FinishInputSchema,
258
+ positional: ['pr_ref'],
259
+ shortFlags: { dry_run: '-n', delete_branch: '-d' },
260
+ interactive: false,
261
+ run: async (input, io) => {
262
+ await runFinish(input, io);
263
+ return 0;
264
+ },
265
+ });
266
+ export const SESSION_FINISH_COMMANDS = [sessionFinishCommand];
267
+ //# sourceMappingURL=session-finish.js.map
@@ -0,0 +1,3 @@
1
+ import type { CommandDefinition } from './command-definition.js';
2
+ export declare const SESSION_START_COMMANDS: readonly CommandDefinition[];
3
+ //# sourceMappingURL=session-start.d.ts.map