Skip to content

Commit 4a2b895

Browse files
committed
fix(shared): inverted terminal ranges emit no fake lines
lineEnd < lineStart made slice(0, negative) take from the end of the buffer and invent line numbers. Clamp the count to zero instead.
1 parent c6ef3b4 commit 4a2b895

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎packages/shared/src/composerContextReferences.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ describe("provider projection", () => {
221221
expect(project("a\n")).toContain("3 | a\n4 | \n</context>");
222222
});
223223

224+
it("emits no terminal lines when the captured range is inverted", () => {
225+
const projected = projectComposerContextForProvider({
226+
text: "[log](t3-context://v1/terminal/ctx_t)",
227+
records: [{ ...terminal, lineStart: 10, lineEnd: 5, text: "a\nb\nc\nd\ne\nf" }],
228+
});
229+
expect(projected).toContain('<context kind="terminal" id="ctx_t">');
230+
expect(projected).toContain("terminal: Terminal 1\n</context>");
231+
expect(projected).not.toContain(" | ");
232+
});
233+
224234
it("emits every marker in place and each payload once, escaped, in first-reference order", () => {
225235
const text = [
226236
"Look at ![shot.png](t3-context://v1/image/ctx_i) and [T1](t3-context://v1/terminal/ctx_t).",

‎packages/shared/src/composerContextReferences.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,10 @@ function formatComposerContextProviderPayload(record: KnownComposerContextRecord
253253
`attachmentId: ${record.attachmentId}`,
254254
].join("\n");
255255
case "terminal": {
256+
const lineCount = Math.max(0, record.lineEnd - record.lineStart + 1);
256257
const lines = record.text
257258
.split("\n")
258-
.slice(0, record.lineEnd - record.lineStart + 1)
259+
.slice(0, lineCount)
259260
.map((line, index) => `${record.lineStart + index} | ${line}`);
260261
return [`terminal: ${record.terminalLabel}`, ...lines].join("\n");
261262
}

0 commit comments

Comments
 (0)