Phase 5: iteration & polish (visualizations, goal history, smart tagging)

Final roadmap phase. No DB migration — it reads data already captured.

- Goal-evolution history: GET /discovery/profile/me/goal-history derives a
  per-goal timeline from the profile_revision snapshots (pure aggregator in
  app/services/profile_history.py).
- Smart tagging: POST /discovery/integration/suggest-foundation suggests which
  foundation a task builds toward + rationale/confidence (FoundationTagger,
  app/services/tagging.py). Suggestion only; the person confirms by posting the
  task mapping.
- Deeper goal-refinement: the reflect loop accepts an optional focus ("goals")
  that steers the coach toward sharpening goals — still mirror, not compass.
- Visualizations: visuals.html renders an Ikigai Venn and an Enneagram diagram
  (plain-language callouts, not the raw type number) plus the goal-evolution
  timeline; linked from profile.html.

Tests: 99 passing (added pure goal-history tests, goal-history + suggest
endpoint tests, reflect-focus passthrough; run in-container). README updated.

This completes the ImpactFlow Vision roadmap (Phases 1-5) on the Discovery side.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joel Salmon
2026-06-16 21:37:33 -05:00
parent c4fc1cccd7
commit b9d7b0e22b
14 changed files with 798 additions and 9 deletions
+145
View File
@@ -0,0 +1,145 @@
"""Tests for Phase 5 endpoints: goal-history and smart-tagging suggestion."""
import json
import uuid
from datetime import datetime, timedelta, timezone
import pytest
API_KEY = {"X-API-Key": "test-api-key"}
async def _seed_profile() -> str:
from app.auth import API_KEY_ADMIN_ID, ensure_api_key_admin
from app.database import AsyncSessionLocal
from app.models import DiscoveryConversation, DiscoveryProfile
async with AsyncSessionLocal() as db:
await ensure_api_key_admin(db)
conv = DiscoveryConversation(
id=str(uuid.uuid4()),
user_id=API_KEY_ADMIN_ID,
started_at=datetime.now(timezone.utc),
)
db.add(conv)
await db.commit()
profile = DiscoveryProfile(
id=str(uuid.uuid4()),
user_id=API_KEY_ADMIN_ID,
conversation_id=conv.id,
generated_at=datetime.now(timezone.utc),
triad="gut",
short_term_goals="run a pilot",
long_term_goals="statewide outfit",
)
db.add(profile)
await db.commit()
return profile.id
async def _add_revision(profile_id, short, source, day):
from app.auth import API_KEY_ADMIN_ID
from app.database import AsyncSessionLocal
from app.models import ProfileRevision
async with AsyncSessionLocal() as db:
db.add(
ProfileRevision(
id=str(uuid.uuid4()),
profile_id=profile_id,
user_id=API_KEY_ADMIN_ID,
source=source,
fields_json=json.dumps(
{"short_term_goals": short, "long_term_goals": "statewide outfit"}
),
note=None,
created_at=datetime(2026, 6, day, tzinfo=timezone.utc),
)
)
await db.commit()
# ---- goal history ----------------------------------------------------------
async def test_goal_history_timeline(app_client):
pid = await _seed_profile()
await _add_revision(pid, "run a pilot", "extraction", 1)
await _add_revision(pid, "run a pilot", "manual_edit", 2) # unchanged
await _add_revision(pid, "launch fall cohort", "reflection", 3)
r = await app_client.get(
"/discovery/profile/me/goal-history", headers=API_KEY
)
assert r.status_code == 200
st = r.json()["short_term_goals"]
assert [e["value"] for e in st] == ["run a pilot", "launch fall cohort"]
assert st[-1]["source"] == "reflection"
async def test_goal_history_without_profile_404(app_client):
r = await app_client.get(
"/discovery/profile/me/goal-history", headers=API_KEY
)
assert r.status_code == 404
# ---- smart tagging ---------------------------------------------------------
class FakeTagger:
result = {"foundation": "short_term", "rationale": "builds your pilot", "confidence": "high"}
def __init__(self, api_key=None, model=None):
pass
async def suggest(self, task_label, foundations):
FakeTagger.last_label = task_label
return dict(FakeTagger.result)
@pytest.fixture(autouse=True)
async def fake_tagger(monkeypatch, app_client):
monkeypatch.setattr("app.routers.integration.FoundationTagger", FakeTagger)
yield
async def test_suggest_foundation(app_client):
await _seed_profile()
r = await app_client.post(
"/discovery/integration/suggest-foundation",
headers=API_KEY,
json={"task_label": "practice welding for the cohort"},
)
assert r.status_code == 200
body = r.json()
assert body["foundation"] == "short_term"
assert body["label"] # human label filled in
assert body["confidence"] == "high"
assert FakeTagger.last_label == "practice welding for the cohort"
async def test_suggest_foundation_empty_label_400(app_client):
await _seed_profile()
r = await app_client.post(
"/discovery/integration/suggest-foundation",
headers=API_KEY,
json={"task_label": " "},
)
assert r.status_code == 400
async def test_suggest_foundation_without_profile_404(app_client):
r = await app_client.post(
"/discovery/integration/suggest-foundation",
headers=API_KEY,
json={"task_label": "anything"},
)
assert r.status_code == 404
async def test_suggest_requires_auth(app_client):
r = await app_client.post(
"/discovery/integration/suggest-foundation",
json={"task_label": "x"},
)
assert r.status_code == 401
+47
View File
@@ -0,0 +1,47 @@
"""Unit tests for the pure goal-evolution aggregator."""
from datetime import datetime, timezone
from app.services.profile_history import goal_history
def _rev(short, long_, source, day):
return {
"fields": {"short_term_goals": short, "long_term_goals": long_},
"source": source,
"created_at": datetime(2026, 6, day, tzinfo=timezone.utc),
}
def test_collapses_consecutive_identical_values():
revs = [
_rev("a", "x", "extraction", 1),
_rev("a", "x", "manual_edit", 2), # no change -> skipped
_rev("b", "x", "reflection", 3), # short changed
]
h = goal_history(revs)
assert [e["value"] for e in h["short_term_goals"]] == ["a", "b"]
assert [e["value"] for e in h["long_term_goals"]] == ["x"]
assert h["short_term_goals"][1]["source"] == "reflection"
def test_each_field_tracked_independently():
revs = [
_rev("a", "x", "extraction", 1),
_rev("a", "y", "reflection", 2), # only long changed
]
h = goal_history(revs)
assert len(h["short_term_goals"]) == 1
assert [e["value"] for e in h["long_term_goals"]] == ["x", "y"]
def test_empty_revisions_yield_empty_timelines():
h = goal_history([])
assert h == {"short_term_goals": [], "long_term_goals": []}
def test_first_value_emitted_even_if_none():
revs = [_rev(None, None, "extraction", 1), _rev("a", None, "reflection", 2)]
h = goal_history(revs)
assert [e["value"] for e in h["short_term_goals"]] == [None, "a"]
# long stayed None throughout -> a single None entry
assert [e["value"] for e in h["long_term_goals"]] == [None]
+15 -2
View File
@@ -55,8 +55,10 @@ class FakeCoach:
def __init__(self, api_key=None, model=None):
pass
async def reflect(self, profile, history):
FakeCoach.calls.append({"profile": profile, "history": list(history)})
async def reflect(self, profile, history, focus=""):
FakeCoach.calls.append(
{"profile": profile, "history": list(history), "focus": focus}
)
return dict(FakeCoach.response)
@@ -180,6 +182,17 @@ async def test_empty_message_after_opener_returns_400(app_client):
assert r.status_code == 400
async def test_reflect_passes_focus_to_coach(app_client):
"""Phase 5: a goal-refinement focus reaches the coach."""
await _seed_profile()
await app_client.post(
"/discovery/profile/me/reflect",
headers=API_KEY,
json={"message": "let's sharpen my goals", "focus": "goals"},
)
assert FakeCoach.calls[-1]["focus"] == "goals"
async def test_reflect_requires_auth(app_client):
r = await app_client.post("/discovery/profile/me/reflect", json={})
assert r.status_code == 401
+14
View File
@@ -75,6 +75,20 @@ def test_profile_page_links_to_dashboard():
assert "/static/dashboard.html" in html
def test_visuals_page_reads_profile_and_goal_history():
"""Phase 5 visualizations read the profile and goal history via authedFetch."""
html = Path("app/static/visuals.html").read_text(encoding="utf-8")
assert "/discovery/profile/me" in html
assert "/discovery/profile/me/goal-history" in html
assert "authedFetch" in html
assert "user_id" not in html
def test_profile_page_links_to_visuals():
html = Path("app/static/profile.html").read_text(encoding="utf-8")
assert "/static/visuals.html" in html
def test_auth_helper_sends_credentials_and_refreshes():
js = Path("app/static/auth.js").read_text(encoding="utf-8")