File size: 24,066 Bytes
7bfb73d
 
 
 
 
 
 
 
 
 
 
 
 
43e3e30
7bfb73d
43e3e30
7bfb73d
43e3e30
7bfb73d
 
 
43e3e30
 
7bfb73d
 
 
 
 
 
 
43e3e30
ca60ce9
7bfb73d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c0d98
 
7bfb73d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c0d98
 
 
 
7bfb73d
40c0d98
7bfb73d
 
 
 
40c0d98
7bfb73d
 
 
 
40c0d98
7bfb73d
40c0d98
7bfb73d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c0d98
7bfb73d
40c0d98
 
7bfb73d
40c0d98
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfb73d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40c0d98
 
7bfb73d
 
 
 
 
 
40c0d98
 
 
 
7bfb73d
40c0d98
 
 
7bfb73d
40c0d98
 
 
 
7bfb73d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca60ce9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a25cc
ca60ce9
 
 
 
26a25cc
 
ca60ce9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a25cc
ca60ce9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43e3e30
 
 
 
 
 
 
 
 
 
8ef15b5
 
 
 
 
 
 
43e3e30
8ef15b5
43e3e30
 
 
26a25cc
 
43e3e30
 
 
 
 
 
 
8ef15b5
 
 
43e3e30
8ef15b5
 
 
 
43e3e30
 
 
 
 
 
 
 
 
 
26a25cc
 
43e3e30
 
26a25cc
43e3e30
 
 
8ef15b5
 
 
ca60ce9
8ef15b5
 
ca60ce9
 
8ef15b5
 
 
 
 
 
 
 
26a25cc
 
8ef15b5
 
 
 
 
 
 
 
26a25cc
8ef15b5
 
 
 
 
 
43e3e30
 
7bfb73d
43e3e30
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
"""Papers Reproducibility demo β€” Gradio Space.

Three tabs:
  - Browse: see papers that have been (or are being) reproduced, with the
    generated report and an interactive trace of how the run went.
  - Request: ask for a paper to be reproduced. Goes in as "pending".
  - Admin: password-gated approve/reject queue. Approving only flips a
    status flag β€” the actual agent run happens on a separate machine via
    local_runner.py, which polls this same store. See README.md.
"""

from __future__ import annotations

import hmac
import html as html_lib
import json
import os
from datetime import date, datetime, timedelta, timezone

import gradio as gr

import daily_scan
import slack_bot
import store
from trace_view import render_trace
from dotenv import load_dotenv

load_dotenv()

ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD")
SCAN_TRIGGER_TOKEN = os.environ.get("SCAN_TRIGGER_TOKEN")
DAILY_SCAN_HOUR = int(os.environ.get("DAILY_SCAN_HOUR", "8"))
REFRESH_SECONDS = 6

STATUS_ICON = {
    "pending": "⏳",
    "approved": "πŸ—“οΈ",
    "running": "πŸƒ",
    "completed": "βœ…",
    "failed": "❌",
    "rejected": "🚫",
}

# (text color, tinted background) β€” semi-transparent backgrounds so these
# read fine on both light and dark Gradio themes without separate palettes.
STATUS_COLORS = {
    "pending": ("#6b7280", "rgba(107,114,128,0.16)"),
    "approved": ("#2563eb", "rgba(37,99,235,0.16)"),
    "running": ("#b45309", "rgba(180,83,9,0.16)"),
    "completed": ("#0d9488", "rgba(13,148,136,0.16)"),
    "failed": ("#b91c1c", "rgba(185,28,28,0.16)"),
    "rejected": ("#6b7280", "rgba(107,114,128,0.16)"),
}

THEME = gr.themes.Soft(
    primary_hue="teal",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont("Inter"), "-apple-system", "sans-serif"],
)

CUSTOM_CSS = """
.app-header { padding: 4px 2px 18px; margin-bottom: 6px; border-bottom: 1px solid var(--border-color-primary); }
.app-header-title { font-size: 1.7rem; font-weight: 800; letter-spacing: -0.02em; display: flex; align-items: center; gap: 10px; }
.app-header-sub { color: var(--body-text-color-subdued); margin-top: 4px; font-size: 0.95rem; }

.section-card {
  border: 1px solid var(--border-color-primary) !important;
  border-radius: 14px !important;
  background: var(--background-fill-secondary) !important;
  padding: 18px !important;
}

.job-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; margin: 4px 0 2px; }
.job-card {
  border: 1px solid var(--border-color-primary); border-radius: 12px; padding: 12px 14px;
  background: var(--background-fill-primary);
}
.job-card.selected { border-color: #0d9488; box-shadow: 0 0 0 1px #0d9488 inset; }
.job-card-clickable { cursor: pointer; transition: border-color 0.1s ease, box-shadow 0.1s ease; }
.job-card-clickable:hover { border-color: #0d9488; box-shadow: 0 0 0 1px rgba(13,148,136,0.35) inset; }
.job-card h4 { margin: 8px 0 6px; font-size: 0.95rem; font-weight: 700; line-height: 1.3; }
.job-card .job-card-links { font-size: 0.82rem; }
.job-card .job-card-links a { color: #0d9488; text-decoration: none; margin-right: 10px; }
.job-card .job-card-links a:hover { text-decoration: underline; }
.job-card .job-card-meta { font-size: 0.76rem; color: var(--body-text-color-subdued); margin-top: 6px; }
.job-card .job-card-error { font-size: 0.78rem; color: #b91c1c; margin-top: 6px; }

.status-badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px;
  border-radius: 999px; font-size: 0.74rem; font-weight: 700; letter-spacing: 0.01em;
}
.mode-chip {
  display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: 0.72rem;
  background: var(--background-fill-secondary); border: 1px solid var(--border-color-primary);
  color: var(--body-text-color-subdued); margin-left: 6px;
}

.empty-state { padding: 28px; text-align: center; color: var(--body-text-color-subdued); border: 1px dashed var(--border-color-primary); border-radius: 12px; }

.detail-header { padding: 2px 2px 14px; }
.detail-header h3 { margin: 6px 0 8px; }
.detail-links a { color: #0d9488; text-decoration: none; margin-right: 4px; }
.detail-links a:hover { text-decoration: underline; }

.report-frame { width: 100%; height: 800px; border: 1px solid var(--border-color-primary); border-radius: 10px; }
"""


def _now_iso() -> str:
    return datetime.now(timezone.utc).isoformat()


def status_badge(status: str) -> str:
    icon = STATUS_ICON.get(status, "")
    text_color, bg = STATUS_COLORS.get(status, ("#6b7280", "rgba(107,114,128,0.16)"))
    return f'<span class="status-badge" style="color:{text_color}; background:{bg};">{icon} {status}</span>'


def _job_label(job: dict) -> str:
    return f"{STATUS_ICON.get(job['status'], '')} {job['title']}"


def _safe_list_jobs() -> list[dict]:
    try:
        return store.list_jobs()
    except Exception as exc:  # store not configured yet, or transient HF error
        gr.Warning(f"Could not reach the job store: {exc}")
        return []


def _job_links_html(job: dict) -> str:
    # onclick stopPropagation so these links don't also trigger the card's
    # own click-to-select behavior underneath them.
    stop = "onclick='event.stopPropagation()'"
    links = [f"<a href='{job['paper_url']}' target='_blank' {stop}>paper</a>", f"<a href='{job['code_url']}' target='_blank' {stop}>code</a>"]
    if job.get("data_url"):
        links.append(f"<a href='{job['data_url']}' target='_blank' {stop}>data</a>")
    return " Β· ".join(links)


def job_card_html(job: dict, selected: bool = False) -> str:
    cls = "job-card job-card-clickable selected" if selected else "job-card job-card-clickable"
    ts = job.get("finished_at") or job.get("started_at") or job.get("requested_at") or ""
    error_html = ""
    if job["status"] == "failed" and job.get("error"):
        error_html = f'<div class="job-card-error">{html_lib.escape(job["error"][:140])}…</div>'
    nav_js = html_lib.escape(f"window.location.search = '?job_id={job['id']}'", quote=True)
    return (
        f'<div class="{cls}" onclick="{nav_js}">'
        f'<div>{status_badge(job["status"])}<span class="mode-chip">{job["mode"]}</span></div>'
        f'<h4>{html_lib.escape(job["title"])}</h4>'
        f'<div class="job-card-links">{_job_links_html(job)}</div>'
        f'<div class="job-card-meta">{ts}</div>'
        f"{error_html}"
        f"</div>"
    )


def pending_card_html(job: dict, selected: bool = False) -> str:
    cls = "job-card selected" if selected else "job-card"
    extra = ""
    if job.get("requested_by"):
        extra += f'<div class="job-card-meta">from {html_lib.escape(job["requested_by"])}</div>'
    if job.get("notes"):
        extra += f'<div class="job-card-meta">"{html_lib.escape(job["notes"][:120])}"</div>'
    return (
        f'<div class="{cls}">'
        f'<div><span class="mode-chip">{job["mode"]}</span></div>'
        f'<h4>{html_lib.escape(job["title"])}</h4>'
        f'<div class="job-card-links">{_job_links_html(job)}</div>'
        f'<div class="job-card-meta">requested {job.get("requested_at", "")}</div>'
        f"{extra}"
        f"</div>"
    )


def gallery_html(jobs: list[dict], selected_id, empty_message: str, card_fn=job_card_html) -> str:
    if not jobs:
        return f'<div class="empty-state">{empty_message}</div>'
    cards = "".join(card_fn(j, selected=(j["id"] == selected_id)) for j in jobs)
    return f'<div class="job-gallery">{cards}</div>'


# ---------------------------------------------------------------------------
# Browse tab
# ---------------------------------------------------------------------------

def _visible_browse_jobs() -> list[dict]:
    return [j for j in _safe_list_jobs() if j["status"] in ("completed", "running", "failed")]


def refresh_browse(selected_id):
    jobs = _visible_browse_jobs()
    ids = [j["id"] for j in jobs]
    value = selected_id if selected_id in ids else (ids[0] if ids else None)
    gallery = gallery_html(jobs, value, "No papers yet β€” submit a request on the next tab and have an admin approve it.")
    return gallery, value


def browse_and_load(selected_id):
    gallery, value = refresh_browse(selected_id)
    header_body, trace_block = load_job_view(value)
    return gallery, header_body, trace_block, value


def browse_on_page_load(request: gr.Request | None = None):
    # Clicking a card navigates to "?job_id=<id>" (see job_card_html), which
    # reloads the page β€” so the initial selection on load comes from the URL.
    initial_id = request.query_params.get("job_id") if request else None
    return browse_and_load(initial_id)


def load_job_view(job_id):
    if not job_id:
        return "", ""

    job = store.get_job(job_id)
    if not job:
        return '<div class="empty-state">This job is no longer in the store.</div>', ""

    header = (
        '<div class="detail-header">'
        f'<h3>{html_lib.escape(job["title"])}</h3>'
        f'<div>{status_badge(job["status"])}<span class="mode-chip">{job["mode"]}</span></div>'
        f'<p class="detail-links" style="margin-top:8px">{_job_links_html(job)}</p>'
        "</div>"
    )

    if job["status"] == "running":
        body = '<div class="empty-state">⏳ Still running β€” the report will appear here once the local runner finishes.</div>'
    elif job["status"] == "failed":
        body = f'<div class="empty-state" style="text-align:left; border-color:#b91c1c;">This run failed.<pre style="white-space:pre-wrap; margin-top:10px;">{html_lib.escape(job.get("error") or "No error details recorded.")}</pre></div>'
    else:
        report = store.read_report(job_id)
        if report:
            escaped = html_lib.escape(report, quote=True)
            body = f'<iframe srcdoc="{escaped}" class="report-frame"></iframe>'
        else:
            body = '<div class="empty-state">No report available yet.</div>'

    events = store.read_trace(job_id)
    trace_block = render_trace(events, running=(job["status"] == "running"))
    return header + body, trace_block


# ---------------------------------------------------------------------------
# Request tab
# ---------------------------------------------------------------------------

def submit_request(title, paper_url, code_url, data_url, mode, notes, requested_by):
    title, paper_url, code_url = title.strip(), paper_url.strip(), code_url.strip()
    if not title or not paper_url or not code_url:
        return "⚠️ Please fill in at least the title, paper link, and code link.", title, paper_url, code_url, data_url, notes, requested_by
    try:
        job = store.create_request(
            title=title,
            paper_url=paper_url,
            code_url=code_url,
            data_url=data_url,
            mode=mode,
            notes=notes,
            requested_by=requested_by,
        )
    except Exception as exc:
        return f"⚠️ Could not submit the request: {exc}", title, paper_url, code_url, data_url, notes, requested_by
    return f"βœ… Submitted β€” **{job['title']}** is now pending admin approval.", "", "", "", "", "", ""


# ---------------------------------------------------------------------------
# Admin tab
# ---------------------------------------------------------------------------

def unlock_admin(password):
    if not ADMIN_PASSWORD:
        return False, "⚠️ ADMIN_PASSWORD is not set on this deployment β€” the admin tab is disabled."
    if password == ADMIN_PASSWORD:
        return True, "πŸ”“ Unlocked."
    return False, "❌ Wrong password."


def _pending_jobs() -> list[dict]:
    jobs = [j for j in _safe_list_jobs() if j["status"] == "pending"]
    jobs.sort(key=lambda j: j.get("requested_at", ""))
    return jobs


def refresh_pending(authed, current_value):
    if not authed:
        return gr.update(choices=[], value=None), ""
    jobs = _pending_jobs()
    choices = [(_job_label(j), j["id"]) for j in jobs]
    ids = [c[1] for c in choices]
    value = current_value if current_value in ids else (ids[0] if ids else None)
    gallery = gallery_html(jobs, value, "Nothing pending β€” you're all caught up.", card_fn=pending_card_html)
    return gr.update(choices=choices, value=value), gallery


def approve_job(authed, job_id):
    if not authed or not job_id:
        return gr.update(), ""
    store.set_status(job_id, "approved", approved_at=_now_iso())
    gr.Info("Approved β€” local_runner.py will pick it up.")
    return refresh_pending(authed, None)


def reject_job(authed, job_id):
    if not authed or not job_id:
        return gr.update(), ""
    store.set_status(job_id, "rejected")
    gr.Info("Rejected.")
    return refresh_pending(authed, None)


# ---------------------------------------------------------------------------
# Layout
# ---------------------------------------------------------------------------

with gr.Blocks(title="Papers Reproducibility") as demo:
    gr.HTML(
        '<div class="app-header">'
        '<div class="app-header-title">πŸ”¬ Papers Reproducibility</div>'
        '<div class="app-header-sub">Reproduce a paper\'s figures with a Claude Code agent β€” '
        "browse what's already done, or request the next one.</div>"
        "</div>"
    )

    # timer = gr.Timer(REFRESH_SECONDS)

    with gr.Tab("Browse"):
        browse_selected_id = gr.State(None)
        browse_refresh_btn = gr.Button("↻ Refresh")
        browse_gallery = gr.HTML()
        with gr.Group(elem_classes="section-card"):
            report_html = gr.HTML()
            gr.Markdown("#### Reproduction trace")
            trace_html = gr.HTML()

        browse_refresh_btn.click(
            browse_and_load,
            inputs=[browse_selected_id],
            outputs=[browse_gallery, report_html, trace_html, browse_selected_id],
        )
        #timer.tick(
        #    browse_and_load, inputs=[browse_selected_id],
        #    outputs=[browse_gallery, report_html, trace_html, browse_selected_id],
        #)
        demo.load(
            browse_on_page_load,
            inputs=None,
            outputs=[browse_gallery, report_html, trace_html, browse_selected_id],
        )

    with gr.Tab("Request a reproduction"):
        with gr.Group(elem_classes="section-card"):
            gr.Markdown("Fill this in and an admin will review it before anything runs.")
            req_title = gr.Textbox(label="Paper title *")
            with gr.Row():
                req_paper_url = gr.Textbox(label="Paper link *", placeholder="https://arxiv.org/abs/...")
                req_code_url = gr.Textbox(label="Code link *", placeholder="GitHub repo or Zenodo record")
            with gr.Row():
                req_data_url = gr.Textbox(label="Data link (optional)", placeholder="Zenodo record, if separate from the code")
                req_mode = gr.Radio(
                    ["replicator", "author"],
                    value="replicator",
                    label="Mode",
                    info="'replicator' = reproduce someone else's paper. 'author' = audit your own code.",
                )
            req_notes = gr.Textbox(label="Notes (optional)", lines=3)
            req_by = gr.Textbox(label="Your name / email (optional)")
            req_submit = gr.Button("Submit request", variant="primary")
            req_status = gr.Markdown()

        req_submit.click(
            submit_request,
            inputs=[req_title, req_paper_url, req_code_url, req_data_url, req_mode, req_notes, req_by],
            outputs=[req_status, req_title, req_paper_url, req_code_url, req_data_url, req_notes, req_by],
        )

    with gr.Tab("Admin"):
        authed_state = gr.State(False)

        with gr.Group(elem_classes="section-card"):
            with gr.Row():
                admin_password = gr.Textbox(label="Admin password", type="password", scale=3)
                admin_unlock_btn = gr.Button("Unlock", scale=1)
            admin_unlock_status = gr.Markdown()

        gr.Markdown(
            "Approving a request only marks it `approved` here β€” a separate "
            "process, `local_runner.py`, running on a machine with Docker and "
            "the `claude` CLI, polls for approved jobs and actually runs the "
            "agent. See README.md to set that up."
        )
        with gr.Row():
            pending_dropdown = gr.Dropdown(label="Pending requests", choices=[], value=None, scale=4)
            with gr.Column(scale=1):
                approve_btn = gr.Button("βœ“ Approve", variant="primary")
                reject_btn = gr.Button("βœ• Reject", variant="stop")
        pending_gallery = gr.HTML()

        admin_unlock_btn.click(unlock_admin, inputs=[admin_password], outputs=[authed_state, admin_unlock_status]).then(
            refresh_pending, inputs=[authed_state, pending_dropdown], outputs=[pending_dropdown, pending_gallery]
        )
        pending_dropdown.change(
            lambda authed, jid: gallery_html(_pending_jobs(), jid, "Nothing pending β€” you're all caught up.", card_fn=pending_card_html)
            if authed
            else "",
            inputs=[authed_state, pending_dropdown],
            outputs=[pending_gallery],
        )
        approve_btn.click(approve_job, inputs=[authed_state, pending_dropdown], outputs=[pending_dropdown, pending_gallery])
        reject_btn.click(reject_job, inputs=[authed_state, pending_dropdown], outputs=[pending_dropdown, pending_gallery])
        # timer.tick(refresh_pending, inputs=[authed_state, pending_dropdown], outputs=[pending_dropdown, pending_gallery])


# ---------------------------------------------------------------------------
# Daily-scan runner + scheduler
#
# _run_daily_scan actually kicks off daily_scan.py's fetch-discover-judge-
# shortlist pipeline; it runs in-process on this same always-on Space since,
# unlike the actual reproduction runs, it needs no Docker/claude CLI. There
# are three ways to reach it:
#   - the scheduler below, which fires it automatically once a day β€” the
#     primary path, no external caller involved at all
#   - POST /trigger/daily-scan, an external-facing fallback (testing, or
#     forcing a run from some other scheduler you set up instead)
#   - POST /slack/commands, a human-triggered fallback from Slack itself
# All three run it as a background task, since two of them (the HTTP routes)
# must ack fast and none of them should block the Gradio UI.
# ---------------------------------------------------------------------------

from contextlib import asynccontextmanager

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from fastapi import BackgroundTasks, FastAPI, Header, HTTPException, Request, Response

_scan_state = {"running": False}


def _run_daily_scan(scan_date: str | None, limit: int) -> None:
    if _scan_state["running"]:
        return
    _scan_state["running"] = True
    try:
        day = date.fromisoformat(scan_date) if scan_date else date.today()
        created = daily_scan.scan(day, limit, mode="live")
        print(f"daily scan: created {len(created)} candidate(s) for {day.isoformat()}")
    except Exception as exc:
        print(f"daily scan failed: {exc}")
    finally:
        _scan_state["running"] = False


_scheduler = AsyncIOScheduler(timezone="UTC")


@asynccontextmanager
async def _lifespan(_app: FastAPI):
    if os.environ.get("SLACK_WEBHOOK_URL"):
        _scheduler.add_job(
            _run_daily_scan,
            CronTrigger(hour=DAILY_SCAN_HOUR, minute=0, timezone="UTC"),
            args=[None, 10],
            id="daily_scan",
            replace_existing=True,
        )
        _scheduler.start()
        print(f"[scheduler] daily scan scheduled at {DAILY_SCAN_HOUR:02d}:00 UTC")
    else:
        print("[scheduler] SLACK_WEBHOOK_URL not set β€” scheduled daily scan disabled")
    yield
    if _scheduler.running:
        _scheduler.shutdown(wait=False)


fastapi_app = FastAPI(lifespan=_lifespan)


# ---------------------------------------------------------------------------
# Slack interactivity callback
#
# Approve/Reject button clicks on daily-scan candidates (see slack_bot.py,
# posted by daily_scan.py) land here. Mounted on the same FastAPI app as the
# Gradio UI so the Space's existing public URL doubles as the Slack request
# URL β€” no separate service to deploy.
# ---------------------------------------------------------------------------


def _handle_slack_interaction_safely(payload: dict) -> None:
    try:
        slack_bot.handle_interaction(payload)
    except Exception as exc:  # background task β€” nothing left to return this to, just log it
        print(f"slack interaction handling failed: {exc}")


@fastapi_app.post("/slack/interactions")
async def slack_interactions(request: Request, background_tasks: BackgroundTasks) -> Response:
    body = await request.body()
    timestamp = request.headers.get("X-Slack-Request-Timestamp", "")
    signature = request.headers.get("X-Slack-Signature", "")
    #if not slack_bot.verify_signature(timestamp, body.decode("utf-8"), signature):
    #    return Response(status_code=401)

    form = await request.form()
    payload_raw = form.get("payload")
    if not payload_raw:
        return Response(status_code=400)

    try:
        payload = json.loads(payload_raw)
    except json.JSONDecodeError:
        return Response(status_code=400)

    # Ack immediately β€” the actual store update + Slack message edit involves
    # a few full HF Hub round-trips, easily enough to blow past Slack's
    # 3-second interactivity timeout if done before responding.
    background_tasks.add_task(_handle_slack_interaction_safely, payload)
    return Response(status_code=200)


@fastapi_app.post("/trigger/daily-scan")
async def trigger_daily_scan(
    background_tasks: BackgroundTasks,
    scan_date: str | None = None,
    limit: int = 10,
    x_trigger_token: str = Header(default=""),
) -> dict:
    #if not SCAN_TRIGGER_TOKEN or not hmac.compare_digest(x_trigger_token, SCAN_TRIGGER_TOKEN):
    #    raise HTTPException(status_code=401, detail="invalid or missing X-Trigger-Token")
    if _scan_state["running"]:
        return {"status": "already_running"}
    background_tasks.add_task(_run_daily_scan, scan_date, limit)
    return {"status": "started"}


# ---------------------------------------------------------------------------
# Manual trigger β€” Slack Slash Command
#
# A human-invoked alternative to letting the scheduler above fire on its own.
# Authenticated the same way /slack/interactions is (Slack's own request
# signature, not SCAN_TRIGGER_TOKEN) since this request comes from Slack
# itself, not from an external caller. Must ack within Slack's 3-second
# window, so the scan runs as a background task here too.
# ---------------------------------------------------------------------------


@fastapi_app.post("/slack/commands", response_model=None)
async def slack_commands(request: Request, background_tasks: BackgroundTasks):
    body = await request.body()
    timestamp = request.headers.get("X-Slack-Request-Timestamp", "")
    signature = request.headers.get("X-Slack-Signature", "")
    #if not slack_bot.verify_signature(timestamp, body.decode("utf-8"), signature):
    #    return Response(status_code=401)

    form = await request.form()
    text = (form.get("text") or "").strip()

    if _scan_state["running"]:
        return {"response_type": "ephemeral", "text": "⏳ A scan is already running β€” check back shortly."}

    limit = int(text) if text.isdigit() else 10
    background_tasks.add_task(_run_daily_scan, None, limit)
    return {
        "response_type": "ephemeral",
        "text": f"πŸ”¬ Scan started (top {limit}) β€” candidates will post to this channel shortly.",
    }


app = gr.mount_gradio_app(fastapi_app, demo, path="/", theme=THEME, css=CUSTOM_CSS)

if __name__ == "__main__":
    import uvicorn

    host = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
    port = int(os.environ.get("GRADIO_SERVER_PORT", os.environ.get("PORT", 7860)))
    uvicorn.run(app, host=host, port=port)