Keep broken model answers and articles from stopping the news (refs #58, refs #61)

The independent review of phase 7 found that an answer with half a Unicode
character, or of an unexpected shape, escaped the validation as an exception:
its cost was lost, it was paid again every two minutes past the daily cap, and
the diary of the news stopped. The same happened to the whole round with an
article cut in the middle of an emoji.

Such answers are now refused like any other, the cost is written before the
request leaves and settled with what the service used, never clamped to the
estimate, and neither the analyst nor one source can stop the others. The
estimate counts every byte as a token, so it is a true worst case. Summaries
with bare domains or invisible characters are refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 09:24:36 +01:00
1 parent ae4ae4ed92
commit 61878c2023
7 files changed
+353 -40

No files matched your search

+16 -5
View File
@@ -13,7 +13,7 @@ namespace Anubis.Tests;
/// news as untrusted data, a restriction that can only be added, the daily cap, the cache and the batches.
/// </summary>
[TestClass]
public sealed class NewsAnalystTests
public sealed partial class NewsAnalystTests
{
private static readonly CancellationToken _none = CancellationToken.None;
@@ -190,22 +190,23 @@ public sealed class NewsAnalystTests
await using var test = await DatabaseTests.MigratedAsync();
var (clock, settings, specs) = await SetUpAsync(test.Db);
// Prices that make one request of eight cost about 0.30 € in the worst case: the second would pass 0.50.
await settings.SetAsync(NewsModelSettings.PriceIn.Key, "10", "test", _none);
await settings.SetAsync(NewsModelSettings.PriceIn.Key, "5", "test", _none);
await settings.SetAsync(NewsModelSettings.PriceOut.Key, "50", "test", _none);
for (var i = 0; i < 20; i++)
{
await NewsRulesTests.StoryAsync(test.Db, clock, specs["SOL"], $"Solana story number {i}", Now.AddMinutes(-5 - i), 2, 0.1, "CoinDesk", trusted: true);
}
// A service that bills far more than it should: the analyst never counts more than the estimate it checked.
var model = new FakeModel(r => new ModelReply(Answer(r, item => Verdict(item, ["SOL"], "neutral", 2)), 50_000_000, 50_000_000, null, true));
// A service that bills the worst case it was allowed: every byte a token and the whole room for the answer.
var model = new FakeModel(r => new ModelReply(Answer(r, item => Verdict(item, ["SOL"], "neutral", 2)),
System.Text.Encoding.UTF8.GetByteCount(r.Instructions + r.Message + r.Schema.ToJsonString()), r.MaxOutput, null, true));
var analyst = Analyst(test.Db, clock, settings, TestKey, model);
var first = await analyst.RunOnceAsync(_none);
Assert.AreEqual(1, first.Calls);
Assert.AreEqual("tetto di spesa raggiunto", first.Skipped);
var estimate = NewsAnalyst.Estimate(await NewsModelSettings.LoadAsync(settings, _none), model.Requests[0]);
Assert.AreEqual(estimate, first.Spent);
Assert.IsTrue(first.Spent <= estimate && first.Spent > estimate * 0.9m, "The estimate is a true worst case.");
Assert.IsTrue(estimate is > 0.25m and < 0.50m, estimate.ToString(CultureInfo.InvariantCulture));
var second = await analyst.RunOnceAsync(_none);
@@ -230,6 +231,16 @@ public sealed class NewsAnalystTests
Assert.IsTrue(next.Calls >= 1, "Asked again, as far as the new day allows.");
Assert.IsTrue((await analyst.SpendingAsync(_none)).Spent <= 0.50m);
// A service that ignores its own limits: what it billed is shown whole, and nothing more is asked that day.
clock.Advance(TimeSpan.FromDays(1));
await NewsRulesTests.StoryAsync(test.Db, clock, specs["SOL"], "Solana story with a greedy service", clock.UtcNow, 2, 0.1, "CoinDesk", trusted: true);
var greedy = new FakeModel(r => new ModelReply(Answer(r, item => Verdict(item, ["SOL"], "neutral", 2)), 10, 20_000, null, true));
var eager = Analyst(test.Db, clock, settings, TestKey, greedy);
await eager.RunOnceAsync(_none);
Assert.AreEqual(1.00005m, (await eager.SpendingAsync(_none)).Spent, "Never shown as less than it was.");
await NewsRulesTests.StoryAsync(test.Db, clock, specs["SOL"], "Solana story after the greedy service", clock.UtcNow, 2, 0.1, "CoinDesk", trusted: true);
Assert.AreEqual(0, (await eager.RunOnceAsync(_none)).Calls);
// A free model: the cap of zero stops every request that may cost something.
await settings.SetAsync(NewsModelSettings.DailyCap.Key, "0", "test", _none);
clock.Advance(TimeSpan.FromMinutes(1));