Architecture

Purpose

A map of how a user turn flows through snsagent and where the cross-cutting subsystems (memory, TBM, advisor, tools) hook in.

High-level flow

CLI (src/cli/entry.ts)
  → createAgentSession (src/sdk.ts)
      builds TbmManager from settings (tbm.* → resolveTbmConfigFromSettings)
      builds transformContext = composeTransformContext({ tbm, emitContext, wrapSteering })
  → AgentSession (src/session/agent-session.ts)
      owns the pi-agent-core Agent, tools, memory, advisor, compaction
  → Agent.prompt (turn loop, @oh-my-pi/pi-agent-core)
      pre-model  → transformContext → applyTbmPreModel
      model call → streamFn / convertToLlm
      post-tool  → Agent.afterToolCall → applyTbmToolCompression
      post-turn  → Agent.setOnTurnEnd → cacheTbmTurnResponse + advisor onTurnEnd

The agent session (src/session/agent-session.ts)

The single coordination point. It wires the upstream @oh-my-pi/pi-agent-core loop to snsagent’s subsystems:

HookSubsystemWhere
transformContextTBM pre-model (comm-mode, tombstone, delta, pyramid, lazy-skills)src/tbm/session-hooks.tscomposeTransformContext
afterToolCallTBM tool-output compression (+ TTSR reminders)agent-session.ts #afterToolCall
setOnTurnEndTBM response-cache store + advisor turn reviewagent-session.ts turn-end callback
beforeAgentStartPromptmemory auto-recall injectionagent-session.tsagent.setSystemPrompt

All TBM hooks are pure functions of (TbmManager, messages) in src/tbm/session-hooks.ts, so they are testable with real AgentMessage shapes.

Key subsystems

SubsystemPathNotes
Settingssrc/config/settings.ts, settings-schema.tsdot-separated paths, YAML/JSON
Toolssrc/tools/30 built-in names in builtin-names.ts
Memorysrc/memory-backend/ + src/mnemopi/ + src/hindsight/resolver → backend; mnemopi injects
TBMsrc/tbm/integrated, default OFF
Slash commandssrc/slash-commands/builtin-registry.ts62 commands + aliases
TUIsrc/modes/, src/tui/, src/ui/interactive mode
Telegramsrc/adapters/telegram/grammY bot; auth via allowlist
Subagents / multi-agentsrc/task/, src/agents/delegation + ensemble strategies
Evalsrc/eval/Python/JS/Ruby/Julia runtimes
MCPsrc/mcp/Model Context Protocol servers
Cronsrc/cron/SQLite-backed scheduler
Extensibilitysrc/extensibility/skills + plugins + marketplace
Collabsrc/collab/host/join sessions

Token counting

TBM’s estimateTokens (src/tbm/context-delta.ts) delegates to @oh-my-pi/pi-agent-core countTokens, the same estimator the session uses for compaction.

Testing

bun test src            # core unit + integration suites
bun test test/          # committed integration tests
bun scripts/tbm-benchmark.ts   # TBM harness benchmark (not end-to-end)

The turn-lifecycle wiring is covered by src/tbm/__tests__/tbm-agent-loop.test.ts and tbm-session-integration.test.ts (hook-level effects). Memory’s full path is covered by src/memory-backend/__tests__/memory-integration.test.ts.