|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import http2 from 'http2'; |
| 4 | +import { WindsurfClient } from '../src/client.js'; |
| 5 | +import { parseTrajectorySteps } from '../src/windsurf.js'; |
| 6 | +import { repairToolCallArguments } from '../src/handlers/chat.js'; |
| 7 | +import { parseFields, getField, writeMessageField, writeStringField, writeVarintField } from '../src/proto.js'; |
| 8 | +import { endOfStreamEnvelope, unwrapRequest, wrapEnvelope } from '../src/connect.js'; |
| 9 | + |
| 10 | +function grpcFrame(payload) { |
| 11 | + const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload); |
| 12 | + const frame = Buffer.alloc(5 + buf.length); |
| 13 | + frame[0] = 0; |
| 14 | + frame.writeUInt32BE(buf.length, 1); |
| 15 | + buf.copy(frame, 5); |
| 16 | + return frame; |
| 17 | +} |
| 18 | + |
| 19 | +function extractGrpcFrames(buf) { |
| 20 | + const frames = []; |
| 21 | + let offset = 0; |
| 22 | + while (offset + 5 <= buf.length) { |
| 23 | + const compressed = buf[offset]; |
| 24 | + const msgLen = buf.readUInt32BE(offset + 1); |
| 25 | + if (compressed !== 0 || offset + 5 + msgLen > buf.length) break; |
| 26 | + frames.push(buf.subarray(offset + 5, offset + 5 + msgLen)); |
| 27 | + offset += 5 + msgLen; |
| 28 | + } |
| 29 | + return frames; |
| 30 | +} |
| 31 | + |
| 32 | +function requestPayload(body, headers) { |
| 33 | + const contentType = String(headers['content-type'] || ''); |
| 34 | + if (contentType.includes('application/connect+proto')) return unwrapRequest(body, headers); |
| 35 | + const frames = extractGrpcFrames(body); |
| 36 | + return frames.length ? Buffer.concat(frames) : body.subarray(5); |
| 37 | +} |
| 38 | + |
| 39 | +function responseBody(payload, headers) { |
| 40 | + const contentType = String(headers['content-type'] || ''); |
| 41 | + if (contentType.includes('application/connect+proto')) { |
| 42 | + return Buffer.concat([wrapEnvelope(payload, { compress: false }), endOfStreamEnvelope()]); |
| 43 | + } |
| 44 | + return grpcFrame(payload); |
| 45 | +} |
| 46 | + |
| 47 | +function startCascadeResponse(cascadeId) { |
| 48 | + return writeStringField(1, cascadeId); |
| 49 | +} |
| 50 | + |
| 51 | +function trajectoryStatusResponse(status) { |
| 52 | + return writeVarintField(2, status); |
| 53 | +} |
| 54 | + |
| 55 | +function viewFileWrapperStep() { |
| 56 | + const wrapper = Buffer.concat([ |
| 57 | + writeStringField(2, 'file:///home/user/projects/workspace-abc123/README.md'), |
| 58 | + writeMessageField(3, writeStringField(1, 'nested request')), |
| 59 | + writeStringField(4, 'observed content'), |
| 60 | + ]); |
| 61 | + return Buffer.concat([ |
| 62 | + writeVarintField(1, 14), |
| 63 | + writeVarintField(4, 3), |
| 64 | + writeMessageField(19, wrapper), |
| 65 | + ]); |
| 66 | +} |
| 67 | + |
| 68 | +function errorStep(message) { |
| 69 | + const details = writeStringField(1, message); |
| 70 | + const errorMessage = writeMessageField(3, details); |
| 71 | + return Buffer.concat([ |
| 72 | + writeVarintField(1, 17), |
| 73 | + writeVarintField(4, 3), |
| 74 | + writeMessageField(24, errorMessage), |
| 75 | + ]); |
| 76 | +} |
| 77 | + |
| 78 | +function trajectoryStepsResponse(...steps) { |
| 79 | + return Buffer.concat(steps.map(step => writeMessageField(1, step))); |
| 80 | +} |
| 81 | + |
| 82 | +async function withFakeLanguageServer(handler, fn) { |
| 83 | + const server = http2.createServer(); |
| 84 | + server.on('stream', handler); |
| 85 | + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); |
| 86 | + const port = server.address().port; |
| 87 | + try { |
| 88 | + return await fn(port); |
| 89 | + } finally { |
| 90 | + await new Promise(resolve => server.close(resolve)); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +describe('native Read wrapper trajectory parsing', () => { |
| 95 | + it('promotes type 14 field 19 wrapper into a view_file native tool call', () => { |
| 96 | + const steps = parseTrajectorySteps(trajectoryStepsResponse(viewFileWrapperStep())); |
| 97 | + const calls = steps[0].toolCalls.filter(tc => tc.cascade_native); |
| 98 | + assert.equal(calls.length, 1); |
| 99 | + assert.equal(calls[0].name, 'view_file'); |
| 100 | + const args = JSON.parse(calls[0].argumentsJson); |
| 101 | + assert.equal(args.absolute_path_uri, 'file:///home/user/projects/workspace-abc123/README.md'); |
| 102 | + assert.equal(calls[0].result, 'observed content'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('repairs native Read workspace paths to caller cwd-relative paths before sanitizing', () => { |
| 106 | + const tc = { |
| 107 | + name: 'Read', |
| 108 | + argumentsJson: JSON.stringify({ file_path: '/home/user/projects/workspace-abc123/README.md', limit: 20 }), |
| 109 | + }; |
| 110 | + const repaired = repairToolCallArguments(tc, [ |
| 111 | + { role: 'system', content: '- Working directory: D:\\Project\\WindsurfAPI\n- Platform: windows' }, |
| 112 | + { role: 'user', content: 'Use Read for README.md.' }, |
| 113 | + ]); |
| 114 | + assert.equal(JSON.parse(repaired.argumentsJson).file_path, 'D:\\Project\\WindsurfAPI\\README.md'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not rename view_file absolute_path_uri for cascade-style callers', () => { |
| 118 | + const tc = { |
| 119 | + name: 'view_file', |
| 120 | + argumentsJson: JSON.stringify({ absolute_path_uri: 'file:///home/user/projects/workspace-abc123/README.md' }), |
| 121 | + }; |
| 122 | + const repaired = repairToolCallArguments(tc, [ |
| 123 | + { role: 'system', content: '- Working directory: /repo' }, |
| 124 | + { role: 'user', content: 'view README.md' }, |
| 125 | + ]); |
| 126 | + const args = JSON.parse(repaired.argumentsJson); |
| 127 | + assert.equal(args.absolute_path_uri, 'file:///repo/README.md'); |
| 128 | + assert.equal(Object.prototype.hasOwnProperty.call(args, 'file_path'), false); |
| 129 | + }); |
| 130 | + |
| 131 | + it('keeps native proposal when the same trajectory batch also contains a remote execution error', async () => { |
| 132 | + process.env.CASCADE_POLL_INTERVAL_MS = '10'; |
| 133 | + process.env.CASCADE_IDLE_GRACE_MS = '1'; |
| 134 | + process.env.CASCADE_MAX_WAIT_MS = '500'; |
| 135 | + process.env.CASCADE_COLD_STALL_BASE_MS = '500'; |
| 136 | + process.env.CASCADE_WARM_STALL_MS = '500'; |
| 137 | + process.env.GRPC_PROTOCOL = 'connect'; |
| 138 | + |
| 139 | + let statusPolls = 0; |
| 140 | + let stepPolls = 0; |
| 141 | + const streamed = []; |
| 142 | + |
| 143 | + await withFakeLanguageServer((stream, headers) => { |
| 144 | + const chunks = []; |
| 145 | + stream.on('data', chunk => chunks.push(chunk)); |
| 146 | + stream.on('end', () => { |
| 147 | + const method = String(headers[':path'] || '').split('/').pop(); |
| 148 | + const payload = requestPayload(Buffer.concat(chunks), headers); |
| 149 | + if (payload.length) { |
| 150 | + try { parseFields(payload); } catch {} |
| 151 | + } |
| 152 | + |
| 153 | + if (method === 'StartCascade') { |
| 154 | + stream.respond({ ':status': 200, 'content-type': headers['content-type'] || 'application/grpc' }); |
| 155 | + stream.end(responseBody(startCascadeResponse('native-read-cascade'), headers)); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + if (method === 'SendUserCascadeMessage') { |
| 160 | + stream.respond({ ':status': 200, 'content-type': headers['content-type'] || 'application/grpc' }); |
| 161 | + stream.end(responseBody(Buffer.alloc(0), headers)); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + if (method === 'GetCascadeTrajectorySteps') { |
| 166 | + stepPolls++; |
| 167 | + stream.respond({ ':status': 200, 'content-type': headers['content-type'] || 'application/grpc' }); |
| 168 | + stream.end(responseBody(trajectoryStepsResponse( |
| 169 | + viewFileWrapperStep(), |
| 170 | + errorStep('invalid tool call: model_not_available'), |
| 171 | + ), headers)); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + if (method === 'GetCascadeTrajectory') { |
| 176 | + statusPolls++; |
| 177 | + stream.respond({ ':status': 200, 'content-type': headers['content-type'] || 'application/grpc' }); |
| 178 | + stream.end(responseBody(trajectoryStatusResponse(2), headers)); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + if (method === 'GetCascadeTrajectoryGeneratorMetadata') { |
| 183 | + stream.respond({ ':status': 200, 'content-type': headers['content-type'] || 'application/grpc' }); |
| 184 | + stream.end(responseBody(Buffer.alloc(0), headers)); |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + stream.respond({ ':status': 404 }); |
| 189 | + stream.end(); |
| 190 | + }); |
| 191 | + }, async (port) => { |
| 192 | + const client = new WindsurfClient('test-api-key', port, 'csrf-token'); |
| 193 | + const chunks = await client.cascadeChat([{ role: 'user', content: 'read README.md' }], 0, 'claude-4.5-haiku', { |
| 194 | + nativeMode: true, |
| 195 | + nativeAllowlist: ['read_file'], |
| 196 | + onChunk: c => streamed.push(c), |
| 197 | + }); |
| 198 | + |
| 199 | + assert.equal(stepPolls, 1); |
| 200 | + assert.equal(statusPolls, 0); |
| 201 | + assert.equal(chunks.toolCalls.length, 1); |
| 202 | + assert.equal(chunks.toolCalls[0].name, 'view_file'); |
| 203 | + const args = JSON.parse(chunks.toolCalls[0].argumentsJson); |
| 204 | + assert.equal(args.absolute_path_uri, 'file:///home/user/projects/workspace-abc123/README.md'); |
| 205 | + assert.equal(streamed.filter(c => c.nativeToolCall).length, 1); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments