Refresh the open Diary day after sync

This commit is contained in:
waozi committed 2026-11-19 00:29:43 -03:00
1 parent 2b0e1eaaa7
commit eca652de59
11 files changed
+258 -7

No files matched your search

+2
View File
@@ -2,6 +2,8 @@
## [Unreleased]
- Refresh the open Diary day when edits sync from another device.
- Use the private Harmony Lumi chat from the owner's Android debug build.
- Backspace and text corrections work again; recover unfinished practices and backups, add photos reliably and reset break timers after time away.
+2
View File
@@ -2,6 +2,8 @@
## [Unreleased]
- Show synced edits for the open day as they arrive and preserve text still being edited.
- Read and edit formatted Diary entries with inline photos.
- Compare full-size AVIF copies with adjustable quality and storage totals; reopen image storage from the wrench in Diary or Photos.
+67
View File
@@ -388,6 +388,8 @@ Apply :: (message: DiaryMessage) -> DiaryMessage {
entry: DiaryMessage;
editor: DiaryUiFrame;
loaded: bool;
document_scope: string;
document_revision: string;
history: DiaryHistory;
photo_waiting: bool;
photo_day: Date;
@@ -460,6 +462,68 @@ DiaryEnsureLoaded :: () {
}
}
DiaryRefreshPosition :: (position: s32, text: string) -> s32 {
at := ifx position > text.count then cast(s32)text.count else position
while at > 0 && at < text.count && DiaryContinuationByte(text[at]) {
at -= 1
}
return at
}
DiaryObserveDocuments :: (refresh: bool) {
if !AppSupports("document.revision") {
return
}
revision := AppService("document.revision", "")
if revision == "" || revision == document_revision {
return
}
separator := revision.count - 1
while separator >= 0 && revision[separator] != cast(u8)58 {
separator -= 1
}
if separator <= 0 {
return
}
scope := revision[:separator]
if document_scope != "" && scope != document_scope {
// Keep the selected date, but never save or carry editor state from
// another account into it.
date := Date.{entry.year, entry.month, entry.day}
entry = Apply(DiaryMessage.{.action = DIARY_LOAD, .year = date.year,
.month = date.month, .day = date.day})
loaded = entry.succeeded
editor = DiaryUiFrame.{}
DiaryHistoryClear(*history)
photo_waiting = false
spelling_sent = ""
document_scope = scope
document_revision = revision
return
}
document_scope = scope
if !refresh || (loaded && !entry.saved) {
// Leave the revision pending while typing or retrying a failed save.
return
}
document_revision = revision
if !loaded {
return
}
fresh := Load(entry, Date.{entry.year, entry.month, entry.day})
// A manifest and its chunks may arrive on different sync pages. Keep
// the current text until a later committed revision makes it readable.
if fresh.load_error || (fresh.document == entry.document && !entry.load_error) {
return
}
fresh.cursor = DiaryRefreshPosition(entry.cursor, fresh.text)
fresh.anchor = DiaryRefreshPosition(entry.anchor, fresh.text)
if fresh.text != entry.text {
DiaryHistoryClear(*history)
}
entry = fresh
}
DiaryHistoryApply :: (redo: bool) {
changed := ifx redo then DiaryHistoryRedo(*history) else DiaryHistoryUndo(*history)
if !changed { return }
@@ -499,17 +563,20 @@ Main :: () -> s32 {
return APP_PROTOCOL
}
if mode == APP_COMMAND {
DiaryObserveDocuments(false)
entry = Apply(DiaryMessageDecode(AppCommand()))
loaded = entry.year > 0
AppReply(DiaryMessageEncode(entry))
return 0
}
if mode == APP_SAVE {
DiaryObserveDocuments(false)
if loaded { entry = Save(entry) }
AppReply(DiaryMessageEncode(entry))
return ifx !loaded || entry.saved || entry.load_error then 0 else 1
}
if mode != APP_FRAME { return 0 }
DiaryObserveDocuments(true)
DiaryEnsureLoaded()
bounds := AppBounds()
editor.session = AppSession()
+4
View File
@@ -266,6 +266,10 @@ apps-diary-editing-test: $(TARGET)
apps-diary-test: $(TARGET)
sh tests/apps_diary_test.sh $(abspath $(TARGET))
.PHONY: apps-diary-sync-test
apps-diary-sync-test: $(TARGET) storage-sync-zi-test
sh tests/apps_diary_test.sh $(abspath $(TARGET)) --sync-only
.PHONY: practice-downloads-ui-test
practice-downloads-ui-test: $(TARGET)
env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS -u GDK_DISPLAY \
+5 -1
View File
@@ -220,7 +220,11 @@ DiaryStoreRead :: (name: string) -> DiaryFileResult {
diary_file_bytes = DiaryRecordRead(name)
if diary_file_bytes.count > 0 {
result.missing = false
unused PackageWrite(path, diary_file_bytes)
mirror := PackageRead(path, DIARY_FILE_LIMIT)
if mirror != diary_file_bytes {
unused PackageWrite(path, diary_file_bytes)
}
PackageFree(mirror)
} else {
key := DiaryRecordKey(name)
manifest := BundledAppRecordRead(key)
+25 -1
View File
@@ -42,6 +42,9 @@ TextArea :: #import "kryon/text_area";
#import, file "node_apps.zi";
#import, file "node_app_tree.zi";
#import, file "media_store.zi";
#import, file "../storage/db.zi";
#import, file "../storage/state.zi";
#import, file "../storage/storage_settings.zi";
using HostValueKind;
@@ -84,6 +87,7 @@ lumi_message_input: [1024]u8;
diary_message: DiaryMessage;
diary_codec: DiaryMessageCodec;
diary_file_codec: DiaryFileResultCodec;
diary_document_revision: [160]u8;
diary_text: string;
diary_document: string;
local_bundle_path: [4096]u8;
@@ -1800,13 +1804,33 @@ DiaryServiceAllowed :: (context: *void, name: string) -> bool {
name == "AppLocaleHost" || name == "ClipboardCopyHost" ||
name == "media.asset" || name == "media.query" || name == "media.pick" ||
name == "media.pick.status" || name == "media.compression.open" ||
name == "text.correct.start" || name == "text.correct.poll"
name == "text.correct.start" || name == "text.correct.poll" || name == "document.revision"
}
DiaryServiceCall :: (context: *void, module: *u8, function: *u8,
arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
slot := cast(*ModuleSlot)context
name := TextFromCString(function)
if name == "AppServiceHost" && count == 2 && AppArgumentText(arguments[0]) == "document.revision" {
revision := storage_committed_data_revision()
value: string
owner := AppTextFromCString(storage_user_id_buffer(), 64)
if revision >= 0 && owner != "" {
account := storage_get_setting_text("sync_public_id")
at := owner.count + account.count + 2
if at + 24 <= diary_document_revision.count {
unused AppCopyText(owner, diary_document_revision[:])
diary_document_revision[owner.count] = cast(u8)58
unused AppCopyText(account, diary_document_revision[owner.count + 1:])
diary_document_revision[at - 1] = cast(u8)58
if AppFormatS64(revision, diary_document_revision[at:]) {
value = AppTextFromCString(*diary_document_revision[0], cast(s32)diary_document_revision.count)
}
}
}
result.* = NodeAppString(value)
return 1
}
if name == "DocumentReadHost" && count == 1 &&
HostScalarValid(arguments[0], "string", cast(s32)HostString) {
file := DiaryStoreRead(AppArgumentText(arguments[0]))
+20
View File
@@ -7,6 +7,10 @@ LOG_ERROR :: 5
sqlite3_open_v2 :: (path: *u8, database: **void, flags: s32, vfs: *u8) -> s32 #foreign sqlite3_lib "sqlite3_open_v2";
sqlite3_busy_timeout :: (database: *void, milliseconds: s32) -> s32 #foreign sqlite3_lib "sqlite3_busy_timeout";
sqlite3_total_changes64 :: (database: *void) -> s64 #foreign sqlite3_lib "sqlite3_total_changes64";
StorageRevisionMutex :: (database: *void) -> *void #foreign sqlite3_lib "sqlite3_db_mutex";
StorageRevisionLock :: (mutex: *void) #foreign sqlite3_lib "sqlite3_mutex_enter";
StorageRevisionUnlock :: (mutex: *void) #foreign sqlite3_lib "sqlite3_mutex_leave";
StorageRevisionAutocommit :: (database: *void) -> s32 #foreign sqlite3_lib "sqlite3_get_autocommit";
// Counts database opens and closes, so memoized reads can tell a reopened
// connection, whose change counter starts again at zero, from the old one.
@@ -59,6 +63,22 @@ storage_data_revision :: () -> s64 {
}
return storage_revision_value
}
// A view must not consume a revision while the sync worker's savepoint is
// open. If the worker rolls it back, SQLite's row counter does not roll back.
storage_committed_data_revision :: () -> s64 {
database := storage_db_handle()
if database == null {
return -1
}
mutex := StorageRevisionMutex(database)
StorageRevisionLock(mutex)
defer { StorageRevisionUnlock(mutex) }
if StorageRevisionAutocommit(database) == 0 {
return -1
}
return storage_data_revision()
}
sqlite3_db_cacheflush :: (db: *void) -> s32 #foreign sqlite3_lib "sqlite3_db_cacheflush";
storage_sqlite_errmsg :: (db: *void) -> *u8 #foreign sqlite3_lib "sqlite3_errmsg";
STORAGE_PATH_SIZE :: 1024
+71
View File
@@ -1,5 +1,6 @@
"""Verify standalone Diary editing, restart and calendar on an owned test window."""
import contextlib
import datetime
import hashlib
import json
import os
@@ -135,6 +136,76 @@ def application(profile, label, import_source=None, size=(900, 720)):
app.wait(timeout=3)
def sync_refresh():
fixture = ROOT / "build/storage-sync-zi-test/test"
day = (datetime.date.today() - datetime.timedelta(days=1)).isoformat()
filename = day + ".json"
def document(text):
return json.dumps(dict(version=1, date=day, text=text, photos=[]))
def receive(sender, receiver, text, version):
subprocess.run([str(fixture), "--diary-incoming", str(sender), str(receiver),
filename, document(text), str(version)], env=ENV, check=True, timeout=10)
def editor_value(text):
deadline = time.monotonic() + 10
while True:
area = driver.wait(role="text_area")
if area["value"] == text:
return area
assert time.monotonic() < deadline, f"Diary editor still shows {area['value']!r}"
time.sleep(0.05)
with tempfile.TemporaryDirectory(dir=OUTPUT, prefix="sync-") as temporary:
sender, receiver = (Path(temporary) / name for name in ("sender", "receiver"))
receive(sender, receiver, "Earlier words", 1)
with application(receiver, "incoming"):
driver.press(label="‹")
editor_value("Earlier words")
receive(sender, receiver, "Newest words", 2)
editor_value("Newest words")
assert driver.wait(label="›", role="button")["enabled"], "Diary left the selected prior day"
# Hold autosaves so that the sync revision is observed while the
# editor is dirty, even on a slow or busy test machine.
incoming = "Another device edited the same day"
remote_hash = hashlib.sha256(document(incoming).encode()).hexdigest()
with contextlib.closing(sqlite3.connect(receiver / "inbe.db", timeout=3)) as db:
db.execute(f"""CREATE TRIGGER diary_test_hold_save BEFORE UPDATE ON settings
WHEN NEW.key = 'cell.diary.file.{filename}'
AND json_extract(NEW.value, '$.hash') <> '{remote_hash}'
BEGIN SELECT RAISE(IGNORE); END""")
db.commit()
driver.press(role="text_area")
driver.key("end")
driver.type(" local draft")
driver.key("left", shift=True)
dirty = editor_value("Newest words local draft")
receive(sender, receiver, incoming, 3)
driver.wait_idle(frames=3, timeout=3)
after = editor_value("Newest words local draft")
assert after["cursor"] == dirty["cursor"]
driver.type("!")
editor_value("Newest words local draf!")
# Change the linked account while the old draft is held.
# The new scope reopens the selected day from storage.
db.execute("UPDATE settings SET value='other-test-account' WHERE key='sync_public_id'")
db.commit()
editor_value(incoming)
db.execute("DROP TRIGGER diary_test_hold_save")
db.commit()
driver.wait_idle(frames=3, timeout=3)
editor_value(incoming)
print("Diary incoming sync: open day refreshed in place, active draft and selection survived, and account scope discarded the old editor cache")
if sys.argv[2:] == ["--sync-only"]:
sync_refresh()
sys.exit(0)
with tempfile.TemporaryDirectory(prefix="inbe-diary-ui-") as temporary:
profile = Path(temporary)
# The desktop picker runs zenity, which answers with the picked file.
+3 -2
View File
@@ -3,6 +3,7 @@ set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
binary=${1:-"$root/build/bin/linux/breeze-linux-x86_64"}
exec env -u INBE_DIARY_IMPORT -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
-u SESSION_MANAGER YUE_DESKTOP_RECOVERY=0 SDL_VIDEODRIVER=x11 LIBGL_ALWAYS_SOFTWARE=1 \
xvfb-run -a -n 400 -s '-noreset -screen 0 1280x900x24' \
-u GDK_DISPLAY -u SESSION_MANAGER HARMONY_DESKTOP_RECOVERY=0 YUE_DESKTOP_RECOVERY=0 \
SDL_VIDEODRIVER=x11 LIBGL_ALWAYS_SOFTWARE=1 \
xvfb-run -a -n 400 -s '-noreset -screen 0 1280x900x24 -nolisten tcp' \
python3 "$root/tests/apps_diary_test.py" "$binary" "${2:-}"
+57 -1
View File
@@ -2023,7 +2023,12 @@ test_bundled_app_records_round_trip :: () {
set_account()
response := response_with(5, records)
check_true("full app response is valid JSON", storage_json_valid(response) != 0)
revision := storage_committed_data_revision()
check_true("start a storage transaction", exec_sql("SAVEPOINT open_diary_revision") != 0)
check_true("open transactions are invisible to the editor", storage_committed_data_revision() < 0)
check_true("close the storage transaction", exec_sql("RELEASE open_diary_revision") != 0)
check_true("encrypted app restore", storage_apply_sync_response_json(response) != 0)
check_true("committed incoming records advance the editor revision", storage_committed_data_revision() > revision)
LumiStoreReset()
check_int("conversation restored", LumiStoreCount(), 2)
check_true("chat text survives", LumiStoreRow(0).text == "Private conversation 水")
@@ -2042,6 +2047,47 @@ test_bundled_app_records_round_trip :: () {
storage_close()
}
// The UI test runs this in a separate process. A sender produces a real
// encrypted upload, then the normal inbound path applies it to the open
// receiver's database through a second connection.
ApplyDiaryFixture :: (sender: *u8, receiver: *u8, filename: *u8, document: *u8, version: s32) -> s32 {
first := TextFromCString(sender)
second := TextFromCString(receiver)
prefix := "/mnt/storage/Projects/waozixyz/apps/breeze/build/apps-diary-test/sync-"
if !starts_with(first, prefix) || !starts_with(second, prefix) || first == second {
return 100
}
install_english_catalog()
if storage_init(first) == 0 {
return 1
}
set_account()
name := TextFromCString(filename)
value := TextFromCString(document)
saved := DiaryRecordSave(name, value)
// Use consecutive record clocks, even when several fixtures run in one second.
storage_set_sync_enabled(0)
clock: [24]u8
clock_sql := PackageText("UPDATE settings SET updated_at=", FormatInteger(2000000000 + cast(s64)version, clock[:]))
query := PackageText(clock_sql, " WHERE key GLOB 'cell.diary.*'")
ready := saved && exec_sql(query) != 0 && build_payload()
PackageFree(clock_sql)
PackageFree(query)
storage_close()
if !ready || storage_init(second) == 0 {
return 2
}
defer { storage_close() }
set_account()
storage_set_sync_enabled(0)
if storage_apply_sync_response_json(response_with(version, records_array())) == 0 {
return 3
}
received := DiaryRecordRead(name)
defer { PackageFree(received) }
return ifx received == value then 0 else 4
}
// The app choices a new profile starts with, stored as defaults.
store_default_apps :: () {
storage_set_setting_default_text("app_used_lists", "0")
@@ -2744,7 +2790,17 @@ test_storage_note_accounting :: () {
}
#program_export
Check :: () -> s32 {
Check :: (count: s32, arguments: **u8) -> s32 {
if count != 1 {
if count != 7 || TextFromCString(arguments[1]) != "--diary-incoming" {
return 2
}
version := ReadNumber(TextFromCString(arguments[6]), 0)
if !version.valid || version.value < 1 || version.value > 100 {
return 2
}
return ApplyDiaryFixture(arguments[2], arguments[3], arguments[4], arguments[5], cast(s32)version.value)
}
guard := support_begin("/mnt/storage/Projects/waozixyz/apps/breeze/build/storage-sync-zi-test/")
if guard != 0 { return guard }
test_storage_note_accounting()
+2 -2
View File
@@ -41,9 +41,9 @@ C
cat > "$work/generated/main.c" <<'C'
#include "storage_sync_behavior.h"
#include <stdio.h>
int main(void)
int main(int argc, char **argv)
{
int result = Check();
int result = Check(argc, (uint8_t **)argv);
if (result != 0) fprintf(stderr, "check failed with code %d\n", result);
return result == 0 ? 0 : 1;
}