@mmon-co/ncept-local 0.3.11 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/host.d.ts CHANGED
@@ -39,11 +39,17 @@ export interface HostSession {
39
39
  readonly mode: string;
40
40
  readonly entry?: ModeEntry;
41
41
  }
42
+ export type DueCheckIn = Readonly<{
43
+ milestone?: NodeId;
44
+ required: boolean;
45
+ final?: true;
46
+ }>;
42
47
  export interface MissionReading {
43
48
  readonly mission: Mission;
44
49
  readonly run: MissionRunReading;
45
50
  readonly next: readonly LessonReading[];
46
51
  readonly path?: MissionPathReading;
52
+ readonly checkIn?: DueCheckIn;
47
53
  }
48
54
  export interface HostReading {
49
55
  readonly missions: readonly MissionReading[];
@@ -78,7 +84,7 @@ export type SessionOpening = Readonly<{
78
84
  } | {
79
85
  mission: string;
80
86
  kind: "skip-to";
81
- stop: Pick<LessonReading, "kind" | "number" | "checkpoint">;
87
+ stop: Pick<LessonReading, "kind" | "number">;
82
88
  };
83
89
  }>;
84
90
  /** Learner entry controls; resolved against current Profile state by its writing tutor. */
@@ -88,7 +94,7 @@ export type SessionIntent = Readonly<{
88
94
  }> | Readonly<{
89
95
  kind: "skip-to";
90
96
  missionId: string;
91
- stop: Pick<LessonReading, "kind" | "number" | "checkpoint">;
97
+ stop: Pick<LessonReading, "kind" | "number">;
92
98
  }> | Readonly<{
93
99
  kind: "continue" | "revise-lesson" | "revise-route";
94
100
  missionId: string;
@@ -33,6 +33,7 @@ export declare function openLearningReader<S>(profileDir: string, theories: read
33
33
  surface: import("@mmon-co/ncept-core").SurfaceId;
34
34
  about: readonly NodeId[];
35
35
  closed: boolean;
36
+ closedAt?: string;
36
37
  closing?: string;
37
38
  mission?: string;
38
39
  parent?: string;
@@ -79,6 +80,7 @@ export declare class LearningHost<S> {
79
80
  retireMission(id: string, outcome: "achieved" | "abandoned"): Promise<void>;
80
81
  begin(intent: SessionIntent): Promise<HostSession>;
81
82
  open(input: SessionOpening): Promise<HostSession>;
83
+ private requireRefinedSection;
82
84
  switchMode(session: LocalSession, to: ModeSwitch): Promise<void>;
83
85
  context(session: LocalSession, view?: LearningView<S>): Promise<{
84
86
  missions: {
package/dist/learning.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { isMission, isMissionRevision, isRunActiveChange, isRunPastEntry, isRunRetirement, lessonHere, nodeCascadedProperty, readMissionPath, readMissionRun, retire, selectGraph, setActive, tagIndex, } from "@mmon-co/ncept-core";
2
+ import { CHECKED_IN_PROPERTY, checkInRequired, pathProperty, sectionCoarseNodes, } from "@mmon-co/ncept-default-theory/path";
3
+ import { DEFAULT_THEORY_ID, homeDefaultRecords } from "@mmon-co/ncept-default-theory/state";
2
4
  import { join } from "node:path";
3
5
  import { lock } from "proper-lockfile";
4
6
  import { openProfile } from "./directory.js";
@@ -10,9 +12,59 @@ import { mechanics } from "./mechanics.js";
10
12
  import { ENTRY_KINDS, isSurface, modeEntry } from "./inputs.js";
11
13
  import { object, parsePiSession } from "./sessions.js";
12
14
  import { sessionLength } from "./session-policy.js";
13
- function readMissions(view, complete = false) {
15
+ function dueCheckIn(view, run, path, reading, closed, touches) {
16
+ if (run.retired || view.theory.id !== DEFAULT_THEORY_ID)
17
+ return;
18
+ const here = lessonHere(reading);
19
+ const section = here ? reading.sections.indexOf(here.section) : reading.sections.length;
20
+ const milestone = path.sections[section - 1]?.milestone;
21
+ const final = !here && milestone !== undefined ? true : undefined;
22
+ const required = checkInRequired(view.graph, path, milestone);
23
+ const stamp = milestone && pathProperty(view.graph, milestone, CHECKED_IN_PROPERTY);
24
+ let source;
25
+ if (milestone) {
26
+ for (const past of run.past) {
27
+ const session = past.kind === "session" ? closed.get(past.session) : undefined;
28
+ if (!session?.closedAt || (source && session.closedAt <= source.closedAt))
29
+ continue;
30
+ if (!final && typeof stamp === "string" && session.closedAt <= stamp)
31
+ continue;
32
+ if (touches(session.surface, milestone))
33
+ source = { sessionId: session.id, closedAt: session.closedAt };
34
+ }
35
+ }
36
+ if (!source && !required)
37
+ return;
38
+ const context = { ...(milestone === undefined ? {} : { milestone }), ...(final ? { final } : {}) };
39
+ return {
40
+ checkIn: { ...context, required },
41
+ entry: { kind: "checkpoint", missionId: run.mission, ...context, ...source, outcome: { kind: "cleared" } },
42
+ };
43
+ }
44
+ function readMissionStates(view, complete = false) {
14
45
  const projection = view.theory.project(view.graph, view.state);
15
- const sessions = runs(sessionStates(view.profile));
46
+ const states = sessionStates(view.profile);
47
+ const sessions = runs(states);
48
+ const closed = new Map(states.filter(({ closed }) => closed).map((session) => [session.id, session]));
49
+ let recordNodes;
50
+ const touches = (surface, node) => {
51
+ if (!recordNodes) {
52
+ recordNodes = new Map();
53
+ const { records, current } = homeDefaultRecords(view.graph, view.observations, view.now);
54
+ for (const record of records) {
55
+ if (!record.surface)
56
+ continue;
57
+ const homes = current.get(record.id);
58
+ if (!homes?.size)
59
+ continue;
60
+ const nodes = recordNodes.get(record.surface) ?? new Set();
61
+ for (const home of homes)
62
+ nodes.add(home);
63
+ recordNodes.set(record.surface, nodes);
64
+ }
65
+ }
66
+ return recordNodes.get(surface)?.has(node) ?? false;
67
+ };
16
68
  return missions(view.profile).map((mission) => {
17
69
  const run = missionRun(view.profile, mission.id);
18
70
  const context = { graph: view.graph, projection, observations: view.observations, activities: [] };
@@ -24,29 +76,27 @@ function readMissions(view, complete = false) {
24
76
  minutes: sessionLength(localSettings(view.profile)),
25
77
  ...(run.retired ? { retired: run.retired } : {}),
26
78
  });
27
- const itinerary = path ? readMissionPath(path, view.theory, context) : undefined;
79
+ const itinerary = path && readMissionPath(path, view.theory, context);
80
+ const due = path && itinerary && dueCheckIn(view, run, path, itinerary, closed, touches);
28
81
  const structured = itinerary?.sections.flatMap(({ lessons }) => lessons) ?? [];
29
- return {
82
+ const reading = {
30
83
  mission,
31
84
  run: readMissionRun(run, sessions, complete ? run.past.length : 20),
32
85
  path: itinerary,
33
- next: (path?.future ?? [])
34
- .filter((stop) => {
35
- if (!stop.checkpoint || stop.checkpoint.required)
36
- return true;
37
- const progress = view.theory.progress(view.graph, projection, stop.nodes);
38
- return stop.nodes.some((node) => !progress[node] || progress[node].status === "open");
39
- })
40
- .map((lesson, index) => ({
86
+ ...(due ? { checkIn: due.checkIn } : {}),
87
+ next: (path?.future ?? []).map((lesson, index) => ({
41
88
  ...(structured.find((item) => item.kind === lesson.kind &&
42
89
  item.tag === lesson.tag &&
43
90
  JSON.stringify(item.nodes) === JSON.stringify(lesson.nodes)) ?? { ...lesson, number: index + 1, cleared: false }),
44
- name: lesson.name ??
45
- lesson.nodes.map((node) => stringProperty(view.graph, node, "display:label", node)).join(", "),
91
+ name: lesson.name ?? lesson.nodes.map((node) => stringProperty(view.graph, node, "display:label", node)).join(", "),
46
92
  })),
47
93
  };
94
+ return { reading, entry: due?.entry };
48
95
  });
49
96
  }
97
+ function readMissions(view, complete = false) {
98
+ return readMissionStates(view, complete).map(({ reading }) => reading);
99
+ }
50
100
  /** Read current learning state without day processing, receipts or model calls. */
51
101
  export async function openLearningReader(profileDir, theories) {
52
102
  const profile = await openProfile(profileDir, theories, { readOnly: true });
@@ -122,14 +172,23 @@ function lessonAtEntry(session, graph, readings) {
122
172
  function sessionTitle(session, profile, label) {
123
173
  const entry = session.entry;
124
174
  const mode = session.mode.charAt(0).toUpperCase() + session.mode.slice(1);
125
- const name = entry?.kind === "mission"
126
- ? missions(profile).find((mission) => mission.id === entry.id)?.statement
127
- : entry?.kind === "target"
128
- ? label(entry.node)
129
- : entry?.kind === "goal"
130
- ? entry.text
131
- : undefined;
132
- return (name ? mode + " · " + name : mode).slice(0, 200);
175
+ let title = mode;
176
+ if (entry?.kind === "checkpoint") {
177
+ const name = missions(profile).find((mission) => mission.id === entry.missionId)?.statement;
178
+ title = "Check-in" + (name ? " · " + name : "");
179
+ }
180
+ else {
181
+ const name = entry?.kind === "mission"
182
+ ? missions(profile).find((mission) => mission.id === entry.id)?.statement
183
+ : entry?.kind === "target"
184
+ ? label(entry.node)
185
+ : entry?.kind === "goal"
186
+ ? entry.text
187
+ : undefined;
188
+ if (name)
189
+ title += " · " + name;
190
+ }
191
+ return title.slice(0, 200);
133
192
  }
134
193
  function identity() {
135
194
  return crypto.randomUUID();
@@ -385,15 +444,15 @@ export class LearningHost {
385
444
  async begin(intent) {
386
445
  if (intent.kind === "new-mission")
387
446
  return this.open({ mode: "charting" });
388
- const reading = (await this.missionReadings()).find(({ mission }) => mission.id === intent.missionId);
389
- if (!reading || reading.run.retired?.outcome === "abandoned")
447
+ const state = readMissionStates(await this.runtime.snapshot()).find(({ reading }) => reading.mission.id === intent.missionId);
448
+ if (!state || state.reading.run.retired?.outcome === "abandoned")
390
449
  throw new Error("Mission is not available");
391
- const { mission, run, next, path } = reading;
450
+ const { mission, run, next, path } = state.reading;
392
451
  const entry = { kind: "mission", id: mission.id };
393
452
  if (intent.kind === "continue" && run.active)
394
453
  return this.open({ mode: "empty", resume: run.active });
395
- if (run.retired && intent.kind !== "continue")
396
- throw new Error("A finished Mission can only continue or resume a Session");
454
+ if (run.retired)
455
+ throw new Error("This Mission is finished");
397
456
  if (intent.kind === "skip-lesson" || intent.kind === "skip-to")
398
457
  return this.open({ mode: "lesson", skip: { ...intent, mission: mission.id } });
399
458
  if (intent.kind === "tell-tutor" && !intent.request.trim())
@@ -407,18 +466,17 @@ export class LearningHost {
407
466
  }
408
467
  if (intent.kind === "tell-tutor" || intent.kind === "revise-route" || !mission.targets.length)
409
468
  return this.open({ mode: "charting", entry: { kind: "mission", id: mission.id } });
410
- if (run.retired)
411
- throw new Error("This Mission is finished");
469
+ if (state.entry)
470
+ return this.open({ mode: "charting", entry: state.entry });
412
471
  const stop = next[0];
413
472
  if (!stop)
414
473
  throw new Error("This Mission has no following stop; revise the route or finish it");
415
- if (stop.checkpoint)
416
- return this.open({ mode: "charting", entry: { kind: "mission", id: mission.id } });
417
474
  return this.open({ mode: "lesson", entry });
418
475
  }
419
476
  async open(input) {
420
477
  const skipMission = input.skip?.mission;
421
478
  let skipped = [];
479
+ let skipSection;
422
480
  if (input.skip) {
423
481
  if (input.resume || input.entry)
424
482
  throw new Error("A skip cannot resume or supply an entry");
@@ -436,19 +494,15 @@ export class LearningHost {
436
494
  const skip = input.skip;
437
495
  const index = skip.kind === "skip-lesson"
438
496
  ? stops.findIndex((stop, position) => position > now && !stop.cleared)
439
- : stops.findIndex((stop) => stop.kind === skip.stop.kind &&
440
- stop.number === skip.stop.number &&
441
- stop.checkpoint?.milestone === skip.stop.checkpoint?.milestone);
497
+ : stops.findIndex((stop) => stop.kind === skip.stop.kind && stop.number === skip.stop.number);
442
498
  if (index < 0 || !stops[index])
443
499
  throw new Error("This Mission has no following stop; revise the route or finish it");
444
500
  if (now < 0 || index <= now || stops[index].cleared)
445
501
  throw new Error("Choose an uncleared stop after the current Lesson");
446
- skipped = stops.slice(now, index).filter((stop) => stop.numbered && !stop.cleared && !stop.checkpoint);
502
+ skipped = stops.slice(now, index).filter((stop) => stop.numbered && !stop.cleared);
447
503
  const stop = stops[index];
448
- input = {
449
- mode: stop.checkpoint ? "charting" : "lesson",
450
- entry: { kind: "mission", id: reading.mission.id },
451
- };
504
+ skipSection = path.sections.find(({ lessons }) => lessons.includes(stop));
505
+ input = { mode: "lesson", entry: { kind: "mission", id: reading.mission.id } };
452
506
  }
453
507
  const view = await this.runtime.snapshot();
454
508
  const known = sessionStates(view.profile);
@@ -494,6 +548,9 @@ export class LearningHost {
494
548
  const mission = missionId ? missions(view.profile).find(({ id }) => id === missionId) : undefined;
495
549
  if (missionId && !mission)
496
550
  throw new Error("Unknown Mission");
551
+ // Resuming an open Lesson stays possible, so Continue never strands an active run.
552
+ if (mode === "lesson" && mission && !resumed && (entry?.kind === "mission" || skipSection))
553
+ this.requireRefinedSection(view, mission.id, skipSection);
497
554
  const about = (mode === "lesson" && entry?.kind === "mission" ? undefined : resumed?.about) ??
498
555
  (entry?.kind === "target"
499
556
  ? [entry.node]
@@ -574,12 +631,22 @@ export class LearningHost {
574
631
  this.pendingSwitch = undefined;
575
632
  return session;
576
633
  }
634
+ requireRefinedSection(view, missionId, destination) {
635
+ if (view.theory.id !== DEFAULT_THEORY_ID)
636
+ return;
637
+ const path = readMissions(view).find(({ mission }) => mission.id === missionId)?.path;
638
+ const here = path && lessonHere(path)?.section;
639
+ if ([here, destination].some((section) => section && sectionCoarseNodes(view.graph, section).length > 0))
640
+ throw new Error("Complete the check-in before starting the next Lesson");
641
+ }
577
642
  async switchMode(session, to) {
578
643
  const view = await this.runtime.snapshot();
579
644
  const selected = this.mode(view.theory, session.mode, session.entry);
580
645
  if (!selected?.switches.some((allowed) => allowed.mode === to.mode && allowed.then === to.then))
581
646
  throw new Error("This mode does not allow that switch");
582
647
  this.mode(view.theory, to.mode, to.entry);
648
+ if (to.mode === "lesson" && to.entry?.kind === "mission")
649
+ this.requireRefinedSection(view, to.entry.id);
583
650
  if (to.then === "here") {
584
651
  const missionId = to.entry?.kind === "mission"
585
652
  ? to.entry.id
package/dist/runtime.d.ts CHANGED
@@ -6,6 +6,7 @@ export interface LocalSession extends HostSession {
6
6
  readonly surface: SurfaceId;
7
7
  readonly about: readonly NodeId[];
8
8
  readonly closed: boolean;
9
+ readonly closedAt?: string;
9
10
  /** Identifies a pending close; resuming the Session clears it. */
10
11
  readonly closing?: string;
11
12
  readonly mission?: string;
package/dist/runtime.js CHANGED
@@ -26,7 +26,7 @@ export function sessionStates(profile) {
26
26
  const states = new Map();
27
27
  for (const entry of profile.log) {
28
28
  const payload = localPayload(entry, "local.session");
29
- if (payload && object(payload.session)) {
29
+ if (payload && isObservation(entry) && object(payload.session)) {
30
30
  const session = payload.session;
31
31
  if (!nonempty(session.id) ||
32
32
  !isSurface(session.surface) ||
@@ -38,6 +38,7 @@ export function sessionStates(profile) {
38
38
  surface: session.surface,
39
39
  mode: session.mode,
40
40
  closed: session.closed,
41
+ ...(session.closed ? { closedAt: entry.at } : {}),
41
42
  ...(typeof session.closing === "string" ? { closing: session.closing } : {}),
42
43
  about: nodeIds(session.about),
43
44
  ...(session.entry === undefined ? {} : { entry: modeEntry(session.entry) }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmon-co/ncept-local",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,16 +32,16 @@
32
32
  }
33
33
  },
34
34
  "scripts": {
35
- "build": "bun run --cwd ../core build && tsc -p tsconfig.build.json",
35
+ "build": "bun run --cwd ../default-theory build && tsc -p tsconfig.build.json",
36
36
  "prepack": "bun run build"
37
37
  },
38
38
  "dependencies": {
39
39
  "@mmon-co/ncept-core": "^0.4.0",
40
+ "@mmon-co/ncept-default-theory": "^3.0.0",
40
41
  "@modelcontextprotocol/sdk": "1.32.1",
41
42
  "proper-lockfile": "4.1.2"
42
43
  },
43
44
  "devDependencies": {
44
- "@mmon-co/ncept-default-theory": "^3.0.0",
45
45
  "@types/proper-lockfile": "^4.1.4",
46
46
  "bun-types": "^1.3.14",
47
47
  "typescript": "6.0.3"