curated_feed.score

Turning a day of candidates into the model’s judgements.

A scorer produces the raw response text and nothing else. Persisting it, validating it and retrying live here, in one place, so that every backend gets the same treatment and a new one is a single small class.

Two backends so far, neither needing an API client or a key:

  • StubScorer invents a response from publication dates alone. It exists to prove the stages after it — selection, rendering, publishing — against a real feed before a token is spent.

  • FileScorer reads a response saved from a chat session, which is how the evaluation workflow drives the real pipeline.

The failure policy is decided here rather than at six in the morning: save the raw text before parsing, retry once with the validation error appended, and on a second failure publish nothing. The previously published feed stays valid and served, so a failed day costs one missing day.

Attributes

Exceptions

ScoreError

Raised when a usable response cannot be obtained.

Classes

TokenUsage

What a day of scoring cost, in tokens.

Scorer

Anything that can produce a response for a day of candidates.

StubScorer

Grades the most recently published candidates, calling nothing.

FileScorer

Reads a response saved from a chat session.

Functions

describe_api_error(→ str)

What an API error says, plus what it was raised from.

score_day(→ curated_feed.models.Response)

Obtain a validated response for one day.

parse_response(→ curated_feed.models.Response)

Validate raw response text, tolerating reasoning and a Markdown fence.

read_response(→ curated_feed.models.Response)

Read a saved response artifact.

unfence(→ str)

The JSON inside a Markdown code fence, or the text unchanged.

strip_reasoning(→ str)

The text after the model's <think> block, or the text unchanged.

make_scorer(→ Scorer)

The scorer named in configuration.

format_summary(→ str)

Human-readable run report.

build_parser(→ argparse.ArgumentParser)

main(→ int)

Module Contents

curated_feed.score.CODE_FENCE
curated_feed.score.THINK_CLOSE = '</think>'
curated_feed.score.NO_SUBSTANCE = 'no-substance'
curated_feed.score.MAX_ATTEMPTS = 2
curated_feed.score.RESPONSE_STAGE = 'response'
curated_feed.score.RETRY_INSTRUCTION = Multiline-String
Show Value
"""Your previous response did not validate: {feedback}
Return the whole response again as valid JSON, corrected."""
exception curated_feed.score.ScoreError[source]

Bases: Exception

Raised when a usable response cannot be obtained.

class curated_feed.score.TokenUsage[source]

What a day of scoring cost, in tokens.

Accumulated across attempts, so a day that needed a retry reports what the retry actually cost rather than what the successful call did. It takes plain counts because every provider names the same numbers differently.

calls: int = 0
input_tokens: int = 0
output_tokens: int = 0
cache_read_tokens: int = 0
cache_write_tokens: int = 0
unreported_calls: int = 0
add(*, input_tokens: int = 0, output_tokens: int = 0, cache_read_tokens: int = 0, cache_write_tokens: int = 0) → None[source]

Fold one response’s usage into the running total.

add_unreported() → None[source]

Count a call whose usage the server did not report.

Kept apart rather than added as zeroes, so the report cannot pass off an unknown cost as a free one.

format() → str[source]

One line for the run report.

>>> TokenUsage(calls=1, input_tokens=2, output_tokens=3).format()
'1 call, 2 in (0 cached), 3 out'
>>> TokenUsage(calls=2, unreported_calls=1).format()
'2 calls, 0 in (0 cached), 0 out; usage not reported for 1'
curated_feed.score.describe_api_error(exc: Exception) → str[source]

What an API error says, plus what it was raised from.

The provider SDKs turn every exception raised while sending into a connection error whose whole message is “Connection error.” — so a key with a trailing newline, which is illegal in a header and fails before any socket is opened, is reported identically to an unreachable host. The distinction lives on __cause__, and only there.

>>> describe_api_error(ValueError("Connection error."))
'Connection error.'
>>> wrapped = ValueError("Connection error.")
>>> wrapped.__cause__ = OSError("nodename nor servname provided")
>>> describe_api_error(wrapped)
'Connection error. (OSError: nodename nor servname provided)'
class curated_feed.score.Scorer[source]

Bases: Protocol

Anything that can produce a response for a day of candidates.

feedback carries the validation error from a previous attempt, and is None on the first. A backend that cannot act on it should say so rather than silently repeating itself.

name: str
generate(candidates: collections.abc.Sequence[curated_feed.models.Candidate], prompt: curated_feed.policy.Prompt, *, feedback: str | None = None) → str[source]
class curated_feed.score.StubScorer(*, items: int)[source]

Grades the most recently published candidates, calling nothing.

Judgement is not the point: publishing something Feedly can ingest is. The graded items therefore clear the floor on consequence alone, and are marked durable so the staleness adjustment cannot pull them back under it.

name = 'stub'
items
generate(candidates: collections.abc.Sequence[curated_feed.models.Candidate], prompt: curated_feed.policy.Prompt, *, feedback: str | None = None) → str[source]
class curated_feed.score.FileScorer(path: pathlib.Path)[source]

Reads a response saved from a chat session.

This is what connects the manual evaluation loop to the pipeline: export a day, paste it into a chat, save the JSON, and the real selection, rendering and publishing run against it.

name = 'file'
path
generate(candidates: collections.abc.Sequence[curated_feed.models.Candidate], prompt: curated_feed.policy.Prompt, *, feedback: str | None = None) → str[source]
curated_feed.score.score_day(scorer: Scorer, candidates: collections.abc.Sequence[curated_feed.models.Candidate], prompt: curated_feed.policy.Prompt, *, raw_path: pathlib.Path) → curated_feed.models.Response[source]

Obtain a validated response for one day.

The raw text of each attempt is written to raw_path before it is parsed, so a response that fails validation can still be read afterwards — fence and all, since what is saved is what arrived. The last attempt is the one left on disk.

curated_feed.score.parse_response(raw: str) → curated_feed.models.Response[source]

Validate raw response text, tolerating reasoning and a Markdown fence.

curated_feed.score.read_response(path: pathlib.Path) → curated_feed.models.Response[source]

Read a saved response artifact.

The artifact is the raw text the model returned, fence and all, because it is the audit trail for a day that went wrong. Everything that reads it back therefore has to be as tolerant as the parse that first accepted it — which is why this exists rather than a bare read_artifact(path, Response).

curated_feed.score.unfence(text: str) → str[source]

The JSON inside a Markdown code fence, or the text unchanged.

Asking for bare JSON does not reliably get it: wrapping it in a fence is a deeply trained habit, and a response saved from a chat session almost always arrives fenced. Spending the day’s one retry on punctuation would be absurd, so the fence is simply removed here.

>>> unfence('```json\n{"graded": []}\n```')
'{"graded": []}'
>>> unfence('{"graded": []}')
'{"graded": []}'
curated_feed.score.strip_reasoning(text: str) → str[source]

The text after the model’s <think> block, or the text unchanged.

Open-weight reasoning models served behind an OpenAI-compatible API often return their reasoning inline rather than in a field of its own. Everything up to the last closing tag goes, which also covers chat templates that open the block in the prompt and leave only </think> in the response. It runs before unfence, so a fenced draft inside the reasoning is never mistaken for the answer.

>>> strip_reasoning('<think>Index 3.</think>\n{"graded": []}')
'{"graded": []}'
>>> strip_reasoning('{"graded": []}')
'{"graded": []}'
curated_feed.score.make_scorer(config: curated_feed.config.Config, *, response_path: pathlib.Path | None) → Scorer[source]

The scorer named in configuration.

The provider SDK is imported only when it is the one selected, so the offline backends — and every other command — stay free of it.

curated_feed.score.format_summary(response: curated_feed.models.Response, *, candidates: int, path: pathlib.Path, scorer: Scorer) → str[source]

Human-readable run report.

curated_feed.score.build_parser() → argparse.ArgumentParser[source]
curated_feed.score.main(argv: collections.abc.Sequence[str] | None = None) → int[source]