Repository navigation
fix(ai): runTool's dbg was out of scope — the auto-preview never opened - #93
Merged
Merged
Conversation
runTool logs through a bare dbg(...) in three places, but dbg was only ever
declared inside runAgent — a different function. Each call was a ReferenceError
the moment its line ran:
preview.detected run_command's stdout handler. It threw BEFORE
ctx.openPreview(url) was reached, so the built-in browser
never opened when the agent started a dev server — and the
exception escaped into a live stream handler.
preview.rejected the .catch on that same openPreview promise.
bg.error the catch of the fire-and-forget background launcher, where
it turned a HANDLED start failure into an unhandled rejection.
The fix is one line: runTool resolves its own logger from ctx.dbg, exactly as
runAgent does. runAgent already hands its ctx straight through, so both log to
the one sink the host passes. The call sites are untouched.
The two preview calls arrived with auto-preview (b438b6c, 738e85f); bg.error is
older, out of scope since background commands landed (3fc2eb9). Every release
from v0.9.2 to v1.2.0 carries all three, and this path is openPreview's only
caller, so no shipped build can have opened a tab — this is the change that
turns auto-preview on. Nothing caught it because an out-of-scope name is
invisible to `node --check` and to the source-regex tests, there is no
type-check gate, and no test executed this path.
test/agentRunCommand.test.js now does. It runs the real
runAgent -> runTool -> run_command -> runCommand -> onChunk chain with vscode,
the provider and child_process.spawn replaced (no process is started), and
covers all three call sites plus a host that passes no dbg. runTool is not
exported, so going in through runAgent also proves the logger the host passes
is the one runTool ends up with.
Verified: 41 suites, 0 failures. On the old code the new test fails with
"dbg is not defined" at onChunk (agent.js:444). Mutation-checked: breaking each
call site alone, dropping the no-op fallback, or swapping in a private no-op
each fails its own case. tsc --checkJs over extension.js reported exactly three
TS2304 (these calls) before and none after; nothing else in its output moved.
Not verified: the built editor. The last hop — simpleBrowser.api.open actually
showing the tab — needs a packaged app and a live agent run.
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The targeted fix is correct and its affected runtime paths are comprehensively tested.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes auto-preview failures by making runTool use the host-provided debug logger.
Changes:
- Resolves
dbgwithinrunTool, with a no-op fallback. - Adds execution tests covering preview and background-command error paths.
| File | Description |
|---|---|
extensions/levelcode-ai/agent.js |
Fixes the out-of-scope logger reference. |
extensions/levelcode-ai/test/agentRunCommand.test.js |
Tests preview opening, failures, and optional logging. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When the agent starts a dev server, the built-in browser is supposed to open on its own. It doesn't: the handler that detects the address throws one statement before it would open the tab.
runToollogs through a baredbg(...), butdbgwas only ever declared insiderunAgent— a different function. An out-of-scope name is not an error until the line runs, so nothing failed until a background command printed a local address:The three call sites
preview.detectedrun_command's stdout handleropenPreview, and escaped into a live stream handlerpreview.rejected.catchon thatopenPreviewpromisebg.errorcatchof the fire-and-forget background launcherThe two
preview.*calls arrived with auto-preview (b438b6c, 738e85f).bg.erroris older — out of scope since background commands landed (3fc2eb9).The fix
One line at the top of
runTool, matchingrunAgent:runAgentalready hands itsctxstraight through (runTool(tu, ctx)) and the host putsdbgon that ctx, so both functions now log to the one sink. The call sites are untouched.The test
test/agentRunCommand.test.jsis the first test that executes this path. The neighbouring agent tests assert from source, and an out-of-scope name is invisible to a regex and tonode --check— only running the line finds it.It drives the real
runAgent → runTool → run_command → runCommand → onChunkchain with three things replaced:vscode(asworkspacePaths.test.jsdoes), the provider (two scripted turns), andchild_process.spawn(a fake child the test feeds stdout into — no process is started).runToolisn't exported, and going in throughrunAgentis the point anyway: it proves the logger the host passes is the onerunToolends up with. No export was added for the test.Four cases: the reported path, the
.catchsite, thebg.errorsite, and a host that passes nodbg. Thebg.errorcase uses fault injection (a stop registry whosesetthrows):runCommandresolves even whenspawnitself throws, so thatcatchis purely defensive and there is no natural way into it.Verification
node --check extensions/levelcode-ai/agent.jspasses.ReferenceError: dbg is not definedatonChunk (agent.js:444), throughcapfrom the stdoutdatahandler.|| (() => {})fallback fails the no-logger case; a private no-op that ignoresctx.dbgfails the logging assertion.tsc --checkJsoverextension.jsreported exactly threeTS2304— these calls — before, and none after. Nothing else in its output moved: the remaining 104 diagnostics are the pre-existing ones (missingvscode/Node types, property-shape complaints).openPreviewis never called and theReferenceErrorsurfaces from the stream handler; on the new code it is called withhttp://localhost:5173/, with the address split across two real stdout chunks.Worth knowing before merging
This turns auto-preview on for the first time. Every release from v0.9.2 to v1.2.0 carries all three out-of-scope calls, and this path is
openPreview's only caller, so no shipped build can have opened a tab. After this, the host'sopenPreviewruns in a real session for the first time; the intent is a Simple Browser tab beside the chat whenever the agent starts a server in the background.levelcode.ai.preview.autoOpen(defaulttrue) turns it off.Not covered by CI, and not run here: the built editor. The last hop —
simpleBrowser.api.openactually showing the tab — needs a packaged app and a live agent run, the same gap #35 called out. Worth one manual check before the next release.The same static check over every
.jsfile inextensions/,tools/andscripts/finds no other undefined name; what is left is Node globals without@types/node.