@mmon-co/ncept-local 0.3.7 → 0.3.9

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/directory.js CHANGED
@@ -120,14 +120,20 @@ async function locked(dir, operation) {
120
120
  await release();
121
121
  }
122
122
  }
123
+ function installedPin(files, theories) {
124
+ const { theory, range } = parseManifest(files.get("profile.json")).theory;
125
+ const version = theories.find((candidate) => candidate.id === theory)?.version;
126
+ return version !== undefined && range === "~" + version.split(".").slice(0, 2).join(".");
127
+ }
123
128
  export async function openProfile(dir, theories, options = {}) {
124
129
  dir = resolve(dir);
125
130
  const readOnlyFiles = options.readOnly ? await filesAt(dir) : undefined;
126
- if (!readOnlyFiles || migrateLessonLog(readOnlyFiles) !== readOnlyFiles)
131
+ // A read-only open migrates like a writable one, but only takes the lock (and pays a second
132
+ // validation beyond read()'s) when the Lesson log or the stored Theory pin is behind.
133
+ if (!readOnlyFiles || migrateLessonLog(readOnlyFiles) !== readOnlyFiles || !installedPin(readOnlyFiles, theories))
127
134
  await locked(dir, async () => {
128
135
  const stored = await filesAt(dir);
129
- const initial = options.readOnly ? migrateLessonLog(stored) : migrateProfileFiles(stored, theories);
130
- const profile = validate(initial, theories);
136
+ const initial = migrateProfileFiles(stored, theories);
131
137
  // Immutable data first, then a single atomic live-head/log/pin switch. An interruption
132
138
  // leaves either the old generation or the complete new one; retry never repeats a step.
133
139
  for (const [path, bytes] of initial) {
@@ -140,7 +146,7 @@ export async function openProfile(dir, theories, options = {}) {
140
146
  if (options.readOnly)
141
147
  return;
142
148
  // A copied or moved Profile must still appear in pi's cwd-filtered /resume list.
143
- for (const filename of Object.values(profile.manifest.sessions)) {
149
+ for (const filename of Object.values(parseManifest(manifest).sessions)) {
144
150
  const path = "sessions/" + filename;
145
151
  const bytes = initial.get(path);
146
152
  if (bytes) {
@@ -1,5 +1,5 @@
1
- import { type HostCapability, type Skill } from "@mmon-co/ncept-core";
1
+ import { type GraphDraftBatch, type HostCapability, type Skill } from "@mmon-co/ncept-core";
2
2
  import type { HostTool } from "./host.js";
3
3
  import type { LearningHost } from "./learning.js";
4
4
  import { type LocalSession } from "./runtime.js";
5
- export declare function hostTools<S>(host: LearningHost<S>, opened: LocalSession, capabilities: readonly HostCapability[], skills: readonly Skill[], metadataKeys: readonly string[]): readonly HostTool[];
5
+ export declare function hostTools<S>(host: LearningHost<S>, opened: LocalSession, capabilities: readonly HostCapability[], skills: readonly Skill[], metadataKeys: readonly string[], batch?: GraphDraftBatch): readonly HostTool[];
@@ -1,6 +1,6 @@
1
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
- import { applyGraphDiff, attachmentDigest, compileGraphDiff, composePatch, isMaterialMeta, isMission, isNodeId, nodeCascadedProperty, parseGraphQuery, revisionContent, projectVisualGraph, sameExactPatchRef, selectGraph, tagIndex, } from "@mmon-co/ncept-core";
3
+ import { applyGraphDiff, attachmentDigest, compileGraphDiff, composePatch, graphDraftOperationKey, isMaterialMeta, isMission, isNodeId, nodeCascadedProperty, parseGraphQuery, revisionContent, projectVisualGraph, sameExactPatchRef, selectGraph, tagIndex, } from "@mmon-co/ncept-core";
4
4
  import { localPayload, platform, sessionStates, transcript } from "./runtime.js";
5
5
  import { mechanics } from "./mechanics.js";
6
6
  import { object } from "./sessions.js";
@@ -60,12 +60,30 @@ async function acceptContent(host, session, from, content, about = session.about
60
60
  ]);
61
61
  return to;
62
62
  }
63
- function graphDiff(host, input, session) {
63
+ function putBatch(previous, puts) {
64
+ const indexed = new Map();
65
+ for (const [i, operation] of previous.entries())
66
+ indexed.set(graphDraftOperationKey(operation) ?? i, operation);
67
+ for (const operation of puts)
68
+ indexed.set(graphDraftOperationKey(operation) ?? operation, operation);
69
+ return [...indexed.values()];
70
+ }
71
+ function graphDiff(host, input, session, batch) {
64
72
  return host.runtime.snapshot().then(async (view) => {
65
- if (!Array.isArray(input.operations))
73
+ const draft = batch && host.drafts.get(session.id);
74
+ const previous = draft && draft.base === view.graph.terminal.digest ? draft.edits : undefined;
75
+ const typed = batch?.compile(input, view.graph, previous?.operations ?? []);
76
+ if (!typed && !Array.isArray(input.operations))
66
77
  throw new Error("Expected graph operations");
67
- const compiled = compileGraphDiff(view.bundle, operations(input.operations), input.targets === undefined ? session.about : nodes(input.targets), citations(input.citations), input.retractions === undefined ? [] : strings(input.retractions));
68
- return { view, compiled, content: applyGraphDiff(await host.runtime.content(), compiled) };
78
+ const ordered = typed && input.operations === undefined ? [] : operations(input.operations);
79
+ const edits = {
80
+ operations: typed ? putBatch(previous?.operations ?? [], [...typed.operations, ...ordered]) : ordered,
81
+ targets: typed?.targets ?? (input.targets === undefined ? (previous?.targets ?? session.about) : nodes(input.targets)),
82
+ citations: input.citations === undefined ? (previous?.citations ?? []) : citations(input.citations),
83
+ retractions: input.retractions === undefined ? (previous?.retractions ?? []) : strings(input.retractions),
84
+ };
85
+ const compiled = compileGraphDiff(view.bundle, edits.operations, edits.targets, edits.citations, edits.retractions);
86
+ return { view, compiled, edits, content: applyGraphDiff(await host.runtime.content(), compiled) };
69
87
  });
70
88
  }
71
89
  function page(items, input) {
@@ -80,7 +98,7 @@ function page(items, input) {
80
98
  continueCursor: next >= items.length ? null : String(next),
81
99
  };
82
100
  }
83
- export function hostTools(host, opened, capabilities, skills, metadataKeys) {
101
+ export function hostTools(host, opened, capabilities, skills, metadataKeys, batch) {
84
102
  const tools = [];
85
103
  function mount(name, description, definition, run) {
86
104
  tools.push({
@@ -211,9 +229,18 @@ export function hostTools(host, opened, capabilities, skills, metadataKeys) {
211
229
  });
212
230
  }
213
231
  if (capabilities.includes("graph-draft")) {
214
- mount("edit_graph_draft", "Compile graph operations against the personal Patch. This replaces the unsaved draft, not the Profile. Propose graph-diff to ask the learner to accept it.", schema(graphProperties, ["operations"]), async (raw, session) => {
215
- const prepared = await graphDiff(host, raw, session);
216
- host.drafts.set(session.id, { base: prepared.view.graph.terminal.digest, content: prepared.content });
232
+ mount("edit_graph_draft", (batch
233
+ ? "Add nodes, edges, tags and targets to the unsaved graph draft; raw Core operations run after the typed fields. A call with operations alone replaces the draft. "
234
+ : "Compile graph operations into a draft against the personal Patch. ") +
235
+ "Propose graph-diff to ask the learner to accept it.", schema({ ...graphProperties, ...batch?.properties }, batch ? [] : ["operations"]), async (raw, session) => {
236
+ const typed = batch &&
237
+ Object.keys(batch.properties).some((key) => Object.hasOwn(raw, key) && (key !== "targets" || raw.operations === undefined));
238
+ const prepared = await graphDiff(host, raw, session, typed ? batch : undefined);
239
+ host.drafts.set(session.id, {
240
+ base: prepared.view.graph.terminal.digest,
241
+ content: prepared.content,
242
+ edits: prepared.edits,
243
+ });
217
244
  return { content: { status: "draft", report: prepared.compiled.report, graph: prepared.compiled.graph } };
218
245
  });
219
246
  }
@@ -1,4 +1,4 @@
1
- import { type LessonProgress, type LessonReading, type Mission, type ModeEntry, type NodeId, type PatchContent, type SessionMode, type Theory } from "@mmon-co/ncept-core";
1
+ import { type LessonProgress, type LessonReading, type Mission, type ModeEntry, type NodeId, type PatchContent, type Citation, type SemanticOperation, type SessionMode, type Theory } from "@mmon-co/ncept-core";
2
2
  import type { Host, HostChatSetup, HostConfig, HostSession, MissionReading, ModeSwitch, OpenHostOptions, SessionOpening, SessionIntent } from "./host.js";
3
3
  import { Runtime, type LocalSession } from "./runtime.js";
4
4
  type LearningView<S> = Awaited<ReturnType<Runtime<S>["snapshot"]>>;
@@ -53,6 +53,12 @@ export declare class LearningHost<S> {
53
53
  readonly drafts: Map<string, {
54
54
  base: string;
55
55
  content: PatchContent;
56
+ edits: {
57
+ operations: readonly SemanticOperation[];
58
+ targets: readonly NodeId[];
59
+ citations: readonly Citation[];
60
+ retractions: readonly string[];
61
+ };
56
62
  }>;
57
63
  constructor(runtime: Runtime<S>, confirm: OpenHostOptions<S>["confirm"], onCloseError?: (error: unknown) => void);
58
64
  serial<T>(operation: () => Promise<T>): Promise<T>;
package/dist/learning.js CHANGED
@@ -761,7 +761,10 @@ export class LearningHost {
761
761
  ]
762
762
  .filter(Boolean)
763
763
  .join("\n\n"),
764
- tools: [...tools, ...hostTools(this, session, capabilities, setup.skills, selected?.metadataKeys ?? [])],
764
+ tools: [
765
+ ...tools,
766
+ ...hostTools(this, session, capabilities, setup.skills, selected?.metadataKeys ?? [], selected?.graphDraftBatch),
767
+ ],
765
768
  skills: [...setup.skills, mechanics],
766
769
  };
767
770
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmon-co/ncept-local",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",