@evident-ai/runner-cdk 3.4.1-dev.c463782 → 3.4.1-dev.d0469f4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,11 +21,15 @@ OPENCODE_PORT="${OPENCODE_PORT:-4096}"
21
21
  # /proc/<pid>/environ, which runs as that same uid. /terminate removes it.
22
22
  # shellcheck disable=SC2034 # read by the scripts that source this file
23
23
  CONTEXT_FILE="/dev/shm/evident-run-context"
24
+
25
+ # The runtime injects MICROVM_ID only into /run, never /resume. This tmpfs file
26
+ # survives suspend/resume so /resume can restore the id for every fresh CLI
27
+ # process to self-report and acknowledge a fulfilled recycle request (#1906).
28
+ # shellcheck disable=SC2034 # read by the scripts that source this file
29
+ MICROVM_ID_FILE="/dev/shm/evident-microvm-id"
24
30
  TUNNEL_PID_FILE="/dev/shm/evident-tunnel.pid"
25
31
  OPENCODE_PID_FILE="/dev/shm/evident-opencode.pid"
26
32
  LITESTREAM_PID_FILE="/dev/shm/evident-litestream.pid"
27
- CREDS_SYNC_PID_FILE="/dev/shm/evident-creds-sync.pid"
28
- CREDS_SYNC_LAST_ERROR_FILE="/dev/shm/evident-creds-sync.last-error"
29
33
 
30
34
  # Where the runner's credential store lives inside the durable-state bucket.
31
35
  # The BUCKET is the same for every VM from an image version, so the stack bakes
@@ -47,24 +51,29 @@ LITESTREAM_CONFIG_FILE="/dev/shm/evident-litestream.yml"
47
51
  # `/terminate` removes it with the rest of the per-VM state.
48
52
  SESSION_DB_NO_REPLICATE_MARKER="/dev/shm/evident-session-db-no-replicate"
49
53
 
54
+ # Completion evidence for the CLI-owned credential flush. It lives in tmpfs,
55
+ # is removed before every handshake, and is also removed by /terminate.
56
+ CREDENTIAL_FLUSH_MARKER_FILE="/dev/shm/evident-credential-flush"
57
+
50
58
  hook_name() { printf '%s' "${0##*/}"; }
51
59
  log() { echo "[hook:$(hook_name)] $*"; }
52
60
  warn() { echo "[hook:$(hook_name)] $*" >&2; }
53
61
  error() { echo "[hook:$(hook_name)] ERROR: $*" >&2; }
54
62
 
55
- # Returns the CLI's own exit code. Domain outcomes (nothing persisted yet, a
56
- # corrupt object) are LOGGED and exit 0, the predicates answer "no" with 10, and
63
+ # Returns the CLI's own exit code. `restore` logs domain outcomes and returns 0;
64
+ # a non-zero status means the tool itself failed. `sync-once` returns 40 for
65
+ # `failed`, `hashFailed`, and `localInvalid` (credentials not persisted), and 0
66
+ # for every other outcome. The predicates answer "no" with 10, and
57
67
  # `session-db-classify`'s three typed answers are 30 (fatal)/31 (replica
58
68
  # unusable)/32 (retry), extended by `session-db-verify`'s 33 (integrity
59
69
  # exhausted, replica separated and local disposed) / 34 (could not prove
60
- # separation or disposal) see `runner/synchroniser/src/cli.ts`'s own
61
- # comment for what each means, not restated here. Any OTHER non-zero status
70
+ # separation or disposal). Any status outside a command's contractual answers
62
71
  # means the tool itself broke, which is the only case worth an ERROR here.
63
72
  run_synchroniser() {
64
73
  local rc=0
65
74
  "${SYNCHRONISER}" "$@" || rc=$?
66
75
  case "${rc}" in
67
- 0 | 10 | 30 | 31 | 32 | 33 | 34) ;;
76
+ 0 | 10 | 30 | 31 | 32 | 33 | 34 | 40) ;;
68
77
  *) error "synchroniser '$*' exited ${rc}; the '${SYNCHRONISER}' command is missing from PATH, corrupt, or it threw" ;;
69
78
  esac
70
79
  return "${rc}"
@@ -317,7 +326,6 @@ regenerate_machine_id() {
317
326
  tunnel_is_running() { is_running "${TUNNEL_PID_FILE}"; }
318
327
  opencode_is_running() { is_running "${OPENCODE_PID_FILE}"; }
319
328
  litestream_is_running() { is_running "${LITESTREAM_PID_FILE}"; }
320
- creds_sync_is_running() { is_running "${CREDS_SYNC_PID_FILE}"; }
321
329
 
322
330
  # `jq -e` alone is not enough: its exit status reflects the LAST OUTPUT VALUE,
323
331
  # and an interpolation of a missing field is still a non-empty string, so a
@@ -462,149 +470,6 @@ kill_litestream() {
462
470
  }
463
471
  # --- litestream replicate (end) ----------------------------------------------
464
472
 
465
- # --- credential sync loop (#1868 WI-3, ECS parity) ---------------------------
466
- #
467
- # sync_credentials (above) covers the three boundary flushes /run's restore,
468
- # /suspend and /terminate already call. What it does NOT cover is a VM that
469
- # runs for a long time between those boundaries: a provider re-authenticated
470
- # through the proxied UI hours into a run would sit unflushed until the next
471
- # suspend/terminate, and a VM that dies without one (a crash, an OOM kill)
472
- # loses everything since boot. runner/docker-images/fargate/entrypoint.sh's
473
- # own sync_credentials_loop is the ECS side of the identical gap; this is the
474
- # same fix, backgrounded like the other long-lived services so it
475
- # outlives this hook process, `( … ) &` rather than `setsid`: a plain
476
- # backgrounded subshell is reparented to init and keeps running once its
477
- # parent hook script exits (verified: PPID=1, still alive, with no controlling
478
- # terminal in this image to send it a stray SIGHUP), and it inherits every
479
- # function this file defines, so it can call run_synchroniser directly with no
480
- # re-exec.
481
-
482
- # Bounded confirmation window `stop_credential_sync` polls after signalling the
483
- # loop. The loop's current child is one fast `run_synchroniser sync-once` call,
484
- # so this stays a short backstop rather than a graceful drain.
485
- CREDS_SYNC_STOP_WAIT_SECONDS=2
486
-
487
- # Best-effort per tick, exactly like sync_credentials above: a failed tick
488
- # must never end the loop, or a single transient S3 error would silently
489
- # disable sync for the rest of the VM's life.
490
- start_credential_sync() {
491
- if [ -z "${PERSISTENCE_BUCKET:-}" ]; then
492
- warn "CREDS-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot."
493
- return 0
494
- fi
495
-
496
- if creds_sync_is_running; then
497
- warn "credential sync loop already running (pid $(cat "${CREDS_SYNC_PID_FILE}")); reusing it"
498
- return 0
499
- fi
500
-
501
- # This is the same environment input runner-synchroniser validates. The
502
- # variable is normally absent, so that ordinary case uses the documented
503
- # default without a warning; a present invalid value is named and rejected.
504
- local interval="${CREDS_SYNC_INTERVAL:-60}"
505
- if [ -n "${CREDS_SYNC_INTERVAL+x}" ] && [[ ! "${CREDS_SYNC_INTERVAL}" =~ ^[1-9][0-9]*$ ]]; then
506
- warn "CREDS-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${CREDS_SYNC_INTERVAL}' is not a positive integer; using 60s"
507
- interval=60
508
- fi
509
-
510
- rm -f "${CREDS_SYNC_LAST_ERROR_FILE}"
511
-
512
- (
513
- # Releases the fds this subshell inherited from the hook process before
514
- # settling in for the VM's whole remaining life: nothing here writes to
515
- # them (every synchroniser call already redirects its own), so there is
516
- # no reason to keep holding the hook's original stdout/stderr open. A
517
- # long-lived process that instead inherited a pipe's write end (a test
518
- # harness reading the hook's own output, for one) would keep that pipe
519
- # from ever reporting EOF — testing-guide.mdc's own lesson, and the same
520
- # reason the long-lived services never inherit stdio either. That
521
- # redirect also means `warn`/`log`/`error` calls in here go nowhere, so a
522
- # failed sync-once is instead recorded to CREDS_SYNC_LAST_ERROR_FILE and
523
- # surfaced by stop_credential_sync, which DOES have live stdio.
524
- exec >/dev/null 2>&1 </dev/null
525
-
526
- # A TERM this subshell receives (from stop_credential_sync, below) only
527
- # kills THIS wrapper by default — its currently-running child (`sleep`,
528
- # or a `run_synchroniser sync-once` call) is a separate process that
529
- # would otherwise be orphaned and keep running, free to upload STALE
530
- # credentials to S3 after the boundary flush that /suspend and
531
- # /terminate perform immediately following the stop. Tracking the
532
- # current child explicitly and forwarding the signal closes that race.
533
- creds_sync_child_pid=""
534
- trap 'trap - TERM; [ -n "${creds_sync_child_pid}" ] && kill -TERM "${creds_sync_child_pid}" 2>/dev/null; exit 0' TERM
535
-
536
- while true; do
537
- sleep "${interval}" &
538
- creds_sync_child_pid=$!
539
- wait "${creds_sync_child_pid}" 2>/dev/null
540
- creds_sync_child_pid=""
541
-
542
- run_synchroniser sync-once claude &
543
- creds_sync_child_pid=$!
544
- wait "${creds_sync_child_pid}" 2>/dev/null || echo "claude" >"${CREDS_SYNC_LAST_ERROR_FILE}"
545
- creds_sync_child_pid=""
546
-
547
- run_synchroniser sync-once opencode &
548
- creds_sync_child_pid=$!
549
- wait "${creds_sync_child_pid}" 2>/dev/null || echo "opencode" >"${CREDS_SYNC_LAST_ERROR_FILE}"
550
- creds_sync_child_pid=""
551
- done
552
- ) &
553
-
554
- echo $! >"${CREDS_SYNC_PID_FILE}"
555
- log "CREDS-SYNC-STARTED: pid=$! interval=${interval}s"
556
- }
557
-
558
- # Signals the loop, then confirms (bounded — see CREDS_SYNC_STOP_WAIT_SECONDS)
559
- # that it and its current child are actually gone before returning: /suspend
560
- # and /terminate start their own boundary flush immediately after this call,
561
- # and an orphaned in-flight sync-once surviving past that point can overwrite
562
- # fresher credentials with stale ones. The TERM trap inside the loop (above)
563
- # forwards the signal to its current child almost instantly — this poll is a
564
- # defensive confirmation, not the primary mechanism, so it stays short; a
565
- # SIGKILL backstop covers a child that ignores TERM entirely.
566
- #
567
- # The DIED branch is a liveness report, not a no-op: every recovery/no-op path
568
- # must say what it found (development-workflow.mdc) — a stopped-before-called
569
- # loop and a died-on-its-own loop are different facts an operator needs told
570
- # apart, not the same "nothing to stop" line.
571
- stop_credential_sync() {
572
- if [ ! -s "${CREDS_SYNC_PID_FILE}" ]; then
573
- log "CREDS-SYNC-NOT-RUNNING: no credential sync loop to stop"
574
- return 0
575
- fi
576
-
577
- local pid
578
- pid="$(cat "${CREDS_SYNC_PID_FILE}")"
579
- if ! process_is_alive "${pid}"; then
580
- rm -f "${CREDS_SYNC_PID_FILE}"
581
- warn "CREDS-SYNC-DIED: credential sync loop (pid=${pid}) had already exited before this stop"
582
- return 0
583
- fi
584
-
585
- kill -TERM "${pid}" 2>/dev/null || true
586
- rm -f "${CREDS_SYNC_PID_FILE}"
587
-
588
- local waited_ms=0
589
- while process_is_alive "${pid}" && [ "${waited_ms}" -lt $((CREDS_SYNC_STOP_WAIT_SECONDS * 1000)) ]; do
590
- sleep 0.1
591
- waited_ms=$((waited_ms + 100))
592
- done
593
-
594
- if process_is_alive "${pid}"; then
595
- kill -KILL "${pid}" 2>/dev/null || true
596
- warn "CREDS-SYNC-STOP-TIMEOUT: pid=${pid} still alive after ${CREDS_SYNC_STOP_WAIT_SECONDS}s; sent SIGKILL"
597
- fi
598
-
599
- if [ -s "${CREDS_SYNC_LAST_ERROR_FILE}" ]; then
600
- warn "CREDS-SYNC-HAD-FAILURES: sync-once failed at least once for: $(tr '\n' ' ' <"${CREDS_SYNC_LAST_ERROR_FILE}")"
601
- rm -f "${CREDS_SYNC_LAST_ERROR_FILE}"
602
- fi
603
-
604
- log "CREDS-SYNC-STOPPED: pid=${pid}"
605
- }
606
- # --- credential sync loop (end) -----------------------------------------------
607
-
608
473
  # --- flush_session_db (#812 WI-4) -------------------------------------------
609
474
  #
610
475
  # The checked, synchronous flush /suspend and /terminate need before they
@@ -676,16 +541,19 @@ flush_session_db() {
676
541
  # --- flush_session_db (end) --------------------------------------------------
677
542
 
678
543
  # How long the guest CLI runs with no activity before it exits itself
679
- # (`evident run --idle-timeout`, apps/cli/src/commands/run.ts), which is what
544
+ # (`evident run --idle-timeout`), which is what
680
545
  # turns a truly-abandoned VM into the clean-offline POST that lets Evident
681
546
  # suspend it (#732). Sized from the measured cost of guessing wrong rather than
682
547
  # the saving: suspend reaches SUSPENDED in ~7 s and a resume is RUNNING in
683
548
  # ~0.6 s with the tunnel back ~2 s later (README, "Measured, end to end"), so
684
- # napping a VM whose user comes straight back costs ~10 s — against ~$0.30/h
685
- # that is cheap enough that the balance sits far nearer the floor than the
686
- # ceiling. Not AT the floor, though: the CLI's idle detector needs 2 clear poll
687
- # cycles (`run.ts`'s `idlePolls >= 2`, ≥4 s of real time), so a value near that
688
- # would spend more time suspending/resuming than idle.
549
+ # napping a VM whose user comes straight back costs ~10 s — against the
550
+ # legacy conservative baseline input of ~$0.30/h, AWS can burst a loaded VM
551
+ # to a 16 GB / 8 vCPU peak (~$1.06/h at sustained full load—a worst-case
552
+ # ceiling, not an expectation); that is cheap enough that the balance
553
+ # sits far nearer the floor than the ceiling. Not AT the floor, though: the
554
+ # CLI's idle detector needs 2 clear poll cycles (≥4 s of real time), so a value
555
+ # near that would spend more time
556
+ # suspending/resuming than idle.
689
557
  # ECS's waker uses 900 s instead only because *its* cold start is far slower
690
558
  # than this VM's ~2 s resume — not evidence this default should match it.
691
559
  #
@@ -697,7 +565,9 @@ flush_session_db() {
697
565
  # runner/docker-images/fargate/entrypoint.sh uses for the ECS runner's own
698
566
  # idle-timeout flag, so an operator who knows one knows the other. A
699
567
  # non-numeric override must never silently DROP the flag — that degrades to
700
- # an always-on VM burning ~$2.40/day, exactly the bug this closes so it
568
+ # an always-on VM burning ~$7.17/day at baseline, or up to ~$25.49/day at
569
+ # sustained full peak as load increases (a worst-case ceiling, not an
570
+ # expectation), exactly the bug this closes — so it
701
571
  # warns and falls back to the default instead.
702
572
  #
703
573
  # Deliberately NOT in the /run payload yet: doing so would touch the doorbell
@@ -765,6 +635,7 @@ start_tunnel() {
765
635
  --litestream-config "${LITESTREAM_CONFIG_FILE}" \
766
636
  --litestream-pid-file "${LITESTREAM_PID_FILE}" \
767
637
  --session-db-no-replicate-marker "${SESSION_DB_NO_REPLICATE_MARKER}" \
638
+ --credential-sync-marker "${CREDENTIAL_FLUSH_MARKER_FILE}" \
768
639
  "${credential_flags[@]}" \
769
640
  "${session_db_flags[@]}" \
770
641
  "${opencode_config_flags[@]}" \
@@ -776,22 +647,20 @@ start_tunnel() {
776
647
  }
777
648
 
778
649
  # The worst case `evident run` can take to shut down gracefully on SIGTERM, in
779
- # whole seconds: 25 s in-flight drain (SHUTDOWN_DRAIN_TIMEOUT_MS,
780
- # apps/cli/src/commands/run.ts) + 2 s offline POST (notifyAgentDisconnected,
781
- # apps/cli/src/commands/agent-lookup.ts) + 5 s telemetry flush
782
- # (TELEMETRY_SHUTDOWN_TIMEOUT_MS, run.ts). Each of the three is bounded there, so
783
- # this is a ceiling rather than a typical cost — an idle suspend finishes in a
784
- # couple of seconds. This is the ONE place the budget is written down; the CLI
785
- # only points back here, because restating it in three places produced #657.
650
+ # whole seconds: 25 s in-flight drain (SHUTDOWN_DRAIN_TIMEOUT_MS) + 2 s offline
651
+ # POST (notifyAgentDisconnected) + 5 s telemetry flush
652
+ # (TELEMETRY_SHUTDOWN_TIMEOUT_MS) + 8.5 s pre-drain credential flush
653
+ # + 8.5 s post-drain credential flush. Each phase is bounded there, so this is
654
+ # a ceiling rather than a typical cost — an idle suspend finishes in a couple
655
+ # of seconds. This is the ONE place the hand-maintained budget is written down;
656
+ # a guard in the source repository keeps this number in step with the CLI's
657
+ # declared bounds.
786
658
  #
787
- # DOCUMENTATION ONLY — nothing is derived from this any more. The wait below
788
- # used to be pinned above it, which is the reasoning that talked #699 into 40 s;
789
- # since #718 an exited CLI is noticed on the first poll, so the wait is a
790
- # backstop chosen on its own merits and the two numbers are unrelated. Nothing
791
- # cross-checks the 32 against apps/cli either: it is a hand-maintained sum of the
792
- # three bounds cited above, so if one of them moves, update it here.
659
+ # DOCUMENTATION ONLY — nothing is derived from this value. It is the hand-maintained
660
+ # sum of the five bounded phases above (25 + 2 + 5 + 8.5 + 8.5); update it here if
661
+ # any of those bounds changes.
793
662
  # shellcheck disable=SC2034 # documentation; deliberately read by nothing
794
- CLI_SHUTDOWN_CEILING_SECONDS=32
663
+ CLI_SHUTDOWN_CEILING_SECONDS=49
795
664
 
796
665
  # How long stop_tunnel waits for that shutdown before the SIGKILL backstop.
797
666
  # Since #718 this binds ONLY for a CLI that is still draining: one that has
@@ -867,12 +736,91 @@ stop_tunnel() {
867
736
  log "tunnel stopped ${outcome}"
868
737
  }
869
738
 
739
+ # The CLI owns the interval loop and its boundary flush. Two seconds of slack
740
+ # over the CLI's 8s flush deadline keeps this marker handshake inside the 55s
741
+ # hook ceiling while leaving the fallback flush and tunnel stop budget intact.
742
+ #
743
+ # This bounds only the CLI's pre-drain flush, which always runs first and
744
+ # unconditionally (run.ts's cleanup(), before the channel-work drain) — not
745
+ # the CLI's post-drain second pass, which can take up to
746
+ # SHUTDOWN_DRAIN_TIMEOUT_MS longer than this wait covers. A drain that
747
+ # consumes the whole window loses the SECOND pass, not the credential
748
+ # guarantee itself: the pre-drain flush already persisted everything on disk
749
+ # at signal time, exactly what the old bash `sync_credentials` guaranteed in
750
+ # one synchronous call — so the worst case here is no worse than before this
751
+ # handshake existed, never a fresh data-loss window. See
752
+ # docs/decisions/0063-microvm-boot-orchestration-in-cli.md's two-phase-flush
753
+ # section for the full reasoning.
754
+ CREDENTIAL_FLUSH_WAIT_SECONDS="${EVIDENT_CREDENTIAL_FLUSH_WAIT_SECONDS:-10}"
755
+
756
+ # Remove the previous answer, signal the same CLI that stop_tunnel handles, and
757
+ # wait for either its marker or its death. A fallback sync runs only after the
758
+ # CLI is known to be absent, never alongside a live CLI that may still write.
759
+ stop_runner_and_flush_credentials() {
760
+ rm -f "${CREDENTIAL_FLUSH_MARKER_FILE}"
761
+
762
+ if ! tunnel_is_running; then
763
+ warn "CREDS-FLUSH-NO-RUNNER: no live CLI at handshake entry"
764
+ stop_tunnel
765
+ sync_credentials
766
+ return 0
767
+ fi
768
+
769
+ local pid
770
+ pid="$(cat "${TUNNEL_PID_FILE}")"
771
+ kill -TERM "${pid}" 2>/dev/null || true
772
+
773
+ local waited_ms=0 marker_found=false runner_exited=false
774
+ while [ "${waited_ms}" -lt $((CREDENTIAL_FLUSH_WAIT_SECONDS * 1000)) ]; do
775
+ if [ -s "${CREDENTIAL_FLUSH_MARKER_FILE}" ]; then
776
+ marker_found=true
777
+ break
778
+ fi
779
+ if ! process_is_alive "${pid}"; then
780
+ runner_exited=true
781
+ break
782
+ fi
783
+ sleep 0.1
784
+ waited_ms=$((waited_ms + 100))
785
+ done
786
+
787
+ # The CLI can publish the marker and exit inside one poll tick. A final
788
+ # marker check after a death break preserves that answer instead of falling
789
+ # through to the fallback.
790
+ if [ "${runner_exited}" = true ] && [ -s "${CREDENTIAL_FLUSH_MARKER_FILE}" ]; then
791
+ marker_found=true
792
+ runner_exited=false
793
+ fi
794
+
795
+ if [ "${marker_found}" = true ]; then
796
+ local failures
797
+ failures="$(grep -Ev '^(claude|opencode)=ok$' "${CREDENTIAL_FLUSH_MARKER_FILE}" || true)"
798
+ if [ -n "${failures}" ]; then
799
+ warn "CREDS-FLUSH-HAD-FAILURES: ${failures//$'\n'/ }"
800
+ else
801
+ log "CREDS-FLUSH-OK"
802
+ fi
803
+ stop_tunnel
804
+ return 0
805
+ fi
806
+
807
+ if [ "${runner_exited}" = true ]; then
808
+ warn "CREDS-FLUSH-RUNNER-EXITED: CLI exited without a completion marker"
809
+ stop_tunnel
810
+ sync_credentials
811
+ return 0
812
+ fi
813
+
814
+ warn "CREDS-FLUSH-TIMEOUT: live CLI did not write a completion marker within ${CREDENTIAL_FLUSH_WAIT_SECONDS}s"
815
+ stop_tunnel
816
+ }
817
+
870
818
  # --- check_runner_key (#1172) ------------------------------------------------
871
819
  #
872
820
  # Answers exactly one question before opencode/the tunnel start spending this
873
821
  # boot's SIGTERM budget on a key that cannot work: "does the runner key in this
874
822
  # payload authenticate against Evident?" Delegates entirely to the CLI's own
875
- # `evident status --json` (apps/cli/src/commands/status.ts) rather than
823
+ # `evident status --json` rather than
876
824
  # reimplementing its auth logic here — that command's `reason` field is the
877
825
  # published contract this function reads, and its own header states the
878
826
  # absent-vs-contrary distinction this function must honour.
@@ -2,10 +2,8 @@
2
2
  #
3
3
  # Re-dial with the identity /run left behind. There is no step that could fetch
4
4
  # a fresh runner key, so the one from /run is what resumes. The CLI delegated by
5
- # start_tunnel owns the session-DB replicator's post-resume start. The interval
6
- # credential sync loop (#1868 WI-3) stays here because /suspend stops it too, so
7
- # a resumed VM that never restarted it here would never sync credentials again
8
- # for the rest of its life.
5
+ # start_tunnel owns the credential loop and the session-DB replicator's
6
+ # post-resume start.
9
7
  set -euo pipefail
10
8
 
11
9
  # shellcheck source=./common.sh
@@ -39,12 +37,18 @@ fi
39
37
  read -r runner_key
40
38
  } <"${CONTEXT_FILE}"
41
39
 
40
+ # Restore the id so the resumed CLI can self-report and acknowledge any
41
+ # outstanding recycle request (#1906). Older images may not have this file.
42
+ if [ -s "${MICROVM_ID_FILE}" ]; then
43
+ MICROVM_ID="$(cat "${MICROVM_ID_FILE}")"
44
+ export MICROVM_ID
45
+ fi
46
+
42
47
  # Tolerant, never fatal: a resume that fails costs the user their whole
43
48
  # session, so a broken durable-state config degrades to "no session-DB
44
49
  # replication" rather than a failed resume. The CLI's own guards handle the
45
50
  # rest — this needs no logic of its own.
46
51
  load_state_config || warn "could not resolve durable-state config; the session DB will not resume replicating"
47
- start_credential_sync
48
52
 
49
53
  # Diagnostic-only, unlike /run's gate: a failed resume costs the user their
50
54
  # whole session, so this never exits — it only converts a silent "resumed but
@@ -69,9 +73,8 @@ start_credential_sync
69
73
  # list, so a later edit cannot accidentally foreground the check again.
70
74
  ( check_runner_key "${runner_key}" "${api_url}" || true ) &
71
75
 
72
- # Not waited on: the CLI backs off and keeps retrying its own dial with no
73
- # attempt cap (apps/cli/src/lib/tunnel/connection.ts), and a control-plane
74
- # reclaim pass (the api-worker lifecycle cron) is what decides whether a
76
+ # Not waited on: the Evident CLI backs off and keeps retrying its own dial with no
77
+ # attempt cap, and Evident's control-plane reclaim pass is what decides whether a
75
78
  # runner that never reconnects gets suspended.
76
79
  start_tunnel "${runner_key}" "${api_url}" "${tunnel_url}"
77
80
 
@@ -28,7 +28,6 @@ cleanup() {
28
28
  stop_tunnel || warn "stop_tunnel failed while cleaning up"
29
29
  stop_opencode || warn "stop_opencode failed while cleaning up"
30
30
  kill_litestream || warn "kill_litestream failed while cleaning up"
31
- stop_credential_sync || warn "stop_credential_sync failed while cleaning up"
32
31
  }
33
32
  trap cleanup EXIT
34
33
 
@@ -73,9 +72,12 @@ load_state_config || exit 1
73
72
  # it warns and this VM still boots.
74
73
  check_runner_key "${runner_key}" "${api_url}" || exit 1
75
74
 
76
- # 6 — the interval credential sync (#1868 WI-3). It stays before the context file
77
- # is written. Bare: every guard inside start_credential_sync is its own `return 0`.
78
- start_credential_sync
75
+ # 6 — preserve the VM id across suspend/resume. The runtime always supplies it
76
+ # for a /run hook, and the subshell keeps the persisted value protected.
77
+ (
78
+ umask 077
79
+ printf '%s\n' "${MICROVM_ID}" >"${MICROVM_ID_FILE}"
80
+ )
79
81
 
80
82
  # 7 — the first per-VM identity on the wire. The subshell's umask makes the file
81
83
  # unreadable to anyone else from the moment it exists, before the key is in it.
@@ -85,9 +87,8 @@ start_credential_sync
85
87
  >"${CONTEXT_FILE}"
86
88
  )
87
89
 
88
- # Not waited on: the CLI backs off and keeps retrying its own dial with no
89
- # attempt cap (apps/cli/src/lib/tunnel/connection.ts), and a control-plane
90
- # reclaim pass (the api-worker lifecycle cron) is what decides whether a
90
+ # Not waited on: the Evident CLI backs off and keeps retrying its own dial with no
91
+ # attempt cap, and Evident's control-plane reclaim pass is what decides whether a
91
92
  # runner that never connects gets suspended.
92
93
  start_tunnel "${runner_key}" "${api_url}" "${tunnel_url}" true true
93
94
 
@@ -8,11 +8,10 @@ set -euo pipefail
8
8
  # shellcheck source=./common.sh
9
9
  source "$(dirname "$0")/common.sh"
10
10
 
11
- # Stopped BEFORE the boundary flush (#1868 WI-3): the interval loop and this
12
- # flush must not race each other on the same credential stores.
13
- stop_credential_sync
14
- sync_credentials
15
- stop_tunnel
11
+ # load_state_config exports PERSISTENCE_BUCKET, which flush_session_db below
12
+ # requires; the CLI flush completes, or is proven absent, before teardown continues.
13
+ load_state_config || warn "could not resolve durable-state config; neither the credential flush nor the session-DB flush below can run"
14
+ stop_runner_and_flush_credentials
16
15
 
17
16
  # opencode is deliberately NOT stopped here (#812 WI-4) — it is inside the
18
17
  # snapshot and must resume with it — so a turn still in flight may not be
@@ -8,11 +8,10 @@ set -uo pipefail
8
8
  # shellcheck source=./common.sh
9
9
  source "$(dirname "$0")/common.sh"
10
10
 
11
- # Stopped BEFORE the boundary flush (#1868 WI-3): the interval loop and this
12
- # flush must not race each other on the same credential stores.
13
- stop_credential_sync
14
- sync_credentials
15
- stop_tunnel
11
+ # load_state_config exports PERSISTENCE_BUCKET, which flush_session_db below
12
+ # requires; the CLI flush completes, or is proven absent, before teardown continues.
13
+ load_state_config || warn "could not resolve durable-state config; neither the credential flush nor the session-DB flush below can run"
14
+ stop_runner_and_flush_credentials
16
15
 
17
16
  # Writers stopped BEFORE litestream's final sync (#812 WI-4, the
18
17
  # ordered-shutdown invariant, plan §6): otherwise litestream could snapshot
@@ -32,6 +31,6 @@ flush_session_db
32
31
  # this hook runs under `set -u` (above), so an unbound one aborts the script
33
32
  # AT THIS LINE, leaving the runner key sitting in CONTEXT_FILE on a VM that is
34
33
  # being torn down. That is the one thing this line exists to prevent.
35
- rm -f "${CONTEXT_FILE}" "${SESSION_DB_NO_REPLICATE_MARKER}"
34
+ rm -f "${CONTEXT_FILE}" "${SESSION_DB_NO_REPLICATE_MARKER}" "${CREDENTIAL_FLUSH_MARKER_FILE}"
36
35
 
37
36
  exit 0
@@ -60,8 +60,7 @@ class EvidentWaker extends constructs_1.Construct {
60
60
  // dist/waker-lambda/handler.js, so neither this package's published npm
61
61
  // artifact (which ships dist/ already built) nor a consuming app needs
62
62
  // that private workspace package, or esbuild, on synth's PATH. Requires
63
- // a build before synth when consuming this package via `workspace:*` —
64
- // see infrastructure/evident-runner's README "local gate".
63
+ // a build before synth when consuming this package via `workspace:*`.
65
64
  //
66
65
  // Resolved from the PACKAGE ROOT (../.. ), not `__dirname` directly:
67
66
  // `__dirname` is `dist/waker` at runtime (compiled) but `src/waker` when
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evident-ai/runner-cdk",
3
- "version": "3.4.1-dev.c463782",
3
+ "version": "3.4.1-dev.d0469f4",
4
4
  "description": "Reusable CDK constructs for an Evident agent runner: a single scale-to-zero Fargate runner (task + service + per-agent self-stop role + waker Lambda), or a per-session AWS Lambda MicroVM that boots on demand and suspends between messages. Instantiate once per agent from your own stack.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -14,8 +14,8 @@
14
14
  "dist"
15
15
  ],
16
16
  "scripts": {
17
- "build": "pnpm run build-bundled-deps && ts-node scripts/build.ts",
18
- "//build-bundled-deps": "scripts/build.ts esbuild-bundles runner/docker-images/microvm/hook-server.ts, which imports @evident-ai/lambda-microvm-runtime by its built `main`. @evident-ai/lambda-microvm-cdk is a workspace:* devDependency whose types resolve through its own dist/, so tsc --project tsconfig.build.json cannot compile src/microvm/{shapes,construct}.ts without it. Chained into `build` rather than left to turbo's `^build` because publish-runner-cdk.yaml and infrastructure/evident-runner's build-runner-cdk-dep invoke `pnpm --filter @evident-ai/runner-cdk build` directly, which bypasses turbo entirely.",
17
+ "build": "pnpm run build-bundled-deps && ts-node scripts/build.ts && node -e 'const m=require(\"./package.json\").main; if (!require(\"node:fs\").existsSync(m)) { console.error(`build produced no ${m}`); process.exit(1); }'",
18
+ "//build-bundled-deps": "scripts/build.ts esbuild-bundles runner/docker-images/microvm/hook-server.ts, which imports @evident-ai/lambda-microvm-runtime by its built `main`. @evident-ai/lambda-microvm-cdk is a workspace:* devDependency whose types resolve through its own dist/, so tsc --project tsconfig.build.json cannot compile src/microvm/{shapes,construct}.ts without it. Chained into `build` rather than left to turbo's `^build` because callers outside turbo invoke `pnpm --filter @evident-ai/runner-cdk build` directly, which bypasses turbo entirely.",
19
19
  "build-bundled-deps": "pnpm --filter @evident-ai/lambda-microvm-cdk build && pnpm --filter @evident-ai/lambda-microvm-runtime build",
20
20
  "typecheck": "tsc --noEmit",
21
21
  "test": "node --test --require ts-node/register 'src/**/*.test.ts'",