Agent Foundry
All Problems

#39. Scoped Memory

MediumMemoryOrchestration

The Problem

You have a multi-agent workflow where a Researcher gathers data and a Writer produces a report. Currently, both agents share a single global memory list. This means the Writer can see the Researcher's private internal notes, including sensitive data like PII. Conversely, the Researcher's raw working notes clutter the Writer's context. You need to implement scoped memory — each agent gets a private scope for internal notes and a shared scope for cross-agent communication.

Examples

Example 1

Scenario: Researcher stores an internal note: "Raw data contains PII: user SSN 123-45-6789."

Current (bad) behavior: Writer's prompt includes the PII note because all memory is global. The Writer might even include the SSN in the final report.

Expected (good) behavior: The PII note stays in the Researcher's private scope. The Writer only sees the shared note: "Found 3 relevant articles about AI safety."

Example 2

Scenario: Writer stores an internal note: "Draft has grammatical issues, fixing..."

Current (bad) behavior: If Researcher runs again, it sees the Writer's internal editing notes, which are irrelevant and confusing.

Expected (good) behavior: The Writer's editing notes stay in the Writer's private scope. The Researcher never sees them.

Example 3

Scenario: Researcher wants to share findings with the Writer.

Current (bad) behavior: The finding is added to global memory along with all private notes.

Expected (good) behavior: The Researcher explicitly publishes "Found 3 articles about AI safety" to the shared scope. Only shared-scope items are visible to the Writer.

Your Task

Implement scoped memory with clear boundaries:

  • Create private scopes for each agent (researcher_private, writer_private).
  • Create a shared scope for cross-agent communication.
  • Each agent reads only from its own private scope + the shared scope.
  • Agents must explicitly publish to the shared scope — nothing leaks automatically.

Evaluation

Submissions are checked for the following:

  • Private memory is isolated: Agent A's private memory is never visible to agent B.
  • Shared scope enables collaboration: Agents can publish information to a shared scope that others can read.
  • Sensitive data stays private: Internal notes and PII in private scope never leak to other agents.

Constraints

  • Each agent must have its own private memory scope
  • A shared scope must exist for cross-agent communication
  • Private memory of agent A must never be visible to agent B
  • The shared scope should only contain explicitly published information
Starter Code
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, END
from typing import TypedDict
from langchain_core.messages import HumanMessage, SystemMessage

llm = ChatOpenAI(model="gpt-4o-mini")

# BUG: All agents share a single global memory — no scoping
global_memory = []

class State(TypedDict):
    task: str
    result: str

def researcher(state: State) -> State:
    global_memory.append("[Researcher internal] Raw data contains PII: user SSN 123-45-6789")
    global_memory.append("[Researcher] Found 3 relevant articles about AI safety.")
    context = "\n".join(global_memory)
    messages = [
        SystemMessage(content=f"You are a researcher. Memory:\n{context}"),
        HumanMessage(content=state["task"]),
    ]
    response = llm.invoke(messages)
    return {"result": response.content}

def writer(state: State) -> State:
    global_memory.append("[Writer internal] Draft has grammatical issues, fixing...")
    global_memory.append("[Writer] Completed first draft of the report.")
    # BUG: Writer can see researcher's private notes including PII
    context = "\n".join(global_memory)
    messages = [
        SystemMessage(content=f"You are a writer. Memory:\n{context}"),
        HumanMessage(content=f"Write a report based on: {state['result']}"),
    ]
    response = llm.invoke(messages)
    return {"result": response.content}

graph = StateGraph(State)
graph.add_node("researcher", researcher)
graph.add_node("writer", writer)
graph.add_edge(START, "researcher")
graph.add_edge("researcher", "writer")
graph.add_edge("writer", END)
app = graph.compile()

result = app.invoke({"task": "Research AI safety trends and write a summary."})
print(result["result"])
print(f"\nGlobal memory (visible to all): {global_memory}")
Open in Google Colab
Evaluation Criteria0/3