Implement the following plan: Fix: CLI Startup Triggers Backend Migration Services…
- User
Implement the following plan:
Fix: CLI Startup Triggers Backend Migration Services
Context
Running
./bin/secman manage-user-mappings list-bucketboots the full Micronaut application context — including Flyway, Hibernate, HikariCP, and 4 backend startup listeners — even though the command only needsS3DownloadService. TheRequirementIdMigrationServicefires @Async on a background thread; by the time it executes, the.use { }block has already closed the context, causing a "Session/EntityManager is closed" stack trace after the S3 listing completes successfully.Root cause:
SecmanCli.ktline 182 callsApplicationContext.run()which triggers full bean scanning of thebackendngmodule. No mechanism distinguishes CLI mode from backend mode.Affected startup listeners (all in backendng, all do DB operations on every CLI invocation):
RequirementIdMigrationService— @Async @EventListener(StartupEvent), queries+updates all requirements (crashes)DefaultAdminBootstrapper— creates default admin user if none existSystemUseCaseInitializer— creates IT/OT/NT use casesProviderInitializationService— creates GitHub/Microsoft identity providers with placeholder credentialsFlywayRepairCustomizer— connects to DB to inspect/repairflyway_schema_history
None of these are needed by any CLI command.
Recommended Approach: Micronaut "cli" Environment
Use
ApplicationContext.builder().environments("cli").start()instead ofApplicationContext.run(). Then suppress backend-only startup beans with@Requires(notEnv = ["cli"])and disable Flyway via environment-specific YAML.This is the standard Micronaut mechanism for exactly this use case. It fixes the crash, eliminates unnecessary startup work for ALL CLI commands, and is easily maintained (future startup listeners just add the same annotation).
Files to Modify
# File Change 1 src/cli/src/main/kotlin/com/secman/cli/SecmanCli.ktReplace all 9 ApplicationContext.run()→ApplicationContext.builder().environments("cli").start()2 Assistant
★ Insight ─────────────────────────────────────This is a classic problem with shared application contexts. Micronaut's environment system is the idiomatic solution — it lets you conditionally load beans based on the runtime context (CLI vs backend). The@Requires(notEnv = ["cli"])annotation tells Micronaut's dependency injection to skip those beans entirely when the "cli" environment is active, rather than creating them and hoping they don't cause issues.─────────────────────────────────────────────────Let me start by reading all the files that need modification.
Assistant
Now let me check if the
application-cli.ymlresource directory exists and verify the existing application.yml for Flyway config reference.Assistant
Good, I have all the context. Let me now implement all 7 changes. I'll work on the independent backend files in parallel first.
Assistant
Now let me implement all the changes. I'll start with the 5 backend bean annotations (all independent) plus the new YAML file in parallel.
Assistant
Three edits failed — the whitespace/content didn't match exactly. Let me re-read those files to see the exact content.
Assistant
Now I can see the exact content. The comment blocks between imports and
@Singletoncaused the mismatch. Let me fix all three.Assistant
All 5 backend beans updated. Now let me update
SecmanCli.kt— the main change.Assistant
Now for
SecmanCli.kt— I'll add the helper method and replace all 9ApplicationContext.run()calls.Assistant
Now add the helper method and replace all 9
ApplicationContext.run()calls.Assistant
Now replace all 9
ApplicationContext.run()calls withcreateCliContext().Assistant
Let me verify the
Environmentimport is actually needed (Micronaut'sApplicationContext.builder()doesn't require it explicitly).Assistant
The
Environmentimport isn't used directly. Let me remove it to keep imports clean.Assistant
Let me quickly verify the replacements landed correctly by checking a couple of the
createCliContext()call sites.Assistant
All 9 call sites replaced, zero
ApplicationContext.run()remaining. Now let me update the task and run the build.Assistant
The output was truncated. Let me check what actually failed.
Assistant
The build failed, but the 2 test failures are pre-existing — they're integration tests (
EdgeCaseTestandVulnerabilityIntegrationTest) that likely require Docker/Testcontainers. They're not related to our changes (our changes are purely annotation/config additions). Let me verify by checking if these tests were failing before our changes.Assistant
Confirmed — same 2 tests fail on the clean main branch. These are pre-existing failures, not related to our changes. Let me restore our work.
Assistant
Good, our changes are back. Let me verify compilation succeeds (skipping the pre-existing test failures).
Assistant
All code compiles successfully. Let me also verify the CLI tests pass.
Assistant
All tests pass. Let me mark the build task complete.
Assistant
All changes are implemented and verified. Here's a summary:
Changes Made
1.
src/cli/src/main/kotlin/com/secman/cli/SecmanCli.kt- Added
createCliContext()helper that usesApplicationContext.builder().environments("cli").start() - Replaced all 9
ApplicationContext.run()calls withcreateCliContext()
2.
src/cli/src/main/resources/application-cli.yml(new)- Added