Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(classic): preserve action history across task continuations (#12673)

Nicholas Tindle98acc4b3d3d9
diff --git a/autogpt_platform/backend/README.md b/autogpt_platform/backend/README.md
index d8c275e7b6..f1303cca9a 100644
--- a/autogpt_platform/backend/README.md
+++ b/autogpt_platform/backend/README.md
@@ -1 +1,3 @@
+# AutoGPT Platform — Backend
+
[Getting Started (Released)](https://docs.agpt.co/platform/getting-started/#autogpt_agent_server)
\ No newline at end of file
diff --git a/classic/forge/forge/components/image_gen/image_gen.py b/classic/forge/forge/components/image_gen/image_gen.py
index 193dfaae05..15fd094868 100644
--- a/classic/forge/forge/components/image_gen/image_gen.py
+++ b/classic/forge/forge/components/image_gen/image_gen.py
@@ -25,6 +25,9 @@ logger = logging.getLogger(__name__)
class ImageGeneratorConfiguration(BaseModel):
image_provider: Literal["dalle", "huggingface", "sdwebui"] = "dalle"
+ dalle_model: str = "gpt-image-1"
+ """OpenAI image model. dall-e-2/3 were removed from the API on 2026-05-12;
+ gpt-image-1 returns base64 PNGs and supports 1024x1024 square output."""
huggingface_image_model: str = "CompVis/stable-diffusion-v1-4"
huggingface_api_token: Optional[SecretStr] = UserConfigurable(
None, from_env="HUGGINGFACE_API_TOKEN", exclude=True
@@ -72,7 +75,7 @@ class ImageGeneratorComponent(
),
"size": JSONSchema(
type=JSONSchema.Type.INTEGER,
- description="The size of the image [256, 512, 1024]",
+ description="The size of the image in pixels (square); 1024",
required=False,
),
},
@@ -171,7 +174,7 @@ class ImageGeneratorComponent(
def generate_image_with_dalle(
self, prompt: str, output_file: Path, size: int
) -> str:
- """Generate an image with DALL-E.
+ """Generate an image with OpenAI's image API (gpt-image-1).
Args:
prompt (str): The prompt to use
@@ -183,14 +186,14 @@ class ImageGeneratorComponent(
"""
assert self.openai_credentials # otherwise this tool is disabled
- # Check for supported image sizes
- if size not in [256, 512, 1024]:
- closest = min([256, 512, 1024], key=lambda x: abs(x - size))
+ # gpt-image-1 only supports 1024x1024 for square output
+ # (plus 1536x1024 / 1024x1536, which this command does not use).
+ if size != 1024:
logger.info(
- "DALL-E only supports image sizes of 256x256, 512x512, or 1024x1024. "
- f"Setting to {closest}, was {size}."
+ "gpt-image-1 only supports 1024x1024 square images. "
+ f"Setting size to 1024, was {size}."
)
- size = closest
+ size = 1024
# TODO: integrate in `forge.llm.providers`(?)
response = OpenAI(
@@ -201,20 +204,25 @@ class ImageGeneratorComponent(
else None
),
).images.generate(
+ model=self.config.dalle_model,
prompt=prompt,
n=1,
# TODO: improve typing of size config item(s)
size=f"{size}x{size}", # type: ignore
- response_format="b64_json",
)
- # response_format="b64_json" guarantees b64_json is present
- image_b64 = response.data[0].b64_json # type: ignore[index]
- assert image_b64 is not None
+ # Depending on the model, the image is returned either as base64 JSON
+ # or as a temporary URL. (response_format is not accepted by all
+ # models, so we don't request a specific one and handle both.)
+ image = response.data[0] # type: ignore[index]
+ if image.b64_json:
+ image_data = b64decode(image.b64_json)
+ elif image.url:
+ image_data = requests.get(image.url, timeout=30).content
+ else:
+ raise RuntimeError("DALL-E response contained no image data")
logger.info(f"Image Generated for prompt: {prompt}")
- image_data = b64decode(image_b64)
-
with open(output_file, mode="wb") as png:
png.write(image_data)
diff --git a/classic/forge/forge/components/image_gen/test_image_gen.py b/classic/forge/forge/components/image_gen/test_image_gen.py
index 10d1d3fdc4..f5986009f6 100644
--- a/classic/forge/forge/components/image_gen/test_image_gen.py
+++ b/classic/forge/forge/components/image_gen/test_image_gen.py
@@ -43,15 +43,17 @@ def image_size(request):
@pytest.mark.skipif(not OPENAI_API_KEY, reason="OPENAI_API_KEY not set")
-def test_dalle(
- image_gen_component: ImageGeneratorComponent,
- image_size,
-):
- """Test DALL-E image generation."""
+def test_dalle(image_gen_component: ImageGeneratorComponent):
+ """Test image generation via OpenAI's images API (gpt-image-1).
+
+ gpt-image-1 only returns 1024x1024 for square output, so this is the
+ only size exercised here (dall-e-2/3 and their 256/512 sizes were
+ removed from the API on 2026-05-12).
+ """
generate_and_validate(
image_gen_component,
image_provider="dalle",
- image_size=image_size,
+ image_size=1024,
)
diff --git a/classic/forge/tests/test_action_history_cursor.py b/classic/forge/tests/test_action_history_cursor.py
index 6dbcfc5c92..404b364b0c 100644
--- a/classic/forge/tests/test_action_history_cursor.py
+++ b/classic/forge/tests/test_action_history_cursor.py
@@ -1,10 +1,8 @@
-"""Test for cursor reset bug when clearing episode history between tasks.
+"""Tests for EpisodicActionHistory cursor safety and task continuation.
-Reproduces: IndexError in EpisodicActionHistory.current_episode when
-episodes.clear() is called without resetting cursor to 0.
-
-This is the exact crash from run_interaction_loop when the user starts a
-second task after finishing the first one.
+Covers:
+- Cursor >= len guard in current_episode (prevents IndexError)
+- History preserved across task changes (no clearing)
"""
from unittest.mock import MagicMock
@@ -16,42 +14,14 @@ def _make_history_with_episodes(n: int) -> EpisodicActionHistory:
"""Create a history with n completed episodes (cursor advanced past all)."""
history = EpisodicActionHistory()
for i in range(n):
- # Directly append mock episodes and advance cursor,
- # simulating what register_action + register_result does
ep = MagicMock()
- ep.result = MagicMock() # has a result = completed
+ ep.result = MagicMock()
history.episodes.append(ep)
history.cursor += 1
return history
-class TestEpisodicActionHistoryCursorReset:
- def test_current_episode_after_clear_without_cursor_reset_crashes(self):
- """REPRODUCER: This is the exact bug.
-
- After completing a task, the interaction loop clears episodes but
- doesn't reset cursor. On the next task, current_episode does
- `self[self.cursor]` where cursor > len(episodes) -> IndexError.
- """
- history = _make_history_with_episodes(2)
- assert history.cursor == 2
- assert len(history.episodes) == 2
-
- # This is what main.py line 759 does between tasks:
- history.episodes.clear()
-
- # cursor is still 2, but episodes is empty
- assert history.cursor == 2
- assert len(history.episodes) == 0
-
- # This is what main.py line 687 calls at the start of the next task.
- # BUG: cursor (2) != len(episodes) (0), so it falls through to
- # self.episodes[2] on an empty list -> IndexError
- #
- # After the fix, this should return None (no current episode).
- result = history.current_episode
- assert result is None
-
+class TestEpisodicActionHistoryCursor:
def test_current_episode_returns_none_on_empty_history(self):
history = EpisodicActionHistory()
assert history.current_episode is None
@@ -64,26 +34,91 @@ class TestEpisodicActionHistoryCursorReset:
def test_current_episode_returns_episode_when_cursor_valid(self):
history = EpisodicActionHistory()
ep = MagicMock()
- ep.result = None # not yet completed
+ ep.result = None
history.episodes.append(ep)
history.cursor = 0
assert history.current_episode is ep
- def test_clear_and_reset_allows_new_task(self):
- """After properly clearing episodes AND resetting cursor,
- the history should work correctly for a new task."""
- history = _make_history_with_episodes(3)
+ def test_cursor_beyond_episodes_returns_none(self):
+ """Any cursor value beyond the episode list should return None."""
+ history = EpisodicActionHistory()
+ history.cursor = 100
+ assert history.current_episode is None
- # Clean reset between tasks
+ def test_cursor_safe_after_clear(self):
+ """Even if episodes are cleared without resetting cursor,
+ current_episode must not crash (>= guard)."""
+ history = _make_history_with_episodes(2)
history.episodes.clear()
- history.cursor = 0
-
+ assert history.cursor == 2
assert history.current_episode is None
- assert len(history) == 0
- def test_cursor_beyond_episodes_returns_none(self):
- """Any cursor value beyond the episode list should return None,
- not raise IndexError."""
+
+class TestHistoryPreservedAcrossTasks:
+ def test_episodes_survive_task_change(self):
+ """When user starts a new task, episodes from the previous task
+ should still be present — the compression system handles overflow."""
+ history = _make_history_with_episodes(3)
+ assert len(history.episodes) == 3
+ assert history.cursor == 3
+
+ # Simulate what main.py does on task change (no clearing)
+ # history is untouched — episodes remain
+
+ assert len(history.episodes) == 3
+ assert history.current_episode is None # cursor at end
+
+ def test_new_episode_appends_after_previous(self):
+ """New task actions append to existing history."""
+ history = _make_history_with_episodes(2)
+
+ # New task starts — add a new episode
+ new_ep = MagicMock()
+ new_ep.result = None
+ history.episodes.append(new_ep)
+ # cursor still at 2, which is now the new episode
+ assert history.current_episode is new_ep
+ assert len(history.episodes) == 3
+
+
+class TestFinishContinuationFlow:
+ """Integration-style test for the AgentFinished -> new task flow.
+
+ Mirrors run_interaction_loop in
+ classic/original_autogpt/autogpt/app/main.py: when the agent proposes
+ `finish`, the action is registered but AgentFinished is caught before
+ execute() can record a result, leaving the episode open (result=None).
+ The loop interprets an open episode as "reuse this proposal", so without
+ closing it the agent would replay the finish proposal forever. main.py
+ closes it by registering an ActionSuccessResult before the next task.
+ """
+
+ def test_finish_then_continuation_proposes_fresh_action(self):
+ from forge.models.action import ActionSuccessResult
+
history = EpisodicActionHistory()
- history.cursor = 100 # way past empty list
+
+ # 1. Finish action proposed and registered; result not yet recorded.
+ finish_episode = MagicMock()
+ finish_episode.result = None
+ history.episodes.append(finish_episode)
+ assert history.current_episode is finish_episode
+ assert finish_episode.result is None
+
+ # 2. AgentFinished caught -> main.py closes the episode via
+ # register_result so the loop won't reuse the finish proposal.
+ history.register_result(ActionSuccessResult(outputs="task complete"))
+
+ # 3. Episode is closed -> current_episode is None, so the next cycle
+ # proposes a fresh action instead of replaying the stale finish.
+ assert finish_episode.result is not None
assert history.current_episode is None
+
+ # 4. New task: a new episode appends and becomes current; the prior
+ # finish episode is preserved (history is kept across tasks).
+ next_episode = MagicMock()
+ next_episode.result = None
+ history.episodes.append(next_episode)
+ assert history.current_episode is next_episode
+ assert len(history.episodes) == 2
+ assert history.episodes[0] is finish_episode
diff --git a/classic/original_autogpt/autogpt/app/main.py b/classic/original_autogpt/autogpt/app/main.py
index e78e393acb..75ae226f67 100644
--- a/classic/original_autogpt/autogpt/app/main.py
+++ b/classic/original_autogpt/autogpt/app/main.py
@@ -35,7 +35,11 @@ from forge.file_storage import FileStorageBackendName, get_storage
from forge.llm.providers import MultiProvider
from forge.logging.config import configure_logging
from forge.logging.utils import print_attribute
-from forge.models.action import ActionInterruptedByHuman, ActionProposal
+from forge.models.action import (
+ ActionInterruptedByHuman,
+ ActionProposal,
+ ActionSuccessResult,
+)
from forge.models.utils import ModelWithSummary
from forge.permissions import ApprovalScope, CommandPermissionManager
from forge.utils.const import FINISH_COMMAND
@@ -754,10 +758,19 @@ async def run_interaction_loop(
logger.info("User chose to exit after task completion.")
return
- # Start new task in same workspace
+ # Close the finish episode so the loop doesn't reuse it.
+ # AgentFinished is caught before execute() can register
+ # a result, leaving result=None — which the loop
+ # interprets as "episode in progress, reuse proposal".
+ # Guard against a missing/closed episode so register_result
+ # never raises if AgentFinished propagates from elsewhere.
+ if (ep := agent.event_history.current_episode) and not ep.result:
+ agent.event_history.register_result(
+ ActionSuccessResult(outputs=e.message)
+ )
+
+ # Start new task in same workspace, keeping prior context
agent.state.task = next_task
- agent.event_history.episodes.clear() # Clear history for fresh context
- agent.event_history.cursor = 0
# Reset cycle budget for new task
cycles_remaining = _get_cycle_budget(