I completed Part D of the Instamart Copilot, adding a policy‑RAG layer and a deterministic ReAct‑style autonomous refund agent that uses five tools to answer policy questions and process refund requests.
- The policy corpus consists of 5 text files in
data/policies/(refund_policy.txt,delivery_policy.txt,escalation_policy.txt,payment_policy.txt,privacy_policy.txt). - TF‑IDF indexing uses scikit‑learn's
TfidfVectorizerover N = 15 chunks. - Refund Agent Business Rules
- Late Delivery:
delivery_minutes > 45→ refund = 100 % of order amount. - On‑Time:
delivery_minutes <= 45→ refund = 0 INR. - Eligibility: only orders with status completed are refundable; cancelled orders are never charged.
- Approval Threshold: refunds ≤ INR 1000 are auto‑issued (
issue_refund); refunds > INR 1000 are escalated to a human (escalate_to_human). - All monetary values are stored with two‑decimal INR precision, no dollar sign.
- Late Delivery:
The agent runs a deterministic ReAct loop (run_agent). It parses tool calls from the mock LLM, executes them via TOOL_MAP, records each observation, and stops when the model returns plain text. Final statuses include REFUND, ESCALATE, DENIED, NOT_FOUND, and guard‑rail fallbacks.
- Guardrails
• Maximum Iterations:
max_iterations = 6→FALLBACK_MAX_ITERATIONSif exceeded (demonstrated in scenario S7). • Token Budget:token_limit = 6000→FALLBACK_TOKEN_LIMITif cumulative tokens exceed the limit (demo withtoken_limit=200). • Timeout:time_limit_seconds = 10.0→FALLBACK_TIMEOUTwhen wall‑clock time is exhausted (demo with0.0).
All successful refunds and escalations are appended as JSON‑Lines to outputs/refund_ledger.jsonl, and each full run trace is saved to outputs/agent_runs.jsonl. After every tool observation the agent checkpoints its state to state/{run_id}.json for crash‑safe resumption.
- TF formula:
TF(t,d) = f_{t,d} / Σ_{t'∈d} f_{t',d}. - IDF formula:
IDF(t) = ln((1+N)/(1+DF(t))) + 1. - Cosine similarity scores retrieve the most relevant chunks.
- Lexical bottleneck: only exact token matches are considered.
- Chunking with a window size = 40 words, overlap = 10 words, and stride = 30 words yields 15 chunks total:
refund_policy.txt: 4 chunks (119 words)delivery_policy.txt: 3 chunks (100 words)escalation_policy.txt: 3 chunks (78 words)payment_policy.txt: 3 chunks (86 words)privacy_policy.txt: 2 chunks (68 words)

