THU-MAIC / THU-MAIC/OpenMAIC

wb_edit_code replace_lines corrupts the code block when lineIds are out of document order

Open
#653 0 comments 0 reactions 0 assignees View on GitHub
area:editor bug priority:P1 status:in-progress
Dominant language
TypeScript
Stars
37.2k
Forks
5.9k
Avg merge
1d 3h
Merged PRs (30d)
195

Description

`ActionEngine.executeWbEditCode`'s `replace_lines` operation (`lib/action/engine.ts`) computes the splice anchor as `lines.findIndex(l => l.id === replaceIds[0])` **before** filtering out the replaced lines. `WbEditCodeAction.lineIds` (`lib/types/action.ts`) is a plain `string[]` with no ordering contract, and the agent producing it may emit IDs out of document order.

When `replaceIds[0]` is not the topmost replaced line (e.g. `["L5", "L2"]`), the subsequent `.filter()` removes a line **above** the anchor, so the saved index goes stale and the replacement lines are spliced at the wrong position — corrupting the displayed code block.

### Reproduction (logic)
Lines `[A, B, C, D, E]`, `replace_lines` with `lineIds: ["D", "B"]`, content `"X"` → produces `[A, C, E, X]` (X appended at the end) instead of `[A, X, C, E]` (X where B was).

### Suggested fix
Anchor the insertion at the topmost (minimum original index) replaced line. PR to follow.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.