@tekmidian/pai 0.64.0 → 0.65.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 (180) hide show
  1. package/README.md +19 -5
  2. package/dist/{aibroker-client-BpfuGPe5.mjs → aibroker-client-B8c42Lh8.mjs} +1 -1
  3. package/dist/{aibroker-client-DRld7g6z.mjs → aibroker-client-C5Fw7DNz.mjs} +10 -4
  4. package/dist/{aibroker-client-DRld7g6z.mjs.map → aibroker-client-C5Fw7DNz.mjs.map} +1 -1
  5. package/dist/{async-CY_cd_8j.mjs → async-CNn36gm4.mjs} +2 -2
  6. package/dist/{async-CY_cd_8j.mjs.map → async-CNn36gm4.mjs.map} +1 -1
  7. package/dist/{auto-route-CPlam4iv.mjs → auto-route-sLMU-NnM.mjs} +2 -2
  8. package/dist/{auto-route-CPlam4iv.mjs.map → auto-route-sLMU-NnM.mjs.map} +1 -1
  9. package/dist/{chain-o-g8AgE3.mjs → chain-C8QO8gwj.mjs} +4 -4
  10. package/dist/{chain-o-g8AgE3.mjs.map → chain-C8QO8gwj.mjs.map} +1 -1
  11. package/dist/cli/index.mjs +19 -19
  12. package/dist/cli/program.d.mts.map +1 -1
  13. package/dist/cli/program.mjs +19 -19
  14. package/dist/{clusters-DsMf20PP.mjs → clusters-PgIUYT_v.mjs} +1 -1
  15. package/dist/{clusters-DsMf20PP.mjs.map → clusters-PgIUYT_v.mjs.map} +1 -1
  16. package/dist/{config-2wkz744W.mjs → config-BbLFD7Uf.mjs} +3 -3
  17. package/dist/{config-2wkz744W.mjs.map → config-BbLFD7Uf.mjs.map} +1 -1
  18. package/dist/{config-CUTg6VDq.mjs → config-YinjgXEJ.mjs} +1 -1
  19. package/dist/{context-handover-cache-BOpjLsKe.mjs → context-handover-cache-mGxq2-8f.mjs} +11 -29
  20. package/dist/context-handover-cache-mGxq2-8f.mjs.map +1 -0
  21. package/dist/daemon/index.mjs +16 -16
  22. package/dist/daemon-CX9JomIJ.mjs +19 -0
  23. package/dist/{daemon-BYieXduQ.mjs → daemon-D2r1AQqE.mjs} +30 -30
  24. package/dist/{daemon-BYieXduQ.mjs.map → daemon-D2r1AQqE.mjs.map} +1 -1
  25. package/dist/daemon-mcp/index.mjs +17 -17
  26. package/dist/daemon-mcp/index.mjs.map +1 -1
  27. package/dist/detector-C3Q7mxQU.mjs +3 -0
  28. package/dist/{detector-BAlmrLQ0.mjs → detector-CIHEXKsV.mjs} +1 -1
  29. package/dist/{detector-BAlmrLQ0.mjs.map → detector-CIHEXKsV.mjs.map} +1 -1
  30. package/dist/{embeddings-DK9XfQic.mjs → embeddings-BbNVXa_0.mjs} +1 -1
  31. package/dist/{embeddings-DK9XfQic.mjs.map → embeddings-BbNVXa_0.mjs.map} +1 -1
  32. package/dist/{embeddings-Xg8XLf2K.mjs → embeddings-CDfM63uC.mjs} +1 -1
  33. package/dist/{factory-BMK0tC1b.mjs → factory-CaswxuJ0.mjs} +1 -1
  34. package/dist/{factory-C4We3xR_.mjs → factory-y36qGegI.mjs} +13 -13
  35. package/dist/{factory-C4We3xR_.mjs.map → factory-y36qGegI.mjs.map} +1 -1
  36. package/dist/{fallback-ECiqCuh6.mjs → fallback-CWDQYmJi.mjs} +1205 -1204
  37. package/dist/fallback-CWDQYmJi.mjs.map +1 -0
  38. package/dist/{federation-db-CUgy0WYs.mjs → federation-db-BTyoufBh.mjs} +2 -2
  39. package/dist/{federation-db-CUgy0WYs.mjs.map → federation-db-BTyoufBh.mjs.map} +1 -1
  40. package/dist/federation-db-HfIFc7FG.mjs +3 -0
  41. package/dist/hooks/block-sleep-poll.mjs +36 -11
  42. package/dist/hooks/block-sleep-poll.mjs.map +2 -2
  43. package/dist/hooks/capture-all-events.mjs +3 -17
  44. package/dist/hooks/capture-all-events.mjs.map +3 -3
  45. package/dist/hooks/capture-session-summary.mjs +3 -17
  46. package/dist/hooks/capture-session-summary.mjs.map +3 -3
  47. package/dist/hooks/capture-tool-output.mjs +3 -17
  48. package/dist/hooks/capture-tool-output.mjs.map +3 -3
  49. package/dist/hooks/cleanup-session-files.mjs +3 -17
  50. package/dist/hooks/cleanup-session-files.mjs.map +2 -2
  51. package/dist/hooks/context-compression-hook.mjs +3 -17
  52. package/dist/hooks/context-compression-hook.mjs.map +3 -3
  53. package/dist/hooks/initialize-session.mjs +3 -17
  54. package/dist/hooks/initialize-session.mjs.map +2 -2
  55. package/dist/hooks/inject-observations.mjs +3 -17
  56. package/dist/hooks/inject-observations.mjs.map +2 -2
  57. package/dist/hooks/load-core-context.mjs +4 -22
  58. package/dist/hooks/load-core-context.mjs.map +2 -2
  59. package/dist/hooks/load-project-context.mjs +3 -17
  60. package/dist/hooks/load-project-context.mjs.map +3 -3
  61. package/dist/hooks/mcp-deferred-gate.mjs +3 -17
  62. package/dist/hooks/mcp-deferred-gate.mjs.map +2 -2
  63. package/dist/hooks/observe.mjs +3 -17
  64. package/dist/hooks/observe.mjs.map +2 -2
  65. package/dist/hooks/security-validator.mjs +3 -17
  66. package/dist/hooks/security-validator.mjs.map +3 -3
  67. package/dist/hooks/stop-hook.mjs +3 -17
  68. package/dist/hooks/stop-hook.mjs.map +3 -3
  69. package/dist/hooks/sync-todo-to-md.mjs +3 -17
  70. package/dist/hooks/sync-todo-to-md.mjs.map +2 -2
  71. package/dist/hooks/worker-guard.mjs.map +2 -2
  72. package/dist/hooks/worker-status-line.mjs +4 -4
  73. package/dist/hooks/worker-status-line.mjs.map +2 -2
  74. package/dist/{indexer-backend-CJ0RGgMz.mjs → indexer-backend-Cnc7Tf5C.mjs} +2 -2
  75. package/dist/{ipc-client-EhWY8XL0.mjs → ipc-client-D16Xw6Uo.mjs} +2 -2
  76. package/dist/{ipc-client-EhWY8XL0.mjs.map → ipc-client-D16Xw6Uo.mjs.map} +1 -1
  77. package/dist/{kg-entity-1uqCnk4u.mjs → kg-entity-COTUj1ZC.mjs} +1 -1
  78. package/dist/{kg-entity-1uqCnk4u.mjs.map → kg-entity-COTUj1ZC.mjs.map} +1 -1
  79. package/dist/{latent-ideas-CSuKfiq3.mjs → latent-ideas-ByvMjVcb.mjs} +3 -3
  80. package/dist/{latent-ideas-CSuKfiq3.mjs.map → latent-ideas-ByvMjVcb.mjs.map} +1 -1
  81. package/dist/{link-boost-BtjzfE3c.mjs → link-boost-UiE-uooT.mjs} +1 -1
  82. package/dist/{link-boost-BtjzfE3c.mjs.map → link-boost-UiE-uooT.mjs.map} +1 -1
  83. package/dist/{main-resolver-DyOrwNYw.mjs → main-resolver-CDe7DCso.mjs} +6 -6
  84. package/dist/{main-resolver-DyOrwNYw.mjs.map → main-resolver-CDe7DCso.mjs.map} +1 -1
  85. package/dist/main-resolver-DI-A7lSO.mjs +7 -0
  86. package/dist/merge-2gqRPFu2.mjs +3 -0
  87. package/dist/{merge-D58n_-LZ.mjs → merge-DgU9OgZy.mjs} +1 -1
  88. package/dist/{merge-D58n_-LZ.mjs.map → merge-DgU9OgZy.mjs.map} +1 -1
  89. package/dist/module-paths-DdRzbkUI.mjs +44 -0
  90. package/dist/module-paths-DdRzbkUI.mjs.map +1 -0
  91. package/dist/{neighborhood-DNuRelRB.mjs → neighborhood-CvRHlqdR.mjs} +2 -2
  92. package/dist/{neighborhood-DNuRelRB.mjs.map → neighborhood-CvRHlqdR.mjs.map} +1 -1
  93. package/dist/{note-context-D9JZ4-7o.mjs → note-context-BrbfUIoP.mjs} +1 -1
  94. package/dist/{note-context-D9JZ4-7o.mjs.map → note-context-BrbfUIoP.mjs.map} +1 -1
  95. package/dist/{pai-home-UncxWxlX.mjs → pai-home-Cm9rcJgX.mjs} +1 -1
  96. package/dist/{pai-home-UncxWxlX.mjs.map → pai-home-Cm9rcJgX.mjs.map} +1 -1
  97. package/dist/{planner-DgC3oSvb.mjs → planner-8-shIa8t.mjs} +7 -7
  98. package/dist/{planner-DgC3oSvb.mjs.map → planner-8-shIa8t.mjs.map} +1 -1
  99. package/dist/{postgres-CcsRKir-.mjs → postgres-DYtZg7J7.mjs} +5 -13
  100. package/dist/{postgres-CcsRKir-.mjs.map → postgres-DYtZg7J7.mjs.map} +1 -1
  101. package/dist/{program-JhF2GgJY.mjs → program-DiktbiWa.mjs} +225 -245
  102. package/dist/program-DiktbiWa.mjs.map +1 -0
  103. package/dist/{query-feedback-BPa0dISE.mjs → query-feedback-BIaZTTFO.mjs} +2 -2
  104. package/dist/{query-feedback-BPa0dISE.mjs.map → query-feedback-BIaZTTFO.mjs.map} +1 -1
  105. package/dist/query-feedback-B_iigYj-.mjs +3 -0
  106. package/dist/{registry-db-DCzdI4sC.mjs → registry-db-C7voqML9.mjs} +2 -2
  107. package/dist/{registry-db-DCzdI4sC.mjs.map → registry-db-C7voqML9.mjs.map} +1 -1
  108. package/dist/registry-db-JHPhA8vF.mjs +3 -0
  109. package/dist/{registry-postgres-BUnkHKYs.mjs → registry-postgres-DSrkxqfF.mjs} +2 -2
  110. package/dist/{registry-postgres-BUnkHKYs.mjs.map → registry-postgres-DSrkxqfF.mjs.map} +1 -1
  111. package/dist/{registry-sqlite-CKYNwkUv.mjs → registry-sqlite-B2436JgX.mjs} +2 -2
  112. package/dist/{registry-sqlite-CKYNwkUv.mjs.map → registry-sqlite-B2436JgX.mjs.map} +1 -1
  113. package/dist/router-S6C5BxzZ.mjs +3 -0
  114. package/dist/{router-CGZATlvm.mjs → router-aqLMvNMg.mjs} +1 -1
  115. package/dist/{router-CGZATlvm.mjs.map → router-aqLMvNMg.mjs.map} +1 -1
  116. package/dist/{run-W2rV_9j0.mjs → run-DUZRSnIT.mjs} +120 -42
  117. package/dist/run-DUZRSnIT.mjs.map +1 -0
  118. package/dist/{run-env-Br4Bc7Vz.mjs → run-env-BHWnXqle.mjs} +3 -3
  119. package/dist/{run-env-Br4Bc7Vz.mjs.map → run-env-BHWnXqle.mjs.map} +1 -1
  120. package/dist/{runtime-paths-QGQJAekd.mjs → runtime-paths-CHTg3ywb.mjs} +1 -1
  121. package/dist/{runtime-paths-QGQJAekd.mjs.map → runtime-paths-CHTg3ywb.mjs.map} +1 -1
  122. package/dist/{server-B71rem4q.mjs → server-DlL1QI2k.mjs} +4 -5
  123. package/dist/server-DlL1QI2k.mjs.map +1 -0
  124. package/dist/{session-keepalive-Dgil9hjw.mjs → session-keepalive-CZUwp5IQ.mjs} +7 -7
  125. package/dist/{session-keepalive-Dgil9hjw.mjs.map → session-keepalive-CZUwp5IQ.mjs.map} +1 -1
  126. package/dist/skills/Art/SKILL.md +1 -1
  127. package/dist/skills/Observability/SKILL.md +4 -4
  128. package/dist/skills/Research/SKILL.md +1 -1
  129. package/dist/{sources-LcGptLA-.mjs → sources-DX4ElmrE.mjs} +1 -1
  130. package/dist/{sources-LcGptLA-.mjs.map → sources-DX4ElmrE.mjs.map} +1 -1
  131. package/dist/{sqlite-CAJcw2zL.mjs → sqlite-BrEu3avy.mjs} +3 -3
  132. package/dist/{sqlite-CAJcw2zL.mjs.map → sqlite-BrEu3avy.mjs.map} +1 -1
  133. package/dist/{state-S9wlKarB.mjs → state-DW8zdweW.mjs} +1 -1
  134. package/dist/{state-CHltNjXI.mjs → state-HyjqTihC.mjs} +1 -1
  135. package/dist/{state-CHltNjXI.mjs.map → state-HyjqTihC.mjs.map} +1 -1
  136. package/dist/{themes-D1FQFthd.mjs → themes-BgqahYVM.mjs} +2 -2
  137. package/dist/{themes-D1FQFthd.mjs.map → themes-BgqahYVM.mjs.map} +1 -1
  138. package/dist/{tools-BW7OXf-N.mjs → tools-BbqQIHPe.mjs} +4 -4
  139. package/dist/{tools-BFC-113F.mjs → tools-gMGDIpJ9.mjs} +18 -18
  140. package/dist/{tools-BFC-113F.mjs.map → tools-gMGDIpJ9.mjs.map} +1 -1
  141. package/dist/{trace-IDKK1VFs.mjs → trace-CmAB7iJZ.mjs} +1 -1
  142. package/dist/{trace-IDKK1VFs.mjs.map → trace-CmAB7iJZ.mjs.map} +1 -1
  143. package/dist/{vault-indexer-y6YC2-mh.mjs → vault-indexer-Ddq51X20.mjs} +1 -1
  144. package/dist/{vault-indexer-y6YC2-mh.mjs.map → vault-indexer-Ddq51X20.mjs.map} +1 -1
  145. package/dist/{wakeup-CNZQZzsY.mjs → wakeup-CZw88uXf.mjs} +3 -3
  146. package/dist/{wakeup-CNZQZzsY.mjs.map → wakeup-CZw88uXf.mjs.map} +1 -1
  147. package/dist/{work-queue-worker-Dy5FT2ak.mjs → work-queue-worker-DCzfH0d1.mjs} +4 -4
  148. package/dist/{work-queue-worker-Dy5FT2ak.mjs.map → work-queue-worker-DCzfH0d1.mjs.map} +1 -1
  149. package/dist/work-queue-worker-Taba_vcA.mjs +11 -0
  150. package/dist/{zettelkasten-Dx-63BEk.mjs → zettelkasten-Dwj67e4p.mjs} +4 -4
  151. package/dist/{zettelkasten-Dx-63BEk.mjs.map → zettelkasten-Dwj67e4p.mjs.map} +1 -1
  152. package/docker/docker-compose.yml +39 -0
  153. package/docs/commands/README.md +3 -3
  154. package/docs/commands/config.md +3 -3
  155. package/docs/commands/setup.md +9 -2
  156. package/docs/commands/worker.md +2 -2
  157. package/package.json +3 -2
  158. package/scripts/build-hooks.mjs +6 -6
  159. package/src/hooks/ts/lib/pai-paths-case.test.ts +12 -0
  160. package/src/hooks/ts/lib/pai-paths-import.test.ts +25 -0
  161. package/src/hooks/ts/lib/pai-paths.ts +5 -28
  162. package/src/hooks/ts/lib/sleep-poll-gate.test.ts +17 -1
  163. package/src/hooks/ts/lib/sleep-poll-gate.ts +21 -9
  164. package/src/hooks/ts/session-start/load-core-context.ts +2 -6
  165. package/src/hooks/ts/session-start/session-start-worker-guard.test.ts +24 -1
  166. package/dist/context-handover-cache-BOpjLsKe.mjs.map +0 -1
  167. package/dist/daemon-q3xjHwa4.mjs +0 -19
  168. package/dist/detector-C-YmZsOi.mjs +0 -3
  169. package/dist/fallback-ECiqCuh6.mjs.map +0 -1
  170. package/dist/federation-db-BZ8PyxFe.mjs +0 -3
  171. package/dist/main-resolver-vN09bkXv.mjs +0 -7
  172. package/dist/merge-CXGz5wXz.mjs +0 -3
  173. package/dist/program-JhF2GgJY.mjs.map +0 -1
  174. package/dist/query-feedback-BPHFFSu1.mjs +0 -3
  175. package/dist/registry-db-Hphlj3H4.mjs +0 -3
  176. package/dist/router-CoA8Uy1m.mjs +0 -3
  177. package/dist/run-W2rV_9j0.mjs.map +0 -1
  178. package/dist/server-B71rem4q.mjs.map +0 -1
  179. package/dist/work-queue-worker-CkShUa0z.mjs +0 -11
  180. /package/dist/{indexer-backend-DotpxHJd.mjs → indexer-backend-isSLg6yE.mjs} +0 -0
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/hooks/ts/session-end/capture-session-summary.ts", "../../../src/hooks/ts/lib/pai-paths.ts", "../../../src/config/pai-home.ts"],
4
- "sourcesContent": ["#!/usr/bin/env node\n\n/**\n * SessionEnd Hook - Captures session summary for UOCS\n *\n * Generates a session summary document when a Claude Code session ends,\n * documenting what was accomplished during the session.\n */\n\nimport { writeFileSync, mkdirSync, existsSync, readFileSync, readdirSync } from 'fs';\nimport { join } from 'path';\nimport { historyDir } from '../lib/pai-paths.js';\n\ninterface SessionData {\n conversation_id: string;\n timestamp: string;\n [key: string]: any;\n}\n\nasync function main() {\n try {\n // Read input from stdin FIRST \u2014 this must complete before CC's abort signal fires.\n // Then fork the heavy work into a detached child so CC can't kill it.\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk);\n }\n const input = Buffer.concat(chunks).toString('utf-8');\n if (!input || input.trim() === '') {\n process.exit(0);\n }\n\n // Fork: re-exec ourselves with --background flag and pipe the stdin data via env.\n // This detaches the heavy work (JSONL scan, IPC) from CC's abort signal.\n if (!process.env.__PAI_HOOK_BG) {\n const { spawn } = await import('child_process');\n const child = spawn(process.execPath, [process.argv[1], '--background'], {\n detached: true,\n stdio: 'ignore',\n env: { ...process.env, __PAI_HOOK_BG: '1', __PAI_HOOK_INPUT: input },\n });\n child.unref();\n process.exit(0); // Return immediately \u2014 CC sees success, abort signal is harmless\n }\n\n // Background mode: we're detached, safe from abort signals\n const data: SessionData = JSON.parse(process.env.__PAI_HOOK_INPUT || input);\n\n // Generate timestamp for filename\n const now = new Date();\n const timestamp = now.toISOString()\n .replace(/:/g, '')\n .replace(/\\..+/, '')\n .replace('T', '-'); // YYYY-MM-DD-HHMMSS\n\n const yearMonth = timestamp.substring(0, 7); // YYYY-MM\n\n // Try to extract session info from raw outputs\n const sessionInfo = await analyzeSession(data.conversation_id, yearMonth);\n\n // Generate filename\n const filename = `${timestamp}_SESSION_${sessionInfo.focus}.md`;\n\n // Ensure directory exists\n const sessionDir = join(historyDir(), 'sessions', yearMonth);\n if (!existsSync(sessionDir)) {\n mkdirSync(sessionDir, { recursive: true });\n }\n\n // Generate session document\n const sessionDoc = formatSessionDocument(timestamp, data, sessionInfo);\n\n // Write session file\n writeFileSync(join(sessionDir, filename), sessionDoc);\n\n // Also store structured summary via daemon IPC for the observations system\n await storeStructuredSummary(data.conversation_id, sessionInfo);\n\n // Exit successfully\n process.exit(0);\n } catch (error) {\n // Silent failure - don't disrupt workflow\n console.error(`[UOCS] SessionEnd hook error: ${error}`);\n process.exit(0);\n }\n}\n\nasync function analyzeSession(conversationId: string, yearMonth: string): Promise<any> {\n // Try to read raw outputs for this session\n const rawOutputsDir = join(historyDir(), 'raw-outputs', yearMonth);\n\n let filesChanged: string[] = [];\n let commandsExecuted: string[] = [];\n let toolsUsed: Set<string> = new Set();\n\n try {\n if (existsSync(rawOutputsDir)) {\n // Only scan today's file \u2014 not the entire month (which can be 400MB+).\n // JSONL filenames are prefixed with YYYY-MM-DD.\n const todayPrefix = new Date().toISOString().substring(0, 10);\n const files = readdirSync(rawOutputsDir).filter(\n f => f.endsWith('.jsonl') && f.startsWith(todayPrefix)\n );\n\n for (const file of files) {\n const filePath = join(rawOutputsDir, file);\n const content = readFileSync(filePath, 'utf-8');\n const lines = content.split('\\n').filter(l => l.trim());\n\n for (const line of lines) {\n try {\n const entry = JSON.parse(line);\n if (entry.session === conversationId) {\n toolsUsed.add(entry.tool);\n\n // Extract file changes\n if (entry.tool === 'Edit' || entry.tool === 'Write') {\n if (entry.input?.file_path) {\n filesChanged.push(entry.input.file_path);\n }\n }\n\n // Extract bash commands\n if (entry.tool === 'Bash' && entry.input?.command) {\n commandsExecuted.push(entry.input.command);\n }\n }\n } catch (e) {\n // Skip invalid JSON lines\n }\n }\n }\n }\n } catch (error) {\n // Silent failure\n }\n\n return {\n focus: 'general-work',\n filesChanged: [...new Set(filesChanged)].slice(0, 10), // Unique, max 10\n commandsExecuted: commandsExecuted.slice(0, 10), // Max 10\n toolsUsed: Array.from(toolsUsed),\n duration: 0 // Unknown\n };\n}\n\nfunction formatSessionDocument(timestamp: string, data: SessionData, info: any): string {\n const date = timestamp.substring(0, 10); // YYYY-MM-DD\n const time = timestamp.substring(11).replace(/-/g, ':'); // HH:MM:SS\n const da = process.env.DA || 'PAI';\n\n return `---\ncapture_type: SESSION\ntimestamp: ${new Date().toISOString()}\nsession_id: ${data.conversation_id}\nduration_minutes: ${info.duration}\nexecutor: ${da}\n---\n\n# Session: ${info.focus}\n\n**Date:** ${date}\n**Time:** ${time}\n**Session ID:** ${data.conversation_id}\n\n---\n\n## Session Overview\n\n**Focus:** General development work\n**Duration:** ${info.duration > 0 ? `${info.duration} minutes` : 'Unknown'}\n\n---\n\n## Tools Used\n\n${info.toolsUsed.length > 0 ? info.toolsUsed.map((t: string) => `- ${t}`).join('\\n') : '- None recorded'}\n\n---\n\n## Files Modified\n\n${info.filesChanged.length > 0 ? info.filesChanged.map((f: string) => `- \\`${f}\\``).join('\\n') : '- None recorded'}\n\n**Total Files Changed:** ${info.filesChanged.length}\n\n---\n\n## Commands Executed\n\n${info.commandsExecuted.length > 0 ? '```bash\\n' + info.commandsExecuted.join('\\n') + '\\n```' : 'None recorded'}\n\n---\n\n## Notes\n\nThis session summary was automatically generated by the UOCS SessionEnd hook.\n\nFor detailed tool outputs, see: \\`${join(historyDir(), 'raw-outputs', timestamp.substring(0, 7))}/\\`\n\n---\n\n**Session Outcome:** Completed\n**Generated:** ${new Date().toISOString()}\n`;\n}\n\nasync function storeStructuredSummary(\n sessionId: string,\n info: { focus: string; filesChanged: string[]; commandsExecuted: string[]; toolsUsed: string[]; duration: number }\n): Promise<void> {\n try {\n const cwd = process.cwd();\n const net = await import('net');\n\n await new Promise<void>((resolve, _reject) => {\n const client = net.createConnection('/tmp/pai.sock', () => {\n const msg = JSON.stringify({\n id: 1,\n method: 'session_summary_store',\n params: {\n session_id: sessionId,\n cwd,\n request: null, // We don't have the original request\n investigated: null,\n learned: null,\n completed: info.filesChanged.length > 0\n ? `Modified ${info.filesChanged.length} file(s): ${info.filesChanged.slice(0, 5).join(', ')}`\n : null,\n next_steps: null,\n observation_count: 0, // Will be filled by daemon from actual count\n }\n }) + '\\n';\n client.write(msg);\n });\n\n client.on('data', () => { client.end(); resolve(); });\n client.on('error', () => resolve()); // Silent failure\n setTimeout(() => { client.destroy(); resolve(); }, 3000);\n });\n } catch {\n // Silent failure \u2014 don't disrupt session end\n }\n}\n\nmain();\n", "/**\n * PAI Path Resolution - Single Source of Truth\n *\n * This module provides consistent path resolution across all PAI hooks.\n *\n * Two different things live here, and they must not be confused:\n *\n * - ADAPTER_DIR (~/.claude by default) is the Claude Code harness adapter \u2014\n * fixed, hardcoded paths the harness itself loads from (Hooks/, Skills/,\n * Agents/, Commands/, settings.json, statusline-command.sh,\n * tab-color-command.sh). It is harness-specific: a future harness would\n * need its own adapter directory with its own conventions.\n * - PAI_HOME (~/.claude/pai by default \u2014 see ../../../config/pai-home.ts) is\n * where PAI's own state lives: everything hooks WRITE (History/,\n * agent-sessions.json, session-routing.json, ...) resolves there, with a\n * fallback to the pre-2026-09-19 ADAPTER_DIR location and a one-time\n * stderr notice, exactly like every other PAI_HOME file.\n *\n * ALSO loads .env file from ADAPTER_DIR so all hooks get environment\n * variables without relying on Claude Code's settings.json injection.\n *\n * Usage in hooks:\n * import { ADAPTER_DIR, HOOKS_DIR, SKILLS_DIR, historyDir } from './lib/pai-paths';\n */\n\nimport { homedir } from 'os';\nimport { resolve, join } from 'path';\nimport { existsSync, readFileSync } from 'fs';\nimport {\n paiHomePath,\n resolvePaiFile,\n migratePaiFile,\n migratePaiDir,\n type MigrateFileResult,\n type MigrateDirResult,\n} from '../../../config/pai-home.js';\n\n/**\n * Load .env file and inject into process.env\n * Must run BEFORE ADAPTER_DIR resolution so .env can set ADAPTER_DIR/PAI_DIR if needed\n */\nfunction loadEnvFile(): void {\n // Check common locations for .env\n const possiblePaths = [\n resolve(process.env.ADAPTER_DIR || process.env.PAI_DIR || '', '.env'),\n resolve(homedir(), '.claude', '.env'),\n ];\n\n for (const envPath of possiblePaths) {\n if (existsSync(envPath)) {\n try {\n const content = readFileSync(envPath, 'utf-8');\n for (const line of content.split('\\n')) {\n const trimmed = line.trim();\n // Skip comments and empty lines\n if (!trimmed || trimmed.startsWith('#')) continue;\n\n const eqIndex = trimmed.indexOf('=');\n if (eqIndex > 0) {\n const key = trimmed.substring(0, eqIndex).trim();\n let value = trimmed.substring(eqIndex + 1).trim();\n\n // Remove surrounding quotes if present\n if ((value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n value = value.slice(1, -1);\n }\n\n // Expand $HOME and ~ in values\n value = value.replace(/\\$HOME/g, homedir());\n value = value.replace(/^~(?=\\/|$)/, homedir());\n\n // Only set if not already defined (env vars take precedence)\n if (process.env[key] === undefined) {\n process.env[key] = value;\n }\n }\n }\n // Found and loaded, don't check other paths\n break;\n } catch {\n // Silently continue if .env can't be read\n }\n }\n }\n}\n\n// Load .env FIRST, before any other initialization\nloadEnvFile();\n\nfunction defaultAdapterDir(): string {\n return resolve(homedir(), '.claude');\n}\n\n/**\n * Never print when stdout/stderr is a data channel, not a human: hook\n * bundles and worker-status-line.mjs set PAI_QUIET_NOTICES=1 via their\n * esbuild banner (scripts/build-hooks.mjs), and a `pai worker run\n * --output-format json` invocation is detected directly off argv since its\n * env can't be set before this module's static imports resolve.\n */\nfunction suppressDeprecationNotices(): boolean {\n if (process.env.PAI_QUIET_NOTICES === '1') return true;\n const idx = process.argv.indexOf('--output-format');\n return idx !== -1 && process.argv[idx + 1] === 'json';\n}\n\n/**\n * At most once per process \u2014 a `globalThis` flag rather than a module-level\n * `let` because hooks and the CLI can end up with more than one instance of\n * this module loaded into the same process (separate bundles), each with its\n * own module scope; only a property on the shared global survives that.\n */\nfunction warnPaiDirDeprecatedOnce(message: string): void {\n const g = globalThis as typeof globalThis & { __paiDirNoticePrinted?: boolean };\n if (g.__paiDirNoticePrinted || suppressDeprecationNotices()) return;\n g.__paiDirNoticePrinted = true;\n process.stderr.write(`pai: ${message}\\n`);\n}\n\n/**\n * Smart ADAPTER_DIR detection with fallback\n * Priority:\n * 1. ADAPTER_DIR environment variable (if set) \u2014 warns once if PAI_DIR is\n * ALSO set and resolves to a different path (naming both).\n * 2. PAI_DIR environment variable (deprecated alias, one release) \u2014 warns\n * once only when it differs from the default adapter root; PAI_DIR set to\n * the same value as the default is the operator's current, correct\n * setting and stays silent.\n * 3. ~/.claude (standard location)\n */\nfunction resolveAdapterDir(): string {\n if (process.env.ADAPTER_DIR) {\n const adapterDir = resolve(process.env.ADAPTER_DIR);\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== adapterDir) {\n warnPaiDirDeprecatedOnce(\n `ADAPTER_DIR (${adapterDir}) and PAI_DIR (${paiDir}) are both set and differ \u2014 ADAPTER_DIR wins. PAI_DIR still works for one release.`\n );\n }\n }\n return adapterDir;\n }\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== defaultAdapterDir()) {\n warnPaiDirDeprecatedOnce(\n 'PAI_DIR is deprecated \u2014 use ADAPTER_DIR for the harness adapter root (~/.claude). PAI_DIR still works for one release.'\n );\n }\n return paiDir;\n }\n return defaultAdapterDir();\n}\n\n/** The Claude Code harness adapter root \u2014 NOT PAI's state home. See module doc above. */\nexport const ADAPTER_DIR = resolveAdapterDir();\n\n/** @deprecated alias for ADAPTER_DIR, kept for one release. Prefer ADAPTER_DIR. */\nexport const PAI_DIR = ADAPTER_DIR;\n\n/**\n * Adapter directories \u2014 fixed paths the Claude Code harness itself loads\n * from. These stay under ADAPTER_DIR; they are not PAI state.\n */\nexport const HOOKS_DIR = join(ADAPTER_DIR, 'Hooks');\nexport const SKILLS_DIR = join(ADAPTER_DIR, 'Skills');\nexport const AGENTS_DIR = join(ADAPTER_DIR, 'Agents');\nexport const COMMANDS_DIR = join(ADAPTER_DIR, 'Commands');\n\n/**\n * Validate PAI directory structure on first import\n * This fails fast with a clear error if PAI is misconfigured\n */\nfunction validatePAIStructure(): void {\n if (!existsSync(ADAPTER_DIR)) {\n console.error(`ADAPTER_DIR does not exist: ${ADAPTER_DIR}`);\n console.error(` Expected ~/.claude or set ADAPTER_DIR environment variable`);\n process.exit(1);\n }\n\n if (!existsSync(HOOKS_DIR)) {\n console.error(`PAI hooks directory not found: ${HOOKS_DIR}`);\n console.error(` Your ADAPTER_DIR may be misconfigured`);\n console.error(` Current ADAPTER_DIR: ${ADAPTER_DIR}`);\n process.exit(1);\n }\n}\n\n// Run validation on module import\n// This ensures any hook that imports this module will fail fast if paths are wrong\nvalidatePAIStructure();\n\n// ---------------------------------------------------------------------------\n// PAI state written by hooks \u2014 resolves under PAI_HOME, falling back to the\n// pre-2026-09-19 ADAPTER_DIR location (one-time stderr notice) until\n// `pai config migrate --history` moves it. Actively written on every hook\n// event across every session, so unlike most PAI_HOME files the live move is\n// deliberately NOT automatic \u2014 see `pai config migrate --history`.\n// ---------------------------------------------------------------------------\n\nfunction oldHistoryDir(): string {\n return join(ADAPTER_DIR, 'History');\n}\n\n/** Read/write location for hook-captured history: PAI_HOME/History if\n * present, else the old ADAPTER_DIR/History (one-time stderr notice). */\nexport function historyDir(): string {\n return resolvePaiFile(paiHomePath('History'), [oldHistoryDir()], 'pai config migrate --history');\n}\n\nexport function migrateHistoryDir(opts: { dryRun?: boolean } = {}): MigrateDirResult {\n return migratePaiDir(paiHomePath('History'), [oldHistoryDir()], opts);\n}\n\nfunction oldAgentSessionsPath(): string {\n return join(ADAPTER_DIR, 'agent-sessions.json');\n}\n\nexport function agentSessionsPath(): string {\n return resolvePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], 'pai config migrate --history');\n}\n\nexport function migrateAgentSessions(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], opts);\n}\n\nfunction oldSecurityEventsPath(): string {\n return join(ADAPTER_DIR, 'history', 'security', 'security-events.jsonl');\n}\n\nexport function securityEventsPath(): string {\n return resolvePaiFile(\n paiHomePath('History', 'security', 'security-events.jsonl'),\n [oldSecurityEventsPath()],\n 'pai config migrate --history'\n );\n}\n\nexport function migrateSecurityEvents(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('History', 'security', 'security-events.jsonl'), [oldSecurityEventsPath()], opts);\n}\n\nfunction oldSessionRoutingPath(): string {\n return join(ADAPTER_DIR, 'session-routing.json');\n}\n\nexport function sessionRoutingPath(): string {\n return resolvePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], 'pai config migrate --history');\n}\n\nexport function migrateSessionRouting(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], opts);\n}\n\n/**\n * Helper to get history file path with date-based organization\n */\nexport function getHistoryFilePath(subdir: string, filename: string): string {\n const now = new Date();\n const tz = process.env.TIME_ZONE || Intl.DateTimeFormat().resolvedOptions().timeZone;\n const localDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));\n const year = localDate.getFullYear();\n const month = String(localDate.getMonth() + 1).padStart(2, '0');\n\n return join(historyDir(), subdir, `${year}-${month}`, filename);\n}\n", "/**\n * pai-home.ts \u2014 the PAI_HOME namespace directory and generic per-user file\n * migration into it.\n *\n * Every PAI-owned per-user file (workers.yaml, config.json, whisper-rules.md,\n * advisor-mode.json, ...) lives under PAI_HOME (~/.claude/pai by default) so\n * nothing PAI writes can ever collide with a file Claude Code itself\n * introduces under ~/.claude. Decided 2026-09-19 \u2014 see docs/workers-config.md.\n */\n\nimport { existsSync, mkdirSync, readFileSync, copyFileSync, renameSync, readdirSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, dirname } from \"node:path\";\nimport { execFileSync } from \"node:child_process\";\n\n/**\n * Byte-identical check that works for files of any size. `cmp` compares by\n * streaming rather than loading either file into memory, unlike a\n * readFileSync + Buffer.compare \u2014 which throws (\"File size is greater than\n * 2 GiB\") on anything past Node's 2GiB single-read ceiling, a real limit hit\n * migrating a multi-gigabyte legacy federation database file.\n */\nfunction filesByteIdentical(a: string, b: string): boolean {\n try {\n execFileSync(\"cmp\", [\"-s\", a, b], { stdio: \"pipe\" });\n return true;\n } catch (e) {\n if (e && typeof e === \"object\" && \"status\" in e && (e as { status: number }).status === 1) return false;\n // cmp missing or errored for an unrelated reason \u2014 fall back to an\n // in-memory compare (fine for the small files this path normally sees).\n return Buffer.compare(readFileSync(a), readFileSync(b)) === 0;\n }\n}\n\n/** PAI_HOME (test isolation, power users) overrides the default namespace dir. */\nexport function paiHomeDir(): string {\n return process.env.PAI_HOME || join(homedir(), \".claude\", \"pai\");\n}\n\n/** A path under PAI_HOME, e.g. `paiHomePath(\"config.json\")`. */\nexport function paiHomePath(...segments: string[]): string {\n return join(paiHomeDir(), ...segments);\n}\n\nconst noticesPrinted = new Set<string>();\n\n/**\n * Resolve a per-user PAI file: the new PAI_HOME path if it exists, else the\n * first existing entry in `oldCandidates` (checked in order \u2014 most recent\n * old location first), else the new path (the target a first write\n * creates). Prints one stderr notice per process per new-path when a\n * fallback location is actually used.\n */\nexport function resolvePaiFile(newPath: string, oldCandidates: string[], migrateHint: string): string {\n if (existsSync(newPath)) return newPath;\n for (const old of oldCandidates) {\n if (existsSync(old)) {\n if (!noticesPrinted.has(newPath)) {\n noticesPrinted.add(newPath);\n process.stderr.write(\n `pai: ${old} is at an old location \u2014 run \\`${migrateHint}\\` to move it to ${newPath}\\n`\n );\n }\n return old;\n }\n }\n return newPath;\n}\n\nexport class PaiFileMigrationError extends Error {}\n\nexport interface MigrateFileResult {\n fromPath: string | null;\n toPath: string;\n dryRun: boolean;\n note?: string;\n}\n\n/**\n * Move a per-user PAI file to its new PAI_HOME location: copy, verify\n * byte-identical, then rename the source to `<name>.migrated-<YYYYMMDD>`\n * (never deleted). Idempotent: if the new path already holds bytes\n * identical to the found source, the source is just renamed aside; if it\n * differs, this refuses rather than overwrite silently.\n */\nexport function migratePaiFile(\n newPath: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateFileResult {\n const fromPath = oldCandidates.find((p) => existsSync(p)) ?? null;\n if (!fromPath) {\n const note = existsSync(newPath) ? \"already at new location\" : \"nothing to migrate \u2014 file does not exist yet\";\n return { fromPath: null, toPath: newPath, dryRun: !!opts.dryRun, note };\n }\n if (opts.dryRun) return { fromPath, toPath: newPath, dryRun: true };\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n\n if (existsSync(newPath)) {\n if (filesByteIdentical(fromPath, newPath)) {\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false, note: \"identical \u2014 old file renamed aside\" };\n }\n throw new PaiFileMigrationError(\n `${newPath} already exists and differs from ${fromPath} \u2014 resolve manually, nothing changed`\n );\n }\n\n const dir = dirname(newPath);\n if (!existsSync(dir)) mkdirSync(dir, { recursive: true });\n copyFileSync(fromPath, newPath);\n\n if (!filesByteIdentical(fromPath, newPath)) {\n throw new PaiFileMigrationError(\n `${newPath}: copy did not match ${fromPath} byte-for-byte \u2014 aborting, old file left in place`\n );\n }\n\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false };\n}\n\nexport interface MigrateDirResult {\n fromDir: string | null;\n toDir: string;\n dryRun: boolean;\n movedCount?: number;\n note?: string;\n}\n\n/**\n * Move a per-user PAI directory (queries/, session-state/, ...) into\n * PAI_HOME: move every entry from the first found old candidate into the\n * new dir (never overwriting an existing entry there), then rename the now-\n * empty old dir aside to `<name>.migrated-<YYYYMMDD>` (never deleted).\n * Idempotent \u2014 an already-migrated dir has nothing left to find.\n */\nexport function migratePaiDir(\n newDir: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateDirResult {\n const fromDir = oldCandidates.find((p) => existsSync(p) && statSync(p).isDirectory()) ?? null;\n if (!fromDir) {\n const note = existsSync(newDir) ? \"already at new location\" : \"nothing to migrate \u2014 directory does not exist yet\";\n return { fromDir: null, toDir: newDir, dryRun: !!opts.dryRun, note };\n }\n\n const entries = readdirSync(fromDir);\n if (opts.dryRun) return { fromDir, toDir: newDir, dryRun: true, movedCount: entries.length };\n\n if (!existsSync(newDir)) mkdirSync(newDir, { recursive: true });\n\n let moved = 0;\n const collided: string[] = [];\n for (const entry of entries) {\n const src = join(fromDir, entry);\n const dst = join(newDir, entry);\n if (existsSync(dst)) {\n collided.push(entry);\n continue;\n }\n renameSync(src, dst);\n moved++;\n }\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n const remaining = readdirSync(fromDir);\n const note = collided.length\n ? `${collided.length} entrie(s) left in ${fromDir} \u2014 name already existed in ${newDir}`\n : undefined;\n\n if (remaining.length === 0) {\n renameSync(fromDir, `${fromDir}.migrated-${stamp}`);\n }\n\n return { fromDir, toDir: newDir, dryRun: false, movedCount: moved, note };\n}\n"],
5
- "mappings": ";;;;;;AASA,SAAS,eAAe,aAAAA,YAAW,cAAAC,aAAY,gBAAAC,eAAc,eAAAC,oBAAmB;AAChF,SAAS,QAAAC,aAAY;;;ACerB,SAAS,WAAAC,gBAAe;AACxB,SAAS,SAAS,QAAAC,aAAY;AAC9B,SAAS,cAAAC,aAAY,gBAAAC,qBAAoB;;;ACjBzC,SAAS,YAAY,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AQ;AAAA,UACR,QAAQ;AAAA,YACN,YAAY;AAAA,YACZ;AAAA,YACA,SAAS;AAAA;AAAA,YACT,cAAc;AAAA,YACd,SAAS;AAAA,YACT,WAAW,KAAK,aAAa,SAAS,IAClC,YAAY,KAAK,aAAa,MAAM,aAAa,KAAK,aAAa,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,KACzF;AAAA,YACJ,YAAY;AAAA,YACZ,mBAAmB;AAAA;AAAA,UACrB;AAAA,QACF,CAAC,IAAI;AACL,eAAO,MAAM,GAAG;AAAA,MAClB,CAAC;AAED,aAAO,GAAG,QAAQ,MAAM;AAAE,eAAO,IAAI;AAAG,QAAAA,SAAQ;AAAA,MAAG,CAAC;AACpD,aAAO,GAAG,SAAS,MAAMA,SAAQ,CAAC;AAClC,iBAAW,MAAM;AAAE,eAAO,QAAQ;AAAG,QAAAA,SAAQ;AAAA,MAAG,GAAG,GAAI;AAAA,IACzD,CAAC;AAAA,EACH,QAAQ;AAAA,EAER;AACF;AAEA,KAAK;",
6
- "names": ["mkdirSync", "existsSync", "readFileSync", "readdirSync", "join", "homedir", "join", "existsSync", "readFileSync", "homedir", "existsSync", "readFileSync", "join", "existsSync", "join", "existsSync", "mkdirSync", "readdirSync", "readFileSync", "resolve"]
4
+ "sourcesContent": ["#!/usr/bin/env node\n\n/**\n * SessionEnd Hook - Captures session summary for UOCS\n *\n * Generates a session summary document when a Claude Code session ends,\n * documenting what was accomplished during the session.\n */\n\nimport { writeFileSync, mkdirSync, existsSync, readFileSync, readdirSync } from 'fs';\nimport { join } from 'path';\nimport { historyDir } from '../lib/pai-paths.js';\n\ninterface SessionData {\n conversation_id: string;\n timestamp: string;\n [key: string]: any;\n}\n\nasync function main() {\n try {\n // Read input from stdin FIRST \u2014 this must complete before CC's abort signal fires.\n // Then fork the heavy work into a detached child so CC can't kill it.\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk);\n }\n const input = Buffer.concat(chunks).toString('utf-8');\n if (!input || input.trim() === '') {\n process.exit(0);\n }\n\n // Fork: re-exec ourselves with --background flag and pipe the stdin data via env.\n // This detaches the heavy work (JSONL scan, IPC) from CC's abort signal.\n if (!process.env.__PAI_HOOK_BG) {\n const { spawn } = await import('child_process');\n const child = spawn(process.execPath, [process.argv[1], '--background'], {\n detached: true,\n stdio: 'ignore',\n env: { ...process.env, __PAI_HOOK_BG: '1', __PAI_HOOK_INPUT: input },\n });\n child.unref();\n process.exit(0); // Return immediately \u2014 CC sees success, abort signal is harmless\n }\n\n // Background mode: we're detached, safe from abort signals\n const data: SessionData = JSON.parse(process.env.__PAI_HOOK_INPUT || input);\n\n // Generate timestamp for filename\n const now = new Date();\n const timestamp = now.toISOString()\n .replace(/:/g, '')\n .replace(/\\..+/, '')\n .replace('T', '-'); // YYYY-MM-DD-HHMMSS\n\n const yearMonth = timestamp.substring(0, 7); // YYYY-MM\n\n // Try to extract session info from raw outputs\n const sessionInfo = await analyzeSession(data.conversation_id, yearMonth);\n\n // Generate filename\n const filename = `${timestamp}_SESSION_${sessionInfo.focus}.md`;\n\n // Ensure directory exists\n const sessionDir = join(historyDir(), 'sessions', yearMonth);\n if (!existsSync(sessionDir)) {\n mkdirSync(sessionDir, { recursive: true });\n }\n\n // Generate session document\n const sessionDoc = formatSessionDocument(timestamp, data, sessionInfo);\n\n // Write session file\n writeFileSync(join(sessionDir, filename), sessionDoc);\n\n // Also store structured summary via daemon IPC for the observations system\n await storeStructuredSummary(data.conversation_id, sessionInfo);\n\n // Exit successfully\n process.exit(0);\n } catch (error) {\n // Silent failure - don't disrupt workflow\n console.error(`[UOCS] SessionEnd hook error: ${error}`);\n process.exit(0);\n }\n}\n\nasync function analyzeSession(conversationId: string, yearMonth: string): Promise<any> {\n // Try to read raw outputs for this session\n const rawOutputsDir = join(historyDir(), 'raw-outputs', yearMonth);\n\n let filesChanged: string[] = [];\n let commandsExecuted: string[] = [];\n let toolsUsed: Set<string> = new Set();\n\n try {\n if (existsSync(rawOutputsDir)) {\n // Only scan today's file \u2014 not the entire month (which can be 400MB+).\n // JSONL filenames are prefixed with YYYY-MM-DD.\n const todayPrefix = new Date().toISOString().substring(0, 10);\n const files = readdirSync(rawOutputsDir).filter(\n f => f.endsWith('.jsonl') && f.startsWith(todayPrefix)\n );\n\n for (const file of files) {\n const filePath = join(rawOutputsDir, file);\n const content = readFileSync(filePath, 'utf-8');\n const lines = content.split('\\n').filter(l => l.trim());\n\n for (const line of lines) {\n try {\n const entry = JSON.parse(line);\n if (entry.session === conversationId) {\n toolsUsed.add(entry.tool);\n\n // Extract file changes\n if (entry.tool === 'Edit' || entry.tool === 'Write') {\n if (entry.input?.file_path) {\n filesChanged.push(entry.input.file_path);\n }\n }\n\n // Extract bash commands\n if (entry.tool === 'Bash' && entry.input?.command) {\n commandsExecuted.push(entry.input.command);\n }\n }\n } catch (e) {\n // Skip invalid JSON lines\n }\n }\n }\n }\n } catch (error) {\n // Silent failure\n }\n\n return {\n focus: 'general-work',\n filesChanged: [...new Set(filesChanged)].slice(0, 10), // Unique, max 10\n commandsExecuted: commandsExecuted.slice(0, 10), // Max 10\n toolsUsed: Array.from(toolsUsed),\n duration: 0 // Unknown\n };\n}\n\nfunction formatSessionDocument(timestamp: string, data: SessionData, info: any): string {\n const date = timestamp.substring(0, 10); // YYYY-MM-DD\n const time = timestamp.substring(11).replace(/-/g, ':'); // HH:MM:SS\n const da = process.env.DA || 'PAI';\n\n return `---\ncapture_type: SESSION\ntimestamp: ${new Date().toISOString()}\nsession_id: ${data.conversation_id}\nduration_minutes: ${info.duration}\nexecutor: ${da}\n---\n\n# Session: ${info.focus}\n\n**Date:** ${date}\n**Time:** ${time}\n**Session ID:** ${data.conversation_id}\n\n---\n\n## Session Overview\n\n**Focus:** General development work\n**Duration:** ${info.duration > 0 ? `${info.duration} minutes` : 'Unknown'}\n\n---\n\n## Tools Used\n\n${info.toolsUsed.length > 0 ? info.toolsUsed.map((t: string) => `- ${t}`).join('\\n') : '- None recorded'}\n\n---\n\n## Files Modified\n\n${info.filesChanged.length > 0 ? info.filesChanged.map((f: string) => `- \\`${f}\\``).join('\\n') : '- None recorded'}\n\n**Total Files Changed:** ${info.filesChanged.length}\n\n---\n\n## Commands Executed\n\n${info.commandsExecuted.length > 0 ? '```bash\\n' + info.commandsExecuted.join('\\n') + '\\n```' : 'None recorded'}\n\n---\n\n## Notes\n\nThis session summary was automatically generated by the UOCS SessionEnd hook.\n\nFor detailed tool outputs, see: \\`${join(historyDir(), 'raw-outputs', timestamp.substring(0, 7))}/\\`\n\n---\n\n**Session Outcome:** Completed\n**Generated:** ${new Date().toISOString()}\n`;\n}\n\nasync function storeStructuredSummary(\n sessionId: string,\n info: { focus: string; filesChanged: string[]; commandsExecuted: string[]; toolsUsed: string[]; duration: number }\n): Promise<void> {\n try {\n const cwd = process.cwd();\n const net = await import('net');\n\n await new Promise<void>((resolve, _reject) => {\n const client = net.createConnection('/tmp/pai.sock', () => {\n const msg = JSON.stringify({\n id: 1,\n method: 'session_summary_store',\n params: {\n session_id: sessionId,\n cwd,\n request: null, // We don't have the original request\n investigated: null,\n learned: null,\n completed: info.filesChanged.length > 0\n ? `Modified ${info.filesChanged.length} file(s): ${info.filesChanged.slice(0, 5).join(', ')}`\n : null,\n next_steps: null,\n observation_count: 0, // Will be filled by daemon from actual count\n }\n }) + '\\n';\n client.write(msg);\n });\n\n client.on('data', () => { client.end(); resolve(); });\n client.on('error', () => resolve()); // Silent failure\n setTimeout(() => { client.destroy(); resolve(); }, 3000);\n });\n } catch {\n // Silent failure \u2014 don't disrupt session end\n }\n}\n\nmain();\n", "/**\n * PAI Path Resolution - Single Source of Truth\n *\n * This module provides consistent path resolution across all PAI hooks.\n *\n * Two different things live here, and they must not be confused:\n *\n * - ADAPTER_DIR (~/.claude by default) is the Claude Code harness adapter \u2014\n * fixed, hardcoded paths the harness itself loads from (Hooks/, skills/,\n * agents/, commands/, settings.json, statusline-command.sh,\n * tab-color-command.sh). It is harness-specific: a future harness would\n * need its own adapter directory with its own conventions.\n * - PAI_HOME (~/.claude/pai by default \u2014 see ../../../config/pai-home.ts) is\n * where PAI's own state lives: everything hooks WRITE (History/,\n * agent-sessions.json, session-routing.json, ...) resolves there, with a\n * fallback to the pre-2026-09-19 ADAPTER_DIR location and a one-time\n * stderr notice, exactly like every other PAI_HOME file.\n *\n * ALSO loads .env file from ADAPTER_DIR so all hooks get environment\n * variables without relying on Claude Code's settings.json injection.\n *\n * Usage in hooks:\n * import { ADAPTER_DIR, HOOKS_DIR, SKILLS_DIR, historyDir } from './lib/pai-paths';\n */\n\nimport { homedir } from 'os';\nimport { resolve, join } from 'path';\nimport { existsSync, readFileSync } from 'fs';\nimport {\n paiHomePath,\n resolvePaiFile,\n migratePaiFile,\n migratePaiDir,\n type MigrateFileResult,\n type MigrateDirResult,\n} from '../../../config/pai-home.js';\n\n/**\n * Load .env file and inject into process.env\n * Must run BEFORE ADAPTER_DIR resolution so .env can set ADAPTER_DIR/PAI_DIR if needed\n */\nfunction loadEnvFile(): void {\n // Check common locations for .env\n const possiblePaths = [\n resolve(process.env.ADAPTER_DIR || process.env.PAI_DIR || '', '.env'),\n resolve(homedir(), '.claude', '.env'),\n ];\n\n for (const envPath of possiblePaths) {\n if (existsSync(envPath)) {\n try {\n const content = readFileSync(envPath, 'utf-8');\n for (const line of content.split('\\n')) {\n const trimmed = line.trim();\n // Skip comments and empty lines\n if (!trimmed || trimmed.startsWith('#')) continue;\n\n const eqIndex = trimmed.indexOf('=');\n if (eqIndex > 0) {\n const key = trimmed.substring(0, eqIndex).trim();\n let value = trimmed.substring(eqIndex + 1).trim();\n\n // Remove surrounding quotes if present\n if ((value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n value = value.slice(1, -1);\n }\n\n // Expand $HOME and ~ in values\n value = value.replace(/\\$HOME/g, homedir());\n value = value.replace(/^~(?=\\/|$)/, homedir());\n\n // Only set if not already defined (env vars take precedence)\n if (process.env[key] === undefined) {\n process.env[key] = value;\n }\n }\n }\n // Found and loaded, don't check other paths\n break;\n } catch {\n // Silently continue if .env can't be read\n }\n }\n }\n}\n\n// Load .env FIRST, before any other initialization\nloadEnvFile();\n\nfunction defaultAdapterDir(): string {\n return resolve(homedir(), '.claude');\n}\n\n/**\n * Never print when stdout/stderr is a data channel, not a human: hook\n * bundles and worker-status-line.mjs set PAI_QUIET_NOTICES=1 via their\n * esbuild banner (scripts/build-hooks.mjs), and a `pai worker run\n * --output-format json` invocation is detected directly off argv since its\n * env can't be set before this module's static imports resolve.\n */\nfunction suppressDeprecationNotices(): boolean {\n if (process.env.PAI_QUIET_NOTICES === '1') return true;\n const idx = process.argv.indexOf('--output-format');\n return idx !== -1 && process.argv[idx + 1] === 'json';\n}\n\n/**\n * At most once per process \u2014 a `globalThis` flag rather than a module-level\n * `let` because hooks and the CLI can end up with more than one instance of\n * this module loaded into the same process (separate bundles), each with its\n * own module scope; only a property on the shared global survives that.\n */\nfunction warnPaiDirDeprecatedOnce(message: string): void {\n const g = globalThis as typeof globalThis & { __paiDirNoticePrinted?: boolean };\n if (g.__paiDirNoticePrinted || suppressDeprecationNotices()) return;\n g.__paiDirNoticePrinted = true;\n process.stderr.write(`pai: ${message}\\n`);\n}\n\n/**\n * Smart ADAPTER_DIR detection with fallback\n * Priority:\n * 1. ADAPTER_DIR environment variable (if set) \u2014 warns once if PAI_DIR is\n * ALSO set and resolves to a different path (naming both).\n * 2. PAI_DIR environment variable (deprecated alias, one release) \u2014 warns\n * once only when it differs from the default adapter root; PAI_DIR set to\n * the same value as the default is the operator's current, correct\n * setting and stays silent.\n * 3. ~/.claude (standard location)\n */\nfunction resolveAdapterDir(): string {\n if (process.env.ADAPTER_DIR) {\n const adapterDir = resolve(process.env.ADAPTER_DIR);\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== adapterDir) {\n warnPaiDirDeprecatedOnce(\n `ADAPTER_DIR (${adapterDir}) and PAI_DIR (${paiDir}) are both set and differ \u2014 ADAPTER_DIR wins. PAI_DIR still works for one release.`\n );\n }\n }\n return adapterDir;\n }\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== defaultAdapterDir()) {\n warnPaiDirDeprecatedOnce(\n 'PAI_DIR is deprecated \u2014 use ADAPTER_DIR for the harness adapter root (~/.claude). PAI_DIR still works for one release.'\n );\n }\n return paiDir;\n }\n return defaultAdapterDir();\n}\n\n/** The Claude Code harness adapter root \u2014 NOT PAI's state home. See module doc above. */\nexport const ADAPTER_DIR = resolveAdapterDir();\n\n/** @deprecated alias for ADAPTER_DIR, kept for one release. Prefer ADAPTER_DIR. */\nexport const PAI_DIR = ADAPTER_DIR;\n\n/**\n * Adapter directories \u2014 fixed paths the Claude Code harness itself loads\n * from. These stay under ADAPTER_DIR; they are not PAI state.\n */\nexport const HOOKS_DIR = join(ADAPTER_DIR, 'Hooks');\nexport const SKILLS_DIR = join(ADAPTER_DIR, 'skills');\nexport const AGENTS_DIR = join(ADAPTER_DIR, 'agents');\nexport const COMMANDS_DIR = join(ADAPTER_DIR, 'commands');\n\n// ---------------------------------------------------------------------------\n// PAI state written by hooks \u2014 resolves under PAI_HOME, falling back to the\n// pre-2026-09-19 ADAPTER_DIR location (one-time stderr notice) until\n// `pai config migrate --history` moves it. Actively written on every hook\n// event across every session, so unlike most PAI_HOME files the live move is\n// deliberately NOT automatic \u2014 see `pai config migrate --history`.\n// ---------------------------------------------------------------------------\n\nfunction oldHistoryDir(): string {\n return join(ADAPTER_DIR, 'History');\n}\n\n/** Read/write location for hook-captured history: PAI_HOME/History if\n * present, else the old ADAPTER_DIR/History (one-time stderr notice). */\nexport function historyDir(): string {\n return resolvePaiFile(paiHomePath('History'), [oldHistoryDir()], 'pai config migrate --history');\n}\n\nexport function migrateHistoryDir(opts: { dryRun?: boolean } = {}): MigrateDirResult {\n return migratePaiDir(paiHomePath('History'), [oldHistoryDir()], opts);\n}\n\nfunction oldAgentSessionsPath(): string {\n return join(ADAPTER_DIR, 'agent-sessions.json');\n}\n\nexport function agentSessionsPath(): string {\n return resolvePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], 'pai config migrate --history');\n}\n\nexport function migrateAgentSessions(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], opts);\n}\n\nfunction oldSecurityEventsPath(): string {\n return join(ADAPTER_DIR, 'history', 'security', 'security-events.jsonl');\n}\n\nexport function securityEventsPath(): string {\n return resolvePaiFile(\n paiHomePath('History', 'security', 'security-events.jsonl'),\n [oldSecurityEventsPath()],\n 'pai config migrate --history'\n );\n}\n\nexport function migrateSecurityEvents(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('History', 'security', 'security-events.jsonl'), [oldSecurityEventsPath()], opts);\n}\n\nfunction oldSessionRoutingPath(): string {\n return join(ADAPTER_DIR, 'session-routing.json');\n}\n\nexport function sessionRoutingPath(): string {\n return resolvePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], 'pai config migrate --history');\n}\n\nexport function migrateSessionRouting(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], opts);\n}\n\n/**\n * Helper to get history file path with date-based organization\n */\nexport function getHistoryFilePath(subdir: string, filename: string): string {\n const now = new Date();\n const tz = process.env.TIME_ZONE || Intl.DateTimeFormat().resolvedOptions().timeZone;\n const localDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));\n const year = localDate.getFullYear();\n const month = String(localDate.getMonth() + 1).padStart(2, '0');\n\n return join(historyDir(), subdir, `${year}-${month}`, filename);\n}\n", "/**\n * pai-home.ts \u2014 the PAI_HOME namespace directory and generic per-user file\n * migration into it.\n *\n * Every PAI-owned per-user file (workers.yaml, config.json, whisper-rules.md,\n * advisor-mode.json, ...) lives under PAI_HOME (~/.claude/pai by default) so\n * nothing PAI writes can ever collide with a file Claude Code itself\n * introduces under ~/.claude. Decided 2026-09-19 \u2014 see docs/workers-config.md.\n */\n\nimport { existsSync, mkdirSync, readFileSync, copyFileSync, renameSync, readdirSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, dirname } from \"node:path\";\nimport { execFileSync } from \"node:child_process\";\n\n/**\n * Byte-identical check that works for files of any size. `cmp` compares by\n * streaming rather than loading either file into memory, unlike a\n * readFileSync + Buffer.compare \u2014 which throws (\"File size is greater than\n * 2 GiB\") on anything past Node's 2GiB single-read ceiling, a real limit hit\n * migrating a multi-gigabyte legacy federation database file.\n */\nfunction filesByteIdentical(a: string, b: string): boolean {\n try {\n execFileSync(\"cmp\", [\"-s\", a, b], { stdio: \"pipe\" });\n return true;\n } catch (e) {\n if (e && typeof e === \"object\" && \"status\" in e && (e as { status: number }).status === 1) return false;\n // cmp missing or errored for an unrelated reason \u2014 fall back to an\n // in-memory compare (fine for the small files this path normally sees).\n return Buffer.compare(readFileSync(a), readFileSync(b)) === 0;\n }\n}\n\n/** PAI_HOME (test isolation, power users) overrides the default namespace dir. */\nexport function paiHomeDir(): string {\n return process.env.PAI_HOME || join(homedir(), \".claude\", \"pai\");\n}\n\n/** A path under PAI_HOME, e.g. `paiHomePath(\"config.json\")`. */\nexport function paiHomePath(...segments: string[]): string {\n return join(paiHomeDir(), ...segments);\n}\n\nconst noticesPrinted = new Set<string>();\n\n/**\n * Resolve a per-user PAI file: the new PAI_HOME path if it exists, else the\n * first existing entry in `oldCandidates` (checked in order \u2014 most recent\n * old location first), else the new path (the target a first write\n * creates). Prints one stderr notice per process per new-path when a\n * fallback location is actually used.\n */\nexport function resolvePaiFile(newPath: string, oldCandidates: string[], migrateHint: string): string {\n if (existsSync(newPath)) return newPath;\n for (const old of oldCandidates) {\n if (existsSync(old)) {\n if (!noticesPrinted.has(newPath)) {\n noticesPrinted.add(newPath);\n process.stderr.write(\n `pai: ${old} is at an old location \u2014 run \\`${migrateHint}\\` to move it to ${newPath}\\n`\n );\n }\n return old;\n }\n }\n return newPath;\n}\n\nexport class PaiFileMigrationError extends Error {}\n\nexport interface MigrateFileResult {\n fromPath: string | null;\n toPath: string;\n dryRun: boolean;\n note?: string;\n}\n\n/**\n * Move a per-user PAI file to its new PAI_HOME location: copy, verify\n * byte-identical, then rename the source to `<name>.migrated-<YYYYMMDD>`\n * (never deleted). Idempotent: if the new path already holds bytes\n * identical to the found source, the source is just renamed aside; if it\n * differs, this refuses rather than overwrite silently.\n */\nexport function migratePaiFile(\n newPath: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateFileResult {\n const fromPath = oldCandidates.find((p) => existsSync(p)) ?? null;\n if (!fromPath) {\n const note = existsSync(newPath) ? \"already at new location\" : \"nothing to migrate \u2014 file does not exist yet\";\n return { fromPath: null, toPath: newPath, dryRun: !!opts.dryRun, note };\n }\n if (opts.dryRun) return { fromPath, toPath: newPath, dryRun: true };\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n\n if (existsSync(newPath)) {\n if (filesByteIdentical(fromPath, newPath)) {\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false, note: \"identical \u2014 old file renamed aside\" };\n }\n throw new PaiFileMigrationError(\n `${newPath} already exists and differs from ${fromPath} \u2014 resolve manually, nothing changed`\n );\n }\n\n const dir = dirname(newPath);\n if (!existsSync(dir)) mkdirSync(dir, { recursive: true });\n copyFileSync(fromPath, newPath);\n\n if (!filesByteIdentical(fromPath, newPath)) {\n throw new PaiFileMigrationError(\n `${newPath}: copy did not match ${fromPath} byte-for-byte \u2014 aborting, old file left in place`\n );\n }\n\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false };\n}\n\nexport interface MigrateDirResult {\n fromDir: string | null;\n toDir: string;\n dryRun: boolean;\n movedCount?: number;\n note?: string;\n}\n\n/**\n * Move a per-user PAI directory (queries/, session-state/, ...) into\n * PAI_HOME: move every entry from the first found old candidate into the\n * new dir (never overwriting an existing entry there), then rename the now-\n * empty old dir aside to `<name>.migrated-<YYYYMMDD>` (never deleted).\n * Idempotent \u2014 an already-migrated dir has nothing left to find.\n */\nexport function migratePaiDir(\n newDir: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateDirResult {\n const fromDir = oldCandidates.find((p) => existsSync(p) && statSync(p).isDirectory()) ?? null;\n if (!fromDir) {\n const note = existsSync(newDir) ? \"already at new location\" : \"nothing to migrate \u2014 directory does not exist yet\";\n return { fromDir: null, toDir: newDir, dryRun: !!opts.dryRun, note };\n }\n\n const entries = readdirSync(fromDir);\n if (opts.dryRun) return { fromDir, toDir: newDir, dryRun: true, movedCount: entries.length };\n\n if (!existsSync(newDir)) mkdirSync(newDir, { recursive: true });\n\n let moved = 0;\n const collided: string[] = [];\n for (const entry of entries) {\n const src = join(fromDir, entry);\n const dst = join(newDir, entry);\n if (existsSync(dst)) {\n collided.push(entry);\n continue;\n }\n renameSync(src, dst);\n moved++;\n }\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n const remaining = readdirSync(fromDir);\n const note = collided.length\n ? `${collided.length} entrie(s) left in ${fromDir} \u2014 name already existed in ${newDir}`\n : undefined;\n\n if (remaining.length === 0) {\n renameSync(fromDir, `${fromDir}.migrated-${stamp}`);\n }\n\n return { fromDir, toDir: newDir, dryRun: false, movedCount: moved, note };\n}\n"],
5
+ "mappings": ";;;;;;AASA,SAAS,eAAe,aAAAA,YAAW,cAAAC,aAAY,gBAAAC,eAAc,eAAAC,oBAAmB;AAChF,SAAS,QAAAC,aAAY;;;ACerB,SAAS,WAAAC,gBAAe;AACxB,SAAS,SAAS,QAAAC,aAAY;AAC9B,SAAS,cAAAC,aAAY,gBAAAC,qBAAoB;;;ACjBzC,SAAS,YAAY,WAAW,cAAc,cAAc,YAAY,aAAa,gBAAgB;AACrG,SAAS,eAAe;AACxB,SAAS,MAAM,eAAe;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pD,aAAO,GAAG,SAAS,MAAMA,SAAQ,CAAC;AAClC,iBAAW,MAAM;AAAE,eAAO,QAAQ;AAAG,QAAAA,SAAQ;AAAA,MAAG,GAAG,GAAI;AAAA,IACzD,CAAC;AAAA,EACH,QAAQ;AAAA,EAER;AACF;AAEA,KAAK;",
6
+ "names": ["mkdirSync", "existsSync", "readFileSync", "readdirSync", "join", "homedir", "join", "existsSync", "readFileSync", "homedir", "existsSync", "readFileSync", "join", "join", "existsSync", "mkdirSync", "readdirSync", "readFileSync", "resolve"]
7
7
  }
@@ -115,23 +115,9 @@ function resolveAdapterDir() {
115
115
  }
116
116
  var ADAPTER_DIR = resolveAdapterDir();
117
117
  var HOOKS_DIR = join2(ADAPTER_DIR, "Hooks");
118
- var SKILLS_DIR = join2(ADAPTER_DIR, "Skills");
119
- var AGENTS_DIR = join2(ADAPTER_DIR, "Agents");
120
- var COMMANDS_DIR = join2(ADAPTER_DIR, "Commands");
121
- function validatePAIStructure() {
122
- if (!existsSync2(ADAPTER_DIR)) {
123
- console.error(`ADAPTER_DIR does not exist: ${ADAPTER_DIR}`);
124
- console.error(` Expected ~/.claude or set ADAPTER_DIR environment variable`);
125
- process.exit(1);
126
- }
127
- if (!existsSync2(HOOKS_DIR)) {
128
- console.error(`PAI hooks directory not found: ${HOOKS_DIR}`);
129
- console.error(` Your ADAPTER_DIR may be misconfigured`);
130
- console.error(` Current ADAPTER_DIR: ${ADAPTER_DIR}`);
131
- process.exit(1);
132
- }
133
- }
134
- validatePAIStructure();
118
+ var SKILLS_DIR = join2(ADAPTER_DIR, "skills");
119
+ var AGENTS_DIR = join2(ADAPTER_DIR, "agents");
120
+ var COMMANDS_DIR = join2(ADAPTER_DIR, "commands");
135
121
  function oldHistoryDir() {
136
122
  return join2(ADAPTER_DIR, "History");
137
123
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/hooks/ts/post-tool-use/capture-tool-output.ts", "../../../src/hooks/ts/lib/pai-paths.ts", "../../../src/config/pai-home.ts"],
4
- "sourcesContent": ["#!/usr/bin/env node\n\n/**\n * PostToolUse Hook - Captures tool outputs for UOCS\n *\n * Automatically logs all tool executions to daily JSONL files\n * for later processing and analysis.\n */\n\nimport { appendFileSync, mkdirSync, existsSync } from 'fs';\nimport { join } from 'path';\nimport { historyDir } from '../lib/pai-paths.js';\n\ninterface ToolUseData {\n tool_name: string;\n tool_input: Record<string, any>;\n tool_response: Record<string, any>;\n conversation_id: string;\n timestamp: string;\n}\n\n// Configuration\nconst CAPTURE_DIR = join(historyDir(), 'raw-outputs');\nconst INTERESTING_TOOLS = ['Bash', 'Edit', 'Write', 'Read', 'Task', 'NotebookEdit'];\n\nasync function main() {\n try {\n // Read input from stdin\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk);\n }\n const input = Buffer.concat(chunks).toString('utf-8');\n if (!input || input.trim() === '') {\n process.exit(0);\n }\n\n const data: ToolUseData = JSON.parse(input);\n\n // Only capture interesting tools\n if (!INTERESTING_TOOLS.includes(data.tool_name)) {\n process.exit(0);\n }\n\n // Get today's date for organization\n const now = new Date();\n const today = now.toISOString().split('T')[0]; // YYYY-MM-DD\n const yearMonth = today.substring(0, 7); // YYYY-MM\n\n // Ensure capture directory exists\n const dateDir = join(CAPTURE_DIR, yearMonth);\n if (!existsSync(dateDir)) {\n mkdirSync(dateDir, { recursive: true });\n }\n\n // Format output as JSONL (one JSON object per line)\n const captureFile = join(dateDir, `${today}_tool-outputs.jsonl`);\n const captureEntry = JSON.stringify({\n timestamp: data.timestamp || now.toISOString(),\n tool: data.tool_name,\n input: data.tool_input,\n output: data.tool_response,\n session: data.conversation_id\n }) + '\\n';\n\n // Append to daily log\n appendFileSync(captureFile, captureEntry);\n\n // Exit successfully (code 0 = continue normally)\n process.exit(0);\n } catch (error) {\n // Silent failure - don't disrupt workflow\n console.error(`[UOCS] PostToolUse hook error: ${error}`);\n process.exit(0);\n }\n}\n\nmain();\n", "/**\n * PAI Path Resolution - Single Source of Truth\n *\n * This module provides consistent path resolution across all PAI hooks.\n *\n * Two different things live here, and they must not be confused:\n *\n * - ADAPTER_DIR (~/.claude by default) is the Claude Code harness adapter \u2014\n * fixed, hardcoded paths the harness itself loads from (Hooks/, Skills/,\n * Agents/, Commands/, settings.json, statusline-command.sh,\n * tab-color-command.sh). It is harness-specific: a future harness would\n * need its own adapter directory with its own conventions.\n * - PAI_HOME (~/.claude/pai by default \u2014 see ../../../config/pai-home.ts) is\n * where PAI's own state lives: everything hooks WRITE (History/,\n * agent-sessions.json, session-routing.json, ...) resolves there, with a\n * fallback to the pre-2026-09-19 ADAPTER_DIR location and a one-time\n * stderr notice, exactly like every other PAI_HOME file.\n *\n * ALSO loads .env file from ADAPTER_DIR so all hooks get environment\n * variables without relying on Claude Code's settings.json injection.\n *\n * Usage in hooks:\n * import { ADAPTER_DIR, HOOKS_DIR, SKILLS_DIR, historyDir } from './lib/pai-paths';\n */\n\nimport { homedir } from 'os';\nimport { resolve, join } from 'path';\nimport { existsSync, readFileSync } from 'fs';\nimport {\n paiHomePath,\n resolvePaiFile,\n migratePaiFile,\n migratePaiDir,\n type MigrateFileResult,\n type MigrateDirResult,\n} from '../../../config/pai-home.js';\n\n/**\n * Load .env file and inject into process.env\n * Must run BEFORE ADAPTER_DIR resolution so .env can set ADAPTER_DIR/PAI_DIR if needed\n */\nfunction loadEnvFile(): void {\n // Check common locations for .env\n const possiblePaths = [\n resolve(process.env.ADAPTER_DIR || process.env.PAI_DIR || '', '.env'),\n resolve(homedir(), '.claude', '.env'),\n ];\n\n for (const envPath of possiblePaths) {\n if (existsSync(envPath)) {\n try {\n const content = readFileSync(envPath, 'utf-8');\n for (const line of content.split('\\n')) {\n const trimmed = line.trim();\n // Skip comments and empty lines\n if (!trimmed || trimmed.startsWith('#')) continue;\n\n const eqIndex = trimmed.indexOf('=');\n if (eqIndex > 0) {\n const key = trimmed.substring(0, eqIndex).trim();\n let value = trimmed.substring(eqIndex + 1).trim();\n\n // Remove surrounding quotes if present\n if ((value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n value = value.slice(1, -1);\n }\n\n // Expand $HOME and ~ in values\n value = value.replace(/\\$HOME/g, homedir());\n value = value.replace(/^~(?=\\/|$)/, homedir());\n\n // Only set if not already defined (env vars take precedence)\n if (process.env[key] === undefined) {\n process.env[key] = value;\n }\n }\n }\n // Found and loaded, don't check other paths\n break;\n } catch {\n // Silently continue if .env can't be read\n }\n }\n }\n}\n\n// Load .env FIRST, before any other initialization\nloadEnvFile();\n\nfunction defaultAdapterDir(): string {\n return resolve(homedir(), '.claude');\n}\n\n/**\n * Never print when stdout/stderr is a data channel, not a human: hook\n * bundles and worker-status-line.mjs set PAI_QUIET_NOTICES=1 via their\n * esbuild banner (scripts/build-hooks.mjs), and a `pai worker run\n * --output-format json` invocation is detected directly off argv since its\n * env can't be set before this module's static imports resolve.\n */\nfunction suppressDeprecationNotices(): boolean {\n if (process.env.PAI_QUIET_NOTICES === '1') return true;\n const idx = process.argv.indexOf('--output-format');\n return idx !== -1 && process.argv[idx + 1] === 'json';\n}\n\n/**\n * At most once per process \u2014 a `globalThis` flag rather than a module-level\n * `let` because hooks and the CLI can end up with more than one instance of\n * this module loaded into the same process (separate bundles), each with its\n * own module scope; only a property on the shared global survives that.\n */\nfunction warnPaiDirDeprecatedOnce(message: string): void {\n const g = globalThis as typeof globalThis & { __paiDirNoticePrinted?: boolean };\n if (g.__paiDirNoticePrinted || suppressDeprecationNotices()) return;\n g.__paiDirNoticePrinted = true;\n process.stderr.write(`pai: ${message}\\n`);\n}\n\n/**\n * Smart ADAPTER_DIR detection with fallback\n * Priority:\n * 1. ADAPTER_DIR environment variable (if set) \u2014 warns once if PAI_DIR is\n * ALSO set and resolves to a different path (naming both).\n * 2. PAI_DIR environment variable (deprecated alias, one release) \u2014 warns\n * once only when it differs from the default adapter root; PAI_DIR set to\n * the same value as the default is the operator's current, correct\n * setting and stays silent.\n * 3. ~/.claude (standard location)\n */\nfunction resolveAdapterDir(): string {\n if (process.env.ADAPTER_DIR) {\n const adapterDir = resolve(process.env.ADAPTER_DIR);\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== adapterDir) {\n warnPaiDirDeprecatedOnce(\n `ADAPTER_DIR (${adapterDir}) and PAI_DIR (${paiDir}) are both set and differ \u2014 ADAPTER_DIR wins. PAI_DIR still works for one release.`\n );\n }\n }\n return adapterDir;\n }\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== defaultAdapterDir()) {\n warnPaiDirDeprecatedOnce(\n 'PAI_DIR is deprecated \u2014 use ADAPTER_DIR for the harness adapter root (~/.claude). PAI_DIR still works for one release.'\n );\n }\n return paiDir;\n }\n return defaultAdapterDir();\n}\n\n/** The Claude Code harness adapter root \u2014 NOT PAI's state home. See module doc above. */\nexport const ADAPTER_DIR = resolveAdapterDir();\n\n/** @deprecated alias for ADAPTER_DIR, kept for one release. Prefer ADAPTER_DIR. */\nexport const PAI_DIR = ADAPTER_DIR;\n\n/**\n * Adapter directories \u2014 fixed paths the Claude Code harness itself loads\n * from. These stay under ADAPTER_DIR; they are not PAI state.\n */\nexport const HOOKS_DIR = join(ADAPTER_DIR, 'Hooks');\nexport const SKILLS_DIR = join(ADAPTER_DIR, 'Skills');\nexport const AGENTS_DIR = join(ADAPTER_DIR, 'Agents');\nexport const COMMANDS_DIR = join(ADAPTER_DIR, 'Commands');\n\n/**\n * Validate PAI directory structure on first import\n * This fails fast with a clear error if PAI is misconfigured\n */\nfunction validatePAIStructure(): void {\n if (!existsSync(ADAPTER_DIR)) {\n console.error(`ADAPTER_DIR does not exist: ${ADAPTER_DIR}`);\n console.error(` Expected ~/.claude or set ADAPTER_DIR environment variable`);\n process.exit(1);\n }\n\n if (!existsSync(HOOKS_DIR)) {\n console.error(`PAI hooks directory not found: ${HOOKS_DIR}`);\n console.error(` Your ADAPTER_DIR may be misconfigured`);\n console.error(` Current ADAPTER_DIR: ${ADAPTER_DIR}`);\n process.exit(1);\n }\n}\n\n// Run validation on module import\n// This ensures any hook that imports this module will fail fast if paths are wrong\nvalidatePAIStructure();\n\n// ---------------------------------------------------------------------------\n// PAI state written by hooks \u2014 resolves under PAI_HOME, falling back to the\n// pre-2026-09-19 ADAPTER_DIR location (one-time stderr notice) until\n// `pai config migrate --history` moves it. Actively written on every hook\n// event across every session, so unlike most PAI_HOME files the live move is\n// deliberately NOT automatic \u2014 see `pai config migrate --history`.\n// ---------------------------------------------------------------------------\n\nfunction oldHistoryDir(): string {\n return join(ADAPTER_DIR, 'History');\n}\n\n/** Read/write location for hook-captured history: PAI_HOME/History if\n * present, else the old ADAPTER_DIR/History (one-time stderr notice). */\nexport function historyDir(): string {\n return resolvePaiFile(paiHomePath('History'), [oldHistoryDir()], 'pai config migrate --history');\n}\n\nexport function migrateHistoryDir(opts: { dryRun?: boolean } = {}): MigrateDirResult {\n return migratePaiDir(paiHomePath('History'), [oldHistoryDir()], opts);\n}\n\nfunction oldAgentSessionsPath(): string {\n return join(ADAPTER_DIR, 'agent-sessions.json');\n}\n\nexport function agentSessionsPath(): string {\n return resolvePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], 'pai config migrate --history');\n}\n\nexport function migrateAgentSessions(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], opts);\n}\n\nfunction oldSecurityEventsPath(): string {\n return join(ADAPTER_DIR, 'history', 'security', 'security-events.jsonl');\n}\n\nexport function securityEventsPath(): string {\n return resolvePaiFile(\n paiHomePath('History', 'security', 'security-events.jsonl'),\n [oldSecurityEventsPath()],\n 'pai config migrate --history'\n );\n}\n\nexport function migrateSecurityEvents(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('History', 'security', 'security-events.jsonl'), [oldSecurityEventsPath()], opts);\n}\n\nfunction oldSessionRoutingPath(): string {\n return join(ADAPTER_DIR, 'session-routing.json');\n}\n\nexport function sessionRoutingPath(): string {\n return resolvePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], 'pai config migrate --history');\n}\n\nexport function migrateSessionRouting(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], opts);\n}\n\n/**\n * Helper to get history file path with date-based organization\n */\nexport function getHistoryFilePath(subdir: string, filename: string): string {\n const now = new Date();\n const tz = process.env.TIME_ZONE || Intl.DateTimeFormat().resolvedOptions().timeZone;\n const localDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));\n const year = localDate.getFullYear();\n const month = String(localDate.getMonth() + 1).padStart(2, '0');\n\n return join(historyDir(), subdir, `${year}-${month}`, filename);\n}\n", "/**\n * pai-home.ts \u2014 the PAI_HOME namespace directory and generic per-user file\n * migration into it.\n *\n * Every PAI-owned per-user file (workers.yaml, config.json, whisper-rules.md,\n * advisor-mode.json, ...) lives under PAI_HOME (~/.claude/pai by default) so\n * nothing PAI writes can ever collide with a file Claude Code itself\n * introduces under ~/.claude. Decided 2026-09-19 \u2014 see docs/workers-config.md.\n */\n\nimport { existsSync, mkdirSync, readFileSync, copyFileSync, renameSync, readdirSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, dirname } from \"node:path\";\nimport { execFileSync } from \"node:child_process\";\n\n/**\n * Byte-identical check that works for files of any size. `cmp` compares by\n * streaming rather than loading either file into memory, unlike a\n * readFileSync + Buffer.compare \u2014 which throws (\"File size is greater than\n * 2 GiB\") on anything past Node's 2GiB single-read ceiling, a real limit hit\n * migrating a multi-gigabyte legacy federation database file.\n */\nfunction filesByteIdentical(a: string, b: string): boolean {\n try {\n execFileSync(\"cmp\", [\"-s\", a, b], { stdio: \"pipe\" });\n return true;\n } catch (e) {\n if (e && typeof e === \"object\" && \"status\" in e && (e as { status: number }).status === 1) return false;\n // cmp missing or errored for an unrelated reason \u2014 fall back to an\n // in-memory compare (fine for the small files this path normally sees).\n return Buffer.compare(readFileSync(a), readFileSync(b)) === 0;\n }\n}\n\n/** PAI_HOME (test isolation, power users) overrides the default namespace dir. */\nexport function paiHomeDir(): string {\n return process.env.PAI_HOME || join(homedir(), \".claude\", \"pai\");\n}\n\n/** A path under PAI_HOME, e.g. `paiHomePath(\"config.json\")`. */\nexport function paiHomePath(...segments: string[]): string {\n return join(paiHomeDir(), ...segments);\n}\n\nconst noticesPrinted = new Set<string>();\n\n/**\n * Resolve a per-user PAI file: the new PAI_HOME path if it exists, else the\n * first existing entry in `oldCandidates` (checked in order \u2014 most recent\n * old location first), else the new path (the target a first write\n * creates). Prints one stderr notice per process per new-path when a\n * fallback location is actually used.\n */\nexport function resolvePaiFile(newPath: string, oldCandidates: string[], migrateHint: string): string {\n if (existsSync(newPath)) return newPath;\n for (const old of oldCandidates) {\n if (existsSync(old)) {\n if (!noticesPrinted.has(newPath)) {\n noticesPrinted.add(newPath);\n process.stderr.write(\n `pai: ${old} is at an old location \u2014 run \\`${migrateHint}\\` to move it to ${newPath}\\n`\n );\n }\n return old;\n }\n }\n return newPath;\n}\n\nexport class PaiFileMigrationError extends Error {}\n\nexport interface MigrateFileResult {\n fromPath: string | null;\n toPath: string;\n dryRun: boolean;\n note?: string;\n}\n\n/**\n * Move a per-user PAI file to its new PAI_HOME location: copy, verify\n * byte-identical, then rename the source to `<name>.migrated-<YYYYMMDD>`\n * (never deleted). Idempotent: if the new path already holds bytes\n * identical to the found source, the source is just renamed aside; if it\n * differs, this refuses rather than overwrite silently.\n */\nexport function migratePaiFile(\n newPath: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateFileResult {\n const fromPath = oldCandidates.find((p) => existsSync(p)) ?? null;\n if (!fromPath) {\n const note = existsSync(newPath) ? \"already at new location\" : \"nothing to migrate \u2014 file does not exist yet\";\n return { fromPath: null, toPath: newPath, dryRun: !!opts.dryRun, note };\n }\n if (opts.dryRun) return { fromPath, toPath: newPath, dryRun: true };\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n\n if (existsSync(newPath)) {\n if (filesByteIdentical(fromPath, newPath)) {\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false, note: \"identical \u2014 old file renamed aside\" };\n }\n throw new PaiFileMigrationError(\n `${newPath} already exists and differs from ${fromPath} \u2014 resolve manually, nothing changed`\n );\n }\n\n const dir = dirname(newPath);\n if (!existsSync(dir)) mkdirSync(dir, { recursive: true });\n copyFileSync(fromPath, newPath);\n\n if (!filesByteIdentical(fromPath, newPath)) {\n throw new PaiFileMigrationError(\n `${newPath}: copy did not match ${fromPath} byte-for-byte \u2014 aborting, old file left in place`\n );\n }\n\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false };\n}\n\nexport interface MigrateDirResult {\n fromDir: string | null;\n toDir: string;\n dryRun: boolean;\n movedCount?: number;\n note?: string;\n}\n\n/**\n * Move a per-user PAI directory (queries/, session-state/, ...) into\n * PAI_HOME: move every entry from the first found old candidate into the\n * new dir (never overwriting an existing entry there), then rename the now-\n * empty old dir aside to `<name>.migrated-<YYYYMMDD>` (never deleted).\n * Idempotent \u2014 an already-migrated dir has nothing left to find.\n */\nexport function migratePaiDir(\n newDir: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateDirResult {\n const fromDir = oldCandidates.find((p) => existsSync(p) && statSync(p).isDirectory()) ?? null;\n if (!fromDir) {\n const note = existsSync(newDir) ? \"already at new location\" : \"nothing to migrate \u2014 directory does not exist yet\";\n return { fromDir: null, toDir: newDir, dryRun: !!opts.dryRun, note };\n }\n\n const entries = readdirSync(fromDir);\n if (opts.dryRun) return { fromDir, toDir: newDir, dryRun: true, movedCount: entries.length };\n\n if (!existsSync(newDir)) mkdirSync(newDir, { recursive: true });\n\n let moved = 0;\n const collided: string[] = [];\n for (const entry of entries) {\n const src = join(fromDir, entry);\n const dst = join(newDir, entry);\n if (existsSync(dst)) {\n collided.push(entry);\n continue;\n }\n renameSync(src, dst);\n moved++;\n }\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n const remaining = readdirSync(fromDir);\n const note = collided.length\n ? `${collided.length} entrie(s) left in ${fromDir} \u2014 name already existed in ${newDir}`\n : undefined;\n\n if (remaining.length === 0) {\n renameSync(fromDir, `${fromDir}.migrated-${stamp}`);\n }\n\n return { fromDir, toDir: newDir, dryRun: false, movedCount: moved, note };\n}\n"],
5
- "mappings": ";;;;;;AASA,SAAS,gBAAgB,aAAAA,YAAW,cAAAC,mBAAkB;AACtD,SAAS,QAAAC,aAAY;;;ACerB,SAAS,WAAAC,gBAAe;AACxB,SAAS,SAAS,QAAAC,aAAY;AAC9B,SAAS,cAAAC,aAAY,gBAAAC,qBAAoB;;;ACjBzC,SAAS,YAAY,WAAW,cAAc,cAAc,YAAY,aAAa,gBAAgB;AACrG,SAAS,eAAe;AACxB,SAAS,MAAM,eAAe;AAuBvB,SAAS,aAAqB;AACnC,SAAO,QAAQ,IAAI,YAAY,KAAK,QAAQ,GAAG,WAAW,KAAK;AACjE;AAGO,SAAS,eAAe,UAA4B;AACzD,SAAO,KAAK,WAAW,GAAG,GAAG,QAAQ;AACvC;AAEA,IAAM,iBAAiB,oBAAI,IAAY;AAShC,SAAS,eAAe,SAAiB,eAAyB,aAA6B;AACpG,MAAI,WAAW,OAAO,EAAG,QAAO;AAChC,aAAW,OAAO,eAAe;AAC/B,QAAI,WAAW,GAAG,GAAG;AACnB,UAAI,CAAC,eAAe,IAAI,OAAO,GAAG;AAChC,uBAAe,IAAI,OAAO;AAC1B,gBAAQ,OAAO;AAAA,UACb,QAAQ,GAAG,uCAAkC,WAAW,oBAAoB,OAAO;AAAA;AAAA,QACrF;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;;;AD1BA,SAAS,cAAoB;AAE3B,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AAS,OAAO;AAEd,YAAQ,MAAM,kCAAkC,KAAK,EAAE;AACvD,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;AAEA,KAAK;",
6
- "names": ["mkdirSync", "existsSync", "join", "homedir", "join", "existsSync", "readFileSync", "homedir", "existsSync", "readFileSync", "join", "existsSync", "join", "existsSync", "mkdirSync"]
4
+ "sourcesContent": ["#!/usr/bin/env node\n\n/**\n * PostToolUse Hook - Captures tool outputs for UOCS\n *\n * Automatically logs all tool executions to daily JSONL files\n * for later processing and analysis.\n */\n\nimport { appendFileSync, mkdirSync, existsSync } from 'fs';\nimport { join } from 'path';\nimport { historyDir } from '../lib/pai-paths.js';\n\ninterface ToolUseData {\n tool_name: string;\n tool_input: Record<string, any>;\n tool_response: Record<string, any>;\n conversation_id: string;\n timestamp: string;\n}\n\n// Configuration\nconst CAPTURE_DIR = join(historyDir(), 'raw-outputs');\nconst INTERESTING_TOOLS = ['Bash', 'Edit', 'Write', 'Read', 'Task', 'NotebookEdit'];\n\nasync function main() {\n try {\n // Read input from stdin\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk);\n }\n const input = Buffer.concat(chunks).toString('utf-8');\n if (!input || input.trim() === '') {\n process.exit(0);\n }\n\n const data: ToolUseData = JSON.parse(input);\n\n // Only capture interesting tools\n if (!INTERESTING_TOOLS.includes(data.tool_name)) {\n process.exit(0);\n }\n\n // Get today's date for organization\n const now = new Date();\n const today = now.toISOString().split('T')[0]; // YYYY-MM-DD\n const yearMonth = today.substring(0, 7); // YYYY-MM\n\n // Ensure capture directory exists\n const dateDir = join(CAPTURE_DIR, yearMonth);\n if (!existsSync(dateDir)) {\n mkdirSync(dateDir, { recursive: true });\n }\n\n // Format output as JSONL (one JSON object per line)\n const captureFile = join(dateDir, `${today}_tool-outputs.jsonl`);\n const captureEntry = JSON.stringify({\n timestamp: data.timestamp || now.toISOString(),\n tool: data.tool_name,\n input: data.tool_input,\n output: data.tool_response,\n session: data.conversation_id\n }) + '\\n';\n\n // Append to daily log\n appendFileSync(captureFile, captureEntry);\n\n // Exit successfully (code 0 = continue normally)\n process.exit(0);\n } catch (error) {\n // Silent failure - don't disrupt workflow\n console.error(`[UOCS] PostToolUse hook error: ${error}`);\n process.exit(0);\n }\n}\n\nmain();\n", "/**\n * PAI Path Resolution - Single Source of Truth\n *\n * This module provides consistent path resolution across all PAI hooks.\n *\n * Two different things live here, and they must not be confused:\n *\n * - ADAPTER_DIR (~/.claude by default) is the Claude Code harness adapter \u2014\n * fixed, hardcoded paths the harness itself loads from (Hooks/, skills/,\n * agents/, commands/, settings.json, statusline-command.sh,\n * tab-color-command.sh). It is harness-specific: a future harness would\n * need its own adapter directory with its own conventions.\n * - PAI_HOME (~/.claude/pai by default \u2014 see ../../../config/pai-home.ts) is\n * where PAI's own state lives: everything hooks WRITE (History/,\n * agent-sessions.json, session-routing.json, ...) resolves there, with a\n * fallback to the pre-2026-09-19 ADAPTER_DIR location and a one-time\n * stderr notice, exactly like every other PAI_HOME file.\n *\n * ALSO loads .env file from ADAPTER_DIR so all hooks get environment\n * variables without relying on Claude Code's settings.json injection.\n *\n * Usage in hooks:\n * import { ADAPTER_DIR, HOOKS_DIR, SKILLS_DIR, historyDir } from './lib/pai-paths';\n */\n\nimport { homedir } from 'os';\nimport { resolve, join } from 'path';\nimport { existsSync, readFileSync } from 'fs';\nimport {\n paiHomePath,\n resolvePaiFile,\n migratePaiFile,\n migratePaiDir,\n type MigrateFileResult,\n type MigrateDirResult,\n} from '../../../config/pai-home.js';\n\n/**\n * Load .env file and inject into process.env\n * Must run BEFORE ADAPTER_DIR resolution so .env can set ADAPTER_DIR/PAI_DIR if needed\n */\nfunction loadEnvFile(): void {\n // Check common locations for .env\n const possiblePaths = [\n resolve(process.env.ADAPTER_DIR || process.env.PAI_DIR || '', '.env'),\n resolve(homedir(), '.claude', '.env'),\n ];\n\n for (const envPath of possiblePaths) {\n if (existsSync(envPath)) {\n try {\n const content = readFileSync(envPath, 'utf-8');\n for (const line of content.split('\\n')) {\n const trimmed = line.trim();\n // Skip comments and empty lines\n if (!trimmed || trimmed.startsWith('#')) continue;\n\n const eqIndex = trimmed.indexOf('=');\n if (eqIndex > 0) {\n const key = trimmed.substring(0, eqIndex).trim();\n let value = trimmed.substring(eqIndex + 1).trim();\n\n // Remove surrounding quotes if present\n if ((value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n value = value.slice(1, -1);\n }\n\n // Expand $HOME and ~ in values\n value = value.replace(/\\$HOME/g, homedir());\n value = value.replace(/^~(?=\\/|$)/, homedir());\n\n // Only set if not already defined (env vars take precedence)\n if (process.env[key] === undefined) {\n process.env[key] = value;\n }\n }\n }\n // Found and loaded, don't check other paths\n break;\n } catch {\n // Silently continue if .env can't be read\n }\n }\n }\n}\n\n// Load .env FIRST, before any other initialization\nloadEnvFile();\n\nfunction defaultAdapterDir(): string {\n return resolve(homedir(), '.claude');\n}\n\n/**\n * Never print when stdout/stderr is a data channel, not a human: hook\n * bundles and worker-status-line.mjs set PAI_QUIET_NOTICES=1 via their\n * esbuild banner (scripts/build-hooks.mjs), and a `pai worker run\n * --output-format json` invocation is detected directly off argv since its\n * env can't be set before this module's static imports resolve.\n */\nfunction suppressDeprecationNotices(): boolean {\n if (process.env.PAI_QUIET_NOTICES === '1') return true;\n const idx = process.argv.indexOf('--output-format');\n return idx !== -1 && process.argv[idx + 1] === 'json';\n}\n\n/**\n * At most once per process \u2014 a `globalThis` flag rather than a module-level\n * `let` because hooks and the CLI can end up with more than one instance of\n * this module loaded into the same process (separate bundles), each with its\n * own module scope; only a property on the shared global survives that.\n */\nfunction warnPaiDirDeprecatedOnce(message: string): void {\n const g = globalThis as typeof globalThis & { __paiDirNoticePrinted?: boolean };\n if (g.__paiDirNoticePrinted || suppressDeprecationNotices()) return;\n g.__paiDirNoticePrinted = true;\n process.stderr.write(`pai: ${message}\\n`);\n}\n\n/**\n * Smart ADAPTER_DIR detection with fallback\n * Priority:\n * 1. ADAPTER_DIR environment variable (if set) \u2014 warns once if PAI_DIR is\n * ALSO set and resolves to a different path (naming both).\n * 2. PAI_DIR environment variable (deprecated alias, one release) \u2014 warns\n * once only when it differs from the default adapter root; PAI_DIR set to\n * the same value as the default is the operator's current, correct\n * setting and stays silent.\n * 3. ~/.claude (standard location)\n */\nfunction resolveAdapterDir(): string {\n if (process.env.ADAPTER_DIR) {\n const adapterDir = resolve(process.env.ADAPTER_DIR);\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== adapterDir) {\n warnPaiDirDeprecatedOnce(\n `ADAPTER_DIR (${adapterDir}) and PAI_DIR (${paiDir}) are both set and differ \u2014 ADAPTER_DIR wins. PAI_DIR still works for one release.`\n );\n }\n }\n return adapterDir;\n }\n if (process.env.PAI_DIR) {\n const paiDir = resolve(process.env.PAI_DIR);\n if (paiDir !== defaultAdapterDir()) {\n warnPaiDirDeprecatedOnce(\n 'PAI_DIR is deprecated \u2014 use ADAPTER_DIR for the harness adapter root (~/.claude). PAI_DIR still works for one release.'\n );\n }\n return paiDir;\n }\n return defaultAdapterDir();\n}\n\n/** The Claude Code harness adapter root \u2014 NOT PAI's state home. See module doc above. */\nexport const ADAPTER_DIR = resolveAdapterDir();\n\n/** @deprecated alias for ADAPTER_DIR, kept for one release. Prefer ADAPTER_DIR. */\nexport const PAI_DIR = ADAPTER_DIR;\n\n/**\n * Adapter directories \u2014 fixed paths the Claude Code harness itself loads\n * from. These stay under ADAPTER_DIR; they are not PAI state.\n */\nexport const HOOKS_DIR = join(ADAPTER_DIR, 'Hooks');\nexport const SKILLS_DIR = join(ADAPTER_DIR, 'skills');\nexport const AGENTS_DIR = join(ADAPTER_DIR, 'agents');\nexport const COMMANDS_DIR = join(ADAPTER_DIR, 'commands');\n\n// ---------------------------------------------------------------------------\n// PAI state written by hooks \u2014 resolves under PAI_HOME, falling back to the\n// pre-2026-09-19 ADAPTER_DIR location (one-time stderr notice) until\n// `pai config migrate --history` moves it. Actively written on every hook\n// event across every session, so unlike most PAI_HOME files the live move is\n// deliberately NOT automatic \u2014 see `pai config migrate --history`.\n// ---------------------------------------------------------------------------\n\nfunction oldHistoryDir(): string {\n return join(ADAPTER_DIR, 'History');\n}\n\n/** Read/write location for hook-captured history: PAI_HOME/History if\n * present, else the old ADAPTER_DIR/History (one-time stderr notice). */\nexport function historyDir(): string {\n return resolvePaiFile(paiHomePath('History'), [oldHistoryDir()], 'pai config migrate --history');\n}\n\nexport function migrateHistoryDir(opts: { dryRun?: boolean } = {}): MigrateDirResult {\n return migratePaiDir(paiHomePath('History'), [oldHistoryDir()], opts);\n}\n\nfunction oldAgentSessionsPath(): string {\n return join(ADAPTER_DIR, 'agent-sessions.json');\n}\n\nexport function agentSessionsPath(): string {\n return resolvePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], 'pai config migrate --history');\n}\n\nexport function migrateAgentSessions(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('agent-sessions.json'), [oldAgentSessionsPath()], opts);\n}\n\nfunction oldSecurityEventsPath(): string {\n return join(ADAPTER_DIR, 'history', 'security', 'security-events.jsonl');\n}\n\nexport function securityEventsPath(): string {\n return resolvePaiFile(\n paiHomePath('History', 'security', 'security-events.jsonl'),\n [oldSecurityEventsPath()],\n 'pai config migrate --history'\n );\n}\n\nexport function migrateSecurityEvents(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('History', 'security', 'security-events.jsonl'), [oldSecurityEventsPath()], opts);\n}\n\nfunction oldSessionRoutingPath(): string {\n return join(ADAPTER_DIR, 'session-routing.json');\n}\n\nexport function sessionRoutingPath(): string {\n return resolvePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], 'pai config migrate --history');\n}\n\nexport function migrateSessionRouting(opts: { dryRun?: boolean } = {}): MigrateFileResult {\n return migratePaiFile(paiHomePath('session-routing.json'), [oldSessionRoutingPath()], opts);\n}\n\n/**\n * Helper to get history file path with date-based organization\n */\nexport function getHistoryFilePath(subdir: string, filename: string): string {\n const now = new Date();\n const tz = process.env.TIME_ZONE || Intl.DateTimeFormat().resolvedOptions().timeZone;\n const localDate = new Date(now.toLocaleString('en-US', { timeZone: tz }));\n const year = localDate.getFullYear();\n const month = String(localDate.getMonth() + 1).padStart(2, '0');\n\n return join(historyDir(), subdir, `${year}-${month}`, filename);\n}\n", "/**\n * pai-home.ts \u2014 the PAI_HOME namespace directory and generic per-user file\n * migration into it.\n *\n * Every PAI-owned per-user file (workers.yaml, config.json, whisper-rules.md,\n * advisor-mode.json, ...) lives under PAI_HOME (~/.claude/pai by default) so\n * nothing PAI writes can ever collide with a file Claude Code itself\n * introduces under ~/.claude. Decided 2026-09-19 \u2014 see docs/workers-config.md.\n */\n\nimport { existsSync, mkdirSync, readFileSync, copyFileSync, renameSync, readdirSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, dirname } from \"node:path\";\nimport { execFileSync } from \"node:child_process\";\n\n/**\n * Byte-identical check that works for files of any size. `cmp` compares by\n * streaming rather than loading either file into memory, unlike a\n * readFileSync + Buffer.compare \u2014 which throws (\"File size is greater than\n * 2 GiB\") on anything past Node's 2GiB single-read ceiling, a real limit hit\n * migrating a multi-gigabyte legacy federation database file.\n */\nfunction filesByteIdentical(a: string, b: string): boolean {\n try {\n execFileSync(\"cmp\", [\"-s\", a, b], { stdio: \"pipe\" });\n return true;\n } catch (e) {\n if (e && typeof e === \"object\" && \"status\" in e && (e as { status: number }).status === 1) return false;\n // cmp missing or errored for an unrelated reason \u2014 fall back to an\n // in-memory compare (fine for the small files this path normally sees).\n return Buffer.compare(readFileSync(a), readFileSync(b)) === 0;\n }\n}\n\n/** PAI_HOME (test isolation, power users) overrides the default namespace dir. */\nexport function paiHomeDir(): string {\n return process.env.PAI_HOME || join(homedir(), \".claude\", \"pai\");\n}\n\n/** A path under PAI_HOME, e.g. `paiHomePath(\"config.json\")`. */\nexport function paiHomePath(...segments: string[]): string {\n return join(paiHomeDir(), ...segments);\n}\n\nconst noticesPrinted = new Set<string>();\n\n/**\n * Resolve a per-user PAI file: the new PAI_HOME path if it exists, else the\n * first existing entry in `oldCandidates` (checked in order \u2014 most recent\n * old location first), else the new path (the target a first write\n * creates). Prints one stderr notice per process per new-path when a\n * fallback location is actually used.\n */\nexport function resolvePaiFile(newPath: string, oldCandidates: string[], migrateHint: string): string {\n if (existsSync(newPath)) return newPath;\n for (const old of oldCandidates) {\n if (existsSync(old)) {\n if (!noticesPrinted.has(newPath)) {\n noticesPrinted.add(newPath);\n process.stderr.write(\n `pai: ${old} is at an old location \u2014 run \\`${migrateHint}\\` to move it to ${newPath}\\n`\n );\n }\n return old;\n }\n }\n return newPath;\n}\n\nexport class PaiFileMigrationError extends Error {}\n\nexport interface MigrateFileResult {\n fromPath: string | null;\n toPath: string;\n dryRun: boolean;\n note?: string;\n}\n\n/**\n * Move a per-user PAI file to its new PAI_HOME location: copy, verify\n * byte-identical, then rename the source to `<name>.migrated-<YYYYMMDD>`\n * (never deleted). Idempotent: if the new path already holds bytes\n * identical to the found source, the source is just renamed aside; if it\n * differs, this refuses rather than overwrite silently.\n */\nexport function migratePaiFile(\n newPath: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateFileResult {\n const fromPath = oldCandidates.find((p) => existsSync(p)) ?? null;\n if (!fromPath) {\n const note = existsSync(newPath) ? \"already at new location\" : \"nothing to migrate \u2014 file does not exist yet\";\n return { fromPath: null, toPath: newPath, dryRun: !!opts.dryRun, note };\n }\n if (opts.dryRun) return { fromPath, toPath: newPath, dryRun: true };\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n\n if (existsSync(newPath)) {\n if (filesByteIdentical(fromPath, newPath)) {\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false, note: \"identical \u2014 old file renamed aside\" };\n }\n throw new PaiFileMigrationError(\n `${newPath} already exists and differs from ${fromPath} \u2014 resolve manually, nothing changed`\n );\n }\n\n const dir = dirname(newPath);\n if (!existsSync(dir)) mkdirSync(dir, { recursive: true });\n copyFileSync(fromPath, newPath);\n\n if (!filesByteIdentical(fromPath, newPath)) {\n throw new PaiFileMigrationError(\n `${newPath}: copy did not match ${fromPath} byte-for-byte \u2014 aborting, old file left in place`\n );\n }\n\n renameSync(fromPath, `${fromPath}.migrated-${stamp}`);\n return { fromPath, toPath: newPath, dryRun: false };\n}\n\nexport interface MigrateDirResult {\n fromDir: string | null;\n toDir: string;\n dryRun: boolean;\n movedCount?: number;\n note?: string;\n}\n\n/**\n * Move a per-user PAI directory (queries/, session-state/, ...) into\n * PAI_HOME: move every entry from the first found old candidate into the\n * new dir (never overwriting an existing entry there), then rename the now-\n * empty old dir aside to `<name>.migrated-<YYYYMMDD>` (never deleted).\n * Idempotent \u2014 an already-migrated dir has nothing left to find.\n */\nexport function migratePaiDir(\n newDir: string,\n oldCandidates: string[],\n opts: { dryRun?: boolean } = {}\n): MigrateDirResult {\n const fromDir = oldCandidates.find((p) => existsSync(p) && statSync(p).isDirectory()) ?? null;\n if (!fromDir) {\n const note = existsSync(newDir) ? \"already at new location\" : \"nothing to migrate \u2014 directory does not exist yet\";\n return { fromDir: null, toDir: newDir, dryRun: !!opts.dryRun, note };\n }\n\n const entries = readdirSync(fromDir);\n if (opts.dryRun) return { fromDir, toDir: newDir, dryRun: true, movedCount: entries.length };\n\n if (!existsSync(newDir)) mkdirSync(newDir, { recursive: true });\n\n let moved = 0;\n const collided: string[] = [];\n for (const entry of entries) {\n const src = join(fromDir, entry);\n const dst = join(newDir, entry);\n if (existsSync(dst)) {\n collided.push(entry);\n continue;\n }\n renameSync(src, dst);\n moved++;\n }\n\n const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, \"\");\n const remaining = readdirSync(fromDir);\n const note = collided.length\n ? `${collided.length} entrie(s) left in ${fromDir} \u2014 name already existed in ${newDir}`\n : undefined;\n\n if (remaining.length === 0) {\n renameSync(fromDir, `${fromDir}.migrated-${stamp}`);\n }\n\n return { fromDir, toDir: newDir, dryRun: false, movedCount: moved, note };\n}\n"],
5
+ "mappings": ";;;;;;AASA,SAAS,gBAAgB,aAAAA,YAAW,cAAAC,mBAAkB;AACtD,SAAS,QAAAC,aAAY;;;ACerB,SAAS,WAAAC,gBAAe;AACxB,SAAS,SAAS,QAAAC,aAAY;AAC9B,SAAS,cAAAC,aAAY,gBAAAC,qBAAoB;;;ACjBzC,SAAS,YAAY,WAAW,cAAc,cAAc,YAAY,aAAa,gBAAgB;AACrG,SAAS,eAAe;AACxB,SAAS,MAAM,eAAe;AAuBvB,SAAS,aAAqB;AACnC,SAAO,QAAQ,IAAI,YAAY,KAAK,QAAQ,GAAG,WAAW,KAAK;AACjE;AAGO,SAAS,eAAe,UAA4B;AACzD,SAAO,KAAK,WAAW,GAAG,GAAG,QAAQ;AACvC;AAEA,IAAM,iBAAiB,oBAAI,IAAY;AAShC,SAAS,eAAe,SAAiB,eAAyB,aAA6B;AACpG,MAAI,WAAW,OAAO,EAAG,QAAO;AAChC,aAAW,OAAO,eAAe;AAC/B,QAAI,WAAW,GAAG,GAAG;AACnB,UAAI,CAAC,eAAe,IAAI,OAAO,GAAG;AAChC,uBAAe,IAAI,OAAO;AAC1B,gBAAQ,OAAO;AAAA,UACb,QAAQ,GAAG,uCAAkC,WAAW,oBAAoB,OAAO;AAAA;AAAA,QACrF;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;;;AD1BA,SAAS,cAAoB;AAE3B,QAAM,gBAAgB;AAAA,IACpB,QAAQ,QAAQ,IAAI,eAAe,QAAQ,IAAI,WAAW,IAAI,MAAM;AAAA,IACpE,QAAQC,SAAQ,GAAG,WAAW,MAAM;AAAA,EACtC;AAEA,aAAW,WAAW,eAAe;AACnC,QAAIC,YAAW,OAAO,GAAG;AACvB,UAAI;AACF,cAAM,UAAUC,cAAa,SAAS,OAAO;AAC7C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AAQ,KAAK,CAAC;AAAA,IAChB;AAGA,UAAM,MAAM,oBAAI,KAAK;AACrB,UAAM,QAAQ,IAAI,YAAY,EAAE,MAAM,GAAG,EAAE,CAAC;AAC5C,UAAM,YAAY,MAAM,UAAU,GAAG,CAAC;AAGtC,UAAM,UAAUA,MAAK,aAAa,SAAS;AAC3C,QAAI,CAACC,YAAW,OAAO,GAAG;AACxB,MAAAC,WAAU,SAAS,EAAE,WAAW,KAAK,CAAC;AAAA,IACxC;AAGA,UAAM,cAAcF,MAAK,SAAS,GAAG,KAAK,qBAAqB;AAC/D,UAAM,eAAe,KAAK,UAAU;AAAA,MAClC,WAAW,KAAK,aAAa,IAAI,YAAY;AAAA,MAC7C,MAAM,KAAK;AAAA,MACX,OAAO,KAAK;AAAA,MACZ,QAAQ,KAAK;AAAA,MACb,SAAS,KAAK;AAAA,IAChB,CAAC,IAAI;AAGL,mBAAe,aAAa,YAAY;AAGxC,YAAQ,KAAK,CAAC;AAAA,EAChB,SAAS,OAAO;AAEd,YAAQ,MAAM,kCAAkC,KAAK,EAAE;AACvD,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;AAEA,KAAK;",
6
+ "names": ["mkdirSync", "existsSync", "join", "homedir", "join", "existsSync", "readFileSync", "homedir", "existsSync", "readFileSync", "join", "join", "existsSync", "mkdirSync"]
7
7
  }
@@ -88,23 +88,9 @@ function resolveAdapterDir() {
88
88
  }
89
89
  var ADAPTER_DIR = resolveAdapterDir();
90
90
  var HOOKS_DIR = join(ADAPTER_DIR, "Hooks");
91
- var SKILLS_DIR = join(ADAPTER_DIR, "Skills");
92
- var AGENTS_DIR = join(ADAPTER_DIR, "Agents");
93
- var COMMANDS_DIR = join(ADAPTER_DIR, "Commands");
94
- function validatePAIStructure() {
95
- if (!existsSync(ADAPTER_DIR)) {
96
- console.error(`ADAPTER_DIR does not exist: ${ADAPTER_DIR}`);
97
- console.error(` Expected ~/.claude or set ADAPTER_DIR environment variable`);
98
- process.exit(1);
99
- }
100
- if (!existsSync(HOOKS_DIR)) {
101
- console.error(`PAI hooks directory not found: ${HOOKS_DIR}`);
102
- console.error(` Your ADAPTER_DIR may be misconfigured`);
103
- console.error(` Current ADAPTER_DIR: ${ADAPTER_DIR}`);
104
- process.exit(1);
105
- }
106
- }
107
- validatePAIStructure();
91
+ var SKILLS_DIR = join(ADAPTER_DIR, "skills");
92
+ var AGENTS_DIR = join(ADAPTER_DIR, "agents");
93
+ var COMMANDS_DIR = join(ADAPTER_DIR, "commands");
108
94
 
109
95
  // src/hooks/ts/lib/project-utils/paths.ts
110
96
  var PROJECTS_DIR = join2(ADAPTER_DIR, "projects");