Files
archie/modules/connectors/connector_test.go
T
sam cf249d7643 feat(connectors): MCP servers as a fourth accepted package, with their declared tools offered to agents (archie #2005)
An investigation needs Prometheus's own metrics, and Prometheus publishes an
MCP server, so the connectors module now runs MCP servers as published instead
of wrapping them: a stdio binary in its own user and network namespace with an
egress allow-list, or an HTTP endpoint dialled through declared egress.

Acceptance names the transport, the tools it permits, and the credential slots
that resolve into the server's environment (stdio) or Authorization header
(HTTP). Each permitted tool is declared as a lookup, or as an action with its
effect; a tool the server lists but the acceptance doesn't name is never
offered, and a declared tool the server doesn't list fails the start rather
than going silently missing. Declared tools reach agents beside connector
lookups through the same declaration and the same call path, with the server's
own input schema, under a name no connector can take: an accepted name runs one
package.

A server that fails to start, exits or stops answering is reported by readiness
and reconnected with backoff. Tool calls carry a deadline, results are capped,
and an error result fails its call with the server's own text rather than the
run.

The investigate prompt now asks for a time-until-full figure from
`predict_linear` through the metrics server instead of an estimate from raw
points.

The old core MCP path stays for now: the legacy gateway and task containers
still read config.Tools.MCPServers, so it goes with them.
2026-11-15 23:43:39 +11:00

97 lines
3.9 KiB
Go

package connectors
import (
"context"
"encoding/json/jsontext"
"errors"
"testing"
"google.golang.org/grpc"
pb "github.com/samcharles93/archie-core/internal/contracts/connector/v1"
)
// fakeClient is a connector that declares one action and answers Invoke as
// told, so the host's checks can be driven directly.
type fakeClient struct {
pb.ConnectorServiceClient
readOnly bool
idempotent bool
// executed overrides the digest the connector reports; empty echoes the
// host's digest.
executed string
unknown bool
invoked int
}
func (f *fakeClient) Configure(context.Context, *pb.ConfigureRequest, ...grpc.CallOption) (*pb.ConfigureResponse, error) {
return &pb.ConfigureResponse{}, nil
}
func (f *fakeClient) Describe(context.Context, *pb.DescribeRequest, ...grpc.CallOption) (*pb.DescribeResponse, error) {
return &pb.DescribeResponse{Contract: contractVersion, Actions: []*pb.ActionType{{
Name: "delete",
InputSchema: []byte(`{"type":"object","required":["path"],"properties":{"path":{"type":"string"}}}`),
Effect: &pb.Effect{ReadOnly: f.readOnly, Reversibility: pb.Reversibility_REVERSIBILITY_RECOVERABLE},
Idempotent: f.idempotent,
}}}, nil
}
func (f *fakeClient) Invoke(_ context.Context, req *pb.InvokeRequest, _ ...grpc.CallOption) (*pb.InvokeResponse, error) {
f.invoked++
executed := req.GetDigest()
if f.executed != "" {
executed = f.executed
}
return &pb.InvokeResponse{Output: []byte(`{"deleted":3}`), ExecutedDigest: executed, OutcomeUnknown: f.unknown}, nil
}
// TestInvokeRunsOnlyTheJudgedAction guards R18 and 2.5's checks at the
// connector boundary: input that breaks the declared schema never reaches the
// connector, an action with external effects is refused unless the connector
// honours idempotency keys, a connector that ran something other than the
// judged action is caught by its digest, and an unconfirmed outcome is
// reported as unknown rather than success.
func TestInvokeRunsOnlyTheJudgedAction(t *testing.T) {
good := jsontext.Value(`{"path":"/tmp/runner"}`)
tests := []struct {
name string
client *fakeClient
input jsontext.Value
wantErr error
wantAnyErr bool
wantInvoked int
}{
{name: "matching digest returns the output", client: &fakeClient{idempotent: true}, input: good, wantInvoked: 1},
{name: "a different executed digest is refused", client: &fakeClient{idempotent: true, executed: "other"}, input: good, wantErr: ErrDigestMismatch, wantInvoked: 1},
{name: "external effects without idempotency are refused before running", client: &fakeClient{}, input: good, wantErr: ErrNotIdempotent},
{name: "read-only actions need no idempotency", client: &fakeClient{readOnly: true}, input: good, wantInvoked: 1},
{name: "an unconfirmed outcome is unknown", client: &fakeClient{idempotent: true, unknown: true}, input: good, wantErr: ErrOutcomeUnknown, wantInvoked: 1},
{name: "input breaking the schema never reaches the connector", client: &fakeClient{idempotent: true}, input: jsontext.Value(`{"path":7}`), wantAnyErr: true, wantInvoked: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := open(t.Context(), "fake", tt.client, nil, nil)
if err != nil {
t.Fatalf("open: %v", err)
}
out, err := c.Invoke(t.Context(), Invocation{Action: "delete", Input: tt.input, IdempotencyKey: "run:1"})
wantOK := tt.wantErr == nil && !tt.wantAnyErr
switch {
case wantOK && err != nil:
t.Fatalf("Invoke: %v", err)
case !wantOK && err == nil:
t.Fatal("Invoke succeeded, want an error")
case tt.wantErr != nil && !errors.Is(err, tt.wantErr):
t.Fatalf("Invoke error = %v, want %v", err, tt.wantErr)
}
if wantOK && out != `{"deleted":3}` {
t.Fatalf("output = %q, want the connector's own %q", out, `{"deleted":3}`)
}
if tt.client.invoked != tt.wantInvoked {
t.Fatalf("connector invoked %d times, want %d", tt.client.invoked, tt.wantInvoked)
}
})
}
}