Skip to content

Add port discovery for automatic daemon detection#13

Open
Nic-dorman wants to merge 4 commits intomainfrom
feat/port-discovery
Open

Add port discovery for automatic daemon detection#13
Nic-dorman wants to merge 4 commits intomainfrom
feat/port-discovery

Conversation

@Nic-dorman
Copy link
Collaborator

Summary

  • antd daemon (Rust): Writes a daemon.port file on startup (atomic write via temp+rename), cleans up on shutdown. Adds --rest-port / --grpc-port CLI flags supporting --rest-port 0 for OS-assigned dynamic ports. Pre-binds both REST and gRPC listeners to capture actual ports before serving.
  • All 15 SDKs: New discover module + auto-discover constructors for REST and gRPC clients. Reads the port file from the platform-specific data directory (%APPDATA%\ant\ on Windows, ~/.local/share/ant/ on Linux, ~/Library/Application Support/ant/ on macOS).
  • Default REST port corrected from 8080 to 8082 across all SDKs, docs, and tooling.
  • ant-dev CLI: status, wallet, and start commands now use port-file discovery.
  • Docs: README, llms.txt, llms-full.txt, skill.md, and MCP README updated with discovery documentation.

Port file format is two lines (REST port, gRPC port). Single-line files are forward-compatible (gRPC falls back to default 50051).

Enables managed mode for consumers like indelible that spawn antd as a child process with a dynamic port.

Test plan

  • cargo check passes for antd (Rust)
  • go test ./... passes for antd-go (8 discovery tests + existing suite)
  • Python discovery tests pass (20 tests via pytest)
  • Rust SDK unit tests pass (5 discovery tests)
  • Manual: start antd, verify daemon.port file written
  • Manual: start antd with --rest-port 0, verify auto-discover connects
  • Manual: stop antd, verify port file cleaned up

🤖 Generated with Claude Code

Nic-dorman and others added 4 commits March 25, 2026 11:09
antd now writes a daemon.port file on startup containing the REST and
gRPC ports, enabling all SDK clients to auto-discover the daemon without
hardcoded URLs. This supports managed mode where antd is spawned with
--rest-port 0 for OS-assigned ports.

Changes:
- antd (Rust): port file lifecycle (atomic write/cleanup), --rest-port
  and --grpc-port CLI flags, pre-bound listeners for both servers
- All 15 SDKs: discover module + auto-discover constructors for REST
  and gRPC clients
- Default REST port corrected from 8080 to 8082 across all SDKs
- ant-dev CLI: status/wallet/start commands use port-file discovery
- Docs: README, llms.txt, llms-full.txt, skill.md updated

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Windows: remove target before rename (fs::rename fails if target
  exists on Windows, unlike Unix atomic replace)
- Stale port file detection: antd now writes its PID as line 3 of the
  port file. Go SDK checks if the process is still alive before trusting
  the discovered ports (platform-specific: signal 0 on Unix,
  FindProcess on Windows)
- Split Go processAlive into discover_unix.go / discover_windows.go
  with build tags for clean cross-compilation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
antd now writes its PID as line 3 of the port file. All SDKs validate
the PID before trusting discovered ports, preventing connections to
dead daemons after crashes.

Platform-specific approaches per language:
- Go: signal 0 (Unix), FindProcess (Windows) via build tags
- Python/MCP: os.kill(pid, 0) — cross-platform on Python 3
- Rust: kill -0 (Unix), tasklist (Windows) via cfg
- Java/Kotlin: ProcessHandle.of(pid).isPresent() — cross-platform
- C#: Process.GetProcessById — cross-platform on .NET 8+
- TypeScript: process.kill(pid, 0) — cross-platform in Node.js
- C++: kill(pid, 0) (Unix), OpenProcess (Windows) via ifdef
- Ruby: Process.kill(0, pid) — cross-platform
- Elixir: System.cmd("kill", ["-0", pid]) on Unix, trust on Windows
- Swift: kill(pid_t, 0) via C interop
- Dart: kill -0 on Unix, trust on Windows
- Lua: os.execute("kill -0") on Unix, trust on Windows
- PHP: posix_kill or /proc check on Unix, tasklist on Windows
- Zig: /proc check on Linux, trust on other platforms

All backward-compatible with 2-line port files (no PID).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
CORS: Restrict allowed origin to http://127.0.0.1:{port} instead of
permissive Any. Prevents cross-origin CSRF from malicious webpages.
Non-browser clients (SDKs, CLI, AI agents) are unaffected as they
don't send Origin headers. Matches approach from ant-client.

Wallet: Add GET /v1/wallet/address and GET /v1/wallet/balance REST
endpoints to antd. Returns 400 if no EVM wallet is configured.

Also adds NotImplemented error variant (HTTP 501 / gRPC UNIMPLEMENTED)
for stubbed endpoints.

SDK bindings added for wallet_address() and wallet_balance() across
all 15 language SDKs + MCP server (2 new MCP tools).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant