Files
vdm/daemon/src/store/tasks.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

121 lines
4.9 KiB
C++

#pragma once
// Read/write access to the `tasks` table, plus the projection onto the wire TaskSummary.
// download.list does its filtering, sorting and paging here (M1 DoD: a 1000-row list under
// 50 ms, never materialised client-side).
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
#include "store/sqlite.hpp"
#include "velox_proto.hpp"
namespace velox::daemon::store {
// One row of `tasks`, 1:1 with the schema. std::optional maps a NULL column.
struct TaskRow {
std::string task_id;
std::string url;
std::string save_dir;
std::string filename;
std::string state = "new";
std::string start_mode = "now"; // contract StartMode: 'now'|'later'|'queue'
std::string created_at;
std::optional<std::string> effective_url;
std::optional<std::string> category_id;
std::optional<std::string> queue_id;
std::optional<std::string> description;
std::optional<std::string> pause_reason;
std::optional<std::string> etag;
std::optional<std::string> last_modified;
std::optional<std::string> content_type;
std::optional<std::string> last_try_at;
std::optional<std::string> completed_at;
std::optional<std::string> checksum_algo;
std::optional<std::string> checksum_value;
std::optional<std::int64_t> size_bytes;
std::int64_t downloaded_bytes = 0;
std::int64_t speed_bps = 0;
bool resumable = false;
std::optional<std::int64_t> req_segments;
std::int64_t eff_segments = 0;
std::optional<std::int64_t> req_buffer_bytes;
std::optional<std::int64_t> eff_buffer_bytes;
std::optional<std::int64_t> queue_position;
std::optional<std::string> error_code;
std::optional<std::string> error_message;
std::optional<std::int64_t> error_http_status;
std::optional<bool> error_retryable;
std::optional<std::int64_t> error_attempt;
std::optional<std::string> error_next_retry_at;
};
class Tasks {
public:
explicit Tasks(Db& db) : db_(db) {}
DbResult<void> insert(const TaskRow& row);
DbResult<std::optional<TaskRow>> get(std::string_view task_id);
struct Page {
std::int64_t total = 0; // rows matching the filter, ignoring paging
std::vector<TaskRow> rows;
};
DbResult<Page> list(const std::optional<velox::proto::TaskFilter>& filter,
const std::optional<velox::proto::TaskSort>& sort, std::int64_t offset,
std::int64_t limit);
// Move a task to `state`; `pause_reason` is written only when state == "paused"
// (cleared otherwise). Returns false if there is no such task.
DbResult<bool> set_state(std::string_view task_id, std::string_view state,
const std::optional<std::string>& pause_reason);
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,
std::int64_t speed_bps, std::int64_t eff_segments,
std::int64_t eff_buffer_bytes);
// What the probe learned, persisted before start() so a task that completes before any
// progress tick still reports a real sizeBytes / resumable (not the pre-probe default).
struct ProbeFields {
std::optional<std::int64_t> size_bytes;
bool resumable = false;
std::optional<std::string> etag;
std::optional<std::string> last_modified;
std::optional<std::string> content_type;
std::optional<std::string> effective_url;
};
DbResult<bool> set_probe_result(std::string_view task_id, const ProbeFields& fields);
// on_finished's byte count, for a task that completes before any progress tick ever
// ran (see AGENT-DAEMON review: the bug this closes). size_bytes is only filled in if
// still unset — the probe's total_size is the more authoritative source when both
// exist and happen to disagree (a chunked source with no declared length, say).
DbResult<bool> set_final_bytes(std::string_view task_id, std::int64_t bytes);
private:
Db& db_;
};
// Project a row onto the wire type. `state` and `error.code` strings are assumed valid
// (the CHECK constraints and the state machine keep them so).
velox::proto::TaskSummary to_summary(const TaskRow& row);
} // namespace velox::daemon::store