@goondocks/myco 0.2.7 → 0.2.8

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 (290) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/CONTRIBUTING.md +1 -1
  3. package/commands/init.md +10 -26
  4. package/dist/chunk-4JML636J.js +52 -0
  5. package/dist/chunk-4JML636J.js.map +1 -0
  6. package/dist/chunk-AOMX45LH.js +8974 -0
  7. package/dist/chunk-AOMX45LH.js.map +1 -0
  8. package/dist/chunk-I7PMGO6S.js +58 -0
  9. package/dist/chunk-I7PMGO6S.js.map +1 -0
  10. package/dist/chunk-N33KUCFP.js +33 -0
  11. package/dist/chunk-N33KUCFP.js.map +1 -0
  12. package/dist/chunk-NYNEJ5QY.js +71 -0
  13. package/dist/chunk-NYNEJ5QY.js.map +1 -0
  14. package/dist/chunk-PA3VMINE.js +111 -0
  15. package/dist/chunk-PA3VMINE.js.map +1 -0
  16. package/dist/chunk-PZUWP5VK.js +44 -0
  17. package/dist/chunk-PZUWP5VK.js.map +1 -0
  18. package/dist/chunk-SVUINMDD.js +104 -0
  19. package/dist/chunk-SVUINMDD.js.map +1 -0
  20. package/dist/chunk-TH6GIBXG.js +91 -0
  21. package/dist/chunk-TH6GIBXG.js.map +1 -0
  22. package/dist/chunk-TWDS6MSU.js +354 -0
  23. package/dist/chunk-TWDS6MSU.js.map +1 -0
  24. package/dist/chunk-UIIZRTJU.js +21172 -0
  25. package/dist/chunk-UIIZRTJU.js.map +1 -0
  26. package/dist/chunk-YMYJ7FNH.js +19 -0
  27. package/dist/chunk-YMYJ7FNH.js.map +1 -0
  28. package/dist/chunk-ZJQ5G637.js +21 -0
  29. package/dist/chunk-ZJQ5G637.js.map +1 -0
  30. package/dist/chunk-ZTZVX5E6.js +421 -0
  31. package/dist/chunk-ZTZVX5E6.js.map +1 -0
  32. package/dist/cli-K5FSKLQC.js +625 -0
  33. package/dist/cli-K5FSKLQC.js.map +1 -0
  34. package/dist/client-4JMOYNKK.js +11 -0
  35. package/dist/client-4JMOYNKK.js.map +1 -0
  36. package/dist/main-5W4ADOBG.js +3224 -0
  37. package/dist/main-5W4ADOBG.js.map +1 -0
  38. package/dist/server-PIEPVUUH.js +14725 -0
  39. package/dist/server-PIEPVUUH.js.map +1 -0
  40. package/dist/session-start-2NNQHT5S.js +189 -0
  41. package/dist/session-start-2NNQHT5S.js.map +1 -0
  42. package/dist/src/cli.js +9 -582
  43. package/dist/src/cli.js.map +1 -1
  44. package/dist/src/daemon/main.js +9 -737
  45. package/dist/src/daemon/main.js.map +1 -1
  46. package/dist/src/hooks/post-tool-use.js +47 -35
  47. package/dist/src/hooks/post-tool-use.js.map +1 -1
  48. package/dist/src/hooks/session-end.js +29 -18
  49. package/dist/src/hooks/session-end.js.map +1 -1
  50. package/dist/src/hooks/session-start.js +9 -48
  51. package/dist/src/hooks/session-start.js.map +1 -1
  52. package/dist/src/hooks/stop.js +39 -30
  53. package/dist/src/hooks/stop.js.map +1 -1
  54. package/dist/src/hooks/user-prompt-submit.js +48 -40
  55. package/dist/src/hooks/user-prompt-submit.js.map +1 -1
  56. package/dist/src/mcp/server.js +9 -304
  57. package/dist/src/mcp/server.js.map +1 -1
  58. package/package.json +3 -2
  59. package/dist/src/agents/adapter.d.ts +0 -76
  60. package/dist/src/agents/adapter.d.ts.map +0 -1
  61. package/dist/src/agents/adapter.js +0 -124
  62. package/dist/src/agents/adapter.js.map +0 -1
  63. package/dist/src/agents/claude-code.d.ts +0 -3
  64. package/dist/src/agents/claude-code.d.ts.map +0 -1
  65. package/dist/src/agents/claude-code.js +0 -22
  66. package/dist/src/agents/claude-code.js.map +0 -1
  67. package/dist/src/agents/cursor.d.ts +0 -3
  68. package/dist/src/agents/cursor.d.ts.map +0 -1
  69. package/dist/src/agents/cursor.js +0 -154
  70. package/dist/src/agents/cursor.js.map +0 -1
  71. package/dist/src/agents/index.d.ts +0 -6
  72. package/dist/src/agents/index.d.ts.map +0 -1
  73. package/dist/src/agents/index.js +0 -5
  74. package/dist/src/agents/index.js.map +0 -1
  75. package/dist/src/agents/registry.d.ts +0 -34
  76. package/dist/src/agents/registry.d.ts.map +0 -1
  77. package/dist/src/agents/registry.js +0 -95
  78. package/dist/src/agents/registry.js.map +0 -1
  79. package/dist/src/artifacts/candidates.d.ts +0 -20
  80. package/dist/src/artifacts/candidates.d.ts.map +0 -1
  81. package/dist/src/artifacts/candidates.js +0 -84
  82. package/dist/src/artifacts/candidates.js.map +0 -1
  83. package/dist/src/artifacts/slugify.d.ts +0 -2
  84. package/dist/src/artifacts/slugify.d.ts.map +0 -1
  85. package/dist/src/artifacts/slugify.js +0 -22
  86. package/dist/src/artifacts/slugify.js.map +0 -1
  87. package/dist/src/capture/buffer.d.ts +0 -20
  88. package/dist/src/capture/buffer.d.ts.map +0 -1
  89. package/dist/src/capture/buffer.js +0 -55
  90. package/dist/src/capture/buffer.js.map +0 -1
  91. package/dist/src/capture/transcript-miner.d.ts +0 -31
  92. package/dist/src/capture/transcript-miner.d.ts.map +0 -1
  93. package/dist/src/capture/transcript-miner.js +0 -61
  94. package/dist/src/capture/transcript-miner.js.map +0 -1
  95. package/dist/src/cli.d.ts +0 -3
  96. package/dist/src/cli.d.ts.map +0 -1
  97. package/dist/src/config/loader.d.ts +0 -4
  98. package/dist/src/config/loader.d.ts.map +0 -1
  99. package/dist/src/config/loader.js +0 -32
  100. package/dist/src/config/loader.js.map +0 -1
  101. package/dist/src/config/schema.d.ts +0 -83
  102. package/dist/src/config/schema.d.ts.map +0 -1
  103. package/dist/src/config/schema.js +0 -55
  104. package/dist/src/config/schema.js.map +0 -1
  105. package/dist/src/constants.d.ts +0 -73
  106. package/dist/src/constants.d.ts.map +0 -1
  107. package/dist/src/constants.js +0 -86
  108. package/dist/src/constants.js.map +0 -1
  109. package/dist/src/context/injector.d.ts +0 -18
  110. package/dist/src/context/injector.d.ts.map +0 -1
  111. package/dist/src/context/injector.js +0 -71
  112. package/dist/src/context/injector.js.map +0 -1
  113. package/dist/src/context/relevance.d.ts +0 -13
  114. package/dist/src/context/relevance.d.ts.map +0 -1
  115. package/dist/src/context/relevance.js +0 -44
  116. package/dist/src/context/relevance.js.map +0 -1
  117. package/dist/src/daemon/batch.d.ts +0 -22
  118. package/dist/src/daemon/batch.d.ts.map +0 -1
  119. package/dist/src/daemon/batch.js +0 -38
  120. package/dist/src/daemon/batch.js.map +0 -1
  121. package/dist/src/daemon/lifecycle.d.ts +0 -27
  122. package/dist/src/daemon/lifecycle.d.ts.map +0 -1
  123. package/dist/src/daemon/lifecycle.js +0 -50
  124. package/dist/src/daemon/lifecycle.js.map +0 -1
  125. package/dist/src/daemon/lineage.d.ts +0 -42
  126. package/dist/src/daemon/lineage.d.ts.map +0 -1
  127. package/dist/src/daemon/lineage.js +0 -116
  128. package/dist/src/daemon/lineage.js.map +0 -1
  129. package/dist/src/daemon/logger.d.ts +0 -33
  130. package/dist/src/daemon/logger.d.ts.map +0 -1
  131. package/dist/src/daemon/logger.js +0 -88
  132. package/dist/src/daemon/logger.js.map +0 -1
  133. package/dist/src/daemon/main.d.ts +0 -2
  134. package/dist/src/daemon/main.d.ts.map +0 -1
  135. package/dist/src/daemon/processor.d.ts +0 -44
  136. package/dist/src/daemon/processor.d.ts.map +0 -1
  137. package/dist/src/daemon/processor.js +0 -142
  138. package/dist/src/daemon/processor.js.map +0 -1
  139. package/dist/src/daemon/server.d.ts +0 -24
  140. package/dist/src/daemon/server.d.ts.map +0 -1
  141. package/dist/src/daemon/server.js +0 -117
  142. package/dist/src/daemon/server.js.map +0 -1
  143. package/dist/src/daemon/watcher.d.ts +0 -29
  144. package/dist/src/daemon/watcher.d.ts.map +0 -1
  145. package/dist/src/daemon/watcher.js +0 -67
  146. package/dist/src/daemon/watcher.js.map +0 -1
  147. package/dist/src/hooks/client.d.ts +0 -20
  148. package/dist/src/hooks/client.d.ts.map +0 -1
  149. package/dist/src/hooks/client.js +0 -111
  150. package/dist/src/hooks/client.js.map +0 -1
  151. package/dist/src/hooks/post-tool-use.d.ts +0 -2
  152. package/dist/src/hooks/post-tool-use.d.ts.map +0 -1
  153. package/dist/src/hooks/read-stdin.d.ts +0 -2
  154. package/dist/src/hooks/read-stdin.d.ts.map +0 -1
  155. package/dist/src/hooks/read-stdin.js +0 -10
  156. package/dist/src/hooks/read-stdin.js.map +0 -1
  157. package/dist/src/hooks/session-end.d.ts +0 -2
  158. package/dist/src/hooks/session-end.d.ts.map +0 -1
  159. package/dist/src/hooks/session-start.d.ts +0 -2
  160. package/dist/src/hooks/session-start.d.ts.map +0 -1
  161. package/dist/src/hooks/stop.d.ts +0 -2
  162. package/dist/src/hooks/stop.d.ts.map +0 -1
  163. package/dist/src/hooks/user-prompt-submit.d.ts +0 -2
  164. package/dist/src/hooks/user-prompt-submit.d.ts.map +0 -1
  165. package/dist/src/index/fts.d.ts +0 -16
  166. package/dist/src/index/fts.d.ts.map +0 -1
  167. package/dist/src/index/fts.js +0 -53
  168. package/dist/src/index/fts.js.map +0 -1
  169. package/dist/src/index/rebuild.d.ts +0 -4
  170. package/dist/src/index/rebuild.d.ts.map +0 -1
  171. package/dist/src/index/rebuild.js +0 -40
  172. package/dist/src/index/rebuild.js.map +0 -1
  173. package/dist/src/index/sqlite.d.ts +0 -33
  174. package/dist/src/index/sqlite.d.ts.map +0 -1
  175. package/dist/src/index/sqlite.js +0 -99
  176. package/dist/src/index/sqlite.js.map +0 -1
  177. package/dist/src/index/vectors.d.ts +0 -24
  178. package/dist/src/index/vectors.d.ts.map +0 -1
  179. package/dist/src/index/vectors.js +0 -97
  180. package/dist/src/index/vectors.js.map +0 -1
  181. package/dist/src/intelligence/anthropic.d.ts +0 -17
  182. package/dist/src/intelligence/anthropic.d.ts.map +0 -1
  183. package/dist/src/intelligence/anthropic.js +0 -36
  184. package/dist/src/intelligence/anthropic.js.map +0 -1
  185. package/dist/src/intelligence/embeddings.d.ts +0 -3
  186. package/dist/src/intelligence/embeddings.d.ts.map +0 -1
  187. package/dist/src/intelligence/embeddings.js +0 -15
  188. package/dist/src/intelligence/embeddings.js.map +0 -1
  189. package/dist/src/intelligence/llm.d.ts +0 -33
  190. package/dist/src/intelligence/llm.d.ts.map +0 -1
  191. package/dist/src/intelligence/llm.js +0 -26
  192. package/dist/src/intelligence/llm.js.map +0 -1
  193. package/dist/src/intelligence/lm-studio.d.ts +0 -20
  194. package/dist/src/intelligence/lm-studio.d.ts.map +0 -1
  195. package/dist/src/intelligence/lm-studio.js +0 -59
  196. package/dist/src/intelligence/lm-studio.js.map +0 -1
  197. package/dist/src/intelligence/ollama.d.ts +0 -22
  198. package/dist/src/intelligence/ollama.d.ts.map +0 -1
  199. package/dist/src/intelligence/ollama.js +0 -64
  200. package/dist/src/intelligence/ollama.js.map +0 -1
  201. package/dist/src/intelligence/response.d.ts +0 -29
  202. package/dist/src/intelligence/response.d.ts.map +0 -1
  203. package/dist/src/intelligence/response.js +0 -71
  204. package/dist/src/intelligence/response.js.map +0 -1
  205. package/dist/src/logs/format.d.ts +0 -6
  206. package/dist/src/logs/format.d.ts.map +0 -1
  207. package/dist/src/logs/format.js +0 -46
  208. package/dist/src/logs/format.js.map +0 -1
  209. package/dist/src/logs/reader.d.ts +0 -28
  210. package/dist/src/logs/reader.d.ts.map +0 -1
  211. package/dist/src/logs/reader.js +0 -106
  212. package/dist/src/logs/reader.js.map +0 -1
  213. package/dist/src/mcp/server.d.ts +0 -16
  214. package/dist/src/mcp/server.d.ts.map +0 -1
  215. package/dist/src/mcp/tools/consolidate.d.ts +0 -15
  216. package/dist/src/mcp/tools/consolidate.d.ts.map +0 -1
  217. package/dist/src/mcp/tools/consolidate.js +0 -49
  218. package/dist/src/mcp/tools/consolidate.js.map +0 -1
  219. package/dist/src/mcp/tools/graph.d.ts +0 -30
  220. package/dist/src/mcp/tools/graph.d.ts.map +0 -1
  221. package/dist/src/mcp/tools/graph.js +0 -106
  222. package/dist/src/mcp/tools/graph.js.map +0 -1
  223. package/dist/src/mcp/tools/logs.d.ts +0 -3
  224. package/dist/src/mcp/tools/logs.d.ts.map +0 -1
  225. package/dist/src/mcp/tools/logs.js +0 -7
  226. package/dist/src/mcp/tools/logs.js.map +0 -1
  227. package/dist/src/mcp/tools/plans.d.ts +0 -23
  228. package/dist/src/mcp/tools/plans.d.ts.map +0 -1
  229. package/dist/src/mcp/tools/plans.js +0 -63
  230. package/dist/src/mcp/tools/plans.js.map +0 -1
  231. package/dist/src/mcp/tools/recall.d.ts +0 -30
  232. package/dist/src/mcp/tools/recall.d.ts.map +0 -1
  233. package/dist/src/mcp/tools/recall.js +0 -34
  234. package/dist/src/mcp/tools/recall.js.map +0 -1
  235. package/dist/src/mcp/tools/remember.d.ts +0 -15
  236. package/dist/src/mcp/tools/remember.d.ts.map +0 -1
  237. package/dist/src/mcp/tools/remember.js +0 -18
  238. package/dist/src/mcp/tools/remember.js.map +0 -1
  239. package/dist/src/mcp/tools/search.d.ts +0 -19
  240. package/dist/src/mcp/tools/search.d.ts.map +0 -1
  241. package/dist/src/mcp/tools/search.js +0 -59
  242. package/dist/src/mcp/tools/search.js.map +0 -1
  243. package/dist/src/mcp/tools/sessions.d.ts +0 -21
  244. package/dist/src/mcp/tools/sessions.d.ts.map +0 -1
  245. package/dist/src/mcp/tools/sessions.js +0 -36
  246. package/dist/src/mcp/tools/sessions.js.map +0 -1
  247. package/dist/src/mcp/tools/supersede.d.ts +0 -14
  248. package/dist/src/mcp/tools/supersede.d.ts.map +0 -1
  249. package/dist/src/mcp/tools/supersede.js +0 -30
  250. package/dist/src/mcp/tools/supersede.js.map +0 -1
  251. package/dist/src/mcp/tools/team.d.ts +0 -16
  252. package/dist/src/mcp/tools/team.d.ts.map +0 -1
  253. package/dist/src/mcp/tools/team.js +0 -32
  254. package/dist/src/mcp/tools/team.js.map +0 -1
  255. package/dist/src/obsidian/formatter.d.ts +0 -80
  256. package/dist/src/obsidian/formatter.d.ts.map +0 -1
  257. package/dist/src/obsidian/formatter.js +0 -227
  258. package/dist/src/obsidian/formatter.js.map +0 -1
  259. package/dist/src/prompts/index.d.ts +0 -13
  260. package/dist/src/prompts/index.d.ts.map +0 -1
  261. package/dist/src/prompts/index.js +0 -75
  262. package/dist/src/prompts/index.js.map +0 -1
  263. package/dist/src/vault/frontmatter.d.ts +0 -6
  264. package/dist/src/vault/frontmatter.d.ts.map +0 -1
  265. package/dist/src/vault/frontmatter.js +0 -10
  266. package/dist/src/vault/frontmatter.js.map +0 -1
  267. package/dist/src/vault/observations.d.ts +0 -10
  268. package/dist/src/vault/observations.d.ts.map +0 -1
  269. package/dist/src/vault/observations.js +0 -33
  270. package/dist/src/vault/observations.js.map +0 -1
  271. package/dist/src/vault/reader.d.ts +0 -10
  272. package/dist/src/vault/reader.d.ts.map +0 -1
  273. package/dist/src/vault/reader.js +0 -48
  274. package/dist/src/vault/reader.js.map +0 -1
  275. package/dist/src/vault/resolve.d.ts +0 -18
  276. package/dist/src/vault/resolve.d.ts.map +0 -1
  277. package/dist/src/vault/resolve.js +0 -51
  278. package/dist/src/vault/resolve.js.map +0 -1
  279. package/dist/src/vault/session-id.d.ts +0 -16
  280. package/dist/src/vault/session-id.d.ts.map +0 -1
  281. package/dist/src/vault/session-id.js +0 -29
  282. package/dist/src/vault/session-id.js.map +0 -1
  283. package/dist/src/vault/types.d.ts +0 -88
  284. package/dist/src/vault/types.d.ts.map +0 -1
  285. package/dist/src/vault/types.js +0 -94
  286. package/dist/src/vault/types.js.map +0 -1
  287. package/dist/src/vault/writer.d.ts +0 -66
  288. package/dist/src/vault/writer.d.ts.map +0 -1
  289. package/dist/src/vault/writer.js +0 -217
  290. package/dist/src/vault/writer.js.map +0 -1
@@ -0,0 +1,3224 @@
1
+ import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
2
+ import {
3
+ VaultWriter,
4
+ bareSessionId,
5
+ formatMemoryBody,
6
+ formatSessionBody,
7
+ sessionNoteId,
8
+ sessionRelativePath,
9
+ sessionWikilink
10
+ } from "./chunk-ZTZVX5E6.js";
11
+ import {
12
+ ARTIFACT_TYPES,
13
+ DaemonLogger,
14
+ VectorIndex,
15
+ createEmbeddingProvider,
16
+ createLlmProvider,
17
+ generateEmbedding,
18
+ indexNote,
19
+ initFts,
20
+ rebuildIndex
21
+ } from "./chunk-AOMX45LH.js";
22
+ import {
23
+ MycoIndex
24
+ } from "./chunk-PA3VMINE.js";
25
+ import {
26
+ external_exports,
27
+ loadConfig,
28
+ require_dist
29
+ } from "./chunk-UIIZRTJU.js";
30
+ import {
31
+ EventBuffer
32
+ } from "./chunk-I7PMGO6S.js";
33
+ import {
34
+ AgentRegistry,
35
+ claudeCodeAdapter,
36
+ createPerProjectAdapter,
37
+ extensionForMimeType
38
+ } from "./chunk-TWDS6MSU.js";
39
+ import {
40
+ AI_RESPONSE_PREVIEW_CHARS,
41
+ CANDIDATE_CONTENT_PREVIEW,
42
+ CHARS_PER_TOKEN,
43
+ CLASSIFICATION_MAX_TOKENS,
44
+ COMMAND_PREVIEW_CHARS,
45
+ CONTENT_SNIPPET_CHARS,
46
+ CONTEXT_SESSION_PREVIEW_CHARS,
47
+ EMBEDDING_INPUT_LIMIT,
48
+ EXTRACTION_MAX_TOKENS,
49
+ FILE_WATCH_STABILITY_MS,
50
+ LINEAGE_RECENT_SESSIONS_LIMIT,
51
+ MAX_SLUG_LENGTH,
52
+ PROMPT_CONTEXT_MAX_MEMORIES,
53
+ PROMPT_CONTEXT_MIN_LENGTH,
54
+ PROMPT_CONTEXT_MIN_SIMILARITY,
55
+ PROMPT_PREVIEW_CHARS,
56
+ RELATED_MEMORIES_LIMIT,
57
+ SESSION_CONTEXT_MAX_PLANS,
58
+ STALE_BUFFER_MAX_AGE_MS,
59
+ SUMMARY_MAX_TOKENS,
60
+ TITLE_MAX_TOKENS
61
+ } from "./chunk-NYNEJ5QY.js";
62
+ import {
63
+ __toESM
64
+ } from "./chunk-PZUWP5VK.js";
65
+
66
+ // src/daemon/server.ts
67
+ import http from "http";
68
+ import fs from "fs";
69
+ import path from "path";
70
+ var DaemonServer = class {
71
+ port = 0;
72
+ server = null;
73
+ vaultDir;
74
+ logger;
75
+ routes = /* @__PURE__ */ new Map();
76
+ constructor(config) {
77
+ this.vaultDir = config.vaultDir;
78
+ this.logger = config.logger;
79
+ this.registerDefaultRoutes();
80
+ }
81
+ registerRoute(method, routePath, handler) {
82
+ this.routes.set(`${method} ${routePath}`, { method, handler });
83
+ }
84
+ async start() {
85
+ return new Promise((resolve3, reject) => {
86
+ this.server = http.createServer((req, res) => this.handleRequest(req, res));
87
+ this.server.on("error", reject);
88
+ this.server.listen(0, "127.0.0.1", () => {
89
+ const addr = this.server.address();
90
+ this.port = addr.port;
91
+ this.writeDaemonJson();
92
+ this.logger.info("daemon", "Server started", { port: this.port });
93
+ resolve3();
94
+ });
95
+ });
96
+ }
97
+ async stop() {
98
+ return new Promise((resolve3) => {
99
+ this.removeDaemonJson();
100
+ if (this.server) {
101
+ this.server.close(() => {
102
+ this.logger.info("daemon", "Server stopped");
103
+ resolve3();
104
+ });
105
+ } else {
106
+ resolve3();
107
+ }
108
+ });
109
+ }
110
+ registerDefaultRoutes() {
111
+ this.registerRoute("GET", "/health", async () => ({
112
+ myco: true,
113
+ pid: process.pid,
114
+ uptime: process.uptime()
115
+ }));
116
+ }
117
+ async handleRequest(req, res) {
118
+ const key = `${req.method} ${req.url}`;
119
+ const route = this.routes.get(key);
120
+ if (!route) {
121
+ res.writeHead(404, { "Content-Type": "application/json" });
122
+ res.end(JSON.stringify({ error: "not found" }));
123
+ return;
124
+ }
125
+ try {
126
+ const body = req.method === "POST" ? await readBody(req) : void 0;
127
+ const result = await route.handler(body);
128
+ res.writeHead(200, { "Content-Type": "application/json" });
129
+ res.end(JSON.stringify(result));
130
+ } catch (error) {
131
+ this.logger.error("daemon", "Request handler error", {
132
+ path: req.url,
133
+ error: error.message
134
+ });
135
+ res.writeHead(500, { "Content-Type": "application/json" });
136
+ res.end(JSON.stringify({ error: error.message }));
137
+ }
138
+ }
139
+ updateDaemonJsonSessions(sessions) {
140
+ const jsonPath = path.join(this.vaultDir, "daemon.json");
141
+ try {
142
+ const info = JSON.parse(fs.readFileSync(jsonPath, "utf-8"));
143
+ info.sessions = sessions;
144
+ fs.writeFileSync(jsonPath, JSON.stringify(info, null, 2));
145
+ } catch {
146
+ }
147
+ }
148
+ writeDaemonJson() {
149
+ const info = {
150
+ pid: process.pid,
151
+ port: this.port,
152
+ started: (/* @__PURE__ */ new Date()).toISOString(),
153
+ sessions: []
154
+ };
155
+ const jsonPath = path.join(this.vaultDir, "daemon.json");
156
+ fs.writeFileSync(jsonPath, JSON.stringify(info, null, 2));
157
+ }
158
+ removeDaemonJson() {
159
+ const jsonPath = path.join(this.vaultDir, "daemon.json");
160
+ try {
161
+ fs.unlinkSync(jsonPath);
162
+ } catch {
163
+ }
164
+ }
165
+ };
166
+ function readBody(req) {
167
+ return new Promise((resolve3, reject) => {
168
+ let data = "";
169
+ req.on("data", (chunk) => {
170
+ data += chunk;
171
+ });
172
+ req.on("end", () => {
173
+ try {
174
+ resolve3(data ? JSON.parse(data) : {});
175
+ } catch (e) {
176
+ reject(e);
177
+ }
178
+ });
179
+ req.on("error", reject);
180
+ });
181
+ }
182
+
183
+ // src/daemon/lifecycle.ts
184
+ var SessionRegistry = class {
185
+ _sessions = /* @__PURE__ */ new Map();
186
+ graceTimer = null;
187
+ gracePeriod;
188
+ onEmpty;
189
+ constructor(options) {
190
+ this.gracePeriod = options.gracePeriod;
191
+ this.onEmpty = options.onEmpty;
192
+ }
193
+ get sessions() {
194
+ return [...this._sessions.keys()];
195
+ }
196
+ register(sessionId, metadata) {
197
+ if (!this._sessions.has(sessionId)) {
198
+ this._sessions.set(sessionId, metadata ?? { started_at: (/* @__PURE__ */ new Date()).toISOString() });
199
+ }
200
+ this.cancelGrace();
201
+ }
202
+ getSession(sessionId) {
203
+ const meta = this._sessions.get(sessionId);
204
+ if (!meta) return void 0;
205
+ return { id: sessionId, ...meta };
206
+ }
207
+ unregister(sessionId) {
208
+ this._sessions.delete(sessionId);
209
+ if (this._sessions.size === 0) {
210
+ this.startGrace();
211
+ }
212
+ }
213
+ destroy() {
214
+ this.cancelGrace();
215
+ this._sessions.clear();
216
+ }
217
+ startGrace() {
218
+ this.cancelGrace();
219
+ this.graceTimer = setTimeout(() => {
220
+ if (this._sessions.size === 0) {
221
+ this.onEmpty();
222
+ }
223
+ }, this.gracePeriod * 1e3);
224
+ }
225
+ cancelGrace() {
226
+ if (this.graceTimer) {
227
+ clearTimeout(this.graceTimer);
228
+ this.graceTimer = null;
229
+ }
230
+ }
231
+ };
232
+
233
+ // src/daemon/batch.ts
234
+ var BatchManager = class {
235
+ batches = /* @__PURE__ */ new Map();
236
+ onBatchClosed;
237
+ constructor(onBatchClosed) {
238
+ this.onBatchClosed = onBatchClosed;
239
+ }
240
+ addEvent(event) {
241
+ const sid = event.session_id;
242
+ if (event.type === "user_prompt") {
243
+ const current = this.batches.get(sid);
244
+ if (current && current.length > 0) {
245
+ Promise.resolve(this.onBatchClosed(current)).catch((err) => {
246
+ console.error(`[mycod] batch callback error: ${err.message}`);
247
+ });
248
+ }
249
+ this.batches.set(sid, [event]);
250
+ } else {
251
+ const current = this.batches.get(sid);
252
+ if (current) {
253
+ current.push(event);
254
+ }
255
+ }
256
+ }
257
+ finalize(sessionId) {
258
+ const current = this.batches.get(sessionId);
259
+ this.batches.delete(sessionId);
260
+ return current ?? [];
261
+ }
262
+ hasOpenBatch(sessionId) {
263
+ return this.batches.has(sessionId) && this.batches.get(sessionId).length > 0;
264
+ }
265
+ batchSize(sessionId) {
266
+ return this.batches.get(sessionId)?.length ?? 0;
267
+ }
268
+ };
269
+
270
+ // src/prompts/index.ts
271
+ import fs2 from "fs";
272
+ import path2 from "path";
273
+ import { fileURLToPath } from "url";
274
+ var __dirname = path2.dirname(fileURLToPath(import.meta.url));
275
+ var promptCache = /* @__PURE__ */ new Map();
276
+ function loadPrompt(name) {
277
+ let cached = promptCache.get(name);
278
+ if (!cached) {
279
+ cached = fs2.readFileSync(path2.join(__dirname, `${name}.md`), "utf-8").trim();
280
+ promptCache.set(name, cached);
281
+ }
282
+ return cached;
283
+ }
284
+ function interpolate(template, vars) {
285
+ let result = template;
286
+ for (const [key, value] of Object.entries(vars)) {
287
+ result = result.replaceAll(`{{${key}}}`, value);
288
+ }
289
+ return result;
290
+ }
291
+ function buildExtractionPrompt(sessionId, eventCount, toolSummary) {
292
+ return interpolate(loadPrompt("extraction"), {
293
+ sessionId,
294
+ eventCount: String(eventCount),
295
+ toolSummary
296
+ });
297
+ }
298
+ function buildSummaryPrompt(sessionId, user, content) {
299
+ return interpolate(loadPrompt("summary"), {
300
+ sessionId,
301
+ user,
302
+ content
303
+ });
304
+ }
305
+ function buildTitlePrompt(summary, sessionId) {
306
+ return interpolate(loadPrompt("title"), {
307
+ summary,
308
+ sessionId
309
+ });
310
+ }
311
+ var ARTIFACT_TYPE_DESCRIPTIONS = [
312
+ '"spec" \u2014 Design specifications, architecture documents',
313
+ '"plan" \u2014 Implementation plans, roadmaps',
314
+ '"rfc" \u2014 Requests for comment, proposals',
315
+ '"doc" \u2014 Documentation, guides, READMEs',
316
+ '"other" \u2014 Other substantive documents'
317
+ ];
318
+ function buildSimilarityPrompt(currentSummary, candidateSummary) {
319
+ return interpolate(loadPrompt("session-similarity"), {
320
+ currentSummary,
321
+ candidateSummary
322
+ });
323
+ }
324
+ function buildClassificationPrompt(sessionId, candidates) {
325
+ const fileList = candidates.map((c) => {
326
+ const truncated = c.content.slice(0, CANDIDATE_CONTENT_PREVIEW);
327
+ return `### ${c.path}
328
+ \`\`\`
329
+ ${truncated}
330
+ \`\`\``;
331
+ }).join("\n\n");
332
+ return interpolate(loadPrompt("classification"), {
333
+ sessionId,
334
+ fileList,
335
+ artifactTypes: ARTIFACT_TYPE_DESCRIPTIONS.map((d) => `- ${d}`).join("\n"),
336
+ validTypes: ARTIFACT_TYPES.join("|")
337
+ });
338
+ }
339
+
340
+ // src/intelligence/response.ts
341
+ var REASONING_PATTERNS = [
342
+ // <think>...</think>answer (DeepSeek, Qwen, GLM, many others)
343
+ /<think>[\s\S]*?<\/think>\s*/gi,
344
+ // Implicit opening: reasoning...</think>answer (GLM-4.7 observed)
345
+ /^[\s\S]*?<\/think>\s*/i,
346
+ // <reasoning>...</reasoning>answer
347
+ /<reasoning>[\s\S]*?<\/reasoning>\s*/gi,
348
+ // <|thinking|>...<|/thinking|>answer
349
+ /<\|thinking\|>[\s\S]*?<\|\/thinking\|>\s*/gi
350
+ ];
351
+ function stripReasoningTokens(text) {
352
+ if (!text) return text;
353
+ for (const pattern of REASONING_PATTERNS) {
354
+ const stripped = text.replace(pattern, "").trim();
355
+ if (stripped && stripped !== text.trim()) {
356
+ return stripped;
357
+ }
358
+ }
359
+ return text;
360
+ }
361
+ function extractJson(text) {
362
+ const cleaned = stripReasoningTokens(text);
363
+ const fenceMatch = cleaned.match(/```(?:json)?\s*\n?([\s\S]*?)\n?```/);
364
+ if (fenceMatch) {
365
+ return JSON.parse(fenceMatch[1].trim());
366
+ }
367
+ const objectMatch = cleaned.match(/\{[\s\S]*\}/);
368
+ if (objectMatch) {
369
+ return JSON.parse(objectMatch[0]);
370
+ }
371
+ return JSON.parse(cleaned);
372
+ }
373
+ function extractNumber(text) {
374
+ const cleaned = stripReasoningTokens(text).trim();
375
+ const match = cleaned.match(/(\d+\.?\d*)/);
376
+ if (match) return parseFloat(match[1]);
377
+ return parseFloat(cleaned);
378
+ }
379
+
380
+ // src/daemon/processor.ts
381
+ var ClassificationResponseSchema = external_exports.object({
382
+ artifacts: external_exports.array(external_exports.object({
383
+ source_path: external_exports.string(),
384
+ artifact_type: external_exports.enum(ARTIFACT_TYPES),
385
+ title: external_exports.string(),
386
+ tags: external_exports.array(external_exports.string()).default([])
387
+ })).default([])
388
+ });
389
+ var BufferProcessor = class {
390
+ constructor(backend, contextWindow = 8192) {
391
+ this.backend = backend;
392
+ this.contextWindow = contextWindow;
393
+ }
394
+ truncateForContext(data, maxTokens) {
395
+ const available = this.contextWindow - maxTokens;
396
+ const dataTokens = Math.ceil(data.length / CHARS_PER_TOKEN);
397
+ if (dataTokens <= available) return data;
398
+ const charBudget = available * CHARS_PER_TOKEN;
399
+ return data.slice(0, charBudget);
400
+ }
401
+ async process(events, sessionId) {
402
+ const rawPrompt = this.buildPromptForExtraction(events, sessionId);
403
+ const prompt = this.truncateForContext(rawPrompt, EXTRACTION_MAX_TOKENS);
404
+ try {
405
+ const response = await this.backend.summarize(prompt, { maxTokens: EXTRACTION_MAX_TOKENS });
406
+ const parsed = extractJson(response.text);
407
+ return {
408
+ summary: parsed.summary,
409
+ observations: parsed.observations ?? [],
410
+ degraded: false
411
+ };
412
+ } catch (error) {
413
+ return {
414
+ summary: `LLM processing failed for session ${sessionId}. ${events.length} events captured. Error: ${error.message}`,
415
+ observations: [],
416
+ degraded: true
417
+ };
418
+ }
419
+ }
420
+ buildPromptForExtraction(events, sessionId) {
421
+ const toolSummary = this.summarizeEvents(events);
422
+ return buildExtractionPrompt(sessionId, events.length, toolSummary);
423
+ }
424
+ async summarizeSession(conversationMarkdown, sessionId, user) {
425
+ const truncatedContent = this.truncateForContext(conversationMarkdown, SUMMARY_MAX_TOKENS);
426
+ const summaryPrompt = buildSummaryPrompt(sessionId, user ?? "unknown", truncatedContent);
427
+ let summaryText;
428
+ try {
429
+ const response = await this.backend.summarize(summaryPrompt, { maxTokens: SUMMARY_MAX_TOKENS });
430
+ summaryText = stripReasoningTokens(response.text);
431
+ } catch (error) {
432
+ summaryText = `Session ${sessionId} \u2014 summarization failed: ${error.message}`;
433
+ }
434
+ const titlePrompt = buildTitlePrompt(summaryText, sessionId);
435
+ let title;
436
+ try {
437
+ const response = await this.backend.summarize(titlePrompt, { maxTokens: TITLE_MAX_TOKENS });
438
+ title = stripReasoningTokens(response.text).trim();
439
+ } catch {
440
+ title = `Session ${sessionId}`;
441
+ }
442
+ return { summary: summaryText, title };
443
+ }
444
+ async classifyArtifacts(candidates, sessionId) {
445
+ if (candidates.length === 0) return [];
446
+ const prompt = this.buildPromptForClassification(candidates, sessionId);
447
+ const response = await this.backend.summarize(prompt, { maxTokens: CLASSIFICATION_MAX_TOKENS });
448
+ const raw = extractJson(response.text);
449
+ const parsed = ClassificationResponseSchema.parse(raw);
450
+ return parsed.artifacts;
451
+ }
452
+ buildPromptForClassification(candidates, sessionId) {
453
+ return buildClassificationPrompt(sessionId, candidates);
454
+ }
455
+ summarizeEvents(events) {
456
+ const toolCounts = /* @__PURE__ */ new Map();
457
+ const filesAccessed = /* @__PURE__ */ new Set();
458
+ const prompts = [];
459
+ const aiResponses = [];
460
+ for (const event of events) {
461
+ if (event.type === "user_prompt") {
462
+ const prompt = String(event.prompt ?? "");
463
+ if (prompt) prompts.push(prompt.slice(0, PROMPT_PREVIEW_CHARS));
464
+ continue;
465
+ }
466
+ if (event.type === "ai_response") {
467
+ const content = String(event.content ?? "");
468
+ if (content) aiResponses.push(content.slice(0, AI_RESPONSE_PREVIEW_CHARS));
469
+ continue;
470
+ }
471
+ const tool = String(event.tool_name ?? event.tool ?? "unknown");
472
+ toolCounts.set(tool, (toolCounts.get(tool) ?? 0) + 1);
473
+ const input = event.tool_input ?? event.input;
474
+ if (input?.path) filesAccessed.add(String(input.path));
475
+ if (input?.file_path) filesAccessed.add(String(input.file_path));
476
+ if (input?.command) filesAccessed.add(`[cmd] ${String(input.command).slice(0, COMMAND_PREVIEW_CHARS)}`);
477
+ }
478
+ const lines = [];
479
+ if (prompts.length > 0) {
480
+ lines.push("### User Prompts");
481
+ for (const p of prompts) {
482
+ lines.push(`- "${p}"`);
483
+ }
484
+ }
485
+ lines.push("\n### Tool Usage");
486
+ for (const [tool, count] of toolCounts) {
487
+ lines.push(`- ${tool}: ${count} calls`);
488
+ }
489
+ if (filesAccessed.size > 0) {
490
+ lines.push("\n### Files Accessed");
491
+ for (const file of filesAccessed) {
492
+ lines.push(`- ${file}`);
493
+ }
494
+ }
495
+ if (aiResponses.length > 0) {
496
+ lines.push("\n### AI Responses");
497
+ for (const r of aiResponses) {
498
+ lines.push(`- "${r}"`);
499
+ }
500
+ }
501
+ return lines.join("\n");
502
+ }
503
+ };
504
+
505
+ // src/daemon/lineage.ts
506
+ import fs3 from "fs";
507
+ import path3 from "path";
508
+ var LINEAGE_IMMEDIATE_GAP_SECONDS = 5;
509
+ var LINEAGE_FALLBACK_MAX_HOURS = 24;
510
+ var LINEAGE_SIMILARITY_THRESHOLD = 0.7;
511
+ var LINEAGE_SIMILARITY_HIGH_CONFIDENCE = 0.9;
512
+ var LINEAGE_SIMILARITY_CANDIDATES = 3;
513
+ var LINEAGE_SIMILARITY_MAX_TOKENS = 8;
514
+ var MS_PER_SECOND = 1e3;
515
+ var MS_PER_HOUR = 36e5;
516
+ var LineageGraph = class {
517
+ state;
518
+ filePath;
519
+ constructor(vaultDir) {
520
+ this.filePath = path3.join(vaultDir, "lineage.json");
521
+ fs3.mkdirSync(path3.dirname(this.filePath), { recursive: true });
522
+ this.state = this.load();
523
+ }
524
+ addLink(link) {
525
+ if (this.state.links.some((l) => l.parent === link.parent && l.child === link.child)) return;
526
+ this.state.links.push({ ...link, timestamp: link.timestamp ?? (/* @__PURE__ */ new Date()).toISOString() });
527
+ this.persist();
528
+ }
529
+ /** Register an artifact (plan, spec, doc) as produced by a session. */
530
+ registerArtifactForSession(sessionId, artifactId) {
531
+ if (!this.state.sessionArtifacts[sessionId]) this.state.sessionArtifacts[sessionId] = [];
532
+ if (!this.state.sessionArtifacts[sessionId].includes(artifactId)) {
533
+ this.state.sessionArtifacts[sessionId].push(artifactId);
534
+ this.persist();
535
+ }
536
+ }
537
+ /** @deprecated Use registerArtifactForSession instead */
538
+ registerPlanForSession(sessionId, planId) {
539
+ this.registerArtifactForSession(sessionId, planId);
540
+ }
541
+ detectLineage(childSessionId, firstPrompt) {
542
+ for (const [sessionId, artifactIds] of Object.entries(this.state.sessionArtifacts)) {
543
+ if (sessionId === childSessionId) continue;
544
+ for (const artifactId of artifactIds) {
545
+ if (firstPrompt.includes(artifactId)) {
546
+ const link = { parent: sessionId, child: childSessionId, signal: "plan_reference", confidence: "high" };
547
+ this.addLink(link);
548
+ return link;
549
+ }
550
+ }
551
+ }
552
+ return null;
553
+ }
554
+ detectHeuristicParent(childSessionId, context, recentSessions, activeSessions, firstPrompt) {
555
+ const startedAt = new Date(context.started_at).getTime();
556
+ for (const session of recentSessions) {
557
+ if (!session.ended) continue;
558
+ const endedAt = new Date(session.ended).getTime();
559
+ const gapSeconds = (startedAt - endedAt) / MS_PER_SECOND;
560
+ if (gapSeconds >= 0 && gapSeconds <= LINEAGE_IMMEDIATE_GAP_SECONDS) {
561
+ const link = { parent: session.id, child: childSessionId, signal: "clear", confidence: "high" };
562
+ this.addLink(link);
563
+ return link;
564
+ }
565
+ }
566
+ for (const session of activeSessions) {
567
+ if (session.id === childSessionId) continue;
568
+ const link = { parent: session.id, child: childSessionId, signal: "clear_active", confidence: "high" };
569
+ this.addLink(link);
570
+ return link;
571
+ }
572
+ if (context.branch) {
573
+ for (const session of recentSessions) {
574
+ if (!session.ended || !session.branch) continue;
575
+ if (session.branch !== context.branch) continue;
576
+ const endedAt = new Date(session.ended).getTime();
577
+ const hoursAgo = (startedAt - endedAt) / MS_PER_HOUR;
578
+ if (hoursAgo >= 0 && hoursAgo <= LINEAGE_FALLBACK_MAX_HOURS) {
579
+ const link = { parent: session.id, child: childSessionId, signal: "inferred", confidence: "medium" };
580
+ this.addLink(link);
581
+ return link;
582
+ }
583
+ }
584
+ }
585
+ if (firstPrompt) {
586
+ return this.detectLineage(childSessionId, firstPrompt);
587
+ }
588
+ return null;
589
+ }
590
+ getLinks() {
591
+ return [...this.state.links];
592
+ }
593
+ getChildren(sessionId) {
594
+ return this.state.links.filter((l) => l.parent === sessionId).map((l) => l.child);
595
+ }
596
+ getParent(sessionId) {
597
+ return this.state.links.find((l) => l.child === sessionId)?.parent;
598
+ }
599
+ load() {
600
+ try {
601
+ const raw = JSON.parse(fs3.readFileSync(this.filePath, "utf-8"));
602
+ const sessionArtifacts = raw.sessionArtifacts ?? raw.sessionPlans ?? {};
603
+ return { links: raw.links ?? [], sessionArtifacts };
604
+ } catch {
605
+ return { links: [], sessionArtifacts: {} };
606
+ }
607
+ }
608
+ persist() {
609
+ const tmp = this.filePath + ".tmp";
610
+ fs3.writeFileSync(tmp, JSON.stringify(this.state, null, 2));
611
+ fs3.renameSync(tmp, this.filePath);
612
+ }
613
+ };
614
+
615
+ // node_modules/chokidar/index.js
616
+ import { EventEmitter } from "events";
617
+ import { stat as statcb, Stats } from "fs";
618
+ import { readdir as readdir2, stat as stat3 } from "fs/promises";
619
+ import * as sp2 from "path";
620
+
621
+ // node_modules/readdirp/index.js
622
+ import { lstat, readdir, realpath, stat } from "fs/promises";
623
+ import { join as pjoin, relative as prelative, resolve as presolve, sep as psep } from "path";
624
+ import { Readable } from "stream";
625
+ var EntryTypes = {
626
+ FILE_TYPE: "files",
627
+ DIR_TYPE: "directories",
628
+ FILE_DIR_TYPE: "files_directories",
629
+ EVERYTHING_TYPE: "all"
630
+ };
631
+ var defaultOptions = {
632
+ root: ".",
633
+ fileFilter: (_entryInfo) => true,
634
+ directoryFilter: (_entryInfo) => true,
635
+ type: EntryTypes.FILE_TYPE,
636
+ lstat: false,
637
+ depth: 2147483648,
638
+ alwaysStat: false,
639
+ highWaterMark: 4096
640
+ };
641
+ Object.freeze(defaultOptions);
642
+ var RECURSIVE_ERROR_CODE = "READDIRP_RECURSIVE_ERROR";
643
+ var NORMAL_FLOW_ERRORS = /* @__PURE__ */ new Set(["ENOENT", "EPERM", "EACCES", "ELOOP", RECURSIVE_ERROR_CODE]);
644
+ var ALL_TYPES = [
645
+ EntryTypes.DIR_TYPE,
646
+ EntryTypes.EVERYTHING_TYPE,
647
+ EntryTypes.FILE_DIR_TYPE,
648
+ EntryTypes.FILE_TYPE
649
+ ];
650
+ var DIR_TYPES = /* @__PURE__ */ new Set([
651
+ EntryTypes.DIR_TYPE,
652
+ EntryTypes.EVERYTHING_TYPE,
653
+ EntryTypes.FILE_DIR_TYPE
654
+ ]);
655
+ var FILE_TYPES = /* @__PURE__ */ new Set([
656
+ EntryTypes.EVERYTHING_TYPE,
657
+ EntryTypes.FILE_DIR_TYPE,
658
+ EntryTypes.FILE_TYPE
659
+ ]);
660
+ var isNormalFlowError = (error) => NORMAL_FLOW_ERRORS.has(error.code);
661
+ var wantBigintFsStats = process.platform === "win32";
662
+ var emptyFn = (_entryInfo) => true;
663
+ var normalizeFilter = (filter) => {
664
+ if (filter === void 0)
665
+ return emptyFn;
666
+ if (typeof filter === "function")
667
+ return filter;
668
+ if (typeof filter === "string") {
669
+ const fl = filter.trim();
670
+ return (entry) => entry.basename === fl;
671
+ }
672
+ if (Array.isArray(filter)) {
673
+ const trItems = filter.map((item) => item.trim());
674
+ return (entry) => trItems.some((f) => entry.basename === f);
675
+ }
676
+ return emptyFn;
677
+ };
678
+ var ReaddirpStream = class extends Readable {
679
+ parents;
680
+ reading;
681
+ parent;
682
+ _stat;
683
+ _maxDepth;
684
+ _wantsDir;
685
+ _wantsFile;
686
+ _wantsEverything;
687
+ _root;
688
+ _isDirent;
689
+ _statsProp;
690
+ _rdOptions;
691
+ _fileFilter;
692
+ _directoryFilter;
693
+ constructor(options = {}) {
694
+ super({
695
+ objectMode: true,
696
+ autoDestroy: true,
697
+ highWaterMark: options.highWaterMark
698
+ });
699
+ const opts = { ...defaultOptions, ...options };
700
+ const { root, type } = opts;
701
+ this._fileFilter = normalizeFilter(opts.fileFilter);
702
+ this._directoryFilter = normalizeFilter(opts.directoryFilter);
703
+ const statMethod = opts.lstat ? lstat : stat;
704
+ if (wantBigintFsStats) {
705
+ this._stat = (path8) => statMethod(path8, { bigint: true });
706
+ } else {
707
+ this._stat = statMethod;
708
+ }
709
+ this._maxDepth = opts.depth != null && Number.isSafeInteger(opts.depth) ? opts.depth : defaultOptions.depth;
710
+ this._wantsDir = type ? DIR_TYPES.has(type) : false;
711
+ this._wantsFile = type ? FILE_TYPES.has(type) : false;
712
+ this._wantsEverything = type === EntryTypes.EVERYTHING_TYPE;
713
+ this._root = presolve(root);
714
+ this._isDirent = !opts.alwaysStat;
715
+ this._statsProp = this._isDirent ? "dirent" : "stats";
716
+ this._rdOptions = { encoding: "utf8", withFileTypes: this._isDirent };
717
+ this.parents = [this._exploreDir(root, 1)];
718
+ this.reading = false;
719
+ this.parent = void 0;
720
+ }
721
+ async _read(batch) {
722
+ if (this.reading)
723
+ return;
724
+ this.reading = true;
725
+ try {
726
+ while (!this.destroyed && batch > 0) {
727
+ const par = this.parent;
728
+ const fil = par && par.files;
729
+ if (fil && fil.length > 0) {
730
+ const { path: path8, depth } = par;
731
+ const slice = fil.splice(0, batch).map((dirent) => this._formatEntry(dirent, path8));
732
+ const awaited = await Promise.all(slice);
733
+ for (const entry of awaited) {
734
+ if (!entry)
735
+ continue;
736
+ if (this.destroyed)
737
+ return;
738
+ const entryType = await this._getEntryType(entry);
739
+ if (entryType === "directory" && this._directoryFilter(entry)) {
740
+ if (depth <= this._maxDepth) {
741
+ this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
742
+ }
743
+ if (this._wantsDir) {
744
+ this.push(entry);
745
+ batch--;
746
+ }
747
+ } else if ((entryType === "file" || this._includeAsFile(entry)) && this._fileFilter(entry)) {
748
+ if (this._wantsFile) {
749
+ this.push(entry);
750
+ batch--;
751
+ }
752
+ }
753
+ }
754
+ } else {
755
+ const parent = this.parents.pop();
756
+ if (!parent) {
757
+ this.push(null);
758
+ break;
759
+ }
760
+ this.parent = await parent;
761
+ if (this.destroyed)
762
+ return;
763
+ }
764
+ }
765
+ } catch (error) {
766
+ this.destroy(error);
767
+ } finally {
768
+ this.reading = false;
769
+ }
770
+ }
771
+ async _exploreDir(path8, depth) {
772
+ let files;
773
+ try {
774
+ files = await readdir(path8, this._rdOptions);
775
+ } catch (error) {
776
+ this._onError(error);
777
+ }
778
+ return { files, depth, path: path8 };
779
+ }
780
+ async _formatEntry(dirent, path8) {
781
+ let entry;
782
+ const basename3 = this._isDirent ? dirent.name : dirent;
783
+ try {
784
+ const fullPath = presolve(pjoin(path8, basename3));
785
+ entry = { path: prelative(this._root, fullPath), fullPath, basename: basename3 };
786
+ entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
787
+ } catch (err) {
788
+ this._onError(err);
789
+ return;
790
+ }
791
+ return entry;
792
+ }
793
+ _onError(err) {
794
+ if (isNormalFlowError(err) && !this.destroyed) {
795
+ this.emit("warn", err);
796
+ } else {
797
+ this.destroy(err);
798
+ }
799
+ }
800
+ async _getEntryType(entry) {
801
+ if (!entry && this._statsProp in entry) {
802
+ return "";
803
+ }
804
+ const stats = entry[this._statsProp];
805
+ if (stats.isFile())
806
+ return "file";
807
+ if (stats.isDirectory())
808
+ return "directory";
809
+ if (stats && stats.isSymbolicLink()) {
810
+ const full = entry.fullPath;
811
+ try {
812
+ const entryRealPath = await realpath(full);
813
+ const entryRealPathStats = await lstat(entryRealPath);
814
+ if (entryRealPathStats.isFile()) {
815
+ return "file";
816
+ }
817
+ if (entryRealPathStats.isDirectory()) {
818
+ const len = entryRealPath.length;
819
+ if (full.startsWith(entryRealPath) && full.substr(len, 1) === psep) {
820
+ const recursiveError = new Error(`Circular symlink detected: "${full}" points to "${entryRealPath}"`);
821
+ recursiveError.code = RECURSIVE_ERROR_CODE;
822
+ return this._onError(recursiveError);
823
+ }
824
+ return "directory";
825
+ }
826
+ } catch (error) {
827
+ this._onError(error);
828
+ return "";
829
+ }
830
+ }
831
+ }
832
+ _includeAsFile(entry) {
833
+ const stats = entry && entry[this._statsProp];
834
+ return stats && this._wantsEverything && !stats.isDirectory();
835
+ }
836
+ };
837
+ function readdirp(root, options = {}) {
838
+ let type = options.entryType || options.type;
839
+ if (type === "both")
840
+ type = EntryTypes.FILE_DIR_TYPE;
841
+ if (type)
842
+ options.type = type;
843
+ if (!root) {
844
+ throw new Error("readdirp: root argument is required. Usage: readdirp(root, options)");
845
+ } else if (typeof root !== "string") {
846
+ throw new TypeError("readdirp: root argument must be a string. Usage: readdirp(root, options)");
847
+ } else if (type && !ALL_TYPES.includes(type)) {
848
+ throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(", ")}`);
849
+ }
850
+ options.root = root;
851
+ return new ReaddirpStream(options);
852
+ }
853
+
854
+ // node_modules/chokidar/handler.js
855
+ import { watch as fs_watch, unwatchFile, watchFile } from "fs";
856
+ import { realpath as fsrealpath, lstat as lstat2, open, stat as stat2 } from "fs/promises";
857
+ import { type as osType } from "os";
858
+ import * as sp from "path";
859
+ var STR_DATA = "data";
860
+ var STR_END = "end";
861
+ var STR_CLOSE = "close";
862
+ var EMPTY_FN = () => {
863
+ };
864
+ var pl = process.platform;
865
+ var isWindows = pl === "win32";
866
+ var isMacos = pl === "darwin";
867
+ var isLinux = pl === "linux";
868
+ var isFreeBSD = pl === "freebsd";
869
+ var isIBMi = osType() === "OS400";
870
+ var EVENTS = {
871
+ ALL: "all",
872
+ READY: "ready",
873
+ ADD: "add",
874
+ CHANGE: "change",
875
+ ADD_DIR: "addDir",
876
+ UNLINK: "unlink",
877
+ UNLINK_DIR: "unlinkDir",
878
+ RAW: "raw",
879
+ ERROR: "error"
880
+ };
881
+ var EV = EVENTS;
882
+ var THROTTLE_MODE_WATCH = "watch";
883
+ var statMethods = { lstat: lstat2, stat: stat2 };
884
+ var KEY_LISTENERS = "listeners";
885
+ var KEY_ERR = "errHandlers";
886
+ var KEY_RAW = "rawEmitters";
887
+ var HANDLER_KEYS = [KEY_LISTENERS, KEY_ERR, KEY_RAW];
888
+ var binaryExtensions = /* @__PURE__ */ new Set([
889
+ "3dm",
890
+ "3ds",
891
+ "3g2",
892
+ "3gp",
893
+ "7z",
894
+ "a",
895
+ "aac",
896
+ "adp",
897
+ "afdesign",
898
+ "afphoto",
899
+ "afpub",
900
+ "ai",
901
+ "aif",
902
+ "aiff",
903
+ "alz",
904
+ "ape",
905
+ "apk",
906
+ "appimage",
907
+ "ar",
908
+ "arj",
909
+ "asf",
910
+ "au",
911
+ "avi",
912
+ "bak",
913
+ "baml",
914
+ "bh",
915
+ "bin",
916
+ "bk",
917
+ "bmp",
918
+ "btif",
919
+ "bz2",
920
+ "bzip2",
921
+ "cab",
922
+ "caf",
923
+ "cgm",
924
+ "class",
925
+ "cmx",
926
+ "cpio",
927
+ "cr2",
928
+ "cur",
929
+ "dat",
930
+ "dcm",
931
+ "deb",
932
+ "dex",
933
+ "djvu",
934
+ "dll",
935
+ "dmg",
936
+ "dng",
937
+ "doc",
938
+ "docm",
939
+ "docx",
940
+ "dot",
941
+ "dotm",
942
+ "dra",
943
+ "DS_Store",
944
+ "dsk",
945
+ "dts",
946
+ "dtshd",
947
+ "dvb",
948
+ "dwg",
949
+ "dxf",
950
+ "ecelp4800",
951
+ "ecelp7470",
952
+ "ecelp9600",
953
+ "egg",
954
+ "eol",
955
+ "eot",
956
+ "epub",
957
+ "exe",
958
+ "f4v",
959
+ "fbs",
960
+ "fh",
961
+ "fla",
962
+ "flac",
963
+ "flatpak",
964
+ "fli",
965
+ "flv",
966
+ "fpx",
967
+ "fst",
968
+ "fvt",
969
+ "g3",
970
+ "gh",
971
+ "gif",
972
+ "graffle",
973
+ "gz",
974
+ "gzip",
975
+ "h261",
976
+ "h263",
977
+ "h264",
978
+ "icns",
979
+ "ico",
980
+ "ief",
981
+ "img",
982
+ "ipa",
983
+ "iso",
984
+ "jar",
985
+ "jpeg",
986
+ "jpg",
987
+ "jpgv",
988
+ "jpm",
989
+ "jxr",
990
+ "key",
991
+ "ktx",
992
+ "lha",
993
+ "lib",
994
+ "lvp",
995
+ "lz",
996
+ "lzh",
997
+ "lzma",
998
+ "lzo",
999
+ "m3u",
1000
+ "m4a",
1001
+ "m4v",
1002
+ "mar",
1003
+ "mdi",
1004
+ "mht",
1005
+ "mid",
1006
+ "midi",
1007
+ "mj2",
1008
+ "mka",
1009
+ "mkv",
1010
+ "mmr",
1011
+ "mng",
1012
+ "mobi",
1013
+ "mov",
1014
+ "movie",
1015
+ "mp3",
1016
+ "mp4",
1017
+ "mp4a",
1018
+ "mpeg",
1019
+ "mpg",
1020
+ "mpga",
1021
+ "mxu",
1022
+ "nef",
1023
+ "npx",
1024
+ "numbers",
1025
+ "nupkg",
1026
+ "o",
1027
+ "odp",
1028
+ "ods",
1029
+ "odt",
1030
+ "oga",
1031
+ "ogg",
1032
+ "ogv",
1033
+ "otf",
1034
+ "ott",
1035
+ "pages",
1036
+ "pbm",
1037
+ "pcx",
1038
+ "pdb",
1039
+ "pdf",
1040
+ "pea",
1041
+ "pgm",
1042
+ "pic",
1043
+ "png",
1044
+ "pnm",
1045
+ "pot",
1046
+ "potm",
1047
+ "potx",
1048
+ "ppa",
1049
+ "ppam",
1050
+ "ppm",
1051
+ "pps",
1052
+ "ppsm",
1053
+ "ppsx",
1054
+ "ppt",
1055
+ "pptm",
1056
+ "pptx",
1057
+ "psd",
1058
+ "pya",
1059
+ "pyc",
1060
+ "pyo",
1061
+ "pyv",
1062
+ "qt",
1063
+ "rar",
1064
+ "ras",
1065
+ "raw",
1066
+ "resources",
1067
+ "rgb",
1068
+ "rip",
1069
+ "rlc",
1070
+ "rmf",
1071
+ "rmvb",
1072
+ "rpm",
1073
+ "rtf",
1074
+ "rz",
1075
+ "s3m",
1076
+ "s7z",
1077
+ "scpt",
1078
+ "sgi",
1079
+ "shar",
1080
+ "snap",
1081
+ "sil",
1082
+ "sketch",
1083
+ "slk",
1084
+ "smv",
1085
+ "snk",
1086
+ "so",
1087
+ "stl",
1088
+ "suo",
1089
+ "sub",
1090
+ "swf",
1091
+ "tar",
1092
+ "tbz",
1093
+ "tbz2",
1094
+ "tga",
1095
+ "tgz",
1096
+ "thmx",
1097
+ "tif",
1098
+ "tiff",
1099
+ "tlz",
1100
+ "ttc",
1101
+ "ttf",
1102
+ "txz",
1103
+ "udf",
1104
+ "uvh",
1105
+ "uvi",
1106
+ "uvm",
1107
+ "uvp",
1108
+ "uvs",
1109
+ "uvu",
1110
+ "viv",
1111
+ "vob",
1112
+ "war",
1113
+ "wav",
1114
+ "wax",
1115
+ "wbmp",
1116
+ "wdp",
1117
+ "weba",
1118
+ "webm",
1119
+ "webp",
1120
+ "whl",
1121
+ "wim",
1122
+ "wm",
1123
+ "wma",
1124
+ "wmv",
1125
+ "wmx",
1126
+ "woff",
1127
+ "woff2",
1128
+ "wrm",
1129
+ "wvx",
1130
+ "xbm",
1131
+ "xif",
1132
+ "xla",
1133
+ "xlam",
1134
+ "xls",
1135
+ "xlsb",
1136
+ "xlsm",
1137
+ "xlsx",
1138
+ "xlt",
1139
+ "xltm",
1140
+ "xltx",
1141
+ "xm",
1142
+ "xmind",
1143
+ "xpi",
1144
+ "xpm",
1145
+ "xwd",
1146
+ "xz",
1147
+ "z",
1148
+ "zip",
1149
+ "zipx"
1150
+ ]);
1151
+ var isBinaryPath = (filePath) => binaryExtensions.has(sp.extname(filePath).slice(1).toLowerCase());
1152
+ var foreach = (val, fn) => {
1153
+ if (val instanceof Set) {
1154
+ val.forEach(fn);
1155
+ } else {
1156
+ fn(val);
1157
+ }
1158
+ };
1159
+ var addAndConvert = (main2, prop, item) => {
1160
+ let container = main2[prop];
1161
+ if (!(container instanceof Set)) {
1162
+ main2[prop] = container = /* @__PURE__ */ new Set([container]);
1163
+ }
1164
+ container.add(item);
1165
+ };
1166
+ var clearItem = (cont) => (key) => {
1167
+ const set = cont[key];
1168
+ if (set instanceof Set) {
1169
+ set.clear();
1170
+ } else {
1171
+ delete cont[key];
1172
+ }
1173
+ };
1174
+ var delFromSet = (main2, prop, item) => {
1175
+ const container = main2[prop];
1176
+ if (container instanceof Set) {
1177
+ container.delete(item);
1178
+ } else if (container === item) {
1179
+ delete main2[prop];
1180
+ }
1181
+ };
1182
+ var isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
1183
+ var FsWatchInstances = /* @__PURE__ */ new Map();
1184
+ function createFsWatchInstance(path8, options, listener, errHandler, emitRaw) {
1185
+ const handleEvent = (rawEvent, evPath) => {
1186
+ listener(path8);
1187
+ emitRaw(rawEvent, evPath, { watchedPath: path8 });
1188
+ if (evPath && path8 !== evPath) {
1189
+ fsWatchBroadcast(sp.resolve(path8, evPath), KEY_LISTENERS, sp.join(path8, evPath));
1190
+ }
1191
+ };
1192
+ try {
1193
+ return fs_watch(path8, {
1194
+ persistent: options.persistent
1195
+ }, handleEvent);
1196
+ } catch (error) {
1197
+ errHandler(error);
1198
+ return void 0;
1199
+ }
1200
+ }
1201
+ var fsWatchBroadcast = (fullPath, listenerType, val1, val2, val3) => {
1202
+ const cont = FsWatchInstances.get(fullPath);
1203
+ if (!cont)
1204
+ return;
1205
+ foreach(cont[listenerType], (listener) => {
1206
+ listener(val1, val2, val3);
1207
+ });
1208
+ };
1209
+ var setFsWatchListener = (path8, fullPath, options, handlers) => {
1210
+ const { listener, errHandler, rawEmitter } = handlers;
1211
+ let cont = FsWatchInstances.get(fullPath);
1212
+ let watcher;
1213
+ if (!options.persistent) {
1214
+ watcher = createFsWatchInstance(path8, options, listener, errHandler, rawEmitter);
1215
+ if (!watcher)
1216
+ return;
1217
+ return watcher.close.bind(watcher);
1218
+ }
1219
+ if (cont) {
1220
+ addAndConvert(cont, KEY_LISTENERS, listener);
1221
+ addAndConvert(cont, KEY_ERR, errHandler);
1222
+ addAndConvert(cont, KEY_RAW, rawEmitter);
1223
+ } else {
1224
+ watcher = createFsWatchInstance(
1225
+ path8,
1226
+ options,
1227
+ fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
1228
+ errHandler,
1229
+ // no need to use broadcast here
1230
+ fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
1231
+ );
1232
+ if (!watcher)
1233
+ return;
1234
+ watcher.on(EV.ERROR, async (error) => {
1235
+ const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
1236
+ if (cont)
1237
+ cont.watcherUnusable = true;
1238
+ if (isWindows && error.code === "EPERM") {
1239
+ try {
1240
+ const fd = await open(path8, "r");
1241
+ await fd.close();
1242
+ broadcastErr(error);
1243
+ } catch (err) {
1244
+ }
1245
+ } else {
1246
+ broadcastErr(error);
1247
+ }
1248
+ });
1249
+ cont = {
1250
+ listeners: listener,
1251
+ errHandlers: errHandler,
1252
+ rawEmitters: rawEmitter,
1253
+ watcher
1254
+ };
1255
+ FsWatchInstances.set(fullPath, cont);
1256
+ }
1257
+ return () => {
1258
+ delFromSet(cont, KEY_LISTENERS, listener);
1259
+ delFromSet(cont, KEY_ERR, errHandler);
1260
+ delFromSet(cont, KEY_RAW, rawEmitter);
1261
+ if (isEmptySet(cont.listeners)) {
1262
+ cont.watcher.close();
1263
+ FsWatchInstances.delete(fullPath);
1264
+ HANDLER_KEYS.forEach(clearItem(cont));
1265
+ cont.watcher = void 0;
1266
+ Object.freeze(cont);
1267
+ }
1268
+ };
1269
+ };
1270
+ var FsWatchFileInstances = /* @__PURE__ */ new Map();
1271
+ var setFsWatchFileListener = (path8, fullPath, options, handlers) => {
1272
+ const { listener, rawEmitter } = handlers;
1273
+ let cont = FsWatchFileInstances.get(fullPath);
1274
+ const copts = cont && cont.options;
1275
+ if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
1276
+ unwatchFile(fullPath);
1277
+ cont = void 0;
1278
+ }
1279
+ if (cont) {
1280
+ addAndConvert(cont, KEY_LISTENERS, listener);
1281
+ addAndConvert(cont, KEY_RAW, rawEmitter);
1282
+ } else {
1283
+ cont = {
1284
+ listeners: listener,
1285
+ rawEmitters: rawEmitter,
1286
+ options,
1287
+ watcher: watchFile(fullPath, options, (curr, prev) => {
1288
+ foreach(cont.rawEmitters, (rawEmitter2) => {
1289
+ rawEmitter2(EV.CHANGE, fullPath, { curr, prev });
1290
+ });
1291
+ const currmtime = curr.mtimeMs;
1292
+ if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
1293
+ foreach(cont.listeners, (listener2) => listener2(path8, curr));
1294
+ }
1295
+ })
1296
+ };
1297
+ FsWatchFileInstances.set(fullPath, cont);
1298
+ }
1299
+ return () => {
1300
+ delFromSet(cont, KEY_LISTENERS, listener);
1301
+ delFromSet(cont, KEY_RAW, rawEmitter);
1302
+ if (isEmptySet(cont.listeners)) {
1303
+ FsWatchFileInstances.delete(fullPath);
1304
+ unwatchFile(fullPath);
1305
+ cont.options = cont.watcher = void 0;
1306
+ Object.freeze(cont);
1307
+ }
1308
+ };
1309
+ };
1310
+ var NodeFsHandler = class {
1311
+ fsw;
1312
+ _boundHandleError;
1313
+ constructor(fsW) {
1314
+ this.fsw = fsW;
1315
+ this._boundHandleError = (error) => fsW._handleError(error);
1316
+ }
1317
+ /**
1318
+ * Watch file for changes with fs_watchFile or fs_watch.
1319
+ * @param path to file or dir
1320
+ * @param listener on fs change
1321
+ * @returns closer for the watcher instance
1322
+ */
1323
+ _watchWithNodeFs(path8, listener) {
1324
+ const opts = this.fsw.options;
1325
+ const directory = sp.dirname(path8);
1326
+ const basename3 = sp.basename(path8);
1327
+ const parent = this.fsw._getWatchedDir(directory);
1328
+ parent.add(basename3);
1329
+ const absolutePath = sp.resolve(path8);
1330
+ const options = {
1331
+ persistent: opts.persistent
1332
+ };
1333
+ if (!listener)
1334
+ listener = EMPTY_FN;
1335
+ let closer;
1336
+ if (opts.usePolling) {
1337
+ const enableBin = opts.interval !== opts.binaryInterval;
1338
+ options.interval = enableBin && isBinaryPath(basename3) ? opts.binaryInterval : opts.interval;
1339
+ closer = setFsWatchFileListener(path8, absolutePath, options, {
1340
+ listener,
1341
+ rawEmitter: this.fsw._emitRaw
1342
+ });
1343
+ } else {
1344
+ closer = setFsWatchListener(path8, absolutePath, options, {
1345
+ listener,
1346
+ errHandler: this._boundHandleError,
1347
+ rawEmitter: this.fsw._emitRaw
1348
+ });
1349
+ }
1350
+ return closer;
1351
+ }
1352
+ /**
1353
+ * Watch a file and emit add event if warranted.
1354
+ * @returns closer for the watcher instance
1355
+ */
1356
+ _handleFile(file, stats, initialAdd) {
1357
+ if (this.fsw.closed) {
1358
+ return;
1359
+ }
1360
+ const dirname3 = sp.dirname(file);
1361
+ const basename3 = sp.basename(file);
1362
+ const parent = this.fsw._getWatchedDir(dirname3);
1363
+ let prevStats = stats;
1364
+ if (parent.has(basename3))
1365
+ return;
1366
+ const listener = async (path8, newStats) => {
1367
+ if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5))
1368
+ return;
1369
+ if (!newStats || newStats.mtimeMs === 0) {
1370
+ try {
1371
+ const newStats2 = await stat2(file);
1372
+ if (this.fsw.closed)
1373
+ return;
1374
+ const at = newStats2.atimeMs;
1375
+ const mt = newStats2.mtimeMs;
1376
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
1377
+ this.fsw._emit(EV.CHANGE, file, newStats2);
1378
+ }
1379
+ if ((isMacos || isLinux || isFreeBSD) && prevStats.ino !== newStats2.ino) {
1380
+ this.fsw._closeFile(path8);
1381
+ prevStats = newStats2;
1382
+ const closer2 = this._watchWithNodeFs(file, listener);
1383
+ if (closer2)
1384
+ this.fsw._addPathCloser(path8, closer2);
1385
+ } else {
1386
+ prevStats = newStats2;
1387
+ }
1388
+ } catch (error) {
1389
+ this.fsw._remove(dirname3, basename3);
1390
+ }
1391
+ } else if (parent.has(basename3)) {
1392
+ const at = newStats.atimeMs;
1393
+ const mt = newStats.mtimeMs;
1394
+ if (!at || at <= mt || mt !== prevStats.mtimeMs) {
1395
+ this.fsw._emit(EV.CHANGE, file, newStats);
1396
+ }
1397
+ prevStats = newStats;
1398
+ }
1399
+ };
1400
+ const closer = this._watchWithNodeFs(file, listener);
1401
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
1402
+ if (!this.fsw._throttle(EV.ADD, file, 0))
1403
+ return;
1404
+ this.fsw._emit(EV.ADD, file, stats);
1405
+ }
1406
+ return closer;
1407
+ }
1408
+ /**
1409
+ * Handle symlinks encountered while reading a dir.
1410
+ * @param entry returned by readdirp
1411
+ * @param directory path of dir being read
1412
+ * @param path of this item
1413
+ * @param item basename of this item
1414
+ * @returns true if no more processing is needed for this entry.
1415
+ */
1416
+ async _handleSymlink(entry, directory, path8, item) {
1417
+ if (this.fsw.closed) {
1418
+ return;
1419
+ }
1420
+ const full = entry.fullPath;
1421
+ const dir = this.fsw._getWatchedDir(directory);
1422
+ if (!this.fsw.options.followSymlinks) {
1423
+ this.fsw._incrReadyCount();
1424
+ let linkPath;
1425
+ try {
1426
+ linkPath = await fsrealpath(path8);
1427
+ } catch (e) {
1428
+ this.fsw._emitReady();
1429
+ return true;
1430
+ }
1431
+ if (this.fsw.closed)
1432
+ return;
1433
+ if (dir.has(item)) {
1434
+ if (this.fsw._symlinkPaths.get(full) !== linkPath) {
1435
+ this.fsw._symlinkPaths.set(full, linkPath);
1436
+ this.fsw._emit(EV.CHANGE, path8, entry.stats);
1437
+ }
1438
+ } else {
1439
+ dir.add(item);
1440
+ this.fsw._symlinkPaths.set(full, linkPath);
1441
+ this.fsw._emit(EV.ADD, path8, entry.stats);
1442
+ }
1443
+ this.fsw._emitReady();
1444
+ return true;
1445
+ }
1446
+ if (this.fsw._symlinkPaths.has(full)) {
1447
+ return true;
1448
+ }
1449
+ this.fsw._symlinkPaths.set(full, true);
1450
+ }
1451
+ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
1452
+ directory = sp.join(directory, "");
1453
+ const throttleKey = target ? `${directory}:${target}` : directory;
1454
+ throttler = this.fsw._throttle("readdir", throttleKey, 1e3);
1455
+ if (!throttler)
1456
+ return;
1457
+ const previous = this.fsw._getWatchedDir(wh.path);
1458
+ const current = /* @__PURE__ */ new Set();
1459
+ let stream = this.fsw._readdirp(directory, {
1460
+ fileFilter: (entry) => wh.filterPath(entry),
1461
+ directoryFilter: (entry) => wh.filterDir(entry)
1462
+ });
1463
+ if (!stream)
1464
+ return;
1465
+ stream.on(STR_DATA, async (entry) => {
1466
+ if (this.fsw.closed) {
1467
+ stream = void 0;
1468
+ return;
1469
+ }
1470
+ const item = entry.path;
1471
+ let path8 = sp.join(directory, item);
1472
+ current.add(item);
1473
+ if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path8, item)) {
1474
+ return;
1475
+ }
1476
+ if (this.fsw.closed) {
1477
+ stream = void 0;
1478
+ return;
1479
+ }
1480
+ if (item === target || !target && !previous.has(item)) {
1481
+ this.fsw._incrReadyCount();
1482
+ path8 = sp.join(dir, sp.relative(dir, path8));
1483
+ this._addToNodeFs(path8, initialAdd, wh, depth + 1);
1484
+ }
1485
+ }).on(EV.ERROR, this._boundHandleError);
1486
+ return new Promise((resolve3, reject) => {
1487
+ if (!stream)
1488
+ return reject();
1489
+ stream.once(STR_END, () => {
1490
+ if (this.fsw.closed) {
1491
+ stream = void 0;
1492
+ return;
1493
+ }
1494
+ const wasThrottled = throttler ? throttler.clear() : false;
1495
+ resolve3(void 0);
1496
+ previous.getChildren().filter((item) => {
1497
+ return item !== directory && !current.has(item);
1498
+ }).forEach((item) => {
1499
+ this.fsw._remove(directory, item);
1500
+ });
1501
+ stream = void 0;
1502
+ if (wasThrottled)
1503
+ this._handleRead(directory, false, wh, target, dir, depth, throttler);
1504
+ });
1505
+ });
1506
+ }
1507
+ /**
1508
+ * Read directory to add / remove files from `@watched` list and re-read it on change.
1509
+ * @param dir fs path
1510
+ * @param stats
1511
+ * @param initialAdd
1512
+ * @param depth relative to user-supplied path
1513
+ * @param target child path targeted for watch
1514
+ * @param wh Common watch helpers for this path
1515
+ * @param realpath
1516
+ * @returns closer for the watcher instance.
1517
+ */
1518
+ async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath2) {
1519
+ const parentDir = this.fsw._getWatchedDir(sp.dirname(dir));
1520
+ const tracked = parentDir.has(sp.basename(dir));
1521
+ if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
1522
+ this.fsw._emit(EV.ADD_DIR, dir, stats);
1523
+ }
1524
+ parentDir.add(sp.basename(dir));
1525
+ this.fsw._getWatchedDir(dir);
1526
+ let throttler;
1527
+ let closer;
1528
+ const oDepth = this.fsw.options.depth;
1529
+ if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath2)) {
1530
+ if (!target) {
1531
+ await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
1532
+ if (this.fsw.closed)
1533
+ return;
1534
+ }
1535
+ closer = this._watchWithNodeFs(dir, (dirPath, stats2) => {
1536
+ if (stats2 && stats2.mtimeMs === 0)
1537
+ return;
1538
+ this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
1539
+ });
1540
+ }
1541
+ return closer;
1542
+ }
1543
+ /**
1544
+ * Handle added file, directory, or glob pattern.
1545
+ * Delegates call to _handleFile / _handleDir after checks.
1546
+ * @param path to file or ir
1547
+ * @param initialAdd was the file added at watch instantiation?
1548
+ * @param priorWh depth relative to user-supplied path
1549
+ * @param depth Child path actually targeted for watch
1550
+ * @param target Child path actually targeted for watch
1551
+ */
1552
+ async _addToNodeFs(path8, initialAdd, priorWh, depth, target) {
1553
+ const ready = this.fsw._emitReady;
1554
+ if (this.fsw._isIgnored(path8) || this.fsw.closed) {
1555
+ ready();
1556
+ return false;
1557
+ }
1558
+ const wh = this.fsw._getWatchHelpers(path8);
1559
+ if (priorWh) {
1560
+ wh.filterPath = (entry) => priorWh.filterPath(entry);
1561
+ wh.filterDir = (entry) => priorWh.filterDir(entry);
1562
+ }
1563
+ try {
1564
+ const stats = await statMethods[wh.statMethod](wh.watchPath);
1565
+ if (this.fsw.closed)
1566
+ return;
1567
+ if (this.fsw._isIgnored(wh.watchPath, stats)) {
1568
+ ready();
1569
+ return false;
1570
+ }
1571
+ const follow = this.fsw.options.followSymlinks;
1572
+ let closer;
1573
+ if (stats.isDirectory()) {
1574
+ const absPath = sp.resolve(path8);
1575
+ const targetPath = follow ? await fsrealpath(path8) : path8;
1576
+ if (this.fsw.closed)
1577
+ return;
1578
+ closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
1579
+ if (this.fsw.closed)
1580
+ return;
1581
+ if (absPath !== targetPath && targetPath !== void 0) {
1582
+ this.fsw._symlinkPaths.set(absPath, targetPath);
1583
+ }
1584
+ } else if (stats.isSymbolicLink()) {
1585
+ const targetPath = follow ? await fsrealpath(path8) : path8;
1586
+ if (this.fsw.closed)
1587
+ return;
1588
+ const parent = sp.dirname(wh.watchPath);
1589
+ this.fsw._getWatchedDir(parent).add(wh.watchPath);
1590
+ this.fsw._emit(EV.ADD, wh.watchPath, stats);
1591
+ closer = await this._handleDir(parent, stats, initialAdd, depth, path8, wh, targetPath);
1592
+ if (this.fsw.closed)
1593
+ return;
1594
+ if (targetPath !== void 0) {
1595
+ this.fsw._symlinkPaths.set(sp.resolve(path8), targetPath);
1596
+ }
1597
+ } else {
1598
+ closer = this._handleFile(wh.watchPath, stats, initialAdd);
1599
+ }
1600
+ ready();
1601
+ if (closer)
1602
+ this.fsw._addPathCloser(path8, closer);
1603
+ return false;
1604
+ } catch (error) {
1605
+ if (this.fsw._handleError(error)) {
1606
+ ready();
1607
+ return path8;
1608
+ }
1609
+ }
1610
+ }
1611
+ };
1612
+
1613
+ // node_modules/chokidar/index.js
1614
+ var SLASH = "/";
1615
+ var SLASH_SLASH = "//";
1616
+ var ONE_DOT = ".";
1617
+ var TWO_DOTS = "..";
1618
+ var STRING_TYPE = "string";
1619
+ var BACK_SLASH_RE = /\\/g;
1620
+ var DOUBLE_SLASH_RE = /\/\//g;
1621
+ var DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
1622
+ var REPLACER_RE = /^\.[/\\]/;
1623
+ function arrify(item) {
1624
+ return Array.isArray(item) ? item : [item];
1625
+ }
1626
+ var isMatcherObject = (matcher) => typeof matcher === "object" && matcher !== null && !(matcher instanceof RegExp);
1627
+ function createPattern(matcher) {
1628
+ if (typeof matcher === "function")
1629
+ return matcher;
1630
+ if (typeof matcher === "string")
1631
+ return (string) => matcher === string;
1632
+ if (matcher instanceof RegExp)
1633
+ return (string) => matcher.test(string);
1634
+ if (typeof matcher === "object" && matcher !== null) {
1635
+ return (string) => {
1636
+ if (matcher.path === string)
1637
+ return true;
1638
+ if (matcher.recursive) {
1639
+ const relative3 = sp2.relative(matcher.path, string);
1640
+ if (!relative3) {
1641
+ return false;
1642
+ }
1643
+ return !relative3.startsWith("..") && !sp2.isAbsolute(relative3);
1644
+ }
1645
+ return false;
1646
+ };
1647
+ }
1648
+ return () => false;
1649
+ }
1650
+ function normalizePath(path8) {
1651
+ if (typeof path8 !== "string")
1652
+ throw new Error("string expected");
1653
+ path8 = sp2.normalize(path8);
1654
+ path8 = path8.replace(/\\/g, "/");
1655
+ let prepend = false;
1656
+ if (path8.startsWith("//"))
1657
+ prepend = true;
1658
+ path8 = path8.replace(DOUBLE_SLASH_RE, "/");
1659
+ if (prepend)
1660
+ path8 = "/" + path8;
1661
+ return path8;
1662
+ }
1663
+ function matchPatterns(patterns, testString, stats) {
1664
+ const path8 = normalizePath(testString);
1665
+ for (let index = 0; index < patterns.length; index++) {
1666
+ const pattern = patterns[index];
1667
+ if (pattern(path8, stats)) {
1668
+ return true;
1669
+ }
1670
+ }
1671
+ return false;
1672
+ }
1673
+ function anymatch(matchers, testString) {
1674
+ if (matchers == null) {
1675
+ throw new TypeError("anymatch: specify first argument");
1676
+ }
1677
+ const matchersArray = arrify(matchers);
1678
+ const patterns = matchersArray.map((matcher) => createPattern(matcher));
1679
+ if (testString == null) {
1680
+ return (testString2, stats) => {
1681
+ return matchPatterns(patterns, testString2, stats);
1682
+ };
1683
+ }
1684
+ return matchPatterns(patterns, testString);
1685
+ }
1686
+ var unifyPaths = (paths_) => {
1687
+ const paths = arrify(paths_).flat();
1688
+ if (!paths.every((p) => typeof p === STRING_TYPE)) {
1689
+ throw new TypeError(`Non-string provided as watch path: ${paths}`);
1690
+ }
1691
+ return paths.map(normalizePathToUnix);
1692
+ };
1693
+ var toUnix = (string) => {
1694
+ let str = string.replace(BACK_SLASH_RE, SLASH);
1695
+ let prepend = false;
1696
+ if (str.startsWith(SLASH_SLASH)) {
1697
+ prepend = true;
1698
+ }
1699
+ str = str.replace(DOUBLE_SLASH_RE, SLASH);
1700
+ if (prepend) {
1701
+ str = SLASH + str;
1702
+ }
1703
+ return str;
1704
+ };
1705
+ var normalizePathToUnix = (path8) => toUnix(sp2.normalize(toUnix(path8)));
1706
+ var normalizeIgnored = (cwd = "") => (path8) => {
1707
+ if (typeof path8 === "string") {
1708
+ return normalizePathToUnix(sp2.isAbsolute(path8) ? path8 : sp2.join(cwd, path8));
1709
+ } else {
1710
+ return path8;
1711
+ }
1712
+ };
1713
+ var getAbsolutePath = (path8, cwd) => {
1714
+ if (sp2.isAbsolute(path8)) {
1715
+ return path8;
1716
+ }
1717
+ return sp2.join(cwd, path8);
1718
+ };
1719
+ var EMPTY_SET = Object.freeze(/* @__PURE__ */ new Set());
1720
+ var DirEntry = class {
1721
+ path;
1722
+ _removeWatcher;
1723
+ items;
1724
+ constructor(dir, removeWatcher) {
1725
+ this.path = dir;
1726
+ this._removeWatcher = removeWatcher;
1727
+ this.items = /* @__PURE__ */ new Set();
1728
+ }
1729
+ add(item) {
1730
+ const { items } = this;
1731
+ if (!items)
1732
+ return;
1733
+ if (item !== ONE_DOT && item !== TWO_DOTS)
1734
+ items.add(item);
1735
+ }
1736
+ async remove(item) {
1737
+ const { items } = this;
1738
+ if (!items)
1739
+ return;
1740
+ items.delete(item);
1741
+ if (items.size > 0)
1742
+ return;
1743
+ const dir = this.path;
1744
+ try {
1745
+ await readdir2(dir);
1746
+ } catch (err) {
1747
+ if (this._removeWatcher) {
1748
+ this._removeWatcher(sp2.dirname(dir), sp2.basename(dir));
1749
+ }
1750
+ }
1751
+ }
1752
+ has(item) {
1753
+ const { items } = this;
1754
+ if (!items)
1755
+ return;
1756
+ return items.has(item);
1757
+ }
1758
+ getChildren() {
1759
+ const { items } = this;
1760
+ if (!items)
1761
+ return [];
1762
+ return [...items.values()];
1763
+ }
1764
+ dispose() {
1765
+ this.items.clear();
1766
+ this.path = "";
1767
+ this._removeWatcher = EMPTY_FN;
1768
+ this.items = EMPTY_SET;
1769
+ Object.freeze(this);
1770
+ }
1771
+ };
1772
+ var STAT_METHOD_F = "stat";
1773
+ var STAT_METHOD_L = "lstat";
1774
+ var WatchHelper = class {
1775
+ fsw;
1776
+ path;
1777
+ watchPath;
1778
+ fullWatchPath;
1779
+ dirParts;
1780
+ followSymlinks;
1781
+ statMethod;
1782
+ constructor(path8, follow, fsw) {
1783
+ this.fsw = fsw;
1784
+ const watchPath = path8;
1785
+ this.path = path8 = path8.replace(REPLACER_RE, "");
1786
+ this.watchPath = watchPath;
1787
+ this.fullWatchPath = sp2.resolve(watchPath);
1788
+ this.dirParts = [];
1789
+ this.dirParts.forEach((parts) => {
1790
+ if (parts.length > 1)
1791
+ parts.pop();
1792
+ });
1793
+ this.followSymlinks = follow;
1794
+ this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
1795
+ }
1796
+ entryPath(entry) {
1797
+ return sp2.join(this.watchPath, sp2.relative(this.watchPath, entry.fullPath));
1798
+ }
1799
+ filterPath(entry) {
1800
+ const { stats } = entry;
1801
+ if (stats && stats.isSymbolicLink())
1802
+ return this.filterDir(entry);
1803
+ const resolvedPath = this.entryPath(entry);
1804
+ return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
1805
+ }
1806
+ filterDir(entry) {
1807
+ return this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
1808
+ }
1809
+ };
1810
+ var FSWatcher = class extends EventEmitter {
1811
+ closed;
1812
+ options;
1813
+ _closers;
1814
+ _ignoredPaths;
1815
+ _throttled;
1816
+ _streams;
1817
+ _symlinkPaths;
1818
+ _watched;
1819
+ _pendingWrites;
1820
+ _pendingUnlinks;
1821
+ _readyCount;
1822
+ _emitReady;
1823
+ _closePromise;
1824
+ _userIgnored;
1825
+ _readyEmitted;
1826
+ _emitRaw;
1827
+ _boundRemove;
1828
+ _nodeFsHandler;
1829
+ // Not indenting methods for history sake; for now.
1830
+ constructor(_opts = {}) {
1831
+ super();
1832
+ this.closed = false;
1833
+ this._closers = /* @__PURE__ */ new Map();
1834
+ this._ignoredPaths = /* @__PURE__ */ new Set();
1835
+ this._throttled = /* @__PURE__ */ new Map();
1836
+ this._streams = /* @__PURE__ */ new Set();
1837
+ this._symlinkPaths = /* @__PURE__ */ new Map();
1838
+ this._watched = /* @__PURE__ */ new Map();
1839
+ this._pendingWrites = /* @__PURE__ */ new Map();
1840
+ this._pendingUnlinks = /* @__PURE__ */ new Map();
1841
+ this._readyCount = 0;
1842
+ this._readyEmitted = false;
1843
+ const awf = _opts.awaitWriteFinish;
1844
+ const DEF_AWF = { stabilityThreshold: 2e3, pollInterval: 100 };
1845
+ const opts = {
1846
+ // Defaults
1847
+ persistent: true,
1848
+ ignoreInitial: false,
1849
+ ignorePermissionErrors: false,
1850
+ interval: 100,
1851
+ binaryInterval: 300,
1852
+ followSymlinks: true,
1853
+ usePolling: false,
1854
+ // useAsync: false,
1855
+ atomic: true,
1856
+ // NOTE: overwritten later (depends on usePolling)
1857
+ ..._opts,
1858
+ // Change format
1859
+ ignored: _opts.ignored ? arrify(_opts.ignored) : arrify([]),
1860
+ awaitWriteFinish: awf === true ? DEF_AWF : typeof awf === "object" ? { ...DEF_AWF, ...awf } : false
1861
+ };
1862
+ if (isIBMi)
1863
+ opts.usePolling = true;
1864
+ if (opts.atomic === void 0)
1865
+ opts.atomic = !opts.usePolling;
1866
+ const envPoll = process.env.CHOKIDAR_USEPOLLING;
1867
+ if (envPoll !== void 0) {
1868
+ const envLower = envPoll.toLowerCase();
1869
+ if (envLower === "false" || envLower === "0")
1870
+ opts.usePolling = false;
1871
+ else if (envLower === "true" || envLower === "1")
1872
+ opts.usePolling = true;
1873
+ else
1874
+ opts.usePolling = !!envLower;
1875
+ }
1876
+ const envInterval = process.env.CHOKIDAR_INTERVAL;
1877
+ if (envInterval)
1878
+ opts.interval = Number.parseInt(envInterval, 10);
1879
+ let readyCalls = 0;
1880
+ this._emitReady = () => {
1881
+ readyCalls++;
1882
+ if (readyCalls >= this._readyCount) {
1883
+ this._emitReady = EMPTY_FN;
1884
+ this._readyEmitted = true;
1885
+ process.nextTick(() => this.emit(EVENTS.READY));
1886
+ }
1887
+ };
1888
+ this._emitRaw = (...args) => this.emit(EVENTS.RAW, ...args);
1889
+ this._boundRemove = this._remove.bind(this);
1890
+ this.options = opts;
1891
+ this._nodeFsHandler = new NodeFsHandler(this);
1892
+ Object.freeze(opts);
1893
+ }
1894
+ _addIgnoredPath(matcher) {
1895
+ if (isMatcherObject(matcher)) {
1896
+ for (const ignored of this._ignoredPaths) {
1897
+ if (isMatcherObject(ignored) && ignored.path === matcher.path && ignored.recursive === matcher.recursive) {
1898
+ return;
1899
+ }
1900
+ }
1901
+ }
1902
+ this._ignoredPaths.add(matcher);
1903
+ }
1904
+ _removeIgnoredPath(matcher) {
1905
+ this._ignoredPaths.delete(matcher);
1906
+ if (typeof matcher === "string") {
1907
+ for (const ignored of this._ignoredPaths) {
1908
+ if (isMatcherObject(ignored) && ignored.path === matcher) {
1909
+ this._ignoredPaths.delete(ignored);
1910
+ }
1911
+ }
1912
+ }
1913
+ }
1914
+ // Public methods
1915
+ /**
1916
+ * Adds paths to be watched on an existing FSWatcher instance.
1917
+ * @param paths_ file or file list. Other arguments are unused
1918
+ */
1919
+ add(paths_, _origAdd, _internal) {
1920
+ const { cwd } = this.options;
1921
+ this.closed = false;
1922
+ this._closePromise = void 0;
1923
+ let paths = unifyPaths(paths_);
1924
+ if (cwd) {
1925
+ paths = paths.map((path8) => {
1926
+ const absPath = getAbsolutePath(path8, cwd);
1927
+ return absPath;
1928
+ });
1929
+ }
1930
+ paths.forEach((path8) => {
1931
+ this._removeIgnoredPath(path8);
1932
+ });
1933
+ this._userIgnored = void 0;
1934
+ if (!this._readyCount)
1935
+ this._readyCount = 0;
1936
+ this._readyCount += paths.length;
1937
+ Promise.all(paths.map(async (path8) => {
1938
+ const res = await this._nodeFsHandler._addToNodeFs(path8, !_internal, void 0, 0, _origAdd);
1939
+ if (res)
1940
+ this._emitReady();
1941
+ return res;
1942
+ })).then((results) => {
1943
+ if (this.closed)
1944
+ return;
1945
+ results.forEach((item) => {
1946
+ if (item)
1947
+ this.add(sp2.dirname(item), sp2.basename(_origAdd || item));
1948
+ });
1949
+ });
1950
+ return this;
1951
+ }
1952
+ /**
1953
+ * Close watchers or start ignoring events from specified paths.
1954
+ */
1955
+ unwatch(paths_) {
1956
+ if (this.closed)
1957
+ return this;
1958
+ const paths = unifyPaths(paths_);
1959
+ const { cwd } = this.options;
1960
+ paths.forEach((path8) => {
1961
+ if (!sp2.isAbsolute(path8) && !this._closers.has(path8)) {
1962
+ if (cwd)
1963
+ path8 = sp2.join(cwd, path8);
1964
+ path8 = sp2.resolve(path8);
1965
+ }
1966
+ this._closePath(path8);
1967
+ this._addIgnoredPath(path8);
1968
+ if (this._watched.has(path8)) {
1969
+ this._addIgnoredPath({
1970
+ path: path8,
1971
+ recursive: true
1972
+ });
1973
+ }
1974
+ this._userIgnored = void 0;
1975
+ });
1976
+ return this;
1977
+ }
1978
+ /**
1979
+ * Close watchers and remove all listeners from watched paths.
1980
+ */
1981
+ close() {
1982
+ if (this._closePromise) {
1983
+ return this._closePromise;
1984
+ }
1985
+ this.closed = true;
1986
+ this.removeAllListeners();
1987
+ const closers = [];
1988
+ this._closers.forEach((closerList) => closerList.forEach((closer) => {
1989
+ const promise = closer();
1990
+ if (promise instanceof Promise)
1991
+ closers.push(promise);
1992
+ }));
1993
+ this._streams.forEach((stream) => stream.destroy());
1994
+ this._userIgnored = void 0;
1995
+ this._readyCount = 0;
1996
+ this._readyEmitted = false;
1997
+ this._watched.forEach((dirent) => dirent.dispose());
1998
+ this._closers.clear();
1999
+ this._watched.clear();
2000
+ this._streams.clear();
2001
+ this._symlinkPaths.clear();
2002
+ this._throttled.clear();
2003
+ this._closePromise = closers.length ? Promise.all(closers).then(() => void 0) : Promise.resolve();
2004
+ return this._closePromise;
2005
+ }
2006
+ /**
2007
+ * Expose list of watched paths
2008
+ * @returns for chaining
2009
+ */
2010
+ getWatched() {
2011
+ const watchList = {};
2012
+ this._watched.forEach((entry, dir) => {
2013
+ const key = this.options.cwd ? sp2.relative(this.options.cwd, dir) : dir;
2014
+ const index = key || ONE_DOT;
2015
+ watchList[index] = entry.getChildren().sort();
2016
+ });
2017
+ return watchList;
2018
+ }
2019
+ emitWithAll(event, args) {
2020
+ this.emit(event, ...args);
2021
+ if (event !== EVENTS.ERROR)
2022
+ this.emit(EVENTS.ALL, event, ...args);
2023
+ }
2024
+ // Common helpers
2025
+ // --------------
2026
+ /**
2027
+ * Normalize and emit events.
2028
+ * Calling _emit DOES NOT MEAN emit() would be called!
2029
+ * @param event Type of event
2030
+ * @param path File or directory path
2031
+ * @param stats arguments to be passed with event
2032
+ * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
2033
+ */
2034
+ async _emit(event, path8, stats) {
2035
+ if (this.closed)
2036
+ return;
2037
+ const opts = this.options;
2038
+ if (isWindows)
2039
+ path8 = sp2.normalize(path8);
2040
+ if (opts.cwd)
2041
+ path8 = sp2.relative(opts.cwd, path8);
2042
+ const args = [path8];
2043
+ if (stats != null)
2044
+ args.push(stats);
2045
+ const awf = opts.awaitWriteFinish;
2046
+ let pw;
2047
+ if (awf && (pw = this._pendingWrites.get(path8))) {
2048
+ pw.lastChange = /* @__PURE__ */ new Date();
2049
+ return this;
2050
+ }
2051
+ if (opts.atomic) {
2052
+ if (event === EVENTS.UNLINK) {
2053
+ this._pendingUnlinks.set(path8, [event, ...args]);
2054
+ setTimeout(() => {
2055
+ this._pendingUnlinks.forEach((entry, path9) => {
2056
+ this.emit(...entry);
2057
+ this.emit(EVENTS.ALL, ...entry);
2058
+ this._pendingUnlinks.delete(path9);
2059
+ });
2060
+ }, typeof opts.atomic === "number" ? opts.atomic : 100);
2061
+ return this;
2062
+ }
2063
+ if (event === EVENTS.ADD && this._pendingUnlinks.has(path8)) {
2064
+ event = EVENTS.CHANGE;
2065
+ this._pendingUnlinks.delete(path8);
2066
+ }
2067
+ }
2068
+ if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this._readyEmitted) {
2069
+ const awfEmit = (err, stats2) => {
2070
+ if (err) {
2071
+ event = EVENTS.ERROR;
2072
+ args[0] = err;
2073
+ this.emitWithAll(event, args);
2074
+ } else if (stats2) {
2075
+ if (args.length > 1) {
2076
+ args[1] = stats2;
2077
+ } else {
2078
+ args.push(stats2);
2079
+ }
2080
+ this.emitWithAll(event, args);
2081
+ }
2082
+ };
2083
+ this._awaitWriteFinish(path8, awf.stabilityThreshold, event, awfEmit);
2084
+ return this;
2085
+ }
2086
+ if (event === EVENTS.CHANGE) {
2087
+ const isThrottled = !this._throttle(EVENTS.CHANGE, path8, 50);
2088
+ if (isThrottled)
2089
+ return this;
2090
+ }
2091
+ if (opts.alwaysStat && stats === void 0 && (event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
2092
+ const fullPath = opts.cwd ? sp2.join(opts.cwd, path8) : path8;
2093
+ let stats2;
2094
+ try {
2095
+ stats2 = await stat3(fullPath);
2096
+ } catch (err) {
2097
+ }
2098
+ if (!stats2 || this.closed)
2099
+ return;
2100
+ args.push(stats2);
2101
+ }
2102
+ this.emitWithAll(event, args);
2103
+ return this;
2104
+ }
2105
+ /**
2106
+ * Common handler for errors
2107
+ * @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
2108
+ */
2109
+ _handleError(error) {
2110
+ const code = error && error.code;
2111
+ if (error && code !== "ENOENT" && code !== "ENOTDIR" && (!this.options.ignorePermissionErrors || code !== "EPERM" && code !== "EACCES")) {
2112
+ this.emit(EVENTS.ERROR, error);
2113
+ }
2114
+ return error || this.closed;
2115
+ }
2116
+ /**
2117
+ * Helper utility for throttling
2118
+ * @param actionType type being throttled
2119
+ * @param path being acted upon
2120
+ * @param timeout duration of time to suppress duplicate actions
2121
+ * @returns tracking object or false if action should be suppressed
2122
+ */
2123
+ _throttle(actionType, path8, timeout) {
2124
+ if (!this._throttled.has(actionType)) {
2125
+ this._throttled.set(actionType, /* @__PURE__ */ new Map());
2126
+ }
2127
+ const action = this._throttled.get(actionType);
2128
+ if (!action)
2129
+ throw new Error("invalid throttle");
2130
+ const actionPath = action.get(path8);
2131
+ if (actionPath) {
2132
+ actionPath.count++;
2133
+ return false;
2134
+ }
2135
+ let timeoutObject;
2136
+ const clear = () => {
2137
+ const item = action.get(path8);
2138
+ const count = item ? item.count : 0;
2139
+ action.delete(path8);
2140
+ clearTimeout(timeoutObject);
2141
+ if (item)
2142
+ clearTimeout(item.timeoutObject);
2143
+ return count;
2144
+ };
2145
+ timeoutObject = setTimeout(clear, timeout);
2146
+ const thr = { timeoutObject, clear, count: 0 };
2147
+ action.set(path8, thr);
2148
+ return thr;
2149
+ }
2150
+ _incrReadyCount() {
2151
+ return this._readyCount++;
2152
+ }
2153
+ /**
2154
+ * Awaits write operation to finish.
2155
+ * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
2156
+ * @param path being acted upon
2157
+ * @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
2158
+ * @param event
2159
+ * @param awfEmit Callback to be called when ready for event to be emitted.
2160
+ */
2161
+ _awaitWriteFinish(path8, threshold, event, awfEmit) {
2162
+ const awf = this.options.awaitWriteFinish;
2163
+ if (typeof awf !== "object")
2164
+ return;
2165
+ const pollInterval = awf.pollInterval;
2166
+ let timeoutHandler;
2167
+ let fullPath = path8;
2168
+ if (this.options.cwd && !sp2.isAbsolute(path8)) {
2169
+ fullPath = sp2.join(this.options.cwd, path8);
2170
+ }
2171
+ const now = /* @__PURE__ */ new Date();
2172
+ const writes = this._pendingWrites;
2173
+ function awaitWriteFinishFn(prevStat) {
2174
+ statcb(fullPath, (err, curStat) => {
2175
+ if (err || !writes.has(path8)) {
2176
+ if (err && err.code !== "ENOENT")
2177
+ awfEmit(err);
2178
+ return;
2179
+ }
2180
+ const now2 = Number(/* @__PURE__ */ new Date());
2181
+ if (prevStat && curStat.size !== prevStat.size) {
2182
+ writes.get(path8).lastChange = now2;
2183
+ }
2184
+ const pw = writes.get(path8);
2185
+ const df = now2 - pw.lastChange;
2186
+ if (df >= threshold) {
2187
+ writes.delete(path8);
2188
+ awfEmit(void 0, curStat);
2189
+ } else {
2190
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
2191
+ }
2192
+ });
2193
+ }
2194
+ if (!writes.has(path8)) {
2195
+ writes.set(path8, {
2196
+ lastChange: now,
2197
+ cancelWait: () => {
2198
+ writes.delete(path8);
2199
+ clearTimeout(timeoutHandler);
2200
+ return event;
2201
+ }
2202
+ });
2203
+ timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval);
2204
+ }
2205
+ }
2206
+ /**
2207
+ * Determines whether user has asked to ignore this path.
2208
+ */
2209
+ _isIgnored(path8, stats) {
2210
+ if (this.options.atomic && DOT_RE.test(path8))
2211
+ return true;
2212
+ if (!this._userIgnored) {
2213
+ const { cwd } = this.options;
2214
+ const ign = this.options.ignored;
2215
+ const ignored = (ign || []).map(normalizeIgnored(cwd));
2216
+ const ignoredPaths = [...this._ignoredPaths];
2217
+ const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
2218
+ this._userIgnored = anymatch(list, void 0);
2219
+ }
2220
+ return this._userIgnored(path8, stats);
2221
+ }
2222
+ _isntIgnored(path8, stat4) {
2223
+ return !this._isIgnored(path8, stat4);
2224
+ }
2225
+ /**
2226
+ * Provides a set of common helpers and properties relating to symlink handling.
2227
+ * @param path file or directory pattern being watched
2228
+ */
2229
+ _getWatchHelpers(path8) {
2230
+ return new WatchHelper(path8, this.options.followSymlinks, this);
2231
+ }
2232
+ // Directory helpers
2233
+ // -----------------
2234
+ /**
2235
+ * Provides directory tracking objects
2236
+ * @param directory path of the directory
2237
+ */
2238
+ _getWatchedDir(directory) {
2239
+ const dir = sp2.resolve(directory);
2240
+ if (!this._watched.has(dir))
2241
+ this._watched.set(dir, new DirEntry(dir, this._boundRemove));
2242
+ return this._watched.get(dir);
2243
+ }
2244
+ // File helpers
2245
+ // ------------
2246
+ /**
2247
+ * Check for read permissions: https://stackoverflow.com/a/11781404/1358405
2248
+ */
2249
+ _hasReadPermissions(stats) {
2250
+ if (this.options.ignorePermissionErrors)
2251
+ return true;
2252
+ return Boolean(Number(stats.mode) & 256);
2253
+ }
2254
+ /**
2255
+ * Handles emitting unlink events for
2256
+ * files and directories, and via recursion, for
2257
+ * files and directories within directories that are unlinked
2258
+ * @param directory within which the following item is located
2259
+ * @param item base path of item/directory
2260
+ */
2261
+ _remove(directory, item, isDirectory) {
2262
+ const path8 = sp2.join(directory, item);
2263
+ const fullPath = sp2.resolve(path8);
2264
+ isDirectory = isDirectory != null ? isDirectory : this._watched.has(path8) || this._watched.has(fullPath);
2265
+ if (!this._throttle("remove", path8, 100))
2266
+ return;
2267
+ if (!isDirectory && this._watched.size === 1) {
2268
+ this.add(directory, item, true);
2269
+ }
2270
+ const wp = this._getWatchedDir(path8);
2271
+ const nestedDirectoryChildren = wp.getChildren();
2272
+ nestedDirectoryChildren.forEach((nested) => this._remove(path8, nested));
2273
+ const parent = this._getWatchedDir(directory);
2274
+ const wasTracked = parent.has(item);
2275
+ parent.remove(item);
2276
+ if (this._symlinkPaths.has(fullPath)) {
2277
+ this._symlinkPaths.delete(fullPath);
2278
+ }
2279
+ let relPath = path8;
2280
+ if (this.options.cwd)
2281
+ relPath = sp2.relative(this.options.cwd, path8);
2282
+ if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
2283
+ const event = this._pendingWrites.get(relPath).cancelWait();
2284
+ if (event === EVENTS.ADD)
2285
+ return;
2286
+ }
2287
+ this._watched.delete(path8);
2288
+ this._watched.delete(fullPath);
2289
+ const eventName = isDirectory ? EVENTS.UNLINK_DIR : EVENTS.UNLINK;
2290
+ if (wasTracked && !this._isIgnored(path8))
2291
+ this._emit(eventName, path8);
2292
+ this._closePath(path8);
2293
+ }
2294
+ /**
2295
+ * Closes all watchers for a path
2296
+ */
2297
+ _closePath(path8) {
2298
+ this._closeFile(path8);
2299
+ const dir = sp2.dirname(path8);
2300
+ this._getWatchedDir(dir).remove(sp2.basename(path8));
2301
+ }
2302
+ /**
2303
+ * Closes only file-specific watchers
2304
+ */
2305
+ _closeFile(path8) {
2306
+ const closers = this._closers.get(path8);
2307
+ if (!closers)
2308
+ return;
2309
+ closers.forEach((closer) => closer());
2310
+ this._closers.delete(path8);
2311
+ }
2312
+ _addPathCloser(path8, closer) {
2313
+ if (!closer)
2314
+ return;
2315
+ let list = this._closers.get(path8);
2316
+ if (!list) {
2317
+ list = [];
2318
+ this._closers.set(path8, list);
2319
+ }
2320
+ list.push(closer);
2321
+ }
2322
+ _readdirp(root, opts) {
2323
+ if (this.closed)
2324
+ return;
2325
+ const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true, ...opts, depth: 0 };
2326
+ let stream = readdirp(root, options);
2327
+ this._streams.add(stream);
2328
+ stream.once(STR_CLOSE, () => {
2329
+ stream = void 0;
2330
+ });
2331
+ stream.once(STR_END, () => {
2332
+ if (stream) {
2333
+ this._streams.delete(stream);
2334
+ stream = void 0;
2335
+ }
2336
+ });
2337
+ return stream;
2338
+ }
2339
+ };
2340
+ function watch(paths, options = {}) {
2341
+ const watcher = new FSWatcher(options);
2342
+ watcher.add(paths);
2343
+ return watcher;
2344
+ }
2345
+
2346
+ // src/daemon/watcher.ts
2347
+ import path4 from "path";
2348
+ var PlanWatcher = class {
2349
+ config;
2350
+ fsWatcher = null;
2351
+ knownPlans = /* @__PURE__ */ new Set();
2352
+ constructor(config) {
2353
+ this.config = config;
2354
+ }
2355
+ checkToolEvent(event) {
2356
+ if (event.tool_name === "EnterPlanMode" || event.tool_name === "ExitPlanMode") {
2357
+ this.config.onPlan({
2358
+ source: "hook",
2359
+ sessionId: event.session_id,
2360
+ detail: `Plan mode ${event.tool_name === "EnterPlanMode" ? "entered" : "exited"}`,
2361
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
2362
+ });
2363
+ return;
2364
+ }
2365
+ if (["Write", "Edit", "Read"].includes(event.tool_name) && event.tool_input) {
2366
+ const filePath = event.tool_input.file_path ?? event.tool_input.path;
2367
+ if (filePath && this.isInPlanDirectory(filePath)) {
2368
+ this.knownPlans.add(filePath);
2369
+ this.config.onPlan({
2370
+ source: "tool",
2371
+ filePath,
2372
+ sessionId: event.session_id,
2373
+ detail: `${event.tool_name} on plan file: ${path4.basename(filePath)}`,
2374
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
2375
+ });
2376
+ }
2377
+ }
2378
+ }
2379
+ startFileWatcher() {
2380
+ const absPaths = this.config.watchPaths.map(
2381
+ (p) => path4.resolve(this.config.projectRoot, p)
2382
+ );
2383
+ this.fsWatcher = watch(absPaths, {
2384
+ ignoreInitial: true,
2385
+ persistent: true,
2386
+ depth: 3,
2387
+ awaitWriteFinish: { stabilityThreshold: FILE_WATCH_STABILITY_MS }
2388
+ });
2389
+ this.fsWatcher.on("add", (fp) => this.onFileChange(fp, "created"));
2390
+ this.fsWatcher.on("change", (fp) => this.onFileChange(fp, "modified"));
2391
+ }
2392
+ stopFileWatcher() {
2393
+ this.fsWatcher?.close();
2394
+ this.fsWatcher = null;
2395
+ }
2396
+ onFileChange(absolutePath, action) {
2397
+ const rel = path4.relative(this.config.projectRoot, absolutePath);
2398
+ this.knownPlans.add(absolutePath);
2399
+ this.config.onPlan({
2400
+ source: "filesystem",
2401
+ filePath: absolutePath,
2402
+ detail: `Plan file ${action}: ${rel}`,
2403
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
2404
+ });
2405
+ }
2406
+ isInPlanDirectory(filePath) {
2407
+ const abs = path4.isAbsolute(filePath) ? filePath : path4.resolve(this.config.projectRoot, filePath);
2408
+ return this.config.watchPaths.some(
2409
+ (wp) => abs.startsWith(path4.resolve(this.config.projectRoot, wp))
2410
+ );
2411
+ }
2412
+ };
2413
+
2414
+ // src/capture/transcript-miner.ts
2415
+ var TranscriptMiner = class {
2416
+ registry;
2417
+ constructor(config) {
2418
+ this.registry = new AgentRegistry(config?.additionalAdapters);
2419
+ }
2420
+ /**
2421
+ * Extract all conversation turns for a session.
2422
+ * Convenience wrapper — delegates to getAllTurnsWithSource.
2423
+ */
2424
+ getAllTurns(sessionId) {
2425
+ return this.getAllTurnsWithSource(sessionId).turns;
2426
+ }
2427
+ /**
2428
+ * Extract turns using the hook-provided transcript path first (fast, no scanning),
2429
+ * then fall back to adapter registry scanning if the path isn't provided.
2430
+ */
2431
+ getAllTurnsWithSource(sessionId, transcriptPath) {
2432
+ if (transcriptPath) {
2433
+ const result2 = this.registry.parseTurnsFromPath(transcriptPath);
2434
+ if (result2) return result2;
2435
+ }
2436
+ const result = this.registry.getTranscriptTurns(sessionId);
2437
+ if (result) return result;
2438
+ return { turns: [], source: "none" };
2439
+ }
2440
+ };
2441
+ function extractTurnsFromBuffer(events) {
2442
+ const turns = [];
2443
+ let current = null;
2444
+ for (const event of events) {
2445
+ const type = event.type;
2446
+ if (type === "user_prompt") {
2447
+ if (current) turns.push(current);
2448
+ current = {
2449
+ prompt: String(event.prompt ?? "").slice(0, PROMPT_PREVIEW_CHARS),
2450
+ toolCount: 0,
2451
+ timestamp: String(event.timestamp ?? (/* @__PURE__ */ new Date()).toISOString())
2452
+ };
2453
+ } else if (type === "tool_use") {
2454
+ if (current) current.toolCount++;
2455
+ }
2456
+ }
2457
+ if (current) turns.push(current);
2458
+ return turns;
2459
+ }
2460
+
2461
+ // src/vault/observations.ts
2462
+ function writeObservationNotes(observations, sessionId, writer, index, vaultDir) {
2463
+ const results = [];
2464
+ for (const obs of observations) {
2465
+ const obsId = `${obs.type}-${sessionId.slice(-6)}-${Date.now()}`;
2466
+ const body = formatMemoryBody({
2467
+ title: obs.title,
2468
+ observationType: obs.type,
2469
+ content: obs.content,
2470
+ sessionId,
2471
+ root_cause: obs.root_cause,
2472
+ fix: obs.fix,
2473
+ rationale: obs.rationale,
2474
+ alternatives_rejected: obs.alternatives_rejected,
2475
+ gained: obs.gained,
2476
+ sacrificed: obs.sacrificed,
2477
+ tags: obs.tags
2478
+ });
2479
+ const relativePath = writer.writeMemory({
2480
+ id: obsId,
2481
+ observation_type: obs.type,
2482
+ session: sessionNoteId(sessionId),
2483
+ tags: obs.tags,
2484
+ content: body
2485
+ });
2486
+ indexNote(index, vaultDir, relativePath);
2487
+ results.push({ id: obsId, path: relativePath, observation: obs });
2488
+ }
2489
+ return results;
2490
+ }
2491
+
2492
+ // src/artifacts/candidates.ts
2493
+ import { execFileSync } from "child_process";
2494
+ import fs4 from "fs";
2495
+ import path5 from "path";
2496
+ var EXCLUDED_FILENAMES = /* @__PURE__ */ new Set([
2497
+ "claude.md",
2498
+ "agents.md",
2499
+ "gemini.md",
2500
+ "readme.md",
2501
+ "contributing.md",
2502
+ "changelog.md",
2503
+ "license.md",
2504
+ "pull_request_template.md"
2505
+ ]);
2506
+ var EXCLUDED_PREFIXES = [
2507
+ "commands/",
2508
+ "skills/",
2509
+ "hooks/",
2510
+ ".claude-plugin/",
2511
+ ".cursor-plugin/",
2512
+ ".claude/",
2513
+ ".github/"
2514
+ ];
2515
+ function isExcludedPath(relativePath) {
2516
+ const basename3 = path5.basename(relativePath).toLowerCase();
2517
+ if (EXCLUDED_FILENAMES.has(basename3)) return true;
2518
+ const normalized = relativePath.replace(/\\/g, "/");
2519
+ return EXCLUDED_PREFIXES.some((prefix) => normalized.startsWith(prefix));
2520
+ }
2521
+ function collectArtifactCandidates(filePaths, config, projectRoot) {
2522
+ if (filePaths.size === 0) return [];
2523
+ const extFiltered = [...filePaths].filter(
2524
+ (absPath) => config.artifact_extensions.includes(path5.extname(absPath))
2525
+ );
2526
+ if (extFiltered.length === 0) return [];
2527
+ const ignoredSet = getGitIgnored(extFiltered, projectRoot);
2528
+ const candidates = [];
2529
+ for (const absPath of extFiltered) {
2530
+ if (ignoredSet.has(absPath)) continue;
2531
+ try {
2532
+ const content = fs4.readFileSync(absPath, "utf-8");
2533
+ const relativePath = path5.relative(projectRoot, absPath);
2534
+ if (isExcludedPath(relativePath)) continue;
2535
+ candidates.push({ path: relativePath, content });
2536
+ } catch {
2537
+ }
2538
+ }
2539
+ return candidates;
2540
+ }
2541
+ function getGitIgnored(filePaths, cwd) {
2542
+ try {
2543
+ const result = execFileSync("git", ["check-ignore", ...filePaths], {
2544
+ cwd,
2545
+ stdio: ["pipe", "pipe", "pipe"],
2546
+ encoding: "utf-8"
2547
+ });
2548
+ return new Set(result.trim().split("\n").filter(Boolean));
2549
+ } catch {
2550
+ return /* @__PURE__ */ new Set();
2551
+ }
2552
+ }
2553
+
2554
+ // src/artifacts/slugify.ts
2555
+ import crypto from "crypto";
2556
+ import path6 from "path";
2557
+ function slugifyPath(relativePath) {
2558
+ const ext = path6.extname(relativePath);
2559
+ const withoutExt = ext ? relativePath.slice(0, -ext.length) : relativePath;
2560
+ let slug = withoutExt.replace(/[/\\]/g, "-").toLowerCase().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "");
2561
+ if (slug.length > MAX_SLUG_LENGTH) {
2562
+ const hash = crypto.createHash("sha256").update(relativePath).digest("hex").slice(0, 6);
2563
+ slug = slug.slice(0, MAX_SLUG_LENGTH) + "-" + hash;
2564
+ }
2565
+ return slug;
2566
+ }
2567
+
2568
+ // src/daemon/main.ts
2569
+ var import_yaml = __toESM(require_dist(), 1);
2570
+ import fs5 from "fs";
2571
+ import path7 from "path";
2572
+ import { fileURLToPath as fileURLToPath2 } from "url";
2573
+ function indexAndEmbed(relativePath, noteId, embeddingText, metadata, deps) {
2574
+ indexNote(deps.index, deps.vaultDir, relativePath);
2575
+ if (deps.vectorIndex && embeddingText) {
2576
+ generateEmbedding(deps.embeddingProvider, embeddingText.slice(0, EMBEDDING_INPUT_LIMIT)).then((emb) => deps.vectorIndex.upsert(noteId, emb.embedding, metadata)).catch((err) => deps.logger.debug("embeddings", "Embedding failed", { id: noteId, error: err.message }));
2577
+ }
2578
+ }
2579
+ function writeObservations(observations, sessionId, deps) {
2580
+ const written = writeObservationNotes(observations, sessionId, deps.vault, deps.index, deps.vaultDir);
2581
+ for (const note of written) {
2582
+ indexAndEmbed(
2583
+ note.path,
2584
+ note.id,
2585
+ `${note.observation.title}
2586
+ ${note.observation.content}`,
2587
+ { type: "memory", importance: "high", session_id: sessionId },
2588
+ deps
2589
+ );
2590
+ deps.logger.info("processor", "Observation written", { type: note.observation.type, title: note.observation.title, session_id: sessionId });
2591
+ }
2592
+ }
2593
+ async function captureArtifacts(candidates, classified, sessionId, deps, lineage) {
2594
+ const candidateMap = new Map(candidates.map((c) => [c.path, c]));
2595
+ for (const artifact of classified) {
2596
+ const candidate = candidateMap.get(artifact.source_path);
2597
+ if (!candidate) continue;
2598
+ const artifactId = slugifyPath(artifact.source_path);
2599
+ const artifactPath = deps.vault.writeArtifact({
2600
+ id: artifactId,
2601
+ artifact_type: artifact.artifact_type,
2602
+ source_path: artifact.source_path,
2603
+ title: artifact.title,
2604
+ session: sessionId,
2605
+ tags: artifact.tags,
2606
+ content: candidate.content
2607
+ });
2608
+ indexAndEmbed(
2609
+ artifactPath,
2610
+ artifactId,
2611
+ `${artifact.title}
2612
+ ${candidate.content}`,
2613
+ { type: "artifact", artifact_type: artifact.artifact_type, session_id: sessionId },
2614
+ deps
2615
+ );
2616
+ deps.logger.info("processor", "Artifact captured", {
2617
+ id: artifactId,
2618
+ type: artifact.artifact_type,
2619
+ source: artifact.source_path
2620
+ });
2621
+ lineage?.registerArtifactForSession(sessionId, artifactId);
2622
+ }
2623
+ }
2624
+ function migrateMemoryFiles(vaultDir) {
2625
+ const memoriesDir = path7.join(vaultDir, "memories");
2626
+ if (!fs5.existsSync(memoriesDir)) return 0;
2627
+ let moved = 0;
2628
+ const entries = fs5.readdirSync(memoriesDir);
2629
+ for (const entry of entries) {
2630
+ const fullPath = path7.join(memoriesDir, entry);
2631
+ if (!entry.endsWith(".md")) continue;
2632
+ if (fs5.statSync(fullPath).isDirectory()) continue;
2633
+ try {
2634
+ const content = fs5.readFileSync(fullPath, "utf-8");
2635
+ const fmMatch = content.match(/^---\n([\s\S]*?)\n---/);
2636
+ if (!fmMatch) continue;
2637
+ const parsed = import_yaml.default.parse(fmMatch[1]);
2638
+ const obsType = parsed.observation_type;
2639
+ if (!obsType) continue;
2640
+ const normalizedType = obsType.replace(/_/g, "-");
2641
+ const targetDir = path7.join(memoriesDir, normalizedType);
2642
+ fs5.mkdirSync(targetDir, { recursive: true });
2643
+ const targetPath = path7.join(targetDir, entry);
2644
+ fs5.renameSync(fullPath, targetPath);
2645
+ const now = /* @__PURE__ */ new Date();
2646
+ fs5.utimesSync(targetPath, now, now);
2647
+ moved++;
2648
+ } catch {
2649
+ }
2650
+ }
2651
+ return moved;
2652
+ }
2653
+ async function main() {
2654
+ const vaultArg = process.argv.find((_, i) => process.argv[i - 1] === "--vault");
2655
+ if (!vaultArg) {
2656
+ process.stderr.write("Usage: mycod --vault <path>\n");
2657
+ process.exit(1);
2658
+ }
2659
+ const vaultDir = path7.resolve(vaultArg);
2660
+ const config = loadConfig(vaultDir);
2661
+ const logger = new DaemonLogger(path7.join(vaultDir, "logs"), {
2662
+ level: config.daemon.log_level,
2663
+ maxSize: config.daemon.max_log_size
2664
+ });
2665
+ const server = new DaemonServer({ vaultDir, logger });
2666
+ const registry = new SessionRegistry({
2667
+ gracePeriod: config.daemon.grace_period,
2668
+ onEmpty: async () => {
2669
+ logger.info("daemon", "Grace period expired, shutting down");
2670
+ planWatcher.stopFileWatcher();
2671
+ await server.stop();
2672
+ vectorIndex?.close();
2673
+ index.close();
2674
+ logger.close();
2675
+ process.exit(0);
2676
+ }
2677
+ });
2678
+ const llmProvider = createLlmProvider(config.intelligence.llm);
2679
+ const embeddingProvider = createEmbeddingProvider(config.intelligence.embedding);
2680
+ let vectorIndex = null;
2681
+ try {
2682
+ const testEmbed = await embeddingProvider.embed("test");
2683
+ vectorIndex = new VectorIndex(path7.join(vaultDir, "vectors.db"), testEmbed.dimensions);
2684
+ logger.info("embeddings", "Vector index initialized", { dimensions: testEmbed.dimensions });
2685
+ } catch (error) {
2686
+ logger.warn("embeddings", "Vector index unavailable", { error: error.message });
2687
+ }
2688
+ const processor = new BufferProcessor(llmProvider, config.intelligence.llm.context_window);
2689
+ const vault = new VaultWriter(vaultDir);
2690
+ const index = new MycoIndex(path7.join(vaultDir, "index.db"));
2691
+ const lineageGraph = new LineageGraph(vaultDir);
2692
+ const transcriptMiner = new TranscriptMiner({
2693
+ additionalAdapters: config.capture.transcript_paths.map(
2694
+ (p) => createPerProjectAdapter(p, claudeCodeAdapter.parseTurns)
2695
+ )
2696
+ });
2697
+ let activeStopProcessing = null;
2698
+ const indexDeps = { index, vaultDir, vectorIndex, embeddingProvider, logger };
2699
+ const bufferDir = path7.join(vaultDir, "buffer");
2700
+ const sessionBuffers = /* @__PURE__ */ new Map();
2701
+ const sessionFilePaths = /* @__PURE__ */ new Map();
2702
+ if (fs5.existsSync(bufferDir)) {
2703
+ const cutoff = Date.now() - STALE_BUFFER_MAX_AGE_MS;
2704
+ for (const file of fs5.readdirSync(bufferDir)) {
2705
+ const filePath = path7.join(bufferDir, file);
2706
+ const stat4 = fs5.statSync(filePath);
2707
+ if (stat4.mtimeMs < cutoff) {
2708
+ fs5.unlinkSync(filePath);
2709
+ logger.debug("daemon", "Cleaned stale buffer", { file });
2710
+ }
2711
+ }
2712
+ }
2713
+ const migrated = migrateMemoryFiles(vaultDir);
2714
+ if (migrated > 0) {
2715
+ logger.info("daemon", "Migrated memory files to type subdirectories", { count: migrated });
2716
+ initFts(index);
2717
+ rebuildIndex(index, vaultDir);
2718
+ }
2719
+ const RegisterBody = external_exports.object({
2720
+ session_id: external_exports.string(),
2721
+ branch: external_exports.string().optional(),
2722
+ started_at: external_exports.string().optional()
2723
+ });
2724
+ const UnregisterBody = external_exports.object({ session_id: external_exports.string() });
2725
+ const EventBody = external_exports.object({ type: external_exports.string(), session_id: external_exports.string() }).passthrough();
2726
+ const StopBody = external_exports.object({
2727
+ session_id: external_exports.string(),
2728
+ user: external_exports.string().optional(),
2729
+ transcript_path: external_exports.string().optional(),
2730
+ last_assistant_message: external_exports.string().optional()
2731
+ });
2732
+ const ContextBody = external_exports.object({
2733
+ session_id: external_exports.string().optional(),
2734
+ branch: external_exports.string().optional(),
2735
+ files: external_exports.array(external_exports.string()).optional()
2736
+ });
2737
+ const planWatcher = new PlanWatcher({
2738
+ projectRoot: process.cwd(),
2739
+ watchPaths: config.capture.artifact_watch,
2740
+ onPlan: (event) => {
2741
+ logger.info("watcher", "Plan detected", { source: event.source, file: event.filePath });
2742
+ if (event.filePath) {
2743
+ try {
2744
+ const content = fs5.readFileSync(event.filePath, "utf-8");
2745
+ const relativePath = path7.relative(vaultDir, event.filePath);
2746
+ const title = content.match(/^#\s+(.+)$/m)?.[1] ?? path7.basename(event.filePath);
2747
+ const planId = `plan-${path7.basename(event.filePath, ".md")}`;
2748
+ indexAndEmbed(
2749
+ relativePath,
2750
+ planId,
2751
+ `${title}
2752
+ ${content}`,
2753
+ { type: "plan" },
2754
+ indexDeps
2755
+ );
2756
+ if (event.sessionId) {
2757
+ lineageGraph.registerArtifactForSession(event.sessionId, planId);
2758
+ logger.debug("lineage", "Plan registered for session", { planId, session: event.sessionId });
2759
+ }
2760
+ logger.info("watcher", "Plan indexed", { path: relativePath });
2761
+ } catch (err) {
2762
+ logger.debug("watcher", "Plan index failed", { error: err.message });
2763
+ }
2764
+ }
2765
+ }
2766
+ });
2767
+ planWatcher.startFileWatcher();
2768
+ const batchManager = new BatchManager(async (closedBatch) => {
2769
+ if (closedBatch.length === 0) return;
2770
+ const sessionId = closedBatch[0].session_id;
2771
+ const asRecords = closedBatch;
2772
+ const result = await processor.process(asRecords, sessionId);
2773
+ if (!result.degraded) {
2774
+ writeObservations(result.observations, sessionId, { vault, ...indexDeps });
2775
+ }
2776
+ logger.debug("processor", "Batch processed", {
2777
+ session_id: sessionId,
2778
+ events: closedBatch.length,
2779
+ observations: result.observations.length,
2780
+ degraded: result.degraded
2781
+ });
2782
+ });
2783
+ server.registerRoute("POST", "/sessions/register", async (body) => {
2784
+ const { session_id, branch, started_at } = RegisterBody.parse(body);
2785
+ const resolvedStartedAt = started_at ?? (/* @__PURE__ */ new Date()).toISOString();
2786
+ registry.register(session_id, { started_at: resolvedStartedAt, branch });
2787
+ server.updateDaemonJsonSessions(registry.sessions);
2788
+ try {
2789
+ const recentSessions = index.query({ type: "session", limit: LINEAGE_RECENT_SESSIONS_LIMIT }).map((n) => {
2790
+ const fm = n.frontmatter;
2791
+ return { id: bareSessionId(n.id), ended: fm.ended, branch: fm.branch };
2792
+ });
2793
+ const activeSessions = registry.sessions.filter((s) => s !== session_id).map((s) => registry.getSession(s)).filter((s) => s !== void 0);
2794
+ const link = lineageGraph.detectHeuristicParent(session_id, {
2795
+ started_at: resolvedStartedAt,
2796
+ branch
2797
+ }, recentSessions, activeSessions);
2798
+ if (link) {
2799
+ logger.info("lineage", "Heuristic parent detected", { child: session_id, parent: link.parent, signal: link.signal });
2800
+ }
2801
+ } catch (err) {
2802
+ logger.debug("lineage", "Heuristic detection failed", { error: err.message });
2803
+ }
2804
+ logger.info("lifecycle", "Session registered", { session_id, branch });
2805
+ return { ok: true, sessions: registry.sessions };
2806
+ });
2807
+ server.registerRoute("POST", "/sessions/unregister", async (body) => {
2808
+ const { session_id } = UnregisterBody.parse(body);
2809
+ registry.unregister(session_id);
2810
+ try {
2811
+ const cutoff = Date.now() - STALE_BUFFER_MAX_AGE_MS;
2812
+ for (const file of fs5.readdirSync(bufferDir)) {
2813
+ if (!file.endsWith(".jsonl")) continue;
2814
+ const bufferSessionId = file.replace(".jsonl", "");
2815
+ if (bufferSessionId === session_id) continue;
2816
+ const filePath = path7.join(bufferDir, file);
2817
+ const stat4 = fs5.statSync(filePath);
2818
+ if (stat4.mtimeMs < cutoff) {
2819
+ fs5.unlinkSync(filePath);
2820
+ logger.debug("daemon", "Cleaned stale buffer", { file });
2821
+ }
2822
+ }
2823
+ } catch {
2824
+ }
2825
+ sessionBuffers.delete(session_id);
2826
+ sessionFilePaths.delete(session_id);
2827
+ server.updateDaemonJsonSessions(registry.sessions);
2828
+ logger.info("lifecycle", "Session unregistered", { session_id });
2829
+ return { ok: true, sessions: registry.sessions };
2830
+ });
2831
+ server.registerRoute("POST", "/events", async (body) => {
2832
+ const validated = EventBody.parse(body);
2833
+ const event = { ...validated, timestamp: validated.timestamp ?? (/* @__PURE__ */ new Date()).toISOString() };
2834
+ logger.debug("hooks", "Event received", { type: event.type, session_id: event.session_id });
2835
+ if (!registry.getSession(event.session_id)) {
2836
+ registry.register(event.session_id, { started_at: event.timestamp });
2837
+ logger.debug("lifecycle", "Auto-registered session from event", { session_id: event.session_id });
2838
+ }
2839
+ if (!sessionBuffers.has(event.session_id)) {
2840
+ sessionBuffers.set(event.session_id, new EventBuffer(bufferDir, event.session_id));
2841
+ }
2842
+ sessionBuffers.get(event.session_id).append(event);
2843
+ batchManager.addEvent(event);
2844
+ if (validated.type === "tool_use") {
2845
+ const v = validated;
2846
+ planWatcher.checkToolEvent({ tool_name: String(v.tool_name ?? ""), tool_input: v.tool_input, session_id: validated.session_id });
2847
+ const toolName = String(v.tool_name ?? "");
2848
+ if (toolName === "Write" || toolName === "Edit") {
2849
+ const filePath = v.tool_input?.file_path;
2850
+ if (filePath) {
2851
+ if (!sessionFilePaths.has(event.session_id)) {
2852
+ sessionFilePaths.set(event.session_id, /* @__PURE__ */ new Set());
2853
+ }
2854
+ sessionFilePaths.get(event.session_id).add(filePath);
2855
+ }
2856
+ }
2857
+ }
2858
+ return { ok: true };
2859
+ });
2860
+ server.registerRoute("POST", "/events/stop", async (body) => {
2861
+ const { session_id: sessionId, user, transcript_path: hookTranscriptPath, last_assistant_message: lastAssistantMessage } = StopBody.parse(body);
2862
+ if (!registry.getSession(sessionId)) {
2863
+ registry.register(sessionId, { started_at: (/* @__PURE__ */ new Date()).toISOString() });
2864
+ logger.debug("lifecycle", "Auto-registered session from stop event", { session_id: sessionId });
2865
+ }
2866
+ const sessionMeta = registry.getSession(sessionId);
2867
+ logger.info("hooks", "Stop received", { session_id: sessionId, has_transcript_path: !!hookTranscriptPath, has_response: !!lastAssistantMessage });
2868
+ const run = () => processStopEvent(sessionId, user, sessionMeta, hookTranscriptPath, lastAssistantMessage).catch((err) => {
2869
+ logger.error("processor", "Stop processing failed", { session_id: sessionId, error: err.message });
2870
+ });
2871
+ const prev = activeStopProcessing ?? Promise.resolve();
2872
+ activeStopProcessing = prev.then(run).finally(() => {
2873
+ activeStopProcessing = null;
2874
+ });
2875
+ return { ok: true };
2876
+ });
2877
+ async function processStopEvent(sessionId, user, sessionMeta, hookTranscriptPath, lastAssistantMessage) {
2878
+ const lastBatch = batchManager.finalize(sessionId);
2879
+ const transcriptResult = transcriptMiner.getAllTurnsWithSource(sessionId, hookTranscriptPath);
2880
+ let allTurns = transcriptResult.turns;
2881
+ let turnSource = transcriptResult.source;
2882
+ const bufferEvents = sessionBuffers.get(sessionId)?.readAll() ?? [];
2883
+ if (allTurns.length === 0) {
2884
+ allTurns = extractTurnsFromBuffer(bufferEvents);
2885
+ turnSource = "buffer";
2886
+ } else if (bufferEvents.length > 0) {
2887
+ const lastTranscriptTs = allTurns[allTurns.length - 1].timestamp;
2888
+ if (lastTranscriptTs) {
2889
+ const newerEvents = bufferEvents.filter(
2890
+ (e) => String(e.timestamp ?? "") > lastTranscriptTs
2891
+ );
2892
+ if (newerEvents.length > 0) {
2893
+ const bufferTurns = extractTurnsFromBuffer(newerEvents);
2894
+ allTurns = [...allTurns, ...bufferTurns];
2895
+ turnSource = `${transcriptResult.source}+buffer`;
2896
+ logger.info("processor", "Appended buffer turns missing from transcript", {
2897
+ session_id: sessionId,
2898
+ transcriptTurns: transcriptResult.turns.length,
2899
+ bufferTurns: bufferTurns.length
2900
+ });
2901
+ }
2902
+ }
2903
+ }
2904
+ if (lastAssistantMessage && allTurns.length > 0) {
2905
+ const lastTurn = allTurns[allTurns.length - 1];
2906
+ if (!lastTurn.aiResponse) {
2907
+ lastTurn.aiResponse = lastAssistantMessage;
2908
+ }
2909
+ }
2910
+ const ended = (/* @__PURE__ */ new Date()).toISOString();
2911
+ let started = allTurns.length > 0 && allTurns[0].timestamp ? allTurns[0].timestamp : ended;
2912
+ const sessionsDir = path7.join(vaultDir, "sessions");
2913
+ const sessionFileName = `${sessionNoteId(sessionId)}.md`;
2914
+ let existingContent;
2915
+ const duplicatePaths = [];
2916
+ try {
2917
+ for (const dateDir of fs5.readdirSync(sessionsDir)) {
2918
+ const candidate = path7.join(sessionsDir, dateDir, sessionFileName);
2919
+ try {
2920
+ const content = fs5.readFileSync(candidate, "utf-8");
2921
+ if (!existingContent || content.length > existingContent.length) {
2922
+ existingContent = content;
2923
+ }
2924
+ duplicatePaths.push(candidate);
2925
+ } catch {
2926
+ }
2927
+ }
2928
+ } catch {
2929
+ }
2930
+ let existingTurnCount = 0;
2931
+ if (existingContent) {
2932
+ const startedMatch = existingContent.match(/^started:\s*"?(.+?)"?\s*$/m);
2933
+ if (startedMatch) started = startedMatch[1];
2934
+ const turnMatches = existingContent.match(/^### Turn \d+/gm);
2935
+ existingTurnCount = turnMatches?.length ?? 0;
2936
+ }
2937
+ const writtenFiles = sessionFilePaths.get(sessionId) ?? /* @__PURE__ */ new Set();
2938
+ const artifactCandidates = collectArtifactCandidates(
2939
+ writtenFiles,
2940
+ { artifact_extensions: config.capture.artifact_extensions },
2941
+ process.cwd()
2942
+ );
2943
+ if (allTurns.length === 0 && existingTurnCount > 0) {
2944
+ logger.warn("processor", "Transcript unreadable, skipping rewrite to preserve existing data", { session_id: sessionId, existingTurns: existingTurnCount });
2945
+ return;
2946
+ }
2947
+ if (allTurns.length > 0 && allTurns.length === existingTurnCount && lastBatch.length === 0 && artifactCandidates.length === 0) {
2948
+ logger.debug("processor", "No new turns, skipping session rewrite", { session_id: sessionId, turns: allTurns.length });
2949
+ return;
2950
+ }
2951
+ const conversationText = allTurns.map((t, i) => {
2952
+ const parts = [`### Turn ${i + 1}`];
2953
+ if (t.prompt) parts.push(`Prompt: ${t.prompt}`);
2954
+ if (t.toolCount > 0) parts.push(`Tools: ${t.toolCount} calls`);
2955
+ if (t.aiResponse) parts.push(`Response: ${t.aiResponse}`);
2956
+ return parts.join("\n");
2957
+ }).join("\n\n");
2958
+ const conversationSection = `## Conversation
2959
+
2960
+ ${conversationText}`;
2961
+ const observationPromise = lastBatch.length > 0 ? processor.process(lastBatch, sessionId).catch((err) => {
2962
+ logger.warn("processor", "Observation extraction failed", { session_id: sessionId, error: err.message });
2963
+ return null;
2964
+ }) : Promise.resolve(null);
2965
+ const artifactPromise = artifactCandidates.length > 0 ? processor.classifyArtifacts(artifactCandidates, sessionId).then((classified) => captureArtifacts(artifactCandidates, classified, sessionId, { vault, ...indexDeps }, lineageGraph)).catch((err) => {
2966
+ logger.warn("processor", "Artifact capture failed", { session_id: sessionId, error: err.message });
2967
+ }) : Promise.resolve();
2968
+ const summaryPromise = processor.summarizeSession(conversationSection, sessionId, user).catch((err) => {
2969
+ logger.warn("processor", "Session summarization failed", { session_id: sessionId, error: err.message });
2970
+ return null;
2971
+ });
2972
+ const [observationResult, , summaryResult] = await Promise.all([observationPromise, artifactPromise, summaryPromise]);
2973
+ if (observationResult && !observationResult.degraded) {
2974
+ writeObservations(observationResult.observations, sessionId, { vault, ...indexDeps });
2975
+ }
2976
+ const date = started.slice(0, 10);
2977
+ const relativePath = sessionRelativePath(sessionId, date);
2978
+ const targetFullPath = path7.join(vaultDir, relativePath);
2979
+ for (const dup of duplicatePaths) {
2980
+ if (dup !== targetFullPath) {
2981
+ try {
2982
+ fs5.unlinkSync(dup);
2983
+ logger.debug("lifecycle", "Removed duplicate session file", { path: dup });
2984
+ } catch {
2985
+ }
2986
+ }
2987
+ }
2988
+ const attachmentsDir = path7.join(vaultDir, "attachments");
2989
+ const hasImages = allTurns.some((t) => t.images?.length);
2990
+ if (hasImages) {
2991
+ fs5.mkdirSync(attachmentsDir, { recursive: true });
2992
+ }
2993
+ const turnImageNames = /* @__PURE__ */ new Map();
2994
+ for (let i = 0; i < allTurns.length; i++) {
2995
+ const turn = allTurns[i];
2996
+ if (!turn.images?.length) continue;
2997
+ const names = [];
2998
+ for (let j = 0; j < turn.images.length; j++) {
2999
+ const img = turn.images[j];
3000
+ const ext = extensionForMimeType(img.mediaType);
3001
+ const filename = `${bareSessionId(sessionId)}-t${i + 1}-${j + 1}.${ext}`;
3002
+ const filePath = path7.join(attachmentsDir, filename);
3003
+ if (!fs5.existsSync(filePath)) {
3004
+ fs5.writeFileSync(filePath, Buffer.from(img.data, "base64"));
3005
+ logger.debug("processor", "Image saved", { filename, turn: i + 1 });
3006
+ }
3007
+ names.push(filename);
3008
+ }
3009
+ turnImageNames.set(i, names);
3010
+ }
3011
+ let title = `Session ${sessionId}`;
3012
+ let narrative = "";
3013
+ if (summaryResult) {
3014
+ title = summaryResult.title;
3015
+ narrative = summaryResult.summary;
3016
+ }
3017
+ const relatedMemories = index.query({ type: "memory", limit: RELATED_MEMORIES_LIMIT }).filter((n) => {
3018
+ const fm = n.frontmatter;
3019
+ return fm.session === sessionNoteId(sessionId) || fm.session === sessionId;
3020
+ }).map((n) => ({ id: n.id, title: n.title }));
3021
+ const summary = formatSessionBody({
3022
+ title,
3023
+ narrative,
3024
+ sessionId,
3025
+ user,
3026
+ started,
3027
+ ended,
3028
+ branch: sessionMeta?.branch,
3029
+ relatedMemories,
3030
+ turns: allTurns.map((t, i) => ({
3031
+ prompt: t.prompt,
3032
+ toolCount: t.toolCount,
3033
+ aiResponse: t.aiResponse,
3034
+ images: turnImageNames.get(i)
3035
+ }))
3036
+ });
3037
+ const parentId = lineageGraph.getParent(sessionId);
3038
+ const parentLink = parentId ? lineageGraph.getLinks().find((l) => l.child === sessionId) : void 0;
3039
+ vault.writeSession({
3040
+ id: sessionId,
3041
+ user,
3042
+ started,
3043
+ ended,
3044
+ branch: sessionMeta?.branch,
3045
+ parent: parentId ? sessionWikilink(parentId) : void 0,
3046
+ parent_reason: parentLink?.signal,
3047
+ tools_used: allTurns.reduce((sum, t) => sum + t.toolCount, 0),
3048
+ summary
3049
+ });
3050
+ indexAndEmbed(
3051
+ relativePath,
3052
+ sessionNoteId(sessionId),
3053
+ narrative,
3054
+ { type: "session", session_id: sessionId },
3055
+ indexDeps
3056
+ );
3057
+ logger.debug("processor", "Session turns", { source: turnSource, total: allTurns.length });
3058
+ await artifactPromise;
3059
+ if (!parentId && vectorIndex && narrative) {
3060
+ generateEmbedding(embeddingProvider, narrative).then(async (emb) => {
3061
+ const candidates = vectorIndex.search(emb.embedding, { limit: LINEAGE_SIMILARITY_CANDIDATES }).filter((r) => r.metadata.type === "session" && r.id !== sessionNoteId(sessionId));
3062
+ if (candidates.length === 0) return;
3063
+ const candidateNotes = index.queryByIds(candidates.map((c) => c.id));
3064
+ const noteMap = new Map(candidateNotes.map((n) => [n.id, n]));
3065
+ const scores = await Promise.all(candidates.map(async (candidate) => {
3066
+ const note = noteMap.get(candidate.id);
3067
+ if (!note) return { id: candidate.id, score: 0 };
3068
+ try {
3069
+ const prompt = buildSimilarityPrompt(narrative, note.content.slice(0, CANDIDATE_CONTENT_PREVIEW));
3070
+ const response = await llmProvider.summarize(prompt, { maxTokens: LINEAGE_SIMILARITY_MAX_TOKENS });
3071
+ const score = extractNumber(response.text);
3072
+ return { id: candidate.id, score: isNaN(score) ? 0 : score };
3073
+ } catch {
3074
+ return { id: candidate.id, score: 0 };
3075
+ }
3076
+ }));
3077
+ const best = scores.reduce((a, b) => b.score > a.score ? b : a);
3078
+ if (best.score >= LINEAGE_SIMILARITY_THRESHOLD) {
3079
+ const bestParentId = bareSessionId(best.id);
3080
+ const confidence = best.score >= LINEAGE_SIMILARITY_HIGH_CONFIDENCE ? "high" : "medium";
3081
+ lineageGraph.addLink({
3082
+ parent: bestParentId,
3083
+ child: sessionId,
3084
+ signal: "semantic_similarity",
3085
+ confidence
3086
+ });
3087
+ try {
3088
+ vault.updateNoteFrontmatter(relativePath, {
3089
+ parent: sessionWikilink(bestParentId),
3090
+ parent_reason: "semantic_similarity"
3091
+ });
3092
+ indexNote(index, vaultDir, relativePath);
3093
+ } catch {
3094
+ }
3095
+ logger.info("lineage", "LLM similarity parent detected", {
3096
+ child: sessionId,
3097
+ parent: bestParentId,
3098
+ score: best.score
3099
+ });
3100
+ }
3101
+ }).catch((err) => logger.debug("lineage", "Similarity detection failed", { error: err.message }));
3102
+ }
3103
+ logger.info("processor", "Session note written", { session_id: sessionId, path: relativePath });
3104
+ }
3105
+ server.registerRoute("POST", "/context", async (body) => {
3106
+ const { session_id, branch } = ContextBody.parse(body);
3107
+ logger.debug("hooks", "Session context query", { session_id });
3108
+ try {
3109
+ const parts = [];
3110
+ const plans = index.query({ type: "plan" });
3111
+ const activePlans = plans.filter((p) => {
3112
+ const status = p.frontmatter.status;
3113
+ return status === "active" || status === "in_progress";
3114
+ });
3115
+ if (activePlans.length > 0) {
3116
+ const planLines = activePlans.slice(0, SESSION_CONTEXT_MAX_PLANS).map((p) => {
3117
+ const status = p.frontmatter.status;
3118
+ return `- **${p.title}** (${status}) \`[${p.id}]\``;
3119
+ });
3120
+ parts.push(`### Active Plans
3121
+ ${planLines.join("\n")}`);
3122
+ }
3123
+ if (session_id) {
3124
+ const parentId = lineageGraph.getParent(session_id);
3125
+ if (parentId) {
3126
+ const parentNotes = index.queryByIds([sessionNoteId(parentId)]);
3127
+ if (parentNotes.length > 0) {
3128
+ const parent = parentNotes[0];
3129
+ parts.push(`### Previous Session
3130
+ - **${parent.title}**: ${parent.content.slice(0, CONTEXT_SESSION_PREVIEW_CHARS)} \`[${parent.id}]\``);
3131
+ }
3132
+ }
3133
+ }
3134
+ if (branch) {
3135
+ parts.push(`Branch:: \`${branch}\``);
3136
+ }
3137
+ if (parts.length > 0) {
3138
+ return { text: parts.join("\n\n") };
3139
+ }
3140
+ return { text: "" };
3141
+ } catch (error) {
3142
+ logger.error("daemon", "Session context failed", { error: error.message });
3143
+ return { text: "" };
3144
+ }
3145
+ });
3146
+ const PromptContextBody = external_exports.object({
3147
+ prompt: external_exports.string(),
3148
+ session_id: external_exports.string().optional()
3149
+ });
3150
+ server.registerRoute("POST", "/context/prompt", async (body) => {
3151
+ const { prompt, session_id } = PromptContextBody.parse(body);
3152
+ if (!prompt || prompt.length < PROMPT_CONTEXT_MIN_LENGTH || !vectorIndex) {
3153
+ return { text: "" };
3154
+ }
3155
+ try {
3156
+ const emb = await generateEmbedding(embeddingProvider, prompt.slice(0, EMBEDDING_INPUT_LIMIT));
3157
+ const results = vectorIndex.search(emb.embedding, {
3158
+ limit: PROMPT_CONTEXT_MAX_MEMORIES,
3159
+ type: "memory",
3160
+ relativeThreshold: PROMPT_CONTEXT_MIN_SIMILARITY
3161
+ });
3162
+ if (results.length === 0) return { text: "" };
3163
+ const noteMap = new Map(
3164
+ index.queryByIds(results.map((r) => r.id)).map((n) => [n.id, n])
3165
+ );
3166
+ const lines = [];
3167
+ for (const r of results) {
3168
+ const note = noteMap.get(r.id);
3169
+ if (!note) continue;
3170
+ const fm = note.frontmatter;
3171
+ if (fm.status === "superseded" || fm.status === "archived") continue;
3172
+ const obsType = fm.observation_type ?? "note";
3173
+ lines.push(`- [${obsType}] ${note.title}: ${note.content.slice(0, CONTENT_SNIPPET_CHARS)} \`[${note.id}]\``);
3174
+ }
3175
+ if (lines.length === 0) return { text: "" };
3176
+ const injected = `**Relevant memories for this task:**
3177
+ ${lines.join("\n")}`;
3178
+ logger.debug("context", "Prompt context injected", {
3179
+ session_id,
3180
+ memories: lines.length,
3181
+ prompt_preview: prompt.slice(0, 50)
3182
+ });
3183
+ return { text: injected };
3184
+ } catch (err) {
3185
+ logger.debug("context", "Prompt context failed", { error: err.message });
3186
+ return { text: "" };
3187
+ }
3188
+ });
3189
+ await server.start();
3190
+ logger.info("daemon", "Daemon ready", { vault: vaultDir, port: server.port });
3191
+ const shutdown = async (signal) => {
3192
+ logger.info("daemon", `${signal} received`);
3193
+ if (activeStopProcessing) {
3194
+ logger.info("daemon", "Waiting for active stop processing to complete...");
3195
+ await activeStopProcessing;
3196
+ }
3197
+ planWatcher.stopFileWatcher();
3198
+ registry.destroy();
3199
+ await server.stop();
3200
+ vectorIndex?.close();
3201
+ index.close();
3202
+ logger.close();
3203
+ process.exit(0);
3204
+ };
3205
+ process.on("SIGTERM", () => shutdown("SIGTERM"));
3206
+ process.on("SIGINT", () => shutdown("SIGINT"));
3207
+ }
3208
+ var __filename = fileURLToPath2(import.meta.url);
3209
+ if (process.argv[1] === __filename) {
3210
+ main().catch((err) => {
3211
+ process.stderr.write(`[mycod] Fatal: ${err.message}
3212
+ `);
3213
+ process.exit(1);
3214
+ });
3215
+ }
3216
+ export {
3217
+ migrateMemoryFiles
3218
+ };
3219
+ /*! Bundled license information:
3220
+
3221
+ chokidar/index.js:
3222
+ (*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) *)
3223
+ */
3224
+ //# sourceMappingURL=main-5W4ADOBG.js.map