Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,34 @@ The simulation is a bounded model, not a production digital twin. Real payment
providers, adversarial security coverage, database transactions, model changes,
and live network failure behavior have not been qualified.

### Planner request schemas

`shadow-plan` accepts schema 1 with its existing string keys and response fields.
Schema 2 keeps the same request shape and adds explicit keys in `symbols` and
test `dependencies`. Use `function:<id>`, `file:<path>`, `directory:<path>`,
`closure:<id>`, `module:<path>`, `session`, or `unobserved`. `directory:`
represents the repository root. Use unique map keys and nonblank fingerprints
in both inventories, including the shared fingerprint. Unknown key kinds or
blank fingerprints reject a schema 2 request.

Schema 2 can reuse existing evidence when a function key is added. Changes to
the values of existing keys and removals of function, file or directory keys
select the tests that depended on those keys. A removed module, other key-set
changes, missing or empty evidence, or a changed shared fingerprint expand
the plan. A failed test or a receipt with empty or unknown dependencies is
always selected. Collectors must fingerprint binding, discovery and runtime
changes that can affect previous tests, even when a function addition itself
is harmless. To detect unobserved module removals, include the full module
inventory independently of observed test dependencies.

An expanded schema 2 result includes one `expand_reason` code in priority
order: `no_baseline`, `shared_input_changed`, `removed_module`,
`inventory_changed`, or `empty_baseline`. Successful bounded plans omit
`expand_reason`. Callers must run the full current inventory on expansion,
including tests absent from the baseline. They must check current test
collection before using any bounded plan. The bundled shadow adapter still
uses schema 1.

### Large shadow plans

The Python adapter automatically partitions baseline test receipts by encoded byte
Expand Down
130 changes: 117 additions & 13 deletions src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,131 @@ pub struct Request {
#[derive(Debug, Serialize)]
pub struct Plan {
pub expand: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub expand_reason: Option<ExpandReason>,
pub selected: Vec<String>,
pub reasons: BTreeMap<String, String>,
}

#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ExpandReason {
NoBaseline,
EmptyBaseline,
SharedInputChanged,
RemovedModule,
InventoryChanged,
}

#[derive(PartialEq, Eq)]
enum InputKind {
Function,
File,
Directory,
Closure,
Module,
Session,
Unobserved,
}

fn input_kind(key: &str) -> Option<InputKind> {
use InputKind::*;
match key.split_once(':') {
Some(("function", name)) if !name.is_empty() => Some(Function),
Some(("file", path)) if !path.is_empty() => Some(File),
Some(("directory", _)) => Some(Directory),
Some(("closure", name)) if !name.is_empty() => Some(Closure),
Some(("module", path)) if !path.is_empty() => Some(Module),
None if key == "session" => Some(Session),
None if key == "unobserved" => Some(Unobserved),
_ => None,
}
}

fn inventory_expansion(request: &Request, old: &Baseline) -> Option<ExpandReason> {
use ExpandReason::*;
if request.schema == 1 {
return old
.symbols
.keys()
.ne(request.symbols.keys())
.then_some(InventoryChanged);
}

let mut inventory_changed = old.symbols.is_empty() || request.symbols.is_empty();
for key in old
.symbols
.keys()
.filter(|key| !request.symbols.contains_key(*key))
{
match input_kind(key) {
Some(InputKind::Module) => return Some(RemovedModule),
Some(InputKind::Function | InputKind::File | InputKind::Directory) => {}
_ => inventory_changed = true,
}
}
if inventory_changed
|| request.symbols.keys().any(|key| {
input_kind(key) != Some(InputKind::Function) && !old.symbols.contains_key(key)
})
{
return Some(InventoryChanged);
}
None
}

pub fn plan(request: Request) -> Result<Plan> {
if request.schema != 1 || request.symbols.is_empty() {
if !matches!(request.schema, 1 | 2) || (request.schema == 1 && request.symbols.is_empty()) {
return Err("invalid shadow request schema or empty symbol inventory".into());
}
if request.schema == 2 {
if request.shared.trim().is_empty()
|| request
.baseline
.iter()
.any(|old| old.shared.trim().is_empty())
{
return Err("missing shared fingerprint in shadow request".into());
}
for (key, fingerprint) in request
.symbols
.iter()
.chain(request.baseline.iter().flat_map(|old| old.symbols.iter()))
{
if input_kind(key).is_none() {
return Err("invalid typed input key in shadow request".into());
}
if fingerprint.trim().is_empty() {
return Err("missing typed input fingerprint in shadow request".into());
}
}
}

use ExpandReason::*;
let expansion = match &request.baseline {
None => Some(NoBaseline),
Some(old) if old.shared != request.shared => Some(SharedInputChanged),
Some(old) => inventory_expansion(&request, old)
.or_else(|| old.tests.is_empty().then_some(EmptyBaseline)),
};
let mut result = Plan {
expand: true,
expand: expansion.is_some(),
expand_reason: if request.schema == 2 { expansion } else { None },
selected: Vec::new(),
reasons: BTreeMap::new(),
};
let Some(old) = request.baseline else {
return Ok(result);
};
result.expand = old.shared != request.shared
|| old.symbols.keys().ne(request.symbols.keys())
|| old.tests.is_empty();
let changed: BTreeSet<_> = request
.symbols
.iter()
.filter(|(key, value)| old.symbols.get(*key) != Some(value))
.map(|(key, _)| key)
.collect();
let changed: BTreeSet<_> = if result.expand {
BTreeSet::new()
} else {
old.symbols
.iter()
.filter(|(key, value)| request.symbols.get(*key) != Some(value))
.map(|(key, _)| key)
.collect()
};
for (id, evidence) in old.tests {
let reason = if result.expand {
Some("shared input or inventory changed")
Expand All @@ -63,11 +163,15 @@ pub fn plan(request: Request) -> Result<Plan> {
|| evidence
.dependencies
.iter()
.any(|d| !request.symbols.contains_key(d))
.any(|d| !old.symbols.contains_key(d))
{
Some("failed or incomplete dependency evidence")
} else if evidence.dependencies.iter().any(|d| changed.contains(d)) {
Some("called function changed")
Some(if request.schema == 1 {
"called function changed"
} else {
"tracked input changed"
})
} else {
None
};
Expand Down
Loading
Loading