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:
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
#include <string>
|
||||
@@ -58,6 +60,22 @@ DbResult<std::optional<proto::Category>> Categories::get(std::string_view catego
|
||||
return std::optional<proto::Category>{project_row(*st)};
|
||||
}
|
||||
|
||||
std::string Categories::guess_by_extension(std::string_view filename_or_ext) {
|
||||
std::string ext(filename_or_ext);
|
||||
if (const auto dot = ext.find_last_of('.'); dot != std::string::npos) ext = ext.substr(dot + 1);
|
||||
if (ext.empty()) return "general";
|
||||
std::transform(ext.begin(), ext.end(), ext.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
|
||||
auto cats = list();
|
||||
if (!cats) return "general";
|
||||
for (const auto& c : *cats) {
|
||||
for (const auto& e : c.extensions)
|
||||
if (e == ext) return c.categoryId;
|
||||
}
|
||||
return "general";
|
||||
}
|
||||
|
||||
DbResult<proto::Category> Categories::upsert(proto::Category category) {
|
||||
// A replace keeps the existing row's builtin flag; a create is never builtin. Either
|
||||
// way the payload's own `builtin` is ignored — a client cannot mint or revoke it.
|
||||
|
||||
@@ -24,6 +24,14 @@ public:
|
||||
DbResult<std::vector<velox::proto::Category>> list();
|
||||
DbResult<std::optional<velox::proto::Category>> get(std::string_view category_id);
|
||||
|
||||
// Extension match against categories.extensions — not the real rules engine (no
|
||||
// host/mime/size clauses), just enough that a capture or a File Info preselect isn't
|
||||
// always "general". `filename_or_ext` may be a whole filename ("movie.mp4") or a bare
|
||||
// extension ("mp4", no leading dot); matched case-insensitively. "general" (this
|
||||
// project's always-present default category) on no match, an empty/dotless filename,
|
||||
// or a store error — this never fails outward, it just falls back.
|
||||
std::string guess_by_extension(std::string_view filename_or_ext);
|
||||
|
||||
// "Omit categoryId to create; supply it to replace" (category.upsert's own words) —
|
||||
// the caller (dispatcher) decides create vs replace by whether `category.categoryId`
|
||||
// is empty and generates the id; this just writes the row. `builtin` is never taken
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "store/rules.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace velox::daemon::store {
|
||||
|
||||
namespace proto = velox::proto;
|
||||
|
||||
namespace {
|
||||
|
||||
proto::Rule project_row(Stmt& st) {
|
||||
proto::Rule r;
|
||||
r.ruleId = st.column_text(0);
|
||||
if (!st.column_is_null(1)) r.name = st.column_text(1);
|
||||
r.enabled = st.column_int(2) != 0;
|
||||
r.priority = st.column_int(3);
|
||||
if (auto j = nlohmann::json::parse(st.column_text(4), nullptr, false); !j.is_discarded()) {
|
||||
if (auto m = proto::parse<proto::RuleMatch>(j, "match")) r.match = *m;
|
||||
}
|
||||
if (auto j = nlohmann::json::parse(st.column_text(5), nullptr, false); !j.is_discarded()) {
|
||||
if (auto a = proto::parse<proto::RuleAction>(j, "action")) r.action = *a;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string new_rule_id() {
|
||||
std::random_device rd;
|
||||
std::uniform_int_distribution<std::uint64_t> d;
|
||||
char buf[17];
|
||||
std::snprintf(buf, sizeof(buf), "%016llx", static_cast<unsigned long long>(d(rd)));
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DbResult<std::vector<proto::Rule>> Rules::list() {
|
||||
auto st = db_.prepare(
|
||||
"SELECT rule_id, name, enabled, priority, match, action FROM rules "
|
||||
"ORDER BY priority, rule_id");
|
||||
if (!st) return std::unexpected(st.error());
|
||||
|
||||
std::vector<proto::Rule> out;
|
||||
for (;;) {
|
||||
auto row = st->step();
|
||||
if (!row) return std::unexpected(row.error());
|
||||
if (!*row) break;
|
||||
out.push_back(project_row(*st));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
DbResult<std::vector<proto::Rule>> Rules::apply(std::vector<proto::Rule> upsert,
|
||||
const std::vector<std::string>& remove) {
|
||||
auto txn = db_.transaction([&]() -> DbResult<void> {
|
||||
for (const auto& id : remove) {
|
||||
auto del = db_.prepare("DELETE FROM rules WHERE rule_id = ?1");
|
||||
if (!del) return std::unexpected(del.error());
|
||||
if (auto b = del->bind(1, std::string_view(id)); !b) return std::unexpected(b.error());
|
||||
if (auto r = del->step(); !r) return std::unexpected(r.error());
|
||||
}
|
||||
for (auto& rule : upsert) {
|
||||
if (rule.ruleId.empty()) rule.ruleId = new_rule_id();
|
||||
|
||||
nlohmann::json match_json = rule.match;
|
||||
nlohmann::json action_json = rule.action;
|
||||
|
||||
auto st = db_.prepare(
|
||||
"INSERT INTO rules(rule_id, name, enabled, priority, match, action) "
|
||||
"VALUES(?1,?2,?3,?4,?5,?6) "
|
||||
"ON CONFLICT(rule_id) DO UPDATE SET "
|
||||
"name=excluded.name, enabled=excluded.enabled, priority=excluded.priority, "
|
||||
"match=excluded.match, action=excluded.action");
|
||||
if (!st) return std::unexpected(st.error());
|
||||
if (auto b = st->bind(1, std::string_view(rule.ruleId)); !b)
|
||||
return std::unexpected(b.error());
|
||||
if (auto r = rule.name ? st->bind(2, std::string_view(*rule.name)) : st->bind_null(2); !r)
|
||||
return std::unexpected(r.error());
|
||||
if (auto b = st->bind(3, static_cast<std::int64_t>(rule.enabled)); !b)
|
||||
return std::unexpected(b.error());
|
||||
if (auto b = st->bind(4, rule.priority); !b) return std::unexpected(b.error());
|
||||
if (auto b = st->bind(5, std::string_view(match_json.dump())); !b)
|
||||
return std::unexpected(b.error());
|
||||
if (auto b = st->bind(6, std::string_view(action_json.dump())); !b)
|
||||
return std::unexpected(b.error());
|
||||
if (auto r = st->step(); !r) return std::unexpected(r.error());
|
||||
}
|
||||
return {};
|
||||
});
|
||||
if (!txn) return std::unexpected(txn.error());
|
||||
return list();
|
||||
}
|
||||
|
||||
} // namespace velox::daemon::store
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
// Read/write access to the `rules` table (rules.list / rules.upsert / capture.offer's own
|
||||
// read path). match/action are stored as their generated-JSON text (velox::proto::RuleMatch
|
||||
// / RuleAction), so no bespoke schema lives here beyond the table's own columns.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "store/sqlite.hpp"
|
||||
#include "velox_proto.hpp"
|
||||
|
||||
namespace velox::daemon::store {
|
||||
|
||||
class Rules {
|
||||
public:
|
||||
explicit Rules(Db& db) : db_(db) {}
|
||||
|
||||
// Enabled and disabled rules alike, in priority order (ties broken by rule_id) —
|
||||
// rules.list's own contract ("the rules engine's table, in priority order"); capture
|
||||
// offer's own caller filters to enabled ones itself, same as vdm::rules::match_rules
|
||||
// already does internally.
|
||||
DbResult<std::vector<velox::proto::Rule>> list();
|
||||
|
||||
// rules.upsert: "upsert carries the rules to store and remove the ruleIds to drop;
|
||||
// applying both at once means a reprioritisation never leaves the table in a
|
||||
// half-valid state" (the schema's own words) — one transaction. An empty ruleId in
|
||||
// `upsert` generates one (create); a non-empty one replaces. Returns the full table
|
||||
// after the write, in priority order.
|
||||
DbResult<std::vector<velox::proto::Rule>> apply(std::vector<velox::proto::Rule> upsert,
|
||||
const std::vector<std::string>& remove);
|
||||
|
||||
private:
|
||||
Db& db_;
|
||||
};
|
||||
|
||||
} // namespace velox::daemon::store
|
||||
@@ -318,6 +318,17 @@ DbResult<std::int64_t> Tasks::count() {
|
||||
return (*row) ? st->column_int(0) : 0;
|
||||
}
|
||||
|
||||
DbResult<bool> Tasks::has_active_duplicate(std::string_view url) {
|
||||
auto st = db_.prepare(
|
||||
"SELECT 1 FROM tasks WHERE url = ?1 "
|
||||
"AND state NOT IN ('complete','failed','cancelled') LIMIT 1");
|
||||
if (!st) return std::unexpected(st.error());
|
||||
if (auto b = st->bind(1, url); !b) return std::unexpected(b.error());
|
||||
auto row = st->step();
|
||||
if (!row) return std::unexpected(row.error());
|
||||
return *row;
|
||||
}
|
||||
|
||||
proto::TaskSummary to_summary(const TaskRow& r) {
|
||||
proto::TaskSummary s;
|
||||
s.taskId = r.task_id;
|
||||
|
||||
@@ -79,6 +79,12 @@ public:
|
||||
DbResult<bool> remove(std::string_view task_id);
|
||||
DbResult<std::int64_t> count();
|
||||
|
||||
// capture.offer's dedupe check: true if a non-terminal task already targets this exact
|
||||
// URL (the same rule download.add itself does not enforce — a deliberate re-add is
|
||||
// allowed there; capture is the automatic path where re-grabbing an in-flight download
|
||||
// is almost always a mistake, e.g. two tabs triggering the same link).
|
||||
DbResult<bool> has_active_duplicate(std::string_view url);
|
||||
|
||||
// Byte-counter update from an engine progress tick — cheaper than a full row rewrite,
|
||||
// and keeps download.list / download.get current between state transitions.
|
||||
DbResult<bool> update_progress(std::string_view task_id, std::int64_t downloaded_bytes,
|
||||
|
||||
Reference in New Issue
Block a user