Pass 3 of 3 · Deep dive of PHAROSPass 1 · OverviewPass 2 · How it works
Code Observatory / Source
Assemble the context
The call the REST endpoint, the MCP tool and the CLI share.
PINNED SOURCE Copied from the repository at this commit. Source, not an execution.
PHAROS at 975a38dMIT license
assemble_contextbackend/app/codeintel/context.py, lines 154–261Open source
def assemble_context(db: Session, embedder: Embedder | None, req: ContextRequest, *, fetch_token: str | None = None) -> dict: t0 = time.perf_counter() timings: dict[str, float] = {} repos = resolve_repositories(db, req.repository) filters = Filters( repository_ids=[r.id for r in repos] or None, languages=req.languages, kinds=req.kinds, path_prefix=req.path_prefix, ) retriever = Retriever(db, embedder) outcome = retriever.search(req.query, mode=req.mode, k=req.max_chunks, filters=filters, hints=req.hints) timings.update({f"retrieval_{k}": v for k, v in outcome.timings_ms.items()}) chunks = [hit_to_dict(h, i) for i, h in enumerate(outcome.hits, start=1)] t = time.perf_counter() if req.include_code and outcome.hits: results = resolve_sources(source_requests(db, outcome.hits), token=fetch_token) for c in chunks: res = results.get(c["chunk_id"]) c["code_status"] = res.status if res else "unavailable:unknown" c["code"] = res.code if res else None c["verified"] = bool(res and res.verified) if c["kind"] == "module" and res and res.code is not None: c["code_truncated_to_lines"] = MODULE_CODE_LINES else: for c in chunks: c["code_status"] = "not_requested" c["code"] = None c["verified"] = False timings["resolve_code"] = (time.perf_counter() - t) * 1000 related: list[dict] = [] if req.include_related and outcome.hits: t = time.perf_counter() hit_ids = {h.chunk_id for h in outcome.hits} edges = retriever.neighbors([h.chunk_id for h in outcome.hits[:3]], limit=12) neighbor_ids = [uuid.UUID(n) for _, n, _ in edges if n not in hit_ids] rows = { str(c.id): c for c in db.execute(select(DocumentChunk).where(DocumentChunk.id.in_(neighbor_ids))).scalars() } if neighbor_ids else {} seen = set() for seed, neighbor, relation in edges: c = rows.get(neighbor) if c is None or neighbor in seen: continue seen.add(neighbor) meta = c.chunk_metadata or {} related.append({ "chunk_id": neighbor, "relation": relation, "via_chunk_id": seed, "path": meta.get("path"), "symbol": c.symbol_name, "kind": c.ast_node_type, "start_line": c.start_line, "end_line": c.end_line, "summary": c.semantic_summary, }) timings["related"] = (time.perf_counter() - t) * 1000 rules: list[dict] = [] warnings = list(outcome.warnings) if req.include_rules: if req.profile_id: if db.get(CodingProfile, req.profile_id) is None: warnings.append(f"unknown coding profile {req.profile_id!r}: no profile rules applied") rules = select_rules(db, profile_id=req.profile_id) else: urls = [r.url for r in repos] or list({h.repository_url for h in outcome.hits}) names = [r.name for r in repos] or list({h.repository_name for h in outcome.hits}) rules = select_rules(db, repositories=urls + names) memory: dict = {"briefs": [], "items": [], "skills": []} if req.include_memory and chunks: t = time.perf_counter() memory = memory_for_context(db, repos, chunks, req.query) timings["memory"] = (time.perf_counter() - t) * 1000 budget = _Budget(req.max_chars) context_text = _render(req.query, chunks, related, rules, budget, memory) timings["total"] = (time.perf_counter() - t0) * 1000 return { "query": req.query, "mode_requested": outcome.mode_requested, "mode_used": outcome.mode_used, "embedding_model": outcome.embedding_model, "warnings": warnings, "repositories": [ {"id": str(r.id), "name": r.name, "url": r.url, "commit_sha": r.last_commit_sha} for r in repos ], "chunks": chunks, "related": related, "rules": rules, "memory": memory, "context_text": context_text, "stats": { "timings_ms": {k: round(v, 2) for k, v in timings.items()}, "chars": len(context_text), "approx_tokens": len(context_text) // 4, "truncated": budget.truncated, }, }Where this comes from
- Commit
975a38d5d17839c26ee219295a5be6ac66b6d910- File
backend/app/codeintel/context.py- Lines
- 154–261
- Git blob
0c4a7dd4639651a5c13142d168630e344439f22a- License
- MIT, © 2024–2026 Pharos Contributors. Notice
Read it on GitHub at this commit
Check it yourself
From a clone of the repository, the first command prints the blob above and the second prints these lines.
git rev-parse 975a38d:backend/app/codeintel/context.py
git show 975a38d:backend/app/codeintel/context.py | sed -n '154,261p'