Skip to content

hylo.codeagents.tool_context

Package: hylo-code-agents · Core contracts

tool_context

Context for a tool-calling model (RFC-0007 part 3): the conversation is kept whole; what is sent is a projection.

The canonical history is append-only. Before every model call ToolContext.project() builds the request:

  head       the system message and the task message, always verbatim
  tail       the last `tail_decisions` actions (an action = an act call and everything after it up to the next
             act) verbatim, images included for the last `image_decisions`
  older      tool results longer than `inline_chars` become references: {"stored": "ref:<id>", "preview": ...};
             their images become a note naming the reference; identical results are stored once
  pressure   when the estimated request exceeds `compact_tokens`, everything between the head and the tail is
             replaced by a summary the model wrote in a separate call (no tools), plus the list of references
             it covered; the exact turns stay readable through `read_artifact`

Nothing is lost: every stored item is in self.artifacts (and in the trace), and the model can open it again with read_artifact (a character range of a text, or an image). Tool calls and their results always travel together, so a projection is always a valid conversation for the provider.

SUMMARY_KEYS module-attribute

SUMMARY_KEYS = ('progress', 'confirmed', 'ruled_out', 'attempts', 'constraints', 'avoid', 'next_checks')

SUMMARY_PROMPT module-attribute

SUMMARY_PROMPT = "You are condensing your own earlier work on a robot task so you can continue with less context. Write ONE JSON object with exactly these keys, each a list of short strings:\n  progress     what has been achieved so far, in task terms\n  confirmed    facts established by measurement or by an action's receipt\n  ruled_out    beliefs that turned out wrong\n  attempts     each failed attempt as 'what failed / evidence / what to change'\n  constraints  limits that still apply (reach, collisions, objects that must not move)\n  avoid        calls or plans not to repeat\n  next_checks  what to measure again before acting (positions go stale after motion)\nDo not copy coordinates or other numbers: they are stale; re-measure instead. At most 250 words in total. Reply with the JSON object only."

Entry dataclass

Entry(message: dict[str, Any], decision: int, ref: str | None = None, tags: dict[str, Any] = dict())

message instance-attribute

message: dict[str, Any]

decision instance-attribute

decision: int

ref class-attribute instance-attribute

ref: str | None = None

tags class-attribute instance-attribute

tags: dict[str, Any] = field(default_factory=dict)

ToolContext

ToolContext(system: str, task: str, task_images: list[Any] | None = None, *, tail_decisions: int = 3, image_decisions: int = 1, inline_chars: int = 1500, preview_chars: int = 300, compact_tokens: int = 48000, image_tokens: int = 800, summarize: Callable[[list[dict[str, Any]]], str] | None = None)

head instance-attribute

head = [{'role': 'system', 'content': system}, {'role': 'user', 'content': task, **{'images': list(task_images)} if task_images else {}}]

entries instance-attribute

entries: list[Entry] = []

decision instance-attribute

decision = 0

summarize instance-attribute

summarize = summarize

artifacts instance-attribute

artifacts: dict[str, dict[str, Any]] = {}

summary instance-attribute

summary: dict[str, Any] | None = None

stats instance-attribute

stats = {'stored': 0, 'summaries': 0, 'reopened': 0}

add

add(message: Mapping[str, Any], **tags: Any) -> None

acted

acted() -> None

An act call ran: the next messages belong to the next decision.

store

store(kind: str, content: Any, note: str = '') -> str

project

project() -> list[dict[str, Any]]

read_artifact

read_artifact(args: dict[str, Any]) -> Any

tool

tool() -> ToolDef

estimate_tokens

estimate_tokens(messages: list[Mapping[str, Any]], image_tokens: int = 800) -> int