-
Notifications
You must be signed in to change notification settings - Fork 285
fix(codeact): preserve text-only model turns #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
718bb71
fix(codeact): preserve text-only model turns
furgalep 89d796c
docs(codeact): explain text-only recovery callbacks
furgalep 61b2748
fix(codeact): harden text-only recovery boundaries
furgalep 2e19381
docs(codeact): keep event documentation current
furgalep f7b4660
fix(codeact): harden callback result recovery
furgalep 131b8ad
fix(llm): reject truncated tool-call responses
furgalep df47637
fix(llm): reject truncated XML tool calls
furgalep 8b00c62
fix(codeact): redact provider output diagnostics
furgalep b882643
fix(codeact): reject incomplete text-only outputs
furgalep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,7 +195,16 @@ async def classify(self, text: str) -> Intent: ... | |
| async def implement(self, task: str) -> str: ... | ||
| ``` | ||
|
|
||
| **Constructors take `config=` only.** `PredictStrategy(max_retries=3)` and `CodeActStrategy(max_iterations=10)` are errors — wrap options in `PredictConfig(...)`/`CodeActConfig(...)`. Useful `CodeActConfig` fields: `max_iterations`, `max_retries`, `cell_timeout`, `max_tokens`, `temperature`, `max_consecutive_text_only`, `restrictions`. | ||
| **Configuration fields go through `config=`.** `PredictStrategy(max_retries=3)` and `CodeActStrategy(max_iterations=10)` are errors — wrap options in `PredictConfig(...)`/`CodeActConfig(...)`. Strategy-level extension points such as `CodeActStrategy(on_text_only=...)` remain direct constructor arguments. Useful `CodeActConfig` fields: `max_iterations`, `max_retries`, `cell_timeout`, `max_tokens`, `temperature`, `max_consecutive_text_only`, `restrictions`. | ||
|
|
||
| CodeAct preserves a model response that contains prose but no tool call. By | ||
| default it appends an `Error` asking the model to use `execute_python` or | ||
| `return_result`, then retries. For a method where prose is a valid final value, | ||
| use `CodeActStrategy(on_text_only=return_text_as_result)`. `on_text_only` | ||
| receives a sync or async callback; the callback receives | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "The action is |
||
| `TextOnlyResponseContext` and returns `TextOnlyResponseAction`. The action is | ||
| the callback result—it is not passed to `@strategy`. See | ||
| `nooa-codeact-advanced` for custom retry and synthetic-tool examples. | ||
|
|
||
| `max_iterations` is a safety net, not the main tuning dial — decompose the task instead of raising the cap. For prefill control, truncation tuning, code restrictions, and the full config surface, see `nooa-codeact-advanced`. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| --- | ||
| name: nooa-codeact-advanced | ||
| description: Advanced tuning of NOOA strategies — CodeAct prefill (understanding, disabling, custom, pre-ellipsis code), loop guards (max_iterations, retries, text-only stop), truncation tuning (TruncationConfig/CaptureConfig/FormatConfig), code restrictions (RestrictionsConfig), execution environment internals, and PredictStrategy tuning (retries, param guards, output_serialization). Use when configuring CodeActConfig or PredictConfig beyond defaults, writing a custom prefill, restricting generated code, or debugging truncation/eviction behavior. | ||
| description: Advanced tuning of NOOA strategies — CodeAct prefill, loop guards, text-only recovery callbacks, truncation, code restrictions, execution internals, and PredictStrategy tuning. Use when configuring CodeActConfig or PredictConfig beyond defaults, handling models that return prose instead of tool calls, writing a custom prefill, restricting generated code, or debugging truncation and eviction. | ||
| compatibility: nooa package | ||
| --- | ||
|
|
||
|
|
@@ -10,7 +10,7 @@ The authoring basics are in `nooa-agent-authoring`. This skill covers the deep c | |
|
|
||
| ## Config plumbing rules (read first) | ||
|
|
||
| - Strategy constructors take **`config=` only**: `CodeActStrategy(config=CodeActConfig(...))`, `PredictStrategy(PredictConfig(...))`. Flat kwargs (`CodeActStrategy(max_iterations=10)`, `CodeActStrategy(prefill=...)`) do not exist, despite some docstring examples. | ||
| - Configuration fields go through **`config=`**: `CodeActStrategy(config=CodeActConfig(...))`, `PredictStrategy(PredictConfig(...))`. Flat config kwargs (`CodeActStrategy(max_iterations=10)`, `CodeActStrategy(prefill=...)`) do not exist. Strategy-level extension points such as `CodeActStrategy(on_text_only=...)` and `CodeActStrategy(error_formatter=...)` are separate keyword arguments. | ||
| - All config objects are frozen Pydantic models with `merge_with(other)`: only fields explicitly set on `other` override. **Configs must be freshly constructed** — anything round-tripped through `model_dump()`/`model_validate()` has an empty `model_fields_set` and `merge_with` raises. | ||
| - Truncation layers: framework default → `Agent` class kwarg `truncation=` → instance kwarg → `@strategy(..., truncation=...)` per method. `TruncationConfig.merge_with` deep-merges sub-configs field-by-field; the strategy configs merge flat. | ||
|
|
||
|
|
@@ -42,15 +42,71 @@ class Notifier(Agent, llm=llm): | |
| | `max_iterations` | `None` | **Unlimited.** The loop then stops only on completion, the error budget, or a hard abort. | | ||
| | `max_retries` | `3` | **Cumulative session error budget, not consecutive** (the counter is never reset). LLM API errors, bad tool JSON, empty code, and `return_result` validation failures all count. | | ||
| | `max_consecutive_text_only` | `3` | Consecutive no-tool-call text replies before hard abort; `0` disables. Any real tool call resets the counter. | | ||
| | `text_only_stop_behavior` | `"return_result"` | Text-only reply → try to validate the text as the final result; on failure, a visible correction `Error` is added. `"synthetic_reasoning"` instead converts the text to a no-op `reasoning(...)` cell whose tool result says the task is NOT finished. | | ||
| | `cell_timeout` | `None` | Per-cell `asyncio.wait_for` limit in seconds; `None` = unlimited. Cannot interrupt a truly blocking sync syscall — that's what the blocking-call AST validation is for. | | ||
| | `max_tokens` / `temperature` / `top_p` | `None` | Passed to every generation call when set (model defaults otherwise). On empty responses with `finish_reason="length"` CodeAct aborts and tells you to raise `max_tokens` (16384+ for reasoning models). | | ||
| | `translate_tool_calls` | `False` | When a weak model calls an agent method directly as a tool (instead of via `execute_python`), rewrite it into equivalent code and run it — teaching the right pattern. Off = error listing the two valid tools. | | ||
| | `restrictions` | `RestrictionsConfig()` | See Restrictions below. | | ||
| | `prefill` | `InspectInputsPrefill()` | See Prefill above. | | ||
| | `max_tool_calls` | `None` | **Dead — declared but never read.** Setting it does nothing. | | ||
|
|
||
| `tool_choice` is hardcoded `"auto"`. There is no `allow_text_response` option (older docs mention one) — text handling is entirely the two text-only knobs. | ||
| `tool_choice` is hardcoded `"auto"`. | ||
|
|
||
| ## Text-only recovery callback | ||
|
|
||
| CodeAct expects each model turn to call `execute_python` or `return_result`. | ||
| When a model emits only prose, NOOA preserves that exact assistant turn and | ||
| then invokes the strategy's `on_text_only` callback. | ||
|
|
||
| Do not pass `TextOnlyResponseAction` to `@strategy`. The objects have distinct | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The action is remove. |
||
| roles: | ||
|
|
||
| ```text | ||
| @strategy attaches CodeActStrategy to a method | ||
| -> CodeActStrategy(on_text_only=handler) registers the callback | ||
| -> handler(TextOnlyResponseContext) returns TextOnlyResponseAction | ||
| ``` | ||
|
|
||
| The callback may be synchronous or asynchronous. Its context contains the | ||
| untouched `LLMResponse`, normalized text, current method call, and declared | ||
| return type. Its action chooses one of three append-only recovery paths: | ||
|
|
||
| | Action constructor | Effect | | ||
| |---|---| | ||
| | `TextOnlyResponseAction.return_result(value)` | Validate `value` through CodeAct's normal result path. Invalid values produce model-visible correction feedback. | | ||
| | `TextOnlyResponseAction.retry(*events)` | Append feedback events, then ask the model again. | | ||
| | `TextOnlyResponseAction.tool_calls(*calls)` | Run synthetic calls while retaining the original assistant turn. Use `execute_python`, not TUI-only tools. | | ||
|
|
||
| The safe default is `retry_text_only_response`: it appends an `Error` asking | ||
| the model to call `return_result(value)` or `execute_python(code)`, then retries. | ||
| Opt into accepting prose only when that is appropriate for the method contract: | ||
|
|
||
| ```python | ||
| from nooa import Agent, CodeActStrategy, return_text_as_result, strategy | ||
|
|
||
|
|
||
| class Summarizer(Agent, llm=llm): | ||
| @strategy(CodeActStrategy(on_text_only=return_text_as_result)) | ||
| async def summarize(self, text: str) -> str: | ||
| """Summarize the text.""" | ||
| ... | ||
| ``` | ||
|
|
||
| For custom policy, return an action from the callback: | ||
|
|
||
| ```python | ||
| from nooa import TextOnlyResponseAction, TextOnlyResponseContext | ||
| from nooa.events import Error | ||
|
|
||
|
|
||
| def require_tool(context: TextOnlyResponseContext) -> TextOnlyResponseAction: | ||
| return TextOnlyResponseAction.retry( | ||
| Error(content=f"Plain text cannot finish {context.call.method_name}; call a tool.") | ||
| ) | ||
|
|
||
|
|
||
| @strategy(CodeActStrategy(on_text_only=require_tool)) | ||
| async def investigate(self, question: str) -> str: ... | ||
| ``` | ||
|
|
||
| ## `return_result` mechanics | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an internal note based on an agent conversation, not general documentation. Make sure this is just documentation.