aws-samples / aws-samples/sample-agent-assisted-sdlc

fix: handle RuntimeCommandError on reinvocation check when microVM is being reaped

Open
#85 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
42
Forks
9
PR merge metrics
No merged PRs in 30d

Description

## Description

The Setup Lambda's reinvocation check (`test -d .dev-claude/invocation-1`) fails with `RuntimeCommandError: AgentCore execute_command HTTP failure` when the microVM is being reaped (maxLifetime hit) at the exact moment the pipeline triggers.

## Root cause

After PR #84, the session stop was moved **after** the reinvocation check (to allow the claude-running probe to see the existing microVM). If the microVM is in the process of being reaped (38-40 min uptime), the first `execute_command` call fails with an HTTP error.

Previously, `stop_runtime_session()` ran first, which forced a clean slate. Now the first command hits a dying microVM.

## Observed

```json
{
"errorMessage": "AgentCore execute_command HTTP failure",
"errorType": "RuntimeCommandError",
"stackTrace": [
"File \"/var/task/index.py\", line 103, in handler\n check = execute_command(\n",
"File \"/var/task/pipeline.py\", line 88, in execute_command\n raise RuntimeCommandError(...)"
]
}
```

## Proposed fix

Wrap the reinvocation check in a try/except. On `RuntimeCommandError`:
1. Stop the session (force fresh microVM)
2. Retry the reinvocation check once
3. If retry also fails, treat as first invocation (safe default — clone will handle it)

```python
try:
check = execute_command(session_id, "sh -c 'test -d ... && echo REINVOKE || echo FIRST'", timeout=10)
except RuntimeCommandError:
logger.warning("reinvocation_check_failed_retrying", extra={"session_id": session_id})
try:
stop_runtime_session(session_id)
except Exception:
pass
try:
check = execute_command(session_id, "sh -c 'test -d ... && echo REINVOKE || echo FIRST'", timeout=10)
except RuntimeCommandError:
logger.warning("reinvocation_check_retry_failed", extra={"session_id": session_id})
check = {"stdout": "FIRST"}
```

## Files

- `project-management/github/connector/lambda/index.py` — add retry logic around line 103

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.