Skip to content

Commit 7bf2aee

Browse files
committed
fix(import): resolve OEC agent reference via deployed state when runtime has custom name
extractAgentName() derives the AWS runtime name from the OEC service name pattern, but this fails to match when the runtime was imported with --name since the project spec stores the local name. Now falls back to listing runtimes to find the runtime ID, then looks up the local name in deployed-state.json.
1 parent 5bed8ed commit 7bf2aee

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

src/cli/commands/import/import-online-eval.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import type { OnlineEvalConfig } from '../../../schema';
22
import type { GetOnlineEvalConfigResult, OnlineEvalConfigSummary } from '../../aws/agentcore-control';
3-
import { getOnlineEvaluationConfig, listAllOnlineEvaluationConfigs } from '../../aws/agentcore-control';
3+
import {
4+
getOnlineEvaluationConfig,
5+
listAllAgentRuntimes,
6+
listAllOnlineEvaluationConfigs,
7+
} from '../../aws/agentcore-control';
48
import { ANSI } from './constants';
5-
import { failResult } from './import-utils';
9+
import { failResult, findResourceInDeployedState } from './import-utils';
610
import { executeResourceImport } from './resource-import';
711
import type { ImportResourceOptions, ImportResourceResult, ResourceImportDescriptor } from './types';
812
import type { Command } from '@commander-js/extra-typings';
@@ -97,13 +101,12 @@ function createOnlineEvalDescriptor(): ResourceImportDescriptor<GetOnlineEvalCon
97101

98102
buildDeployedStateEntry: (name, id, d) => ({ type: 'online-eval', name, id, arn: d.configArn }),
99103

100-
// eslint-disable-next-line @typescript-eslint/require-await -- interface requires Promise return type
101-
beforeConfigWrite: async ({ detail, localName, projectSpec, target, onProgress, logger }) => {
104+
beforeConfigWrite: async ({ detail, localName, projectSpec, ctx, target, onProgress, logger }) => {
102105
logger.startStep('Resolve references');
103106

104107
// Extract agent name from service names
105-
const agentName = extractAgentName(detail.serviceNames ?? []);
106-
if (!agentName) {
108+
const awsAgentName = extractAgentName(detail.serviceNames ?? []);
109+
if (!awsAgentName) {
107110
return failResult(
108111
logger,
109112
'Could not determine agent name from online eval config. The config has no data source service names.',
@@ -112,12 +115,40 @@ function createOnlineEvalDescriptor(): ResourceImportDescriptor<GetOnlineEvalCon
112115
);
113116
}
114117

115-
// Validate agent exists in project
118+
// Resolve the local agent name. The AWS name from the OEC service names
119+
// may differ from the local name if the runtime was imported with --name.
116120
const agentNames = new Set((projectSpec.runtimes ?? []).map(r => r.name));
117-
if (!agentNames.has(agentName)) {
121+
let agentName: string | undefined;
122+
123+
if (agentNames.has(awsAgentName)) {
124+
// Direct match — local name equals AWS name
125+
agentName = awsAgentName;
126+
} else {
127+
// Look up the AWS runtime ID for the AWS name, then find the local name
128+
// that maps to it in deployed state.
129+
onProgress(`Agent "${awsAgentName}" not found by name, checking deployed state...`);
130+
const runtimes = await listAllAgentRuntimes({ region: target.region });
131+
const matchingRuntime = runtimes.find(r => r.agentRuntimeName === awsAgentName);
132+
133+
if (matchingRuntime) {
134+
const targetName = target.name ?? 'default';
135+
const localMatch = await findResourceInDeployedState(
136+
ctx.configIO,
137+
targetName,
138+
'runtime',
139+
matchingRuntime.agentRuntimeId
140+
);
141+
if (localMatch && agentNames.has(localMatch)) {
142+
agentName = localMatch;
143+
onProgress(`Resolved AWS runtime "${awsAgentName}" to local name "${agentName}"`);
144+
}
145+
}
146+
}
147+
148+
if (!agentName) {
118149
return failResult(
119150
logger,
120-
`Online eval config references agent "${agentName}" which is not in this project. ` +
151+
`Online eval config references agent "${awsAgentName}" which is not in this project. ` +
121152
`Import or add the agent first with \`agentcore import runtime\` or \`agentcore add agent\`.`,
122153
'online-eval',
123154
localName

0 commit comments

Comments
 (0)