Skip to content

Commit df3b981

Browse files
committed
fix(packaging): ship keyring native addon
1 parent cbe0d40 commit df3b981

18 files changed

Lines changed: 341 additions & 18 deletions

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,9 @@ jobs:
13111311

13121312
- name: Install dependencies
13131313
run: |
1314-
bun install --frozen-lockfile
1314+
# The universal sidecar executes as either architecture, so its resource set must carry
1315+
# both Darwin N-API packages even though the hosted runner itself is arm64.
1316+
bun install --frozen-lockfile --os=darwin --cpu=*
13151317
cd desktop
13161318
bun install --frozen-lockfile
13171319

‎.github/workflows/release.yml‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,28 @@ jobs:
126126
include:
127127
- os: ubuntu-latest
128128
target: bun-linux-x64
129+
dependency_os: linux
130+
dependency_cpu: x64
129131
smoke: true
130132
- os: macos-latest
131133
target: bun-darwin-arm64
134+
dependency_os: darwin
135+
dependency_cpu: arm64
132136
smoke: true
133137
- os: macos-latest
134138
target: bun-darwin-x64
139+
dependency_os: darwin
140+
dependency_cpu: x64
135141
smoke: false
136142
- os: windows-latest
137143
target: bun-windows-x64
144+
dependency_os: win32
145+
dependency_cpu: x64
138146
smoke: true
139147
- os: ubuntu-latest
140148
target: bun-linux-arm64
149+
dependency_os: linux
150+
dependency_cpu: arm64
141151
smoke: false
142152
runs-on: ${{ matrix.os }}
143153
timeout-minutes: 25
@@ -153,7 +163,7 @@ jobs:
153163
uses: ./.github/actions/setup-project-bun
154164

155165
- name: Install dependencies
156-
run: bun install --frozen-lockfile
166+
run: bun install --frozen-lockfile --os=${{ matrix.dependency_os }} --cpu=${{ matrix.dependency_cpu }}
157167

158168
- name: Build dashboard
159169
run: bun run build:gui
@@ -200,9 +210,9 @@ jobs:
200210
set -euo pipefail
201211
cd "dist/standalone/$STANDALONE_TARGET"
202212
if [[ "$RUNNER_OS" == "Windows" ]]; then
203-
powershell -NoProfile -Command 'Compress-Archive -Path ocx.exe,gui -DestinationPath ("../../ocx-{0}-{1}.zip" -f $env:RELEASE_VERSION,$env:STANDALONE_TARGET) -Force'
213+
powershell -NoProfile -Command 'Compress-Archive -Path ocx.exe,gui,keyring -DestinationPath ("../../ocx-{0}-{1}.zip" -f $env:RELEASE_VERSION,$env:STANDALONE_TARGET) -Force'
204214
else
205-
tar -czf "../../ocx-${RELEASE_VERSION}-${STANDALONE_TARGET}.tar.gz" ocx gui
215+
tar -czf "../../ocx-${RELEASE_VERSION}-${STANDALONE_TARGET}.tar.gz" ocx gui keyring
206216
fi
207217
cd ../..
208218
# The pre-publication verifier resolves every recorded checksum from
@@ -232,16 +242,22 @@ jobs:
232242
include:
233243
- os: macos-latest
234244
target: universal-apple-darwin
245+
dependency_os: darwin
246+
dependency_cpu: "*"
235247
bundles: app,dmg
236248
sidecar-targets: macos
237249
artifact-suffixes: macos.dmg,macos.app.tar.gz
238250
- os: windows-latest
239251
target: x86_64-pc-windows-msvc
252+
dependency_os: win32
253+
dependency_cpu: x64
240254
bundles: msi
241255
sidecar-targets: x86_64-pc-windows-msvc
242256
artifact-suffixes: windows-x64.msi
243257
- os: ubuntu-22.04
244258
target: x86_64-unknown-linux-gnu
259+
dependency_os: linux
260+
dependency_cpu: x64
245261
bundles: appimage,deb
246262
sidecar-targets: x86_64-unknown-linux-gnu
247263
artifact-suffixes: linux-x86_64.AppImage,linux-amd64.deb
@@ -275,7 +291,7 @@ jobs:
275291
run: bun scripts/release-version-sources.ts check "$RELEASE_VERSION"
276292

277293
- name: Install project dependencies
278-
run: bun install --frozen-lockfile
294+
run: bun install --frozen-lockfile --os=${{ matrix.dependency_os }} --cpu=${{ matrix.dependency_cpu }}
279295

280296
- name: Build dashboard
281297
run: bun run build:gui

‎desktop/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ bunx tauri dev
1010
```
1111

1212
The sidecar is generated from the repository's standalone binary build and is
13-
not checked into git.
13+
not checked into git. That build also stages the target-matching native keyring addon
14+
under `keyring/`; Tauri copies it as a resource because Bun cannot load a `.node` addon
15+
from the compiled executable's virtual filesystem. Universal macOS preparation requires
16+
both Darwin optional packages (`bun install --frozen-lockfile --os=darwin --cpu=*`).
1417

1518
The macOS tray panel is a SwiftUI/AppKit static library built from
1619
`app/Sources/NativeTray` by the Rust build script and linked into this process.

‎desktop/scripts/prepare-sidecar.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if (!existsSync(executable)) {
5252
const desktopRoot = resolve(import.meta.dir, "..");
5353
const binaries = join(desktopRoot, "src-tauri", "binaries");
5454
const resources = join(desktopRoot, "src-tauri", "resources", "gui", "dist");
55+
const keyringResources = join(desktopRoot, "src-tauri", "resources", "keyring");
5556
mkdirSync(binaries, { recursive: true });
5657
mkdirSync(resources, { recursive: true });
5758
const destination = join(binaries, `ocx-${triple}${target.startsWith("bun-windows-") ? ".exe" : ""}`);
@@ -60,5 +61,6 @@ if (shouldAdHocSignSidecar(process.platform, target)) {
6061
const signed = adHocSignSidecar(destination);
6162
if (signed !== 0) process.exit(signed);
6263
}
64+
cpSync(join(source, "keyring"), keyringResources, { recursive: true });
6365
cpSync(join(repoRoot, "gui", "dist"), resources, { recursive: true });
6466
console.log(`Prepared ${destination}`);

‎desktop/scripts/verify-macos-runtime.sh‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ app="${1:?usage: verify-macos-runtime.sh /path/to/OpenCodex.app}"
66
executable="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "$app/Contents/Info.plist")"
77
[[ -n "$executable" && "$executable" != */* ]] || { echo 'Invalid app executable name' >&2; exit 1; }
88
scratch="$(mktemp -d "${TMPDIR:-/tmp}/opencodex-bundle-check.XXXXXX")"
9-
trap 'rm -rf "$scratch"' EXIT
9+
proxy_pid=""
10+
cleanup() {
11+
if [[ -n "$proxy_pid" ]]; then
12+
kill "$proxy_pid" 2>/dev/null || true
13+
wait "$proxy_pid" 2>/dev/null || true
14+
fi
15+
rm -rf "$scratch"
16+
}
17+
trap cleanup EXIT
1018

1119
codesign --verify --strict --deep "$app"
1220
verify_member() {
@@ -39,4 +47,32 @@ value = json.loads(pathlib.Path(sys.argv[1]).read_text())
3947
assert value.get("schema") == "ocx-resolve/1", "Unexpected resolve schema"
4048
assert value.get("liveness", {}).get("status") in ("live", "absent-proven"), "Unusable resolve result"
4149
PY
42-
printf '%s\n' 'PASS: macOS signatures, exact entitlements, hardened runtime, Liquid Glass and bundled CLI resolve'
50+
51+
# Reproduce the packaged-keyring boundary from an unrelated cwd. Source-tree smoke tests prove the
52+
# OS store, but they do not prove that Bun can find the N-API addon outside its virtual `$bunfs`.
53+
mkdir -p "$scratch/home" "$scratch/work"
54+
(
55+
cd "$scratch/work"
56+
HOME="$scratch/home" OPENCODEX_HOME="$scratch/home/.opencodex" \
57+
"$app/Contents/MacOS/ocx" start --port 10179 > "$scratch/proxy.log" 2>&1
58+
) &
59+
proxy_pid=$!
60+
for _ in $(seq 1 30); do
61+
curl -fsS http://127.0.0.1:10179/healthz >/dev/null 2>&1 && break
62+
sleep 1
63+
done
64+
curl -fsS http://127.0.0.1:10179/healthz >/dev/null
65+
(
66+
cd "$scratch/work"
67+
HOME="$scratch/home" OPENCODEX_HOME="$scratch/home/.opencodex" \
68+
"$app/Contents/MacOS/ocx" provider keychain openai status --json > "$scratch/keyring.json"
69+
)
70+
python3 - "$scratch/keyring.json" <<'PY'
71+
import json, pathlib, sys
72+
value = json.loads(pathlib.Path(sys.argv[1]).read_text())
73+
assert value.get("keychainAvailable") is True, "Packaged keyring binding is unavailable"
74+
PY
75+
kill "$proxy_pid" 2>/dev/null || true
76+
wait "$proxy_pid" 2>/dev/null || true
77+
proxy_pid=""
78+
printf '%s\n' 'PASS: macOS signatures, entitlements, hardened runtime, Liquid Glass, bundled CLI resolve and packaged keyring'

‎desktop/src-tauri/tauri.conf.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"binaries/ocx"
2323
],
2424
"resources": {
25-
"resources/gui/dist": "gui/dist"
25+
"resources/gui/dist": "gui/dist",
26+
"resources/keyring": "keyring"
2627
},
2728
"icon": [
2829
"icons/icon.icns",

‎scripts/build-standalone.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
22
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
33
import { join, resolve, basename } from "node:path";
44
import { isStandaloneTarget, standaloneExecutableName } from "./standalone-targets";
5+
import { stageStandaloneKeyringAddon } from "./standalone-keyring";
56

67
function hostTarget(): string {
78
const platform = process.platform === "darwin" ? "darwin" : process.platform === "win32" ? "windows" : "linux";
@@ -77,6 +78,10 @@ try {
7778
}
7879
if (compileExitCode !== 0) process.exit(compileExitCode);
7980

81+
// N-API binaries cannot execute from Bun's virtual `$bunfs`. Keep the exact target addon outside
82+
// the compiled executable so source/npm resolution and packaged resolution share one binding API.
83+
stageStandaloneKeyringAddon(repoRoot, output, target);
84+
8085
// bun's ad-hoc linker signature does not always cover the embedded payload;
8186
// macOS kills the executable on launch (SIGKILL) unless it is re-signed.
8287
if (process.platform === "darwin") {

‎scripts/keyring-smoke.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bun
22
import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
3+
import { loadKeyringBinding } from "../src/lib/keyring-native";
34

45
export interface KeyringSmokeEntry {
56
setSecret(secret: Uint8Array, signal?: AbortSignal): Promise<void>;
@@ -15,8 +16,8 @@ export interface KeyringSmokeOptions {
1516
}
1617

1718
async function createOsEntry(service: string, account: string): Promise<KeyringSmokeEntry> {
18-
const { AsyncEntry } = await import("@napi-rs/keyring");
19-
return new AsyncEntry(service, account);
19+
const { AsyncEntry } = loadKeyringBinding();
20+
return new AsyncEntry(service, account) as unknown as KeyringSmokeEntry;
2021
}
2122

2223
export async function runKeyringSmoke({

‎scripts/standalone-keyring.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
2+
import { join } from "node:path";
3+
import { keyringAssetForStandaloneTarget } from "../src/lib/keyring-native";
4+
5+
/** Stage the platform N-API addon outside Bun's virtual filesystem beside a standalone binary. */
6+
export function stageStandaloneKeyringAddon(repoRoot: string, output: string, target: string): string {
7+
const asset = keyringAssetForStandaloneTarget(target);
8+
if (!asset) throw new Error(`No keyring native asset is declared for standalone target ${target}`);
9+
const source = join(repoRoot, "node_modules", asset.packageName, asset.filename);
10+
if (!existsSync(source)) {
11+
throw new Error(
12+
`Missing ${asset.packageName}/${asset.filename}; install target optional dependencies before building ${target}`,
13+
);
14+
}
15+
const keyringDir = join(output, "keyring");
16+
mkdirSync(keyringDir, { recursive: true });
17+
const destination = join(keyringDir, asset.filename);
18+
copyFileSync(source, destination);
19+
return destination;
20+
}

‎src/codex/native-profile-store.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from "node:fs";
2121
import { basename, dirname, join, resolve } from "node:path";
2222
import { getConfigDir } from "../config";
23+
import { loadKeyringBinding } from "../lib/keyring-native";
2324
import { extractAccountId } from "../oauth/chatgpt";
2425
import { getCodexHome, readRootTomlString } from "./paths";
2526
import {
@@ -120,7 +121,7 @@ export class OsNativeProfileKeyProvider implements NativeProfileKeyProvider {
120121

121122
private async entry(homeId: string): Promise<NativeKeyringEntry> {
122123
try {
123-
const { AsyncEntry } = await import("@napi-rs/keyring");
124+
const { AsyncEntry } = loadKeyringBinding();
124125
return new AsyncEntry(KEYRING_SERVICE, homeId) as unknown as NativeKeyringEntry;
125126
} catch {
126127
throw new NativeProfileError(

0 commit comments

Comments
 (0)