@cmd/entire/cli/agent/external/capabilities.go can you explain me why we need Wrap?
- User
@cmd/entire/cli/agent/external/capabilities.go can you explain me why we need Wrap?
Assistant
The
Wrapfunction exists because of how Go interfaces work with capability-based dispatch.The problem: The
*Agentstruct implements all optional interface methods (HookSupport,TranscriptAnalyzer,TokenCalculator, etc.) since it needs to handle any capability an external binary might declare. But if you return*Agentdirectly, Go's type system sees it as satisfying every optional interface — even ones the external binary didn't declare support for.This matters because the rest of the codebase uses type assertions to check capabilities:
goif hs, ok := a.(agent.HookSupport); ok { // only called if agent supports hooks }If
*Agentwere returned directly, these checks would always succeed, regardless of what the binary actually supports.The solution:
Wrapcreates a thin wrapper struct that only forwards the interfaces matching the declared capabilities. For example:- Binary declares only
hooks→ returns*hooksAgent(satisfiesagent.Agent+HookSupportonly) - Binary declares nothing → returns
*baseAgent(satisfies onlyagent.Agent) - Binary declares everything → returns
*fullAgent(satisfies all interfaces)
Why not embedding? The comment on line 17-18 explains: Go embedding would expose all methods from
*Agent, defeating the purpose. Each wrapper must explicitly list only the methods it should expose.The trade-off is boilerplate (each wrapper re-declares its forwarding methods), but it gives correct runtime interface satisfaction that matches what the external binary actually supports.
- Binary declares only
- User
the problem though is that the Wrap command doesn't cover all the possible combinations, can we fix that in another way?
Assistant
You're right — the current switch statement only covers a few combinations, and with 6 capability flags the full combinatorial explosion would be 2^6 = 64 wrapper types, which is unmaintainable.
The clean alternative is to build the wrapper dynamically at runtime by composing interfaces. Go doesn't support this natively, but there's a well-known pattern: define a set of concrete types covering every combination you need, or — more practically — use a struct with optional interface fields and a custom type assertion helper.
However, the simplest approach that actually works in Go is: check capabilities at the call site instead of relying on type assertions.
Let me look at how the optional interfaces are actually consumed.
Assistant
Good observation. With 6 capability flags, you'd need 64 wrapper types to cover all combinations. The current code only covers 5.
There are two practical approaches:
Option A: Code generation
A
go:generatescript produces all 2^6 wrapper types mechanically. Call sites stay the same (type assertions). It's verbose but correct and zero-overhead.Option B: Replace type assertions with a capability check
Add a method to the
Agentinterface:gotype Capabilities struct { Hooks, TranscriptAnalyzer, TokenCalculator bool TextGenerator, HookResponseWriter, SubagentAwareExtractor bool } type Agent interface { // ... existing methods AgentCapabilities() Capabilities }