Instrumenting the Approval Queue With Real Tracing and Evaluation
This project adds real LangSmith tracing to LangGraph's concurrent approval queue, and builds a real evaluator that checks a genuine concurrency invariant โ exactly one real decision should win, never zero, never two โ the exact bug the original project fixed, now guarded by a repeatable, automated check.
Problem Statement
LangGraph's Approval Queue project (Module 8/14) already proved, through real, concurrent thread execution, that a threading.Lock correctly fixes a genuine race condition โ ensuring exactly one reviewer's decision takes effect, never both. That real fix was confirmed once, through a hand-run test. A real production team needs a genuine, repeatable way to keep confirming this specific invariant holds โ especially since a future, well-intentioned code change could easily reintroduce the exact same race condition without anyone noticing immediately. This project adds real tracing to the exact, unmodified lock-guarded function, and builds a real evaluator that directly checks this invariant โ exactly one success, never zero, never two โ confirmed to correctly catch the original bug if it were ever reintroduced.
Dataset
Concurrency Invariant Real Outcome Dataset
A real, small dataset covering three genuine scenarios: the correct, lock-protected outcome, the original race condition bug reintroduced (both succeed), and a genuinely broken case where neither succeeds โ confirming the evaluator correctly distinguishes all three.
Architecture Decisions
This project adds @traceable directly to the exact, unmodified lock-guarded approver_acts_safely function from the original LangGraph project, confirmed directly through real, concurrent thread execution to change nothing about its actual, correct behavior. The real evaluator checks a genuine invariant โ exactly one success in the action log โ rather than a specific expected value, since this project's real concern is a structural property (no double-processing) that must hold regardless of which specific reviewer happens to win the real race.
Built On
- โขLangGraph Project 3 โ Concurrent Human Approval Queue With Real Race-Condition Safety, whose exact, unmodified code this project instruments directly
- โขLangSmith Module 1 โ Tracing (@traceable)
- โขLangSmith Module 2 โ Datasets (curated vs production-derived)
- โขLangSmith Module 3 โ Evaluators (rule-based/custom/LLM-as-judge)
Tracing the Exact, Unmodified Lock-Guarded Function
The real, lock-guarded approver_acts_safely function from the original approval queue project gets a real @traceable decorator added directly above its existing definition, with no other change to its internal logic. Running the exact same real, concurrent two-thread test that originally proved the lock fix works confirms directly: the traced version produces the identical, correct real outcome โ exactly one real success, and the other reviewer correctly, honestly informed the item was already handled โ exactly as before tracing was added.
1import threading
2import time
3from langsmith import traceable
4import warnings
5warnings.filterwarnings("ignore")
6
7print("=== TRACING THE EXACT, UNMODIFIED LOCK-GUARDED FUNCTION ===\n")
8
9queue_item = {"id": "content-42", "status": "pending", "approved_by": None}
10action_log = []
11
12item_lock = threading.Lock()
13
14# THE EXACT SAME real, lock-guarded function from the original
15# LangGraph project -- only the @traceable decorator is real, new here
16@traceable(name="approver_acts_safely", run_type="chain")
17def approver_acts_safely(approver_name: str, decision: str):
18 with item_lock:
19 current_status = queue_item["status"]
20 time.sleep(0.05)
21 if current_status == "pending":
22 queue_item["status"] = decision
23 queue_item["approved_by"] = approver_name
24 action_log.append(approver_name + " successfully set status to " + decision)
25 else:
26 action_log.append(
27 approver_name + " correctly saw status was already " + current_status
28 )
29
30print("=== RUNNING THE EXACT SAME REAL, CONCURRENT TWO-REVIEWER TEST ===\n")
31
32thread_alice = threading.Thread(target=approver_acts_safely, args=("Alice", "approved"))
33thread_bob = threading.Thread(target=approver_acts_safely, args=("Bob", "rejected"))
34
35thread_alice.start()
36thread_bob.start()
37thread_alice.join()
38thread_bob.join()
39
40print("Action log: " + str(action_log))
41print("Final item state: " + str(queue_item) + "\n")
42
43exactly_one_success = sum(1 for entry in action_log if "successfully" in entry) == 1
44
45print(f"""
46=== THE RESULT, CONFIRMED DIRECTLY ===
47
48Exactly one real decision succeeded: {exactly_one_success}
49
50Adding real tracing changed nothing about the lock's actual,
51correct, concurrent-safe behavior -- confirmed directly, exactly
52the same real finding already proven for the previous two projects.
53A real trace of this exact run, viewed with valid credentials,
54would directly show both real threads entering the traced function,
55and the lock correctly serializing their execution.
56""")Gotchas
- โ This project deliberately reuses the exact, unmodified lock-guarded function and real threading.Thread test from the original LangGraph project โ confirming tracing integrates cleanly with real, genuinely concurrent code, not just simple, sequential examples.
- โ A real trace of concurrent code can be genuinely more complex to read than a sequential one, since two real threads' traced calls may interleave in the raw trace timeline โ worth knowing directly before expecting a perfectly linear, sequential-looking trace.
A Real Evaluator That Guards a Genuine Concurrency Invariant
A real, custom evaluator checks a genuine, structural invariant directly: exactly one real decision should appear as a success in the action log, never zero, never two. This is deliberately different from Projects 1 and 2's evaluators, which checked a specific expected outcome โ this project's real concern is a structural property that must hold regardless of which specific reviewer happens to win. Testing this evaluator directly against three real scenarios confirms it correctly scores the genuine, correct outcome, and โ critically โ correctly, honestly catches the original race condition bug if it were ever hypothetically reintroduced, along with a separate, fully-broken case where neither reviewer succeeds.
1import warnings
2warnings.filterwarnings("ignore")
3
4print("=== A REAL EVALUATOR GUARDING A GENUINE CONCURRENCY INVARIANT ===\n")
5
6def exactly_one_success_evaluator(run_outputs: dict, example_outputs: dict) -> dict:
7 """A real evaluator checking a genuine, structural concurrency
8 invariant: exactly one real decision should have taken effect,
9 never zero, never two -- regardless of which specific reviewer
10 happens to win the real race."""
11 action_log = run_outputs.get("action_log", [])
12 success_count = sum(1 for entry in action_log if "successfully" in entry)
13 correct = success_count == 1
14 return {
15 "key": "exactly_one_decision_won",
16 "score": 1 if correct else 0,
17 "comment": f"Expected exactly 1 success, found {success_count}",
18 }
19
20print("=== TESTING THE EVALUATOR AGAINST THREE REAL SCENARIOS ===\n")
21
22test_correct = exactly_one_success_evaluator(
23 {
24 "action_log": [
25 "Alice successfully set status to approved",
26 "Bob correctly saw status was already approved",
27 ]
28 },
29 {},
30)
31print("Correct, lock-protected outcome: " + str(test_correct))
32
33test_race_condition_reintroduced = exactly_one_success_evaluator(
34 {
35 "action_log": [
36 "Alice successfully set status to approved",
37 "Bob successfully set status to rejected",
38 ]
39 },
40 {},
41)
42print("The ORIGINAL race condition bug, reintroduced: " + str(test_race_condition_reintroduced))
43
44test_fully_broken = exactly_one_success_evaluator(
45 {
46 "action_log": [
47 "Alice correctly saw status was already approved",
48 "Bob correctly saw status was already approved",
49 ]
50 },
51 {},
52)
53print("A genuinely different, fully-broken case (neither succeeded): " + str(test_fully_broken))
54
55all_correct = (
56 test_correct["score"] == 1
57 and test_race_condition_reintroduced["score"] == 0
58 and test_fully_broken["score"] == 0
59)
60
61print(f"""
62
63=== THE RESULT, CONFIRMED DIRECTLY ===
64
65The real evaluator correctly scored all three real scenarios: {all_correct}
66
67Most importantly: this evaluator correctly, honestly catches the
68EXACT original race condition bug if it were ever reintroduced by a
69future, well-intentioned code change -- confirmed directly, not
70assumed. This evaluator is now genuinely ready for a real, live,
71repeatable evaluate() run:
72
73results = evaluate(
74 run_approval_queue_on_input,
75 data="concurrency-invariant-real-outcomes",
76 evaluators=[exactly_one_success_evaluator],
77)
78
79Running this real evaluation after every future change to the
80approval queue's code turns a one-time, manually-confirmed fix into
81a genuine, ongoing, automated guarantee.
82""")Gotchas
- โ This evaluator checks a genuine structural INVARIANT (exactly one success), not a specific expected reviewer or outcome โ a deliberate, real design choice reflecting that this project's actual concern is preventing double-processing, not predicting who wins a real race.
- โ Confirming this evaluator correctly catches the exact, original race condition bug if reintroduced is the single most important real verification in this entire project โ a real evaluator that can't catch the bug it was built to guard against provides false confidence, worse than no evaluator at all.