Skip to content

Bug Bash #1: Fix block execution, ANSI errors, cls scrollback, remove IPC timeout#592

Merged
tnaum-ms merged 9 commits intorel/release_0.8.0from
db/tnaum/bug-bash-1-fixes
Apr 20, 2026
Merged

Bug Bash #1: Fix block execution, ANSI errors, cls scrollback, remove IPC timeout#592
tnaum-ms merged 9 commits intorel/release_0.8.0from
db/tnaum/bug-bash-1-fixes

Conversation

@tnaum-ms
Copy link
Copy Markdown
Collaborator

@tnaum-ms tnaum-ms commented Apr 20, 2026

Bug Bash #1 — Fixes

Four issues fixed, one closed as not reproduced.


1. use/show in multi-line blocks silently drops subsequent statements

Problem: When a playground block starts with use mydb followed by a query, only the use executes. The query is silently discarded because the evaluation pipeline treats the entire multi-line input as arguments to the use handler.

Fix: Normalize bare use <name> and show <name> into function-call form in multi-line input before evaluation. Single-line inputs are unchanged.

  • DocumentDBShellRuntime.ts: Added normalizeDirectCommands()
  • DocumentDBShellRuntime.test.ts: 13 new test cases

Credit: Jade Welton


2. ANSI escape codes visible in playground error messages

Problem: Errors contain ANSI color codes that appear as raw escape characters in the playground output panel.

Fix: Added stripAnsi() in resultFormatter.ts.

  • resultFormatter.ts: Added stripAnsi(), applied in formatError()
  • resultFormatter.test.ts: New test file with test cases

Credit: Marcelo Fonseca


3. cls/clear does not clear scrollback buffer

Problem: cls/clear clears the visible screen but leaves scrollback history.

Fix: Added \x1b[3J (Erase Scrollback) to the clear escape sequence.

  • DocumentDBShellPty.ts: \x1b[2J\x1b[H\x1b[2J\x1b[3J\x1b[H
  • DocumentDBShellPty.test.ts: Updated assertion

Credit: Marcelo Fonseca


4. Remove IPC timeout for query execution

Problem: The documentDB.timeout setting controlled an IPC-level setTimeout that killed the worker thread after 30s — ignoring user-specified .maxTimeMS() values. The timeout was applied at the wrong layer (IPC, not query).

Fix: Removed the documentDB.timeout setting and all IPC-level timeout logic for eval messages entirely. Users control query execution time via:

  • .maxTimeMS() in playground/shell code
  • Cancel button (playground) or Ctrl+C (shell) for immediate abort
  • Init timeout (documentDB.shell.initTimeout) is unchanged

Additionally:

  • Error code 50 (MaxTimeMSExpired / ExceededTimeLimit) is now passed through worker IPC via a new code field on the evalError message
  • Timeout errors are detected by error code (not message regex) and shown with a .maxTimeMS() tip in both playground and shell

Changes

  • package.json: Removed documentDB.timeout setting
  • extensionVariables.ts: Removed setting key
  • workerTypes.ts: Added code to evalError message
  • playgroundWorker.ts: Extracts and sends error code
  • WorkerSessionManager.ts: Timeout optional (init/shutdown only), reconstructs error code
  • PlaygroundEvaluator.ts: No longer reads/passes timeout
  • ShellSessionManager.ts: Removed timeout from evaluate() signature
  • DocumentDBShellPty.ts: Removed getShellTimeoutMs(), added error code 50 detection
  • resultFormatter.ts: Added isMaxTimeMSError() using error code 50

Credit: Marcelo Fonseca


Not reproduced

  • "Run All" fails for valid script — Could not reproduce. Closed as false alarm.

… statements

When a playground block starts with a bare direct command like 'use mydb'
followed by a query on the next line, only the 'use' is executed and the
query is silently discarded. This happens because the evaluation pipeline
detects 'use' as a special command token by splitting on all whitespace
(including newlines), consuming the entire multi-line input as arguments
to the 'use' handler.

Fix: normalize bare 'use <name>' and 'show <name>' into function-call
form ('use("name")' / 'show("name")') before evaluation when the
input contains multiple lines. The function-call form bypasses the
direct command short-circuit and lets the full block be evaluated
normally. Single-line inputs are left unchanged.
@tnaum-ms tnaum-ms requested a review from a team as a code owner April 20, 2026 09:50
Bug Bash #1 — two additional fixes:

1. Playground error messages contained raw ANSI escape sequences (color
   codes, bold, reset) that rendered as visible control characters in the
   read-only output panel. Added stripAnsi() in resultFormatter.ts to
   remove SGR sequences before display. The interactive shell is unaffected
   (it renders ANSI codes via the PTY).

2. The cls/clear command in the interactive shell cleared the visible
   screen but left the scrollback buffer intact — users could scroll up
   to see previous output. Added the \x1b[3J (Erase Scrollback) escape
   sequence to match standard terminal behavior.
@tnaum-ms tnaum-ms changed the title Fix: bare use/show in multi-line playground blocks silently drops subsequent statements Bug Bash #1: Fix use/show block execution, ANSI codes in errors, cls scrollback Apr 20, 2026
@tnaum-ms tnaum-ms changed the title Bug Bash #1: Fix use/show block execution, ANSI codes in errors, cls scrollback Bug Bash #1: Fix block execution, ANSI errors, cls scrollback, replace timeout with maxTimeMS Apr 20, 2026
@tnaum-ms tnaum-ms force-pushed the db/tnaum/bug-bash-1-fixes branch from 1db925f to a704078 Compare April 20, 2026 13:35
The documentDB.timeout setting controlled a setTimeout that killed the
worker thread after 30s, ignoring user-specified .maxTimeMS() values.

Changes:
- Removed the documentDB.timeout setting entirely
- Removed all IPC-level setTimeout logic for eval messages in
  WorkerSessionManager — the user cancels via Cancel button or Ctrl+C
- Init timeout (documentDB.shell.initTimeout) is unchanged
- MaxTimeMSExpired errors from the DocumentDB API are now detected in
  both playground and shell, with a tip to use .maxTimeMS()
@tnaum-ms tnaum-ms force-pushed the db/tnaum/bug-bash-1-fixes branch from a704078 to 6acebbb Compare April 20, 2026 14:24
@tnaum-ms tnaum-ms changed the title Bug Bash #1: Fix block execution, ANSI errors, cls scrollback, replace timeout with maxTimeMS Bug Bash #1: Fix block execution, ANSI errors, cls scrollback, remove IPC timeout Apr 20, 2026
@tnaum-ms tnaum-ms requested a review from Copilot April 20, 2026 14:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes several Query Playground and Interactive Shell “Bug Bash #1” issues by normalizing multi-line use/show handling, improving error output readability, making terminal clear fully clear scrollback, and removing the IPC-level eval timeout in favor of query-level timeouts/cancellation.

Changes:

  • Normalize bare use <db> / show <arg> in multi-line blocks to avoid @mongosh direct-command short-circuiting.
  • Strip ANSI SGR sequences from playground error output and surface a .maxTimeMS() tip for DB timeout errors (code 50) in both shell and playground.
  • Remove the documentDB.timeout setting and IPC eval timeout plumbing; keep init timeout behavior; clear terminal scrollback on cls/clear.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/extensionVariables.ts Removes the documentDB.timeout settings key constant.
src/documentdb/shell/ShellSessionManager.ts Drops eval-timeout parameter from evaluate() and worker calls.
src/documentdb/shell/DocumentDBShellPty.ts Removes shell timeout usage, adds code-50 timeout tip, and clears scrollback on clear.
src/documentdb/shell/DocumentDBShellPty.test.ts Updates mocks/assertions to match new evaluate() signature and clear sequence.
src/documentdb/playground/workerTypes.ts Adds optional code to evalError IPC messages.
src/documentdb/playground/resultFormatter.ts Adds ANSI stripping for error output and code-50 timeout tip.
src/documentdb/playground/resultFormatter.test.ts Adds unit tests for ANSI stripping and code-50 hint behavior.
src/documentdb/playground/playgroundWorker.ts Extracts numeric error code and includes it in evalError messages.
src/documentdb/playground/WorkerSessionManager.ts Removes eval IPC timeout; makes timeouts optional for init/shutdown; reconstructs error code.
src/documentdb/playground/PlaygroundEvaluator.ts Stops reading/passing documentDB.timeout; updates VS Code import to type-only.
packages/documentdb-shell-runtime/src/index.ts Re-exports normalizeDirectCommands from the package entrypoint.
packages/documentdb-shell-runtime/src/DocumentDBShellRuntime.ts Adds normalizeDirectCommands() and applies it before evaluation.
packages/documentdb-shell-runtime/src/DocumentDBShellRuntime.test.ts Adds tests for normalization behavior.
package.json Removes the documentDB.timeout contributes.configuration entry.
l10n/bundle.l10n.json Adds localized string for the .maxTimeMS() tip.

Comment thread packages/documentdb-shell-runtime/src/index.ts Outdated
Comment thread package.json
Comment thread packages/documentdb-shell-runtime/src/DocumentDBShellRuntime.ts Outdated
Comment thread packages/documentdb-shell-runtime/src/DocumentDBShellRuntime.ts Outdated
The previous global regex rewrote any matching `use <name>` / `show <name>`
line anywhere in the multi-line input, including text inside template
literals, multi-line strings, and block comments. That changed program
semantics for any block that happened to contain the pattern in a non-code
region.

Scope the rewrite to the first non-empty statement line only — that is
the single observed failure mode (bare `use`/`show` as the first token
of a multi-line block short-circuits the evaluator and silently drops the
rest of the block).

Addresses PR #592 review comment (discussion r3111470977).
`normalizeDirectCommands()` rewrote `use <name>` to `use("<name>")`
without a trailing semicolon. If the next line began with `[`, `(`,
`+`, `-`, or `/`, JavaScript's Automatic Semicolon Insertion would
fail to split the statements and parse them as a single expression
(e.g. `use("mydb")[1,2,3].forEach(...)` becomes member access on
the call result).

Always emit a trailing `;` in the rewrite so the next line is
guaranteed to start a fresh statement. Added a regression test.

Addresses PR #592 review comment (discussion r3111470941).
The `documentDB.timeout` setting was removed in the preceding commit but
a few references lingered in JSDoc examples and in the shell terminal
link-provider tests. Replace them with `documentDB.shell.initTimeout`
(the remaining timeout-related setting) so the code and tests no longer
reference a non-existent configuration key.

No behavioral change.

Addresses PR #592 review comment (discussion r3111470914).
`normalizeDirectCommands` is an internal helper applied automatically by
`DocumentDBShellRuntime.evaluate()`. Re-exporting it from
`@microsoft/documentdb-vscode-shell-runtime` expanded the public API
surface for a test-oriented helper. The unit test already imports it
directly from `./DocumentDBShellRuntime`, so the entrypoint export was
unused by consumers and is safe to remove.

Addresses PR #592 review comment (discussion r3111470833).
`sendRequest()` used a hardcoded `documentDB.shell.initTimeout` setting
key in the `SettingsHintError` it raised on timeout. That made sense
for the init call, but the shutdown path (5-second timeout, error is
swallowed) would have produced a misleading hint had it ever surfaced,
since `documentDB.shell.initTimeout` does not control shutdown.

Parameterize the setting key. The init caller passes
`documentDB.shell.initTimeout`; the shutdown caller omits it, which
causes a plain `Error` to be thrown instead of a `SettingsHintError`
with the wrong key.

No user-facing behavior change today (shutdown swallows the error), but
the class invariant is now consistent and robust to future callers.
@tnaum-ms
Copy link
Copy Markdown
Collaborator Author

Additional follow-up fix: sendRequest timeout hint key (aedacde)

While validating the Copilot review, I noticed WorkerSessionManager.sendRequest() had a hardcoded documentDB.shell.initTimeout setting key in its timeout SettingsHintError. That was fine for the init call but would have been misleading for the shutdown path (which uses a hardcoded 5 s timeout and has no user-facing setting).

Fixed in aedacde: sendRequest now takes an optional timeoutSettingKey. The init caller passes 'documentDB.shell.initTimeout'; the shutdown caller omits it, so a plain Error is thrown instead of a SettingsHintError with an unrelated setting key.

No user-facing behavior change today (shutdown swallows the error), but the invariant is now correct and robust to future callers.

The previous first-line-only heuristic was conservative but narrow:
a bare `use`/`show` on any subsequent line was still passed through
unchanged and would short-circuit the evaluator.

Switch to a tiny hand-written lexical scanner that tracks strings,
template literals (including `${...}` nesting), line/block comments,
and regex literals. Line starts that fall outside every one of those
contexts are candidates for rewriting, so a `use`/`show` anywhere
in the input is normalized while text inside quoted/escaped regions is
still left alone.

Zero new dependencies. A full JS parser (e.g. `acorn`) would be
needed to also disambiguate nested-block positions and declared-
identifier shadowing from statement starters, but those cases are not
observed in user input and adding a parser to a package intended to
ship standalone for CLI tooling is disproportionate.

Tests updated to assert the extended coverage (mid-block rewrites,
multiple top-level commands, strings/templates/comments/regex literals
left unchanged).
@tnaum-ms tnaum-ms merged commit 34143e6 into rel/release_0.8.0 Apr 20, 2026
2 checks passed
@tnaum-ms tnaum-ms deleted the db/tnaum/bug-bash-1-fixes branch April 20, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants