Inspect requirements by profile identity

This commit is contained in:
2026-10-09 00:38:01 +00:00
parent 74b91fae99
commit 6c5dec761c
4 changed files with 125 additions and 13 deletions

View File

@@ -15,16 +15,27 @@ type resolvedProfileSelection struct {
profile *domain.ExecutionProfile
}
type profileIdentityRepository interface {
GetProfileIdentity(context.Context, string) (string, error)
}
func (r *Runner) profileLookupID(profileID string) (string, error) {
if strings.TrimSpace(profileID) == "" {
return "", fmt.Errorf("%w: profile id is required", ErrInvalidRequest)
}
if r == nil || r.profiles == nil {
return "", fmt.Errorf("%w: profile repository is not configured", ErrProfileLoad)
}
return profileID, nil
}
func (r *Runner) resolveProfileSelection(
ctx context.Context,
profileID string,
) (*resolvedProfileSelection, error) {
lookupID := profileID
if strings.TrimSpace(lookupID) == "" {
return nil, fmt.Errorf("%w: profile id is required", ErrInvalidRequest)
}
if r == nil || r.profiles == nil {
return nil, fmt.Errorf("%w: profile repository is not configured", ErrProfileLoad)
lookupID, err := r.profileLookupID(profileID)
if err != nil {
return nil, err
}
selectedProfile, err := r.profiles.GetProfile(ctx, lookupID)
if err != nil {
@@ -45,6 +56,25 @@ func (r *Runner) resolveProfileSelection(
}, nil
}
func (r *Runner) resolveProfileIdentity(ctx context.Context, profileID string) (string, error) {
lookupID, err := r.profileLookupID(profileID)
if err != nil {
return "", err
}
if indexed, ok := r.profiles.(profileIdentityRepository); ok {
identity, err := indexed.GetProfileIdentity(ctx, lookupID)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrProfileLoad, err)
}
return identity, nil
}
selection, err := r.resolveProfileSelection(ctx, lookupID)
if err != nil {
return "", err
}
return selection.id, nil
}
func normalizeResolvedExecutionTarget(target domain.ExecutionTarget) (domain.ExecutionTarget, error) {
endpoint, err := domain.NormalizeOpenAICompatibleBaseEndpoint(target.Endpoint)
if err != nil {

View File

@@ -54,7 +54,7 @@ func (r *Runner) InspectRequirements(
if strings.TrimSpace(selectedProfileID) == "" {
return nil, fmt.Errorf("%w: %w: profile id is required either in request or prompt default_profile", ErrInvalidRequest, ErrProfileRequired)
}
profileSelection, err := r.resolveProfileSelection(ctx, selectedProfileID)
profileIdentity, err := r.resolveProfileIdentity(ctx, selectedProfileID)
if err != nil {
return nil, err
}
@@ -74,6 +74,6 @@ func (r *Runner) InspectRequirements(
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrPromptLoad, err)
}
inspection.ProfileID = profileSelection.id
inspection.ProfileID = profileIdentity
return &inspection, nil
}

View File

@@ -24,6 +24,23 @@ type requirementInspectionPromptRepository struct {
snapshotVersion string
}
type inspectionProfileIdentityRepository struct {
*inspectionProfileRepository
identity string
identityErr error
identityID string
identityCalls int
}
func (repository *inspectionProfileIdentityRepository) GetProfileIdentity(
_ context.Context,
id string,
) (string, error) {
repository.identityCalls++
repository.identityID = id
return repository.identity, repository.identityErr
}
func (repository *requirementInspectionPromptRepository) GetPromptDefinition(
context.Context,
string,
@@ -94,6 +111,68 @@ func TestRunnerInspectRequirementsUsesCapturedSelectionWithoutExecutionWork(t *t
}
}
func TestRunnerInspectRequirementsUsesProfileIdentityCapabilityOrFallback(t *testing.T) {
message, err := prompt.CompileTemplate("message", `{{.name}}`)
if err != nil {
t.Fatal(err)
}
definition := &domain.PromptDefinition{ID: "selected", Version: "v1"}
makePromptRepository := func() *requirementInspectionPromptRepository {
return &requirementInspectionPromptRepository{
definition: definition, snapshotFound: true,
snapshot: prompt.RequirementSnapshot{Templates: prompt.CompiledTemplates{Messages: []*template.Template{message}}},
}
}
requestedID := " literal-profile "
t.Run("optional capability", func(t *testing.T) {
profiles := &inspectionProfileIdentityRepository{
inspectionProfileRepository: &inspectionProfileRepository{profile: &domain.ExecutionProfile{
ID: "repository-profile", ResolvedTarget: &domain.ExecutionTarget{},
}},
identity: requestedID,
}
inspection, err := (&Runner{promptDefs: makePromptRepository(), profiles: profiles}).InspectRequirements(
context.Background(), RequirementInspectionRequest{
PromptID: "selected", ProfileID: requestedID, Vars: map[string]string{"name": "Ada"},
},
)
if err != nil || inspection.ProfileID != requestedID || profiles.identityCalls != 1 ||
profiles.identityID != requestedID || profiles.calls != 0 {
t.Fatalf("identity capability inspection=(%+v, %v), capability calls=%d id=%q full lookups=%d",
inspection, err, profiles.identityCalls, profiles.identityID, profiles.calls)
}
})
t.Run("full-profile fallback", func(t *testing.T) {
profiles := &inspectionProfileRepository{profile: &domain.ExecutionProfile{
ID: "repository-profile", ResolvedTarget: &domain.ExecutionTarget{},
}}
inspection, err := (&Runner{promptDefs: makePromptRepository(), profiles: profiles}).InspectRequirements(
context.Background(), RequirementInspectionRequest{
PromptID: "selected", ProfileID: requestedID, Vars: map[string]string{"name": "Ada"},
},
)
if err != nil || inspection.ProfileID != requestedID || profiles.calls != 1 || profiles.id != requestedID {
t.Fatalf("fallback inspection=(%+v, %v), full lookup=(%d,%q)", inspection, err, profiles.calls, profiles.id)
}
})
t.Run("capability failure", func(t *testing.T) {
lookupErr := errors.New("profile identity lookup failed")
profiles := &inspectionProfileIdentityRepository{
inspectionProfileRepository: &inspectionProfileRepository{},
identityErr: lookupErr,
}
_, err := (&Runner{promptDefs: makePromptRepository(), profiles: profiles}).InspectRequirements(
context.Background(), RequirementInspectionRequest{PromptID: "selected", ProfileID: requestedID},
)
if !errors.Is(err, ErrProfileLoad) || !errors.Is(err, lookupErr) || profiles.identityCalls != 1 || profiles.calls != 0 {
t.Fatalf("capability failure=(%v), capability calls=%d full lookups=%d", err, profiles.identityCalls, profiles.calls)
}
})
}
func TestRunnerInspectRequirementsUsesPromptDefaultAndIncludesSessionWhenNotBypassed(t *testing.T) {
message, err := prompt.CompileTemplate("message", `{{input "message-input"}}`)
if err != nil {