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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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│
└─────────────────────┬───────────────────────────────┘
│
┌─────────────────────▼───────────────────────────────┐
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/seclab_taskflow_agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Comment thread
anticomputer marked this conversation as resolved.
Resolved
) -> None:
"""Run a taskflow or personality-based agent session."""
# Debug mode from flag or env var
Expand Down Expand Up @@ -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,
)
Expand Down
8 changes: 8 additions & 0 deletions src/seclab_taskflow_agent/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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"
)
Expand All @@ -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
Comment thread
anticomputer marked this conversation as resolved.
Resolved
model_keys: list[str] = []
Comment thread
anticomputer marked this conversation as resolved.
Resolved
model_dict: dict[str, str] = {}
models_params: dict[str, dict[str, Any]] = {}
Expand All @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/seclab_taskflow_agent/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
86 changes: 85 additions & 1 deletion tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
anticomputer marked this conversation as resolved.
Fixed
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)."""

Expand Down
17 changes: 17 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Loading