Repository navigation
Refactor: Move MCP and A2A parsing to core/parse - #1701
Conversation
Go's internal rule lets only packages under core/plugins/ import core/plugins/internal/parsercommon. The parser libraries of rossoctl#1503 go in core/parse/, so they cannot import it from there. This commit moves the package to core/internal/parsercommon with git mv. It also does these changes: - Change TokenUsage.Fill to take *model.InferenceExtension. The package then does not import core/pipeline. - Change the import path in the three parser plugins, their tests and cpex/cmf_presentation.go. gofmt sorts the new path into a different place. - Update the comments that name the old place: the package comment, cost/usage/usage.go, cost/pricing/rates.go and cost/pricing/incompletereason_test.go. There is no change in behaviour. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
The fixture tests of mcp-parser, a2a-parser and inference-parser had their own copies of checkFieldPaths, endHead, sseFrames and most of runFixture. This commit puts one copy in the new package core/internal/parsefixture. Only tests import it. - parsefixture.Run runs each case directory that has a request.headers file. The caller gives the parser and the extension to compare. - The package owns the -update flag. The three test files do not define it now. - TestFixtureReadersWithoutBlankLine moves to the new package. The same 33 fixture subtests pass before and after this change, with the same names. No file under testdata changes, also with -update. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
rossoctl#1503 asks for parser libraries that take plain values and give core/model types. This commit moves the MCP parse code from the mcp-parser plugin to the new package core/parse/mcp. - git mv moves plugin.go to core/parse/mcp/mcp.go. The plugin half is in the new file core/plugins/mcpparser/hooks.go. - parseMCPRequest is now mcp.ParseRequest. - The new mcp.ParseResponse reads one response into the extension. It sets Err or Result. It returns false when the body has no JSON-RPC result or error. The JSON-RPC types stay unexported. - The plugin keeps the hooks, the config, the "first response wins" rule and the log and Observe rows. recordResponse replaces applyMCPResponseRPC for the log and the Observe row. - plugin_test.go uses the string literals of the two synthetic method names, because the constants are in the library now. SanitizeLogValue now wraps these log values: the method and body of "mcp-parser: payload", the data of "mcp-parser: skipping malformed SSE data frame", and the method and message of the two response logs. The log text changes only for a value with a control rune or a newline. The exported API of mcpparser does not change. The 11 mcp fixture cases pass with no change to testdata. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
This commit moves the A2A parse code from the a2a-parser plugin to the new package core/parse/a2a, as the previous commit did for MCP. - git mv moves plugin.go to core/parse/a2a/a2a.go. The plugin half is in the new file core/plugins/a2aparser/hooks.go. - parseA2ARequest is now a2a.ParseRequest. - The new a2a.ParseResponse reads the response summary, then captures the session id when the extension has none. OnResponse and OnResponseFrame both call it, for a whole body and for one frame. - The plugin keeps the hooks, the Skip and Observe rows and logA2AFinalized. OnResponse captured the session id before the summary. ParseResponse does the summary first. The two steps write disjoint fields, and neither step reads a field that the other step writes. The doc comment of ParseResponse tells this. SanitizeLogValue now wraps the method, sessionId, role and messageId of "a2a-parser: extracted", and the kind and content of "a2a-parser: part". The log text changes only for a value with a control rune or a newline. The exported API of a2aparser does not change. The 13 a2a fixture cases pass with no change to testdata. core/README.md gets a parse/ row. It names MCP and A2A only, because inference is not in core/parse yet. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
rossoctl#1503 says that the parser libraries must not import cost/settle or cost/pricing. They must also build without the plugin engine, as the core/model data packages do. This commit extends core/model/imports_test.go to check both rules. - core/parse/mcp and core/parse/a2a are in dataOnly, so they must not reach core/pipeline. - The new TestParsersDoNotImportPricing fails when a parser library reaches cost/settle or cost/pricing, also through another package. - The two tests share one go list loop, checkDeps. - Blank imports of the two packages make the test rebuild when they change, so the test cache cannot hide a violation. Check: a scratch file in core/parse/a2a with an import of cost/settle makes both tests fail and name core/parse/a2a. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
parsercommon moved to core/internal, so every package in core can import it now. Some comments still told the old rule. - cost/pricing/rates.go and cost/pricing/incompletereason_test.go tell why each mirror of parsercommon exists, and that the package can import parsercommon now. The code does not change. - cost/usage/usage.go and parsercommon/kindbits_usage_test.go do not say that the kind-bits test is the one place both sets are visible. parsercommon/kindbits_usage_test.go also says "outside core", not "outside core/plugins". - parsefixture.go tells that a test package that imports it must not define its own -update flag, because two definitions panic at init. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
git shows core/plugins/a2aparser/hooks.go as a new file. The diff of core/plugins/mcpparser/hooks.go (renamed from plugin.go) also shows its debug logs as added lines. CodeQL reads such lines as new log calls, so this commit wraps their client-controlled values in SanitizeLogValue: - a2aparser: the sessionId, finalStatus and error of "a2a-parser: response parsed". - mcpparser: the method and body of "response detail" and "streaming frame", the method of "ignoring a later response frame", and the frame of "response frame carried no JSON-RPC result/error". The log text changes only for a value with a control rune or a newline. It also fixes a comment in core/parse/a2a: extractStreamResponse folds each event as ParseResponse does for one frame. OnResponseFrame is in another package now. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
The previous commit wrapped four debug-log values in core/plugins/mcpparser/hooks.go. These lines did not change in the move from plugin.go. The wraps put the similarity of the two files below the 50% that git needs to show a rename. git then showed plugin.go as deleted and hooks.go as a new file, and the diff of the PR became twice as large. This commit removes those four wraps. The log values are the same as on the base. The wraps in a2aparser/hooks.go, a new file, stay. Part of rossoctl#1437. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 30 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
rossoctl#1699, the PR this branch was stacked on, is merged. Bring the branch up to date with main. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Fixes review: parsefixture.Run skips a capture with no request.headers, and it reads a missing response file as an empty body. Files: - core/plugins/inferenceparser/fixtures_test.go Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Fixes review: the Usage comment said that pricing can import parsercommon. That import makes a test import cycle. Files: - core/cost/pricing/rates.go Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
clawgenti
left a comment
There was a problem hiding this comment.
Summary
The refactor does what it says. I diffed the moved code against main function by function: mcp.ParseRequest / parseResponse are byte-identical to parseMCPRequest / parseMCPResponse apart from the rename, pipeline.→model. and the added SanitizeLogValue wraps. The A2A response half (extractResponseSummary … parseA2AParts) is identical apart from types and one comment. The A2A ParseResponse reorder is safe: the summary writes FinalStatus/TaskID/Artifact/ErrorMessage, and the session-id capture writes only SessionID. mcp.ParseResponse + recordResponse give the same Err/Result/Observe results as applyMCPResponseRPC, including a body that carries both error and result. No new typed decoder fields, so no strictness regression for wrong-typed client bodies.
What I verified locally (GOWORK=off, head d1d390e):
go vetandgofmt -lare clean onparse/,internal/, the three parser plugins andmodel/.go testpasses for those packages and forpipeline/(33 fixture subtests).- The ban tests catch a violation: a scratch
cost/settleimport inparse/a2afails both tests, and a scratchcost/pricingimport inparse/mcpfailsTestParsersDoNotImportPricing. - No stale
plugins/internal/parsercommonimports remain. The only mention left is a historical plan doc. cost/usageandcost/ledgerfail locally, but they fail the same way onmain(DST/date-sensitive tests on today's date). These failures did not come from this PR.
One suggestion inline: the shared fixture runner is looser than the old inference-parser runner was. It is not blocking.
Optional follow-up, not for this PR: the response detail / streaming frame debug logs in mcpparser/hooks.go still log the raw body. You kept that on purpose so git shows the file as a rename, and that matches main. A small follow-up could wrap them once the rename has landed.
Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go (core/parse, core/internal, parser plugins, import-ban tests), Docs (README, comments)
Agent/IDE config (.claude/.vscode): none
Commits: 9 commits, all signed-off: yes
CI status: passing
| } | ||
| } | ||
| if respFile == "" { | ||
| respFile, contentType = "response.json", "application/json" |
There was a problem hiding this comment.
suggestion: For inference-parser, this shared runner is looser than the runner it replaces. The old inferenceparser/fixtures_test.go globbed testdata/*/* and then failed hard on three things: a missing request.headers, a missing request.json (os.ReadFile + t.Fatal), and a case with no response file ("no response.sse, response.json or response.html"). Now:
Runglobs<pattern>/request.headers, so a capture dir without a headers file is silently skipped. I checked: atestdata/claude-code/zz-nohead/holding onlyrequest.jsongivesok.- A missing response file falls back to an empty
application/jsonbody here, andreadBodyturns a missingrequest.jsoninto an empty body. With-update, a half-copied capture would quietly write a golden from an empty response, not fail.
The defaults suit the hand-built MCP/A2A cases (empty bodies are real cases there). They hide mistakes for the real inference captures. One cheap fix is to let the caller opt in, e.g. a RequireBodies bool option, or a check in the inference TestFixtures that every testdata/*/* dir has request.headers and one response file.
clawgenti
left a comment
There was a problem hiding this comment.
Summary
Re-review of the 2 commits since my approval at d1d390e. I re-read the full diff too. Nothing else changed since then: d1d390e..7c55834 touches only inferenceparser/fixtures_test.go and the pricing.Usage comment.
c1f3a290(TestFixtureCasesAreComplete) answers my earlier suggestion about the looser shared runner. I checked that it really fails: a scratchtestdata/claude-code/zz-probe/that holds onlyrequest.jsonfails with two errors, one for the missingrequest.headersand one for the missing response file. On the real tree it passes, because all 9 captures have headers and one response file. One small gap is noted inline (request.json), and it is not blocking.7c558348(therates.gocomment) is correct now. I added a scratchimport _ ".../core/internal/parsercommon"tocost/pricing, andgo vet ./internal/parsercommon/reportsimport cycle not allowed in testthroughkindbits_usage_test.go→cost/usage(apportion.go) →cost/pricing. That matches the new comment exactly.
What I verified locally (GOWORK=off, head 7c55834): go vet and gofmt -l are clean on cost/pricing, plugins/inferenceparser, internal/... and parse/.... go test passes for the three parser plugins, internal/parsefixture, internal/parsercommon, model, pipeline and cost/pricing. (plugins/tokenbroker/client timed out in TestClient_AcquireToken_DNSFailure. That is the sandbox DNS through the egress proxy, it is unrelated to this PR, and it passes in CI.)
Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go (fixture completeness test, pricing doc comment; full diff re-checked against the prior review)
Agent/IDE config (.claude/.vscode): none
Commits: 11 commits, all signed-off: yes
CI status: passing
| if v, ok := bytes.CutPrefix(line, []byte("data:")); ok { | ||
| data = append(data, bytes.TrimPrefix(v, []byte(" "))) | ||
| n := 0 | ||
| for _, name := range []string{"response.sse", "response.json", "response.html"} { |
There was a problem hiding this comment.
nit: My earlier comment listed three ways a half-copied capture slips through. This test covers two of them, request.headers and the response file. A capture that has no request.json still passes here, and readBody turns the missing file into an empty request body, so -update would write a golden from it. All 9 current captures have the file, so nothing is wrong today. If you want it covered, one more os.Stat(filepath.Join(dir, "request.json")) next to the headers check would do it. Fine to skip.
rossoctl#1701, the PR this branch was stacked on, is merged. Bring the branch up to date with main. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Bring in main (#1699 merged) through the base branch, P3a (#1701). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Part of #1437: the P6 item of #1503 (#1688 closed #1503). Phase 2 of the Praxis prep. The inference parser follows in a second PR, stacked on this one.
What this changes
core/parse/mcpandcore/parse/a2a. They take plain values and returncore/modeltypes:mcp.ParseRequest(method, path, headers, rawBody, paths)andmcp.ParseResponse(body, ext) bool.a2a.ParseRequest(path, headers, rawBody)anda2a.ParseResponse(body, ext).parseMCPRequestandparseA2ARequest, and from the response code of each plugin.hooks.go). They keep config,Contextwiring, the "first response wins" rule,Skip/Observeand their log lines. Their exported API does not change.parsercommonmoves fromcore/plugins/internal/tocore/internal/, because Go'sinternalrule stopscore/parsefrom importing it there. It now importscore/model, notcore/pipeline.core/internal/parsefixture, replaces the three copies in the parser plugins'fixtures_test.go. It owns the-updateflag.core/model/imports_test.gonow also checks thatcore/parse/{mcp,a2a}reach neithercore/pipelinenorcost/settle/cost/pricing.How to review
Most new lines are moved code. Read with
git diff -M --color-moved main....plugins/a2aparser/plugin.goparse/a2a/a2a.goplugins/a2aparser/plugin.go(plugin half)plugins/a2aparser/hooks.goplugins/mcpparser/plugin.go(plugin half)plugins/mcpparser/hooks.goplugins/mcpparser/plugin.go(parse half)parse/mcp/mcp.goplugins/internal/parsercommon/*internal/parsercommon/*With rename detection the diff is +746 / −997 in 29 files.
Behaviour
testdata/file changed.SanitizeLogValue.ParseResponsereads the summary before the session id. The old order was the other way. The two steps write different fields, and neither reads what the other writes.Checks run
go vet,gofmt -landgo mod tidy -diffwithGOWORK=offin all 8 workspace modules, andtidy -diffin the 3 demos modules.go test -count=1 ./...incoreandcmd/agentop.cmd/cortexvet with thefull,local,liteandfullcpexprofiles.cmd/cortex-envoybuild withenvoy.corevet withfullcpex.cost/settleimport is added tocore/parse/a2a.pre-commit runon every changed file.Deferred review nits
core/parse/{mcp,a2a}have no tests of their own. The plugin tests and the goldens cover them.core/pipelineand the cost packages, notcore/plugins/.... No such import exists.parsefixture.Runskips an MCP or A2A case that has norequest.headers. The old MCP and A2A helpers did the same. Only the inference test checks for this file now. The check can move intoRunlater.TestFixtureCasesAreCompletewrites the response file names again. They are also the keys ofparsefixture.responseTypes. A new response type makes the test fail until its list changes.Assisted-By: Claude (Anthropic AI) [email protected]