MVP M7: Socrates can propose model changes — review impact + Accept/Reject

The dock now has a "propose" button that asks Socrates to suggest one
high-value structural change. He returns a structured payload (reasoning
+ ops + impactSummary) which renders as a ProposalCard inline in the
thread. Accept routes the ops through the same useApply() pipeline as
user-originated edits (optimistic local + persisted POST + server
reconciliation).

apps/web/lib/llm/prompts/socrates/propose.md
- Promoted verbatim from phase-0/src/prompts/.

apps/web/lib/llm/proposeChange.ts
- Server-side proposeChange() — calls the LLM with character + propose
  prompts + trimmed model + active issues. JSON Schema uses oneOf per
  op kind (Phase 0 lesson: small models need this to produce real op
  shapes instead of cramming everything into the kind name).
- normalizeOps() converts the LLM-emitted op shapes into canonical
  ModelOp[] (fills in property ids/multiplicity defaults, expands
  satisfiedBy[] into RequirementRelation[], splits PropertyType union
  per kind).

apps/web/lib/sysml/impact.ts
- Pure pre-apply impact analysis. Runs the ops through the same
  applyOps() reducer locally, then diffs:
    - structural: added / removed / changed elements (per kind)
    - validation: issues created vs resolved (by canonical issue key)
    - dep-graph blast radius (closure of touched element ids on the
      post-apply graph)
  Headline stats summarized as deltas (+1 block, −1 assoc, etc.) for
  the proposal card.

apps/web/app/api/projects/[projectId]/socrates/propose
- POST: returns { reasoning, ops, impactSummary, meta }. Pure read of
  the model — does not apply anything; client must POST /apply with the
  same ops to commit.

apps/web/components/socrates/ProposalCard.tsx
- In-dock card: PROPOSAL tag + delta stats / reasoning / collapsible
  ops list / Impact section (added/removed/changed) / Validation diff
  (resolves ✓ / creates ⚠) / Accept + Reject. Disabled when impact
  analysis flagged the apply as illegal.

apps/web/components/socrates/SocratesDock.tsx
- New "propose" button between the input field and send. Renders
  ProposalCard for assistant turns of role "proposal". Accept calls
  useApply(); on success the card collapses to a "✓ Applied" system
  bubble. On apply failure the dock shows the structured error
  messages.
- New "system" bubble role for apply confirmations + dismissals.

apps/web/components/diagram-canvas/DiagramCanvas.tsx
- Bug fix: new blocks/constraints arriving via the model→RF sync (e.g.
  from accepted proposals) are now positioned to the right of the
  existing layout instead of stacking at (0, 0) offscreen.

apps/web/lib/sync/ModelStore.tsx
- Bug fix / observability: background-POST failures and version
  mismatches now log to the console with structured context instead of
  being silently swallowed. The server's authoritative state still
  replaces the optimistic local state on response, but the user can now
  see why their accept appeared to do nothing (typically: page tab
  was at version N but server had advanced to N+1).
This commit is contained in:
2026-04-30 00:27:29 +02:00
parent 78faca9968
commit 4b8e3f04ee
9 changed files with 1134 additions and 48 deletions

View File

@@ -0,0 +1,44 @@
# Mode: Propose a model change
You are reviewing the existing model and the active findings (assumptions, risks, inconsistencies). The PM has asked you to propose ONE concrete change to the model.
Pick the highest-value change you can make. Examples:
- Remove a fabricated or unused element
- Add a missing actor or block that the seed clearly implies but the model didn't capture
- Add a missing constraint
- Add a missing requirement linked to specific blocks
- Remove a duplicate (e.g., a constraint already covered by a requirement, or vice versa)
- Add an association the model is missing
## Output
Return a JSON object with:
- `reasoning` — 13 sentences explaining what you propose to change and why. Reference specific element ids.
- `ops` — an array of operations. Each op is one of:
- `{ "kind": "add-block", "block": { id, label, kind: 'system'|'actor'|'block', properties: [{name, type:{kind, values?}}] } }`
- `{ "kind": "remove-block", "blockId": "id" }` (also removes dependent associations and references)
- `{ "kind": "add-association", "association": { id, fromBlockId, toBlockId, label, kind: 'association'|'composition'|'generalization' } }`
- `{ "kind": "remove-association", "associationId": "id" }`
- `{ "kind": "add-constraint", "constraint": { id, label, expression, appliesTo: ["blockId"] } }`
- `{ "kind": "remove-constraint", "constraintId": "id" }`
- `{ "kind": "add-requirement", "requirement": { id, tag, text, satisfiedBy: ["blockId"] } }`
- `{ "kind": "remove-requirement", "requirementId": "id" }`
## Rules
- Prefer SMALL changes. 14 ops is ideal. A proposal that rewrites half the model is too big.
- Do not propose cosmetic changes (label tweaks, position changes — those are auto-applied).
- Every id you reference (`blockId`, `associationId`, etc.) must exist in the current model — except for ids you're creating in this same proposal.
- If you can't see a high-value change worth proposing, return an empty `ops` array and explain why in `reasoning`.
## Output schema
```json
{
"reasoning": "string (13 sentences)",
"ops": [ /* array of op objects per the kinds above */ ]
}
```
Return ONLY the JSON object. No prose preamble, no code fences.

View File

@@ -0,0 +1,315 @@
// Server-side: ask Socrates to propose a single model change.
//
// Returns { reasoning, ops } as a structured JSON object. The op schema uses
// oneOf per kind (Phase 0 lesson: small models cram all op fields into the
// kind name without it). Caller is responsible for applying the ops; this
// function only generates them.
import "server-only";
import { defaultGateway, chatJSON, type Message } from "./gateway";
import { loadPrompt } from "./prompts";
import type { SysMLModel } from "../sysml/model";
import type { ValidationIssue } from "../sysml/validate";
import type { ModelOp } from "../sync/ops";
// ─── Op-shape JSON Schema (oneOf per kind) ──────────────────────────────
const propertyTypeSchema = {
type: "object",
additionalProperties: false,
required: ["kind"],
properties: {
kind: { type: "string", enum: ["string", "number", "boolean", "enum"] },
values: { type: "array", items: { type: "string" } },
},
} as const;
const blockShape = {
type: "object",
additionalProperties: false,
required: ["id", "label", "kind"],
properties: {
id: { type: "string", minLength: 1 },
label: { type: "string", minLength: 1 },
kind: { type: "string", enum: ["system", "actor", "block"] },
properties: {
type: "array",
items: {
type: "object",
additionalProperties: false,
required: ["name", "type"],
properties: {
name: { type: "string", minLength: 1 },
type: propertyTypeSchema,
},
},
},
},
} as const;
const associationShape = {
type: "object",
additionalProperties: false,
required: ["id", "fromBlockId", "toBlockId", "kind"],
properties: {
id: { type: "string", minLength: 1 },
fromBlockId: { type: "string", minLength: 1 },
toBlockId: { type: "string", minLength: 1 },
label: { type: "string" },
kind: { type: "string", enum: ["association", "composition", "aggregation", "generalization"] },
},
} as const;
const constraintShape = {
type: "object",
additionalProperties: false,
required: ["id", "label"],
properties: {
id: { type: "string", minLength: 1 },
label: { type: "string", minLength: 1 },
expression: { type: "string" },
appliesTo: { type: "array", items: { type: "string" } },
},
} as const;
const requirementShape = {
type: "object",
additionalProperties: false,
required: ["id", "tag", "text"],
properties: {
id: { type: "string", minLength: 1 },
tag: { type: "string", minLength: 1 },
text: { type: "string", minLength: 1 },
satisfiedBy: { type: "array", items: { type: "string" } },
},
} as const;
const proposalJsonSchema = {
type: "object",
additionalProperties: false,
required: ["reasoning", "ops"],
properties: {
reasoning: { type: "string", minLength: 1 },
ops: {
type: "array",
items: {
oneOf: [
{ type: "object", additionalProperties: false, required: ["kind", "block"],
properties: { kind: { type: "string", enum: ["add-block"] }, block: blockShape } },
{ type: "object", additionalProperties: false, required: ["kind", "blockId"],
properties: { kind: { type: "string", enum: ["remove-block"] }, blockId: { type: "string", minLength: 1 } } },
{ type: "object", additionalProperties: false, required: ["kind", "association"],
properties: { kind: { type: "string", enum: ["add-association"] }, association: associationShape } },
{ type: "object", additionalProperties: false, required: ["kind", "associationId"],
properties: { kind: { type: "string", enum: ["remove-association"] }, associationId: { type: "string", minLength: 1 } } },
{ type: "object", additionalProperties: false, required: ["kind", "constraint"],
properties: { kind: { type: "string", enum: ["add-constraint"] }, constraint: constraintShape } },
{ type: "object", additionalProperties: false, required: ["kind", "constraintId"],
properties: { kind: { type: "string", enum: ["remove-constraint"] }, constraintId: { type: "string", minLength: 1 } } },
{ type: "object", additionalProperties: false, required: ["kind", "requirement"],
properties: { kind: { type: "string", enum: ["add-requirement"] }, requirement: requirementShape } },
{ type: "object", additionalProperties: false, required: ["kind", "requirementId"],
properties: { kind: { type: "string", enum: ["remove-requirement"] }, requirementId: { type: "string", minLength: 1 } } },
],
},
},
},
} as const;
// ─── Public API ──────────────────────────────────────────────────────────
export interface ProposeArgs {
model: SysMLModel;
issues?: ValidationIssue[];
/** Optional user-supplied prompt narrowing what to propose. */
userPrompt?: string;
}
interface RawProposalOp {
kind: string;
[k: string]: unknown;
}
export interface ProposeResult {
reasoning: string;
/** Ops in their LLM-emitted shape — caller normalizes to the canonical
* ModelOp tagged-union (this handles add-block.block.properties needing
* id/multiplicity defaults, etc.). */
ops: ModelOp[];
inputTokens: number;
outputTokens: number;
provider: string;
model: string;
}
export async function proposeChange(args: ProposeArgs): Promise<ProposeResult> {
const character = loadPrompt("socrates/character.md");
const propose = loadPrompt("socrates/propose.md");
const userPayload =
`Current model:\n\`\`\`json\n${JSON.stringify(trimModel(args.model), null, 2)}\n\`\`\`\n\n` +
(args.issues?.length
? `Active validation issues:\n\`\`\`json\n${JSON.stringify(args.issues.slice(0, 20), null, 2)}\n\`\`\`\n\n`
: "") +
(args.userPrompt
? `User has asked:\n${args.userPrompt}\n`
: `Pick the highest-value change you can make right now.\n`);
const messages: Message[] = [
{ role: "system", content: `${character}\n\n---\n\n${propose}` },
{ role: "user", content: userPayload },
];
const gateway = defaultGateway();
const { value, result } = await chatJSON<{ reasoning: string; ops: RawProposalOp[] }>(
gateway,
messages,
{
temperature: 0.3,
maxTokens: 1024,
jsonSchema: { name: "proposal", schema: proposalJsonSchema as Record<string, unknown> },
jsonObjectMode: true,
maxRepairs: 2,
}
);
const ops = normalizeOps(value?.ops ?? []);
return {
reasoning: typeof value?.reasoning === "string" ? value.reasoning : "",
ops,
inputTokens: result.inputTokens,
outputTokens: result.outputTokens,
provider: gateway.provider,
model: gateway.model,
};
}
// ─── Normalize the LLM-shape ops to canonical ModelOp ───────────────────
function normalizeOps(raw: RawProposalOp[]): ModelOp[] {
const out: ModelOp[] = [];
for (const op of raw) {
if (!op || typeof op.kind !== "string") continue;
switch (op.kind) {
case "add-block": {
const block = op.block as { id?: string; label?: string; kind?: string; properties?: Array<{ name: string; type: { kind: string; values?: string[] } }> } | undefined;
if (!block?.id || !block.label || !isBlockKind(block.kind)) continue;
out.push({
kind: "add-block",
block: {
id: block.id,
label: block.label,
kind: block.kind,
stereotypes: [block.kind],
properties: (block.properties ?? []).map((p, i) => ({
id: `p${i + 1}`,
name: p.name,
type: p.type.kind === "enum"
? { kind: "enum" as const, values: p.type.values ?? [] }
: { kind: p.type.kind as "string" | "number" | "boolean" },
multiplicity: "0..1" as const,
})),
},
tempId: block.id,
});
break;
}
case "remove-block": {
if (typeof op.blockId === "string") out.push({ kind: "remove-block", blockId: op.blockId });
break;
}
case "add-association": {
const a = op.association as { id?: string; fromBlockId?: string; toBlockId?: string; label?: string; kind?: string } | undefined;
if (!a?.id || !a.fromBlockId || !a.toBlockId || !isAssocKind(a.kind)) continue;
out.push({
kind: "add-association",
association: {
id: a.id,
fromBlockId: a.fromBlockId,
toBlockId: a.toBlockId,
label: a.label ?? "",
kind: a.kind,
},
tempId: a.id,
});
break;
}
case "remove-association": {
if (typeof op.associationId === "string") out.push({ kind: "remove-association", associationId: op.associationId });
break;
}
case "add-constraint": {
const c = op.constraint as { id?: string; label?: string; expression?: string; appliesTo?: string[] } | undefined;
if (!c?.id || !c.label) continue;
out.push({
kind: "add-constraint",
constraint: { id: c.id, label: c.label, expression: c.expression ?? "", appliesTo: c.appliesTo ?? [] },
tempId: c.id,
});
break;
}
case "remove-constraint": {
if (typeof op.constraintId === "string") out.push({ kind: "remove-constraint", constraintId: op.constraintId });
break;
}
case "add-requirement": {
const r = op.requirement as { id?: string; tag?: string; text?: string; satisfiedBy?: string[] } | undefined;
if (!r?.id || !r.tag || !r.text) continue;
out.push({
kind: "add-requirement",
requirement: {
id: r.id,
tag: r.tag,
text: r.text,
relations: (r.satisfiedBy ?? []).map(b => ({ kind: "satisfy" as const, blockId: b })),
},
tempId: r.id,
});
break;
}
case "remove-requirement": {
if (typeof op.requirementId === "string") out.push({ kind: "remove-requirement", requirementId: op.requirementId });
break;
}
}
}
return out;
}
function isBlockKind(s: unknown): s is "system" | "actor" | "block" {
return s === "system" || s === "actor" || s === "block";
}
function isAssocKind(s: unknown): s is "association" | "composition" | "aggregation" | "generalization" {
return s === "association" || s === "composition" || s === "aggregation" || s === "generalization";
}
function trimModel(model: SysMLModel): unknown {
return {
blocks: model.blocks.map(b => ({
id: b.id,
label: b.label,
kind: b.kind,
properties: b.properties.map(p => p.name),
})),
associations: model.associations.map(a => ({
id: a.id,
from: a.fromBlockId,
to: a.toBlockId,
label: a.label,
kind: a.kind,
})),
constraints: model.constraints.map(c => ({
id: c.id,
label: c.label,
appliesTo: c.appliesTo,
})),
requirements: model.requirements.map(r => ({
id: r.id,
tag: r.tag,
text: r.text,
satisfiedBy: r.relations
.filter(rel => rel.kind === "satisfy")
.map(rel => (rel as { kind: "satisfy"; blockId: string }).blockId),
})),
};
}

View File

@@ -48,7 +48,7 @@ export function ModelStoreProvider({ initialModel, initialVersion, projectId, ch
return result.applied ? result.model : current;
});
// 2. Background: POST to the server (best-effort; server is the truth).
// 2. Background: POST to the server (server is authoritative).
if (projectId && result.applied) {
const expectedVersion = versionRef.current;
void fetch(`/api/projects/${encodeURIComponent(projectId)}/apply`, {
@@ -57,24 +57,31 @@ export function ModelStoreProvider({ initialModel, initialVersion, projectId, ch
body: JSON.stringify({ ops, expectedVersion }),
})
.then(async res => {
if (!res.ok) return;
if (!res.ok) {
const body = await res.text().catch(() => "");
console.error("[ModelStore] /apply HTTP", res.status, body.slice(0, 200));
return;
}
const data = (await res.json()) as {
applied: boolean;
model: SysMLModel;
version: number;
idMapping: Record<string, string>;
errors?: Array<{ opIndex: number; code: string; message: string }>;
};
// Always trust the server's model + version after a successful apply
if (!data.applied) {
console.warn(
"[ModelStore] server rejected ops; reverting to server state.",
data.errors,
"expected", expectedVersion, "got", data.version
);
}
versionRef.current = data.version;
setVersion(data.version);
// Note: server's model may differ in tempId resolution from our local
// optimistic apply. Replacing wholesale is safe because the model
// shape is structural; React Flow's useEffect will diff and update.
setModel(data.model);
})
.catch(() => {
// Network error — keep optimistic state. The next successful apply
// will reconcile via the new server model.
.catch(err => {
console.error("[ModelStore] /apply network error:", err);
});
}
return result;

View File

@@ -0,0 +1,164 @@
// Pre-apply impact analysis: given the current model + a candidate set of
// ops, simulate the apply (without persisting) and report what would change.
//
// Used for the M7 proposal card so the PM can see consequences before
// approving. Composed of:
// - structural diff (added/removed/changed elements)
// - validation diff (issues created/resolved)
// - dep-graph blast radius (closure of element ids touched)
import { applyOps } from "../sync/applyOps";
import { validate, type ValidationIssue } from "./validate";
import { buildDepGraph, dependentsOf } from "./depgraph";
import type { SysMLModel } from "./model";
import type { ModelOp } from "../sync/ops";
export interface ElementSummary {
id: string;
kind: "block" | "association" | "constraint" | "requirement" | "property";
label: string;
}
export interface ImpactSummary {
/** Was the candidate apply legal at all? Errors mean something rejected. */
ok: boolean;
errors: Array<{ opIndex: number; code: string; message: string }>;
added: ElementSummary[];
removed: ElementSummary[];
changed: ElementSummary[];
issuesCreated: ValidationIssue[];
issuesResolved: ValidationIssue[];
/** All element ids in the dep-graph closure of the touched elements. */
blastRadius: string[];
/** Compact stats for headline display. */
stats: {
blocksDelta: number;
associationsDelta: number;
constraintsDelta: number;
requirementsDelta: number;
issuesDelta: number;
};
}
export function analyzeImpact(currentModel: SysMLModel, ops: ModelOp[]): ImpactSummary {
const beforeIssues = validate(currentModel);
const result = applyOps(currentModel, ops);
if (!result.applied) {
return {
ok: false,
errors: result.errors,
added: [], removed: [], changed: [],
issuesCreated: [], issuesResolved: [],
blastRadius: [],
stats: { blocksDelta: 0, associationsDelta: 0, constraintsDelta: 0, requirementsDelta: 0, issuesDelta: 0 },
};
}
const after = result.model;
const afterIssues = validate(after);
const beforeBlocks = idMap(currentModel.blocks);
const afterBlocks = idMap(after.blocks);
const beforeAssocs = idMap(currentModel.associations);
const afterAssocs = idMap(after.associations);
const beforeCons = idMap(currentModel.constraints);
const afterCons = idMap(after.constraints);
const beforeReqs = idMap(currentModel.requirements);
const afterReqs = idMap(after.requirements);
const added: ElementSummary[] = [];
const removed: ElementSummary[] = [];
const changed: ElementSummary[] = [];
// Blocks
for (const [id, b] of afterBlocks) {
const prev = beforeBlocks.get(id);
if (!prev) added.push({ id, kind: "block", label: b.label });
else if (prev.label !== b.label || prev.kind !== b.kind || prev.properties.length !== b.properties.length) {
changed.push({ id, kind: "block", label: b.label });
}
}
for (const [id, b] of beforeBlocks) {
if (!afterBlocks.has(id)) removed.push({ id, kind: "block", label: b.label });
}
// Associations
for (const [id, a] of afterAssocs) {
const prev = beforeAssocs.get(id);
if (!prev) added.push({ id, kind: "association", label: a.label || `${a.fromBlockId}${a.toBlockId}` });
else if (prev.label !== a.label || prev.kind !== a.kind || prev.fromBlockId !== a.fromBlockId || prev.toBlockId !== a.toBlockId) {
changed.push({ id, kind: "association", label: a.label || `${a.fromBlockId}${a.toBlockId}` });
}
}
for (const [id, a] of beforeAssocs) {
if (!afterAssocs.has(id)) removed.push({ id, kind: "association", label: a.label || `${a.fromBlockId}${a.toBlockId}` });
}
// Constraints
for (const [id, c] of afterCons) {
const prev = beforeCons.get(id);
if (!prev) added.push({ id, kind: "constraint", label: c.label });
else if (prev.label !== c.label || prev.expression !== c.expression || prev.appliesTo.length !== c.appliesTo.length) {
changed.push({ id, kind: "constraint", label: c.label });
}
}
for (const [id, c] of beforeCons) {
if (!afterCons.has(id)) removed.push({ id, kind: "constraint", label: c.label });
}
// Requirements
for (const [id, r] of afterReqs) {
const prev = beforeReqs.get(id);
if (!prev) added.push({ id, kind: "requirement", label: r.tag });
else if (prev.tag !== r.tag || prev.text !== r.text || prev.relations.length !== r.relations.length) {
changed.push({ id, kind: "requirement", label: r.tag });
}
}
for (const [id, r] of beforeReqs) {
if (!afterReqs.has(id)) removed.push({ id, kind: "requirement", label: r.tag });
}
// Validation diff (by message — issues carry no stable id)
const beforeKeys = new Set(beforeIssues.map(issueKey));
const afterKeys = new Set(afterIssues.map(issueKey));
const issuesCreated = afterIssues.filter(i => !beforeKeys.has(issueKey(i)));
const issuesResolved = beforeIssues.filter(i => !afterKeys.has(issueKey(i)));
// Dep-graph blast radius — closure over each touched element on the AFTER graph
const graph = buildDepGraph(after);
const touched = new Set<string>([
...added.map(e => e.id),
...removed.map(e => e.id),
...changed.map(e => e.id),
]);
const blast = new Set<string>();
for (const id of touched) {
blast.add(id);
for (const r of dependentsOf(graph, id)) blast.add(r);
}
return {
ok: true,
errors: [],
added, removed, changed,
issuesCreated, issuesResolved,
blastRadius: [...blast],
stats: {
blocksDelta: after.blocks.length - currentModel.blocks.length,
associationsDelta: after.associations.length - currentModel.associations.length,
constraintsDelta: after.constraints.length - currentModel.constraints.length,
requirementsDelta: after.requirements.length - currentModel.requirements.length,
issuesDelta: afterIssues.length - beforeIssues.length,
},
};
}
function idMap<T extends { id: string }>(items: T[]): Map<string, T> {
return new Map(items.map(i => [i.id, i]));
}
function issueKey(i: ValidationIssue): string {
return `${i.code}|${i.severity}|${i.message}`;
}