Skip to content

Bug (Workflow): projectText ignores maxLines and duplicates input during line-triggered projection #329

Closed

Description

@JuiXiang

Summary

projectText(content, { maxBytes, maxLines, recovery }) does not reliably enforce maxLines.

When projection is triggered only because the input exceeds the line limit—but remains well below the byte limit—the current implementation may select the entire input as both the head and the tail. The resulting projection duplicates the source text and can greatly exceed maxLines while still passing the final byte-only check.

Reproduced against upstream main at commit:

5ad7c6e3b4f268a29413764a9fe10c7bb502d56b

Minimal reproduction

import { Buffer } from "node:buffer";
import { projectText } from "./extensions/shared/text-projection.ts";

const content = Array.from(
  { length: 40 },
  (_, index) => `L${index}`,
).join("\n");

const projected = projectText(content, {
  maxBytes: 2048,
  maxLines: 3,
  recovery: "kept in artifacts",
});

console.log({
  inputBytes: Buffer.byteLength(content, "utf8"),
  inputLines: content.split("\n").length,
  outputBytes: Buffer.byteLength(projected, "utf8"),
  outputLines: projected.split("\n").length,
});

console.log(projected);

Actual behavior

inputBytes:  149
inputLines:  40
outputBytes: 416
outputLines: 85

The projected output is effectively:

L0
...
L39

[... middle omitted ...]

L0
...
L39

[Projection bounded: showing 298 B of 149 B across the head and tail. kept in artifacts]

The complete 40-line input appears once as the head and again as the tail.

The footer also reports showing 298 B of 149 B, which demonstrates that the selected head and tail overlap.

The result satisfies the byte limit but violates the three-line limit by 82 lines.

Expected behavior

maxBytes and maxLines should be simultaneous hard bounds:

outputBytes <= maxBytes
AND
outputLines <= maxLines

For the reproduction above, a reasonable compact projection would be:

L0
[... middle omitted ...]
L39

The exact formatting is less important than preserving these properties:

  • the output does not exceed either configured limit;
  • omitted content is indicated explicitly;
  • head and tail selections do not overlap;
  • useful beginning and ending context is retained when the budget permits;
  • the exact result remains recoverable from Workflow artifacts.

Root cause

The projection pipeline applies different constraints at different stages:

  1. The initial truncateHead probe checks both maxBytes and maxLines.
  2. Once truncation is required, head and tail selection uses byte-only helpers.
  3. The final acceptance check verifies only Buffer.byteLength(projected).
  4. Therefore, a line-only overflow is detected initially but the line constraint is lost during transformation and final validation.

With a 2048-byte budget, the current 75%/25% split gives approximately:

head budget: 1536 bytes
tail budget:  512 bytes

Because the complete input is only 149 bytes, it fits independently inside both budgets. Both selectors therefore return the complete input.

Impact

projectText is currently used by several Workflow projections, including:

  • completed Workflow results delivered to the parent model;
  • background Workflow completion batches;
  • upstream results appended to downstream Workflow prompts;
  • expanded operator-facing completion reports.

This does not appear to corrupt the canonical Workflow result or its persisted audit artifacts. It affects bounded projections derived from that result.

Possible consequences include:

  • model context unexpectedly exceeding the intended line budget;
  • duplicated evidence influencing model interpretation;
  • unnecessary context/token consumption;
  • misleading projection telemetry;
  • operator-facing reports exceeding their documented bounds.

The smallest reproduction uses maxLines: 3, but production call sites use larger limits such as 200, 400, and 600. Outputs containing many short lines—test summaries, file lists, logs, or compact records—can still trigger the same class of failure.

Why this is easy to miss

Most large-output tests naturally exceed the byte limit as well as the line limit. In that situation, byte-based head/tail selection usually produces two distinct slices and appears correct.

The defect is exposed specifically by a different input shape:

many lines + few bytes per line

This is a useful general testing lesson for APIs with multiple limits: each limit needs an independent test where only that dimension exceeds its bound.

A { maxBytes, maxLines } contract is conjunctive. Both constraints must remain active during:

  1. overflow detection;
  2. budget allocation;
  3. content selection;
  4. framing/footer construction;
  5. final validation.

Checking both limits only at the beginning is insufficient.

The invariant below would also have exposed the overlap in this case:

reported source bytes shown <= total source bytes

Acceptance criteria

  • Content that fits within both limits is returned byte-for-byte unchanged.
  • A projection caused only by maxLines does not exceed maxLines.
  • A projection caused only by maxBytes does not exceed maxBytes.
  • When both limits are exceeded, the result satisfies both limits.
  • Head and tail selection does not duplicate overlapping source ranges.
  • Framing lines, omission markers, footers, and multi-line recovery text are included in the line budget.
  • Very small positive line budgets still produce an explicit bounded/omission representation.
  • Zero-byte and zero-line budgets produce an empty projection.
  • UTF-8 characters are not split or replaced with �.
  • Tests cover LF, CRLF, ASCII, CJK text, and emoji.
  • A regression test covers the exact 40-line, 149-byte reproduction above.

Scope

This issue is about enforcing the existing projection contract.

It does not require:

  • changing Workflow execution semantics;
  • changing canonical result persistence;
  • removing full audit artifacts;
  • introducing a new global resource or artifact abstraction;
  • changing the configured production limits themselves.

Related issues

Activity

  1. JuiXiang commented on Sep 1, 2026

    @JuiXiang
    ContributorAuthor

    I’d like to take this issue. I have reproduced it consistently and am preparing a minimal fix with focused regression tests covering line-only overflow, small budgets, UTF-8 text, and LF/CRLF inputs. I’ll open a PR and link it here once it is ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions