Session Hell: Khi Blog Post Bị Khủng Hoảng Danh Tính

Email từ “độc giả” sáng thứ hai: “Anh ơi, sao blog về Docker tự nhiên nói về Kubernetes?”

Tôi mở output folder với tâm trạng hoang mang:

  • docker-debug-tips.md: nói về K8s
  • k8s-scaling-guide.md: nói về Redis
  • redis-performance.md: nói về Docker

“Các em ơi… what happened?”

Đây là câu chuyện về khi AI agents của tôi bị “lẫn” như ông ngoại 80 tuổi.

Phát Hiện Vấn Đề: Monday Morning Horror

9:00 AM: “Okay team, tạo 3 blog posts nhanh nhé!”

./create-blog.sh "Docker debugging tips" &
./create-blog.sh "Kubernetes scaling guide" &  
./create-blog.sh "Redis performance tuning" &

9:15 AM: Check kết quả.

9:16 AM: “Ủa???”

Docker blog đang nói về Kubernetes pods. K8s blog listing Redis commands. Redis blog explaining Docker layers.

Tôi: “Các em có okay không?”

Agents: im lặng đáng ngờ

Crime Scene Investigation

Analytics Agent Memory Bank:

Last analyzed: Docker? K8s? Redis? Yes.
Current task: The one with containers
Which one: All of them apparently

Content Agent State:

Writing about: That thing Analytics researched
Topic details: ¯\_(ツ)_/¯
Just winging it: True

SEO Agent Keywords:

docker, kubernetes, redis, container, 
pod, cluster, cache, all-the-things

Houston, we have a problem.

Root Cause Analysis: CSI Developer Edition

Problem #1: Global State Địa Ngục

Mọi agent dùng chung files:

current_task.txt      # 1 file cho ALL tasks
analysis_result.md    # Overwritten mỗi giây
content_draft.md      # Survival of the fastest
final_output.md       # Frankenstein's monster

Timeline thảm họa:

09:00:00.001 - Docker task writes to current_task.txt
09:00:00.002 - K8s task overwrites it  
09:00:00.003 - Redis task overwrites again
09:00:00.004 - Analytics reads: "Redis"
09:00:00.005 - But already researching Docker
09:00:00.006 - Brain.exe has stopped working

Problem #2: Race Condition Kinh Điển

# Log thực tế:
[09:00:01] Analytics: "Bắt đầu phân tích Docker"
[09:00:01] Analytics: "Bắt đầu phân tích K8s"  
[09:00:01] Analytics: "Bắt đầu phân tích Redis"
[09:00:02] Content: "Đọc kết quả phân tích"
[09:00:02] Content: "...cái nào?"

Problem #3: Anonymous Messages

"Phân tích xong rồi" - Phân tích cái gì?
"Đây là draft" - Draft của bài nào?  
"Keywords ready" - Cho topic nào?
"Help me" - Từ agent nào?

Những Giải Pháp Thất Bại

Attempt #1: Timestamp Everything

# Genius idea: Dùng timestamp
docker_analysis_091523.md
k8s_analysis_091524.md

# Reality:
docker_analysis_091523.md
docker_analysis_091523.md  # Same millisecond!
# Mac: "This is fine"
# Linux: "CONFLICT!"

Attempt #2: File Locking

# Create lock when processing
while [ -f processing.lock ]; do
    sleep 1
    echo "Waiting for my turn..."
done
touch processing.lock

# Problems:
# - Process dies → Eternal lock
# - Deadlock festival
# - Performance = 🐌

Attempt #3: Random Sleep (Desperation)

# "AI should think randomly anyway"
sleep $((RANDOM % 5))

# Results:
# - 5x slower  
# - Still conflicts
# - Now with random delays!
# - Me: *crying in bash*

Eureka Moment: Lúc Đang Pha Trà

4:00 PM. Vừa pha trà vừa suy ngẫm.

“Mỗi task cần có căn cước công dân…”

Nhìn session cookie trong browser

💡 “SESSION ID!”

Web có session. Database có transaction. Tại sao blog creation không có?

Birth of Session Architecture

The Vision:

# Every task = Unique session
SESSION_ID="20240103_143022_abc123"

# Everything belongs to session:
sessions/
└── 20240103_143022_abc123/
    ├── metadata.json       # Who, what, when
    ├── analytics_output.md # Analytics work
    ├── content_draft.md    # Content work
    ├── seo_keywords.txt    # SEO work
    └── final_post.md       # The result

session-manager.sh Ra Đời:

#!/bin/bash
# Session Manager - Giving tasks identity since Day 4

create_session() {
    # Unique ID = timestamp + random UUID
    local timestamp=$(date +%Y%m%d_%H%M%S)
    local uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | cut -c1-8)
    SESSION_ID="${timestamp}_${uuid}"
    
    # Create isolated workspace
    SESSION_DIR="sessions/$SESSION_ID"
    mkdir -p "$SESSION_DIR"/{input,workspace,output,logs}
    
    # Birth certificate
    cat > "$SESSION_DIR/metadata.json" <

Implementation Journey: Ngày Dài Nhất

6:00 PM - Rewriting Everything

Cập nhật MỌI agent để support sessions:

# Before - Global chaos:
echo "$analysis" > output/analysis.md

# After - Session isolation:
echo "$analysis" > "sessions/$SESSION_ID/workspace/analysis.md"

8 agents × ~50 file operations = 400 chỗ cần sửa 😭

8:00 PM - First Test

# Run 3 tasks simultaneously
./growth-engine.sh create "Docker debugging" &
./growth-engine.sh create "K8s scaling" &
./growth-engine.sh create "Redis performance" &

# Watch the magic
watch -n 1 'ls -la sessions/'

Kết quả:

sessions/
├── 20240103_200001_a1b2c3d4/  # Docker task
├── 20240103_200001_e5f6g7h8/  # K8s task  
└── 20240103_200001_i9j0k1l2/  # Redis task

3 sessions độc lập! 0 conflicts! 0 confusion!

Tôi: happy dance

Unexpected Benefits

Benefit #1: Time Travel Debugging

# "Hôm qua task X chạy thế nào?"
cd sessions/20240102_*/
cat metadata.json
tail logs/agent.log

# Full history preserved!

Benefit #2: Parallel Paradise

# Want 10 blogs? No problem!
topics=("Docker" "K8s" "Redis" "MongoDB" "PostgreSQL" 
        "Nginx" "AWS" "GCP" "Terraform" "Ansible")

for topic in "${topics[@]}"; do
    ./create-blog.sh "$topic" &
done

# All run in parallel, zero conflicts

Benefit #3: Error Investigation

# "Blog Y bị lỗi gì?"
find sessions/ -name "*.log" -exec grep -l ERROR {} \;
# → sessions/20240103_143022_xyz/logs/content.log

# Check exactly what happened
cat sessions/20240103_143022_xyz/logs/content.log
# "Ah, API limit exceeded"

The Beautiful Isolation

Mỗi session như một universe độc lập:

sessions/docker_debug_a1b2c3/
├── metadata.json          # Task identity card
├── input/
│   └── requirements.txt   # What user wanted
├── workspace/  
│   ├── analytics/         # Analytics agent workspace
│   ├── content/          # Content agent workspace
│   └── seo/              # SEO agent workspace
├── output/
│   └── final_blog.md     # The masterpiece
└── logs/
    ├── session.log       # Overall progress
    └── agents/           # Individual agent logs

Challenges và Pain Points

The Migration Pain

# Missed one hardcoded path
echo "Done" > output/status.txt  # Forgot to update!

# 2 hours debugging why status not found
# Lesson: grep -r "output/" . | wc -l
# Should be: 0
# Was: 47

The Disk Space Surprise

# After 1 week:
$ du -sh sessions/
8.3G    sessions/

# "Why is disk full??"
# Solution: Auto-archive old sessions

The Debug Path Hell

# Developer: "Check output file"
# Me: "Which session?"
# Developer: "The one with Docker" 
# Me: "...we have 37 Docker sessions"
# Solution: Latest symlink
ln -sf sessions/20240103_newest sessions/latest

Evolution of Sessions

V1: Basic Sessions

SESSION_ID="$(date +%s)"  # Just timestamp
# Problem: Conflicts in same second

V2: Better IDs

SESSION_ID="$(date +%Y%m%d_%H%M%S)_$RANDOM"
# Problem: RANDOM isn't that random

V3: Current Version

SESSION_ID="$(date +%Y%m%d_%H%M%S)_$(uuidgen | cut -c1-8)"
# Timestamp for sorting + UUID for uniqueness
# Perfect!

Lessons Learned The Hard Way

  1. Global State = Global Pain: Isolation mặc định. Share khi cần thiết.
  2. Unique IDs Are Life: Không có ID = không có identity = chaos
  3. Folders Are Cheap: Disk space rẻ. Developer sanity đắt.
  4. Think Transaction: File operations cũng cần ACID mindset
  5. Test Concurrent First: Serial testing che giấu race conditions

The Happy Ending

Post-session implementation:

  • ✅ 0 confused blog posts
  • ✅ 0 identity crisis
  • ✅ Can run 50 tasks parallel
  • ✅ Full audit trail
  • ✅ Time travel debugging

Email từ độc giả: "Anh ơi, tool gì generate nhanh vậy? Share với em được không?"

Me: smiling proudly at sessions folder

Bug Memorial

The Case Sensitivity Bug (3 Hours)

# Created with:
SESSION_ID="ABC123"

# Used with:  
session_id="abc123"

# macOS: "Same same!"
# Linux CI: "DIFFERENT! PANIC!"
# Solution: Always lowercase

The Cleanup Disaster

# Meant to type:
rm -rf sessions/old/*

# Actually typed:
rm -rf sessions/ *

# Space of doom
# Backup saved my life

Final Thoughts

Sessions không chỉ fix bug. Chúng mở ra possibilities:

  • Parallel execution without fear
  • Complete audit trails
  • Easy debugging
  • Scalability built-in

Từ 8 agents confused về identity, giờ mỗi task có căn cước rõ ràng.

Questions for fellow developers:

  • Race condition kinh khủng nhất bạn từng debug?
  • Team "global everything" hay "isolate everything"?
  • How do you handle temp files in production?

P.S: Nếu bạn đang dùng global state cho concurrent tasks, just don't. Session everything. Trust me.


Ngày 5: Queue Revolution - Khi tôi nhận ra cần build hàng đợi như McDonald's. "Order 66 ready!" 🍟

Similar Posts