Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(codex): reuse loaded skills across conversation turns (#2094)

Call Whiteec74948a8a68
diff --git a/core/prompt_registry.py b/core/prompt_registry.py
index e90793b0f..7f53fbbc3 100644
--- a/core/prompt_registry.py
+++ b/core/prompt_registry.py
@@ -80,6 +80,7 @@ PROMPT_MODULES: tuple[PromptModule, ...] = (
PromptModule("forked-session-prompt", "Forked Session Prompt", "forked-session.md", trailing_newlines=2, placeholders=("source_session_id", "default_session_id")),
PromptModule("base-capabilities-body", "Base Capabilities Body", "base-capabilities.md", trailing_newlines=1),
PromptModule("skills-prompt", "Skills Usage", "skills.md", leading_newlines=2, trailing_newlines=2),
+ PromptModule("codex-skill-reuse", "Codex Skill Reuse", "codex-skill-reuse.md", leading_newlines=1, trailing_newlines=1),
PromptModule("codex-generated-images", "Codex-generated images", "codex-generated-images.md", leading_newlines=1, trailing_newlines=1, placeholders=("example_uri",)),
PromptModule("show-pages-prompt", "Show Pages Prompt", "show-pages.md", leading_newlines=1, trailing_newlines=1),
PromptModule("quick-replies-prompt", "Quick Replies Prompt", "quick-replies.md", leading_newlines=1, trailing_newlines=1),
diff --git a/core/prompt_studio_catalog.py b/core/prompt_studio_catalog.py
index 7ee24f7a1..4f5f59643 100644
--- a/core/prompt_studio_catalog.py
+++ b/core/prompt_studio_catalog.py
@@ -197,8 +197,8 @@ def _render_options(raw: object) -> dict[str, Any]:
options: dict[str, Any] = {}
for name, value in raw.items():
field = f"options.{name}"
- # Agent text has one JSON owner: the existing top-level field.
- if name not in parameters or name == "agent_instructions":
+ # Agent text and backend each have one JSON owner: their top-level fields.
+ if name not in parameters or name in {"agent_instructions", "backend"}:
raise PromptRenderInputError("unknownField", field=field)
if name == "context" and isinstance(value, dict):
unknown = set(value) - set(MessageContext.__dataclass_fields__)
@@ -239,7 +239,7 @@ def render_prompt_context(request: dict[str, Any]) -> dict[str, Any]:
raise PromptRenderInputError("invalidField", field="agent_instructions")
options = _render_options(request.get("options", {}))
options.setdefault("include_codex_generated_images", backend == "codex")
- blocks = build_system_prompt_blocks(agent_instructions=agent_instructions, **options)
+ blocks = build_system_prompt_blocks(agent_instructions=agent_instructions, backend=backend, **options)
if backend == "codex":
blocks = runtime_snapshot_blocks(blocks)
text = join_prompt_blocks(blocks)
diff --git a/core/prompts/codex-skill-reuse.md b/core/prompts/codex-skill-reuse.md
new file mode 100644
index 000000000..6e71f1a1e
--- /dev/null
+++ b/core/prompts/codex-skill-reuse.md
@@ -0,0 +1 @@
+Choose skills by task relevance, but load a skill only if it has not already been read in this conversation; reuse already-loaded instructions across turns, callbacks, and resumed work instead of reloading them for each turn.
diff --git a/core/system_prompt_injection.py b/core/system_prompt_injection.py
index 36af34863..e4d3cf1d0 100644
--- a/core/system_prompt_injection.py
+++ b/core/system_prompt_injection.py
@@ -199,6 +199,7 @@ def _context_block(
def build_system_prompt_blocks(
*,
agent_instructions: str = "",
+ backend: str = "",
include_quick_replies: bool = True,
include_codex_generated_images: bool = False,
include_context_guidance: bool = True,
@@ -239,6 +240,8 @@ def build_system_prompt_blocks(
if correction:
blocks.append(RenderedPromptBlock("forked-session-prompt", correction))
blocks.append(render_prompt_block("base-capabilities-body"))
+ if backend == "codex":
+ blocks.append(render_prompt_block("codex-skill-reuse"))
if include_codex_generated_images:
blocks.append(_codex_generated_images_block())
blocks.append(render_prompt_block("show-pages-prompt"))
diff --git a/modules/agents/codex/agent.py b/modules/agents/codex/agent.py
index 1f601a16b..31b08b048 100644
--- a/modules/agents/codex/agent.py
+++ b/modules/agents/codex/agent.py
@@ -2959,6 +2959,7 @@ class CodexAgent(BaseAgent):
instructions = await asyncio.to_thread(
build_system_prompt_injection,
agent_instructions=agent_instructions or "",
+ backend="codex",
include_quick_replies=getattr(self.controller.config, "reply_enhancements", True)
and platform != "wechat",
include_codex_generated_images=True,
diff --git a/tests/test_codex_prompt_delivery_contract.py b/tests/test_codex_prompt_delivery_contract.py
index 368e88a10..9ec7f1b27 100644
--- a/tests/test_codex_prompt_delivery_contract.py
+++ b/tests/test_codex_prompt_delivery_contract.py
@@ -18,6 +18,7 @@ from unittest.mock import Mock
from aiohttp import web
import pytest
+from core.prompt_registry import prompt_text
from modules.agents.codex.agent import CodexAgent
from modules.agents.codex.transport import CodexTransport
from modules.im import MessageContext
@@ -292,6 +293,8 @@ async def test_production_agent_instructions_are_last_in_native_baseline_and_ove
assert baseline.startswith("# Avibe")
assert baseline.endswith("\n\n" + custom)
assert baseline.count(custom) == 1
+ guidance = prompt_text("codex-skill-reuse")
+ assert baseline.count(guidance) == 1
assert "## Session Title" in baseline
thread_id = await agent._start_or_resume_thread(
harness.native, request, developer_instructions=baseline,
@@ -300,6 +303,7 @@ async def test_production_agent_instructions_are_last_in_native_baseline_and_ove
await harness.finish_turn()
native_text = "\n".join(_developer_texts(harness.requests[-1]))
assert native_text.count(baseline) == 1
+ assert native_text.count(guidance) == 1
assert "<avibe_runtime_instructions>" not in native_text
updated_custom = "Updated Agent:保留 {new}。"
@@ -311,6 +315,7 @@ async def test_production_agent_instructions_are_last_in_native_baseline_and_ove
await harness.finish_turn()
text = "\n".join(_developer_texts(harness.requests[-1]))
snapshot = "<avibe_runtime_instructions>\n\n" + updated + "\n</avibe_runtime_instructions>"
+ assert snapshot.count(guidance) == 1
assert text.count(snapshot) == 1
assert text.count(baseline) == 1 # Delivery is not destructive history replacement.
diff --git a/tests/test_prompt_render_export.py b/tests/test_prompt_render_export.py
index 15e1a34ba..af649780d 100644
--- a/tests/test_prompt_render_export.py
+++ b/tests/test_prompt_render_export.py
@@ -32,6 +32,11 @@ _EVIDENCE_LED_PRINCIPLE = (
"Ground judgments in facts and take responsibility for their reliability and practical value. "
"Turn uncertainty into motivation for exploration and action, rather than an excuse to stop prematurely."
)
+_CODEX_SKILL_REUSE_GUIDANCE = (
+ "Choose skills by task relevance, but load a skill only if it has not already been read "
+ "in this conversation; reuse already-loaded instructions across turns, callbacks, "
+ "and resumed work instead of reloading them for each turn."
+)
def _inputs(backend="codex", memory=True, history="managed", skill_mode="pages"):
@@ -79,6 +84,7 @@ def test_export_reconstructs_production_text_with_source_for_every_block(monkeyp
request = _inputs(backend, memory, history, skill_mode)
result = render_prompt_context(request)
options = dict(request["options"])
+ options["backend"] = backend
options["context"] = MessageContext(**options["context"])
common = build_system_prompt_injection(**options)
production = build_system_prompt_injection(agent_instructions=request["agent_instructions"], **options)
@@ -159,6 +165,7 @@ def test_evidence_led_principle_is_shared_by_production_export_and_studio(monkey
request = _inputs(backend)
rendered = render_prompt_context(request)
options = dict(request["options"])
+ options["backend"] = backend
options["context"] = MessageContext(**options["context"])
production = build_system_prompt_injection(agent_instructions=request["agent_instructions"], **options)
if backend == "codex":
@@ -182,6 +189,18 @@ def test_runtime_snapshot_has_only_tags_and_verbatim_content():
)
[email protected]("backend", ["claude", "codex", "opencode"])
[email protected]("skill_mode", ["empty", "manual", "single", "pages"])
+def test_skill_reuse_clarification_is_codex_only(monkeypatch, backend, skill_mode):
+ _environment(monkeypatch, skill_mode=skill_mode)
+ rendered = render_prompt_context(_inputs(backend, skill_mode=skill_mode))
+ assert rendered["text"].count(_CODEX_SKILL_REUSE_GUIDANCE) == (backend == "codex")
+ if backend == "codex":
+ guidance = next(block for block in rendered["blocks"] if block["id"] == "codex-skill-reuse")
+ assert guidance["source_path"] == "core/prompts/codex-skill-reuse.md"
+ assert guidance["text"].count(_CODEX_SKILL_REUSE_GUIDANCE) == 1
+
+
@pytest.mark.parametrize("backend,memory,history,skill_mode", itertools.product(
("claude", "codex", "opencode"), (False, True), ("off", "managed", "self-managed"),
("empty", "manual", "single", "pages"),
@@ -233,9 +252,13 @@ def test_working_principles_remain_without_session_skills_or_optional_capabiliti
blocks = rendered["blocks"]
if backend == "codex":
blocks = blocks[1:-1]
- assert [block["id"] for block in blocks] == [
- "base-capabilities-intro", "agent-working-principles", "base-capabilities-body", "show-pages-prompt",
+ expected = [
+ "base-capabilities-intro", "agent-working-principles", "base-capabilities-body",
]
+ if backend == "codex":
+ expected.append("codex-skill-reuse")
+ assert [block["id"] for block in blocks] == [*expected, "show-pages-prompt"]
+ assert rendered["text"].count(_CODEX_SKILL_REUSE_GUIDANCE) == (backend == "codex")
assert blocks[1]["text"] == prompt_text("agent-working-principles")
assert rendered == render_prompt_context(request)
@@ -355,7 +378,10 @@ def test_real_cli_discovers_updated_skill_and_exports_its_rendered_row(monkeypat
assert second["revision"] != first["revision"]
[email protected]("payload", [[], None, {"backend": "unknown"}, {"backend": "codex", "options": {"unknown": True}}])
[email protected]("payload", [
+ [], None, {"backend": "unknown"}, {"backend": "codex", "options": {"unknown": True}},
+ {"backend": "claude", "options": {"backend": "codex"}},
+])
def test_debug_cli_rejects_invalid_context_without_partial_json(tmp_path, capsys, payload):
context_file = tmp_path / "context.json"
context_file.write_text(json.dumps(payload), encoding="utf-8")
@@ -474,7 +500,7 @@ def test_cli_localizes_invalid_context_files(monkeypatch, tmp_path, capsys, lang
def test_approved_guidance_changes_preserve_all_other_injection_bytes(monkeypatch):
outputs = []
- changed = {"skills-prompt", "skills-manual-prompt"}
+ changed = {"skills-prompt", "skills-manual-prompt", "codex-skill-reuse"}
for backend, memory, history, skill_mode in itertools.product(
("claude", "codex", "opencode"), (False, True), ("off", "managed", "self-managed"), ("empty", "manual", "pages"),
):
@@ -485,6 +511,7 @@ def test_approved_guidance_changes_preserve_all_other_injection_bytes(monkeypatc
outputs.append(text.replace(_EVIDENCE_LED_PRINCIPLE, _PREVIOUS_VERIFICATION_PRINCIPLE))
digest = hashlib.sha256(json.dumps(outputs, ensure_ascii=False).encode()).hexdigest()
# Captured before editing from 1e9ba96bc61b0e86027d4871553e68e8ed85c1ac,
- # omitting only the two approved Skill-loading guidance blocks and restoring
- # the previous verification principle. Every other byte/order stays pinned.
+ # omitting only the approved Skill-loading guidance blocks (including the
+ # Codex-only reuse sentence) and restoring the previous verification principle.
+ # Every other byte/order stays pinned, including all Claude/OpenCode output.
assert digest == "fd82657d51e3193f54f939a3f159e7c7034a40d0b44388399e34a4aa17b14ea6"