Skip to content

Context

dotagents._context

Assemble effective context (Plan 04).

assemble_context(agent, agents_dir, project_root, global_scope=False)

Assemble the effective context text (markdown) for the given agent.

Returns '' if, after subtracting what the agent's harness already loads, there is nothing new to emit (no empty double of already-loaded content).

Source code in src/dotagents/_context.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def assemble_context(
    agent: _agents.Agent,
    agents_dir: Path,
    project_root: Path,
    global_scope: bool = False,
) -> str:
    """Assemble the effective context text (markdown) for the given agent.

    Returns '' if, after subtracting what the agent's harness already loads,
    there is nothing new to emit (no empty double of already-loaded content)."""
    filtered_sources = _resolve_and_filter_sources(
        agent, agents_dir, project_root, global_scope
    )
    if not filtered_sources:
        return ""

    assembled_parts = []
    search_roots = [project_root, agents_dir]
    overlay_roots = []

    for level, path, root in filtered_sources:
        try:
            content = path.read_text(encoding="utf-8")
            if root:
                search_roots.append(root)
                if level == "overlay":
                    overlay_roots.append(root)
            assembled_parts.append(f"<!-- Source: {path} -->\n{content.strip()}\n")
        except OSError:
            pass

    text = "\n\n".join(assembled_parts)
    text = _expand_placeholders(text, project_root, overlay_roots)
    text = _inline_referenced_files(text, search_roots)
    text += _get_skills_listing(agents_dir, project_root, global_scope)

    return text

assemble_context_data(agent, agents_dir, project_root, global_scope=False)

Structured form of the assembled context, for --format json.

Shape

{ "agent": , "harness": , "sources": [, ...], # after harness subtraction "context": , "skills": [{"name": ..., "description": ...}, ...], # opt-in listing }

context is the same assembled+inlined text the markdown format emits, but WITHOUT the skills listing appended -- skills are their own structured field so a consumer can render them separately and keep the opt-in distinction.

Source code in src/dotagents/_context.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def assemble_context_data(
    agent: _agents.Agent,
    agents_dir: Path,
    project_root: Path,
    global_scope: bool = False,
) -> "dict[str, object]":
    """Structured form of the assembled context, for ``--format json``.

    Shape:
        {
          "agent": <registry name>,
          "harness": <harness_id>,
          "sources": [<absolute source path>, ...],  # after harness subtraction
          "context": <assembled markdown text, minus the skills listing>,
          "skills": [{"name": ..., "description": ...}, ...],  # opt-in listing
        }
    ``context`` is the same assembled+inlined text the markdown format emits, but
    WITHOUT the skills listing appended -- skills are their own structured field
    so a consumer can render them separately and keep the opt-in distinction."""
    filtered_sources = _resolve_and_filter_sources(
        agent, agents_dir, project_root, global_scope
    )

    assembled_parts = []
    search_roots = [project_root, agents_dir]
    overlay_roots = []
    source_paths: "list[str]" = []

    for level, path, root in filtered_sources:
        try:
            content = path.read_text(encoding="utf-8")
        except OSError:
            continue
        source_paths.append(str(path))
        if root:
            search_roots.append(root)
            if level == "overlay":
                overlay_roots.append(root)
        assembled_parts.append(f"<!-- Source: {path} -->\n{content.strip()}\n")

    text = "\n\n".join(assembled_parts)
    text = _expand_placeholders(text, project_root, overlay_roots)
    text = _inline_referenced_files(text, search_roots)

    skills = [
        {"name": n, "description": d}
        for n, d in _collect_skills(agents_dir, project_root, global_scope)
    ]

    return {
        "agent": agent.name,
        "harness": agent.harness_id or agent.name,
        "sources": source_paths,
        "context": text,
        "skills": skills,
    }