Files
Strata/engine/include/strata/spec/draft_source.hpp
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

63 lines
3.2 KiB
C++

// include/strata/spec/draft_source.hpp - a source of draft tokens beside the MTP layer (--lookup-chain).
//
// A source proposes how the context continues; the verify window checks the proposal, so a source can only change
// the speed, never the output. The engine asks its sources after the MTP has drafted: the context it passes ends
// with those MTP drafts (not yet verified), and whatever a source returns is appended to the same window.
//
// The built-in source is the request's own text (PromptLookupSource: the suffix drafter over prompt + output).
// Other sources (a retrieval store over a codebase or earlier conversations) register with
// extra_draft_sources() before the first request; the engine takes the proposal with the longest match.
#pragma once
#include "strata/spec/suffix_drafter.hpp"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>
namespace strata::spec {
class DraftSource {
public:
virtual ~DraftSource() = default;
virtual const char* name() const = 0;
/// A new request starts (its prompt follows through append()).
virtual void reset() {}
/// Committed context, in order: the request's prompt, then every token the verify windows accept.
virtual void append(const int32_t* tokens, size_t n) { (void) tokens; (void) n; }
/// Propose up to `max_k` tokens that follow the context. `tail` holds the context's last `n_tail` tokens (at most
/// 64); its last `n_pending` are drafts not yet verified (never passed to append()). Writes `out`, returns how
/// many (0 = nothing to propose) and sets `*match` to the number of trailing context tokens the proposal is based
/// on (its confidence: the engine requires --lookup-chain-min and prefers the longest).
virtual int propose(const int32_t* tail, int n_tail, int n_pending, int max_k, int32_t* out, int* match) = 0;
};
/// The request's own text: the suffix drafter (prompt lookup) behind the DraftSource interface.
class PromptLookupSource final : public DraftSource {
public:
explicit PromptLookupSource(SuffixDrafter& d) : d_(d) {}
const char* name() const override { return "prompt-lookup"; }
void reset() override { d_.reset(); }
void append(const int32_t* tokens, size_t n) override { d_.append(tokens, n); }
int propose(const int32_t* tail, int n_tail, int n_pending, int max_k, int32_t* out, int* match) override {
n_pending = n_pending < n_tail ? n_pending : n_tail;
const int k = d_.propose_after(tail + (n_tail - n_pending), n_pending, max_k, out);
*match = k > 0 ? d_.last_match() : 0;
return k;
}
private:
SuffixDrafter& d_;
};
/// Sources besides the request's own text, asked after it (empty unless something registered one at startup).
std::vector<std::unique_ptr<DraftSource>>& extra_draft_sources();
/// Ask `own` and then every extra source; the proposal with the longest match of at least `min_match` wins
/// (ties keep the earlier source). Returns its length (0 = none); `*match` and `*source` (index: 0 = own) describe it.
int propose_from_sources(DraftSource& own, const int32_t* tail, int n_tail, int n_pending, int max_k, int min_match,
int32_t* out, int* match, int* source);
} // namespace strata::spec