firecrawl / firecrawl/open-lovable

apply-ai-code parser silently drops a file and corrupts another when a </file> tag is missing

Open
#215 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
28.4k
Forks
5.4k
PR merge metrics
No merged PRs in 30d

Description

## Summary

The file-block parser in `apply-ai-code` silently **drops a file and corrupts another** when the model's output is missing a `` closing tag on an earlier `` block. The dropped file is never written, and the corrupted file is marked as "complete" so no truncation warning is emitted.

## Location

- `app/api/apply-ai-code/route.ts:32` (`parseAIResponse`)
- `app/api/apply-ai-code-stream/route.ts:70` (same regex)
- `app/api/generate-ai-code-stream/route.ts:1568` (strict variant of the same parser)

```ts
const fileRegex = /([\s\S]*?)(?:<\/file>|$)/g;
```

## Root cause

The lazy `[\s\S]*?` stops at the first place the trailing group can match. If an earlier `` block is missing its ``, the only `` (or end-of-string) is *after a later file*, so the first capture spans across the next file's opening tag — absorbing that later file's tag + body and never emitting it as its own entry. There is no `(?=`):

```
AAABBB
```

Current parse result:

```json
[{ "path": "src/A.jsx", "content": "AAABBB", "hasClosingTag": true }]
```

`src/B.jsx` is dropped entirely, and `src/A.jsx` is written with the next file's tag embedded in its body — and flagged complete, so no warning.

## Impact

A single malformed/truncated `` (common with LLM output) silently discards a whole file the user asked for and writes garbage into another, with no diagnostic. The user sees a broken app and no indication a file went missing.

## Suggested fix

Add a `(?=([\s\S]*?)(?:<\/file>|$)/g;
+const fileRegex = /([\s\S]*?)(?:<\/file>|(?=

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with parseAIResponse in app/api/apply-ai-code/route.ts:32 and compare the matching logic with app/api/apply-ai-code-stream/route.ts:70 and app/api/generate-ai-code-stream/route.ts:1568. Reproduce the missing-closing-tag example, then verify that each file remains a separate entry, the earlier file is marked incomplete, and well-formed or final-truncated input still behaves as before.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.