Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 2x 2x 2x 1x 1x 1x | /**
* @module parliamentary-data/mcp-retry-queue/classifier
* @description Transient vs permanent retry classification — per-resource-type
* drain handlers for `document_fulltext` and `voteringar_search` entries.
*
* Each handler:
* - Invokes the appropriate MCP client method.
* - Returns a structured `DrainOutcome` so the orchestrator can decide
* whether to resolve, retain (with incremented `attemptCount`), or
* classify as `fetch_error`.
* - Emits the diagnostic that lands in the data-download-manifest's
* `## MCP Query Diagnostics` section (including `fetch_error` for failed
* retries — without this, failures disappear into the aggregate retained
* counter).
*
* @author Hack23 AB
* @license Apache-2.0
*/
import type { MCPClient } from '../../mcp-client/client.js';
import type { FetchVotingFilters, MCPToolInvocationDiagnostic } from '../../types/mcp.js';
import type { MCPRetryQueueEntry } from './persistence.js';
export type DrainOutcome =
| {
kind: 'resolved';
diagnostic: MCPToolInvocationDiagnostic;
document?: Record<string, unknown>;
voteringar?: unknown[];
}
| {
kind: 'retained';
diagnostic: MCPToolInvocationDiagnostic | null;
entry: MCPRetryQueueEntry;
};
export async function drainDocumentFulltextEntry(
client: MCPClient,
entry: MCPRetryQueueEntry,
now: Date,
): Promise<DrainOutcome> {
const lastAttemptAt = now.toISOString();
try {
const result = await client.fetchDocumentDetailsWithCoverage(
entry.resourceId,
true,
{
requestedDate: (entry.params['requestedDate'] as string | undefined) ?? null,
retrieval: 'retry_queue',
},
);
const diagnostic: MCPToolInvocationDiagnostic = {
tool: entry.tool,
query: { ...entry.params, dok_id: entry.resourceId, include_full_text: true },
resultCount: result.resultCount,
coverageState: result.coverageState,
provenance: result.provenance,
notes: entry.reason,
};
Eif (result.coverageState === 'full_text') {
return { kind: 'resolved', diagnostic, document: result.document };
}
return {
kind: 'retained',
diagnostic,
entry: {
...entry,
attemptCount: entry.attemptCount + 1,
coverageState: result.coverageState,
reason: entry.reason ?? `Deferred ${entry.tool} retry still ${result.coverageState}`,
lastAttemptAt,
},
};
} catch (drainErr) {
const errMessage = drainErr instanceof Error ? drainErr.message : String(drainErr);
console.warn(
`[mcp-retry-queue] Document retry failed for ${entry.resourceId}:`,
errMessage,
);
return {
kind: 'retained',
diagnostic: {
tool: entry.tool,
query: { ...entry.params, dok_id: entry.resourceId, include_full_text: true },
resultCount: 0,
coverageState: 'fetch_error',
provenance: {
provider: 'riksdag-regering',
endpoint: client.baseURL,
tool: entry.tool,
query: { ...entry.params, dok_id: entry.resourceId, include_full_text: true },
resultCount: 0,
coverageState: 'fetch_error',
retrieval: 'retry_queue',
retrievedAt: now.toISOString(),
},
notes: `Retry failed: ${errMessage}`,
},
entry: {
...entry,
attemptCount: entry.attemptCount + 1,
coverageState: 'fetch_error',
reason: `Retry failed: ${errMessage}`,
lastAttemptAt,
},
};
}
}
export async function drainVoteringarSearchEntry(
client: MCPClient,
entry: MCPRetryQueueEntry,
now: Date,
): Promise<DrainOutcome> {
const lastAttemptAt = now.toISOString();
const votingParams = entry.params;
if (typeof votingParams !== 'object' || votingParams === null) {
return {
kind: 'retained',
diagnostic: null,
entry: {
...entry,
attemptCount: entry.attemptCount + 1,
reason: 'retry queue entry has invalid voting params payload',
lastAttemptAt,
},
};
}
try {
const votingResult = await client.fetchVotingRecordsWithDiagnostics(
votingParams as FetchVotingFilters,
);
const diagnostic: MCPToolInvocationDiagnostic = {
tool: entry.tool,
query: { ...(entry.params as Record<string, unknown>) },
resultCount: votingResult.resultCount,
coverageState: votingResult.coverageState,
provenance: votingResult.provenance,
notes: entry.reason,
...(votingResult.signal ? { signal: votingResult.signal } : {}),
};
if (votingResult.resultCount > 0) {
return {
kind: 'resolved',
diagnostic,
voteringar: Array.isArray(votingResult.items)
? (votingResult.items as unknown[])
: undefined,
};
}
return {
kind: 'retained',
diagnostic,
entry: {
...entry,
attemptCount: entry.attemptCount + 1,
coverageState: votingResult.coverageState,
reason: votingResult.signal?.message ?? entry.reason,
lastAttemptAt,
},
};
} catch (drainErr) {
const errMessage = drainErr instanceof Error ? drainErr.message : String(drainErr);
console.warn(
`[mcp-retry-queue] Voting retry failed for ${entry.resourceId}:`,
errMessage,
);
return {
kind: 'retained',
diagnostic: {
tool: entry.tool,
query: { ...(entry.params as Record<string, unknown>) },
resultCount: 0,
coverageState: 'fetch_error',
provenance: {
provider: 'riksdag-regering',
endpoint: client.baseURL,
tool: entry.tool,
query: { ...(entry.params as Record<string, unknown>) },
resultCount: 0,
coverageState: 'fetch_error',
retrieval: 'retry_queue',
retrievedAt: now.toISOString(),
},
notes: `Retry failed: ${errMessage}`,
},
entry: {
...entry,
attemptCount: entry.attemptCount + 1,
coverageState: 'fetch_error',
reason: `Retry failed: ${errMessage}`,
lastAttemptAt,
},
};
}
}
|