-
Notifications
You must be signed in to change notification settings - Fork 0
feat(routes): publish routes.d.ts through Effect FileSystem with an ensuringRemoved staging file (FileSystem phase 1, module 3) #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agent-bundle": patch | ||
| --- | ||
|
|
||
| Publish `.agent-bundle/routes.d.ts` during project preparation (`agent-bundle build`, `agent-bundle dev`, and every API call that prepares a project) through Effect `FileSystem`: the staging file is removed on every exit path, including interruption, while the generated declarations, the atomic rename, and thrown Node errors are unchanged. (#520) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
packages/agent-bundle/tests/route-typegen-write.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { access, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| import { Effect, FileSystem, PlatformError } from 'effect'; | ||
| import { afterEach, describe, expect, it } from '@rstest/core'; | ||
|
|
||
| import { runWithPlatform } from '../src/effect/platform.ts'; | ||
| import { emptyCompiledRouteGraph } from '../src/routes/graph.ts'; | ||
| import { generateRouteTypes, routeTypesRelativePath, writeRouteTypes, writeRouteTypesProgram } from '../src/routes/typegen.ts'; | ||
| import type { CompiledRouteGraph } from '../src/routes/types.ts'; | ||
|
|
||
| /** | ||
| * `writeRouteTypes` publishes `.agent-bundle/routes.d.ts` with a | ||
| * same-directory rename and never leaves its temporary file behind. The | ||
| * real-filesystem cases pin the published bytes and the cleanup; the | ||
| * `FileSystem.layerNoop` cases pin the call protocol around a failing | ||
| * rename, where the temporary must still be removed and the caller must | ||
| * still see the Node error. | ||
| */ | ||
| const roots: string[] = []; | ||
| afterEach(async () => { | ||
| await Promise.all(roots.splice(0).map((root) => rm(root, { force: true, recursive: true }))); | ||
| }); | ||
|
|
||
| const scratchRoot = async (): Promise<string> => { | ||
| const root = await mkdtemp(join(tmpdir(), 'agent-bundle-typegen-')); | ||
| roots.push(root); | ||
| return root; | ||
| }; | ||
|
|
||
| const oneRouteGraph = (root: string): CompiledRouteGraph => ({ | ||
| ...emptyCompiledRouteGraph, | ||
| servers: [{ | ||
| id: 'mcp:curator', | ||
| mode: 'generated', | ||
| name: 'curator', | ||
| routes: [{ | ||
| config: {}, | ||
| id: 'tool:curator/inspect', | ||
| kind: 'tool', | ||
| provenance: { kind: 'conventional', relativePath: 'src/mcp/curator/tools/inspect.tsx' }, | ||
| serverId: 'mcp:curator', | ||
| source: join(root, 'src/mcp/curator/tools/inspect.tsx'), | ||
| }], | ||
| }], | ||
| }); | ||
|
|
||
| describe('writeRouteTypes', () => { | ||
| it('publishes the generated declarations and leaves no temporary file', async () => { | ||
| const root = await scratchRoot(); | ||
| const graph = oneRouteGraph(root); | ||
| await expect(writeRouteTypes(root, graph)).resolves.toBe(routeTypesRelativePath); | ||
| expect(await readFile(join(root, routeTypesRelativePath), 'utf8')).toBe(generateRouteTypes(graph)); | ||
| expect(await readdir(join(root, '.agent-bundle'))).toEqual(['routes.d.ts']); | ||
| }); | ||
|
|
||
| it('removes a stale declaration file when the graph has nothing to type', async () => { | ||
| const root = await scratchRoot(); | ||
| await writeRouteTypes(root, oneRouteGraph(root)); | ||
| await expect(writeRouteTypes(root, emptyCompiledRouteGraph)).resolves.toBe(routeTypesRelativePath); | ||
| await expect(access(join(root, routeTypesRelativePath))).rejects.toMatchObject({ code: 'ENOENT' }); | ||
| // And again when there is nothing to remove. | ||
| await expect(writeRouteTypes(root, emptyCompiledRouteGraph)).resolves.toBe(routeTypesRelativePath); | ||
| }); | ||
|
|
||
| it('rejects with the Node error when the declarations directory cannot be created', async () => { | ||
| const root = await scratchRoot(); | ||
| await writeFile(join(root, '.agent-bundle'), 'not a directory'); | ||
| await expect(writeRouteTypes(root, oneRouteGraph(root))).rejects.toMatchObject({ | ||
| code: expect.stringMatching(/^(EEXIST|ENOTDIR)$/u), | ||
| syscall: 'mkdir', | ||
| }); | ||
| }); | ||
|
|
||
| describe('over FileSystem.layerNoop', () => { | ||
| const exdev: NodeJS.ErrnoException = new Error('EXDEV: cross-device link not permitted, rename'); | ||
| exdev.code = 'EXDEV'; | ||
|
|
||
| const recordingFileSystem = () => { | ||
| const calls: string[] = []; | ||
| const written = new Map<string, string>(); | ||
| const layer = FileSystem.layerNoop({ | ||
| makeDirectory: (path) => Effect.sync(() => { calls.push(`makeDirectory ${path}`); }), | ||
| remove: (path, options) => Effect.sync(() => { | ||
| calls.push(`remove ${path} force=${String(options?.force ?? false)} recursive=${String(options?.recursive ?? false)}`); | ||
| }), | ||
| rename: (from, to) => Effect.suspend(() => { | ||
| calls.push(`rename ${from} -> ${to}`); | ||
| return Effect.fail(PlatformError.systemError({ | ||
| _tag: 'Unknown', | ||
| cause: exdev, | ||
| method: 'rename', | ||
| module: 'FileSystem', | ||
| pathOrDescriptor: from, | ||
| })); | ||
| }), | ||
| writeFileString: (path, data) => Effect.sync(() => { | ||
| calls.push(`writeFile ${path}`); | ||
| written.set(path, data); | ||
| }), | ||
| }); | ||
| return { calls, layer, written }; | ||
| }; | ||
|
|
||
| it('removes the temporary file and rethrows the Node error when the rename fails', async () => { | ||
| const root = '/virtual/project'; | ||
| const graph = oneRouteGraph(root); | ||
| const { calls, layer, written } = recordingFileSystem(); | ||
| await expect(runWithPlatform(writeRouteTypesProgram(root, graph).pipe(Effect.provide(layer)))).rejects.toBe(exdev); | ||
|
|
||
| const output = join(root, routeTypesRelativePath); | ||
| expect(calls[0]).toBe(`makeDirectory ${join(root, '.agent-bundle')}`); | ||
| const temporary = calls[1]?.replace(/^writeFile /u, ''); | ||
| expect(temporary).toMatch(new RegExp(`^${output.replaceAll('.', '\\.')}\\.${String(process.pid)}\\.[0-9a-f-]{36}\\.tmp$`, 'u')); | ||
| expect(calls.slice(2)).toEqual([ | ||
| `rename ${temporary} -> ${output}`, | ||
| `remove ${temporary} force=true recursive=true`, | ||
| ]); | ||
| expect(written.get(temporary!)).toBe(generateRouteTypes(graph)); | ||
| }); | ||
|
|
||
| it('removes the declaration file, and nothing else, for an empty graph', async () => { | ||
| const root = '/virtual/project'; | ||
| const { calls, layer } = recordingFileSystem(); | ||
| await expect(runWithPlatform(writeRouteTypesProgram(root, emptyCompiledRouteGraph).pipe(Effect.provide(layer)))) | ||
| .resolves.toBe(routeTypesRelativePath); | ||
| expect(calls).toEqual([`remove ${join(root, routeTypesRelativePath)} force=true recursive=false`]); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new helper repeats the
Effect.exit/fs.removefinalization that remains inline inwithTempDirectoryat lines 102–108, even thoughdocs/effect-conventions.mdnow saysensuringRemovedis the bracket behind that function. Any future fix to cleanup, interruption, or error precedence can therefore update one path without the other—the exact divergence this repository's extract-and-rewire rule is intended to prevent. Factor the shared bracket sowithTempDirectorydelegates to it while still creating the directory inside the outer interruption mask.AGENTS.md reference: AGENTS.md:L5-L10
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 4502442:
withTempDirectorynow creates the directory inside its mask and delegates toensuringRemoved(directory, restore(use(directory)))— one bracket implementation. The inner mask is a no-op inside the outer one, so the operation is restored to the caller's interruptibility before it enters the bracket. Added a test that an externalFiber.interruptreaches an operation parked onEffect.neverand the directory is gone afterwards; it fails (2 s timeout) if therestoreis dropped, which is the mistake this delegation could otherwise hide.