|
| 1 | +"""CE043: Agents must not truncate a command's output when recording it. |
| 2 | +
|
| 3 | +``CommandTelemetry.result_summary`` is contractually the *untruncated* tool-result |
| 4 | +body, and its length drives the ``result_tokens`` computed field (the cost |
| 5 | +simulator's cache-independent measure of tool-output size). An agent that clips a |
| 6 | +command's captured output before storing it silently under-reports every tool |
| 7 | +result — exactly the bug the Codex agent shipped with (``f"Output: {output[:100]}"``), |
| 8 | +which pinned ~77% of its Bash results at ~31 tokens and skewed the cost model. |
| 9 | +
|
| 10 | +This rule flags, inside ``src/coder_eval/agents/``, a constant-upper-bound slice |
| 11 | +(``x[:N]``) applied to a value that denotes captured command output: |
| 12 | +
|
| 13 | + * a name whose id is/ends with ``output`` / ``stdout`` / ``stderr`` |
| 14 | + (e.g. ``output[:100]``, ``aggregated_output[:512]``, ``proc_stdout[:80]``) |
| 15 | + * an attribute access ``.aggregated_output`` / ``.output`` / ``.stdout`` / ``.stderr`` |
| 16 | + (e.g. ``command_item.aggregated_output[:100]``) |
| 17 | +
|
| 18 | +Store the output whole (it is already bounded by the harness's own exec-output |
| 19 | +truncation) and trim for DISPLAY in the renderers/reports instead. |
| 20 | +
|
| 21 | +Add ``# noqa: CE043`` on the offending line for a genuinely non-recorded use |
| 22 | +(e.g. slicing stdout only to build a short crash/log message that never becomes a |
| 23 | +``result_summary``), with a comment explaining why. |
| 24 | +""" |
| 25 | + |
| 26 | +import ast |
| 27 | + |
| 28 | +from tests.lint.rules.base import BaseRule |
| 29 | + |
| 30 | + |
| 31 | +_OUTPUT_NAMES = {"output", "stdout", "stderr", "aggregated_output"} |
| 32 | + |
| 33 | + |
| 34 | +def _denotes_output(value: ast.AST) -> str | None: |
| 35 | + """Return the output-ish identifier being sliced, or None.""" |
| 36 | + if isinstance(value, ast.Name): |
| 37 | + low = value.id.lower() |
| 38 | + if low in _OUTPUT_NAMES or low.endswith("_output") or low.endswith("_stdout") or low.endswith("_stderr"): |
| 39 | + return value.id |
| 40 | + elif isinstance(value, ast.Attribute): |
| 41 | + if value.attr.lower() in _OUTPUT_NAMES: |
| 42 | + return value.attr |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +def _is_const_upper_slice(sl: ast.AST) -> bool: |
| 47 | + """True for ``[:N]`` / ``[:N:...]`` with a constant int upper bound.""" |
| 48 | + return ( |
| 49 | + isinstance(sl, ast.Slice) |
| 50 | + and sl.lower is None |
| 51 | + and isinstance(sl.upper, ast.Constant) |
| 52 | + and isinstance(sl.upper.value, int) |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +class NoCommandOutputTruncation(BaseRule): |
| 57 | + id = "CE043" |
| 58 | + |
| 59 | + def check(self, tree: ast.AST) -> list: |
| 60 | + # Scope to the agent implementations — only they record CommandTelemetry. |
| 61 | + if "/agents/" not in self.filepath.replace("\\", "/"): |
| 62 | + return [] |
| 63 | + self.visit(tree) |
| 64 | + return self.violations |
| 65 | + |
| 66 | + def visit_Subscript(self, node: ast.Subscript) -> None: |
| 67 | + name = _denotes_output(node.value) |
| 68 | + if name is not None and _is_const_upper_slice(node.slice): |
| 69 | + self.violation( |
| 70 | + node, |
| 71 | + f"Do not truncate command output ({name}[:...]); CommandTelemetry.result_summary " |
| 72 | + "must stay whole (it drives result_tokens). Store the full output and trim for " |
| 73 | + "display in the renderers/reports instead.", |
| 74 | + ) |
| 75 | + self.generic_visit(node) |
0 commit comments