-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrading-cli
More file actions
executable file
·1753 lines (1600 loc) · 50.2 KB
/
trading-cli
File metadata and controls
executable file
·1753 lines (1600 loc) · 50.2 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
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
STATE_DIR="$ROOT_DIR/.trading-cli"
DEV_DIR="$STATE_DIR/dev"
LOG_DIR="$DEV_DIR/logs"
RUN_BOTS_SCRIPT="$ROOT_DIR/run-bots.sh"
KNOWN_COMMANDS=(
help
test
weather
arbitrage
llm-workflow
temporal
observability
sports-agent
clawdbot-trading
clawdbot-trading-up
clawdbot-trading-down
clawdbot-trading-ps
clawdbot-trading-logs
clawdbot-trading-ping
clawdbot-trading-status
clawdbot-trading-capabilities
clawdbot-trading-doctor
clawdbot-trading-start
clawdbot-trading-stop
clawdbot-trading-venue-list
clawdbot-trading-portfolio-balances
clawdbot-trading-portfolio-positions
dev
down
status
bots
)
usage() {
cat <<'EOF'
Usage:
./trading-cli <command> [args]
Commands:
./trading-cli help # show this help + examples
./trading-cli test [suite] # run tests from repo root (inclusive by default)
./trading-cli weather [go|up|down|status|logs] [prod|dry-run|check-auth] # deterministic weather orchestrator
./trading-cli arbitrage [go|up|down|status|logs] [prod|dry-run|check-auth] # deterministic arbitrage orchestrator
./trading-cli llm-workflow [go|up|down|status|logs] # llm workflow orchestrator
./trading-cli temporal [up|down|status|logs|ui|list|describe|show] # temporal debugger/server orchestrator
./trading-cli observability [up|down|status|logs|ui] [service] # observability stack orchestrator
./trading-cli sports-agent [go|up|down|status|logs|approve|execute|cancel|hard-cancel|stop-service] [--mode <hitl|auto_ultra_strict>] [--dry-run] <workflow_id_or_trace_id>
./trading-cli clawdbot-trading [up|down|ps|logs|ping|status|capabilities|doctor|start|stop|venue-list|venue-status|venue-enable|venue-disable|portfolio-balances|portfolio-positions|order-list|order-cancel|order-submit|execution-mode-get|execution-mode-set] [args...]
# OpenClaw + Rust trading bridge controls (up checks required service health)
./trading-cli clawdbot-trading-up|clawdbot-trading-down|clawdbot-trading-ps
# convenience aliases for compose lifecycle
./trading-cli clawdbot-trading-ping|clawdbot-trading-status|clawdbot-trading-capabilities|clawdbot-trading-doctor|clawdbot-trading-start|clawdbot-trading-stop
# convenience aliases for bridge health/control
./trading-cli clawdbot-trading-venue-list|clawdbot-trading-portfolio-balances|clawdbot-trading-portfolio-positions
# convenience aliases for additive multi-asset controls
# note: openclaw-cli is an on-demand helper container and may be Exited
./trading-cli dev # launch weather+arbitrage+llm-workflow (bg)
./trading-cli down # stop all background services
./trading-cli status # show all background service statuses
./trading-cli bots [prod|dry-run|check-auth] # legacy wrapper for run-bots.sh
Test suites:
all|inclusive (default)
common
kalshi-client
weather|weather-bot
arbitrage|arbitrage-bot
llm-workflow|llm-rules-bot
llm-candidate-engine
llm-decision-engine
llm-execution-engine
llm-client
llm-rules-bot-bin
sports-agent
observability
Examples:
./trading-cli test
./trading-cli test inclusive
./trading-cli test weather-bot
./trading-cli test observability
./trading-cli weather go dry-run
./trading-cli weather up check-auth
./trading-cli arbitrage go check-auth
./trading-cli llm-workflow up
./trading-cli llm-workflow logs
./trading-cli temporal up
./trading-cli temporal ui
./trading-cli temporal list
./trading-cli temporal describe <workflow_id>
./trading-cli observability up
./trading-cli observability ui
./trading-cli clawdbot-trading up
./trading-cli clawdbot-trading ping
./trading-cli clawdbot-trading status
./trading-cli clawdbot-trading capabilities
./trading-cli clawdbot-trading doctor
./trading-cli clawdbot-trading start
./trading-cli clawdbot-trading stop
./trading-cli clawdbot-trading venue-list
./trading-cli clawdbot-trading portfolio-balances
./trading-cli clawdbot-trading execution-mode-get --venue-id kalshi --market-type binary
./trading-cli clawdbot-trading down
./trading-cli sports-agent up --mode hitl
./trading-cli sports-agent execute <workflow_id_or_trace_id>
./trading-cli sports-agent cancel <workflow_id_or_trace_id>
./trading-cli sports-agent cancel <workflow_id_or_trace_id> --hard
./trading-cli sports-agent stop-service
./trading-cli temporal down
./trading-cli dev
./trading-cli down
EOF
}
suggest_command() {
local input="$1"
if [[ "$input" == "llm" || "$input" == "llm-workflow" ]]; then
echo "llm-workflow"
return
fi
if [[ "$input" == "arb" || "$input" == "arbitrage" ]]; then
echo "arbitrage"
return
fi
if [[ "$input" == "temp" || "$input" == "temporal" || "$input" == "debugger" ]]; then
echo "temporal"
return
fi
if [[ "$input" == "observability" || "$input" == "observe" || "$input" == "obs" ]]; then
echo "observability"
return
fi
if [[ "$input" == "sports-agent" || "$input" == "sports" || "$input" == "agent" ]]; then
echo "sports-agent"
return
fi
for cmd in "${KNOWN_COMMANDS[@]}"; do
if [[ "$cmd" == "$input"* ]]; then
echo "$cmd"
return
fi
done
for cmd in "${KNOWN_COMMANDS[@]}"; do
if [[ "$cmd" == *"$input"* ]]; then
echo "$cmd"
return
fi
done
# Fuzzy fallback: match first 3 chars when available.
if [[ "${#input}" -ge 3 ]]; then
local prefix="${input:0:3}"
for cmd in "${KNOWN_COMMANDS[@]}"; do
if [[ "$cmd" == "$prefix"* ]]; then
echo "$cmd"
return
fi
done
fi
}
run_step() {
local label="$1"
shift
echo
echo "==> $label"
"$@"
}
test_common() {
run_step \
"common tests" \
cargo test --manifest-path "$ROOT_DIR/non-agent-workflows/libs/common/Cargo.toml"
}
test_kalshi_client() {
run_step \
"kalshi-client tests" \
cargo test --manifest-path "$ROOT_DIR/non-agent-workflows/libs/kalshi_client/Cargo.toml"
}
test_weather_bot() {
run_step \
"weather-bot workspace tests" \
cargo test --manifest-path "$ROOT_DIR/non-agent-workflows/weather-bot/Cargo.toml" --workspace
}
test_arbitrage_bot() {
run_step \
"arbitrage-bot workspace tests" \
cargo test --manifest-path "$ROOT_DIR/non-agent-workflows/arbitrage-bot/Cargo.toml" --workspace
}
test_llm_rules_bot() {
run_step \
"llm-rules-bot workspace tests" \
cargo test --manifest-path "$ROOT_DIR/llm-workflows/llm-rules-bot/Cargo.toml" --workspace
}
test_llm_candidate_engine() {
run_step \
"llm candidate-engine tests" \
cargo test --manifest-path "$ROOT_DIR/llm-workflows/llm-rules-bot/crates/candidate-engine/Cargo.toml"
}
test_llm_decision_engine() {
run_step \
"llm decision-engine tests" \
cargo test --manifest-path "$ROOT_DIR/llm-workflows/llm-rules-bot/crates/decision-engine/Cargo.toml"
}
test_llm_execution_engine() {
run_step \
"llm execution-engine tests" \
cargo test --manifest-path "$ROOT_DIR/llm-workflows/llm-rules-bot/crates/execution-engine/Cargo.toml"
}
test_llm_client() {
run_step \
"llm client tests" \
cargo test --manifest-path "$ROOT_DIR/llm-workflows/llm-rules-bot/crates/llm-client/Cargo.toml"
}
test_llm_rules_bot_bin() {
run_step \
"llm-rules-bot binary tests" \
cargo test --manifest-path "$ROOT_DIR/llm-workflows/llm-rules-bot/Cargo.toml" --bin llm-rules-bot
}
test_sports_agent() {
run_step \
"sports-agent worker tests" \
cargo test --manifest-path "$ROOT_DIR/observability-platform/sports-agent-worker/Cargo.toml"
}
test_observability() {
local node_bin
node_bin="$(resolve_cmd node || true)"
if [[ -z "$node_bin" ]]; then
echo
echo "==> observability tests"
echo "Skipping observability tests: Node.js 18+ not found."
return 0
fi
run_step \
"observability platform tests" \
run_in_dir "$ROOT_DIR/observability-platform" "$node_bin" --test \
tests/http-json.test.mjs \
tests/local-ops-cache.test.mjs \
tests/config-response.test.mjs \
tests/temporal-broker.api.test.mjs \
tests/trace-api.api.test.mjs
}
test_llm_workflow_inclusive() {
test_llm_rules_bot
test_llm_candidate_engine
test_llm_decision_engine
test_llm_execution_engine
test_llm_client
test_llm_rules_bot_bin
}
run_tests() {
local suite="${1:-all}"
case "$suite" in
all|inclusive)
test_common
test_kalshi_client
test_weather_bot
test_arbitrage_bot
test_llm_workflow_inclusive
test_sports_agent
test_observability
;;
common)
test_common
;;
kalshi-client)
test_kalshi_client
;;
weather|weather-bot)
test_weather_bot
;;
arbitrage|arbitrage-bot)
test_arbitrage_bot
;;
llm-workflow|llm|llm-rules-bot)
test_llm_workflow_inclusive
;;
llm-candidate-engine)
test_llm_candidate_engine
;;
llm-decision-engine)
test_llm_decision_engine
;;
llm-execution-engine)
test_llm_execution_engine
;;
llm-client)
test_llm_client
;;
llm-rules-bot-bin)
test_llm_rules_bot_bin
;;
sports-agent|sports-agent-worker)
test_sports_agent
;;
observability|observability-platform|trace-api|temporal-broker)
test_observability
;;
*)
echo "Unknown test suite: $suite" >&2
usage
exit 1
;;
esac
}
run_legacy_bots() {
local mode="${1:-}"
if [[ -z "$mode" ]]; then
echo "Missing bots mode. Choose one of: prod | dry-run | check-auth" >&2
usage
exit 1
fi
case "$mode" in
prod|dry-run|check-auth) ;;
*)
echo "Invalid bots mode: $mode" >&2
usage
exit 1
;;
esac
exec "$RUN_BOTS_SCRIPT" "$mode"
}
temporal_address() {
echo "${TEMPORAL_ADDRESS:-127.0.0.1:7233}"
}
temporal_ui_url() {
local ui_ip="${TEMPORAL_UI_IP:-127.0.0.1}"
local ui_port="${TEMPORAL_UI_PORT:-8233}"
echo "http://${ui_ip}:${ui_port}"
}
temporal_server_ip() {
echo "${TEMPORAL_SERVER_IP:-127.0.0.1}"
}
temporal_server_ui_port() {
echo "${TEMPORAL_UI_PORT:-8233}"
}
resolve_cmd() {
local name="$1"
if command -v "$name" >/dev/null 2>&1; then
command -v "$name"
return 0
fi
if [[ -x "/opt/homebrew/bin/$name" ]]; then
echo "/opt/homebrew/bin/$name"
return 0
fi
if [[ -x "/usr/local/bin/$name" ]]; then
echo "/usr/local/bin/$name"
return 0
fi
return 1
}
require_temporal_cli() {
if ! resolve_cmd temporal >/dev/null 2>&1; then
echo "Temporal CLI not found. Install with: brew install temporal" >&2
exit 1
fi
}
js_runtime() {
if resolve_cmd node >/dev/null 2>&1; then
resolve_cmd node
return 0
fi
if resolve_cmd bun >/dev/null 2>&1; then
resolve_cmd bun
return 0
fi
return 1
}
require_js_runtime() {
local runtime
runtime="$(js_runtime || true)"
if [[ -z "$runtime" ]]; then
echo "No JS runtime found. Install a JS runtime (for example, Node.js 18+ or Bun) to run observability services." >&2
echo "macOS install example: brew install node" >&2
exit 1
fi
}
docker_compose_bin() {
local docker_bin
docker_bin="$(resolve_cmd docker || true)"
if [[ -z "$docker_bin" ]]; then
echo "Docker is required for clawdbot-trading commands." >&2
echo "Install Docker Desktop and retry." >&2
return 1
fi
if ! "$docker_bin" compose version >/dev/null 2>&1; then
echo "Docker Compose v2 is required for clawdbot-trading commands." >&2
return 1
fi
echo "$docker_bin"
}
clawdbot_trading_compose_file() {
echo "$ROOT_DIR/clawdbot-workflows/docker-compose.yml"
}
clawdbot_trading_container_id() {
local docker_bin="$1"
local compose_file="$2"
local service="$3"
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" ps -q "$service" 2>/dev/null || true
}
clawdbot_trading_service_state() {
local docker_bin="$1"
local compose_file="$2"
local service="$3"
local container_id
container_id="$(clawdbot_trading_container_id "$docker_bin" "$compose_file" "$service")"
if [[ -z "$container_id" ]]; then
echo "missing"
return
fi
local state
state="$("$docker_bin" inspect -f '{{.State.Status}}' "$container_id" 2>/dev/null || true)"
if [[ -z "$state" ]]; then
echo "unknown"
return
fi
echo "$state"
}
clawdbot_trading_wait_for_required_services() {
local docker_bin="$1"
local compose_file="$2"
local timeout_seconds="${3:-30}"
local start_epoch now
start_epoch="$(date +%s)"
while true; do
local daemon_state gateway_state
daemon_state="$(clawdbot_trading_service_state "$docker_bin" "$compose_file" "trading-daemon" || true)"
gateway_state="$(clawdbot_trading_service_state "$docker_bin" "$compose_file" "openclaw-gateway" || true)"
if [[ "$daemon_state" == "running" && "$gateway_state" == "running" ]]; then
local cli_state
cli_state="$(clawdbot_trading_service_state "$docker_bin" "$compose_file" "openclaw-cli" || true)"
echo
echo "Trading bridge services are up:"
echo " trading-daemon: $daemon_state"
echo " openclaw-gateway: $gateway_state"
echo " openclaw-cli: ${cli_state:-unknown} (ephemeral helper container; may be exited)"
return 0
fi
now="$(date +%s)"
if (( now - start_epoch >= timeout_seconds )); then
echo >&2
echo "Timed out waiting for required trading bridge services to be healthy." >&2
echo "Required services:" >&2
echo " trading-daemon: ${daemon_state:-unknown}" >&2
echo " openclaw-gateway: ${gateway_state:-unknown}" >&2
echo >&2
echo "Recent logs (openclaw-gateway, trading-daemon):" >&2
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" logs --tail=80 openclaw-gateway trading-daemon >&2 || true
return 1
fi
sleep 1
done
}
dotenv_value() {
local key="$1"
local default_value="${2:-}"
local env_value="${!key:-}"
if [[ -n "$env_value" ]]; then
echo "$env_value"
return
fi
local env_file="$ROOT_DIR/.env"
if [[ -f "$env_file" ]]; then
local line
line="$(grep -E "^${key}=" "$env_file" | tail -n 1 || true)"
if [[ -n "$line" ]]; then
local value="${line#*=}"
value="${value%$'\r'}"
value="${value%\"}"
value="${value#\"}"
value="${value%\'}"
value="${value#\'}"
echo "$value"
return
fi
fi
echo "$default_value"
}
gateway_trading_hft_health_ok() {
local response="$1"
local jq_bin
jq_bin="$(resolve_cmd jq || true)"
if [[ -n "$jq_bin" ]]; then
if printf '%s' "$response" | "$jq_bin" -e '.ok == true and (.result | type == "object") and .result.ok == true' >/dev/null 2>&1; then
return 0
fi
return 1
fi
local node_bin
node_bin="$(resolve_cmd node || true)"
if [[ -n "$node_bin" ]]; then
if printf '%s' "$response" | "$node_bin" -e '
let data = "";
process.stdin.on("data", (chunk) => (data += chunk));
process.stdin.on("end", () => {
try {
const parsed = JSON.parse(data);
process.exit(parsed?.ok === true && parsed?.result?.ok === true ? 0 : 1);
} catch {
process.exit(1);
}
});
' >/dev/null 2>&1; then
return 0
fi
return 1
fi
local compact
compact="$(printf '%s' "$response" | tr -d '[:space:]')"
[[ "$compact" == *'"ok":true'* && "$compact" == *'"result":{'* && "$compact" == *'"result":{"ok":true'* ]]
}
clawdbot_trading_doctor() {
local docker_bin="$1"
local compose_file="$2"
local failures=0
local pass="PASS"
local fail="FAIL"
echo "Running clawdbot trading doctor..."
local daemon_state gateway_state
daemon_state="$(clawdbot_trading_service_state "$docker_bin" "$compose_file" "trading-daemon" || true)"
gateway_state="$(clawdbot_trading_service_state "$docker_bin" "$compose_file" "openclaw-gateway" || true)"
if [[ "$daemon_state" == "running" ]]; then
echo "[$pass] trading-daemon service running"
else
echo "[$fail] trading-daemon service not running (state=$daemon_state)" >&2
failures=$((failures + 1))
fi
if [[ "$gateway_state" == "running" ]]; then
echo "[$pass] openclaw-gateway service running"
else
echo "[$fail] openclaw-gateway service not running (state=$gateway_state)" >&2
failures=$((failures + 1))
fi
if [[ "$daemon_state" == "running" ]]; then
local capabilities_output
if capabilities_output="$("$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" exec -T trading-daemon tradingctl capabilities 2>&1)"; then
if echo "$capabilities_output" | grep -q '"protocol_version"'; then
echo "[$pass] daemon exposes protocol_version in capabilities"
else
echo "[$fail] daemon capabilities missing protocol_version" >&2
failures=$((failures + 1))
fi
if echo "$capabilities_output" | grep -q '"status_schema_version"'; then
echo "[$pass] daemon exposes status_schema_version in capabilities"
else
echo "[$fail] daemon capabilities missing status_schema_version" >&2
failures=$((failures + 1))
fi
if echo "$capabilities_output" | grep -q 'Control.Capabilities'; then
echo "[$pass] daemon advertises Control.Capabilities command support"
else
echo "[$fail] daemon command surface does not include Control.Capabilities" >&2
failures=$((failures + 1))
fi
if echo "$capabilities_output" | grep -q 'Engine.GetMode'; then
echo "[$pass] daemon advertises Engine.GetMode command support"
else
echo "[$fail] daemon command surface does not include Engine.GetMode" >&2
failures=$((failures + 1))
fi
if echo "$capabilities_output" | grep -q 'Execution.Place'; then
echo "[$pass] daemon advertises Execution.Place command support"
else
echo "[$fail] daemon command surface does not include Execution.Place" >&2
failures=$((failures + 1))
fi
if echo "$capabilities_output" | grep -q 'Portfolio.Exposure'; then
echo "[$pass] daemon advertises Portfolio.Exposure command support"
else
echo "[$fail] daemon command surface does not include Portfolio.Exposure" >&2
failures=$((failures + 1))
fi
else
echo "[$fail] tradingctl capabilities command failed" >&2
echo "$capabilities_output" >&2
failures=$((failures + 1))
fi
local status_output
if status_output="$("$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" exec -T trading-daemon tradingctl status 2>&1)"; then
if echo "$status_output" | grep -q '"status_schema_version"'; then
echo "[$pass] status payload includes status_schema_version"
else
echo "[$fail] status payload missing status_schema_version (schema drift)" >&2
failures=$((failures + 1))
fi
else
echo "[$fail] tradingctl status command failed" >&2
echo "$status_output" >&2
failures=$((failures + 1))
fi
local mode_output
if mode_output="$("$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" exec -T trading-daemon tradingctl engine-get-mode 2>&1)"; then
if echo "$mode_output" | grep -q '"mode"'; then
echo "[$pass] engine mode endpoint responds with mode payload"
else
echo "[$fail] engine-get-mode missing mode payload" >&2
failures=$((failures + 1))
fi
else
echo "[$fail] tradingctl engine-get-mode command failed" >&2
echo "$mode_output" >&2
failures=$((failures + 1))
fi
fi
if [[ "$gateway_state" == "running" ]]; then
local curl_bin
curl_bin="$(resolve_cmd curl || true)"
if [[ -z "$curl_bin" ]]; then
echo "[$fail] curl not found; cannot verify gateway trading_hft tool availability" >&2
failures=$((failures + 1))
else
local gateway_port gateway_token gateway_response
gateway_port="$(dotenv_value OPENCLAW_GATEWAY_PORT 18789)"
gateway_token="$(dotenv_value OPENCLAW_GATEWAY_TOKEN "")"
if [[ -z "$gateway_token" ]]; then
echo "[$fail] OPENCLAW_GATEWAY_TOKEN is empty; cannot call /tools/invoke" >&2
failures=$((failures + 1))
else
if gateway_response="$("$curl_bin" -fsS -X POST "http://127.0.0.1:${gateway_port}/tools/invoke" \
-H "content-type: application/json" \
-H "authorization: Bearer ${gateway_token}" \
--data '{"tool":"trading_hft","action":"json","args":{"action":"health"}}' 2>&1)"; then
if echo "$gateway_response" | grep -q 'Tool not available: trading_hft'; then
echo "[$fail] trading_hft tool is not registered in gateway runtime" >&2
failures=$((failures + 1))
elif gateway_trading_hft_health_ok "$gateway_response"; then
echo "[$pass] trading_hft tool is available and returns healthy result via gateway /tools/invoke"
else
echo "[$fail] gateway response did not confirm trading_hft readiness" >&2
echo "$gateway_response" >&2
failures=$((failures + 1))
fi
else
echo "[$fail] failed to call gateway /tools/invoke trading_hft health" >&2
echo "$gateway_response" >&2
failures=$((failures + 1))
fi
local gateway_mode_response
if gateway_mode_response="$("$curl_bin" -fsS -X POST "http://127.0.0.1:${gateway_port}/tools/invoke" \
-H "content-type: application/json" \
-H "authorization: Bearer ${gateway_token}" \
--data '{"tool":"trading_hft","action":"json","args":{"action":"engine_get_mode"}}' 2>&1)"; then
if echo "$gateway_mode_response" | grep -q '"ok":true'; then
echo "[$pass] trading_hft engine_get_mode action responds via gateway /tools/invoke"
else
echo "[$fail] gateway engine_get_mode action did not return ok=true" >&2
echo "$gateway_mode_response" >&2
failures=$((failures + 1))
fi
else
echo "[$fail] failed to call gateway /tools/invoke trading_hft engine_get_mode" >&2
echo "$gateway_mode_response" >&2
failures=$((failures + 1))
fi
local gateway_open_orders_response
if gateway_open_orders_response="$("$curl_bin" -fsS -X POST "http://127.0.0.1:${gateway_port}/tools/invoke" \
-H "content-type: application/json" \
-H "authorization: Bearer ${gateway_token}" \
--data '{"tool":"trading_hft","action":"json","args":{"action":"execution_open_orders"}}' 2>&1)"; then
if echo "$gateway_open_orders_response" | grep -q '"ok":true'; then
echo "[$pass] trading_hft execution_open_orders action responds via gateway /tools/invoke"
else
echo "[$fail] gateway execution_open_orders action did not return ok=true" >&2
echo "$gateway_open_orders_response" >&2
failures=$((failures + 1))
fi
else
echo "[$fail] failed to call gateway /tools/invoke trading_hft execution_open_orders" >&2
echo "$gateway_open_orders_response" >&2
failures=$((failures + 1))
fi
fi
fi
fi
if (( failures > 0 )); then
echo "Doctor completed with $failures failure(s)." >&2
return 1
fi
echo "Doctor completed successfully."
}
run_clawdbot_trading() {
local action="${1:-status}"
shift || true
local docker_bin compose_file
docker_bin="$(docker_compose_bin)" || exit 1
compose_file="$(clawdbot_trading_compose_file)"
case "$action" in
up)
if [[ $# -gt 0 ]]; then
echo "Too many arguments for clawdbot-trading up command" >&2
echo "Use: ./trading-cli clawdbot-trading up" >&2
exit 1
fi
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" up -d --build
local timeout_seconds="${CLAWDBOT_TRADING_UP_TIMEOUT_SECONDS:-30}"
clawdbot_trading_wait_for_required_services "$docker_bin" "$compose_file" "$timeout_seconds"
;;
down)
if [[ $# -gt 0 ]]; then
echo "Too many arguments for clawdbot-trading down command" >&2
echo "Use: ./trading-cli clawdbot-trading down" >&2
exit 1
fi
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" down
;;
ps|services)
if [[ $# -gt 0 ]]; then
echo "Too many arguments for clawdbot-trading ps command" >&2
echo "Use: ./trading-cli clawdbot-trading ps" >&2
exit 1
fi
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" ps
;;
logs)
local service="${1:-trading-daemon}"
if [[ $# -gt 1 ]]; then
echo "Too many arguments for clawdbot-trading logs command" >&2
echo "Use: ./trading-cli clawdbot-trading logs [trading-daemon|openclaw-gateway|openclaw-cli]" >&2
exit 1
fi
case "$service" in
trading-daemon|openclaw-gateway|openclaw-cli) ;;
*)
echo "Unknown clawdbot-trading service: $service" >&2
echo "Use: ./trading-cli clawdbot-trading logs [trading-daemon|openclaw-gateway|openclaw-cli]" >&2
exit 1
;;
esac
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" logs -f "$service"
;;
doctor)
if [[ $# -gt 0 ]]; then
echo "Too many arguments for clawdbot-trading doctor command" >&2
echo "Use: ./trading-cli clawdbot-trading doctor" >&2
exit 1
fi
clawdbot_trading_doctor "$docker_bin" "$compose_file"
;;
ping|status|capabilities|start|stop)
if [[ $# -gt 0 ]]; then
echo "Too many arguments for clawdbot-trading $action command" >&2
echo "Use: ./trading-cli clawdbot-trading $action" >&2
exit 1
fi
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" exec trading-daemon tradingctl "$action"
;;
venue-list|venue-enable|venue-disable|venue-status|portfolio-balances|portfolio-positions|order-submit|order-cancel|order-list|execution-mode-get|execution-mode-set)
"$docker_bin" compose -f "$compose_file" --project-directory "$ROOT_DIR" exec trading-daemon tradingctl "$action" "$@"
;;
*)
echo "Unknown clawdbot-trading action: $action" >&2
echo "Use: ./trading-cli clawdbot-trading [up|down|ps|logs|ping|status|capabilities|doctor|start|stop|venue-list|venue-status|venue-enable|venue-disable|portfolio-balances|portfolio-positions|order-list|order-cancel|order-submit|execution-mode-get|execution-mode-set]" >&2
exit 1
;;
esac
}
temporal_reachable() {
local temporal_bin
temporal_bin="$(resolve_cmd temporal || true)"
if [[ -z "$temporal_bin" ]]; then
return 1
fi
"$temporal_bin" workflow list --address "$(temporal_address)" --limit 1 >/dev/null 2>&1
}
workflow_mode_to_arg() {
local mode="$1"
case "$mode" in
prod)
echo ""
;;
dry-run)
echo "--dry-run"
;;
check-auth)
echo "--check-auth"
;;
*)
return 1
;;
esac
}
run_in_dir() {
local workdir="$1"
shift
(
cd "$workdir"
exec "$@"
)
}
pidfile_for() {
local service="$1"
echo "$DEV_DIR/${service}.pid"
}
logfile_for() {
local service="$1"
echo "$LOG_DIR/${service}.log"
}
is_pid_running() {
local pid="$1"
kill -0 "$pid" 2>/dev/null
}
service_status_line() {
local service="$1"
local pidfile
pidfile="$(pidfile_for "$service")"
if [[ ! -f "$pidfile" ]]; then
printf "%-13s stopped\n" "$service"
return
fi
local pid
pid="$(cat "$pidfile" 2>/dev/null || true)"
if [[ -z "$pid" ]]; then
printf "%-13s stopped\n" "$service"
rm -f "$pidfile"
return
fi
if is_pid_running "$pid"; then
printf "%-13s running (pid=%s)\n" "$service" "$pid"
else
printf "%-13s stopped (stale pid cleaned)\n" "$service"
rm -f "$pidfile"
fi
}
start_service_bg() {
local service="$1"
local workdir="$2"
shift 2
local pidfile logfile
pidfile="$(pidfile_for "$service")"
logfile="$(logfile_for "$service")"
if [[ -f "$pidfile" ]]; then
local existing_pid
existing_pid="$(cat "$pidfile" 2>/dev/null || true)"
if [[ -n "$existing_pid" ]] && is_pid_running "$existing_pid"; then
echo "$service already running (pid=$existing_pid)." >&2
return 1
fi
rm -f "$pidfile"
fi
(
cd "$workdir"
exec "$@"
) >"$logfile" 2>&1 &
local pid="$!"
echo "$pid" > "$pidfile"
# Quick liveness check for early crashes.
sleep 0.3
if ! is_pid_running "$pid"; then
echo "Failed to start $service. Check log: $logfile" >&2
tail -n 40 "$logfile" 2>/dev/null || true
rm -f "$pidfile"
return 1
fi
echo "Started $service (pid=$pid, log=$logfile)"
return 0
}
run_dev() {
mkdir -p "$LOG_DIR"
if ! start_service_bg "weather" "$ROOT_DIR/non-agent-workflows/weather-bot" cargo run -- --dry-run; then
echo "dev startup failed while starting weather" >&2
run_down >/dev/null 2>&1 || true
exit 1
fi
if ! start_service_bg "arbitrage" "$ROOT_DIR/non-agent-workflows/arbitrage-bot" cargo run --release -- --dry-run; then
echo "dev startup failed while starting arbitrage" >&2
run_down >/dev/null 2>&1 || true
exit 1
fi
if ! start_service_bg "llm-workflow" "$ROOT_DIR/llm-workflows/llm-rules-bot" cargo run; then
echo "dev startup failed while starting llm-workflow" >&2
run_down >/dev/null 2>&1 || true
exit 1
fi
echo
echo "Development services are up."
run_status
}
stop_service_bg() {
local service="$1"
local pidfile
pidfile="$(pidfile_for "$service")"
if [[ ! -f "$pidfile" ]]; then
echo "$service already stopped"
return
fi
local pid
pid="$(cat "$pidfile" 2>/dev/null || true)"
if [[ -z "$pid" ]]; then
rm -f "$pidfile"
echo "$service already stopped"
return
fi
if ! is_pid_running "$pid"; then
rm -f "$pidfile"
echo "$service already stopped (stale pid removed)"