00:03 搜索框竞态问题与先写下自己的判断
搜索框中先输入 cat、再输入 cats,会发出两次请求;如果较早的 cat 请求最后返回,旧结果就会覆盖 cats。清空输入时也必须保持空白。第一项习惯是在询问智能体之前写下自己的故障猜测:这里猜测的是“最后返回者获胜”,并把期望行为和不得更改搜索 API 的限制写入需求。清空场景虽然列在需求里,却没有包含在最初的原因猜测中,这个缺口之后会影响测试覆盖。
AICodeKing · 06:33
hi, welcome to another video.
嗨,欢迎收看新一期视频。
so Addy Osmani wrote an article on x called critical thinking, when agents do the work.
那么,Addy Osmani 在 X 上写了一篇文章,题为《当智能体开始干活时的批判性思维》。
it's from november 3rd, and one line from it really stuck with me.
文章发表于 11 月 3 日,其中有一句话让我印象特别深刻。
when a check goes green, ask what it proved, then ask what it didn't, because agents are good now.
检查通过时,要问它证明了什么,再问它没证明什么,因为现在的智能体已经很厉害了。
most of what they hand back reads well, and most of it is right, and that's the problem.
它们交出的东西大多读起来很不错,而且大多也确实正确,问题就在这里。
nothing about it asks for a second look.
没有任何东西会让你想再检查一遍。
so, i'm going to apply a few habits from that article to one small, very common kind of bug.
所以,我打算把那篇文章里提到的几个习惯,应用到一种很小、很常见的 bug 上。
it's a search box.
它就是一个搜索框。
you type cat, then you keep typing, and it becomes cats.
你输入 cat,然后继续输入,它就变成 cats。
two requests go out.
于是发出了两个请求。
the cat request starts first, but it comes back last, and the box has to show cats, not cat.
cat 的请求先发出,却最后才返回,而搜索框必须显示 cats,而不是 cat。
there's a second rule too.
还有第二条规则。
if you clear the box while a request is still running, the box has to stay empty.
如果请求还在运行时你清空了搜索框,那么搜索框就必须保持为空。
tiny bug.
这是个小 bug。
but it can be fixed halfway, and a green check will still say everything is fine.
但它可能只被修好了一半,而绿色的通过标记仍会显示一切正常。
so, let's get right into it.
那么,我们这就开始吧。
the first habit is to write down your own guess before you ask the agent anything, even if the guess is bad.
第一个习惯是,在向智能体提问之前,先写下自己的猜测,哪怕这个猜测不靠谱。
the article's reasoning is that once you've read a polished answer, it's really hard to see any other option.
文章的理由是,一旦你读过一个经过润色的答案,就很难再看到其他可能性。
you won't notice when the agent is confidently wrong, so here's my guess for the search box.
你不会注意到智能体什么时候自信满满地答错了,所以这是我对这个搜索框的猜测。
the cat response arrives after the cats response, and it overwrites the results.
cat 的响应比 cats 的响应晚到,于是覆盖了搜索结果。
last response wins.
最后返回的响应胜出。
that's the bug.
这就是那个 bug。
so, the prompt is.
所以,提示词是这样的。
the search box shows old results.
搜索框会显示旧结果。
show results for the latest query only.
只显示最新查询的结果。
when the user clears the box, keep it empty.
用户清空搜索框时,让它保持为空。
do not change the search api.
不要修改搜索 API。
if you want a template you can copy, it's this.
如果你想要一个可以直接套用的模板,就是这个。
here is the symptom.
这是出现的症状。
here is my guess at the cause.
这是我对原因的猜测。
here is the behavior i want.
这是我想要的行为。
here is what you must not change.
这是你绝不能修改的部分。
now, notice that i put the clear case in the prompt.
现在,注意我在提示词中写明了清空搜索框时的情况。
but my guess only talks about the order of the responses.
但我对原因的猜测只提到了响应的先后顺序。
keep that in mind, because that gap comes back.
记住这一点,因为之后还会遇到这个缺口。
the second habit is to decide what done means before you look at any fix.
第二个习惯是,在查看任何修复方案之前,先明确怎样才算完成。
the article says to define verified up front, because after the fact you'll rationalize any outcome.
文章建议一开始就定义好什么叫验证通过,因为事后你会为任何结果找理由。
and done should not be, i typed in the box and it looked fine.
而且,完成的标准不应该是:我在搜索框里输入了内容,看起来没问题。
this bug depends on network timing.
这个 bug 取决于网络时序。
on your machine, the cat's response might always arrive last, so a manual check can pass by pure luck.
在你的机器上,cat 的响应也许总是最后到达,所以手动检查可能纯属运气好,才通过了。
i want a test that forces the bad order every single run.
我想要一个每次运行都会强制触发错误顺序的测试。
so, the prompt is, write a test with a fake server.
所以,提示词是:编写一个使用假服务器的测试。
each request returns a promise that i resolve by hand.
每个请求都返回一个由我手动解决的 Promise。
start a search for cat, then start a search for cats.
先搜索 cat,再搜索 cats。
resolve cats first, then resolve cat.
先解决 cats 的请求,再解决 cat 的请求。
the box must show cats.
搜索框必须显示 cats。
that's a deferred promise, so the test decides the order, not the network.
这是一个延迟 Promise,所以顺序由测试决定,而不是由网络决定。
and here's a template for this one.
这是针对这一项的模板。
write a test that fails on the current code.
编写一个在当前代码上会失败的测试。
control the timing by hand.
手动控制时机。
do not use sleeps or timeouts.
不要使用 sleep 或 timeout。
show me the test failing before you fix anything.
修复之前,先让我看看测试失败。
now, for this demo, i have three versions of the same search box.
现在,在这个演示中,我准备了同一个搜索框的三个版本。
i wrote them for this video.
这是我为这段视频写的。
they're not from a real agent session.
它们并非来自真实的智能体会话。
the buggy one gives every search a number from a counter, but nothing checks it.
有 bug 的版本会用计数器给每次搜索分配一个编号,但没有任何地方会检查它。
whatever arrives last wins.
最后到达的结果会胜出。
the partial fix checks the number when a response comes back.
部分修复版会在响应返回时检查编号。
if a newer search has started, the response drops itself.
如果已经开始了更新的搜索,就丢弃这个响应。
and honestly, that sounds like the whole fix.
说实话,这听起来就像是完整的修复方案了。
then there's the full fix, which we'll get to.
然后是完整修复方案,我们稍后会讲到。
now, the third habit, when an agent says it's finished, the article says to read the command that ran and its output before you read the summary.
现在说第三个习惯:当智能体说已经完成时,文章建议先阅读运行的命令及其输出,再看总结。
so let's run the test on the buggy version.
那么,我们在有 bug 的版本上运行测试。
the command is node --test with a test name pattern of older.
命令是 node --test,测试名称模式设为 older。
that picks only the cat and cats test, which is all we have so far, and it fails.
这样只会选中 cat 和 cats 这个测试,这是目前唯一的测试,但它失败了。
expected cats, actual cat, pass 0, fail 1.
预期是 cats,实际是 cat,通过 0 项,失败 1 项。
so, my guess was right.
看来我的猜测是对的。
now, the same command on the partial fix, and it passes.
现在,在部分修复版本上运行同一条命令,测试通过了。
pass 1, fail 0, exit status 0 green.
通过 1 项,失败 0 项,退出状态为 0,显示绿色。
but look at the command.
但看看这条命令。
there's a name filter on it.
它带有名称筛选条件。
only one test ran.
只运行了一个测试。
imagine an agent ran this and wrote back, all tests pass, technically true, but you only see how little ran.
试想智能体运行了这条命令,然后回复说“所有测试都通过了”——严格来说这没错,但你只有看了命令,才会发现实际运行的测试少得可怜。
by reading the command.
只有看了命令,你才会发现这一点。
this is where most of us would ship it.
我们大多数人到这里就会把它发布出去。
so what did this green check prove?
那么,这个绿色勾号证明了什么?
that an old cat response can't overwrite cats.
它证明旧的 cat 响应不会覆盖 cats。
that's it.
仅此而已。
it proved nothing about clearing the box.
它并没有证明清空盒子这件事没问题。
here's a template for when an agent says it's done.
下面是一个模板,以便在智能体说已经完成时使用。
before your summary, show me the exact command you ran and its full output.
在总结之前,把你运行的确切命令和完整输出给我看。
tell me which tests ran and which ones were skipped or filtered.
告诉我运行了哪些测试,哪些测试被跳过或筛选掉了。
the fourth habit is to get a second opinion from something that doesn't share your guess.
第四个习惯是向一个不认同你猜测的对象征求第二意见。
the article points out that a coding agent can critique its own work through a sub-agent with a fresh context.
文章指出,编程代理可以通过一个拥有全新上下文的子代理来审查自己的工作。
so, i use a new session or a sub-agent.
所以,我会开启一个新会话,或者使用一个子代理。
it only sees the requirements and the diff.
它只会看到需求和差异内容。
the article also suggests asking what would have to be true for the result to be wrong, then checking it.
文章还建议问问,要让结果出错,必须满足什么条件,然后再进行检查。
so the reviewer prompt is, review this diff.
所以,给审查者的提示是:审查这份差异内容。
do not trust the summary.
不要相信总结。
find one path that the tests do not cover.
找出一条测试未覆盖的路径。
write a test for it.
为它写一个测试。
and a fresh reviewer is likely to go straight for the clear path.
而一个全新的审查者很可能会直接找到这条明显的路径。
i'm saying likely because this part is illustrative, but it's in the requirements and it has no test.
我说“很可能”,是因为这部分只是举例说明,但它确实写在需求里,而且没有对应的测试。
so the new test is, start a search for cat, clear the box, then the cat response arrives.
所以,新测试的内容是:开始搜索 cat,清空输入框,然后 cat 的响应到达。
the box must stay empty.
输入框必须保持为空。
now, we run the whole test file on the partial fix with no filter, one pass, one fail.
现在,我们不加任何筛选,运行整个测试文件来检查这个部分修复;一项通过,一项失败。
the clear test expected an empty box, and the actual result is cat.
清空测试期望输入框为空,但实际结果是 cat。
here's why.
原因如下。
the partial fix only drops a response when a newer search has started.
这个部分修复只会在开始了更新的搜索时丢弃响应。
the cat search took number one.
cat 搜索的编号是 1。
clearing the box didn't take a new number, so when the cat response arrives, number one is still the latest, and it writes cat into a box the user just emptied.
清空这个框并没有生成一个新编号,所以当猫的响应返回时,编号一仍然是最新的,它就把“猫”写进了用户刚清空的框里。
and remember, my guess only talked about order.
记住,我的猜测只谈到了顺序。
my test matched my guess.
我的测试验证了我的猜测。
so it missed the clear path too.
所以它也漏掉了清空的情况。
a test can only check what you thought to check.
测试只能检查你想到要检查的内容。
now, the full fix.
现在说完整的修复方案。
the only difference is that clearing the box also takes a new number.
唯一的区别是,清空这个框时也会生成一个新编号。
we run the whole file again.
我们再运行一遍整个文件。
two pass, zero fail.
两项通过,零项失败。
the last habit is to explain the fix from memory, without the code.
最后一个习惯是,不看代码,凭记忆解释修复方案。
the article's point is that if you can't explain the answer, you've given up ownership of it.
文章的重点是,如果你无法解释答案,就等于放弃了对它的掌控。
every search takes a new number.
每次搜索都会生成一个新编号。
clearing the box also takes a new number.
清空这个框时也会生成一个新编号。
when a response comes back, it only writes to the box if its number is still the latest.
响应返回时,只有在它的编号仍是最新编号的情况下,才会写入框中。
so the late cat response finds a newer number and it just drops itself.
所以,迟到的猫的响应会发现已经有了更新的编号,于是就直接丢弃自己。
if i can't say that out loud, i don't own the fix.
如果我不能把它大声说出来,那我就没有真正掌握这个修复方案。
i just merged it.
我刚刚把它合并了。
anyway, none of this needs to be heavy.
总之,这些都不必搞得很复杂。
so a quick recap of the five habits.
下面快速回顾一下这五个习惯。
one, write your guess before you ask.
第一,提问之前先写下你的猜测。
two, decide what done means before you see the result.
第二,在看到结果之前,先决定什么才算完成。
three, read the command and the output before the summary.
第三,先阅读命令和输出,再看总结。
in our demo, only one test ran, and it still went green.
在我们的演示中,只运行了一个测试,但结果仍然是通过。
4.
第四。
ask a fresh reviewer what the checks didn't cover.
找一位新的审查者,问问这些检查还遗漏了什么。
5.
第五。
explain the change in your own words before it ships.
在发布之前,用你自己的话解释这项改动。
and one more from the article that i really like.
还有一条我很喜欢的建议,来自那篇文章。
write some code without an agent each week, because skills you stop using fade faster than you'd expect, so the agent can do the work, but you still have to be able to stand behind it.
每周在没有智能体帮助的情况下写一些代码,因为不用的技能会比你想象中更快退化。智能体可以替你完成工作,但你仍然得有能力为它的成果负责。
the article is a really good read, and i'll link it below.
那篇文章真的很值得一读,我会把链接放在下面。
anyway, let me know your thoughts in the comments.
总之,欢迎在评论区告诉我你的想法。
if you like this video, consider donating through the super thanks option or becoming a member by clicking the join button.
如果你喜欢这期视频,可以通过“超级感谢”选项捐赠,或者点击“加入”按钮成为会员。
also give this video a thumbs up and subscribe to my channel.
也请给这期视频点个赞,并订阅我的频道。
i'll see you in the next one.
我们下期再见。
until then, bye.
那就先这样,拜拜。
anyway, let me know your thoughts in the comments.
总之,欢迎在评论区告诉我你的想法。
if you like this video, consider donating through the super thanks option or becoming a member by clicking the join button.
如果你喜欢这期视频,可以通过“超级感谢”选项捐赠,或者点击“加入”按钮成为会员。
also, give this video a thumbs up and subscribe to my channel.
也请给这期视频点个赞,并订阅我的频道。
i'll see you in the next one.
我们下期再见。
until then, bye.
那就先这样,拜拜。
视频以搜索框的异步竞态问题为例,演示如何在使用编程智能体时保持对代码的判断和责任:先独立写下对故障原因的猜测,再预先定义完成标准,并用可手动控制响应顺序的延迟 Promise 测试验证。演示比较了有缺陷、部分修复和完整修复三个版本,说明只运行经过名称筛选的测试会让未覆盖清空输入场景的部分修复呈现绿色。随后通过独立审查补充清空路径测试,并验证完整修复。最后总结五种习惯,并提醒每周仍要亲自写代码,以维持技能和对交付结果的所有权。
搜索框中先输入 cat、再输入 cats,会发出两次请求;如果较早的 cat 请求最后返回,旧结果就会覆盖 cats。清空输入时也必须保持空白。第一项习惯是在询问智能体之前写下自己的故障猜测:这里猜测的是“最后返回者获胜”,并把期望行为和不得更改搜索 API 的限制写入需求。清空场景虽然列在需求里,却没有包含在最初的原因猜测中,这个缺口之后会影响测试覆盖。
完成标准应在检查修复之前确定,不能用一次手动输入看起来正常作为验证,因为网络时序可能让错误偶然不出现。测试应使用假服务器和手动控制的 Promise:先发起 cat,再发起 cats,先解析 cats、再解析 cat,并要求界面仍显示 cats;不使用 sleep 或 timeout,还要先证明测试在旧代码上失败。演示准备了三个搜索框版本:缺陷版给请求编号却不检查编号;部分修复在响应返回时检查编号,若已有更新搜索就丢弃旧响应;完整修复留待后续说明。
检查智能体结果时,应先读实际运行的命令和输出,再看总结。对缺陷版运行带有 older 名称筛选的 node --test 命令,只执行 cat 与 cats 的测试,结果为预期 cats、实际 cat,0 通过、1 失败。相同命令在部分修复上得到 1 通过、0 失败和绿色退出状态,但命令的名称筛选表明只运行了一个测试。这个绿色结果只证明旧 cat 响应不能覆盖 cats,完全没有验证清空输入后的行为;因此要求提供完整命令、完整输出,以及运行、跳过或筛选掉的测试信息。
第二意见应来自不共享原有猜测的新会话或子智能体,只提供需求和差异,并要求找出测试未覆盖的路径、为其编写测试。一个可能被发现的路径是:发起 cat 搜索、清空输入、随后 cat 响应才到达;此时输入框必须保持空白。对部分修复运行未加筛选的完整测试文件,得到一项通过、一项失败,失败测试中实际结果是 cat。原因是部分修复只在新搜索开始时更新编号,而清空操作没有取得新编号;因此旧请求返回时仍被视为最新请求,重新写入了 cat。原先的猜测和测试都只关注响应顺序,因而漏掉了清空路径。
完整修复与部分修复的唯一区别是:清空输入时也取得一个新编号。重新运行整个测试文件后,两项通过、零项失败。其逻辑是每次搜索都取得新编号,清空时同样更新编号;响应返回时,只有编号仍是最新的请求才能写入结果,所以迟到的 cat 响应会因编号过期而被丢弃。能够脱离代码用自己的话解释这一机制,意味着没有放弃对修复的理解和责任。
五项习惯是:先写下自己的猜测;在查看结果前定义完成标准;阅读运行命令及输出;请新审查者指出检查未覆盖的部分;在交付前用自己的话解释改动。另一个建议是每周在没有智能体的情况下亲自写一些代码,因为停止使用的技能会逐渐退化;即使智能体承担了工作,人仍须能够理解并为结果负责。
00:31 异步搜索应只展示最新查询的结果,清空输入后也必须保持为空。
01:39 应在查看修复结果之前定义可重复的验证标准,并通过手动控制 Promise 解析顺序稳定复现竞态。
02:58 测试通过不等于验证充分;检查命令和输出才能确认测试实际覆盖了什么。
04:38 只让新搜索更新请求编号会漏掉清空输入的路径,因此清空时也必须更新编号,才能阻止旧响应重新填入结果。
05:10 使用编程智能体时仍要亲自理解改动、保持审查能力,并定期不依赖智能体写代码以维持技能。