@mmon-co/ncept-default-theory 0.0.0-stage → 1.1.0
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/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/catalog-contract.d.ts +102 -0
- package/dist/catalog-contract.js +75 -0
- package/dist/catalog.d.ts +14 -0
- package/dist/catalog.js +164 -0
- package/dist/chat.d.ts +171 -0
- package/dist/chat.js +232 -0
- package/dist/completion.d.ts +74 -0
- package/dist/completion.js +277 -0
- package/dist/dependent-cone.d.ts +3 -0
- package/dist/dependent-cone.js +26 -0
- package/dist/diagnosis.d.ts +9 -0
- package/dist/diagnosis.js +1 -0
- package/dist/diagnostics.d.ts +25 -0
- package/dist/diagnostics.js +34 -0
- package/dist/evaluation.d.ts +33 -0
- package/dist/evaluation.js +376 -0
- package/dist/event.d.ts +7 -0
- package/dist/event.js +405 -0
- package/dist/history.d.ts +4 -0
- package/dist/history.js +61 -0
- package/dist/implementation.d.ts +184 -0
- package/dist/implementation.js +31 -0
- package/dist/index.d.ts +200 -0
- package/dist/index.js +27 -0
- package/dist/messages.d.ts +167 -0
- package/dist/messages.js +56 -0
- package/dist/opening.d.ts +20 -0
- package/dist/opening.js +33 -0
- package/dist/outcomes.d.ts +143 -0
- package/dist/outcomes.js +323 -0
- package/dist/path.d.ts +19 -0
- package/dist/path.js +293 -0
- package/dist/reading.d.ts +12 -0
- package/dist/reading.js +75 -0
- package/dist/release.d.ts +1 -0
- package/dist/release.js +1 -0
- package/dist/roleplay.d.ts +17 -0
- package/dist/roleplay.js +2 -0
- package/dist/skills/sources.d.ts +12 -0
- package/dist/skills/sources.js +13 -0
- package/dist/skills.d.ts +12 -0
- package/dist/skills.js +70 -0
- package/dist/state.d.ts +316 -0
- package/dist/state.js +1309 -0
- package/dist/tags.d.ts +34 -0
- package/dist/tags.js +306 -0
- package/dist/timing.d.ts +21 -0
- package/dist/timing.js +79 -0
- package/dist/tools.d.ts +394 -0
- package/dist/tools.js +872 -0
- package/package.json +52 -4
package/dist/chat.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { renderSpeech } from "@mmon-co/ncept-core/theory/speaker";
|
|
2
|
+
/* eslint-disable require-yield -- Chat setup is a zero-effect synchronous Program. */
|
|
3
|
+
import { lastMetadataContribution, metadataSetsForEntity, NODE_CRITERION_PROPERTY, NODE_LABEL_PROPERTY, PEDAGOGY_OPENING_PROPERTY, PEDAGOGY_FIRST_MESSAGE_PROPERTY, nodeCascadedProperty, } from "@mmon-co/ncept-core/patch";
|
|
4
|
+
import { CATALOG_MODE_TOOLS, CATALOG_MODE_TITLES, CATALOG_MODE_METADATA_KEYS } from "./catalog-contract.js";
|
|
5
|
+
import { isDefaultObservation, isMemory, memoryDefault } from "./state.js";
|
|
6
|
+
import { skillBody, skillResources, skillsForMode, tutorSystemPrompt } from "./skills.js";
|
|
7
|
+
import { catalogToolsForMode } from "./catalog.js";
|
|
8
|
+
import { evaluationContext, evaluateWatchedReply } from "./evaluation.js";
|
|
9
|
+
import { lessonOpening, openingLanguage } from "./opening.js";
|
|
10
|
+
const COMMON_CONCEPT_PATCH_PROPERTY = "app:common-concept";
|
|
11
|
+
const RAISE_WHEN_PROPERTY = "pedagogy:raise-when";
|
|
12
|
+
const COMMON_CONCEPT_DOCUMENT_URL = "/ncept/playgrounds/document?material=";
|
|
13
|
+
function metadataValue(ctx, node, property) {
|
|
14
|
+
const candidates = metadataSetsForEntity(ctx.graph, { kind: "node", id: node }).flatMap((set) => {
|
|
15
|
+
if (!(property in set.properties))
|
|
16
|
+
return [];
|
|
17
|
+
const source = ctx.graph.provenance.metadataProperties[set.id]?.[property]?.at(-1);
|
|
18
|
+
if (source === undefined)
|
|
19
|
+
throw new Error(`Missing provenance for ${property} on ${node}`);
|
|
20
|
+
return [{ value: set.properties[property], source }];
|
|
21
|
+
});
|
|
22
|
+
return lastMetadataContribution(ctx.graph, candidates)?.value;
|
|
23
|
+
}
|
|
24
|
+
function officialDocuments(ctx) {
|
|
25
|
+
const officialPatches = new Set(ctx.graph.patchOrder.flatMap(({ identity }) => ctx.graph.patchMetadata[identity]?.properties[COMMON_CONCEPT_PATCH_PROPERTY] === true ? [identity] : []));
|
|
26
|
+
return ctx.graph.nodes.flatMap(({ id }) => {
|
|
27
|
+
if (!id.startsWith("node-mmon-") ||
|
|
28
|
+
!ctx.graph.provenance.nodes[id]?.some(({ patch }) => officialPatches.has(patch.identity)))
|
|
29
|
+
return [];
|
|
30
|
+
const label = metadataValue(ctx, id, NODE_LABEL_PROPERTY);
|
|
31
|
+
const summary = metadataValue(ctx, id, NODE_CRITERION_PROPERTY);
|
|
32
|
+
const document = ctx.graph.materials.find((material) => material.playground === "document" && material.type === "markdown" && material.about.includes(id));
|
|
33
|
+
if (typeof label !== "string" ||
|
|
34
|
+
label.trim() === "" ||
|
|
35
|
+
typeof summary !== "string" ||
|
|
36
|
+
summary.trim() === "" ||
|
|
37
|
+
document === undefined)
|
|
38
|
+
return [];
|
|
39
|
+
return [
|
|
40
|
+
{
|
|
41
|
+
node: id,
|
|
42
|
+
label,
|
|
43
|
+
summary,
|
|
44
|
+
documentId: document.id,
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Exclude Document links already carried by tutor text, in this conversation or a closed Session. */
|
|
50
|
+
function recommendableAffordances(ctx, documents) {
|
|
51
|
+
const told = [
|
|
52
|
+
...(ctx.tutorMessages ?? []),
|
|
53
|
+
...ctx.state.observations.flatMap((observation) => isDefaultObservation(observation) && isMemory(observation.payload)
|
|
54
|
+
? observation.payload.entries.filter(({ speaker }) => speaker.kind === "tutor").map(({ text }) => text)
|
|
55
|
+
: []),
|
|
56
|
+
];
|
|
57
|
+
return documents.flatMap((document) => {
|
|
58
|
+
const state = ctx.state.nodes[document.node];
|
|
59
|
+
const raiseWhen = metadataValue(ctx, document.node, RAISE_WHEN_PROPERTY);
|
|
60
|
+
const documentUrl = `${COMMON_CONCEPT_DOCUMENT_URL}${encodeURIComponent(document.documentId)}`;
|
|
61
|
+
if (state === undefined ||
|
|
62
|
+
state.featureUses.length !== 0 ||
|
|
63
|
+
typeof raiseWhen !== "string" ||
|
|
64
|
+
raiseWhen.trim() === "" ||
|
|
65
|
+
told.some((text) => text.includes(documentUrl)))
|
|
66
|
+
return [];
|
|
67
|
+
return [{ ...document, raiseWhen, documentUrl }];
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
export function documentListing(ctx) {
|
|
71
|
+
const documents = officialDocuments(ctx);
|
|
72
|
+
return { documents, candidates: recommendableAffordances(ctx, documents) };
|
|
73
|
+
}
|
|
74
|
+
function chatSkills(mode) {
|
|
75
|
+
return skillsForMode(mode).map((meta) => ({
|
|
76
|
+
...meta,
|
|
77
|
+
resources: skillResources(meta.name),
|
|
78
|
+
body: skillBody(meta.name),
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
/** Preserve the existing scoped-memory prompt bound; the memory tool reads the rest. */
|
|
82
|
+
const MEMORY_PREFETCH_LIMIT = 16 * 1024;
|
|
83
|
+
function scopedMemory(ctx) {
|
|
84
|
+
const lines = ctx.about.flatMap((node) => {
|
|
85
|
+
const memory = ctx.state.memories[node];
|
|
86
|
+
if (memory === undefined || memory.entries.length === 0)
|
|
87
|
+
return [];
|
|
88
|
+
const retrieval = memoryDefault(memory);
|
|
89
|
+
return [
|
|
90
|
+
`## ${node}${retrieval.marker ? ` (${retrieval.marker})` : ""}`,
|
|
91
|
+
...retrieval.entries.map((entry) => entry.kind === "summary"
|
|
92
|
+
? `- [summary of ${entry.sources.join(", ")}; Material sources ${JSON.stringify(entry.materialSources)}] quoted derivative: ${JSON.stringify(entry.text)}`
|
|
93
|
+
: `- [${entry.source.kind === "observation" ? entry.source.observation : `surface ${entry.source.surface} ${entry.source.from}–${entry.source.to}`}] ${renderSpeech(entry.speaker, entry.text)}`),
|
|
94
|
+
];
|
|
95
|
+
});
|
|
96
|
+
const text = lines.join("\n");
|
|
97
|
+
return text.length > MEMORY_PREFETCH_LIMIT
|
|
98
|
+
? `${text.slice(0, MEMORY_PREFETCH_LIMIT)}\n… (truncated; use theory {name:'memory',input:{node}} for the rest)`
|
|
99
|
+
: text;
|
|
100
|
+
}
|
|
101
|
+
function tutorOpening(ctx) {
|
|
102
|
+
if (!ctx.opening)
|
|
103
|
+
return undefined;
|
|
104
|
+
const entry = ctx.entry;
|
|
105
|
+
const nodes = entry?.kind === "lesson" ? entry.lesson.nodes : [];
|
|
106
|
+
const lesson = entry?.kind === "lesson"
|
|
107
|
+
? lessonOpening(ctx.graph, nodes, ctx.missions?.find(({ id }) => id === entry.lesson.mission.id))
|
|
108
|
+
: undefined;
|
|
109
|
+
return [
|
|
110
|
+
"Initial tutor-led turn (host context, not learner speech). " +
|
|
111
|
+
(lesson?.instruction ?? "Begin this Session in its selected mode."),
|
|
112
|
+
openingLanguage(ctx.opening.latestLearnerMessage),
|
|
113
|
+
lesson === undefined ? undefined : "Lesson opening facts: " + JSON.stringify(lesson.facts),
|
|
114
|
+
nodes.length === 0
|
|
115
|
+
? undefined
|
|
116
|
+
: "Authored Node openings (context for when each Node is reached; firstMessage is addressed to the learner): " +
|
|
117
|
+
JSON.stringify(nodes.map((node) => ({
|
|
118
|
+
node,
|
|
119
|
+
firstAction: nodeCascadedProperty(ctx.graph, node, PEDAGOGY_OPENING_PROPERTY),
|
|
120
|
+
firstMessage: nodeCascadedProperty(ctx.graph, node, PEDAGOGY_FIRST_MESSAGE_PROPERTY),
|
|
121
|
+
}))),
|
|
122
|
+
]
|
|
123
|
+
.filter(Boolean)
|
|
124
|
+
.join("\n");
|
|
125
|
+
}
|
|
126
|
+
function* setup(ctx, mode) {
|
|
127
|
+
for (const node of ctx.about)
|
|
128
|
+
if (!ctx.graph.nodes.some(({ id }) => id === node))
|
|
129
|
+
throw new Error(`Missing Node ${node}`);
|
|
130
|
+
const prompt = tutorSystemPrompt(mode);
|
|
131
|
+
return {
|
|
132
|
+
prompt,
|
|
133
|
+
context: [
|
|
134
|
+
...evaluationContext(ctx),
|
|
135
|
+
tutorOpening(ctx),
|
|
136
|
+
ctx.about.length
|
|
137
|
+
? `This Session is attached to ${ctx.about.join(", ")}. Current Node memory (other Sessions' spans and Records; use theory {name:'memory',input:{node}} for more):\n\n${scopedMemory(ctx) || "(none)"}`
|
|
138
|
+
: "This Session is attached to no Nodes; name Nodes explicitly where a tool needs them.",
|
|
139
|
+
...(ctx.entry === undefined ? [] : [`Session opening (host-resolved facts):\n${JSON.stringify(ctx.entry)}`]),
|
|
140
|
+
...(ctx.missions?.length ? [`Current Missions (context data):\n${JSON.stringify(ctx.missions)}`] : []),
|
|
141
|
+
...(ctx.entry?.kind === "review"
|
|
142
|
+
? [`Current Review (frozen batch; check retention, not teach):\n${JSON.stringify(ctx.entry)}`]
|
|
143
|
+
: []),
|
|
144
|
+
...(ctx.lesson === undefined
|
|
145
|
+
? []
|
|
146
|
+
: [
|
|
147
|
+
`Current Lesson progress (fixed checklist; current theory reading):\n${JSON.stringify({
|
|
148
|
+
...ctx.lesson,
|
|
149
|
+
completion: ctx.lesson.lesson.nodes.map((node) => ({
|
|
150
|
+
node,
|
|
151
|
+
query: ctx.state.nodes[node]?.completion.query ?? "no completion set",
|
|
152
|
+
counts: ctx.state.nodes[node]?.completion.counts ?? [],
|
|
153
|
+
})),
|
|
154
|
+
})}`,
|
|
155
|
+
]),
|
|
156
|
+
]
|
|
157
|
+
.filter(Boolean)
|
|
158
|
+
.join("\n\n"),
|
|
159
|
+
skills: chatSkills(mode),
|
|
160
|
+
tools: catalogToolsForMode(mode ?? "lesson"),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The host supplies a current value context at both entry points, including cross-surface
|
|
165
|
+
* evidence. The learner's saved preferences are not part of it: the Application assembles them
|
|
166
|
+
* into the interactive conversation it is running, so no theory Program can read or cite them.
|
|
167
|
+
*/
|
|
168
|
+
export const chat = { open: setup, turn: setup, reply: evaluateWatchedReply };
|
|
169
|
+
/** Host context follows the same roots that the default catalog admits. */
|
|
170
|
+
function catalogCapabilities(mode) {
|
|
171
|
+
const roots = CATALOG_MODE_TOOLS[mode];
|
|
172
|
+
return [
|
|
173
|
+
...(roots.includes("read") ? ["reads"] : []),
|
|
174
|
+
...(roots.includes("material") ? ["materials", "applets"] : []),
|
|
175
|
+
...(roots.includes("edit_graph_draft") ? ["graph-draft"] : []),
|
|
176
|
+
...(roots.includes("web_search") || roots.includes("fetch_page") ? ["web"] : []),
|
|
177
|
+
...(roots.includes("bash") ? ["bash"] : []),
|
|
178
|
+
];
|
|
179
|
+
}
|
|
180
|
+
/** The default theory’s Session choices; host capabilities are mounted separately. */
|
|
181
|
+
export const modes = {
|
|
182
|
+
empty: {
|
|
183
|
+
title: CATALOG_MODE_TITLES.empty,
|
|
184
|
+
frontTab: "map",
|
|
185
|
+
entry: { required: false, kinds: [] },
|
|
186
|
+
capabilities: catalogCapabilities("empty"),
|
|
187
|
+
switches: [
|
|
188
|
+
{ mode: "charting", then: "here" },
|
|
189
|
+
{ mode: "lesson", then: "here" },
|
|
190
|
+
],
|
|
191
|
+
setup: (ctx) => setup(ctx, "empty"),
|
|
192
|
+
},
|
|
193
|
+
charting: {
|
|
194
|
+
title: CATALOG_MODE_TITLES.charting,
|
|
195
|
+
frontTab: "map",
|
|
196
|
+
noteTemplates: [
|
|
197
|
+
{ id: "default.charting.note.known", defaultMessage: "I already know this." },
|
|
198
|
+
{ id: "default.charting.note.unwanted", defaultMessage: "I don't need this." },
|
|
199
|
+
{ id: "default.charting.note.why", defaultMessage: "Why is this needed?" },
|
|
200
|
+
{ id: "default.charting.note.merge", defaultMessage: "These are one thing for me." },
|
|
201
|
+
],
|
|
202
|
+
entry: { required: false, kinds: ["goal", "mission", "target", "checkpoint"] },
|
|
203
|
+
capabilities: catalogCapabilities("charting"),
|
|
204
|
+
switches: [{ mode: "lesson", then: "next", carry: { kind: "brief" } }],
|
|
205
|
+
setup: (ctx) => setup(ctx, "charting"),
|
|
206
|
+
},
|
|
207
|
+
authoring: {
|
|
208
|
+
title: CATALOG_MODE_TITLES.authoring,
|
|
209
|
+
frontTab: "map",
|
|
210
|
+
entry: { required: false, kinds: ["goal", "mission"] },
|
|
211
|
+
capabilities: catalogCapabilities("authoring"),
|
|
212
|
+
switches: [],
|
|
213
|
+
setup: (ctx) => setup(ctx, "authoring"),
|
|
214
|
+
},
|
|
215
|
+
lesson: {
|
|
216
|
+
title: CATALOG_MODE_TITLES.lesson,
|
|
217
|
+
frontTab: "document",
|
|
218
|
+
entry: { required: true, kinds: ["lesson", "review"] },
|
|
219
|
+
capabilities: catalogCapabilities("lesson"),
|
|
220
|
+
metadataKeys: CATALOG_MODE_METADATA_KEYS.lesson,
|
|
221
|
+
switches: [
|
|
222
|
+
{ mode: "lesson", then: "next", carry: { kind: "brief" } },
|
|
223
|
+
{ mode: "charting", then: "next", carry: { kind: "brief" } },
|
|
224
|
+
{
|
|
225
|
+
mode: "charting",
|
|
226
|
+
then: "aside",
|
|
227
|
+
carry: { kind: "compacted", focus: "Re-chart the current Lesson and its blockers" },
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
setup: (ctx) => setup(ctx, "lesson"),
|
|
231
|
+
},
|
|
232
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Observation, type ObservationId } from "@mmon-co/ncept-core/observations";
|
|
2
|
+
import { type NodeId } from "@mmon-co/ncept-core/patch";
|
|
3
|
+
import type { Claim, CompletionOutcome, Demonstration } from "./state.js";
|
|
4
|
+
export type Json = null | boolean | number | string | readonly Json[] | {
|
|
5
|
+
readonly [key: string]: Json;
|
|
6
|
+
};
|
|
7
|
+
export type Field = "kind" | "node" | "at" | "material" | "item" | "outcome" | "unaided" | "grader" | "playground" | "scope" | "state" | "current";
|
|
8
|
+
export type Completion = {
|
|
9
|
+
readonly all: readonly Completion[];
|
|
10
|
+
} | {
|
|
11
|
+
readonly any: readonly Completion[];
|
|
12
|
+
} | {
|
|
13
|
+
readonly count: number;
|
|
14
|
+
readonly of: Predicate;
|
|
15
|
+
};
|
|
16
|
+
export type Predicate = {
|
|
17
|
+
readonly all: readonly Predicate[];
|
|
18
|
+
} | {
|
|
19
|
+
readonly any: readonly Predicate[];
|
|
20
|
+
} | {
|
|
21
|
+
readonly not: Predicate;
|
|
22
|
+
} | {
|
|
23
|
+
readonly latest: {
|
|
24
|
+
readonly per: readonly Field[];
|
|
25
|
+
readonly of: Predicate;
|
|
26
|
+
};
|
|
27
|
+
} | {
|
|
28
|
+
readonly field: Field;
|
|
29
|
+
readonly is: Json;
|
|
30
|
+
};
|
|
31
|
+
export declare const COMPLETION_PROPERTY = "pedagogy:completion";
|
|
32
|
+
/** Authored queries are checked once against the resolved revision, never while reading history. */
|
|
33
|
+
export declare function validateCompletion(value: unknown, graph: {
|
|
34
|
+
readonly nodes: readonly {
|
|
35
|
+
readonly id: NodeId;
|
|
36
|
+
}[];
|
|
37
|
+
readonly materials: readonly {
|
|
38
|
+
readonly playground: string;
|
|
39
|
+
readonly id: string;
|
|
40
|
+
}[];
|
|
41
|
+
}, path?: string): asserts value is Completion;
|
|
42
|
+
export interface CompletionObservation {
|
|
43
|
+
readonly observation: Observation<Demonstration | Claim | CompletionOutcome>;
|
|
44
|
+
readonly nodes: readonly NodeId[];
|
|
45
|
+
}
|
|
46
|
+
export interface CountProgress {
|
|
47
|
+
readonly k: number;
|
|
48
|
+
readonly required: number;
|
|
49
|
+
}
|
|
50
|
+
export interface CompletionProgress {
|
|
51
|
+
readonly complete: boolean;
|
|
52
|
+
readonly counts: readonly CountProgress[];
|
|
53
|
+
readonly citations: readonly ObservationId[];
|
|
54
|
+
readonly validUntil?: string;
|
|
55
|
+
}
|
|
56
|
+
/** Ordered candidates and all report expiries survive clock reads unchanged. */
|
|
57
|
+
export declare function prepareCompletionHistory(history: readonly CompletionObservation[]): {
|
|
58
|
+
ordered: CompletionObservation[];
|
|
59
|
+
expiries: string[];
|
|
60
|
+
};
|
|
61
|
+
type CompletionHistory = ReturnType<typeof prepareCompletionHistory>;
|
|
62
|
+
/** First strictly future expiry; works for backwards clocks and duplicate deadlines too. */
|
|
63
|
+
export declare function completionBoundary(history: CompletionHistory, now: string): number;
|
|
64
|
+
/** Compile the authored tree once. Arbitrary predicates still inspect the shared ordered corpus
|
|
65
|
+
* when history or an expiry boundary changes, never on an unchanged clock interval.
|
|
66
|
+
* Latest's input is its own 'of', not sibling filters. */
|
|
67
|
+
export declare function compileCompletion(query: Completion, node: NodeId): (history: CompletionHistory, now: string, boundary?: number) => CompletionProgress;
|
|
68
|
+
export declare function evaluateCompletion(query: Completion, node: NodeId, history: readonly CompletionObservation[], now: string): CompletionProgress;
|
|
69
|
+
/** Hosts discover report producers in the query instead of recognizing a canonical binding. */
|
|
70
|
+
export declare function completionNamesPlayground(query: Completion | undefined, playground: string): boolean;
|
|
71
|
+
/** Author a count of current complete reports for this Node and one Playground scope. */
|
|
72
|
+
export declare function playgroundCompletion(playground: string, scope: Json): Completion;
|
|
73
|
+
export declare function completionCaption(progress: CompletionProgress): string;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { canonicalJson } from "@mmon-co/ncept-core/digest";
|
|
2
|
+
import { compareObservations, isObservationTimestamp, } from "@mmon-co/ncept-core/observations";
|
|
3
|
+
import { isMaterialMeta, isNodeId } from "@mmon-co/ncept-core/patch";
|
|
4
|
+
export const COMPLETION_PROPERTY = "pedagogy:completion";
|
|
5
|
+
const fields = [
|
|
6
|
+
"kind",
|
|
7
|
+
"node",
|
|
8
|
+
"at",
|
|
9
|
+
"material",
|
|
10
|
+
"item",
|
|
11
|
+
"outcome",
|
|
12
|
+
"unaided",
|
|
13
|
+
"grader",
|
|
14
|
+
"playground",
|
|
15
|
+
"scope",
|
|
16
|
+
"state",
|
|
17
|
+
"current",
|
|
18
|
+
];
|
|
19
|
+
const object = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
20
|
+
/** Authored queries are checked once against the resolved revision, never while reading history. */
|
|
21
|
+
export function validateCompletion(value, graph, path = COMPLETION_PROPERTY) {
|
|
22
|
+
function fail(at, message) {
|
|
23
|
+
throw new Error(at + ": " + message);
|
|
24
|
+
}
|
|
25
|
+
if (!isMaterialMeta(value))
|
|
26
|
+
fail(path, "expected a bounded JSON query object");
|
|
27
|
+
function shape(value, keys, at) {
|
|
28
|
+
if (!object(value) || Object.keys(value).length !== keys.length || !keys.every((key) => key in value))
|
|
29
|
+
fail(at, "expected exactly " + keys.join(", "));
|
|
30
|
+
}
|
|
31
|
+
function visit(value, at, predicate) {
|
|
32
|
+
if (!object(value))
|
|
33
|
+
fail(at, "expected an object");
|
|
34
|
+
const entry = value;
|
|
35
|
+
for (const key of ["all", "any"]) {
|
|
36
|
+
if (!(key in entry))
|
|
37
|
+
continue;
|
|
38
|
+
shape(entry, [key], at);
|
|
39
|
+
if (!Array.isArray(entry[key]))
|
|
40
|
+
fail(at + "." + key, "expected an array");
|
|
41
|
+
entry[key].forEach((child, index) => visit(child, at + "." + key + "[" + index + "]", predicate));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!predicate) {
|
|
45
|
+
shape(entry, ["count", "of"], at);
|
|
46
|
+
if (typeof entry.count !== "number" || !Number.isSafeInteger(entry.count) || entry.count < 0)
|
|
47
|
+
fail(at + ".count", "expected a nonnegative integer");
|
|
48
|
+
visit(entry.of, at + ".of", true);
|
|
49
|
+
}
|
|
50
|
+
else if ("not" in entry) {
|
|
51
|
+
shape(entry, ["not"], at);
|
|
52
|
+
visit(entry.not, at + ".not", true);
|
|
53
|
+
}
|
|
54
|
+
else if ("latest" in entry) {
|
|
55
|
+
shape(entry, ["latest"], at);
|
|
56
|
+
shape(entry.latest, ["per", "of"], at + ".latest");
|
|
57
|
+
const { per, of } = entry.latest;
|
|
58
|
+
if (!Array.isArray(per) || !per.every((field) => fields.includes(field)) || new Set(per).size !== per.length)
|
|
59
|
+
fail(at + ".latest.per", "expected distinct observation fields");
|
|
60
|
+
visit(of, at + ".latest.of", true);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
shape(entry, ["field", "is"], at);
|
|
64
|
+
const { field, is } = entry;
|
|
65
|
+
if (typeof field !== "string" || !fields.includes(field))
|
|
66
|
+
fail(at + ".field", "unknown observation field");
|
|
67
|
+
let valid = false;
|
|
68
|
+
switch (field) {
|
|
69
|
+
case "kind":
|
|
70
|
+
valid = ["demonstration", "claim", "completion"].some((value) => value === is);
|
|
71
|
+
break;
|
|
72
|
+
case "node":
|
|
73
|
+
valid = is === "self" || (isNodeId(is) && graph.nodes.some(({ id }) => id === is));
|
|
74
|
+
break;
|
|
75
|
+
case "at":
|
|
76
|
+
valid = isObservationTimestamp(is);
|
|
77
|
+
break;
|
|
78
|
+
case "unaided":
|
|
79
|
+
case "current":
|
|
80
|
+
valid = typeof is === "boolean";
|
|
81
|
+
break;
|
|
82
|
+
case "outcome":
|
|
83
|
+
valid = ["success", "failure", "dont-know"].some((value) => value === is);
|
|
84
|
+
break;
|
|
85
|
+
case "grader":
|
|
86
|
+
valid = ["mechanical", "tutor", "learner", "decision"].some((value) => value === is);
|
|
87
|
+
break;
|
|
88
|
+
case "state":
|
|
89
|
+
valid = ["complete", "incomplete"].some((value) => value === is);
|
|
90
|
+
break;
|
|
91
|
+
case "scope":
|
|
92
|
+
valid = true;
|
|
93
|
+
break; // any bounded JSON value; owned by the Playground
|
|
94
|
+
case "material":
|
|
95
|
+
shape(is, ["playground", "id"], at + ".is");
|
|
96
|
+
valid =
|
|
97
|
+
typeof is.playground === "string" &&
|
|
98
|
+
typeof is.id === "string" &&
|
|
99
|
+
graph.materials.some((material) => material.playground === is.playground && material.id === is.id);
|
|
100
|
+
break;
|
|
101
|
+
case "item":
|
|
102
|
+
case "playground":
|
|
103
|
+
valid = typeof is === "string" && is.trim().length > 0;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
if (!valid)
|
|
107
|
+
fail(at + ".is", "invalid " + String(field) + " value or unresolved id");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
visit(value, path, false);
|
|
111
|
+
}
|
|
112
|
+
/** Ordered candidates and all report expiries survive clock reads unchanged. */
|
|
113
|
+
export function prepareCompletionHistory(history) {
|
|
114
|
+
// Removed cards keep their evidence for memory and retention, but no longer satisfy completion.
|
|
115
|
+
const ordered = history
|
|
116
|
+
.filter(({ observation }) => observation.materialPresent !== false)
|
|
117
|
+
.sort((a, b) => compareObservations(a.observation, b.observation));
|
|
118
|
+
const expiries = ordered
|
|
119
|
+
.flatMap(({ observation: { payload } }) => payload.kind === "completion" && payload.validUntil !== undefined ? [payload.validUntil] : [])
|
|
120
|
+
.sort((a, b) => Date.parse(a) - Date.parse(b));
|
|
121
|
+
return { ordered, expiries };
|
|
122
|
+
}
|
|
123
|
+
/** First strictly future expiry; works for backwards clocks and duplicate deadlines too. */
|
|
124
|
+
export function completionBoundary(history, now) {
|
|
125
|
+
let low = 0;
|
|
126
|
+
let high = history.expiries.length;
|
|
127
|
+
const time = Date.parse(now);
|
|
128
|
+
while (low < high) {
|
|
129
|
+
const mid = (low + high) >>> 1;
|
|
130
|
+
if (Date.parse(history.expiries[mid] ?? "") <= time)
|
|
131
|
+
low = mid + 1;
|
|
132
|
+
else
|
|
133
|
+
high = mid;
|
|
134
|
+
}
|
|
135
|
+
return low;
|
|
136
|
+
}
|
|
137
|
+
/** Compile the authored tree once. Arbitrary predicates still inspect the shared ordered corpus
|
|
138
|
+
* when history or an expiry boundary changes, never on an unchanged clock interval.
|
|
139
|
+
* Latest's input is its own 'of', not sibling filters. */
|
|
140
|
+
export function compileCompletion(query, node) {
|
|
141
|
+
const value = (entry, field, now) => {
|
|
142
|
+
const { observation: { payload, at }, nodes, } = entry;
|
|
143
|
+
if (field === "node")
|
|
144
|
+
return nodes;
|
|
145
|
+
if (field === "at")
|
|
146
|
+
return at;
|
|
147
|
+
if (field === "playground" && payload.kind === "demonstration")
|
|
148
|
+
return payload.material?.playground;
|
|
149
|
+
if (field === "current")
|
|
150
|
+
return payload.kind === "completion"
|
|
151
|
+
? payload.validUntil === undefined || Date.parse(payload.validUntil) > now
|
|
152
|
+
: undefined;
|
|
153
|
+
return field in payload ? Reflect.get(payload, field) : undefined;
|
|
154
|
+
};
|
|
155
|
+
function select(predicate) {
|
|
156
|
+
if ("all" in predicate) {
|
|
157
|
+
const children = predicate.all.map(select);
|
|
158
|
+
return (context) => {
|
|
159
|
+
const sets = children.map((child) => child(context));
|
|
160
|
+
return new Set(context.ordered.filter((entry) => sets.every((set) => set.has(entry))));
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if ("any" in predicate) {
|
|
164
|
+
const children = predicate.any.map(select);
|
|
165
|
+
return (context) => new Set(children.flatMap((child) => [...child(context)]));
|
|
166
|
+
}
|
|
167
|
+
if ("not" in predicate) {
|
|
168
|
+
const child = select(predicate.not);
|
|
169
|
+
return (context) => {
|
|
170
|
+
const excluded = child(context);
|
|
171
|
+
return new Set(context.ordered.filter((entry) => !excluded.has(entry)));
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if ("latest" in predicate) {
|
|
175
|
+
const child = select(predicate.latest.of);
|
|
176
|
+
return (context) => {
|
|
177
|
+
const matches = child(context);
|
|
178
|
+
const latest = new Map();
|
|
179
|
+
for (const entry of context.ordered)
|
|
180
|
+
if (matches.has(entry))
|
|
181
|
+
latest.set(canonicalJson(predicate.latest.per.map((field) => value(entry, field, context.now) ?? null)), entry);
|
|
182
|
+
return new Set(latest.values());
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const expected = canonicalJson(predicate.is);
|
|
186
|
+
return (context) => new Set(context.ordered.filter((entry) => {
|
|
187
|
+
if (predicate.field === "node")
|
|
188
|
+
return entry.nodes.some((id) => id === (predicate.is === "self" ? node : predicate.is));
|
|
189
|
+
const selected = value(entry, predicate.field, context.now);
|
|
190
|
+
return selected !== undefined && canonicalJson(selected) === expected;
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
193
|
+
function compile(completion) {
|
|
194
|
+
if ("all" in completion) {
|
|
195
|
+
const children = completion.all.map(compile);
|
|
196
|
+
return (context, counts, matched) => children.map((child) => child(context, counts, matched)).every(Boolean);
|
|
197
|
+
}
|
|
198
|
+
if ("any" in completion) {
|
|
199
|
+
const children = completion.any.map(compile);
|
|
200
|
+
return (context, counts, matched) => children.map((child) => child(context, counts, matched)).some(Boolean);
|
|
201
|
+
}
|
|
202
|
+
const selection = select(completion.of);
|
|
203
|
+
return (context, counts, matched) => {
|
|
204
|
+
const matches = selection(context);
|
|
205
|
+
for (const entry of matches)
|
|
206
|
+
matched.add(entry);
|
|
207
|
+
counts.push({ k: matches.size, required: completion.count });
|
|
208
|
+
return matches.size >= completion.count;
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
const evaluate = compile(query);
|
|
212
|
+
return (history, now, boundary = completionBoundary(history, now)) => {
|
|
213
|
+
const counts = [];
|
|
214
|
+
const matched = new Set();
|
|
215
|
+
const complete = evaluate({ ordered: history.ordered, now: Date.parse(now) }, counts, matched);
|
|
216
|
+
// Even negated predicates and queries without 'current' advertise the global next expiry.
|
|
217
|
+
const validUntil = history.expiries[boundary];
|
|
218
|
+
return {
|
|
219
|
+
complete,
|
|
220
|
+
counts,
|
|
221
|
+
citations: [...matched].map(({ observation }) => observation.id),
|
|
222
|
+
...(validUntil === undefined ? {} : { validUntil }),
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
export function evaluateCompletion(query, node, history, now) {
|
|
227
|
+
return compileCompletion(query, node)(prepareCompletionHistory(history), now);
|
|
228
|
+
}
|
|
229
|
+
/** Hosts discover report producers in the query instead of recognizing a canonical binding. */
|
|
230
|
+
export function completionNamesPlayground(query, playground) {
|
|
231
|
+
if (query === undefined)
|
|
232
|
+
return false;
|
|
233
|
+
function names(predicate) {
|
|
234
|
+
if ("all" in predicate)
|
|
235
|
+
return predicate.all.some(names);
|
|
236
|
+
if ("any" in predicate)
|
|
237
|
+
return predicate.any.some(names);
|
|
238
|
+
if ("not" in predicate)
|
|
239
|
+
return names(predicate.not);
|
|
240
|
+
if ("latest" in predicate)
|
|
241
|
+
return names(predicate.latest.of);
|
|
242
|
+
return predicate.field === "playground" && predicate.is === playground;
|
|
243
|
+
}
|
|
244
|
+
if ("all" in query)
|
|
245
|
+
return query.all.some((child) => completionNamesPlayground(child, playground));
|
|
246
|
+
if ("any" in query)
|
|
247
|
+
return query.any.some((child) => completionNamesPlayground(child, playground));
|
|
248
|
+
return names(query.of);
|
|
249
|
+
}
|
|
250
|
+
/** Author a count of current complete reports for this Node and one Playground scope. */
|
|
251
|
+
export function playgroundCompletion(playground, scope) {
|
|
252
|
+
return {
|
|
253
|
+
count: 1,
|
|
254
|
+
of: {
|
|
255
|
+
all: [
|
|
256
|
+
{
|
|
257
|
+
latest: {
|
|
258
|
+
per: ["playground"],
|
|
259
|
+
of: {
|
|
260
|
+
all: [
|
|
261
|
+
{ field: "kind", is: "completion" },
|
|
262
|
+
{ field: "node", is: "self" },
|
|
263
|
+
{ field: "playground", is: playground },
|
|
264
|
+
{ field: "scope", is: scope },
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
{ field: "state", is: "complete" },
|
|
270
|
+
{ field: "current", is: true },
|
|
271
|
+
],
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
export function completionCaption(progress) {
|
|
276
|
+
return progress.counts.map(({ k, required }) => k + " of " + required).join("; ");
|
|
277
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Share adjacency, not results: each strict cone is independently visited and sorted. */
|
|
2
|
+
export function dependentCones(graph) {
|
|
3
|
+
const outgoing = new Map();
|
|
4
|
+
for (const edge of graph.edges) {
|
|
5
|
+
const neighbors = outgoing.get(edge.from);
|
|
6
|
+
if (neighbors === undefined)
|
|
7
|
+
outgoing.set(edge.from, [edge.to]);
|
|
8
|
+
else
|
|
9
|
+
neighbors.push(edge.to);
|
|
10
|
+
}
|
|
11
|
+
return (node) => {
|
|
12
|
+
const seen = new Set([node]);
|
|
13
|
+
const pending = [node];
|
|
14
|
+
const result = [];
|
|
15
|
+
for (const current of pending) {
|
|
16
|
+
for (const next of outgoing.get(current) ?? []) {
|
|
17
|
+
if (seen.has(next))
|
|
18
|
+
continue;
|
|
19
|
+
seen.add(next);
|
|
20
|
+
pending.push(next);
|
|
21
|
+
result.push(next);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result.sort();
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { NodeId } from "@mmon-co/ncept-core/patch";
|
|
2
|
+
/** Host-derived Lesson outcome, not demonstration evidence or a learner label. */
|
|
3
|
+
export type MilestoneDiagnosis = Readonly<{
|
|
4
|
+
errors: readonly Readonly<{
|
|
5
|
+
error: string;
|
|
6
|
+
cause: "integration" | "component-regression" | "missing-capability";
|
|
7
|
+
}>[];
|
|
8
|
+
regressedComponents: readonly NodeId[];
|
|
9
|
+
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { NodeId, ResolvedGraph } from "@mmon-co/ncept-core/patch";
|
|
2
|
+
import type { Observation } from "@mmon-co/ncept-core/observations";
|
|
3
|
+
import type { NodeProgress } from "@mmon-co/ncept-core/theory";
|
|
4
|
+
import { type Demonstration, type HomedRecord, type NodeState, type NodeSchedule } from "./state.js";
|
|
5
|
+
/** Decoded attempt facts; no host row identity or source-key policy lives here. */
|
|
6
|
+
export type DefaultDemonstrationReading = Omit<Demonstration, "kind" | "node"> & {
|
|
7
|
+
readonly node: NodeId;
|
|
8
|
+
readonly provenance: Demonstration["node"]["provenance"];
|
|
9
|
+
};
|
|
10
|
+
export declare function readDefaultDemonstration(payload: unknown): DefaultDemonstrationReading | undefined;
|
|
11
|
+
export interface DefaultNodeDiagnostics {
|
|
12
|
+
readonly progress: NodeProgress["status"];
|
|
13
|
+
readonly completion: NodeState["completion"];
|
|
14
|
+
readonly schedule?: NodeSchedule;
|
|
15
|
+
readonly citations: NodeState["citations"];
|
|
16
|
+
readonly records: readonly HomedRecord[];
|
|
17
|
+
}
|
|
18
|
+
/** Fold accepted history at an explicit instant, exposing facts, never the default state.
|
|
19
|
+
* Completion exposes the query and each count's observed/required progress. Effective recall
|
|
20
|
+
* records remain chronological and independently observable; no report-to-record link is inferred.
|
|
21
|
+
* Schedule retrievability is clock-dependent; all other schedule facts are replay-stable.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readDefaultDiagnostics(graph: ResolvedGraph, observations: readonly Observation[], now: string, requested: readonly NodeId[]): {
|
|
24
|
+
readonly nodes: Readonly<Partial<Record<NodeId, DefaultNodeDiagnostics>>>;
|
|
25
|
+
};
|