Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 18 additions & 3 deletions src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ pub struct Evidence {
pub length: i64,
pub sentence: i64,
pub quote: String,
/// On a claim's first quote: how many claims of the same kind in its window start their
/// first quote in the same sentence before this one, by where the quote starts (#125). Left
/// out when 0, so a claim alone in its sentence keeps the uid it always had.
#[serde(default, skip_serializing_if = "is_zero")]
pub nth: i64,
}

fn is_zero(n: &i64) -> bool {
*n == 0
}

/// The body of a claim op, which the curation phase writes (Task 7).
Expand Down Expand Up @@ -78,12 +87,17 @@ pub fn normalize(kind: &str, status: &str) -> (&'static str, &'static str) {
}

/// MUST-M18: a claim's uid is its kind and the sentence its first quote starts in, never a name
/// the model gives, so a recuration that rewords it derives the same uid.
/// the model gives, so a recuration that rewords it derives the same uid; and its place among
/// the claims of that kind in that sentence, so two of them keep two uids (#125).
pub fn uid(kind: &str, first: &Evidence) -> String {
crate::curate::sha256_hex(&format!(
let key = format!(
"{kind}\0{}\0{}\0{}",
first.device, first.seq, first.sentence
))
);
crate::curate::sha256_hex(&match first.nth {
0 => key,
n => format!("{key}\0{n}"),
})
}

pub(crate) fn schema(k: &Connection) -> Result<()> {
Expand Down Expand Up @@ -317,6 +331,7 @@ pub fn pref_add(home: &std::path::Path, text: &str) -> Result<String> {
length: i64::try_from(quote.len())?,
sentence: 0,
quote: quote.clone(),
nth: 0,
};
let uid = uid("preference", &evidence);
let op = ClaimOp {
Expand Down
2 changes: 2 additions & 0 deletions src/consumer/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl Consumer for Anchors {
length: r.get(3)?,
sentence: r.get(4)?,
quote: r.get(5)?,
nth: 0,
})
})?
.collect::<rusqlite::Result<_>>()?;
Expand Down Expand Up @@ -476,6 +477,7 @@ mod tests {
length: quote.len() as i64,
sentence,
quote: quote.into(),
nth: 0,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/consumer/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ mod tests {
length: quote.len() as i64,
sentence: 0,
quote: quote.clone(),
nth: 0,
};
let uid = crate::claims::uid(kind, &evidence);
let op = crate::claims::ClaimOp {
Expand Down
94 changes: 94 additions & 0 deletions src/curate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ pub fn locate(window: &Window, line: &str, quote: &str) -> Option<crate::claims:
length: as_i64(quote.len())?,
sentence: as_i64(source.sentence(at))?,
quote: quote.to_owned(),
nth: 0,
})
}

Expand Down Expand Up @@ -1538,6 +1539,7 @@ fn anchored_in(k: &Connection, w: &Window) -> Result<Vec<(String, crate::claims:
length: r.get(3)?,
sentence: r.get(4)?,
quote: r.get(5)?,
nth: 0,
Comment thread
ojungo69 marked this conversation as resolved.
Resolved
})
})?
.collect::<rusqlite::Result<_>>()?;
Expand Down Expand Up @@ -1786,9 +1788,38 @@ fn located(
if any && found.is_empty() {
return Err(AnswerFailure::Unanchored);
}
number(&mut found);
Comment thread
ojungo69 marked this conversation as resolved.
Resolved
Ok((summary, found, lost))
}

/// Numbers the drafts of one kind whose first quotes start in one sentence, for their uids
/// (#125): drafts whose quotes overlap are one claim drafted twice and share a number (the gates
/// settle them as one), and a quote apart from those before it takes the next. A claim alone in
/// its sentence is 0, the uid it always had.
fn number(found: &mut [Located]) {
let key = |(d, e, _): &Located| {
let (kind, _) = crate::claims::normalize(&d.kind, &d.status);
(kind, e.device.clone(), e.seq, e.sentence)
};
let mut order: Vec<usize> = (0..found.len()).collect();
order.sort_by_key(|&i| (key(&found[i]), found[i].1.offset));
let (mut last, mut n) = (None, 0);
for i in order {
let (k, e) = (key(&found[i]), &mut found[i].1);
let end = e.offset + e.length;
match &mut last {
Some((prev, reach)) if *prev == k => {
if e.offset >= *reach {
n += 1;
}
*reach = end.max(*reach);
}
_ => (last, n) = (Some((k, end)), 0),
}
e.nth = n;
Comment thread
ojungo69 marked this conversation as resolved.
Resolved
}
}

/// At most `k` of `all`, spread evenly over the whole of it (a window's trigrams: not its first
/// lines only), all of them when there are no more than `k`.
fn spread<T>(all: &[T], k: usize) -> impl Iterator<Item = &T> {
Expand Down Expand Up @@ -3315,6 +3346,7 @@ mod tests {
length: text.len() as i64,
sentence: 0,
quote: text.into(),
nth: 0,
};
let op = crate::claims::ClaimOp {
id: "c".into(),
Expand Down Expand Up @@ -4037,6 +4069,68 @@ mod tests {
.collect()
}

/// Two decisions in one sentence are two claims with two uids, and a recuration that gives
/// them again, reworded and in another order, keeps both; two drafts whose quotes overlap are
/// one claim drafted twice (#125).
#[test]
fn two_decisions_in_one_sentence_get_two_uids_and_keep_them_on_recuration() {
let home = tempfile::tempdir().unwrap();
let (mut raw, db) = open(home.path());
let mut k = crate::knowledge::open(home.path()).unwrap();
raw.append(&prompt("Use tabs and log to stderr.")).unwrap();
let draft = |id: &str, quote: &str, body: &str| {
let mut c = claim(id, "decided", "L1", quote, json!([]));
c["body"] = json!(body);
c
};
let answers = std::cell::RefCell::new(vec![
json!({"claims": [draft("c1", "log to stderr", "Logs go to stderr."),
draft("c2", "Use tabs", "Tabs, not spaces."),
draft("c3", "Use tabs and", "Indent with tabs.")], "summary": "s"}),
json!({"claims": [draft("c1", "Use tabs", "Tabs for indents."),
draft("c2", "log to stderr", "Log to stderr.")], "summary": "s"}),
]);
let mut chain = |_: &str, _: &str, _: &AnswerCheck| -> Result<ChainResult> {
Ok(ChainResult {
output: answers.borrow_mut().remove(0),
..answered("fake")
})
};
let (rules, summary) = (Rules::default(), curating(WINDOW_TOKENS));
run_phase(&mut raw, &k, &db, &rules, &summary, "", &mut chain).unwrap();
consume(&raw, &mut k);
let uids = |k: &Connection| -> i64 {
k.query_row("SELECT count(DISTINCT uid) FROM derivations", [], |r| {
r.get(0)
})
.unwrap()
};
let decided = |b: &str| (b.to_owned(), "decided".to_owned());
assert_eq!(uids(&k), 2);
assert_eq!(
active(&k),
[decided("Indent with tabs."), decided("Logs go to stderr.")]
);
let span = Span::records(1, 1);
let windows = span_windows(&raw, &span, WINDOW_TOKENS, &rules).unwrap();
let again = recurate_window(
&mut raw,
&k,
&rules,
&summary,
&mut chain,
&windows[0],
None,
);
assert_eq!(again.unwrap(), Ok((2, 0)));
consume(&raw, &mut k);
assert_eq!(uids(&k), 2);
assert_eq!(
active(&k),
[decided("Log to stderr."), decided("Tabs for indents.")]
);
}

/// Task 11: a recuration's answer replaces its window's unsettled claims: one anchored in the
/// window that it no longer gives is retracted (a newer derivation at the same tier). A
/// settled one it leaves out stays, since a curator's answers vary and `retracted` needs the
Expand Down
1 change: 1 addition & 0 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ mod tests {
length: quote.len() as i64,
sentence: 0,
quote: quote.clone(),
nth: 0,
};
uids.push(crate::claims::uid("decision", &evidence));
let op = crate::claims::ClaimOp {
Expand Down
3 changes: 2 additions & 1 deletion src/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ pub fn check(
own.push((own_words, line.repo.as_deref(), &line.key));
}
// Once every status is settled: what a sibling is, after the gates. Siblings of one kind
// quoting one sentence are one claim (`claims::uid`), settled when any of them is.
// whose quotes overlap in one sentence are one claim (`claims::uid`, `curate::number`),
// settled when any of them is.
let unsettled = |s: &str| matches!(s, "proposed" | "unverified");
let uids: Vec<String> = g
.kept
Expand Down
Loading