garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

cartopolis-agent · 2026-11-30T17:54:33Z

feat(display): past notifications survive a restart

The list of past notifications was kept in memory only, so a restart or a
flat battery emptied it. The display server now writes it whole at each
change (a notification, the list opened, Clear) to data:/display/notices
in the vault (system:/settings/notices without one), as a records log:
how many are unseen, then a notification an entry. It reads it back when
the system starts after the data is unlocked, so the bell shows at once
with the count it had.

notices-demo now has a third text come, resets the machine (QMP
system_reset, nothing shut down), unlocks, and waits for the three to be
read back, one unseen, and listed.
README.md                   |  2 +-
 docs/roadmap.md             |  2 +-
 programs/src/bin/display.rs | 83 +++++++++++++++++++++++++++++++++++++++++++--
 xtask/src/main.rs           | 28 +++++++++++++--
 4 files changed, 109 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 45937d8..777ac66 100644
--- a/README.md
+++ b/README.md
@@ -71,7 +71,7 @@ cargo xtask demo                       # drive it like a person, a screenshot a
 cargo xtask pair                       # two devices messaging and calling, no server; then by phone and text, one offline
 cargo xtask contacts-demo              # the address book: write, keep, export, remove, import, search
 cargo xtask files-demo                 # Files: notes found in Notes' folder, one opened and moved, one deleted
-cargo xtask notices-demo               # past notifications: two texts, their list, one opened from it, cleared
+cargo xtask notices-demo               # past notifications: two texts, their list, one opened from it, a restart, still listed, cleared
 cargo xtask network-demo               # Settings' network page: online, switched off, on again
 cargo xtask bluetooth-demo             # the devices around, one paired, found again after it moved its address; a keyboard paired by a code, typing in Notes
 cargo xtask pinebook-demo              # a Pinebook Pro's input only: the password typed, Settings clicked open, on USB 1.1 (OHCI)
diff --git a/docs/roadmap.md b/docs/roadmap.md
index a63023d..c61bef5 100644
--- a/docs/roadmap.md
+++ b/docs/roadmap.md
@@ -56,7 +56,7 @@ What is still missing for daily use, and the projects it will take, is in
 | Text input: an on-screen keyboard; a notes app | Done (Notes: a file a note in its own folder, saved half a second after the last key; the caret put down by a tap or moved by the arrow keys, text typed, deleted or pasted where it is) |
 | The everyday apps made whole (s924): Calculator with a decimal key in the language's separator, ±, %, ⌫, pocket-calculator chaining with the sum shown above, and a physical keyboard; Clock with alarms (kept, on and off, ringing while Clock is open), a timer with steps and presets, and a stopwatch with laps; Paint keeping its drawing in its folder as a BMP, with undo and redo by stroke (Clear too), an eraser and four brush sizes; Notes found by their text, pinned to the top, each with its date of change; Messages with each message's time and a line between days, people found by name, one message or a whole conversation deleted from the device; Files making, renaming, copying and deleting folders (a folder with all in it, after asking); the Store uninstalling an app it installed, the person choosing whether its data goes too | Done (alarms ring only while Clock runs; deleting a message does not reach the person's other devices; a rename copies, as the file system has no rename) |
 | Settings: network, storage, about | Done (a system program: the device's name, the screen and turning it, the network, each disk's free space, the system; the name reaches contacts at the p2p service's next start) |
-| Notifications: any program sends one to the display server, a banner for four seconds unless its app is in front, a tap opens the app; Messages notifies of a message that came while it was not in front; each is kept (the last 50, those that came while the screen was locked too), and a bell on the status bar, with how many came since it was last opened, opens their list over the app (drawn by the display server, so no app reads another's), the newest first, a tap on one opening its app, Clear forgetting them; `cargo xtask notices-demo` | Done (kept in memory only, so a restart empties the list; no scrolling past the ones that fit, no dismissing one at a time, no grouping by app) |
+| Notifications: any program sends one to the display server, a banner for four seconds unless its app is in front, a tap opens the app; Messages notifies of a message that came while it was not in front; each is kept (the last 50, those that came while the screen was locked too), in the data vault so a restart finds them (`data:/display/notices`, not synced to other devices), and a bell on the status bar, with how many came since it was last opened, opens their list over the app (drawn by the display server, so no app reads another's), the newest first, a tap on one opening its app, Clear forgetting them; `cargo xtask notices-demo` | Done (no scrolling past the ones that fit, no dismissing one at a time, no grouping by app; a notice's time shows no day) |
 | A clipboard every app shares, in the display server: Copy and Paste in Notes, Paste in Messages, a Paste key on the on-screen keyboard, ctrl+C and ctrl+V on a physical one | Done (texts up to 4 KiB; part of a text selected by dragging the caret's handle or with shift and the arrows, then copied, typed over or pasted over) |
 | Screen lock: one strong password (no PIN: chosen 2026-11-23), the one that opens the data vault; the display server locks after five minutes without input and from Settings, shows the time and the password field and nothing of the apps, passes them no input, checks the password against the vault, and waits 30 s after five wrong ones; Settings changes it (the vault's key sealed again) | Done (a fingerprint beside it waits for a device with a sensor and a secure element) |
 | Encrypted storage: the person's data on a vault disk (each block encrypted as a whole, LIONESS from ChaCha20 and HMAC-SHA-256), opened at start by a passphrase stretched with PBKDF2 (100 000 rounds), chosen at the first start; key slots leave room for a key hardware releases after a fingerprint | Done: apps reach their own folder as `app:`, in the vault once it is open; the p2p service keeps the identity, contacts and messages in it; what the system disk kept is moved in when the vault is made or opened |
diff --git a/programs/src/bin/display.rs b/programs/src/bin/display.rs
index 78868db..0915d6c 100644
--- a/programs/src/bin/display.rs
+++ b/programs/src/bin/display.rs
@@ -237,10 +237,39 @@ struct Notice {
     at: String,
 }
 
-/// How many notifications the list keeps; the oldest go first. They are
-/// kept in memory only: a restart starts the list empty.
+/// How many notifications the list keeps; the oldest go first.
 const NOTICES_KEPT: usize = 50;
 
+/// Where the list is kept, so a restart finds it: in the data vault once it
+/// is open, else on the system disk, as the phone service keeps texts. A
+/// log of `pinnacle_records::log` written whole at each change: its first
+/// entry how many are unseen (u32), then a notification an entry, the oldest
+/// first, each its app, title and time a line and then its text.
+fn notices_file() -> &'static str {
+    if pinnacle_rt::fs::disk("data").is_some() {
+        "data:/display/notices"
+    } else {
+        "system:/settings/notices"
+    }
+}
+
+impl Notice {
+    fn encode(&self) -> Vec<u8> {
+        alloc::format!("{}\n{}\n{}\n{}", self.app, self.title, self.at, self.text).into_bytes()
+    }
+
+    fn decode(body: &[u8]) -> Option<Notice> {
+        let body = core::str::from_utf8(body).ok()?;
+        let mut lines = body.splitn(4, '\n');
+        Some(Notice {
+            app: lines.next()?.into(),
+            title: lines.next()?.into(),
+            at: lines.next()?.into(),
+            text: lines.next()?.into(),
+        })
+    }
+}
+
 /// What a press on the list, or on the button that opens it, was on.
 #[derive(Clone, Copy, PartialEq, Eq)]
 enum ListTarget {
@@ -574,6 +603,7 @@ impl Server {
             at: clock_text(),
         });
         self.unseen += 1;
+        self.keep_notices();
         if self.covered() {
             println!("display: a notice for {app}, while locked: kept for the list");
             return;
@@ -601,11 +631,56 @@ impl Server {
         self.banner = None;
         self.listing = true;
         self.unseen = 0;
+        self.keep_notices();
         self.draw_list();
         self.draw_status();
         println!("display: listed {} notices", self.notices.len());
     }
 
+    /// Writes the list where [`notices_file`] says, whole: one transaction,
+    /// so a power cut leaves the list before or after.
+    fn keep_notices(&self) {
+        let path = notices_file();
+        if let Some(folder) = path.strip_suffix("/notices") {
+            pinnacle_rt::fs::create_dir(folder);
+        }
+        let unseen = (self.unseen as u32).to_le_bytes();
+        let mut file = pinnacle_records::log::entry(&unseen, true);
+        for notice in &self.notices {
+            file.extend(pinnacle_records::log::entry(&notice.encode(), false));
+        }
+        if !pinnacle_rt::fs::write(path, &file) {
+            println!("display: could not keep the notices in {path}");
+        }
+    }
+
+    /// The list as it was kept before this start, once the person's data is
+    /// open: the bell shows at once, with how many were not seen.
+    fn read_notices(&mut self) {
+        let Some(file) = pinnacle_rt::fs::read(notices_file()) else {
+            return;
+        };
+        let Some(entries) = pinnacle_records::log::entries(&file) else {
+            return;
+        };
+        let Some((unseen, rest)) = entries.split_first() else {
+            return;
+        };
+        self.notices = rest.iter().filter_map(|e| Notice::decode(e)).collect();
+        let excess = self.notices.len().saturating_sub(NOTICES_KEPT);
+        self.notices.drain(..excess);
+        self.unseen = unseen
+            .get(..4)
+            .map_or(0, |u| u32::from_le_bytes(u.try_into().unwrap()) as usize)
+            .min(self.notices.len());
+        println!(
+            "display: {} notices kept from before, {} unseen",
+            self.notices.len(),
+            self.unseen
+        );
+        self.draw_status();
+    }
+
     /// Closes the list, showing the app in front again.
     fn close_list(&mut self) {
         self.listing = false;
@@ -667,6 +742,7 @@ impl Server {
             ListTarget::Button => self.open_list(),
             ListTarget::Clear => {
                 self.notices.clear();
+                self.keep_notices();
                 println!("display: notices cleared");
                 self.draw_list();
                 self.draw_status();
@@ -838,6 +914,9 @@ impl Server {
     /// p2p, Bluetooth, phone and model services in the background, and the home
     /// screen.
     fn start_system(&mut self) {
+        // The notifications of before, ahead of any new one from the
+        // services started here.
+        self.read_notices();
         // The peer-to-peer service runs in the background, with no window.
         pinnacle_rt::spawn("p2p", 0, Vec::new());
         // The Bluetooth service too: this device's host stack, for apps and
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index c55005f..0704aa1 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -3777,7 +3777,9 @@ fn wifi_demo() -> Result {
 /// come while the person is on the home screen, their banners go, and the
 /// status bar's button says two came; its list shows both, the newest
 /// first, and a tap on one opens Messages. Opened again, the count is gone
-/// and the list still holds them; Clear empties it.
+/// and the list still holds them. A third comes, the device is reset as a
+/// flat battery would leave it, and once unlocked the bell lists all three,
+/// one unseen. Clear empties it.
 fn notices_demo() -> Result {
     use pinnacle_layout::shell;
     let dir = demo_dir("demo-notices")?;
@@ -3815,10 +3817,32 @@ fn notices_demo() -> Result {
     s.wait_for("display: listed 2 notices")?;
     std::thread::sleep(Duration::from_millis(300));
     s.shot(&dir.join("5-seen-still-listed.png"))?;
+    s.tap(x, y)?;
+    std::thread::sleep(Duration::from_millis(300));
+
+    // A third, not seen; then the power goes (a hardware reset, nothing
+    // shut down) and the device starts again. Once its data is unlocked the
+    // bell has all three, the new one unseen.
+    cell().text_guest(FRIEND, 0, "I'll bring the cake.")?;
+    s.wait_for("display: notice from messages")?;
+    std::thread::sleep(Duration::from_millis(300));
+    s.call(r#"{"execute":"system_reset"}"#)?;
+    s.wait_for("display: your data is locked")?;
+    s.type_keys(&format!("{DEMO_PASSPHRASE}\n"))?;
+    s.wait_for("display: data opened")?;
+    s.wait_for("display: 3 notices kept from before, 1 unseen")?;
+    s.wait_for(&home_apps(0))?;
+    std::thread::sleep(Duration::from_millis(500));
+    s.shot(&dir.join("6-after-a-restart.png"))?;
+    s.tap(x, y)?;
+    s.wait_for("display: listed 3 notices")?;
+    std::thread::sleep(Duration::from_millis(300));
+    s.shot(&dir.join("7-the-list-after-a-restart.png"))?;
+
     s.tap_named("display: button ", "clear")?;
     s.wait_for("display: notices cleared")?;
     std::thread::sleep(Duration::from_millis(300));
-    s.shot(&dir.join("6-cleared.png"))?;
+    s.shot(&dir.join("8-cleared.png"))?;
     s.home()?;
     s.wait_for(&home_apps(0))?;
     Ok(())