okstra 0.233.0 → 0.234.1

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 (36) hide show
  1. package/dist/cli-registry.mjs +6 -0
  2. package/dist/cli-registry.mjs.map +1 -1
  3. package/dist/commands/trace/progression.mjs +6 -5
  4. package/dist/commands/trace/progression.mjs.map +1 -1
  5. package/dist/commands/trace/trace.mjs +2 -2
  6. package/dist/commands/trace/trace.mjs.map +1 -1
  7. package/docs/architecture/storage-model.md +19 -0
  8. package/docs/cli.md +41 -1
  9. package/docs/project-structure-overview.md +3 -2
  10. package/package.json +1 -1
  11. package/runtime/BUILD.json +2 -2
  12. package/runtime/python/okstra_ctl/business_flow/cli.py +60 -6
  13. package/runtime/python/okstra_ctl/business_flow/contracts.py +12 -1
  14. package/runtime/python/okstra_ctl/business_flow/engine.py +17 -5
  15. package/runtime/python/okstra_ctl/business_flow/history.py +60 -0
  16. package/runtime/python/okstra_ctl/business_flow/source.py +35 -2
  17. package/runtime/python/okstra_ctl/business_flow/store.py +51 -14
  18. package/runtime/python/okstra_ctl/business_flow/sweep.py +124 -0
  19. package/runtime/python/okstra_ctl/doyouknow/cli.py +9 -9
  20. package/runtime/python/okstra_ctl/doyouknow/config.py +2 -2
  21. package/runtime/python/okstra_ctl/doyouknow/group_view.py +152 -0
  22. package/runtime/python/okstra_ctl/doyouknow/source_contracts.py +4 -3
  23. package/runtime/python/okstra_ctl/doyouknow/source_store.py +2 -2
  24. package/runtime/python/okstra_ctl/doyouknow/sources/business.py +116 -0
  25. package/runtime/python/okstra_ctl/doyouknow/sync.py +34 -9
  26. package/runtime/python/okstra_ctl/fixed_text.py +10 -0
  27. package/runtime/python/okstra_ctl/project_group.py +175 -0
  28. package/runtime/python/okstra_ctl/project_group_cli.py +62 -0
  29. package/runtime/schemas/business-flow-v1.schema.json +3 -0
  30. package/runtime/schemas/doyouknow-config-v1.schema.json +1 -1
  31. package/runtime/skills/okstra-brief-gen/SKILL.md +52 -6
  32. package/runtime/skills/okstra-explain-flow/SKILL.md +27 -2
  33. package/runtime/skills/okstra-tdd/SKILL.md +7 -4
  34. package/runtime/templates/doyouknow/group-view.css +103 -0
  35. package/runtime/templates/doyouknow/group-view.js +317 -0
  36. package/runtime/templates/doyouknow/group-view.template.html +72 -0
@@ -140,7 +140,9 @@ def _investigate(
140
140
  request.project_root, result["facts"], execution["projects"], producer
141
141
  )
142
142
  if result["resolutions"]:
143
- resolve_conflicts(request.project_root, result["resolutions"], producer)
143
+ resolve_conflicts(
144
+ request.project_root, result["resolutions"], producer, request.project_group
145
+ )
144
146
  write_artifact(path, execution, "execution")
145
147
  _reinvestigate_conflicts(request, execution, path, executor)
146
148
  if request.mode != "contribute":
@@ -349,7 +351,7 @@ def _validate_reconciliation(
349
351
  raise BusinessFlowError(
350
352
  "conflict resolutions require a dedicated reinvestigation"
351
353
  )
352
- knowledge = read_shared_knowledge(request.project_root)
354
+ knowledge = read_shared_knowledge(request.project_root, request.project_group)
353
355
  validate_resolutions(knowledge, result["resolutions"])
354
356
  claims = {row["id"]: row for row in knowledge["claims"]}
355
357
  states = {row["projectId"]: row["digest"] for row in projects}
@@ -440,7 +442,11 @@ def _investigation_context(
440
442
  if baseline
441
443
  else "",
442
444
  "knowledge": query_knowledge(
443
- request.project_root, request.question, request.source_root
445
+ request.project_root,
446
+ request.question,
447
+ request.source_root,
448
+ request.project_group,
449
+ members_only=request.mode != "reconcile",
444
450
  ),
445
451
  }
446
452
 
@@ -453,7 +459,10 @@ def _reinvestigate_conflicts(
453
459
  conflict_keys = {
454
460
  row["key"]
455
461
  for row in query_knowledge(
456
- request.project_root, request.question, request.source_root
462
+ request.project_root,
463
+ request.question,
464
+ request.source_root,
465
+ request.project_group,
457
466
  )["claims"]
458
467
  if row["status"] == "conflict"
459
468
  }
@@ -477,7 +486,10 @@ def _reinvestigate_conflicts(
477
486
  unresolved = [
478
487
  row["key"]
479
488
  for row in query_knowledge(
480
- request.project_root, request.question, request.source_root
489
+ request.project_root,
490
+ request.question,
491
+ request.source_root,
492
+ request.project_group,
481
493
  )["claims"]
482
494
  if row["status"] == "conflict"
483
495
  ]
@@ -0,0 +1,60 @@
1
+ """Which stored explanation currently answers each question of a project."""
2
+ from __future__ import annotations
3
+
4
+ import hashlib
5
+ from collections.abc import Callable
6
+ from pathlib import Path
7
+ from typing import cast
8
+
9
+ from ..json_boundary import JsonBoundaryError
10
+ from .contracts import BusinessFlowError, FlowExecution, business_root, read_artifact
11
+
12
+ # `pre` describes the state before a change, and `contribute` / `reconcile` are not explanations.
13
+ DESCRIBING_MODES = ("current", "post")
14
+ ACCEPTED_STATUSES = ("complete", "partial")
15
+
16
+
17
+ def flow_id(question: str) -> str:
18
+ """One question is one flow; a later explanation of it replaces the earlier one."""
19
+ normalized = " ".join(question.casefold().split())
20
+ return "flow-" + hashlib.sha256(normalized.encode("utf-8")).hexdigest()[:16]
21
+
22
+
23
+ def _read(path: Path) -> FlowExecution:
24
+ return cast(FlowExecution, read_artifact(path, "execution"))
25
+
26
+
27
+ def latest_explanations(
28
+ project_root: Path,
29
+ *,
30
+ read: Callable[[Path], FlowExecution] = _read,
31
+ unreadable: tuple[type[Exception], ...] = (),
32
+ ) -> tuple[dict[str, tuple[Path, FlowExecution]], int]:
33
+ """The newest accepted explanation per `flow_id`, and how many records could not be read.
34
+
35
+ `read` must validate a record against the execution schema; `unreadable` names the
36
+ extra errors that reader raises for a record it refuses.
37
+ """
38
+ executions = business_root(project_root) / "executions"
39
+ latest: dict[str, tuple[Path, FlowExecution]] = {}
40
+ refused = 0
41
+ for directory in sorted(executions.iterdir()) if executions.is_dir() else []:
42
+ if not directory.is_dir():
43
+ continue
44
+ path = directory / "execution.json"
45
+ try:
46
+ execution = read(path)
47
+ if execution["status"] not in ACCEPTED_STATUSES or execution["request"]["mode"] not in DESCRIBING_MODES:
48
+ continue
49
+ if "result" not in execution:
50
+ raise BusinessFlowError("accepted explanation has no result")
51
+ key = flow_id(execution["request"]["question"])
52
+ if key not in latest or execution["createdAt"] > latest[key][1]["createdAt"]:
53
+ latest[key] = (path, execution)
54
+ except (BusinessFlowError, JsonBoundaryError, OSError, RecursionError, *unreadable):
55
+ refused += 1
56
+ return latest, refused
57
+
58
+
59
+ def recorded_source_states(execution: FlowExecution) -> dict[str, dict[str, str]]:
60
+ return {row["projectId"]: {"head": row["head"], "digest": row["digest"]} for row in execution["projects"]}
@@ -10,6 +10,7 @@ from typing import Any
10
10
  from ..json_boundary import load_owned_object
11
11
  from ..manager_store import discover_projects
12
12
  from ..paths import okstra_home
13
+ from ..project_group import ProjectGroupError, read_group
13
14
  from .contracts import (
14
15
  BusinessEvidence,
15
16
  BusinessFlowError,
@@ -21,6 +22,22 @@ from .contracts import (
21
22
 
22
23
 
23
24
  def candidate_projects(request: FlowRequest) -> list[ProjectCandidate]:
25
+ """Projects an investigation inspects: the group's members when a group is set."""
26
+ # A reconciliation settles a conflict whose other side may sit outside the group.
27
+ grouped = bool(request.project_group) and request.mode != "reconcile"
28
+ return _projects(request, with_run_history=not grouped)
29
+
30
+
31
+ def knowledge_projects(request: FlowRequest) -> list[ProjectCandidate]:
32
+ """Projects whose knowledge stores are read: run history plus the group's members.
33
+
34
+ A group only adds stores here. Conflicts and their resolutions are judged over this
35
+ set, so a group never hides one side of a stored resolution.
36
+ """
37
+ return _projects(request, with_run_history=True)
38
+
39
+
40
+ def _projects(request: FlowRequest, *, with_run_history: bool) -> list[ProjectCandidate]:
24
41
  root = request.project_root.resolve()
25
42
  config = root / ".okstra" / "project.json"
26
43
  project_id = root.name
@@ -30,8 +47,10 @@ def candidate_projects(request: FlowRequest) -> list[ProjectCandidate]:
30
47
  or project_id
31
48
  )
32
49
  candidates = {
33
- row["projectId"]: {"projectId": row["projectId"], "root": row["projectRoot"]}
34
- for row in discover_projects(okstra_home())
50
+ name: {"projectId": name, "root": registered_root}
51
+ for name, registered_root in registered_roots(
52
+ request.project_group, with_run_history=with_run_history
53
+ ).items()
35
54
  }
36
55
  candidates[project_id] = {
37
56
  "projectId": project_id,
@@ -40,6 +59,20 @@ def candidate_projects(request: FlowRequest) -> list[ProjectCandidate]:
40
59
  return sorted(candidates.values(), key=lambda row: row["projectId"])
41
60
 
42
61
 
62
+ def registered_roots(
63
+ project_group: str, *, with_run_history: bool, home: Path | None = None
64
+ ) -> dict[str, str]:
65
+ """The root of each projectId from run history and the group's registration; the group's row wins."""
66
+ home = home or okstra_home()
67
+ registered = discover_projects(home) if with_run_history else []
68
+ if project_group:
69
+ try:
70
+ registered += read_group(home, project_group)["projects"]
71
+ except ProjectGroupError as exc:
72
+ raise BusinessFlowError(str(exc)) from exc
73
+ return {row["projectId"]: row["projectRoot"] for row in registered}
74
+
75
+
43
76
  def git_output(root: Path, *args: str) -> bytes:
44
77
  result = subprocess.run(
45
78
  ["git", "-C", str(root), *args], capture_output=True, check=False
@@ -25,7 +25,7 @@ from .contracts import (
25
25
  read_artifact,
26
26
  write_artifact,
27
27
  )
28
- from .source import candidate_projects, capture_project
28
+ from .source import candidate_projects, capture_project, knowledge_projects
29
29
 
30
30
  CONFIRMED_LEVELS = frozenset({"static", "execution-verified"})
31
31
  PACKET_CLAIM_LIMIT = 40
@@ -160,12 +160,17 @@ def _mark_conflicts(claims: list[StoredClaim]) -> None:
160
160
 
161
161
 
162
162
  def resolve_conflicts(
163
- project_root: Path, resolutions: list[BusinessResolution], producer: ClaimProducer
163
+ project_root: Path,
164
+ resolutions: list[BusinessResolution],
165
+ producer: ClaimProducer,
166
+ project_group: str,
164
167
  ) -> None:
165
168
  path = artifact_path(project_root, "knowledge.json")
166
169
  with dir_flock(path.parent, ".knowledge.lock"):
167
170
  data = read_knowledge(project_root)
168
- validate_resolutions(read_shared_knowledge(project_root), resolutions)
171
+ validate_resolutions(
172
+ read_shared_knowledge(project_root, project_group), resolutions
173
+ )
169
174
  by_id = {row["id"]: row for row in data["claims"]}
170
175
  for resolution in resolutions:
171
176
  ids = resolution["claimIds"]
@@ -238,9 +243,11 @@ def _resolution_rows(
238
243
  return rows
239
244
 
240
245
 
241
- def read_shared_knowledge(project_root: Path) -> dict[str, Any]:
246
+ def read_shared_knowledge(project_root: Path, project_group: str) -> dict[str, Any]:
242
247
  claims, resolutions, errors = [], [], []
243
- candidates = candidate_projects(FlowRequest(project_root, "business"))
248
+ candidates = knowledge_projects(
249
+ FlowRequest(project_root, "business", project_group=project_group)
250
+ )
244
251
  for candidate in candidates:
245
252
  root = Path(candidate["root"])
246
253
  try:
@@ -289,17 +296,40 @@ def _apply_shared_resolutions(
289
296
 
290
297
 
291
298
  def query_knowledge(
292
- project_root: Path, query: str, source_root: Path | None = None
299
+ project_root: Path,
300
+ query: str,
301
+ source_root: Path | None = None,
302
+ project_group: str = "",
303
+ *,
304
+ members_only: bool = True,
293
305
  ) -> dict[str, Any]:
306
+ """Select stored claims for a question.
307
+
308
+ With a group, `members_only` keeps the claims its members own. Their status still
309
+ comes from the whole knowledge scope, so a conflict with an outside claim shows.
310
+ """
294
311
  sources = {
295
312
  row["projectId"]: row["root"]
296
- for row in candidate_projects(
297
- FlowRequest(project_root, query or "business", source_root=source_root)
313
+ for row in knowledge_projects(
314
+ FlowRequest(
315
+ project_root,
316
+ query or "business",
317
+ source_root=source_root,
318
+ project_group=project_group,
319
+ )
298
320
  )
299
321
  }
300
322
  tokens = set(re.findall(r"[\w-]{2,}", query.casefold()))
301
- shared = read_shared_knowledge(project_root)
323
+ shared = read_shared_knowledge(project_root, project_group)
302
324
  claims, errors = shared["claims"], shared["errors"]
325
+ if project_group and members_only:
326
+ member_roots = {
327
+ row["root"]
328
+ for row in candidate_projects(
329
+ FlowRequest(project_root, "business", project_group=project_group)
330
+ )
331
+ }
332
+ claims = [row for row in claims if row["ownerRoot"] in member_roots]
303
333
  selected = [
304
334
  row
305
335
  for row in claims
@@ -328,15 +358,22 @@ def query_knowledge(
328
358
  selected, keys = expanded, {row["key"] for row in expanded}
329
359
  states: dict[str, dict[str, Any]] = {}
330
360
  for row in selected:
331
- row["freshness"] = _freshness(row, states, sources)
361
+ row["freshness"] = source_freshness(row["sourceStates"], states, sources)
332
362
  row["selectionReason"] = "business terms or a connected claim key"
333
363
  return {"claims": selected, "errors": errors, "query": query}
334
364
 
335
365
 
336
- def _freshness(
337
- claim: dict[str, Any], cache: dict[str, dict[str, Any]], sources: dict[str, str]
366
+ def source_freshness(
367
+ source_states: dict[str, Any], cache: dict[str, dict[str, Any]], sources: dict[str, str]
338
368
  ) -> str:
339
- for project_id, expected in claim["sourceStates"].items():
369
+ """Compare recorded source versions with the projects as they are now.
370
+
371
+ `cache` keeps one capture per root across calls; `sources` maps a projectId to its root.
372
+ """
373
+ for project_id, expected in source_states.items():
374
+ # A version that was not captured when it was recorded cannot be compared.
375
+ if not expected.get("digest"):
376
+ return "unverified"
340
377
  root = sources.get(project_id)
341
378
  if root is None:
342
379
  return "unverified"
@@ -347,7 +384,7 @@ def _freshness(
347
384
  return "unverified"
348
385
  if current["digest"] != expected["digest"]:
349
386
  return "historical"
350
- return "current" if claim["sourceStates"] else "unverified"
387
+ return "current" if source_states else "unverified"
351
388
 
352
389
 
353
390
  def knowledge_packet(
@@ -0,0 +1,124 @@
1
+ """Explain a list of questions across a project group, skipping the ones still current."""
2
+ from __future__ import annotations
3
+
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ from ..paths import okstra_home
8
+ from ..json_boundary import JsonBoundaryError
9
+ from ..project_group import ProjectGroupError, show_group
10
+ from .contracts import BusinessFlowError, FlowRequest, flow_report_language
11
+ from .engine import explain_flow
12
+ from .history import flow_id, latest_explanations, recorded_source_states
13
+ from .invocation import FlowExecutor
14
+ from .source import registered_roots
15
+ from .store import source_freshness
16
+
17
+
18
+ def _questions(rows: object) -> list[tuple[str, str]]:
19
+ if not isinstance(rows, list):
20
+ raise BusinessFlowError("sweep questions must be a JSON array")
21
+ questions = []
22
+ for row in rows:
23
+ if (
24
+ not isinstance(row, dict)
25
+ or not isinstance(row.get("projectId"), str)
26
+ or not isinstance(row.get("question"), str)
27
+ or not row["question"].strip()
28
+ ):
29
+ raise BusinessFlowError('each sweep question needs a "projectId" and a non-empty "question"')
30
+ questions.append((row["projectId"], row["question"]))
31
+ return questions
32
+
33
+
34
+ def plan_sweep(project_group: str, questions: object, *, stale: bool, home: Path | None = None) -> dict[str, Any]:
35
+ """Decide what a sweep would explain. No model is called.
36
+
37
+ A listed question is a target unless its newest explanation is still `current`; its
38
+ reason is `new` (never explained), `historical` (a recorded project changed) or
39
+ `unverified` (a recorded version cannot be compared, so it is a target on every sweep).
40
+ With `stale`, every stored explanation that is `historical` is a target too.
41
+ A member whose root is missing or unreadable is reported in `problems`, and each of
42
+ its listed questions is reported there as not planned.
43
+ """
44
+ home = home or okstra_home()
45
+ try:
46
+ group = show_group(home, project_group)
47
+ except ProjectGroupError as exc:
48
+ raise BusinessFlowError(str(exc)) from exc
49
+ members = {member["projectId"]: member for member in group["projects"]}
50
+ roots = registered_roots(group["projectGroup"], with_run_history=True, home=home)
51
+ captured: dict[str, dict[str, Any]] = {}
52
+ targets: dict[tuple[str, str], dict[str, str]] = {}
53
+ skipped: dict[tuple[str, str], dict[str, str]] = {}
54
+ problems = []
55
+ stored: dict[str, dict[str, tuple[str, str]]] = {}
56
+ for project_id, member in sorted(members.items()):
57
+ if not member["rootExists"]:
58
+ problems.append(f"{project_id}: registered root is missing: {member['projectRoot']}")
59
+ continue
60
+ try:
61
+ latest, refused = latest_explanations(Path(member["projectRoot"]))
62
+ except (OSError, BusinessFlowError) as exc:
63
+ problems.append(f"{project_id}: stored explanations could not be listed: {exc}")
64
+ continue
65
+ if refused:
66
+ problems.append(f"{project_id}: {refused} stored explanation record(s) could not be read")
67
+ stored[project_id] = {
68
+ key: (execution["request"]["question"], source_freshness(recorded_source_states(execution), captured, roots))
69
+ for key, (_, execution) in latest.items()
70
+ }
71
+ for project_id, question in _questions(questions):
72
+ if project_id not in members:
73
+ raise BusinessFlowError(f"project {project_id} is not in project-group {group['projectGroup']}")
74
+ if project_id not in stored:
75
+ problems.append(f"{project_id}: not planned because the project could not be read: {question}")
76
+ continue
77
+ key = flow_id(question)
78
+ freshness = stored[project_id].get(key, (question, "new"))[1]
79
+ row = {"projectId": project_id, "question": question, "reason": freshness}
80
+ (skipped if freshness == "current" else targets)[(project_id, key)] = row
81
+ if stale:
82
+ for project_id, flows in stored.items():
83
+ for key, (question, freshness) in flows.items():
84
+ if freshness == "historical":
85
+ targets.setdefault((project_id, key), {"projectId": project_id, "question": question, "reason": freshness})
86
+ return {
87
+ "projectGroup": group["projectGroup"],
88
+ "targets": [targets[key] for key in sorted(targets)],
89
+ "skipped": [skipped[key] for key in sorted(skipped)],
90
+ "problems": problems,
91
+ "roots": {name: member["projectRoot"] for name, member in members.items()},
92
+ }
93
+
94
+
95
+ def run_sweep(
96
+ plan: dict[str, Any], *, report_language: str, host_runtime: str, executor: FlowExecutor | None = None
97
+ ) -> dict[str, Any]:
98
+ """Explain every planned target in order. One failure does not stop the rest.
99
+
100
+ A target that fails before its execution record exists is reported with an empty
101
+ `executionId`.
102
+ """
103
+ results = []
104
+ for target in plan["targets"]:
105
+ root = Path(plan["roots"][target["projectId"]])
106
+ try:
107
+ execution = explain_flow(
108
+ FlowRequest(
109
+ root,
110
+ target["question"],
111
+ report_language=flow_report_language(root, report_language),
112
+ host_runtime=host_runtime,
113
+ project_group=plan["projectGroup"],
114
+ ),
115
+ executor,
116
+ )
117
+ except (OSError, BusinessFlowError, JsonBoundaryError) as exc:
118
+ # explain_flow validates the request and writes its first record before it starts catching.
119
+ results.append({**target, "executionId": "", "status": "failed", "error": str(exc)})
120
+ continue
121
+ results.append({
122
+ **target, "executionId": execution["id"], "status": execution["status"], "error": execution.get("error", ""),
123
+ })
124
+ return {**plan, "results": results, "status": "failed" if any(row["status"] == "failed" for row in results) else "complete"}
@@ -1,11 +1,10 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import argparse
4
- import json
5
4
  from pathlib import Path
6
5
  from typing import get_args
7
6
 
8
- from ..fixed_text import line, value_lines
7
+ from ..fixed_text import emit_result
9
8
  from ..json_boundary import JsonBoundaryError, external_user_json_source, load_external_json
10
9
  from .config import load_config, save_config
11
10
  from .contracts import DoyouknowError, MAX_INPUT_BYTES, read_envelope
@@ -16,7 +15,7 @@ from .source_contracts import SourceKind
16
15
  def _parser() -> argparse.ArgumentParser:
17
16
  parser = argparse.ArgumentParser(prog="okstra doyouknow", description="Store linked work records without starting a run.")
18
17
  commands = parser.add_subparsers(dest="command", required=True)
19
- for name in ("capture", "ingest", "show", "sync", "list", "config", "register", "retry"):
18
+ for name in ("capture", "ingest", "show", "sync", "list", "config", "register", "retry", "view"):
20
19
  command = commands.add_parser(name)
21
20
  if name == "show":
22
21
  command.add_argument("record_id")
@@ -26,6 +25,8 @@ def _parser() -> argparse.ArgumentParser:
26
25
  command.add_argument("--file", type=Path, help="replace source settings from JSON")
27
26
  elif name == "register":
28
27
  command.add_argument("--ticket", required=True, help="pending registration UUID")
28
+ elif name == "view":
29
+ command.add_argument("--project-group", required=True, help="registered project group to render")
29
30
  if name in ("sync", "list", "retry"):
30
31
  command.add_argument("--source", choices=get_args(SourceKind))
31
32
  if name == "retry":
@@ -41,12 +42,7 @@ def _parser() -> argparse.ArgumentParser:
41
42
 
42
43
 
43
44
  def _emit(payload: dict, *, text: bool) -> None:
44
- if not text:
45
- print(json.dumps(payload, ensure_ascii=False, allow_nan=False))
46
- return
47
- rows = ["Okstra doyouknow\n", line("OK", payload["ok"])]
48
- rows.extend(value_lines("Result", payload))
49
- print("".join(rows), end="")
45
+ emit_result(payload, text=text, title="Okstra doyouknow")
50
46
 
51
47
 
52
48
  def main(argv: list[str] | None = None) -> int:
@@ -62,6 +58,10 @@ def main(argv: list[str] | None = None) -> int:
62
58
  result = sync_sources(source=args.source)
63
59
  elif args.command == "register":
64
60
  result = register_ticket(args.ticket)
61
+ elif args.command == "view":
62
+ from .group_view import write_group_view
63
+
64
+ result = write_group_view(args.project_group)
65
65
  elif args.command == "retry":
66
66
  result = retry_pending(source=args.source, limit=args.limit)
67
67
  elif args.command == "list":
@@ -6,7 +6,7 @@ from typing import TypedDict
6
6
  from ..json_boundary import load_owned_object, serialize_owned_object, write_owned_object_atomic
7
7
  from ..run_context import dir_flock
8
8
  from .contracts import MAX_INPUT_BYTES, asset_object
9
- from .source_contracts import validate_source_value
9
+ from .source_contracts import SOURCE_KINDS, validate_source_value
10
10
  from .paths import database_path, ensure_storage_root, require_database_location as _require_database_location
11
11
 
12
12
 
@@ -26,7 +26,7 @@ def load_config(*, home: Path | None = None) -> SourceConfig:
26
26
  _require_database_location(path)
27
27
  if not path.exists():
28
28
  return {"schemaVersion": 1, "autoRegister": True, "scope": None,
29
- "sources": ["memory", "manager", "task"], "includeProjects": [],
29
+ "sources": list(SOURCE_KINDS), "includeProjects": [],
30
30
  "excludeProjects": [], "includeGroups": [], "excludeGroups": []}
31
31
  payload = load_owned_object(path, artifact="doyouknow config", max_bytes=MAX_INPUT_BYTES)
32
32
  validate_source_value(payload)
@@ -0,0 +1,152 @@
1
+ """One project group's projects, business flows and linked records as a single HTML page.
2
+
3
+ The page is a derived view: it reads the group registry and the sources `okstra doyouknow
4
+ sync` already stored, and compares each flow's recorded source versions with the
5
+ projects as they are now. It collects nothing itself.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import html
10
+ import json
11
+ import re
12
+ from datetime import datetime, timezone
13
+ from pathlib import Path
14
+
15
+ from ..business_flow.contracts import BusinessFlowError
16
+ from ..business_flow.source import registered_roots
17
+ from ..business_flow.store import source_freshness
18
+ from ..paths import find_asset_root
19
+ from ..project_group import ProjectGroupError, group_path, show_group
20
+ from .contracts import DoyouknowError
21
+ from .paths import database_path, ensure_storage_root
22
+ from .source_store import list_sources
23
+
24
+ ASSET_DIRECTORY = ("templates", "doyouknow")
25
+ PAGE, STYLE, SCRIPT = "group-view.template.html", "group-view.css", "group-view.js"
26
+ _MARKER = re.compile(r"\{\{(TITLE|STYLE|SCRIPT|DATA)\}\}")
27
+
28
+
29
+ def _stored_sources(home: Path) -> tuple[list[dict], list[str]]:
30
+ """Every active stored source, and a note for each one too large to be listed."""
31
+ sources, problems, after = [], [], None
32
+ while True:
33
+ listing = list_sources(home=home, limit=1000, after=after)
34
+ for source in listing["sources"]:
35
+ if source["lifecycle"] != "active":
36
+ continue
37
+ if source.get("detailsOmitted"):
38
+ # An omitted item carries no project, so it cannot be matched to a group.
39
+ problems.append(f"A stored source is too large to list, so its project group is unknown: {source['sourceId']}")
40
+ else:
41
+ sources.append(source)
42
+ if not listing["truncated"]:
43
+ return sources, problems
44
+ after = listing["nextCursor"]
45
+
46
+
47
+ def _title(source: dict) -> str:
48
+ return next(node["title"] for node in source["nodes"] if node["id"] == source["nodeId"])
49
+
50
+
51
+ def _project_ids(source: dict) -> set[str]:
52
+ return {node["key"] for node in source["nodes"] if node["kind"] == "project"}
53
+
54
+
55
+ def _flow(source: dict, freshness: str) -> dict:
56
+ metadata = source["metadata"]
57
+ return {
58
+ "id": source["sourceId"], "title": _title(source), "owner": metadata["projectId"], "locator": source["locator"],
59
+ "updatedAt": source["sourceUpdatedAt"] or "", "freshness": freshness, "projects": sorted(_project_ids(source)),
60
+ **{key: metadata[key] for key in (
61
+ "question", "summary", "status", "terms", "steps", "facts", "gaps", "questions",
62
+ "inspectedProjects", "relationships",
63
+ )},
64
+ }
65
+
66
+
67
+ def _edges(source: dict) -> list[dict]:
68
+ projects = {node["id"]: node["key"] for node in source["nodes"] if node["kind"] == "project"}
69
+ return [
70
+ {
71
+ "from": projects[relation["fromNodeId"]], "to": projects[relation["toNodeId"]],
72
+ "kind": relation["kind"], "reason": relation["reason"], "evidence": relation["evidence"],
73
+ "standing": relation["standing"], "flowId": source["sourceId"],
74
+ }
75
+ for relation in source["relations"]
76
+ if relation["fromNodeId"] in projects and relation["toNodeId"] in projects
77
+ ]
78
+
79
+
80
+ def group_view_data(home: Path, project_group: str, *, now: str | None = None) -> dict:
81
+ try:
82
+ group = show_group(home, project_group)
83
+ # The same projects the knowledge query compares against, so a flow gets one freshness in both.
84
+ roots = registered_roots(group["projectGroup"], with_run_history=True, home=home)
85
+ except (ProjectGroupError, BusinessFlowError) as exc:
86
+ raise DoyouknowError(str(exc)) from exc
87
+ members = {member["projectId"]: member for member in group["projects"]}
88
+ stored, problems = _stored_sources(home)
89
+ captured: dict[str, dict] = {}
90
+ flows, edges, records = [], [], []
91
+ for source in stored:
92
+ if source["ref"]["kind"] != "business":
93
+ named_members = _project_ids(source) & members.keys()
94
+ if named_members:
95
+ records.append({
96
+ "kind": source["ref"]["kind"], "title": _title(source), "projects": sorted(named_members),
97
+ "locator": source["locator"], "updatedAt": source["sourceUpdatedAt"] or "",
98
+ })
99
+ elif source["ref"]["owner_id"] in members:
100
+ flows.append(_flow(source, source_freshness(source["metadata"]["sourceStates"], captured, roots)))
101
+ edges.extend(_edges(source))
102
+ inspected = {name for flow in flows for name in flow["inspectedProjects"]}
103
+ return {
104
+ "projectGroup": group["projectGroup"],
105
+ "generatedAt": now or datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
106
+ "projects": [
107
+ {
108
+ "projectId": name, "projectRoot": member["projectRoot"], "rootExists": member["rootExists"],
109
+ "investigated": name in inspected, "flowCount": sum(flow["owner"] == name for flow in flows),
110
+ }
111
+ for name, member in sorted(members.items())
112
+ ],
113
+ "flows": sorted(flows, key=lambda flow: flow["title"].casefold()),
114
+ "edges": edges,
115
+ "records": sorted(records, key=lambda record: record["updatedAt"], reverse=True),
116
+ "problems": problems,
117
+ }
118
+
119
+
120
+ def _asset(name: str) -> str:
121
+ asset_root = find_asset_root((*ASSET_DIRECTORY, name))
122
+ if asset_root is None:
123
+ raise DoyouknowError("group view asset not found: " + "/".join((*ASSET_DIRECTORY, name)))
124
+ return asset_root.joinpath(*ASSET_DIRECTORY, name).read_text(encoding="utf-8")
125
+
126
+
127
+ def render_group_view(home: Path, project_group: str, *, now: str | None = None) -> tuple[str, dict]:
128
+ data = group_view_data(home, project_group, now=now)
129
+ replacements = {
130
+ "TITLE": html.escape("Okstra project group · " + data["projectGroup"]),
131
+ "STYLE": _asset(STYLE),
132
+ "SCRIPT": _asset(SCRIPT),
133
+ # Inside a script element only `<` can end the element or open a comment.
134
+ "DATA": json.dumps(data, ensure_ascii=False).replace("<", "\\u003c"),
135
+ }
136
+ # One pass: replacing in turn would rescan stored text that happens to contain a marker.
137
+ return _MARKER.sub(lambda match: replacements[match.group(1)], _asset(PAGE)), data
138
+
139
+
140
+ def write_group_view(project_group: str, *, home: Path | None = None, now: str | None = None) -> dict:
141
+ database = database_path(home)
142
+ okstra_home_root = database.parent.parent
143
+ html_page, data = render_group_view(okstra_home_root, project_group, now=now)
144
+ ensure_storage_root(database.parent)
145
+ path = database.parent / "view" / group_path(okstra_home_root, data["projectGroup"]).stem / "index.html"
146
+ path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
147
+ path.write_text(html_page, encoding="utf-8")
148
+ return {
149
+ "ok": True, "projectGroup": data["projectGroup"], "viewPath": str(path), "viewUrl": path.as_uri(),
150
+ "projects": len(data["projects"]), "flows": len(data["flows"]), "relationships": len(data["edges"]),
151
+ "records": len(data["records"]), "problems": data["problems"],
152
+ }