-
Notifications
You must be signed in to change notification settings - Fork 48
fix(agent): tell users to clear a broken npx cache instead of "API Error" #1200
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
Draft
posthog
wants to merge
7
commits into
main
Choose a base branch
from
posthog-self-driving/fixagent-stop-filing-broken-npx-ba5349
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d2041fb
fix(agent): tell users to clear a broken npx cache instead of "API Er…
posthog[bot] 1f1232f
docs(errors): document PHW_AGENT_MODULE_MISSING in the error catalog
posthog[bot] 2b5867a
fix(agent): classify module-not-found before the rate-limit substring…
posthog[bot] 8059314
fix(errors): stop naming the default flow in the npx recovery message
posthog[bot] 5174d88
fix(agent): route orchestrator module-missing results to the recovery…
posthog[bot] 64d81be
fix(errors): keep an npx cache path with a space in it whole
posthog[bot] acc76be
fix(errors): label the Windows cache removal command per shell
posthog[bot] 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { | ||
| formatModuleMissingMessage, | ||
| isModuleNotFoundError, | ||
| } from '../module-missing'; | ||
|
|
||
| describe('isModuleNotFoundError', () => { | ||
| it('recognises the Node error code', () => { | ||
| const err = Object.assign(new Error('boom'), { | ||
| code: 'ERR_MODULE_NOT_FOUND', | ||
| }); | ||
| expect(isModuleNotFoundError(err)).toBe(true); | ||
| }); | ||
|
|
||
| // A dynamic import rethrown across a boundary keeps the text, not the code. | ||
| it('recognises the message alone', () => { | ||
| expect( | ||
| isModuleNotFoundError( | ||
| new Error( | ||
| "Cannot find package '/Users/a/.npm/_npx/9f2/node_modules/chalk/index.js'", | ||
| ), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('leaves an ordinary API failure alone', () => { | ||
| expect(isModuleNotFoundError(new Error('502 Bad Gateway'))).toBe(false); | ||
| }); | ||
|
|
||
| // An npx cache hash is 16 hex characters, so it can contain "429" — which the | ||
| // harness's rate-limit substring test would otherwise claim first. | ||
| it('recognises a cache hash that looks like a rate limit', () => { | ||
| expect( | ||
| isModuleNotFoundError( | ||
| new Error( | ||
| "Cannot find package '/Users/a/.npm/_npx/429abc0d15e7f318/node_modules/chalk/index.js'", | ||
| ), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatModuleMissingMessage', () => { | ||
| it('names the exact download to delete', () => { | ||
| const message = formatModuleMissingMessage( | ||
| "Cannot find package '/Users/a/.npm/_npx/9f2/node_modules/chalk/index.js'", | ||
| ); | ||
| expect(message).toContain('rm -rf "/Users/a/.npm/_npx/9f2"'); | ||
| }); | ||
|
|
||
| // A Windows profile is routinely `C:\Users\John Smith`, and a POSIX home can | ||
| // hold a space too. Capturing from the last space left a relative path, and | ||
| // `rm -rf` on a relative path matches nothing and exits 0 — the user believes | ||
| // the cache is clear, reruns, and hits the identical failure. | ||
| it('keeps a POSIX cache path that contains a space whole', () => { | ||
| expect( | ||
| formatModuleMissingMessage( | ||
| "Cannot find package 'chalk' imported from /Users/First Last/.npm/_npx/9f2abc1234567890/node_modules/pi/dist/index.js", | ||
| ), | ||
| ).toContain('rm -rf "/Users/First Last/.npm/_npx/9f2abc1234567890"'); | ||
| }); | ||
|
|
||
| it('keeps a Windows cache path that contains a space whole', () => { | ||
| expect( | ||
| formatModuleMissingMessage( | ||
| "Cannot find package 'chalk' imported from C:\\Users\\John Smith\\AppData\\Local\\npm-cache\\_npx\\abc1234567890def\\node_modules\\x.js", | ||
| ), | ||
| ).toContain( | ||
| 'rm -rf "C:\\Users\\John Smith\\AppData\\Local\\npm-cache\\_npx\\abc1234567890def"', | ||
| ); | ||
| }); | ||
|
|
||
| // Half a path is worse than none, so a fragment with no root is not printed. | ||
| it('falls back rather than naming a path it cannot root', () => { | ||
| const message = formatModuleMissingMessage( | ||
| "Cannot find module 'foo/_npx/9f2/node_modules/x'", | ||
| ); | ||
| expect(message).not.toContain('rm -rf "foo/_npx/9f2"'); | ||
| expect(message).toMatch(/rm -rf "\/[^"]*_npx"/); | ||
| }); | ||
|
|
||
| // Every wizard command reaches this message, so it must not name one. | ||
| it('asks for the same command again rather than the default flow', () => { | ||
| const message = formatModuleMissingMessage('Cannot find module x'); | ||
| expect(message).toContain('run the same wizard command again'); | ||
| expect(message).not.toContain('npx @posthog/wizard@latest'); | ||
| }); | ||
|
|
||
| it('falls back to the whole npx cache', () => { | ||
| expect(formatModuleMissingMessage('Cannot find module x')).toContain( | ||
| '_npx', | ||
| ); | ||
| }); | ||
|
|
||
| // `process.platform` is the OS, not the shell: a Windows user may be at a | ||
| // Command Prompt, where `Remove-Item` is not a command at all. | ||
| describe('on Windows', () => { | ||
| const originalPlatform = process.platform; | ||
|
|
||
| beforeEach(() => { | ||
| Object.defineProperty(process, 'platform', { | ||
| value: 'win32', | ||
| writable: true, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(process, 'platform', { | ||
| value: originalPlatform, | ||
| writable: true, | ||
| }); | ||
| }); | ||
|
|
||
| it('labels a removal command for each shell', () => { | ||
| const dir = | ||
| 'C:\\Users\\John Smith\\AppData\\Local\\npm-cache\\_npx\\abc1234567890def'; | ||
| const message = formatModuleMissingMessage( | ||
| `Cannot find package 'chalk' imported from ${dir}\\node_modules\\x.js`, | ||
| ); | ||
| expect(message).toContain( | ||
| `PowerShell: Remove-Item -Recurse -Force "${dir}"`, | ||
| ); | ||
| expect(message).toContain(`Command Prompt: rmdir /s /q "${dir}"`); | ||
| }); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.