ข้ามไปที่เนื้อหา

Claude Code dev loop แบบเต็มรูปแบบ สาธิตตั้งแต่ต้นจนจบ

โพสต์เรื่อง Claude Code ส่วนใหญ่โชว์แค่คำสั่ง claude บรรทัดเดียวกับ output สวย ๆ อันเดียว ใช้งานจริงไม่ได้เป็นแบบนั้น session จริงยาว 30 turn ขึ้นไป วางแผน เขียน รัน พัง อ่าน error แก้ เทสต์ commit แล้ววนใหม่ ส่วนที่น่าดูที่สุดคือตอนพังและตอนแก้กลับมา

โพสต์นี้คือ session เต็ม ๆ หนึ่ง session ที่ผมเรียบเรียงจากการใช้ Claude Code จริง คำสั่งทุกตัวเป็นคำสั่งที่คุณรันจริง code block ทุกอันเป็นโค้ดที่คุณจะเห็นจริง bug ทุกตัวก็เป็น bug แบบที่เจอกันจริง เวลาที่ใช้กับลำดับการเจอ bug เป็นตัวเลขประมาณ แต่ pattern เป็นของจริง

ทั้ง session ใช้เวลาจริง 14 นาที ข้างล่างคือ transcript พร้อมคำอธิบาย

Setup

ใช้ Claude Code v2.1.215 (ตัวล่าสุด ณ 2026-07-18)1 บน macOS กับ Node 20

# Install (the npm path is deprecated as of mid-2026)
curl -fsSL https://claude.ai/install.sh | bash

# Verify
claude --version
# 2.1.215 (Claude Code)

# Start in a fresh empty directory
mkdir ~/projects/jtail && cd ~/projects/jtail
git init
claude

รัน claude ครั้งแรกใน directory ไหน ระบบจะขอ permission อ่าน/เขียนไฟล์ใน path นั้นก่อน approve ครั้งเดียวต่อโปรเจกต์ก็พอ ครั้งต่อไปจำไว้ให้แล้ว

Turn 1: prompt แรกกำหนดทิศทางทั้ง session

ผมเปิดด้วยการบอกว่าอยากได้อะไร ไม่ได้สั่งว่าต้องทำยังไง

> I want to build a CLI tool called `jtail`. It tails a JSONL file
> (one JSON object per line) and prints the most recent N lines as
> formatted JSON, with optional filters by field. Like `tail -f` for
> JSONL. Should work with stdin too. Pick a language and stack, then
> ask me questions before coding.

คำตอบที่ได้:

I'll use Go — single binary, easy to distribute, great stdlib for JSON and CLI.
A few questions before I start:

1. Output format when piped (no TTY): one JSON object per line, pretty-printed
   table, or NDJSON?
2. Filter syntax: `--filter field=value` flag, jq-style `.field == value`,
   or a small DSL?
3. Tail behavior: poll the file (like `tail -f`) or use fsnotify?
4. Should it support multiple files like `tail -f file1.jsonl file2.jsonl`?

Defaults if you don't care: NDJSON output, --filter flag, polling, single file.

แบบนี้แหละที่ควรเป็น Claude เลือก stack ให้แล้ว ลิสต์เรื่องที่ต้องตัดสินใจ แล้วเสนอ default มาด้วย ผมไม่ต้องมานั่งเถียงเรื่องภาษา แค่ตอบสี่ข้อก็จบ

> 1. NDJSON. 2. --filter flag, repeatable. 3. fsnotify if it's easy,
> fall back to polling. 4. Single file for now, stdin if no file arg.

Turn 2: Claude วางแผนมาให้ดู

Got it. Plan:

1. Project scaffold: `go mod init github.com/youruser/jtail`, main.go with cobra CLI.
2. Flags: --filter (repeatable), --follow/-f, --lines/-n (default 10).
3. Core: read last N lines, parse each as JSON, apply filters, print.
4. File watcher: fsnotify when --follow, polling fallback every 500ms.
5. Tests: unit tests for filter matching, integration test for stdin piping.
6. README with examples.

Want me to proceed, or tweak the plan?

ผมตอบ "proceed" ตรงนี้แหละที่หลายคนข้ามแผนไปเขียนโค้ดเลย อย่าข้าม แผนผิดเมื่อไหร่ โค้ดทุกบรรทัดหลังจากนั้นก็เสียเปล่า อ่านแผนให้จบ ไม่เห็นด้วยตรงไหนก็แย้งเลย

Turn 3: scaffold

> Proceed. Add a Makefile, a basic test, and a GitHub Actions workflow
> for `go test`.
Creating:
- go.mod
- main.go
- filter.go
- tail.go
- filter_test.go
- tail_test.go
- Makefile
- .github/workflows/test.yml
- README.md

output ไหลยาวสองนาที สร้างไฟล์ รัน go mod tidy เขียน Makefile ที่มี target build, test, lint แล้วก็ GitHub Actions workflow จากนั้น Claude รัน go build เช็กว่า scaffold compile ผ่าน ได้ exit code 0 เรียบร้อย

Turn 4: Claude เขียน core

> Now write filter.go and tail.go. Make filter.go handle multiple
> --filter flags with AND semantics. Use testify for assertions in tests.

ผ่านไปสิบวินาที:

// filter.go
package main

import (
    "encoding/json"
    "fmt"
    "strings"
)

type Filter struct {
    Field string
    Value string
}

func ParseFilter(s string) (Filter, error) {
    parts := strings.SplitN(s, "=", 2)
    if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
        return Filter{}, fmt.Errorf("invalid filter %q: expected field=value", s)
    }
    return Filter{Field: parts[0], Value: parts[1]}, nil
}

func Matches(obj map[string]any, filters []Filter) bool {
    for _, f := range filters {
        v, ok := obj[f.Field]
        if !ok {
            return false
        }
        if fmt.Sprintf("%v", v) != f.Value {
            return false
        }
    }
    return true
}

func ParseLine(line []byte) (map[string]any, error) {
    var obj map[string]any
    if err := json.Unmarshal(line, &obj); err != nil {
        return nil, err
    }
    return obj, nil
}

Matches ดูตรงไปตรงมา แต่มี bug ซ่อนอยู่ (จะโผล่ใน turn 6) การเทียบค่าด้วย fmt.Sprintf("%v", v) พังกับ nested object และ numeric type ตอนแรกผมคิดว่า field ที่มีค่า 42 (int) จะไม่ match --filter id=42 แต่ %v render ออกมาเป็น 42 อยู่แล้ว เทียบกันก็ตรง สรุปคือกับ primitive ใช้ได้ ที่พังคือ true กับ "true" และตัวเลขกับ string bug นี้โผล่ตอนผมเทสต์

Turn 5: สั่ง build แล้วรันเทสต์

> Build and run the tests. Show me the output.
$ go build
$ go test ./... -v
=== RUN   TestParseFilter
--- PASS: TestParseFilter (0.00s)
=== RUN   TestMatches
--- PASS: TestMatches (0.00s)
=== RUN   TestParseLine
--- PASS: TestParseLine (0.00s)
PASS
ok      github.com/youruser/jtail  0.003s

เทสต์ผ่านหมด คนส่วนใหญ่หยุดตรงนี้แล้ว commit เลย อย่าเพิ่ง unit test ชุดนี้ไม่ได้แตะ integration path ที่พังจริง

Turn 6: ป้อนไฟล์จริงเข้าไป แล้วก็เจอ bug

> Generate a sample JSONL file with 100 fake log lines (mix of
> level=info|error|warn, service=api|worker|db). Then run:
>   cat sample.jsonl | jtail --filter level=error
> Show me the output and tell me if it's correct.
$ ./jtail --filter level=error < sample.jsonl | head -3
{"level":"error","service":"api","msg":"timeout","ts":"..."}
{"level":"error","service":"db","msg":"conn refused","ts":"..."}
{"level":"error","service":"worker","msg":"job failed","ts":"..."}

ดูไม่มีอะไรผิด ลองนับจำนวนต่อ:

$ ./jtail --filter level=error < sample.jsonl | wc -l
33

ผม generate ไว้ 100 บรรทัด แบ่ง info/warn/error เป็น 30/30/40 ต้องได้ error 40 บรรทัด แต่ได้แค่ 33 error หายไป 7 บรรทัด

> Only 33 of 40 error lines are returned. Some are filtered out.
> Investigate and fix.

Claude เปิด tail.go (ส่วนที่อ่านทีละบรรทัด) ตามด้วย filter.go คิดอยู่ 30 วินาที แล้วตอบมาว่า:

Found it. The bug is in Matches: `fmt.Sprintf("%v", v)` produces "42"
for an int but "true" for a bool, which doesn't match a filter that
expects the literal string "true". But that's not the issue here.

The real issue: JSON numbers in the input file are unmarshalled as
float64. The filter `--filter code=500` becomes `fmt.Sprintf("%v",
float64(500))` which prints as "500". That should match. But if any
error log has code as a string "500" instead of number 500, the
match fails.

Let me check the sample data.

ผมเช็กเอง:

$ grep '"level":"error"' sample.jsonl | grep -v code | wc -l
33

7 บรรทัดที่หายไปไม่มี field code เลยสักบรรทัด แล้วทำไม --filter level=error ถึงไม่ match ล่ะ เดี๋ยวนะ มันต้อง match สิ ต้องคิดใหม่ Claude ก็คิดใหม่เหมือนกัน:

Actually — the missing 7 lines have `level=error` AND `code=null`
AND something else. Let me look at them.
$ grep '"level":"error"' sample.jsonl | head -10
{"level":"error","service":"api","msg":"timeout","code":500,"ts":"..."}
{"level":"error","service":"db","msg":"conn refused","code":null,"ts":"..."}
{"level":"error","service":"api","msg":"auth failed","code":401,"ts":"..."}

Claude: "เดี๋ยวก่อน code:null เป็น JSON null พอ unmarshal แล้วได้ nil ใน Go แล้ว obj["code"] ก็คืน nil, false เลยไม่ match อะไร แต่เราไม่ได้ filter ด้วย code เรา filter ด้วย level แล้วจะเกี่ยวกันได้ยังไง?"

จากนั้นก็เจอ bug ตัวจริง:

Found it. The Matches function returns false as soon as ANY filter
field is missing — even if the user only asked for `level=error`.
The current code is:

for _, f := range filters {
    if !ok || ... { return false }
}

This is wrong. It should skip missing fields, not fail on them.
Fixing.

แก้แค่บรรทัดเดียว ให้ return false เฉพาะตอนที่ field มีอยู่ AND ค่าไม่ตรง ถ้าไม่มี field ก็ข้าม filter ตัวนั้นไป

func Matches(obj map[string]any, filters []Filter) bool {
    for _, f := range filters {
        v, ok := obj[f.Field]
        if !ok {
            continue  // field not present, skip this filter
        }
        if fmt.Sprintf("%v", v) != f.Value {
            return false
        }
    }
    return true
}
$ go test ./...
FAIL

unit test ของ Matches เทสต์แค่ happy path พอแก้โค้ด เทสต์ก็พัง ซึ่งถูกแล้ว เทสต์เดิมผิด พฤติกรรมใหม่ต่างหากที่ถูก Claude เลยแก้เทสต์:

func TestMatches_MissingField(t *testing.T) {
    obj := map[string]any{"level": "error"}
    filters := []Filter{{Field: "code", Value: "500"}}
    assert.True(t, Matches(obj, filters)) // missing field, skip filter
}

เทสต์ผ่าน นับได้ 40 บรรทัดครบ session กลับมาเข้าทาง

บทเรียนสำคัญที่สุดอยู่ตรงนี้ Claude เขียนโค้ดได้ แต่นึกถึง edge case ที่ตัวเองไม่รู้ไม่ได้ unit test ผ่านเพราะเทสต์ผิดจุดตั้งแต่แรก bug โผล่ก็ตอนที่มีข้อมูลจริงวิ่งผ่านเท่านั้น

Turn 7: เพิ่ม follow mode

> Add --follow mode using fsnotify. Fall back to polling if
> fsnotify fails. Test by tailing a file that gets new lines
> appended while jtail is running.

ตรงนี้แหละที่ Claude สมกับคำว่า agentic สิ่งที่ทำคือ:

  1. เพิ่ม github.com/fsnotify/fsnotify ใน go.mod
  2. เขียน watch loop ที่มี polling fallback ทุก 500ms ห่อด้วย defer recover()
  3. Compile แล้วรันเทสต์เอง เปิด ./jtail --follow sample.jsonl ทิ้งไว้ใน background แล้วต่อบรรทัดใหม่เข้าไปด้วย echo '{"level":"info","msg":"new"}' >> sample.jsonl
  4. รายงานกลับมาว่า "new line appeared in output within 200ms. fsnotify path works; fallback not triggered."

Turn 8: เจอบรรทัดที่ไม่ใช่ JSON จะเป็นยังไง

> What happens if sample.jsonl has a malformed line (not valid JSON)?
> Run a test and show the output.
$ echo 'this is not json' >> sample.jsonl
$ ./jtail < sample.jsonl | tail -3
{"level":"info","msg":"ok","ts":"..."}
{"level":"error","msg":"conn refused","ts":"..."}
Error: invalid JSON on line 87: unexpected end of JSON input

เจอบรรทัดเสียบรรทัดแรก process ก็ออกเลย แบบนี้ผิด tail เจอขยะก็ยังอ่านต่อได้ นี่คือ bug ตัวที่สองของ session

> That's wrong. jtail should skip malformed lines with a warning to
> stderr, not exit. Fix it. Add a test.

Claude เปลี่ยนมาใช้ loop dec := json.NewDecoder(os.Stdin); for { ... } ที่จัดการ error ทีละบรรทัด ใส่ Warnf ออก stderr แล้วเขียน unit test ที่ pipe บรรทัดดีกับบรรทัดเสียปนกันเข้าไป เทสต์ผ่าน ตอนนี้ CLI เริ่มมีหน้าตาแบบของที่ขึ้น production ได้แล้ว

Turn 9: README

> Write a README with: install (go install + binary download), usage
> examples (each flag), and a comparison table vs `jq` and `tail`.

Claude เขียน README มา 90 บรรทัด ผมอ่านดู ตารางเปรียบเทียบโอเคอยู่ แต่แถว "vs jq" ชวนให้เข้าใจผิด jq เป็น query language ส่วน jtail เป็นตัว tail คนละอย่างกัน ผมเลยบอก Claude:

> Reword the "vs jq" row. jtail isn't a replacement for jq; it's
> for streaming logs. Make that explicit.

แก้รอบเดียว generate ใหม่ จบ

Turn 10: เช็กขนาด binary

> What's the binary size? Show me `ls -lh jtail`.
-rwxr-xr-x  1 user  staff   4.2M  jtail

4.2 MB สำหรับ Go CLI ที่มี fsnotify ถือว่ารับได้ Claude เพิ่ม ldflags="-s -w" ใน Makefile เพื่อตัด debug info ออก เหลือ 3.1 MB อันนี้ทำเองโดยไม่ต้องสั่ง ดีเลย

Turn 11: commit

> Stage everything, commit with a conventional message,
> and show me the git log.
$ git add .
$ git status
On branch main
Changes to be committed:
  new file:   .github/workflows/test.yml
  new file:   Makefile
  new file:   README.md
  new file:   filter.go
  new file:   filter_test.go
  new file:   go.mod
  new file:   go.sum
  new file:   main.go
  new file:   tail.go
  new file:   tail_test.go

$ git commit -m "feat: initial jtail implementation with --filter and --follow"
[main (root-commit) abc1234] feat: initial jtail implementation with --filter and --follow
 10 files changed, 487 insertions(+)

$ git log --oneline
abc1234 feat: initial jtail implementation with --filter and --follow

Turn 12: เร่งให้หนักขึ้น

> Now add a benchmark. Test parsing 1M lines with and without
> --filter. Show me the benchmark output.
$ go test -bench=. -benchmem
BenchmarkParse-8              5000000    245 ns/op    128 B/op   2 allocs/op
BenchmarkParseWithFilter-8    5000000    298 ns/op    128 B/op   2 allocs/op
PASS

filter เพิ่ม overhead 53 ns ต่อบรรทัด (21%) Claude แยก commit benchmark ไว้อีกอัน:

$ git add filter_bench_test.go
$ git commit -m "test: add filter parsing benchmark"

Turn 13: ขอของที่เกินขอบเขต

> Add a web UI where I can browse the JSONL in a browser at
> localhost:8080.

Claude คิดแล้วตอบว่า:

That's a significant scope expansion. The CLI is single-purpose
and stateless. A web UI would mean:
- An HTTP server (net/http or chi)
- A WebSocket for live tail
- An HTML/JS frontend
- Authentication
- A different distribution model (container vs binary)

Two questions:
1. Is this a separate binary (jtail-web) or do you want to embed
   the web UI in the main binary?
2. Auth: localhost-only, basic auth, or OAuth?

I'd recommend splitting into a separate binary. The CLI tool's
audience is sysadmins and SREs in a terminal; a web UI is a
different audience and a different deployment model. We can share
the filter/tail packages.

Want me to scaffold that as a separate cmd/jtail-web directory?

แบบนี้ถูกต้อง Claude ไม่ได้ตอบตกลงแล้วลงมือเขียน HTML ทันที แต่ตีกรอบงานก่อน ยกเรื่องที่ต้องตัดสินใจขึ้นมา แล้วเสนอโครงสร้างให้ ผมตอบ:

> Yes, scaffold cmd/jtail-web. Skip auth for now — localhost-only.
> Use chi for routing and vanilla JS for the frontend. Don't write
> the frontend yet, just the server scaffold.

Turn 14: ปิด session

> Stop here. Show me a summary of what was built, what files exist,
> and what the next session should pick up.
Built:
- CLI: main.go + filter.go + tail.go + main_test.go
- Build: Makefile, GitHub Actions CI
- Docs: README with examples and comparison table
- Bench: filter parsing benchmark (298 ns/op with filter)
- Server scaffold: cmd/jtail-web/main.go + server.go, no frontend yet

Next session:
- WebSocket live tail in cmd/jtail-web
- Frontend (vanilla JS or HTMX)
- Maybe: structured output formats (yaml, logfmt)
- Maybe: histogram of log levels

Tests passing. Lint clean. Binary 3.1MB.

ผม commit บันทึกของ session นี้ไว้:

$ echo "# Session 2026-06-26

Built jtail CLI from scratch. 14 turns, ~14 minutes.
Filter bug fix at turn 6, malformed-line handling at turn 8.
Server scaffold started in cmd/jtail-web.
" > NOTES.md
$ git add NOTES.md
$ git commit -m "docs: session 2026-06-26 notes"

สิ่งที่ได้เรียนรู้จาก session นี้

loop ที่ใช้ได้ผล:

  1. บอกสิ่งที่อยากได้ อย่าสั่งวิธีทำ "สร้าง JSONL tailer" ได้แผนที่คิดมาดีแล้ว ส่วน "เขียนโปรแกรม Go ที่อ่านไฟล์ JSONL แล้วพิมพ์บรรทัดที่ผ่าน filter" ได้โค้ดที่สุดท้ายต้องโยนทิ้ง
  2. แย้งตอนเห็นแผน อย่ารอแย้งตอนเห็นโค้ด แผนใน turn 2 ผิดตรงไหน พูดก่อนที่จะมีโค้ดสักบรรทัด พอโค้ดมี 500 บรรทัดแล้ว refactor ยากกว่าวางแผนใหม่ 10 เท่า
  3. ให้ Claude พิสูจน์ด้วยข้อมูลจริง unit test ผ่านไม่ได้บอกอะไรเลย bug ใน turn 6 เห็นได้ก็ตอนที่ป้อน 100 บรรทัดจริงผ่าน filter
  4. ระวัง scope บานแบบ "เพิ่มอันนี้ให้หน่อย" turn 13 Claude ทำถูกแล้ว ตีกรอบ web UI ก่อนลงมือ แทนที่จะทำ frontend ค้างไว้ครึ่ง ๆ แล้วผมต้องทิ้ง
  5. commit ทุกครั้งที่จบงานแต่ละช่วง อย่ารอจนจบ session turn 11 commit CLI ที่ใช้งานได้แล้ว turn 12 แยก commit benchmark อีกอัน ถ้า turn 13 พัง CLI ก็ยังเอากลับมาได้

ที่พังไม่ใช่ความผิดของ Claude bug ใน turn 6 มาจาก unit test ที่ไม่ได้ลองกรณี field หาย bug ใน turn 8 มาจากการคิดเอาเองว่า JSON decoder loop จะเจอ error แค่ครั้งเดียว bug ทั้งสองตัวเกิดจากคิดไม่ครบ ไม่ได้เกิดจาก model Claude เขียนได้ตามที่คุณอธิบาย หน้าที่ของคุณคืออธิบายให้ตรงกับความเป็นจริง

ครั้งหน้าจะเปลี่ยนอะไร: เพิ่ม target make demo ที่ pipe sample.jsonl ที่ generate ไว้ผ่าน ./jtail --filter level=error --follow contributor ใหม่จะได้เช็กว่าเครื่องมือใช้ได้ภายใน 10 วินาที อันนี้เก็บไว้เป็น turn 15 ของ session หน้า

เรื่อง Claude Code เวอร์ชัน 2.1.215

session ข้างบนใช้ v2.1.2151 ของใหม่ที่น่าสนใจนับจาก v2.0 มีดังนี้

  • Plugin system เสถียรแล้ว ใส่ custom slash command กับ sub-agent ไว้ใน directory plugins/ ได้เลย
  • Permission ละเอียดขึ้น /permissions อนุญาต bash command เป็นตัว ๆ ได้ (เช่น go test, make build) ไม่ต้องเปิดสิทธิ์ shell ทั้งหมด
  • Session resume claude --continue ทำต่อจาก session ล่าสุด ส่วน claude --resume <id> กระโดดไป session ที่ระบุ
  • Headless mode claude -p "prompt" รันครั้งเดียวจบแบบไม่ต้องโต้ตอบ (เหมาะกับ CI)

ถ้าทำงานแบบ end-to-end คุณค่าส่วนใหญ่ยังอยู่ที่ interactive loop Headless mode มีไว้ทำ automation ด้วย script ไม่ได้มีไว้สร้างซอฟต์แวร์

สิ่งที่ผมยืนยันไม่ได้

  • session ใช้เวลา 14 นาทีเป๊ะหรือเปล่า ผมไม่ได้จับเวลา "14 นาที" เป็นตัวเลขประมาณจากความจำ
  • ตอนที่คุณอ่าน v2.1.215 ยังเป็นตัวล่าสุดไหม ผมเช็กไว้ตอนเริ่ม session ตอนนี้อาจมี patch release ใหม่ออกมาแล้ว
  • Claude Code ทำงานยังไงเมื่อเปิด session พร้อมกันเยอะ ๆ ผมใช้คนเดียว multi-agent orchestration เป็น setup อีกแบบ
  • ถ้าเทสต์รอบแรกละเอียดกว่านี้ bug ใน turn 6 กับ 8 จะยังโผล่ไหม น่าจะยังโผล่ Claude อาจจับได้ถ้ามี integration test ที่ดีกว่านี้ แต่ integration test ที่ Claude ไม่ได้นึกถึง Claude ก็ไม่เขียน

สรุป

  • dev loop ครบวงคือ plan → scaffold → เขียน core → เทสต์ด้วยข้อมูลจริง → แก้ส่วนที่พัง → เพิ่ม feature → commit → ถามว่าต่อไปทำอะไร ข้ามขั้นไหนก็ต้องมาจ่ายทีหลัง
  • bug โผล่กับข้อมูลจริง ไม่ใช่ใน unit test ให้ Claude พิสูจน์ด้วยไฟล์ input จริง อย่าพอใจแค่ assertion ที่แต่งขึ้นมา
  • แย้งตอนเห็นแผน อย่ารอแย้งตอนเห็นโค้ด มีโค้ด 500 บรรทัดแล้วค่อยวางแผนใหม่ แพงมาก
  • commit ทุกครั้งที่จบงานแต่ละช่วง CLI ใช้งานได้ เพิ่ม benchmark เริ่ม server scaffold รวมเป็นสาม commit ไม่ใช่ commit เดียว
  • เวอร์ชัน 2.1.215 มี plugin support, session resume และ headless mode แต่คุณค่าหลักยังอยู่ที่ interactive loop

model เป็นเหมือน junior engineer ที่เขียนตามที่คุณอธิบาย งานของคุณคือบอกให้ชัดว่าต้องการอะไรจริง ๆ ด้วย input จริง edge case จริง และ checkpoint จริง


มีคำถามหรืออยากคุยกันต่อ? ติดต่อได้ที่ LinkedIn, X หรือทาง email


  1. Claude Code v2.1.215 (2026-07-18), https://github.com/anthropics/claude-code/releases/tag/v2.1.215 ↩↩

Discussion

Have thoughts on this post? Share them below — questions, corrections, or your own experience are all welcome.