-
Notifications
You must be signed in to change notification settings - Fork 46
fix(lint): require single to target on transition statement
#62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
958c192
fix(lint): require single `to` target on transition statement
monga-puneet 41e13e9
test(lint): drop unused .agent fixtures, rely on inline tests
monga-puneet 6b25ec6
fix(lint): drop spec section reference from transition diagnostics
monga-puneet c70e1e3
fix(lint): restore isAstNodeLike guard for TS narrowing
monga-puneet 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
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,108 @@ | ||
| /* | ||
| * Copyright (c) 2026, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect } from 'vitest'; | ||
| import { parse } from '@agentscript/parser'; | ||
| import { Dialect } from '../core/dialect.js'; | ||
| import { NamedBlock, NamedCollectionBlock } from '../core/block.js'; | ||
| import { StringValue, ProcedureValue } from '../core/primitives.js'; | ||
| import { LintEngine } from '../core/analysis/lint-engine.js'; | ||
| import { createSchemaContext } from '../core/analysis/scope.js'; | ||
| import { transitionTargetPass } from './transition-target.js'; | ||
|
|
||
| const ProcBlock = NamedBlock('ProcBlock', { | ||
| label: StringValue.describe('Label'), | ||
| body: ProcedureValue.describe('Procedure body'), | ||
| }); | ||
|
|
||
| const TestSchema = { | ||
| proc: NamedCollectionBlock(ProcBlock), | ||
| }; | ||
|
|
||
| const schemaCtx = createSchemaContext({ schema: TestSchema, aliases: {} }); | ||
|
|
||
| function getDiagnostics(source: string, code?: string) { | ||
| const { rootNode: root } = parse(source); | ||
| const mappingNode = | ||
| root.namedChildren.find(n => n.type === 'mapping') ?? root; | ||
|
|
||
| const dialect = new Dialect(); | ||
| const result = dialect.parse(mappingNode, TestSchema); | ||
|
|
||
| const engine = new LintEngine({ | ||
| passes: [transitionTargetPass()], | ||
| source: 'test', | ||
| }); | ||
| const { diagnostics } = engine.run(result.value, schemaCtx); | ||
| if (!code) return diagnostics; | ||
| return diagnostics.filter(d => d.code === code); | ||
| } | ||
|
|
||
| describe('transition-target lint pass', () => { | ||
| it('does not flag a valid `transition to <target>`', () => { | ||
| const diags = getDiagnostics(` | ||
| proc one: | ||
| label: "one" | ||
| body: -> | ||
| transition to @subagent.a | ||
| `); | ||
| expect(diags).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('flags a bare `transition` with no target (F1)', () => { | ||
| const diags = getDiagnostics( | ||
| ` | ||
| proc one: | ||
| label: "one" | ||
| body: -> | ||
| transition | ||
| `, | ||
| 'transition-missing-target' | ||
| ); | ||
| expect(diags).toHaveLength(1); | ||
| expect(diags[0].message).toContain('requires a target'); | ||
| expect(diags[0].severity).toBe(1); // Error | ||
| }); | ||
|
|
||
| it('flags multiple `to` clauses on a single transition (F2)', () => { | ||
| const diags = getDiagnostics( | ||
| ` | ||
| proc one: | ||
| label: "one" | ||
| body: -> | ||
| transition to @subagent.a, to @subagent.b | ||
| `, | ||
| 'transition-multiple-targets' | ||
| ); | ||
| expect(diags).toHaveLength(1); | ||
| expect(diags[0].message).toContain('single'); | ||
| expect(diags[0].severity).toBe(1); | ||
| }); | ||
|
|
||
| it('flags every extra `to` in a 3-target transition', () => { | ||
| const diags = getDiagnostics( | ||
| ` | ||
| proc one: | ||
| label: "one" | ||
| body: -> | ||
| transition to @subagent.a, to @subagent.b, to @subagent.c | ||
| `, | ||
| 'transition-multiple-targets' | ||
| ); | ||
| expect(diags).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('does not flag `transition to <target> with k=v`', () => { | ||
| const diags = getDiagnostics(` | ||
| proc one: | ||
| label: "one" | ||
| body: -> | ||
| transition to @subagent.a with x="hi" | ||
| `); | ||
| expect(diags).toHaveLength(0); | ||
| }); | ||
| }); |
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,93 @@ | ||
| /* | ||
| * Copyright (c) 2026, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| import type { AstRoot } from '../core/types.js'; | ||
| import { isAstNodeLike } from '../core/types.js'; | ||
| import { DiagnosticSeverity, attachDiagnostic } from '../core/diagnostics.js'; | ||
| import { | ||
| storeKey, | ||
| type LintPass, | ||
| type PassStore, | ||
| } from '../core/analysis/lint-engine.js'; | ||
| import { lintDiagnostic } from './lint-utils.js'; | ||
| import { TransitionStatement, ToClause } from '../core/statements.js'; | ||
| import { toRange } from '@agentscript/types'; | ||
|
|
||
| const MISSING_TARGET_MESSAGE = | ||
| "'transition' requires a target. Use 'transition to <target>'."; | ||
|
|
||
| const MULTIPLE_TARGETS_MESSAGE = | ||
| "'transition' accepts a single 'to' target. " + | ||
| "Multiple 'to' clauses are not allowed."; | ||
|
|
||
| class TransitionTargetPass implements LintPass { | ||
| readonly id = storeKey('transition-target'); | ||
| readonly description = | ||
| "Validates that 'transition' statements have exactly one 'to <target>' clause."; | ||
|
|
||
| private transitions: TransitionStatement[] = []; | ||
|
|
||
| init(): void { | ||
| this.transitions = []; | ||
| } | ||
|
|
||
| enterNode(_key: string, value: unknown, _parent: unknown): void { | ||
| if ( | ||
| isAstNodeLike(value) && | ||
| value.__kind === 'TransitionStatement' && | ||
| value instanceof TransitionStatement | ||
| ) { | ||
| this.transitions.push(value); | ||
| } | ||
| } | ||
|
|
||
| run(_store: PassStore, _root: AstRoot): void { | ||
| for (const stmt of this.transitions) { | ||
| const toClauses = stmt.clauses.filter(c => c instanceof ToClause); | ||
| const range = stmt.__cst?.range; | ||
| if (!range) continue; | ||
| // Type narrowing for attachDiagnostic; runtime guaranteed by enterNode. | ||
| if (!isAstNodeLike(stmt)) continue; | ||
|
|
||
| if (toClauses.length === 0) { | ||
| // Highlight just the `transition` keyword token (first child of the | ||
| // CST node), not the whole statement — keeps the squiggle pointed | ||
| // at the actual problem. | ||
| const keywordNode = stmt.__cst?.node?.children?.[0]; | ||
| const keywordRange = keywordNode ? toRange(keywordNode) : range; | ||
| attachDiagnostic( | ||
| stmt, | ||
| lintDiagnostic( | ||
| keywordRange, | ||
| MISSING_TARGET_MESSAGE, | ||
| DiagnosticSeverity.Error, | ||
| 'transition-missing-target' | ||
| ) | ||
| ); | ||
| } else if (toClauses.length > 1) { | ||
| // Flag every extra `to` clause (keep the first as the intended one). | ||
| for (let i = 1; i < toClauses.length; i++) { | ||
| const extra = toClauses[i]; | ||
| const extraRange = extra.__cst?.range ?? range; | ||
| attachDiagnostic( | ||
| stmt, | ||
| lintDiagnostic( | ||
| extraRange, | ||
| MULTIPLE_TARGETS_MESSAGE, | ||
| DiagnosticSeverity.Error, | ||
| 'transition-multiple-targets' | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function transitionTargetPass(): LintPass { | ||
| return new TransitionTargetPass(); | ||
| } | ||
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.
These just get used here, so weird to have them defined as constants.
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.
java habbit :)