Skip to content

fix(compress): repair missing closing brace on last content entry - #254

Merged
ranxianglei merged 1 commit into
masterfrom
2026-08-30_compress-tail-repair-missing-brace
Sep 1, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-08-30_compress-tail-repair-missing-brace

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Summary

Fixes #253.

With Qwen-family models in non-strict tool-call mode, the compress tool's content argument arrives as a JSON-encoded array string whose last entry object is missing its closing } (the tail is \" + ] instead of \" + }]). JSON.parse rejects the whole string, and the tool surfaced a misleading error — content must be an ARRAY — even though the input is an array. This sent capable models into restructuring the payload instead of fixing the brace.

Changes (src/compress-tool.ts only)

  1. Deterministic tail-repair — tailRepair() / repairContentTail(): when args.content is a string that fails to parse and its trimmed form ends with \"] (preceded by \"), re-append } before the final ] and retry. A no-op for well-formed input (a valid array never still parses after appending }), so there are no false positives. Wired into normalizeRanges before parseCompressArgs.
  2. Accurate error message — describeDiagnostics() now detects array/object-shaped-but-unparseable content and reports the parser's own diagnostic (the JSON failed to parse: Expecting ',' delimiter near char N) instead of must be an ARRAY, giving the model a retryable signal. The must be an ARRAY fallback is preserved for genuinely non-array-shaped garbage.

Why here and not in acp-kernel

The issue suggested the repair in acp-kernel's parseContentArray. I placed it in the adapter instead so this is a self-contained, single-repo fix — an acp-kernel change would require a cross-repo release (human PR merge + npm publish) before it could ship. The tail-repair could still be upstreamed to acp-kernel later so other hosts (opencode-acp) benefit; happy to open that as a follow-up if you want.

Verification

Notes

  • Content commit — no version bump (release-branch territory).
  • Not merging (human-only).

When non-strict tool-call providers (Qwen-family) emit the compress
content array as a JSON string with the last entry's closing '}'
dropped, JSON.parse fails and the tool reported a misleading
'content must be an ARRAY' error.

- tailRepair/repairContentTail: deterministically re-append '}' before
  the final ']' when the trimmed string ends with '"]' preceded by '"';
  a no-op for well-formed input (no false positives).
- describeDiagnostics: for array/object-shaped-but-unparseable content,
  surface the parser's own diagnostic ('the JSON failed to parse: ...')
  instead of 'must be an ARRAY', giving the model a retryable signal.

Closes #253
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Extension Artifact

Branch: 2026-08-30_compress-tail-repair-missing-brace (812ddc0)

Option A — Install from npm PR tag (recommended)

pi install npm:billion-context-pi@pr-254

Each push to this PR publishes a new version under the pr-254 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pi-pr254.tgz
pi install ./package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] Reviewed & verified — ready to merge (merge left to you, human-only).

I didn't just trust the PR description — I re-ran everything on the branch (2026-08-30_compress-tail-repair-missing-brace, commit 812ddc0) and added a before/after reproduction to prove the new test is meaningful.

Verification (all green, matches the claims)

  • npm run typecheck — clean
  • npm test — 439 pass, 0 fail (incl. the 11 new tests/compress-tail-repair.test.ts)
  • npm run build — clean, dist/index.js 521.71 KB, repair code present in the bundle

Before/after (the part I added)

Replayed the exact #253 payload (single entry, ~6 KB summary, dropped closing }) through the real compress tool execute path on both trees:

  • master: rejects with Invalid compress content (no-valid-ranges): … content must be an ARRAY of {startId, endId, summary} objects. — the misleading error, even though the input is an array.
  • PR branch: the same payload is repaired and compresses successfully (returns the ▣ ACP | panel, no parse error).

So the e2e test genuinely reproduces the bug and the fix resolves it — not passing vacuously.

Code review (traced the logic, no issues)

  • tailRepair is sound and false-positive-free: a valid object array ends in }] (body ends in }, not ") so it never reaches the parse retry; string arrays / empty arrays / non-array shapes / mid-array defects all fall through untouched. I traced each case.
  • repairContentTail is pure — only touches string content, returns args unchanged when the repair doesn't apply, so well-formed input is byte-for-byte unaffected.
  • describeDiagnostics ordering is correct: invalidItems first, then the new jsonParseError (parser's own diagnostic), then the must be an ARRAY fallback. A genuinely non-array shape (e.g. a single JSON object, or [1,2,3]) still gets the right message.
  • normalizeRanges is now exported for testability — acceptable.
  • Test conventions match the repo (harness any types, /tmp/pai-acp-* state files are established patterns).

Notes

  • Content commit, no version bump — correct per spec.
  • Upstreaming the tail-repair to acp-kernel's parseContentArray later (so opencode-acp benefits) is a sensible optional follow-up — happy to open that issue if you want it tracked.

LGTM. Merging is yours.

@ranxianglei
ranxianglei merged commit 4d7bce4 into master Sep 1, 2026
9 checks passed
This was referenced Sep 1, 2026
HangxiangMa pushed a commit to HangxiangMa/billion-context-pi that referenced this pull request Sep 21, 2026
…ss-tail-repair-missing-brace

fix(compress): repair missing closing brace on last content entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant