Skip to content

refactor(runtime): dedupe isLikelySandboxDenial into sandbox/detect l… - #1438

Merged
Astro-Han merged 2 commits into
apache:mainfrom
chinawch007:refactor/sandbox-denial-predicate
Jul 26, 2026
Merged

Astro-Han merged 2 commits into
apache:mainfrom
chinawch007:refactor/sandbox-denial-predicate

Conversation

@chinawch007

Copy link
Copy Markdown
Contributor

Summary

Dedupe the isLikelySandboxDenial predicate — three copies (shell-tools.ts, builtin-tools.ts, and a PTY-aware variant in shell-run-tool-result.ts) shared the same regex but differed in input shape. Extracts one primitive isLikelySandboxDenial({ stdout, stderr, sandboxed }) into a new sandbox/detect.ts leaf, so the regex and the sandboxed-gating live in exactly one place. Addresses the "Sandbox-denial predicate" slice from #1404.

The three call sites keep their own input shaping, since that's where their scenarios genuinely differ:

File sandboxed source text source
shell-tools.ts 'sandboxed' in result && result.sandboxed === true result.stdout / result.stderr (BoundedShellResult lacks the field, so the in narrowing stays at the call site)
builtin-tools.ts result.sandboxed === true result.stdout / result.stderr
shell-run-tool-result.ts hard-coded true (wrapper already gated on sandboxExecution.enforced) new local flattenSandboxDenialText(output) — preserves the original pipes-vs-pty flattening (pty: scrollback + screen + lastAlternateScreen → stdout)

Behavior-neutral: the regex is byte-identical to all three originals, and the PTY-flattened text matches the original variant's output (leading \n from the empty stderr doesn't affect regex matching). No public surface change — all three call sites are internal, isLikelySandboxDenial is an internal leaf consumed only by these three files (not re-exported from sandbox/index.ts barrel, since runtime/index.ts doesn't expose it externally).

Verification

  • npm --workspace @maka/runtime run typecheck — clean
  • npm --workspace @maka/runtime test — 2403 pass, 0 fail, 7 skipped (full suite, including the shell-run-tool-result sandbox-denial projection tests that cover the variant call site)
  • npm run lint — clean (2041 files)
  • npm run format:check — clean (1019 files)
  • git diff origin/main...HEAD --check — no whitespace errors

Review focus

  • The variant (shell-run-tool-result.ts) is the only non-mechanical change. Confirm flattenSandboxDenialText preserves the original PTY text shape — the pty branch stuffs scrollback + screen + lastAlternateScreen into the stdout slot and leaves stderr empty, so the primitive's ${stderr}\n${stdout} reconstructs the original ${scrollback}\n${screen}\n${lastAlternateScreen} (with a leading \n that doesn't affect the regex).
  • sandboxed resolution lives at each call site, not in the primitive — this is deliberate (each scenario computes "is this a sandboxed run" from different data: a sandboxed? field, a 'sandboxed' in result narrowing, or an upstream sandboxExecution.enforced gate). Confirm the three resolutions are equivalent to the originals.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No correctness issue found. The new leaf owns the regex and sandbox gate, while each caller keeps its own input shaping.

Suggested follow-up: add direct tests for the predicate and PTY flattening. The current suite covers a pipes/stderr match, but not the PTY text fields, stdout matches, the other regex branches, near-misses, or all three callers. This is not blocking the refactor.

@chinawch007

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Pushed a follow-up commit (f86097f9) that addresses the suggested coverage gaps — the predicate and PTY flattening now have direct tests, all 24 new tests pass.

  • sandbox-detect.test.ts (new) — direct tests for isLikelySandboxDenial:
    • all 3 regex branches (operation not permitted, sandbox-exec, sandbox...denied)
    • stdout vs stderr scanning
    • 7 near-miss cases: sandboxed: false, operation permitted (missing "not"), sandbox alone, permission denied (no sandbox prefix), sandbox denial (denial ≠ deny/denied), newline-split (sandbox + denied across fields), and plain non-sandbox text
    • case-insensitivity (uppercase + mixed-case)
  • shell-run-tool-result.test.ts — PTY flattening via terminalContent (kept flattenSandboxDenialText private per the test: replace source-regex extraction contracts with behavioral assertions #1403 behavior-test-first preference): denial detected in each of scrollback, screen, lastAlternateScreen; cross-field split (sandbox in scrollback + denied in screen) correctly NOT matched, since the regex [^\n]* honors the \n separator.
  • shell-tools.test.ts + builtin-tools.test.ts — caller smoke tests: shapeTerminalResult surfaces sandboxDenial, and the Bash tool throws a SandboxCommandError (reason: 'sandbox_denial', recoverable: true) on a sandboxed denial.

The two commits are cleanly separated: 8ab59fed is the pure dedup (source only), f86097f9 is test-only (no source changes).

@Astro-Han

Copy link
Copy Markdown
Contributor

LGTM! Merging.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed f86097f. The new tests cover the sandbox-denial patterns and near-misses, stdout and stderr, all three PTY text fields, newline boundaries, and the three call sites.

Production code did not change. All required checks pass. No findings. Approving.

@Astro-Han
Astro-Han merged commit f906f89 into apache:main Jul 26, 2026
3 checks passed
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