From 0e1b4ef0140af01282048bc8ed6cacd8465f4868 Mon Sep 17 00:00:00 2001 From: raymondginger2018-sudo Date: Fri, 27 Nov 2026 21:28:59 +0800 Subject: [PATCH] feat(loop): add an opt-out prompt cue for stepwise output Tells the model to work stepwise, verify claims with tools before asserting them, and keep the final answer tight. The cue rides the existing transient_context_messages channel, so it lands immediately before the user request and never inside system-level instructions or the compaction summarizer prefix. On by default; DEEPCODE_PROMPT_CUE=0 turns it off. - core/loop/cue.py: prompt_cue_enabled + build_cue_context - core/compat/agent.py: pass the cue as transient context on routed runs - tests/test_prompt_cue.py: 6 cases (env spellings, injected shape) --- core/compat/agent.py | 11 +++++++ core/loop/cue.py | 63 ++++++++++++++++++++++++++++++++++++++++ tests/test_prompt_cue.py | 41 ++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 core/loop/cue.py create mode 100644 tests/test_prompt_cue.py diff --git a/core/compat/agent.py b/core/compat/agent.py index 1243fe9f9..2c7464ac8 100644 --- a/core/compat/agent.py +++ b/core/compat/agent.py @@ -280,8 +280,19 @@ async def generate( else: max_iterations = requested_iterations + # P3-A (GenAI lesson 04): attach the prompt cue as transient turn + # context — applied to routed requests, never to the compaction + # summarizer prefix (keeps the dsh prefix/KV-cache alignment intact). + try: + from core.loop.cue import build_cue_context + + cue_context = build_cue_context() + except Exception: + cue_context = () + spec = AgentRunSpec( initial_messages=messages, + transient_context_messages=cue_context, tools=tools, model=params.model or self.provider.default_model, max_iterations=max_iterations, diff --git a/core/loop/cue.py b/core/loop/cue.py new file mode 100644 index 000000000..a3aa68dd2 --- /dev/null +++ b/core/loop/cue.py @@ -0,0 +1,63 @@ +"""P3-A (GenAI lesson 04): prompt cue guidance for structured / stepwise output. + +Lesson 04 (prompt engineering) stresses that explicit *cues* — short +signposts telling the model what shape the answer should take — cut down on +free-form filler and keep multi-step work on rails. DeepCode already carries +heavy system-prompt scaffolding; this module adds one *thin, model-facing* +cue injected as transient turn context on the routed request. + +Design constraints +----------------- +- The cue must NOT be appended to every provider request. The runner replays + the routed request view as the prefix of its compaction summarizer call + (the dsh rule — prefix/KV cache reuse). Injecting different text on the + compaction path would desynchronize that prefix. We therefore attach the + cue via ``transient_context_messages``, which the runner applies to routed + requests and never to the summarizer prefix. +- The cue is advisory prose, not a hard schema: it must never contradict a + caller-provided JSON schema or structured-output mode. It only biases + *default* behavior (stepwise work, cite-before-claim, no filler). + +Enable: env ``DEEPCODE_PROMPT_CUE`` (default on). Set ``0``/``false``/``off`` +to disable. +""" + +from __future__ import annotations + +import os +from typing import Any + +_CUE_TEXT = ( + "Work stepwise: state each step as you take it, use the available tools " + "to verify claims before asserting them, and keep the final answer " + "tight — no restating the task, no filler. If you cite a retrieved " + "memory or tool result, reference it by its label ([n] / tool name)." +) + + +def prompt_cue_enabled() -> bool: + """Whether the prompt cue is on (env ``DEEPCODE_PROMPT_CUE``; default on).""" + value = os.environ.get("DEEPCODE_PROMPT_CUE", "").strip().lower() + if not value: + return True + return value not in {"0", "false", "off", "no"} + + +def build_cue_context() -> tuple[dict[str, Any], ...]: + """Transient turn-context messages carrying the cue, or empty when off. + + Returns a single ``user``-role message so the runner's + ``_with_transient_context`` places it immediately before the canonical + user request (user-priority turn context), never inside system-level + instructions. + """ + if not prompt_cue_enabled(): + return () + return ({"role": "user", "content": _CUE_TEXT},) + + +__all__ = [ + "_CUE_TEXT", + "build_cue_context", + "prompt_cue_enabled", +] diff --git a/tests/test_prompt_cue.py b/tests/test_prompt_cue.py new file mode 100644 index 000000000..6ec6395cf --- /dev/null +++ b/tests/test_prompt_cue.py @@ -0,0 +1,41 @@ +"""The prompt cue: env switch and the shape of the injected turn context.""" + +from __future__ import annotations + +from core.loop.cue import _CUE_TEXT, build_cue_context, prompt_cue_enabled + +OFF_VALUES = ("0", "false", "off", "no", "OFF", "No") + + +def test_cue_is_on_by_default(monkeypatch): + monkeypatch.delenv("DEEPCODE_PROMPT_CUE", raising=False) + assert prompt_cue_enabled() is True + + +def test_blank_env_value_keeps_the_cue_on(monkeypatch): + monkeypatch.setenv("DEEPCODE_PROMPT_CUE", " ") + assert prompt_cue_enabled() is True + + +def test_each_off_spelling_disables_the_cue(monkeypatch): + for value in OFF_VALUES: + monkeypatch.setenv("DEEPCODE_PROMPT_CUE", value) + assert prompt_cue_enabled() is False, value + + +def test_any_other_value_keeps_the_cue_on(monkeypatch): + for value in ("1", "true", "yes", "on"): + monkeypatch.setenv("DEEPCODE_PROMPT_CUE", value) + assert prompt_cue_enabled() is True, value + + +def test_context_is_one_user_message_carrying_the_cue(monkeypatch): + monkeypatch.delenv("DEEPCODE_PROMPT_CUE", raising=False) + context = build_cue_context() + assert len(context) == 1 + assert context[0] == {"role": "user", "content": _CUE_TEXT} + + +def test_disabled_cue_injects_nothing(monkeypatch): + monkeypatch.setenv("DEEPCODE_PROMPT_CUE", "0") + assert build_cue_context() == ()