core: a client owns the protocols it created, and its teardown is scoped to them
Follow-up to #52, raised by cubic on the release PR #53 and sharpened by three findings here.
The gap
The client never recorded which protocol instances it created. So its cleanup could neither reach instances a constructor-time failure left behind, nor leave alone the shared instances every other client is using — and create() rejecting meant the caller had no client through which to close anything.
The mechanism
The client records the instances it makes from factories as owned, and every teardown is scoped to those.
- The constructor no longer instantiates factories — a constructor can't await, so nothing that needs closing on failure is created there.
create()adopts them one by one inside its cleanup guard, so a factory that throws part-way leaves the ones before it closable.- On any initialization failure
create()closes what the client owns and rethrows. A cleanup that fails is reported (console.error), with the initialization failure kept as the error the caller sees. close()is scoped the same way — it closes what the client owns and leaves shared instances to the process.
Behaviour change in close()
Before, client.close() closed every registered protocol, shared ones included. Concretely: one client's close() cleared @utcp/http's process-wide OAuth token cache and wiped @utcp/direct-call's decorator registry for every other client in the process — exactly the cross-client damage per-client instances exist to prevent. Shared instances now live as long as the process that registered them; the README's Cleanup section says so. Nothing in-repo asserted the old behaviour.
CodeModeUtcpClient is unaffected: it builds through UtcpClient.create, never the protected constructor.
Tests
Five, in the existing save/restore-the-registry style: a factory instance for a create() that rejects is closed; a factory that throws leaves the earlier instances closed; a shared instance survives a failed create(); a failing cleanup is reported and the original error surfaces; a shared instance survives a finished client's close(). Every guard mutation-checked — each mutation fails exactly its test. Client suite 17/17, MCP suites 78/78.
Merging this into dev updates #53 (dev → main) automatically.
🤖 Generated with Claude Code