hermes-pilot 0.0.0-stage → 0.2.2

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 (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja.md +419 -0
  3. package/README.md +428 -2
  4. package/__init__.py +15 -0
  5. package/apps/bridge/src/api-errors.js +165 -0
  6. package/apps/bridge/src/architect.js +1298 -0
  7. package/apps/bridge/src/auth.js +202 -0
  8. package/apps/bridge/src/capabilities.js +256 -0
  9. package/apps/bridge/src/capture.js +145 -0
  10. package/apps/bridge/src/chat-profile.js +95 -0
  11. package/apps/bridge/src/config.js +73 -0
  12. package/apps/bridge/src/discord.js +89 -0
  13. package/apps/bridge/src/gitinfo.js +211 -0
  14. package/apps/bridge/src/hermes.js +97 -0
  15. package/apps/bridge/src/inbox.js +347 -0
  16. package/apps/bridge/src/kanban.js +361 -0
  17. package/apps/bridge/src/lang.js +60 -0
  18. package/apps/bridge/src/live.js +285 -0
  19. package/apps/bridge/src/memos.js +150 -0
  20. package/apps/bridge/src/ops.js +201 -0
  21. package/apps/bridge/src/pair-codes.js +125 -0
  22. package/apps/bridge/src/pipeline-optimizer.js +513 -0
  23. package/apps/bridge/src/pipeline-watcher.js +1022 -0
  24. package/apps/bridge/src/qr.js +808 -0
  25. package/apps/bridge/src/repos.js +544 -0
  26. package/apps/bridge/src/review-watcher.js +271 -0
  27. package/apps/bridge/src/review.js +1749 -0
  28. package/apps/bridge/src/search.js +162 -0
  29. package/apps/bridge/src/server.js +1745 -0
  30. package/apps/bridge/src/snippets.js +373 -0
  31. package/apps/bridge/src/specs.js +806 -0
  32. package/apps/bridge/src/tail.js +28 -0
  33. package/apps/bridge/src/telemetry.js +667 -0
  34. package/apps/bridge/src/term-snippets.js +228 -0
  35. package/apps/bridge/src/transcript.js +231 -0
  36. package/apps/bridge/src/usage.js +294 -0
  37. package/apps/bridge/src/workerFeed.js +72 -0
  38. package/apps/dashboard/index.js +2014 -0
  39. package/apps/dashboard/manifest.json +14 -0
  40. package/apps/dashboard/pilot_dashboard.py +1466 -0
  41. package/apps/dashboard/plugin_api.py +237 -0
  42. package/apps/dashboard/plugin_init.py +15 -0
  43. package/apps/dashboard/style.css +928 -0
  44. package/apps/web/dist/assets/TermPane-DYP7pi_n.css +32 -0
  45. package/apps/web/dist/assets/TermPane-q4f5Zu0I.js +12 -0
  46. package/apps/web/dist/assets/index-BzUof8lw.js +94 -0
  47. package/apps/web/dist/assets/index-DwZdbPU7.css +1 -0
  48. package/apps/web/dist/favicon.ico +0 -0
  49. package/apps/web/dist/icon-180-v6.png +0 -0
  50. package/apps/web/dist/icon-192-v6.png +0 -0
  51. package/apps/web/dist/icon-512-v6.png +0 -0
  52. package/apps/web/dist/icon-maskable-192-v6.png +0 -0
  53. package/apps/web/dist/icon-maskable-512-v6.png +0 -0
  54. package/apps/web/dist/icon-v6.svg +15 -0
  55. package/apps/web/dist/index.html +21 -0
  56. package/apps/web/dist/manifest.webmanifest +49 -0
  57. package/apps/web/dist/sw.js +69 -0
  58. package/bin/hermes-pilot.mjs +964 -0
  59. package/dashboard/dist/index.js +2014 -0
  60. package/dashboard/dist/style.css +928 -0
  61. package/dashboard/manifest.json +14 -0
  62. package/dashboard/pilot_dashboard.py +1466 -0
  63. package/dashboard/plugin_api.py +237 -0
  64. package/package.json +75 -4
  65. package/plugin.json +70 -0
  66. package/plugin.yaml +4 -0
  67. package/scripts/deploy.mjs +58 -0
  68. package/scripts/migrate-to-hermes-pilot.mjs +472 -0
  69. package/scripts/postinstall.mjs +32 -0
  70. package/scripts/sync-dashboard.mjs +98 -0
  71. package/skills/annotation/SKILL.md +138 -0
  72. package/skills/quick-review/SKILL.md +200 -0
  73. package/skills/quick-review/assets/sheet-template.html +175 -0
  74. package/skills/spec-architect/SKILL.md +226 -0
@@ -0,0 +1,271 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import { DatabaseSync } from 'node:sqlite'
4
+ import { parseReviewLine } from './review.js'
5
+
6
+ /**
7
+ * ブリッジ内蔵のレビュー完了ウォッチャー(D7 / SPEC §4.3)。
8
+ *
9
+ * 旧 `mobile-akapen-watch`(外部 cron)を置き換える。カンバン DB を **読み取り専用**で
10
+ * 10 秒ごとに見て、`[review]` タスクの `done` 遷移と `running → blocked` 遷移を
11
+ * `review/feed.jsonl` と `review/chat-reports.jsonl` に 1 行ずつ追記する。
12
+ *
13
+ * 大事な性質:
14
+ * - **初回起動はベースライン確立のみ**(既存の done / blocked を通知しない。通知スパム防止)
15
+ * - **冪等**: 状態は `watcher-state.json` に持ち、`reportedDoneAt` / `reportedBlockedAt` が
16
+ * 入っている遷移は二度書かない。`done` から離れたら印を消し、再度 done になったら 1 回だけ書く
17
+ * - **アトミック**: 状態ファイルは `.tmp` に書いて `fs.renameSync`。JSONL は 1 行完結 append
18
+ *
19
+ * @param {object} cfg ブリッジ設定(kanbanRoot / pluginDataDir)
20
+ * @param {{review?: object, intervalMs?: number, dbPaths?: string[], now?: () => number, logger?: object}} [opts]
21
+ */
22
+
23
+ /** 1 行の末尾改行を保証して追記する(JSONL が途中で連結しないように)。 */
24
+ function appendJsonl(file, obj) {
25
+ fs.mkdirSync(path.dirname(file), { recursive: true })
26
+ let prefix = ''
27
+ try {
28
+ const st = fs.statSync(file)
29
+ if (st.size > 0) {
30
+ const fd = fs.openSync(file, 'r')
31
+ const buf = Buffer.alloc(1)
32
+ fs.readSync(fd, buf, 0, 1, st.size - 1)
33
+ fs.closeSync(fd)
34
+ if (buf.toString() !== '\n') prefix = '\n'
35
+ }
36
+ } catch {
37
+ /* 無ければそのまま作る */
38
+ }
39
+ fs.appendFileSync(file, `${prefix}${JSON.stringify(obj)}\n`)
40
+ }
41
+
42
+ /** 結果の 1 行要約(長すぎる result は切る)。 */
43
+ function oneLine(text, max = 180) {
44
+ const s = String(text ?? '')
45
+ .replace(/\s+/g, ' ')
46
+ .trim()
47
+ return s.length > max ? `${s.slice(0, max - 1)}…` : s
48
+ }
49
+
50
+ export function createReviewWatcher(cfg, opts = {}) {
51
+ const intervalMs = Math.max(1000, Number(opts.intervalMs) || 10_000)
52
+ const now = typeof opts.now === 'function' ? opts.now : () => Date.now()
53
+ const log = opts.logger ?? console
54
+ const root = cfg.reviewDir ?? path.join(cfg.pluginDataDir ?? path.join(cfg.kanbanRoot, '..', 'plugin-data', 'hermes-pilot'), 'review')
55
+ const statePath = path.join(root, 'watcher-state.json')
56
+ const feedPath = path.join(root, 'feed.jsonl')
57
+ const reportsPath = path.join(root, 'chat-reports.jsonl')
58
+
59
+ let timer = null
60
+ let running = false
61
+ let ticks = 0
62
+ let lastError = null
63
+
64
+ /** 状態を読む(無ければ null = 初回起動)。壊れていても null 扱い(ベースラインからやり直す)。 */
65
+ function readState() {
66
+ try {
67
+ const j = JSON.parse(fs.readFileSync(statePath, 'utf8'))
68
+ if (!j || typeof j !== 'object' || typeof j.tasks !== 'object' || Array.isArray(j.tasks)) return null
69
+ return j
70
+ } catch {
71
+ return null
72
+ }
73
+ }
74
+
75
+ /** `.tmp` → rename でアトミックに書く。 */
76
+ function writeState(state) {
77
+ fs.mkdirSync(path.dirname(statePath), { recursive: true })
78
+ const tmp = `${statePath}.tmp`
79
+ fs.writeFileSync(tmp, JSON.stringify(state, null, 2), 'utf8')
80
+ fs.renameSync(tmp, statePath)
81
+ }
82
+
83
+ /** 監視対象の DB。ボードごとの `kanban.db`(+ルートの `kanban.db` があれば)。 */
84
+ function dbPaths() {
85
+ if (Array.isArray(opts.dbPaths)) return opts.dbPaths
86
+ const out = []
87
+ const boardsDir = path.join(cfg.kanbanRoot, 'boards')
88
+ try {
89
+ for (const e of fs.readdirSync(boardsDir, { withFileTypes: true })) {
90
+ if (!e.isDirectory()) continue
91
+ const p = path.join(boardsDir, e.name, 'kanban.db')
92
+ if (fs.existsSync(p)) out.push({ board: e.name, path: p })
93
+ }
94
+ } catch {
95
+ /* boards が無い環境では何もしない */
96
+ }
97
+ const rootDb = path.join(cfg.kanbanRoot, 'kanban.db')
98
+ if (fs.existsSync(rootDb)) out.push({ board: null, path: rootDb })
99
+ return out
100
+ }
101
+
102
+ /** 1 つの DB からレビュータスクを読む(読み取り専用・失敗は空)。 */
103
+ function readTasks(dbPath) {
104
+ let db
105
+ try {
106
+ db = new DatabaseSync(dbPath, { readOnly: true })
107
+ } catch {
108
+ return []
109
+ }
110
+ try {
111
+ return db
112
+ .prepare(
113
+ `SELECT id, title, body, status, result, completed_at, session_id
114
+ FROM tasks
115
+ WHERE COALESCE(body, '') LIKE '%[review]%' OR COALESCE(body, '') LIKE '%[akapen]%'
116
+ ORDER BY COALESCE(completed_at, created_at) DESC
117
+ LIMIT 500`,
118
+ )
119
+ .all()
120
+ } catch {
121
+ return []
122
+ } finally {
123
+ try {
124
+ db.close()
125
+ } catch {
126
+ /* noop */
127
+ }
128
+ }
129
+ }
130
+
131
+ /** 宛先の会話 ID(本文の `chat=` → `task-chats.json`)。 */
132
+ function sessionFor(task, review) {
133
+ const line = parseReviewLine(task.body)
134
+ const fromBody = String(line?.chat ?? '').replace(/[^A-Za-z0-9_-]/g, '').slice(0, 128)
135
+ if (fromBody) return fromBody
136
+ if (review && typeof review.chatFor === 'function') {
137
+ const c = review.chatFor(task.id)
138
+ if (c) return c
139
+ }
140
+ return null
141
+ }
142
+
143
+ /**
144
+ * 1 ティック分の走査。テストから直接呼べる。
145
+ * @returns {{baseline: boolean, seen: number, reported: Array<object>}}
146
+ */
147
+ function tick() {
148
+ ticks += 1
149
+ const stored = readState()
150
+ const baseline = stored === null
151
+ const state = stored ?? { version: 1, tasks: {}, updatedAt: new Date(now()).toISOString() }
152
+ const reported = []
153
+ let seen = 0
154
+ let dirty = baseline
155
+
156
+ const dbs = dbPaths()
157
+ const live = new Set()
158
+ for (const { board, path: dbPath } of dbs) {
159
+ for (const task of readTasks(dbPath)) {
160
+ const line = parseReviewLine(task.body)
161
+ if (!line) continue // `[review]` 行が無い(本文の言及だけ)ものはレビューではない
162
+ seen += 1
163
+ const id = String(task.id)
164
+ live.add(id)
165
+ const status = String(task.status ?? '')
166
+ const prev = state.tasks[id]
167
+ const at = new Date(now()).toISOString()
168
+ const label = line.label ?? null
169
+
170
+ if (!prev) {
171
+ // 初回 or 新しいタスク。どちらも「今の状態」を覚えるだけで通知しない(遡って通知しない)
172
+ state.tasks[id] = { board, status, updatedAt: at, reportedDoneAt: null, reportedBlockedAt: null }
173
+ dirty = true
174
+ continue
175
+ }
176
+
177
+ if (prev.status === status) {
178
+ if (prev.board !== board) {
179
+ prev.board = board ?? null
180
+ dirty = true
181
+ }
182
+ continue
183
+ }
184
+
185
+ const prevStatus = String(prev.status ?? '')
186
+ prev.status = status
187
+ prev.updatedAt = at
188
+ prev.board = board ?? null
189
+ dirty = true
190
+
191
+ if (status === 'done' && !prev.reportedDoneAt) {
192
+ const text = `✅ ${oneLine(task.title)} (${id})`
193
+ appendJsonl(feedPath, { at, task_id: id, kind: 'done', label, text })
194
+ const sessionId = sessionFor(task, opts.review)
195
+ if (sessionId) {
196
+ const summary = oneLine(task.result) || oneLine(task.title)
197
+ appendJsonl(reportsPath, { at, session_id: sessionId, task_id: id, label, text: summary })
198
+ }
199
+ prev.reportedDoneAt = at
200
+ reported.push({ kind: 'done', taskId: id, label, sessionId })
201
+ } else if (status === 'blocked' && prevStatus === 'running' && !prev.reportedBlockedAt) {
202
+ const text = `⏸ ${oneLine(task.title)} (${id})`
203
+ appendJsonl(feedPath, { at, task_id: id, kind: 'blocked', label, text })
204
+ prev.reportedBlockedAt = at
205
+ reported.push({ kind: 'blocked', taskId: id, label })
206
+ }
207
+
208
+ // done から離れたら印を消す(再度 done になったら 1 回だけ通知する)
209
+ if (status !== 'done') prev.reportedDoneAt = null
210
+ if (status !== 'blocked') prev.reportedBlockedAt = null
211
+ }
212
+ }
213
+
214
+ // 消えたタスクは状態から落とす(ボードを消した・アーカイブした等)。印だけ残すと育つため。
215
+ for (const id of Object.keys(state.tasks)) {
216
+ if (!live.has(id)) {
217
+ delete state.tasks[id]
218
+ dirty = true
219
+ }
220
+ }
221
+
222
+ if (dirty) {
223
+ state.updatedAt = new Date(now()).toISOString()
224
+ writeState(state)
225
+ }
226
+ return { baseline, seen, reported }
227
+ }
228
+
229
+ /** 例外でプロセスを落とさないティック(定期実行用)。 */
230
+ function safeTick() {
231
+ if (running) return null
232
+ running = true
233
+ try {
234
+ const r = tick()
235
+ lastError = null
236
+ return r
237
+ } catch (err) {
238
+ lastError = String(err?.message ?? err)
239
+ log.warn?.(`[review-watcher] ${lastError}`)
240
+ return null
241
+ } finally {
242
+ running = false
243
+ }
244
+ }
245
+
246
+ /** 監視を始める(初回は即ティック=ベースライン確立)。 */
247
+ function start() {
248
+ if (timer) return
249
+ safeTick()
250
+ timer = setInterval(safeTick, intervalMs)
251
+ // ブリッジの停止を妨げない(テストや CLI が終われなくなるため)
252
+ if (typeof timer.unref === 'function') timer.unref()
253
+ }
254
+
255
+ function stop() {
256
+ if (!timer) return
257
+ clearInterval(timer)
258
+ timer = null
259
+ }
260
+
261
+ return {
262
+ intervalMs,
263
+ statePath,
264
+ feedPath,
265
+ reportsPath,
266
+ tick,
267
+ start,
268
+ stop,
269
+ status: () => ({ running: Boolean(timer), ticks, lastError, statePath }),
270
+ }
271
+ }