-
Notifications
You must be signed in to change notification settings - Fork 665
feat(tree-sitter-bash): add a pure-TypeScript bash parser and an agent-core-v2 bashParser service #2016
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
Open
sailist
wants to merge
7
commits into
MoonshotAI:main
Choose a base branch
from
sailist:feat/tree-sitter-bash
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.
Open
feat(tree-sitter-bash): add a pure-TypeScript bash parser and an agent-core-v2 bashParser service #2016
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed169bd
feat(tree-sitter-bash): scaffold pure-TypeScript bash parser package
sailist d306c0c
feat(tree-sitter-bash): add lexer and core recursive-descent parser
sailist 9a9f9ca
feat(tree-sitter-bash): support the full bash grammar
sailist 83b9636
test(tree-sitter-bash): add differential fixtures, corpus, fuzz and p…
sailist 31401d5
feat(agent-core-v2): add App-scope bashParser service
sailist a9ded49
chore: add changeset for the bash parser service
sailist 2925c0a
chore: update pnpmDeps hash after adding tree-sitter-bash package
sailist 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Add an internal bash parsing capability that turns shell command strings into syntax trees, in preparation for per-command permission analysis. No user-facing behavior change yet. |
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,42 @@ | ||
| /** | ||
| * `bashParser` domain (L1) — bash source parsing capability. | ||
| * | ||
| * Defines the `IBashParserService` that parses a bash source string into a | ||
| * syntax tree through the pure `@moonshot-ai/tree-sitter-bash` package, plus | ||
| * the wire-safe DTO types it returns: `BashSyntaxNode` drops the cyclic | ||
| * `parent` link so results can cross the RPC boundary, and offsets are | ||
| * UTF-16 code units (`text` always equals `source.slice(start, end)`). The | ||
| * parse runs under a deterministic budget — budget exhaustion yields | ||
| * `{ ok: false, reason: 'aborted' }` and malformed input yields | ||
| * `hasError: true`, never a throw; callers that cannot analyze a command | ||
| * must degrade on either signal. Bound at App scope. | ||
| */ | ||
|
|
||
| import { createDecorator, type ServiceIdentifier } from '#/_base/di/instantiation'; | ||
|
|
||
| export interface BashSyntaxNode { | ||
| readonly type: string; | ||
| readonly text: string; | ||
| readonly startIndex: number; | ||
| readonly endIndex: number; | ||
| readonly isNamed: boolean; | ||
| readonly children: readonly BashSyntaxNode[]; | ||
| } | ||
|
|
||
| export interface BashParseOptions { | ||
| readonly timeoutMs?: number; | ||
| readonly maxNodes?: number; | ||
| } | ||
|
|
||
| export type BashParseResult = | ||
| | { readonly ok: true; readonly hasError: boolean; readonly root: BashSyntaxNode } | ||
| | { readonly ok: false; readonly reason: 'aborted' }; | ||
|
|
||
| export interface IBashParserService { | ||
| readonly _serviceBrand: undefined; | ||
|
|
||
| parse(source: string, options?: BashParseOptions): BashParseResult; | ||
| } | ||
|
|
||
| export const IBashParserService: ServiceIdentifier<IBashParserService> = | ||
| createDecorator<IBashParserService>('bashParserService'); |
49 changes: 49 additions & 0 deletions
49
packages/agent-core-v2/src/app/bashParser/bashParserService.ts
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,49 @@ | ||
| /** | ||
| * `bashParser` domain (L1) — `IBashParserService` implementation. | ||
| * | ||
| * Thin adapter over the pure `@moonshot-ai/tree-sitter-bash` package: runs | ||
| * its budgeted `parse` and snapshots the returned tree into the wire-safe | ||
| * `BashSyntaxNode` DTO (source-ordered children including anonymous tokens, | ||
| * `parent` links dropped). Owns no state and injects no services. Bound at | ||
| * App scope. | ||
| */ | ||
|
|
||
| import { parse } from '@moonshot-ai/tree-sitter-bash'; | ||
| import type { SyntaxNode } from '@moonshot-ai/tree-sitter-bash'; | ||
|
|
||
| import { InstantiationType } from '#/_base/di/extensions'; | ||
| import { LifecycleScope, registerScopedService } from '#/_base/di/scope'; | ||
|
|
||
| import type { BashParseOptions, BashParseResult, BashSyntaxNode } from './bashParser'; | ||
| import { IBashParserService } from './bashParser'; | ||
|
|
||
| function snapshot(node: SyntaxNode): BashSyntaxNode { | ||
| return { | ||
| type: node.type, | ||
| text: node.text, | ||
| startIndex: node.startIndex, | ||
| endIndex: node.endIndex, | ||
| isNamed: node.isNamed, | ||
| children: node.children.map(snapshot), | ||
| }; | ||
| } | ||
|
|
||
| export class BashParserService implements IBashParserService { | ||
| declare readonly _serviceBrand: undefined; | ||
|
|
||
| parse(source: string, options: BashParseOptions = {}): BashParseResult { | ||
| const result = parse(source, options); | ||
| if (!result.ok) { | ||
| return { ok: false, reason: result.reason }; | ||
| } | ||
| return { ok: true, hasError: result.hasError, root: snapshot(result.rootNode) }; | ||
| } | ||
| } | ||
|
|
||
| registerScopedService( | ||
| LifecycleScope.App, | ||
| IBashParserService, | ||
| BashParserService, | ||
| InstantiationType.Delayed, | ||
| 'bashParser', | ||
| ); | ||
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
72 changes: 72 additions & 0 deletions
72
packages/agent-core-v2/test/app/bashParser/bashParserService.test.ts
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,72 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { DisposableStore } from '#/_base/di/lifecycle'; | ||
| import { createServices, type TestInstantiationService } from '#/_base/di/test'; | ||
| import type { BashSyntaxNode } from '#/app/bashParser/bashParser'; | ||
| import { IBashParserService } from '#/app/bashParser/bashParser'; | ||
| import { BashParserService } from '#/app/bashParser/bashParserService'; | ||
|
|
||
| describe('BashParserService', () => { | ||
| let disposables: DisposableStore; | ||
| let ix: TestInstantiationService; | ||
| let service: IBashParserService; | ||
|
|
||
| beforeEach(() => { | ||
| disposables = new DisposableStore(); | ||
| ix = createServices(disposables, { | ||
| additionalServices: (reg) => { | ||
| reg.define(IBashParserService, BashParserService); | ||
| }, | ||
| }); | ||
| service = ix.get(IBashParserService); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| disposables.dispose(); | ||
| }); | ||
|
|
||
| it('splits a compound command into per-command nodes', () => { | ||
| const result = service.parse('git status && rm -rf /'); | ||
| if (!result.ok) { | ||
| throw new Error('expected ok'); | ||
| } | ||
| expect(result.hasError).toBe(false); | ||
| expect(result.root.type).toBe('program'); | ||
| const commands: string[] = []; | ||
| const walk = (node: BashSyntaxNode): void => { | ||
| if (node.type === 'command') { | ||
| commands.push(node.text); | ||
| } | ||
| node.children.forEach(walk); | ||
| }; | ||
| walk(result.root); | ||
| expect(commands).toEqual(['git status', 'rm -rf /']); | ||
| }); | ||
|
|
||
| it('flags malformed input with hasError instead of throwing', () => { | ||
| const result = service.parse('echo "unterminated'); | ||
| if (!result.ok) { | ||
| throw new Error('expected ok'); | ||
| } | ||
| expect(result.hasError).toBe(true); | ||
| }); | ||
|
|
||
| it('reports budget exhaustion as aborted', () => { | ||
| const result = service.parse('ls; '.repeat(20000), { maxNodes: 100 }); | ||
| expect(result).toEqual({ ok: false, reason: 'aborted' }); | ||
| }); | ||
|
|
||
| it('returns a JSON-serializable tree with text/range fidelity', () => { | ||
| const source = 'echo "你好 🎉" | grep 你'; | ||
| const result = service.parse(source); | ||
| if (!result.ok) { | ||
| throw new Error('expected ok'); | ||
| } | ||
| const roundTripped = JSON.parse(JSON.stringify(result.root)) as BashSyntaxNode; | ||
| const check = (node: BashSyntaxNode): void => { | ||
| expect(node.text).toBe(source.slice(node.startIndex, node.endIndex)); | ||
| node.children.forEach(check); | ||
| }; | ||
| check(roundTripped); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When callers parse a long but budget-acceptable arithmetic expression, e.g.
echo $((1+1+...))with a few thousand operands and a largertimeoutMs, the parser returns a deeply left-nestedbinary_expressiontree under the defaultmaxNodes; this recursivemap(snapshot)then overflows the JS call stack and makesIBashParserService.parsethrowRangeErrorinstead of returninghasError/aborted. Since this service is meant to be the wire-safe RPC adapter, the DTO conversion needs the same iterative approach asmaterialize.Useful? React with 👍 / 👎.