@namzu/computer-use 4.4.0 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/SubprocessComputerUseHost.d.ts +8 -1
  3. package/dist/SubprocessComputerUseHost.d.ts.map +1 -1
  4. package/dist/SubprocessComputerUseHost.js +15 -4
  5. package/dist/SubprocessComputerUseHost.js.map +1 -1
  6. package/dist/a11y/a11y.d.ts +15 -7
  7. package/dist/a11y/a11y.d.ts.map +1 -1
  8. package/dist/a11y/a11y.js +12 -5
  9. package/dist/a11y/a11y.js.map +1 -1
  10. package/dist/a11y/index.d.ts +7 -1
  11. package/dist/a11y/index.d.ts.map +1 -1
  12. package/dist/a11y/index.js +4 -1
  13. package/dist/a11y/index.js.map +1 -1
  14. package/dist/a11y/macos-helper.d.ts +80 -0
  15. package/dist/a11y/macos-helper.d.ts.map +1 -0
  16. package/dist/a11y/macos-helper.js +436 -0
  17. package/dist/a11y/macos-helper.js.map +1 -0
  18. package/dist/a11y/macos-signing.d.ts +56 -0
  19. package/dist/a11y/macos-signing.d.ts.map +1 -0
  20. package/dist/a11y/macos-signing.js +228 -0
  21. package/dist/a11y/macos-signing.js.map +1 -0
  22. package/dist/a11y/macos.d.ts +173 -0
  23. package/dist/a11y/macos.d.ts.map +1 -0
  24. package/dist/a11y/macos.js +271 -0
  25. package/dist/a11y/macos.js.map +1 -0
  26. package/dist/a11y/windows.d.ts +14 -2
  27. package/dist/a11y/windows.d.ts.map +1 -1
  28. package/dist/a11y/windows.js +31 -6
  29. package/dist/a11y/windows.js.map +1 -1
  30. package/dist/adapters/cua-driver/adapter.d.ts.map +1 -1
  31. package/dist/adapters/cua-driver/adapter.js +48 -10
  32. package/dist/adapters/cua-driver/adapter.js.map +1 -1
  33. package/dist/adapters/cua-driver/ui-tree.d.ts +1 -0
  34. package/dist/adapters/cua-driver/ui-tree.d.ts.map +1 -1
  35. package/dist/adapters/cua-driver/ui-tree.js +2 -0
  36. package/dist/adapters/cua-driver/ui-tree.js.map +1 -1
  37. package/dist/adapters/darwin-ax.d.ts +94 -0
  38. package/dist/adapters/darwin-ax.d.ts.map +1 -0
  39. package/dist/adapters/darwin-ax.js +754 -0
  40. package/dist/adapters/darwin-ax.js.map +1 -0
  41. package/dist/adapters/darwin.d.ts +43 -2
  42. package/dist/adapters/darwin.d.ts.map +1 -1
  43. package/dist/adapters/darwin.js +145 -7
  44. package/dist/adapters/darwin.js.map +1 -1
  45. package/dist/adapters/png.d.ts +6 -0
  46. package/dist/adapters/png.d.ts.map +1 -0
  47. package/dist/adapters/png.js +7 -0
  48. package/dist/adapters/png.js.map +1 -0
  49. package/dist/adapters/win32-native.d.ts +53 -0
  50. package/dist/adapters/win32-native.d.ts.map +1 -0
  51. package/dist/adapters/win32-native.js +279 -0
  52. package/dist/adapters/win32-native.js.map +1 -0
  53. package/dist/adapters/win32.d.ts +19 -1
  54. package/dist/adapters/win32.d.ts.map +1 -1
  55. package/dist/adapters/win32.js +35 -8
  56. package/dist/adapters/win32.js.map +1 -1
  57. package/dist/foreground/ladder.d.ts.map +1 -1
  58. package/dist/foreground/ladder.js +5 -1
  59. package/dist/foreground/ladder.js.map +1 -1
  60. package/dist/foreground/types.d.ts +6 -0
  61. package/dist/foreground/types.d.ts.map +1 -1
  62. package/dist/foreground/types.js.map +1 -1
  63. package/dist/foreground/windows.d.ts +2 -0
  64. package/dist/foreground/windows.d.ts.map +1 -1
  65. package/dist/foreground/windows.js +24 -11
  66. package/dist/foreground/windows.js.map +1 -1
  67. package/dist/index.d.ts +9 -0
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +7 -0
  70. package/dist/index.js.map +1 -1
  71. package/dist/native/a11y.d.ts +61 -0
  72. package/dist/native/a11y.d.ts.map +1 -0
  73. package/dist/native/a11y.js +197 -0
  74. package/dist/native/a11y.js.map +1 -0
  75. package/dist/native/env.d.ts +19 -0
  76. package/dist/native/env.d.ts.map +1 -0
  77. package/dist/native/env.js +29 -0
  78. package/dist/native/env.js.map +1 -0
  79. package/dist/native/foreground-monitor.d.ts +51 -0
  80. package/dist/native/foreground-monitor.d.ts.map +1 -0
  81. package/dist/native/foreground-monitor.js +120 -0
  82. package/dist/native/foreground-monitor.js.map +1 -0
  83. package/dist/native/presence.d.ts +24 -0
  84. package/dist/native/presence.d.ts.map +1 -0
  85. package/dist/native/presence.js +47 -0
  86. package/dist/native/presence.js.map +1 -0
  87. package/dist/native/process.d.ts +73 -0
  88. package/dist/native/process.d.ts.map +1 -0
  89. package/dist/native/process.js +302 -0
  90. package/dist/native/process.js.map +1 -0
  91. package/dist/native/protocol.d.ts +476 -0
  92. package/dist/native/protocol.d.ts.map +1 -0
  93. package/dist/native/protocol.js +601 -0
  94. package/dist/native/protocol.js.map +1 -0
  95. package/dist/native/shared.d.ts +16 -0
  96. package/dist/native/shared.d.ts.map +1 -0
  97. package/dist/native/shared.js +62 -0
  98. package/dist/native/shared.js.map +1 -0
  99. package/dist/native/watch.d.ts +35 -0
  100. package/dist/native/watch.d.ts.map +1 -0
  101. package/dist/native/watch.js +45 -0
  102. package/dist/native/watch.js.map +1 -0
  103. package/dist/presence/presence.d.ts +3 -2
  104. package/dist/presence/presence.d.ts.map +1 -1
  105. package/dist/presence/presence.js.map +1 -1
  106. package/dist/presence/windows.d.ts +2 -0
  107. package/dist/presence/windows.d.ts.map +1 -1
  108. package/dist/presence/windows.js +15 -0
  109. package/dist/presence/windows.js.map +1 -1
  110. package/helper-macos/main.swift +1756 -0
  111. package/package.json +3 -2
  112. package/src/SubprocessComputerUseHost.ts +26 -2
  113. package/src/a11y/a11y.ts +25 -11
  114. package/src/a11y/index.ts +28 -1
  115. package/src/a11y/macos-helper.ts +538 -0
  116. package/src/a11y/macos-signing.ts +280 -0
  117. package/src/a11y/macos.ts +496 -0
  118. package/src/a11y/windows.ts +31 -4
  119. package/src/adapters/cua-driver/adapter.ts +55 -15
  120. package/src/adapters/cua-driver/ui-tree.ts +2 -0
  121. package/src/adapters/darwin-ax.ts +920 -0
  122. package/src/adapters/darwin.ts +184 -4
  123. package/src/adapters/png.ts +5 -0
  124. package/src/adapters/win32-native.ts +329 -0
  125. package/src/adapters/win32.ts +53 -5
  126. package/src/foreground/ladder.ts +5 -1
  127. package/src/foreground/types.ts +6 -0
  128. package/src/foreground/windows.ts +25 -11
  129. package/src/index.ts +14 -0
  130. package/src/native/a11y.ts +267 -0
  131. package/src/native/env.ts +33 -0
  132. package/src/native/foreground-monitor.ts +165 -0
  133. package/src/native/presence.ts +58 -0
  134. package/src/native/process.ts +347 -0
  135. package/src/native/protocol.ts +1103 -0
  136. package/src/native/shared.ts +75 -0
  137. package/src/native/watch.ts +78 -0
  138. package/src/presence/presence.ts +3 -2
  139. package/src/presence/windows.ts +15 -0
@@ -0,0 +1,280 @@
1
+ import { randomBytes } from 'node:crypto'
2
+ import {
3
+ chmodSync,
4
+ existsSync,
5
+ mkdirSync,
6
+ readFileSync,
7
+ renameSync,
8
+ rmSync,
9
+ writeFileSync,
10
+ } from 'node:fs'
11
+ import { join } from 'node:path'
12
+ import { runCommand } from '../util/spawn.js'
13
+
14
+ /**
15
+ * A code-signing identity that belongs to this person's Namzu home and to
16
+ * nothing else.
17
+ *
18
+ * Why: macOS keeps the Accessibility switch (on macOS 27 it is named "Device
19
+ * Control and Data Access") against the *designated requirement* of the
20
+ * signed app. An ad-hoc signature has none that survives a rebuild (it is the
21
+ * code's own hash), and macOS 27 does not trust an ad-hoc app at all, even
22
+ * with its switch on. A self-signed identity gives the requirement
23
+ * `identifier "com.namzu.helper" and certificate leaf = H"<sha1>"`, which is
24
+ * the same for every build signed with it, so the grant is made once and
25
+ * outlives rebuilds.
26
+ *
27
+ * The identity lives in a keychain of its own, never in the login keychain:
28
+ * `<NAMZU_HOME>/computer-use/macos/signing/namzu-signing.keychain-db`, opened
29
+ * with a random password kept 0600 next to it (`kc.pw`). The certificate
30
+ * (public) is kept as `cert.pem`; the private key is imported and the loose
31
+ * key files are removed. Nothing here needs sudo or touches the system trust
32
+ * settings.
33
+ */
34
+
35
+ export const SIGNING_KEYCHAIN = 'namzu-signing.keychain-db'
36
+ export const SIGNING_PASSWORD_FILE = 'kc.pw'
37
+ export const SIGNING_CERT = 'cert.pem'
38
+ export const SIGNING_COMMON_NAME = 'Namzu Helper Local Signing'
39
+
40
+ type Run = typeof runCommand
41
+
42
+ export interface MacSigningIdentity {
43
+ /** Absolute path of the dedicated keychain. */
44
+ readonly keychain: string
45
+ /** The keychain's password. */
46
+ readonly password: string
47
+ /** The certificate's SHA-1, upper-case hex: what `codesign --sign` takes. */
48
+ readonly identity: string
49
+ }
50
+
51
+ export function macSigningDir(root: string): string {
52
+ return join(root, 'signing')
53
+ }
54
+
55
+ const OPENSSL_CONFIG = `[req]
56
+ distinguished_name=dn
57
+ x509_extensions=ext
58
+ prompt=no
59
+ [dn]
60
+ CN=${SIGNING_COMMON_NAME}
61
+ [ext]
62
+ basicConstraints=critical,CA:false
63
+ keyUsage=critical,digitalSignature
64
+ extendedKeyUsage=critical,codeSigning
65
+ `
66
+
67
+ /** `SHA1 Fingerprint=AA:BB:…` (or `sha1 Fingerprint=`) → `AABB…`. */
68
+ export function parseFingerprint(output: string): string | undefined {
69
+ const match = /sha1 fingerprint=([0-9a-f:]+)/i.exec(output)
70
+ const hex = match?.[1]?.replace(/:/g, '').toUpperCase()
71
+ return hex && /^[0-9A-F]{40}$/.test(hex) ? hex : undefined
72
+ }
73
+
74
+ function userKeychains(output: string): string[] {
75
+ return [...output.matchAll(/"([^"]+)"/g)].map((m) => m[1] as string)
76
+ }
77
+
78
+ async function identityFrom(
79
+ dir: string,
80
+ run: Run,
81
+ env: NodeJS.ProcessEnv,
82
+ ): Promise<MacSigningIdentity | undefined> {
83
+ const keychain = join(dir, SIGNING_KEYCHAIN)
84
+ const cert = join(dir, SIGNING_CERT)
85
+ const passwordFile = join(dir, SIGNING_PASSWORD_FILE)
86
+ if (!existsSync(keychain) || !existsSync(cert) || !existsSync(passwordFile)) return undefined
87
+ const result = await run('openssl', ['x509', '-in', cert, '-noout', '-fingerprint', '-sha1'], {
88
+ timeoutMs: 15_000,
89
+ env,
90
+ }).catch(() => undefined)
91
+ const identity =
92
+ result && result.exitCode === 0 ? parseFingerprint(result.stdout.toString('utf8')) : undefined
93
+ if (!identity) return undefined
94
+ // The files can all be there while the keychain is gone, corrupt or holds another key.
95
+ const listed = await run('security', ['find-identity', '-p', 'codesigning', keychain], {
96
+ timeoutMs: 15_000,
97
+ env,
98
+ }).catch(() => undefined)
99
+ if (
100
+ !listed ||
101
+ listed.exitCode !== 0 ||
102
+ !listed.stdout.toString('utf8').toUpperCase().includes(identity)
103
+ )
104
+ return undefined
105
+ return { keychain, password: readFileSync(passwordFile, 'utf8').trim(), identity }
106
+ }
107
+
108
+ /** The system openssl on a Mac (LibreSSL, which `security import` reads); a bare name elsewhere. */
109
+ function opensslCommand(): string {
110
+ return process.platform === 'darwin' && existsSync('/usr/bin/openssl')
111
+ ? '/usr/bin/openssl'
112
+ : 'openssl'
113
+ }
114
+
115
+ /** OpenSSL 3 writes a PKCS#12 that `security import` cannot read unless the old algorithms are asked for. */
116
+ async function pkcs12Compat(run: Run, env: NodeJS.ProcessEnv, openssl: string): Promise<string[]> {
117
+ const version = await run(openssl, ['version'], { timeoutMs: 15_000, env }).catch(() => undefined)
118
+ return version && /OpenSSL 3\./.test(version.stdout.toString('utf8'))
119
+ ? ['-certpbe', 'PBE-SHA1-3DES', '-keypbe', 'PBE-SHA1-3DES', '-macalg', 'sha1']
120
+ : []
121
+ }
122
+
123
+ async function must(run: Run, env: NodeJS.ProcessEnv, command: string, args: readonly string[]) {
124
+ const result = await run(command, args, { timeoutMs: 60_000, env })
125
+ if (result.exitCode !== 0)
126
+ throw new Error(`${command} ${args[0] ?? ''} failed: ${result.stderr.trim().slice(0, 300)}`)
127
+ return result
128
+ }
129
+
130
+ /**
131
+ * The identity for this Namzu home: the existing one, or a new one made once.
132
+ * Resolves `undefined` (with the reason in `onFailure`) when it cannot be
133
+ * made, so the caller can fall back to an ad-hoc signature.
134
+ */
135
+ export async function ensureSigningIdentity(options: {
136
+ readonly root: string
137
+ readonly env: NodeJS.ProcessEnv
138
+ readonly run?: Run
139
+ readonly onFailure?: (reason: string) => void
140
+ /** Called when an identity that was there could not be used and was set aside; the grant must be given again. */
141
+ readonly onBroken?: () => void
142
+ }): Promise<MacSigningIdentity | undefined> {
143
+ const run = options.run ?? runCommand
144
+ const dir = macSigningDir(options.root)
145
+ const existing = await identityFrom(dir, run, options.env)
146
+ if (existing) return existing
147
+ if (existsSync(dir)) {
148
+ // Files without a working keychain: keep them for inspection and make a new identity.
149
+ try {
150
+ renameSync(dir, `${dir}.broken-${Date.now()}`)
151
+ options.onBroken?.()
152
+ } catch {
153
+ // Another process moved it first; what is there now is tried below.
154
+ }
155
+ }
156
+ const stage = join(options.root, `signing-make-${process.pid}-${randomBytes(4).toString('hex')}`)
157
+ const openssl = opensslCommand()
158
+ try {
159
+ mkdirSync(stage, { recursive: true, mode: 0o700 })
160
+ const password = randomBytes(24).toString('hex')
161
+ const config = join(stage, 'cs.cnf')
162
+ const keyFile = join(stage, 'key.pem')
163
+ const certFile = join(stage, SIGNING_CERT)
164
+ const p12 = join(stage, 'id.p12')
165
+ const passwordFile = join(stage, SIGNING_PASSWORD_FILE)
166
+ const keychain = join(stage, SIGNING_KEYCHAIN)
167
+ writeFileSync(config, OPENSSL_CONFIG, { mode: 0o600 })
168
+ writeFileSync(passwordFile, `${password}\n`, { mode: 0o600 })
169
+ chmodSync(passwordFile, 0o600)
170
+ await must(run, options.env, openssl, [
171
+ 'req',
172
+ '-x509',
173
+ '-newkey',
174
+ 'rsa:2048',
175
+ '-nodes',
176
+ '-keyout',
177
+ keyFile,
178
+ '-out',
179
+ certFile,
180
+ '-days',
181
+ '3650',
182
+ '-config',
183
+ config,
184
+ ])
185
+ chmodSync(keyFile, 0o600)
186
+ await must(run, options.env, openssl, [
187
+ 'pkcs12',
188
+ '-export',
189
+ '-inkey',
190
+ keyFile,
191
+ '-in',
192
+ certFile,
193
+ '-out',
194
+ p12,
195
+ '-passout',
196
+ `file:${passwordFile}`,
197
+ ...(await pkcs12Compat(run, options.env, openssl)),
198
+ ])
199
+ const listed = await run('security', ['list-keychains', '-d', 'user'], {
200
+ timeoutMs: 15_000,
201
+ env: options.env,
202
+ }).catch(() => undefined)
203
+ const before = listed ? userKeychains(listed.stdout.toString('utf8')) : []
204
+ await must(run, options.env, 'security', ['create-keychain', '-p', password, keychain])
205
+ // create-keychain may add the new keychain to the person's search list; put the list back as it was.
206
+ if (before.length > 0)
207
+ await run('security', ['list-keychains', '-d', 'user', '-s', ...before], {
208
+ timeoutMs: 15_000,
209
+ env: options.env,
210
+ }).catch(() => undefined)
211
+ await must(run, options.env, 'security', ['set-keychain-settings', '-t', '300', '-l', keychain])
212
+ await must(run, options.env, 'security', ['unlock-keychain', '-p', password, keychain])
213
+ await must(run, options.env, 'security', [
214
+ 'import',
215
+ p12,
216
+ '-k',
217
+ keychain,
218
+ '-P',
219
+ password,
220
+ '-T',
221
+ '/usr/bin/codesign',
222
+ ])
223
+ await must(run, options.env, 'security', [
224
+ 'set-key-partition-list',
225
+ '-S',
226
+ 'apple-tool:,apple:',
227
+ '-s',
228
+ '-k',
229
+ password,
230
+ keychain,
231
+ ])
232
+ await lockSigningKeychain(keychain, options.env, run)
233
+ // The private key is in the keychain now; the loose copies go.
234
+ rmSync(keyFile, { force: true })
235
+ rmSync(p12, { force: true })
236
+ rmSync(config, { force: true })
237
+ chmodSync(passwordFile, 0o600)
238
+ // One step: the whole folder appears at once or, when another process was first, not at all.
239
+ mkdirSync(options.root, { recursive: true, mode: 0o700 })
240
+ try {
241
+ renameSync(stage, dir)
242
+ } catch {
243
+ // The other process's identity is used if it works.
244
+ }
245
+ return await identityFrom(dir, run, options.env)
246
+ } catch (error) {
247
+ options.onFailure?.(error instanceof Error ? error.message : String(error))
248
+ return undefined
249
+ } finally {
250
+ rmSync(stage, { recursive: true, force: true })
251
+ }
252
+ }
253
+
254
+ /** Opens the identity's keychain; codesign cannot ask for a password. */
255
+ export async function unlockSigningKeychain(
256
+ identity: MacSigningIdentity,
257
+ env: NodeJS.ProcessEnv,
258
+ run: Run = runCommand,
259
+ ): Promise<boolean> {
260
+ const result = await run(
261
+ 'security',
262
+ ['unlock-keychain', '-p', identity.password, identity.keychain],
263
+ {
264
+ timeoutMs: 15_000,
265
+ env,
266
+ },
267
+ ).catch(() => undefined)
268
+ return result?.exitCode === 0
269
+ }
270
+
271
+ /** Closes the keychain again; the private key is only needed while codesign runs. */
272
+ export async function lockSigningKeychain(
273
+ keychain: string,
274
+ env: NodeJS.ProcessEnv,
275
+ run: Run = runCommand,
276
+ ): Promise<void> {
277
+ await run('security', ['lock-keychain', keychain], { timeoutMs: 15_000, env }).catch(
278
+ () => undefined,
279
+ )
280
+ }