Skip to content

fix(syntax): keep chunked parses intact and nested paren probes fast - #894

Merged
ewhauser merged 3 commits into
mainfrom
claude/agitated-satoshi-9f770c
Oct 20, 2026
Merged

ewhauser merged 3 commits into
mainfrom
claude/agitated-satoshi-9f770c

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes from the second review of shell/syntax.

1. Unclosed-heredoc nil delimiter. doHeredocs read r.HdocDelim.Value without checking for nil. At 19ff56a I could not trigger a nil delimiter with the reported input (a missing delimiter word always sets p.err first), so this adds the guard as a defensive fix. A regression test parses the reported inputs in POSIX and Bash mode.

2. Chunked reader changed the parse (regression from #820). There were two root causes:

  • When a nested paren probe called detachSourceReplay, it dropped the enclosing probe's source view. After that, sourceBuffer() fell back to p.bs, which with a chunked reader holds only the last chunk. So the outer arithmetic salvage lost its text. (( $((1)) ((echo a); echo b); )) parsed as (($((1)))) with no error. Detaching now restores the enclosing view.
  • loadReplay (backquote recovery) appended into p.bs[:0]. Inside a probe, p.bs aliases the shared replay buffer, so this overwrote input that later probes read. For example, (( `"` )) became (( ))` )). This only showed with a string reader. loadReplay now copies.

New reader_differential_test.go parses fixed and generated inputs with both a string reader and iotest.OneByteReader and requires identical errors and printed ASTs. Over 60k generated Bash inputs, what still differs is exactly the set that already differed at ddcd090 (before #820). All of it is backquote recovery outside probes, where recoverableBackquoteSource only sees the current chunk. That is pre-existing and out of scope here, so the generated alphabet leaves backquotes out.

3. Superlinear parse of nested $( (( / $((. Each start offset was probed only once, so a per-offset memo of the probe decision would never hit. The cost came from three things:

  • Every probe re-parsed all deeper levels in no-probe mode.
  • arithmSuffixWord built a fresh sub-parser for the same salvaged tails about n² times.
  • Probes copied each level's remaining source into ArithmCmd.Source/ArithmExp.Source, which is about n³ bytes.

Fixes:

  • probeMemo (new probe_memo.go) memoizes nested no-probe $((/(( parses across probe clones, keyed by offset and lexer state. On a hit it replays the nesting counters and moves the lexer past the construct.
  • Probe parses of arithmetic tails are cached in the heredoc cache.
  • Probes no longer build the Source strings, which no parser decision reads.
depth before after
400 1.4 s 17 ms
1600 29.8 s 55 ms
3200 — 127 ms

go test ./shell/syntax dropped from 27 s to 9 s locally. TestProbeMemoMatchesFreshParse requires identical results with the memo on and off; I also ran it locally on 200k generated inputs. TestParseNestedParenAmbiguityIsFast requires depth 3200 to parse within a deadline. A residual non-linear term remains: copying the source text that each nested node keeps, which is inherent to the AST.

4. Duplicate pattern package. Not changed. shell/internal/pattern/*.go and internal/shellpattern/*.go are byte-identical, but another session is changing pattern logic in both right now, so deduplicating here would conflict. Pattern fixes must go into both copies until one becomes a thin wrapper over the other.

No SPEC changes: there are no boundary, policy or API changes.

Checks: go test ./shell/... and make lint are clean. FuzzParsePrint and FuzzParseAdversarial each ran 60 s with no failures and left no new testdata.

@ewhauser
ewhauser enabled auto-merge (squash) October 20, 2026 15:16
@ewhauser
ewhauser merged commit cffed2a into main Oct 20, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/agitated-satoshi-9f770c branch October 20, 2026 15:29
@ewhauser ewhauser mentioned this pull request Oct 20, 2026
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.

1 participant