Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions backend/src/db/migrations/0045_prescreen_reasons.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Migration 0045: prescreen_reasons for the advisory pre-screen (#756,
-- epic #749 Phase 7).
--
-- The publication pre-screen (#666) used to flip the request to
-- status='blocked' + block_reason='prescreen_failed' and open a "Block:" issue
-- on the dataset repo -- a hard block admins overrode anyway, which masked REAL
-- blockers (bids_validation_failed also uses status='blocked'). Phase 7 makes it
-- a non-blocking advisory: the result is recorded as prescreen_status='concern'
-- with the reasons here, surfaced in the publish-status views, and the request
-- stays in the normal admin-review queue. Reasons are persisted inline (a JSON
-- array) so the status views don't need the GitHub issue that #756 removes.
--
-- Additive nullable column; publication_requests is not FTS-backed.
ALTER TABLE publication_requests ADD COLUMN prescreen_reasons TEXT;
3 changes: 3 additions & 0 deletions backend/src/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,9 @@ adminRoutes.get("/publish/requests", async (c) => {
steps_completed: string;
current_step: string | null;
last_error: string | null;
prescreen_status: string | null;
prescreen_reasons: string | null;
prescreen_issue_url: string | null;
}>();

return c.json({
Expand Down
39 changes: 30 additions & 9 deletions backend/src/routes/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ const BLOCK_MESSAGES: Record<string, string> = {
"BIDS validation has not run yet. Please wait for CI to complete, then re-request publication.",
bids_validation_in_progress:
"BIDS validation is currently running. Please wait for it to complete, then re-request publication.",
// Legacy only: the pre-screen no longer blocks (#756), so new rows never get
// this block_reason. Kept (without the now-removed repo-issue reference) so a
// pre-deploy 'blocked' row still renders a sensible message until re-request.
prescreen_failed:
"The automated pre-screen found missing publication essentials (data, README, or dataset_description). See the issue opened on your dataset repository, address the gaps, then re-request publication.",
"The automated pre-screen flagged missing publication essentials (data, README, or dataset_description). Address the gaps and re-request publication; the pre-screen now runs as a non-blocking advisory.",
};

/**
Expand Down Expand Up @@ -2351,7 +2354,7 @@ datasetRoutes.post("/:id/publish/request", authMiddleware, async (c) => {
// re-request doesn't leave a stale 'failed'/nonce on a 'requested' row).
await db
.prepare(
"UPDATE publication_requests SET status = 'requested', block_reason = NULL, prescreen_status = NULL, prescreen_nonce = NULL, prescreen_issue_url = NULL, updated_at = datetime('now') WHERE id = ?",
"UPDATE publication_requests SET status = 'requested', block_reason = NULL, prescreen_status = NULL, prescreen_nonce = NULL, prescreen_issue_url = NULL, prescreen_reasons = NULL, updated_at = datetime('now') WHERE id = ?",
)
.bind(requestId)
.run();
Expand Down Expand Up @@ -2404,7 +2407,7 @@ datasetRoutes.post("/:id/publish/request", authMiddleware, async (c) => {
);
await db
.prepare(
"UPDATE publication_requests SET prescreen_status = 'pending', prescreen_nonce = ?, prescreen_issue_url = NULL, prescreen_at = NULL, updated_at = datetime('now') WHERE id = ?",
"UPDATE publication_requests SET prescreen_status = 'pending', prescreen_nonce = ?, prescreen_issue_url = NULL, prescreen_reasons = NULL, prescreen_at = NULL, updated_at = datetime('now') WHERE id = ?",
)
.bind(nonce, prId)
.run();
Expand All @@ -2425,7 +2428,7 @@ datasetRoutes.post("/:id/publish/request", authMiddleware, async (c) => {
try {
await db
.prepare(
"UPDATE publication_requests SET prescreen_status = NULL, prescreen_nonce = NULL WHERE id = ?",
"UPDATE publication_requests SET prescreen_status = NULL, prescreen_nonce = NULL, prescreen_reasons = NULL WHERE id = ?",
)
.bind(prId)
.run();
Expand Down Expand Up @@ -2503,6 +2506,8 @@ datasetRoutes.get("/:id/publish/status", authMiddleware, async (c) => {
denied_at: string | null;
denied_reason: string | null;
block_reason: string | null;
prescreen_status: string | null;
prescreen_reasons: string | null;
prescreen_issue_url: string | null;
steps_completed: string;
current_step: string | null;
Expand All @@ -2521,6 +2526,26 @@ datasetRoutes.get("/:id/publish/status", authMiddleware, async (c) => {

const repoName = request.github_repo?.split("/")[1];

// Pre-screen is advisory (#756): when it flagged a concern, surface it as a
// non-blocking advisory (the request is NOT blocked by it). Real blockers keep
// status='blocked' with a BLOCK_MESSAGE below.
let prescreenAdvisory: { source: "prescreen"; reasons: string[]; issue_url?: string } | undefined;
if (request.prescreen_status === "concern") {
let reasons: string[] = [];
try {
const parsed = JSON.parse(request.prescreen_reasons || "[]");
if (Array.isArray(parsed)) reasons = parsed.filter((r) => typeof r === "string");
} catch (err) {
// malformed reasons JSON -> empty list (the advisory flag still shows)
console.error(`[publish-status] malformed prescreen_reasons for ${datasetId}:`, err);
}
prescreenAdvisory = {
source: "prescreen",
reasons,
...(request.prescreen_issue_url ? { issue_url: request.prescreen_issue_url } : {}),
};
}

return c.json({
dataset_id: datasetId,
status: request.status,
Expand All @@ -2536,11 +2561,7 @@ datasetRoutes.get("/:id/publish/status", authMiddleware, async (c) => {
ci_url: `https://github.com/nemarDatasets/${repoName}/actions`,
}
: {}),
// Surface the pre-screen issue so `nemar dataset publish status` shows the
// fix list even if the block email was missed.
...(request.block_reason === "prescreen_failed" && request.prescreen_issue_url
? { prescreen_issue_url: request.prescreen_issue_url }
: {}),
...(prescreenAdvisory ? { advisory: prescreenAdvisory } : {}),
steps_completed: JSON.parse(request.steps_completed || "[]"),
current_step: request.current_step,
last_error: request.last_error,
Expand Down
101 changes: 49 additions & 52 deletions backend/src/routes/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { purgeCacheUrls, zarrPurgeTargets } from "../services/cloudflare.js";
import { runDatasetSync } from "../services/dataset-reindex.js";
import { isValidDatasetId } from "../services/datasetId.js";
import { createEzidVersionDoi, parseDoiProvider } from "../services/doi.js";
import { resolveEmailConfig, sendPublicationBlockedEmail } from "../services/email.js";
import { enrichDataset } from "../services/enrich-dataset.js";
import { TEST_SHOULDER } from "../services/ezid.js";
import { getDatasetsToken } from "../services/github-auth.js";
Expand Down Expand Up @@ -1843,34 +1842,42 @@ export function isDataShortageReason(reason: string): boolean {
);
}

/**
* Decide whether the pre-screen result should be surfaced as an advisory
* concern. `flagged` only FLAGS (the screen found a gap) -- it never blocks
* publication (#756); the handler records it as prescreen_status='concern'. The
* S3-authority logic is unchanged: storage with real blobs refutes a
* data-shortage reason (and can clear an all-data-shortage flag), an empty
* prefix adds one.
*/
export function decidePrescreenOutcome(
verdict: "pass" | "block",
reasons: string[],
s3: PrescreenS3Presence | null,
): { blocked: boolean; reasons: string[] } {
): { flagged: boolean; reasons: string[] } {
let out = [...reasons];
let blocked = verdict === "block";
let flagged = verdict === "block";

const s3Missing = !!s3 && s3.totalSize === 0 && s3.objectCount === 0;
// objectCount === undefined => first-page cap hit => many objects => present.
const s3Present = !!s3 && (s3.totalSize > 0 || s3.objectCount === undefined);

if (s3Present) {
// Storage confirms real blobs: any data-shortage reason is a false
// negative. Drop those; keep README/metadata reasons. Downgrade to pass
// only when the block carried data-shortage reason(s) that ALL got
// stripped -- never silently clear a reasonless or non-data block.
// negative. Drop those; keep README/metadata reasons. Clear the flag only
// when it carried data-shortage reason(s) that ALL got stripped -- never
// silently clear a reasonless or non-data flag.
const kept = out.filter((r) => !isDataShortageReason(r));
if (blocked && kept.length === 0 && out.length > 0) blocked = false;
if (flagged && kept.length === 0 && out.length > 0) flagged = false;
out = kept;
} else if (s3Missing) {
blocked = true;
flagged = true;
if (!out.some((r) => /no data|\bs3\b|storage/i.test(r))) {
out.push("No data files were found in storage for this dataset.");
}
}

return { blocked, reasons: out };
return { flagged, reasons: out };
}

/**
Expand Down Expand Up @@ -1984,58 +1991,37 @@ webhooks.post("/prescreen-result", async (c) => {
console.error(`[prescreen-result] S3 stats failed for ${body.dataset_id} (non-fatal):`, err);
}

const { blocked, reasons } = decidePrescreenOutcome(body.verdict, body.reasons ?? [], s3);
const { flagged, reasons } = decidePrescreenOutcome(body.verdict, body.reasons ?? [], s3);
// Audit the S3 override: if the authoritative S3 check changed the workflow's
// verdict (e.g. cleared an annex-blind false block, #753), record what was
// dropped so a downgrade-to-pass is never silent or untraceable.
const finalVerdict = blocked ? "block" : "pass";
if (body.verdict !== finalVerdict) {
// raw verdict (e.g. cleared an annex-blind false flag, #753), record what was
// dropped so the change is never silent.
const effectiveVerdict = flagged ? "block" : "pass";
if (body.verdict !== effectiveVerdict) {
const stripped = (body.reasons ?? []).filter((r) => !reasons.includes(r));
console.log(
`[prescreen-result] S3 override for ${body.dataset_id}: ${body.verdict} -> ${finalVerdict}; stripped=${JSON.stringify(stripped)} s3_size=${s3?.totalSize ?? "unknown"}`,
`[prescreen-result] S3 override for ${body.dataset_id}: ${body.verdict} -> ${effectiveVerdict}; stripped=${JSON.stringify(stripped)} s3_size=${s3?.totalSize ?? "unknown"}`,
);
}
const issueUrl = body.issue_url ?? null;

if (blocked) {
const res = await c.env.DB.prepare(
// `|| null` (not `?? null`): the advisory workflow sends issue_url="" now, and
// we want NULL in the column, not an empty string.
const issueUrl = body.issue_url || null;

let res: D1Result;
if (flagged) {
// Advisory (#756): record the concern but DO NOT block. The request stays in
// the normal admin-review queue; the concern + reasons are surfaced in the
// publish-status views. No status flip, no block email, no repo issue (the
// workflow no longer opens one). Real blockers (BIDS) keep status='blocked'.
res = await c.env.DB.prepare(
`UPDATE publication_requests
SET status = 'blocked', block_reason = 'prescreen_failed',
prescreen_status = 'failed', prescreen_issue_url = ?,
SET prescreen_status = 'concern', prescreen_reasons = ?, prescreen_issue_url = ?,
prescreen_at = datetime('now'), updated_at = datetime('now')
WHERE id = ? AND prescreen_status = 'pending'`,
)
.bind(issueUrl, request.id)
.bind(JSON.stringify(reasons), issueUrl, request.id)
.run();

// Only email if THIS call did the transition. Guards against a duplicate
// email if two callbacks race past the 'pending' read (the second sees
// changes=0 because the first already flipped the row).
if (res.meta.changes > 0) {
const { fromEmail, replyTo, isDev } = resolveEmailConfig(c.env);
c.executionCtx.waitUntil(
sendPublicationBlockedEmail(
request.requested_by_email,
request.requested_by_username,
body.dataset_id,
reasons,
issueUrl,
c.env.RESEND_API_KEY,
fromEmail,
replyTo,
isDev,
).catch((emailErr) => {
// waitUntil swallows rejections to the runtime log; catch here so the
// failure is attributable and the dataset id is in the message.
console.error(
`[prescreen-result] block email send failed for ${body.dataset_id}:`,
emailErr,
);
}),
);
}
} else {
await c.env.DB.prepare(
res = await c.env.DB.prepare(
`UPDATE publication_requests
SET prescreen_status = 'passed', prescreen_at = datetime('now'),
updated_at = datetime('now')
Expand All @@ -2044,12 +2030,23 @@ webhooks.post("/prescreen-result", async (c) => {
.bind(request.id)
.run();
}
if (res.meta.changes === 0) {
// One-shot guard: the screen was no longer 'pending' (a duplicate/late
// callback). Harmless, but log so a double-dispatch is explicable.
console.warn(
`[prescreen-result] no-op for ${body.dataset_id} request_id=${request.id}: prescreen_status was not 'pending' (duplicate callback?)`,
);
}

console.log(
`[prescreen-result] dataset=${body.dataset_id} request_id=${request.id} verdict=${body.verdict} blocked=${blocked} s3_objects=${s3 ? (s3.objectCount ?? "capped") : "unknown"}`,
`[prescreen-result] dataset=${body.dataset_id} request_id=${request.id} verdict=${body.verdict} prescreen_status=${flagged ? "concern" : "passed"} s3_objects=${s3 ? (s3.objectCount ?? "capped") : "unknown"}`,
);

return c.json({ ok: true, dataset_id: body.dataset_id, blocked });
return c.json({
ok: true,
dataset_id: body.dataset_id,
prescreen_status: flagged ? "concern" : "passed",
});
});

/**
Expand Down
Loading
Loading