open lists: origin order for follow-ups only (an import never carries a memory); tests follow the clip

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-09 12:02:59 +01:00
commit 7c919a2146
3 changed files with 32 additions and 43 deletions

View file

@ -170,8 +170,8 @@ pub(crate) const SHORT_MAX_BYTES: usize = 200;
/// The most bytes each open-item list of the responsibilities section carries, outside `GOALS_MAX_BYTES`.
const OPEN_COMMITTED_MAX_BYTES: usize = 4 * 1024;
const OPEN_ASKED_MAX_BYTES: usize = 4 * 1024;
/// The page size of an open-item fetch. It bounds the cost of one read, not what shows: every page is read, because the
/// order is the item's origin time and a page of admission order is not that order.
/// The most open items one list fetches. It bounds the cost of a read only: the byte bounds above show fewer than this on any
/// line a memory can make, and `matched` still counts every item.
const OPEN_FETCH: u32 = 64;
/// The most event ids a Persona memory cites as its sources.
pub(crate) const SOURCES_MAX: usize = 8;
@ -2320,6 +2320,14 @@ fn ruling_ids_in(text: &str) -> Vec<String> {
.collect()
}
/// The sort key of oldest first: when the document began, then the mind's own admission ordinal for equal times. An imported
/// document began when the giver's receipt committed (`imported_from.committed_at`, the same RFC 3339 spelling as an admission
/// time), any other when it was admitted here.
fn origin_order(admission: &huginn_mind::AdmissionFacts) -> (&str, u64) {
let began = admission.provenance.imported_from.as_ref().map_or(admission.admitted_at.as_str(), |source| source.committed_at.0.as_str());
(began, admission.ordinal)
}
/// The campaigns whose dropped follow-ups the note names; the rest are counted together.
const DROPPED_CAMPAIGNS_NAMED: usize = 6;
@ -2387,24 +2395,15 @@ pub(crate) fn render_responsibilities(mind: &dyn MindPort, instance: &Slug, name
(MemorySort::Committed, "You committed to, oldest first:", "commitments", OPEN_COMMITTED_MAX_BYTES),
(MemorySort::Asked, "Asked of you, not yet taken on, oldest first:", "asks", OPEN_ASKED_MAX_BYTES),
] {
let mut asking = Selection {
// Ascending is admission order. A memory is instance-rooted, so an import never carries one: its admission is its origin.
let selection = Selection {
limit: Some(OPEN_FETCH),
projection: "document".into(),
..any_of(in_force(of_kind(PipelineKind::Memory)), "sort", &[format!("{sort:?}").as_str()])
};
let mut views = Vec::new();
loop {
let page = mind.query(&asking)?;
let PipelinePageItems::Documents(items) = page.items else { bail!("the mind answered headers to a document selection") };
views.extend(items);
match page.next {
Some(cursor) => asking.cursor = Some(cursor),
None => break,
}
}
// Oldest first by origin: an imported item is as old as it was in the giver, not as its import.
views.sort_by(|a, b| recall::origin_order(&a.admission).cmp(&recall::origin_order(&b.admission)));
let matched = views.len();
let page = mind.query(&selection)?;
let matched = page.matched as usize;
let PipelinePageItems::Documents(views) = page.items else { bail!("the mind answered headers to a document selection") };
let (mut section, mut shown) = (String::new(), 0usize);
for view in &views {
let Some(line) = recall::memory_line(view, names, true) else { continue };
@ -2429,10 +2428,10 @@ pub(crate) fn render_responsibilities(mind: &dyn MindPort, instance: &Slug, name
}
// The open_ids items are outside the follow-ups' and rulings' bound.
let open_len = out.len() - open_start;
// Oldest first by origin: an imported follow-up is as old as it was in the giver, not as its import. Work that has waited
// longest leads, and the bound drops the newest.
// Oldest first by origin: an imported follow-up is as old as it was in the giver, not as its import. The work that has
// waited longest leads, and the bound drops the newest.
let mut follow_ups = headers(mind, &in_force(of_kind(PipelineKind::FollowUp)))?;
follow_ups.sort_by(|a, b| recall::origin_order(&a.admission).cmp(&recall::origin_order(&b.admission)));
follow_ups.sort_by(|a, b| origin_order(&a.admission).cmp(&origin_order(&b.admission)));
let mut dropped: BTreeMap<String, usize> = BTreeMap::new();
// Every open follow-up counts as open work for its campaign, whether or not the bound below lets its line show.
let mut open_roots: Vec<String> = Vec::new();

View file

@ -2370,7 +2370,7 @@ fn each_open_list_holds_its_own_bound_exactly_and_counts_what_it_drops() {
// Short fillers make the lines fine-grained; the oldest item's text (under the clip) tunes the total to the byte.
let render = |fillers: usize, adjustable: usize| {
let mind = TestMind::seeded();
mind.committed(vec![crate::testkit::memory("m0", sort, &"w".repeat(adjustable), &[])]);
mind.committed(vec![crate::testkit::memory("m0", sort, &"w".repeat(adjustable + 1), &[])]);
for index in 1..=fillers {
mind.committed(vec![crate::testkit::memory(&format!("m{index:02}"), sort, "f", &[])]);
}
@ -2413,15 +2413,15 @@ fn each_open_list_holds_its_own_bound_exactly_and_counts_what_it_drops() {
fn a_full_open_section_shows_the_oldest_and_counts_the_newest() {
for (sort, heading) in [(MemorySort::Committed, "You committed to, oldest first:"), (MemorySort::Asked, "Asked of you, not yet taken on, oldest first:")] {
let mind = TestMind::seeded();
for index in 0..10 {
for index in 0..40 {
mind.committed(vec![crate::testkit::memory(&format!("m{index:02}"), sort, &format!("M{index:02} {}", "x".repeat(900)), &[])]);
}
let text = render_responsibilities(&mind, &Slug(INSTANCE.into()), &BTreeMap::new()).unwrap().text;
let section = under(&text, heading);
let shown: Vec<usize> = section.match_indices(":memory:m").map(|(at, found)| section[at + found.len()..][..2].parse().unwrap()).collect();
assert!(!shown.is_empty() && shown.len() < 10, "{text}");
assert!(!shown.is_empty() && shown.len() < 40, "{text}");
assert_eq!(shown, (0..shown.len()).collect::<Vec<_>>(), "ascending by admission, no gap: {text}");
assert!(section.contains(&format!("({} more", 10 - shown.len())), "{text}");
assert!(section.contains(&format!("({} more", 40 - shown.len())), "{text}");
}
}
@ -5767,7 +5767,7 @@ fn a_follow_up_the_goals_bound_hides_still_counts_as_open_work() {
mind.committed(campaign_on("hidden-in", &[REPO]));
mind.committed(chain("hidden-in", "src", REPO));
mind.committed(chain(CAMPAIGN, "src", REPO));
for n in 0..12 {
for n in 0..60 {
let PipelineDocument::FollowUp(mut long) = follow_up(&format!("long{n}"), &spec_ref("src")) else { unreachable!("a follow-up") };
long.item = Line("x".repeat(900));
mind.committed(vec![PipelineDocument::FollowUp(long)]);
@ -7383,10 +7383,12 @@ fn an_imported_item_sorts_by_its_origin_time_not_its_import() {
let mind = TestMind::seeded();
mind.committed(chain(CAMPAIGN, "src", REPO));
mind.committed(vec![follow_up_in(CAMPAIGN, "native", "Native, admitted first of the two.")]);
mind.committed_imported("2020-01-01T00:00:00Z", vec![follow_up_in(CAMPAIGN, "imported-old", "Imported, born in the giver long ago.")]);
mind.committed_imported("2999-01-01T00:00:00Z", vec![follow_up_in(CAMPAIGN, "imported-new", "Imported, born in the giver later than anything.")]);
mind.committed(vec![crate::testkit::memory("m-native", MemorySort::Asked, "native ask", &[])]);
mind.committed_imported("2020-01-01T00:00:00Z", vec![crate::testkit::memory("m-imported", MemorySort::Asked, "imported ask", &[])]);
for (campaign, born) in [("imp-old", "2020-01-01T00:00:00Z"), ("imp-new", "2999-01-01T00:00:00Z")] {
let mut batch = campaign_on(campaign, &[REPO]);
batch.extend(chain(campaign, "src", REPO));
batch.push(follow_up_in(campaign, campaign, "Imported."));
mind.committed_imported(born, batch);
}
let text = render_responsibilities(&mind, &Slug(INSTANCE.into()), &BTreeMap::new()).unwrap().text;
assert_eq!(listed_follow_ups(&text), ["imported-old", "native", "imported-new"], "{text}");
let at = |needle: &str| text.find(needle).unwrap_or_else(|| panic!("{needle} in {text}"));
@ -7422,7 +7424,7 @@ fn a_line_that_does_not_fit_is_skipped_and_a_later_short_one_shows() {
for index in 0..fillers {
mind.committed(vec![follow_up_in(CAMPAIGN, &format!("fill{index:03}"), "filler")]);
}
mind.committed(vec![follow_up_in(CAMPAIGN, &format!("big{}", "g".repeat(190)), &"b".repeat(160))]);
mind.committed(vec![follow_up_in(CAMPAIGN, &format!("big{}", "g".repeat(57)), &"b".repeat(160))]);
if with_short {
mind.committed(vec![follow_up_in(CAMPAIGN, "short", "s")]);
}

View file

@ -29,7 +29,7 @@ use anyhow::{Result, bail};
use chrono::{DateTime, Utc};
use cultnet_rs::Selection;
use eureka_pipeline::{MemoryOrigin, MemorySort, PipelineDocument, PipelineKind};
use huginn_mind::{AdmissionFacts, PipelineDocumentView, PipelinePageItems};
use huginn_mind::{PipelineDocumentView, PipelinePageItems};
use crate::persona::quoted;
use crate::queue::{MindPort, any_of, in_force, of_kind};
@ -217,8 +217,7 @@ pub(crate) fn origin_phrase(origin: &MemoryOrigin, names: &BTreeMap<&str, &str>)
pub(crate) fn memory_line(view: &PipelineDocumentView, names: &BTreeMap<&str, &str>, with_id: bool) -> Option<String> {
// An open item of the responsibilities (the one caller that leads with the id) shows a clipped text; recall shows it whole.
let PipelineDocument::Memory(memory) = &view.document else { return None };
let when = origin_time(&view.admission);
let date = when.get(..10).unwrap_or(when);
let date = view.admission.admitted_at.get(..10).unwrap_or(&view.admission.admitted_at);
let origin = origin_phrase(&memory.origin, names);
let about = who(memory.about.iter().map(|id| id.0.as_str()), names);
let sort = match (memory.sort, memory.about.is_empty()) {
@ -244,17 +243,6 @@ pub(crate) fn clipped(text: &str) -> String {
format!("{}…", &text[..cut])
}
/// When a document first stood, as the time it was admitted where it began: an imported document carries the source
/// receipt's time, any other its own admission.
pub(crate) fn origin_time(admission: &AdmissionFacts) -> &str {
admission.provenance.imported_from.as_ref().map_or(admission.admitted_at.as_str(), |source| source.committed_at.0.as_str())
}
/// The sort key of oldest first: origin time, then the mind's own admission ordinal for equal times.
pub(crate) fn origin_order(admission: &AdmissionFacts) -> (&str, u64) {
(origin_time(admission), admission.ordinal)
}
/// One recalled document as its line, or None for a kind recall does not show.
fn line_of(view: &PipelineDocumentView, names: &BTreeMap<&str, &str>) -> Option<String> {
match &view.document {