forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·1834 lines (1686 loc) · 84.6 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·1834 lines (1686 loc) · 84.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -e
# Force C locale for numeric formatting so `printf %f` accepts the
# dot-decimal values produced by `bc` even when the user's shell runs in
# a non-English locale (e.g. de_DE.UTF-8 expects a comma separator).
export LC_NUMERIC=C
# Codex, launchd, and other non-login shells may not inherit Homebrew's bin
# directory. Keep the launcher self-contained so tools such as pkg-config are
# discoverable on both Apple Silicon and Intel Macs.
# shellcheck source=launcher-bootstrap.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/scripts/launcher-bootstrap.sh"
omi_configure_homebrew_path
# ─── Arguments ─────────────────────────────────────────────────────────
YOLO_MODE=0
FORCE_FULL_BUNDLE="${OMI_FORCE_FULL_BUNDLE:-0}"
# Who asked for the full bundle. `--full` / OMI_FORCE_FULL_BUNDLE=1 is the
# caller's habit and is refused on a reusable E2E pool slot; OMI_FORCE_REWIND_SEED
# forces the install/seed path from inside the launcher and stays allowed.
FULL_BUNDLE_EXPLICIT=0
[ "$FORCE_FULL_BUNDLE" = "1" ] && FULL_BUNDLE_EXPLICIT=1
# Reseeding replaces an existing named profile after preserving it. It must run
# through the install/seed path; a fast executable patch intentionally skips it.
if [ "${OMI_FORCE_REWIND_SEED:-0}" = "1" ]; then
FORCE_FULL_BUNDLE=1
fi
FAST_ONLY=0
NO_WAIT="${NO_WAIT:-0}"
SHOW_HELP=0
for arg in "$@"; do
case "$arg" in
--yolo)
YOLO_MODE=1
;;
--full)
FORCE_FULL_BUNDLE=1
FULL_BUNDLE_EXPLICIT=1
;;
--fast-only)
FAST_ONLY=1
;;
--no-wait)
NO_WAIT=1
;;
--help|-h)
SHOW_HELP=1
;;
*)
echo "ERROR: unknown option: $arg" >&2
exit 2
;;
esac
done
if [ "$FAST_ONLY" = "1" ] && [ "$FORCE_FULL_BUNDLE" = "1" ]; then
echo "ERROR: --fast-only cannot be combined with --full or OMI_FORCE_FULL_BUNDLE=1" >&2
exit 2
fi
# ─── Help ──────────────────────────────────────────────────────────────
if [ "$SHOW_HELP" = "1" ]; then
cat <<'USAGE'
Usage: ./run.sh [options]
Build and run the Omi Desktop dev app with local backend services.
Options (via environment variables):
OMI_SKIP_BACKEND=1 Skip starting Python backend (use remote backend via OMI_DESKTOP_API_URL)
OMI_SKIP_TUNNEL=1 Skip Cloudflare tunnel (use OMI_DESKTOP_API_URL from .env directly)
PORT=10201 Desktop backend port (default: 10201, never use 8080)
OMI_APP_NAME="Omi Dev" App name (default: "Omi Dev")
OMI_SKIP_AUTH_SEED=1 Do not copy auth/onboarding from the auth seed source into named bundles
OMI_AUTH_DUMP_SOURCE="..." Auth seed source bundle id (default: com.omi.desktop-dev, with a
production com.omi.computer-macos fallback when that has no session)
OMI_SKIP_SETTINGS_SEED=1 Do not mirror shortcuts/settings into named bundles
OMI_SETTINGS_SEED_SOURCE="..." Settings authority bundle id (default: resolve production
com.omi.computer-macos when installed, else com.omi.desktop-dev;
a named-but-missing domain fails closed)
OMI_SKIP_REWIND_SEED=1 Do not copy the local Rewind history into a new named bundle
OMI_FORCE_REWIND_SEED=1 Replace an existing named-bundle Rewind history with a fresh snapshot
OMI_DEV_EAGER_PERMISSIONS=1 Preserve eager mic/screen/file startup behavior in named bundles
OMI_PYTHON_API_URL="..." Python backend URL (explicit override; named bundles default to dev)
OMI_JIT_QA_TARGET="..." omi-jit-qa only: local-dev-gcp, deployed-dev, or cloud-qa atomic endpoint tuple
OMI_SIGN_IDENTITY="..." Code signing identity (auto-detected if not set)
OMI_FORCE_FULL_BUNDLE=1 Rebuild the complete app bundle on this launch
(E2E pool slots: refused while the fast bundle is reusable — use --fast-only)
OMI_SCAN_STALE_BUNDLES=1 Remove stale same-named app bundles under $HOME (recovery only)
OMI_ENABLE_LOCAL_AUTOMATION=1 Force the automation bridge on (auto-on for non-prod bundles; see scripts/omi-ctl)
OMI_DISABLE_LOCAL_AUTOMATION=1 Run a dev build "clean" with the bridge off
OMI_AUTOMATION_PORT=47777 Bridge port (set per bundle when running several at once)
OMI_AUTOMATION_UI_MODE=quiet Window presentation for automation: quiet, interactive, or normal
OMI_FORCE_CANONICAL_MEMORY_ATLAS=1 Non-production-only local QA override for the canonical atlas rollout gate
OMI_DESKTOP_LOCAL_PROFILE=1 Local harness profile; localhost endpoints/Auth emulator only
Required files:
../../backend/.env Environment variables (copy from ../../backend/.env.template)
../../backend/google-credentials.json GCP service account key
Required tools:
xcrun/swift, python3, uv, npm, node, codesign, cloudflared (unless skipped)
Port allocation (avoid 8080 to prevent port conflicts):
Backend default: 10201
Examples:
./run.sh # Fast incremental launch after first run
OMI_SKIP_BACKEND=1 ./run.sh # App only (backend running elsewhere)
OMI_SKIP_TUNNEL=1 ./run.sh # No Cloudflare tunnel (use direct URL)
./run.sh --yolo # Quick start: use dev backend, no local services
./run.sh --full # Rebuild every packaged dependency
./run.sh --fast-only # Reuse an eligible installed bundle or fail without packaging
./run.sh --fast-only --no-wait # Relaunch an eligible remote/harness bundle, then return
USAGE
exit 0
fi
# ─── YOLO mode: use dev backend, zero local setup ────────────────────
# Keep these endpoint values aligned with DesktopBackendEnvironment's dev
# defaults. The dev services currently mint prod Firebase identities, so this
# is a service-revision target, not an isolated local-data harness.
apply_yolo_env() {
export OMI_SKIP_BACKEND=1
export OMI_SKIP_TUNNEL=1
# `--yolo` supplies remote-development defaults, but must not discard an
# explicitly targeted backend. Named QA and fault-injection bundles use
# these overrides to exercise a chosen service revision without requiring
# a local desktop backend or .env file.
export OMI_DESKTOP_API_URL="${OMI_DESKTOP_API_URL:-https://desktop-backend-dt5lrfkkoa-uc.a.run.app}"
export OMI_PYTHON_API_URL="${OMI_PYTHON_API_URL:-https://api.omiapi.com}"
export FIREBASE_API_KEY="AIzaSyD9dzBdglc7IO9pPDIOvqnCoTis_xKkkC8"
}
if [ "$YOLO_MODE" = "1" ]; then
echo ""
echo "=========================================="
echo " YOLO MODE — using development backend"
echo "=========================================="
echo ""
echo " WARNING: This connects directly to the dev Cloud Run backends."
echo " They currently use production Firebase identities and data stores."
echo " No local Python backend, no local auth, no tunnel."
echo " This is a temporary shortcut — will be removed once"
echo " desktop dev setup friction is fully resolved."
echo ""
echo "=========================================="
echo ""
apply_yolo_env
fi
# Clear system OPENAI_API_KEY so .env takes precedence
unset OPENAI_API_KEY
# Use Xcode's default toolchain to match the SDK version
unset TOOLCHAINS
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=fast-dev-bundle.sh
source "$SCRIPT_DIR/scripts/fast-dev-bundle.sh"
# shellcheck source=local-profile-env.sh
source "$SCRIPT_DIR/scripts/local-profile-env.sh"
# shellcheck source=jit-qa-target.sh
source "$SCRIPT_DIR/scripts/jit-qa-target.sh"
# Reject an invalid reserved-bundle request before dev-instance creates a
# scratch directory or the launcher acquires a build lock.
REQUESTED_LOCAL_PROFILE=false
[ "${OMI_DESKTOP_LOCAL_PROFILE:-0}" = "1" ] && REQUESTED_LOCAL_PROFILE=true
omi_preflight_jit_qa_launch_request \
"${OMI_APP_NAME:-}" "${OMI_BUNDLE_ID:-}" "$YOLO_MODE" "$REQUESTED_LOCAL_PROFILE" || exit $?
if [ -n "${OMI_JIT_QA_TARGET:-}" ]; then
omi_jit_qa_set_exact_tuple || exit $?
fi
# The backend .env is shell-sourced later and the selected app env is copied
# into the bundle. Reject stale/mixed source tuples before dev-instance creates
# scratch state or the launcher acquires a build lock.
EARLY_BACKEND_DIR="$(cd "$SCRIPT_DIR/../../backend" && pwd)"
omi_preflight_jit_qa_config_file "$EARLY_BACKEND_DIR/.env" || exit $?
if [ -f "$SCRIPT_DIR/.env.app.dev" ]; then
omi_preflight_jit_qa_config_file "$SCRIPT_DIR/.env.app.dev" || exit $?
elif [ -f "$SCRIPT_DIR/.env.app" ]; then
omi_preflight_jit_qa_config_file "$SCRIPT_DIR/.env.app" || exit $?
fi
# shellcheck source=python-desktop-backend-dev.sh
source "$SCRIPT_DIR/scripts/python-desktop-backend-dev.sh"
# Timing utilities
SCRIPT_START_TIME=$(date +%s.%N)
STEP_START_TIME=$SCRIPT_START_TIME
step() {
local now=$(date +%s.%N)
local step_elapsed=$(echo "$now - $STEP_START_TIME" | bc)
local total_elapsed=$(echo "$now - $SCRIPT_START_TIME" | bc)
if [ "$STEP_START_TIME" != "$SCRIPT_START_TIME" ]; then
printf " └─ done (%.2fs)\n" "$step_elapsed"
fi
STEP_START_TIME=$now
printf "[%6.1fs] %s\n" "$total_elapsed" "$1"
}
substep() {
local now=$(date +%s.%N)
local total_elapsed=$(echo "$now - $SCRIPT_START_TIME" | bc)
printf "[%6.1fs] ├─ %s\n" "$total_elapsed" "$1"
}
macos_copy_tree() {
local src="$1"
local dest="$2"
if [ "$(uname -s)" = "Darwin" ] && command -v ditto >/dev/null 2>&1; then
ditto --norsrc "$src" "$dest"
elif [ "$(uname -s)" = "Darwin" ]; then
cp -R -X "$src" "$dest"
else
cp -R "$src" "$dest"
fi
}
# Per-worktree isolation: derive unique ports + bundle name so parallel worktrees don't
# collide. Sets OMI_INSTANCE / RUST_PORT / PYTHON_PORT / AUTOMATION_PORT / OMI_APP_NAME /
# OMI_DEV_DIR (explicit overrides always win; the primary checkout keeps "Omi Dev" + 10201).
source "$SCRIPT_DIR/../../scripts/dev-instance.sh"
BACKEND_PORT="${PORT:-$RUST_PORT}"
export PORT="$BACKEND_PORT"
# Serialize same-worktree builds only (shared Desktop/.build + build/$APP_NAME.app).
# Cross-worktree ./run.sh must not block each other. Hold through install/seed/open,
# then release before the long-running wait — see scripts/run-sh-build-lock.sh.
# Explicit OMI_APP_NAME overrides that collide across worktrees are unsupported
# (/Applications/$APP_NAME.app is machine-global and not cross-locked).
source "$SCRIPT_DIR/scripts/run-sh-build-lock.sh"
omi_run_sh_acquire_build_lock "another ./run.sh in this worktree" 600 || exit 1
# Temporary until `trap cleanup EXIT` below chains release into cleanup().
trap 'omi_run_sh_release_build_lock' EXIT INT TERM
# App configuration
BINARY_NAME="Omi Computer" # Package.swift target — binary paths, pkill, CFBundleExecutable
source "$SCRIPT_DIR/scripts/app-config.sh"
derive_omi_app_config "${OMI_APP_NAME:-Omi Dev}" || exit 1
# A pre-authorized E2E pool slot is machine-global and shared by every worktree;
# building one without holding its lease clobbers another lane's app mid-test.
# Non-pool names pass through untouched. See docs/e2e-bundle-pool.md.
# verify passes for the holder and prints the pool slot number on stdout
# (empty for a non-pool named bundle); the slot number drives the pool launch
# policy below — the fail-closed guards key on it.
if [ "$IS_NAMED_BUNDLE" = true ]; then
E2E_POOL_SLOT="$("$SCRIPT_DIR/scripts/omi-e2e-pool" verify "$APP_SLUG")" || exit 1
fi
LOCAL_PROFILE=false
[ "${OMI_DESKTOP_LOCAL_PROFILE:-0}" = "1" ] && LOCAL_PROFILE=true
# Gate-G bundle routing is a whole-tuple authority. Revalidate the fully
# derived app identity, then reapply after .env loads below.
omi_prepare_jit_qa_target "$APP_NAME" "$BUNDLE_ID" "$YOLO_MODE" derived "$LOCAL_PROFILE" || exit $?
# A named QA bundle should exercise the shared development service unless its
# launcher deliberately selects another profile. Check variable *presence*,
# not values: `OMI_SKIP_BACKEND=0` is an explicit local-launch request and
# must never be overwritten by the remote-dev defaults.
should_default_named_bundle_to_dev_backend() {
[ "${IS_NAMED_BUNDLE:-false}" = true ] \
&& [ "${LOCAL_PROFILE:-false}" = false ] \
&& [ "${YOLO_MODE:-0}" != "1" ] \
&& [ -z "${OMI_SKIP_BACKEND+x}" ] \
&& [ -z "${OMI_SKIP_TUNNEL+x}" ] \
&& [ -z "${OMI_DESKTOP_API_URL+x}" ] \
&& [ -z "${OMI_PYTHON_API_URL+x}" ]
}
NAMED_BUNDLE_DEFAULT_DEV_BACKEND=false
if should_default_named_bundle_to_dev_backend; then
NAMED_BUNDLE_DEFAULT_DEV_BACKEND=true
fi
# Named QA bundles are remote-dev by default. Apply this before any launch
# preparation so they do not start a local backend or tunnel, and reapply it
# after sourcing backend/.env below so repository-local defaults cannot
# silently retarget a QA bundle. Explicit launch environment values above opt
# out and remain authoritative.
if [ "$NAMED_BUNDLE_DEFAULT_DEV_BACKEND" = true ]; then
substep "Named bundle default: using development backend"
apply_yolo_env
fi
# A detached launch is safe only when another service owns the backend. It is
# intended for the remote-dev and local-harness fast lanes; a run.sh-owned
# backend must remain attached so its lifecycle and logs stay scoped here.
if [ "$NO_WAIT" = "1" ] \
&& { [ "${OMI_SKIP_BACKEND:-0}" != "1" ] || [ "${OMI_SKIP_TUNNEL:-0}" != "1" ]; }; then
echo "ERROR: --no-wait requires OMI_SKIP_BACKEND=1 and OMI_SKIP_TUNNEL=1 (use --yolo or the local harness)." >&2
exit 2
fi
BUILD_DIR="build"
APP_BUNDLE="$BUILD_DIR/$APP_NAME.app"
APP_PATH="/Applications/$APP_NAME.app"
# Agent runtime source (staged into the app bundle at Resources/agent below).
# Without this, `[ -d "$AGENT_DIR/dist" ]` tests an empty path and the agent
# copy is silently skipped → app shows "AI components missing".
AGENT_DIR="$SCRIPT_DIR/agent"
AGENT_PACKAGED_NODE_MODULES="$SCRIPT_DIR/.harness/agent-runtime/agent-node_modules"
PI_MONO_PACKAGED_NODE_MODULES="$SCRIPT_DIR/.harness/agent-runtime/pi-mono-extension-node_modules"
APP_DESKTOP_PATH="$HOME/Desktop/$APP_NAME.app"
APP_DOWNLOADS_PATH="$HOME/Downloads/$APP_NAME.app"
SIGN_IDENTITY="${OMI_SIGN_IDENTITY:-}"
# Stable self-signed fallback identity for machines with no Apple certificate.
# See desktop/macos/AGENTS.md → Local Code Signing for how to create it.
OMI_LOCAL_DEV_SIGN_IDENTITY="Omi Local Dev Signing"
SIGN_IDENTITY_TEAM_ID=""
SIGN_IDENTITY_TEAM_ID_RESOLVED_FOR=""
if [ "$LOCAL_PROFILE" = true ]; then
if [ "$BUNDLE_ID" = "com.omi.desktop-dev" ] || { [ "$IS_NAMED_BUNDLE" = false ] && [ "$APP_NAME" = "Omi Dev" ]; }; then
echo "ERROR: OMI_DESKTOP_LOCAL_PROFILE=1 cannot target Omi Dev (com.omi.desktop-dev)."
echo " Local profile would overwrite Omi Dev auth/state/binary. Use a named omi- bundle instead:"
echo " DESKTOP_APP_NAME=omi-memory make desktop-run-local"
echo " or: cd desktop/macos && OMI_APP_NAME=omi-memory OMI_DESKTOP_LOCAL_PROFILE=1 OMI_SKIP_BACKEND=1 OMI_SKIP_TUNNEL=1 ./run.sh"
exit 1
fi
if [ "$IS_NAMED_BUNDLE" = true ]; then
case "$APP_NAME" in
omi-*|Omi-*) ;;
*)
echo "ERROR: OMI_DESKTOP_LOCAL_PROFILE=1 with OMI_APP_NAME requires an omi- prefixed bundle (got OMI_APP_NAME='$APP_NAME')"
exit 1
;;
esac
export OMI_LOCAL_PROFILE_STORAGE_NAME="${OMI_LOCAL_PROFILE_STORAGE_NAME:-$APP_NAME}"
else
echo "ERROR: OMI_DESKTOP_LOCAL_PROFILE=1 requires an omi- prefixed named bundle (OMI_APP_NAME or DESKTOP_APP_NAME)"
exit 1
fi
if [ "${OMI_SKIP_BACKEND:-0}" != "1" ] || [ "${OMI_SKIP_TUNNEL:-0}" != "1" ]; then
echo "ERROR: Omi Dev local harness requires OMI_SKIP_BACKEND=1 and OMI_SKIP_TUNNEL=1; start the harness with make dev-up first"
exit 1
fi
case "${OMI_DESKTOP_API_URL:-}" in http://127.*|http://localhost*) ;; *) echo "ERROR: OMI_DESKTOP_API_URL must be localhost for Omi Dev local harness"; exit 1 ;; esac
case "${OMI_PYTHON_API_URL:-}" in http://127.*|http://localhost*) ;; *) echo "ERROR: OMI_PYTHON_API_URL must be localhost for Omi Dev local harness"; exit 1 ;; esac
if [ "${FIREBASE_PROJECT_ID:-}" != "demo-omi-local" ] || [ "${FIREBASE_AUTH_PROJECT_ID:-demo-omi-local}" != "demo-omi-local" ]; then
echo "ERROR: Omi Dev local harness must use Firebase project demo-omi-local"
exit 1
fi
fi
AUTOMATION_PORT="${OMI_AUTOMATION_PORT:-${AUTOMATION_PORT:-47777}}"
AUTOMATION_CAPTURE_ROOT="${OMI_AUTOMATION_CAPTURE_ROOT:-$SCRIPT_DIR/.harness/runs}"
AUTOMATION_UI_MODE="$(derive_omi_automation_ui_mode "$IS_NAMED_BUNDLE" "${OMI_AUTOMATION_UI_MODE:-}")" || exit 2
# An external harness may request an ownership proof for a detached `open`
# launch. The token is passed as an app argument (and therefore visible in the
# spawned process command) and copied into the owner-only launch signal. It is
# intentionally opt-in so ordinary local launches keep their existing behavior.
DESKTOP_LAUNCH_TOKEN="${OMI_DESKTOP_LAUNCH_TOKEN:-}"
if [[ -n "$DESKTOP_LAUNCH_TOKEN" && ! "$DESKTOP_LAUNCH_TOKEN" =~ ^[A-Za-z0-9_-]{16,128}$ ]]; then
echo "ERROR: OMI_DESKTOP_LAUNCH_TOKEN must be 16-128 URL-safe characters" >&2
exit 2
fi
AUTOMATION_ARGS=("--automation-port=$AUTOMATION_PORT" "--automation-capture-root=$AUTOMATION_CAPTURE_ROOT")
if [ "$AUTOMATION_UI_MODE" != "normal" ]; then
AUTOMATION_ARGS+=("--automation-ui=$AUTOMATION_UI_MODE")
fi
if [ "${OMI_ENABLE_LOCAL_AUTOMATION:-0}" = "1" ]; then
AUTOMATION_ARGS=(--automation-bridge "${AUTOMATION_ARGS[@]}")
fi
# Backend configuration
BACKEND_DIR="$(cd "$SCRIPT_DIR/../../backend" && pwd)"
BACKEND_PID=""
BACKEND_REUSED_PID=""
BACKEND_PIDFILE="$OMI_DEV_DIR/python-desktop-backend.pid"
BACKEND_METADATA="$OMI_DEV_DIR/python-desktop-backend.meta"
TUNNEL_PID=""
TUNNEL_URL="${TUNNEL_URL:-}"
AUTH_CACHE=""
# Cleanup function to stop backend, auth, and tunnel on exit
cleanup() {
# Release the build lock if still held (early exit before install, or if
# the post-install release was skipped). Chained here because
# `trap cleanup EXIT` overwrites the earlier lock-only trap.
omi_run_sh_release_build_lock
if [ -n "$AUTH_CACHE" ]; then
rm -f "$AUTH_CACHE"
fi
if [ -n "$TUNNEL_PID" ] && kill -0 "$TUNNEL_PID" 2>/dev/null; then
echo "Stopping tunnel (PID: $TUNNEL_PID)..."
kill "$TUNNEL_PID" 2>/dev/null || true
fi
if [ -n "$BACKEND_PID" ] && kill -0 "$BACKEND_PID" 2>/dev/null; then
echo "Stopping backend (PID: $BACKEND_PID)..."
kill "$BACKEND_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT
# The local self-signed identity, created without a GUI and without a password prompt.
#
# The documented way to make this identity is Keychain Access -> Certificate Assistant, which
# is a wizard: unusable from CI, from an agent, and from anyone who has not read the doc. The
# ingredients are all stock, so the wizard is not actually required -- /usr/bin/openssl is
# LibreSSL on every Mac and supports the legacy PKCS#12 encoding Apple's importer accepts
# (OpenSSL 3's AES/SHA256 default is rejected with "MAC verification failed").
#
# It lives in its own keychain rather than in login.keychain on purpose. We own that
# keychain's password, so we can unlock it and grant codesign a partition non-interactively --
# which is the entire point, since the login keychain is exactly what we cannot do that to.
# The password protects a throwaway self-signed certificate and nothing else, so it is a
# constant rather than a secret.
OMI_LOCAL_SIGN_KEYCHAIN="$HOME/Library/Keychains/omi-local-dev-signing.keychain-db"
OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD="omi-local-dev"
# Whether an identity can actually be USED here, which is not the same question as whether it
# exists. `security find-identity` lists identities whose private key the keychain may still
# refuse to hand over -- and refuse instantly, with no prompt, in any session that cannot draw
# one. Probing costs one signature on a temp file and turns a failure that used to surface
# after the whole build into a fact known before it starts.
signing_identity_usable() {
local identity="$1" probe status
[ -n "$identity" ] || return 1
probe="$(mktemp "${TMPDIR:-/tmp}/omi-sign-probe.XXXXXX")" || return 1
cp /usr/bin/true "$probe" 2>/dev/null || { rm -f "$probe"; return 1; }
codesign --force --sign "$identity" "$probe" >/dev/null 2>&1
status=$?
rm -f "$probe"
return $status
}
# Make the keychain visible to codesign without disturbing the user's search list.
#
# `security list-keychains -s` REPLACES the list rather than appending, so the existing entries
# have to be read and passed back verbatim. They come back one per line, quoted and indented,
# and each path may contain spaces -- so they are collected into an array and re-quoted by the
# shell. Word-splitting them instead corrupts the list, which is not a theoretical concern:
# an earlier version of this function did exactly that and rewrote the user's login keychain
# entry into a nested-quoted path that no longer resolved.
add_keychain_to_search_list() {
local keychain="$1" line
local -a current=()
while IFS= read -r line; do
line="${line#"${line%%[![:space:]]*}"}" # strip leading whitespace
line="${line#\"}"
line="${line%\"}"
[ -n "$line" ] || continue
[ "$line" = "$keychain" ] && return 0 # already present, nothing to do
current+=("$line")
done < <(security list-keychains -d user)
security list-keychains -d user -s "${current[@]}" "$keychain" 2>/dev/null
}
ensure_local_dev_signing_identity() {
if signing_identity_usable "$OMI_LOCAL_DEV_SIGN_IDENTITY"; then
return 0
fi
if [ -f "$OMI_LOCAL_SIGN_KEYCHAIN" ]; then
# Present but unusable almost always means "locked since reboot".
security unlock-keychain -p "$OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD" "$OMI_LOCAL_SIGN_KEYCHAIN" 2>/dev/null
add_keychain_to_search_list "$OMI_LOCAL_SIGN_KEYCHAIN"
signing_identity_usable "$OMI_LOCAL_DEV_SIGN_IDENTITY" && return 0
fi
step "Creating local signing identity ($OMI_LOCAL_DEV_SIGN_IDENTITY)..."
local work
work="$(mktemp -d "${TMPDIR:-/tmp}/omi-local-sign.XXXXXX")" || return 1
# codeSigning EKU and CA:false are both required, or codesign declines the identity.
cat > "$work/openssl.cnf" <<EOF
[req]
distinguished_name = dn
x509_extensions = v3
prompt = no
[dn]
CN = $OMI_LOCAL_DEV_SIGN_IDENTITY
[v3]
basicConstraints = critical,CA:false
keyUsage = critical,digitalSignature
extendedKeyUsage = critical,codeSigning
EOF
if ! /usr/bin/openssl req -x509 -newkey rsa:2048 -keyout "$work/key.pem" -out "$work/cert.pem" \
-days 3650 -nodes -config "$work/openssl.cnf" >/dev/null 2>&1; then
rm -rf "$work"; return 1
fi
if ! /usr/bin/openssl pkcs12 -export -inkey "$work/key.pem" -in "$work/cert.pem" \
-out "$work/identity.p12" -passout "pass:$OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD" \
-name "$OMI_LOCAL_DEV_SIGN_IDENTITY" \
-certpbe PBE-SHA1-3DES -keypbe PBE-SHA1-3DES -macalg sha1 >/dev/null 2>&1; then
rm -rf "$work"; return 1
fi
if [ ! -f "$OMI_LOCAL_SIGN_KEYCHAIN" ]; then
security create-keychain -p "$OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD" "$OMI_LOCAL_SIGN_KEYCHAIN" 2>/dev/null || { rm -rf "$work"; return 1; }
# No idle timeout and no lock-on-sleep: a keychain that relocks mid-build reintroduces
# exactly the interactive prompt this exists to avoid.
security set-keychain-settings "$OMI_LOCAL_SIGN_KEYCHAIN" 2>/dev/null
fi
security unlock-keychain -p "$OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD" "$OMI_LOCAL_SIGN_KEYCHAIN" 2>/dev/null
security import "$work/identity.p12" -k "$OMI_LOCAL_SIGN_KEYCHAIN" \
-P "$OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD" -T /usr/bin/codesign -T /usr/bin/security >/dev/null 2>&1
# The partition list is what lets codesign use the key with no dialog. We can set it here
# only because this keychain's password is ours.
security set-key-partition-list -S apple-tool:,apple:,codesign: -s \
-k "$OMI_LOCAL_SIGN_KEYCHAIN_PASSWORD" "$OMI_LOCAL_SIGN_KEYCHAIN" >/dev/null 2>&1
add_keychain_to_search_list "$OMI_LOCAL_SIGN_KEYCHAIN"
rm -rf "$work"
if signing_identity_usable "$OMI_LOCAL_DEV_SIGN_IDENTITY"; then
substep "Created $OMI_LOCAL_DEV_SIGN_IDENTITY (self-signed, stable, no prompt)"
return 0
fi
return 1
}
# Which identity this bundle was signed with last time.
#
# **TCC binds every grant to the code requirement at the moment it was granted**, and that
# requirement names the signing certificate. So changing a bundle's signing identity silently
# revokes its Microphone, Screen Recording and Files permissions -- the app then re-prompts on
# every launch and looks broken, with nothing in its own logs to explain why.
#
# That is easy to do by accident: an Apple Development identity that the keychain refuses in one
# session (see the errSecInternalComponent notes) falls back to the self-signed identity, and the
# next build silently switches the bundle over. Remembering the last identity makes the switch
# deliberate rather than accidental, and loud when it has to happen anyway.
signing_identity_stamp_path() {
[ "$IS_NAMED_BUNDLE" = true ] || return 1
printf '%s/Library/Application Support/Omi Dev Bundles/%s/.signing-identity\n' "$HOME" "$BUNDLE_ID"
}
remembered_signing_identity() {
local stamp
stamp="$(signing_identity_stamp_path)" || return 1
[ -f "$stamp" ] || return 1
cat "$stamp"
}
remember_signing_identity() {
local stamp
stamp="$(signing_identity_stamp_path)" || return 0
mkdir -p "$(dirname "$stamp")" 2>/dev/null
printf '%s' "$1" > "$stamp" 2>/dev/null || true
}
resolve_signing_identity() {
if [ -n "$SIGN_IDENTITY" ]; then
return
fi
local candidate
# An identity this bundle already wears wins over a "better" one, because switching costs the
# user every permission they have granted it.
local remembered
if remembered="$(remembered_signing_identity)" && [ -n "$remembered" ]; then
if signing_identity_usable "$remembered"; then
SIGN_IDENTITY="$remembered"
substep "Reusing this bundle's existing identity: $SIGN_IDENTITY"
return
fi
echo ""
echo " WARNING: this bundle was last signed with \"$remembered\", which is not usable"
echo " here. Signing it with anything else RESETS ITS PERMISSIONS: macOS binds"
echo " Microphone, Screen Recording and Files grants to the signing certificate,"
echo " so the app will prompt for all of them again on next launch."
echo " The remedy is the same one printed below."
fi
# Prefer a real Apple identity so local permissions stay stable AND the bundle carries a
# Team ID. Each candidate is probed rather than merely found: an Apple Development identity
# whose key the keychain will not release is worse than useless, because picking it makes
# the build fail an hour of work later instead of falling back now.
for candidate in \
"$(security find-identity -v -p codesigning | grep "Apple Development" | head -1 | sed 's/.*"\(.*\)"/\1/')" \
"$(security find-identity -v -p codesigning | grep "Developer ID Application" | head -1 | sed 's/.*"\(.*\)"/\1/')"
do
[ -n "$candidate" ] || continue
if signing_identity_usable "$candidate"; then
SIGN_IDENTITY="$candidate"
return
fi
# Authorization can be granted between one call and the next -- clicking "Allow" on the
# SecurityAgent dialog authorizes a single use, so a refusal is not necessarily a
# standing state. Probe once more before writing the identity off, or a dialog answered
# a moment too late silently costs the build its Team ID.
sleep 1
if signing_identity_usable "$candidate"; then
SIGN_IDENTITY="$candidate"
return
fi
echo ""
echo " WARNING: $candidate is present but the keychain refused its key."
echo " Falling back to a teamless identity, which is a DOWNGRADE:"
echo " the bundle gets no Team ID, so it cannot share Keychain items or"
echo " entitlements with the team-scoped builds (Omi Dev, beta, prod) and"
echo " needs library validation relaxed to launch."
echo " If a keychain dialog is appearing, answer it with \"Always Allow\"."
echo " To stop it recurring, grant codesign a standing partition once:"
echo ""
echo " security set-key-partition-list -S apple-tool:,apple:,codesign: -s \\"
echo " -l \"$candidate\" ~/Library/Keychains/login.keychain-db"
echo ""
echo " Or force it for this build: OMI_SIGN_IDENTITY=\"$candidate\" ./run.sh"
echo ""
done
# A stable self-signed identity keeps this bundle's own TCC grants, because its designated
# requirement pins that certificate. Ad-hoc signing has no such requirement and silently
# drops Screen Recording, so this is preferred over ad-hoc always -- and created on demand
# rather than waiting for someone to run a GUI wizard.
if [ "${OMI_SKIP_LOCAL_SIGN_IDENTITY:-0}" != "1" ] && ensure_local_dev_signing_identity; then
SIGN_IDENTITY="$OMI_LOCAL_DEV_SIGN_IDENTITY"
substep "Using local self-signed identity: $SIGN_IDENTITY"
return
fi
if [ "${OMI_ALLOW_ADHOC_SIGN:-0}" = "1" ] && [ "$IS_NAMED_BUNDLE" = true ]; then
SIGN_IDENTITY="-"
substep "Using ad-hoc signing for named test bundle ($BUNDLE_ID)"
fi
}
# The Team ID codesign will stamp for the resolved identity ("" when the
# identity carries none). Resolved once per identity: it costs a probe
# signature, and sign_app_bundle runs on both the fast and full lanes.
resolve_signing_identity_team_id() {
if [ -n "$SIGN_IDENTITY" ] && [ "$SIGN_IDENTITY_TEAM_ID_RESOLVED_FOR" = "$SIGN_IDENTITY" ]; then
return
fi
SIGN_IDENTITY_TEAM_ID="$("$SCRIPT_DIR/scripts/prepare-local-dev-entitlements.sh" \
--identity-team-id "$SIGN_IDENTITY")"
SIGN_IDENTITY_TEAM_ID_RESOLVED_FOR="$SIGN_IDENTITY"
}
# Which entitlements a local signature must use. Prints the reason the
# generated local entitlements are required, or nothing when the checked-in
# Desktop/Omi.entitlements are correct as they stand.
#
# Pure decision over already-gathered metadata so it can be exercised directly;
# see tests/test-prepare-local-dev-entitlements.sh.
local_entitlements_fallback_reason() {
local signing_mode="$1"
local is_named_bundle="$2"
local profile_present="$3"
local profile_team_id="$4"
local identity_team_id="$5"
if [ "$signing_mode" = "teamless" ]; then
# Hardened-runtime library validation cannot match the bundled
# frameworks without a Team ID, so this bundle needs the generated
# entitlements whether or not it is a named bundle.
printf '%s\n' "signing identity has no Team ID; library validation would block the bundled frameworks"
return 0
fi
if [ "$is_named_bundle" = true ]; then
printf '%s\n' "named bundle IDs are not covered by Omi Dev's provisioning profile"
return 0
fi
if [ "$profile_present" != true ]; then
return 0
fi
if [ -z "$profile_team_id" ]; then
printf '%s\n' "could not extract profile team ID (security cms failed)"
return 0
fi
if [ "$profile_team_id" != "$identity_team_id" ]; then
printf '%s\n' "profile team ($profile_team_id) != identity team ($identity_team_id)"
fi
return 0
}
fast_bundle_fingerprint() {
local desktop_api_fingerprint="${OMI_DESKTOP_API_URL:-}"
local python_api_fingerprint="${OMI_PYTHON_API_URL:-}"
local auth_api_fingerprint="${OMI_AUTH_API_URL:-}"
# The local-profile writer refreshes both endpoint settings plus disposable
# Auth-emulator values inside the installed bundle on every fast patch.
# They are launch configuration, not a packaged-input boundary.
if [ "$LOCAL_PROFILE" = true ]; then
desktop_api_fingerprint="local-profile-refreshed"
python_api_fingerprint="local-profile-refreshed"
fi
omi_fast_bundle_fingerprint \
"$SCRIPT_DIR" \
"bundle-id=$BUNDLE_ID" \
"signing-identity=$SIGN_IDENTITY" \
"local-profile=$LOCAL_PROFILE" \
"yolo=$YOLO_MODE" \
"skip-backend=${OMI_SKIP_BACKEND:-0}" \
"skip-tunnel=${OMI_SKIP_TUNNEL:-0}" \
"desktop-api-url=$desktop_api_fingerprint" \
"python-api-url=$python_api_fingerprint" \
"auth-api-url=$auth_api_fingerprint" \
"jit-qa-target=${OMI_JIT_QA_TARGET:-}" \
"env-stage=${OMI_ENV_STAGE:-}" \
"backend-port=$BACKEND_PORT"
}
fast_bundle_profile_root() {
if [ "$IS_NAMED_BUNDLE" = true ]; then
printf '%s\n' "$HOME/Library/Application Support/Omi Dev Bundles/$BUNDLE_ID"
else
printf '%s\n' "$HOME/Library/Application Support/Omi"
fi
}
reset_local_profile_keychain_state() {
if [ "$LOCAL_PROFILE" = true ]; then
step "Resetting local-profile Keychain state..."
# Local profiles sign into the synthetic Auth emulator on every launch.
# Clear only this installed named bundle's scoped disposable items so an
# earlier ad-hoc build cannot block startup on a stale TrustedApplication
# ACL. The reset helper rejects Prod, Beta, Omi Dev, and identity mismatch.
./scripts/omi-local-profile-keychain-reset.sh "$BUNDLE_ID" "$APP_PATH"
fi
}
# Pool launch policy, as a pure decision so tests/test-omi-e2e-pool.sh can
# drive it directly. An explicitly requested full rebuild of a LEASED E2E pool
# slot is agent habit, not necessity: it is exactly the launch that, from a
# background shell, cannot seed auth and reads as a cold start. Refuse it when
# the fast bundle is still reusable; allow it whenever the fingerprint path
# already decided a full rebuild is required, when the launcher itself forced
# full (rewind reseed: $3 = 0), and for every non-pool named bundle.
omi_pool_refuses_explicit_full() {
# $1 = E2E pool slot number ("" when the named bundle is not a pool slot)
# $2 = underlying fast-bundle eligibility reason
# $3 = 1 when --full / OMI_FORCE_FULL_BUNDLE requested the rebuild
[ -n "$1" ] && [ "$3" = "1" ] && [ "$2" = "reusable" ]
}
fail_fast_only() {
local reason="$1"
printf 'launch_mode=failed fast_reason=%s bundle_id=%s profile_root=%q\n' \
"$reason" "$BUNDLE_ID" "$(fast_bundle_profile_root)" >&2
exit 3
}
# `--fast-only` is an inspection-first agent operation. Read the same selected
# configuration that a normal launch will use, but do it before killing an app,
# clearing a build directory, starting a tunnel, or starting a backend. This
# makes an ineligible fast launch safe to probe repeatedly.
prepare_fast_only_configuration() {
if [ "$LOCAL_PROFILE" = false ] && [ -f "$BACKEND_DIR/.env" ]; then
set -a
# shellcheck disable=SC1090
source "$BACKEND_DIR/.env"
set +a
fi
if [ "$YOLO_MODE" = "1" ] || [ "$NAMED_BUNDLE_DEFAULT_DEV_BACKEND" = true ]; then
apply_yolo_env
fi
omi_prepare_jit_qa_target "$APP_NAME" "$BUNDLE_ID" "$YOLO_MODE" refresh "$LOCAL_PROFILE" || exit $?
}
FAST_BUNDLE_STAMP="$OMI_DEV_DIR/fast-dev-bundles/$BUNDLE_ID.stamp"
if [ "$FAST_ONLY" = "1" ]; then
prepare_fast_only_configuration
resolve_signing_identity
FAST_BUNDLE_FINGERPRINT="$(fast_bundle_fingerprint)"
FAST_BUNDLE_REASON="$(omi_fast_bundle_eligibility_reason "$APP_PATH" "$FAST_BUNDLE_STAMP" "$FAST_BUNDLE_FINGERPRINT")"
if [ "$FAST_BUNDLE_REASON" != "reusable" ]; then
fail_fast_only "$FAST_BUNDLE_REASON"
fi
fi
# Every codesign call in this file goes through here.
#
# When signing fails because the *keychain* refused to hand over the private key, the raw
# message is `errSecInternalComponent` — six syllables of nothing, and the failure looks
# identical to a corrupt bundle or a bad identity. It sent one agent down a rabbit hole that
# ended in ad-hoc signing, which is the one fix that silently breaks Rewind capture.
#
# The actual cause is almost always the session, not the certificate: a process launched
# outside the GUI login session (`launchctl managername` reports `Background` rather than
# `Aqua` — CI, an ssh shell, an agent harness, a LaunchDaemon) has no window server, so
# SecurityAgent cannot draw the "codesign wants to use key X" dialog. Rather than hang, the
# Security framework fails the call immediately. The key is present, the keychain is unlocked,
# and the caller is the right user; only the authorization is missing.
#
# So say that, and say the one command that fixes it for good.
omi_codesign() {
local output status
output="$(codesign "$@" 2>&1)"
status=$?
[ -n "$output" ] && printf '%s\n' "$output"
if [ $status -ne 0 ] && printf '%s' "$output" | grep -qE 'errSecInternalComponent|interaction is not allowed|User interaction'; then
echo ""
echo "ERROR: the keychain refused the signing key (not a bad bundle, not a bad identity)."
echo ""
echo " identity: $SIGN_IDENTITY"
echo " session : $(launchctl managername 2>/dev/null || echo unknown) <- must be Aqua to show a keychain prompt"
echo ""
echo " This shell cannot display the SecurityAgent dialog that would authorize the key,"
echo " so macOS fails the request instead of asking. Grant codesign a standing partition"
echo " on the key once, from a terminal in the GUI session:"
echo ""
echo " security set-key-partition-list -S apple-tool:,apple:,codesign: -s \\"
echo " -l \"$SIGN_IDENTITY\" ~/Library/Keychains/login.keychain-db"
echo ""
echo " Omit -k so it prompts for the keychain password instead of putting it in history."
echo " After that this build works from any session, including this one."
echo ""
echo " DO NOT reach for OMI_ALLOW_ADHOC_SIGN=1 here. An ad-hoc signature has no"
echo " designated requirement, so the bundle loses its own Screen Recording grant and"
echo " Rewind captures nothing — which reads as a broken feature, not a signing problem."
echo " See desktop/macos/docs/local-code-signing.md."
echo ""
exit 1
fi
return $status
}
sign_app_bundle() {
local bundle="$1"
local sign_nested="$2"
local effective_entitlements="Desktop/Omi.entitlements"
local profile_path="$bundle/Contents/embedded.provisionprofile"
local local_signing_mode
local fallback_reason
resolve_signing_identity
if [ -z "$SIGN_IDENTITY" ]; then
echo ""
echo "ERROR: No signing identity found. Ad-hoc signing causes macOS to reset"
echo " Screen Recording permissions for ALL Omi apps (including prod/beta)."
echo ""
echo " Fix: Install an Apple Development certificate in Keychain Access,"
echo " or set OMI_SIGN_IDENTITY to a valid identity:"
echo " OMI_SIGN_IDENTITY=\"Apple Development: you@example.com\" ./run.sh"
echo ""
echo " With no Apple certificate, create the stable self-signed local"
echo " identity once (see desktop/macos/AGENTS.md → Local Code Signing):"
echo " \"$OMI_LOCAL_DEV_SIGN_IDENTITY\" is picked up automatically."
echo ""
echo " For named throwaway bundles only, tests may opt into ad-hoc signing."
echo " It invalidates that bundle's own Screen Recording grant, so Rewind"
echo " captures nothing:"
echo " OMI_APP_NAME=\"omi-my-test\" OMI_ALLOW_ADHOC_SIGN=1 ./run.sh"
echo ""
exit 1
fi
resolve_signing_identity_team_id
# One authority for both the launch-safety gate and the entitlement choice:
# a configuration that validates is exactly the one whose mode is used.
local_signing_mode="$("$SCRIPT_DIR/scripts/prepare-local-dev-entitlements.sh" \
--validate-identity \
"$SIGN_IDENTITY" \
"$SIGN_IDENTITY_TEAM_ID" \
"$IS_NAMED_BUNDLE" \
"${OMI_ALLOW_ADHOC_SIGN:-0}")"
substep "Using identity: $SIGN_IDENTITY (team=${SIGN_IDENTITY_TEAM_ID:-none}, mode=$local_signing_mode)"
if [ "$sign_nested" = true ]; then
if [ -d "$bundle/Contents/Frameworks/Sparkle.framework" ]; then
substep "Signing Sparkle framework"
omi_codesign --force --options runtime --sign "$SIGN_IDENTITY" "$bundle/Contents/Frameworks/Sparkle.framework"
fi
if [ -d "$bundle/Contents/Frameworks/Sentry.framework" ]; then
substep "Signing Sentry framework"
omi_codesign --force --options runtime --sign "$SIGN_IDENTITY" "$bundle/Contents/Frameworks/Sentry.framework"
fi
if [ -d "$bundle/Contents/Frameworks/onnxruntime.framework" ]; then
substep "Signing onnxruntime framework"
omi_codesign --force --options runtime --sign "$SIGN_IDENTITY" "$bundle/Contents/Frameworks/onnxruntime.framework"
fi
if [ -f "$bundle/Contents/Frameworks/libsharpyuv.0.dylib" ]; then
substep "Signing libsharpyuv"
omi_codesign --force --options runtime --sign "$SIGN_IDENTITY" "$bundle/Contents/Frameworks/libsharpyuv.0.dylib"
fi
if [ -f "$bundle/Contents/Frameworks/libwebp.7.dylib" ]; then
substep "Signing libwebp"
omi_codesign --force --options runtime --sign "$SIGN_IDENTITY" "$bundle/Contents/Frameworks/libwebp.7.dylib"
fi
local node_bin="$bundle/Contents/Resources/Omi Computer_Omi Computer.bundle/Contents/Resources/node"
if [ -f "$node_bin" ]; then
substep "Signing bundled node binary"
omi_codesign --force --options runtime --entitlements Desktop/Node.entitlements --sign "$SIGN_IDENTITY" "$node_bin"
fi
fi
# Named bundles deliberately omit Sign in with Apple because their bundle
# IDs are not covered by Omi Dev's provisioning profile, and a teamless
# identity additionally needs library validation relaxed.
local profile_present=false profile_team_id="" profile_plist
if [ -f "$profile_path" ]; then
profile_present=true
profile_plist=$(mktemp /tmp/omi-dev-profile.XXXXXX)
profile_team_id=$(security cms -D -i "$profile_path" > "$profile_plist" 2>/dev/null && \
/usr/libexec/PlistBuddy -c "Print :TeamIdentifier:0" "$profile_plist" 2>/dev/null || true)
rm -f "$profile_plist"
fi
fallback_reason="$(local_entitlements_fallback_reason \
"$local_signing_mode" \
"$IS_NAMED_BUNDLE" \
"$profile_present" \
"$profile_team_id" \
"$SIGN_IDENTITY_TEAM_ID")"
if [ -n "$fallback_reason" ]; then
substep "Local entitlements fallback: $fallback_reason"
effective_entitlements="$("$SCRIPT_DIR/scripts/prepare-local-dev-entitlements.sh" \
Desktop/Omi.entitlements \
"$OMI_DEV_DIR" \
"$BUNDLE_ID" \
"$local_signing_mode")"
rm -f "$profile_path"
fi
substep "Signing app bundle"
omi_codesign --force --options runtime --entitlements "$effective_entitlements" --sign "$SIGN_IDENTITY" "$bundle"
remember_signing_identity "$SIGN_IDENTITY"
}
update_app_desktop_api_url() {
local env_file="$1"
# An explicit environment or .env endpoint is authoritative over a tunnel.
# Tunnels are a local-dev fallback only.
if [ -n "${OMI_DESKTOP_API_URL:-}" ]; then
EFFECTIVE_API_URL="$OMI_DESKTOP_API_URL"
elif [ -n "$TUNNEL_URL" ]; then
EFFECTIVE_API_URL="$TUNNEL_URL"
else
EFFECTIVE_API_URL="http://localhost:$BACKEND_PORT"
fi
if grep -q "^OMI_DESKTOP_API_URL=" "$env_file"; then
sed -i '' "s|^OMI_DESKTOP_API_URL=.*|OMI_DESKTOP_API_URL=$EFFECTIVE_API_URL|" "$env_file"
else
echo "OMI_DESKTOP_API_URL=$EFFECTIVE_API_URL" >> "$env_file"
fi
substep "OMI_DESKTOP_API_URL=$EFFECTIVE_API_URL"
}
rewrite_bundled_dylib_load_path() {
local binary="$1"
local dylib_name="$2"
local bundled_load_path="@rpath/$dylib_name"
local current_load_path
current_load_path="$(otool -L "$binary" | awk -v dylib_name="$dylib_name" '
NR > 1 {
sub(/^[[:space:]]+/, "")
sub(/ \(compatibility version.*$/, "")
if ($0 ~ ("/" dylib_name "$")) {
print
exit
}
}
')"
if [ -z "$current_load_path" ]; then
echo "ERROR: expected $binary to link $dylib_name" >&2
return 1
fi
if [ "$current_load_path" != "$bundled_load_path" ]; then
install_name_tool -change "$current_load_path" "$bundled_load_path" "$binary"
current_load_path="$(otool -L "$binary" | awk -v dylib_name="$dylib_name" '
NR > 1 {
sub(/^[[:space:]]+/, "")
sub(/ \(compatibility version.*$/, "")
if ($0 ~ ("/" dylib_name "$")) {
print
exit
}
}
')"
fi
if [ "$current_load_path" != "$bundled_load_path" ]; then
echo "ERROR: $binary must load $dylib_name from $bundled_load_path, found ${current_load_path:-none}" >&2
return 1
fi
}
step "Killing existing instances..."
# Only kill the dev app — never touch Omi Beta (production)
pkill -f "$APP_NAME.app" 2>/dev/null || true
# Note: don't pkill cloudflared here — other agents may have tunnels running on this machine
# Keep an owned local Python backend alive until a replacement has started. The
# backend startup path refreshes Firebase keys before it binds, so restarting it
# for a Swift-only edit adds network-dependent delay and makes a compiler error
# take down an otherwise healthy development server.
if [ -n "${OMI_HARNESS_INSTANCE:-}" ]; then
substep "Keeping harness desktop-backend (OMI_HARNESS_INSTANCE=${OMI_HARNESS_INSTANCE})"
elif omi_python_desktop_backend_pid_is_alive "$BACKEND_PIDFILE"; then
OLD_BACKEND_PID="$(omi_python_desktop_backend_read_pid "$BACKEND_PIDFILE")"
substep "Deferring recorded backend verification until a candidate is ready (PID: $OLD_BACKEND_PID, port $BACKEND_PORT)"
else
if [ -f "$BACKEND_PIDFILE" ] || [ -f "$BACKEND_METADATA" ]; then
substep "Removing stale owned backend metadata"
rm -f "$BACKEND_PIDFILE" "$BACKEND_METADATA"
fi
fi
sleep 0.5 # Let cfprefsd flush after process death