8000 perf(timeline): reuse a person's For you affinity for a minute while their signals stay the same by adeelahmad · Pull Request #671 · 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-for-you-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For you on the timeline reuses each person's affinity for a minute while their reactions, comments, marks and opens
stay the same, so paging through it looks up fewer activities.
2 changes: 1 addition & 1 deletion docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ request, to another check that proves the same thing; loosening a requirement or
| Entities and topics in a knowledge graph to explore and query with Cypher or SPARQL | [test_graph.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_graph.py) `test_query`; [test_rdf.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_rdf.py) `test_sparql`; [journeys.mjs](https://github.com/adeelahmad/lense/blob/main/ci/clicks/journeys.mjs) `graph-find` |
| The graph as it was at any version | [test_graph_history.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_graph_history.py) `test_the_explorer_and_cypher_as_of_a_version`, `test_the_canvas_as_of_a_version`; [test_graph_history.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_graph_history.py) `test_every_curation_is_an_event_and_every_version_replays` |
| Sources, sensors, people and agents as actors in the graph, with ADDED links to what they brought in, on the canvas, in the explorer and in Cypher | [test_graph.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_graph.py) `test_sources_are_actors_in_the_graph`; jest [graph-explorer.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/graph-explorer.test.ts) `shows a source only while something it brought in is visible` |
| A For you order of the timeline, learned from each person's own reactions, comments, marks and opens, each item saying why, with Latest the default; through the API, MCP, the assistant and the page | [test_timeline_for_you.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_for_you.py) `test_for_you_learns_from_reactions_comments_and_marks_and_says_why`, `test_opening_an_item_counts_for_you_a_little_each_time_up_to_a_few`, `test_agents_read_for_you_too`; jest [timeline-model.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-model.test.ts) `keep the For you order as a view, not a filter`, [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `For you: the toggle goes in the address, each placed line says why, and Latest stays the default` |
| A For you order of the timeline, learned from each person's own reactions, comments, marks and opens, each item saying why, with Latest the default; through the API, MCP, the assistant and the page | [test_timeline_for_you.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_for_you.py) `test_for_you_learns_from_reactions_comments_and_marks_and_says_why`, `test_opening_an_item_counts_for_you_a_little_each_time_up_to_a_few`, `test_a_persons_affinity_is_reused_for_a_minute_until_a_signal_changes`, `test_agents_read_for_you_too`; jest [timeline-model.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-model.test.ts) `keep the For you order as a view, not a filter`, [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `For you: the toggle goes in the address, each placed line says why, and Latest stays the default` |
| Muting a source, a person or a kind on the timeline, for good or until a time, for the person alone, with nothing deleted; through the API, MCP, the assistant (asking first after outside text) and the page | [test_timeline_mutes.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_mutes.py) `test_a_mute_hides_an_actor_or_a_kind_for_the_person_alone`, `test_agents_mute_too_and_the_assistant_asks_first_after_outside_text`; jest [timeline-ui.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-ui.test.tsx) `muting a source takes everything from it out, lists it as muted, and Undo or the chip brings it back`, `the menu beside Mute snoozes a source for an hour, a day or a week, or mutes the item's kind`, `a snooze can end at a time you pick, nine tomorrow to start with, and Enter mutes`, [timeline-model.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/timeline-model.test.ts) `a snooze ends an hour, a day or a week from now`, `a snooze to a time you pick starts at nine tomorrow, in the browser's own time` |
| An asset moved to another namespace is `asset.moved`, a Move seen where it is now, and a sensor a person pauses, ignores, resumes or sets back to new is `sensor.changed`, an Update with the status it had before, seen in its namespace or by admins; a change that changes nothing logs nothing | [test_timeline_verbs.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_timeline_verbs.py) `test_moving_an_asset_is_a_move_seen_where_it_is_now`, `test_switching_a_sensor_is_an_update_with_what_it_was` |
| A file a watched folder finds changed in its source and reads again is `file.changed`, an Update linking the asset it was before, not a second `file.added`; it is still a child of `asset.added`, so triggers on new assets fire on it as before | [test_events_content.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_events_content.py) `test_a_file_changed_in_its_source_is_file_changed_linked_to_the_asset_it_was_before` |
Expand Down
4 changes: 3 additions & 1 deletion docs/timeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ nobody's reactions or marks change anyone else's order, and no model is called.
it. An item with no signal in common keeps its recency alone.
- A page takes the 200 newest items from where it starts, orders them by score and shows the best `limit`; **Older**
moves past all 200. With no signals yet, For you is Latest, and the page says so.
- Your signals are read on every page, but the sum over the activities they were on is kept for a minute while they
stay the same, so paging and polling don't look those activities up again; a new reaction, comment, mark or open
changes the signals and shows at once.
- `why` on the page names the strongest signal that placed each item ("You often react to Work Gmail", "About St
Mary's School, which you often react to", "You hid items from Dropbox before"), shown under the item.
- `GET /api/v1/activities?order=for_you` is the same for agents, with `why` by id; the other filters apply as in
Expand Down Expand Up @@ -282,7 +285,6 @@ has any more. Comments on them stay, since they are what people wrote, and an as
back to it.

- Server-sent updates instead of the page polling for new items.
- For you could cache each person's affinity for a minute rather than summing it on every read.
- A muted actor's own line in the Filters.
- 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
Expand Down
23 changes: 22 additions & 1 deletion fastapi_backend/app/domain/for_you.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import hashlib
import math
import time

from . import events, store

Expand All @@ -23,6 +24,9 @@
OPENS = 4 # opens of one item that count at most, so reading one thing again and again doesn't drown the rest
SCALE = 4.0 # affinity this strong doubles (or, negative, halves) an item's score
ORDERS = ("latest", "for_you")
CACHE_SECONDS = 60.0 # how long a person's affinity is reused while their signals stay the same
CACHE_SIZE = 256 # people whose affinity is kept at most, per process
_cache: dict = {}


def signals(db, account):
Expand Down Expand Up @@ -63,8 +67,25 @@ def opened(db, account, activity):

def affinity(db, cfg, who):
"""{actor: {id: weight}, kind: {name: weight}, entity: {id: (weight, name)}, n}: the person's signals summed
over the actor, kind and tagged entities of the activities they were on; `n` is how many counted."""
over the actor, kind and tagged entities of the activities they were on; `n` is how many counted. The signals are
read on every call (small, indexed queries); looking up the activities they were on is the costly part, so the sum
is reused for CACHE_SECONDS while the person, what they may see and their signals are all the same. A new
reaction, comment, mark or open changes the signals, so it shows at once."""
sig = signals(db, who.get("account"))
spaces = who.get("spaces")
key = (who.get("account"), bool(who.get("admin")), None if spaces is None else tuple(sorted(map(str, spaces))))
now = time.monotonic()
hit = _cache.get(key)
if hit and hit[0] == sig and now - hit[1] < CACHE_SECONDS:
return hit[2]
out = _sum(db, cfg, who, sig)
if len(_cache) >= CACHE_SIZE and key not in _cache:
_cache.pop(min(_cache, key=lambda k: _cache[k][1]))
_cache[key] = (sig, now, out)
return out


def _sum(db, cfg, who, sig):
acts = events.get_many(db, cfg, [a for a, _ in sig], who) if sig else {}
out = {"actor": {}, "kind": {}, "entity": {}, "n": 0}
for aid, w in sig:
Expand Down
27 changes: 27 additions & 0 deletions fastapi_backend/tests/api/test_timeline_for_you.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ def test_opening_an_item_counts_for_you_a_little_each_time_up_to_a_few(client, d
assert client.post(f"{URL}/no-such-thing/open", headers=h).status_code == 404


def test_a_persons_affinity_is_reused_for_a_minute_until_a_signal_changes(client, db, cfg, folder, monkeypatch):
env = _env(client, db, cfg, folder)
pods1, pods2, call = env["ids"]
h = env["hr"]
looked = []
get_many = for_you.events.get_many
monkeypatch.setattr(for_you.events, "get_many", lambda *a, **k: looked.append(1) or get_many(*a, **k))
clock = [1000.0]
monkeypatch.setattr(for_you.time, "monotonic", lambda: clock[0])
for_you_page = {**ASSETS, "order": "for_you"}
client.post(f"{URL}/{pods1}/react", json={"emoji": "👍"}, headers=h)
first = client.get(URL, params=for_you_page, headers=h).json()
n = len(looked)
# the same signals within the minute: the same order, without looking the activities up again
assert client.get(URL, params=for_you_page, headers=h).json()["items"] == first["items"]
assert len(looked) == n
# a new signal shows at once
client.post(f"{URL}/{pods1}/mark", json={"hidden": True}, headers=h)
assert _ids(client.get(URL, params=for_you_page, headers=h).json()) == [call, pods2, pods1]
assert len(looked) > n
# and after the minute it is summed again, as the activities it was on may have changed
n = len(looked)
clock[0] += for_you.CACHE_SECONDS + 1
client.get(URL, params=for_you_page, headers=h)
assert len(looked) > n


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

Expand Down
3 changes: 2 additions & 1 deletion fastapi_backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest # noqa: E402
from fastapi.testclient import TestClient # noqa: E402

from app.domain import auth, store # noqa: E402
from app.domain import auth, for_you, store # noqa: E402

# Tests use an in-memory SurrealDB; LENS_TEST_SURREAL_URL=ws://host:8000 runs them against a server instead
# (each test gets its own database there).
Expand Down Expand Up @@ -56,6 +56,7 @@ def cfg(folder):
def db(cfg):
conn = store.connect(cfg)
auth._FAILS.clear()
for_you._cache.clear()
yield conn
conn.close()

Expand Down
Loading
0