@vzn/vx 0.0.652 → 0.0.653

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 (167) hide show
  1. package/README.md +2 -2
  2. package/launcher.sh +2 -1
  3. package/package.json +5 -5
  4. package/schemas/error.json +1 -1
  5. package/schemas/show.json +1 -0
  6. package/skills/vx/SKILL.md +3 -3
  7. package/src/bin.ts +25 -3
  8. package/src/cache/archive.ts +196 -31
  9. package/src/cache/cache.ts +863 -225
  10. package/src/cache/checkpointer.ts +102 -0
  11. package/src/cache/file-hashes.ts +2 -1
  12. package/src/cache/fold-memo.ts +24 -4
  13. package/src/cache/fs-clock.ts +26 -0
  14. package/src/cache/git-inputs.ts +188 -22
  15. package/src/cache/history-stream.ts +22 -5
  16. package/src/cache/index.ts +10 -1
  17. package/src/cache/inputs.ts +573 -152
  18. package/src/cache/key-fold.ts +1 -1
  19. package/src/cache/layer.ts +23 -10
  20. package/src/cache/layered-cache.ts +28 -7
  21. package/src/cache/output-index.ts +106 -44
  22. package/src/cache/plugin-cache.ts +2 -2
  23. package/src/cache/policy.ts +6 -2
  24. package/src/cache/remote-lane.ts +3 -1
  25. package/src/cache/run-history.ts +50 -10
  26. package/src/cache/schema.ts +42 -9
  27. package/src/cli/cache.ts +147 -34
  28. package/src/cli/completions.ts +2 -2
  29. package/src/cli/core-alias.ts +9 -2
  30. package/src/cli/docs.ts +4 -2
  31. package/src/cli/help.ts +25 -2
  32. package/src/cli/index.ts +38 -7
  33. package/src/cli/info.ts +4 -3
  34. package/src/cli/init.ts +55 -20
  35. package/src/cli/last.ts +184 -41
  36. package/src/cli/lock.ts +187 -24
  37. package/src/cli/plan-format.ts +24 -21
  38. package/src/cli/plugin-commands.ts +6 -1
  39. package/src/cli/plugin-templates.ts +1 -1
  40. package/src/cli/run.ts +68 -34
  41. package/src/cli/select.ts +160 -64
  42. package/src/cli/show.ts +63 -18
  43. package/src/cli/task-verb.ts +12 -7
  44. package/src/cli/upgrade.ts +210 -76
  45. package/src/cli/watch-filter.ts +88 -15
  46. package/src/cli/watch-fs.ts +34 -47
  47. package/src/cli/watch-judge.ts +72 -8
  48. package/src/cli/watch-set.ts +6 -4
  49. package/src/cli/watch.ts +179 -25
  50. package/src/cli/why.ts +107 -33
  51. package/src/cli/workspace-config.ts +11 -1
  52. package/src/config.ts +9 -0
  53. package/src/exec/env.ts +3 -1
  54. package/src/exec/executor.ts +76 -15
  55. package/src/exec/kill-tree.ts +7 -7
  56. package/src/exec/runner.ts +270 -108
  57. package/src/exec/sandbox-binds.ts +109 -18
  58. package/src/exec/sandbox-runtime.ts +134 -37
  59. package/src/exec/sandbox-violations.ts +250 -46
  60. package/src/exec/spawn-off.ts +10 -5
  61. package/src/exec/warm-shell.ts +29 -7
  62. package/src/graph/folds.ts +89 -16
  63. package/src/graph/index.ts +8 -0
  64. package/src/graph/priorities.ts +8 -5
  65. package/src/graph/scheduler.ts +173 -38
  66. package/src/graph/stand-ins.ts +36 -0
  67. package/src/graph/task-graph.ts +327 -141
  68. package/src/graph/task-status.ts +5 -1
  69. package/src/handoff.ts +45 -10
  70. package/src/history/failure-mode.ts +15 -5
  71. package/src/history/history.ts +103 -30
  72. package/src/history/index.ts +1 -0
  73. package/src/history/metrics.ts +189 -69
  74. package/src/index.ts +2 -1
  75. package/src/orchestrator/admission.ts +13 -9
  76. package/src/orchestrator/affected-tasks.ts +35 -13
  77. package/src/orchestrator/command-windows.ts +8 -5
  78. package/src/orchestrator/deferred-outputs.ts +41 -12
  79. package/src/orchestrator/doctor.ts +21 -9
  80. package/src/orchestrator/download-policy.ts +3 -3
  81. package/src/orchestrator/excluded-keys.ts +14 -2
  82. package/src/orchestrator/execute-args.ts +9 -2
  83. package/src/orchestrator/execute-task.ts +564 -123
  84. package/src/orchestrator/graph-memo.ts +26 -4
  85. package/src/orchestrator/graph-stage.ts +27 -4
  86. package/src/orchestrator/held-servers.ts +36 -14
  87. package/src/orchestrator/hit-restore.ts +136 -87
  88. package/src/orchestrator/index.ts +4 -1
  89. package/src/orchestrator/keyed-projects.ts +25 -0
  90. package/src/orchestrator/local-floor.ts +21 -3
  91. package/src/orchestrator/lockfile-claim.ts +30 -4
  92. package/src/orchestrator/miss-save.ts +33 -53
  93. package/src/orchestrator/options.ts +13 -0
  94. package/src/orchestrator/output-host.ts +16 -4
  95. package/src/orchestrator/output-log.ts +1 -54
  96. package/src/orchestrator/persistent-task.ts +31 -17
  97. package/src/orchestrator/persistent.ts +152 -15
  98. package/src/orchestrator/placement.ts +81 -15
  99. package/src/orchestrator/plan.ts +37 -15
  100. package/src/orchestrator/plugin-host.ts +232 -108
  101. package/src/orchestrator/plugin.ts +53 -27
  102. package/src/orchestrator/prepare.ts +194 -38
  103. package/src/orchestrator/projects.ts +89 -10
  104. package/src/orchestrator/read-only.ts +115 -0
  105. package/src/orchestrator/run-context.ts +6 -2
  106. package/src/orchestrator/run-failures.ts +43 -14
  107. package/src/orchestrator/run-id.ts +13 -2
  108. package/src/orchestrator/run-keys.ts +87 -28
  109. package/src/orchestrator/run-live.ts +16 -13
  110. package/src/orchestrator/run-lock.ts +27 -4
  111. package/src/orchestrator/run-records.ts +12 -10
  112. package/src/orchestrator/run-report.ts +2 -1
  113. package/src/orchestrator/run-stop.ts +17 -9
  114. package/src/orchestrator/run.ts +443 -107
  115. package/src/orchestrator/sandbox-fix.ts +40 -12
  116. package/src/orchestrator/sandbox-request.ts +286 -59
  117. package/src/orchestrator/save-lane.ts +24 -13
  118. package/src/orchestrator/shell-verdict.ts +20 -1
  119. package/src/orchestrator/signals.ts +103 -8
  120. package/src/orchestrator/stable-keys.ts +17 -9
  121. package/src/orchestrator/task-artifacts.ts +15 -2
  122. package/src/orchestrator/task-hash.ts +187 -30
  123. package/src/orchestrator/telemetry-host.ts +25 -6
  124. package/src/orchestrator/telemetry.ts +80 -12
  125. package/src/orchestrator/upstream.ts +3 -1
  126. package/src/report/events.ts +20 -3
  127. package/src/report/forecast.ts +103 -37
  128. package/src/report/framed-output.ts +165 -157
  129. package/src/report/index.ts +2 -1
  130. package/src/report/logger.ts +107 -30
  131. package/src/report/plain-output.ts +13 -9
  132. package/src/report/status-line.ts +28 -55
  133. package/src/report/summary.ts +57 -14
  134. package/src/settings.ts +42 -6
  135. package/src/ui/components.ts +36 -42
  136. package/src/ui/index.ts +3 -1
  137. package/src/ui/live-region.ts +67 -8
  138. package/src/ui/terminal.ts +12 -0
  139. package/src/ui/theme.ts +16 -1
  140. package/src/util/config-frame.ts +17 -3
  141. package/src/util/edit-distance.ts +17 -1
  142. package/src/util/errors.ts +23 -7
  143. package/src/util/foreign-cache-dir.ts +60 -0
  144. package/src/util/git-ignored.ts +59 -5
  145. package/src/util/index.ts +15 -1
  146. package/src/util/paths.ts +50 -6
  147. package/src/util/plain-text.ts +19 -0
  148. package/src/util/secret-mask.ts +57 -1
  149. package/src/util/settle.ts +16 -0
  150. package/src/util/size.ts +12 -2
  151. package/src/util/write-file.ts +86 -9
  152. package/src/workspace/affected.ts +225 -27
  153. package/src/workspace/config-bytecode.ts +23 -4
  154. package/src/workspace/config-eval.ts +90 -16
  155. package/src/workspace/config-imports.ts +96 -41
  156. package/src/workspace/config-schema.ts +126 -25
  157. package/src/workspace/dependencies-lock.ts +7 -14
  158. package/src/workspace/filter.ts +10 -1
  159. package/src/workspace/index.ts +3 -1
  160. package/src/workspace/json-data.ts +34 -8
  161. package/src/workspace/lockfile.ts +86 -37
  162. package/src/workspace/migration.ts +46 -25
  163. package/src/workspace/module-state.ts +62 -0
  164. package/src/workspace/nested-dirs.ts +8 -0
  165. package/src/workspace/project-loader.ts +276 -32
  166. package/src/workspace/repo-id.ts +29 -6
  167. package/src/workspace/workspace.ts +190 -30
@@ -86,16 +86,22 @@ export function errorCode(err: unknown): string {
86
86
  * Does this argv ask for JSON (`--format json`, `--format=json`,
87
87
  * `--dry=json`)? Then a refusal is a JSON document on stdout too, so a
88
88
  * reader of stdout gets an answer, not nothing. Args after `--` are the
89
- * task's, not vx's.
89
+ * task's, not vx's. Each flag's LAST value counts, as every verb's parser
90
+ * keeps it: `--format json --format pretty` prints pretty. `--dry` alone is
91
+ * run's pretty plan, and a switch beside `--format` elsewhere.
90
92
  */
91
93
  export function wantsJson(argv: readonly string[]): boolean {
94
+ let format: string | undefined
95
+ let dry: string | undefined
92
96
  for (let i = 0; i < argv.length; i++) {
93
97
  const a = argv[i]!
94
- if (a === '--') return false
95
- if (a === '--format=json' || a === '--dry=json') return true
96
- if (a === '--format' && argv[i + 1] === 'json') return true
98
+ if (a === '--') break
99
+ if (a === '--format') format = argv[++i]
100
+ else if (a.startsWith('--format=')) format = a.slice('--format='.length)
101
+ else if (a === '--dry') dry = 'pretty'
102
+ else if (a.startsWith('--dry=')) dry = a.slice('--dry='.length)
97
103
  }
98
- return false
104
+ return format === 'json' || dry === 'json'
99
105
  }
100
106
 
101
107
  /**
@@ -185,9 +191,18 @@ export function isOutOfFds(err: unknown): err is NodeJS.ErrnoException {
185
191
  export const OUT_OF_FDS_HINT =
186
192
  'the process is out of file descriptors; raise the limit (ulimit -n 4096) and re-run'
187
193
 
188
- /** A file system refusing a write for a reason no code path caused: permission or space. */
194
+ /**
195
+ * `ENAMETOOLONG`: a path past what the file system takes (PATH_MAX, 4096
196
+ * bytes on Linux; NAME_MAX for one name). The path is the user's: an
197
+ * output that deep failed its save as an internal error.
198
+ */
199
+ function isNameTooLong(err: unknown): err is NodeJS.ErrnoException {
200
+ return err instanceof Error && (err as NodeJS.ErrnoException).code === 'ENAMETOOLONG'
201
+ }
202
+
203
+ /** A file system refusing a path for a reason no code path caused: permission, space or length. */
189
204
  export function isFsRefusal(err: unknown): err is NodeJS.ErrnoException {
190
- return isPermissionError(err) || isDiskFull(err)
205
+ return isPermissionError(err) || isDiskFull(err) || isNameTooLong(err)
191
206
  }
192
207
 
193
208
  export const DISK_FULL_HINT = 'the disk that path is on is full'
@@ -205,6 +220,7 @@ const READ_SYSCALLS = new Set(['scandir', 'stat', 'lstat', 'readlink', 'read', '
205
220
  export function fsRefusalHint(err: NodeJS.ErrnoException): string {
206
221
  const { syscall } = err
207
222
  if (isDiskFull(err)) return DISK_FULL_HINT
223
+ if (isNameTooLong(err)) return 'that path is longer than the file system allows; shorten it'
208
224
  // A listing told to be a write sent the user to chmod the wrong thing.
209
225
  return READ_SYSCALLS.has(syscall ?? '') ? READ_PERMISSION_HINT : PERMISSION_HINT
210
226
  }
@@ -0,0 +1,60 @@
1
+ import { existsSync, realpathSync, statSync } from 'node:fs'
2
+ import { homedir } from 'node:os'
3
+ import path from 'node:path'
4
+ import { UserError } from './errors.js'
5
+
6
+ export const OWN_DIR_HINT =
7
+ 'Point `cache.dir` in .vx/workspace.ts (or --cache-dir) at a directory of its own, such as .vx/cache.'
8
+
9
+ /** Cache directories this process already found to be vx's own, with the workspace root asked for. */
10
+ const passed = new Set<string>()
11
+
12
+ /**
13
+ * Refuses a cache directory that resolves to the home directory, the file
14
+ * system root, or a directory holding the workspace or a repository:
15
+ * compared unresolved, a `.vx/cache` linked to `~` or `..` passed, the run
16
+ * wrote its index and config bundle there, and `vx cache clean` deleted
17
+ * every entry (the home directory; the workspace's `.git` and config). The
18
+ * config bundle asks first and the cache's open again: one realpath, as a
19
+ * pass is kept for the process; a refusal is decided afresh each time. A
20
+ * directory not there yet is made as named, by its caller.
21
+ */
22
+ export function refuseForeignCacheDir(cacheDir: string, workspaceRoot: string | undefined): void {
23
+ const memo = `${path.resolve(cacheDir)}\0${workspaceRoot ?? ''}`
24
+ if (passed.has(memo)) return
25
+ let real: string
26
+ try {
27
+ real = realpathSync(cacheDir)
28
+ } catch {
29
+ return
30
+ }
31
+ const home = process.env['HOME'] || homedir()
32
+ let what: string | undefined
33
+ if (real === path.parse(real).root) what = 'the file system root'
34
+ else if (sameDir(real, home)) what = 'the home directory'
35
+ else if (
36
+ (workspaceRoot !== undefined && !path.relative(real, workspaceRoot).startsWith('..')) ||
37
+ existsSync(path.join(real, '.git'))
38
+ )
39
+ what = 'a directory holding the workspace or a repository'
40
+ if (what === undefined) {
41
+ passed.add(memo)
42
+ return
43
+ }
44
+ throw new UserError(
45
+ `cache directory ${cacheDir} resolves to ${real}, ${what}: vx would keep its index there and \`vx cache clean\` would delete from it. ${OWN_DIR_HINT}`,
46
+ 'VX_E_CONFIG',
47
+ )
48
+ }
49
+
50
+ function sameDir(a: string, b: string): boolean {
51
+ if (a === path.resolve(b)) return true
52
+ // A home named through a link: stats only when the names could agree.
53
+ if (path.basename(a) !== path.basename(b)) return false
54
+ try {
55
+ const [x, y] = [statSync(a), statSync(b)]
56
+ return x.ino === y.ino && x.dev === y.dev
57
+ } catch {
58
+ return false
59
+ }
60
+ }
@@ -4,6 +4,8 @@
4
4
  // knows every rule source (nested files, `info/exclude`, `core.excludesFile`)
5
5
  // and that a directory holding a tracked file is not ignored.
6
6
 
7
+ import { existsSync } from 'node:fs'
8
+ import path from 'node:path'
7
9
  import { gitEnv } from './hide-env.js'
8
10
  import { isLiteralPattern, normalizeGlob } from './paths.js'
9
11
  import { executablePath } from './which.js'
@@ -37,6 +39,56 @@ export function ignoreProbes(glob: string): { paths: string[]; deep: boolean; di
37
39
 
38
40
  const CHECK_IGNORE = ['check-ignore', '--stdin', '-z']
39
41
 
42
+ /**
43
+ * Per directory git runs in, whether its `core.ignorecase=true` is false on
44
+ * disk: a `.git` copied from macOS to Linux keeps the setting, and git then
45
+ * takes a new `a.txt` beside the tracked `A.txt` for the tracked file and
46
+ * lists it nowhere. Asked only where git's config was read anyway.
47
+ */
48
+ const caseLies = new Map<string, boolean>()
49
+
50
+ /**
51
+ * Whether `core.ignorecase=true`, which git in `dir` reported, is false on
52
+ * disk: one stat of `probe` (an existing path) with its name's case
53
+ * swapped, once per `dir`. A name with no cased letter cannot tell, so git
54
+ * is trusted.
55
+ */
56
+ export function ignoreCaseLies(dir: string, probe: string): boolean {
57
+ const at = path.resolve(dir)
58
+ let lies = caseLies.get(at)
59
+ if (lies === undefined) {
60
+ const name = path.basename(probe)
61
+ const lower = name.toLowerCase()
62
+ const twin = lower === name ? name.toUpperCase() : lower
63
+ lies = twin !== name && !existsSync(path.join(path.dirname(probe), twin))
64
+ caseLies.set(at, lies)
65
+ }
66
+ return lies
67
+ }
68
+
69
+ /**
70
+ * What a git spawn in `cwd` prepends so a wrong `core.ignorecase` hides no
71
+ * file: `-c core.ignorecase=false` at or under a directory `ignoreCaseLies`
72
+ * found lying (the disk there tells case apart, so the override is git's
73
+ * own answer for any repository on it), else nothing.
74
+ */
75
+ export function gitCaseArgs(cwd: string): readonly string[] {
76
+ if (caseLies.size === 0) return []
77
+ const at = path.resolve(cwd)
78
+ for (const [dir, lies] of caseLies)
79
+ if (lies && (at === dir || at.startsWith(dir.endsWith(path.sep) ? dir : dir + path.sep)))
80
+ return CASE_SENSITIVE
81
+ return []
82
+ }
83
+
84
+ const CASE_SENSITIVE = ['-c', 'core.ignorecase=false']
85
+
86
+ // git reads each line as a pathspec, so `:top/dist/` was asked as
87
+ // `top/dist/`. `./` keeps it the path it is (this command refuses
88
+ // `:(literal)`), and git echoes each path back as given.
89
+ const stdinOf = (paths: readonly string[]): Buffer =>
90
+ Buffer.from(paths.map((p) => `./${p}\0`).join(''))
91
+
40
92
  function ignoredOf(exitCode: number, stdout: Uint8Array): Set<string> | null {
41
93
  // 0: some ignored; 1: none; anything else (no work tree): undecided.
42
94
  if (exitCode === 1) return new Set()
@@ -45,7 +97,8 @@ function ignoredOf(exitCode: number, stdout: Uint8Array): Set<string> | null {
45
97
  new TextDecoder()
46
98
  .decode(stdout)
47
99
  .split('\0')
48
- .filter((p) => p !== ''),
100
+ .filter((p) => p !== '')
101
+ .map((p) => p.slice(2)),
49
102
  )
50
103
  }
51
104
 
@@ -57,10 +110,10 @@ export async function checkIgnore(
57
110
  if (paths.length === 0) return new Set()
58
111
  try {
59
112
  const proc = Bun.spawn({
60
- cmd: [executablePath('git'), ...CHECK_IGNORE],
113
+ cmd: [executablePath('git'), ...gitCaseArgs(cwd), ...CHECK_IGNORE],
61
114
  env: gitEnv(),
62
115
  cwd,
63
- stdin: Buffer.from(paths.map((p) => `${p}\0`).join('')),
116
+ stdin: stdinOf(paths),
64
117
  stdout: 'pipe',
65
118
  stderr: 'ignore',
66
119
  })
@@ -76,10 +129,10 @@ export function checkIgnoreSync(cwd: string, paths: readonly string[]): Set<stri
76
129
  if (paths.length === 0) return new Set()
77
130
  try {
78
131
  const r = Bun.spawnSync({
79
- cmd: [executablePath('git'), ...CHECK_IGNORE],
132
+ cmd: [executablePath('git'), ...gitCaseArgs(cwd), ...CHECK_IGNORE],
80
133
  env: gitEnv(),
81
134
  cwd,
82
- stdin: Buffer.from(paths.map((p) => `${p}\0`).join('')),
135
+ stdin: stdinOf(paths),
83
136
  stdout: 'pipe',
84
137
  stderr: 'ignore',
85
138
  })
@@ -99,6 +152,7 @@ export function listedSync(cwd: string, dirs: readonly string[]): string[] | nul
99
152
  const r = Bun.spawnSync({
100
153
  cmd: [
101
154
  executablePath('git'),
155
+ ...gitCaseArgs(cwd),
102
156
  'ls-files',
103
157
  '-z',
104
158
  '--cached',
package/src/util/index.ts CHANGED
@@ -75,6 +75,7 @@ export {
75
75
  dieOf,
76
76
  exitClaimedBySignal,
77
77
  killGraceMs,
78
+ POST_EXIT_DRAIN_MS,
78
79
  settleWithin,
79
80
  teardownTimeoutMs,
80
81
  testTimerMs,
@@ -101,18 +102,30 @@ export { gitEnv, helperEnv, hideEnvFromTasks } from './hide-env.js'
101
102
  export {
102
103
  checkIgnore,
103
104
  checkIgnoreSync,
105
+ gitCaseArgs,
106
+ ignoreCaseLies,
104
107
  ignoreProbes,
105
108
  listedSync,
106
109
  wholeIgnored,
107
110
  } from './git-ignored.js'
111
+ export { OWN_DIR_HINT, refuseForeignCacheDir } from './foreign-cache-dir.js'
108
112
  export { realPath, realpathOf } from './real-path.js'
109
- export { IGNORE_ALL, ignoreDir, refuseSymlink, writeNoFollow } from './write-file.js'
113
+ export {
114
+ IGNORE_ALL,
115
+ ignoreDir,
116
+ type PendingWrite,
117
+ refuseSymlink,
118
+ writeAllOrNone,
119
+ writeNoFollow,
120
+ } from './write-file.js'
110
121
  export {
111
122
  heldSecrets,
123
+ holdSecret,
112
124
  maskedCommand,
113
125
  maskedStreams,
114
126
  maskedLine,
115
127
  MASKED,
128
+ maskStreams,
116
129
  secretMask,
117
130
  secretNamed,
118
131
  type SecretMask,
@@ -134,3 +147,4 @@ export {
134
147
  parseDependencySpec,
135
148
  type DependencySpec,
136
149
  } from './dependency-spec.js'
150
+ export { stripEscapes } from './plain-text.js'
package/src/util/paths.ts CHANGED
@@ -306,13 +306,50 @@ function compileAnyTaskGlob(rawPatterns: readonly string[]): (rel: string) => bo
306
306
  if (patterns.length === 0) return () => false
307
307
  const sources = patterns.map(regExpSource)
308
308
  const globs = patterns.map(taskGlob)
309
- if (sources.every((r) => r !== null)) {
310
- const re = new RegExp(`^(?:${sources.join('|')})$`, 'u')
309
+ if (!sources.every((r) => r !== null)) return (rel) => globs.some((g) => g.match(rel))
310
+ // Each `**/` alternative opened with its own prefix, retried at every
311
+ // slash of the path: 1000 `**/fileN.ts` globs took 24 s over 200,000
312
+ // paths. One shared prefix before their alternation takes 50 ms.
313
+ const deep: string[] = []
314
+ const rest: string[] = []
315
+ for (const s of sources as string[]) {
316
+ if (s.startsWith(LEADING_GLOBSTAR)) deep.push(s.slice(LEADING_GLOBSTAR.length))
317
+ else rest.push(s)
318
+ }
319
+ const res = alternations(rest)
320
+ for (let i = 0; i < deep.length; i += ALTERNATIVES_PER_REGEXP) {
321
+ const part = deep.slice(i, i + ALTERNATIVES_PER_REGEXP)
322
+ res.push(new RegExp(`^${LEADING_GLOBSTAR}(?:${part.join('|')})$`, 'u'))
323
+ }
324
+ if (res.length === 1) {
325
+ const re = res[0]!
311
326
  return (rel) => (rel.endsWith('/') ? globs.some((g) => g.match(rel)) : re.test(rel))
312
327
  }
313
- return (rel) => globs.some((g) => g.match(rel))
328
+ return (rel) =>
329
+ rel.endsWith('/') ? globs.some((g) => g.match(rel)) : res.some((re) => re.test(rel))
330
+ }
331
+
332
+ /**
333
+ * Alternatives per compiled RegExp. Bun's engine falls off a cliff past a
334
+ * few thousand: over 200k paths, 2000 literal-file alternatives took 32 ms,
335
+ * 4000 took 482 ms and 8000 took 15 s, so a project listing 4000 files in
336
+ * `inputs.files` paid that on every key. Below the cliff, the cost of
337
+ * splitting is flat.
338
+ */
339
+ const ALTERNATIVES_PER_REGEXP = 1000
340
+
341
+ function alternations(sources: readonly string[]): RegExp[] {
342
+ const out: RegExp[] = []
343
+ for (let i = 0; i < sources.length; i += ALTERNATIVES_PER_REGEXP) {
344
+ const part = sources.slice(i, i + ALTERNATIVES_PER_REGEXP)
345
+ out.push(new RegExp(`^(?:${part.join('|')})$`, 'u'))
346
+ }
347
+ return out
314
348
  }
315
349
 
350
+ /** What {@link regExpSource} writes for a leading `**` segment. */
351
+ const LEADING_GLOBSTAR = '(?:[^]*/)?'
352
+
316
353
  const REGEXP_SYNTAX = /[\\^$.*+?()[\]{}|]/g
317
354
 
318
355
  /**
@@ -336,7 +373,7 @@ function regExpSource(pattern: string): string | null {
336
373
  // more names (`src/**` does not match `src`).
337
374
  if (segments.length === 1) out += '[^]*'
338
375
  else if (last) out += '/[^]+'
339
- else out += i === 0 ? '(?:[^]*/)?' : '/(?:[^]*/)?'
376
+ else out += i === 0 ? LEADING_GLOBSTAR : `/${LEADING_GLOBSTAR}`
340
377
  continue
341
378
  }
342
379
  if (seg === '' || seg === '.' || seg === '..' || seg.includes('**')) return null
@@ -535,9 +572,15 @@ function classEnd(pattern: string, i: number): number {
535
572
  return end === -1 ? i : end
536
573
  }
537
574
 
575
+ // A `\x` is one character to both scans: it opens, closes and cuts nothing,
576
+ // and stays in its alternative for the glob to read. Expansion turned off
577
+ // at any escape, `{src,app/\[id\]}` named neither tree.
538
578
  function expandBraces(pattern: string, slashOnly: boolean): string[] {
539
- if (pattern.includes('\\')) return [pattern]
540
579
  for (let open = 0; open < pattern.length; open++) {
580
+ if (pattern[open] === '\\') {
581
+ open++
582
+ continue
583
+ }
541
584
  if (pattern[open] === '[') {
542
585
  open = classEnd(pattern, open)
543
586
  continue
@@ -548,7 +591,8 @@ function expandBraces(pattern: string, slashOnly: boolean): string[] {
548
591
  let close = -1
549
592
  for (let i = open; i < pattern.length && close === -1; i++) {
550
593
  const c = pattern[i]
551
- if (c === '[') i = classEnd(pattern, i)
594
+ if (c === '\\') i++
595
+ else if (c === '[') i = classEnd(pattern, i)
552
596
  else if (c === '{') depth++
553
597
  else if (c === '}' && --depth === 0) close = i
554
598
  else if (c === ',' && depth === 1) cuts.push(i)
@@ -0,0 +1,19 @@
1
+ // A string escape (OSC, DCS, SOS, PM, APC) through its terminator, or to the
2
+ // line's end when it has none: Bun.stripANSI drops an unterminated one with
3
+ // everything after it, so a stray title escape ate the failure reason
4
+ // printed below it. An ESC that is not ST ends it too and is left for
5
+ // Bun.stripANSI, as a terminal aborts the string there. C1 openers are not
6
+ // looked for: output is decoded UTF-8, where an 8-bit C1 byte reads as
7
+ // U+FFFD, and scanning for them doubled the cost on non-Latin-1 text.
8
+ // eslint-disable-next-line no-control-regex -- ESC is the point
9
+ const STRING_OPENER = /\x1b[\]P_^X]/
10
+ // eslint-disable-next-line no-control-regex -- ESC is the point
11
+ const STRING_ESCAPE = /\x1b[\]P_^X][^\x07\x1b\x9c\n]*(?:\x07|\x1b\\|\x9c)?/g
12
+
13
+ /** `text` without terminal escapes, never losing a line to an unterminated one. */
14
+ export function stripEscapes(text: string): string {
15
+ // Tested first, after the cheaper ESC search: the replace alone cost half
16
+ // again as much on coloured text, the regex alone 3x on plain text.
17
+ if (text.indexOf('\x1b') === -1 || !STRING_OPENER.test(text)) return Bun.stripANSI(text)
18
+ return Bun.stripANSI(text.replace(STRING_ESCAPE, ''))
19
+ }
@@ -36,8 +36,11 @@ export function secretNamed(name: string): boolean {
36
36
  /**
37
37
  * A URL's password (`postgres://app:<pw>@db/app`): a credential whatever
38
38
  * the variable is named, so `DATABASE_URL` and `REDIS_URL` printed it.
39
+ * Split as WHATWG splits it: the userinfo ends at the LAST `@` before the
40
+ * path, query or fragment, and the user name at its first `:`, so either
41
+ * may hold an `@` (`app:p@ss@db` printed `p@ss` whole).
39
42
  */
40
- const URL_PASSWORD = /[a-z][a-z0-9+.-]*:\/\/[^\s:/@]*:([^\s/@]+)@/gi
43
+ const URL_PASSWORD = /[a-z][a-z0-9+.-]*:\/\/[^\s:/?#]*:([^\s/?#]+)@/gi
41
44
 
42
45
  /**
43
46
  * Values shorter than this are not masked: `KEY=1` would mask every `1`
@@ -260,6 +263,59 @@ export interface TaskEnvSecrets {
260
263
  readonly secret?: readonly string[]
261
264
  }
262
265
 
266
+ /**
267
+ * Each stream's run of adjacent non-empty chunks joined, with `mask`
268
+ * applied to each stream's text as a whole: a value is masked where it
269
+ * starts and its rest dropped from the runs it reaches. The live stream
270
+ * masks chunk by chunk and lets a held piece go after a pause, so a value
271
+ * written in two pieces reached the capture in two chunks, and a write to
272
+ * the other stream between them made them two runs. A hit replays each
273
+ * stream to its own descriptor, so `vx run > log` joined them again.
274
+ */
275
+ export function maskStreams(
276
+ chunks: ReadonlyArray<{ text: string; err: boolean }>,
277
+ mask: SecretMask | undefined,
278
+ ): Array<{ text: string; err: boolean }> {
279
+ if (mask === undefined) return chunks.filter((c) => c.text.length > 0)
280
+ const runs: Array<{ text: string; err: boolean }> = []
281
+ for (const c of chunks) {
282
+ if (c.text.length === 0) continue
283
+ const last = runs.at(-1)
284
+ if (last !== undefined && last.err === c.err) last.text += c.text
285
+ else runs.push({ text: c.text, err: c.err })
286
+ }
287
+ for (const err of [false, true]) {
288
+ const own = runs.filter((r) => r.err === err)
289
+ const spans = mask.spans(own.map((r) => r.text).join(''))
290
+ if (spans.length === 0) continue
291
+ let at = 0
292
+ let i = 0
293
+ for (const r of own) {
294
+ const end = at + r.text.length
295
+ let text = ''
296
+ let from = at
297
+ for (; i < spans.length && spans[i]![0] < end; i++) {
298
+ const [s, e] = spans[i]!
299
+ if (s >= from) text += r.text.slice(from - at, s - at) + MASKED
300
+ from = Math.max(from, Math.min(e, end))
301
+ // A value that runs past this run is cut from the next one too.
302
+ if (e > end) break
303
+ }
304
+ r.text = text + r.text.slice(from - at)
305
+ at = end
306
+ }
307
+ }
308
+ // A run emptied whole leaves its neighbours, the other stream, adjacent.
309
+ const kept: typeof runs = []
310
+ for (const r of runs) {
311
+ if (r.text.length === 0) continue
312
+ const last = kept.at(-1)
313
+ if (last !== undefined && last.err === r.err) last.text += r.text
314
+ else kept.push(r)
315
+ }
316
+ return kept
317
+ }
318
+
263
319
  /**
264
320
  * `mask.stream()` driving `emit`: what it holds back (a tail that could
265
321
  * begin a value) is emitted `idleMs` after the last chunk when none
@@ -27,6 +27,22 @@ export function teardownTimeoutMs(): number {
27
27
  return testTimerMs('TEARDOWN_TIMEOUT', DEFAULT_TIMEOUT_MS, true)
28
28
  }
29
29
 
30
+ /**
31
+ * Grace after the direct child exits before we abort the stdout/stderr readers.
32
+ * The pipe reaches EOF only when EVERY write-end fd is closed, so a task that
33
+ * backgrounds a process inheriting fd 1/2 (`server & echo up` — `execWrap`
34
+ * leaves compound commands as `sh -c`, so `sh` exits while the grandchild holds
35
+ * the pipe) never EOFs. On a clean exit with no lingering writer the pipe EOFs
36
+ * at once, so the readers win this race immediately and normal tasks pay
37
+ * nothing; only a stuck reader waits out the grace. Residual buffered output is
38
+ * bounded by the OS pipe buffer (streamToString drains continuously DURING the
39
+ * run), so this is far longer than any real drain needs. What the leftover
40
+ * process writes after the bound is lost, and `POST_EXIT_CUT_LINE`
41
+ * (exec/runner.ts) says so.
42
+ * A runtime probe's drain is bounded the same (cache/inputs.ts).
43
+ */
44
+ export const POST_EXIT_DRAIN_MS = 250
45
+
30
46
  /**
31
47
  * The SIGTERM→SIGKILL grace a run grants a child that ignores SIGTERM:
32
48
  * the one-shot timeout escalation (`exec/runner.ts`),
package/src/util/size.ts CHANGED
@@ -70,10 +70,20 @@ export function formatBytes(n: number): string {
70
70
  return `${v.toFixed(0)} PB`
71
71
  }
72
72
 
73
- /** Bytes under `p` on disk, links not followed; 0 when it is missing. */
73
+ /** Bytes under `p` on disk, links not followed; 0 when missing, and for a directory this user cannot list. */
74
74
  export function treeBytes(p: string): number {
75
75
  const st = lstatSync(p, { throwIfNoEntry: false })
76
76
  if (st === undefined) return 0
77
77
  if (!st.isDirectory()) return st.size
78
- return readdirSync(p).reduce((n, e) => n + treeBytes(path.join(p, e)), 0)
78
+ let names: string[]
79
+ try {
80
+ names = readdirSync(p)
81
+ } catch (err) {
82
+ // A directory this user cannot list (another user's store, a task's
83
+ // 0700 output) is a size nobody here can read, not a failed report.
84
+ const code = (err as NodeJS.ErrnoException).code
85
+ if (code === 'EACCES' || code === 'EPERM') return 0
86
+ throw err
87
+ }
88
+ return names.reduce((n, e) => n + treeBytes(path.join(p, e)), 0)
79
89
  }
@@ -1,14 +1,43 @@
1
- import { lstatSync, readlinkSync, writeFileSync } from 'node:fs'
2
- import { mkdir, open, rename, rm } from 'node:fs/promises'
1
+ import { lstatSync, readlinkSync, realpathSync, writeFileSync } from 'node:fs'
2
+ import { lstat, mkdir, open, rename, rm } from 'node:fs/promises'
3
3
  import path from 'node:path'
4
4
  import { UserError } from './errors.js'
5
5
 
6
6
  /**
7
- * Refuses a `file` that is a symlink: a repo can carry `vx.config.ts ->
8
- * /anywhere`, and a write through it lands outside the workspace. Called
9
- * before the first of a set of writes, so a refusal leaves none half-made.
7
+ * Refuses a `file` whose directory resolves outside `root`: a repo can
8
+ * carry `.vx -> ../anywhere` (or `plugins -> ...`), and checking only the
9
+ * file's own name wrote through it. The nearest directory that exists is
10
+ * asked; one not made yet is made under it.
10
11
  */
11
- export function refuseSymlink(file: string): void {
12
+ function refuseOutside(file: string, root: string): void {
13
+ const top = realpathSync(root)
14
+ for (let at = path.dirname(file); ; at = path.dirname(at)) {
15
+ let real: string
16
+ try {
17
+ real = realpathSync(at)
18
+ } catch {
19
+ // quiet: not made yet (or a dangling link mkdir cannot make through)
20
+ if (at === path.dirname(at)) return
21
+ continue
22
+ }
23
+ const rel = path.relative(top, real)
24
+ if (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
25
+ throw new UserError(
26
+ `refusing to write ${file}: ${at} resolves to ${real}, outside the workspace; remove the link and run again`,
27
+ )
28
+ }
29
+ return
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Refuses a `file` that is a symlink, or whose directory resolves outside
35
+ * `root`: a repo can carry `vx.config.ts -> /anywhere`, and a write
36
+ * through it lands outside the workspace. Called before the first of a
37
+ * set of writes, so a refusal leaves none half-made.
38
+ */
39
+ export function refuseSymlink(file: string, root: string): void {
40
+ refuseOutside(file, root)
12
41
  let link: string
13
42
  try {
14
43
  if (!lstatSync(file).isSymbolicLink()) return
@@ -22,13 +51,61 @@ export function refuseSymlink(file: string): void {
22
51
  )
23
52
  }
24
53
 
54
+ /**
55
+ * A rename over a directory (EISDIR) as a refusal naming `shown`, not a
56
+ * stack trace; any other error as it was.
57
+ */
58
+ function directoryInTheWay(err: unknown, shown: string): unknown {
59
+ return (err as NodeJS.ErrnoException | null)?.code === 'EISDIR'
60
+ ? new UserError(`cannot write ${shown}: a directory stands there; remove it and run again`)
61
+ : err
62
+ }
63
+
64
+ /** A file to write: its path, its bytes, and the name a refusal shows. */
65
+ export interface PendingWrite {
66
+ file: string
67
+ text: string
68
+ shown: string
69
+ }
70
+
71
+ /**
72
+ * Writes each file to a temp beside it, then renames them all over their
73
+ * names: a reader never sees half a file, and a refusal (one outside
74
+ * `root`, as `refuseSymlink`'s) leaves every file
75
+ * as it was, not one new beside one old (`vx lock` said it wrote nothing
76
+ * while the config lock held the edit). A refusal names the file the user
77
+ * knows, never its temp.
78
+ */
79
+ export async function writeAllOrNone(files: readonly PendingWrite[], root: string): Promise<void> {
80
+ for (const f of files) refuseOutside(f.file, root)
81
+ const tag = `.tmp-${process.pid}-${Math.random().toString(36).slice(2)}`
82
+ let at = 0
83
+ try {
84
+ for (; at < files.length; at++) await Bun.write(files[at]!.file + tag, files[at]!.text)
85
+ // A rename over a directory fails only after the ones before it landed.
86
+ for (at = 0; at < files.length; at++)
87
+ if ((await lstat(files[at]!.file).catch(() => null))?.isDirectory())
88
+ throw Object.assign(new Error('directory'), { code: 'EISDIR' })
89
+ for (at = 0; at < files.length; at++) await rename(files[at]!.file + tag, files[at]!.file)
90
+ } catch (err) {
91
+ // Best effort: where the write was refused the temp cannot be named
92
+ // either (ENOTDIR), and that must not replace the refusal itself.
93
+ await Promise.all(files.map((f) => rm(f.file + tag, { force: true }).catch(() => {})))
94
+ const f = files[Math.min(at, files.length - 1)]!
95
+ const e = err as NodeJS.ErrnoException
96
+ if (typeof e.message === 'string') e.message = e.message.split(f.file + tag).join(f.file)
97
+ if (e.path === f.file + tag) e.path = f.file
98
+ throw directoryInTheWay(err, f.shown)
99
+ }
100
+ }
101
+
25
102
  /**
26
103
  * Writes `text` to `file`, never through a link: a temp beside it, made
27
104
  * with O_EXCL, renamed over it. A rename replaces a link put there after
28
- * the check, never its target.
105
+ * the check, never its target. Refused outside `root` (`refuseSymlink`).
29
106
  */
30
- export async function writeNoFollow(file: string, text: string): Promise<void> {
31
- refuseSymlink(file)
107
+ export async function writeNoFollow(file: string, text: string, root: string): Promise<void> {
108
+ refuseSymlink(file, root)
32
109
  await mkdir(path.dirname(file), { recursive: true })
33
110
  const tmp = `${file}.tmp-${process.pid}-${Math.random().toString(36).slice(2)}`
34
111
  try {