From 6fa7c153989a7a41442d5b7496dfe7067da70a9f Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 20 Oct 2026 07:10:58 +1100 Subject: [PATCH] fix(staterpc): carry each model provider's credential reference The State Store wire described a provider's class, base URL and key variable but never where its key lives, so every provider reached the daemon credential-less. A task then built its runtime with no key: the egress session had nothing to bind and the agent parked on 'APIKey is required'. Carry api_key_engine and api_key_key alongside the provider. They are a reference, not a secret; the key is still resolved daemon-side and never travels to a container. --- internal/contracts/state/v1/state.pb.go | 33 ++++++++++++--- internal/infrastructure/staterpc/client.go | 2 +- .../staterpc/model_provider_wire.go | 29 +++++++++++++ .../staterpc/model_provider_wire_test.go | 42 +++++++++++++++++++ internal/infrastructure/staterpc/server.go | 2 +- proto/state/v1/state.proto | 5 +++ 6 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 internal/infrastructure/staterpc/model_provider_wire.go create mode 100644 internal/infrastructure/staterpc/model_provider_wire_test.go diff --git a/internal/contracts/state/v1/state.pb.go b/internal/contracts/state/v1/state.pb.go index 1bcc85838..fd60f3547 100644 --- a/internal/contracts/state/v1/state.pb.go +++ b/internal/contracts/state/v1/state.pb.go @@ -16616,10 +16616,15 @@ func (x *TaskModelsResponse) GetProviders() map[string]*ModelProvider { // ModelProvider is an org's own provider. Its key is the org's credential // binding named after it. type ModelProvider struct { - state protoimpl.MessageState `protogen:"open.v1"` - Class string `protobuf:"bytes,1,opt,name=class,proto3" json:"class,omitempty"` - BaseUrl string `protobuf:"bytes,2,opt,name=base_url,json=baseUrl,proto3" json:"base_url,omitempty"` - ApiKeyEnv string `protobuf:"bytes,3,opt,name=api_key_env,json=apiKeyEnv,proto3" json:"api_key_env,omitempty"` + state protoimpl.MessageState `protogen:"open.v1"` + Class string `protobuf:"bytes,1,opt,name=class,proto3" json:"class,omitempty"` + BaseUrl string `protobuf:"bytes,2,opt,name=base_url,json=baseUrl,proto3" json:"base_url,omitempty"` + ApiKeyEnv string `protobuf:"bytes,3,opt,name=api_key_env,json=apiKeyEnv,proto3" json:"api_key_env,omitempty"` + // The provider's credential reference: where its key lives, never the key + // itself. A daemon resolves a model egress credential from it; without it + // every provider arrives credential-less and no runtime can be built. + ApiKeyEngine string `protobuf:"bytes,4,opt,name=api_key_engine,json=apiKeyEngine,proto3" json:"api_key_engine,omitempty"` + ApiKeyKey string `protobuf:"bytes,5,opt,name=api_key_key,json=apiKeyKey,proto3" json:"api_key_key,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -16675,6 +16680,20 @@ func (x *ModelProvider) GetApiKeyEnv() string { return "" } +func (x *ModelProvider) GetApiKeyEngine() string { + if x != nil { + return x.ApiKeyEngine + } + return "" +} + +func (x *ModelProvider) GetApiKeyKey() string { + if x != nil { + return x.ApiKeyKey + } + return "" +} + type CallerModelsRequest struct { state protoimpl.MessageState `protogen:"open.v1"` unknownFields protoimpl.UnknownFields @@ -20612,11 +20631,13 @@ const file_state_v1_state_proto_rawDesc = "" + "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\x1aU\n" + "\x0eProvidersEntry\x12\x10\n" + "\x03key\x18\x01 \x01(\tR\x03key\x12-\n" + - "\x05value\x18\x02 \x01(\v2\x17.state.v1.ModelProviderR\x05value:\x028\x01\"`\n" + + "\x05value\x18\x02 \x01(\v2\x17.state.v1.ModelProviderR\x05value:\x028\x01\"\xa6\x01\n" + "\rModelProvider\x12\x14\n" + "\x05class\x18\x01 \x01(\tR\x05class\x12\x19\n" + "\bbase_url\x18\x02 \x01(\tR\abaseUrl\x12\x1e\n" + - "\vapi_key_env\x18\x03 \x01(\tR\tapiKeyEnv\"\x15\n" + + "\vapi_key_env\x18\x03 \x01(\tR\tapiKeyEnv\x12$\n" + + "\x0eapi_key_engine\x18\x04 \x01(\tR\fapiKeyEngine\x12\x1e\n" + + "\vapi_key_key\x18\x05 \x01(\tR\tapiKeyKey\"\x15\n" + "\x13CallerModelsRequest\"\xb0\x01\n" + "\x14CallerModelsResponse\x12\x15\n" + "\x06org_id\x18\x01 \x01(\tR\x05orgId\x12E\n" + diff --git a/internal/infrastructure/staterpc/client.go b/internal/infrastructure/staterpc/client.go index c84d0004a..4d172b439 100644 --- a/internal/infrastructure/staterpc/client.go +++ b/internal/infrastructure/staterpc/client.go @@ -150,7 +150,7 @@ func (c *Client) TaskModels(ctx context.Context, taskID int64) (config.OrgModels } providers := make(map[string]config.Provider, len(r.Providers)) for name, p := range r.Providers { - providers[name] = config.Provider{Class: p.GetClass(), BaseURL: p.GetBaseUrl(), APIKeyEnv: p.GetApiKeyEnv()} + providers[name] = modelProviderFromPB(p) } return config.OrgModels{Org: r.OrgId, Aliases: r.Aliases, Providers: providers}, nil } diff --git a/internal/infrastructure/staterpc/model_provider_wire.go b/internal/infrastructure/staterpc/model_provider_wire.go new file mode 100644 index 000000000..e5989d9f0 --- /dev/null +++ b/internal/infrastructure/staterpc/model_provider_wire.go @@ -0,0 +1,29 @@ +package staterpc + +import ( + "github.com/samcharles93/archie-core/internal/config" + pb "github.com/samcharles93/archie-core/internal/contracts/state/v1" +) + +// modelProviderToPB describes a provider on the State Store wire. The +// credential reference travels with it: it names where the key lives, not the +// key, and the daemon cannot resolve a model egress credential without it. +func modelProviderToPB(p config.Provider) *pb.ModelProvider { + return &pb.ModelProvider{ + Class: p.Class, + BaseUrl: p.BaseURL, + ApiKeyEnv: p.APIKeyEnv, + ApiKeyEngine: p.APIKey.Engine, + ApiKeyKey: p.APIKey.Key, + } +} + +// modelProviderFromPB rebuilds a provider from the State Store wire. +func modelProviderFromPB(p *pb.ModelProvider) config.Provider { + return config.Provider{ + Class: p.GetClass(), + BaseURL: p.GetBaseUrl(), + APIKeyEnv: p.GetApiKeyEnv(), + APIKey: config.SecretRef{Engine: p.GetApiKeyEngine(), Key: p.GetApiKeyKey()}, + } +} diff --git a/internal/infrastructure/staterpc/model_provider_wire_test.go b/internal/infrastructure/staterpc/model_provider_wire_test.go new file mode 100644 index 000000000..a4070c24f --- /dev/null +++ b/internal/infrastructure/staterpc/model_provider_wire_test.go @@ -0,0 +1,42 @@ +package staterpc + +import ( + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +// A provider's credential reference must survive the State Store wire. It is a +// reference, not a secret: the daemon needs it to resolve the key for a model +// egress session, and without it every provider arrives credential-less and a +// run is built with no runtime key at all. +func TestModelProviderWireCarriesCredentialRef(t *testing.T) { + t.Parallel() + + sent := map[string]config.Provider{ + "remote": { + Class: "deepseek", + BaseURL: "https://example.invalid/v1", + APIKeyEnv: "REMOTE_API_KEY", + APIKey: config.SecretRef{Engine: "bws", Key: "REMOTE_SECRET"}, + }, + "local": { + Class: "ollama", + BaseURL: "http://127.0.0.1:5800/v1", + }, + } + + got := map[string]config.Provider{} + for name, provider := range sent { + got[name] = modelProviderFromPB(modelProviderToPB(provider)) + } + + for name, want := range sent { + if got[name] != want { + t.Errorf("provider %q round-trip = %+v, want %+v", name, got[name], want) + } + } + if got["remote"].APIKey != (config.SecretRef{Engine: "bws", Key: "REMOTE_SECRET"}) { + t.Errorf("credential reference was dropped on the wire: %+v", got["remote"].APIKey) + } +} diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index 8b326ef6e..827bf14c1 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -320,7 +320,7 @@ func (s *server) TaskModels(ctx context.Context, r *pb.TaskModelsRequest) (*pb.T } out := make(map[string]*pb.ModelProvider, len(providers)) for name, p := range providers { - out[name] = &pb.ModelProvider{Class: p.Class, BaseUrl: p.BaseURL, ApiKeyEnv: p.APIKeyEnv} + out[name] = modelProviderToPB(p) } return &pb.TaskModelsResponse{OrgId: orgID, Aliases: aliases, Providers: out}, nil } diff --git a/proto/state/v1/state.proto b/proto/state/v1/state.proto index aa6427df4..b337aab8a 100644 --- a/proto/state/v1/state.proto +++ b/proto/state/v1/state.proto @@ -1678,6 +1678,11 @@ message ModelProvider { string class = 1; string base_url = 2; string api_key_env = 3; + // The provider's credential reference: where its key lives, never the key + // itself. A daemon resolves a model egress credential from it; without it + // every provider arrives credential-less and no runtime can be built. + string api_key_engine = 4; + string api_key_key = 5; } message CallerModelsRequest {} message CallerModelsResponse {