Records 模块详细设计
本文档展开 kgent/records/ 的代码设计。
Records 模块负责将 run 的过程证据写成机器可读和人类可读 artifacts,是可审计交付、未来调度、revision 和质量治理的基础。
目标关联
Records 模块支撑:
- Sandbox-First Work Execution
- Tool-Mediated Action System
- Quality and Governance System
- Professional Workflow Orchestration
- Learning and Capability Evolution
它直接支撑可审计交付、专业可靠性、上下文连续性和未来调度。
模块范围
包含:
events.jsonlappend-only 写入。- event sequence 管理。
transcript.md写入和 finalize。logs/tools.jsonl写入。logs/errors.jsonl写入。- tool call record 写入。
- error record 写入。
不包含:
- 决定治理结果。
- 执行工具。
- 执行模型。
- 远程 tracing backend。
- dashboard。
文件结构
src/kgent/records/
__init__.py
context.py
events.py
transcript.py
logs.py
errors.py
数据模型
Event
位置:events.py
class Event:
event_id: str
sequence: int
run_id: str
session_id: str
task_id: str
type: str
timestamp: str
actor: str
severity: Literal["debug", "info", "warning", "error"]
summary: str
data: dict
correlation_id: str | None
parent_event_id: str | None
要求:
sequence在单个 run 内单调递增。sequence是排序 source of truth。timestamp用于 wall-clock 分析。data不存大型内容和敏感值。
ToolCallRecord
位置:logs.py
class ToolCallRecord:
call_id: str
tool_name: str
action: str
started_at: str
completed_at: str | None
duration_ms: int | None
status: Literal["started", "completed", "failed", "blocked"]
args_summary: dict
result_summary: str | None
artifacts: list[str]
error: ErrorInfo | None
ErrorInfo
统一位置:kgent/errors.py
class ErrorInfo:
code: str
message: str
category: Literal["config", "sandbox", "skill", "tool", "memory", "engine", "governance", "unknown"]
retryable: bool
details: dict | None
records/errors.py 中的 ErrorLogger 使用该统一模型,不重新定义错误结构。
RunRecords
位置:context.py
class RunRecords:
events: EventRecorder
transcript: TranscriptWriter
tools: ToolLogger
errors: ErrorLogger
核心接口
EventRecorder
位置:events.py
class EventRecorder:
def __init__(self, path: Path, session_id: str, task_id: str, run_id: str):
...
def emit(
self,
event_type: str,
summary: str,
data: dict | None = None,
*,
actor: str,
severity: str = "info",
correlation_id: str | None = None,
parent_event_id: str | None = None,
) -> Event:
...
行为:
- 自动生成
event_id。 - 自动递增
sequence。 - 自动填充 IDs 和 timestamp。
- 追加写入 JSONL。
- 返回写入的
Event。
TranscriptWriter
位置:transcript.py
class TranscriptWriter:
def __init__(self, path: Path):
...
def append_section(self, title: str, content: str) -> None:
...
def finalize(self, summary: dict) -> None:
...
最小 transcript 结构:
# Run Transcript
## Metadata
## Prompt
## Effective Role Summary
## Skills Used
## Tool Activity Summary
## Deliverables
## Errors and Warnings
ToolLogger
位置:logs.py
class ToolLogger:
def started(self, tool_name: str, action: str, args_summary: dict) -> str:
...
def completed(self, call_id: str, result_summary: str, artifacts: list[str], duration_ms: int) -> None:
...
def failed(self, call_id: str, error: ErrorInfo, duration_ms: int) -> None:
...
def blocked(self, call_id: str, error: ErrorInfo) -> None:
...
ErrorLogger
位置:errors.py
class ErrorLogger:
def write(self, error: ErrorInfo, *, context: dict | None = None) -> None:
...