Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

dsh/artifacts: cover the depth and entry bounds and unstat-able entries

dee28500a638ca2
diff --git a/cli/src/dsh/artifacts.spec.ts b/cli/src/dsh/artifacts.spec.ts
index 2c1ac7411..84dacf593 100644
--- a/cli/src/dsh/artifacts.spec.ts
+++ b/cli/src/dsh/artifacts.spec.ts
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
-import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
+import { mkdirSync, mkdtempSync, rmSync, symlinkSync, utimesSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { isCandidateArtifact, newestArtifact } from './artifacts.js'
@@ -30,6 +30,35 @@ describe('newestArtifact', () => {
expect(newestArtifact(workspace, ['.glb'])).toBeNull()
expect(newestArtifact(workspace, [])).toBeNull()
})
+
+ it('walks six directories deep and no deeper', () => {
+ touch('1/2/3/4/5/6/deep.step', 1_000)
+ touch('1/2/3/4/5/6/7/deeper.step', 9_000)
+ expect(newestArtifact(workspace, ['.step'])?.path).toBe('1/2/3/4/5/6/deep.step')
+ })
+
+ it('stops looking once it has seen its bound of entries, directories included', () => {
+ touch('only/model.step', 1_000)
+ // the directory is the one entry the bound allows; its contents are past it
+ expect(newestArtifact(workspace, ['.step'], 0)).toBeNull()
+ expect(newestArtifact(workspace, ['.step'], 1)?.path).toBe('only/model.step')
+ rmSync(join(workspace, 'only'), { recursive: true })
+ touch('a.txt', 1_000)
+ touch('b.txt', 1_000)
+ touch('c.step', 1_000)
+ // three files and a bound of one: two are looked at and the walk stops before the third; a bound
+ // of two sees all three
+ expect(newestArtifact(workspace, ['.step', '.txt'], 1)).not.toBeNull()
+ expect(newestArtifact(workspace, ['.step'], 2)?.path).toBe('c.step')
+ })
+
+ it('skips an entry it cannot stat, such as a dangling link, and a file with no extension', () => {
+ symlinkSync(join(workspace, 'nowhere.step'), join(workspace, 'dangling.step'))
+ touch('Makefile', 5_000)
+ touch('real.step', 1_000)
+ expect(newestArtifact(workspace, ['.step'])?.path).toBe('real.step')
+ expect(newestArtifact(join(workspace, 'missing'), ['.step'])).toBeNull()
+ })
})
describe('isCandidateArtifact', () => {
diff --git a/cli/src/dsh/artifacts.ts b/cli/src/dsh/artifacts.ts
index 55b957c31..4f304c910 100644
--- a/cli/src/dsh/artifacts.ts
+++ b/cli/src/dsh/artifacts.ts
@@ -26,13 +26,14 @@ export interface ArtifactHit {
mtimeMs: number
}
-export function newestArtifact(workspace: string, extensions: readonly string[]): ArtifactHit | null {
+/** `maxFiles` bounds how many directory entries are looked at; a test seam, the default in production. */
+export function newestArtifact(workspace: string, extensions: readonly string[], maxFiles = MAX_FILES): ArtifactHit | null {
if (!extensions.length) return null
const wanted = new Set(extensions.map((ext) => ext.toLowerCase()))
let best: ArtifactHit | null = null
let seen = 0
const walk = (dir: string, depth: number): void => {
- if (depth > MAX_DEPTH || seen > MAX_FILES) return
+ if (depth > MAX_DEPTH || seen > maxFiles) return
let names: string[]
try {
names = readdirSync(dir)
@@ -40,7 +41,7 @@ export function newestArtifact(workspace: string, extensions: readonly string[])
return
}
for (const name of names) {
- if (seen++ > MAX_FILES) return
+ if (seen++ > maxFiles) return
const path = join(dir, name)
let st: ReturnType<typeof statSync>
try {