activating-expertise 0.1.0.dev0__py3-none-any.whl

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.
Files changed (61) hide show
  1. activating_expertise-0.1.0.dev0.dist-info/METADATA +28 -0
  2. activating_expertise-0.1.0.dev0.dist-info/RECORD +61 -0
  3. activating_expertise-0.1.0.dev0.dist-info/WHEEL +5 -0
  4. activating_expertise-0.1.0.dev0.dist-info/entry_points.txt +2 -0
  5. activating_expertise-0.1.0.dev0.dist-info/licenses/LICENSE-DOCS +29 -0
  6. activating_expertise-0.1.0.dev0.dist-info/licenses/LICENSE-SOFTWARE +661 -0
  7. activating_expertise-0.1.0.dev0.dist-info/top_level.txt +1 -0
  8. ae/__init__.py +0 -0
  9. ae/analysis.py +193 -0
  10. ae/cli.py +496 -0
  11. ae/concept_server.py +209 -0
  12. ae/concept_viewer.py +640 -0
  13. ae/concepts.py +155 -0
  14. ae/core.py +1303 -0
  15. ae/dspy_xp.py +894 -0
  16. ae/graph.py +733 -0
  17. ae/guides/__init__.py +0 -0
  18. ae/guides/debugging.md +434 -0
  19. ae/guides/design_maturation.md +141 -0
  20. ae/guides/hygienator.md +334 -0
  21. ae/guides/landscape_review.md +175 -0
  22. ae/guides/model_evaluation.md +313 -0
  23. ae/guides/model_onboarding.md +386 -0
  24. ae/guides/operational_guide.md +869 -0
  25. ae/guides/process_improvement.md +296 -0
  26. ae/guides/quick_start.md +123 -0
  27. ae/guides/roadmap.md +443 -0
  28. ae/guides/synergistic_xp.md +416 -0
  29. ae/seed_concepts/__init__.py +1 -0
  30. ae/seed_concepts/ae-concept-context.jsonld +62 -0
  31. ae/seed_concepts/ai-keeping-up.jsonld +121 -0
  32. ae/seed_concepts/bc-activities.jsonld +97 -0
  33. ae/seed_concepts/bounded-rationality.jsonld +88 -0
  34. ae/seed_concepts/collaborative-agent.jsonld +91 -0
  35. ae/seed_concepts/coming-to-know.jsonld +123 -0
  36. ae/seed_concepts/complement.jsonld +360 -0
  37. ae/seed_concepts/comprehensibility-as-participation.jsonld +101 -0
  38. ae/seed_concepts/constellation-architecture.jsonld +136 -0
  39. ae/seed_concepts/decision-network.jsonld +120 -0
  40. ae/seed_concepts/design-maturation.jsonld +71 -0
  41. ae/seed_concepts/discourse-ontology.jsonld +126 -0
  42. ae/seed_concepts/divergence-regimes.jsonld +94 -0
  43. ae/seed_concepts/dominator-pattern.jsonld +103 -0
  44. ae/seed_concepts/emergent-paradigm-adoption.jsonld +107 -0
  45. ae/seed_concepts/epistemic-integrity.jsonld +102 -0
  46. ae/seed_concepts/human-universe.jsonld +154 -0
  47. ae/seed_concepts/kinship.jsonld +122 -0
  48. ae/seed_concepts/override-safety.jsonld +97 -0
  49. ae/seed_concepts/partnership.jsonld +136 -0
  50. ae/seed_concepts/persona-activation-as-ceremony.jsonld +130 -0
  51. ae/seed_concepts/process-as-medium.jsonld +102 -0
  52. ae/seed_concepts/process-selection.jsonld +71 -0
  53. ae/seed_concepts/tangent-as-signal.jsonld +85 -0
  54. ae/serve.py +192 -0
  55. ae/session_server.py +305 -0
  56. ae/sync.py +134 -0
  57. ae/teams/__init__.py +0 -0
  58. ae/teams/problem_solving.md +34 -0
  59. ae/teams/problem_solving_complete.md +735 -0
  60. ae/view.py +1357 -0
  61. ae/work.py +815 -0
ae/core.py ADDED
@@ -0,0 +1,1303 @@
1
+ """Core logic for the ae CLI."""
2
+
3
+ import hashlib
4
+ import importlib.resources
5
+ import json
6
+ import re
7
+ import shutil
8
+ from datetime import date
9
+ from pathlib import Path
10
+
11
+ DEFAULT_HOME = Path.home() / ".activating-expertise"
12
+
13
+ AE_DIR_NAME = ".ae"
14
+
15
+
16
+ def _read_text_safe(filepath: Path) -> str:
17
+ """Read a text file, trying UTF-8 first then falling back to cp1252.
18
+
19
+ Windows may create files in cp1252 encoding. This handles both.
20
+ """
21
+ try:
22
+ return filepath.read_text(encoding="utf-8")
23
+ except UnicodeDecodeError:
24
+ return filepath.read_text(encoding="cp1252")
25
+
26
+
27
+ def find_project_root(start: Path | None = None) -> Path | None:
28
+ """Walk up from start (default cwd) looking for a .ae/ directory.
29
+
30
+ Returns the directory containing .ae/, or None if not found.
31
+ """
32
+ current = (start or Path.cwd()).resolve()
33
+ while True:
34
+ if (current / AE_DIR_NAME).is_dir():
35
+ return current
36
+ parent = current.parent
37
+ if parent == current:
38
+ return None
39
+ current = parent
40
+
41
+
42
+ class AEProject:
43
+ """Represents a single project found via a .ae/ directory.
44
+
45
+ Provides the same interface as AEHome for project-level paths,
46
+ so existing functions that take (home, project) can also work
47
+ with an AEProject via the resolve_project() helper.
48
+ """
49
+
50
+ def __init__(self, root: Path):
51
+ self.root = root
52
+
53
+ @property
54
+ def ae_dir(self) -> Path:
55
+ return self.root / AE_DIR_NAME
56
+
57
+ @property
58
+ def sessions_path(self) -> Path:
59
+ return self.ae_dir / "sessions"
60
+
61
+ @property
62
+ def team_path(self) -> Path:
63
+ return self.ae_dir / "team.md"
64
+
65
+ @property
66
+ def memory_path(self) -> Path:
67
+ return self.ae_dir / "memory.md"
68
+
69
+ @property
70
+ def viewer_path(self) -> Path:
71
+ return self.ae_dir / "viewer"
72
+
73
+ @property
74
+ def concepts_path(self) -> Path:
75
+ return self.ae_dir / "concepts"
76
+
77
+ @property
78
+ def name(self) -> str:
79
+ return self.root.name
80
+
81
+
82
+ class AEHome:
83
+ """Represents the ~/.activating-expertise/ directory."""
84
+
85
+ def __init__(self, path: Path | None = None):
86
+ self.path = path or DEFAULT_HOME
87
+
88
+ @property
89
+ def projects_dir(self) -> Path:
90
+ return self.path / "projects"
91
+
92
+ @property
93
+ def config_dir(self) -> Path:
94
+ return self.path / "config"
95
+
96
+ def project_path(self, name: str) -> Path:
97
+ return self.projects_dir / name
98
+
99
+ def sessions_path(self, project: str) -> Path:
100
+ return self.project_path(project) / "sessions"
101
+
102
+
103
+ def _node_id(name: str, email: str) -> str:
104
+ """Generate a node ID: name + 8 hex chars of SHA-256 of email."""
105
+ email_hash = hashlib.sha256(email.encode()).hexdigest()[:8]
106
+ return f"{name}-{email_hash}"
107
+
108
+
109
+ _PLACEHOLDER_NAMES = {"yourname", "your-name", "your_name", "name", "username", "user"}
110
+
111
+
112
+ def configure_node(home: AEHome, name: str, email: str) -> dict:
113
+ """Configure this node's identity. Returns the config dict.
114
+
115
+ Rejects placeholder names like 'yourname' that users copy from examples.
116
+ """
117
+ if name.lower() in _PLACEHOLDER_NAMES:
118
+ raise ValueError(
119
+ f"'{name}' looks like a placeholder from the documentation. "
120
+ f"Use your actual name: ae set-node stuart stuart@example.com"
121
+ )
122
+ config = {
123
+ "name": name,
124
+ "email": email,
125
+ "node_id": _node_id(name, email),
126
+ }
127
+ _write_json(home.config_dir / "node.json", config)
128
+ return config
129
+
130
+
131
+ def get_node_config(home: AEHome) -> dict | None:
132
+ """Read node config, or None if not configured."""
133
+ config = _read_json(home.config_dir / "node.json")
134
+ return config or None
135
+
136
+
137
+ def session_uri(node_id: str, project: str, session_filename: str) -> str:
138
+ """Build a session URI."""
139
+ return f"ae://{node_id}/{project}/{session_filename}"
140
+
141
+
142
+ def _read_json(path: Path, default=None):
143
+ """Read a JSON file, returning default if it doesn't exist."""
144
+ if not path.exists():
145
+ return default if default is not None else {}
146
+ return json.loads(path.read_text())
147
+
148
+
149
+ def _write_json(path: Path, data):
150
+ """Write data to a JSON file."""
151
+ path.write_text(json.dumps(data, indent=2) + "\n")
152
+
153
+
154
+ def add_peer(home: AEHome, name: str, address: str) -> dict:
155
+ """Add a peer node."""
156
+ peers_file = home.config_dir / "peers.json"
157
+ peers = _read_json(peers_file)
158
+ peers[name] = {"address": address}
159
+ _write_json(peers_file, peers)
160
+ return peers[name]
161
+
162
+
163
+ def remove_peer(home: AEHome, name: str):
164
+ """Remove a peer node."""
165
+ peers_file = home.config_dir / "peers.json"
166
+ peers = _read_json(peers_file)
167
+ peers.pop(name, None)
168
+ _write_json(peers_file, peers)
169
+
170
+
171
+ def get_peers(home: AEHome) -> dict:
172
+ """Get all configured peers."""
173
+ return _read_json(home.config_dir / "peers.json")
174
+
175
+
176
+ def share_project(home: AEHome, project: str, peer: str):
177
+ """Share a project with a peer. Both project and peer must exist."""
178
+ if not home.sessions_path(project).exists():
179
+ raise FileNotFoundError(f"Project '{project}' not found.")
180
+ peers = get_peers(home)
181
+ if peer not in peers:
182
+ raise ValueError(f"Peer '{peer}' not found. Run 'ae add-peer {peer} <address>' first.")
183
+
184
+ sharing_file = home.config_dir / "sharing.json"
185
+ sharing = _read_json(sharing_file)
186
+ if project not in sharing:
187
+ sharing[project] = []
188
+ if peer not in sharing[project]:
189
+ sharing[project].append(peer)
190
+ _write_json(sharing_file, sharing)
191
+
192
+
193
+ def unshare_project(home: AEHome, project: str, peer: str):
194
+ """Stop sharing a project with a peer."""
195
+ sharing_file = home.config_dir / "sharing.json"
196
+ sharing = _read_json(sharing_file)
197
+ if project in sharing and peer in sharing[project]:
198
+ sharing[project].remove(peer)
199
+ _write_json(sharing_file, sharing)
200
+
201
+
202
+ def get_sharing(home: AEHome) -> dict:
203
+ """Get sharing configuration."""
204
+ return _read_json(home.config_dir / "sharing.json")
205
+
206
+
207
+ def _copy_seed_concepts(concepts_path: Path):
208
+ """Copy bundled seed concept files into a project's concepts directory.
209
+
210
+ Only copies .jsonld files. Never overwrites existing files.
211
+ Safe to call on directories that already have concepts.
212
+ """
213
+ try:
214
+ seed_pkg = importlib.resources.files("ae.seed_concepts")
215
+ except (ModuleNotFoundError, TypeError):
216
+ return # Package not installed or seed_concepts not found
217
+
218
+ for item in seed_pkg.iterdir():
219
+ if item.name.endswith(".jsonld"):
220
+ dest = concepts_path / item.name
221
+ if not dest.exists():
222
+ dest.write_text(item.read_text(encoding="utf-8"), encoding="utf-8")
223
+
224
+
225
+ def _write_starter_team(path: Path, name: str):
226
+ """Write starter team.md if it doesn't exist."""
227
+ if not path.exists():
228
+ path.write_text(
229
+ f"# {name} — Team Document\n"
230
+ f"**Last updated**: Session 0 ({date.today().isoformat()})\n"
231
+ "\n"
232
+ "## Problem\n"
233
+ "\n"
234
+ "[What is this team assembled to work on?]\n"
235
+ "\n"
236
+ "## Current State\n"
237
+ "\n"
238
+ "Project just initialized. No sessions yet.\n"
239
+ "\n"
240
+ "## Team Roster\n"
241
+ "\n"
242
+ "### Structural Team (default)\n"
243
+ "- **Peter Drucker** — management, project framing\n"
244
+ "- **Riane Eisler** — partnership dynamics, power awareness\n"
245
+ "- **Keith Sawyer** — group creativity, synergy\n"
246
+ "- **Denis Diderot** — knowledge stewardship, session protocol\n"
247
+ "- **John Dewey** — ethical and developmental practice\n"
248
+ "\n"
249
+ "*Add domain experts as the problem becomes clearer.*\n"
250
+ )
251
+
252
+
253
+ def _write_starter_memory(path: Path, name: str):
254
+ """Write starter memory.md if it doesn't exist."""
255
+ if not path.exists():
256
+ path.write_text(
257
+ f"# {name} — Project Memory\n"
258
+ f"**Last updated**: Session 0 ({date.today().isoformat()})\n"
259
+ "\n"
260
+ "## Project Identity\n"
261
+ "\n"
262
+ "[What is this project? Describe in one to three sentences.]\n"
263
+ "\n"
264
+ "## Living Backlog\n"
265
+ "\n"
266
+ "- [ ] Define the problem this project addresses\n"
267
+ "- [ ] Assemble domain experts for the team\n"
268
+ "- [ ] Run first working session\n"
269
+ )
270
+
271
+
272
+ def _add_to_gitignore(directory: Path):
273
+ """Add .ae/ to .gitignore if a git repo exists. Idempotent."""
274
+ gitignore = directory / ".gitignore"
275
+ entry = ".ae/"
276
+ if gitignore.exists():
277
+ content = gitignore.read_text()
278
+ # Check if already listed (exact line match)
279
+ for line in content.splitlines():
280
+ if line.strip() == entry or line.strip() == ".ae":
281
+ return
282
+ # Append entry
283
+ if not content.endswith("\n") and content:
284
+ content += "\n"
285
+ content += entry + "\n"
286
+ gitignore.write_text(content)
287
+ elif (directory / ".git").exists():
288
+ # Git repo exists but no .gitignore — create one
289
+ gitignore.write_text(entry + "\n")
290
+
291
+
292
+ def _list_starter_sets() -> list[str]:
293
+ """List available starter session sets bundled with the package."""
294
+ import importlib.resources
295
+ community = importlib.resources.files("ae").parent.parent / "community" / "starter-sessions"
296
+ if community.is_dir():
297
+ return sorted(d.name for d in community.iterdir() if d.is_dir())
298
+ return []
299
+
300
+
301
+ def _load_starter_sessions(project: "AEProject", starter_name: str) -> int:
302
+ """Copy starter session files into a project. Returns count of files copied."""
303
+ import importlib.resources
304
+ import shutil
305
+ starter_dir = importlib.resources.files("ae").parent.parent / "community" / "starter-sessions" / starter_name
306
+ if not starter_dir.is_dir():
307
+ raise FileNotFoundError(f"Starter set '{starter_name}' not found. Available: {', '.join(_list_starter_sets())}")
308
+
309
+ copied = 0
310
+
311
+ # Copy team.md and memory.md if starter has them (overwrite the generic ones)
312
+ for name in ["team.md", "memory.md"]:
313
+ src = starter_dir / name
314
+ if src.exists():
315
+ dst = project.ae_dir / name
316
+ shutil.copy2(str(src), str(dst))
317
+ copied += 1
318
+
319
+ # Copy session files
320
+ sessions_src = starter_dir / "sessions"
321
+ if sessions_src.is_dir():
322
+ for f in sorted(sessions_src.iterdir()):
323
+ if f.is_file():
324
+ dst = project.sessions_path / f.name
325
+ if not dst.exists():
326
+ shutil.copy2(str(f), str(dst))
327
+ copied += 1
328
+
329
+ return copied
330
+
331
+
332
+ def init_project_local(directory: Path | None = None, starter: str | None = None) -> AEProject:
333
+ """Initialize a .ae/ project in directory (default: cwd).
334
+
335
+ Creates:
336
+ - .ae/sessions/ — session summaries directory
337
+ - .ae/team.md — starter team document (or from starter set)
338
+ - .ae/memory.md — starter project memory (or from starter set)
339
+ - .ae/config.json — project-level config
340
+
341
+ If starter is given, loads sessions from that starter set.
342
+ Adds .ae/ to .gitignore if a git repo is detected.
343
+ Safe to call on existing projects (never overwrites).
344
+ """
345
+ directory = (directory or Path.cwd()).resolve()
346
+ project = AEProject(directory)
347
+
348
+ project.sessions_path.mkdir(parents=True, exist_ok=True)
349
+ project.viewer_path.mkdir(parents=True, exist_ok=True)
350
+ project.concepts_path.mkdir(parents=True, exist_ok=True)
351
+
352
+ # Copy seed concepts if concepts directory is empty
353
+ _copy_seed_concepts(project.concepts_path)
354
+
355
+ name = directory.name
356
+ _write_starter_team(project.team_path, name)
357
+ _write_starter_memory(project.memory_path, name)
358
+
359
+ # Write config.json if it doesn't exist
360
+ config_path = project.ae_dir / "config.json"
361
+ if not config_path.exists():
362
+ _write_json(config_path, {"name": name})
363
+
364
+ # Load starter sessions if requested
365
+ if starter:
366
+ _load_starter_sessions(project, starter)
367
+
368
+ _add_to_gitignore(directory)
369
+
370
+ return project
371
+
372
+
373
+ def init_project(home: AEHome, name: str) -> Path:
374
+ """Initialize a new project in ~/.activating-expertise/. Safe to call on existing projects.
375
+
376
+ Creates:
377
+ - {project}/sessions/ — session summaries directory
378
+ - {project}/team.md — starter team document
379
+ - {project}/memory.md — starter project memory
380
+ - config/ — ae configuration directory
381
+
382
+ Existing files are never overwritten.
383
+ """
384
+ project_path = home.project_path(name)
385
+ home.sessions_path(name).mkdir(parents=True, exist_ok=True)
386
+ home.config_dir.mkdir(parents=True, exist_ok=True)
387
+ concepts_path = project_path / "concepts"
388
+ concepts_path.mkdir(parents=True, exist_ok=True)
389
+
390
+ # Copy seed concepts if concepts directory is empty
391
+ _copy_seed_concepts(concepts_path)
392
+
393
+ _write_starter_team(project_path / "team.md", name)
394
+ _write_starter_memory(project_path / "memory.md", name)
395
+
396
+ return project_path
397
+
398
+
399
+ def setup_mcp(root: Path, targets: list[str] | None = None) -> dict[str, Path]:
400
+ """Configure MCP servers for AI tools (Claude Code, Goose).
401
+
402
+ Detects the Python executable that has ae installed and writes
403
+ configuration files for the requested targets.
404
+
405
+ Args:
406
+ root: Project root directory (where .mcp.json goes)
407
+ targets: List of targets to configure. Default: auto-detect.
408
+ Options: 'claude', 'goose'
409
+
410
+ Returns:
411
+ Dict of target name -> config file path written.
412
+ """
413
+ import sys as _sys
414
+
415
+ python_path = _sys.executable
416
+ written = {}
417
+
418
+ if targets is None:
419
+ targets = ["claude"]
420
+ # Auto-detect Goose
421
+ goose_config = Path.home() / ".config" / "goose" / "config.yaml"
422
+ if goose_config.parent.exists():
423
+ targets.append("goose")
424
+
425
+ # Detect sessions directory for this project
426
+ sessions_dir = None
427
+ ae_dir = root / AE_DIR_NAME / "sessions"
428
+ dev_dir = root / "development" / "sessions"
429
+ if ae_dir.is_dir():
430
+ sessions_dir = str(ae_dir)
431
+ elif dev_dir.is_dir():
432
+ sessions_dir = str(dev_dir)
433
+
434
+ # Detect project model
435
+ project_model = None
436
+ for candidate in [root / "development" / "project_model.md",
437
+ root / AE_DIR_NAME / "project_model.md"]:
438
+ if candidate.exists():
439
+ project_model = str(candidate)
440
+ break
441
+
442
+ # Build env vars for MCP servers
443
+ session_env = {}
444
+ if sessions_dir:
445
+ session_env["AE_SESSIONS_DIR"] = sessions_dir
446
+ if project_model:
447
+ session_env["AE_PROJECT_MODEL"] = project_model
448
+
449
+ if "claude" in targets:
450
+ # Write .mcp.json
451
+ ae_sessions_config = {
452
+ "command": python_path,
453
+ "args": ["-m", "ae.session_server"],
454
+ }
455
+ if session_env:
456
+ ae_sessions_config["env"] = session_env
457
+
458
+ mcp_json = {
459
+ "mcpServers": {
460
+ "ae-concepts": {
461
+ "command": python_path,
462
+ "args": ["-m", "ae.concept_server"],
463
+ },
464
+ "ae-sessions": ae_sessions_config,
465
+ }
466
+ }
467
+ mcp_path = root / ".mcp.json"
468
+ _write_json(mcp_path, mcp_json)
469
+ written["claude-mcp"] = mcp_path
470
+
471
+ # Write .claude/settings.json
472
+ claude_dir = root / ".claude"
473
+ claude_dir.mkdir(exist_ok=True)
474
+ settings_path = claude_dir / "settings.json"
475
+ # Merge with existing settings if present
476
+ existing = _read_json(settings_path)
477
+ permissions = existing.get("permissions", {})
478
+ allow = set(permissions.get("allow", []))
479
+ allow.update([
480
+ "mcp__ae-concepts__concept_retrieve",
481
+ "mcp__ae-concepts__concept_list",
482
+ "mcp__ae-sessions__session_search",
483
+ "mcp__ae-sessions__session_decisions",
484
+ "mcp__ae-sessions__session_open_items",
485
+ "mcp__ae-sessions__feature_inventory",
486
+ "mcp__ae-sessions__session_related",
487
+ ])
488
+ existing["permissions"] = {"allow": sorted(allow)}
489
+ _write_json(settings_path, existing)
490
+ written["claude-settings"] = settings_path
491
+
492
+ if "goose" in targets:
493
+ goose_config_path = Path.home() / ".config" / "goose" / "config.yaml"
494
+ goose_config_path.parent.mkdir(parents=True, exist_ok=True)
495
+
496
+ ae_concepts_block = f""" ae-concepts:
497
+ enabled: true
498
+ type: stdio
499
+ name: ae-concepts
500
+ description: "AE concept store — team concepts with complement, boundary, perspectives"
501
+ cmd: {python_path}
502
+ args: ["-m", "ae.concept_server"]
503
+ envs: {{}}
504
+ env_keys: []
505
+ timeout: 300
506
+ bundled: null
507
+ available_tools: []"""
508
+
509
+ # Build envs dict for Goose YAML
510
+ goose_envs = "{" + ", ".join(f'"{k}": "{v}"' for k, v in session_env.items()) + "}" if session_env else "{}"
511
+
512
+ ae_sessions_block = f""" ae-sessions:
513
+ enabled: true
514
+ type: stdio
515
+ name: ae-sessions
516
+ description: "AE session history — search, decisions, open items, feature inventory"
517
+ cmd: {python_path}
518
+ args: ["-m", "ae.session_server"]
519
+ envs: {goose_envs}
520
+ env_keys: []
521
+ timeout: 300
522
+ bundled: null
523
+ available_tools: []"""
524
+
525
+ if goose_config_path.exists():
526
+ content = goose_config_path.read_text()
527
+ else:
528
+ content = ""
529
+
530
+ changed = False
531
+ if "ae-concepts" not in content:
532
+ if "extensions:" not in content:
533
+ content += "\nextensions:\n"
534
+ # Insert after extensions: line
535
+ content = content.replace("extensions:\n",
536
+ "extensions:\n" + ae_concepts_block + "\n")
537
+ changed = True
538
+ if "ae-sessions" not in content:
539
+ content = content.replace("extensions:\n",
540
+ "extensions:\n" + ae_sessions_block + "\n")
541
+ changed = True
542
+
543
+ if changed:
544
+ goose_config_path.write_text(content)
545
+ written["goose"] = goose_config_path
546
+
547
+ return written
548
+
549
+
550
+ def _slugify(text: str) -> str:
551
+ """Convert text to a filename-safe slug."""
552
+ text = text.lower().strip()
553
+ text = re.sub(r"[^\w\s-]", "", text)
554
+ text = re.sub(r"[\s_]+", "-", text)
555
+ text = re.sub(r"-+", "-", text)
556
+ return text.strip("-")
557
+
558
+
559
+ def _extract_date(content: str) -> str | None:
560
+ """Extract a date from a session summary — tries multiple formats."""
561
+ # Try: # Session ... — YYYY-MM-DD or # Session ... — ... YYYY-MM-DD
562
+ match = re.search(r"^#\s+Session.+?[-—]\s*.*?(\d{4}-\d{2}-\d{2})", content, re.MULTILINE)
563
+ if match:
564
+ return match.group(1)
565
+ # Try: **Date**: YYYY-MM-DD (skip TBD)
566
+ match = re.search(r"\*\*Date\*\*:\s*(\d{4}-\d{2}-\d{2})", content)
567
+ if match:
568
+ return match.group(1)
569
+ # Try: # Session N — Title (YYYY-MM-DD) or (YYYY-MM-DD, continued)
570
+ match = re.search(r"^#\s+Session\s+\d+\w?\s*[-—].+?\((\d{4}-\d{2}-\d{2})", content, re.MULTILINE)
571
+ if match:
572
+ return match.group(1)
573
+ return None
574
+
575
+
576
+ def _extract_title(content: str) -> str:
577
+ """Extract a title from a session summary's markdown header."""
578
+ match = re.search(r"^#\s+Session:\s*(.+?)(?:\s*—\s*\d{4})", content, re.MULTILINE)
579
+ if match:
580
+ return _slugify(match.group(1))
581
+ # Fallback: first heading
582
+ match = re.search(r"^#\s+(.+)", content, re.MULTILINE)
583
+ if match:
584
+ return _slugify(match.group(1))
585
+ return "untitled"
586
+
587
+
588
+ def _extract_jsonld(content: str) -> str | None:
589
+ """Extract a JSON-LD block from session content.
590
+
591
+ Looks for a fenced code block marked ```json-ld in the markdown.
592
+ Returns the JSON string if found, None otherwise.
593
+ """
594
+ pattern = r"```json-ld\s*\n(.*?)```"
595
+ match = re.search(pattern, content, re.DOTALL)
596
+ if match:
597
+ jsonld_str = match.group(1).strip()
598
+ # Validate it's parseable JSON
599
+ try:
600
+ json.loads(jsonld_str)
601
+ return jsonld_str
602
+ except json.JSONDecodeError:
603
+ return None
604
+ return None
605
+
606
+
607
+ def _save_session_to_dir(
608
+ sessions_dir: Path,
609
+ content: str,
610
+ title: str | None = None,
611
+ session_date: str | None = None,
612
+ project_name: str = "project",
613
+ ) -> Path:
614
+ """Save a session to a sessions directory. Core implementation."""
615
+ if not sessions_dir.exists():
616
+ raise FileNotFoundError(f"Project '{project_name}' not found. Run 'ae init' first.")
617
+
618
+ if title is None:
619
+ title = _extract_title(content)
620
+
621
+ if session_date is None:
622
+ session_date = _extract_date(content) or date.today().isoformat()
623
+
624
+ filename = f"{session_date}_{title}.md"
625
+ path = sessions_dir / filename
626
+ path.write_text(content, encoding="utf-8")
627
+
628
+ # Extract and save JSON-LD companion file if present in markdown,
629
+ # otherwise auto-generate from markdown extraction
630
+ jsonld_text = _extract_jsonld(content)
631
+ jsonld_path = path.with_suffix(".jsonld")
632
+ if jsonld_text:
633
+ jsonld_path.write_text(jsonld_text + "\n", encoding="utf-8")
634
+ else:
635
+ sequence = len(list(sessions_dir.glob("*.md")))
636
+ jsonld_data = migrate_session(path, sequence)
637
+ jsonld_path.write_text(
638
+ json.dumps(jsonld_data, indent=2) + "\n", encoding="utf-8")
639
+
640
+ return path
641
+
642
+
643
+ def save_session(
644
+ home: AEHome,
645
+ project: str,
646
+ content: str,
647
+ title: str | None = None,
648
+ session_date: str | None = None,
649
+ ) -> Path:
650
+ """Save a session summary to a project's sessions directory.
651
+
652
+ If the session contains a ```json-ld fenced block, extracts it
653
+ and saves as a companion .jsonld file alongside the markdown.
654
+ """
655
+ return _save_session_to_dir(
656
+ home.sessions_path(project), content, title, session_date,
657
+ project_name=project,
658
+ )
659
+
660
+
661
+ def save_session_for(
662
+ home_or_project,
663
+ project: str | None,
664
+ content: str,
665
+ title: str | None = None,
666
+ session_date: str | None = None,
667
+ ) -> Path:
668
+ """Save a session — works with both AEHome+project and AEProject."""
669
+ sessions_dir = _resolve_sessions_dir(home_or_project, project)
670
+ name = _resolve_project_name(home_or_project, project)
671
+ return _save_session_to_dir(sessions_dir, content, title, session_date, project_name=name)
672
+
673
+
674
+ def _extract_section(content: str, heading: str) -> str:
675
+ """Extract the text under a ## heading, up to the next ## heading."""
676
+ pattern = rf"^##\s+{re.escape(heading)}\s*\n(.*?)(?=^##\s|\Z)"
677
+ match = re.search(pattern, content, re.MULTILINE | re.DOTALL)
678
+ if match:
679
+ return match.group(1).strip()
680
+ return ""
681
+
682
+
683
+ def _extract_bullet_items(text: str) -> list[str]:
684
+ """Extract bullet-point items from text."""
685
+ items = []
686
+ for line in text.splitlines():
687
+ line = line.strip()
688
+ if line.startswith("- "):
689
+ items.append(line[2:].strip())
690
+ return items
691
+
692
+
693
+ def _resolve_sessions_dir(home_or_project, project: str | None = None) -> Path:
694
+ """Resolve the sessions directory from either AEHome+project or AEProject."""
695
+ if isinstance(home_or_project, AEProject):
696
+ return home_or_project.sessions_path
697
+ return home_or_project.sessions_path(project)
698
+
699
+
700
+ def _resolve_project_name(home_or_project, project: str | None = None) -> str:
701
+ """Resolve the project name from either AEHome+project or AEProject."""
702
+ if isinstance(home_or_project, AEProject):
703
+ return home_or_project.name
704
+ return project
705
+
706
+
707
+ def _get_session_files(home: AEHome, project: str) -> list[Path]:
708
+ """Get session files for a project, sorted by modification time."""
709
+ sessions_dir = home.sessions_path(project)
710
+ if not sessions_dir.exists():
711
+ return []
712
+ files = sorted(sessions_dir.glob("*.md"), key=lambda p: p.name)
713
+ return files
714
+
715
+
716
+ def _get_session_files_for(home_or_project, project: str | None = None) -> list[Path]:
717
+ """Get session files, works with both AEHome+project and AEProject."""
718
+ sessions_dir = _resolve_sessions_dir(home_or_project, project)
719
+ if not sessions_dir.exists():
720
+ return []
721
+ files = sorted(sessions_dir.glob("*.md"), key=lambda p: p.name)
722
+ return files
723
+
724
+
725
+ def get_status(home: AEHome) -> list[dict]:
726
+ """Get status across all projects, sorted by most recent activity."""
727
+ if not home.projects_dir.exists():
728
+ return []
729
+
730
+ projects = []
731
+ for project_dir in sorted(home.projects_dir.iterdir()):
732
+ if not project_dir.is_dir():
733
+ continue
734
+
735
+ name = project_dir.name
736
+ session_files = _get_session_files(home, name)
737
+
738
+ entry = {
739
+ "name": name,
740
+ "session_count": len(session_files),
741
+ "last_session": None,
742
+ "latest_summary": "",
743
+ "open_questions": [],
744
+ "last_modified": 0.0,
745
+ }
746
+
747
+ if session_files:
748
+ latest = session_files[-1]
749
+ entry["last_session"] = latest.name
750
+ entry["last_modified"] = latest.stat().st_mtime
751
+
752
+ content = latest.read_text()
753
+ worked_on = _extract_section(content, "What We Worked On")
754
+ entry["latest_summary"] = worked_on
755
+
756
+ questions_text = _extract_section(content, "Open Questions")
757
+ entry["open_questions"] = _extract_bullet_items(questions_text)
758
+
759
+ projects.append(entry)
760
+
761
+ # Sort by most recent activity (projects with sessions first, then by mtime)
762
+ projects.sort(key=lambda p: p["last_modified"], reverse=True)
763
+ return projects
764
+
765
+
766
+ def get_local_project_status(project: AEProject) -> dict | None:
767
+ """Get status for a .ae/ project."""
768
+ session_files = _get_session_files_for(project)
769
+
770
+ entry = {
771
+ "name": project.name,
772
+ "source": "local",
773
+ "root": str(project.root),
774
+ "session_count": len(session_files),
775
+ "last_session": None,
776
+ "latest_summary": "",
777
+ "open_questions": [],
778
+ "last_modified": 0.0,
779
+ }
780
+
781
+ if session_files:
782
+ latest = session_files[-1]
783
+ entry["last_session"] = latest.name
784
+ entry["last_modified"] = latest.stat().st_mtime
785
+
786
+ content = latest.read_text()
787
+ worked_on = _extract_section(content, "What We Worked On")
788
+ entry["latest_summary"] = worked_on
789
+
790
+ questions_text = _extract_section(content, "Open Questions")
791
+ entry["open_questions"] = _extract_bullet_items(questions_text)
792
+
793
+ return entry
794
+
795
+
796
+ def get_peer_status(home: AEHome) -> list[dict]:
797
+ """Get status of peer-synced projects."""
798
+ peers_dir = home.path / "peers"
799
+ if not peers_dir.exists():
800
+ return []
801
+
802
+ projects = []
803
+ for peer_dir in sorted(peers_dir.iterdir()):
804
+ if not peer_dir.is_dir():
805
+ continue
806
+ peer_name = peer_dir.name
807
+
808
+ for project_dir in sorted(peer_dir.iterdir()):
809
+ if not project_dir.is_dir():
810
+ continue
811
+ sessions_dir = project_dir / "sessions"
812
+ if not sessions_dir.exists():
813
+ continue
814
+
815
+ session_files = sorted(sessions_dir.glob("*.md"), key=lambda p: p.name)
816
+
817
+ entry = {
818
+ "name": project_dir.name,
819
+ "peer": peer_name,
820
+ "session_count": len(session_files),
821
+ "last_session": None,
822
+ "latest_summary": "",
823
+ "open_questions": [],
824
+ "last_modified": 0.0,
825
+ }
826
+
827
+ if session_files:
828
+ latest = session_files[-1]
829
+ entry["last_session"] = latest.name
830
+ entry["last_modified"] = latest.stat().st_mtime
831
+ content = latest.read_text()
832
+ entry["latest_summary"] = _extract_section(content, "What We Worked On")
833
+ questions_text = _extract_section(content, "Open Questions")
834
+ entry["open_questions"] = _extract_bullet_items(questions_text)
835
+
836
+ projects.append(entry)
837
+
838
+ projects.sort(key=lambda p: p["last_modified"], reverse=True)
839
+ return projects
840
+
841
+
842
+ def list_sessions(home: AEHome, project: str) -> list[Path]:
843
+ """List session files for a project, sorted by modification time."""
844
+ sessions_dir = home.sessions_path(project)
845
+ if not sessions_dir.exists():
846
+ raise FileNotFoundError(f"Project '{project}' not found.")
847
+ return _get_session_files(home, project)
848
+
849
+
850
+ def list_sessions_for(home_or_project, project: str | None = None) -> list[Path]:
851
+ """List session files — works with both AEHome+project and AEProject."""
852
+ sessions_dir = _resolve_sessions_dir(home_or_project, project)
853
+ if not sessions_dir.exists():
854
+ name = _resolve_project_name(home_or_project, project)
855
+ raise FileNotFoundError(f"Project '{name}' not found.")
856
+ return _get_session_files_for(home_or_project, project)
857
+
858
+
859
+ def migrate_session(filepath: Path, sequence: int, node_id: str | None = None) -> dict:
860
+ """Generate JSON-LD from a session markdown file.
861
+
862
+ Extracts structured data from the markdown using regex, then produces
863
+ a JSON-LD document conforming to the ae: ontology. For sessions that
864
+ already have a JSON-LD block, returns the existing data instead.
865
+
866
+ Returns the JSON-LD dict (ready for json.dumps).
867
+ """
868
+ content = _read_text_safe(filepath)
869
+
870
+ # If the session already has a JSON-LD block, parse and return it
871
+ existing_jsonld = _extract_jsonld(content)
872
+ if existing_jsonld:
873
+ return json.loads(existing_jsonld)
874
+
875
+ # Extract metadata — try multiple title formats
876
+ # Format 1: # Session: Title — YYYY-MM-DD
877
+ title_match = re.search(r"^#\s+Session:\s*(.+?)(?:\s*[-\u2014]\s*\d{4})", content, re.MULTILINE)
878
+ if not title_match:
879
+ # Format 2: # Session N: Title — YYYY-MM-DD (date at end after em-dash)
880
+ title_match = re.search(r"^#\s+Session\s+\d+\w?:\s*(.+?)(?:\s*[-—]\s*\d{4}-\d{2}-\d{2})", content, re.MULTILINE)
881
+ if not title_match:
882
+ # Format 3: # Session N — Title (YYYY-MM-DD)
883
+ title_match = re.search(r"^#\s+Session\s+\d+\w?\s*[-—]\s*(.+?)(?:\s*\(\d{4})", content, re.MULTILINE)
884
+ if not title_match:
885
+ # Format 4: # Session N: Full Title (may include em-dash as separator)
886
+ title_match = re.search(r"^#\s+Session\s+\d+\w?:\s*(.+?)\s*$", content, re.MULTILINE)
887
+ if not title_match:
888
+ # Format 5: # Session N — Title (no date)
889
+ title_match = re.search(r"^#\s+Session\s+\d+\w?\s*[-—]\s*(.+?)\s*$", content, re.MULTILINE)
890
+ if not title_match:
891
+ # Format 6: # Session N Prompt: Title (session plan files)
892
+ title_match = re.search(r"^#\s+Session\s+\d+\w?\s+Prompt:\s*(.+?)\s*$", content, re.MULTILINE)
893
+ if not title_match:
894
+ # Format 7: # Title (no "Session" prefix, e.g., "# Sprint 6 — ...")
895
+ title_match = re.search(r"^#\s+(.+?)(?:\s*[-—]\s*\d{4}|\s*$)", content, re.MULTILINE)
896
+ title = title_match.group(1).strip() if title_match else filepath.stem.split("_", 1)[-1].replace("-", " ").title()
897
+
898
+ date_str = _extract_date(content) or filepath.name[:10]
899
+
900
+ status_match = re.search(r"\*\*Status\*\*:\s*(\S+)", content)
901
+ status = status_match.group(1).lower() if status_match else "working"
902
+
903
+ type_match = re.search(r"\*\*Type\*\*:\s*(\S+)", content)
904
+ session_type = type_match.group(1).lower() if type_match else "record"
905
+
906
+ # Build URI
907
+ project = filepath.parent.parent.name
908
+ slug = _slugify(title)
909
+ session_id = f"ae:session/{date_str}-{slug}"
910
+
911
+ # Extract sections (both ## and ### headings)
912
+ sections = {}
913
+ pattern = r"^##\s+(.+?)\s*\n(.*?)(?=^##\s|\Z)"
914
+ for match in re.finditer(pattern, content, re.MULTILINE | re.DOTALL):
915
+ sections[match.group(1).strip()] = match.group(2).strip()
916
+ # Also extract ### subsections (e.g., under "## For Your AI Team")
917
+ subsection_pattern = r"^###\s+(.+?)\s*\n(.*?)(?=^###\s|^##\s|\Z)"
918
+ for match in re.finditer(subsection_pattern, content, re.MULTILINE | re.DOTALL):
919
+ sections[match.group(1).strip()] = match.group(2).strip()
920
+
921
+ # Extract topics from Problem Structure or domain references
922
+ topics = []
923
+ prob_structure = sections.get("Problem Structure", "")
924
+ domain_match = re.search(r"\*\*Domain\*\*:\s*(.+)", prob_structure)
925
+ if domain_match:
926
+ for domain in domain_match.group(1).split(","):
927
+ d = domain.strip().lower()
928
+ if d:
929
+ topics.append({"@id": f"ae:topic/{_slugify(d)}", "dcterms:title": d})
930
+
931
+ # Extract structural patterns
932
+ patterns = []
933
+ pattern_match = re.search(r"\*\*Structural pattern\*\*:\s*(.+)", prob_structure)
934
+ if pattern_match:
935
+ for p in pattern_match.group(1).split(","):
936
+ p = p.strip().lower()
937
+ if p:
938
+ patterns.append({"@id": f"ae:pattern/{_slugify(p)}", "dcterms:title": p})
939
+
940
+ # Extract insights from Key Insights section
941
+ insights = []
942
+ insights_text = sections.get("Key Insights", "")
943
+ for line in insights_text.splitlines():
944
+ line = line.strip()
945
+ if line.startswith("- "):
946
+ content_text = line[2:].strip()
947
+ if content_text:
948
+ insights.append({
949
+ "@type": "ae:Insight",
950
+ "ae:content": content_text,
951
+ })
952
+
953
+ # Extract team composition — check multiple section names and formats
954
+ participants = []
955
+ seen_names = set()
956
+ team_text = ""
957
+ for key in ("Team Composition", "Team Members Most Relevant", "Team"):
958
+ team_text = sections.get(key, "")
959
+ if team_text:
960
+ break
961
+
962
+ # Known persona last names for matching in comma-separated lists
963
+ _KNOWN_PERSONAS = {
964
+ "Simon", "Schon", "Belbin", "Drucker", "McEnerney", "Shneiderman",
965
+ "Sawyer", "Eisler", "Dewey", "Diderot", "Deming", "Chomsky", "Veblen",
966
+ "Hinton", "Claude", "Engelbart", "Berners-Lee", "Cunningham", "Swartz",
967
+ "Kahle", "Tufte", "Fowler", "Raymond", "Willison", "Hogberg",
968
+ "Pinker", "Nussbaum", "Sen", "Mozi", "Corbin",
969
+ }
970
+
971
+ def _add_participant(name):
972
+ if name not in seen_names:
973
+ seen_names.add(name)
974
+ participants.append({
975
+ "@id": f"ae:persona/{_slugify(name)}",
976
+ "schema:name": name,
977
+ })
978
+
979
+ if team_text:
980
+ # Pattern 1: bold names **Name Name** (original pattern)
981
+ for m in re.finditer(r"\*\*([A-Z][a-zA-Z'-]+(?: [A-Z][a-zA-Z'-]+)+)\*\*", team_text):
982
+ _add_participant(m.group(1))
983
+
984
+ # Pattern 2: comma-separated known last names (with optional role in parens)
985
+ # e.g., "Drucker, Eisler, Sawyer" or "Cunningham (simplicity), Tufte (visualization)"
986
+ for m in re.finditer(r"\b(" + "|".join(re.escape(n) for n in _KNOWN_PERSONAS) + r")\b", team_text):
987
+ _add_participant(m.group(1))
988
+
989
+ # Pattern 3: bold list items — "- **Name**: role"
990
+ for m in re.finditer(r"-\s+\*\*([A-Z][a-zA-Z'-]+)\*\*", team_text):
991
+ _add_participant(m.group(1))
992
+
993
+ # Extract synergy assessment
994
+ synergy = {}
995
+ synergy_text = sections.get("Synergy Assessment", "")
996
+ state_match = re.search(r"\*\*Team state\*\*:\s*(.+?)(?:\n|$)", synergy_text)
997
+ if state_match:
998
+ raw_state = state_match.group(1).strip().lower()
999
+ # Normalize to one of: synergy, productive-tension, echo-chamber
1000
+ if "synergy" in raw_state and "not" not in raw_state:
1001
+ synergy["ae:teamState"] = "synergy"
1002
+ elif "productive" in raw_state:
1003
+ synergy["ae:teamState"] = "productive-tension"
1004
+ elif "echo" in raw_state:
1005
+ synergy["ae:teamState"] = "echo-chamber"
1006
+ # Skip values like "[not assessed]" — leave teamState unset
1007
+ assessment_match = re.search(r"(?:Scale|rating|assessment).*?(\d)/5", synergy_text, re.IGNORECASE)
1008
+ if assessment_match:
1009
+ synergy["ae:humanAssessment"] = int(assessment_match.group(1))
1010
+
1011
+ # Extract synergy detail
1012
+ synergy_detail = sections.get("Synergy Detail", "")
1013
+ ratio_match = re.search(r"\*\*Build-on ratio\*\*:\s*(.+?)(?:\n|$)", synergy_detail)
1014
+ if ratio_match:
1015
+ # Try to extract a float
1016
+ float_match = re.search(r"(\d+\.?\d*)", ratio_match.group(1))
1017
+ if float_match:
1018
+ synergy["ae:buildOnRatio"] = float(float_match.group(1))
1019
+ temporal_match = re.search(r"\*\*Temporal signature\*\*:\s*(.+?)(?:\n|$)", synergy_detail)
1020
+ if temporal_match:
1021
+ synergy["ae:temporalSignature"] = temporal_match.group(1).strip()
1022
+ suppression_match = re.search(r"\*\*Suppression check.*?\*\*:\s*(.+?)(?:\n|$)", synergy_detail)
1023
+ if suppression_match:
1024
+ synergy["ae:suppressionCheckResult"] = suppression_match.group(1).strip()
1025
+
1026
+ if synergy:
1027
+ synergy["@type"] = "ae:SynergyAssessment"
1028
+
1029
+ # Extract learning (What Shifted)
1030
+ learning = {}
1031
+ shifted_text = sections.get("What Shifted", "")
1032
+ if shifted_text:
1033
+ learning = {
1034
+ "@type": "ae:LearningEvent",
1035
+ "ae:shifted": shifted_text[:500], # Truncate if very long
1036
+ }
1037
+
1038
+ # Extract connections to previous sessions
1039
+ connections_text = sections.get("Connections to Previous Sessions", "")
1040
+ if connections_text and learning:
1041
+ # Look for session references
1042
+ conn_matches = re.findall(r"[Ss]ession\s+(\d+)", connections_text)
1043
+ if conn_matches:
1044
+ learning["ae:connectedTo"] = [
1045
+ {"@id": f"ae:session/session-{num}"} for num in conn_matches
1046
+ ]
1047
+
1048
+ # Extract narrative description from main content sections
1049
+ description = ""
1050
+ narrative_text = "" # Full narrative for bold-finding extraction
1051
+ for key in ("What We Worked On", "Session Record", "What Happened",
1052
+ "Context", "Purpose"):
1053
+ desc_text = sections.get(key, "")
1054
+ if desc_text:
1055
+ description = desc_text[:1000] # First 1000 chars for description field
1056
+ narrative_text = desc_text
1057
+ break
1058
+ # Also gather text from "What Happened in Session N" sections
1059
+ for key, text in sections.items():
1060
+ if key.startswith("What Happened in Session") or key.startswith("Phase "):
1061
+ narrative_text += "\n" + text
1062
+
1063
+ # Extract bold-led findings from narrative sections
1064
+ # These appear as "**Finding title**: description" paragraphs or
1065
+ # "- **Name** finding text" bullet items in What Happened / Session Record
1066
+ narrative_findings = []
1067
+ _seen_findings = set()
1068
+ # Labels that indicate status updates, not findings
1069
+ _NOISE_LABELS = {"domain", "structural pattern", "key tensions", "status",
1070
+ "version", "updated", "paper structure", "length history",
1071
+ "backlog", "file", "files updated", "type", "project"}
1072
+ if narrative_text:
1073
+ # Pattern 1: Bold paragraph leaders — "**Title**: description"
1074
+ for m in re.finditer(r"\*\*(.+?)\*\*:\s*(.+?)(?=\n\n|\n\*\*|\n-\s+\*\*|\Z)",
1075
+ narrative_text, re.DOTALL):
1076
+ finding_label = m.group(1).strip()
1077
+ body = m.group(2).strip()
1078
+ # Skip noise: short fragments, section labels, status updates
1079
+ if len(body) < 30:
1080
+ continue
1081
+ if any(finding_label.lower().startswith(n) for n in _NOISE_LABELS):
1082
+ continue
1083
+ text = f"**{finding_label}**: {body[:300]}"
1084
+ # Deduplicate by label
1085
+ if finding_label.lower() not in _seen_findings:
1086
+ _seen_findings.add(finding_label.lower())
1087
+ narrative_findings.append(text)
1088
+
1089
+ # Pattern 2: Bullet items from Session Record (early sessions)
1090
+ for m in re.finditer(r"^-\s+(.{30,})", narrative_text, re.MULTILINE):
1091
+ text = m.group(1).strip()
1092
+ # Skip bold items (already captured above) and noise
1093
+ if text.startswith("**"):
1094
+ continue
1095
+ # Deduplicate by first 50 chars
1096
+ dedup_key = text[:50].lower()
1097
+ if dedup_key not in _seen_findings:
1098
+ _seen_findings.add(dedup_key)
1099
+ narrative_findings.append(text[:300])
1100
+
1101
+ # Extract decisions — from structured sections first, then narrative findings
1102
+ decisions = []
1103
+ for key in ("Decisions Made", "Key Decisions"):
1104
+ decisions_text = sections.get(key, "")
1105
+ if decisions_text:
1106
+ for line in decisions_text.splitlines():
1107
+ line = line.strip()
1108
+ m = re.match(r"(?:\d+\.\s+|-\s+)(.*)", line)
1109
+ if m:
1110
+ text = m.group(1).strip()
1111
+ if text:
1112
+ decisions.append({
1113
+ "@type": "ae:Decision",
1114
+ "ae:content": text,
1115
+ })
1116
+
1117
+ # If no structured decisions found, promote narrative findings as insights
1118
+ # (they're findings, not formal decisions — use ae:hasInsight)
1119
+ narrative_insights = []
1120
+ if not decisions and narrative_findings:
1121
+ for text in narrative_findings:
1122
+ narrative_insights.append({
1123
+ "@type": "ae:Insight",
1124
+ "ae:content": text,
1125
+ })
1126
+
1127
+ # Extract open items / questions
1128
+ open_items = []
1129
+ for key in ("Open Questions", "Open Questions for Next Session",
1130
+ "Open Questions / Growth Questions",
1131
+ "Disagreements and Open Questions", "Open Items"):
1132
+ open_text = sections.get(key, "")
1133
+ if open_text:
1134
+ for line in open_text.splitlines():
1135
+ line = line.strip()
1136
+ m = re.match(r"(?:\d+\.\s+|-\s+)(.*)", line)
1137
+ if m:
1138
+ text = m.group(1).strip()
1139
+ if text:
1140
+ open_items.append({
1141
+ "@type": "ae:OpenItem",
1142
+ "ae:content": text,
1143
+ })
1144
+
1145
+ # Extract deliverables — handles both bullet lists and table format
1146
+ deliverables = []
1147
+ for key in ("Deliverables", "Deliverables This Session",
1148
+ "Concrete Deliverables Produced"):
1149
+ deliv_text = sections.get(key, "")
1150
+ if deliv_text:
1151
+ for line in deliv_text.splitlines():
1152
+ line = line.strip()
1153
+ # Bullet items
1154
+ if line.startswith("- "):
1155
+ text = line[2:].strip()
1156
+ if text:
1157
+ deliverables.append({
1158
+ "@type": "ae:Deliverable",
1159
+ "ae:content": text,
1160
+ })
1161
+ # Table rows: | content | ... | (skip header separators and header rows)
1162
+ elif line.startswith("| ") and "---" not in line:
1163
+ cells = [c.strip() for c in line.split("|")[1:-1]]
1164
+ text = " — ".join(c for c in cells if c)
1165
+ if text and not text.startswith("File") and not text.startswith("Artifact"):
1166
+ deliverables.append({
1167
+ "@type": "ae:Deliverable",
1168
+ "ae:content": text,
1169
+ })
1170
+
1171
+ # Build the JSON-LD document
1172
+ jsonld = {
1173
+ "@context": {
1174
+ "ae": "https://activating-expertise.org/ns/",
1175
+ "local": "https://activating-expertise.org/local/",
1176
+ "dcterms": "http://purl.org/dc/terms/",
1177
+ "schema": "http://schema.org/",
1178
+ "xsd": "http://www.w3.org/2001/XMLSchema#",
1179
+ },
1180
+ "@id": session_id,
1181
+ "@type": "ae:Session",
1182
+ "ae:sequence": sequence,
1183
+ "ae:sessionType": session_type,
1184
+ "dcterms:date": {"@value": date_str, "@type": "xsd:date"},
1185
+ "dcterms:title": title,
1186
+ "ae:project": {"@id": f"ae:project/{_slugify(project)}"},
1187
+ "ae:status": status,
1188
+ }
1189
+
1190
+ if node_id:
1191
+ jsonld["ae:practitioner"] = {"@id": f"ae:practitioner/{node_id}"}
1192
+ if description:
1193
+ jsonld["dcterms:description"] = description
1194
+ if topics:
1195
+ jsonld["ae:hasTopic"] = topics
1196
+ if patterns:
1197
+ jsonld["ae:hasPattern"] = patterns
1198
+ if participants:
1199
+ jsonld["ae:hasParticipant"] = participants
1200
+ all_insights = insights + narrative_insights
1201
+ if all_insights:
1202
+ jsonld["ae:hasInsight"] = all_insights
1203
+ if synergy:
1204
+ jsonld["ae:synergyAssessment"] = synergy
1205
+ if learning:
1206
+ jsonld["ae:learning"] = learning
1207
+ if decisions:
1208
+ jsonld["ae:hasDecision"] = decisions
1209
+ if open_items:
1210
+ jsonld["ae:hasOpenItem"] = open_items
1211
+ if deliverables:
1212
+ jsonld["ae:hasDeliverable"] = deliverables
1213
+
1214
+ return jsonld
1215
+
1216
+
1217
+ def migrate_project(home: AEHome, project: str) -> list[Path]:
1218
+ """Migrate all sessions in a project to JSON-LD.
1219
+
1220
+ For each .md session file without a companion .jsonld, generates
1221
+ JSON-LD from the markdown and saves it. Returns list of created
1222
+ .jsonld file paths.
1223
+ """
1224
+ session_files = _get_session_files(home, project)
1225
+ node_config = get_node_config(home)
1226
+ node_id = node_config["node_id"] if node_config else None
1227
+
1228
+ created = []
1229
+ for i, filepath in enumerate(session_files, 1):
1230
+ jsonld_path = filepath.with_suffix(".jsonld")
1231
+ if jsonld_path.exists():
1232
+ continue # Already migrated
1233
+
1234
+ jsonld = migrate_session(filepath, i, node_id)
1235
+ jsonld_path.write_text(json.dumps(jsonld, indent=2) + "\n", encoding="utf-8")
1236
+ created.append(jsonld_path)
1237
+
1238
+ return created
1239
+
1240
+
1241
+ def migrate_project_for(home_or_project, project: str | None, home: AEHome | None = None) -> list[Path]:
1242
+ """Migrate sessions — works with both AEHome+project and AEProject."""
1243
+ session_files = _get_session_files_for(home_or_project, project)
1244
+ # Try to get node config from AEHome if available
1245
+ node_id = None
1246
+ ae_home = home if home else (home_or_project if isinstance(home_or_project, AEHome) else None)
1247
+ if ae_home:
1248
+ node_config = get_node_config(ae_home)
1249
+ node_id = node_config["node_id"] if node_config else None
1250
+
1251
+ created = []
1252
+ for i, filepath in enumerate(session_files, 1):
1253
+ jsonld_path = filepath.with_suffix(".jsonld")
1254
+ if jsonld_path.exists():
1255
+ continue
1256
+ jsonld = migrate_session(filepath, i, node_id)
1257
+ jsonld_path.write_text(json.dumps(jsonld, indent=2) + "\n", encoding="utf-8")
1258
+ created.append(jsonld_path)
1259
+
1260
+ return created
1261
+
1262
+
1263
+ def read_session(home: AEHome, project: str, query: str) -> str:
1264
+ """Read a session by exact filename or partial match."""
1265
+ sessions_dir = home.sessions_path(project)
1266
+ if not sessions_dir.exists():
1267
+ raise FileNotFoundError(f"Project '{project}' not found.")
1268
+
1269
+ # Try exact filename first
1270
+ exact = sessions_dir / query
1271
+ if exact.exists():
1272
+ return _read_text_safe(exact)
1273
+
1274
+ # Try partial match
1275
+ matches = [f for f in _get_session_files(home, project) if query in f.name]
1276
+ if len(matches) == 1:
1277
+ return _read_text_safe(matches[0])
1278
+ if len(matches) > 1:
1279
+ names = ", ".join(m.name for m in matches)
1280
+ raise FileNotFoundError(f"Multiple sessions match '{query}': {names}")
1281
+
1282
+ raise FileNotFoundError(f"No session matching '{query}' in project '{project}'.")
1283
+
1284
+
1285
+ def read_session_for(home_or_project, project: str | None, query: str) -> str:
1286
+ """Read a session — works with both AEHome+project and AEProject."""
1287
+ sessions_dir = _resolve_sessions_dir(home_or_project, project)
1288
+ name = _resolve_project_name(home_or_project, project)
1289
+ if not sessions_dir.exists():
1290
+ raise FileNotFoundError(f"Project '{name}' not found.")
1291
+
1292
+ exact = sessions_dir / query
1293
+ if exact.exists():
1294
+ return _read_text_safe(exact)
1295
+
1296
+ matches = [f for f in _get_session_files_for(home_or_project, project) if query in f.name]
1297
+ if len(matches) == 1:
1298
+ return _read_text_safe(matches[0])
1299
+ if len(matches) > 1:
1300
+ names = ", ".join(m.name for m in matches)
1301
+ raise FileNotFoundError(f"Multiple sessions match '{query}': {names}")
1302
+
1303
+ raise FileNotFoundError(f"No session matching '{query}' in project '{name}'.")