Repository navigation
feat(pcap): read tshark JSON packets through one envelope owner with an adjustable expansion bound - #10788
Merged
Conversation
PcapOutput and the packet-capture reader each decoded tshark -T json and checked the packet envelope. The capture reader let SyntaxError escape and accepted any shape until its field schema ran. src/pcap/TsharkJsonPackets.ts now owns the envelope (malformed JSON, the packet array, each packet's _source.layers) as typed failures, and the pcap byte and expansion budget. Both consumers map those failures to their existing error kinds. An architecture guard keeps _source.layers reads in the owner.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Contributor
[Medium impact] The PR appears safe to merge; no new actionable issues were found. SummaryBoth TShark readers now use
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
P[PcapOutput: fixed shared budget] --> R[readTsharkJsonPackets]
C[parsePacketOutput: selected budget] --> R
R --> B[Check bytes and estimated expansion]
B --> E[parseTsharkJsonPackets]
E --> L[Ordered packet layers]
L --> I[Pcap inventory and DNS join]
L --> F[Selected-field checks]
F --> T[TsharkCaptureProvider attaches captured output]
Reviews (3) · Last reviewed commit: "feat(captures): bound packet-capture JSO..." · Reviewed by Greptile |
parsePacketOutput threw AnalysisOutputError itself, so the provider's failure mapping returned it without the captured stdout and stderr, both for envelope refusals and for unordered or out-of-limit frames. It now returns a Result with the reason and cause, and the provider builds the AnalysisOutputError with capturedOutput.
…setting The capture path parsed TShark's selected-field JSON without the expansion estimate the pcap reader charges. It now reads through readTsharkJsonPackets under capture.packet_json_expansion_bytes (REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES). The estimate charges at most 130 bytes per code unit and the output is one Node string, so no admissible output reaches 69,793,215,440 bytes; the default and maximum are 128 GiB, so the budget refuses only when lowered. A refusal is an adjustable_budget resource constraint naming the setting and keeps the producer output; a malformed launch seed is refused before the capture is read.
This was referenced Nov 15, 2026
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Two readers decoded
tshark -T json:src/pcap/PcapOutput.ts(pcap inventory) andsrc/captures/parsePacketOutput.ts(selected packet fields). They now share one envelope owner,src/pcap/TsharkJsonPackets.ts. Plan W13 step 5 (report 16, "twotshark -T jsonreaders").Problem and expected behavior
JSON.parsedirectly, so truncated producer output escaped as a bareSyntaxError. A non-array root or a packet without_source.layersonly failed later as a schema error on a path like[0, "_source", "layers", …]._source.layersin three places: the selected-field pass, the DNS join and the inventory loop.Expected: one reader owns the export envelope (malformed JSON, the packet array, each packet's
_source.layers, dropping_index/_score) and returns typed failures. Each consumer interprets only the layers.Change and scope
parseTsharkJsonPackets(text)is synchronous. It returnsResult<layers[], malformed-json | malformed-packet>.readTsharkJsonPackets(text, budget, signal)adds pcap's byte bound, its expansion estimate (charged across acquisitions that share one budget) and cancellation, then parses withparseTsharkJsonPackets.PcapOutput maps the failures to the error kinds it already returned:
AnalysisResourceConstraintError,AnalysisProtocolErrorandAnalysisCancelledError. Its three local envelope checks are removed.parsePacketOutputreturnsResult<packets, { reason, cause? }>for every refusal: the envelope, the field shape, unordered frames and frames over the limit.TsharkCaptureProviderbuilds theAnalysisOutputErrorwithcapturedOutput, so the producer's stdout and stderr stay attached. Before this, unordered and out-of-limit refusals on main dropped them, and so did the first commit of this PR for envelope refusals (review thread). The capture read stays synchronous and is not signal-aware, so a late abort still keeps complete Evidence (captureProvider.test.ts"post-run integrity checks").Guard:
tshark-json-envelopeinscripts/verify/architecture-guards.mjsallows_source.layersreads only in the owner, with a zero baseline.Capture expansion budget (coordinator decision, 8b38c62): the capture path now charges the same expansion estimate through
readTsharkJsonPacketsunder a new runtime settingcapture.packet_json_expansion_bytes(REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES,src/config/capturePacketJsonExpansion.ts). Measured bound: the estimate charges at most 130 bytes per UTF-16 code unit and the producer text is one Node string (buffer.constants.MAX_STRING_LENGTH= 536,870,888 on Node 24.22.1), so no output callers can select today is charged more than 69,793,215,440 bytes (65.0 GiB). The default and maximum are 137,438,953,472 bytes (128 GiB), floor 1 MiB, so nothing that parses today is refused; the budget refuses only when lowered. Representative-eoutput was charged 16.9 (pretty-printed, five fields) and 28.2 (compact, one field) bytes per code unit; a dense[[],[],...]worst case 87.3. Over budget, the provider returns anAnalysisResourceConstraintErrorwhoselimitnames the setting, sodetails.refusalisadjustable_budget; the producer output stays attached. A malformed launch seed is refused as aConfigurationErrorbefore the capture is read. The read stays unsignalled, so a late abort still keeps complete Evidence.Not included:
Contribution scope: not applicable (refactor within existing providers).
Contract and boundary impact
src/pcap/TsharkJsonPackets.tsnow produces the parsed tshark envelope.PcapOutput.parseTsharkJsonOutputandparsePacketOutputconsume its layers.capture.packet_json_expansion_bytes, soupdate_analysis_settingsgains one branch (twoconstmembers;advertisedDialectBaseline.tsrecords 83/1857). Otherwise unchanged schemas. Error kinds are unchanged.inspect_packet_capturerefusals now carrycapturedOutputin every case. Reasons change: envelope failures readTShark emitted malformed JSON.andTShark packet N lacks _source.layers., and field-shape failures readTShark packet fields do not match the selected-field shape.with the Zod error as cause. Pcap root and packet messages now saypacket root must be an arrayandpacket N lacks _source.layers.Expected effects:
Evidence and regression coverage
src/pcap/TsharkJsonPackets.test.ts: layer order and metadata dropping, typed failures for truncated JSON, a non-array root and missing layers, the first malformed packet named, byte bound checked before the estimate, expansion charged across shared acquisitions, abort reason returned.tests/boundary/captures/packetOutput.test.ts: assertions now use theResultshape. Three new cases cover the malformed envelope reasons. Schema issue paths are now relative to the layers.tests/boundary/captures/captureProvider.test.ts: "keeps the producer output of a refused %s packet reply" (invalid JSON, unordered frames) asserts the reason andcapturedOutput.stdout. Both cases fail on main and on this PR's first commit.capturedOutput, and malformed JSON reports the rawSyntaxErrormessage.packetOutput.test.ts"refuses output whose expansion estimate exceeds the selected budget";captureProvider.test.ts"refuses a packet reply over the selected JSON expansion budget with the typed setting refusal" (setting, effective value, source, remediation naming the setting, producer output kept, and the same reply admitted at the default) and "refuses a malformed JSON expansion setting before reading the capture".Boundary regressions covered: malformed input (truncated JSON, wrong root, missing layers), capacity (byte and expansion budget), cancellation during the estimate, and late abort after the producer completed.
Validation performed
npm run test:local -- src/pcap/TsharkJsonPackets.test.ts src/pcap/PcapOutput.test.ts src/pcap/PcapConfiguration.test.ts: 92 passednpm run test:focused -- tests/boundary/captures/packetOutput.test.ts tests/boundary/captures/captureProvider.test.ts tests/boundary/pcap/pcapProvider.test.ts tests/boundary/pcap/pcapProviderCancellation.test.ts: 77 passed, 39 skipped (platform-gated)npm run test:focused -- tests/boundary/captures/packetOutput.test.ts tests/boundary/captures/captureProvider.test.ts: 69 passed. Against main's and c490f89's capture sources, the two new provider cases fail.node scripts/verify/architecture-guards.mjs: passed. With main'sPcapOutput.tsit reportssrc/pcap/PcapOutput.ts: 12 (baseline 0)for the new guard.npm run check:fast: passednpx oxfmt --check src/pcap src/captures tests/boundary/captures scripts/verify: passednpm run test:focused -- tests/boundary/captures/packetOutput.test.ts tests/boundary/captures/captureProvider.test.ts tests/boundary/mcp/advertisedDialect.test.ts src/pcap/TsharkJsonPackets.test.ts: passed;npm run test:focused -- tests/acceptance/analysisSettingsMcp.test.ts src/application/Doctor.test.ts src/setupEnvironment.test.ts tests/boundary/filesystem/clientConfigurationEnvironment.test.ts src/application/AnalysisSettings.test.ts tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts src/domain/analysisSettingAdmission.test.ts: passed;npm run check:fast,npm run docs:check,npx oxfmt --check src tests docs: passed. The bound was measured with a standalone copy of the estimator on Node 24.22.1.Compatibility, safety, and release
Author readiness
Human owner:
Before merge
type(scope): outcome.