daemon: capture.offer for real (D7/D8) — rules, category resolution, dedupe, deadline

capture.offer applies capture.enabled/excludedHosts/monitoredExtensions/
monitoredMimeTypes/minSizeBytes from settings, then the rules table (new
store::Rules + CORE's vdm::rules::match_rules/glob_match — DAEMON only converts its
own stored proto::Rule JSON into CORE's plain vocabulary, per that header's own
layering note), resolves the category folder (a rule's explicit categoryId/saveDir,
else store::Categories::guess_by_extension — the same extension guess
download.probe's suggestedCategoryId already used, now shared instead of
duplicated), dedupes against active (non-terminal) tasks by exact URL, and on `take`
calls add_one() — the same path download.add itself uses, so a captured download is
a real, admitted, persisted task, not a special case.

The 750ms deadline (CLAUDE.md §4 / AGENT-DAEMON.md build step 6) is checked
cooperatively between every step via a new rpc::CaptureDataSource seam: the real
implementation wraps store::Settings/Categories/Rules/Tasks; a test fake can jump its
own injected clock forward to simulate "the store was slow just now" with zero real
sleep. This catches the realistic failure mode (several slow steps adding up) though
it cannot preempt a single pathologically stuck call mid-flight — a true preemptive
guarantee would need the same async/background-thread treatment as download.probe,
which isn't safe to do against the same sqlite3 connection (opened SQLITE_OPEN_NOMUTEX,
explicitly not for concurrent use) without a second connection; left as a known,
documented limit of this pass rather than adding that plumbing speculatively.

capture.getRules returns the same settings-backed fields capture.offer itself reads,
so the two can never drift. rulesVersion is a placeholder constant (1) — no persisted
revision counter exists yet, and the extension already re-fetches on
event.settings.changed regardless.

New store/rules.{hpp,cpp}: rules table CRUD (list, and an atomic upsert+remove for
rules.upsert later). store::Categories::guess_by_extension replaces a duplicate copy
that used to live in sched/scheduler.cpp. store::Tasks::has_active_duplicate for the
dedupe check.

Verified against real veloxd + tools/testserver: a monitored-type offer answers in
~5ms and actually creates + downloads the task, correctly categorized; an
unmonitored type, an excluded host, a rule-vetoed host, and a second offer for a
still-active URL all answer ignore with the right reason; a bad category save dir
surfaces its real -32011. New capture_offer_test covers all of the above plus the
deadline itself (two cases, one per "slow" checkpoint), asserting real wall-clock
time barely moves even though the fake clock jumped 2 simulated seconds. Full ctest:
54/54 (excluding the pre-existing, unrelated conformance failure noted in the
previous commit).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GRDjHGgpYmMoPE2UFbe7pP
This commit is contained in:
2026-09-12 13:58:45 +04:00
co-authored by Claude Sonnet 5
parent e0edf7a084
commit 6632b75099
14 changed files with 696 additions and 31 deletions
+1 -25
View File
@@ -580,30 +580,6 @@ bool Scheduler::provide_auth(const std::string& wire_id, const std::string& user
return true;
}
namespace {
// Extension match against the categories table (categories.extensions, per category.list),
// the same table download.add would consult once rules.* actually exists (D3). Not the
// real rules engine — no host/mime/size clauses — just enough that the File Info dialog's
// preselect isn't always "general".
std::string guess_category_id(store::Db& db, const std::string& filename) {
const auto dot = filename.find_last_of('.');
if (dot == std::string::npos || dot + 1 >= filename.size()) return "general";
std::string ext = filename.substr(dot + 1);
std::transform(ext.begin(), ext.end(), ext.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
store::Categories categories(db);
auto cats = categories.list();
if (!cats) return "general";
for (const auto& c : *cats) {
for (const auto& e : c.extensions)
if (e == ext) return c.categoryId;
}
return "general";
}
} // namespace
void Scheduler::probe_now(
const proto::DownloadProbeParams& params,
@@ -635,7 +611,7 @@ void Scheduler::probe_now(
r.mime = pr->mime;
r.resumable = pr->resumable;
r.effectiveUrl = pr->effective_url.empty() ? params.url : pr->effective_url;
r.suggestedCategoryId = guess_category_id(db_, r.filename);
r.suggestedCategoryId = store::Categories(db_).guess_by_extension(r.filename);
if (!pr->etag.empty()) r.etag = pr->etag;
if (!pr->last_modified.empty()) r.lastModified = pr->last_modified;
r.acceptRanges = pr->accept_ranges;