Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions kits/agent-kiro-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ async for event in adapter.stream("Summarize changes"):
print(event)
```

## Delivery stages

A Delivery stage declares trust through the step's `config.sandbox` value:

- `read-only` uses `--trust-tools=`.
- `workspace-write` uses `-a`.
- Headless mode ignores `sandbox` and uses the adapter's `trust_tools`; the
default is `--trust-all-tools`.

The step's `timeout` is the stage time bound. When `beddel run` builds this
adapter with no constructor arguments, the adapter timeout is 600 seconds, so
the step timeout must be at most 600 seconds. A step timeout kills and reaps
the `kiro-cli` process. The subprocess inherits the repository cwd and
environment and receives closed stdin. Login comes from the existing
`kiro-cli` login or `KIRO_API_KEY` in the environment; credentials are never
passed as command-line arguments.

The CLI does not report changed files, so `files_changed` is always `[]` and a
Delivery stage reads repository changes separately. Adapter errors use
`BEDDEL-AGENT-701` for execution failures and `BEDDEL-AGENT-702` for adapter
timeouts. A step timeout is raised as `BEDDEL-EXEC-005`; under the default FAIL
strategy the step failure `BEDDEL-EXEC-002` wraps it as its cause.

## Headless Mode (CI/CD)

For containers, CI/CD pipelines, and headless environments where browser-based SSO is unavailable, set the `KIRO_API_KEY` environment variable. The adapter automatically detects it and activates headless mode.
Expand Down Expand Up @@ -125,7 +148,8 @@ If all steps fail, an `AgentError(BEDDEL-AGENT-701)` is raised.

## Running Tests

Run the kit tests from the kit directory with the Beddel SDK installed:

```bash
cd repo/kits/agent-kiro-kit
PYTHONPATH="python:../../src/beddel-py/tests" python -m pytest tests/ -x
pytest -q
```
45 changes: 39 additions & 6 deletions kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
command. It implements the :class:`~beddel.domain.ports.IAgentAdapter`
protocol via structural subtyping (no explicit inheritance).

Each execution inherits the caller's cwd and environment, closes stdin,
and is bounded by the constructor timeout. Interactive trust maps
``read-only`` to ``--trust-tools=`` and ``workspace-write`` to ``-a``;
headless trust uses ``trust_tools`` or defaults to ``--trust-all-tools``.
The CLI reports no changed-file list, so successful results always use
``files_changed=[]``.

The CLI is auto-discovered from ``~/.local/bin/kiro-cli`` or via
``shutil.which``. If neither resolves, the adapter attempts a one-time
auto-install from ``https://cli.kiro.dev/install``.
Expand All @@ -18,6 +25,7 @@
from __future__ import annotations

import asyncio
import contextlib
import os
import shutil
import subprocess
Expand All @@ -38,22 +46,42 @@
_DEFAULT_CLI_PATH = Path.home() / ".local" / "bin" / "kiro-cli"


_REAP_GRACE_SECONDS = 2.0


async def _terminate(proc: asyncio.subprocess.Process) -> None:
"""Kill a subprocess during timeout or cancellation and bound the reap.

``Process.wait`` only returns after the output pipes close, so a child
of the CLI that inherited them would otherwise keep a timed-out stage
waiting. The direct child is reaped by the event loop either way.
"""
with contextlib.suppress(ProcessLookupError):
proc.kill()
with contextlib.suppress(TimeoutError):
async with asyncio.timeout(_REAP_GRACE_SECONDS):
await proc.wait()


class KiroCLIAgentAdapter:
"""Kiro CLI agent adapter using subprocess execution.

Implements the ``IAgentAdapter`` protocol structurally by exposing
:meth:`execute` and :meth:`stream` with matching signatures. All
interaction with the Kiro CLI happens through
``asyncio.create_subprocess_exec``.
``asyncio.create_subprocess_exec``. The subprocess inherits the
caller's cwd and environment, receives closed stdin, and is killed and
reaped on timeout or cancellation.

Args:
model: Default model identifier for CLI invocations.
cli_path: Explicit path to the ``kiro-cli`` binary. When ``None``,
auto-discovered from ``~/.local/bin/kiro-cli``, then
``shutil.which``, then auto-installed from the official
installer script.
timeout: Maximum execution time in seconds before the subprocess
is killed.
timeout: Maximum execution time in seconds. The subprocess is
terminated and reaped when this bound expires or the caller
cancels the execution.
api_key: Optional API key for headless mode. When provided (or
when ``KIRO_API_KEY`` env var is set), the adapter operates
in headless mode where sandbox is ignored and tool trust is
Expand Down Expand Up @@ -171,7 +199,9 @@ async def execute(

Returns:
An :class:`AgentResult` with the CLI's stdout, exit code,
and execution metadata.
and execution metadata. ``files_changed`` is always empty
because the CLI does not report a file list; callers reconcile
repository changes separately.

Raises:
AgentError: ``BEDDEL-AGENT-702`` on timeout, or
Expand All @@ -182,6 +212,7 @@ async def execute(

proc = await asyncio.create_subprocess_exec(
*cmd,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
Expand All @@ -192,13 +223,15 @@ async def execute(
timeout=self._timeout,
)
except TimeoutError as exc:
proc.kill()
await proc.wait()
await _terminate(proc)
raise AgentError(
code=AGENT_TIMEOUT,
message=f"Kiro CLI process timed out after {self._timeout}s",
details={"timeout": self._timeout, "cmd": [str(c) for c in cmd]},
) from exc
except asyncio.CancelledError:
await _terminate(proc)
raise

stdout_text = stdout_bytes.decode("utf-8", errors="replace") if stdout_bytes else ""
stderr_text = stderr_bytes.decode("utf-8", errors="replace") if stderr_bytes else ""
Expand Down
Loading
Loading