Phase 4: task-to-goal integration (Vision side)

Build the boundary the ImpactFlow core time-tracker plugs into. A task maps to
a foundation — one of the six stable profile elements (love, strength, mission,
vocation, short_term, long_term) — so the tracker can ask "which goal does this
build toward?" and post the answer back to Vision.

- Models + migration 006: task_mapping (one row per logged time entry).
- app/services/foundations.py: the six foundations + a pure, testable
  work-pattern aggregator (rollup) and a plain-language summary.
- app/routers/integration.py (user-scoped; tracker calls as the user or via
  X-API-Key): GET /foundations, POST/GET /task-mappings,
  GET /work-patterns?days=N (per-foundation minutes/share/neglected).
- Reminder engine now pulls from real work patterns: CheckinCoach takes an
  optional work-pattern summary (last 14 days) and reflects where time has gone
  against the person's own words — an observation, never a verdict.
- Frontend: dashboard.html (time per foundation + neglected); linked from
  profile.html.

Documented the core-tracker integration contract in the README. Phase 4
completes the Vision module's roadmap on the Discovery side; the core tracker
integrates by calling these endpoints.

Tests: 86 passing (added pure-aggregator, integration-endpoint, and
work-pattern-into-check-in tests; run in-container).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joel Salmon
2026-06-16 21:24:58 -05:00
parent 50453901b3
commit c4fc1cccd7
16 changed files with 857 additions and 5 deletions
+26 -1
View File
@@ -45,10 +45,13 @@ async def _seed_profile(locked: bool = False, triad: str = "gut") -> str:
class FakeCheckinCoach:
body = 'You said you want to "run a pilot welding cohort." Does that still feel true?'
last_work_patterns = "unset"
def __init__(self, api_key=None, model=None):
pass
async def generate(self, profile, prefs):
async def generate(self, profile, prefs, work_patterns=None):
FakeCheckinCoach.last_work_patterns = work_patterns
return FakeCheckinCoach.body
@@ -181,6 +184,28 @@ async def test_run_batch_skips_unlocked_profile(app_client):
assert r["considered"] == 0
async def test_checkin_receives_recent_work_patterns(app_client):
"""Phase 4 wiring: a generated check-in is fed the recent work-pattern
summary so the coach can reflect where time has gone."""
await _seed_profile()
await app_client.post(
"/discovery/integration/task-mappings",
headers=API_KEY,
json={"external_task_id": "t1", "foundation": "short_term", "minutes": 90},
)
FakeCheckinCoach.last_work_patterns = "unset"
await app_client.post("/discovery/coaching/checkins", headers=API_KEY)
assert FakeCheckinCoach.last_work_patterns is not None
assert "last 14 days" in FakeCheckinCoach.last_work_patterns
async def test_checkin_without_work_patterns_passes_none(app_client):
await _seed_profile()
FakeCheckinCoach.last_work_patterns = "unset"
await app_client.post("/discovery/coaching/checkins", headers=API_KEY)
assert FakeCheckinCoach.last_work_patterns is None
async def test_due_logic_unit():
from app.routers.coaching import _is_due
+57
View File
@@ -0,0 +1,57 @@
"""Unit tests for the pure work-pattern aggregator."""
from datetime import datetime, timezone
from app.services.foundations import FOUNDATIONS, rollup, work_pattern_text
def _dt(day):
return datetime(2026, 6, day, tzinfo=timezone.utc)
def test_rollup_sums_minutes_and_counts_per_foundation():
entries = [
{"foundation": "short_term", "minutes": 120, "occurred_at": _dt(10)},
{"foundation": "short_term", "minutes": 60, "occurred_at": _dt(12)},
{"foundation": "vocation", "minutes": 60, "occurred_at": _dt(11)},
]
out = rollup(entries)
assert out["total_minutes"] == 240
top = out["by_foundation"][0]
assert top["foundation"] == "short_term"
assert top["minutes"] == 180
assert top["task_count"] == 2
assert top["last_at"] == _dt(12)
assert round(top["share"], 2) == 0.75
def test_rollup_covers_all_foundations_and_lists_neglected():
out = rollup([{"foundation": "love", "minutes": 30, "occurred_at": _dt(9)}])
assert len(out["by_foundation"]) == len(FOUNDATIONS)
# Every foundation except 'love' has zero minutes.
assert set(out["neglected"]) == set(FOUNDATIONS) - {"love"}
def test_rollup_empty_is_zero_and_all_neglected():
out = rollup([])
assert out["total_minutes"] == 0
assert all(f["minutes"] == 0 for f in out["by_foundation"])
assert set(out["neglected"]) == set(FOUNDATIONS)
def test_rollup_ignores_unknown_foundation():
out = rollup([{"foundation": "nonsense", "minutes": 99, "occurred_at": _dt(9)}])
assert out["total_minutes"] == 0
def test_work_pattern_text_summarizes_activity():
out = rollup([
{"foundation": "short_term", "minutes": 100, "occurred_at": _dt(10)},
])
text = work_pattern_text(out, 14)
assert "last 14 days" in text
assert "Near-term goals" in text
assert "Nothing was logged toward" in text
def test_work_pattern_text_none_when_no_activity():
assert work_pattern_text(rollup([]), 14) is None
+143
View File
@@ -0,0 +1,143 @@
"""Tests for the Phase 4 task-to-goal integration endpoints."""
import uuid
from datetime import datetime, timedelta, timezone
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 welding cohort",
long_term_goals="a statewide trades outfit",
)
db.add(profile)
await db.commit()
return profile.id
async def test_foundations_lists_six_with_profile_text(app_client):
await _seed_profile()
r = await app_client.get(
"/discovery/integration/foundations", headers=API_KEY
)
assert r.status_code == 200
items = r.json()
assert len(items) == 6
keys = {i["key"] for i in items}
assert keys == {"love", "strength", "mission", "vocation", "short_term", "long_term"}
short = next(i for i in items if i["key"] == "short_term")
assert short["text"] == "run a pilot welding cohort"
async def test_foundations_without_profile_404(app_client):
r = await app_client.get(
"/discovery/integration/foundations", headers=API_KEY
)
assert r.status_code == 404
async def test_create_task_mapping(app_client):
await _seed_profile()
r = await app_client.post(
"/discovery/integration/task-mappings",
headers=API_KEY,
json={
"external_task_id": "task-123",
"foundation": "short_term",
"minutes": 90,
"task_label": "weld practice",
},
)
assert r.status_code == 200
body = r.json()
assert body["foundation"] == "short_term"
assert body["minutes"] == 90
assert body["external_task_id"] == "task-123"
async def test_invalid_foundation_rejected(app_client):
await _seed_profile()
r = await app_client.post(
"/discovery/integration/task-mappings",
headers=API_KEY,
json={"external_task_id": "t", "foundation": "vibes", "minutes": 10},
)
assert r.status_code == 400
async def test_negative_minutes_rejected(app_client):
await _seed_profile()
r = await app_client.post(
"/discovery/integration/task-mappings",
headers=API_KEY,
json={"external_task_id": "t", "foundation": "love", "minutes": -5},
)
assert r.status_code == 400
async def _post_mapping(app_client, foundation, minutes, occurred_at=None):
payload = {
"external_task_id": str(uuid.uuid4()),
"foundation": foundation,
"minutes": minutes,
}
if occurred_at:
payload["occurred_at"] = occurred_at
return await app_client.post(
"/discovery/integration/task-mappings", headers=API_KEY, json=payload
)
async def test_work_patterns_aggregate(app_client):
await _seed_profile()
await _post_mapping(app_client, "short_term", 120)
await _post_mapping(app_client, "short_term", 60)
await _post_mapping(app_client, "vocation", 60)
r = await app_client.get(
"/discovery/integration/work-patterns?days=30", headers=API_KEY
)
assert r.status_code == 200
data = r.json()
assert data["total_minutes"] == 240
top = data["by_foundation"][0]
assert top["foundation"] == "short_term"
assert top["minutes"] == 180
assert "long_term" in data["neglected"]
assert "mission" in data["neglected"]
async def test_work_patterns_window_excludes_old(app_client):
await _seed_profile()
old = (datetime.now(timezone.utc) - timedelta(days=40)).isoformat()
await _post_mapping(app_client, "love", 100, occurred_at=old)
await _post_mapping(app_client, "strength", 50) # recent
data = (await app_client.get(
"/discovery/integration/work-patterns?days=7", headers=API_KEY
)).json()
assert data["total_minutes"] == 50 # only the recent one
assert "love" in data["neglected"]
async def test_task_mappings_requires_auth(app_client):
r = await app_client.get("/discovery/integration/work-patterns")
assert r.status_code == 401
+13
View File
@@ -62,6 +62,19 @@ def test_profile_page_links_to_coaching():
assert "/static/coaching.html" in html
def test_dashboard_page_uses_work_patterns_endpoint():
"""Phase 4 dashboard reads work patterns via authedFetch."""
html = Path("app/static/dashboard.html").read_text(encoding="utf-8")
assert "/discovery/integration/work-patterns" in html
assert "authedFetch" in html
assert "user_id" not in html
def test_profile_page_links_to_dashboard():
html = Path("app/static/profile.html").read_text(encoding="utf-8")
assert "/static/dashboard.html" in html
def test_auth_helper_sends_credentials_and_refreshes():
js = Path("app/static/auth.js").read_text(encoding="utf-8")