Skip to content

feat(gateway): add restart trace instrumentation#82396

Merged
steipete merged 3 commits into
openclaw:mainfrom
samzong:gw-restart-trace-bench
May 16, 2026
Merged

feat(gateway): add restart trace instrumentation#82396
steipete merged 3 commits into
openclaw:mainfrom
samzong:gw-restart-trace-bench

Conversation

@samzong

@samzong samzong commented May 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Gateway restarts had no restart-scoped trace that showed where time was spent across drain, shutdown, next start, and ready phases.
  • Why it matters: Slow or degraded restarts were hard to diagnose from logs because close-path and startup timings were split across generic shutdown/startup output.
  • What changed: Add OPENCLAW_GATEWAY_RESTART_TRACE support for parseable restart trace lines, covering restart signal, active-work drain, close-path phases, next start, ready spans, and memory metrics.
  • What did NOT change (scope boundary): This does not add a standalone benchmark script, change restart budgets, change auth/config behavior, or change the gateway protocol.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes N/A
  • Related N/A
  • This PR fixes a bug or regression

Real behavior proof (required for external PRs)

Behavior addressed: With OPENCLAW_GATEWAY_RESTART_TRACE=1, a real dev gateway restart now emits parseable restart trace spans and still recovers /healthz and /readyz.

Real environment tested: Local macOS checkout on branch gw-restart-trace-bench, commit 8129cdd78a, Node v24.14.0, built dist/entry.js from this patch, dev gateway on loopback port 19984.

Exact steps or command run after this patch: Started node dist/entry.js --dev gateway run --auth none --bind loopback --port 19984 --force --allow-unconfigured --verbose with OPENCLAW_GATEWAY_RESTART_TRACE=1 OPENCLAW_NO_RESPAWN=1 FORCE_COLOR=0, probed /healthz and /readyz, sent SIGUSR1, waited for restart trace lines, then probed /healthz and /readyz again.

Evidence after fix:

{
  "initialHealthz": 200,
  "initialReadyz": 200,
  "postRestartHealthz": 200,
  "postRestartReadyz": 200,
  "startupMs": 2342,
  "signalLine": "restart trace: restart.signal.received 0.0ms total=0.0ms signal=SIGUSR1 reason=SIGUSR1 force=false waitMs=default",
  "closeLine": "restart trace: restart.close.total 58.0ms total=67.0ms reason=gateway_restarting restartExpectedMs=1500.0 rssMb=648.4 heapTotalMb=441.8 heapUsedMb=332.9 externalMb=7.1 arrayBuffersMb=0.5",
  "nextStartLine": "restart trace: restart.next-start 2.3ms total=69.3ms",
  "readyLine": "restart trace: restart.ready 362.1ms total=431.3ms rssMb=653.1 heapTotalMb=452.8 heapUsedMb=358.7 externalMb=7.1 arrayBuffersMb=0.5"
}

Observed result after fix: The gateway returned HTTP 200 before and after restart, emitted restart signal, close total, next-start, and ready trace lines, and shut down cleanly after the proof run.

What was not tested: Full-process respawn trace output, Linux/WSL2 runtime proof, and broad Testbox/Crabbox gates.

Root Cause (if applicable)

For bug fixes or regressions, explain why this happened, not just what changed. Otherwise write N/A. If the cause is unclear, write Unknown.

  • Root cause: N/A. This is an observability feature, not a bug/regression fix.
  • Missing detection / guardrail: Restart trace output did not previously have focused tests for parseable close and ready spans.
  • Contributing context (if known): Existing startup trace and shutdown logs exposed related facts, but not as one restart-scoped timeline.

Regression Test Plan (if applicable)

For bug fixes or regressions, name the smallest reliable test coverage that should catch this. Otherwise write N/A.

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/gateway/server-close.test.ts, with existing restart path coverage in src/cli/gateway-cli/run-loop.test.ts.
  • Scenario the test should lock in: Enabling OPENCLAW_GATEWAY_RESTART_TRACE emits parseable close-path spans and ready child spans without shortening the parent ready span.
  • Why this is the smallest reliable guardrail: The formatter and close/ready trace hooks can be verified without starting every channel/plugin runtime.
  • Existing test that already covers this (if any): Existing run-loop tests cover restart drain, restart intent, SIGUSR1, and respawn behavior.
  • If no new test is added, why not: N/A.

User-visible / Behavior Changes

When OPENCLAW_GATEWAY_RESTART_TRACE=1 is set, gateway restart logs include parseable restart trace: lines for drain, close, next-start, and ready timing. Default logging is unchanged when the env var is unset.

Diagram (if applicable)

Before:
SIGUSR1 restart -> drain/close/startup logs split across generic lifecycle output

After:
SIGUSR1 restart -> restart.signal -> restart.drain -> restart.close.* -> restart.next-start -> restart.ready -> health/ready recovered

Security Impact (required)

  • New permissions/capabilities? (Yes/No) No
  • Secrets/tokens handling changed? (Yes/No) No
  • New/changed network calls? (Yes/No) No
  • Command/tool execution surface changed? (Yes/No) No
  • Data access scope changed? (Yes/No) No
  • If any Yes, explain risk + mitigation: N/A

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: Node v24.14.0, local dist/entry.js
  • Model/provider: N/A
  • Integration/channel (if any): None; dev gateway only
  • Relevant config (redacted): --dev --auth none --bind loopback --port 19984 --allow-unconfigured, OPENCLAW_GATEWAY_RESTART_TRACE=1, OPENCLAW_NO_RESPAWN=1

Steps

  1. Run node scripts/run-vitest.mjs src/gateway/server-close.test.ts src/cli/gateway-cli/run-loop.test.ts.
  2. Start the dev gateway from dist/entry.js with restart tracing enabled.
  3. Probe /healthz and /readyz.
  4. Send SIGUSR1.
  5. Confirm restart trace lines and probe /healthz and /readyz again.

Expected

  • Targeted tests pass.
  • Gateway stays recoverable after restart.
  • Restart trace logs include signal, close total, next-start, and ready timing.

Actual

  • node scripts/run-vitest.mjs src/gateway/server-close.test.ts src/cli/gateway-cli/run-loop.test.ts: passed, 2 files / 45 tests.
  • Runtime proof returned HTTP 200 for initial and post-restart /healthz and /readyz.
  • Runtime proof emitted parseable restart trace lines shown above.

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: Targeted restart tests passed; local dev gateway restarted in-process with trace enabled and recovered health/readiness.
  • Edge cases checked: Close-path trace span format, ready child span format, parent ready span duration, memory metric formatting.
  • What you did not verify: Full-process respawn, Linux/WSL2, broad Testbox/Crabbox, and production managed service restart.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.

Compatibility / Migration

  • Backward compatible? (Yes/No) Yes
  • Config/env changes? (Yes/No) Yes, optional OPENCLAW_GATEWAY_RESTART_TRACE=1 enables restart trace logging.
  • Migration needed? (Yes/No) No
  • If yes, exact upgrade steps: N/A

Risks and Mitigations

  • Risk: Wrapping shutdown phases for measurement could accidentally perturb close ordering.
    • Mitigation: The implementation preserves the existing close sequence and targeted shutdown/restart tests passed.
  • Risk: Trace output could become noisy when enabled.
    • Mitigation: It is behind OPENCLAW_GATEWAY_RESTART_TRACE; default behavior is unchanged.

@github-actions github-actions Bot added the dependencies-changed PR changes dependency-related files label May 16, 2026
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes scripts Repository scripts size: XL triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 16, 2026
@samzong
samzong marked this pull request as draft May 16, 2026 02:29
@clawsweeper

clawsweeper Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Summary
The PR adds opt-in Gateway restart trace logging, respawn handoff/resume support, focused tests, docs, and a changelog entry.

Reproducibility: not applicable. this is a new Gateway observability feature, not a broken existing behavior report. The behavior to validate is current-head trace output during restart, and the supplied runtime proof is for an earlier head.

Real behavior proof
Needs stronger real behavior proof before merge: Terminal proof covers commit 8129cdd78a, but latest head a267bc5ebeb054459cbb37b226133536cde06d10 adds runtime respawn-handoff changes; add redacted current-head proof to the PR body for re-review. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, ask a maintainer to comment @clawsweeper re-review.

Next step before merge
This is a new core Gateway diagnostic env var with stale current-head proof, so the next step is owner review and validation rather than an automated repair job.

Security
Cleared: No concrete security or supply-chain regression was found in the Gateway/CLI instrumentation, docs, and tests reviewed.

Review details

Best possible solution:

If Gateway owners accept the diagnostic surface, land the opt-in trace with the existing docs/tests plus current-head real gateway proof that covers the claimed restart modes or narrows the documented claim.

Do we have a high-confidence way to reproduce the issue?

Not applicable: this is a new Gateway observability feature, not a broken existing behavior report. The behavior to validate is current-head trace output during restart, and the supplied runtime proof is for an earlier head.

Is this the best way to solve the issue?

Unclear: the opt-in trace approach matches the existing startup profiling style and keeps default logging unchanged, but the new core env-var contract and respawn-handoff scope need Gateway owner acceptance and current-head proof.

Acceptance criteria:

  • node scripts/run-vitest.mjs src/gateway/server-close.test.ts src/gateway/restart-trace.test.ts src/infra/restart-handoff.test.ts src/cli/gateway-cli/run-loop.test.ts
  • Run a real Gateway restart proof on latest head with OPENCLAW_GATEWAY_RESTART_TRACE=1, showing /healthz and /readyz recover and trace lines are emitted.
  • If respawn-handoff trace output remains in scope, include a full-process or supervised respawn proof, or explicitly limit the documented behavior.

What I checked:

  • Current main lacks the feature: A current-main search found no OPENCLAW_GATEWAY_RESTART_TRACE or restart trace: implementation, so the central change is not already implemented on main. (68a4c77f5bb1)
  • Opt-in trace helper: The PR head adds a restart-trace helper that only activates when OPENCLAW_GATEWAY_RESTART_TRACE is truthy and emits parseable restart trace: lines. (src/gateway/restart-trace.ts:100, a267bc5ebeb0)
  • Restart loop instrumentation: The PR head starts tracing when restart signals are accepted and measures active-work drain before server close. (src/cli/gateway-cli/run-loop.ts:330, a267bc5ebeb0)
  • Respawn handoff support: The latest PR head carries restart trace timing through spawned and supervised restarts and normalizes persisted trace timing in restart handoff payloads. (src/cli/gateway-cli/run-loop.ts:237, a267bc5ebeb0)
  • Documented user surface: The PR documents the new OPENCLAW_GATEWAY_RESTART_TRACE=1 profiling knob and adds a changelog entry for the user-visible Gateway logging feature. Public docs: docs/cli/gateway.md. (docs/cli/gateway.md:127, a267bc5ebeb0)
  • Focused test coverage: The PR adds tests for parseable close trace spans, ready child span duration handling, respawn env handoff, and restart handoff timing persistence. (src/gateway/server-close.test.ts:184, a267bc5ebeb0)

Likely related people:

  • steipete: Shortlog history makes Peter Steinberger the dominant contributor across the touched Gateway/CLI/docs files, and the PR context shows the latest docs and respawn-handoff commits on this branch came from him. (role: recent gateway lifecycle and diagnostics contributor; confidence: high; commits: a2661f5aa301, a267bc5ebeb0, d16efadc00d4; files: src/gateway/server.impl.ts, src/gateway/server-close.ts, src/cli/gateway-cli/run-loop.ts)
  • vincentkoc: Git history includes Vincent Koc commits for gateway restart false-timeout handling, restart health changelog work, and unmanaged listener restart behavior in the same operational area. (role: restart health and run-loop contributor; confidence: medium; commits: bf9c362129e2, 2b98cb6d8bfc, 9c6847074d77; files: src/cli/gateway-cli/run-loop.ts, src/infra/process-respawn.ts, src/gateway/server.impl.ts)
  • Takhoffman: Local shortlog and topic history show repeated touches around Gateway lifecycle, restart/startup probes, and shutdown behavior adjacent to this instrumentation. (role: gateway lifecycle robustness contributor; confidence: medium; commits: 1be39d4250e3, ec122796f864, 8873e13f1e6f; files: src/gateway/server.impl.ts, src/gateway/server-close.ts, src/cli/gateway-cli/run-loop.ts)

Remaining risk / open question:

  • The PR body's real runtime proof covers commit 8129cdd78a, not the latest head a267bc5ebeb054459cbb37b226133536cde06d10 after respawn-handoff changes.
  • Full-process respawn trace output and Linux/WSL2 behavior remain unverified in the supplied proof.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 862be9fb3d6c.

@samzong
samzong force-pushed the gw-restart-trace-bench branch from d69be66 to 3301135 Compare May 16, 2026 07:06
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 16, 2026
@samzong
samzong marked this pull request as ready for review May 16, 2026 07:09
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 16, 2026
@samzong

samzong commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@clawsweeper clawsweeper Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 16, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3301135b82

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/bench-gateway-restart.ts Outdated
@samzong
samzong force-pushed the gw-restart-trace-bench branch from 3301135 to 47e3d40 Compare May 16, 2026 07:45
@samzong

samzong commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 16, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@samzong
samzong force-pushed the gw-restart-trace-bench branch from 47e3d40 to 8129cdd Compare May 16, 2026 11:40
@github-actions github-actions Bot removed the dependencies-changed PR changes dependency-related files label May 16, 2026
@openclaw-barnacle openclaw-barnacle Bot added dependencies-changed PR changes dependency-related files size: L and removed scripts Repository scripts size: XL labels May 16, 2026
@clawsweeper clawsweeper Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 16, 2026
@samzong samzong changed the title feat(gateway): add restart benchmark and close-path trace instrumentation feat(gateway): add restart trace instrumentation May 16, 2026
@samzong

samzong commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review
@codex review

galiniliev pushed a commit to galiniliev/openclaw that referenced this pull request May 20, 2026
galiniliev pushed a commit to galiniliev/openclaw that referenced this pull request May 20, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 24, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 24, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 24, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 24, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 24, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 24, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
galiniliev pushed a commit to galiniliev/openclaw that referenced this pull request May 25, 2026
galiniliev pushed a commit to galiniliev/openclaw that referenced this pull request May 25, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 26, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 26, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 26, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 26, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 26, 2026
SebTardif pushed a commit to SebTardif/openclaw that referenced this pull request May 26, 2026
qiaokuan1992 pushed a commit to qiaokuan1992/openclaw that referenced this pull request Jun 2, 2026
qiaokuan1992 pushed a commit to qiaokuan1992/openclaw that referenced this pull request Jun 2, 2026
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
SYU8384 pushed a commit to SYU8384/openclaw that referenced this pull request Jun 3, 2026
SYU8384 pushed a commit to SYU8384/openclaw that referenced this pull request Jun 3, 2026
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes dependencies-changed PR changes dependency-related files docs Improvements or additions to documentation gateway Gateway runtime proof: supplied External PR includes structured after-fix real behavior proof. size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants