Files
scientific-agent-skills/scientific-skills/autoskill/tests/test_cluster.py
T
Renato Umeton 412d3f22ae add: autoskill — synthesize new skills from observed local screen activity
A meta-skill that observes a researcher's own screen activity via a local
screenpipe daemon, clusters repeated workflow patterns, compares each
cluster against this repo's existing 135 skills using local embeddings,
and drafts either a thin composition recipe (a SKILL.md that chains
existing skills) or a full new SKILL.md when no combination fits.

Key design properties

- On-demand, no daemon of its own; invoked by the user with a time window.
- Local-first. Detection, clustering, redaction, and embeddings all run
  on-device. Screenpipe's native app/window filters run at capture time;
  a defense-in-depth regex pass scrubs emails, JWTs, SSH private-key
  blocks, known-env-var secret assignments, Slack / HuggingFace / OpenAI
  / GitHub / AWS / Google / Stripe tokens, US SSNs, and US phone numbers
  before any cluster summary reaches the LLM.
- LLM backend user-selectable, default local via LM Studio
  (Gemma-4-31B-it recommended). Opt-in Anthropic or BYOK Foundry
  backends sit behind the same interface.
- sentence-transformers/all-MiniLM-L6-v2 indexes every SKILL.md
  frontmatter description locally. For each cluster, top-k matches go to
  the LLM, which emits reuse / compose / novel and a SKILL.md body where
  appropriate.
- Staged output lives under ~/.autoskill/proposed/<ts>/ (default,
  outside the repo tree). A promote CLI moves approved ones into
  scientific-skills/<name>/.
- Unified CLI: autoskill run | doctor | promote. doctor preflights
  screenpipe reachability + auth, the configured LLM backend, the
  config, and the skills directory.

What's shipped

- 8 pipeline scripts (fetch_window, redact, cluster, match_skills,
  synthesize, promote, backends, run) plus the autoskill.py dispatcher.
- 83 pytest tests: unit, integration, and end-to-end with
  httpx.MockTransport fakes for screenpipe and LM Studio.
- Verified live end-to-end on macOS: source-built screenpipe daemon
  (17k+ real OCR events captured) + real all-MiniLM-L6-v2 embedding the
  full 135-skill index + real Gemma-4-31B-it at 128K context returning a
  parseable JSON verdict, producing a report.md with sensible top-k
  matches.
- SKILL.md frontmatter matches the convention used by literature-review,
  hypothesis-generation, and other sibling skills.
- skill-scanner scan scientific-skills/autoskill --use-behavioral run
  per the repo's CONTRIBUTING requirement; findings profile (15 total,
  5 critical, 8 medium, 2 low) is in line with already-merged script-
  shipping peers (literature-review: 14/3/8/2, citation-management:
  17/5/10/1). Remaining findings are intentional-behavior flags
  covering env-var-based auth to the user-configured LLM and screenpipe
  endpoints; these are documented explicitly in SKILL.md.

Tracked by issue #140.
2026-04-17 16:06:17 -04:00

68 lines
2.7 KiB
Python

from cluster import segment_sessions, cluster_sessions
def _evt(ts: int, app: str, title: str = "", text: str = "") -> dict:
return {"ts": ts, "app": app, "window_title": title, "text": text}
def test_contiguous_events_form_one_session():
events = [_evt(i * 30, "VSCode") for i in range(20)] # 10 minutes
sessions = segment_sessions(events, idle_gap_seconds=600, min_session_seconds=300)
assert len(sessions) == 1
assert sessions[0]["apps"] == ["VSCode"]
assert sessions[0]["duration_seconds"] == 19 * 30
def test_idle_gap_splits_sessions():
left = [_evt(i * 30, "Chrome") for i in range(20)] # 0..570
right = [_evt(10000 + i * 30, "Chrome") for i in range(20)] # gap of ~9400s
sessions = segment_sessions(left + right, idle_gap_seconds=600, min_session_seconds=300)
assert len(sessions) == 2
def test_sessions_below_minimum_are_dropped():
events = [_evt(i * 10, "Finder") for i in range(5)] # 40 seconds total
sessions = segment_sessions(events, idle_gap_seconds=600, min_session_seconds=300)
assert sessions == []
def test_session_records_distinct_apps_in_order_of_first_appearance():
events = [
_evt(0, "Chrome"), _evt(30, "Chrome"),
_evt(60, "Zotero"), _evt(90, "Zotero"),
_evt(120, "VSCode"), _evt(600, "VSCode"),
]
sessions = segment_sessions(events, idle_gap_seconds=600, min_session_seconds=300)
assert len(sessions) == 1
assert sessions[0]["apps"] == ["Chrome", "Zotero", "VSCode"]
def test_clusters_group_sessions_with_identical_app_signatures():
sessions = [
{"apps": ["Chrome", "Zotero"], "duration_seconds": 600, "window_titles": ["PubMed"]},
{"apps": ["Chrome", "Zotero"], "duration_seconds": 800, "window_titles": ["bioRxiv"]},
{"apps": ["VSCode"], "duration_seconds": 900, "window_titles": ["paper.tex"]},
]
clusters = cluster_sessions(sessions, min_cluster_size=2)
assert len(clusters) == 1
assert clusters[0]["apps"] == ["Chrome", "Zotero"]
assert clusters[0]["session_count"] == 2
def test_cluster_summary_exposes_total_duration_and_example_titles():
sessions = [
{"apps": ["Chrome"], "duration_seconds": 300, "window_titles": ["arXiv"]},
{"apps": ["Chrome"], "duration_seconds": 500, "window_titles": ["Nature"]},
]
clusters = cluster_sessions(sessions, min_cluster_size=2)
assert clusters[0]["total_duration_seconds"] == 800
assert set(clusters[0]["example_titles"]) == {"arXiv", "Nature"}
def test_singleton_clusters_are_dropped_when_below_min_cluster_size():
sessions = [
{"apps": ["Slack"], "duration_seconds": 400, "window_titles": ["general"]},
]
clusters = cluster_sessions(sessions, min_cluster_size=2)
assert clusters == []