-
Notifications
You must be signed in to change notification settings - Fork 52
fix(schema): auto-migrate pre-v0.4.0 agentcore.json legacy keys (#719) #1649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aidandaly24
wants to merge
4
commits into
aws:main
Choose a base branch
from
aidandaly24:fix/719
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+559
−10
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
82a9d85
fix(unit-only): auto-migrate pre-v0.4.0 agentcore.json legacy keys (#…
aidandaly24 7c26e20
fix(schema): auto-migrate pre-v0.4.0 agentcore.json legacy keys (#719)
aidandaly24 c07913f
test(schema): cover issue #719 canonical shape and add migration tele…
aidandaly24 20ac6df
fix(telemetry): defer legacy-migration notice past TUI and unit-test …
aidandaly24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/cli/telemetry/__tests__/legacy-project-migration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import type { LegacyProjectMigrationInfo, LegacyProjectMigrationReporter } from '../../../lib/schemas/io/config-io'; | ||
| import * as configIo from '../../../lib/schemas/io/config-io'; | ||
| import { TelemetryClient } from '../client'; | ||
| import { TelemetryClientAccessor } from '../client-accessor'; | ||
| import { | ||
| printLegacyProjectMigrationNotice, | ||
| registerLegacyProjectMigrationReporter, | ||
| resetLegacyProjectMigrationNotice, | ||
| } from '../legacy-project-migration'; | ||
| import { InMemorySink } from '../sinks/in-memory-sink'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| let sink: InMemorySink; | ||
| let capturedReporter: LegacyProjectMigrationReporter | undefined; | ||
|
|
||
| const ALL_LEGACY: LegacyProjectMigrationInfo = { | ||
| hadAgentsKey: true, | ||
| hadCredentialTypeKey: true, | ||
| hadRuntimeTypeKey: true, | ||
| }; | ||
|
|
||
| /** Wait for the reporter's fire-and-forget telemetry promise chain to settle. */ | ||
| async function flushMicrotasks(): Promise<void> { | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| sink = new InMemorySink(); | ||
| capturedReporter = undefined; | ||
| vi.spyOn(TelemetryClientAccessor, 'get').mockResolvedValue(new TelemetryClient(sink)); | ||
| // Capture the reporter the CLI installs, instead of letting it mutate lib module state. | ||
| vi.spyOn(configIo, 'setLegacyProjectMigrationReporter').mockImplementation(reporter => { | ||
| capturedReporter = reporter; | ||
| }); | ||
| resetLegacyProjectMigrationNotice(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| resetLegacyProjectMigrationNotice(); | ||
| }); | ||
|
|
||
| describe('registerLegacyProjectMigrationReporter', () => { | ||
| it('emits cli.legacy_project_migrated with the camelCase->snake_case attribute mapping', async () => { | ||
| registerLegacyProjectMigrationReporter(); | ||
| expect(capturedReporter).toBeDefined(); | ||
|
|
||
| capturedReporter!({ hadAgentsKey: true, hadCredentialTypeKey: false, hadRuntimeTypeKey: true }); | ||
| await flushMicrotasks(); | ||
|
|
||
| expect(sink.metrics).toHaveLength(1); | ||
| expect(sink.metrics[0]!.metric).toBe('cli.legacy_project_migrated'); | ||
| expect(sink.metrics[0]!.value).toBe(1); | ||
| // Booleans are serialized to strings by TelemetryClient.emit; assert each key maps to its own field. | ||
| expect(sink.metrics[0]!.attrs).toEqual({ | ||
| had_agents_key: 'true', | ||
| had_credential_type_key: 'false', | ||
| had_runtime_type_key: 'true', | ||
| }); | ||
| }); | ||
|
|
||
| it('does not let a TelemetryClientAccessor.get() rejection propagate', async () => { | ||
| vi.spyOn(TelemetryClientAccessor, 'get').mockRejectedValue(new Error('no client')); | ||
| registerLegacyProjectMigrationReporter(); | ||
|
|
||
| expect(() => capturedReporter!(ALL_LEGACY)).not.toThrow(); | ||
| await flushMicrotasks(); | ||
| expect(sink.metrics).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('printLegacyProjectMigrationNotice', () => { | ||
| it('is a no-op when no migration was observed', () => { | ||
| const write = vi.spyOn(process.stderr, 'write').mockReturnValue(true); | ||
| printLegacyProjectMigrationNotice(); | ||
| expect(write).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('prints once after a migration is observed, then is a no-op (one-time latch)', async () => { | ||
| const write = vi.spyOn(process.stderr, 'write').mockReturnValue(true); | ||
|
|
||
| registerLegacyProjectMigrationReporter(); | ||
| capturedReporter!(ALL_LEGACY); | ||
| await flushMicrotasks(); | ||
|
|
||
| printLegacyProjectMigrationNotice(); | ||
| printLegacyProjectMigrationNotice(); | ||
|
|
||
| expect(write).toHaveBeenCalledTimes(1); | ||
| expect(String(write.mock.calls[0]![0])).toContain('pre-v0.4.0'); | ||
| }); | ||
|
|
||
| it('does not print synchronously from the reporter (deferred to keep it out of the TUI alt-screen)', async () => { | ||
| const write = vi.spyOn(process.stderr, 'write').mockReturnValue(true); | ||
|
|
||
| registerLegacyProjectMigrationReporter(); | ||
| capturedReporter!(ALL_LEGACY); | ||
| await flushMicrotasks(); | ||
|
|
||
| // The reporter only arms the notice; nothing is written until printPostCommandNotices flushes it. | ||
| expect(write).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type { LegacyProjectMigrationInfo } from '../../lib/schemas/io/config-io.js'; | ||
| import { setLegacyProjectMigrationReporter } from '../../lib/schemas/io/config-io.js'; | ||
| import { ANSI } from '../constants.js'; | ||
| import { TelemetryClientAccessor } from './client-accessor.js'; | ||
|
|
||
| /** | ||
| * Set once a pre-v0.4.0 agentcore.json is auto-migrated on read, so the deprecation notice can be | ||
| * printed *after* the command/TUI exits. Printing synchronously from the reporter would land the | ||
| * notice inside the Ink alt-screen buffer (most legacy-project reads happen inside a TUI flow) where | ||
| * it is immediately repainted over and lost — so we defer, mirroring `printTelemetryNotice` / | ||
| * `printUpdateNotification` which are also flushed via `printPostCommandNotices`. | ||
| */ | ||
| let migrationObserved = false; | ||
| let noticePrinted = false; | ||
|
|
||
| /** Reset the deferred-notice state. Test-only. */ | ||
| export function resetLegacyProjectMigrationNotice(): void { | ||
| migrationObserved = false; | ||
| noticePrinted = false; | ||
| } | ||
|
|
||
| /** | ||
| * Print the one-time pre-v0.4.0 deprecation notice if a legacy agentcore.json was migrated during | ||
| * this invocation. No-op if no migration was observed or the notice already fired. Call after the | ||
| * TUI/command finishes (the alt-screen buffer has been restored) — see `printPostCommandNotices`. | ||
| */ | ||
| export function printLegacyProjectMigrationNotice(): void { | ||
| if (!migrationObserved || noticePrinted) return; | ||
| noticePrinted = true; | ||
| const { yellow, reset } = ANSI; | ||
| process.stderr.write( | ||
| [ | ||
| '', | ||
| `${yellow}Your agentcore.json uses pre-v0.4.0 keys (\`agents\`, and/or \`type\` on`, | ||
| 'credentials/runtimes). These are auto-migrated for now, but support will be', | ||
| 'removed in a future release. Update the file to use `runtimes` and', | ||
| `\`authorizerType\` to silence this notice.${reset}`, | ||
| '', | ||
| ].join('\n') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Wire the CLI's observability into the lib config loader: when a pre-v0.4.0 agentcore.json is | ||
| * auto-migrated on read, emit the `cli.legacy_project_migrated` metric (so legacy-project adoption | ||
| * is measurable and the shim can eventually be removed) and arm a one-time deprecation notice that | ||
| * `printPostCommandNotices` flushes after the alt-screen buffer is restored. | ||
| * | ||
| * Kept here in the CLI layer so `src/lib` stays free of any telemetry/CLI import. | ||
| */ | ||
| export function registerLegacyProjectMigrationReporter(): void { | ||
| setLegacyProjectMigrationReporter((info: LegacyProjectMigrationInfo) => { | ||
| migrationObserved = true; | ||
| void TelemetryClientAccessor.get() | ||
| .then(client => | ||
| client.emit('cli.legacy_project_migrated', 1, { | ||
| had_agents_key: info.hadAgentsKey, | ||
| had_credential_type_key: info.hadCredentialTypeKey, | ||
| had_runtime_type_key: info.hadRuntimeTypeKey, | ||
| }) | ||
| ) | ||
| .catch(() => { | ||
| // Telemetry is best-effort and must never affect CLI behavior. | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new CLI-layer module has no direct unit test. The lib-layer tests in
config-io.test.tsverify that the reporter is invoked with the rightLegacyProjectMigrationInfo, but nothing exercises what this file actually does with that info, specifically:hadAgentsKey→had_agents_key, etc.) on thecli.legacy_project_migratedemit. TypeScript catches a missing/renamed attribute, but it won't catch a value going to the wrong attribute (e.g.had_agents_key: info.hadCredentialTypeKey). A one-line assertion againstInMemorySinkwould lock this down.printDeprecationNotice— that the second invocation of the reporter is a no-op for the notice.resetLegacyProjectMigrationNoticeis exported as test-only specifically for this, but is currently unused (dead export).TelemetryClientAccessor.get()rejection doesn't propagate (the.catch(() => {})contract).The existing
src/cli/telemetry/__tests__/client.test.tsis a good template — it spiesTelemetryClientAccessor.getto return aTelemetryClient(InMemorySink)and asserts onsink.metrics. A few similar cases here would (a) actually use theresetLegacyProjectMigrationNoticeexport and (b) prevent silent regressions in the attribute mapping / one-time latch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
src/cli/telemetry/__tests__/legacy-project-migration.test.tscovering exactly these gaps, using theTelemetryClientAccessor.get+InMemorySinktemplate fromclient.test.ts:cli.legacy_project_migratedis emitted with{ had_agents_key, had_credential_type_key, had_runtime_type_key }each carrying its own value (e.g.hadCredentialTypeKey: falsemaps tohad_credential_type_key: 'false'), which would catch a wrong-attribute wiring bug.printLegacyProjectMigrationNotice()twice and assertsprocess.stderr.writefires exactly once. This now uses theresetLegacyProjectMigrationNoticeexport inbeforeEach/afterEach(no longer a dead export).TelemetryClientAccessor.get()to reject and asserts the reporter doesn't throw and no metric is recorded.