Skip to content

feat(cli): add debug heap-dump command - #58252

Merged
Hona merged 4 commits into
anomalyco:devfrom
Hona:heap-dump-cli
Oct 22, 2026
Merged

Hona merged 4 commits into
anomalyco:devfrom
Hona:heap-dump-cli

Conversation

@Hona

@Hona Hona commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Windows has no SIGUSR1, so there was no way to capture a heap snapshot of a running V2 background server there. This adds a CLI command that asks the server for one over its authenticated API.

$ opencode debug heap-dump
C:\Users\me\.local\share\opencode\log\heap-21852-2026-10-22T223752156Z.heapsnapshot
sequenceDiagram
  participant CLI as opencode debug heap-dump
  participant Reg as service registration
  participant S as background server
  CLI->>Reg: Service.discover (never ensure)
  CLI->>S: POST /api/debug/heap-dump (basic auth)
  S->>S: HeapSnapshot.write(global.log)<br/>one permit per process, file pre-created 0600
  S-->>CLI: { path, pid } once the file is complete
Loading
  • Only a running server. The command uses Service.discover, not ensure, so it never starts, replaces, or restarts the service. Starting a new server would only capture an empty heap, and replacing a mismatched one would destroy the leak being investigated.
  • --server <url> targets a server started with opencode serve (password from OPENCODE_PASSWORD).
  • Older servers. A 404 prints The server at <url> does not support heap dumps. Update and restart it first. instead of a stack trace.
  • One writer. packages/util/src/heap-snapshot.ts is shared by the endpoint and the existing Unix SIGUSR1 listener in packages/cli/src/heap.ts. A process-wide semaphore queues concurrent captures, so two heaps never serialize at once.
  • Private files. The snapshot is created owner-only (0600) before V8 writes into it, because it can contain prompts, file contents, and credentials.
  • Failures are logged with a ref and returned as UnknownError (500). The semaphore is released, so the next capture still works.
  • workerd. node:v8 is imported lazily inside the writer, so the workerd bundle carries no static dependency. A capture there fails as a normal UnknownError.
// packages/protocol/src/groups/debug.ts
HttpApiEndpoint.post("debug.heapDump", "/api/debug/heap-dump", {
  success: Schema.Struct({ path: Schema.String, pid: Schema.Int }),
  error: UnknownError,
})

Docs: troubleshooting.mdx gets a "Capture a heap snapshot" section, cli/commands.mdx lists the command, and the opencode-dev skill now leads with the command and keeps SIGUSR1 for non-server processes.

The OpenAPI documents include only this endpoint. The unrelated drift from dev (/api/experimental/client-mcp) is left out.


Added by the opencode bot (AI-generated).

Related: #54928

Public API: adds POST /api/debug/heap-dump, client debug.heapDump() and the opencode debug heap-dump [--server] command (backwards compatible)

Copilot AI balanced review requested due to automatic review settings October 22, 2026 22:40

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@opencode-agent opencode-agent 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.

I recommend merging this: the command works as described, and I found nothing that should block it.

I ran an opencode serve server from this branch and ran opencode debug heap-dump --server … against it. It printed the path of a complete snapshot file that only its owner can read (0600) and exited with 0. With no background server it fails without starting one. A wrong password gets a clear error. Pointed at a server from the base branch, it prints the "does not support heap dumps" message instead of a stack trace. The new server and CLI tests pass, and type checks pass for cli, server, util and protocol.

Public API changes, all additions and backwards compatible:

  • HTTP: new POST /api/debug/heap-dump (debug.heapDump) returning { path, pid }, with UnknownError (500) on failure. It is in both OpenAPI documents.
  • Client: new client.debug.heapDump() in the promise client and heapDump on the effect DebugApi.
  • CLI: new opencode debug heap-dump [--server <url>]; with --server the password comes from OPENCODE_PASSWORD.

Related: #54928

@opencode-agent opencode-agent 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.

The new commit only edits documentation: it renames opencode2 to opencode in the opencode-dev skill. That's fine, and nothing else changed since my last review, which had no open points. The public API impact is the same as before: new POST /api/debug/heap-dump, client.debug.heapDump() and opencode debug heap-dump [--server], all backwards compatible.

@opencode-agent opencode-agent 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.

The two new commits are fine. One adds heapDump to the expected client.debug methods in the client test, which passes. The other refreshes the opencode-dev skill; the statements I spot-checked match the code (bun dev entrypoint, GET /api/info, dev:live). My last review had no open points. The public API impact is the same: new POST /api/debug/heap-dump, client.debug.heapDump() and opencode debug heap-dump [--server], all backwards compatible.

@Hona
Hona merged commit 6102079 into anomalyco:dev Oct 22, 2026
19 checks passed
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.

2 participants