Files
Open_Ultra_SAST/.kiro/specs/dspy-driver/brief.md
T
Marius CiepluchandClaude Fable 5.2 b38dcdbc60 spec: the scope is cut to a safety net that produces evidence; the plane is retired
The maintainer's second decision of 2026-11-30. The requirements of dspy-driver are
approved, with a memory amendment: a cache in stage one, a case memory and a curator
that prunes as later stages. The kube and AX plane is retired and its removal is
listed as steps C1 to C5 of the roadmap. search-with-proof is deferred to a follow-up
spec in Docker on a dedicated runner, opened once the driver's baseline is read.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-30 09:59:56 +01:00

196 lines
15 KiB
Markdown

# Brief: the DSPy driver
Opened 2026-11-30 on the maintainer's words: "I would like to approach this empircally as a focus, and
measure the driver by example. I would like to focus on a dspy strategy and map out this driving soldily
because it is the core of the engine. can we move this into an extra spec?"
This spec is line 1 of the roadmap of 2026-11-30 (`.kiro/steering/roadmap.md`, top section). It takes the
open work of `hunter-learning-loop` (tasks 4.1, 6 and the group 7 added that day) and owns the driver from
here. `hunter-learning-loop` keeps what it delivered: the feature table, the paired ablation, the proposer
and the acceptance rule. This spec builds on that code and does not rebuild it.
## Problem
- **Who has it.** The maintainer, and after them a developer who pushes AI-written web application code.
- **Today.** The driver is the model-driven hunter, a DSPy program (`src/openultrasast/hunt/program.py`).
It was measured in October with inputs that describe the shape of the code around a change, and those
inputs could not decide whether a change is unsafe. The project then built a deterministic prover and
kept Joern, and never connected either to the driver. Read on 2026-11-30: no module under `hunt/`,
`learn/` or the hunter tools imports `cpg`, and the prover's trace is not among the driver's inputs. So
the core of the engine, a model that drives the search with what abstract interpretation establishes,
does not exist as a running thing. Nobody has decided, on evidence, how the DSPy program should be built
either: one call or a tool loop, how many samples, which optimiser, on what examples.
- **What should change.** The driver is mapped out as a DSPy strategy, wired to the abstract
interpretation, and every choice in that strategy is made by an experiment on named examples. The
maintainer can open any example and see what the driver was given, what it answered, and why it missed.
## What exists (read in the code on 2026-11-30, not run)
| Part | Where | State |
| --- | --- | --- |
| Program | `hunt/program.py`: `HuntProgram`, a `dspy.Module`; a `dspy.ReAct` loop when tools are bound, else `dspy.Predict` | built |
| Signature | `signature_for(mask)`: nine text inputs, one output `findings: list[Finding]` (path, line, family, confidence, rationale, cited lines) | built |
| Inputs | change summary, hunks, enclosing function, callers, entry points, roles, def-use, a function-local obligations check, the family list | built; shape features |
| Tools | `read_file`, `grep_repo`, `find_refs`, clamped to the repository | built |
| Model | `build_lm`: DeepSeek through `dspy.LM`, disk cache, fixed temperature | built |
| Runner | `hunt/runner.py`: one record per change, input digest, spend ledger, `InstrumentFailure` | built |
| Metric | `hunt/metric.py`: `search_outcome` (a finding within 10 lines of the labelled location) and `search_metric` | built |
| Ablation | `hunt/ablation.py`: paired arms over the same changes, repository-clustered bootstrap | built |
| Proposer | `hunt/propose.py`: GEPA, MIPROv2 or BootstrapFewShot, one change per round, on compile folds | built |
| Acceptance | `hunt/accept.py`: compile and held-out folds both non-negative, one strictly positive | built |
| Probe | `core/probes.py`: reversed-surface probes against shortcuts | built |
| Prover trace | `hunt/prover.py` over `semantic/` (tree-sitter, control-flow graph, dominance, abstract domain, path composition); access control | built; **not an input of the driver** |
| Joern | `cpg/`, used by `model/taint.py`, `model/dominance.py` and `push/` | built; **not reachable from the driver** |
| Sampling | one answer per question | **missing**: no repeated sampling, no agreement score |
| Examples | pool manifests and a feature table | **missing**: no small set a person can read end to end |
## The driving, mapped
One pushed change goes through these stages. Each stage names the choice that is open and the experiment
that settles it.
1. **The question.** One change, and per changed function one question: does this change introduce a
weakness of family F that an attacker can reach? Open: one question per change or one per changed
function and family (E4).
2. **What abstract interpretation says.** Two producers, each proving it read nonzero input.
- The prover's ordered trace: the entry, the request-derived arguments with their provenance, the
protected operation, the guards and whether one dominates. Family: access control.
- Joern's results for the changed functions: the taint path from a request source to a sink with its
steps, the sanitizers on it, the dominance answer. Family first: path traversal.
- A location with no result says why (no entry recognised, operation not recognised, unresolved call,
engine did not start). An empty annotation and a failed one are different records.
Open: whether the results are handed over up front, or the driver asks for them through a tool (E6).
3. **The signature.** Inputs: the change, the abstract interpretation for it, the family definition.
Output: suspects, each with a location, a family, the step of the trace it rests on, and a rationale.
Open: one signature for all families or one per family; one step or two (locate, then judge) (E4).
4. **The module.** `dspy.Predict`, `dspy.ChainOfThought` or the `dspy.ReAct` tool loop. Open: whether the
tool loop still earns its cost once the trace is in the input (E2).
5. **The probabilistic loop.** The question is asked k times. A suspect's score is the share of samples
that name it. The score orders suspects; it establishes nothing. Open: k, the temperature, and whether
agreement separates true suspects from false ones at all (E3).
6. **Compilation.** Instructions and demonstrations chosen by a DSPy optimiser on compile folds of
training repositories, accepted by the existing rule, and passed through the reversed-surface probe.
Open: instructions only (GEPA with the metric's feedback) against demonstrations (bootstrap), and
whether either beats the uncompiled program on held-out folds (E5).
7. **The output.** Ordered suspects go to the proof arm as starting intents (`search-with-proof` 7.1) and
to the hook as advisory lines (`pre-push-safety-net` 18.1). The driver's opinion never becomes
evidence: a finding needs the trace's own rule or a demonstration.
## Method: measure by example
- **The example book.** A small set of real changes that a person can read end to end, in the chosen
scope: access control and path traversal, in AI-written web applications (Express/TypeScript,
FastAPI/Python). To start: ten vulnerable changes, five per family, each with its fixed side, and
twenty ordinary changes from the same repositories. Each is a `dspy.Example` with the labelled
location and family. The book is development data: it is open, it is looked at, and nothing in it is
ever an admission case.
- **One page per example.** What the producers gave the driver (with byte counts), the k answers, the
agreement score, hit or miss, and for a miss one cause from a closed list: no annotation; annotation
wrong; annotation right and ignored; wrong family; wrong location; ordinary change flagged.
- **One experiment at a time.** An experiment names its lever, changes one thing, runs the same examples
on both arms, and reports the paired difference with the per-example table beside it. Its manifest is
fixed before it runs. This is rule 12 of `detection-quality.md`.
- **Three sets, never mixed.** The example book (read freely), the training set with its compile and
held-out folds (teaches and selects), and the admission populations (untouched, read once).
- **Two numbers and the table.** Search recall on vulnerable changes and suspicions per ordinary push,
with counts and intervals, and top-K recall per push (K = 1, 3, 5). At book size the intervals are wide:
the book shows mechanisms and causes, the training set gives the numbers.
## Experiments, in order
| Id | Question | Arms | Needs |
| --- | --- | --- | --- |
| E0 | Do the producers deliver? | share of examples with a nonempty annotation, per family and producer | D1, D2, D3; no model call |
| E1 | Does abstract interpretation help the driver? | no annotation; prover trace; Joern results | E0 |
| E2 | Does the tool loop earn its cost? | Predict; ChainOfThought; ReAct | E1 |
| E3 | Does agreement mean anything? | k = 1, 3, 5; score against hit or miss | E1 |
| E4 | How should the question be cut? | one signature; one per family; locate then judge | E2 |
| E5 | Does compilation generalise? | uncompiled; GEPA instructions; bootstrap demonstrations; on held-out folds | D6, E4 |
| E6 | Up front or on demand? | annotation in the input; annotation behind a tool | E2 |
E0 costs nothing and comes first. E1 is the baseline of the roadmap's step N1 and has the maintainer's
ceiling of 10 USD. Every later experiment needs its own ceiling.
## Carried work
Input for the requirements and task phases. The ids are this brief's.
| Id | Work | From |
| --- | --- | --- |
| D1 | The example book: the thirty changes, their labels, the page format, the closed list of miss causes | new |
| D2 | The prover's trace as an annotation of `HuntProgram` | `hunter-learning-loop` 7.1a; `reachability-flow-model` 4.1 |
| D3 | Joern's results as an annotation: one run per revision, cached, in the shipped image, engine exit code on record | `hunter-learning-loop` 7.1b |
| D4 | The baseline in three arms (E1) | `hunter-learning-loop` 7.2 |
| D5 | Repeated sampling, the agreement score, top-K recall per push | `hunter-learning-loop` 7.3; `learned-decision-engine` 17.1, 17.2 |
| D6 | The training set apart from every admission population, with vibe-code pairs, one as a whole repository | `hunter-learning-loop` 4.1; `contributor-scan` 5.2 |
| D7 | Compilation on training repositories, the acceptance rule, the reversed-surface probe | `hunter-learning-loop` 7.4; `learned-decision-engine` 16 |
| D8 | The driver gives an opinion on intent and raises no evidence | `hunter-learning-loop` 7.5; `authorization-obligations` 3.3 |
| D9 | The handoff of the top suspects to the proof arm | `hunter-learning-loop` 7.6 |
| D10 | One read of an accepted program on a held-out pilot, at most once per program | `hunter-learning-loop` 6 |
## Memory for the driver (added 2026-11-30)
The maintainer: "we have a rustfs memory. how should dspy use it. memory is key to save cycles, and it can
improve. but we need a memory pruning and memory agent approach here. dspy has this, but it's missing".
Read on 2026-11-30:
- The store exists: `src/openultrasast/plane/memory.py`, a file backend and an S3 backend tested on RustFS,
rows per repository and pin, blobs by content (`excerpts/`, `embeddings/<model>/`, `responses/`,
`programs/`). `learn/` uses it (7 files). The driver in `hunt/` does not: it has only DSPy's local disk
cache. The store sits in the plane package, which is retired; it moves out first (roadmap step C1).
- DSPy 3.4.1 as installed exports `configure_cache`, `History`, `Embedder`, `Embeddings`, `KNN`,
`KNNFewShot`, `Retrieve` and tools for `ReAct`. It has no long-term memory, no pruning and no memory
agent. Those are ours to build.
Three stages. Each is entered by a result, as Requirement 7 demands; only M1 belongs to stage one.
| Stage | What the driver gets | Saves or improves | How it is measured |
| --- | --- | --- | --- |
| M1 Cache | Joern's results keyed by repository, revision, function digest, engine and query version; model answers keyed by the request digest; both in the store, so a second machine or a later session reuses them | saves cycles; changes no answer | run E0 and E1 twice: the second run starts no engine, makes no model call, and gives the same pages |
| M2 Case memory | every read example with a verified label becomes a case: its annotation, family, outcome and miss cause. Before answering, the driver retrieves the nearest cases and sees them as demonstrations | may improve | one experiment: with and without retrieval, on repositories the memory holds no case from |
| M3 Curator | a step that runs after an experiment, never while the driver answers: it writes cases, merges duplicates, scores each case by what happened when it was retrieved, and prunes | keeps M2 from decaying | one experiment: pruned against unpruned memory on the same held-out examples; pruning is a tombstone first, so it can be undone |
Rules that hold from M1 on:
- **Only verified labels enter the case memory:** a label from a fix or, later, from a demonstration. The
driver's own unverified answer is never stored as a case, or the memory learns the driver's mistakes.
- **Retrieval is held out by repository:** a question never retrieves a case from its own repository, and
no case comes from an admission or evaluation population.
- **A key carries its versions.** A cached result is void when the code, the engine, the query or the
program changed. That is the first pruning rule, and it needs no judgment.
- **Pruning reasons are a closed list:** version changed; duplicate of a kept case; contradicted by a later
verified label; retrieved often and unhelpful; over the cap for its family and framework.
- **The memory agent is a DSPy module of its own**, the curator of M3. Whether the driver also asks the
memory through a tool while it works, or receives the cases up front, is experiment E6's question.
Carried work added: **D11** the cache of M1 through the store (with D3); **D12** the case memory (M2);
**D13** the curator and pruning (M3). D12 and D13 have no criteria until E1 is read.
## Scope
- **In.** The driver's inputs, signature, module, sampling, compilation and output; the two producers; the
example book; the experiments above.
- **Out.** New engine queries or facts (they go to `engine-change-scoped-analysis`, and only when an
experiment names the missing fact). The proof arm. The hook's output and its deadline. Benchmark
machinery. Families other than the two chosen, until E1 has been read.
## Constraints
- Admission and evaluation populations never train and are never read during this work.
- Software shape features alone decide nothing; a compiled program passes the reversed-surface probe.
- A zero proves its instrument: every producer and every run records the bytes it read and the exit codes.
- Joern runs in the shipped image, once per revision, offline for these runs. Its start-up time is recorded
and does not gate an experiment.
- Paid runs have a ceiling set by the maintainer before they start. Both API keys are unset for anything
that is not a paid run.
- Standard metrics, plain English, ASCII in written files.
## Open questions for the requirements phase
- Which thirty changes go into the example book, and from which repositories. The maintainer may want to
name some: the bugs they want to avoid.
- Whether the driver stays on DeepSeek for every experiment, or a second model is an arm.
- Whether the driver's records adopt the core's model contract (`hunt/model_contract.py`) from the start.
- What "found" means for path traversal: the sink line, the unsanitised step, or either.