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
+50
View File
@@ -0,0 +1,50 @@
#pragma once
// The seam capture.offer's decision logic reads through, instead of touching store::* (or
// the wall clock) directly — the same reasoning as rpc::TaskActionPort: it lets a test
// substitute a fake that reports "the store took a long time just now" by advancing a
// shared fake clock, and assert that capture.offer's deadline check actually bails to
// `ignore` instead of pressing on, without a real sleep anywhere (deterministic, instant).
//
// vdm::rules::Rule (not velox::proto::Rule) on purpose: this is what
// vdm::rules::match_rules consumes directly, so the real implementation is the only place
// that ever converts the stored proto::Rule/JSON shape into CORE's plain vocabulary.
#include <chrono>
#include <cstdint>
#include <string>
#include <vector>
#include "vdm/rules/match.hpp"
namespace velox::daemon::rpc {
class CaptureDataSource {
public:
virtual ~CaptureDataSource() = default;
// Read at every checkpoint in the offer's decision pipeline; a fake can advance this
// however it likes (including "jump forward 2 real seconds, instantly") to simulate a
// slow step without ever calling sleep.
virtual std::chrono::steady_clock::time_point now() = 0;
virtual bool capture_enabled() = 0;
virtual std::vector<std::string> monitored_extensions() = 0;
virtual std::vector<std::string> monitored_mime_types() = 0;
virtual std::int64_t min_size_bytes() = 0;
virtual std::vector<std::string> excluded_hosts() = 0;
// Enabled rules, in priority order — ready for vdm::rules::match_rules as-is.
virtual std::vector<vdm::rules::Rule> enabled_rules() = 0;
// "resolve the category folder": a plain extension guess (store::Categories'
// guess_by_extension) when no rule named a category explicitly.
virtual std::string guess_category_id(const std::string& filename) = 0;
virtual std::string category_save_dir(const std::string& category_id) = 0;
virtual std::string default_save_dir() = 0;
// True if an active (non-terminal) task already targets this exact URL.
virtual bool has_active_duplicate(const std::string& url) = 0;
};
} // namespace velox::daemon::rpc