Skip to content

feat(agent): implement event-driven sub-turn PoC with context cancell…#1585

Closed
lppp04808 wants to merge 12 commits intosipeed:refactor/agentfrom
lppp04808:feat/subturn-poc
Closed

feat(agent): implement event-driven sub-turn PoC with context cancell…#1585
lppp04808 wants to merge 12 commits intosipeed:refactor/agentfrom
lppp04808:feat/subturn-poc

Conversation

@lppp04808
Copy link

📝 Description

This PR introduces the initial Proof of Concept (PoC) for hierarchical agent execution (Sub-turns). This is a foundational step to enable multi-agent steering and interruptability within the execution loop, as
proposed in Issue #1316.

By allowing agents to safely spawn, monitor, and cancel sub-agents (sub-turns) without polluting the main session history, we lay the groundwork for more complex, multi-layered reasoning and tool execution.

Key Changes

  • Core Lifecycle Management: Added spawnSubTurn to manage nested turn executions safely, including a configurable depth limit (maxSubTurnDepth).
  • Ephemeral Sessions: Implemented newEphemeralSession to ensure sub-turns use isolated memory. This prevents the parent's session history from being polluted by intermediate child reasoning.
  • Context & Interruptability: Sub-turns now inherit a cancelable context (context.WithCancel(ctx)). If a parent turn finishes (ts.Finish()), all running children are automatically aborted, effectively
    preventing goroutine leaks.
  • EventBus Integration (Mock): Introduced core events (SubTurnSpawnEvent, SubTurnResultDeliveredEvent, SubTurnEndEvent, SubTurnOrphanResultEvent) for full observability into the sub-turn lifecycle.
    Currently utilizing a MockEventBus for the PoC.
  • Concurrency & Robustness:
    • Added deferred panic recovery within sub-turns to guarantee the emission of SubTurnEndEvent(Err).
    • Handled "Orphan Results": If a delayed child result arrives after the parent has already finished, it routes to SubTurnOrphanResultEvent to avoid state corruption or history pollution.

Notes for Reviewers

  • This is currently a PoC and uses a mocked EventBus (MockEventBus) and fixed-size channels for pending results.
  • Further iterations will focus on integrating the real EventBus and handling dynamic channel capacities / backpressure for high concurrency scenarios.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

📚 Technical Context (Skip for Docs)

  • Reference URL:
  • Reasoning:

🧪 Test Environment

  • Hardware: PC
  • OS: Ubuntu 24.04
  • Model/Provider:
  • Channels:

📸 Evidence (Optional)

Click to view Logs/Screenshots

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

afjcjsbx and others added 7 commits March 12, 2026 18:58
* Add support for azure openai provider

* Add checks for deployment model name

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Addressing @Copilot suggestion to remove the init() function which seemed redundant

* Fix readme

* Fix linting checks

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…tting

feat(logger): Custom console formatter for JSON and multiline strings
…ation (sipeed#1316)

This introduces the initial Proof of Concept for hierarchical agent execution (Sub-turns) to enable multi-agent steering and interruptability as designed in Issue sipeed#1316.

Key implementations:
- Core Lifecycle: Added spawnSubTurn to manage nested turn executions with depth limits.
- Ephemeral Sessions: Sub-turns now use isolated memory (newEphemeralSession) to prevent polluting the parent's history.
- EventBus Integration: Implemented SubTurnSpawnEvent, SubTurnResultDeliveredEvent, and SubTurnEndEvent for full observability.
- Context & Interrupts: Sub-turns inherit a cancelable context (context.WithCancel(ctx)) from the parent. When a parent finishes (ts.Finish()), all running children are automatically aborted to prevent goroutine leaks.
- Concurrency & Robustness:
  - Added deferred panic recovery to guarantee the emission of SubTurnEndEvent(Err).
  - Implemented Orphan Result Routing (SubTurnOrphanResultEvent) to handle edge cases where a delayed child result arrives after the parent has finished, preventing history pollution.

Ref: sipeed#1316
afjcjsbx and others added 2 commits March 16, 2026 13:45
* feat(agent): steering

* fix loop

* fix lint

* fix lint
…ionStore for ephemeral

- Remove duplicated type definitions (ToolResult, Session, Message) → use official types:
  - tools.ToolResult (from pkg/tools/result.go)
  - session.SessionStore + providers.Message (from pkg/session/session_store.go)
- Change turnState.session from *Session to session.SessionStore
  - Ephemeral session now uses a pure in-memory implementation (no persistence)
- Prepare runTurn() for real integration:
  - Placeholder remains, but designed to accept AgentInstance
  - Ready to hook into runAgentLoop / runLLMIteration once steering/turnState lands
- Rebased onto refactor/agent (steering merged), resolves type mismatches

This makes the sub-turn PoC more compatible with the ongoing agent refactor (sipeed#1316).
@lppp04808 lppp04808 changed the base branch from main to refactor/agent March 16, 2026 06:03
@lppp04808 lppp04808 closed this Mar 16, 2026
@lppp04808 lppp04808 deleted the feat/subturn-poc branch March 16, 2026 06:05
@lppp04808
Copy link
Author

move to #1636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain: agent go Pull requests that update go code type: enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants