Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
/**
* Adapts the legacy tuple-shaped patch input used in tests to the new
* `LineReplacement` + `AbstractText` API surface of `tryRemoveDuplicateAdditions`.
*
* Builds a `StringText` from the file lines (joined with `\n`, no synthetic
* trailing newline added) so `splitLines` round-trips back to the same array.
*/
function
callDedup
(
:
{
:
string
[
]
;
:
string
[
]
;
:
number
}
,
:
string
[
]
,
)
:
|
undefined
{
const
=
new
StringText
(
.
join
(
'\n'
)
)
;
const
=
new
LineReplacement
(
new
LineRange
(
.
+
1
,
.
+
1
+
.
.
)
,
.
,
)
;
return
tryRemoveDuplicateAdditions
(
,
)
;
}
describe
(
'XtabCustomDiffPatchResponseHandler'
,
(
)
=>
{
beforeEach
(
(
)
=>
{
ensureDependenciesAreSet
(
)
;
}
)
;
async
function
collectPatches
(
:
string
)
:
Promise
<
string
>
{
const
=
.
fromArray
(
.
split
(
'\n'
)
)
;
const
=
await
.
toArray
(
.
extractEdits
(
)
)
;
return
.
map
(
=>
.
toString
(
)
)
.
join
(
'\n'
)
;
}
it
(
'should parse a simple patch correctly'
,
async
(
)
=>
{
const
=
`file1.txt:10
-Old line 1
-Old line 2
+New line 1
+New line 2`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toEqual
(
)
;
}
)
;
it
(
'should parse a simple patch correctly despite trailing newline'
,
async
(
)
=>
{
const
=
`file1.txt:10
-Old line 1
-Old line 2
+New line 1
+New line 2
`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toEqual
(
.
trim
(
)
)
;
}
)
;
it
(
'should parse a simple patch correctly'
,
async
(
)
=>
{
const
=
`/absolutePath/to/my_file.ts:1
-Old line 1
+New line 1
+New line 2
relative/path/to/another_file.js:42
-Removed line
+Added line`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toEqual
(
)
;
}
)
;
.
skipIf
(
!
)
(
'applies a patch for a Windows path whose parent folder contains a space'
,
async
(
)
=>
{
const
=
URI
.
file
(
'C:\\workspace'
)
;
const
=
.
create
(
URI
.
file
(
'C:\\workspace\\space folder\\test.py'
)
.
toString
(
)
)
;
const
=
new
CurrentDocument
(
new
StringText
(
'def my_function'
)
,
new
Position
(
1
,
16
)
)
;
const
=
.
fromArray
(
[
'space folder/test.py:0'
,
'-def my_function'
,
'+def my_function():'
,
'+ pass'
,
]
)
;
const
{
}
=
await
consumeHandleResponse
(
,
,
,
,
undefined
,
new
TestLogService
(
)
,
.
,
true
,
)
;
expect
(
.
map
(
=>
(
{
:
.
.
toString
(
)
,
:
.
.
.
toString
(
)
,
:
.
.
,
}
)
)
)
.
toEqual
(
[
{
:
'file:///c%3A/workspace/space%20folder/test.py'
,
:
'[1,2)'
,
:
[
'def my_function():'
]
,
}
,
{
:
'file:///c%3A/workspace/space%20folder/test.py'
,
:
'[2,2)'
,
:
[
' pass'
]
,
}
]
)
;
}
)
;
it
(
'discard a patch if no valid header'
,
async
(
)
=>
{
const
=
`myFile.ts:
+New line 1
+New line 2
another_file.js:32
-Removed line
+Added line`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toMatchInlineSnapshot
(
`
"another_file.js:32
-Removed line
+Added line"
`
)
;
}
)
;
it
(
'discard a patch if no valid header - 2'
,
async
(
)
=>
{
const
=
`myFile.ts:42
+New line 1
+New line 2
another_file.js:
-Removed line
+Added line`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toMatchInlineSnapshot
(
`
"myFile.ts:42
+New line 1
+New line 2"
`
)
;
}
)
;
it
(
'discard a patch has no removed lines'
,
async
(
)
=>
{
const
=
`myFile.ts:42
+New line 1
+New line 2`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toMatchInlineSnapshot
(
`
"myFile.ts:42
+New line 1
+New line 2"
`
)
;
}
)
;
it
(
'discard a patch has no new lines'
,
async
(
)
=>
{
const
=
`myFile.ts:42
-Old line 1
-Old line 2`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toMatchInlineSnapshot
(
`
"myFile.ts:42
-Old line 1
-Old line 2"
`
)
;
}
)
;
it
(
'no-op diff'
,
async
(
)
=>
{
const
=
`myFile.ts:42
-Old line 1
+Old line 1`
;
const
=
await
collectPatches
(
)
;
expect
(
)
.
toMatchInlineSnapshot
(
`
"myFile.ts:42
-Old line 1
+Old line 1"
`
)
;
}
)
;
it
(
'stops yielding edits when stream rejects with FetchStreamError'
,
async
(
)
=>
{
const
=
new
NoNextEditReason
.
GotCancelled
(
'afterFetchCall'
)
;
const
=
.
create
(
'file:///file.ts'
)
;
const
=
new
CurrentDocument
(
new
StringText
(
'old\n'
)
,
new
Position
(
1
,
1
)
)
;
// Emit the first patch completely, then reject with FetchStreamError
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/file.ts:0'
;
yield
'-old'
;
yield
'+new'
;
yield
'/file.ts:5'
;
yield
'-another old'
;
yield
'+another new'
;
throw
new
FetchStreamError
(
)
;
}
const
{
,
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
expect
(
)
.
toEqual
(
)
;
}
)
;
it
(
'returns FetchStreamError reason when stream rejects before any patches'
,
async
(
)
=>
{
const
=
new
NoNextEditReason
.
GotCancelled
(
'afterFetchCall'
)
;
const
=
.
create
(
'file:///file.ts'
)
;
const
=
new
CurrentDocument
(
new
StringText
(
'old\n'
)
,
new
Position
(
1
,
1
)
)
;
const
=
new
AsyncIterableSource<string>
(
)
;
.
reject
(
new
FetchStreamError
(
)
)
;
const
{
,
}
=
await
consumeHandleResponse
(
.
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
)
;
expect
(
)
.
toHaveLength
(
0
)
;
expect
(
)
.
toBe
(
)
;
}
)
;
describe
(
'tryRemoveDuplicateAdditions'
,
(
)
=>
{
it
(
'removes a single trailing duplicate (closing brace case)'
,
(
)
=>
{
// File:
// 0: function foo() {
// 1: let x = 1;
// 2: }
//
// Patch replaces line 1 and tries to add `let y = 2;` after it,
// but mistakenly re-emits the closing brace.
const
=
[
'function foo() {'
,
' let x = 1;'
,
'}'
]
;
const
=
callDedup
(
{
:
[
' let x = 1;'
,
' let y = 2;'
,
'}'
]
,
:
[
' let x = 1;'
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
' let x = 1;'
,
' let y = 2;'
]
)
;
expect
(
?.
)
.
toEqual
(
[
'}'
]
)
;
}
)
;
it
(
'removes a multi-line trailing duplicate'
,
(
)
=>
{
const
=
[
'a'
,
'b'
,
'c'
,
'd'
,
'e'
]
;
// Patch at line 1 replaces `b`. Additions repeat `b`, `c`, `d`.
// `c`, `d` are the lines following the deletion and would be duplicated.
const
=
callDedup
(
{
:
[
'B'
,
'c'
,
'd'
]
,
:
[
'b'
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
'B'
]
)
;
expect
(
?.
)
.
toEqual
(
[
'c'
,
'd'
]
)
;
}
)
;
it
(
'picks the longest suffix match'
,
(
)
=>
{
// File: ['a', 'b', 'c'] following the patch.
// Additions end with ['a', 'b', 'c'] — both k=1 and k=3 would match
// (k=3 only because the whole following matches), but the longest
// match wins to remove as much duplication as possible.
const
=
[
'x'
,
'a'
,
'b'
,
'c'
]
;
const
=
callDedup
(
{
:
[
'new'
,
'a'
,
'b'
,
'c'
]
,
:
[
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
'new'
]
)
;
expect
(
?.
)
.
toEqual
(
[
'a'
,
'b'
,
'c'
]
)
;
}
)
;
it
(
'removes a leading duplicate when the line is meaningful'
,
(
)
=>
{
// File:
// 0: header
// 1: existing line
// 2: trailer
//
// Patch at line 1 with no removals tries to add `existing line`.
const
=
[
'header'
,
'existing line'
,
'trailer'
]
;
const
=
callDedup
(
{
:
[
'existing line'
,
'extra'
]
,
:
[
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'prefix'
)
;
expect
(
?.
)
.
toEqual
(
[
'extra'
]
)
;
expect
(
?.
)
.
toEqual
(
[
'existing line'
]
)
;
}
)
;
it
(
'drops a duplicate inner `}` when the patch is purely additive (headline bug fix)'
,
(
)
=>
{
// Headline bug case: file ends with a `}`, model emits an extra
// `}` addition with no removals at the same line. Adding it would
// produce two adjacent `}` where one is enough — this is a
// duplicate of the existing closing token and must be trimmed.
const
=
[
'function f() {'
,
' doIt();'
,
'}'
]
;
const
=
callDedup
(
{
:
[
'}'
]
,
:
[
]
,
:
2
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
]
)
;
expect
(
?.
)
.
toEqual
(
[
'}'
]
)
;
}
)
;
it
(
'drops a duplicate `}` even when the patch removes content first'
,
(
)
=>
{
// Restructure-shape variant of the headline bug. The previous
// guard preserved this case (k===addedLines.length, all
// non-meaningful, removedLines>0) on the rationale of "legitimate
// inner-scope close". In practice the result still has two
// adjacent `}` after applying — that's a duplicate. We always
// trim, accepting the rare false positive of a mid-typing
// unbalanced file where the model is correctly closing.
const
=
[
'function outer() {'
,
' body();'
,
'}'
]
;
const
=
callDedup
(
{
:
[
'}'
]
,
:
[
' body();'
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
]
)
;
expect
(
?.
)
.
toEqual
(
[
'}'
]
)
;
}
)
;
it
(
'does not drop a leading whitespace-only duplicate'
,
(
)
=>
{
const
=
[
''
,
''
,
'existing'
]
;
const
=
callDedup
(
{
:
[
''
,
'first new line'
]
,
:
[
]
,
:
1
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
it
(
'removes streamEdits-style middle duplicate but preserves new content after the matched range'
,
(
)
=>
{
// Additions partially copy continuation starting at offset 2
// ('cont1 long', 'cont2 long' match the file's following lines),
// but 'extra' is genuinely new and must be preserved. The middle
// shape only drops the verified-equal range — not everything
// trailing.
const
=
[
'x'
,
'cont1 long'
,
'cont2 long'
,
'y'
]
;
const
=
callDedup
(
{
:
[
'new1'
,
'new2'
,
'cont1 long'
,
'cont2 long'
,
'extra'
]
,
:
[
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'middle'
)
;
expect
(
?.
)
.
toEqual
(
[
'new1'
,
'new2'
,
'extra'
]
)
;
expect
(
?.
)
.
toEqual
(
[
'cont1 long'
,
'cont2 long'
]
)
;
}
)
;
it
(
'greedily extends a middle match while the streamEdits regenerated context continues'
,
(
)
=>
{
// When more than two consecutive following lines match, the
// match should extend to cover all of them so we drop the
// entire regenerated continuation block.
const
=
[
'x'
,
'cont1 long'
,
'cont2 long'
,
'cont3 long'
,
'y'
]
;
const
=
callDedup
(
{
:
[
'new1'
,
'new2'
,
'cont1 long'
,
'cont2 long'
,
'cont3 long'
,
'tail'
]
,
:
[
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'middle'
)
;
expect
(
?.
)
.
toEqual
(
[
'new1'
,
'new2'
,
'tail'
]
)
;
expect
(
?.
)
.
toEqual
(
[
'cont1 long'
,
'cont2 long'
,
'cont3 long'
]
)
;
}
)
;
it
(
'does not match a middle pair when the following lines are not meaningful'
,
(
)
=>
{
// Two consecutive blank lines are too common to safely match on.
const
=
[
'x'
,
''
,
''
,
'y'
]
;
const
=
callDedup
(
{
:
[
'new1'
,
'new2'
,
''
,
''
,
'extra'
]
,
:
[
]
,
:
1
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
it
(
'returns undefined when no duplicate is detected'
,
(
)
=>
{
const
=
[
'unrelated1'
,
'unrelated2'
]
;
const
=
callDedup
(
{
:
[
'new1'
,
'new2'
]
,
:
[
]
,
:
0
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
it
(
'returns undefined when there are no following lines'
,
(
)
=>
{
const
=
[
'only line'
]
;
const
=
callDedup
(
{
:
[
'new1'
]
,
:
[
'only line'
]
,
:
0
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
it
(
'returns undefined when there are no additions'
,
(
)
=>
{
const
=
[
'a'
,
'b'
]
;
const
=
callDedup
(
{
:
[
]
,
:
[
'a'
]
,
:
0
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
it
(
'handles patch at the very end of the file (no following lines)'
,
(
)
=>
{
const
=
[
'line0'
,
'line1'
]
;
// Replacing the last line with no extra following content.
const
=
callDedup
(
{
:
[
'new1'
]
,
:
[
'line1'
]
,
:
1
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
it
(
'does not chain shapes — suffix match returns immediately without firing middle'
,
(
)
=>
{
// Without single-shape semantics, the original implementation could
// trim a suffix and then the (now shortened) additions would expose
// a "middle" pair that incorrectly truncated even more lines.
//
// File: outer-scope code where ' }', '}' appear before nextLine.
// Additions: real new code followed by ' }', '}', ' }', '}'.
// The longest-matching suffix is k=2 (matches the 2-line following
// `[' }', '}']`), so only those last 2 lines should be removed.
const
=
[
' }'
,
'}'
,
'nextLine'
]
;
const
=
callDedup
(
{
:
[
' newCode();'
,
' }'
,
'}'
,
' }'
,
'}'
]
,
:
[
]
,
:
0
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
' newCode();'
,
' }'
,
'}'
]
)
;
expect
(
?.
)
.
toEqual
(
[
' }'
,
'}'
]
)
;
}
)
;
it
(
'handles file without trailing newline'
,
(
)
=>
{
// `splitLines('a\\nb\\nc')` returns ['a','b','c'] (no trailing empty).
// The dedup must still work correctly against this shape.
const
=
'function foo() {\n let x = 1;\n}'
.
split
(
'\n'
)
;
const
=
callDedup
(
{
:
[
' let x = 1;'
,
' let y = 2;'
,
'}'
]
,
:
[
' let x = 1;'
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
' let x = 1;'
,
' let y = 2;'
]
)
;
}
)
;
it
(
'handles file with trailing newline (extra empty line at end)'
,
(
)
=>
{
// `splitLines('a\\nb\\nc\\n')` returns ['a','b','c',''] — the trailing
// empty string can become a "following" line and must not cause
// false positives via the prefix/middle checks.
const
=
'function foo() {\n let x = 1;\n}\n'
.
split
(
'\n'
)
;
const
=
callDedup
(
{
:
[
' let x = 1;'
,
' let y = 2;'
,
'}'
]
,
:
[
' let x = 1;'
]
,
:
1
,
}
,
,
)
;
expect
(
?.
)
.
toBe
(
'suffix'
)
;
expect
(
?.
)
.
toEqual
(
[
' let x = 1;'
,
' let y = 2;'
]
)
;
}
)
;
it
(
'returns undefined for an out-of-range lineNumZeroBased'
,
(
)
=>
{
const
=
[
'a'
,
'b'
]
;
const
=
callDedup
(
{
:
[
'x'
]
,
:
[
]
,
:
100
,
}
,
,
)
;
expect
(
)
.
toBeUndefined
(
)
;
}
)
;
}
)
;
describe
(
'handleResponse with duplicateAdditionsMode'
,
(
)
=>
{
it
(
'strips trailing duplicate addition in TrimDuplicate mode'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
1
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+ let y = 2;'
;
yield
'+}'
;
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
const
=
[
0
]
.
as
;
expect
(
.
)
.
toEqual
(
[
' let x = 1;'
,
' let y = 2;'
]
)
;
}
)
;
it
(
'does not strip duplicates in Off mode (default)'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
1
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+ let y = 2;'
;
yield
'+}'
;
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
const
=
[
0
]
.
as
;
expect
(
.
)
.
toEqual
(
[
' let x = 1;'
,
' let y = 2;'
,
'}'
]
)
;
}
)
;
it
(
'returns NoSuggestions (not FilteredOut) when every patch is dropped by TrimDuplicate dedup'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n let y = 2;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
1
,
1
)
)
;
// Patch adds 'let x = 1;' at line 1 with no removals — but that
// line already exists at line 1 (prefix match against a meaningful line).
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'+ let x = 1;'
;
}
const
{
,
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
expect
(
)
.
toHaveLength
(
0
)
;
// No more FilteredOut: cache and cursor-jump retry are preserved.
expect
(
)
.
toBeInstanceOf
(
.
)
;
}
)
;
it
(
'returns NoSuggestions when the model produces no edits at all'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
new
CurrentDocument
(
new
StringText
(
'a\nb\n'
)
,
new
Position
(
1
,
1
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
// no patches
}
const
{
,
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
expect
(
)
.
toHaveLength
(
0
)
;
expect
(
)
.
toBeInstanceOf
(
.
)
;
}
)
;
it
(
'still returns NoSuggestions if at least one edit was yielded after dedup'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n let y = 2;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
1
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
// First patch survives (no duplicate). Second patch adds a
// meaningful prefix duplicate of an existing line — drops to no-op.
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let z = 99;'
;
yield
'/test.ts:2'
;
yield
'+ let y = 2;'
;
}
const
{
,
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
expect
(
)
.
toBeInstanceOf
(
.
)
;
}
)
;
it
(
'skips dedup for non-active documents'
,
async
(
)
=>
{
const
=
.
create
(
'file:///active.ts'
)
;
const
=
'a\nb\nc\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
1
,
1
)
)
;
// Patch targets a different file; the active doc's content must
// not be used to dedup against, so the patch should be yielded as-is.
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/other.ts:0'
;
yield
'-old'
;
yield
'+a'
;
yield
'+b'
;
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
const
=
[
0
]
.
as
;
expect
(
.
)
.
toEqual
(
[
'a'
,
'b'
]
)
;
}
)
;
it
(
'DropPatch mode drops the offending patch but continues yielding subsequent patches'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\nfunction bar() {\n let y = 2;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
1
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
// First patch: duplicates the trailing `}` (suffix shape) — should be dropped.
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+}'
;
// Second patch: legitimate change to a different region — should be yielded.
yield
'/test.ts:4'
;
yield
'- let y = 2;'
;
yield
'+ let y = 42;'
;
}
const
{
,
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
const
=
[
0
]
.
as
;
expect
(
.
)
.
toEqual
(
[
' let y = 42;'
]
)
;
expect
(
)
.
toBeInstanceOf
(
.
)
;
}
)
;
it
(
'DropAllRemaining mode drops the offending patch and every subsequent patch'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\nfunction bar() {\n let y = 2;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
1
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
// First patch: legitimate, yielded as-is.
yield
'/test.ts:0'
;
yield
'-function foo() {'
;
yield
'+function fooRenamed() {'
;
// Second patch: duplicates trailing `}` — triggers DropAllRemaining.
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+}'
;
// Third patch: would have been legitimate, but must NOT be yielded.
yield
'/test.ts:4'
;
yield
'- let y = 2;'
;
yield
'+ let y = 42;'
;
}
const
{
,
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
)
;
// Only the patch yielded before the detection survives. The
// triggering patch and every subsequent patch are dropped.
expect
(
)
.
toHaveLength
(
1
)
;
const
=
[
0
]
.
as
;
expect
(
.
)
.
toEqual
(
[
'function fooRenamed() {'
]
)
;
expect
(
)
.
toBeInstanceOf
(
.
)
;
}
)
;
}
)
;
describe
(
'progressive ghost-text reveal via extractEdits'
,
(
)
=>
{
async
function
collectPatchesWithCursor
(
:
string
,
:
number
,
:
string
=
'file.py'
,
:
boolean
=
false
)
:
Promise
<
string
>
{
const
=
.
fromArray
(
.
split
(
'\n'
)
)
;
const
=
await
.
toArray
(
.
extractEdits
(
,
,
,
)
)
;
return
.
map
(
=>
.
toString
(
)
)
.
join
(
'\n'
)
;
}
it
(
'splits ghost-text patch into cursor-line replacement and continuation insertion'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
'+lineC'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'file.py:5'
,
'+lineB'
,
'+lineC'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'splits when removed line equals first added line'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-lineA'
,
'+lineA'
,
'+lineB'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA'
,
'file.py:5'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when cursor line does not match patch line'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
,
10
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when there are multiple removed lines'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineC'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineC'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when edit is not additive'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-hello world'
,
'+goodbye'
,
'+lineB'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-hello world'
,
'+goodbye'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'only splits the first patch, not subsequent ones'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
'file.py:10'
,
'-lineX'
,
'+lineX_extended'
,
'+lineY'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'file.py:5'
,
'+lineB'
,
'file.py:10'
,
'-lineX'
,
'+lineX_extended'
,
'+lineY'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'handles ghost-text with only one added line (no continuation patch)'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
]
.
join
(
'\n'
)
,
4
,
)
;
// Single added line: early patch is emitted, continuation is empty and not yielded
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when cursorLineZeroBased is not provided'
,
async
(
)
=>
{
const
=
.
fromArray
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
)
;
const
=
await
.
toArray
(
.
extractEdits
(
)
)
;
const
=
.
map
(
=>
.
toString
(
)
)
.
join
(
'\n'
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when patch targets a different file'
,
async
(
)
=>
{
const
=
await
collectPatchesWithCursor
(
[
'other.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
,
4
,
'file.py'
,
)
;
expect
(
)
.
toEqual
(
[
'other.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split second patch even if it matches cursor line'
,
async
(
)
=>
{
// First patch is NOT ghost-text (non-additive), second patch IS at cursor line and additive
const
=
await
collectPatchesWithCursor
(
[
'file.py:0'
,
'-import os'
,
'+import sys'
,
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:0'
,
'-import os'
,
'+import sys'
,
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
}
)
;
describe
(
'progressive ghost-text reveal (multi-line) via extractEdits'
,
(
)
=>
{
async
function
collectMultiLine
(
:
string
,
:
number
,
:
string
=
'file.py'
)
:
Promise
<
string
>
{
const
=
.
fromArray
(
.
split
(
'\n'
)
)
;
const
=
await
.
toArray
(
.
extractEdits
(
,
,
,
/* allowMultiLineRemoval */
true
)
)
;
return
.
map
(
=>
.
toString
(
)
)
.
join
(
'\n'
)
;
}
it
(
'splits a multi-removed-line patch into cursor-line replacement + continuation replacement'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineB_changed'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'file.py:5'
,
'-lineB'
,
'+lineB_changed'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split a multi-removed-line patch when allowMultiLineRemoval is off'
,
async
(
)
=>
{
// Same input, but with allowMultiLineRemoval defaulting to false.
const
=
.
fromArray
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineB_changed'
,
]
)
;
const
=
await
.
toArray
(
.
extractEdits
(
,
4
,
'file.py'
)
)
;
const
=
.
map
(
=>
.
toString
(
)
)
.
join
(
'\n'
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineB_changed'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'continuation is a deletion when fewer added than removed lines'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'file.py:5'
,
'-lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'continuation replaces one line with many when more added than removed lines'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineB1'
,
'+lineB2'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'file.py:5'
,
'-lineB'
,
'+lineB1'
,
'+lineB2'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'splits a single-removed-line patch identically when allowMultiLineRemoval is on'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'+lineB'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'+lineA_extended'
,
'file.py:5'
,
'+lineB'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when the first line edit is not additive'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-hello world'
,
'-lineB'
,
'+goodbye'
,
'+lineC'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-hello world'
,
'-lineB'
,
'+goodbye'
,
'+lineC'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split when the cursor line does not match the patch line'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineC'
,
]
.
join
(
'\n'
)
,
10
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-lineA'
,
'-lineB'
,
'+lineA_extended'
,
'+lineC'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'does not split an empty cursor line that merely absorbs the next line'
,
async
(
)
=>
{
// removed[0] is blank and the first added line equals removed[1] (the line just
// below the cursor): the model is deleting the empty line, not additively editing it.
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-'
,
'-foo()'
,
'+foo()'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-'
,
'-foo()'
,
'+foo()'
,
]
.
join
(
'\n'
)
)
;
}
)
;
it
(
'splits an empty cursor line when the next line is genuinely different'
,
async
(
)
=>
{
const
=
await
collectMultiLine
(
[
'file.py:4'
,
'-'
,
'-bar()'
,
'+foo()'
,
'+baz()'
,
]
.
join
(
'\n'
)
,
4
,
)
;
expect
(
)
.
toEqual
(
[
'file.py:4'
,
'-'
,
'+foo()'
,
'file.py:5'
,
'-bar()'
,
'+baz()'
,
]
.
join
(
'\n'
)
)
;
}
)
;
}
)
;
describe
(
'handleResponse with enableProgressiveGhostText'
,
(
)
=>
{
it
(
'yields two edits for ghost-text when progressive reveal is enabled'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\n'
;
// Cursor on line 2 (1-based) → cursorLineOffset = 1
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
5
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+ let y = 2;'
;
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
true
,
)
;
expect
(
)
.
toHaveLength
(
2
)
;
// First edit: cursor-line replacement
expect
(
[
0
]
.
)
.
toEqual
(
new
LineReplacement
(
new
LineRange
(
2
,
3
)
,
[
' let x = 1;'
]
)
)
;
// Second edit: continuation insertion
expect
(
[
1
]
.
)
.
toEqual
(
new
LineReplacement
(
new
LineRange
(
3
,
3
)
,
[
' let y = 2;'
]
)
)
;
}
)
;
it
(
'yields single edit when progressive reveal is disabled (default)'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
5
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+ let y = 2;'
;
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
)
;
expect
(
)
.
toHaveLength
(
1
)
;
expect
(
[
0
]
.
)
.
toEqual
(
new
LineReplacement
(
new
LineRange
(
2
,
3
)
,
[
' let x = 1;'
,
' let y = 2;'
]
)
)
;
}
)
;
it
(
'progressive reveal with TrimDuplicate trims continuation but keeps early edit'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
// Document: line 1 = "function foo() {", line 2 = " let x = 1;", line 3 = "}"
const
=
'function foo() {\n let x = 1;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
5
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+ let y = 2;'
;
yield
'+}'
;
// duplicates line 3
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
true
,
)
;
// Early edit is yielded, continuation has `}` trimmed by dedup
expect
(
)
.
toHaveLength
(
2
)
;
expect
(
[
0
]
.
)
.
toEqual
(
new
LineReplacement
(
new
LineRange
(
2
,
3
)
,
[
' let x = 1;'
]
)
)
;
expect
(
[
1
]
.
)
.
toEqual
(
new
LineReplacement
(
new
LineRange
(
3
,
3
)
,
[
' let y = 2;'
]
)
)
;
}
)
;
it
(
'progressive reveal with DropPatch drops continuation but keeps early edit'
,
async
(
)
=>
{
const
=
.
create
(
'file:///test.ts'
)
;
const
=
'function foo() {\n let x = 1;\n}\n'
;
const
=
new
CurrentDocument
(
new
StringText
(
)
,
new
Position
(
2
,
5
)
)
;
async
function
*
makeStream
(
)
:
<
string
>
{
yield
'/test.ts:1'
;
yield
'- let x = 1;'
;
yield
'+ let x = 1;'
;
yield
'+}'
;
// duplicates line 3 — triggers DropPatch on continuation
}
const
{
}
=
await
consumeHandleResponse
(
makeStream
(
)
,
,
,
undefined
,
undefined
,
new
TestLogService
(
)
,
.
,
true
,
)
;
// Early edit is yielded, continuation is dropped by dedup
expect
(
)
.
toHaveLength
(
1
)
;
expect
(
[
0
]
.
)
.
toEqual
(
new
LineReplacement
(
new
LineRange
(
2
,
3
)
,
[
' let x = 1;'
]
)
)
;
}
)
;
}
)
;
describe
(
'handleResponse with enableProgressiveGhostTextMultiLine'