Files
james.maes bc74494ada fix(mcp): v0.1 CRIT cluster — turnID resolver wired + tool-result double-cap (#20, #21, #157)
#21 (CRIT): ToolResultPacker.pack(_:) is now a pass-through decoder; redundant inner 8 KB cap removed. MCP dispatcher's SanitizeForModel.prepareForBoundary(_, capBytes: 8192) is the sole cap site. Doc-comment rewritten honestly: replay row reflects boundary-prepared bytes (8 KB), not original helper output. Replay fidelity beyond 8 KB lifted to v0.5/M-3 once SanitizeForModel can move to a shared module without dep cycle.

#20 (CRIT): ReplayingToolResultObserver.turnIDResolver lock-protected via OSAllocatedUnfairLock<ResolverBox> (same Swift-6-async-safe pattern as InProcessConfirmationCache). New attach(turnIDResolver:) method. AgentOrchestrator.currentTurnID() actor-isolated accessor. installAgent attaches the resolver immediately after orchestrator assignment, with defensive systemLogger.warning if observer type mismatches.

Tests: ToolResultPackerTests TP2/TP5 updated to assert no [TRUNCATED] marker; OrchestratorSubmitTests OS6 reframed + new OS6b reads SQLite events table directly to assert 8 KB exact; MCPRuntimeWiringTests adds attach pre/post-attach + replay-log dual-write correlation.

AgentCore 243 + Memory 98 + Replay 34 + MCP all green; all 18 boundary gates green.
2026-05-13 06:31:46 -05:00
..

AgentCore

The agent loop, LLM provider protocol, and concrete providers. Four library products: shared types (AgentCore), the orchestrator (AgentOrchestrator), and two providers (AnthropicProvider, OllamaProvider).

Key public types

Type Purpose
LLMProvider (protocol) Provider-agnostic streaming contract — returns AsyncThrowingStream<LLMEvent, Error>
LLMEvent Stream event union — tokenDelta, thinkingDelta, toolUseRequested, usage, messageStop
LLMMessage Provider-neutral chat message (role + content blocks, including ImageBlock)
ToolSchema / ToolChoice Tool advertising + tool-choice discipline (auto, none, required)
ImageBlock Multimodal extension (Plan 07-05 / D-18); base64 + media type
ModelID Strong-typed model identifier (claude-opus-4-7, qwen2.5-coder:32b)
TurnID / TurnNonce Turn identity (TurnID on bus + replay; TurnNonce for SEC-06, never on bus)
CacheHints eligibleForSystemPrompt (≥1024 tokens), extended-cache-ttl beta header guard
UntrustedWrapper Prompt-injection isolation around tool results
ToolResultPacker 8 KB cap on model-facing tool result content (Opus 4.7 footgun)
BoundedAsyncChannel dropOldest-policy bounded channel for orch → replay events
AnthropicProvider Streaming SSE Opus 4.7 via Anthropic Messages API
OllamaProvider Native NDJSON /api/chat + OpenAI-compat /v1/chat/completions
AgentOrchestrator Actor — turn-lifecycle owner, tool dispatch, cap-recovery, retry
OrchestratorEvent One-consumer event stream — state changes, tokens, tool starts/ends

Depends on

Keychain, Replay, Vision, Logging, Config. External: swift-log.

Used by

App/AppDelegate, App/Voice/VoiceOrchestratorAdapter.swift, App/MCPBusGatewayAdapter.swift, packages/Memory, packages/Vision, packages/Harness, packages/DevOverlay, packages/MCP.

Key invariants / contracts

  • LLMProvider cancellation through Task.isCancelled — provider task closes the underlying transport on consumer cancel.
  • Tool-choice mandatory. Cap-recovery turns pass .none (Anthropic {"type":"none"}; Ollama drops tools array entirely).
  • turnNonce never on the bus or in any OrchestratorEvent (SEC-06).
  • Tool result content capped at 8 KB before model. Full bytes still go to replay.
  • extended-cache-ttl requires the beta header AND ttl: "1h" on cache_control. Without the header, 1h is silently ignored.
  • Cache hints gated on prompts ≥1024 tokens (CacheHints.eligibleForSystemPrompt).
  • Anthropic SSE: close on message_stop, not message_delta. Swallow ping. Route thinking_delta to its own case. Treat stop_reason: "refusal" first-class. Emit partial_tool_use_at_disconnect on mid-delta termination.
  • Ollama NDJSON: tool_calls arrive on the chunk preceding done: true — decoder reads tool_calls whenever seen, never gates on done.
  • One-consumer invariant on OrchestratorEvent stream — scripts/check-orchestrator-events-single-consumer.sh.

Tests

19 swift-testing + 72 XCTest. Includes SSE fixture corpus (packages/Harness/Sources/Harness/Corpus/SSEFixtureCorpus.swift), NDJSON fixture corpus, tool-cap-recovery scenarios, retry-budget tests. Live-network tests gated by JARVIS_REAL_MODELS=1.

Notable files

  • Sources/AgentCore/LLMProvider.swift — the core protocol + multimodal overload
  • Sources/AgentOrchestrator/AgentOrchestrator.swift — the turn-lifecycle actor
  • Sources/AnthropicProvider/SSEDecoder.swift — SSE parser with all the footgun cases
  • Sources/OllamaProvider/NDJSONDecoder.swift — /api/chat decoder; tool_calls-before-done
  • Sources/AgentCore/CacheHints.swift — Track A streamTruncated fix