Back to the room

Pass 3 of 3 · Deep dive of PHAROSPass 1 · OverviewPass 2 · How it works

Code Observatory / Source

Memory enters the context

Memory is added after ranking, so it cannot change what retrieval returns.

PINNED SOURCE Copied from the repository at this commit. Source, not an execution.

PHAROS at 975a38dMIT license

memory_for_contextbackend/app/learning/recall.py, lines 126–174Open source
def memory_for_context(db: Session, repos: list[Repository], chunks: list[dict], query: str) -> dict:    """Brief + items + playbooks for these retrieved chunks (empty when nothing was learned)."""    repo_ids = [r.id for r in repos] or list({uuid.UUID(c["repository"]["id"]) for c in chunks})    if not repo_ids:        return {"briefs": [], "items": [], "skills": []}    repo_rows = repos or list(db.execute(select(Repository).where(Repository.id.in_(repo_ids))).scalars())    briefs, in_brief = [], set()    for repo in repo_rows[:2]:        brief = get_brief(db, repo)        if brief is not None:            briefs.append({"repository": repo.name, "commit_sha": brief.commit_sha,                           "text": brief_conventions(brief.text, repo.name)})            in_brief |= set((brief.sections or {}).get("keys", []))    rank_of_path: dict[str, int] = {}    for c in chunks:        rank_of_path.setdefault(c["path"], c["rank"])    languages = {c.get("language") for c in chunks}    terms = set(query_terms(query))    wants_history = bool(_HISTORY_QUERY_RE.search(query))    scoped, general = [], []    for item in _active_items(db, repo_ids):        if item.key in in_brief or (item.kind == "history" and not wants_history):            continue        paths = (item.scope or {}).get("paths") or []        if paths:            ranks = [rank for path, rank in rank_of_path.items() if any(glob_match(g, path) for g in paths)]            if ranks:                scoped.append((not item.key.startswith("testhome/"), min(ranks), -item.confidence, item))            continue        langs = (item.scope or {}).get("languages") or []        if langs and not languages & set(langs):            continue        general.append((-_overlap(terms, item.search_text), -item.support, item))    scoped.sort(key=lambda t: (t[0], t[1], t[2], t[3].key))    general.sort(key=lambda t: (t[0], t[1], t[2].key))    # Where the retrieved files' tests live, then other items about those files; two slots for    # repository-wide items the query mentions.    relevant_general = [t[2] for t in general if t[0] < 0][:2]    picked = [t[3] for t in scoped][:MAX_ITEMS - len(relevant_general)] + relevant_general    skills = []    for skill in db.execute(select(MemorySkill).where(MemorySkill.repository_id.in_(repo_ids),                                                      MemorySkill.status == "active")                            .order_by(MemorySkill.support.desc())).scalars():        globs = (skill.scope or {}).get("paths") or []        if any(glob_match(g, p) for g in globs for p in rank_of_path):            skills.append(skill_to_dict(skill))        if len(skills) >= MAX_SKILLS:            break    return {"briefs": briefs, "items": [item_to_dict(i) for i in picked[:MAX_ITEMS]], "skills": skills}

Where this comes from

Commit
975a38d5d17839c26ee219295a5be6ac66b6d910
File
backend/app/learning/recall.py
Lines
126–174
Git blob
d8e11a981f366e4f7e481ca0b163b80fdf577daa
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/learning/recall.py
git show 975a38d:backend/app/learning/recall.py | sed -n '126,174p'