-
Notifications
You must be signed in to change notification settings - Fork 1
fix: moderation audit trail — system users, MCP identity, date validation (#442) #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| -- 068_add_system_users.sql | ||
| -- Create system user accounts for auto-publisher and MCP admin so that | ||
| -- moderated_by always has an audit trail. | ||
|
|
||
| INSERT INTO users (id, email, name, oauth_provider, oauth_provider_id, is_admin, role) | ||
| VALUES | ||
| (-1, 'auto-publisher@system.rotv', 'Auto-Publisher', 'system', 'auto-publisher', false, 'viewer'), | ||
| (-2, 'mcp@system.rotv', 'MCP Admin', 'system', 'mcp-admin', false, 'viewer') | ||
| ON CONFLICT (id) DO NOTHING; | ||
|
|
||
| -- Backfill: tag the 508 existing auto-published items with the system user | ||
| UPDATE poi_news SET moderated_by = -1, moderated_at = COALESCE(moderated_at, moderation_date) | ||
| WHERE moderation_status = 'published' AND moderated_by IS NULL AND content_source = 'ai'; | ||
|
|
||
| UPDATE poi_events SET moderated_by = -1, moderated_at = COALESCE(moderated_at, moderation_date) | ||
| WHERE moderation_status = 'published' AND moderated_by IS NULL AND content_source = 'ai'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { renderPage } from './renderPage.js'; | |
| import { deepCrawlForArticle, isGenericUrl } from './deepCrawler.js'; | ||
| import { logInfo, logError, flush as flushJobLogs } from './jobLogger.js'; | ||
| import { parseDate, parseDateTime, localToUTC, scoreDateConsensus, extractUrlDate } from './dateExtractor.js'; | ||
| import { AUTO_PUBLISHER_USER_ID } from '../utils/systemUsers.js'; | ||
| import { scoreDate, normalizeRenderUrl, normalizeTitle } from './newsService.js'; | ||
| import { denyReason, sweepDenyLists } from './filterLists.js'; | ||
|
|
||
|
|
@@ -398,27 +399,30 @@ export async function processItem(pool, contentType, contentId, { forceStatus = | |
| } | ||
|
|
||
| scoring = { confidence_score: newScore / 8.0, reasoning }; | ||
| const autoModeratedBy = resolvedStatus === 'published' ? AUTO_PUBLISHER_USER_ID : null; | ||
| // Only write publication_date when rescore produced a new value — writing the existing | ||
| // value back through this path can silently corrupt a previously-good timestamp | ||
| if (rescoredDate) { | ||
| await pool.query( | ||
| `UPDATE ${table} SET moderation_processed = true, moderation_status = $1, | ||
| publication_date = $2, date_consensus_score = $3, | ||
| ai_reasoning = $4, relevance_signals = $5, moderation_date = CURRENT_TIMESTAMP | ||
| ai_reasoning = $4, relevance_signals = $5, moderation_date = CURRENT_TIMESTAMP, | ||
| moderated_by = COALESCE($7, moderated_by), moderated_at = CASE WHEN $7 IS NOT NULL THEN CURRENT_TIMESTAMP ELSE moderated_at END | ||
| WHERE id = $6`, | ||
| [resolvedStatus, newDate, newScore, reasoning, | ||
| relevanceVotes.length > 0 ? JSON.stringify(relevanceVotes) : null, | ||
| contentId] | ||
| contentId, autoModeratedBy] | ||
| ); | ||
| } else { | ||
| await pool.query( | ||
| `UPDATE ${table} SET moderation_processed = true, moderation_status = $1, | ||
| date_consensus_score = $2, | ||
| ai_reasoning = $3, relevance_signals = $4, moderation_date = CURRENT_TIMESTAMP | ||
| ai_reasoning = $3, relevance_signals = $4, moderation_date = CURRENT_TIMESTAMP, | ||
| moderated_by = COALESCE($6, moderated_by), moderated_at = CASE WHEN $6 IS NOT NULL THEN CURRENT_TIMESTAMP ELSE moderated_at END | ||
| WHERE id = $5`, | ||
| [resolvedStatus, newScore, reasoning, | ||
| relevanceVotes.length > 0 ? JSON.stringify(relevanceVotes) : null, | ||
| contentId] | ||
| contentId, autoModeratedBy] | ||
| ); | ||
|
Comment on lines
405
to
426
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two logic issues with the current
We can fix both issues by conditionally setting these fields based on whether the resolved status is await pool.query(
`UPDATE ${table} SET moderation_processed = true, moderation_status = $1,
publication_date = $2, date_consensus_score = $3,
ai_reasoning = $4, relevance_signals = $5, moderation_date = CURRENT_TIMESTAMP,
moderated_by = CASE WHEN $1 = 'published' THEN COALESCE(moderated_by, $7) ELSE NULL END,
moderated_at = CASE WHEN $1 = 'published' THEN COALESCE(moderated_at, CURRENT_TIMESTAMP) ELSE NULL END
WHERE id = $6`,
[resolvedStatus, newDate, newScore, reasoning,
relevanceVotes.length > 0 ? JSON.stringify(relevanceVotes) : null,
contentId, autoModeratedBy]
);
} else {
await pool.query(
`UPDATE ${table} SET moderation_processed = true, moderation_status = $1,
date_consensus_score = $2,
ai_reasoning = $3, relevance_signals = $4, moderation_date = CURRENT_TIMESTAMP,
moderated_by = CASE WHEN $1 = 'published' THEN COALESCE(moderated_by, $6) ELSE NULL END,
moderated_at = CASE WHEN $1 = 'published' THEN COALESCE(moderated_at, CURRENT_TIMESTAMP) ELSE NULL END
WHERE id = $5`,
[resolvedStatus, newScore, reasoning,
relevanceVotes.length > 0 ? JSON.stringify(relevanceVotes) : null,
contentId, autoModeratedBy]
);
} |
||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const AUTO_PUBLISHER_USER_ID = -1; | ||
| export const MCP_ADMIN_USER_ID = -2; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If both
moderated_atandmoderation_dateareNULL(which can happen for older items published before themoderation_datecolumn was introduced),moderated_atwill remainNULLafter the backfill. Coalescing withcollection_dateprovides a safe fallback timestamp to ensuremoderated_atis populated for all published items.