diff --git a/backend/routes/live.py b/backend/routes/live.py
index 23a1c7c..bd9b0c5 100644
--- a/backend/routes/live.py
+++ b/backend/routes/live.py
@@ -562,7 +562,27 @@ async def review_queue(current_user: User = Depends(require_trainer)):
scope = _scope(current_user)
query = {"direction": "outbound", "status": "draft_pending"}
query.update(scope)
- return await db.live_messages.find(query, {"_id": 0}).sort("at", 1).to_list(500)
+ drafts = await db.live_messages.find(query, {"_id": 0}).sort("at", 1).to_list(500)
+ # Give the reviewer the message being answered and which campaign it is.
+ names: dict[str, str] = {}
+ for d in drafts:
+ cid = d["campaign_id"]
+ if cid not in names:
+ c = await db.live_campaigns.find_one({"id": cid}, {"_id": 0, "name": 1})
+ names[cid] = (c or {}).get("name", "")
+ d["campaign_name"] = names[cid]
+ last_in = await db.live_messages.find_one(
+ {
+ "campaign_id": cid,
+ "email": d["email"],
+ "direction": "inbound",
+ "at": {"$lte": d["at"]},
+ },
+ {"_id": 0, "body_redacted": 1},
+ sort=[("at", -1)],
+ )
+ d["in_reply_to"] = (last_in or {}).get("body_redacted", "")
+ return drafts
class ReviewDecision(BaseModel):
@@ -587,6 +607,8 @@ async def approve_draft(
if not campaign or campaign.get("killed"):
raise HTTPException(status_code=409, detail="Campaign is not active")
body = data.body if data.body is not None else msg["body_redacted"]
+ if not (body or "").strip():
+ raise HTTPException(status_code=400, detail="Write a reply before sending")
sent = await conversation.send_approved_reply(campaign, msg, body, current_user.id)
await audit.record(
current_user.id, "live.review.approve", message_id, {"edited": bool(data.body)}
diff --git a/backend/services/live/conversation.py b/backend/services/live/conversation.py
index 1697996..58054ff 100644
--- a/backend/services/live/conversation.py
+++ b/backend/services/live/conversation.py
@@ -13,9 +13,17 @@
The persona prompt is built here on the server from campaign fields only; the
browser never supplies prompt text (G7). Secrets never reach the model: the
inbound text is scrubbed first.
+
+The prompt includes the original lure so the model knows the thread, and it answers in
+the language the recipient wrote in. A hosted model may still decline to draft;
+that is retried once and otherwise queued as an empty draft marked
+`persona_declined`, so the reviewer writes the reply instead of approving a
+refusal or an empty email.
"""
+import html
import logging
+import re
import uuid
from datetime import UTC, datetime
@@ -46,18 +54,92 @@
}
-def build_persona_prompt(campaign: dict, turn: int) -> str:
- """Locked system prompt. Built only from approved campaign fields."""
+_TAG_RE = re.compile(r"<[^>]+>")
+_TEMPLATE_URL_RE = re.compile(r"\{\{\s*\.URL\s*\}\}")
+_TEMPLATE_RE = re.compile(r"\{\{[^}]*\}\}")
+
+_ID_WORDS = {
+ "apa", "ini", "itu", "saya", "aku", "anda", "kamu", "tidak", "nggak", "gak", "yang",
+ "dan", "untuk", "dengan", "ada", "bisa", "sudah", "belum", "mohon", "kenapa", "mengapa",
+ "siapa", "bapak", "pak", "ibu", "bu", "terima", "kasih", "tolong", "kok", "gimana",
+ "bagaimana", "apakah", "maaf", "kode", "dari", "ke", "di", "akan", "harus", "boleh",
+} # fmt: skip
+_EN_WORDS = {
+ "what", "is", "this", "the", "i", "you", "please", "why", "who", "can", "not", "and",
+ "to", "of", "for", "with", "my", "your", "it", "do", "does", "are", "sorry", "thanks",
+} # fmt: skip
+
+# A model declining to write the persona turn, in English or Indonesian.
+_REFUSAL_VERBS = (
+ r"(?:help|assist|generate|create|write|draft|produce|provide|comply|fulfil+|"
+ r"continue|participate|engage|do that|role-?play)"
+)
+_REFUSAL_RE = re.compile(
+ r"^\W*(?:(?:i'm\s+)?sorry,?\s+(?:but\s+)?i\b|as an ai\b"
+ r"|maaf,?\s+(?:saya|aku)\s+tidak\s+(?:bisa|dapat)\b"
+ r"|(?:saya|aku)\s+tidak\s+(?:bisa|dapat)\s+(?:membantu|membuat|menulis)\b)"
+ r"|\bi\s*(?:cannot|can't|can not|won't|will not|am unable to|'m unable to|am not able to)"
+ rf"\s+(?:\w+\s+)?{_REFUSAL_VERBS}\b",
+ re.IGNORECASE,
+)
+
+
+def lure_text(campaign: dict) -> str:
+ """The original email as plain text, the first turn of the thread."""
+ raw = campaign.get("body_html") or campaign.get("body") or ""
+ raw = _TEMPLATE_URL_RE.sub("[link]", raw)
+ raw = _TEMPLATE_RE.sub("", raw)
+ raw = re.sub(r"(?i)
|
Hi {{.FirstName}},
Verify here: {{.URL}}
& thanks"} + text = conversation.lure_text(camp) + assert "Verify here: [link]" in text and "" not in text and "{{" not in text + assert "& thanks" in text + + +async def test_prompt_carries_lure_and_follows_recipient_language(api): + camp = await _running_campaign() + camp["body_html"] = "
Your mailbox will be locked. Verify: {{.URL}}
" + gw = ScriptedGateway(["Halo, ini tindak lanjut dari IT Helpdesk soal verifikasi tadi."]) + r = await conversation.process_inbound(camp, "bob@acme.test", "Apa ini ?", gw) + assert r["status"] == "draft_pending" + prompt = gw.calls[0]["system_prompt"] + assert "Your mailbox will be locked. Verify: [link]" in prompt + assert "Write in Indonesian" in prompt # campaign is "en", recipient wrote Indonesian + assert gw.calls[0]["history"] == [{"role": "user", "content": "Apa ini ?"}] + + +async def test_refusal_is_retried_once(api): + camp = await _running_campaign() + gw = ScriptedGateway(["I cannot help with phishing.", "A quick follow-up on the reset."]) + r = await conversation.process_inbound(camp, "bob@acme.test", "who is this?", gw) + assert r["status"] == "draft_pending" and r["draft"] == "A quick follow-up on the reset." + assert len(gw.calls) == 2 + + +async def test_persistent_refusal_queues_empty_draft_for_human(api): + camp = await _running_campaign() + gw = ScriptedGateway(["I cannot help with phishing.", ""]) + r = await conversation.process_inbound(camp, "bob@acme.test", "who is this?", gw) + assert r["status"] == "persona_declined" + draft = await db.live_messages.find_one({"campaign_id": camp["id"], "direction": "outbound"}) + assert draft["status"] == "draft_pending" and draft["persona_declined"] is True + assert draft["body_redacted"] == "" # the refusal text is never queued + + +async def test_unsent_drafts_are_not_in_history(api): + camp = await _running_campaign() + await conversation.process_inbound(camp, "bob@acme.test", "who is this?", FakeGateway()) + gw = ScriptedGateway(["Following up again."]) + await conversation.process_inbound(camp, "bob@acme.test", "hello?", gw) + roles = [m["role"] for m in gw.calls[0]["history"]] + assert roles == ["user", "user"] # the first draft was never approved, so never seen + + +async def test_approve_rejects_empty_body(api, _no_real_mail): + _, h = await make_user("trainer", organization_id="o1") + _, draft = await _seed_draft(org="o1") + await db.live_messages.update_one({"id": draft["id"]}, {"$set": {"body_redacted": ""}}) + r = await api.post(f"/api/live/review/{draft['id']}/approve", json={}, headers=h) + assert r.status_code == 400 + r = await api.post(f"/api/live/review/{draft['id']}/approve", json={"body": " "}, headers=h) + assert r.status_code == 400 + assert not _no_real_mail + + +async def test_review_queue_includes_context(api): + _, h = await make_user("trainer", organization_id="o1") + camp, draft = await _seed_draft(org="o1") + await db.live_messages.insert_one( + { + "id": "in-1", + "campaign_id": camp["id"], + "organization_id": "o1", + "email": "bob@acme.test", + "direction": "inbound", + "body_redacted": "who is this?", + "status": "received", + "at": datetime(2020, 1, 1, tzinfo=UTC), + } + ) + row = (await api.get("/api/live/review", headers=h)).json()[0] + assert row["in_reply_to"] == "who is this?" and row["campaign_name"] == "drill" diff --git a/docs/live-campaign-walkthrough.md b/docs/live-campaign-walkthrough.md index 97d2605..6174c75 100644 --- a/docs/live-campaign-walkthrough.md +++ b/docs/live-campaign-walkthrough.md @@ -309,6 +309,7 @@ To run against a real organization instead of the sandbox: | My reply never appears at all | IMAP isn't configured (Settings → Live), or the reply went to the sender address instead of the `Reply-To`. Check the campaign **Conversation** panel and run **Poll now** (Settings/API `POST /live/poll`). | | I replied to the lure but **Reported** stays 0 | Replying is engaging, not reporting. Forward the email to the report mailbox (`lapor@acme.test`); it is credited within ~30 s, or click **Periksa sekarang** on Laporan Phishing. | | My reply lands in the wrong place | The IMAP user must be the reply mailbox (`sim-reply@acme.test`), not the campaign sender. | +| Draft in Reply Review is empty and marked **AI did not draft** | The LLM declined to write that turn (hosted models sometimes refuse, most often for the OTP objective). Pretexta retries once, then queues an empty draft instead of a refusal: write the reply yourself or discard it. A local Ollama model avoids provider-side refusals. | | Reply shows in Conversation but not in Reply Review | No LLM provider configured — the persona can't draft a reply. Configure one in Settings → LLM (local Ollama works). | | Reply read in Thunderbird, poller skipped it | Fixed: the poller tracks UIDs, not the read/\Seen flag, so this no longer happens. | | Phishing link won't open (`http://gophish?rid=…`) | `phish_url` is set to an internal Docker name. Set it to a browser-reachable address (`http://localhost:8080` locally, or your public phishing URL), then **launch a new campaign** — the URL is baked in at launch. | diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json index 4d0fb89..ffd95ca 100644 --- a/frontend/src/i18n/locales/en.json +++ b/frontend/src/i18n/locales/en.json @@ -751,5 +751,23 @@ "live_complied": "In a live phishing simulation you agreed to a request that used {{principle}}. This exercise helps you recognise it next time.", "live_disclosed": "In a live phishing simulation you shared a secret in reply to a message that used {{principle}}. This exercise helps you recognise it next time.", "retest": "Scheduled retest for {{principle}} — a different exercise to confirm the lesson stuck." + }, + "review": { + "title": "Reply Review Queue", + "subtitle": "Approve each AI persona reply before it is sent (review mode)", + "empty_title": "Queue is empty", + "empty_desc": "AI replies awaiting approval will appear here.", + "awaiting": "awaiting review", + "declined": "AI did not draft", + "they_wrote": "They wrote", + "declined_note": "The AI model declined to draft this turn. Write the reply yourself, or discard it.", + "placeholder": "Write the persona's reply…", + "help": "Every AI reply is held here until a human approves it. Edit before sending if needed.", + "approve": "Approve & send", + "discard": "Discard", + "sent": "Reply sent", + "discarded": "Discarded", + "approve_failed": "Approve failed", + "discard_failed": "Discard failed" } } diff --git a/frontend/src/i18n/locales/id.json b/frontend/src/i18n/locales/id.json index 2f43a51..188e884 100644 --- a/frontend/src/i18n/locales/id.json +++ b/frontend/src/i18n/locales/id.json @@ -751,5 +751,23 @@ "live_complied": "Dalam simulasi phishing, Anda menuruti permintaan yang memakai prinsip {{principle}}. Latihan ini membantu Anda mengenalinya lain kali.", "live_disclosed": "Dalam simulasi phishing, Anda membagikan rahasia sebagai balasan pesan yang memakai prinsip {{principle}}. Latihan ini membantu Anda mengenalinya lain kali.", "retest": "Tes ulang terjadwal untuk prinsip {{principle}} — latihan berbeda untuk memastikan pelajarannya melekat." + }, + "review": { + "title": "Antrean Review Balasan", + "subtitle": "Setujui setiap balasan persona AI sebelum dikirim (mode review)", + "empty_title": "Antrean kosong", + "empty_desc": "Balasan AI yang menunggu persetujuan akan muncul di sini.", + "awaiting": "menunggu review", + "declined": "AI tidak membuat draf", + "they_wrote": "Pesan penerima", + "declined_note": "Model AI menolak membuat balasan untuk giliran ini. Tulis balasannya sendiri, atau buang.", + "placeholder": "Tulis balasan persona…", + "help": "Setiap balasan AI ditahan di sini sampai disetujui manusia. Sunting dulu bila perlu.", + "approve": "Setujui & kirim", + "discard": "Buang", + "sent": "Balasan terkirim", + "discarded": "Dibuang", + "approve_failed": "Gagal menyetujui", + "discard_failed": "Gagal membuang" } } diff --git a/frontend/src/pages/LiveCampaignsPage.js b/frontend/src/pages/LiveCampaignsPage.js index f8333fb..de77d32 100644 --- a/frontend/src/pages/LiveCampaignsPage.js +++ b/frontend/src/pages/LiveCampaignsPage.js @@ -656,7 +656,9 @@ function Detail({ id, onChanged, onDeleted }) { {msg.direction === 'inbound' ? t('live.msg.replied', { email: msg.email }) : t('live.msg.persona')}