@clagentic/console 1.9.0 → 1.10.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/LICENSE +117 -12
  2. package/LICENSES/MIT-legacy.txt +22 -0
  3. package/LICENSES/README.md +15 -0
  4. package/README.md +9 -3
  5. package/bin/cli.js +40 -5
  6. package/deploy/clagentic-console.service +10 -6
  7. package/lib/agent-dispatch-tool.js +20 -0
  8. package/lib/audit.js +3 -2
  9. package/lib/build-changes.js +79 -0
  10. package/lib/build-identity.js +192 -0
  11. package/lib/build-monitor.js +125 -0
  12. package/lib/build-sha.json +2 -2
  13. package/lib/build-update-check.js +138 -0
  14. package/lib/build-update-offer.js +141 -0
  15. package/lib/build-update-restart.js +142 -0
  16. package/lib/build-update-standing.js +184 -0
  17. package/lib/cli/daemon-launch.js +25 -22
  18. package/lib/cli/ipc-subcommands.js +6 -0
  19. package/lib/cli/session-subcommands.js +122 -0
  20. package/lib/cli-sessions.js +8 -1
  21. package/lib/codex-defaults.js +8 -2
  22. package/lib/config.js +32 -20
  23. package/lib/daemon-env-settings.js +84 -0
  24. package/lib/daemon-projects.js +57 -0
  25. package/lib/daemon.js +288 -205
  26. package/lib/drain.js +74 -17
  27. package/lib/env-compat.js +61 -0
  28. package/lib/extension-commands.js +45 -0
  29. package/lib/history-visibility.js +4 -0
  30. package/lib/idle-adapter-reaper.js +71 -0
  31. package/lib/link-trap.js +78 -0
  32. package/lib/mcp-local.js +8 -6
  33. package/lib/mcp-tool-input.js +116 -0
  34. package/lib/mcp-tool-schema.js +179 -0
  35. package/lib/memory-limits.js +61 -5
  36. package/lib/memory-setting-validate.js +65 -0
  37. package/lib/memory-shed.js +13 -32
  38. package/lib/new-work-gate.js +47 -0
  39. package/lib/os-users.js +5 -3
  40. package/lib/project-access.js +139 -0
  41. package/lib/project-connection.js +37 -22
  42. package/lib/project-external-trigger.js +166 -105
  43. package/lib/project-file-scope.js +172 -0
  44. package/lib/project-file-watch.js +86 -61
  45. package/lib/project-filesystem.js +144 -128
  46. package/lib/project-git.js +119 -0
  47. package/lib/project-http.js +44 -28
  48. package/lib/project-loop.js +17 -9
  49. package/lib/project-mcp.js +53 -72
  50. package/lib/project-message-gate.js +70 -0
  51. package/lib/project-notifications.js +31 -4
  52. package/lib/project-prompt-responses.js +217 -0
  53. package/lib/project-sessions.js +192 -355
  54. package/lib/project-user-message.js +27 -15
  55. package/lib/project.js +212 -147
  56. package/lib/projects-dir.js +90 -0
  57. package/lib/prompt-access.js +22 -0
  58. package/lib/prompt-kinds/answer-limits.js +15 -0
  59. package/lib/prompt-kinds/ask-user.js +58 -0
  60. package/lib/prompt-kinds/codecs.js +97 -0
  61. package/lib/prompt-kinds/elicitation.js +77 -0
  62. package/lib/prompt-kinds/extension.js +39 -0
  63. package/lib/prompt-kinds/index.js +68 -0
  64. package/lib/prompt-kinds/permission.js +114 -0
  65. package/lib/prompt-kinds/plan.js +79 -0
  66. package/lib/prompt-kinds/tool-call.js +38 -0
  67. package/lib/prompt-registry.js +680 -0
  68. package/lib/public/app.js +1 -1
  69. package/lib/public/css/diagnostics.css +80 -0
  70. package/lib/public/css/input.css +17 -3
  71. package/lib/public/css/menus.css +10 -0
  72. package/lib/public/css/messages.css +5 -0
  73. package/lib/public/css/rewind.css +22 -0
  74. package/lib/public/index.html +4 -12
  75. package/lib/public/modules/agent-dispatch-tool.js +9 -0
  76. package/lib/public/modules/app-connection.js +7 -0
  77. package/lib/public/modules/app-cursors.js +7 -5
  78. package/lib/public/modules/app-messages.js +52 -47
  79. package/lib/public/modules/app-notifications.js +47 -71
  80. package/lib/public/modules/app-panels.js +61 -59
  81. package/lib/public/modules/app-projects.js +2 -1
  82. package/lib/public/modules/app-rendering.js +4 -10
  83. package/lib/public/modules/build-update-detail.js +54 -0
  84. package/lib/public/modules/diagnostics.js +170 -20
  85. package/lib/public/modules/filebrowser.js +18 -16
  86. package/lib/public/modules/history-visibility.js +4 -0
  87. package/lib/public/modules/mention.js +3 -2
  88. package/lib/public/modules/mode-options.js +38 -0
  89. package/lib/public/modules/project-settings.js +19 -8
  90. package/lib/public/modules/prompt-banner.js +78 -0
  91. package/lib/public/modules/prompt-card.js +201 -0
  92. package/lib/public/modules/prompt-kinds/ask-user-codec.js +131 -0
  93. package/lib/public/modules/prompt-kinds/ask-user.js +213 -0
  94. package/lib/public/modules/prompt-kinds/elicitation-codec.js +343 -0
  95. package/lib/public/modules/prompt-kinds/elicitation.js +176 -0
  96. package/lib/public/modules/prompt-kinds/index.js +18 -0
  97. package/lib/public/modules/prompt-kinds/own-key.js +29 -0
  98. package/lib/public/modules/prompt-kinds/package.json +3 -0
  99. package/lib/public/modules/prompt-kinds/permission-choices.js +31 -0
  100. package/lib/public/modules/prompt-kinds/permission.js +209 -0
  101. package/lib/public/modules/prompt-kinds/plan.js +102 -0
  102. package/lib/public/modules/prompt-state.js +142 -0
  103. package/lib/public/modules/prompts.js +134 -0
  104. package/lib/public/modules/rewind.js +4 -1
  105. package/lib/public/modules/server-settings.js +2 -4
  106. package/lib/public/modules/sidebar-mobile.js +19 -9
  107. package/lib/public/modules/sidebar-sessions.js +21 -14
  108. package/lib/public/modules/sticky-notes.js +3 -2
  109. package/lib/public/modules/team-panel.js +5 -4
  110. package/lib/public/modules/tools.js +81 -906
  111. package/lib/public/modules/vendor-picker.js +53 -0
  112. package/lib/public/modules/vendor-registry.js +142 -0
  113. package/lib/public/sw.js +3 -1
  114. package/lib/rc-migrate.js +44 -0
  115. package/lib/runtime-env-keys.js +109 -0
  116. package/lib/runtime-env.js +236 -0
  117. package/lib/sdk-bridge.js +348 -284
  118. package/lib/sdk-message-processor.js +117 -146
  119. package/lib/sdk-skill-discovery.js +65 -60
  120. package/lib/server-hub-sessions.js +13 -10
  121. package/lib/server-palette.js +28 -19
  122. package/lib/server.js +156 -115
  123. package/lib/service-install.js +96 -0
  124. package/lib/session-capabilities.js +76 -0
  125. package/lib/session-id.js +33 -0
  126. package/lib/session-input-ipc.js +32 -0
  127. package/lib/session-input.js +379 -0
  128. package/lib/session-reclaim.js +284 -0
  129. package/lib/session-resume.js +172 -0
  130. package/lib/sessions.js +61 -30
  131. package/lib/supervisor.js +39 -0
  132. package/lib/turn-initiator.js +108 -0
  133. package/lib/users-permissions.js +9 -6
  134. package/lib/users.js +9 -0
  135. package/lib/utils.js +4 -2
  136. package/lib/vendor-auth.js +22 -0
  137. package/lib/vendor-registry-message.js +66 -0
  138. package/lib/ws-origin.js +99 -0
  139. package/lib/ws-schema.js +36 -23
  140. package/lib/yoke/adapters/claude-capabilities.js +38 -0
  141. package/lib/yoke/adapters/claude-vendor.js +34 -0
  142. package/lib/yoke/adapters/claude-worker.js +57 -87
  143. package/lib/yoke/adapters/claude.js +182 -88
  144. package/lib/yoke/adapters/codex-capabilities.js +40 -0
  145. package/lib/yoke/adapters/codex-vendor.js +46 -0
  146. package/lib/yoke/adapters/codex.js +462 -211
  147. package/lib/yoke/adapters/effort-control.js +34 -0
  148. package/lib/yoke/adapters/permission-relay.js +58 -0
  149. package/lib/yoke/adapters/worker-mcp-servers.js +81 -0
  150. package/lib/yoke/auth-checks.js +91 -0
  151. package/lib/yoke/capabilities.js +49 -0
  152. package/lib/yoke/codex-app-server.js +30 -56
  153. package/lib/yoke/codex-binary.js +254 -0
  154. package/lib/yoke/codex-command.js +122 -0
  155. package/lib/yoke/codex-protocol.js +96 -0
  156. package/lib/yoke/codex-version.js +135 -0
  157. package/lib/yoke/env-ownership.js +40 -0
  158. package/lib/yoke/index.js +84 -174
  159. package/lib/yoke/install-probe.js +136 -0
  160. package/lib/yoke/instructions.js +7 -11
  161. package/lib/yoke/interface.js +206 -4
  162. package/lib/yoke/package.json +1 -1
  163. package/lib/yoke/provider-credentials.js +96 -0
  164. package/lib/yoke/registry.js +377 -0
  165. package/package.json +12 -9
  166. package/scripts/check-sdk-lock-drift.js +160 -0
  167. package/scripts/check-test-count.js +76 -1
  168. package/scripts/postinstall.js +218 -136
  169. package/scripts/prompt-harness-history.js +202 -0
  170. package/scripts/verify-installed-build.js +59 -17
  171. package/scripts/verify-installed.js +67 -0
  172. package/lib/sdk-permission-ownership.js +0 -136
@@ -0,0 +1,172 @@
1
+ // File-scope policy shared by the WebSocket fs_* handlers, the HTTP file
2
+ // routes and the file/directory watchers: who may touch files, how a client
3
+ // supplied path is resolved, and how a file is read.
4
+ //
5
+ // Two rules hold everywhere this module is used:
6
+ // - the fileBrowser permission is checked before any file is touched;
7
+ // - in os-users mode a caller without a resolved OS identity is refused,
8
+ // never served by a privileged read as the daemon user.
9
+
10
+ var fs = require("fs");
11
+ var path = require("path");
12
+ var { authOf } = require("./project-access");
13
+
14
+ function isUsablePath(requested) {
15
+ return typeof requested === "string" && requested.indexOf("\0") === -1;
16
+ }
17
+
18
+ // Resolve `requested` inside `base`, following symlinks. Null for anything
19
+ // that is not a string, holds a null byte, or lands outside `base`.
20
+ function safePath(base, requested) {
21
+ if (!isUsablePath(requested)) return null;
22
+ var resolved = path.resolve(base, requested);
23
+ if (resolved !== base && !resolved.startsWith(base + path.sep)) return null;
24
+ try {
25
+ var real = fs.realpathSync(resolved);
26
+ if (real !== base && !real.startsWith(base + path.sep)) return null;
27
+ return real;
28
+ } catch (e) {
29
+ return null;
30
+ }
31
+ }
32
+
33
+ // Resolve an absolute path without requiring it to be within a project root.
34
+ // Only used in os-users mode, where the kernel's ACLs decide access.
35
+ function safeAbsPath(requested) {
36
+ if (!requested || !isUsablePath(requested)) return null;
37
+ var resolved = path.resolve(requested);
38
+ try {
39
+ return fs.realpathSync(resolved);
40
+ } catch (e) {
41
+ return null;
42
+ }
43
+ }
44
+
45
+ // Resolve a path inside the Claude data directory (~/.claude/), read-only
46
+ // fallback so plan files that live outside the project can be previewed.
47
+ function safeClaudePath(requested) {
48
+ if (!requested || !isUsablePath(requested)) return null;
49
+ var claudeDir = path.join(require("./config").REAL_HOME, ".claude");
50
+ var resolved = path.resolve(requested);
51
+ if (resolved !== claudeDir && !resolved.startsWith(claudeDir + path.sep)) return null;
52
+ try {
53
+ var real = fs.realpathSync(resolved);
54
+ if (real !== claudeDir && !real.startsWith(claudeDir + path.sep)) return null;
55
+ return real;
56
+ } catch (e) {
57
+ return null;
58
+ }
59
+ }
60
+
61
+ // Returns an error string when the caller may not use file features, else null.
62
+ // user the authenticated user, if any
63
+ // osUsers whether os-users mode is on
64
+ // osUserInfo the caller's resolved OS identity, or null
65
+ // usersModule module exposing getEffectivePermissions(user, osUsers)
66
+ function checkFileAccess(o) {
67
+ if (o.user) {
68
+ var perms = o.usersModule.getEffectivePermissions(o.user, o.osUsers);
69
+ if (!perms.fileBrowser) return "File browser access is not permitted";
70
+ }
71
+ if (o.osUsers && !o.osUserInfo) return "File access requires an OS user identity";
72
+ return null;
73
+ }
74
+
75
+ // Path a caller may read or write: inside the project, or (os-users) wherever
76
+ // the OS grants that identity access. Used by writes and by the HTTP file route.
77
+ function resolveProjectPath(cwd, requested, osUserInfo) {
78
+ var resolved = safePath(cwd, requested);
79
+ if (!resolved && osUserInfo) resolved = safeAbsPath(requested);
80
+ return resolved;
81
+ }
82
+
83
+ // Path a caller may read over the WebSocket: as above, plus read-only access
84
+ // inside ~/.claude (plan files).
85
+ function resolveReadPath(cwd, requested, osUserInfo) {
86
+ return resolveProjectPath(cwd, requested, osUserInfo) || safeClaudePath(requested);
87
+ }
88
+
89
+ // A project-relative path for git, or null when the request is not a path
90
+ // that stays inside the project. The file need not exist (history can name
91
+ // deleted files), so this is lexical rather than a realpath check.
92
+ function resolveGitPath(cwd, requested) {
93
+ if (!requested || !isUsablePath(requested)) return null;
94
+ var abs = path.resolve(cwd, requested);
95
+ if (abs === cwd || !abs.startsWith(cwd + path.sep)) return null;
96
+ return path.relative(cwd, abs).split(path.sep).join("/");
97
+ }
98
+
99
+ // Read a file as the caller. Result kind: "text", "binary" or "too_large".
100
+ // file, osUsers, osUserInfo, fsAsUser, maxSize, binaryExts
101
+ function readFileForUser(file, o) {
102
+ if (o.osUsers && !o.osUserInfo) throw new Error("File access requires an OS user identity");
103
+ var ext = path.extname(file).toLowerCase();
104
+ var size = o.osUserInfo
105
+ ? o.fsAsUser("stat", { file: file }, o.osUserInfo).size
106
+ : fs.statSync(file).size;
107
+ if (size > o.maxSize) return { kind: "too_large", size: size, ext: ext };
108
+ if (o.binaryExts.has(ext)) return { kind: "binary", size: size, ext: ext };
109
+ var content = o.osUserInfo
110
+ ? o.fsAsUser("read", { file: file, readContent: true }, o.osUserInfo).content
111
+ : fs.readFileSync(file, "utf8");
112
+ return { kind: "text", size: size, content: content, ext: ext };
113
+ }
114
+
115
+ // List a directory as the caller: [{ name, isDir }].
116
+ function listDirForUser(dir, o) {
117
+ if (o.osUsers && !o.osUserInfo) throw new Error("File access requires an OS user identity");
118
+ if (o.osUserInfo) {
119
+ return o.fsAsUser("list", { dir: dir }, o.osUserInfo).map(function (e) {
120
+ return { name: e.name, isDir: !!e.isDir };
121
+ });
122
+ }
123
+ return fs.readdirSync(dir, { withFileTypes: true }).map(function (d) {
124
+ return { name: d.name, isDir: d.isDirectory() };
125
+ });
126
+ }
127
+
128
+ // Binds the policy above to one project's settings so the WebSocket handlers
129
+ // and the watchers ask the same questions of the same code.
130
+ // osUsers, usersModule, fsAsUser, binaryExts, maxSize
131
+ // projectAccess the lib/project-access.js resolver: a connection with no
132
+ // principal (no user in multi-user mode) is refused here
133
+ // as well, however it got this far
134
+ // getOsUserInfoForWs(ws) -> the connection's OS identity, or null
135
+ function createFileAccess(deps) {
136
+ function readOptions(ws) {
137
+ return {
138
+ osUsers: deps.osUsers,
139
+ osUserInfo: deps.getOsUserInfoForWs(ws),
140
+ fsAsUser: deps.fsAsUser,
141
+ maxSize: deps.maxSize,
142
+ binaryExts: deps.binaryExts,
143
+ };
144
+ }
145
+ return {
146
+ // Error string when this connection may not use file features, else null.
147
+ authorize: function (ws) {
148
+ if (deps.projectAccess && !deps.projectAccess.principalFor(ws._clagenticUser, authOf(ws))) return "Authentication required";
149
+ return checkFileAccess({
150
+ user: ws._clagenticUser,
151
+ osUsers: deps.osUsers,
152
+ osUserInfo: deps.getOsUserInfoForWs(ws),
153
+ usersModule: deps.usersModule,
154
+ });
155
+ },
156
+ readFile: function (ws, file) { return readFileForUser(file, readOptions(ws)); },
157
+ listDir: function (ws, dir) { return listDirForUser(dir, readOptions(ws)); },
158
+ };
159
+ }
160
+
161
+ module.exports = {
162
+ createFileAccess: createFileAccess,
163
+ safePath: safePath,
164
+ safeAbsPath: safeAbsPath,
165
+ safeClaudePath: safeClaudePath,
166
+ checkFileAccess: checkFileAccess,
167
+ resolveReadPath: resolveReadPath,
168
+ resolveProjectPath: resolveProjectPath,
169
+ resolveGitPath: resolveGitPath,
170
+ readFileForUser: readFileForUser,
171
+ listDirForUser: listDirForUser,
172
+ };
@@ -1,111 +1,135 @@
1
1
  var fs = require("fs");
2
2
  var path = require("path");
3
3
 
4
+ // Most directory watches one connection may hold; each is an inotify handle.
5
+ var MAX_DIR_WATCHES_PER_CONNECTION = 64;
6
+
4
7
  /**
5
- * Attach file/directory watcher engine to a project context.
8
+ * Attach the file/directory watcher engine to a project context.
9
+ *
10
+ * Watches belong to the connection that asked for them. Every change is read
11
+ * as that connection's user (an OS user in os-users mode) and is sent to that
12
+ * connection only, so a watch can neither read a file the user may not read
13
+ * nor show its contents to anyone else on the project.
6
14
  *
7
15
  * ctx fields:
8
- * cwd, send, safePath, BINARY_EXTS, FS_MAX_SIZE, IGNORED_DIRS
16
+ * cwd, sendTo(ws, msg), safePath(base, requested)
17
+ * authorize(ws) -> error string | null (file-browser policy)
18
+ * readFile(ws, absFile) -> { kind: "text"|"binary"|"too_large", content, size }
19
+ * listDir(ws, absDir) -> [{ name, isDir }]
20
+ * IGNORED_DIRS
9
21
  */
10
22
  function attachFileWatch(ctx) {
11
23
  var cwd = ctx.cwd;
12
- var send = ctx.send;
24
+ var sendTo = ctx.sendTo;
13
25
  var safePath = ctx.safePath;
14
- var BINARY_EXTS = ctx.BINARY_EXTS;
15
- var FS_MAX_SIZE = ctx.FS_MAX_SIZE;
26
+ var authorize = ctx.authorize;
27
+ var readFile = ctx.readFile;
28
+ var listDir = ctx.listDir;
16
29
  var IGNORED_DIRS = ctx.IGNORED_DIRS;
17
30
 
18
- // --- File watcher ---
19
- var fileWatcher = null;
20
- var watchedPath = null;
21
- var watchDebounce = null;
31
+ // --- Single-file watch: at most one per connection ---
32
+ var fileWatches = new Map(); // ws -> { relPath, watcher, debounce }
22
33
 
23
- function startFileWatch(relPath) {
34
+ function startFileWatch(ws, relPath) {
35
+ if (authorize(ws)) return;
24
36
  var absPath = safePath(cwd, relPath);
25
37
  if (!absPath) return;
26
- if (watchedPath === relPath) return;
27
- stopFileWatch();
28
- watchedPath = relPath;
38
+ var existing = fileWatches.get(ws);
39
+ if (existing && existing.relPath === relPath) return;
40
+ stopFileWatch(ws);
41
+ var entry = { relPath: relPath, watcher: null, debounce: null };
29
42
  try {
30
- fileWatcher = fs.watch(absPath, function () {
31
- clearTimeout(watchDebounce);
32
- watchDebounce = setTimeout(function () {
43
+ entry.watcher = fs.watch(absPath, function () {
44
+ clearTimeout(entry.debounce);
45
+ entry.debounce = setTimeout(function () {
33
46
  try {
34
- var stat = fs.statSync(absPath);
35
- var ext = path.extname(absPath).toLowerCase();
36
- if (stat.size > FS_MAX_SIZE || BINARY_EXTS.has(ext)) return;
37
- var content = fs.readFileSync(absPath, "utf8");
38
- send({ type: "fs_file_changed", path: relPath, content: content, size: stat.size });
47
+ if (authorize(ws)) { stopFileWatch(ws); return; }
48
+ var result = readFile(ws, absPath);
49
+ if (result.kind !== "text") return;
50
+ sendTo(ws, { type: "fs_file_changed", path: relPath, content: result.content, size: result.size });
39
51
  } catch (e) {
40
- stopFileWatch();
52
+ stopFileWatch(ws);
41
53
  }
42
54
  }, 200);
43
55
  });
44
- fileWatcher.on("error", function () { stopFileWatch(); });
56
+ entry.watcher.on("error", function () { stopFileWatch(ws); });
57
+ fileWatches.set(ws, entry);
45
58
  } catch (e) {
46
- watchedPath = null;
59
+ // The file vanished or cannot be watched; the client simply gets no events.
47
60
  }
48
61
  }
49
62
 
50
- function stopFileWatch() {
51
- if (fileWatcher) {
52
- try { fileWatcher.close(); } catch (e) {}
53
- fileWatcher = null;
54
- }
55
- clearTimeout(watchDebounce);
56
- watchDebounce = null;
57
- watchedPath = null;
63
+ function stopFileWatch(ws) {
64
+ var entry = fileWatches.get(ws);
65
+ if (!entry) return;
66
+ clearTimeout(entry.debounce);
67
+ try { entry.watcher.close(); } catch (e) {}
68
+ fileWatches.delete(ws);
58
69
  }
59
70
 
60
- // --- Directory watcher ---
61
- var dirWatchers = {}; // relPath -> { watcher, debounce }
71
+ // --- Directory watches: any number per connection, up to the cap ---
72
+ var dirWatches = new Map(); // ws -> Map(relPath -> { watcher, debounce })
62
73
 
63
- function startDirWatch(relPath) {
64
- if (dirWatchers[relPath]) return;
74
+ function startDirWatch(ws, relPath) {
75
+ if (authorize(ws)) return;
76
+ var mine = dirWatches.get(ws);
77
+ if (mine && mine.has(relPath)) return;
78
+ if (mine && mine.size >= MAX_DIR_WATCHES_PER_CONNECTION) return;
65
79
  var absPath = safePath(cwd, relPath);
66
80
  if (!absPath) return;
81
+ var entry = { watcher: null, debounce: null };
67
82
  try {
68
- var debounce = null;
69
- var watcher = fs.watch(absPath, function () {
70
- clearTimeout(debounce);
71
- debounce = setTimeout(function () {
72
- // Re-read directory and broadcast to all clients
83
+ entry.watcher = fs.watch(absPath, function () {
84
+ clearTimeout(entry.debounce);
85
+ entry.debounce = setTimeout(function () {
73
86
  try {
74
- var items = fs.readdirSync(absPath, { withFileTypes: true });
87
+ if (authorize(ws)) { stopDirWatch(ws, relPath); return; }
75
88
  var entries = [];
89
+ var items = listDir(ws, absPath);
76
90
  for (var i = 0; i < items.length; i++) {
77
- if (items[i].isDirectory() && IGNORED_DIRS.has(items[i].name)) continue;
91
+ if (items[i].isDir && IGNORED_DIRS.has(items[i].name)) continue;
78
92
  entries.push({
79
93
  name: items[i].name,
80
- type: items[i].isDirectory() ? "dir" : "file",
94
+ type: items[i].isDir ? "dir" : "file",
81
95
  path: path.relative(cwd, path.join(absPath, items[i].name)).split(path.sep).join("/"),
82
96
  });
83
97
  }
84
- send({ type: "fs_dir_changed", path: relPath, entries: entries });
98
+ sendTo(ws, { type: "fs_dir_changed", path: relPath, entries: entries });
85
99
  } catch (e) {
86
- stopDirWatch(relPath);
100
+ stopDirWatch(ws, relPath);
87
101
  }
88
102
  }, 300);
89
103
  });
90
- watcher.on("error", function () { stopDirWatch(relPath); });
91
- dirWatchers[relPath] = { watcher: watcher, debounce: debounce };
92
- } catch (e) {}
104
+ entry.watcher.on("error", function () { stopDirWatch(ws, relPath); });
105
+ if (!mine) { mine = new Map(); dirWatches.set(ws, mine); }
106
+ mine.set(relPath, entry);
107
+ } catch (e) {
108
+ // Not watchable; listing still works, it just will not refresh itself.
109
+ }
93
110
  }
94
111
 
95
- function stopDirWatch(relPath) {
96
- var entry = dirWatchers[relPath];
97
- if (entry) {
98
- clearTimeout(entry.debounce);
99
- try { entry.watcher.close(); } catch (e) {}
100
- delete dirWatchers[relPath];
101
- }
112
+ function stopDirWatch(ws, relPath) {
113
+ var mine = dirWatches.get(ws);
114
+ var entry = mine && mine.get(relPath);
115
+ if (!entry) return;
116
+ clearTimeout(entry.debounce);
117
+ try { entry.watcher.close(); } catch (e) {}
118
+ mine.delete(relPath);
119
+ if (mine.size === 0) dirWatches.delete(ws);
102
120
  }
103
121
 
104
- function stopAllDirWatches() {
105
- var paths = Object.keys(dirWatchers);
106
- for (var i = 0; i < paths.length; i++) {
107
- stopDirWatch(paths[i]);
108
- }
122
+ // Every directory watch one connection holds; with stopFileWatch(ws) this
123
+ // releases everything the connection owns when it disconnects.
124
+ function stopAllDirWatches(ws) {
125
+ var mine = dirWatches.get(ws);
126
+ if (!mine) return;
127
+ Array.from(mine.keys()).forEach(function (relPath) { stopDirWatch(ws, relPath); });
128
+ }
129
+
130
+ function stopAll() {
131
+ Array.from(fileWatches.keys()).forEach(stopFileWatch);
132
+ Array.from(dirWatches.keys()).forEach(stopAllDirWatches);
109
133
  }
110
134
 
111
135
  return {
@@ -114,6 +138,7 @@ function attachFileWatch(ctx) {
114
138
  startDirWatch: startDirWatch,
115
139
  stopDirWatch: stopDirWatch,
116
140
  stopAllDirWatches: stopAllDirWatches,
141
+ stopAll: stopAll,
117
142
  };
118
143
  }
119
144