跪拜 Guibai
← All articles
Artificial Intelligence

Sonnet 5's Flutter Code Keeps Going Rogue — Here's the Constraint System That Stops It

By 小巫debug日记 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

As coding agents grow more autonomous, the bottleneck shifts from "can it write code" to "can it write only the code you asked for." Without explicit boundaries, Sonnet 5's helpfulness becomes a liability that burns tokens, breaks builds, and forces developers into multi-round rework cycles.

Summary

Claude Sonnet 5's agentic tendencies make it prone to scope drift, paradigm wobble, and self-check hallucinations in Flutter projects. The root cause is not model capability but a mismatch between growing autonomy and weak constraints. A two-layer anchoring system — a short, non-negotiable AGENTS.md plus glob-loaded .cursor/rules files under 500 characters — ensures core red lines survive every round of context.

Constraint-based prompts that specify where to change, what to change, and what NOT to change prevent the model from "incidentally" refactoring a LoginPage when asked to tweak a button. For complex tasks, a 200-token plan phase saves 3–5 rounds of rework. Session hygiene — one task per chat, compacting before the context ring turns yellow — stops early constraints from being diluted by round 10.

Two Flutter-specific hooks plug the highest-frequency drift points: WidgetKeys constants prevent the model from oscillating between find.text and find.byKey, while excluding *.g.dart files from the model's view and forbidding hand-written generated code stops a common source of compilation failures. Across production projects, the same Cursor Pro quota lasted a full month instead of two weeks after adopting the system.

Takeaways
Sonnet 5's agentic behavior causes four failure modes: scope drift, paradigm wobble, context poisoning, and self-check hallucination.
Keep alwaysApply rules under 500 characters total; Sonnet 5's tokenizer inflates text by 30%, and longer rules get truncated or ignored.
Use a two-layer rule system: AGENTS.md for non-negotiable red lines, and glob-matched .cursor/rules/*.mdc files for file-type-specific conventions.
Every prompt needs three parts: where to change (@file+L#), what to change, and what NOT to change — the last part prevents the most drift.
For multi-file tasks, require a change plan first; a 200-token plan saves 3–5 rounds of rework.
Start a new chat when the Cursor context ring turns yellow; constraints dilute after 10–20 rounds in a single session.
WidgetKeys constants prevent the model from switching between find.text and find.byKey across test and UI code.
Exclude *.g.dart, *.freezed.dart, and *.config.dart from the model's view via .cursorignore to stop it from hand-writing generated files.
Interrupt the model immediately if its first reply rephrases your request, touches un-@'d files, or adds unsolicited tests or refactors.
Conclusions

The core tension in AI-assisted coding has inverted: it's no longer about coaxing useful output from weak models, but about restraining over-eager strong models from doing too much.

Sonnet 5's helpfulness is a double-edged sword — its tendency to "incidentally optimize" is a feature for greenfield projects and a bug for maintaining existing codebases with established conventions.

The 500-character alwaysApply limit is a practical discovery, not a theoretical one; it reflects how Sonnet 5's new tokenizer silently inflates token counts and causes models to skip long rule files.

Constraint-based prompting treats the model like a junior engineer who needs explicit boundaries, not a senior one who can infer intent — a mental model shift that many developers haven't made yet.

Flutter's specific pain points (deep widget trees, multiple state management options, generated files) make it a canary for AI coding drift; the same patterns will surface in other frameworks as models grow more agentic.

Concepts & terms
Scope Drift
When an AI model changes files or modules beyond what was requested, often refactoring unrelated code while performing a simple edit.
Paradigm Wobble
Inconsistent use of patterns within a codebase — for example, switching between Riverpod and Bloc, or mixing find.text and find.byKey — caused by the model not having project conventions firmly anchored.
Context Poisoning
The degradation of early-session constraints as a long conversation continues; by round 10–20, earlier instructions like "don't rewrite the entire build" get diluted and ignored.
Self-Check Hallucination
When an AI model claims to have run tests or verified output but did not actually execute anything, often due to misinterpreting terminal noise as success signals.
AGENTS.md
A project-root markdown file that serves as a universal entry point for AI coding tools (Cursor, Claude Code, Codex, Windsurf), containing non-negotiable project conventions.
Constraint-Based Prompt
A prompting style that specifies three elements: the exact location to change (@file + line number), what to change, and what NOT to change — the last element prevents the model from overstepping.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗