forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-packaged-agent-smoke.mjs
More file actions
217 lines (195 loc) · 8.01 KB
/
Copy pathrun-packaged-agent-smoke.mjs
File metadata and controls
217 lines (195 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Packaged-build agent smoke — the test that would have caught the
// "-32603 Internal error on every agent spawn" packaging bug.
//
// WHY THIS EXISTS (and why the existing vitest agent E2E did not catch it):
// src/main/codingAgent/acp.e2e.test.ts constructs AcpRuntimeAdapter DIRECTLY and
// points `acpEntry` at the on-disk source claude-acp-entry.mjs — so it exercises
// neither the `?asset` runtime path resolution nor the packaged app.asar layout.
// The packaging bug lived precisely there: the `?asset` entry resolved INSIDE
// app.asar, and the SDK could not exec claude.exe from the archive. Only booting
// the REAL packaged binary and spawning through the REAL ClaudeCodeRuntimeAdapter
// (which uses `?asset`) reproduces it. This script does exactly that.
//
// WHAT IT DOES
// 1. Takes an already-built dist/win-unpacked (or an explicit --app-dir).
// 2. Creates a THROWAWAY userData profile and seeds the signed-in Claude agent
// creds into it (never touches the real profile — copies out, read-only).
// 3. Boots the real exe with that isolated profile + a CDP port.
// 4. Drives window.omi.codingAgentRun('acp', ...) — the exact path a user's
// agent spawn takes — and asserts the run reaches ok:true with text output.
// 5. Fails loudly (non-zero exit) if the run returns "Internal error" or any
// failure, printing the app's ACP spawn log for diagnosis.
// 6. Kills the isolated app and removes the throwaway profile.
//
// LIVE + NOT HERMETIC: needs a machine already signed in to Claude in the Omi app
// (real OAuth creds) and costs a few cents per run. It is therefore NOT wired into
// `pnpm test` / CI. Run it locally after a build:
//
// pnpm build:win # or: pnpm exec electron-vite build && pnpm exec electron-builder --dir --config electron-builder.config.mjs
// pnpm smoke:packaged-agent # this script
//
// Env overrides:
// OMI_SMOKE_APP_DIR — path to the win-unpacked dir (default: ./dist/win-unpacked)
// OMI_SMOKE_CREDS_DIR — path to a claude-agent creds dir to seed
// (default: %APPDATA%/omi-windows/claude-agent)
// OMI_SMOKE_CDP_PORT — CDP port (default: 9522)
import { spawn } from 'node:child_process'
import { execFileSync } from 'node:child_process'
import { createRequire } from 'node:module'
import { cpSync, existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const require = createRequire(import.meta.url)
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
function arg(name) {
const i = process.argv.indexOf(name)
return i >= 0 ? process.argv[i + 1] : undefined
}
const APP_DIR = arg('--app-dir') || process.env.OMI_SMOKE_APP_DIR || path.join(ROOT, 'dist', 'win-unpacked')
const EXE = path.join(APP_DIR, 'omi-windows.exe')
const CREDS_DIR =
process.env.OMI_SMOKE_CREDS_DIR ||
path.join(process.env.APPDATA || '', 'omi-windows', 'claude-agent')
const CDP_PORT = Number(process.env.OMI_SMOKE_CDP_PORT || 9522)
const PROMPT =
'Reply with exactly the single word READY and nothing else. Do not use any tools.'
function fail(msg) {
console.error(`\n[packaged-agent-smoke] FAIL: ${msg}\n`)
process.exitCode = 1
}
if (!existsSync(EXE)) {
fail(`packaged exe not found at ${EXE}\n Build first: pnpm build:win (or electron-builder --dir)`)
process.exit(1)
}
if (!existsSync(path.join(CREDS_DIR, '.credentials.json'))) {
fail(
`no Claude creds at ${CREDS_DIR}/.credentials.json\n Sign in to Claude in the Omi app first, or set OMI_SMOKE_CREDS_DIR.`
)
process.exit(1)
}
const chromium = require('playwright').chromium
const profileDir = mkdtempSync(path.join(tmpdir(), 'omi-pkg-agent-smoke-'))
const logFile = path.join(profileDir, 'app.log')
let child = null
function cleanup() {
try {
if (child && child.pid) {
execFileSync('taskkill', ['/pid', String(child.pid), '/t', '/f'], { windowsHide: true, stdio: 'ignore' })
}
} catch {
/* already gone */
}
try {
rmSync(profileDir, { recursive: true, force: true })
} catch {
/* best effort */
}
}
async function main() {
// Seed the isolated profile: Claude agent creds go where the app reads them —
// <userData>/claude-agent (app pins CLAUDE_CONFIG_DIR there at startup).
mkdirSync(profileDir, { recursive: true })
cpSync(CREDS_DIR, path.join(profileDir, 'claude-agent'), { recursive: true })
const logStream = require('node:fs').createWriteStream(logFile)
console.log(`[packaged-agent-smoke] booting ${EXE}`)
console.log(`[packaged-agent-smoke] isolated profile: ${profileDir}`)
child = spawn(
EXE,
[`--user-data-dir=${profileDir}`, `--remote-debugging-port=${CDP_PORT}`],
{ env: { ...process.env, OMI_AUTOMATION: '0' }, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true }
)
child.stdout.pipe(logStream)
child.stderr.pipe(logStream)
// Wait for the CDP endpoint to come up.
const browser = await connectWithRetry(`http://127.0.0.1:${CDP_PORT}`, 30_000)
// Find the main app window (index.html), retrying while it loads.
const page = await findMainPage(browser, 20_000)
if (!page) throw new Error('main app window (index.html) never appeared')
// Confirm the Claude adapter reports connected (creds seeded correctly).
const auth = await page.evaluate(async () => {
try {
return await window.omi.codingAgentAuthStatus()
} catch (e) {
return { error: String(e) }
}
})
console.log(`[packaged-agent-smoke] claude auth:`, JSON.stringify(auth))
if (!auth || !auth.connected) {
throw new Error(`Claude adapter not connected in packaged app: ${JSON.stringify(auth)}`)
}
// Drive the real spawn path.
console.log(`[packaged-agent-smoke] spawning agent…`)
const result = await page.evaluate(async (prompt) => {
const taskId = 'pkg-smoke-' + Date.now()
try {
const r = await window.omi.codingAgentRun({ taskId, prompt, agentId: 'acp' })
return { ok: r.ok, error: r.error, adapterId: r.adapterId, text: (r.text || '').slice(0, 200) }
} catch (e) {
return { threw: String(e) }
}
}, PROMPT)
console.log(`[packaged-agent-smoke] run result:`, JSON.stringify(result))
await browser.close().catch(() => {})
if (!result || !result.ok) {
// Surface the app's ACP spawn log — the real cause lives there.
let log = ''
try {
log = readFileSync(logFile, 'utf8')
} catch {
/* ignore */
}
const acpLines = log
.split('\n')
.filter((l) => /acp|claude|subprocess|Internal|failed to launch/i.test(l))
.slice(-15)
.join('\n')
throw new Error(
`agent run did not succeed: ${JSON.stringify(result)}\n--- app ACP log tail ---\n${acpLines}`
)
}
if (!result.text || !result.text.trim()) {
throw new Error(`agent run reported ok but produced no text output: ${JSON.stringify(result)}`)
}
console.log(`\n[packaged-agent-smoke] PASS — agent replied: ${JSON.stringify(result.text)}\n`)
}
async function connectWithRetry(url, timeoutMs) {
const deadline = Date.now() + timeoutMs
let lastErr
while (Date.now() < deadline) {
try {
return await chromium.connectOverCDP(url)
} catch (e) {
lastErr = e
await sleep(500)
}
}
throw new Error(`could not connect to CDP at ${url}: ${lastErr}`)
}
async function findMainPage(browser, timeoutMs) {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
for (const ctx of browser.contexts()) {
for (const p of ctx.pages()) {
if (p.url().includes('index.html')) return p
}
}
await sleep(500)
}
return null
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
// Ctrl-C / kill must still tear down: the throwaway profile holds a COPY of
// real Claude credentials, and an orphaned app process would keep running.
for (const sig of ['SIGINT', 'SIGTERM']) {
process.on(sig, () => {
try {
cleanup()
} finally {
process.exit(130)
}
})
}
main()
.catch((e) => fail(e && e.stack ? e.stack : String(e)))
.finally(cleanup)