diff --git a/alembic/versions/0137_lesson_situations.py b/alembic/versions/0137_lesson_situations.py new file mode 100644 index 00000000..dec4c9b3 --- /dev/null +++ b/alembic/versions/0137_lesson_situations.py @@ -0,0 +1,58 @@ +"""lesson_situations — when a lesson applies, as moments and triggers + +Revision ID: 0137 +Revises: 0136 +Create Date: 2026-11-25 + +One row per (lesson, kind, value). `kind` is `moment` (a catalog moment +key), `trigger` (a typed predicate on the act, in rule_field_judgments' +trigger_value form) or `none` (no situation fits; value ''). `state` is +`suggested` while a pass's proposal waits, `confirmed` or `rejected` once a +person judges it; only confirmed rows deliver. CASCADE on the lesson: a +situation of a record that no longer exists says nothing. No backfill: no +lesson had a situation before this table. +""" +import sqlalchemy as sa +from alembic import op + +revision = "0137" +down_revision = "0136" +branch_labels = None +depends_on = None + +# One place each, so the CHECKs and the model's tuples cannot drift (a new +# value later means DROP + ADD CONSTRAINT in the same migration). +_KINDS = ("moment", "trigger", "none") +_STATES = ("suggested", "confirmed", "rejected") +_SOURCES = ("pass", "edit") + + +def _in(values) -> str: + return "(" + ", ".join(f"'{v}'" for v in values) + ")" + + +def upgrade() -> None: + op.create_table( + "lesson_situations", + sa.Column("id", sa.BigInteger(), primary_key=True), + sa.Column("lesson_id", sa.BigInteger(), sa.ForeignKey("notes.id", ondelete="CASCADE"), nullable=False), + sa.Column("kind", sa.Text(), nullable=False), + sa.Column("value", sa.Text(), nullable=False, server_default=""), + sa.Column("state", sa.Text(), nullable=False, server_default="suggested"), + sa.Column("source", sa.Text(), nullable=False, server_default="pass"), + sa.Column("note", sa.Text(), nullable=True), + sa.Column("judged_at", sa.DateTime(timezone=True), nullable=True), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")), + sa.UniqueConstraint("lesson_id", "kind", "value", name="uq_lesson_situations_value"), + ) + op.create_check_constraint("ck_lesson_situations_kind", "lesson_situations", "kind IN " + _in(_KINDS)) + op.create_check_constraint("ck_lesson_situations_state", "lesson_situations", "state IN " + _in(_STATES)) + op.create_check_constraint("ck_lesson_situations_source", "lesson_situations", "source IN " + _in(_SOURCES)) + op.create_index("ix_lesson_situations_lesson_id", "lesson_situations", ["lesson_id"]) + + +def downgrade() -> None: + op.drop_index("ix_lesson_situations_lesson_id", table_name="lesson_situations") + for name in ("source", "state", "kind"): + op.drop_constraint(f"ck_lesson_situations_{name}", "lesson_situations", type_="check") + op.drop_table("lesson_situations") diff --git a/docs/api-reference.md b/docs/api-reference.md index 1c988f0c..3afe697e 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -114,6 +114,10 @@ endpoint at `/mcp`, not these REST routes. | GET | `/api/lessons/rule-candidates` | Rules a draft lesson resembles, before it is saved | | GET | `/api/lessons/taught-by/:record_id` | Lessons drawn from one record | | PUT | `/api/lessons/:id/rules/:rule_id` | Confirm or reject a lesson → rule link | +| GET / POST | `/api/lessons/:id/situations` | A lesson's situations (moments and triggers it arrives at); add one | +| DELETE | `/api/lessons/situations/:situation_id` | Take a situation off its lesson | +| GET | `/api/lessons/situations/proposals` | Situations a pass proposed, waiting on a judgment, with counts | +| POST | `/api/lessons/situations/judge` | Confirm or reject proposed situations | ## Design systems diff --git a/frontend/src/api/lessons.ts b/frontend/src/api/lessons.ts index 4de28443..66f54cbf 100644 --- a/frontend/src/api/lessons.ts +++ b/frontend/src/api/lessons.ts @@ -1,7 +1,7 @@ import type { RecordUsage } from "@/types/usage"; import { apiGet, apiPost, apiPut, apiPatch, apiDelete } from "@/api/client"; -import type { RuleKind } from "@/api/rulebooks"; +import type { RuleKind, RuleTrigger } from "@/api/rulebooks"; /** Where a lesson stands on "which rule is this an instance of?" * (`services/lesson_rules.py`: LINKED / NO_RULE / UNJUDGED). Three answers, @@ -11,8 +11,8 @@ import type { RuleKind } from "@/api/rulebooks"; * another one fits. */ export type RuleJudgment = "linked" | "no_rule" | "unjudged"; -/** A link's state (`models/lesson_rule_link.py` LINK_STATES). */ -export type LinkState = "suggested" | "confirmed" | "rejected"; +/** A judged link's state; the reads list only these two. */ +export type LinkState = "confirmed" | "rejected"; /** One link from a lesson to a rule, in the reader's view: only rules the * reader owns are listed (`rules_for_lessons`). */ @@ -20,14 +20,11 @@ export interface LessonRuleLink { id: number; title: string; kind: RuleKind; - /** `suggested` carries nothing in retrieval until it is judged; only - * `confirmed` changes what surfaces; `rejected` is kept so the pair is - * never proposed again. */ + /** `confirmed` changes what surfaces; `rejected` is kept so the reason + * stays readable. */ state: LinkState; /** Why it was confirmed or rejected. */ note: string; - /** What a suggestion rests on — sent on suggested links only. */ - evidence?: { situations: number; projects: number; co_surfaced: number }; } /** A lesson: a transferable insight, retrievable by the SITUATION it applies @@ -79,7 +76,7 @@ export interface Lesson { updated_at: string | null; systems?: { id: number; name: string }[]; usage?: RecordUsage; - /** The rules this lesson points at, confirmed first, then suggested, then + /** The rules this lesson was judged against, confirmed first, then * rejected. Absent (with `rule_judgment`) when the links could not be read * — which is "not attached", never "no rule". */ rules?: LessonRuleLink[]; @@ -232,3 +229,77 @@ export function lessonsTaughtBy( `/api/lessons/taught-by/${recordId}`, ); } + +// ── when a lesson applies (services/lesson_situations.py) ──────────────── + +/** `moment` — a catalog moment; `trigger` — a typed predicate on the act. */ +export type SituationKind = "moment" | "trigger"; + +/** One situation of a lesson: live (`confirmed`) or waiting on a person + * (`suggested`). `describes` is how it reads in the line a session sees. */ +export interface LessonSituation { + id: number; + lesson_id: number; + kind: SituationKind; + value: string; + state: "suggested" | "confirmed"; + source: "pass" | "edit"; + note: string; + describes: string; + trigger?: RuleTrigger; +} + +export interface LessonSituationProposals { + lessons: { + lesson_id: number; + title: string; + when_to_apply: string; + proposals: LessonSituation[]; + }[]; + total: number; + /** Readable lessons by where they stand: arriving at a situation, read + * and found to have none, waiting on a judgment, or not yet read. */ + counts: { situated: number; none_fits: number; waiting: number; unsituated: number }; +} + +export interface LessonSituationsPayload { + situations: LessonSituation[]; + catalog: { name: string; means: string }[]; + trigger_kinds: RuleTrigger["kind"][]; +} + +export interface SituationJudgment { + id: number; + verdict: "confirm" | "reject"; + note?: string; +} + +export function getLessonSituationProposals(): Promise { + return apiGet("/api/lessons/situations/proposals"); +} + +/** A confirm makes the lesson arrive at that situation, once per session; a + * reject stops the value being proposed again. */ +export function judgeLessonSituations( + judgments: SituationJudgment[], +): Promise<{ judged: LessonSituation[] }> { + return apiPost("/api/lessons/situations/judge", { judgments }); +} + +export function getLessonSituations(lessonId: number): Promise { + return apiGet(`/api/lessons/${lessonId}/situations`); +} + +/** Make one situation live: a moment, or a trigger. */ +export function addLessonSituation( + lessonId: number, + body: { moment: string } | { trigger: RuleTrigger }, +): Promise { + return apiPost(`/api/lessons/${lessonId}/situations`, body); +} + +/** Take a situation off its lesson; kept as rejected so it is not proposed + * again. `apiDelete` discards the body. */ +export function removeLessonSituation(situationId: number): Promise { + return apiDelete(`/api/lessons/situations/${situationId}`); +} diff --git a/frontend/src/api/moments.ts b/frontend/src/api/moments.ts index 180aca27..0815a37c 100644 --- a/frontend/src/api/moments.ts +++ b/frontend/src/api/moments.ts @@ -290,6 +290,9 @@ export interface RecordKindsDoor { default: string[]; /** The user changed this door; otherwise it follows the shipped default. */ changed: boolean; + /** Its lines are recorded per session; a door that answers in a tool + * reply is not, so it has no counts. */ + measured: boolean; /** Lines this door pushed of each kind, and how many were opened. */ evidence: Record; } @@ -311,7 +314,7 @@ export function setRecordKinds(door: string, kinds: string[] | null): Promise +import { computed, onMounted, ref } from "vue"; +import { + getLessonSituationProposals, judgeLessonSituations, + type LessonSituation, type LessonSituationProposals, +} from "@/api/lessons"; +import { apiErrorMessage } from "@/api/client"; +import { useToastStore } from "@/stores/toast"; +import ConfirmDialog from "@/components/ConfirmDialog.vue"; + +/** + * When each lesson applies, waiting on a person. A pass read each lesson + * beside the rules it is an instance of and proposed the moments of work, or + * the triggers on an act (a path, a command, a tool, a word), at which it + * should arrive. A lesson with a confirmed situation arrives there, once per + * session, instead of whenever an edit's words resemble it. Nothing changes + * until "Apply" is pressed; "Not this" keeps the value from being proposed + * again. The same judgment `judge_lesson_situations` makes in a session. + */ +const toast = useToastStore(); + +const data = ref(null); +const failed = ref(false); +const busy = ref(null); +const confirmingAll = ref(false); + +const lessons = computed(() => data.value?.lessons ?? []); +const counts = computed(() => data.value?.counts); +const allIds = computed(() => lessons.value.flatMap((l) => l.proposals.map((p) => p.id))); + +const allSummary = computed(() => { + const n = allIds.value.length; + const across = `${lessons.value.length} lesson${lessons.value.length === 1 ? "" : "s"}`; + return `${n} situation${n === 1 ? "" : "s"} across ${across} take effect at once: each lesson ` + + "will arrive when its situation is reached, once per session."; +}); + +async function load() { + try { + data.value = await getLessonSituationProposals(); + failed.value = false; + } catch { + failed.value = true; + } +} + +async function decide(p: LessonSituation, verdict: "confirm" | "reject") { + busy.value = p.id; + try { + await judgeLessonSituations([{ + id: p.id, verdict, + note: verdict === "confirm" ? "Applied in Settings." : "Declined in Settings.", + }]); + await load(); + } catch (e) { + toast.show(apiErrorMessage(e, "Could not record that"), "error"); + } finally { + busy.value = null; + } +} + +async function applyAll() { + confirmingAll.value = false; + busy.value = "all"; + try { + const out = await judgeLessonSituations(allIds.value.map((id) => ({ + id, verdict: "confirm" as const, note: "Applied with Apply all in Settings.", + }))); + toast.show(`${out.judged.length} applied.`, "success"); + await load(); + } catch (e) { + toast.show(apiErrorMessage(e, "Could not apply the suggestions"), "error"); + } finally { + busy.value = null; + } +} + +onMounted(load); + + + + + diff --git a/frontend/src/components/LessonSituationsPanel.vue b/frontend/src/components/LessonSituationsPanel.vue new file mode 100644 index 00000000..7541a373 --- /dev/null +++ b/frontend/src/components/LessonSituationsPanel.vue @@ -0,0 +1,230 @@ + + + + + diff --git a/frontend/src/components/MomentsSettings.vue b/frontend/src/components/MomentsSettings.vue index c4e7530a..9bda05a5 100644 --- a/frontend/src/components/MomentsSettings.vue +++ b/frontend/src/components/MomentsSettings.vue @@ -4,6 +4,7 @@ import { useMomentsStore } from "@/stores/moments"; import MomentProposals from "@/components/MomentProposals.vue"; import MomentRuleUsage from "@/components/MomentRuleUsage.vue"; import WeightProposals from "@/components/WeightProposals.vue"; +import LessonSituationProposals from "@/components/LessonSituationProposals.vue"; import RecordKinds from "@/components/RecordKinds.vue"; import RecordReach from "@/components/RecordReach.vue"; import RuleAdherence from "@/components/RuleAdherence.vue"; @@ -171,6 +172,7 @@ onMounted(async () => { + diff --git a/frontend/src/components/RecordKinds.vue b/frontend/src/components/RecordKinds.vue index 681fa5fe..0f61a4c1 100644 --- a/frontend/src/components/RecordKinds.vue +++ b/frontend/src/components/RecordKinds.vue @@ -80,6 +80,9 @@ onMounted(load); {{ row.fires }} shipped default +

+ Named by id in the reply that sets a task in progress, so there are no counts for it. +

  • - {{ evidenceText(row, k.kind) }} + {{ evidenceText(row, k.kind) }}