WS1: Extend seed-demo with agency cluster, deadlines, red flags, artifacts, telemetry, suggestions, notifications

This commit is contained in:
hermes 2026-07-30 21:10:18 +00:00
parent 641f70b68c
commit 71a16d35ed
2 changed files with 314 additions and 33 deletions

View file

@ -1035,27 +1035,81 @@ def interview_prep(app_id: str) -> Any:
# --- v1: Concierge / Demo Seed --- # --- v1: Concierge / Demo Seed ---
def _seed_demo_counts() -> dict[str, Any]:
"""Compute counts for the seed-demo response from current DB state."""
postings = repo_app.list_postings()
apps = repo_app.list_applications()
sections = repo_profile.list_sections()
# Clusters: postings with a non-null cluster_id
cluster_postings = [p for p in postings if p.get("cluster_id")]
cluster_ids = set(p["cluster_id"] for p in cluster_postings if p["cluster_id"])
# Deadlines: postings with apply_by in the next 7 days
from datetime import timedelta
today = datetime.now(timezone.utc).date()
deadline_window = today + timedelta(days=7)
deadline_count = sum(
1 for p in postings
if p.get("apply_by") and today <= p["apply_by"] <= deadline_window
)
# Pending email suggestions
from app.imap_watch import list_pending_suggestions
suggestions = list_pending_suggestions()
# Notification log rows
from app.notify import list_notification_log
notifications = list_notification_log(limit=50)
# Task runs
task_runs = repo_app.list_task_runs()
# CV artifacts (kind=cv, origin=ai_drafted)
cv_count = 0
for a in apps:
artifacts = repo_app.list_artifacts(a["id"])
cv_count += sum(1 for art in artifacts if art["kind"] == "cv" and art["origin"] == "ai_drafted")
return {
"profile": "Demo Demosson",
"postings": len(postings),
"applications": len(apps),
"sections": len(sections),
"clusters": len(cluster_ids),
"deadlines": deadline_count,
"suggestions": len(suggestions),
"notifications": len(notifications),
"task_runs": len(task_runs),
"cv_artifacts": cv_count,
}
@app.post("/api/concierge/seed-demo", response_model=SeedDemoResponse) @app.post("/api/concierge/seed-demo", response_model=SeedDemoResponse)
def seed_demo() -> Any: def seed_demo() -> Any:
"""Idempotent demo seed: profile + 6 postings + varied application states.""" """Idempotent demo seed: profile + postings + varied application states + artifacts + telemetry.
Populates a complete demo-worthy dataset:
- 6 standalone postings with varied application states
- 3-posting agency cluster (same role reposted by 3 fictional agencies)
- 2 postings with apply_by deadlines in the next 4 days
- 1 posting with red_flags in its application score_rationale
- Applications: 1 sent (backdated 8 days for nudge), 1 interviewing, 1 approved with cover letter
- 2 pending email_suggestion rows
- 3 notification_log rows (daily_digest delivered, email_suggestion delivered, webhook failed)
- 6 task_run telemetry rows across providers/models
- 1 cv-tailor artifact (kind cv, origin ai_drafted) on the interviewing application
"""
from datetime import date, timedelta
# Check if demo profile already exists # Check if demo profile already exists
existing = fetch_one( existing = fetch_one(
"SELECT * FROM profile WHERE full_name = 'Demo Demosson'" "SELECT * FROM profile WHERE full_name = 'Demo Demosson'"
) )
if existing: if existing:
# Already seeded -- return current counts return _seed_demo_counts()
profile = repo_profile._normalize_profile(existing)
postings = repo_app.list_postings()
apps = repo_app.list_applications()
sections = repo_profile.list_sections()
return {
"profile": profile["full_name"],
"postings": len(postings),
"applications": len(apps),
"sections": len(sections),
}
# Create demo profile with Swedish characters # -- Create demo profile with Swedish characters --
repo_profile.get_or_create_profile() repo_profile.get_or_create_profile()
profile = repo_profile.update_profile({ profile = repo_profile.update_profile({
"full_name": "Demo Demosson", "full_name": "Demo Demosson",
@ -1078,7 +1132,12 @@ def seed_demo() -> Any:
for s in demo_sections: for s in demo_sections:
repo_profile.create_section(profile["id"], s) repo_profile.create_section(profile["id"], s)
# Create 6 demo postings with varied states # -- Track application IDs for later linking --
sent_app_id: str | None = None
interviewing_app_id: str | None = None
approved_app_id: str | None = None
# -- Create 6 standalone demo postings with varied states --
demo_postings = [ demo_postings = [
{"company": "Skane Tech AB", "title": "Senior Python Developer", "location": "Malmo", "url": "https://example.com/af/1", "state": "scored", "score": 85}, {"company": "Skane Tech AB", "title": "Senior Python Developer", "location": "Malmo", "url": "https://example.com/af/1", "state": "scored", "score": 85},
{"company": "Lund Systems", "title": "Fullstack Engineer", "location": "Lund", "url": "https://example.com/af/2", "state": "discovered", "score": None}, {"company": "Lund Systems", "title": "Fullstack Engineer", "location": "Lund", "url": "https://example.com/af/2", "state": "discovered", "score": None},
@ -1100,48 +1159,264 @@ def seed_demo() -> Any:
) )
app_row = repo_app.create_application(posting["id"]) app_row = repo_app.create_application(posting["id"])
if dp["state"] == "sent":
sent_app_id = app_row["id"]
if dp["state"] == "approved":
approved_app_id = app_row["id"]
# Set state and score # Set state and score
if dp["score"] is not None: if dp["score"] is not None:
repo_app.update_application_score(app_row["id"], dp["score"], {"factors": {}}) repo_app.update_application_score(app_row["id"], dp["score"], {"factors": {}})
if dp["state"] != "discovered" and dp["state"] != "scored": if dp["state"] != "discovered" and dp["state"] != "scored":
# Transition through states
if dp["state"] in ("approved", "rejected"): if dp["state"] in ("approved", "rejected"):
# First set to scored if needed
if dp["score"] is not None: if dp["score"] is not None:
repo_app.update_application_state(app_row["id"], "scored") repo_app.update_application_state(app_row["id"], "scored")
repo_app.update_application_state(app_row["id"], dp["state"]) repo_app.update_application_state(app_row["id"], dp["state"])
elif dp["state"] == "sent": elif dp["state"] == "sent":
# approved -> drafting -> sent
if dp["score"] is not None: if dp["score"] is not None:
repo_app.update_application_state(app_row["id"], "scored") repo_app.update_application_state(app_row["id"], "scored")
repo_app.update_application_state(app_row["id"], "approved") repo_app.update_application_state(app_row["id"], "approved")
repo_app.update_application_state(app_row["id"], "drafting") repo_app.update_application_state(app_row["id"], "drafting")
# We need confirmed approval for drafting->sent, so directly set state
execute( execute(
"UPDATE application SET state = 'sent', state_changed_at = now(), last_activity_at = now() WHERE id = %s", "UPDATE application SET state = 'sent', state_changed_at = now(), last_activity_at = now() WHERE id = %s",
(app_row["id"],), (app_row["id"],),
) )
# Backdate the 'sent' application ( posting 4) to 8 days ago for nudge demo # -- 3-posting AGENCY CLUSTER (same real role, 3 fictional agencies, near-identical title+description) --
from datetime import timedelta cluster_title = "Senior Backend Developer"
backdated = datetime.now(timezone.utc) - timedelta(days=8) cluster_desc = (
"We are looking for a senior backend developer with strong Python skills. "
"You will build and maintain scalable REST APIs, work with PostgreSQL, "
"and collaborate in an agile team. Experience with Docker and cloud "
"deployment is a plus."
)
agency_companies = ["Aderanto AB", "Wise IT", "TechTalent Nord"]
agency_posting_ids: list[str] = []
for i, agency in enumerate(agency_companies):
posting = repo_app.create_job_posting(
source="arbetsformedlingen",
url=f"https://example.com/af/agency/{i+1}",
company=agency,
title=cluster_title,
location="Stockholm",
description=cluster_desc,
raw={},
)
agency_posting_ids.append(posting["id"])
# Create applications for these too (scored for digest demo)
app_row = repo_app.create_application(posting["id"])
repo_app.update_application_score(app_row["id"], 75 + i, {"factors": {}})
# Assign cluster IDs to all postings (the matching package will cluster the 3 agency postings together)
for pid in agency_posting_ids:
_assign_cluster_id(pid)
# -- 2 postings with apply_by in the next 4 days (deadlines strip) --
today = datetime.now(timezone.utc).date()
for j in range(2):
deadline = today + timedelta(days=2 + j)
posting = repo_app.create_job_posting(
source="arbetsformedlingen",
url=f"https://example.com/af/deadline/{j+1}",
company=f"Deadline Corp {j+1}",
title=f"Urgent Developer Role {j+1}",
location="Goteborg",
description="Urgent hire for a developer with deadline approaching.",
raw={},
)
repo_app.update_posting_apply_by(posting["id"], deadline)
app_row = repo_app.create_application(posting["id"])
repo_app.update_application_score(app_row["id"], 60 + j * 5, {"factors": {}})
# -- 1 posting with red_flags on its application score_rationale --
red_flag_posting = repo_app.create_job_posting(
source="arbetsformedlingen",
url="https://example.com/af/redflag/1",
company="ShadyCorp AB",
title="Junior Developer",
location="Remote",
description="Entry level developer position with trial period.",
raw={},
)
red_flag_app = repo_app.create_application(red_flag_posting["id"])
red_flag_rationale = {
"factors": {"salary": "below market rate", "trial_period": "3 months unpaid"},
"red_flags": ["requests unpaid trial work", "salary significantly below market rate"],
"summary": "Multiple red flags detected during scoring.",
}
repo_app.update_application_score(red_flag_app["id"], 25, red_flag_rationale)
# -- Application in 'interviewing' state --
interviewing_posting = repo_app.create_job_posting(
source="arbetsformedlingen",
url="https://example.com/af/interview/1",
company="Festina Digital AB",
title="Full Stack Developer",
location="Malmo",
description="Full stack developer with React, Python, and cloud experience.",
raw={},
)
interviewing_app = repo_app.create_application(interviewing_posting["id"])
repo_app.update_application_score(interviewing_app["id"], 90, {"factors": {}})
repo_app.update_application_state(interviewing_app["id"], "scored")
repo_app.update_application_state(interviewing_app["id"], "approved")
repo_app.update_application_state(interviewing_app["id"], "drafting")
execute( execute(
"UPDATE application SET last_activity_at = %s WHERE state = 'sent'", "UPDATE application SET state = 'sent', state_changed_at = now(), last_activity_at = now() WHERE id = %s",
(backdated,), (interviewing_app["id"],),
)
repo_app.update_application_state(interviewing_app["id"], "interviewing")
interviewing_app_id = interviewing_app["id"]
# -- Cover letter artifact on the 'approved' application (Swedish text, origin user_drafted) --
storage_dir = os.path.join(tempfile.gettempdir(), "jobhunt_artifacts")
os.makedirs(storage_dir, exist_ok=True)
if approved_app_id:
cover_text = (
"Basta rekryterare,\n\n"
"Jag ansoker om tjansen som Backend Developer hos er. "
"Med min erfarenhet av Python, FastAPI och PostgreSQL "
"tror jag att jag skulle vara en bra tillgang for ert team.\n\n"
"Jag ser fram emot att diskutera rollen vidare.\n\n"
"Vanliga halsningar,\n"
"Demo Demosson"
)
cover_bytes = cover_text.encode("utf-8")
cl_filename = f"cover_letter_{approved_app_id[:8]}.txt"
cl_storage_path = os.path.join(storage_dir, cl_filename)
with open(cl_storage_path, "wb") as f:
f.write(cover_bytes)
repo_app.create_artifact(
application_id=approved_app_id,
kind="cover_letter",
filename=cl_filename,
content_bytes=cover_bytes,
storage_path=cl_storage_path,
origin="user_drafted",
)
# -- CV tailor artifact on the interviewing application (kind cv, origin ai_drafted, real PDF bytes + hash) --
if interviewing_app_id and HAS_ARTIFACTS:
_render_fn = _render_cv_pdf # type: ignore[possibly-unbound]
cv_profile = {
"full_name": profile.get("full_name", ""),
"headline": profile.get("headline", ""),
"email": profile.get("email", ""),
"phone": profile.get("phone", ""),
"location": profile.get("location", ""),
"summary": "Tailored CV for full stack developer role at Festina Digital.",
}
cv_sections = [
{
"kind": "experience",
"title": "Backend Developer",
"org": "TechSkane AB",
"bullets": ["Built REST APIs", "Improved performance by 30%"],
},
{
"kind": "skills",
"title": "Technical Skills",
"bullets": ["Python", "PostgreSQL", "Docker", "FastAPI", "React"],
},
]
pdf_bytes = _render_fn(cv_profile, cv_sections)
cv_filename = f"cv_tailored_{interviewing_app_id[:8]}.pdf"
cv_storage_path = os.path.join(storage_dir, cv_filename)
with open(cv_storage_path, "wb") as f:
f.write(pdf_bytes)
repo_app.create_artifact(
application_id=interviewing_app_id,
kind="cv",
filename=cv_filename,
content_bytes=pdf_bytes,
storage_path=cv_storage_path,
origin="ai_drafted",
)
# -- Backdate the 'sent' application to 8 days ago for nudge demo --
backdated = datetime.now(timezone.utc) - timedelta(days=8)
if sent_app_id:
execute(
"UPDATE application SET last_activity_at = %s WHERE id = %s",
(backdated, sent_app_id),
)
# -- 2 pending email_suggestion rows --
from app.imap_watch import create_email_suggestion
suggestion_time = datetime.now(timezone.utc) - timedelta(hours=3)
if sent_app_id:
create_email_suggestion(
application_id=sent_app_id,
mailbox_from="recruiter@festina-demo.se",
subject="Inbjudan till intervju",
snippet="Hej, vi skulle vilja boka in en intervju med dig...",
classification="interview_invite",
state_proposal="interviewing",
received_at=suggestion_time,
)
if interviewing_app_id:
create_email_suggestion(
application_id=interviewing_app_id,
mailbox_from="hr@festina-demo.se",
subject="Fraga om din erfarenhet",
snippet="Vi har nagra fragor om din bakgrund inom Python...",
classification="question",
state_proposal=None,
received_at=datetime.now(timezone.utc) - timedelta(hours=1),
)
# -- 3 notification_log rows --
# 1) daily_digest delivered (via LogChannel-style insert)
execute(
"""
INSERT INTO notification_log (channel, kind, payload, delivered, error)
VALUES ('log', 'daily_digest', %s, true, NULL)
""",
(json.dumps({"text": "Your daily digest is ready", "items": 5}),),
) )
# Count results # 2) email_suggestion delivered
postings_count = len(repo_app.list_postings()) execute(
apps_count = len(repo_app.list_applications()) """
sections_count = len(repo_profile.list_sections()) INSERT INTO notification_log (channel, kind, payload, delivered, error)
VALUES ('log', 'email_suggestion', %s, true, NULL)
""",
(json.dumps({"text": "New email suggestion received", "suggestion_id": "demo"}),),
)
return { # 3) webhook failed with error text
"profile": "Demo Demosson", execute(
"postings": postings_count, """
"applications": apps_count, INSERT INTO notification_log (channel, kind, payload, delivered, error)
"sections": sections_count, VALUES ('webhook', 'daily_digest', %s, false, %s)
} """,
(
json.dumps({"text": "Daily digest delivery attempt", "items": 5}),
"HTTP 503: Service Unavailable (webhook endpoint down)",
),
)
# -- 6 task_run telemetry rows across providers/models --
demo_task_runs = [
{"task": "score_application", "model": "gpt-4o-mini", "provider": "openai", "input_tokens": 1200, "output_tokens": 80, "cost_usd": 0.0012, "duration_ms": 1500},
{"task": "score_application", "model": "gpt-4o", "provider": "openai", "input_tokens": 1500, "output_tokens": 120, "cost_usd": 0.0180, "duration_ms": 2200},
{"task": "cv_tailor", "model": "claude-sonnet-4-20250514", "provider": "anthropic", "input_tokens": 3000, "output_tokens": 800, "cost_usd": 0.0450, "duration_ms": 4500},
{"task": "cl_critique", "model": "gpt-4o-mini", "provider": "openai", "input_tokens": 900, "output_tokens": 200, "cost_usd": 0.0009, "duration_ms": 1800},
{"task": "interview_prep", "model": "claude-3-5-sonnet-20241022", "provider": "anthropic", "input_tokens": 2200, "output_tokens": 600, "cost_usd": 0.0330, "duration_ms": 3100},
{"task": "score_application", "model": "gemini-1.5-flash", "provider": "google", "input_tokens": 1000, "output_tokens": 90, "cost_usd": 0.0005, "duration_ms": 900},
]
link_app = interviewing_app_id or sent_app_id
for idx, tr in enumerate(demo_task_runs):
repo_app.create_task_run({
**tr,
"application_id": link_app if idx % 2 == 0 else None,
})
# -- Return counts --
return _seed_demo_counts()
# --- v1.1: Email Suggestions --- # --- v1.1: Email Suggestions ---

View file

@ -323,6 +323,12 @@ class SeedDemoResponse(BaseModel):
postings: int postings: int
applications: int applications: int
sections: int sections: int
clusters: int = 0
deadlines: int = 0
suggestions: int = 0
notifications: int = 0
task_runs: int = 0
cv_artifacts: int = 0
# --- v1.1: Email Suggestions --- # --- v1.1: Email Suggestions ---