fix(tools): emit Python SDK members in one lexicographic stream

The Python renderer partitioned identifier methods ahead of subscript
comments, so a tool set like {a-tool, z} emitted z first — contradicting
the documented lexicographic contract and the TypeScript flavor, which
quotes exotic keys in place. Interleave both kinds in one ordered stream
and track emitted statements for the pass fallback.

Also correct four stale serialization claims in the base Code Mode note
that the live-parallel scheduler superseded.
This commit is contained in:
Chinesezjc
2026-08-05 12:59:20 +08:00
parent 0220e06633
commit 698fdaea9b
5 changed files with 40 additions and 23 deletions

View File

@@ -390,11 +390,24 @@ describe('renderToolsSdkPy', () => {
// Descriptions on subscript names ride as a comment beside their entry.
expect(text).toContain('# tools["my-mcp.tool"]')
expect(text).toContain('# Exotic name.')
// Lexicographic: `bash` before `my-mcp.tool` (identifier methods first,
// then subscript comments — the emitter partitions).
// Lexicographic: `bash` before `my-mcp.tool`.
expect(text.indexOf('async def bash')).toBeLessThan(text.indexOf('# tools["my-mcp.tool"]'))
})
it('orders subscript entries against methods by name, not by member kind', () => {
// `a-tool` sorts before `z`, so the subscript comment must precede the
// method: one ordered stream, not methods-then-comments.
const noArgs = parameterSchemaSpecToJsonSchema({}) as unknown as Record<string, unknown>
const text = renderToolsSdkPy([
{ name: 'z', description: 'Last by name.', parameters: noArgs, output: { type: 'string' } },
{ name: 'a-tool', description: 'First by name.', parameters: noArgs, output: { type: 'string' } },
])
expect(text.indexOf('# tools["a-tool"]')).toBeLessThan(text.indexOf('async def z'))
// The interleaved comment does not disturb the class body: `z` still parses
// as the statement that keeps `pass` out.
expect(text).not.toContain(`${' '.repeat(4)}pass`)
})
it('is deterministic: byte-identical output regardless of input order or duplication', () => {
expect(renderToolsSdkPy([bash, exotic])).toBe(renderToolsSdkPy([exotic, bash]))
expect(renderToolsSdkPy([bash, bash])).toBe(renderToolsSdkPy([bash, bash]))