#include "ctxstream.hpp" #include #include #include #include #include #include namespace ctxstream { // The extraction contract. Kept short and mechanical on purpose: a small model // asked for prose produces prose that no parser can trust, and prose is exactly // what broke the aggregation in the Python harness this replaces. static const char* kExtractSystem = "You extract structured data from one fragment of a larger corpus.\n" "Rules:\n" "1. Output ONLY lines of the form . Nothing else.\n" "2. No preamble, no explanation, no markdown, no totals.\n" "3. Count only what is present in THIS fragment. Do not guess about the rest.\n" "4. If nothing matches, output exactly: NONE"; std::vector stream(const std::string& text, const std::vector& segments, const std::string& extract_prompt, const BackendOptions& backend, const PipelineOptions& pipe) { std::vector results(segments.size()); if (segments.empty()) return results; std::atomic next{0}; std::atomic done{0}, failed{0}; std::mutex log_mu; const auto t0 = std::chrono::steady_clock::now(); // "Buffer depth" in the video sense: this many segments decoding at once. const int workers = std::max(1, std::min(pipe.concurrency, static_cast(segments.size()))); auto worker = [&] { for (;;) { const std::size_t i = next.fetch_add(1); if (i >= segments.size()) return; const Segment& seg = segments[i]; const std::string body = segment_text(text, seg); const std::string prompt = extract_prompt + "\n\n--- FRAGMENT " + std::to_string(seg.index + 1) + " of " + std::to_string(segments.size()) + " ---\n" + body; SegmentResult r; r.index = seg.index; const auto s0 = std::chrono::steady_clock::now(); for (int attempt = 0; attempt <= pipe.max_retries; ++attempt) { const Completion c = complete(backend, kExtractSystem, prompt); if (!c.ok) { r.error = c.error; continue; } r.prompt_tokens = c.prompt_tokens; r.output_tokens = c.output_tokens; // A server that clipped the prompt will answer confidently from // the part it kept. Never accept that as a segment result. if (looks_truncated(prompt.size(), c.prompt_tokens)) { r.truncated = true; r.error = "server processed " + std::to_string(c.prompt_tokens) + " tokens of a " + std::to_string(prompt.size()) + "-char prompt"; break; } r.raw = c.text; r.ok = true; r.error.clear(); break; } r.seconds = std::chrono::duration( std::chrono::steady_clock::now() - s0).count(); results[i] = r; const int d = ++done; if (!r.ok) ++failed; if (pipe.verbose) { std::lock_guard lk(log_mu); const double elapsed = std::chrono::duration( std::chrono::steady_clock::now() - t0).count(); const double eta = d ? elapsed / d * (segments.size() - d) : 0.0; // Failures first: a climbing error count must be visible within // seconds, not discovered after the run. std::fprintf(stderr, "[ctxstream] failed=%d %d/%zu seg=%d %.1fs eta=%.1fm\n", failed.load(), d, segments.size(), r.index, r.seconds, eta / 60.0); } } }; std::vector pool; pool.reserve(workers); for (int i = 0; i < workers; ++i) pool.emplace_back(worker); for (auto& t : pool) t.join(); if (pipe.verbose && failed) { std::fprintf(stderr, "[ctxstream] WARNING: %d/%zu segments failed\n", failed.load(), segments.size()); } return results; } } // namespace ctxstream