-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifact_materializer.py
More file actions
1157 lines (1104 loc) · 48.8 KB
/
Copy pathartifact_materializer.py
File metadata and controls
1157 lines (1104 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import json
import shutil
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from app.domain.task_agent import TaskAgentServiceSpec
from app.services.coursegen_logging import log_coursegen_event
from app.domain.workflow import (
ArtifactVisibility,
BundleFile,
BundleFileContent,
MaterializedBundle,
WorkflowRun,
)
from app.services.creator_asset_service import CreatorAssetService
from app.services.learner_brief_builder import (
build_task_agent_deliverable_brief,
render_learner_starter_readme,
)
from app.services.runtime_contract_surface import (
load_starter_manifest,
starter_contract_path_sets_for_manifest,
starter_materialization_paths,
)
from app.services.task_agent_contract_surface import (
learner_editable_paths_for_deliverable,
primary_submit_endpoint_for_spec,
)
from app.services.task_agent_starter_templates import (
HIDDEN_GRADER_SCRIPT_PATH,
HIDDEN_MANIFEST_PATH,
RUNTIME_HIDDEN_CHECK_SCRIPT_PATH,
RUNTIME_INSTALL_SCRIPT_PATH,
RUNTIME_RUN_SCRIPT_PATH,
RUNTIME_VERIFY_SCRIPT_PATH,
RUNTIME_VISIBLE_CHECK_SCRIPT_PATH,
build_task_agent_starter_files,
default_preview_command,
task_agent_runtime_base_image,
task_agent_runtime_bootstrap_commands,
task_agent_runtime_environment_lines,
)
# Per-deliverable artifacts that live OUTSIDE the shared starter tree.
VISIBLE_CHECK_SCRIPT_RELATIVE_PATH = "run_visible_checks.py"
HIDDEN_GRADER_SCRIPT_RELATIVE_PATH = "run_hidden_checks.py"
DELIVERABLE_MANIFEST_RELATIVE_PATH = "deliverable.json"
# Shared course-level manifest at the shared starter root. Carries the
# course-wide fields (runtime_plan, runtime_dependencies, course_structure,
# public_endpoints, dependency_contract) once, instead of duplicating them
# inside every per-deliverable manifest.
SHARED_COURSE_MANIFEST_RELATIVE_PATH = ".coursegen/course.json"
def shared_starter_workspace_path(public_dir: Path) -> Path:
"""For shared_codebase courses: the single shared starter root."""
return public_dir / "starter"
def deliverable_visible_checks_dir(public_dir: Path, deliverable_id: str) -> Path:
"""For shared_codebase courses: per-deliverable learner-facing brief + visible script."""
return public_dir / "checks" / deliverable_id
def deliverable_grader_dir(private_dir: Path, deliverable_id: str) -> Path:
"""For shared_codebase courses: per-deliverable hidden grader + manifest."""
return private_dir / "grader" / deliverable_id
def build_shared_course_manifest_payload(
spec: TaskAgentServiceSpec,
*,
dependency_contract: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Course-level fields that previously got copied into every per-deliverable
manifest. Living once at public/starter/.coursegen/course.json keeps the
shared starter authoritative for the whole course."""
payload: dict[str, Any] = {
"title": spec.title,
"summary": spec.summary,
"course_structure": spec.course_structure.model_dump(mode="json"),
"runtime_plan": spec.project_contract.runtime_plan.model_dump(mode="json"),
"runtime_dependencies": spec.runtime_dependencies.model_dump(mode="json"),
"public_endpoints": [endpoint.model_dump(mode="json") for endpoint in spec.public_endpoints],
}
if dependency_contract is not None:
payload["dependency_contract"] = dependency_contract
return payload
def default_generated_dir() -> Path:
return Path(__file__).resolve().parents[2] / "generated"
class ArtifactMaterializer:
def __init__(
self,
base_dir: str | Path | None = None,
creator_asset_service: CreatorAssetService | None = None,
) -> None:
self.base_dir = Path(base_dir or default_generated_dir())
self.base_dir.mkdir(parents=True, exist_ok=True)
self.creator_asset_service = creator_asset_service
def materialize_run(self, run: WorkflowRun, overwrite: bool = True) -> MaterializedBundle:
bundle_root = self.base_dir / run.id
existed = bundle_root.exists()
wiped = False
if existed:
if not overwrite:
manifest = bundle_root / "manifest.json"
raise FileExistsError(f"Bundle already exists at '{manifest}'.")
shutil.rmtree(bundle_root)
wiped = True
log_coursegen_event(
"materialize_run_invoked",
workflow_run_id=run.id,
bundle_root=str(bundle_root),
existed=existed,
wiped=wiped,
overwrite=overwrite,
)
public_dir = bundle_root / "public"
private_dir = bundle_root / "private"
public_dir.mkdir(parents=True, exist_ok=True)
private_dir.mkdir(parents=True, exist_ok=True)
files: list[BundleFile] = []
generated_at = datetime.now(UTC)
if run.artifacts.task_agent_spec is not None:
self._materialize_task_agent(
run=run,
spec=run.artifacts.task_agent_spec,
public_dir=public_dir,
private_dir=private_dir,
files=files,
)
else:
self._write_text(
private_dir / "README.txt",
"This workflow run is blocked and does not have a materializable draft yet.\n",
ArtifactVisibility.private,
files,
bundle_root,
role="blocked_run_readme",
audience="internal",
semantic_source="system_rendered",
)
manifest_payload = {
"bundle_id": f"{run.id}_bundle",
"generated_at": generated_at.isoformat(),
"root_dir": str(bundle_root),
"public_dir": str(public_dir),
"private_dir": str(private_dir),
"files": [entry.model_dump(mode="json") for entry in files],
}
manifest_path = bundle_root / "manifest.json"
manifest_path.write_text(json.dumps(manifest_payload, indent=2) + "\n", encoding="utf-8")
return MaterializedBundle(
bundle_id=f"{run.id}_bundle",
generated_at=generated_at,
root_dir=str(bundle_root),
public_dir=str(public_dir),
private_dir=str(private_dir),
manifest_path=str(manifest_path),
files=files,
)
def read_bundle_file(self, bundle: MaterializedBundle, relative_path: str) -> BundleFileContent:
root = Path(bundle.root_dir).resolve()
target = (root / relative_path).resolve()
if root not in target.parents and target != root:
raise ValueError("Requested file is outside the bundle root.")
if not target.exists() or not target.is_file():
raise FileNotFoundError(relative_path)
media_type = self._guess_media_type(relative_path)
return BundleFileContent(
relative_path=relative_path,
media_type=media_type,
content=target.read_text(encoding="utf-8"),
)
def _materialize_task_agent(
self,
*,
run: WorkflowRun,
spec: TaskAgentServiceSpec,
public_dir: Path,
private_dir: Path,
files: list[BundleFile],
) -> None:
bundle_root = public_dir.parent
self._write_json(
private_dir / "task_agent_spec.json",
spec.model_dump(mode="json"),
ArtifactVisibility.private,
files,
bundle_root,
role="task_agent_spec_snapshot",
audience="internal",
semantic_source="system_snapshot",
)
self._write_json(
private_dir / "validation_summary.json",
run.artifacts.validation_summary or {},
ArtifactVisibility.private,
files,
bundle_root,
role="validation_summary",
audience="internal",
semantic_source="system_snapshot",
)
self._write_json(
private_dir / "progression_preview.json",
run.artifacts.progression_preview,
ArtifactVisibility.private,
files,
bundle_root,
role="progression_preview",
audience="internal",
semantic_source="system_snapshot",
)
self._write_json(
private_dir / "workflow_snapshot.json",
{
"run_id": run.id,
"title": run.title,
"stage": run.stage.value,
"status": run.status.value,
"pending_gate": run.pending_gate.value if run.pending_gate else None,
"origin_template": run.artifacts.origin_template,
},
ArtifactVisibility.private,
files,
bundle_root,
role="workflow_snapshot",
audience="internal",
semantic_source="system_snapshot",
)
self._write_json(
private_dir / "node_executions.json",
[node.model_dump(mode="json") for node in run.artifacts.node_executions],
ArtifactVisibility.private,
files,
bundle_root,
role="node_executions",
audience="internal",
semantic_source="system_snapshot",
)
self._write_json(
private_dir / "review_summary.json",
run.artifacts.review_summary.model_dump(mode="json") if run.artifacts.review_summary is not None else {},
ArtifactVisibility.private,
files,
bundle_root,
role="review_summary",
audience="internal",
semantic_source="system_snapshot",
)
self._write_text(
public_dir / "README.md",
self._task_agent_readme(spec),
ArtifactVisibility.public,
files,
bundle_root,
role="course_readme",
audience="learner",
semantic_source="spec_rendered",
)
self._write_text(
public_dir / "runtime" / "Dockerfile",
self._assignment_runtime_dockerfile(spec),
ArtifactVisibility.public,
files,
bundle_root,
role="runtime_dockerfile",
audience="operator",
semantic_source="starter_compiler",
)
self._write_text(
public_dir / "runtime" / "README.md",
self._assignment_runtime_readme(),
ArtifactVisibility.public,
files,
bundle_root,
role="runtime_readme",
audience="operator",
semantic_source="system_rendered",
)
self._write_text(
public_dir / "runtime" / "verify_assignment.py",
self._assignment_runtime_verifier(),
ArtifactVisibility.public,
files,
bundle_root,
role="runtime_verifier",
audience="operator",
semantic_source="system_rendered",
)
self._write_text(
public_dir / "content" / "course_outline.md",
self._course_outline(spec),
ArtifactVisibility.public,
files,
bundle_root,
role="course_outline",
audience="learner",
semantic_source="spec_rendered",
)
if spec.course_structure.shared_codebase:
self._materialize_shared_codebase(
run=run,
spec=spec,
public_dir=public_dir,
private_dir=private_dir,
bundle_root=bundle_root,
files=files,
)
else:
self._materialize_per_deliverable_starters(
run=run,
spec=spec,
public_dir=public_dir,
bundle_root=bundle_root,
files=files,
)
def _materialize_shared_codebase(
self,
*,
run: WorkflowRun,
spec: TaskAgentServiceSpec,
public_dir: Path,
private_dir: Path,
bundle_root: Path,
files: list[BundleFile],
) -> None:
"""Write the new shared-codebase workspace layout:
public/starter/ # ONE shared root
public/checks/<id>/ # per-deliverable README + visible script
private/grader/<id>/ # per-deliverable manifest + hidden grader
"""
shared_starter_dir = shared_starter_workspace_path(public_dir)
# Decide source of shared starter content: workspace snapshot (already authored)
# or fresh default templates.
workspace_shared_dir = (
Path(run.artifacts.workspace_snapshot.public_dir) / "starter"
if run.artifacts.workspace_snapshot is not None
and Path(run.artifacts.workspace_snapshot.root_dir).resolve() != bundle_root.resolve()
else None
)
first_deliverable = spec.deliverables[0]
default_starter_files = build_task_agent_starter_files(spec, first_deliverable.id)
# Drop per-deliverable artifacts from the shared starter scaffolding.
default_shared_files = {
relative_path: content
for relative_path, content in default_starter_files.items()
if relative_path
not in {HIDDEN_MANIFEST_PATH, HIDDEN_GRADER_SCRIPT_PATH, "checks/run_visible_checks.py"}
and not relative_path.startswith("checks/")
and not relative_path.startswith(".coursegen/grader/")
}
shared_manifest_payload: dict[str, Any] | None = None
if workspace_shared_dir is not None and workspace_shared_dir.exists():
# Mirror the authored shared workspace into the bundle's public/starter/.
workspace_grader_root = (
Path(run.artifacts.workspace_snapshot.root_dir) / "private" / "grader"
if run.artifacts.workspace_snapshot is not None
else None
)
if workspace_grader_root is not None:
first_manifest_path = (
workspace_grader_root / first_deliverable.id / DELIVERABLE_MANIFEST_RELATIVE_PATH
)
if first_manifest_path.exists():
try:
shared_manifest_payload = json.loads(first_manifest_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
shared_manifest_payload = None
for relative_path in self._shared_workspace_paths(
workspace_shared_dir=workspace_shared_dir,
spec=spec,
manifest=shared_manifest_payload,
):
source_path = workspace_shared_dir / relative_path
if not source_path.exists() or not source_path.is_file():
continue
role, audience, semantic_source = self._starter_file_metadata(
relative_path,
manifest_payload=shared_manifest_payload,
)
try:
content = source_path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
continue
self._write_text(
shared_starter_dir / relative_path,
content,
ArtifactVisibility.public,
files,
bundle_root,
role=role,
audience=audience,
deliverable_id=None,
semantic_source=semantic_source,
)
else:
for relative_path, content in default_shared_files.items():
role, audience, semantic_source = self._starter_file_metadata(
relative_path,
manifest_payload=None,
)
self._write_text(
shared_starter_dir / relative_path,
content,
ArtifactVisibility.public,
files,
bundle_root,
role=role,
audience=audience,
deliverable_id=None,
semantic_source=semantic_source,
)
self._write_visible_fixture_files(
spec=spec,
deliverable_dir=shared_starter_dir,
deliverable_id=None,
files=files,
bundle_root=bundle_root,
)
# Shared course-level manifest at public/starter/.coursegen/course.json.
# Sourced from the authored per-deliverable manifest's dependency_contract
# (if present) so the shared course manifest stays coherent with what the
# bundle authoring loop produced.
course_dependency_contract: dict[str, Any] | None = None
if isinstance(shared_manifest_payload, dict):
course_dependency_contract = shared_manifest_payload.get("dependency_contract")
self._write_json(
shared_starter_dir / SHARED_COURSE_MANIFEST_RELATIVE_PATH,
build_shared_course_manifest_payload(
spec,
dependency_contract=course_dependency_contract,
),
ArtifactVisibility.public,
files,
bundle_root,
role="shared_course_manifest",
audience="operator",
semantic_source="starter_compiler",
)
# Per-deliverable: public/checks/<id>/ and private/grader/<id>/.
for deliverable in spec.deliverables:
checks_dir = deliverable_visible_checks_dir(public_dir, deliverable.id)
grader_dir = deliverable_grader_dir(private_dir, deliverable.id)
# public/checks/<id>/README.md
self._write_text(
checks_dir / "README.md",
self._starter_readme(spec, deliverable.id),
ArtifactVisibility.public,
files,
bundle_root,
role="starter_readme",
audience="learner",
deliverable_id=deliverable.id,
semantic_source="spec_rendered",
)
workspace_root_for_run = (
Path(run.artifacts.workspace_snapshot.root_dir)
if run.artifacts.workspace_snapshot is not None
else None
)
# Source for visible/hidden scripts + manifest: prefer the authored workspace
# (under public/checks/<id>/ and private/grader/<id>/), else default templates.
authored_visible = None
authored_hidden = None
authored_manifest_text = None
if (
workspace_root_for_run is not None
and workspace_root_for_run.resolve() != bundle_root.resolve()
):
ws_visible = (
Path(run.artifacts.workspace_snapshot.public_dir)
/ "checks"
/ deliverable.id
/ VISIBLE_CHECK_SCRIPT_RELATIVE_PATH
)
ws_hidden = (
workspace_root_for_run
/ "private"
/ "grader"
/ deliverable.id
/ HIDDEN_GRADER_SCRIPT_RELATIVE_PATH
)
ws_manifest = (
workspace_root_for_run
/ "private"
/ "grader"
/ deliverable.id
/ DELIVERABLE_MANIFEST_RELATIVE_PATH
)
if ws_visible.exists():
try:
authored_visible = ws_visible.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
authored_visible = None
if ws_hidden.exists():
try:
authored_hidden = ws_hidden.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
authored_hidden = None
if ws_manifest.exists():
try:
authored_manifest_text = ws_manifest.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
authored_manifest_text = None
visible_content = (
authored_visible
if authored_visible is not None
else default_starter_files.get("checks/run_visible_checks.py", "")
)
hidden_content = (
authored_hidden
if authored_hidden is not None
else default_starter_files.get(HIDDEN_GRADER_SCRIPT_PATH, "")
)
if deliverable.id == first_deliverable.id or authored_manifest_text is None:
# Default templates produce the same manifest content per deliverable.
manifest_for_this_deliverable = build_task_agent_starter_files(spec, deliverable.id)
manifest_text = (
authored_manifest_text
if authored_manifest_text is not None
else manifest_for_this_deliverable[HIDDEN_MANIFEST_PATH]
)
else:
manifest_text = authored_manifest_text
# Log which path produced the manifest. `default_template`
# means we lost any prior authored state (starter_repo_bundle
# reverts to `starter_default`).
try:
preview = json.loads(manifest_text)
preview_source = (preview.get("starter_repo_bundle") or {}).get("source")
except (json.JSONDecodeError, AttributeError):
preview_source = None
log_coursegen_event(
"materializer_manifest_resolved",
workflow_run_id=run.id,
deliverable_id=deliverable.id,
is_first_deliverable=(deliverable.id == first_deliverable.id),
authored_manifest_text_present=authored_manifest_text is not None,
source_path=(
"authored_workspace"
if authored_manifest_text is not None
else "default_template"
),
starter_repo_bundle_source=preview_source,
)
# public/checks/<id>/run_visible_checks.py
self._write_text(
checks_dir / VISIBLE_CHECK_SCRIPT_RELATIVE_PATH,
visible_content,
ArtifactVisibility.public,
files,
bundle_root,
role="visible_check_runner",
audience="learner",
deliverable_id=deliverable.id,
semantic_source="starter_compiler",
)
# private/grader/<id>/deliverable.json
self._write_text(
grader_dir / DELIVERABLE_MANIFEST_RELATIVE_PATH,
manifest_text,
ArtifactVisibility.private,
files,
bundle_root,
role="starter_manifest",
audience="operator",
deliverable_id=deliverable.id,
semantic_source="starter_compiler",
)
# private/grader/<id>/run_hidden_checks.py
self._write_text(
grader_dir / HIDDEN_GRADER_SCRIPT_RELATIVE_PATH,
hidden_content,
ArtifactVisibility.private,
files,
bundle_root,
role="runtime_hidden_check_script",
audience="operator",
deliverable_id=deliverable.id,
semantic_source="starter_compiler",
)
def _materialize_per_deliverable_starters(
self,
*,
run: WorkflowRun,
spec: TaskAgentServiceSpec,
public_dir: Path,
bundle_root: Path,
files: list[BundleFile],
) -> None:
"""Legacy materialization path for non-shared-codebase courses."""
for deliverable in spec.deliverables:
deliverable_dir = public_dir / "starter" / deliverable.id
self._write_text(
deliverable_dir / "README.md",
self._starter_readme(spec, deliverable.id),
ArtifactVisibility.public,
files,
bundle_root,
role="starter_readme",
audience="learner",
deliverable_id=deliverable.id,
semantic_source="spec_rendered",
)
workspace_starter_dir = (
Path(run.artifacts.workspace_snapshot.public_dir) / "starter" / deliverable.id
if run.artifacts.workspace_snapshot is not None
and Path(run.artifacts.workspace_snapshot.root_dir).resolve() != bundle_root.resolve()
else None
)
if workspace_starter_dir is not None and workspace_starter_dir.exists():
workspace_manifest = load_starter_manifest(workspace_starter_dir)
for relative_path in self._workspace_starter_paths(
workspace_starter_dir=workspace_starter_dir,
spec=spec,
deliverable_id=deliverable.id,
):
source_path = workspace_starter_dir / relative_path
if not source_path.exists() or not source_path.is_file():
continue
role, audience, semantic_source = self._starter_file_metadata(
relative_path,
manifest_payload=workspace_manifest,
)
try:
content = source_path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
continue
self._write_text(
deliverable_dir / relative_path,
content,
ArtifactVisibility.public,
files,
bundle_root,
role=role,
audience=audience,
deliverable_id=deliverable.id,
semantic_source=semantic_source,
)
else:
starter_files = build_task_agent_starter_files(spec, deliverable.id)
starter_manifest = json.loads(starter_files[HIDDEN_MANIFEST_PATH])
for relative_path, content in starter_files.items():
role, audience, semantic_source = self._starter_file_metadata(
relative_path,
manifest_payload=starter_manifest,
)
self._write_text(
deliverable_dir / relative_path,
content,
ArtifactVisibility.public,
files,
bundle_root,
role=role,
audience=audience,
deliverable_id=deliverable.id,
semantic_source=semantic_source,
)
self._write_visible_fixture_files(
spec=spec,
deliverable_dir=deliverable_dir,
deliverable_id=deliverable.id,
files=files,
bundle_root=bundle_root,
)
def _task_agent_readme(self, spec: TaskAgentServiceSpec) -> str:
runtime_plan = spec.project_contract.runtime_plan
stack_bits = [
runtime_plan.implementation_language,
runtime_plan.language_version,
runtime_plan.application_framework,
runtime_plan.framework_version,
]
stack_summary = " ".join(bit for bit in stack_bits if bit) or "not specified"
system_profile = ", ".join(f"`{label}`" for label in spec.capabilities.summary_labels())
visible_fixtures = ", ".join(f"`{path}`" for path in spec.runtime_dependencies.visible_fixture_files) or "`none`"
lines = [
f"# {spec.title}",
"",
spec.summary,
"",
f"- Package type: `{spec.package_type.value}`",
f"- Project family: `{spec.project_contract.family.value}`",
f"- System kind: {spec.project_contract.system_kind}",
f"- Runtime stack: {stack_summary}",
f"- Execution surface: `{spec.runtime_dependencies.execution_surface.value}`",
f"- System profile: {system_profile}",
f"- Visible fixtures: {visible_fixtures}",
"",
"## Public service surface",
"",
]
lines.extend(f"- `{endpoint.method} {endpoint.path}`" for endpoint in spec.public_endpoints if endpoint.required)
lines.extend(["", "## Runtime components", ""])
for service in runtime_plan.services:
technology = f" using `{service.technology}`" if service.technology else ""
lines.append(f"- `{service.service_id}` ({service.role}){technology}")
lines.extend(["", "## Deliverable arc", ""])
for deliverable in spec.deliverables:
lines.append(f"- `{deliverable.id}` - {deliverable.title}: {deliverable.objective}")
return "\n".join(lines)
def _course_outline(self, spec: TaskAgentServiceSpec) -> str:
lines = ["# Course Outline", ""]
for deliverable in spec.deliverables:
gate = spec.gate_for(deliverable.id)
lines.extend(
[
f"## {deliverable.id}: {deliverable.title}",
"",
deliverable.objective,
"",
f"- Starter type: `{spec.runtime_dependencies.starter_type.value}`",
f"- Active visible checks: {', '.join(f'`{item}`' for item in gate.active_public_check_ids) or 'none'}",
"",
]
)
return "\n".join(lines) + "\n"
def _write_visible_fixture_files(
self,
*,
spec: TaskAgentServiceSpec,
deliverable_dir: Path,
deliverable_id: str | None,
files: list[BundleFile],
bundle_root: Path,
) -> None:
visible_paths = list(dict.fromkeys(spec.runtime_dependencies.visible_fixture_files))
sources_by_path = {
source.workspace_path: source
for source in spec.runtime_dependencies.data_sources
if source.learner_visible and source.workspace_path
}
for relative_path in visible_paths:
if not relative_path:
continue
content = self._visible_fixture_content(relative_path, sources_by_path.get(relative_path))
self._write_text(
deliverable_dir / relative_path,
content,
ArtifactVisibility.public,
files,
bundle_root,
role="visible_fixture",
audience="learner",
deliverable_id=deliverable_id,
semantic_source="source_materialized",
)
def _visible_fixture_content(self, relative_path: str, source) -> str:
if source is not None and source.asset_id and self.creator_asset_service is not None:
try:
_record, content = self.creator_asset_service.read_asset_text(source.asset_id)
return content if content.endswith("\n") else content + "\n"
except (FileNotFoundError, KeyError):
pass
description = (getattr(source, "description", None) or "Visible learner fixture.").strip()
suffix = Path(relative_path).suffix.lower()
if suffix == ".json":
return json.dumps(
{"title": getattr(source, "title", "Uploaded data source"), "description": description, "items": []},
indent=2,
) + "\n"
if suffix == ".csv":
return "id,value\n"
if suffix in {".md", ".markdown"}:
return f"# {getattr(source, 'title', 'Uploaded data source')}\n\n{description}\n"
return description + "\n"
def _starter_readme(self, spec: TaskAgentServiceSpec, deliverable_id: str) -> str:
deliverable = next(item for item in spec.deliverables if item.id == deliverable_id)
brief = deliverable.learner_brief or build_task_agent_deliverable_brief(spec, deliverable)
return render_learner_starter_readme(
title=f"Starter for {deliverable.title}",
brief=brief,
summary=deliverable.objective,
learning_outcomes=list(deliverable.learning_outcomes),
visible_check_command=spec.runtime_dependencies.visible_check_command or "sh .coursegen/runtime/check_visible.sh",
preview_command=spec.runtime_dependencies.preview_command or default_preview_command(spec, host="127.0.0.1"),
public_checks=deliverable.public_checks,
implementation_language=spec.runtime_dependencies.implementation_language,
language_version=spec.runtime_dependencies.language_version,
package_manager=spec.runtime_dependencies.package_manager,
)
def _assignment_runtime_dockerfile(self, spec: TaskAgentServiceSpec) -> str:
bootstrap_commands = task_agent_runtime_bootstrap_commands(spec, include_python=True)
environment_lines = task_agent_runtime_environment_lines(spec)
lines = [
f"FROM {task_agent_runtime_base_image(spec)}",
"",
"ENV PYTHONDONTWRITEBYTECODE=1",
"ENV PYTHONUNBUFFERED=1",
*environment_lines,
"",
"WORKDIR /workspace",
]
if bootstrap_commands:
lines.extend(["", "RUN " + " && \\\n ".join(bootstrap_commands)])
lines.extend(["", "COPY . /workspace", 'CMD ["python3", "runtime/verify_assignment.py"]', ""])
return "\n".join(lines)
def _assignment_runtime_readme(self) -> str:
return "\n".join(
[
"# Assignment Runtime Sandbox",
"",
"This Docker image verifies that the generated assignment starters compile and boot before author review opens.",
"",
]
)
def _assignment_runtime_verifier(self) -> str:
return "\n".join(
[
"from __future__ import annotations",
"",
"import json",
"import os",
"import signal",
"import subprocess",
"import time",
"from pathlib import Path",
"from urllib.error import URLError",
"from urllib.request import Request, urlopen",
"",
'ROOT = Path(__file__).resolve().parents[1]',
'STARTERS = ROOT / "starter"',
'PORT = int(os.environ.get("ASSIGNMENT_SANDBOX_PORT", "8010"))',
"",
"",
"def request_json(method: str, url: str, payload=None, timeout: float = 3.0):",
" data = None",
" headers = {}",
" if payload is not None:",
" data = json.dumps(payload).encode('utf-8')",
" headers['content-type'] = 'application/json'",
" request = Request(url, data=data, headers=headers, method=method)",
" with urlopen(request, timeout=timeout) as response:",
" body = response.read().decode('utf-8', errors='replace')",
" return response.status, json.loads(body) if body else {}, dict(response.headers)",
"",
"",
"def wait_for_health(port: int, path: str, timeout_s: float = 12.0):",
" deadline = time.time() + timeout_s",
" last_error = None",
" while time.time() < deadline:",
" try:",
' status, payload, _headers = request_json("GET", f"http://127.0.0.1:{port}{path}")',
" return status, payload",
" except URLError as exc:",
" last_error = str(exc)",
" time.sleep(0.25)",
" except Exception as exc:",
" last_error = str(exc)",
" time.sleep(0.25)",
" raise RuntimeError(last_error or 'health check timed out')",
"",
"",
"def terminate(proc: subprocess.Popen[str]):",
" if proc.poll() is not None:",
" return proc.communicate()",
" try:",
" os.killpg(proc.pid, signal.SIGTERM)",
" proc.wait(timeout=5)",
" except subprocess.TimeoutExpired:",
" os.killpg(proc.pid, signal.SIGKILL)",
" proc.wait(timeout=5)",
" return proc.communicate()",
"",
"",
"def manifest(deliverable_dir: Path) -> dict[str, object]:",
f" manifest_path = deliverable_dir / '{HIDDEN_MANIFEST_PATH}'",
" return json.loads(manifest_path.read_text(encoding='utf-8'))",
"",
"",
"def healthcheck_path(manifest_payload: dict[str, object]) -> str:",
" runtime_plan = manifest_payload.get('runtime_plan') or {}",
" services = runtime_plan.get('services') or []",
" for service in services:",
" if service.get('service_id') == 'app' and service.get('healthcheck_path'):",
" return str(service['healthcheck_path'])",
" return '/health'",
"",
"",
"def runtime_script(deliverable_dir: Path, relative_path: str) -> Path:",
" return deliverable_dir / relative_path",
"",
"",
"def preview_command(manifest_payload: dict[str, object]) -> str:",
" return str(manifest_payload.get('preview_command') or 'sh .coursegen/runtime/run.sh')",
"",
"",
"def primary_check(manifest_payload: dict[str, object]) -> dict[str, object] | None:",
" checks = manifest_payload.get('public_checks') or []",
" return checks[0] if checks else None",
"",
"",
"def verify_deliverable(deliverable_dir: Path, port: int):",
" report = {'deliverable_id': deliverable_dir.name, 'compile_succeeded': False, 'runtime_succeeded': False, 'health_status_code': None, 'stdout': '', 'stderr': '', 'error': None}",
" environment = os.environ.copy()",
" environment['PORT'] = str(port)",
" try:",
" manifest_payload = manifest(deliverable_dir)",
f" install_script = runtime_script(deliverable_dir, '{RUNTIME_INSTALL_SCRIPT_PATH}')",
f" verify_script = runtime_script(deliverable_dir, '{RUNTIME_VERIFY_SCRIPT_PATH}')",
" if not install_script.exists():",
" raise FileNotFoundError(f'missing runtime install script {install_script.relative_to(deliverable_dir)}')",
" if not verify_script.exists():",
" raise FileNotFoundError(f'missing runtime verify script {verify_script.relative_to(deliverable_dir)}')",
f" subprocess.run('sh {RUNTIME_INSTALL_SCRIPT_PATH}', cwd=deliverable_dir, env=environment, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)",
f" subprocess.run('sh {RUNTIME_VERIFY_SCRIPT_PATH}', cwd=deliverable_dir, env=environment, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)",
" report['compile_succeeded'] = True",
" except Exception as exc:",
" report['error'] = f'compile failed: {exc}'",
" return report",
" proc = subprocess.Popen(preview_command(manifest_payload), cwd=deliverable_dir, env=environment, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True, start_new_session=True)",
" try:",
" status_code, _payload = wait_for_health(port, healthcheck_path(manifest_payload))",
" report['runtime_succeeded'] = status_code == 200",
" report['health_status_code'] = status_code",
" check = primary_check(manifest_payload)",
" if check:",
" request_json(str(check.get('request_method') or 'POST').upper(), f\"http://127.0.0.1:{port}{check.get('request_path')}\", check.get('request_body') or None)",
" except Exception as exc:",
" report['error'] = f'runtime failed: {exc}'",
" finally:",
" stdout, stderr = terminate(proc)",
" report['stdout'] = stdout",
" report['stderr'] = stderr",
" return report",
"",
"",
"def main():",
" deliverable_dirs = sorted(path for path in STARTERS.iterdir() if path.is_dir())",
" reports = [verify_deliverable(deliverable_dir, PORT + index) for index, deliverable_dir in enumerate(deliverable_dirs)]",
" success = all(item['compile_succeeded'] and item['runtime_succeeded'] for item in reports)",
" payload = {'success': success, 'deliverable_reports': reports, 'error': None if success else 'One or more generated starters failed sandbox verification.'}",
" print(json.dumps(payload))",
" raise SystemExit(0 if success else 1)",
"",
"",
"if __name__ == '__main__':",
" main()",
"",
]
)
def _shared_workspace_paths(
self,
*,
workspace_shared_dir: Path,
spec: TaskAgentServiceSpec,
manifest: dict[str, Any] | None,
) -> list[str]:
"""Files to mirror from the workspace's shared starter root into the bundle.
Excludes per-deliverable artifacts (manifest, hidden grader, visible script,
their containing folders) — those live outside the shared starter now.
"""
first_deliverable = spec.deliverables[0] if spec.deliverables else None
editable_paths = (
learner_editable_paths_for_deliverable(spec, first_deliverable)
if first_deliverable is not None and manifest is None
else None
)