Repository navigation
Expand file tree
/
Copy pathcodedeck
More file actions
executable file
·1714 lines (1591 loc) · 73.3 KB
/
Copy pathcodedeck
File metadata and controls
executable file
·1714 lines (1591 loc) · 73.3 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
#!/usr/bin/env bash
# codedeck — one entrypoint for managing this fork: the bridge container,
# the optional Tor daemon, Android APK builds, and workspace checks.
#
# Everything here just wraps commands already established elsewhere in the
# repo (docker compose, apps/android/docker/build-apk.sh,
# apps/android/scripts/build-apk-local.sh, cargo, pnpm) — this script adds no
# new mechanism, only a short memorable surface over them. Docker is the
# default engine; on a Linux host with no docker binary at all, the commands
# that have a local form fall back to the vendored toolchain
# (scripts/install-toolchain.sh, installed on first use).
set -euo pipefail
# Everything below is one brace group, closed on the last line. Bash reads a
# script as it executes it, so a file that changes underneath a command that
# takes minutes — a branch switched, this file edited — is carried on from the
# wrong offset and runs whatever it finds there. A group is parsed whole
# before any of it runs, and the `exit` that ends it keeps bash from reading
# on afterwards.
{
cd "$(git rev-parse --show-toplevel)"
# Absolute, because some commands change directory (the Android ones work in
# apps/android) and this script's own files are still theirs to reach.
REPO_ROOT="$PWD"
# The vendored toolchain's directory: the main checkout's, for every checkout
# of this repository (scripts/lib/toolchain-dir.sh says why).
# shellcheck source=scripts/lib/toolchain-dir.sh
. scripts/lib/toolchain-dir.sh
TOOLCHAIN="$(toolchain_dir)"
# Git Bash (MSYS) on Windows rewrites leading-/ arguments as if they were
# Windows paths before docker.exe sees them — breaks `docker exec -w /path`.
# Scoped (not exported globally): doing that also breaks docker cp's HOST-side
# path resolution. No-op on real Linux/macOS. Same fix as build-apk.sh.
dexec() { MSYS_NO_PATHCONV=1 docker exec "$@"; }
# --- Engine selection: Docker when its daemon answers; else (Linux only,
# and only when no docker binary exists at all) the vendored toolchain.
#
# A binary whose daemon is merely DOWN is not "no Docker": that machine chose
# Docker, so it gets "start your daemon" — never a surprise local run (or a
# multi-GB install). bridge and tor wrap docker compose and have no local
# form regardless.
# One probe, three states: 0 = Docker usable; 1 = no docker binary at all;
# 2 = binary present but its daemon does not answer. Every command site acts
# on this single result — with the daemon down, `docker info` itself can hang
# for seconds, so it must never run twice for one command.
docker_state() {
command -v docker >/dev/null 2>&1 || return 1
docker info >/dev/null 2>&1 || return 2
}
fail_docker_down() {
echo "Docker is installed but its daemon is unreachable — start it and re-run." >&2
exit 1
}
# Commands with no local mode (bridge, tor) call this first.
require_docker() {
local state=0
docker_state || state=$?
case $state in
0) return 0 ;;
2) fail_docker_down ;;
esac
echo "Docker is not installed, and this command is only a docker compose" >&2
echo "wrapper — it has no local form. Install Docker and re-run (Docker" >&2
echo "Desktop on Windows/macOS), or on Linux run the bridge from source:" >&2
echo "docs/BRIDGE.md, \"Build from source\"." >&2
exit 1
}
# Whether this host can run a command that needs these components (node,
# rust, java, android, ndk; none named = all of them) without Docker. Linux:
# yes, scripts/install-toolchain.sh vendors every one. Windows (Git Bash):
# Node — the agent host, as CI tests it on Windows; Rust, as the GNU toolchain
# with a GCC unpacked beside it; and the JDK and Android SDK the app's tests,
# snapshots and lint run on. The Rust one is not what CI builds with on
# Windows (MSVC, a system install this script will not make), and a Windows
# build never compiles the `cfg(unix)` code: it is the loop's engine, and the
# Linux and MSVC runs in CI stay the judge. `ndk` is the one thing Windows
# lacks: an APK links through the NDK's Linux toolchain, so `apk` is Docker's
# there. (It is a need, not a component of its own: the installer brings the
# NDK with `android` where it installs one.)
host_local_supports() {
local component
case "$(uname -s)" in
Linux) return 0 ;;
MINGW* | MSYS*)
[ $# -gt 0 ] || return 1
for component in "$@"; do
case "$component" in
node | rust | java | android) ;;
*) return 1 ;;
esac
done
return 0
;;
*) return 1 ;;
esac
}
# Which engine runs a command that needs these components — docker_state's
# three answers (0 Docker, 1 local, 2 Docker's daemon is down), after asking
# what the caller prefers. CODEDECK_ENGINE=local runs on the local toolchain
# whatever this host has one for, with Docker still running: a container is a
# VM's worth of memory and a tree to sync on Windows and macOS, and the agent
# host needs neither. What the host cannot run locally still goes to Docker,
# and says so. Unset (or `docker`), Docker runs everything when it answers.
engine_state() {
case "${CODEDECK_ENGINE:-docker}" in
docker) ;;
local)
if host_local_supports "$@"; then return 1; fi
echo "==> CODEDECK_ENGINE=local, but this host has no local toolchain for: $* — using Docker" >&2
;;
*)
echo "CODEDECK_ENGINE must be 'local' or 'docker', not '$CODEDECK_ENGINE'" >&2
exit 2
;;
esac
docker_state
}
# A path as a native program reads it. Git Bash shows the tree as /c/…, which
# only programs built for it understand; node.exe wants C:/…. Elsewhere a path
# is already what it is.
native_path() {
if command -v cygpath >/dev/null 2>&1; then cygpath -m "$1"; else printf '%s' "$1"; fi
}
# The local engine: the vendored toolchain from scripts/install-toolchain.sh.
# Takes the components the command being run actually needs — `node` for the
# TypeScript workspace, `rust` for the crates, `android` for apps/android — so
# a host that runs one part of the repo never fetches the others (the Android
# SDK and NDK are a few GB on their own). No argument means the whole
# toolchain.
#
# The installer is run every time rather than only when its target is absent:
# it is idempotent, and it checks the directory itself, while any record of
# what was installed can drift from what is there (a deleted NDK, a half-run
# that landed cmdline-tools and nothing else). Every step of it skips without
# touching the network when its target is present, so the check costs a shell
# startup — and says nothing unless it installs something. Sources env.sh into THIS shell so cargo/node/pnpm/java stay on PATH
# for the rest of the script. What a host can run locally is
# host_local_supports' to say: all of it on Linux, all but an APK on Windows.
ensure_toolchain() {
if ! host_local_supports "$@"; then
echo "Docker is unavailable, and this host has no local toolchain for" >&2
echo "this command (scripts/install-toolchain.sh: everything on Linux," >&2
echo "everything but the APK build on Windows). Install and start Docker" >&2
echo "(Docker Desktop on Windows/macOS), then re-run it. See CLAUDE.md," >&2
echo "\"Commands\"." >&2
exit 1
fi
# `ndk` says what a command needs; the installer has no component by that
# name (see host_local_supports).
local component components=()
for component in "$@"; do
[ "$component" = ndk ] || components+=("$component")
done
set -- ${components[@]+"${components[@]}"}
if [ ! -f "$TOOLCHAIN/env.sh" ]; then
echo "==> No toolchain at $TOOLCHAIN yet — installing what this needs"
[ $# -gt 0 ] && echo " (${*})"
echo " one-time; Ctrl-C to abort"
fi
INSTALL_TOOLCHAIN_QUIET=1 bash scripts/install-toolchain.sh "$@"
# shellcheck source=/dev/null
. "$TOOLCHAIN/env.sh"
apply_local_limits
share_cargo_target
}
# A linked worktree builds into the main checkout's target directory. Cargo
# keys what it builds by package, features, profile and — for the workspace's
# own crates — the path they were built from, so two checkouts share every
# dependency (most of a build, and gigabytes on disk) and never each other's
# crates. The one thing shared is the build lock: two builds at once queue. A
# CARGO_TARGET_DIR the caller set is left alone.
share_cargo_target() {
if [ -z "${CARGO_TARGET_DIR:-}" ] && [ -n "${DEV_WORKTREE:-}" ]; then
CARGO_TARGET_DIR="$(native_path "$(main_checkout)/target")"
export CARGO_TARGET_DIR
fi
}
# CODEDECK_CPUS on the local engine. There is no container to put in a cpuset
# here, so the number goes to each tool that would otherwise start one worker
# per core of the machine: cargo's build jobs (its jobserver bounds rustc's
# own threads too), the Rust test harness and vitest's pool. A value the
# caller already set for one of them is left alone. CODEDECK_MEMORY has no
# local form: nothing here can bound a process tree's memory without a
# container.
apply_local_limits() {
[ -n "${CODEDECK_CPUS:-}" ] || return 0
export CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-$CODEDECK_CPUS}"
export RUST_TEST_THREADS="${RUST_TEST_THREADS:-$CODEDECK_CPUS}"
export VITEST_MAX_THREADS="${VITEST_MAX_THREADS:-$CODEDECK_CPUS}"
export VITEST_MAX_FORKS="${VITEST_MAX_FORKS:-$CODEDECK_CPUS}"
}
usage() {
cat <<'EOF'
codedeck — manage this CodeDeck fork
Usage: ./codedeck <command> [args]
First time here, or something looks wrong:
doctor What exists on this machine right now: Docker, the
images and volumes, the containers below, the agent
cache, the Android SDK. Read-only; start here.
Bridge (the deployed Docker service):
bridge up Build (if needed) and start the bridge
bridge down Stop the bridge (and codedeck-tor, if running)
bridge restart Rebuild the image and restart the bridge
bridge build Rebuild the bridge image only
bridge logs Follow the bridge's logs (Ctrl-C to stop)
bridge status Show what's running
bridge pair [--json] Open a pairing window on the running bridge, show its QR
bridge shell Open a shell inside the running bridge container
Tor (optional codedeck-tor service, Compose `tor` profile):
tor up Start codedeck-tor (also (re)starts the bridge,
since Compose profiles apply per `up` invocation)
tor down Stop codedeck-tor
Android APK (Docker: apps/android/docker/build-apk.sh; Docker-less Linux:
apps/android/scripts/build-apk-local.sh — see those files for details):
apk [--target <triple>] Debug APK of the native app into dist/ (default:
arm64 device + x86_64 emulator)
Workspace checks (throwaway Node and Rust containers — nothing installed
locally is required; without Docker, or with CODEDECK_ENGINE=local, these run
on the vendored toolchain instead, installed on first use):
check The bar for a change: the agent host's typecheck,
tests and build; clippy, tests and docs for every
crate, and the host's generated types. Every step
runs and is listed at the end, with its duration
typecheck typecheck only (agent host + cargo clippy)
test test only
--changed Only the parts that differ from master, by the
path rules CI uses: the agent host, the crates,
and the app (its bindings, tests, snapshots and
lint — which only this option runs)
--verbose | --quiet Stream every step's output, or one line per step
and the tail of a failure (full output in
dist/logs/). Default: stream on a terminal only
e2e The two tests that drive a built agent host from
Rust (driver protocol, bridge end to end), which
`check` leaves out and CI runs
Generated files — produced where the toolchain is and written back to this
tree; CI fails when one is stale:
regen cargo-lock Cargo.lock, after any Cargo.toml change
regen ts The agent host's protocol types, after a change to
crates/agent-protocol
regen pins pnpm-lock.yaml and the agents' install pins, after
a change to a package.json
regen bindings The UniFFI Kotlin bindings (gen-android-bindings)
regen all cargo-lock, ts and bindings
Pull request:
ci [PR] [--watch] [--lines N]
The checks of this branch's pull request (or of
PR, a number or a branch), and the end of the log
of each one that failed (needs the GitHub CLI)
Engine — Docker runs everything when its daemon answers; without Docker the
local toolchain does (scripts/install-toolchain.sh, into ./toolchain):
CODEDECK_ENGINE=local Prefer the local toolchain even with Docker up, for
what this host has one for: everything on Linux;
on Windows everything but the APK build (check,
e2e, dev, live, regen, android) — the crates on
the GNU toolchain, where CI's Windows job uses
MSVC. `apk` still runs in Docker, and says so
CODEDECK_ENGINE=docker For one command: run it in the Linux container,
which is what CI's bar is
Limits, for a machine that is also being used (any command above or below
that runs a container; unset = no limit):
CODEDECK_CPUS=8 Cores a container may use — builds and test runners
size themselves to it. On the local engine: the
jobs and workers cargo, its tests and vitest start
CODEDECK_MEMORY=4g Memory a container may use, page cache included
(containers only). Measured with 10 cores: the
crates' tests build and link in 2g and fail in
1.5g, so 4g leaves room. The Android container
has 6g of its own, whatever this says: Gradle's
daemon is killed at 3g and takes twice as long
at 4g
The dev container — one long-lived container with node_modules and the caches
kept warm, for the loop between checks (editing a driver, running one test
file, driving a live agent, poking at a crate). The tree is synced into it on
every command; on a Docker-less Linux host the command runs in this tree. A
git worktree gets a container of its own, codedeck-dev-<name>, so checkouts
do not take turns (the caches stay shared); `live` runs in it too:
dev [--] <cmd…> Run <cmd> in it, at the repo root (no <cmd>: a shell)
dev vitest [args…] The agent host's test files, run once
dev --sync-only Sync the tree and install dependencies, then stop
dev --no-sync Skip the sync (the tree has not changed)
dev --reset Recreate the container from scratch first
dev --timeout SECONDS Stop <cmd> after this long (default 1800, or
CODEDECK_TIMEOUT; 0 = never). Only a command with
no terminal is stopped: a shell is someone typing
clean [--yes] Remove the containers this script keeps (the dev
and Android ones, and any a killed run left), after
asking. Volumes are kept — the caches live there
One live agent session, with everything the host reports about it (this is
what the phone is shown: entries, turn states, context figures, errors). An
agent that is not installed is fetched once, at the version this build pins,
and kept — in the agents volume on the Docker path, in ./toolchain/agents on
a Docker-less host:
live <agent> One turn, "Reply with exactly: codedeck-live-ok"
live <agent> --prompt … One turn per --prompt, in order
live <agent> --models What the agent offers, and what a session with no
model would run — starts nothing, costs nothing
live <agent> [options] --mode ask|plan|default, --model ID, --effort ID,
--persona ID, --cwd DIR, --env K=V (agent paths and keys),
--timeout SECONDS (default 300), --no-sync
agents: claude-code, opencode, deepseek-harness,
qwen-code, pi, and `fake` for the driver-protocol
test agent
live <agent> --fake-model
Point the agent at a local model server instead
(tools/fake-model.mjs): no account, no network,
and the same replies every run. Each prompt is a
scenario — /tool (a tool call; a permission card
on OpenCode), /background (Qwen Code's background
shell), /question, /plan-approval, /error,
/long (a long answer, and a context figure that
has moved) — and anything else is echoed.
--dump DIR writes every request the agent made,
for reading what a driver actually sends (on the
Docker engine it is copied back to
dist/live-<agent>-dump/ afterwards)
Android tests and lint (same idea, in the Android container, or in this tree
on the local engine — a subcommand maps to its Gradle task; anything else is
passed to Gradle as it is):
android [test] app:testDebugUnitTest, then the counts and failures
android lint app:lintDebug, then what the report says
android paparazzi app:verifyPaparazziDebug (the snapshot tests)
android snapshot app:recordPaparazziDebug, goldens copied back
android shell A shell in apps/android
android <task> [args…] Any other Gradle task
--verbose | --quiet Stream Gradle's output, or keep it in
dist/logs/android.log and print the report (and
the end of the log on a failure). Default: stream
on a terminal only
Bindings (throwaway Rust container; on the local engine, in this tree):
gen-android-bindings Regenerate apps/android/app/src/main/java/uniffi/**
from crates/client-ffi via uniffi-bindgen
(source of truth — see that crate's own doc
comment). CI fails if this drifts.
help Show this
EOF
}
require_running_bridge() {
if ! docker ps --filter "name=codedeck-bridge" --filter "status=running" --format '{{.Names}}' | grep -q codedeck-bridge; then
echo "codedeck-bridge is not running — try: ./codedeck bridge up" >&2
exit 1
fi
}
cmd_bridge() {
require_docker
case "${1:-}" in
up)
docker compose up -d --build codedeck-bridge
;;
down)
docker compose down
;;
restart)
docker compose build codedeck-bridge
docker compose up -d codedeck-bridge
;;
build)
docker compose build codedeck-bridge
;;
logs)
docker compose logs -f codedeck-bridge
;;
status)
docker compose ps
;;
pair)
require_running_bridge
shift
# `codedeck-bridge pair` shows a running bridge's open window, or asks
# it for one (docs/BRIDGE.md, Commands), so this runs that inside the
# container: a QR on demand, whether or not a phone is paired already.
# A terminal is attached only when there is one, so `--json` can be
# read by a program.
# Only once the bridge holds its lock: while the entrypoint is still
# setting up (cloning repositories can take minutes), `pair` would find
# no bridge, run one itself, and the real one would then fail to start.
if ! dexec codedeck-bridge /app/codedeck-bridge status 2>/dev/null | grep -q 'bridge: *running'; then
echo "The bridge in the container is still starting — try again in a moment (./codedeck bridge logs)." >&2
exit 1
fi
local tty=()
if [ -t 0 ] && [ -t 1 ]; then tty=(-t); fi
dexec -i ${tty[@]+"${tty[@]}"} codedeck-bridge /app/codedeck-bridge pair "$@"
;;
shell)
require_running_bridge
dexec -it codedeck-bridge bash
;;
*)
echo "usage: $0 bridge {up|down|restart|build|logs|status|pair|shell}" >&2
exit 1
;;
esac
}
cmd_tor() {
require_docker
case "${1:-}" in
up)
docker compose --profile tor up -d --build
echo
echo "codedeck-tor is up. Point the bridge at it with, in .env:"
echo " CODEDECK_TOR_PROXY_URL=socks5h://codedeck-tor:9050"
echo "then: ./codedeck bridge restart"
;;
down)
docker compose stop codedeck-tor
docker compose rm -f codedeck-tor
;;
*)
echo "usage: $0 tor {up|down}" >&2
exit 1
;;
esac
}
cmd_apk() {
# args pass through, e.g. `./codedeck apk --target aarch64-linux-android`
local state=0
engine_state rust java android ndk || state=$?
case $state in
0) exec ./apps/android/docker/build-apk.sh "$@" ;;
2) fail_docker_down ;;
esac
ensure_toolchain rust java android ndk
# Sources toolchain/env.sh itself when present.
exec ./apps/android/scripts/build-apk-local.sh "$@"
}
# shellcheck source=scripts/lib/pack-repo.sh
. scripts/lib/pack-repo.sh
# --- How much of the machine a container may take. ---
#
# A build takes every core it is shown, and on Docker Desktop the VM behind it
# keeps whatever memory its page cache grew into — fine for a machine that is
# only building, not for one somebody is also working on. Two optional
# variables bound every container this script runs (the checks, the dev and
# Android containers; the deployed bridge is Compose's, not touched here):
#
# CODEDECK_CPUS a number of cores. Applied as a cpuset rather than a CPU
# quota: a quota only throttles, and cargo, vitest and
# Gradle would still start one worker per core of the
# machine and fight over the slice. Inside a cpuset the
# core count they read is the limit, so each sizes itself.
# CODEDECK_MEMORY docker's own form (4g, 6g). Page cache is charged to the
# container too, so this also bounds what the VM holds on to.
#
# Unset means no limit of this script's making. A long-lived container picks a
# changed value up on the next command, and a limit that is no longer asked
# for is lifted then too (see ensure_container).
CPU_LIMIT=()
case "${CODEDECK_CPUS:-}" in
'') ;;
*[!0-9]* | 0)
echo "CODEDECK_CPUS must be a number of cores (1 or more), not '$CODEDECK_CPUS'" >&2
exit 2
;;
*) CPU_LIMIT=(--cpuset-cpus "0-$((CODEDECK_CPUS - 1))") ;;
esac
RESOURCE_LIMITS=(${CPU_LIMIT[@]+"${CPU_LIMIT[@]}"})
if [ -n "${CODEDECK_MEMORY:-}" ]; then
# Swap equal to memory: without it Docker allows as much swap again, and a
# build that swaps is the slow machine this limit is meant to prevent.
RESOURCE_LIMITS+=(--memory "$CODEDECK_MEMORY" --memory-swap "$CODEDECK_MEMORY")
fi
# Start a throwaway container that is removed when the script exits, whether
# it succeeds, fails or is interrupted. Extra args go to `docker create`
# (volumes, limits); MSYS path conversion is off so `-v vol:/path` survives.
start_throwaway() {
local name="$1" image="$2"
shift 2
docker rm -f "$name" >/dev/null 2>&1 || true
MSYS_NO_PATHCONV=1 docker create --name "$name" ${RESOURCE_LIMITS[@]+"${RESOURCE_LIMITS[@]}"} \
"$@" "$image" sleep infinity >/dev/null
docker start "$name" >/dev/null
# Double-quoted so $name is baked in NOW, as a literal — the trap fires
# after the calling function has returned (its locals are gone by then), so
# a single-quoted trap referencing $name would fail under `set -u`.
# shellcheck disable=SC2064
trap "docker rm -f '$name' >/dev/null 2>&1 || true" EXIT
}
# The images this script builds for its own containers, one file each, in
# scripts/docker — beside the scripts that use them rather than at the repo
# root, and needing nothing from the repo as build context. Docker's layer
# cache makes every run after the first a no-op, so the per-image install cost
# is paid once per machine.
TOOL_IMAGE_DIR=scripts/docker
ensure_tool_image() {
local tag="$1" file="$2"
shift 2
echo "==> Preparing $tag image (cached after the first run)"
docker build -q -t "$tag" -f "$file" "$TOOL_IMAGE_DIR" "$@" >/dev/null
}
# --- Plumbing for the long-lived containers (dev, android). ---
# Exact-name lookups throughout: Docker's `name=` filter is a regular
# expression matched against every container's name, so `codedeck-dev` would
# also answer for `codedeck-dev-2`. The list is matched in the shell and not
# piped into `grep -q`: under `set -o pipefail`, a match that makes grep exit
# early can leave the writer killed by SIGPIPE, and the pipeline then reports
# failure for a name that was found.
container_named() {
local names
names="$(docker ps ${1:+"$1"} --format '{{.Names}}')"
case $'\n'"$names"$'\n' in
*$'\n'"$2"$'\n'*) return 0 ;;
*) return 1 ;;
esac
}
container_exists() { container_named "-a" "$1"; }
container_running() { container_named "" "$1"; }
# A container this script created carries the label, and runs the image it was
# created from. One without either was made by hand — or by an older workflow
# that kept its own sync scripts — and is not trusted to have what the
# commands below expect, so it is replaced rather than used and puzzled over;
# so is one whose image has moved on, which is what makes an edit to a
# Dockerfile here take effect on the next command instead of on the next
# `dev --reset`.
container_is_ours() {
[ "$(docker inspect -f "{{index .Config.Labels \"$2\"}}" "$1" 2>/dev/null)" = "1" ] &&
[ "$(docker inspect -f '{{.Config.Image}}' "$1" 2>/dev/null)" = "$3" ]
}
ensure_container() {
local name="$1" label="$2" image="$3"
shift 3
if container_exists "$name" && ! container_is_ours "$name" "$label" "$image"; then
echo "==> Replacing $name (not this script's, or its image changed)"
docker rm -f "$name" >/dev/null
fi
# A container created with a memory limit of its own (the Android one:
# Gradle is sized to it) keeps that one — only its cores are bounded here.
local own_memory=0
case " $* " in
*" -m "* | *" --memory "*) own_memory=1 ;;
esac
# A memory limit an earlier command set and this one does not ask for.
# Docker cannot take a memory limit off a running container (an update to
# 0 is "no change", and one to the engine's whole memory is refused), so
# the container is replaced: it is disposable, and its caches are volumes.
if [ "$own_memory" = 0 ] && [ -z "${CODEDECK_MEMORY:-}" ] && container_exists "$name" &&
[ "$(docker inspect -f '{{.HostConfig.Memory}}' "$name" 2>/dev/null)" != 0 ]; then
echo "==> Replacing $name (it was limited by a CODEDECK_MEMORY that is no longer set)"
docker rm -f "$name" >/dev/null
fi
if ! container_exists "$name"; then
echo "==> Creating $name (kept between commands)"
MSYS_NO_PATHCONV=1 docker create --name "$name" --label "$label=1" "$@" "$image" sleep infinity >/dev/null
fi
# Already running is fine: this is "make sure it is up", not "start it".
docker start "$name" >/dev/null 2>&1 || true
# On every command, not only at creation: the container outlives the shell
# that made it, and the limit in force should be the one asked for now.
local limits=(${RESOURCE_LIMITS[@]+"${RESOURCE_LIMITS[@]}"})
if [ "$own_memory" = 1 ]; then limits=(${CPU_LIMIT[@]+"${CPU_LIMIT[@]}"}); fi
# A cpuset an earlier command set and this one does not ask for is widened
# back to every core (an empty cpuset is "no change" to docker update).
if [ ${#CPU_LIMIT[@]} -eq 0 ]; then
local cpuset every_core
cpuset="$(docker inspect -f '{{.HostConfig.CpusetCpus}}' "$name" 2>/dev/null)"
if [ -n "$cpuset" ]; then
every_core="0-$(($(docker info -f '{{.NCPU}}') - 1))"
if [ "$cpuset" != "$every_core" ]; then limits+=(--cpuset-cpus "$every_core"); fi
fi
fi
if [ ${#limits[@]} -gt 0 ]; then
docker update "${limits[@]}" "$name" >/dev/null ||
echo "==> Could not apply CODEDECK_CPUS/CODEDECK_MEMORY to $name (it keeps its previous limits)" >&2
fi
}
# Mirror the working tree into <container>:<dest>. The file list comes from git
# (pack_repo_into: tracked plus untracked-not-ignored, minus tracked files
# deleted in the tree), staged inside the container and copied over, then
# whatever the tree no longer has is removed — so an edit, a new file, a rename
# and a deletion each land with one command, and a file the tree no longer has
# cannot linger in the container and keep compiling. node_modules, dist, build
# and target are never pruned: they belong to the container, and rebuilding
# them per command is exactly what a long-lived container exists to avoid.
#
# It is all shell and coreutils, with no tool the container has to have: the
# images this runs in are the repo's own, and one of them carries no rsync.
# (A file name with a newline in it would confuse the prune; the repo has
# none, and the cost would be a stale file, never a lost one.)
sync_tree() {
local container="$1" dest="$2"
echo "==> Syncing the working tree into $container"
dexec "$container" sh -c 'rm -rf /tmp/codedeck-tree && mkdir -p /tmp/codedeck-tree'
pack_repo_into "$container" /tmp/codedeck-tree
# The script is the container's shell's to expand, not this one's.
# shellcheck disable=SC2016
dexec "$container" sh -c '
set -e
dest="$1"
mkdir -p "$dest"
cd /tmp/codedeck-tree
find . -type f | sed "s|^\./||" | LC_ALL=C sort > /tmp/codedeck-want
cp -a . "$dest"/
cd "$dest"
find . \( -name node_modules -o -name dist -o -name build -o -name target \) -prune -o -type f -print \
| sed "s|^\./||" | LC_ALL=C sort > /tmp/codedeck-have
comm -13 /tmp/codedeck-want /tmp/codedeck-have | while IFS= read -r gone; do rm -f -- "$gone"; done
rm -rf /tmp/codedeck-tree /tmp/codedeck-want /tmp/codedeck-have
' sh "$dest"
}
# shellcheck source=scripts/lib/checks.sh
. scripts/lib/checks.sh
# --- The dev container: what the throwaway checks are too slow for. ---
#
# The checks (scripts/lib/checks.sh) answer "is this green?" in a container thrown away with the
# answer. Everything between — editing a driver, running one test file, driving
# a live agent, poking at a crate — wants the opposite: one container that
# keeps its node_modules and its store, with the working tree kept in step
# with the host. `./codedeck dev` is that container, and it is the difference
# between a two-second test run and a two-minute one. There is one per
# checkout: a git worktree gets its own (see linked_worktree_id).
DEV_IMAGE=codedeck-workspace
DEV_CONTAINER=codedeck-dev
DEV_ROOT=/repo
# A linked git worktree gets a dev container of its own, named after it, so
# two checkouts can loop at once: one shared container would hold whichever
# tree synced last, and a command from the other would run against the wrong
# code. The volumes stay shared, the cargo target included (tens of GB, too
# much to keep per worktree): dependencies are fingerprinted by package,
# features and profile, so trees on one Cargo.lock reuse each other's builds;
# only a workspace crate that differs is rebuilt after the other tree built,
# and cargo's lock makes two builds at once queue rather than collide. Git
# keeps each worktree's admin directory name (.git/worktrees/<name>) unique
# within the repository, which makes it the id; the main checkout keeps the
# plain name.
linked_worktree_id() {
local git_dir common
git_dir="$(cd "$(git rev-parse --git-dir)" && pwd -P)"
common="$(cd "$(git rev-parse --git-common-dir)" && pwd -P)"
# Called as a condition, so errexit is off in here: a git or cd that
# failed must not make an empty id (and a container named "codedeck-dev-").
[ -n "$git_dir" ] && [ -n "$common" ] && [ "$git_dir" != "$common" ] || return 1
# Docker names take letters, digits, '_', '.' and '-' only.
printf '%s' "$(basename "$git_dir")" | tr -c 'A-Za-z0-9_.-' '-'
}
if DEV_WORKTREE="$(linked_worktree_id)"; then
DEV_CONTAINER="codedeck-dev-$DEV_WORKTREE"
fi
# Whether the container's install is current. The stamp lives outside the
# synced tree, which the mirror would otherwise delete.
DEV_LOCK_STAMP=/root/.codedeck-dev-lock
# The whole repository's tooling in one container (scripts/docker/
# workspace.Dockerfile), with the two stores the checks fill mounted into it
# as well: the pnpm store, and cargo's registry and target directory — the
# same target `./codedeck check` compiles into, so a crate built either way is
# not built again by the other.
ensure_dev_image() {
local pnpm_version
pnpm_version="$(sed -n 's/.*"packageManager": *"pnpm@\([^"]*\)".*/\1/p' package.json)"
ensure_tool_image "$DEV_IMAGE" "$TOOL_IMAGE_DIR/workspace.Dockerfile" --build-arg "PNPM_VERSION=$pnpm_version"
}
ensure_dev_container() {
ensure_dev_image
ensure_container "$DEV_CONTAINER" codedeck.dev "$DEV_IMAGE" \
-v codedeck-pnpm-store:/pnpm-store \
-v codedeck-agents:/agents \
-v codedeck-cargo-registry:/usr/local/cargo/registry \
-v codedeck-check-target:"$DEV_ROOT/target"
}
ensure_dev_deps() {
local want have
want="$(git hash-object pnpm-lock.yaml)"
have="$(dexec "$DEV_CONTAINER" cat "$DEV_LOCK_STAMP" 2>/dev/null || true)"
[ "$want" = "$have" ] && return 0
echo "==> Installing the workspace's Node dependencies"
# Its output is a page of progress and a notice about build scripts: shown
# for a failure, which is the only time anyone reads it.
local output
if ! output="$(dexec -w "$DEV_ROOT" "$DEV_CONTAINER" pnpm install --frozen-lockfile 2>&1)"; then
printf '%s\n' "$output" >&2
return 1
fi
dexec "$DEV_CONTAINER" sh -c "printf '%s' '$want' > $DEV_LOCK_STAMP"
}
# The same "install when the lockfile moved" rule for the local engine, where
# the tree is this one: the stamp goes in node_modules, which it describes —
# gitignored, and each checkout's own (the toolchain directory is shared).
# The packages themselves are hard links into pnpm's store, so a second
# checkout's install downloads nothing and costs little disk.
ensure_local_deps() {
local want have output
want="$(git hash-object pnpm-lock.yaml)"
have="$(cat node_modules/.codedeck-lock 2>/dev/null || true)"
[ "$want" = "$have" ] && return 0
echo "==> Installing the workspace's Node dependencies"
# NODE_ENV=production would make pnpm skip devDependencies — which is where
# everything this runs (typescript, vitest) lives.
# CI=true and no stdin: pnpm asks before it replaces a node_modules it did
# not make (another store, another version), and a prompt nobody is there
# to answer holds the command for ever.
# Its output is a page of progress and a notice about build scripts: shown
# for a failure, which is the only time anyone reads it.
if ! output="$(unset NODE_ENV; CI=true pnpm install --frozen-lockfile </dev/null 2>&1)"; then
printf '%s\n' "$output" >&2
return 1
fi
mkdir -p node_modules
printf '%s' "$want" > node_modules/.codedeck-lock
}
# One command in the dev container, with the tree in step first.
# How long a `dev` command with no terminal may run, in seconds (0: no
# limit). Such a command is a script or an agent, and nothing would ever stop
# one that hangs — a watch left open, a session waiting on an answer that
# never comes; closing the `docker exec` client does not stop it either, so
# the limit is applied inside, where the process lives.
DEV_TIMEOUT_DEFAULT=1800
# `timeout` around a command, unless the limit is 0. `--kill-after` follows
# up a command that ignores the TERM.
with_timeout() {
local limit="$1"; shift
if [ "$limit" = 0 ]; then "$@"; else timeout --kill-after=15s "${limit}s" "$@"; fi
}
# What a command stopped by `with_timeout` means, said once, after it.
report_timeout() {
local code="$1" limit="$2"
if [ "$code" = 124 ] || [ "$code" = 137 ]; then
[ "$limit" != 0 ] && echo "==> Stopped after ${limit}s (dev --timeout SECONDS, or CODEDECK_TIMEOUT=0 for no limit)" >&2
fi
return "$code"
}
cmd_dev() {
local reset=0 sync=1 only_sync=0 limit="${CODEDECK_TIMEOUT:-$DEV_TIMEOUT_DEFAULT}"
while [ $# -gt 0 ]; do
case "$1" in
--reset) reset=1; shift ;;
--timeout)
case "${2:-}" in
''|*[!0-9]*) echo "dev --timeout needs a number of seconds" >&2; exit 2 ;;
esac
limit="$2"; shift 2 ;;
--no-sync) sync=0; shift ;;
--sync-only) only_sync=1; shift ;;
--) shift; break ;;
-*) echo "unknown option for dev: $1" >&2; exit 2 ;;
*) break ;;
esac
done
case "$limit" in
''|*[!0-9]*) echo "CODEDECK_TIMEOUT must be a number of seconds (0 = no limit), not '$limit'" >&2; exit 2 ;;
esac
# What the command is, and where it runs — relative to the repo root, which
# both engines then translate. `dev vitest …` is the loop's most-run
# command: it belongs where vitest expects to be (the agent host's package),
# and without an explicit run/watch word it runs once, rather than opening a
# watch UI no session can answer.
# What the command needs, for the engine: the Node tools run wherever the
# agent host's toolchain is; anything else (cargo, a shell, a script that
# may call either) is assumed to want the whole container.
local needs=(node rust)
case "${1:-}" in
vitest | pnpm | node | npx | tsc) needs=(node) ;;
cargo | rustc | rustup) needs=(rust) ;;
esac
local workdir="" vitest=()
if [ "${1:-}" = vitest ]; then
shift
vitest=(npx vitest)
case " $* " in
*" run "*|*" watch "*|*" --watch"*|*" -w "*) ;;
*) vitest+=(run) ;;
esac
workdir="packages/agent-host"
fi
set -- ${vitest[@]+"${vitest[@]}"} "$@"
local state=0
engine_state "${needs[@]}" || state=$?
case $state in
# Docker answers: the container path below is the one that runs. The
# other two branches never return.
0) ;;
2) fail_docker_down ;;
# No Docker: the same command runs in this tree, on the vendored
# toolchain, with no container in the way at all. `--sync-only` has done
# its work once the dependencies are in, and must not open a shell.
*)
# What the command names is what gets installed: `dev cargo …` never
# fetches Node, `dev vitest …` never fetches Rust, and a shell or a
# script (which may call either) gets both. Only a command that runs
# Node needs the workspace's packages.
ensure_toolchain "${needs[@]}"
case " ${needs[*]} " in *" node "*) ensure_local_deps ;; esac
[ "$only_sync" = 1 ] && return 0
cd "${workdir:-.}"
if [ $# -eq 0 ]; then exec "${SHELL:-bash}"; fi
if [ -t 0 ] && [ -t 1 ]; then exec "$@"; fi
local code=0
with_timeout "$limit" "$@" || code=$?
report_timeout "$code" "$limit"
return
;;
esac
[ "$reset" = 1 ] && docker rm -f "$DEV_CONTAINER" >/dev/null 2>&1
ensure_dev_container
if [ "$sync" = 1 ]; then
sync_tree "$DEV_CONTAINER" "$DEV_ROOT"
ensure_dev_deps
fi
[ "$only_sync" = 1 ] && return 0
# A terminal (a person, or an agent with a pty) gets a pty; anything else
# must not have one, or the command's output stops being a pipe.
local tty=()
if [ -t 0 ] && [ -t 1 ]; then tty=(-t); fi
if [ $# -eq 0 ]; then
MSYS_NO_PATHCONV=1 exec docker exec -i ${tty[@]+"${tty[@]}"} -w "$DEV_ROOT" "$DEV_CONTAINER" sh
fi
if [ ${#tty[@]} -gt 0 ] || [ "$limit" = 0 ]; then
MSYS_NO_PATHCONV=1 exec docker exec -i ${tty[@]+"${tty[@]}"} \
-w "$DEV_ROOT${workdir:+/$workdir}" "$DEV_CONTAINER" "$@"
fi
local code=0
MSYS_NO_PATHCONV=1 docker exec -i -w "$DEV_ROOT${workdir:+/$workdir}" "$DEV_CONTAINER" \
timeout --kill-after=15s "${limit}s" "$@" || code=$?
report_timeout "$code" "$limit"
}
# --- The bridge end to end: the two ignored tests that need a built host. ---
#
# The driver-protocol spawn test and the bridge end-to-end test drive the real
# host bundle from Rust, so they are #[ignore]d for a plain `cargo test` and
# CI runs them as a job of their own. This is that job: build the bundle, then
# both tests, in the dev container (or in this tree, without Docker).
cmd_e2e() {
begin_steps
while [ $# -gt 0 ]; do
case "$1" in
--verbose) CHECK_STREAM=1 ;;
--quiet) CHECK_STREAM=0 ;;
*) echo "usage: $0 e2e [--verbose|--quiet]" >&2; exit 2 ;;
esac
shift
done
local run=()
local state=0
engine_state node rust || state=$?
case $state in
0)
ensure_dev_container
sync_tree "$DEV_CONTAINER" "$DEV_ROOT"
ensure_dev_deps
run=(dexec -w "$DEV_ROOT" "$DEV_CONTAINER")
;;
2) fail_docker_down ;;
*)
ensure_toolchain node rust
ensure_local_deps
;;
esac
# Without the bundle both tests fail for a reason that is not theirs.
if step "host: build" ${run[@]+"${run[@]}"} pnpm --filter "@codedeck/agent-host" run build; then
step "driver protocol" ${run[@]+"${run[@]}"} \
cargo test --locked -p agent-protocol --test agent_host -- --ignored || true
step "bridge end to end" ${run[@]+"${run[@]}"} \
cargo test --locked -p bridge-runtime --test e2e --test web_page -- --ignored || true
fi
check_summary
}
# --- The files a tool writes, regenerated and brought back. ---
#
# Four kinds of file in the tree are generated, and CI fails when one is stale:
# Cargo.lock (builds run --locked), the host's TypeScript protocol types, the
# agents' install pins (with pnpm-lock.yaml), and the UniFFI Kotlin bindings.
# On the Docker engine each is produced inside a container, whose tree the next
# sync overwrites — so every one of them used to be a command plus a
# `docker cp` to remember. This is both, for each.
REGEN_KINDS="cargo-lock, ts, pins, bindings, all (cargo-lock + ts + bindings)"
# Copy <path> (a file, or a directory's contents) out of the dev container's
# tree into this one. A relative host path on purpose: with MSYS path
# conversion off for the container side, an absolute one would be read as a
# Windows path.
copy_back() {
local path
for path in "$@"; do
if dexec "$DEV_CONTAINER" test -d "$DEV_ROOT/$path"; then
MSYS_NO_PATHCONV=1 docker cp "$DEV_CONTAINER:$DEV_ROOT/$path/." "$path/"
else
MSYS_NO_PATHCONV=1 docker cp "$DEV_CONTAINER:$DEV_ROOT/$path" "$path"
fi
done
}
# What a regeneration changed, which is the answer to "was it stale?" — on
# either engine: the files as they now are against what git last saw.
report_regen() {
local changed
changed="$(git status --short -- "$@")"
if [ -n "$changed" ]; then printf '%s\n' "$changed"; else echo "==> Already current: $*"; fi
}
regen_one() {
local kind="$1" docker="$2" run=()
if [ "$docker" = 1 ]; then run=(dexec -w "$DEV_ROOT" "$DEV_CONTAINER"); fi
case "$kind" in
cargo-lock)
echo "==> Cargo.lock"
# The cheapest command that resolves the workspace: it rewrites the lock
# for what Cargo.toml now asks and leaves every other entry where it was.
${run[@]+"${run[@]}"} sh -c 'cargo metadata --format-version 1 >/dev/null'
[ "$docker" = 1 ] && copy_back Cargo.lock
report_regen Cargo.lock
;;
ts)
echo "==> The agent host's protocol types"
${run[@]+"${run[@]}"} cargo test --locked -p agent-protocol --test gen_ts_bindings -- --ignored
[ "$docker" = 1 ] && copy_back packages/agent-host/src/generated/protocol.ts
report_regen packages/agent-host/src/generated/protocol.ts
;;
pins)
echo "==> pnpm-lock.yaml and the agents' install pins"
${run[@]+"${run[@]}"} pnpm install --no-frozen-lockfile
${run[@]+"${run[@]}"} pnpm --filter "@codedeck/agent-host" run gen:platform-packages
if [ "$docker" = 1 ]; then
copy_back pnpm-lock.yaml packages/agent-host/src/generated
# The container's install is the one this lockfile describes: say so,
# or the next `dev` would reinstall what is already there.
dexec "$DEV_CONTAINER" sh -c "printf '%s' '$(git hash-object pnpm-lock.yaml)' > $DEV_LOCK_STAMP"
fi
report_regen pnpm-lock.yaml packages/agent-host/src/generated
;;
esac
return 0
}
cmd_regen() {
local kinds=()
case "${1:-}" in
cargo-lock | ts | pins | bindings) kinds=("$1") ;;
all) kinds=(cargo-lock ts bindings) ;;
*)
echo "usage: $0 regen <kind>" >&2
echo " kinds: $REGEN_KINDS" >&2
exit 2
;;
esac
local state docker synced=0 kind needs