@animalabs/agent-framework 0.22.4 → 0.23.1

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 (234) hide show
  1. package/README.md +105 -3
  2. package/dist/src/agent.d.ts +36 -10
  3. package/dist/src/agent.d.ts.map +1 -1
  4. package/dist/src/agent.js +76 -32
  5. package/dist/src/agent.js.map +1 -1
  6. package/dist/src/code-execution/index.d.ts +1 -1
  7. package/dist/src/code-execution/index.d.ts.map +1 -1
  8. package/dist/src/code-execution/index.js.map +1 -1
  9. package/dist/src/code-execution/py-runner.d.ts +34 -2
  10. package/dist/src/code-execution/py-runner.d.ts.map +1 -1
  11. package/dist/src/code-execution/py-runner.js +72 -16
  12. package/dist/src/code-execution/py-runner.js.map +1 -1
  13. package/dist/src/code-execution/runtime-py.d.ts +3 -2
  14. package/dist/src/code-execution/runtime-py.d.ts.map +1 -1
  15. package/dist/src/code-execution/runtime-py.js +47 -7
  16. package/dist/src/code-execution/runtime-py.js.map +1 -1
  17. package/dist/src/code-execution/tool-definition.d.ts +12 -7
  18. package/dist/src/code-execution/tool-definition.d.ts.map +1 -1
  19. package/dist/src/code-execution/tool-definition.js +59 -11
  20. package/dist/src/code-execution/tool-definition.js.map +1 -1
  21. package/dist/src/config-checks.d.ts +2 -2
  22. package/dist/src/config-checks.d.ts.map +1 -1
  23. package/dist/src/config-checks.js +6 -2
  24. package/dist/src/config-checks.js.map +1 -1
  25. package/dist/src/framework.d.ts +744 -120
  26. package/dist/src/framework.d.ts.map +1 -1
  27. package/dist/src/framework.js +3546 -906
  28. package/dist/src/framework.js.map +1 -1
  29. package/dist/src/gate/event-gate.d.ts.map +1 -1
  30. package/dist/src/gate/event-gate.js +13 -6
  31. package/dist/src/gate/event-gate.js.map +1 -1
  32. package/dist/src/held-writes.d.ts +16 -0
  33. package/dist/src/held-writes.d.ts.map +1 -0
  34. package/dist/src/held-writes.js +56 -0
  35. package/dist/src/held-writes.js.map +1 -0
  36. package/dist/src/index.d.ts +7 -3
  37. package/dist/src/index.d.ts.map +1 -1
  38. package/dist/src/index.js +8 -4
  39. package/dist/src/index.js.map +1 -1
  40. package/dist/src/mcpl/capability-grant.d.ts +4 -0
  41. package/dist/src/mcpl/capability-grant.d.ts.map +1 -1
  42. package/dist/src/mcpl/capability-grant.js +1 -1
  43. package/dist/src/mcpl/capability-grant.js.map +1 -1
  44. package/dist/src/mcpl/channel-registry.d.ts +95 -18
  45. package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
  46. package/dist/src/mcpl/channel-registry.js +578 -150
  47. package/dist/src/mcpl/channel-registry.js.map +1 -1
  48. package/dist/src/mcpl/convert-block.d.ts +2 -2
  49. package/dist/src/mcpl/convert-block.d.ts.map +1 -1
  50. package/dist/src/mcpl/convert-block.js +6 -7
  51. package/dist/src/mcpl/convert-block.js.map +1 -1
  52. package/dist/src/mcpl/index.d.ts +5 -1
  53. package/dist/src/mcpl/index.d.ts.map +1 -1
  54. package/dist/src/mcpl/index.js +3 -1
  55. package/dist/src/mcpl/index.js.map +1 -1
  56. package/dist/src/mcpl/inference-router.d.ts +4 -2
  57. package/dist/src/mcpl/inference-router.d.ts.map +1 -1
  58. package/dist/src/mcpl/inference-router.js +6 -7
  59. package/dist/src/mcpl/inference-router.js.map +1 -1
  60. package/dist/src/mcpl/log-value.d.ts +7 -0
  61. package/dist/src/mcpl/log-value.d.ts.map +1 -1
  62. package/dist/src/mcpl/log-value.js +9 -0
  63. package/dist/src/mcpl/log-value.js.map +1 -1
  64. package/dist/src/mcpl/modern-connection.d.ts +227 -0
  65. package/dist/src/mcpl/modern-connection.d.ts.map +1 -0
  66. package/dist/src/mcpl/modern-connection.js +1088 -0
  67. package/dist/src/mcpl/modern-connection.js.map +1 -0
  68. package/dist/src/mcpl/prefix-rule.d.ts +18 -0
  69. package/dist/src/mcpl/prefix-rule.d.ts.map +1 -0
  70. package/dist/src/mcpl/prefix-rule.js +25 -0
  71. package/dist/src/mcpl/prefix-rule.js.map +1 -0
  72. package/dist/src/mcpl/protocol-family.d.ts +179 -0
  73. package/dist/src/mcpl/protocol-family.d.ts.map +1 -0
  74. package/dist/src/mcpl/protocol-family.js +697 -0
  75. package/dist/src/mcpl/protocol-family.js.map +1 -0
  76. package/dist/src/mcpl/push-coalescer.d.ts +38 -19
  77. package/dist/src/mcpl/push-coalescer.d.ts.map +1 -1
  78. package/dist/src/mcpl/push-coalescer.js +52 -23
  79. package/dist/src/mcpl/push-coalescer.js.map +1 -1
  80. package/dist/src/mcpl/reference-fetcher.d.ts +66 -15
  81. package/dist/src/mcpl/reference-fetcher.d.ts.map +1 -1
  82. package/dist/src/mcpl/reference-fetcher.js +243 -76
  83. package/dist/src/mcpl/reference-fetcher.js.map +1 -1
  84. package/dist/src/mcpl/references.d.ts +63 -18
  85. package/dist/src/mcpl/references.d.ts.map +1 -1
  86. package/dist/src/mcpl/references.js +117 -32
  87. package/dist/src/mcpl/references.js.map +1 -1
  88. package/dist/src/mcpl/server-connection.d.ts +326 -30
  89. package/dist/src/mcpl/server-connection.d.ts.map +1 -1
  90. package/dist/src/mcpl/server-connection.js +995 -212
  91. package/dist/src/mcpl/server-connection.js.map +1 -1
  92. package/dist/src/mcpl/server-registry.d.ts +25 -5
  93. package/dist/src/mcpl/server-registry.d.ts.map +1 -1
  94. package/dist/src/mcpl/server-registry.js +51 -12
  95. package/dist/src/mcpl/server-registry.js.map +1 -1
  96. package/dist/src/mcpl/tool-result-normalize.d.ts +54 -0
  97. package/dist/src/mcpl/tool-result-normalize.d.ts.map +1 -0
  98. package/dist/src/mcpl/tool-result-normalize.js +232 -0
  99. package/dist/src/mcpl/tool-result-normalize.js.map +1 -0
  100. package/dist/src/mcpl/transport.d.ts +56 -13
  101. package/dist/src/mcpl/transport.d.ts.map +1 -1
  102. package/dist/src/mcpl/transport.js +206 -70
  103. package/dist/src/mcpl/transport.js.map +1 -1
  104. package/dist/src/mcpl/types.d.ts +66 -20
  105. package/dist/src/mcpl/types.d.ts.map +1 -1
  106. package/dist/src/mcpl/types.js.map +1 -1
  107. package/dist/src/mcpl/visible-content.d.ts +2 -1
  108. package/dist/src/mcpl/visible-content.d.ts.map +1 -1
  109. package/dist/src/mcpl/visible-content.js +2 -1
  110. package/dist/src/mcpl/visible-content.js.map +1 -1
  111. package/dist/src/module-registry.d.ts +16 -6
  112. package/dist/src/module-registry.d.ts.map +1 -1
  113. package/dist/src/module-registry.js +65 -6
  114. package/dist/src/module-registry.js.map +1 -1
  115. package/dist/src/modules/api/index.d.ts +1 -1
  116. package/dist/src/modules/api/index.d.ts.map +1 -1
  117. package/dist/src/modules/api/index.js +3 -2
  118. package/dist/src/modules/api/index.js.map +1 -1
  119. package/dist/src/modules/discord/discord-js-client.d.ts +42 -2
  120. package/dist/src/modules/discord/discord-js-client.d.ts.map +1 -1
  121. package/dist/src/modules/discord/discord-js-client.js +268 -65
  122. package/dist/src/modules/discord/discord-js-client.js.map +1 -1
  123. package/dist/src/modules/discord/index.d.ts +27 -5
  124. package/dist/src/modules/discord/index.d.ts.map +1 -1
  125. package/dist/src/modules/discord/index.js +366 -76
  126. package/dist/src/modules/discord/index.js.map +1 -1
  127. package/dist/src/modules/discord/refusal.d.ts +14 -0
  128. package/dist/src/modules/discord/refusal.d.ts.map +1 -0
  129. package/dist/src/modules/discord/refusal.js +14 -0
  130. package/dist/src/modules/discord/refusal.js.map +1 -0
  131. package/dist/src/modules/discord/types.d.ts +17 -7
  132. package/dist/src/modules/discord/types.d.ts.map +1 -1
  133. package/dist/src/modules/health/index.d.ts.map +1 -1
  134. package/dist/src/modules/health/index.js +6 -2
  135. package/dist/src/modules/health/index.js.map +1 -1
  136. package/dist/src/modules/index.d.ts +1 -1
  137. package/dist/src/modules/index.d.ts.map +1 -1
  138. package/dist/src/modules/index.js +1 -1
  139. package/dist/src/modules/index.js.map +1 -1
  140. package/dist/src/modules/workspace/disk-agreement.d.ts +25 -2
  141. package/dist/src/modules/workspace/disk-agreement.d.ts.map +1 -1
  142. package/dist/src/modules/workspace/disk-agreement.js +45 -5
  143. package/dist/src/modules/workspace/disk-agreement.js.map +1 -1
  144. package/dist/src/modules/workspace/effects.d.ts +2 -1
  145. package/dist/src/modules/workspace/effects.d.ts.map +1 -1
  146. package/dist/src/modules/workspace/effects.js +19 -9
  147. package/dist/src/modules/workspace/effects.js.map +1 -1
  148. package/dist/src/modules/workspace/index.d.ts +214 -42
  149. package/dist/src/modules/workspace/index.d.ts.map +1 -1
  150. package/dist/src/modules/workspace/index.js +667 -113
  151. package/dist/src/modules/workspace/index.js.map +1 -1
  152. package/dist/src/modules/workspace/observe.d.ts +240 -8
  153. package/dist/src/modules/workspace/observe.d.ts.map +1 -1
  154. package/dist/src/modules/workspace/observe.js +554 -24
  155. package/dist/src/modules/workspace/observe.js.map +1 -1
  156. package/dist/src/modules/workspace/types.d.ts +10 -1
  157. package/dist/src/modules/workspace/types.d.ts.map +1 -1
  158. package/dist/src/modules/workspace/types.js.map +1 -1
  159. package/dist/src/modules/workspace/watcher.d.ts +42 -0
  160. package/dist/src/modules/workspace/watcher.d.ts.map +1 -1
  161. package/dist/src/modules/workspace/watcher.js +130 -25
  162. package/dist/src/modules/workspace/watcher.js.map +1 -1
  163. package/dist/src/silent-heartbeat.d.ts +112 -39
  164. package/dist/src/silent-heartbeat.d.ts.map +1 -1
  165. package/dist/src/silent-heartbeat.js +100 -29
  166. package/dist/src/silent-heartbeat.js.map +1 -1
  167. package/dist/src/tool-args.d.ts +40 -0
  168. package/dist/src/tool-args.d.ts.map +1 -0
  169. package/dist/src/tool-args.js +207 -0
  170. package/dist/src/tool-args.js.map +1 -0
  171. package/dist/src/tool-presentation.d.ts.map +1 -1
  172. package/dist/src/tool-presentation.js +9 -2
  173. package/dist/src/tool-presentation.js.map +1 -1
  174. package/dist/src/types/agent.d.ts +27 -17
  175. package/dist/src/types/agent.d.ts.map +1 -1
  176. package/dist/src/types/events.d.ts +8 -0
  177. package/dist/src/types/events.d.ts.map +1 -1
  178. package/dist/src/types/framework.d.ts +17 -2
  179. package/dist/src/types/framework.d.ts.map +1 -1
  180. package/dist/src/types/module.d.ts +54 -10
  181. package/dist/src/types/module.d.ts.map +1 -1
  182. package/dist/src/types/trace.d.ts +2 -2
  183. package/dist/src/types/trace.d.ts.map +1 -1
  184. package/package.json +3 -4
  185. package/src/agent.ts +99 -36
  186. package/src/code-execution/index.ts +1 -0
  187. package/src/code-execution/py-runner.ts +104 -17
  188. package/src/code-execution/runtime-py.ts +47 -7
  189. package/src/code-execution/tool-definition.ts +65 -11
  190. package/src/config-checks.ts +4 -2
  191. package/src/framework.ts +3633 -956
  192. package/src/gate/event-gate.ts +14 -6
  193. package/src/held-writes.ts +59 -0
  194. package/src/index.ts +19 -4
  195. package/src/mcpl/capability-grant.ts +1 -1
  196. package/src/mcpl/channel-registry.ts +613 -160
  197. package/src/mcpl/convert-block.ts +15 -7
  198. package/src/mcpl/index.ts +14 -1
  199. package/src/mcpl/inference-router.ts +8 -7
  200. package/src/mcpl/log-value.ts +10 -0
  201. package/src/mcpl/modern-connection.ts +1182 -0
  202. package/src/mcpl/prefix-rule.ts +32 -0
  203. package/src/mcpl/protocol-family.ts +691 -0
  204. package/src/mcpl/push-coalescer.ts +67 -29
  205. package/src/mcpl/reference-fetcher.ts +288 -82
  206. package/src/mcpl/references.ts +123 -33
  207. package/src/mcpl/server-connection.ts +1050 -221
  208. package/src/mcpl/server-registry.ts +50 -12
  209. package/src/mcpl/tool-result-normalize.ts +294 -0
  210. package/src/mcpl/transport.ts +204 -62
  211. package/src/mcpl/types.ts +67 -20
  212. package/src/mcpl/visible-content.ts +2 -1
  213. package/src/module-registry.ts +68 -13
  214. package/src/modules/api/index.ts +3 -2
  215. package/src/modules/discord/discord-js-client.ts +288 -72
  216. package/src/modules/discord/index.ts +380 -77
  217. package/src/modules/discord/refusal.ts +13 -0
  218. package/src/modules/discord/types.ts +17 -9
  219. package/src/modules/health/index.ts +6 -2
  220. package/src/modules/index.ts +1 -1
  221. package/src/modules/workspace/disk-agreement.ts +46 -5
  222. package/src/modules/workspace/effects.ts +18 -7
  223. package/src/modules/workspace/index.ts +722 -124
  224. package/src/modules/workspace/observe.ts +591 -22
  225. package/src/modules/workspace/types.ts +10 -1
  226. package/src/modules/workspace/watcher.ts +126 -25
  227. package/src/silent-heartbeat.ts +184 -59
  228. package/src/tool-args.ts +213 -0
  229. package/src/tool-presentation.ts +9 -2
  230. package/src/types/agent.ts +27 -17
  231. package/src/types/events.ts +8 -0
  232. package/src/types/framework.ts +17 -2
  233. package/src/types/module.ts +55 -10
  234. package/src/types/trace.ts +2 -2
@@ -37,14 +37,16 @@
37
37
  */
38
38
  import { createHash } from 'node:crypto';
39
39
  import { constants as fsConstants } from 'node:fs';
40
- import { lstat, mkdir, open, readdir, realpath, stat, statfs } from 'node:fs/promises';
41
- import { dirname, join, posix, sep } from 'node:path';
40
+ import { lstat, mkdir, open, readdir, readlink, realpath, stat, statfs } from 'node:fs/promises';
41
+ import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path';
42
42
  import { sameRootIdentity } from './disk-agreement.js';
43
43
  /**
44
- * Opened with every read or write of a workspace path, where the platform has
45
- * it: for a regular file it changes nothing, and a FIFO or other special file
46
- * swapped in at the path neither blocks the open nor passes the regular-file
47
- * check that follows.
44
+ * Opened with every open of a workspace path that may already be there, a
45
+ * read, a write in place or a directory's sync, where the platform has it:
46
+ * for a regular file or a directory it changes nothing, and a FIFO or other
47
+ * special file swapped in at the path neither blocks the open nor passes the
48
+ * check of its kind that follows. A create needs none: O_CREAT | O_EXCL fails
49
+ * at once on anything already at the path.
48
50
  */
49
51
  export const NONBLOCK = typeof fsConstants.O_NONBLOCK === 'number' ? fsConstants.O_NONBLOCK : 0;
50
52
  /** Bytes hashed per read when streaming a file larger than the read limit. */
@@ -79,6 +81,467 @@ export function nameOf(relativePath) {
79
81
  function lexicalOf(view, rel) {
80
82
  return rel === '' ? view.root : join(view.root, rel);
81
83
  }
84
+ const UNSURE_PREFIX = 'cannot tell whether "';
85
+ /**
86
+ * On a mount that doesn't follow symlinks, what is on the way to a
87
+ * mount-relative path: the first directory, from beneath the mount's root
88
+ * down to the path itself (or, `through` 'parents', down to its parent), that
89
+ * is a symlink, or one the walk couldn't ask. Null when there is none, or
90
+ * when the mount follows symlinks. The path is
91
+ * walked as the lookups that follow resolve it, with `.` and `..` taken out,
92
+ * so a `..` after a name that isn't there hides nothing beyond it, and the
93
+ * root, which is the configured path whatever it resolves to, is never on the
94
+ * way, even where the path names it on its way back into the mount. Each
95
+ * directory is asked only whether it is a link, by readlink, from the root
96
+ * down. EINVAL means it isn't, and the walk goes on. Nothing there, a file in
97
+ * the way, or a directory this user can't search (ENOENT, ENOTDIR, EACCES)
98
+ * ends it with nothing found, since nothing beneath is reached through a link
99
+ * and the caller's own lookup meets the same answer. Any other answer, EPERM
100
+ * included, leaves it unsure of what's beneath, and the path is refused for
101
+ * that.
102
+ */
103
+ export async function linkOnTheWay(view, rel, through = 'path') {
104
+ if (view.followSymlinks)
105
+ return null;
106
+ const resolved = relative(resolve(view.root), resolve(view.root, rel));
107
+ if (resolved === '' || resolved === '..' || resolved.startsWith(`..${sep}`) || isAbsolute(resolved))
108
+ return null;
109
+ const parts = resolved.split(sep);
110
+ const last = through === 'path' ? parts.length : parts.length - 1;
111
+ for (let i = 1; i <= last; i++) {
112
+ const at = parts.slice(0, i).join('/');
113
+ try {
114
+ await readlink(join(view.root, at));
115
+ return { link: at };
116
+ }
117
+ catch (err) {
118
+ const code = errno(err);
119
+ if (code === 'EINVAL')
120
+ continue; // not a link: on to the next directory
121
+ if (code === 'ENOENT' || code === 'ENOTDIR' || code === 'EACCES')
122
+ return null;
123
+ return { unsure: at, code: code ?? String(err) };
124
+ }
125
+ }
126
+ return null;
127
+ }
128
+ /**
129
+ * On a mount that doesn't follow symlinks, what refuses the mount-relative
130
+ * `rel`, which realpath resolved to `real` (or, `through` 'parents', whose
131
+ * parent it resolved to `real`): the first symlink on its way, or a
132
+ * directory there the walk couldn't ask. Null when the mount follows
133
+ * symlinks, or there's none. Where `real` is the root's canonical path joined
134
+ * with what was resolved (`rel`, or with `through` 'parents', its parent),
135
+ * nothing beneath the root resolved through a link, and nothing more is
136
+ * asked: that is every path through ordinary directories, at no cost
137
+ * per path. Otherwise each directory is asked in turn (linkOnTheWay), since a
138
+ * name spelled otherwise than disk spells it, on a file system that ignores
139
+ * case or Unicode normalization, also resolves to another string, and isn't
140
+ * a link.
141
+ */
142
+ export async function linkRefusal(view, rootReal, rel, real, through = 'path') {
143
+ if (view.followSymlinks)
144
+ return null;
145
+ const resolvedAt = through === 'path' ? rel : parentOf(rel);
146
+ if (resolvedAt === '' || real === join(rootReal, resolvedAt))
147
+ return null;
148
+ return linkOnTheWay(view, rel, through);
149
+ }
150
+ export async function linkLeads(root, rootReal, link) {
151
+ const leads = await realpath(join(root, link)).catch(() => null);
152
+ if (leads === null)
153
+ return { kind: 'nowhere' };
154
+ if (!contained(rootReal, leads))
155
+ return { kind: 'outside' };
156
+ const directory = await stat(leads).then((info) => info.isDirectory(), () => false);
157
+ return { kind: 'inside', target: relative(rootReal, leads).split(sep).join('/'), directory };
158
+ }
159
+ /**
160
+ * The nested mount whose directory holds `target` (mount-relative), the
161
+ * innermost where nested mounts nest, as #460 picks one; undefined when none
162
+ * does.
163
+ */
164
+ export function nestedHolder(target, nested) {
165
+ return nested
166
+ .filter((n) => target === n.dir || target.startsWith(`${n.dir}/`))
167
+ .sort((a, b) => b.dir.length - a.dir.length)[0];
168
+ }
169
+ /**
170
+ * Why a path is refused for what linkOnTheWay found, given where a link
171
+ * leads (linkLeads). For a link, the link is the subject, with where it
172
+ * leads: for a link inside the mount, its target, and for a link to a
173
+ * directory there, the setting the mount's host can change (the link stands
174
+ * among the path's directories, so following a link to a file can't make the
175
+ * path beneath it exist); for one leading outside the mount, that it does;
176
+ * for one that can't be resolved, nothing more. A target in a nested mount's
177
+ * directory (`nested`) is that mount's: the refusal names the mount and the
178
+ * path to use there in place of the link, and offers no setting, since
179
+ * following the link would only reach into another mount's directory. For a
180
+ * directory the walk couldn't ask, which one, and the answer it got. Paths
181
+ * are spelled as the tools take them, from the mount's name.
182
+ */
183
+ export function linkWords(name, found, leads, nested = []) {
184
+ const spell = (rel) => `"${rel === '' ? name : `${name}/${rel}`}"`;
185
+ if (!('link' in found))
186
+ return `${UNSURE_PREFIX}${name}/${found.unsure}" is a symlink (${found.code})`;
187
+ const link = `${spell(found.link)} is a symlink`;
188
+ if (leads === null || leads.kind === 'nowhere')
189
+ return `${link}, which this mount does not follow`;
190
+ if (leads.kind === 'outside')
191
+ return `${link} leading outside the mount, which this mount does not follow`;
192
+ const holder = nestedHolder(leads.target, nested);
193
+ if (holder) {
194
+ const rest = leads.target.slice(holder.dir.length + 1);
195
+ return `${link} to ${spell(leads.target)}, which this mount does not follow; it lies in mount "${holder.name}", ` +
196
+ `nested in mount "${name}": use "${rest ? `${holder.name}/${rest}` : holder.name}" in place of ${spell(found.link)}`;
197
+ }
198
+ const setting = leads.directory ? "; the mount's host can set followSymlinks: true" : '';
199
+ return `${link} to ${spell(leads.target)}, which this mount does not follow${setting}`;
200
+ }
201
+ /** Why a path is refused for what linkOnTheWay found (linkWords, with where the link leads). */
202
+ export async function linkReason(view, rootReal, found) {
203
+ return linkWords(view.name, found, 'link' in found ? await linkLeads(view.root, rootReal, found.link) : null, view.nested);
204
+ }
205
+ /**
206
+ * The nested mount whose root holds `real`, a canonical path, the innermost,
207
+ * with the path to use there; undefined when none does. A path the lexical
208
+ * checks pass (inOtherMount, the tools' holderOf) reaches another mount's
209
+ * directory this way through a symlink the mount follows, so each check that
210
+ * resolves a path canonically asks this too.
211
+ */
212
+ export function nestedAtReal(real, nested = []) {
213
+ const holder = nested
214
+ .flatMap((n) => (n.real !== undefined && contained(n.real, real) ? [{ name: n.name, real: n.real }] : []))
215
+ .sort((a, b) => b.real.length - a.real.length)[0];
216
+ if (!holder)
217
+ return undefined;
218
+ const rest = relative(holder.real, real).split(sep).join('/');
219
+ return { name: holder.name, use: rest ? `${holder.name}/${rest}` : holder.name };
220
+ }
221
+ /**
222
+ * Why a mount-relative path that resolves into a nested mount's directory is
223
+ * refused (nestedAtReal): what it leads into, and the path to use in its
224
+ * place. Spelled as the tools take paths, from the mount's name.
225
+ */
226
+ export function leadsInto(name, rel, holder) {
227
+ return `"${rel === '' ? name : `${name}/${rel}`}" leads into mount "${holder.name}", nested in mount "${name}": use "${holder.use}" in its place`;
228
+ }
229
+ // ===========================================================================
230
+ // Disk's spelling of a path
231
+ // ===========================================================================
232
+ const sameFile = (a, b) => a.dev === b.dev && a.ino === b.ino;
233
+ /** A letter's other case: its upper case if it's in lower case, else its lower case. */
234
+ const otherCase = (c) => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase());
235
+ /**
236
+ * The name with each letter's case flipped, where its other case is one
237
+ * letter whose own other case is the letter again, so the flip changes only
238
+ * pairs that any folding disk folds together. `ß`, whose upper case is `SS`,
239
+ * and dotless `ı`, whose upper case `I` lowers to `i`, stay as they are.
240
+ */
241
+ const flippedCase = (name) => [...name].map((c) => {
242
+ const other = otherCase(c);
243
+ return [...other].length === 1 && otherCase(other) === c ? other : c;
244
+ }).join('');
245
+ /** Names compared as NFC in lower case. */
246
+ const folded = (name) => name.normalize('NFC').toLowerCase();
247
+ /**
248
+ * Names compared more loosely, through upper case and back, which also folds
249
+ * `ß` with `ss`, `ſ` with `s` and `ς` with `σ`, as a disk that folds fully
250
+ * does. It also folds dotless `ı` with `i`, which a disk may keep apart;
251
+ * the same-file check keeps that from naming another file.
252
+ */
253
+ export const foldedLoosely = (name) => name.normalize('NFC').toLowerCase().toUpperCase().toLowerCase();
254
+ /**
255
+ * Whether `dir` folds case, told without writing: by looking up flipped a
256
+ * name it lists whose flipped spelling it doesn't list too (a name with no
257
+ * letter to flip is its own flipped spelling, and two hard links that differ
258
+ * only in case are one file on any disk). Where no name it lists tells, or
259
+ * it can't be listed, the directory holding it is asked instead, where both
260
+ * are on one device, each as a link to it leads: its own name looked up
261
+ * flipped there, or, for a name with no letter to flip, that directory's own
262
+ * folding. A directory on a device of its own is taken to tell case apart. A
263
+ * directory not there yet folds as the one it would be made in. Where
264
+ * nothing tells, it's taken to tell case apart.
265
+ */
266
+ export async function foldsCase(dir) {
267
+ const holder = dirname(dir);
268
+ let listed;
269
+ try {
270
+ listed = new Set(await readdir(dir));
271
+ }
272
+ catch (error) {
273
+ if (error.code === 'ENOENT' && holder !== dir)
274
+ return foldsCase(holder);
275
+ listed = new Set();
276
+ }
277
+ const look = (path) => lstat(path, { bigint: true }).catch(() => null);
278
+ const name = [...listed].find((n) => !listed.has(flippedCase(n)));
279
+ if (name !== undefined) {
280
+ const [named, flipped] = await Promise.all([look(join(dir, name)), look(join(dir, flippedCase(name)))]);
281
+ return named != null && flipped != null && sameFile(named, flipped);
282
+ }
283
+ if (holder === dir)
284
+ return false;
285
+ // The directory and its holder are looked up through links, so a root
286
+ // that is one, or is reached through one, is compared by the device it
287
+ // lies on. Whether the holder folds is told by its own entries, the root's
288
+ // name and the flipped name, each looked up without following a link
289
+ // there: a link at the flipped name to the root is another file, and a
290
+ // root that is a link is found as itself.
291
+ const see = (path) => stat(path, { bigint: true }).catch(() => null);
292
+ const own = basename(dir);
293
+ const [self, held, entry, flipped] = await Promise.all([see(dir), see(holder), look(dir), look(join(holder, flippedCase(own)))]);
294
+ if (self == null || held == null || self.dev !== held.dev)
295
+ return false;
296
+ if (flippedCase(own) === own)
297
+ return foldsCase(holder);
298
+ return entry != null && flipped != null && sameFile(entry, flipped);
299
+ }
300
+ /**
301
+ * A name in simple case folding, letter by letter: each letter as the lower
302
+ * case of its upper case, so `Σ` and a word's last `ς` are both `σ`, `ſ` is
303
+ * `s` and `µ` is `μ`. A letter whose upper case is more than one letter
304
+ * takes its own lower case instead, as the Greek `ᾈ` takes `ᾀ` and `ß` stays
305
+ * `ß`. A letter whose lower case is more than one letter too, as `İ`'s is,
306
+ * stays as it is, and so does dotless `ı`, whose upper case `I` lowers to
307
+ * `i`, which case folding keeps apart from it. Unicode normalization is left
308
+ * alone, and so are letters only a fuller folding joins, such as `ß` with
309
+ * `ss`: no listing shows two such names to be one file, and a disk that
310
+ * folds case needn't join them.
311
+ */
312
+ export const lowered = (name) => [...name].map((c) => {
313
+ if (c === '\u0131')
314
+ return c;
315
+ const upper = c.toUpperCase();
316
+ const lower = [...upper].length === 1 ? upper.toLowerCase() : c.toLowerCase();
317
+ return [...lower].length === 1 ? lower : c;
318
+ }).join('');
319
+ /**
320
+ * The name `segment` takes among the workspace's entries directly under the
321
+ * directory `prefix` (its path and `/`, or `''` for the mount's root): the
322
+ * segment itself where an entry is there under it, else the first name, in
323
+ * the order given, equal to it in simple case folding (lowered), else null.
324
+ * A name is compared again only where another name came between its entries,
325
+ * as `N.txt` comes between `N` and `N/x` in path order, so the step doesn't
326
+ * fold a name once for every entry beneath it.
327
+ */
328
+ function entryName(paths, prefix, segment) {
329
+ const want = lowered(segment);
330
+ let found = null;
331
+ let previous = null;
332
+ for (const path of paths) {
333
+ if (!path.startsWith(prefix))
334
+ continue;
335
+ const rest = path.slice(prefix.length);
336
+ const slash = rest.indexOf('/');
337
+ const name = slash === -1 ? rest : rest.slice(0, slash);
338
+ if (name === previous)
339
+ continue;
340
+ previous = name;
341
+ if (name === segment)
342
+ return segment;
343
+ if (found === null && lowered(name) === want)
344
+ found = name;
345
+ }
346
+ return found;
347
+ }
348
+ /**
349
+ * The name `dir` lists for `segment`: `segment` itself, unless disk folds it
350
+ * there and lists it otherwise. `dir` is listed only where a variant of
351
+ * `segment`, its case flipped or its other Unicode normalization form, finds
352
+ * the same file (device and inode) as `given`, the segment's own entry, as
353
+ * on a disk that folds names. The last segment isn't looked up as given, so
354
+ * for it any variant found leads to the listing: on a disk that tells
355
+ * spellings apart, that's only where `dir` holds a variant as a name of its
356
+ * own. The name as given wins if it's listed; otherwise the segment takes
357
+ * the listed name that folds to it and is the same file, so the name
358
+ * returned reaches the file the segment does, or is the segment as given.
359
+ * Names are compared as NFC in lower case first, and only where none of
360
+ * those is the file, more loosely (foldedLoosely). A directory that can't be
361
+ * listed, or a name that can't be looked up, leaves the segment as given. A
362
+ * segment with no variant at all, no letter to flip and one normalization
363
+ * form, is looked up as given and keeps its spelling; for a last segment
364
+ * with a letter that case maps, such as `ς`, the listing then says whether
365
+ * disk lists it as given. `found` says whether the lookups found disk's
366
+ * file: for a directory, its own lookup; for a last segment, whether a
367
+ * variant was found, which on a disk that tells spellings apart may be
368
+ * another file, or for one with no variant, whether disk has it as given, by
369
+ * the listing where it was read. `caseFolds` says what looking the segment
370
+ * up with its case flipped showed of `dir`: that it folds case, that it
371
+ * tells case apart, or nothing (null), for a segment with no letter to flip.
372
+ * For a segment not looked up as given (`given` null), `caseFolds` is set
373
+ * from whatever file the flipped spelling finds, which tells nothing of
374
+ * `dir`; diskSpelling reads it only for directories, which are looked up as
375
+ * given.
376
+ */
377
+ async function listedName(dir, segment, given) {
378
+ const flipped = flippedCase(segment);
379
+ const variants = new Set([flipped, segment.normalize('NFC'), segment.normalize('NFD')]);
380
+ variants.delete(segment);
381
+ // A segment with no variant to try, such as `2024` or `ς`, is looked up
382
+ // as given, the only way to learn that disk has it. A last segment with a
383
+ // letter that case maps, as `ς` does, can reach a file disk lists in
384
+ // another spelling, such as `σ`, on a disk that folds more than case
385
+ // pairs, so there the listing settles whether disk lists it as given.
386
+ if (variants.size === 0) {
387
+ const own = given ?? await lstat(join(dir, segment), { bigint: true }).catch(() => null);
388
+ if (given !== null || own === null || (segment.toLowerCase() === segment && segment.toUpperCase() === segment)) {
389
+ return { name: segment, found: own !== null, caseFolds: null };
390
+ }
391
+ const listed = await readdir(dir).catch(() => null);
392
+ return { name: segment, found: listed?.includes(segment) ?? true, caseFolds: null };
393
+ }
394
+ let folds = false;
395
+ let caseFolds = flipped === segment ? null : false;
396
+ for (const variant of variants) {
397
+ const other = await lstat(join(dir, variant), { bigint: true }).catch(() => null);
398
+ if (other !== null && (given === null || sameFile(other, given))) {
399
+ folds = true;
400
+ if (variant === flipped)
401
+ caseFolds = true;
402
+ break;
403
+ }
404
+ }
405
+ const as = (name) => ({ name, found: folds, caseFolds });
406
+ if (!folds)
407
+ return as(segment);
408
+ const listed = await readdir(dir).catch(() => null);
409
+ if (listed === null || listed.includes(segment))
410
+ return as(segment);
411
+ const target = given ?? await lstat(join(dir, segment), { bigint: true }).catch(() => null);
412
+ if (target === null)
413
+ return as(segment);
414
+ for (const fold of [folded, foldedLoosely]) {
415
+ for (const name of listed) {
416
+ if (fold(name) !== fold(segment))
417
+ continue;
418
+ const info = await lstat(join(dir, name), { bigint: true }).catch(() => null);
419
+ if (info !== null && sameFile(info, target))
420
+ return as(name);
421
+ }
422
+ }
423
+ return as(segment);
424
+ }
425
+ /**
426
+ * `relativePath` as disk spells it. On a disk that folds names, as macOS's
427
+ * does by default (case and Unicode normalization), `SRC/a.ts` reaches
428
+ * `src/a.ts`, and whatever names what it finds from the spelling it was
429
+ * given takes the file in a second time, under a name disk doesn't list. So
430
+ * each segment, from the mount's root down, takes the name its directory
431
+ * lists for it (listedName). The walk goes on beneath a segment only if it's
432
+ * a directory, a symlink not included, so nothing past a link is read. It
433
+ * passes over an empty or `.` segment, which names the directory reached,
434
+ * and goes back up at `..`, to the directory it came from. At the mount's
435
+ * root a `..` stops it, keeping the rest as given: a path can climb above
436
+ * the root and come back in, as `../proj/x` does for a root named `proj`, and
437
+ * walking that would list the directory above the root. An absolute path
438
+ * keeps its spelling too: it names a place from the filesystem's root, not
439
+ * the mount's, so walking it from the mount's root would respell it against
440
+ * other directories, and the path returned could lead outside the mount.
441
+ *
442
+ * Given the workspace's `entries`, an entry not yet on disk spells its path
443
+ * as its file will. A path the workspace holds an entry at exactly keeps its
444
+ * spelling whole, and so does a last segment where the workspace holds an
445
+ * entry at exactly that path once its directories have taken disk's
446
+ * spelling, so an entry that disk lists in another spelling, or in a
447
+ * directory disk lists in another, stays reachable by its own path. `has`
448
+ * counts a path the workspace records an intent at, such as a deletion disk
449
+ * hasn't been given yet, so that's reached by its own path too. And where
450
+ * the directory reached folds case, each segment disk has nothing for takes
451
+ * the name of an entry it equals in simple case folding (entryName), as the
452
+ * disk will once that entry is written: a second draft spelled in another
453
+ * case is the first one. None of this applies where disk's spelling puts the
454
+ * path in a nested mount's directory, by the nested-mount checks' own
455
+ * reading (`holder`): there the path keeps disk's spelling, so they refuse
456
+ * at least what they refuse without the entries. The root's folding is read
457
+ * at start (`rootFoldsCase`), and a directory beneath it is taken to fold as
458
+ * the directory holding it did when its name was looked up with its case
459
+ * flipped, or, for a name with no letter to flip, as the nearest directory
460
+ * above that told, so the step reads nothing more of disk for this.
461
+ */
462
+ export async function diskSpelling(root, relativePath, entries) {
463
+ if (relativePath === '' || isAbsolute(relativePath))
464
+ return relativePath;
465
+ // A path that names an entry exactly keeps its spelling, read before the
466
+ // walk awaits disk, so the entries' walk needn't run for it.
467
+ const exact = entries?.has(relativePath) ?? false;
468
+ // The path as disk spells it, and as the workspace's entries spell it.
469
+ const segments = relativePath.split('/');
470
+ const spelled = [...segments];
471
+ // The directories walked into, the mount's root first; their names below
472
+ // the root, as returned; and whether each folds case: the root's as read at
473
+ // start, and beneath it, whether looking its name up with its case flipped
474
+ // found it in the directory holding it (null for a name with no letter to
475
+ // flip).
476
+ const dirs = [root];
477
+ const names = [];
478
+ const folding = [entries?.rootFoldsCase ?? null];
479
+ // From segment `from` on, disk has nothing: each segment takes an entry's
480
+ // name, where the directory reached folds case, passing over an empty or
481
+ // `.` segment, and the rest keeps its spelling from the first segment no
482
+ // entry has a name for, a `..` included.
483
+ const fromEntries = (from) => {
484
+ // The nearest directory, from the one reached up, that told.
485
+ let folds = null;
486
+ for (let k = folding.length - 1; folds === null && k >= 0; k--)
487
+ folds = folding[k];
488
+ if (!entries || exact || !folds)
489
+ return;
490
+ let prefix = names.length === 0 ? '' : `${names.join('/')}/`;
491
+ const paths = entries.under(prefix);
492
+ for (let j = from; j < spelled.length; j++) {
493
+ const segment = spelled[j];
494
+ if (segment === '' || segment === '.')
495
+ continue;
496
+ const name = entryName(paths, prefix, segment);
497
+ if (name === null)
498
+ return;
499
+ spelled[j] = name;
500
+ prefix = `${prefix}${name}/`;
501
+ }
502
+ };
503
+ for (let i = 0; i < segments.length; i++) {
504
+ const segment = segments[i];
505
+ if (segment === '' || segment === '.')
506
+ continue;
507
+ if (segment === '..') {
508
+ if (dirs.length === 1)
509
+ break;
510
+ dirs.pop();
511
+ names.pop();
512
+ folding.pop();
513
+ continue;
514
+ }
515
+ const dir = dirs[dirs.length - 1];
516
+ if (i === segments.length - 1) {
517
+ const { name, found } = await listedName(dir, segment, null);
518
+ segments[i] = name;
519
+ if (!exact && !entries?.has([...names, segment].join('/'))) {
520
+ spelled[i] = name;
521
+ if (!found)
522
+ fromEntries(i);
523
+ }
524
+ break;
525
+ }
526
+ const given = await lstat(join(dir, segment), { bigint: true }).catch(() => null);
527
+ if (given === null) {
528
+ fromEntries(i);
529
+ break;
530
+ }
531
+ const { name, caseFolds } = await listedName(dir, segment, given);
532
+ segments[i] = name;
533
+ spelled[i] = name;
534
+ if (!given.isDirectory())
535
+ break;
536
+ dirs.push(join(dir, name));
537
+ names.push(name);
538
+ folding.push(caseFolds);
539
+ }
540
+ const asDisk = segments.join('/');
541
+ if (!entries || entries.holder(asDisk) !== undefined)
542
+ return asDisk;
543
+ return exact ? relativePath : spelled.join('/');
544
+ }
82
545
  /** Errors meaning a directory can't be synced on this filesystem, not that syncing it failed. */
83
546
  const DIRECTORY_SYNC_UNSUPPORTED = new Set(['EINVAL', 'ENOTSUP']);
84
547
  /**
@@ -128,7 +591,11 @@ export class CheckedDir {
128
591
  async sync() {
129
592
  if (process.platform === 'win32')
130
593
  return;
131
- const handle = await open(this.real, 'r');
594
+ // NONBLOCK: a FIFO put at its path since it was checked opens at once,
595
+ // and is refused below as not this directory, instead of holding the
596
+ // open, and with it the mount's turn, until something opens it for
597
+ // writing.
598
+ const handle = await open(this.real, fsConstants.O_RDONLY | NONBLOCK);
132
599
  try {
133
600
  const info = await handle.stat({ bigint: true });
134
601
  if (!info.isDirectory() || info.dev !== this.dev || info.ino !== this.ino)
@@ -167,8 +634,18 @@ async function locateDir(view, rootReal, rel) {
167
634
  const code = errno(err);
168
635
  if (code === 'ENOENT' || code === 'ENOTDIR')
169
636
  return rel === '' ? { kind: 'unobserved', reason: ROOT_UNAVAILABLE } : { kind: 'missing' };
637
+ // A link on the way that can't be resolved, such as a loop, is still a
638
+ // link this mount doesn't follow.
639
+ const found = rel === '' ? null : await linkOnTheWay(view, rel);
640
+ if (found !== null)
641
+ return { kind: 'unobserved', reason: await linkReason(view, rootReal, found) };
170
642
  return { kind: 'unobserved', reason: `cannot resolve the directory (${code ?? String(err)})` };
171
643
  }
644
+ // A mount that doesn't follow symlinks goes through none on the way to a
645
+ // directory, inside the mount or out of it.
646
+ const found = await linkRefusal(view, rootReal, rel, real);
647
+ if (found !== null)
648
+ return { kind: 'unobserved', reason: await linkReason(view, rootReal, found) };
172
649
  if (rel === '') {
173
650
  // The root is whatever the configured path names, and it must still be
174
651
  // the root the pass was given.
@@ -178,6 +655,13 @@ async function locateDir(view, rootReal, rel) {
178
655
  else if (!contained(rootReal, real)) {
179
656
  return { kind: 'unobserved', reason: OUTSIDE_PARENT, outside: true };
180
657
  }
658
+ else {
659
+ // Through a link this mount follows, a directory can be a nested
660
+ // mount's, which the mount neither reads nor writes.
661
+ const holder = nestedAtReal(real, view.nested);
662
+ if (holder)
663
+ return { kind: 'unobserved', reason: leadsInto(view.name, rel, holder) };
664
+ }
181
665
  const checked = await checkedAt(view, rel, real);
182
666
  if (checked) {
183
667
  // A root that isn't the directory disk last agreed under proves nothing
@@ -238,6 +722,11 @@ async function createDir(view, rootReal, rel) {
238
722
  return { kind: 'unobserved', reason: ROOT_UNAVAILABLE };
239
723
  for (let i = depth; i < parts.length; i++) {
240
724
  const childRel = parts.slice(0, i + 1).join('/');
725
+ // Nothing is made in a nested mount's directory, its root included where
726
+ // it isn't made yet, by a path that reaches it through a link.
727
+ const holder = nestedAtReal(base.at(parts[i]), view.nested);
728
+ if (holder)
729
+ return { kind: 'unobserved', reason: leadsInto(view.name, childRel, holder) };
241
730
  try {
242
731
  await mkdir(base.at(parts[i]));
243
732
  }
@@ -249,8 +738,14 @@ async function createDir(view, rootReal, rel) {
249
738
  // Made, or there already (made meanwhile, or not a directory): it must
250
739
  // be a real directory now, never a symlink or file in its place.
251
740
  const checked = await checkedAt(view, childRel, base.at(parts[i]));
252
- if (!checked)
741
+ if (!checked) {
742
+ // A symlink in the way, such as one that leads nowhere, on a mount that
743
+ // doesn't follow symlinks, is named as one.
744
+ const there = view.followSymlinks ? null : await lstat(base.at(parts[i])).catch(() => null);
745
+ if (there?.isSymbolicLink())
746
+ return { kind: 'unobserved', reason: await linkReason(view, rootReal, { link: childRel }) };
253
747
  return { kind: 'unobserved', reason: 'a parent path is not a directory' };
748
+ }
254
749
  base = checked;
255
750
  }
256
751
  return { kind: 'checked', dir: base };
@@ -263,10 +758,12 @@ async function createDir(view, rootReal, rel) {
263
758
  * which nothing can exist. Either proves it only while every directory the
264
759
  * walk went through, the root included, is still the one its path names
265
760
  * afterwards: a root or parent replaced during the walk would otherwise lend
266
- * its replacement's absence to the original's path. Symlinked directories
267
- * inside the mount are followed as parents; any other symlink proves
268
- * nothing. Returns the directory whose entries prove it, or why absence
269
- * couldn't be shown.
761
+ * its replacement's absence to the original's path. On a mount that follows
762
+ * symlinks, symlinked directories inside the mount are followed as parents;
763
+ * on one that doesn't, a symlinked directory on the way proves nothing, and
764
+ * the reason says so (linkReason). Any other symlink proves nothing.
765
+ * Returns the directory whose entries prove it, or why absence couldn't be
766
+ * shown.
270
767
  */
271
768
  export async function confirmAbsent(view, rootReal, rel, target = 'entry') {
272
769
  const root = await locateDir(view, rootReal, '');
@@ -309,6 +806,9 @@ export async function confirmAbsent(view, rootReal, rel, target = 'entry') {
309
806
  if (info.isSymbolicLink()) {
310
807
  if (last && target === 'entry')
311
808
  return CHANGED; // present: the link itself
809
+ // A mount that doesn't follow symlinks goes through none on the way.
810
+ if (!view.followSymlinks)
811
+ return linkReason(view, rootReal, { link: childRel });
312
812
  const located = await locateDir(view, rootReal, childRel);
313
813
  if (located.kind !== 'checked')
314
814
  return BENEATH_SYMLINK;
@@ -397,17 +897,29 @@ export async function filesystemTrustsCtime(root) {
397
897
  /**
398
898
  * Where a path's parent directory canonically lies: inside the mount, gone
399
899
  * (absence is then proven separately), or somewhere a no-follow open would
400
- * still reach through — O_NOFOLLOW guards only the final component.
900
+ * still reach through — O_NOFOLLOW guards only the final component — which
901
+ * on a mount that doesn't follow symlinks is any symlink on the way.
401
902
  */
402
- async function parentPlacement(rootReal, lexical) {
903
+ async function parentPlacement(view, rootReal, relativePath) {
403
904
  try {
404
- const parent = await realpath(dirname(lexical));
405
- return contained(rootReal, parent) ? 'inside' : { reason: OUTSIDE_PARENT };
905
+ const parent = await realpath(dirname(join(view.root, relativePath)));
906
+ const found = await linkRefusal(view, rootReal, relativePath, parent, 'parents');
907
+ if (found !== null)
908
+ return { reason: await linkReason(view, rootReal, found) };
909
+ if (!contained(rootReal, parent))
910
+ return { reason: OUTSIDE_PARENT };
911
+ const holder = nestedAtReal(parent, view.nested);
912
+ return holder ? { reason: leadsInto(view.name, parentOf(relativePath), holder) } : 'inside';
406
913
  }
407
914
  catch (err) {
408
915
  const code = errno(err);
409
916
  if (code === 'ENOENT' || code === 'ENOTDIR')
410
917
  return 'gone';
918
+ // A link on the way that can't be resolved, such as a loop, is still a
919
+ // link this mount doesn't follow.
920
+ const found = await linkOnTheWay(view, relativePath, 'parents');
921
+ if (found !== null)
922
+ return { reason: await linkReason(view, rootReal, found) };
411
923
  return { reason: `cannot resolve the parent directory (${code ?? String(err)})` };
412
924
  }
413
925
  }
@@ -419,7 +931,7 @@ async function parentPlacement(rootReal, lexical) {
419
931
  */
420
932
  export async function observePath(view, rootReal, relativePath) {
421
933
  const lexical = join(view.root, relativePath);
422
- const placement = await parentPlacement(rootReal, lexical);
934
+ const placement = await parentPlacement(view, rootReal, relativePath);
423
935
  if (placement === 'gone')
424
936
  return absence(view, rootReal, relativePath);
425
937
  if (placement !== 'inside')
@@ -453,6 +965,9 @@ export async function observePath(view, rootReal, relativePath) {
453
965
  }
454
966
  if (!contained(rootReal, real))
455
967
  return { kind: 'unobserved', reason: 'a symlink that leaves the mount' };
968
+ const holder = nestedAtReal(real, view.nested);
969
+ if (holder)
970
+ return { kind: 'unobserved', reason: leadsInto(view.name, relativePath, holder) };
456
971
  if (!target.isFile()) {
457
972
  return { kind: 'unobserved', reason: target.isDirectory() ? 'a symlink to a directory, not walked' : 'not a regular file' };
458
973
  }
@@ -468,9 +983,10 @@ export async function observePath(view, rootReal, relativePath) {
468
983
  }
469
984
  /**
470
985
  * Open a mount-relative file for reading, located: a regular file that
471
- * canonically lies inside the mount — whatever the symlink policy, since a
472
- * symlinked parent escapes O_NOFOLLOW — and is the file the canonical path
473
- * names now. The handle is the caller's to close.
986
+ * canonically lies inside the mount, whatever the symlink policy, since
987
+ * O_NOFOLLOW guards only the final component; on a mount that doesn't follow
988
+ * symlinks, reached through no symlinked directory either (linkRefusal); and
989
+ * the file the canonical path names now. The handle is the caller's to close.
474
990
  */
475
991
  async function openContained(view, rootReal, relativePath) {
476
992
  const lexical = join(view.root, relativePath);
@@ -491,8 +1007,11 @@ async function openContained(view, rootReal, relativePath) {
491
1007
  const code = errno(err);
492
1008
  if (code === 'ENOENT' || code === 'ENOTDIR')
493
1009
  return absence(view, rootReal, relativePath);
494
- if (code === 'ELOOP' && !view.followSymlinks)
495
- return { kind: 'unobserved', reason: NOT_FOLLOWED };
1010
+ if (code === 'ELOOP' && !view.followSymlinks) {
1011
+ // The link at the path, or a looping one on its way.
1012
+ const found = await linkOnTheWay(view, relativePath, 'parents');
1013
+ return { kind: 'unobserved', reason: found === null ? NOT_FOLLOWED : await linkReason(view, rootReal, found) };
1014
+ }
496
1015
  if (code === 'EISDIR')
497
1016
  return { kind: 'unobserved', reason: 'a directory' };
498
1017
  return { kind: 'unobserved', reason: `cannot open (${code ?? String(err)})` };
@@ -500,14 +1019,25 @@ async function openContained(view, rootReal, relativePath) {
500
1019
  let keep = false;
501
1020
  try {
502
1021
  const info = await handle.stat({ bigint: true });
503
- if (!info.isFile())
504
- return { kind: 'unobserved', reason: info.isDirectory() ? 'a directory' : 'not a regular file' };
505
1022
  const real = await realpath(lexical).catch(() => null);
506
1023
  if (real === null)
507
1024
  return { kind: 'unobserved', reason: 'the file changed while it was read' };
1025
+ // O_NOFOLLOW guards only the final component: a mount that doesn't
1026
+ // follow symlinks goes through none among the directories on the way
1027
+ // either. Asked of what the path resolved to after the open, which the
1028
+ // check below holds to the descriptor's file, and before what the
1029
+ // descriptor holds, so a link gets its own answer whatever it leads to.
1030
+ const found = await linkRefusal(view, rootReal, relativePath, real);
1031
+ if (found !== null)
1032
+ return { kind: 'unobserved', reason: await linkReason(view, rootReal, found) };
1033
+ if (!info.isFile())
1034
+ return { kind: 'unobserved', reason: info.isDirectory() ? 'a directory' : 'not a regular file' };
508
1035
  if (!contained(rootReal, real)) {
509
1036
  return { kind: 'unobserved', reason: view.followSymlinks ? 'a symlink that leaves the mount' : OUTSIDE_PARENT };
510
1037
  }
1038
+ const holder = nestedAtReal(real, view.nested);
1039
+ if (holder)
1040
+ return { kind: 'unobserved', reason: leadsInto(view.name, relativePath, holder) };
511
1041
  const named = await stat(real, { bigint: true }).catch(() => null);
512
1042
  if (named === null || named.dev !== info.dev || named.ino !== info.ino) {
513
1043
  return { kind: 'unobserved', reason: 'the file changed while it was read' };