diff --git a/README.md b/README.md index ba43c346..3e44d86d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ You can find a detailed overview of the taskflow grammar [here](doc/GRAMMAR.md) ``` ┌─────────────────────────────────────────────────────┐ │ CLI (cli.py) │ -│ Typer-based entry point: -p, -t, -l, -g, --resume │ +│ Typer-based entry point: -p, -t, -l, -g, -m, --resume│ └─────────────────────┬───────────────────────────────┘ │ ┌─────────────────────▼───────────────────────────────┐ @@ -132,6 +132,14 @@ Resume from the last successful checkpoint: python -m seclab_taskflow_agent --resume abc123def456 ``` +The session checkpoint persists the CLI-provided `--model-config` value (if +any), so resumes use the same model configuration by default. To override the +model config on resume, pass `--model-config` / `-m` explicitly: + +```bash +python -m seclab_taskflow_agent --resume abc123def456 -m examples.model_configs.responses_api +``` + Failed tasks are automatically retried up to 3 times with increasing backoff before the session is saved. Session checkpoints are stored in the platform-specific application data directory. @@ -611,6 +619,19 @@ taskflow: The model version can then be updated by changing `gpt_latest` in the `model_config` file and applied across all taskflows that use the config. +#### CLI override + +The `model_config` can also be specified (or overridden) from the command line +with `--model-config` / `-m`. This takes precedence over any `model_config` +value defined in the taskflow YAML: + +```bash +python -m seclab_taskflow_agent -t examples.taskflows.echo -m examples.model_configs.responses_api +``` + +The CLI-provided model config is persisted in the session checkpoint, so +resumed runs automatically use the same configuration. + In addition, model specific parameters can be provided via `model_config`. To do so, define a `model_settings` section in the `model_config` file. This section has to be a dictionary with the model names as keys: ```yaml diff --git a/src/seclab_taskflow_agent/cli.py b/src/seclab_taskflow_agent/cli.py index 19e7790c..b5bf2ebc 100644 --- a/src/seclab_taskflow_agent/cli.py +++ b/src/seclab_taskflow_agent/cli.py @@ -108,6 +108,10 @@ def main( list[str] | None, typer.Argument(help="Remaining prompt text."), ] = None, + model_config: Annotated[ + str | None, + typer.Option("-m", "--model-config", help="Model configuration module path. Only relevant when running taskflows."), + ] = None, ) -> None: """Run a taskflow or personality-based agent session.""" # Debug mode from flag or env var @@ -158,7 +162,7 @@ def main( asyncio.run( run_main( available_tools, personality, effective_taskflow, - cli_globals, user_prompt, resume_session_id=resume, + cli_globals, user_prompt, resume_session_id=resume, cli_model_config=model_config, ), debug=debug, ) diff --git a/src/seclab_taskflow_agent/runner.py b/src/seclab_taskflow_agent/runner.py index 6a8aa608..12d36bd8 100644 --- a/src/seclab_taskflow_agent/runner.py +++ b/src/seclab_taskflow_agent/runner.py @@ -491,6 +491,7 @@ async def run_main( cli_globals: dict[str, str], prompt: str | None, resume_session_id: str | None = None, + cli_model_config: str | None = None, ) -> None: """Main entry point for taskflow/personality execution. @@ -501,6 +502,7 @@ async def run_main( cli_globals: Global variables from CLI. prompt: User prompt text. resume_session_id: Session ID to resume from a checkpoint. + cli_model_config: Model configuration module path, or None. """ from .session import TaskflowSession @@ -543,6 +545,9 @@ async def on_handoff_hook(context: RunContextWrapper[TContext], agent: Agent[TCo cli_globals = session.cli_globals prompt = session.prompt last_mcp_tool_results = list(session.last_tool_results) + # Restore persisted model config unless explicitly overridden + if not cli_model_config and session.cli_model_config: + cli_model_config = session.cli_model_config await render_model_output( f"** 🤖🔄 Resuming session {resume_session_id} from task {session.next_task_index}\n" ) @@ -557,6 +562,8 @@ async def on_handoff_hook(context: RunContextWrapper[TContext], agent: Agent[TCo # Resolve model config model_config_ref = taskflow_doc.model_config_ref + if cli_model_config: + model_config_ref = cli_model_config model_keys: list[str] = [] model_dict: dict[str, str] = {} models_params: dict[str, dict[str, Any]] = {} @@ -574,6 +581,7 @@ async def on_handoff_hook(context: RunContextWrapper[TContext], agent: Agent[TCo cli_globals=cli_globals, prompt=prompt or "", total_tasks=len(taskflow_doc.taskflow), + cli_model_config=cli_model_config or "", ) session.save() await render_model_output(f"** 🤖📋 Session: {session.session_id}\n") diff --git a/src/seclab_taskflow_agent/session.py b/src/seclab_taskflow_agent/session.py index c682cb92..d829da45 100644 --- a/src/seclab_taskflow_agent/session.py +++ b/src/seclab_taskflow_agent/session.py @@ -60,6 +60,9 @@ class TaskflowSession(BaseModel): finished: bool = False error: str = "" + # CLI model config override persisted for deterministic resume + cli_model_config: str = "" + # Accumulated tool results carried across tasks (used by repeat_prompt) last_tool_results: list[str] = Field(default_factory=list) diff --git a/tests/test_runner.py b/tests/test_runner.py index 35a9a555..a7713953 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -278,9 +278,93 @@ def test_task_engine_keys_override_config(self): # =================================================================== -# _build_prompts_to_run +# CLI model config override # =================================================================== +class TestCliModelConfigOverride: + """Tests for CLI model config overriding taskflow model_config_ref.""" + + def test_cli_overrides_taskflow_model_config(self): + """cli_model_config takes precedence over taskflow_doc.model_config_ref.""" + model_config_ref = self._resolve("taskflow.models.default", "cli.models.override") + assert model_config_ref == "cli.models.override" + + def test_taskflow_model_config_used_when_cli_absent(self): + """Taskflow model_config_ref is used when cli_model_config is None.""" + taskflow_ref = "taskflow.models.default" + cli_ref = None + + model_config_ref = self._resolve(taskflow_ref, cli_ref) + assert model_config_ref == taskflow_ref + + @staticmethod + def _resolve(taskflow_ref: str, cli_ref: str | None) -> str: + """Reproduce the override logic from run_main.""" + model_config_ref = taskflow_ref + if cli_ref: + model_config_ref = cli_ref + return model_config_ref + + def test_cli_model_config_resolves_via_available_tools(self): + """CLI-provided model config is resolved through _resolve_model_config.""" + at = _mock_available_tools() + at.get_model_config.return_value = _make_model_config( + models={"fast": "gpt-4o-mini"}, + ) + keys, mdict, params, api_type, backend = _resolve_model_config(at, "cli.override.ref") + at.get_model_config.assert_called_once_with("cli.override.ref") + assert mdict == {"fast": "gpt-4o-mini"} + + def test_cli_model_config_persisted_in_session(self): + """cli_model_config is stored in session for deterministic resume.""" + from seclab_taskflow_agent.session import TaskflowSession + + session = TaskflowSession( + taskflow_path="test.flow", + cli_model_config="cli.models.fast", + ) + assert session.cli_model_config == "cli.models.fast" + + def test_session_resume_restores_cli_model_config(self, tmp_path, monkeypatch): + """Resumed session restores cli_model_config when not overridden.""" + monkeypatch.setattr("seclab_taskflow_agent.session.session_dir", lambda: tmp_path) + from seclab_taskflow_agent.session import TaskflowSession + + session = TaskflowSession( + taskflow_path="test.flow", + cli_model_config="persisted.models.ref", + ) + session.save() + + loaded = TaskflowSession.load(session.session_id) + + # Simulate the resume logic from run_main + cli_model_config = None # not passed on resume + if not cli_model_config and loaded.cli_model_config: + cli_model_config = loaded.cli_model_config + + assert cli_model_config == "persisted.models.ref" + + def test_session_resume_cli_override_takes_precedence(self, tmp_path, monkeypatch): + """Explicit --model-config on resume overrides persisted value.""" + monkeypatch.setattr("seclab_taskflow_agent.session.session_dir", lambda: tmp_path) + from seclab_taskflow_agent.session import TaskflowSession + + session = TaskflowSession( + taskflow_path="test.flow", + cli_model_config="persisted.models.ref", + ) + session.save() + + loaded = TaskflowSession.load(session.session_id) + + # Simulate the resume logic from run_main with explicit override + cli_model_config = "new.override.ref" + if not cli_model_config and loaded.cli_model_config: + cli_model_config = loaded.cli_model_config + + assert cli_model_config == "new.override.ref" + class TestBuildPromptsToRun: """Tests for _build_prompts_to_run (async, run via asyncio.run).""" diff --git a/tests/test_session.py b/tests/test_session.py index f8563f95..a6c1765e 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -78,6 +78,23 @@ def test_list_sessions(self, tmp_path, monkeypatch): assert s1.session_id in ids assert s2.session_id in ids + def test_cli_model_config_persisted(self, tmp_path, monkeypatch): + """cli_model_config is persisted and restored on load.""" + monkeypatch.setattr("seclab_taskflow_agent.session.session_dir", lambda: tmp_path) + s = TaskflowSession( + taskflow_path="examples.taskflows.echo", + cli_model_config="custom.models.fast", + ) + s.save() + + loaded = TaskflowSession.load(s.session_id) + assert loaded.cli_model_config == "custom.models.fast" + + def test_cli_model_config_defaults_empty(self): + """cli_model_config defaults to empty string.""" + s = TaskflowSession(taskflow_path="test.flow") + assert s.cli_model_config == "" + class TestCompletedTask: """Tests for CompletedTask model."""