@pecot/workflow-mcp 0.0.0-stage → 5.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 (244) hide show
  1. package/.claude-plugin/plugin.json +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +126 -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 +222 -0
  7. package/cli/dist/commands/cell-table.d.ts +3 -0
  8. package/cli/dist/commands/cell-table.js +495 -0
  9. package/cli/dist/commands/command-binding.d.ts +26 -0
  10. package/cli/dist/commands/command-binding.js +266 -0
  11. package/cli/dist/commands/command-definition.d.ts +136 -0
  12. package/cli/dist/commands/command-definition.js +101 -0
  13. package/cli/dist/commands/command-help.d.ts +8 -0
  14. package/cli/dist/commands/command-help.js +173 -0
  15. package/cli/dist/commands/command-stop.d.ts +19 -0
  16. package/cli/dist/commands/command-stop.js +30 -0
  17. package/cli/dist/commands/dependabot.d.ts +3 -0
  18. package/cli/dist/commands/dependabot.js +165 -0
  19. package/cli/dist/commands/directory-option.d.ts +7 -0
  20. package/cli/dist/commands/directory-option.js +48 -0
  21. package/cli/dist/commands/failure.d.ts +13 -0
  22. package/cli/dist/commands/failure.js +33 -0
  23. package/cli/dist/commands/fuzzy.d.ts +7 -0
  24. package/cli/dist/commands/fuzzy.js +79 -0
  25. package/cli/dist/commands/github-client.d.ts +12 -0
  26. package/cli/dist/commands/github-client.js +23 -0
  27. package/cli/dist/commands/global-options.d.ts +25 -0
  28. package/cli/dist/commands/global-options.js +83 -0
  29. package/cli/dist/commands/help-layout.d.ts +40 -0
  30. package/cli/dist/commands/help-layout.js +117 -0
  31. package/cli/dist/commands/help.d.ts +8 -0
  32. package/cli/dist/commands/help.js +55 -0
  33. package/cli/dist/commands/input-schema.d.ts +14 -0
  34. package/cli/dist/commands/input-schema.js +56 -0
  35. package/cli/dist/commands/issue-board.d.ts +38 -0
  36. package/cli/dist/commands/issue-board.js +62 -0
  37. package/cli/dist/commands/issue-comment.d.ts +3 -0
  38. package/cli/dist/commands/issue-comment.js +196 -0
  39. package/cli/dist/commands/issue-create.d.ts +3 -0
  40. package/cli/dist/commands/issue-create.js +329 -0
  41. package/cli/dist/commands/issue-project.d.ts +3 -0
  42. package/cli/dist/commands/issue-project.js +176 -0
  43. package/cli/dist/commands/issue-quick.d.ts +3 -0
  44. package/cli/dist/commands/issue-quick.js +126 -0
  45. package/cli/dist/commands/issue-read.d.ts +3 -0
  46. package/cli/dist/commands/issue-read.js +80 -0
  47. package/cli/dist/commands/issue-shared.d.ts +159 -0
  48. package/cli/dist/commands/issue-shared.js +419 -0
  49. package/cli/dist/commands/issue.d.ts +4 -0
  50. package/cli/dist/commands/issue.js +15 -0
  51. package/cli/dist/commands/names.d.ts +9 -0
  52. package/cli/dist/commands/names.js +16 -0
  53. package/cli/dist/commands/output.d.ts +78 -0
  54. package/cli/dist/commands/output.js +187 -0
  55. package/cli/dist/commands/pr.d.ts +3 -0
  56. package/cli/dist/commands/pr.js +933 -0
  57. package/cli/dist/commands/project-info.d.ts +48 -0
  58. package/cli/dist/commands/project-info.js +181 -0
  59. package/cli/dist/commands/project-sync.d.ts +91 -0
  60. package/cli/dist/commands/project-sync.js +273 -0
  61. package/cli/dist/commands/project.d.ts +5 -0
  62. package/cli/dist/commands/project.js +677 -0
  63. package/cli/dist/commands/registry.d.ts +6 -0
  64. package/cli/dist/commands/registry.js +27 -0
  65. package/cli/dist/commands/release.d.ts +10 -0
  66. package/cli/dist/commands/release.js +414 -0
  67. package/cli/dist/commands/repository-display.d.ts +3 -0
  68. package/cli/dist/commands/repository-display.js +7 -0
  69. package/cli/dist/commands/repository-match.d.ts +14 -0
  70. package/cli/dist/commands/repository-match.js +33 -0
  71. package/cli/dist/commands/repository.d.ts +5 -0
  72. package/cli/dist/commands/repository.js +265 -0
  73. package/cli/dist/commands/rich-panel.d.ts +22 -0
  74. package/cli/dist/commands/rich-panel.js +295 -0
  75. package/cli/dist/commands/security.d.ts +3 -0
  76. package/cli/dist/commands/security.js +164 -0
  77. package/cli/dist/commands/session.d.ts +3 -0
  78. package/cli/dist/commands/session.js +161 -0
  79. package/cli/dist/commands/tracker-sync.d.ts +58 -0
  80. package/cli/dist/commands/tracker-sync.js +53 -0
  81. package/cli/dist/commands/usage-error.d.ts +47 -0
  82. package/cli/dist/commands/usage-error.js +72 -0
  83. package/cli/dist/commands/value-types.d.ts +46 -0
  84. package/cli/dist/commands/value-types.js +94 -0
  85. package/cli/dist/commands/work-target.d.ts +13 -0
  86. package/cli/dist/commands/work-target.js +41 -0
  87. package/cli/dist/commands/worktree.d.ts +3 -0
  88. package/cli/dist/commands/worktree.js +335 -0
  89. package/cli/dist/domain/api-error-detail.d.ts +10 -0
  90. package/cli/dist/domain/api-error-detail.js +25 -0
  91. package/cli/dist/domain/branch-name.d.ts +5 -0
  92. package/cli/dist/domain/branch-name.js +10 -0
  93. package/cli/dist/domain/changelog.d.ts +43 -0
  94. package/cli/dist/domain/changelog.js +107 -0
  95. package/cli/dist/domain/config.d.ts +215 -0
  96. package/cli/dist/domain/config.js +300 -0
  97. package/cli/dist/domain/dependabot-pull-requests.d.ts +82 -0
  98. package/cli/dist/domain/dependabot-pull-requests.js +338 -0
  99. package/cli/dist/domain/dependabot-source.d.ts +51 -0
  100. package/cli/dist/domain/dependabot-source.js +4 -0
  101. package/cli/dist/domain/dependabot.d.ts +47 -0
  102. package/cli/dist/domain/dependabot.js +88 -0
  103. package/cli/dist/domain/errors.d.ts +19 -0
  104. package/cli/dist/domain/errors.js +31 -0
  105. package/cli/dist/domain/field-options.d.ts +31 -0
  106. package/cli/dist/domain/field-options.js +44 -0
  107. package/cli/dist/domain/format.d.ts +47 -0
  108. package/cli/dist/domain/format.js +168 -0
  109. package/cli/dist/domain/frontmatter.d.ts +122 -0
  110. package/cli/dist/domain/frontmatter.js +264 -0
  111. package/cli/dist/domain/git-runner.d.ts +10 -0
  112. package/cli/dist/domain/git-runner.js +4 -0
  113. package/cli/dist/domain/git-url.d.ts +17 -0
  114. package/cli/dist/domain/git-url.js +30 -0
  115. package/cli/dist/domain/issue-kind.d.ts +16 -0
  116. package/cli/dist/domain/issue-kind.js +16 -0
  117. package/cli/dist/domain/issue-readout.d.ts +107 -0
  118. package/cli/dist/domain/issue-readout.js +4 -0
  119. package/cli/dist/domain/json-null.d.ts +2 -0
  120. package/cli/dist/domain/json-null.js +5 -0
  121. package/cli/dist/domain/json.d.ts +21 -0
  122. package/cli/dist/domain/json.js +47 -0
  123. package/cli/dist/domain/prerequisites.d.ts +32 -0
  124. package/cli/dist/domain/prerequisites.js +192 -0
  125. package/cli/dist/domain/pull-request.d.ts +134 -0
  126. package/cli/dist/domain/pull-request.js +232 -0
  127. package/cli/dist/domain/python-format.d.ts +105 -0
  128. package/cli/dist/domain/python-format.js +304 -0
  129. package/cli/dist/domain/readiness.d.ts +172 -0
  130. package/cli/dist/domain/readiness.js +280 -0
  131. package/cli/dist/domain/reference-parser.d.ts +49 -0
  132. package/cli/dist/domain/reference-parser.js +119 -0
  133. package/cli/dist/domain/release.d.ts +49 -0
  134. package/cli/dist/domain/release.js +72 -0
  135. package/cli/dist/domain/repository-policy.d.ts +32 -0
  136. package/cli/dist/domain/repository-policy.js +65 -0
  137. package/cli/dist/domain/repository-resolver.d.ts +57 -0
  138. package/cli/dist/domain/repository-resolver.js +119 -0
  139. package/cli/dist/domain/sections.d.ts +69 -0
  140. package/cli/dist/domain/sections.js +514 -0
  141. package/cli/dist/domain/security-alerts-source.d.ts +29 -0
  142. package/cli/dist/domain/security-alerts-source.js +4 -0
  143. package/cli/dist/domain/security-alerts.d.ts +52 -0
  144. package/cli/dist/domain/security-alerts.js +107 -0
  145. package/cli/dist/domain/security-tracker.d.ts +115 -0
  146. package/cli/dist/domain/security-tracker.js +353 -0
  147. package/cli/dist/domain/state.d.ts +29 -0
  148. package/cli/dist/domain/state.js +56 -0
  149. package/cli/dist/domain/tracker-body.d.ts +70 -0
  150. package/cli/dist/domain/tracker-body.js +65 -0
  151. package/cli/dist/domain/yaml-document.d.ts +5 -0
  152. package/cli/dist/domain/yaml-document.js +206 -0
  153. package/cli/dist/infra/authored-notes.d.ts +37 -0
  154. package/cli/dist/infra/authored-notes.js +66 -0
  155. package/cli/dist/infra/changelog-file.d.ts +19 -0
  156. package/cli/dist/infra/changelog-file.js +43 -0
  157. package/cli/dist/infra/child-stdin.d.ts +4 -0
  158. package/cli/dist/infra/child-stdin.js +8 -0
  159. package/cli/dist/infra/config-store.d.ts +49 -0
  160. package/cli/dist/infra/config-store.js +121 -0
  161. package/cli/dist/infra/diagnostics.d.ts +9 -0
  162. package/cli/dist/infra/diagnostics.js +2 -0
  163. package/cli/dist/infra/file-kind.d.ts +9 -0
  164. package/cli/dist/infra/file-kind.js +26 -0
  165. package/cli/dist/infra/frontmatter-file.d.ts +14 -0
  166. package/cli/dist/infra/frontmatter-file.js +30 -0
  167. package/cli/dist/infra/git.d.ts +74 -0
  168. package/cli/dist/infra/git.js +133 -0
  169. package/cli/dist/infra/github/auth.d.ts +60 -0
  170. package/cli/dist/infra/github/auth.js +158 -0
  171. package/cli/dist/infra/github/client.d.ts +173 -0
  172. package/cli/dist/infra/github/client.js +576 -0
  173. package/cli/dist/infra/github/dependabot-source.d.ts +13 -0
  174. package/cli/dist/infra/github/dependabot-source.js +33 -0
  175. package/cli/dist/infra/github/errors.d.ts +47 -0
  176. package/cli/dist/infra/github/errors.js +42 -0
  177. package/cli/dist/infra/github/index.d.ts +13 -0
  178. package/cli/dist/infra/github/index.js +19 -0
  179. package/cli/dist/infra/github/issues.d.ts +129 -0
  180. package/cli/dist/infra/github/issues.js +273 -0
  181. package/cli/dist/infra/github/payload.d.ts +44 -0
  182. package/cli/dist/infra/github/payload.js +80 -0
  183. package/cli/dist/infra/github/projects.d.ts +108 -0
  184. package/cli/dist/infra/github/projects.js +303 -0
  185. package/cli/dist/infra/github/pulls.d.ts +80 -0
  186. package/cli/dist/infra/github/pulls.js +161 -0
  187. package/cli/dist/infra/github/queries.d.ts +36 -0
  188. package/cli/dist/infra/github/queries.js +883 -0
  189. package/cli/dist/infra/github/readout.d.ts +18 -0
  190. package/cli/dist/infra/github/readout.js +156 -0
  191. package/cli/dist/infra/github/repos.d.ts +68 -0
  192. package/cli/dist/infra/github/repos.js +137 -0
  193. package/cli/dist/infra/github/rest-path.d.ts +14 -0
  194. package/cli/dist/infra/github/rest-path.js +31 -0
  195. package/cli/dist/infra/github/security-alerts-source.d.ts +15 -0
  196. package/cli/dist/infra/github/security-alerts-source.js +73 -0
  197. package/cli/dist/infra/gitignore-spec.d.ts +36 -0
  198. package/cli/dist/infra/gitignore-spec.js +235 -0
  199. package/cli/dist/infra/nx-scope.d.ts +51 -0
  200. package/cli/dist/infra/nx-scope.js +182 -0
  201. package/cli/dist/infra/os-error.d.ts +24 -0
  202. package/cli/dist/infra/os-error.js +38 -0
  203. package/cli/dist/infra/paths.d.ts +88 -0
  204. package/cli/dist/infra/paths.js +234 -0
  205. package/cli/dist/infra/state-store.d.ts +45 -0
  206. package/cli/dist/infra/state-store.js +118 -0
  207. package/cli/dist/infra/template-store.d.ts +15 -0
  208. package/cli/dist/infra/template-store.js +34 -0
  209. package/cli/dist/infra/text-file.d.ts +12 -0
  210. package/cli/dist/infra/text-file.js +33 -0
  211. package/cli/dist/infra/worktree-copy.d.ts +24 -0
  212. package/cli/dist/infra/worktree-copy.js +95 -0
  213. package/cli/dist/infra/worktree-links.d.ts +139 -0
  214. package/cli/dist/infra/worktree-links.js +433 -0
  215. package/cli/dist/infra/worktree-setup.d.ts +21 -0
  216. package/cli/dist/infra/worktree-setup.js +41 -0
  217. package/cli/dist/infra/worktree.d.ts +174 -0
  218. package/cli/dist/infra/worktree.js +567 -0
  219. package/cli/dist/mcp/result.d.ts +33 -0
  220. package/cli/dist/mcp/result.js +52 -0
  221. package/cli/dist/mcp/serve.d.ts +51 -0
  222. package/cli/dist/mcp/serve.js +183 -0
  223. package/cli/dist/mcp/server.d.ts +21 -0
  224. package/cli/dist/mcp/server.js +128 -0
  225. package/cli/dist/mcp/session-directory.d.ts +26 -0
  226. package/cli/dist/mcp/session-directory.js +84 -0
  227. package/cli/dist/package.json +1 -0
  228. package/cli/dist/version.d.ts +17 -0
  229. package/cli/dist/version.js +42 -0
  230. package/cli/templates/comment/merge-diff.md +34 -0
  231. package/cli/templates/comment/plan.md +181 -0
  232. package/cli/templates/comment/precede-notify.md +33 -0
  233. package/cli/templates/comment/review-findings.md +42 -0
  234. package/cli/templates/comment/review-resolution.md +48 -0
  235. package/cli/templates/issue/bug.md +63 -0
  236. package/cli/templates/issue/epic.md +54 -0
  237. package/cli/templates/issue/task-implement.md +67 -0
  238. package/cli/templates/issue/task.md +48 -0
  239. package/cli/templates/issue/tracker.md +48 -0
  240. package/cli/templates/pr/create.md +123 -0
  241. package/cli/templates/pr/release.md +56 -0
  242. package/hooks/lib/core/pecot-gitignore-ensure.sh +48 -0
  243. package/npm-shrinkwrap.json +83 -0
  244. package/package.json +22 -4
@@ -0,0 +1,234 @@
1
+ /* eslint-disable security/detect-non-literal-fs-filename -- パスは呼び出し側が決める。ここは解決と予約だけ */
2
+ // pecot リポジトリ基準のパス解決。`.pecot/` 配下のパス定義の唯一の置き場。
3
+ // projectRoot() でプロジェクトルートを解決し、一時ファイルの出力先を固定する。
4
+ import { closeSync, existsSync, mkdirSync, openSync, realpathSync, unlinkSync } from 'node:fs';
5
+ import path from 'node:path';
6
+ import { PecotError, ProjectRootNotFoundError } from '../domain/errors.js';
7
+ import { gitToplevel } from './git.js';
8
+ import { isFileNotFound, hasErrorCode } from './os-error.js';
9
+ // プロジェクトルート基準の相対パス
10
+ export const PECOT_DIR = '.pecot';
11
+ // フック状態の保存先
12
+ export const STATE_DIR = `${PECOT_DIR}/state`;
13
+ // 人・エージェントの作業ファイル置き場(タスク ID = Issue 番号単位)
14
+ export const WORK_DIR = `${PECOT_DIR}/work`;
15
+ // 設定ファイルの置き場
16
+ export const CONFIG_DIR = `${PECOT_DIR}/config`;
17
+ // CLI / hooks 専用の一時ファイル置き場
18
+ export const TMP_DIR = `${PECOT_DIR}/tmp`;
19
+ // CLI が生成する一時ファイルの出力先
20
+ export const OUTPUT_DIR = `${TMP_DIR}/output`;
21
+ // .pecot/ 配下の生成物を git 管理外にする ignore(hook と pecot worktree が生成)
22
+ export const PECOT_GITIGNORE_FILENAME = `${PECOT_DIR}/.gitignore`;
23
+ // ワークツリー設定(symlink / setup_command)
24
+ export const WORKTREE_CONFIG_FILENAME = `${CONFIG_DIR}/worktree-config.json`;
25
+ // ワークツリー作成時のコピー除外パターン(gitignore 形式)
26
+ export const WORKTREE_IGNORE_FILENAME = `${CONFIG_DIR}/.worktreeignore`;
27
+ // フック設定
28
+ export const HOOKS_CONFIG_FILENAME = `${CONFIG_DIR}/hooks-config.json`;
29
+ // アクティブなプロジェクト設定のポインタ
30
+ export const ACTIVE_PROJECT_FILENAME = `${CONFIG_DIR}/projects/.active`;
31
+ /**
32
+ 候補名を使い切っても出力パスを予約できなかった
33
+ */
34
+ export class OutputPathReservationError extends PecotError {
35
+ }
36
+ /**
37
+ パス文字列を Python の Path と同じ形にする(空のセグメントと `.` を落とし、末尾のスラッシュを落とす)
38
+ */
39
+ export function normalizePath(text) {
40
+ const segments = text.split('/').filter(segment => segment !== '' && segment !== '.');
41
+ return `${text.startsWith('/') ? '/' : ''}${segments.join('/')}` || '.';
42
+ }
43
+ /**
44
+ シンボリックリンクを解決した絶対パス。存在しない部分は、存在する最も深い親を解決して字句どおりにつなぐ(Python の Path.resolve())
45
+ */
46
+ export function resolveRealPath(target) {
47
+ const absolute = path.resolve(target);
48
+ try {
49
+ return realpathSync(absolute);
50
+ }
51
+ catch (error) {
52
+ const parent = path.dirname(absolute);
53
+ if (parent !== absolute && isFileNotFound(error))
54
+ return path.join(resolveRealPath(parent), path.basename(absolute));
55
+ throw error;
56
+ }
57
+ }
58
+ /**
59
+ cwd がセッションの worktree の .pecot/work の実体配下なら、その worktree を返す。
60
+ セッション開始時の SessionStart hook が PECOT_SESSION_PROJECT_DIR に書いた値を起点にする。git は呼ばない。
61
+ hooks の project-root.sh と同じ判定を使う。cwd の文字列が `.pecot/work` で終わるか `.pecot/work/` を含み、実体が起点の .pecot/work の実体配下にある。
62
+ かつ途中に `.git` を持つディレクトリ(入れ子の git リポ)が無いときだけ対象にする
63
+ */
64
+ function sessionWorkOwner(cwd) {
65
+ const sessionDirectory = process.env['PECOT_SESSION_PROJECT_DIR'];
66
+ if (sessionDirectory === undefined || sessionDirectory === '')
67
+ return undefined;
68
+ // 安価な文字列の判定を、realpath の解決より先に行う
69
+ if (!(cwd.endsWith(`/${WORK_DIR}`) || cwd.includes(`/${WORK_DIR}/`)))
70
+ return undefined;
71
+ let workReal;
72
+ let cwdReal;
73
+ try {
74
+ workReal = resolveRealPath(path.join(sessionDirectory, WORK_DIR));
75
+ cwdReal = resolveRealPath(cwd);
76
+ }
77
+ catch (error) {
78
+ if (hasErrorCode(error))
79
+ return undefined;
80
+ throw error;
81
+ }
82
+ if (cwdReal !== workReal && !cwdReal.startsWith(`${workReal}/`))
83
+ return undefined;
84
+ for (let directory = cwdReal;; directory = path.dirname(directory)) {
85
+ if (directory === workReal)
86
+ break;
87
+ if (existsSync(path.join(directory, '.git')))
88
+ return undefined;
89
+ if (path.dirname(directory) === directory)
90
+ break;
91
+ }
92
+ return resolveRealPath(sessionDirectory);
93
+ }
94
+ function currentDirectory(options) {
95
+ try {
96
+ return options.cwd ?? process.cwd();
97
+ }
98
+ catch (error) {
99
+ // 作業ディレクトリが消えているとき
100
+ if (hasErrorCode(error))
101
+ return undefined;
102
+ throw error;
103
+ }
104
+ }
105
+ /**
106
+ プロジェクトルートを取得する。
107
+ 1. CLAUDE_PROJECT_DIR があれば使う(テスト上書き用)。
108
+ 2. cwd が PECOT_SESSION_PROJECT_DIR の .pecot/work 配下(入れ子の git リポを除く)なら、その実体パス。
109
+ worktree の .pecot/work は base への symlink で、そこでは git toplevel が base になる。
110
+ 3. なければ git rev-parse --show-toplevel で解決する(GIT_* を除いて実行する)。
111
+
112
+ どれも得られなければ ProjectRootNotFoundError
113
+ */
114
+ export function projectRoot(options = {}) {
115
+ const fromEnvironment = process.env['CLAUDE_PROJECT_DIR'];
116
+ if (fromEnvironment !== undefined && fromEnvironment !== '')
117
+ return normalizePath(fromEnvironment);
118
+ const cwd = currentDirectory(options);
119
+ if (cwd !== undefined) {
120
+ const sessionRoot = sessionWorkOwner(cwd);
121
+ if (sessionRoot !== undefined)
122
+ return sessionRoot;
123
+ }
124
+ // git push 等のフック内では GIT_DIR 等が設定され、作業ディレクトリの代わりに参照される。
125
+ // そのため GIT_* を除いて作業ディレクトリ基準で解決する
126
+ const toplevel = gitToplevel({ cwd, stripGitEnvironment: true });
127
+ if (toplevel !== undefined)
128
+ return toplevel;
129
+ throw new ProjectRootNotFoundError('project root not found: run inside a git repository or set the CLAUDE_PROJECT_DIR environment variable');
130
+ }
131
+ /**
132
+ 出力ディレクトリ(`{プロジェクトルート}/.pecot/tmp/output`)
133
+ */
134
+ export function outputDirectory(options = {}) {
135
+ return path.join(projectRoot(options), OUTPUT_DIR);
136
+ }
137
+ /**
138
+ `YYYYMMDD-HHMMSS`(ローカル時刻)。reserveOutputPath の nameFor に渡す形
139
+ */
140
+ export function formatTimestamp(milliseconds) {
141
+ const date = new Date(milliseconds);
142
+ const two = (value) => String(value).padStart(2, '0');
143
+ return `${String(date.getFullYear())}${two(date.getMonth() + 1)}${two(date.getDate())}-${two(date.getHours())}${two(date.getMinutes())}${two(date.getSeconds())}`;
144
+ }
145
+ /**
146
+ outputDirectory 配下に空ファイルを排他作成して返す。
147
+ nameFor には `YYYYMMDD-HHMMSS` 形式の文字列を渡し、返ってきた名前で作る。既に同名があれば 1 秒進めた候補で再試行する。
148
+ 時刻は生成物の一意性と生成順の目安であり、実時刻の記録ではない。衝突した分だけ実時刻より先に進む。
149
+ 候補をすべて使い切ったら OutputPathReservationError
150
+ */
151
+ export function reserveOutputPath(output, nameFor, options = {}) {
152
+ const { maxAttempts = 60, now = () => new Date() } = options;
153
+ mkdirSync(output, { recursive: true });
154
+ const baseMilliseconds = now().getTime();
155
+ let candidate = output;
156
+ for (let offset = 0; offset < maxAttempts; offset += 1) {
157
+ const timestamp = formatTimestamp(baseMilliseconds + offset * 1000);
158
+ candidate = path.join(output, nameFor(timestamp));
159
+ try {
160
+ const descriptor = openSync(candidate, 'wx');
161
+ closeSync(descriptor);
162
+ }
163
+ catch (error) {
164
+ if (hasErrorCode(error) && error.code === 'EEXIST')
165
+ continue;
166
+ throw error;
167
+ }
168
+ return candidate;
169
+ }
170
+ throw new OutputPathReservationError(`could not reserve an output path: all ${String(maxAttempts)} candidate names are in use (last candidate: ${candidate})`);
171
+ }
172
+ /**
173
+ reserveOutputPath で予約したパスを本文に渡し、本文が例外を投げたら予約を撤去する。
174
+ 予約から書き込みまでの間で異常終了しても、0 バイトのファイルを出力先へ残さない。正常に終われば何もしない
175
+ */
176
+ export function withReservedOutputPath(output, nameFor, body, options = {}) {
177
+ const reserved = reserveOutputPath(output, nameFor, options);
178
+ try {
179
+ return body(reserved);
180
+ }
181
+ catch (error) {
182
+ removeQuietly(reserved);
183
+ throw error;
184
+ }
185
+ }
186
+ /**
187
+ withReservedOutputPath の、本文が非同期のもの
188
+ */
189
+ export async function withReservedOutputPathAsync(output, nameFor, body, options = {}) {
190
+ const reserved = reserveOutputPath(output, nameFor, options);
191
+ try {
192
+ return await body(reserved);
193
+ }
194
+ catch (error) {
195
+ removeQuietly(reserved);
196
+ throw error;
197
+ }
198
+ }
199
+ // 後始末の失敗は握る。本文が投げた元の例外を隠さないため
200
+ function removeQuietly(target) {
201
+ try {
202
+ unlinkSync(target);
203
+ }
204
+ catch {
205
+ // 撤去できなくても、元の例外を優先する
206
+ }
207
+ }
208
+ function normalizedDataDirectory(raw) {
209
+ const value = raw?.replace(/[\r\n]+$/u, '');
210
+ return value === undefined || value === '' || value.startsWith('${') || !value.startsWith('/') ? undefined : value;
211
+ }
212
+ const DATA_DIRECTORY_PROJECT = 'pecot-workflow';
213
+ /**
214
+ データ置き場。作らずに値を返す。有効な HOME と有効な XDG_DATA_HOME がどちらも無いときは undefined。
215
+
216
+ bin/_lib.sh の plugin_data_dir_resolve と同じ解決順で、同じ入力なら同じ結果になる。
217
+ この対応は infra/tests/paths.test.ts が固定する。
218
+ 順序は CLAUDE_PLUGIN_DATA、PLUGIN_DATA、XDG_DATA_HOME、HOME。
219
+ 有効な値は、空でない絶対パスだけ。
220
+ 先頭が変数展開の書式(ドル記号と開き波括弧)の値は無効とする。
221
+ 末尾の改行と CR は落とす。
222
+ XDG_DATA_HOME の下は `pecot/pecot-workflow`、HOME の下は `.local/share/pecot/pecot-workflow`
223
+ */
224
+ export function dataDirectory() {
225
+ const client = normalizedDataDirectory(process.env['CLAUDE_PLUGIN_DATA']) ?? normalizedDataDirectory(process.env['PLUGIN_DATA']);
226
+ if (client !== undefined)
227
+ return client;
228
+ const xdg = normalizedDataDirectory(process.env['XDG_DATA_HOME']);
229
+ if (xdg !== undefined)
230
+ return `${xdg}/pecot/${DATA_DIRECTORY_PROJECT}`;
231
+ const home = normalizedDataDirectory(process.env['HOME']);
232
+ return home === undefined ? undefined : `${home}/.local/share/pecot/${DATA_DIRECTORY_PROJECT}`;
233
+ }
234
+ //# sourceMappingURL=paths.js.map
@@ -0,0 +1,45 @@
1
+ export interface StateDirectoryOptions {
2
+ /**
3
+ true なら、無ければ作る
4
+ */
5
+ readonly create?: boolean | undefined;
6
+ }
7
+ /**
8
+ 状態ディレクトリ(`{プロジェクトルート}/.pecot/state`)
9
+ */
10
+ export declare function getStateDirectory(options?: Readonly<StateDirectoryOptions>): string;
11
+ /**
12
+ 作業ディレクトリ `.pecot/work/<Issue番号>/` を作って返す。
13
+ .pecot-active へ作業対象を記録するタイミング(create-branch / issue quick / session restore)で呼ぶ。
14
+ 作れないとき(プロジェクトルート不明・OS のエラー)は undefined。作業フロー自体は止めない
15
+ */
16
+ export declare function ensureWorkDirectory(issueReference: string): string | undefined;
17
+ /**
18
+ フラグファイルを作る(あれば更新時刻を更新する)
19
+ */
20
+ export declare function touchFlag(flagName: string): void;
21
+ /**
22
+ フラグファイルを消す。無くてもエラーにならない
23
+ */
24
+ export declare function removeFlag(flagName: string): void;
25
+ /**
26
+ 実投稿成功時にスラッシュコマンド免除フラグを消費(削除)する。
27
+ 投稿自体は成功しているため、フラグ解決の失敗(git リポ外等)でコマンドを失敗させない
28
+ */
29
+ export declare function consumeFlag(flagName: string): void;
30
+ /**
31
+ 通常のファイルとして存在するか
32
+ */
33
+ export declare function hasFlag(flagName: string): boolean;
34
+ export declare function writeFlag(flagName: string, content: string): void;
35
+ /**
36
+ フラグファイルの内容(前後の空白を除く)。通常のファイルでなければ undefined。
37
+ 存在の確認と読み取りの間に hooks が消しても、読んだ結果が無ければ undefined になる
38
+ */
39
+ export declare function readFlag(flagName: string): string | undefined;
40
+ /**
41
+ 作業対象を記録する(.pecot-active と .pecot-session-title を同時に書く)。
42
+ issueReference の末尾が整数でなければ InvalidValueError で、何も書かない
43
+ */
44
+ export declare function writeWorkTarget(issueReference: string, title: string): void;
45
+ //# sourceMappingURL=state-store.d.ts.map
@@ -0,0 +1,118 @@
1
+ /* eslint-disable security/detect-non-literal-fs-filename -- パスはプロジェクトルート配下の固定の相対パスと、呼び出し側のフラグ名 */
2
+ // フック状態(`.pecot/state/` のフラグファイル)と作業ディレクトリ(`.pecot/work/<Issue番号>/`)の読み書き。規則は domain/state.ts にある。
3
+ import { closeSync, mkdirSync, openSync, statSync, unlinkSync, utimesSync, writeFileSync } from 'node:fs';
4
+ import path from 'node:path';
5
+ import { ProjectRootNotFoundError } from '../domain/errors.js';
6
+ import { pythonStrip } from '../domain/python-format.js';
7
+ import { FLAG_PECOT_ACTIVE, FLAG_PECOT_SESSION_TITLE, planWorkTarget, workDirectoryName } from '../domain/state.js';
8
+ import { OsError, hasErrorCode, isFileNotFound, isSystemCallError } from './os-error.js';
9
+ import { projectRoot, STATE_DIR, WORK_DIR } from './paths.js';
10
+ import { readTextFile } from './text-file.js';
11
+ /**
12
+ 状態ディレクトリ(`{プロジェクトルート}/.pecot/state`)
13
+ */
14
+ export function getStateDirectory(options = {}) {
15
+ const stateDirectory = path.join(projectRoot(), STATE_DIR);
16
+ if (options.create === true)
17
+ mkdirSync(stateDirectory, { recursive: true });
18
+ return stateDirectory;
19
+ }
20
+ /**
21
+ 作業ディレクトリ `.pecot/work/<Issue番号>/` を作って返す。
22
+ .pecot-active へ作業対象を記録するタイミング(create-branch / issue quick / session restore)で呼ぶ。
23
+ 作れないとき(プロジェクトルート不明・OS のエラー)は undefined。作業フロー自体は止めない
24
+ */
25
+ export function ensureWorkDirectory(issueReference) {
26
+ const number = workDirectoryName(issueReference);
27
+ if (number === undefined)
28
+ return undefined;
29
+ try {
30
+ const workDirectory = path.join(projectRoot(), WORK_DIR, number);
31
+ mkdirSync(workDirectory, { recursive: true });
32
+ return workDirectory;
33
+ }
34
+ catch (error) {
35
+ if (error instanceof ProjectRootNotFoundError || hasErrorCode(error))
36
+ return undefined;
37
+ throw error;
38
+ }
39
+ }
40
+ /**
41
+ フラグファイルを作る(あれば更新時刻を更新する)
42
+ */
43
+ export function touchFlag(flagName) {
44
+ const flagFile = path.join(getStateDirectory({ create: true }), flagName);
45
+ closeSync(openSync(flagFile, 'a'));
46
+ const now = new Date();
47
+ utimesSync(flagFile, now, now);
48
+ }
49
+ /**
50
+ フラグファイルを消す。無くてもエラーにならない
51
+ */
52
+ export function removeFlag(flagName) {
53
+ try {
54
+ unlinkSync(path.join(getStateDirectory(), flagName));
55
+ }
56
+ catch (error) {
57
+ if (!isFileNotFound(error))
58
+ throw error;
59
+ }
60
+ }
61
+ /**
62
+ 実投稿成功時にスラッシュコマンド免除フラグを消費(削除)する。
63
+ 投稿自体は成功しているため、フラグ解決の失敗(git リポ外等)でコマンドを失敗させない
64
+ */
65
+ export function consumeFlag(flagName) {
66
+ try {
67
+ removeFlag(flagName);
68
+ }
69
+ catch (error) {
70
+ // 握るのは OS の失敗とプロジェクトルート不明だけ。投稿は成功しているので、後始末の失敗でコマンドを失敗させない。
71
+ // プログラムの誤り(TypeError など)は握らない
72
+ if (error instanceof ProjectRootNotFoundError || isSystemCallError(error))
73
+ return;
74
+ throw error;
75
+ }
76
+ }
77
+ /**
78
+ 通常のファイルとして存在するか
79
+ */
80
+ export function hasFlag(flagName) {
81
+ try {
82
+ return statSync(path.join(getStateDirectory(), flagName)).isFile();
83
+ }
84
+ catch (error) {
85
+ if (hasErrorCode(error))
86
+ return false;
87
+ throw error;
88
+ }
89
+ }
90
+ export function writeFlag(flagName, content) {
91
+ writeFileSync(path.join(getStateDirectory({ create: true }), flagName), content);
92
+ }
93
+ /**
94
+ フラグファイルの内容(前後の空白を除く)。通常のファイルでなければ undefined。
95
+ 存在の確認と読み取りの間に hooks が消しても、読んだ結果が無ければ undefined になる
96
+ */
97
+ export function readFlag(flagName) {
98
+ const flagFile = path.join(getStateDirectory(), flagName);
99
+ try {
100
+ return pythonStrip(readTextFile(flagFile));
101
+ }
102
+ catch (error) {
103
+ // 無い・ディレクトリは、通常のファイルではないので undefined
104
+ if (error instanceof OsError && (error.code === 'ENOENT' || error.code === 'EISDIR'))
105
+ return undefined;
106
+ throw error;
107
+ }
108
+ }
109
+ /**
110
+ 作業対象を記録する(.pecot-active と .pecot-session-title を同時に書く)。
111
+ issueReference の末尾が整数でなければ InvalidValueError で、何も書かない
112
+ */
113
+ export function writeWorkTarget(issueReference, title) {
114
+ const target = planWorkTarget(issueReference, title);
115
+ writeFlag(FLAG_PECOT_ACTIVE, target.active);
116
+ writeFlag(FLAG_PECOT_SESSION_TITLE, target.sessionTitle);
117
+ }
118
+ //# sourceMappingURL=state-store.js.map
@@ -0,0 +1,15 @@
1
+ import type { SectionHeading } from '../domain/sections.js';
2
+ export type TemplateKind = 'issue' | 'comment' | 'pr';
3
+ /**
4
+ 種別ごとのテンプレートのディレクトリ
5
+ */
6
+ export declare function templatesDirectory(kind: TemplateKind): string;
7
+ /**
8
+ 種別と名前(拡張子なし)から決まるテンプレートのパス
9
+ */
10
+ export declare function templateFile(kind: TemplateKind, name: string): string;
11
+ /**
12
+ テンプレートファイルから必須セクションを抽出する。ファイルが無い・読めないときは空
13
+ */
14
+ export declare function readTemplateSections(file: string): readonly SectionHeading[];
15
+ //# sourceMappingURL=template-store.d.ts.map
@@ -0,0 +1,34 @@
1
+ /* eslint-disable security/detect-non-literal-fs-filename -- パスは呼び出し側が決める。この層は読むだけ */
2
+ // 配布テンプレート(plugins/pecot-workflow/cli/templates/)の読み出し。
3
+ // 見出しの抽出そのものは domain/sections.ts が行い、ここはファイルの位置と読み込みを受け持つ。
4
+ import { existsSync } from 'node:fs';
5
+ import path from 'node:path';
6
+ import { extractSectionsFromTemplate } from '../domain/sections.js';
7
+ import { pluginRoot } from '../version.js';
8
+ import { readTextFile } from './text-file.js';
9
+ /**
10
+ 種別ごとのテンプレートのディレクトリ
11
+ */
12
+ export function templatesDirectory(kind) {
13
+ return path.join(pluginRoot(), 'cli', 'templates', kind);
14
+ }
15
+ /**
16
+ 種別と名前(拡張子なし)から決まるテンプレートのパス
17
+ */
18
+ export function templateFile(kind, name) {
19
+ return path.join(templatesDirectory(kind), `${name}.md`);
20
+ }
21
+ /**
22
+ テンプレートファイルから必須セクションを抽出する。ファイルが無い・読めないときは空
23
+ */
24
+ export function readTemplateSections(file) {
25
+ if (!existsSync(file))
26
+ return [];
27
+ try {
28
+ return extractSectionsFromTemplate(readTextFile(file));
29
+ }
30
+ catch {
31
+ return [];
32
+ }
33
+ }
34
+ //# sourceMappingURL=template-store.js.map
@@ -0,0 +1,12 @@
1
+ import { BaseError } from '../domain/errors.js';
2
+ /**
3
+ UTF-8 として読めないファイル。Python の UnicodeDecodeError に当たり、OsError ではない
4
+ */
5
+ export declare class TextDecodeError extends BaseError {
6
+ }
7
+ /**
8
+ UTF-8 で読み、改行(\r\n・\r)は \n に揃える。読めなければ Python の OSError と同じ文の OsError。
9
+ 不正な UTF-8 は TextDecodeError
10
+ */
11
+ export declare function readTextFile(file: string): string;
12
+ //# sourceMappingURL=text-file.d.ts.map
@@ -0,0 +1,33 @@
1
+ /* eslint-disable security/detect-non-literal-fs-filename -- パスは呼び出し側が決める。この関数は読むだけ */
2
+ // テキストファイルの読み書き。Python の `read_text()`(UTF-8・universal newlines)と同じ挙動にする。
3
+ import { readFileSync } from 'node:fs';
4
+ import { BaseError } from '../domain/errors.js';
5
+ import { normalizeNewlines } from '../domain/python-format.js';
6
+ import { toOsError } from './os-error.js';
7
+ // BOM は Python の utf-8 と同じく残す。不正なバイトは置き換えず例外にする
8
+ const UTF8_DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
9
+ /**
10
+ UTF-8 として読めないファイル。Python の UnicodeDecodeError に当たり、OsError ではない
11
+ */
12
+ export class TextDecodeError extends BaseError {
13
+ }
14
+ /**
15
+ UTF-8 で読み、改行(\r\n・\r)は \n に揃える。読めなければ Python の OSError と同じ文の OsError。
16
+ 不正な UTF-8 は TextDecodeError
17
+ */
18
+ export function readTextFile(file) {
19
+ let bytes;
20
+ try {
21
+ bytes = readFileSync(file);
22
+ }
23
+ catch (error) {
24
+ throw toOsError(error, file);
25
+ }
26
+ try {
27
+ return normalizeNewlines(UTF8_DECODER.decode(bytes));
28
+ }
29
+ catch (error) {
30
+ throw new TextDecodeError(`'utf-8' codec can't decode file: ${file}`, { cause: error });
31
+ }
32
+ }
33
+ //# sourceMappingURL=text-file.js.map
@@ -0,0 +1,24 @@
1
+ import { BaseError } from '../domain/errors.js';
2
+ export interface CopyResult {
3
+ readonly source: string;
4
+ readonly dest: string;
5
+ readonly action: 'copied' | 'skipped' | 'error';
6
+ /**
7
+ 追加メッセージ(エラー詳細など)。無ければ空
8
+ */
9
+ readonly message: string;
10
+ }
11
+ /**
12
+ ディレクトリの中身を写せないエントリがあった(Python の shutil.Error に当たる)。
13
+ メッセージは、(コピー元, コピー先, 理由) の一覧の repr
14
+ */
15
+ export declare class TreeCopyError extends BaseError {
16
+ constructor(failures: readonly Failure[]);
17
+ }
18
+ type Failure = readonly [string, string, string];
19
+ /**
20
+ ファイルをコピーする。files はコピーする相対パス。ソースが無ければ skipped、OS のエラーなら error として結果に載せる
21
+ */
22
+ export declare function copyFiles(sourceRoot: string, destinationRoot: string, files: readonly string[]): readonly CopyResult[];
23
+ export {};
24
+ //# sourceMappingURL=worktree-copy.d.ts.map
@@ -0,0 +1,95 @@
1
+ /* eslint-disable security/detect-non-literal-fs-filename -- パスは呼び出し側が渡すリポジトリ・ワークツリー配下 */
2
+ // 未追跡ファイルのワークツリーへのコピー。Python の shutil.copy2・copytree と同じ挙動にする。
3
+ //
4
+ // 更新時刻は、ナノ秒の stat から秒の小数に直して写す。
5
+ // 秒の小数は 1.6e9 前後で 0.24 マイクロ秒の刻みになるので、Python の copy2 とは最大でこの刻みの差が出る。
6
+ // ディレクトリは、リンク先の中身を写す。
7
+ // 写せないエントリがあっても残りを写し終えてから、まとめてエラーにする(copytree と同じ)。
8
+ import { BaseError } from '../domain/errors.js';
9
+ import { chmodSync, copyFileSync, existsSync, mkdirSync, readdirSync, statSync, utimesSync } from 'node:fs';
10
+ import path from 'node:path';
11
+ import { pythonStringRepr } from '../domain/python-format.js';
12
+ import { toOsError } from './os-error.js';
13
+ /**
14
+ ディレクトリの中身を写せないエントリがあった(Python の shutil.Error に当たる)。
15
+ メッセージは、(コピー元, コピー先, 理由) の一覧の repr
16
+ */
17
+ export class TreeCopyError extends BaseError {
18
+ constructor(failures) {
19
+ super(`[${failures.map(([source, destination, reason]) => `(${pythonStringRepr(source)}, ${pythonStringRepr(destination)}, ${pythonStringRepr(reason)})`).join(', ')}]`);
20
+ }
21
+ }
22
+ function messageOf(error) {
23
+ const converted = toOsError(error);
24
+ return converted instanceof Error ? converted.message : String(converted);
25
+ }
26
+ // 権限と更新時刻を写す(shutil.copystat)
27
+ function copyStatus(source, destination) {
28
+ const stat = statSync(source, { bigint: true });
29
+ chmodSync(destination, Number(stat.mode & 4095n));
30
+ utimesSync(destination, Number(stat.atimeNs) / 1e9, Number(stat.mtimeNs) / 1e9);
31
+ }
32
+ // リンク先の中身を写す(shutil.copy2 の follow_symlinks=True)。ソースを先に調べるのは、エラーの文を Python に合わせるため
33
+ function copyFileWithStatus(source, destination) {
34
+ statSync(source);
35
+ copyFileSync(source, destination);
36
+ copyStatus(source, destination);
37
+ }
38
+ // 写せなかったエントリの一覧を返す。ディレクトリ自体を作れなければ例外
39
+ function copyTree(source, destination) {
40
+ mkdirSync(destination, { recursive: true });
41
+ const failures = [];
42
+ for (const name of readdirSync(source)) {
43
+ const sourceEntry = path.join(source, name);
44
+ const destinationEntry = path.join(destination, name);
45
+ try {
46
+ if (statSyncOrUndefined(sourceEntry)?.isDirectory() === true)
47
+ failures.push(...copyTree(sourceEntry, destinationEntry));
48
+ else
49
+ copyFileWithStatus(sourceEntry, destinationEntry);
50
+ }
51
+ catch (error) {
52
+ failures.push([sourceEntry, destinationEntry, messageOf(error)]);
53
+ }
54
+ }
55
+ copyStatus(source, destination);
56
+ return failures;
57
+ }
58
+ function statSyncOrUndefined(target) {
59
+ try {
60
+ return statSync(target);
61
+ }
62
+ catch {
63
+ return undefined;
64
+ }
65
+ }
66
+ function copyOne(source, destination) {
67
+ mkdirSync(path.dirname(destination), { recursive: true });
68
+ if (statSync(source).isDirectory()) {
69
+ const failures = copyTree(source, destination);
70
+ if (failures.length > 0)
71
+ throw new TreeCopyError(failures);
72
+ return;
73
+ }
74
+ copyFileWithStatus(source, destination);
75
+ }
76
+ /**
77
+ ファイルをコピーする。files はコピーする相対パス。ソースが無ければ skipped、OS のエラーなら error として結果に載せる
78
+ */
79
+ export function copyFiles(sourceRoot, destinationRoot, files) {
80
+ return files.map((file) => {
81
+ const source = path.join(sourceRoot, file);
82
+ const destination = path.join(destinationRoot, file);
83
+ if (!existsSync(source))
84
+ return { source, dest: destination, action: 'skipped', message: 'source file does not exist' };
85
+ try {
86
+ copyOne(source, destination);
87
+ return { source, dest: destination, action: 'copied', message: '' };
88
+ }
89
+ catch (error) {
90
+ // copy が投げるのはシステムエラーと TreeCopyError だけ。Python の `except OSError` と同じく、結果に載せて続ける
91
+ return { source, dest: destination, action: 'error', message: messageOf(error) };
92
+ }
93
+ });
94
+ }
95
+ //# sourceMappingURL=worktree-copy.js.map