Skip to content

# perf(devshard): port escrow runtime lifecycle + gateway memory optimizations from dl/gateway-v2-mem#1402

Open
libermans wants to merge 5 commits into
gm/gateway-v2-devshard-v2-poc-observabilityfrom
dl/gateway-v2-devshard-v2-mem
Open

# perf(devshard): port escrow runtime lifecycle + gateway memory optimizations from dl/gateway-v2-mem#1402
libermans wants to merge 5 commits into
gm/gateway-v2-devshard-v2-poc-observabilityfrom
dl/gateway-v2-devshard-v2-mem

Conversation

@libermans

Copy link
Copy Markdown
Contributor

Summary

Cherry-picks three commits from dl/gateway-v2-mem (#1399) to reduce devshardctl gateway RSS, adapted where this branch's architecture diverges:

  • feat(devshard): settle depleted escrow after in-flight requests drain (feat(devshard): settle depleted escrow after in-flight requests drain #1314, clean pick) — depleted escrows are marked settlement-pending and settled once the last active request drains; pending settlements are reconciled on restart.
  • fix(devshard): retire runtime escrow (fix(devshard): retire runtime escrow #1349, clean pick) — deactivation paths drop the runtime from memory drain-safely, releasing its state machine, inference map, and SQLite handles.
  • perf(devshard): shrink gateway memory footprint and add profiling (from perf(devshard): shrink gateway memory footprint and add profiling #1399, conflicts resolved) — boot loads only active devshards; non-resident escrows are served via transient rehydration (read-only for debug/status, full for settlement); /v1/state becomes a bounded summary with the inference dump moved to /v1/debug/inferences; adds /v1/debug/memstats + admin-gated pprof; fixes the unbounded chat response cache (periodic expiry sweep + total-bytes cap, DEVSHARD_CHAT_CACHE_MAX_BYTES).

Adaptations for this branch

The memory commit predates this branch's sealed-inference architecture (live records are evicted to SQLite on seal), so it was not applied verbatim:

  • SnapshotStateNoInferences referenced RevealedSeeds, a field that doesn't exist in this branch's EscrowState — auto-merged cleanly but wouldn't compile; removed. Its shallow copy shares SealedAcc, which is safe (only ever replaced wholesale, never mutated).
  • /v1/debug/inferences uses sealed-aware ExportAllInferenceRecords instead of the PR's live-only SnapshotInferences, so sealed records still appear in the dump.
  • /v1/debug/state keeps this branch's phase field and live/sealed count split while adopting the new no-deep-copy accessors (InferenceStatusCounts, LatestNonce, Balance).
  • Dropped the PR's handleInference (X-Inference-Id) handler — this branch removed that endpoint and nothing routes to it.
  • Test adaptations: session_close_test.go and flush_snapshot_test.go updated to this branch's storage.Storage interface and test helpers (newTestStateMachine, RuntimeTestVersion); TestHandleState_IncludesSealedInferences now asserts sealed records via /v1/debug/inferences and that /v1/state stays bounded.

Breaking change

/v1/state no longer includes the inferences map — consumers must read /v1/debug/inferences (admin/debug, unpaginated).

Not included

  • b87e51b (suspicious-host tracking) — this branch already carries a diverged version of that work.
  • The four intermediate commits (MiniMax route, param clamping) — independent, no dependency from the picks (verified: only shared file is escrow_rotator.go, disjoint functions).
  • Known pre-existing gap flagged in perf(devshard): shrink gateway memory footprint and add profiling #1399 review: handleAdminSettleDevshard doesn't clear SettlementPending on success, so an admin-settled escrow is re-broadcast on every restart. Worth a follow-up one-liner.

Testing

  • Full go test ./... passes (all packages, including the picks' new tests: gateway_inactive_access_test.go, flush_snapshot_test.go, session_close_test.go).
  • go vet ./... clean.

qdanik and others added 4 commits July 4, 2026 21:21
…#1314)

* feat(devshard): settle depleted escrow when last active request drains

* fix(devshard): skip settlement reconcile on restart when settlement disabled
* fix(devshard): retire runtime escrow

* review(devshard): copilot suggestions & todo for future task implementation

* fix(devshard): retire runtime escrow when in-fligh request completed
Reduce devshardctl gateway RSS by not holding inactive escrows in memory,
bounding the chat response cache, and avoiding full inference-map deep
copies on the common read paths. Adds pprof/memstats for diagnosis.
Runtime lifecycle:
- Boot loads only devshards marked active; inactive ones are skipped instead
  of being hydrated (and re-queried on chain) at startup.
- Non-resident devshards are served on demand: read-only debug/status routes
  hydrate a transient local-SQLite-only runtime (no chain/host clients), and
  settlement rehydrates a transient full runtime; both are released right
  after use.
- All disable paths (admin deactivate, rotation-without-settle, settle) now
  retire the runtime from memory drain-safely, only after in-flight requests
  complete.
State accessors (avoid copying the full inference map):
- Add StateMachine.Config, SnapshotStateNoInferences, SnapshotInferences, and
  InferenceStatusCounts; use them at status/config/single-inference call
  sites instead of SnapshotState.
Endpoints:
- /v1/state is now summary-only; the full inference dump moves to a new
  admin/debug endpoint /v1/debug/inferences (no pagination).
- /v1/debug/state reports inference status counts; single-inference lookup
  uses GetInference.
- Add /v1/debug/memstats and register net/http/pprof under /debug/pprof/,
  both gated behind admin auth.
Chat response cache:
- Fix unbounded growth: entries keyed by request-body hash were only expired
  lazily on lookup, so unique requests lived until restart. Add a periodic
  expiry sweep and a total-bytes cap (DEVSHARD_CHAT_CACHE_MAX_BYTES).
@a-kuprin

a-kuprin commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

PR mentions #1399
Let's close this known gap for handleAdminSettleDevshard
Suggestion here: https://github.com/gonka-ai/gonka/pull/1399/changes#r3533876458

Comment thread devshard/state/machine.go Outdated
Comment thread devshard/cmd/devshardctl/gateway_store.go Outdated
Centralize settlement-pending cleanup in settleDevshardOnChain so admin
settles clear the flag and avoid re-broadcast on restart. Remove unused
SnapshotInferences and propagate SQLite upsert lookup errors except
sql.ErrNoRows.
@a-kuprin

a-kuprin commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Known pre-existing gap flagged in #1399 review: handleAdminSettleDevshard doesn't clear SettlementPending on success, so an admin-settled escrow is re-broadcast on every restart. Worth a follow-up one-liner.

Now it is included

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants