Skip to content

Commit 5902152

Browse files
authored
Merge branch 'v2/main' into v2/fix/5107-git-show-diff-prefixes
2 parents c2df185 + 7f4922b commit 5902152

5 files changed

Lines changed: 98 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-sequential-thinking": patch
3+
---
4+
5+
Pad the header row of the thought box logged to stderr to the border's width, so its right edge lines up with the rest of the box (#5108).

‎scripts/lib/win-shell-args.mjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ export function quoteWinArg(arg) {
4343
);
4444
}
4545
if (!NEEDS_QUOTING.test(arg)) return arg;
46-
// A literal `"` inside a quoted string is doubled, per the CRT parser.
47-
return `"${arg.replace(/"/g, '""')}"`;
46+
// The CRT parser reads backslashes literally except before a `"`, where 2n
47+
// backslashes yield n and leave the quote special. So any run of backslashes
48+
// that ends up in front of a quote is doubled: one before an embedded `"`,
49+
// and a trailing run, which the closing quote follows (#5111). Undoubled,
50+
// `C:\dir\` becomes `"C:\dir\"`, whose `\"` is an escaped quote and swallows
51+
// the rest of the command line. The embedded `"` itself is then doubled
52+
// rather than written `\"`: `""` keeps `cmd.exe`'s own quote tracking in
53+
// step, while `\"` would flip it and expose any metacharacter after it.
54+
const escaped = arg.replace(/(\\*)"/g, '$1$1""').replace(/(\\+)$/, "$1$1");
55+
return `"${escaped}"`;
4856
}
4957

5058
/**

‎scripts/lib/win-shell-args.test.mjs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,68 @@ test("doubles an embedded quote", () => {
3939
assert.equal(quoteWinArg('a"b c'), '"a""b c"');
4040
});
4141

42+
test("doubles a trailing backslash run, which the closing quote follows (#5111)", () => {
43+
// Undoubled, `"C:\dir with space\"` ends in `\"`, an escaped quote, and the
44+
// CRT parser runs the argument on into the next one.
45+
assert.equal(quoteWinArg("C:\\a b\\"), '"C:\\a b\\\\"');
46+
assert.equal(quoteWinArg("C:\\a b\\\\"), '"C:\\a b\\\\\\\\"');
47+
});
48+
49+
test("doubles backslashes before an embedded quote, then doubles the quote", () => {
50+
assert.equal(quoteWinArg('a\\"b c'), '"a\\\\""b c"');
51+
assert.equal(quoteWinArg('a\\\\"b'), '"a\\\\\\\\""b"');
52+
});
53+
54+
test("leaves backslashes not followed by a quote alone", () => {
55+
assert.equal(quoteWinArg("C:\\a b\\c"), '"C:\\a b\\c"');
56+
// An inert trailing-backslash path is not quoted, so nothing follows it.
57+
assert.equal(quoteWinArg("C:\\dir\\"), "C:\\dir\\");
58+
});
59+
60+
test("quoted output parses back to the original under the CRT rules", () => {
61+
// A reference parser for one quoted argument (the MSVCRT 2008+ rules), so
62+
// the expectations above are checked against what the child actually sees.
63+
const parse = (s) => {
64+
let out = "";
65+
let inQuotes = false;
66+
for (let i = 0; i < s.length; ) {
67+
let n = 0;
68+
while (s[i] === "\\") {
69+
n++;
70+
i++;
71+
}
72+
if (s[i] === '"') {
73+
out += "\\".repeat(n >> 1);
74+
if (n & 1) {
75+
out += '"';
76+
} else if (inQuotes && s[i + 1] === '"') {
77+
out += '"';
78+
i++;
79+
} else {
80+
inQuotes = !inQuotes;
81+
}
82+
i++;
83+
} else {
84+
out += "\\".repeat(n);
85+
if (i < s.length) out += s[i++];
86+
}
87+
}
88+
return out;
89+
};
90+
for (const arg of [
91+
"C:\\a b\\",
92+
"C:\\a b\\\\",
93+
'a\\"b c',
94+
'a\\\\"b',
95+
'a"b c',
96+
'"',
97+
"\\ \\",
98+
"C:\\Temp(1)\\",
99+
]) {
100+
assert.equal(parse(quoteWinArg(arg)), arg, `round trip: ${arg}`);
101+
}
102+
});
103+
42104
test("throws on `%` rather than silently mis-expanding it", () => {
43105
// cmd.exe expands %NAME% before it processes quotes, so quoting cannot save
44106
// this one — the child would receive a different path than we passed.

‎src/sequentialthinking/__tests__/thought-logging.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("sequentialthinking thought logging", () => {
4646
const border = "─".repeat(Math.max(header.length, text.length) + 4);
4747
return `
4848
┌${border}┐
49-
│ ${header} │
49+
│ ${header.padEnd(border.length - 2)} │
5050
├${border}┤
5151
│ ${text.padEnd(border.length - 2)} │
5252
└${border}┘`;
@@ -165,6 +165,19 @@ describe("sequentialthinking thought logging", () => {
165165
});
166166
});
167167

168+
// #5108: the header row is padded to the border's width, so every row of
169+
// the box is the same width, whether the header or the thought is longer.
170+
it.each([
171+
["the header", "t"],
172+
["the thought", "a much longer thought than the header line is"],
173+
])("draws every row the same width when %s is longer", async (_l, text) => {
174+
conn = await connect({ disableThoughtLogging: undefined });
175+
await conn.think(thought({ thought: text }));
176+
const rows = logged()[0].split("\n").slice(1);
177+
expect(rows).toHaveLength(5);
178+
expect(new Set(rows.map((row) => row.length)).size).toBe(1);
179+
});
180+
168181
// #5000: when colour is on, the header carries chalk's escape codes, but the
169182
// border is sized from the visible text, so the box matches the uncoloured one.
170183
it("sizes the border from the visible header, not its escape codes", async () => {

‎src/sequentialthinking/lib.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class SequentialThinkingServer {
3636
} = thoughtData;
3737

3838
// The label is coloured only after the header is measured, so the border
39-
// is sized from the visible text, not from chalk's escape codes. A missing
39+
// and the header's padding are sized from the visible text, not from
40+
// chalk's escape codes. A missing
4041
// revisesThought or branchId is left out of the header rather than printed
4142
// as "undefined".
4243
let label: string;
@@ -63,10 +64,14 @@ export class SequentialThinkingServer {
6364

6465
const counts = ` ${thoughtNumber}/${totalThoughts}${context}`;
6566
const visibleHeader = `${label}${counts}`;
66-
const header = `${colour(label)}${counts}`;
6767
const border = "─".repeat(
6868
Math.max(visibleHeader.length, thought.length) + 4,
6969
);
70+
// The header row is padded to the border's width like the thought row, by
71+
// the visible text's length. Each label's emoji is one surrogate pair, so
72+
// its length of 2 is also its display width in a terminal.
73+
const padding = " ".repeat(border.length - 2 - visibleHeader.length);
74+
const header = `${colour(label)}${counts}${padding}`;
7075

7176
return `
7277
┌${border}┐

0 commit comments

Comments
 (0)