verify-goal-loop
Formally verify the /goal automatic loop before or after changing it. Use when editing internal/tui/goal.go, internal/tui/agent_loop.go's handleAgentDone/cancelAgent/steerPrompt, or internal/tools/goal.go's GoalController — or when a goal loop will not stop, stops one cycle early, closes a goal that is not met, or restarts a goal you finished.
- License 许可
- MIT
- 所属仓库
- dimetron/pi-go
- 最近更新
- 2026-11-15
- Stars
- ★ 224
- Forks
- 34
01 前言 Front Matter
- name
- verify-goal-loop
- description
- Formally verify the /goal automatic loop before or after changing it. Use when editing internal/tui/goal.go, internal/tui/agent_loop.go's handleAgentDone/cancelAgent/steerPrompt, or internal/tools/goal.go's GoalController — or when a goal loop will not stop, stops one cycle early, closes a goal that is not met, or restarts a goal you finished.
03 SKILL.md 正文
Verify the /goal loop
The /goal loop is a state machine spread over four files, and its interesting
bug is an ordering one: which of two things beginGoalCycle reads first. Nothing
in a single-unit test can see a state that only a sequence produces, which is why
this model exists.
The property: a completion reported under one objective can never close a different one, and the cycle budget bounds the automatic turns.
Does my change touch the verified code
Run this; empty output means the model still describes the code.
git diff --stat <sync-commit>..HEAD -- \
internal/tui/goal.go internal/tui/agent_loop.go internal/tools/goal.go \
internal/session/store.go internal/tui/tui.go
Model covers (each model step names the function it stands for):
| Model action | Code |
|---|---|
SetGoalIdle |
setGoal + beginGoalCycle first cycle, goal.go:63-93, 270-296 |
SetGoalBusy |
setGoal then the early return on m.running, goal.go:392-399 |
Pause |
pauseGoal, goal.go:158-180 |
Stop |
cancelAgent's goal branch, agent_loop.go:835-842 |
ResumeClose / ResumeStopped / ResumeRenew |
resumeGoal, goal.go:190-225 |
Clear |
clearGoal, goal.go:144-157 |
TurnEndQueued / TurnEndRefuse / TurnEndSteer / TurnEndNoCycle |
handleAgentDone → continueGoalLoop, agent_loop.go:2013-2075, goal.go:445-460 |
BeginCycle |
beginGoalCycle's slot read then budget check, goal.go:245-296 |
ToolComplete |
GoalController.Complete, internal/tools/goal.go:135-182 |
Steer |
steerPrompt, agent_loop.go:963 |
ManualTurn |
an ordinary prompt with no cycle behind it |
Not modelled, and why: the TUI event loop and its goroutines (the model
assumes a step completes before the next starts); persistGoal and session
restore; AppendActiveGoal's prompt text; the wall-clock UpdatedAt stamps;
plan mode; and the turn_complete/user_input_required lifecycle hooks.
A bug in any of those is invisible to this check.
Run it
hack/formal/check.sh goal # all rules hold, or the first one that breaks
hack/formal/check.sh goal --chart # also draw the statechart into hack/formal/goal/out
hack/formal/check.sh all # every model under hack/formal
TLC and a JDK are found, not installed: the script probes JAVA, then java on
PATH, then Homebrew's keg-only openjdk, then
/Library/Java/JavaVirtualMachines/*. Override with JAVA=/path/to/java and
TLA2TOOLS=/path/to/tla2tools.jar. Without TLA2TOOLS it downloads the pinned
tla2tools 1.7.4 into ~/.cache/pi-go-formal and verifies its sha256, so a
run cannot silently use a different checker.
Runtime is well under a second at the current constants. Exit codes: 0 every
rule holds, 1 a rule broke, 2 TLC or the environment failed — a 2 is never a
verdict about the code.
Read a result
Every rule holds. The model still describes the code within the scope above.
A rule broke. TLC names the rule and the shortest step sequence. Read that sequence as function calls, in order. Then decide which of three things happened:
- A real bug. The sequence is reachable in the product. Replay it against
the real code as a Go test in
internal/tui— the pattern isgoal_loop_e2e_test.go'srunTurns, which drivesbeginGoalCycleandhandleAgentDonethrough a real agent. Report it with the sequence; do not weaken the rule and do not change product code unless asked. - A modelling error. The sequence turns on something the model made indivisible that the code does not, or on a variable that does not mean what the rule assumes. Fix the model, never the rule.
- A rule that is stronger than the code promises. Check the comment the
rule cites. If the code does not claim it, the rule is wrong and must be
restated or removed. This happened twice while building this model
(
turn /= 0does not mean "a cycle is in flight", becausepauseGoalleavesTurnstamped; and "the goal is finished" does not mean "no turn may run").
TLC cannot run. Exit 2. Read the message: a missing variable means an
action does not assign it, which TLA+ reports as Successor state is not completely specified.
Rules, and the harm each one names
| Rule | The promise | What breaking it means for a user |
|---|---|---|
OnlyActiveRuns |
a cycle never runs for a goal that is not active | a paused goal keeps spending billed turns |
CompleteIsTerminal |
no cycle begins after Complete | /goal resume restarts finished work, the loop the comment forbids |
BudgetBounded |
cycles <= budget |
a runaway loop bills without end |
InFlightCycleIsCurrent |
a running cycle was actually spent | the budget under-counts and the cap is not a cap |
SlotAttributable |
a report in the slot names the running cycle | a stale report is read as a fresh one |
CompletionMatchesObjective |
a closure is based on a report about this objective | the previous objective's "done" closes the new one |
Two traps this model has already fallen into
A top-level assignment plus a branch assignment is a conjunction. Writing
/\ canceled' = FALSE and then, in a nested IF branch, /\ canceled' = TRUE
makes that branch unsatisfiable, and the action silently never fires. It cost a
false pass on the very property this model is for. Write such actions as
disjoint alternatives (see ResumeClose/ResumeStopped/ResumeRenew), and
after any edit run the lint, which reports that and three more defect classes:
go run ./hack/formal/lint/main hack/formal/goal
hack/formal/lint is a Go package with a test per defect it catches
(hack/formal/lint/lint_test.go), so every check has been seen to fire on the
shape it is meant to catch.
A small state count can mean the model never ran. An early version reported
"all rules hold" on 16 distinct states because SetGoalIdle never started a
cycle, so no loop ever existed. Check the count is in the hundreds and that a
deliberately broken model fails:
# In a scratch copy, delete setGoal's drain (goal.go:87) -- set pending' = pending
# instead of pending' = 0 in SetGoalIdle. CompletionMatchesObjective must break.
If a break does not reproduce, the model is wrong and its passing result means nothing.
After confirming the model is still true
Move the sync commit forward only once you have confirmed the model describes the code at the new commit:
git log -1 --format=%H # the commit you verified
Record it below, replacing the old value. Do not move it because the diff looked unrelated.
- Sync commit:
cc79aceb43df31012e9e5c1f69a7ba1231c32470 - Constants:
MaxCycles = 1,MaxBudget = 3(chosen to stay under the chart budget of 500 states;MaxCycles = 2, MaxBudget = 4gives 556 states and the same verdicts, and is what to use when you want the wider bound without a chart).
Draw the chart
hack/formal/check.sh goal --chart writes hack/formal/goal/out/report.md and
chart.mmd, and runs the pstack /statechart skill. Read every TLA+ result with
/statechart: it turns each violation into the shortest path in the user's
words and, when nothing breaks, draws how the system moves between states. When
the chart hides steps, the report says how many.
04 同仓库更多 Skills
verify-session-flow
Formally verify the interactive session flow before or after changing it. Use when editing internal/tui/tui.go's handleKey, handleInterruptKey, handleInputSubmit, ctrlCCount or quitting, internal/tui/agent_loop.go's enqueuePrompt, submit, steerPrompt, cancelAgent or handleAgentDone, or the deferred-init splash — or when a user reports that Ctrl+C quit on the first press, that Esc did not stop a turn or a goal loop, that a queued prompt was lost when they canceled, or that the TUI hung on a spinner nothing could clear.
verify-run-pipeline
Formally verify the /run phase machine before or after changing it. Use when editing internal/tui/run.go's phase transitions (handleRunAgentDone, runGates result handling, verifyRunComplete, retryRun, reportGateHang, merge), internal/sop/verdict.go's Passed, or the checklist refresh — or when a run merged work it should not have, retried a hung gate, lost a cycle at the retry budget, or merged on a stale PASS.
verify-history-rewrite
Formally verify the session-history rewrite path before or after changing it. Use when editing internal/session/autocompact.go, internal/session/compaction*.go, internal/session/store.go's Compact/ClearEvents/rewriteEvents, internal/agent/agent.go's Run/RunStreaming/runPreTurn, or internal/tui/commands.go's clearConversation/clearSessionContext and the context gauge — or when a turn is canceled mid-flight, a tool call and its result get separated, a session's history disappears while the agent is still working, or the model is sent a tool result whose call was compacted away.
verify-agent-lifecycle
Formally verify the subagent lifecycle before or after changing it. Use when editing internal/subagent/orchestrator.go's Spawn, trackAgent, forwardAgentEvents, terminalStatus, Cancel, ShutdownWithTimeout or evictCompletedAgentsLocked, or internal/subagent/pool.go — or when a canceled agent is reported as failed, an agent vanishes from the list with no verdict, the orchestrator panics on a pool release, or an agent can be canceled twice.
test-new-skill
暂无简介
ponytail
暂无简介