Skip to content

Commit a898e69

Browse files
Merge pull request #232 from GitHubSecurityLab/cli_model_config
add cli argument for model config
2 parents 171a88a + b27d8b3 commit a898e69

6 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ You can find a detailed overview of the taskflow grammar [here](doc/GRAMMAR.md)
2424
```
2525
┌─────────────────────────────────────────────────────┐
2626
│ CLI (cli.py) │
27-
│ Typer-based entry point: -p, -t, -l, -g, --resume │
27+
│ Typer-based entry point: -p, -t, -l, -g, -m, --resume│
2828
└─────────────────────┬───────────────────────────────┘
2929
│
3030
┌─────────────────────▼───────────────────────────────┐
@@ -132,6 +132,14 @@ Resume from the last successful checkpoint:
132132
python -m seclab_taskflow_agent --resume abc123def456
133133
```
134134

135+
The session checkpoint persists the CLI-provided `--model-config` value (if
136+
any), so resumes use the same model configuration by default. To override the
137+
model config on resume, pass `--model-config` / `-m` explicitly:
138+
139+
```bash
140+
python -m seclab_taskflow_agent --resume abc123def456 -m examples.model_configs.responses_api
141+
```
142+
135143
Failed tasks are automatically retried up to 3 times with increasing backoff
136144
before the session is saved. Session checkpoints are stored in the
137145
platform-specific application data directory.
@@ -611,6 +619,19 @@ taskflow:
611619

612620
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.
613621

622+
#### CLI override
623+
624+
The `model_config` can also be specified (or overridden) from the command line
625+
with `--model-config` / `-m`. This takes precedence over any `model_config`
626+
value defined in the taskflow YAML:
627+
628+
```bash
629+
python -m seclab_taskflow_agent -t examples.taskflows.echo -m examples.model_configs.responses_api
630+
```
631+
632+
The CLI-provided model config is persisted in the session checkpoint, so
633+
resumed runs automatically use the same configuration.
634+
614635
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:
615636

616637
```yaml

‎src/seclab_taskflow_agent/cli.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def main(
108108
list[str] | None,
109109
typer.Argument(help="Remaining prompt text."),
110110
] = None,
111+
model_config: Annotated[
112+
str | None,
113+
typer.Option("-m", "--model-config", help="Model configuration module path. Only relevant when running taskflows."),
114+
] = None,
111115
) -> None:
112116
"""Run a taskflow or personality-based agent session."""
113117
# Debug mode from flag or env var
@@ -158,7 +162,7 @@ def main(
158162
asyncio.run(
159163
run_main(
160164
available_tools, personality, effective_taskflow,
161-
cli_globals, user_prompt, resume_session_id=resume,
165+
cli_globals, user_prompt, resume_session_id=resume, cli_model_config=model_config,
162166
),
163167
debug=debug,
164168
)

‎src/seclab_taskflow_agent/runner.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ async def run_main(
491491
cli_globals: dict[str, str],
492492
prompt: str | None,
493493
resume_session_id: str | None = None,
494+
cli_model_config: str | None = None,
494495
) -> None:
495496
"""Main entry point for taskflow/personality execution.
496497
@@ -501,6 +502,7 @@ async def run_main(
501502
cli_globals: Global variables from CLI.
502503
prompt: User prompt text.
503504
resume_session_id: Session ID to resume from a checkpoint.
505+
cli_model_config: Model configuration module path, or None.
504506
"""
505507
from .session import TaskflowSession
506508

@@ -543,6 +545,9 @@ async def on_handoff_hook(context: RunContextWrapper[TContext], agent: Agent[TCo
543545
cli_globals = session.cli_globals
544546
prompt = session.prompt
545547
last_mcp_tool_results = list(session.last_tool_results)
548+
# Restore persisted model config unless explicitly overridden
549+
if not cli_model_config and session.cli_model_config:
550+
cli_model_config = session.cli_model_config
546551
await render_model_output(
547552
f"** 🤖🔄 Resuming session {resume_session_id} from task {session.next_task_index}\n"
548553
)
@@ -557,6 +562,8 @@ async def on_handoff_hook(context: RunContextWrapper[TContext], agent: Agent[TCo
557562

558563
# Resolve model config
559564
model_config_ref = taskflow_doc.model_config_ref
565+
if cli_model_config:
566+
model_config_ref = cli_model_config
560567
model_keys: list[str] = []
561568
model_dict: dict[str, str] = {}
562569
models_params: dict[str, dict[str, Any]] = {}
@@ -574,6 +581,7 @@ async def on_handoff_hook(context: RunContextWrapper[TContext], agent: Agent[TCo
574581
cli_globals=cli_globals,
575582
prompt=prompt or "",
576583
total_tasks=len(taskflow_doc.taskflow),
584+
cli_model_config=cli_model_config or "",
577585
)
578586
session.save()
579587
await render_model_output(f"** 🤖📋 Session: {session.session_id}\n")

‎src/seclab_taskflow_agent/session.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class TaskflowSession(BaseModel):
6060
finished: bool = False
6161
error: str = ""
6262

63+
# CLI model config override persisted for deterministic resume
64+
cli_model_config: str = ""
65+
6366
# Accumulated tool results carried across tasks (used by repeat_prompt)
6467
last_tool_results: list[str] = Field(default_factory=list)
6568

‎tests/test_runner.py‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,93 @@ def test_task_engine_keys_override_config(self):
278278

279279

280280
# ===================================================================
281-
# _build_prompts_to_run
281+
# CLI model config override
282282
# ===================================================================
283283

284+
class TestCliModelConfigOverride:
285+
"""Tests for CLI model config overriding taskflow model_config_ref."""
286+
287+
def test_cli_overrides_taskflow_model_config(self):
288+
"""cli_model_config takes precedence over taskflow_doc.model_config_ref."""
289+
model_config_ref = self._resolve("taskflow.models.default", "cli.models.override")
290+
assert model_config_ref == "cli.models.override"
291+
292+
def test_taskflow_model_config_used_when_cli_absent(self):
293+
"""Taskflow model_config_ref is used when cli_model_config is None."""
294+
taskflow_ref = "taskflow.models.default"
295+
cli_ref = None
296+
297+
model_config_ref = self._resolve(taskflow_ref, cli_ref)
298+
assert model_config_ref == taskflow_ref
299+
300+
@staticmethod
301+
def _resolve(taskflow_ref: str, cli_ref: str | None) -> str:
302+
"""Reproduce the override logic from run_main."""
303+
model_config_ref = taskflow_ref
304+
if cli_ref:
305+
model_config_ref = cli_ref
306+
return model_config_ref
307+
308+
def test_cli_model_config_resolves_via_available_tools(self):
309+
"""CLI-provided model config is resolved through _resolve_model_config."""
310+
at = _mock_available_tools()
311+
at.get_model_config.return_value = _make_model_config(
312+
models={"fast": "gpt-4o-mini"},
313+
)
314+
keys, mdict, params, api_type, backend = _resolve_model_config(at, "cli.override.ref")
315+
at.get_model_config.assert_called_once_with("cli.override.ref")
316+
assert mdict == {"fast": "gpt-4o-mini"}
317+
318+
def test_cli_model_config_persisted_in_session(self):
319+
"""cli_model_config is stored in session for deterministic resume."""
320+
from seclab_taskflow_agent.session import TaskflowSession
321+
322+
session = TaskflowSession(
323+
taskflow_path="test.flow",
324+
cli_model_config="cli.models.fast",
325+
)
326+
assert session.cli_model_config == "cli.models.fast"
327+
328+
def test_session_resume_restores_cli_model_config(self, tmp_path, monkeypatch):
329+
"""Resumed session restores cli_model_config when not overridden."""
330+
monkeypatch.setattr("seclab_taskflow_agent.session.session_dir", lambda: tmp_path)
331+
from seclab_taskflow_agent.session import TaskflowSession
332+
333+
session = TaskflowSession(
334+
taskflow_path="test.flow",
335+
cli_model_config="persisted.models.ref",
336+
)
337+
session.save()
338+
339+
loaded = TaskflowSession.load(session.session_id)
340+
341+
# Simulate the resume logic from run_main
342+
cli_model_config = None # not passed on resume
343+
if not cli_model_config and loaded.cli_model_config:
344+
cli_model_config = loaded.cli_model_config
345+
346+
assert cli_model_config == "persisted.models.ref"
347+
348+
def test_session_resume_cli_override_takes_precedence(self, tmp_path, monkeypatch):
349+
"""Explicit --model-config on resume overrides persisted value."""
350+
monkeypatch.setattr("seclab_taskflow_agent.session.session_dir", lambda: tmp_path)
351+
from seclab_taskflow_agent.session import TaskflowSession
352+
353+
session = TaskflowSession(
354+
taskflow_path="test.flow",
355+
cli_model_config="persisted.models.ref",
356+
)
357+
session.save()
358+
359+
loaded = TaskflowSession.load(session.session_id)
360+
361+
# Simulate the resume logic from run_main with explicit override
362+
cli_model_config = "new.override.ref"
363+
if not cli_model_config and loaded.cli_model_config:
364+
cli_model_config = loaded.cli_model_config
365+
366+
assert cli_model_config == "new.override.ref"
367+
284368
class TestBuildPromptsToRun:
285369
"""Tests for _build_prompts_to_run (async, run via asyncio.run)."""
286370

‎tests/test_session.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ def test_list_sessions(self, tmp_path, monkeypatch):
7878
assert s1.session_id in ids
7979
assert s2.session_id in ids
8080

81+
def test_cli_model_config_persisted(self, tmp_path, monkeypatch):
82+
"""cli_model_config is persisted and restored on load."""
83+
monkeypatch.setattr("seclab_taskflow_agent.session.session_dir", lambda: tmp_path)
84+
s = TaskflowSession(
85+
taskflow_path="examples.taskflows.echo",
86+
cli_model_config="custom.models.fast",
87+
)
88+
s.save()
89+
90+
loaded = TaskflowSession.load(s.session_id)
91+
assert loaded.cli_model_config == "custom.models.fast"
92+
93+
def test_cli_model_config_defaults_empty(self):
94+
"""cli_model_config defaults to empty string."""
95+
s = TaskflowSession(taskflow_path="test.flow")
96+
assert s.cli_model_config == ""
97+
8198

8299
class TestCompletedTask:
83100
"""Tests for CompletedTask model."""

0 commit comments

Comments
 (0)