def task_run_cli(
project_name: str,
task_id: str,
unrestricted: bool | None = None,
debug: bool = False,
) -> None:
"""Launch a CLI-mode task container and wait for its readiness marker.
Creates a detached Podman container for interactive CLI access.
After the container reports ready the task metadata is marked
``running`` and the user is shown login instructions.
"""
project = load_project(project_name)
validate_host_setup()
_, meta_path = load_task_meta(project.name, task_id, "cli")
cname = container_name(project.name, "cli", task_id)
_log_debug(f"cli run: start project={project.name} task={task_id} cname={cname}")
# One resolve per launch — vault-expensive under krun.
runtime = _rt.resolve_runtime(project)
_refuse_existing_container(runtime, project.name, cname, task_id)
# Resolve layered agent config (global → project → CLI overrides)
agent_config_dir = _prepare_agent_config(project, project_name, task_id)
env, volumes, egress, broker_context = build_task_env_and_volumes(
project, task_id, agent_config_dir=agent_config_dir
)
# Resolve unrestricted mode: CLI flag → config → default (True)
if unrestricted is None:
_effective = resolve_agent_config(
project_name,
agent_config=project.agent_config,
project_root=project.root,
)
_cfg_val = resolve_agent_value(
"unrestricted", _effective, project.default_agent or "claude"
)
unrestricted = _cfg_val is None or _str_to_bool(_cfg_val)
if unrestricted:
_apply_unrestricted_env(env)
# Run detached and keep the container alive so users can exec into it later
# Note: We intentionally do NOT use --rm so containers persist after stopping.
# This allows `task restart` to quickly resume stopped containers.
task_dir = project.tasks_root / str(task_id)
run_hook(
"pre_start",
project.hook_pre_start,
project_name=project.name,
task_id=task_id,
mode="cli",
cname=cname,
task_dir=task_dir,
meta_path=meta_path,
)
startup_warnings = _run_container(
cname=cname,
image=project_cli_image(project.name),
env=env,
volumes=volumes,
project=project,
task_id=task_id,
task_dir=task_dir,
# Ensure init runs and then keep the container alive even without a TTY
# init-ssh-and-repo.sh now prints a readiness marker we can watch for
command=["bash", "-lc", "init-ssh-and-repo.sh && echo __CLI_READY__; tail -f /dev/null"],
allow_debugger=debug,
egress=egress,
broker_context=broker_context,
)
_apply_shield_policy(project, cname, task_dir, is_restart=False)
run_hook(
"post_start",
project.hook_post_start,
project_name=project.name,
task_id=task_id,
mode="cli",
cname=cname,
task_dir=task_dir,
meta_path=meta_path,
)
# Stream initial logs until ready marker is seen (or timeout), then detach
with timed_phase(f"launch[{cname}]: stream init logs"):
runtime.container(cname).stream_initial_logs(
ready_check=lambda line: "__CLI_READY__" in line or ">> init complete" in line,
timeout_sec=60.0,
)
# Verify the container is still alive after log streaming
_assert_running(project, cname)
run_hook(
"post_ready",
project.hook_post_ready,
project_name=project.name,
task_id=task_id,
mode="cli",
cname=cname,
task_dir=task_dir,
meta_path=meta_path,
)
meta, _ = load_task_meta(project.name, task_id, "cli")
meta["mode"] = "cli"
meta["ready_at"] = datetime.now(UTC).isoformat()
meta["unrestricted"] = unrestricted
meta["debug"] = debug
write_task_state(meta_path, meta)
color_enabled = _supports_color()
print(
f"\nCLI container is running in the background.\n- Name: {_green(cname, color_enabled)}"
)
_print_login_instructions(project.name, task_id, cname, color_enabled)
print(f"- To stop: {_red(f'podman stop {cname}', color_enabled)}\n")
_repeat_startup_warnings(startup_warnings)