8000 feat(timeline): an hourly tidy drops marks and reactions left on activities that are gone by adeelahmad · Pull Request #662 · adeelahmad/lense · GitHub
[go: up one dir, main page]

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
2 changes: 2 additions & 0 deletions changelog.d/timeline-tidy-marks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Review marks and reactions left on timeline activities that are gone (an event log entry past `events.keep_days`, a
deleted asset) are now dropped by an hourly tidy, a batch at a time. Comments on them stay.
2 changes: 1 addition & 1 deletion docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ request, to another check that proves the same thing; loosening a requirement or
| A routine's run that fails or leaves graph changes to review is told through Matterbridge and webhooks, to each namespace it runs over | [test_notifications.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notifications.py) `test_routine_runs_that_fail_or_leave_changes_to_review_are_told` |
| Everything that happens is kept as W3C Activity Streams activities, read by agents through the API, MCP and the assistant | [test_events.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_events.py) `test_activities_have_one_shape`; [test_activities.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_activities.py) `test_the_timeline_lists_new_assets_newest_first`, `test_agents_read_the_timeline_too`; [test_news_feed_source.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_news_feed_source.py) `test_a_news_feed_as_a_source` |
| Ask the assistant about a timeline item from the item (its button, or `a` on its line), in the chat beside the page, with the item's id, line and fields as what the question is about | [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `asking about an item hands the page chat its id, line and fields, by button or the a key`; [page-chat.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/page-chat.test.tsx) `opens with what a page's own Ask about this hands it, and sends it as the highlighted part` |
| The Timeline page and Latest on Home, marking what you've seen | [test_timeline_marks.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_marks.py) `test_marks_are_set_cleared_and_filter_the_timeline`; [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `Home's Latest card lists what you haven't reviewed` |
| The Timeline page and Latest on Home, marking what you've seen | [test_timeline_marks.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_marks.py) `test_marks_are_set_cleared_and_filter_the_timeline`, `test_a_tidy_drops_marks_and_reactions_left_on_activities_that_are_gone`; [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `Home's Latest card lists what you haven't reviewed` |
| Reactions and comments on timeline activities, shared by everyone who sees them, each reaction saying who gave it, comments edited and threads resolved in place, through the API, MCP and the page | [test_timeline_social.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_social.py) `test_reactions_are_shared_counted_and_taken_back`, `test_comments_on_an_activity_are_a_thread_everyone_who_sees_it_joins`, `test_comments_on_a_logged_activity_follow_its_audience`, `test_agents_react_and_comment_too`, `test_a_reaction_names_who_else_gave_it_earliest_first_up_to_five`; [timeline-social-model.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-social-model.test.ts) `a reaction's hover says who gave it: you first, then up to five names, then how many more`; [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `reactions and comments: a line sums its items', a 👍 goes to the server, and Enter sends a comment`, `your comment is edited in place, and a thread you can resolve is resolved and reopened from the timeline` |
| Tagging @lens in a comment on the timeline or an asset gets the agent's answer in the thread, as the person who tagged it, with every change waiting for their approval, off with `ai.answer_comments` | [test_comment_agent.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_comment_agent.py) `test_the_agent_answers_a_tagged_comment_in_the_thread`, `test_without_a_model_or_within_a_used_up_budget_the_reply_says_so`; [timeline-social-model.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-social-model.test.ts) `the agent's reply links its addresses` |
| Automations act on the stream, on a schedule, at a time or when their own webhook is called | [test_automation_runs.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_automation_runs.py) `test_each_new_activity_runs_each_automation_it_matches_once`, `test_a_schedule_runs_its_automation_when_its_timer_fires`, `test_a_call_to_a_webhook_runs_its_automation`; [test_timer_triggers.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_timer_triggers.py) `test_an_at_trigger_fires_once_then_reads_done` |
Expand Down
8 changes: 6 additions & 2 deletions docs/timeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ counts are two grouped queries. Mutes are one `event_mute` row per person and ta
the name shown, `until` and when it was set, read once per page and applied with the mark filters in the same query
per source page; they go when the person is removed. Nothing of this costs a model call.

Marks and reactions left on an activity that is gone (an event log entry past `events.keep_days`, a deleted asset) are
dropped by a tidy that runs with the other hourly ones: each time it looks up to 500 activities marked or reacted to
over a day ago, in id order from where it stopped last, and deletes the rows of those no source has any more. Comments
on them stay, since they are what people wrote, and an asset's page still shows its own.

## Refine later

- Ask about an item in the full Chat as well as beside the page, and keep which item a question was about as a link
Expand All @@ -279,7 +284,6 @@ per source page; they go when the person is removed. Nothing of this costs a mod
- A file changed in its source is read again as a new asset beside the earlier one (`file.changed` links them);
reading the change into the same asset is to come. A file in a namespace's own folders changed in place is read
again into the same asset with no activity yet.
- Marks on activities whose rows are gone (an event log entry past `events.keep_days`, a deleted asset) stay until
the person is removed; a tidy could drop them. Reactions and comments on such activities likewise.
- Comments on activities that are gone stay; a person could see and clear their own from somewhere.
- The agent's reply in a thread is plain text with links; the answer's excerpts could open at their moment, as in
chat. Tagging a namespace's own assistant by its name, with its memory.
40 changes: 40 additions & 0 deletions fastapi_backend/app/domain/event_marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
from __future__ import annotations

import hashlib
import time

from . import store

R = store.R
FLAGS = ("reviewed", "important", "hidden")
MAX_IDS = 500 # activities one call marks at most
TIDY_BATCH = 500 # activities one tidy looks up at most
TIDY_SECONDS = 3600
TIDY_TABLES = ("event_mark", "event_reaction")
_tidied = {"at": 0.0, "after": ""}


def _ref(account, activity):
Expand Down Expand Up @@ -98,3 +103,38 @@ def keep(acts):
return {a["id"] for a in acts if all(bool((got.get(a["id"]) or {}).get(f)) == v for f, v in wanted.items())}

return keep


def tidy(db, cfg, now=None):
"""Drop the marks and reactions left on activities that are gone (an event log entry past events.keep_days, a
deleted asset): up to TIDY_BATCH activities marked or reacted to over a day ago are looked up, in id order from
where the last tidy stopped, at most once an hour per process (routines.run_due calls it). How many activities it
found gone. Comments on them stay: they are what people wrote, and the asset's page still shows them."""
from . import events

t = time.monotonic()
if now is None and _tidied["at"] and t - _tidied["at"] < TIDY_SECONDS:
return 0
_tidied["at"] = t
cut, after = events.ago(86400, now), _tidied["after"]
ids = sorted(
{
i
for table in TIDY_TABLES
for i in db.values(
f"SELECT VALUE activity FROM {table} WHERE activity > $a AND at < $c ORDER BY activity LIMIT {TIDY_BATCH}",
a=after,
c=cut,
)
if isinstance(i, str)
}
)[:TIDY_BATCH]
_tidied["after"] = ids[-1] if len(ids) == TIDY_BATCH else "" # past the last one, else from the start next time
if not ids:
return 0
there = events.get_many(db, cfg, ids, events.viewer(None, None, admin=True))
gone = [i for i in ids if i not in there]
for table in TIDY_TABLES:
if gone:
db.q(f"DELETE {table} WHERE activity IN $g", g=gone)
return len(gone)
2 changes: 2 additions & 0 deletions fastapi_backend/app/domain/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
analytics,
backups,
budgets,
event_marks,
events,
fedora,
jobs,
Expand Down Expand Up @@ -611,6 +612,7 @@ def run_due(db, cfg, log=print, now=None):
try:
events.tidy(db, cfg) # the event log past events.keep_days and claims past a day, once an hour
match.tidy(db, cfg) # what automations kept between activities, past its expires_at, once an hour
event_marks.tidy(db, cfg) # marks and reactions on activities that are gone, once an hour
except Exception as e: # noqa: BLE001 - the schedule carries on
if log:
log(f"events: {type(e).__name__}: {e}")
Expand Down
28 changes: 28 additions & 0 deletions fastapi_backend/tests/api/test_timeline_marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ def test_one_look_reads_many(db, cfg, folder, monkeypatch):
assert sorted(events.get_many(db, cfg, [*ids, lid], pods)) == sorted(ids[:2])


def test_a_tidy_drops_marks_and_reactions_left_on_activities_that_are_gone(db, cfg, folder, monkeypatch):
from app.domain import event_reactions

monkeypatch.setattr(events, "_KINDS", dict(events._KINDS))
monkeypatch.setitem(event_marks._tidied, "after", "")
events.kind("test_thing.happened", label="Thing happened", source="log", as_type="Announce", object_type="Note")
pods1, pods2, call = (f"create-recording-{i}" for i in seed(db, cfg, folder))
lid = events.emit(db, "test_thing.happened", actor=events.LENS, obj={"type": "Note", "name": "x"}, space=None, summary="Lens noted x")
event_marks.mark(db, 1, [pods1, pods2, call, lid], {"reviewed": True})
for i in (pods1, lid):
event_reactions.react(db, 1, i, "👍")
db.q("UPDATE event_mark SET at = '2026-01-01T00:00:00+00:00'")
db.q("UPDATE event_reaction SET at = '2026-01-01T00:00:00+00:00'")
# the asset deleted and the log entry past events.keep_days; a mark made today on what was never there waits a day
db.q("DELETE $r", r=store.R("recording", int(pods1.rsplit("-", 1)[1])))
db.q("DELETE $r", r=store.R("event_log", int(lid.rsplit("-", 1)[1])))
event_marks.mark(db, 1, ["create-recording-99999"], {"important": True})
# two at a time: the first look stops at the second id and the next carries on from there
monkeypatch.setattr(event_marks, "TIDY_BATCH", 2)
assert event_marks.tidy(db, cfg, now=store.now()) == 1 # create-recording-<pods1>, then create-recording-<pods2>
assert event_marks._tidied["after"] == sorted([pods1, pods2, call, lid])[1]
while event_marks._tidied["after"]:
event_marks.tidy(db, cfg, now=store.now())
event_marks.tidy(db, cfg, now=store.now()) # past the end: from the start again, nothing more to drop
assert sorted(db.values("SELECT VALUE activity FROM event_mark")) == sorted([pods2, call, "create-recording-99999"])
assert db.values("SELECT VALUE activity FROM event_reaction") == []


def test_agents_mark_and_read_what_they_have_not_reviewed(client, db, cfg, folder):
from tests.api.test_mcp import tool, tool_error

Expand Down
Loading
0