|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import { createServer } from "node:http"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { clearConfigCache, clearRuntimeConfigSnapshot } from "../src/config/config.js"; |
| 7 | +import { clearSessionStoreCacheForTest } from "../src/config/sessions/store-writer-state.js"; |
| 8 | +import { |
| 9 | + disconnectGatewayClient, |
| 10 | + startGatewayWithClient, |
| 11 | +} from "../src/gateway/test-helpers.e2e.js"; |
| 12 | +import { buildMockOpenAiResponsesProvider } from "../src/gateway/test-openai-responses-model.js"; |
| 13 | +import { captureEnv, setTestEnvValue } from "../src/test-utils/env.js"; |
| 14 | + |
| 15 | +const envKeys = [ |
| 16 | + "HOME", |
| 17 | + "OPENCLAW_STATE_DIR", |
| 18 | + "OPENCLAW_CONFIG_PATH", |
| 19 | + "OPENCLAW_GATEWAY_TOKEN", |
| 20 | + "OPENCLAW_SKIP_CHANNELS", |
| 21 | + "OPENCLAW_SKIP_GMAIL_WATCHER", |
| 22 | + "OPENCLAW_SKIP_CRON", |
| 23 | + "OPENCLAW_SKIP_CANVAS_HOST", |
| 24 | + "OPENCLAW_SKIP_BROWSER_CONTROL_SERVER", |
| 25 | + "OPENCLAW_SKIP_PROVIDERS", |
| 26 | + "OPENCLAW_BUNDLED_PLUGINS_DIR", |
| 27 | + "OPENCLAW_DISABLE_BUNDLED_PLUGINS", |
| 28 | +] as const; |
| 29 | + |
| 30 | +describe("PR #119473 real gateway proof", () => { |
| 31 | + let tempHome: string | undefined; |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + if (tempHome) { |
| 35 | + await fs.rm(tempHome, { recursive: true, force: true }); |
| 36 | + tempHome = undefined; |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it( |
| 41 | + "persists the first turn of a fresh SQLite-backed session across gateway restart", |
| 42 | + { timeout: 90_000 }, |
| 43 | + async () => { |
| 44 | + const envSnapshot = captureEnv([...envKeys]); |
| 45 | + let providerServer: ReturnType<typeof createServer> | undefined; |
| 46 | + let first: Awaited<ReturnType<typeof startGatewayWithClient>> | undefined; |
| 47 | + let second: Awaited<ReturnType<typeof startGatewayWithClient>> | undefined; |
| 48 | + |
| 49 | + try { |
| 50 | + tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-pr119473-proof-")); |
| 51 | + const stateDir = path.join(tempHome, ".openclaw"); |
| 52 | + const workspaceDir = path.join(tempHome, "workspace"); |
| 53 | + const configPath = path.join(stateDir, "openclaw.json"); |
| 54 | + const bundledPluginsDir = path.join(tempHome, "bundled-plugins"); |
| 55 | + await Promise.all([ |
| 56 | + fs.mkdir(workspaceDir, { recursive: true }), |
| 57 | + fs.mkdir(bundledPluginsDir, { recursive: true }), |
| 58 | + fs.mkdir(path.dirname(configPath), { recursive: true }), |
| 59 | + ]); |
| 60 | + for (const [key, value] of Object.entries({ |
| 61 | + HOME: tempHome, |
| 62 | + OPENCLAW_STATE_DIR: stateDir, |
| 63 | + OPENCLAW_CONFIG_PATH: configPath, |
| 64 | + OPENCLAW_GATEWAY_TOKEN: "pr119473-proof-token", |
| 65 | + OPENCLAW_SKIP_CHANNELS: "1", |
| 66 | + OPENCLAW_SKIP_GMAIL_WATCHER: "1", |
| 67 | + OPENCLAW_SKIP_CRON: "1", |
| 68 | + OPENCLAW_SKIP_CANVAS_HOST: "1", |
| 69 | + OPENCLAW_SKIP_BROWSER_CONTROL_SERVER: "1", |
| 70 | + OPENCLAW_SKIP_PROVIDERS: "1", |
| 71 | + OPENCLAW_BUNDLED_PLUGINS_DIR: bundledPluginsDir, |
| 72 | + OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1", |
| 73 | + })) { |
| 74 | + setTestEnvValue(key, value); |
| 75 | + } |
| 76 | + |
| 77 | + providerServer = createServer((_request, response) => { |
| 78 | + response.writeHead(200, { "content-type": "text/event-stream" }); |
| 79 | + const message = { |
| 80 | + type: "message", |
| 81 | + id: "pr119473-proof-message", |
| 82 | + role: "assistant", |
| 83 | + status: "completed", |
| 84 | + content: [{ type: "output_text", text: "PR119473_RUNTIME_OK", annotations: [] }], |
| 85 | + }; |
| 86 | + response.end( |
| 87 | + [ |
| 88 | + { |
| 89 | + type: "response.output_item.added", |
| 90 | + output_index: 0, |
| 91 | + item: { ...message, status: "in_progress", content: [] }, |
| 92 | + }, |
| 93 | + { type: "response.output_item.done", output_index: 0, item: message }, |
| 94 | + { |
| 95 | + type: "response.completed", |
| 96 | + response: { |
| 97 | + status: "completed", |
| 98 | + usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + ] |
| 102 | + .map((event) => `data: ${JSON.stringify(event)}\n\n`) |
| 103 | + .concat("data: [DONE]\n\n") |
| 104 | + .join(""), |
| 105 | + ); |
| 106 | + }); |
| 107 | + await new Promise<void>((resolve, reject) => { |
| 108 | + providerServer?.once("error", reject); |
| 109 | + providerServer?.listen(0, "127.0.0.1", resolve); |
| 110 | + }); |
| 111 | + const providerAddress = providerServer.address(); |
| 112 | + if (!providerAddress || typeof providerAddress === "string") { |
| 113 | + throw new Error("proof provider did not bind a loopback port"); |
| 114 | + } |
| 115 | + const provider = buildMockOpenAiResponsesProvider( |
| 116 | + `http://127.0.0.1:${providerAddress.port}/v1`, |
| 117 | + ); |
| 118 | + const cfg = { |
| 119 | + agents: { |
| 120 | + defaults: { |
| 121 | + workspace: workspaceDir, |
| 122 | + skipBootstrap: true, |
| 123 | + model: { primary: provider.modelRef }, |
| 124 | + models: { |
| 125 | + [provider.modelRef]: { params: { transport: "sse", openaiWsWarmup: false } }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + entries: { main: { default: true } }, |
| 129 | + }, |
| 130 | + models: { mode: "replace", providers: { [provider.providerId]: provider.config } }, |
| 131 | + gateway: { auth: { mode: "token", token: "pr119473-proof-token" } }, |
| 132 | + }; |
| 133 | + const sessionKey = "agent:main:pr119473-fresh-runtime"; |
| 134 | + const marker = "PR119473_FIRST_USER_MESSAGE"; |
| 135 | + first = await startGatewayWithClient({ |
| 136 | + cfg, |
| 137 | + configPath, |
| 138 | + token: "pr119473-proof-token", |
| 139 | + clientDisplayName: "pr119473-proof-first-start", |
| 140 | + }); |
| 141 | + const started = await first.client.request<{ runId?: string; status?: string }>( |
| 142 | + "chat.send", |
| 143 | + { |
| 144 | + sessionKey, |
| 145 | + message: marker, |
| 146 | + deliver: false, |
| 147 | + idempotencyKey: "pr119473-proof-first-turn", |
| 148 | + }, |
| 149 | + ); |
| 150 | + expect(started.status).toBe("started"); |
| 151 | + expect(started.runId).toEqual(expect.any(String)); |
| 152 | + await expect( |
| 153 | + first.client.request( |
| 154 | + "agent.wait", |
| 155 | + { runId: started.runId, timeoutMs: 30_000 }, |
| 156 | + { timeoutMs: 35_000 }, |
| 157 | + ), |
| 158 | + ).resolves.toMatchObject({ status: "ok" }); |
| 159 | + await disconnectGatewayClient(first.client); |
| 160 | + await first.server.close({ reason: "PR #119473 proof restart" }); |
| 161 | + first = undefined; |
| 162 | + |
| 163 | + clearRuntimeConfigSnapshot(); |
| 164 | + clearConfigCache(); |
| 165 | + second = await startGatewayWithClient({ |
| 166 | + cfg, |
| 167 | + configPath, |
| 168 | + token: "pr119473-proof-token", |
| 169 | + clientDisplayName: "pr119473-proof-after-restart", |
| 170 | + }); |
| 171 | + const history = await second.client.request<{ messages?: unknown[] }>("chat.history", { |
| 172 | + sessionKey, |
| 173 | + limit: 20, |
| 174 | + }); |
| 175 | + expect(JSON.stringify(history.messages ?? [])).toContain(marker); |
| 176 | + } finally { |
| 177 | + if (first) { |
| 178 | + await disconnectGatewayClient(first.client).catch(() => undefined); |
| 179 | + await first.server.close().catch(() => undefined); |
| 180 | + } |
| 181 | + if (second) { |
| 182 | + await disconnectGatewayClient(second.client).catch(() => undefined); |
| 183 | + await second.server.close().catch(() => undefined); |
| 184 | + } |
| 185 | + if (providerServer?.listening) { |
| 186 | + await new Promise<void>((resolve) => { |
| 187 | + providerServer?.close(() => resolve()); |
| 188 | + }); |
| 189 | + } |
| 190 | + envSnapshot.restore(); |
| 191 | + clearRuntimeConfigSnapshot(); |
| 192 | + clearConfigCache(); |
| 193 | + clearSessionStoreCacheForTest(); |
| 194 | + } |
| 195 | + }, |
| 196 | + ); |
| 197 | +}); |
0 commit comments