Skip to content

Preserve plus-prefixed content in file creation patches - #1758

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/create-patch-plus-lines
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/create-patch-plus-lines

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

apply_patch's create_file parser strips every line starting with +++ as a unified-diff header. An added source line such as ++counter is encoded as +++counter, so it is silently dropped. The tool still reports success and writes a file missing those statements. This also affects content such as ++ counter, whose encoded line includes the space used by a real +++ file header.

Changes

  • Strip a leading paired --- / +++ file header rather than filtering those prefixes throughout the added content. Keep the existing hunk and Codex wrapper handling.
  • Add one regression through applyPatchTool with real temporary files. It checks exact file contents and successful results for the same plus-prefixed source in bare and unified-header create payloads.

Verification

Windows x64, Bun 1.3.14, base c2ce3b69fdbb4a691dfd7d39edc2cb8943d85cc1. Commands use --no-env-file and the repository's placeholder-only sdk/test/setup-env.ts preload in an isolated test home.

  • bun test sdk/src/__tests__/apply-patch.test.ts -t 'preserves added lines that resemble diff headers': before 0 pass, 1 fail; after 1 pass, 0 fail. Before the fix, the written file contains only console.log(counter) instead of all four source lines.
  • bun test sdk/src/__tests__/apply-patch.test.ts sdk/src/__tests__/change-file.test.ts: before 1 pass, 21 fail with the new regression; after 2 pass, 20 fail. The remaining 20 failure names are identical before and after. Existing fixtures use POSIX /repo paths and expectations on Windows; this is not a passing whole-suite claim.
  • bun node_modules/typescript/bin/tsc --noEmit -p sdk: passed.
  • ESLint on both changed files: 0 errors, 5 pre-existing warnings. Prettier comparison against main has the same 7 existing formatting hunks; the added code introduces none. git diff --check: passed.
  • bun install --frozen-lockfile: passed, no dependency changes.
  • SDK build (cd sdk && bun scripts/build.ts), bun freebuff/cli/build.ts 0.0.0-ci, binary --version, and bun cli/scripts/smoke-binary.ts cli/bin/freebuff.exe: passed.

Notes / Scope

This is a source-audit finding with no existing issue being claimed. Only create-payload parsing changes; update/delete operations, path checks and runtime permissions retain their existing behavior. Linux/macOS and the full monorepo suite were not run. The regression exercises the parser and actual filesystem directly without an account, model request or module mock.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch. The old parseCreateDiff filtered every line starting with ---, +++, @@, or *** anywhere in the payload, so a legitimately added source line like ++counter (encoded as +++counter once the diff's own leading + marker is applied) was silently dropped — the tool still reported success while writing a truncated file. That's a real, user-visible correctness bug.

The fix in sdk/src/tools/apply-patch.ts is appropriately narrow: it only treats the first two lines as a unified header pair when they match the real header shape (--- and +++ with trailing spaces, as actual diff tools emit), and otherwise keeps all lines as content. That's a more precise heuristic than prefix-matching every line, and it still preserves support for wrapped unified create payloads.

The added test in apply-patch.test.ts is solid — it exercises both the bare and unified-header create paths through the real tool with a temp directory, asserts exact file contents, and would have failed under the old implementation per your repro notes. Good that you verified tsc/eslint/prettier and ran the existing suite to confirm no regressions beyond pre-existing failures.

One thing to double check before porting: the detection only looks at the very first two lines for the header pair. If a create diff ever has leading blank lines or a different wrapper ordering before the ---/+++ pair (e.g., from the Codex wrapper mentioned in your notes), the header might not be recognized and would then be treated as content. Worth a quick look at how isWrappedAtHeader/Codex handling composes with this function to make sure there's no case where headers land past index 0/1.

Overall this is a well-scoped, well-tested fix in-scope for the SDK. Recommending it for porting.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 28, 2026

This branch has not been deployed

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

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants