Files
vdm/daemon/src/rpc/dispatcher.hpp
T
samiandClaude Sonnet 5 6632b75099 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
2026-09-12 13:58:45 +04:00

140 lines
8.2 KiB
C++

#pragma once
// VeloxDispatcher implements the generated velox::proto::Dispatcher — one virtual per RPC
// method. The generated dispatch() does the envelope, the transport check and the param
// parse; a method here only ever sees a validated, typed params struct and returns a
// typed result.
//
// Scope of this drop (AGENT-DAEMON.md build order): the transports are real and the store
// is behind download.add / download.list / download.get. session.hello / session.pair /
// session.subscribe are handled in the server layer (connection- and transport-stateful)
// and never reach this class in the running daemon. Everything else still returns
// "not implemented in this build" (-> -32603) until its handler and the scheduler land;
// see daemon/docs/deferrals.md.
#include <functional>
#include "rpc/capture_data_source.hpp"
#include "rpc/event_hub.hpp"
#include "rpc/task_action_port.hpp"
#include "store/sqlite.hpp"
#include "velox_proto.hpp"
namespace velox::daemon::rpc {
class VeloxDispatcher final : public velox::proto::Dispatcher {
public:
// `actions` drives download.pause/resume/start/cancel and queue.start/stop
// immediately (D4b) — those cannot wait for the next tick(), unlike download.add's
// on_mutation nudge. Optional so existing tests that only exercise download.add/list/
// get keep building with no scheduler at hand; a null actions_ makes those methods
// answer "not implemented" instead of crashing. See rpc/task_action_port.hpp for why
// this is an interface owned by rpc/ rather than a direct sched::Scheduler* (avoids a
// veloxd_rpc <-> veloxd_sched circular library dependency).
VeloxDispatcher(velox::daemon::store::Db& db, EventHub& hub, TaskActionPort* actions = nullptr)
: db_(db), hub_(hub), actions_(actions) {}
// Called after a handler mutates task state (download.add for now). main.cpp wires it
// to nudge the scheduler; unset in tests.
void set_on_mutation(std::function<void()> fn) { on_mutation_ = std::move(fn); }
// Test-only seam: capture.offer normally builds its own real CaptureDataSource
// (wrapping db_) per call. A test that needs to simulate "the store is slow right
// now" (see rpc/capture_data_source.hpp) supplies one here instead; production code
// never calls this.
void set_capture_source_for_test(CaptureDataSource* src) { capture_source_for_test_ = src; }
velox::proto::HandlerResult<velox::proto::CaptureRules>
on_capture_getRules(const velox::proto::CaptureGetRulesParams&) override;
velox::proto::HandlerResult<velox::proto::CaptureOfferResult>
on_capture_offer(const velox::proto::CaptureOfferParams&) override;
velox::proto::HandlerResult<velox::proto::CategoryListResult>
on_category_list(const velox::proto::CategoryListParams&) override;
velox::proto::HandlerResult<velox::proto::CategoryRemoveResult>
on_category_remove(const velox::proto::CategoryRemoveParams&) override;
velox::proto::HandlerResult<velox::proto::CategoryUpsertResult>
on_category_upsert(const velox::proto::CategoryUpsertParams&) override;
velox::proto::HandlerResult<velox::proto::DownloadAddResult>
on_download_add(const velox::proto::DownloadSpec&) override;
velox::proto::HandlerResult<velox::proto::DownloadAddBatchResult>
on_download_addBatch(const velox::proto::DownloadAddBatchParams&) override;
velox::proto::HandlerResult<velox::proto::BulkTaskResult>
on_download_cancel(const velox::proto::DownloadCancelParams&) override;
velox::proto::HandlerResult<velox::proto::TaskDetail>
on_download_get(const velox::proto::DownloadGetParams&) override;
velox::proto::HandlerResult<velox::proto::DownloadListResult>
on_download_list(const velox::proto::DownloadListParams&) override;
velox::proto::HandlerResult<velox::proto::BulkTaskResult>
on_download_pause(const velox::proto::DownloadPauseParams&) override;
velox::proto::HandlerResult<velox::proto::DownloadProbeResult>
on_download_probe(const velox::proto::DownloadProbeParams&) override;
velox::proto::HandlerResult<velox::proto::DownloadProvideAuthResult>
on_download_provideAuth(const velox::proto::DownloadProvideAuthParams&) override;
velox::proto::HandlerResult<velox::proto::DownloadRefreshUrlResult>
on_download_refreshUrl(const velox::proto::DownloadRefreshUrlParams&) override;
velox::proto::HandlerResult<velox::proto::DownloadRemoveResult>
on_download_remove(const velox::proto::DownloadRemoveParams&) override;
velox::proto::HandlerResult<velox::proto::BulkTaskResult>
on_download_resume(const velox::proto::DownloadResumeParams&) override;
velox::proto::HandlerResult<velox::proto::BulkTaskResult>
on_download_start(const velox::proto::DownloadStartParams&) override;
velox::proto::HandlerResult<velox::proto::TaskSummary>
on_download_update(const velox::proto::DownloadUpdateParams&) override;
velox::proto::HandlerResult<velox::proto::GrabberHarvestResult>
on_grabber_harvest(const velox::proto::GrabberHarvestParams&) override;
velox::proto::HandlerResult<velox::proto::GrabberStartResult>
on_grabber_start(const velox::proto::GrabberStartParams&) override;
velox::proto::HandlerResult<velox::proto::GrabberStatusResult>
on_grabber_status(const velox::proto::GrabberStatusParams&) override;
velox::proto::HandlerResult<velox::proto::Limiter>
on_limiter_get(const velox::proto::LimiterGetParams&) override;
velox::proto::HandlerResult<velox::proto::Limiter> on_limiter_set(const velox::proto::Limiter&) override;
velox::proto::HandlerResult<velox::proto::MediaAddVariantResult>
on_media_addVariant(const velox::proto::MediaAddVariantParams&) override;
velox::proto::HandlerResult<velox::proto::MediaListVariantsResult>
on_media_listVariants(const velox::proto::MediaListVariantsParams&) override;
velox::proto::HandlerResult<velox::proto::QueueListResult>
on_queue_list(const velox::proto::QueueListParams&) override;
velox::proto::HandlerResult<velox::proto::QueueReorderResult>
on_queue_reorder(const velox::proto::QueueReorderParams&) override;
velox::proto::HandlerResult<velox::proto::QueueStartResult>
on_queue_start(const velox::proto::QueueStartParams&) override;
velox::proto::HandlerResult<velox::proto::QueueStopResult>
on_queue_stop(const velox::proto::QueueStopParams&) override;
velox::proto::HandlerResult<velox::proto::QueueUpsertResult>
on_queue_upsert(const velox::proto::QueueUpsertParams&) override;
velox::proto::HandlerResult<velox::proto::RulesListResult>
on_rules_list(const velox::proto::RulesListParams&) override;
velox::proto::HandlerResult<velox::proto::RulesUpsertResult>
on_rules_upsert(const velox::proto::RulesUpsertParams&) override;
velox::proto::HandlerResult<velox::proto::ScheduleGetResult>
on_schedule_get(const velox::proto::ScheduleGetParams&) override;
velox::proto::HandlerResult<velox::proto::ScheduleSetResult>
on_schedule_set(const velox::proto::ScheduleSetParams&) override;
velox::proto::HandlerResult<velox::proto::SessionHelloResult>
on_session_hello(const velox::proto::SessionHelloParams&) override;
velox::proto::HandlerResult<velox::proto::SessionPairResult>
on_session_pair(const velox::proto::SessionPairParams&) override;
velox::proto::HandlerResult<velox::proto::SessionSubscribeResult>
on_session_subscribe(const velox::proto::SessionSubscribeParams&) override;
velox::proto::HandlerResult<velox::proto::SettingsGetResult>
on_settings_get(const velox::proto::SettingsGetParams&) override;
velox::proto::HandlerResult<velox::proto::SettingsSetResult>
on_settings_set(const velox::proto::SettingsSetParams&) override;
private:
// The whole of download.add's body; on_download_add and on_download_addBatch (each
// item merged against DownloadAddBatchParams.defaults first) both call this — exactly
// one place turns a DownloadSpec into a stored, admitted task.
velox::proto::HandlerResult<velox::proto::DownloadAddResult> add_one(
const velox::proto::DownloadSpec& spec);
velox::daemon::store::Db& db_;
EventHub& hub_;
TaskActionPort* actions_;
std::function<void()> on_mutation_;
CaptureDataSource* capture_source_for_test_ = nullptr;
};
} // namespace velox::daemon::rpc