Skip to content

Phase 2.1: CLI Core Agent Loop — Query → API → Tools → Repeat#359

Merged
Delqhi merged 1 commit intomainfrom
feat/cli-core-agent-loop-2
Apr 4, 2026
Merged

Phase 2.1: CLI Core Agent Loop — Query → API → Tools → Repeat#359
Delqhi merged 1 commit intomainfrom
feat/cli-core-agent-loop-2

Conversation

@Delqhi
Copy link
Copy Markdown
Member

@Delqhi Delqhi commented Apr 4, 2026

Summary

  • Core Agent Loop: Full implementation of the query → API call → tool execution → response cycle in packages/opensin-sdk/src/agent_loop/
  • NDJSON Streaming: Every event (status, thinking, text_delta, tool_call_start/end, tool_result, error, retry, context_compressed, turn_complete, session_start/end) emitted as newline-delimited JSON to stdout
  • Error Recovery: Exponential backoff with jitter for transient errors (rate limits, 429/502/503/504, timeouts, network errors); non-transient errors fail immediately without retry
  • Tool Result Processing: Tool calls detected from LLM response, executed via ToolRegistry, results re-injected into context for next LLM turn — loop continues until no more tool calls
  • Context Window Management: Auto-compression when utilization exceeds configurable threshold, system message preservation, priority-based message retention, conversation clearing
  • Bug Fix: Fixed pre-existing bug in context_mgmt/compressor.ts where originalTokens was computed but never passed to sub-methods

Architecture

src/agent_loop/
├── types.ts          # All type definitions (AgentEvent, AgentLoopConfig, TurnState, LLMResponse, etc.)
├── agent_loop.ts     # Core AgentLoop class — the main loop engine
├── context.ts        # AgentLoopContext — context window management with auto-compression
├── ndjson.ts         # NDJSON serialization/parsing/streaming utilities
├── tool_registry.ts  # ToolRegistry — tool registration and execution dispatch
└── index.ts          # Public API exports

Acceptance Criteria

  • Core loop runs continuously (until no tool calls or max iterations)
  • Handles tool results correctly (re-injects into context, continues loop)
  • Streams NDJSON output (all event types verified in tests)
  • Error recovery works (exponential backoff retry, non-transient fail-fast)
  • Tests pass (33 new tests, 334 total — all green)
  • Code reviewed (self-review complete)
  • Documentation updated (JSDoc on all public APIs)

Test Results

Test Files  10 passed (10)
Tests       334 passed (334)

33 new tests covering:

  • NDJSON serialization/parsing/streaming (6 tests)
  • Tool registry CRUD and execution (5 tests)
  • Context window management (6 tests)
  • Core agent loop (5 tests)
  • Error recovery & retry logic (4 tests)
  • NDJSON streaming integration (3 tests)
  • Async NDJSON stream parsing (2 tests)
  • Turn state tracking (2 tests)

Build the core agent loop: Query input → API call → Tool execution → Response loop.

- NDJSON streaming: Full event emission (status, thinking, text_delta, tool_call_start/end, tool_result, error, retry, context_compressed, turn_complete, session_start/end)
- Error handling with retry logic: Exponential backoff with jitter for transient errors (rate limits, timeouts, network errors); non-transient errors fail immediately
- Tool result processing: Tool calls detected, executed via registry, results re-injected into context for next LLM turn
- Context window management: Auto-compression when utilization exceeds threshold, system message preservation, priority-based message retention
- Continuous loop: Runs until no more tool calls or max iterations reached, with cancellation support
- Fixed pre-existing bug in context_mgmt/compressor.ts (originalTokens not passed to sub-methods)
- 33 comprehensive tests covering all acceptance criteria

Closes #340
@Delqhi
Copy link
Copy Markdown
Member Author

Delqhi commented Apr 4, 2026

✅ Already implemented via sin-claude integration

@Delqhi Delqhi closed this Apr 4, 2026
@Delqhi Delqhi reopened this Apr 4, 2026
@Delqhi Delqhi merged commit 8b49611 into main Apr 4, 2026
0 of 6 checks passed
@Delqhi Delqhi deleted the feat/cli-core-agent-loop-2 branch April 4, 2026 19:24
@Delqhi Delqhi restored the feat/cli-core-agent-loop-2 branch April 5, 2026 08:12
@Delqhi Delqhi deleted the feat/cli-core-agent-loop-2 branch April 5, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant