{"record":{"id":"445d23b4c03639dd","repo":"shareAI-lab/learn-claude-code","slug":"goal-condition-cannot-exceed-max-goal-length-cha","errorCode":null,"errorMessage":"goal condition cannot exceed {MAX_GOAL_LENGTH} characters","messagePattern":"goal condition cannot exceed (.+?) characters","errorType":"validation","errorClass":"GoalError","httpStatus":null,"severity":"error","filePath":"s17_goal_loop/code.py","lineNumber":287,"sourceCode":"    ):\n        if block_cap < 1:\n            raise GoalError(\"block_cap must be at least 1\")\n        self.evaluator = evaluator\n        self.block_cap = block_cap\n        self.events = events if events is not None else []\n        self.active: GoalState | None = None\n        self.last_status: dict[str, Any] | None = None\n        self.consecutive_blocks = 0\n\n    def begin_query(self) -> None:\n        self.consecutive_blocks = 0\n\n    def set_goal(self, condition: str, tokens_at_start: int = 0) -> GoalState:\n        condition = condition.strip()\n        if not condition:\n            raise GoalError(\"goal condition cannot be empty\")\n        if len(condition) > MAX_GOAL_LENGTH:\n            raise GoalError(\n                f\"goal condition cannot exceed {MAX_GOAL_LENGTH} characters\"\n            )\n        if self.active is not None:\n            self._record(\n                active=False,\n                met=False,\n                failed=False,\n                reason=\"replaced by a new goal\",\n            )\n        self.active = GoalState(\n            condition=condition,\n            iterations=0,\n            set_at=time.time(),\n            tokens_at_start=tokens_at_start,\n        )\n        self.consecutive_blocks = 0\n        self._record(active=True, met=False, failed=False, reason=\"goal set\")\n        return self.active","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s17_goal_loop/code.py#L269-L305","documentation":"set_goal rejects conditions longer than MAX_GOAL_LENGTH = 4000 characters (s17_goal_loop/code.py:287). The condition is embedded in the evaluator's prompt each turn, so an oversized condition bloats every evaluation call; the hard cap keeps the evaluator prompt bounded and its verdicts reliable.","triggerScenarios":"Calling set_goal with a condition over 4000 chars after stripping — e.g. pasting an entire spec document, a full file's contents, or a long transcript excerpt as the 'goal'.","commonSituations":"Using the goal field to smuggle context/instructions instead of a concise success predicate; programmatically generating the goal from a template that grows unbounded; concatenating many acceptance criteria into one string.","solutions":["Distill the condition to a single verifiable predicate (one or two sentences)","Split a large spec into the transcript (user message) and keep only the success criterion as the goal","If a longer cap is genuinely needed, raise MAX_GOAL_LENGTH deliberately — but prefer trimming the condition"],"exampleFix":"# before\nsession.goal.set_goal(open(\"SPEC.md\").read())\n\n# after\nsession.goal.set_goal(\"SPEC.md: every requirement in the spec is implemented and tests/test_spec.py passes\")","handlingStrategy":"validation","validationCode":"MAX_GOAL_LENGTH = 4000\ncondition = goal_text.strip()\nif len(condition) > MAX_GOAL_LENGTH:\n    raise SystemExit(f\"goal must be at most {MAX_GOAL_LENGTH} chars (got {len(condition)})\")\ncontroller.set_goal(condition)","typeGuard":"def is_within_goal_limit(condition: str) -> bool:\n    return 0 < len(condition.strip()) <= 4000","tryCatchPattern":"try:\n    controller.set_goal(condition)\nexcept GoalError as error:\n    if \"cannot exceed\" in str(error):\n        condition = summarize(condition)  # distill to the success predicate\n        controller.set_goal(condition)\n    else:\n        raise","preventionTips":["Keep the goal a one-sentence verifiable predicate, not a spec dump","Put large context in the query/transcript; keep only the success criterion as the goal","Check len(goal) before calling set_goal when goals are generated programmatically"],"tags":["goal-loop","validation","limits","prompt-size"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}