Skip to content

Conversation

pahud
Copy link

@pahud pahud commented Aug 30, 2025

Description

Implements background execution support for local AgentCore agents by adding --detach/-d and --log-file flags to agentcore launch --local. This addresses the terminal blocking issue that affects AI IDEs like Kiro and other development workflows that need terminal reuse.

Key Features:

  • --detach/-d flag runs agents in background (Docker-style interface)
  • --log-file option redirects logs to custom files (implies --detach)
  • LocalProcessManager for basic process lifecycle management
  • State tracking in .agentcore/local_agents.json
  • Comprehensive validation (flags only work with --local)

Addresses Issue: Background daemon support for local mode (#144)

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Documentation update
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement
  • Code refactoring

Testing

  • Unit tests pass locally
  • Integration tests pass (if applicable)
  • Test coverage remains above 80%
  • Manual testing completed

Test Coverage:

  • CLI validation tests for new flags
  • LocalProcessManager unit tests (process lifecycle, state management)
  • Integration tests for detach functionality
  • Cross-platform compatibility verified (macOS, Linux, Windows)

Manual Testing:

# Tested all flag combinations and validation
agentcore launch --local --detach          ✅
agentcore launch --local -d                ✅
agentcore launch --local --log-file test.log ✅
agentcore launch --detach                  ❌ (proper error)
agentcore launch --log-file test.log       ❌ (proper error)

Checklist

  • My code follows the project's style guidelines (ruff/pre-commit)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Security Checklist

  • No hardcoded secrets or credentials
  • No new security warnings from bandit
  • Dependencies are from trusted sources
  • No sensitive data logged

Security Notes:

  • PID files stored in user's .agentcore directory (secure)
  • No elevation of privileges required
  • Process management uses standard Python subprocess module
  • Log files respect user-specified paths with proper validation

Breaking Changes

List any breaking changes and migration instructions:

N/A - This is a fully backward-compatible addition. Existing agentcore launch --local behavior is unchanged.

Additional Notes

Implementation Scope

This PR focuses specifically on the core detach functionality. Process management commands (ps, stop, logs) are intentionally excluded to maintain a focused, reviewable scope and will be addressed in future PRs if needed.

Benefits for AI IDEs

  • Solves Terminal Blocking: AI IDEs like Kiro can now reuse terminals for multiple operations
  • Docker-Style Interface: Familiar -d/--detach flag pattern for developers
  • Flexible Logging: Custom log file locations for debugging and monitoring
  • Background Execution: Agents run independently of terminal sessions

Architecture Decisions

  • LocalProcessManager: Lightweight process management without external dependencies
  • State File: JSON-based state tracking in .agentcore/local_agents.json
  • Validation: Strict validation ensures flags only work with --local mode
  • Cross-Platform: Uses Python's built-in modules for maximum compatibility

Documentation Updates

  • CLI Reference: Updated with new flags and comprehensive troubleshooting
  • Runtime Overview: Added local development best practices
  • Quickstart Guide: Enhanced with terminal blocking solutions
  • Test Commands: Added background mode testing examples

Future Enhancements (Not in This PR)

Process management commands can be added in future PRs:

  • agentcore ps --local - List running local agents
  • agentcore stop --local - Stop local agents
  • agentcore logs --local - View local agent logs

Testing Strategy

  • Unit Tests: LocalProcessManager functionality and edge cases
  • CLI Tests: Flag validation and error handling
  • Integration Tests: End-to-end detach functionality
  • Manual Tests: Real-world usage scenarios with different IDEs

This implementation provides a solid foundation for background agent execution while maintaining the project's quality standards and backward compatibility.

pahud added 2 commits August 29, 2025 20:06
- Fix issue aws#123 where multibyte characters (Chinese, Japanese, Korean, etc.)
  are displayed as escaped Unicode sequences instead of readable text
- Enhanced _handle_aws_response() to properly decode UTF-8 byte strings
- Added JSON parsing with graceful fallback for decoded content
- Maintains full backward compatibility with existing string/dict events
- Added comprehensive unit tests for Unicode support
- Tested with Chinese, Japanese, Korean, Arabic, Russian, and emoji characters
- Move LocalProcessManager import to module level in commands.py
- Fix test_cli_imports function to use assertions instead of returns
- Remove non-existent local_commands import from tests
- Update main section to handle test functions properly

Fixes:
- AttributeError: LocalProcessManager not found in module
- PytestReturnNotNoneWarning: Test functions should return None
Security improvements:
- Add nosec B603 annotation for subprocess.Popen with list args (safe usage)
- Explicitly set shell=False for subprocess security
- Replace hardcoded /tmp usage with tempfile.TemporaryDirectory
- Add security comments for file operations
- Use secure temporary directories in tests

Addresses Bandit security scan warnings while maintaining functionality.
@pahud pahud changed the title Add --detach and --log-file support for agentcore launch --local feat: add --detach and --log-file support for agentcore launch --local Aug 30, 2025
- Add comprehensive unit tests for LocalAgentProcess class
- Add extensive tests for LocalProcessManager functionality
- Test error handling, edge cases, and concurrent scenarios
- Add tests for AWS credentials handling and security settings
- Test process lifecycle management and state persistence
- Improve detach mode integration tests with lifecycle simulation
- Add tests for corrupted state files and IO error handling
- Test container name generation and subprocess security
- Achieve near-complete test coverage for detach mode functionality
- Fixed broken function definition in test_local_process_manager.py
- The syntax error was preventing proper module imports in CI environment
- Tests now pass locally and should pass in CI
- LocalProcessManager is properly imported and used in CLI commands
pahud added 5 commits August 30, 2025 08:00
- Simplify agent stop tests to avoid complex generators
- Remove complex subprocess mocking that could cause memory issues
- Mark slow tests appropriately
…ed function-based tests

- Root cause: Class-based tests with setup/teardown methods and heavy subprocess mocking were causing memory issues
- Solution: Replaced with function-based tests using temporary directories per test
- Removed complex subprocess.Popen and boto3.Session mocking that was consuming excessive memory
- Tests now run successfully without crashes while maintaining good coverage
- Kept original test file as backup (_original.py)
The test runner was picking up the _original.py backup file which contained
the memory-intensive tests that were causing the crashes. Removing it
completely to ensure only the stable simplified tests run.
- Fixed invalid escape sequences in regex pattern within f-string
- Changed \s and \. to \\s and \\. to properly escape in f-string context
- All tests now pass without warnings
- Coverage remains at 88.74% (still needs improvement to reach 90%)
- Add complete test suite for logs.py utility functions (100% coverage)
- Add extensive tests for LocalProcessManager detach mode functionality
- Test start_agent, stop, _start_detached_process methods
- Test error handling scenarios (no runtime, no AWS creds, boto3 missing)
- Test process lifecycle management (graceful shutdown, force kill)
- Coverage improved from 88.74% to 90.56%, meeting 90% requirement

Related to detach mode PR - these tests cover the core functionality
for running agents in detached/background mode.
@siwachabhi siwachabhi self-requested a review September 5, 2025 00:04
@siwachabhi
Copy link
Contributor

I think this is significant complexity added into toolkit, which can be achieved with existing shell or docker commands, we discussed this internally and don't think right now its a good idea to bring this complexity

@siwachabhi siwachabhi added invalid This doesn't seem right runtime Related to agentcore runtime labels Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right runtime Related to agentcore runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants