Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): accept English confirmation tokens for scheduled research

Haozhe Wucfa9c36f654e
diff --git a/agent/src/channels/runtime.py b/agent/src/channels/runtime.py
index 2cda5195d..27e8dfb12 100644
--- a/agent/src/channels/runtime.py
+++ b/agent/src/channels/runtime.py
@@ -204,15 +204,16 @@ class ChannelRuntime:
job = proposal.get("job") or {}
schedule = job.get("schedule") or {}
delivery = job.get("delivery") or {}
- action = "创建" if proposal.get("operation") == "create" else "取消"
+ action = "create" if proposal.get("operation") == "create" else "cancel"
reply_content = (
f"{reply_content}\n\n"
- f"【定时研究确认 · {action}】\n"
- f"任务:{job.get('title') or job.get('id') or '?'}\n"
- f"节奏:{schedule.get('expression') or '-'} · "
+ f"[Scheduled research confirmation · {action}]\n"
+ f"Task: {job.get('title') or job.get('id') or '?'}\n"
+ f"Schedule: {schedule.get('expression') or '-'} · "
f"{schedule.get('timezone') or 'UTC'}\n"
- f"投递:{delivery.get('target_label') or '仅应用内'}\n"
- "请准确回复「确认」或「取消」。"
+ f"Delivery: {delivery.get('target_label') or 'in-app only'}\n"
+ 'Reply exactly "confirm" (确认) to commit, or "cancel" (取消) '
+ "to discard."
)
await self.bus.publish_outbound(
OutboundMessage(
@@ -270,9 +271,15 @@ class ChannelRuntime:
async def _handle_scheduled_confirmation(
self, msg: InboundMessage, session_id: str
) -> bool:
- """Commit/discard exact IM confirmation replies outside the model."""
- token = msg.content.strip()
- if token not in {"确认", "取消"}:
+ """Commit/discard exact IM confirmation replies outside the model.
+
+ Tokens are exact-match by design so the model can never confirm on the
+ user's behalf; both the English and the Chinese spelling are accepted
+ because the 16 IM adapters serve both audiences.
+ """
+ token = msg.content.strip().casefold()
+ is_commit = token in {"确认", "confirm"}
+ if not is_commit and token not in {"取消", "cancel"}:
return False
try:
from src.scheduled_research.proposals import (
@@ -284,15 +291,20 @@ class ChannelRuntime:
proposal = latest_pending_for_session(session_id)
if proposal is None:
return False
- if token == "确认":
+ if is_commit:
result = commit_proposal(proposal["proposal_id"])
- action = "创建" if proposal.get("operation") == "create" else "取消"
- content = f"✅ 定时研究任务已{action}:{result.get('committed_job_id') or '?'}"
+ action = (
+ "created" if proposal.get("operation") == "create" else "cancelled"
+ )
+ content = (
+ f"✅ Scheduled research job {action}: "
+ f"{result.get('committed_job_id') or '?'}"
+ )
else:
discard_proposal(proposal["proposal_id"])
- content = "已放弃这次定时研究变更。"
+ content = "Discarded this scheduled research change."
except Exception as exc: # noqa: BLE001 - keep proposal pending for retry
- content = f"定时研究确认失败:{exc}"
+ content = f"Scheduled research confirmation failed: {exc}"
await self.bus.publish_outbound(
OutboundMessage(
channel=msg.channel,
diff --git a/agent/tests/test_scheduled_research_confirmation.py b/agent/tests/test_scheduled_research_confirmation.py
index 87f2785ec..83c2dfce3 100644
--- a/agent/tests/test_scheduled_research_confirmation.py
+++ b/agent/tests/test_scheduled_research_confirmation.py
@@ -174,3 +174,64 @@ def test_generic_adapter_acknowledgement_is_persisted_as_accepted(
delivery = store.get("accepted").delivery
assert delivery.status is DeliveryStatus.ACCEPTED
assert delivery.provider_message_id is None
+
+
+def test_im_confirmation_accepts_english_and_chinese_tokens(
+ tmp_path: Path,
+ monkeypatch,
+) -> None:
+ """"confirm"/"确认" commit and "cancel"/"取消" discard; free text passes through.
+
+ The IM runtime's exact-match tokens must cover both spellings: the 16
+ adapters serve both audiences, and a token set that only matches the
+ Chinese words locks English-speaking users out of confirming.
+ """
+ from src.channels.bus.events import InboundMessage
+ from src.channels.bus.queue import MessageBus
+ from src.channels.runtime import ChannelRuntime
+ from src.scheduled_research import proposals
+
+ monkeypatch.setenv("VIBE_TRADING_HOME", str(tmp_path))
+ store = ScheduledResearchJobStore(tmp_path / "jobs.json")
+ monkeypatch.setattr(proposals, "default_store", lambda: store)
+ monkeypatch.setattr(
+ proposals,
+ "scheduler_status",
+ lambda: {"enabled": True, "running": True, "executable": True},
+ )
+ monkeypatch.setattr("src.scheduled_research.service.time.time", lambda: 1_000)
+ monkeypatch.setattr("src.scheduled_research.proposals.time.time", lambda: 1_000)
+
+ runtime = ChannelRuntime(
+ bus=MessageBus(),
+ session_service=None,
+ manager=None,
+ session_map_path=tmp_path / "channel_sessions.json",
+ )
+
+ def _msg(content: str) -> InboundMessage:
+ return InboundMessage(
+ channel="websocket", sender_id="u", chat_id="c", content=content
+ )
+
+ async def scenario() -> None:
+ # Free text is never treated as a confirmation.
+ assert await runtime._handle_scheduled_confirmation(_msg("sounds good"), "s-im") is False
+
+ proposals.propose_create(_draft(), session_id="s-im")
+ assert await runtime._handle_scheduled_confirmation(_msg("Confirm"), "s-im") is True
+ assert len(store.load()) == 1
+
+ proposals.propose_create(_draft(), session_id="s-im")
+ assert await runtime._handle_scheduled_confirmation(_msg("cancel"), "s-im") is True
+ assert len(store.load()) == 1 # discarded, not committed
+
+ proposals.propose_create(_draft(), session_id="s-im")
+ assert await runtime._handle_scheduled_confirmation(_msg("确认"), "s-im") is True
+ assert len(store.load()) == 2
+
+ proposals.propose_create(_draft(), session_id="s-im")
+ assert await runtime._handle_scheduled_confirmation(_msg("取消"), "s-im") is True
+ assert len(store.load()) == 2
+
+ asyncio.run(scenario())