Hooks and Background Tasks: Lifecycle Hooks, Deterministic Checks, and Notifications That Don't Interrupt the Conversation
Some things shouldn't be left to the model's judgment: implement lifecycle hooks that insert deterministic checks and formatting before and after tool execution, then implement background tasks so a long-running command doesn't block the conversation and notifies the REPL when it finishes.
Today's Goals
- Design a hook's trigger points and input/output protocol, and explain what it can and can't block
- Implement background-task launch, status query, and completion notification without interrupting an ongoing conversation
- Judge which constraints belong in a hook and which belong in the prompt or the permission rules
Seventeen days went into making the model better at its job. Today is the first day we go the other way: we take some of the deciding away from it. When you finish, scroll back up and tick the three goals off.
Plain-Language Walkthrough
The shop floor's automatic inspection station
On a production line, each station is followed by an automatic inspection post: the part comes out, a caliper measures it, and if the number is off the line stops. The post is not clever — it measures one dimension against one threshold. That is precisely why it is there: a post that only holds a caliper gives the same answer on the ten-thousandth part; a veteran who looks and thinks gets tired by then.
Everything we added to mca so far was in the "make it smarter" family: more tools, better context, someone else's experience, helpers that split the work. All of it pushes the outcome in a good direction, but what it pushes is probability. A rule in the system prompt — say, "read a file before you edit it" — is followed most of the time, and the whole problem hides in most of the time: the one file it edits from memory will be the one somebody else already changed, the exact replacement lands in the wrong place, and that mistake survives code review because it looks completely normal.
So today we build that inspection post: no judgment, no reasoning, one interception before a tool runs and one after. Yesterday we dispatched a whole model to work for us; today is the mirror image — we take a slice of the deciding back from the model and give it to code. There is a reason this arrives only on day eighteen: you have to see how much a model can do before you understand which parts it should not be doing.
Interview questions
Which constraints belong in hooks and which belong in the prompt? What is the criterion?哪些约束该用钩子实现,哪些该写进提示词?判据是什么?
Common in ChinaCommon overseasBasic#hooks#prompt-engineering#agent-design🔒 Unlock this chapter to see the answer keys.
How do you design hooks so they can veto a call without deadlocking the agent?钩子怎么设计才能既能否决又不至于把 Agent 卡死?
Common in ChinaCommon overseasDeep dive#hooks#reliability#failure-handling🔒 Unlock this chapter to see the answer keys.
When a long-running command goes to the background, how do its status and result get back into the session?长时间运行的命令放到后台,状态与结果怎么回到会话里?
Common in ChinaCommon overseasIntermediate#background-tasks#async#terminal-ux🔒 Unlock this chapter to see the answer keys.
