fix(cli): isolate Desktop compatibility probes [skip ci]

An older terminal can ignore the protocol probe flag and consume inherited gateway credentials or terminal input. Scrub its TUI environment and close stdin before probing, then pass only the selected Desktop resolver metadata to the compatible client.

Validate the complete ready frame before opening mutation gates, not just its identity metadata. Add failing-before regressions and scope the existing recovery test process stub so real lease cleanup remains testable without changing runtime ownership.
This commit is contained in:
Xubin Ren committed 2026-09-08 10:04:15 +08:00
1 parent 48e7c4f697
commit 9a594f74ff
5 files changed
+61 -6

No files matched your search

+14
View File
@@ -107,6 +107,20 @@ describe("Desktop attach-only protocol", () => {
expect(statuses.at(-1)).toBe("error")
})
})
for (const fields of [{}, { chat_id: "" }, { chat_id: 1, client_id: "test" }, { chat_id: "", client_id: null }]) {
test(`rejects an incomplete ready frame even with matching identity: ${JSON.stringify(fields)}`, async () => {
await fixture(async (client, socket, statuses, attempts) => {
socket.emit("message", { data: JSON.stringify({ event: "ready", terminal: { protocolVersion: 1, gatewayId }, ...fields }) })
expect(() => client.newChat()).toThrow("Desktop identity not verified")
await expect(client.updateRecovery("continue", "chat", "recovery")).rejects.toThrow("Desktop identity not verified")
await Bun.sleep(20)
expect(socket.sent).toHaveLength(0)
expect(socket.readyState).toBe(3)
expect(statuses.at(-1)).toBe("error")
expect(attempts()).toBe(1)
})
})
}
test("explicit client exit sends no lifecycle command", async () => {
await fixture(async (client, socket) => {
client.close()
+3 -1
View File
@@ -1335,7 +1335,9 @@ export class NanobotClient {
}
const response = decodeWebUIResponse(value)
if (!this.identityVerified) {
if (!isRecord(value) || value.event !== "ready" || !isRecord(value.terminal)
// Matching metadata is insufficient: an invalid ready frame must not
// unlock mutations or cancel the bounded compatibility handshake.
if (!isRecord(value) || decodeInboundEvent(value)?.event !== "ready" || !isRecord(value.terminal)
|| value.terminal.protocolVersion !== 1 || value.terminal.gatewayId !== this.options.expectedGatewayId) {
this.desktopFailure()
return