Always in the middle of something.

Chasing ideas across ML, AI, and data. Building tools when the rabbit hole gets interesting enough.

Browse by topic →

Python List Comprehensions: Read Them as For-Loops

Python List Comprehensions: Read Them as For-Loops

TL;DR: A list comprehension like [n*n for n in range(5)] does the same thing as a small for-loop. It just writes the result first and the source second, which is the opposite of the order you’d write the loop in. If something trips you up, it’s probably that reversal, not the concept. Translate it back into a for-loop and most of the mystery tends to go away. Seeing [x for x in data if x > 0] for the first time and pausing for a second seems like a pretty normal reaction. It doesn’t look like the statements you’ve been writing. No colon, no indentation, and the for has wandered into the middle. Plenty of tutorials just say “this is a list comprehension, it’s very Pythonic” and move on, but I’m not sure that line actually helps anyone read the thing. ...

2026-05-31 · 10 min read · 1973 words · KbWen · EN
Python 列表推導式:一行取代 for 迴圈

Python 列表推導式:一行取代 for 迴圈

TL;DR:列表推導式 [n*n for n in range(5)] 其實就跟一個 for 迴圈做一樣的事,只是把「結果」寫在最前面、「來源」丟到後面,順序剛好跟念中文相反。會看不懂多半不是因為它難,比較像是這個順序要花點時間習慣。能把它翻回 for 迴圈來看的話,大概就沒那麼可怕了。 第一次看到 [x for x in data if x > 0] 這種東西會愣一下,我覺得滿正常的。它長得不太像一般的句子,沒冒號、沒縮排,for 還跑到中間去。很多地方會直接說「這叫列表推導式(list comprehension),很 Pythonic」就帶過,可是那句話其實對看懂它沒什麼幫助,看完還是一樣霧。 所以這篇就不背語法了,從大家應該都會的 for 迴圈開始慢慢聊好了,不趕時間。 同一件事,兩種寫法 假設要做一個 0 到 4 的平方數清單。用 for 迴圈大概像這樣寫: squares = [] for n in range(5): squares.append(n * n) ## [0, 1, 4, 9, 16] 三行,開個空 list、跑迴圈、一個一個 append 進去。很普通,沒什麼問題,能跑就好。 換成列表推導式的話,就變這樣: squares = [n * n for n in range(5)] ## [0, 1, 4, 9, 16] 一行,結果一樣。這倒不是我隨口說的,文末附的測試檔就是把這兩種寫法的結果丟去 assertEqual 對,跑出來是相等的。所以大概可以放心把它當成上面那段 for 迴圈的縮寫,因為它字面上差不多就是那個意思,只是擠成一行而已。 怎麼讀它:翻回 for 迴圈來看 我覺得重點在閱讀順序。推導式長這樣: [ 運算式 for 變數 in 來源 ] n * n for n in range(5) 拆成三塊來看的話: ...

2026-05-31 · 4 min read · 646 words · KbWen · ZH
A three-stage evolution diagram: a small four-line atomic skill on the left, a cluster of overlapping skills in the middle (pattern emerging), and a taller seventeen-line production skill on the right, connected by dashed timeline arrows

The Skill Your Annoyed Prompt Becomes

TL;DR: Your first Claude Code skill won’t look like the polished examples you’ve read about. It’ll look like a prompt you’ve typed three times in a row, saved into a .md file. This post walks through that minimum-viable shape with a hypothetical four-line /structure-findings skill, shows the three things that break when you save it and type the slash command, then compares it to a real seventeen-line production-grade skill from the framework I use day to day. The longer one has more lines because it has older scars. That’s the whole arc. ...

2026-05-28 · 7 min read · 1480 words · KbWen · EN
三層演化圖:左邊一個 4 行 atomic skill 草稿,中間幾個 atomic skill 群聚,右邊一個成熟的 17 行 production skill,細線串成時間軸

怎麼寫你的第一個 skill — 從一個煩躁的 prompt 開始

TL;DR: 你的第一個 skill 不會長得像書裡那些 production-grade 的成熟形態, 它會長得像「你重複打三次的同一個 prompt」。本篇示範一個假設的 4 行入門版怎麼寫、存到哪裡、為什麼會沒跑、撞坑後怎麼修, 然後對照我自己框架裡真的在用的 17 行版本 — 看那些多出來的東西其實都是「撞坑後加上的補丁」, 不是設計階段一次想出來的。 你有沒有打過同一個 prompt 三次?同一段話、同一個格式、同一個角度, 三次。第三次你會開始煩, 第四次你會想:這個我能不能存起來? 那一刻其實就是你的第一個 skill。 不是說那個 prompt 寫得有多好, 是「想存起來」這個衝動就是 skill 出現的位置 — 你已經辨識出一個重複會發生的模式。剩下的事沒什麼神秘的, 把它丟進一個 .md 檔, 用 slash 叫出來而已。 前三篇我談過 skill 是什麼 ——跟 prompt 的差別在哪一層、邊界怎麼劃、拆一個 13 行的 dispatcher 給你看。但這篇有個之前都沒講的事:你的第一個 skill 不會長得像那些。你看過的成熟 skill, 不管是我框架裡的還是別人的, 都是寫了很多次、撞了幾次坑之後才變成那樣的。直接從那邊倒著學, 容易卡在「為什麼要有這條」這種對學習沒幫助的地方。比較好的起點, 就是你那個煩躁的 prompt。 假設你常做研究 舉個例子。你最近常請 AI 幫你看一些雜七雜八的研究筆記 — 三段你 google 來的東西、半段別人 Slack 給你的、一張 issue 截圖貼的內文。你想把它分類:哪些是已經查證的事實、哪些是還沒驗證但你假設成立的、哪些是現在還不知道的問題。 每次貼進去你都打差不多的字:「幫我把下面這團分成 Facts / Assumptions / Unknowns, 三個列點。」打過三次, 想存。 ...

2026-05-28 · 3 min read · 553 words · KbWen · ZH
A cutaway view of two files side by side: a small 13-line dispatcher on the left, a heavier protocol file on the right, connected by a thin line

What a 13-Line Skill Leaves Out

TL;DR: A skill I asked Claude to draft came back as thirteen lines of markdown. Less than a function. The thirteen lines aren’t the skill — they’re a dispatcher pointing to a longer file where the contract actually lives. That split is what separates a skill from a prompt. And the part the model was reliably wrong about — the real interface of the external tool the skill talks to — is the part a human still has to verify. ...

2026-05-27 · 6 min read · 1235 words · KbWen · EN
兩份檔案的剖視對照:左邊是 13 行的 /codex-cli skill,右邊是它指向的、比較厚的 workflow

13 行的 skill:AI 起稿,我事後才看懂

TL;DR: 我請 AI 幫我寫一個能從 Claude Code 裡呼叫 OpenAI Codex CLI 的 skill,它給我 13 行 markdown。看起來小到不像個東西。但這 13 行不是 skill 的全部——真正讓它變成 skill 而不是 prompt 的,是它指過去的那一份比較厚的東西。Skill 是契約的形狀,不是指令的形狀。 前三篇我談過 skill 是什麼。一個 AI Skill 和 Prompt 到底差在哪 把它放進 stack 裡的某一層, Skill 邊界設計 講為什麼邊界比能力重要, Skill Design as Interface Design 是英文版用 API 設計的角度寫的。 這篇換個角度:真的拿一個給你看。 挑 /codex-cli 這個 skill,因為它小到適合當教材。它在我那邊的工作是:從 Claude Code 想把某個任務丟給 OpenAI 的 codex CLI 去跑,我打 /codex-cli 修 README 的 typo,它就接手。 13 行長這樣 我請 AI 幫我寫第一版的時候,它給我這個: # /codex-cli Execute the canonical workflow: `.agent/workflows/codex-cli.md` ## Execution Follow every step in `.agent/workflows/codex-cli.md` sequentially. The user's task description is: $ARGUMENTS - [OPTIONAL MODULE] Requires globally installed `codex` CLI (`npm install -g @openai/codex`). - If CLI is unavailable, inform the user and fall back to native execution. - End response with ⚡ ACX. 就這樣。13 行。沒有複雜的 prompt engineering, 沒有冗長的 system message, 沒有 chain-of-thought 模板。第一次看到的時候我有點疑惑:這就是一個 skill? ...

2026-05-27 · 2 min read · 377 words · KbWen · ZH
MCP 資安危機:問題不在協定,而在治理

MCP 資安危機:問題不在協定,而在治理

TL;DR: MCP 在一年多內,從 Anthropic 的內部實驗變成 AI 業界共通的介面。但進入 2026 年,資安研究員一個接一個把它拆開:官方 SDK 的 by-design RCE、tool poisoning、rug pull。我的看法是,這些漏洞大多不是協定的 bug,而是「把能力交出去、卻沒把治理一起交出去」的必然結果。現在大家急著補的那些東西,OAuth scope、人工確認、伺服器註冊表,其實就是治理被重新貼回協定上。 2026 年 4 月,資安團隊 OX Security 公布了一個發現:MCP 的官方 SDK(Python、TypeScript、Java、Rust 全中)存在一條從設定檔直接到指令執行的路徑,攻擊者可以在任何跑著有問題實作的機器上執行任意系統指令。根據他們的估算,受影響的套件下載量超過 1.5 億次,潛在波及的伺服器實例上看 20 萬個(The Register 的報導用的標題就是「20 萬台伺服器有風險」)。後續整個生態跟著冒出一連串 CVE,包括 MCP Inspector 的 CVE-2025-49596 和 Cursor 的 CVE-2025-54136。 但真正讓我停下來想的,是 Anthropic 的回應:這是設計如此(by design)。他們不打算改協定,並表示輸入清洗是開發者自己的責任。 這句話可以有兩種讀法,而我認為兩種都對。這正是整件事最值得想的地方。 先講清楚:MCP 為什麼會贏 要評論 MCP 的資安問題,得先承認它解決了一個真的很煩的問題。 在 MCP 之前,每接一個工具到 AI 上,你就得寫一套各自為政的膠水。M 個模型乘上 N 個工具,等於 M×N 種接法。MCP 把它變成 M+N:工具實作一次 server,模型實作一次 client,中間用同一套協定講話。Anthropic 當初的比喻是「AI 的 USB-C」,這個比喻站得住,是因為它真的描述了發生的事。 ...

2026-05-25 · 2 min read · 299 words · KbWen · ZH
MCP Security Isn't a Protocol Bug. It's a Governance Problem.

MCP Security Isn't a Protocol Bug. It's a Governance Problem.

TL;DR: MCP went from an Anthropic side-project to the industry’s default agent-to-tool interface in about a year. Then 2026 brought a steady drip of disclosures: a by-design RCE in the official SDKs, tool poisoning, rug pulls. My read is that almost none of these are protocol bugs. They’re what happens when you ship capability without shipping governance, and the patches now landing (OAuth scopes, human-in-the-loop, registries) are just governance being bolted back on. ...

2026-05-25 · 7 min read · 1464 words · KbWen · EN
一份 SKILL.md 檔上疊著 API 合約圖(輸入、輸出、範圍),顯示兩者其實是同一個形狀

Skill 邊界設計:從能力到合約

TL;DR: 一個 skill 會多可預測,大概就看它的邊界劃得多清楚。把它當成「能力」(這個 skill 讓 AI 會做 X),它容易亂跑;把它當成「合約」(講好輸入、輸出、以及它不會碰什麼),它就比較像一個設計良好的 API。重點不是寫更多「要小心」,而是把它能碰的範圍框住。 我有個 skill,前一天還用得好好的,隔天就開始亂搞:我要它做 A,它順手把旁邊的 B 也「幫我」改了。我第一個反應是怪模型今天狀況不好。後來才想通,問題在我自己——當初寫這個 skill 的時候,我只說了它「會做什麼」,沒說它「不能碰什麼」。 這是英文版的中文對照,英文那篇用 API 設計的角度談;這篇是我自己怎麼從「能力」這個想法,慢慢搬到「合約」這個想法的過程。 能力清單 vs 合約 我們很習慣用「能力」來描述一個 skill:「這個 skill 讓 AI 會跑測試」「這個會幫我部署」。這種講法很自然,也很容易讓你之後被嚇到。因為「會跑測試」這句話沒有邊。它沒講會讀什麼、會動什麼、遇到不在預期內的狀況時會怎麼處理。 合約天生就有邊。它講清楚什麼東西進去、什麼東西出來、以及哪些地方它不會碰。框架文件裡有一句講得比我直接:skill 應該被當成「有版本、受政策約束、能力被框住的封裝」,而不是「一堆鬆散的 prompt 檔」。這跟一個 AI Skill 和 Prompt 到底差在哪講的是同一件事:重點不是 AI「能」做那件事,而是那件事被「講清楚」了。 合約先行:先想清楚它不該做什麼 我看過一個還算貼切的比喻:skill 像是你交給一位很強的廚師的食譜。食譜提供的是結構:材料、順序、限制;廚師提供的是判斷,什麼時候醬汁該再收一下、什麼時候可以換個材料。你不會因為廚師很強,就把食譜寫成「做一道好吃的菜」。 skill 也一樣。模型本身的判斷力很好,所以你要補的不是判斷,是結構。而結構裡最常被漏掉的,就是那條「不要碰」的線。我現在寫一個 skill,會先問自己一個問題:這個 skill 最不該做的事是什麼? 把那條線寫進去,比再多寫三條「請謹慎處理」都有效。 邊界鬆掉,其實是一次沒講的破壞性變更 框架裡有一個原則我覺得很受用:寧可在邊界上把關,也不要去微管模型怎麼想。一個會亂跑的 skill,你通常修不好它的「想法」;你能做的是把它能碰的範圍(哪些檔案、哪些工具、多少預算)框起來。 一個 skill 的範圍如果隨著時間悄悄變大,那其實是你發佈了一次「破壞性變更」卻沒有改版本號。而呼叫它的人(就是你自己)會用最經典的方式發現這件事:在出事的時候。這也是AI 代理常見痛點裡那個「能力邊界」缺口最貴的一種表現形式。範圍,只是它最容易爆出來的地方。 我把一個 skill 從能力改成合約的前後 回到開頭那個亂改 B 的 skill。它原本的描述大概是「整理這個模組的程式碼」。很開放,聽起來很厲害,結果就是它對「整理」的理解跟我不一樣。 我後來把它改寫成比較像合約的樣子:輸入是「指定的那幾個檔案」,輸出是「格式化後的同一批檔案 + 一份它改了什麼的清單」,然後明確寫上「不要新增或刪除檔案、不要碰指定範圍以外的東西」。改完之後它沒有變笨,只是不再自作主張。差別不在能力,在邊界。 順帶一提,邊界清楚的 skill 通常也比較便宜。skill 是漸進載入的:AI 先讀那一小段 metadata 判斷現在用不用得上,真的要用才載入完整內容。一個邊界小而清楚的 skill,光看它的「合約」就能被快速略過;一個什麼都做的 skill,得把整包拖進 context 才發現其實不該用它。這跟我在 Token 成本那篇講的是同一個方向:清楚的小合約,既好預測也比較省。 ...

2026-05-25 · 1 min read · 108 words · KbWen · ZH
An API contract diagram (inputs, outputs, scope) overlaid on a SKILL.md file, showing the two are the same shape

Skill Design as Interface Design

TL;DR: An agent skill behaves predictably to about the degree its boundary is specified. Described as a capability (“the agent can now do X”), a skill tends to drift. Described as a contract (declared inputs, declared outputs, a scope it promises not to exceed), it behaves more like a well-designed API. The interface-design habits engineers already have (stable contracts, explicit scope, versioning) seem to transfer directly. The framework’s own direction points the same way: skills as versioned, capability-bounded packages, with boundary enforcement instead of micromanaging how the model reasons. ...

2026-05-25 · 4 min read · 827 words · KbWen · EN