-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_design_inference.py
More file actions
1381 lines (1300 loc) · 51.3 KB
/
Copy pathassignment_design_inference.py
File metadata and controls
1381 lines (1300 loc) · 51.3 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
from enum import Enum
import re
from pydantic import BaseModel, Field
from app.domain.registry import PackageType, RiskClass
from app.domain.task_agent import (
AssignmentDesignSpec,
AssessmentStrategySpec,
CapabilitySpec,
CourseStructureSpec,
DataSourceKind,
DataSourceSpec,
default_project_contract,
ExecutionSurface,
ProjectContractSpec,
ProjectFamily,
ProjectRuntimeBindingSpec,
ProjectRuntimeCommandSpec,
ProjectRuntimePlanSpec,
ProjectRuntimeServiceSpec,
ProjectServiceBinding,
ProgressionMode,
RetrievalMode,
RuntimeDependencySpec,
WorkspaceScope,
)
from app.domain.registry import StarterType
from app.services.public_surface_quality import extract_project_entities, pluralize_phrase
from app.services.task_agent_starter_templates import (
RUNTIME_RUN_SCRIPT_PATH,
RUNTIME_VISIBLE_CHECK_SCRIPT_PATH,
)
class DesignSupportStatus(str, Enum):
supported = "supported"
manual_review = "manual_review"
unsupported = "unsupported"
class GenerationIntake(BaseModel):
title: str
problem_statement: str
learning_outcomes: list[str] = Field(default_factory=list)
package_type_hint: PackageType | None = None
starter_type: StarterType | None = None
implementation_language: str | None = None
language_version: str | None = None
application_framework: str | None = None
framework_version: str | None = None
package_manager: str | None = None
primary_database: str | None = None
primary_database_version: str | None = None
cache_backend: str | None = None
cache_backend_version: str | None = None
tech_stack: list[str] = Field(default_factory=list)
data_sources: list[DataSourceSpec] = Field(default_factory=list)
class AssignmentDesignInference(BaseModel):
design_spec: AssignmentDesignSpec | None = None
package_type: PackageType
status: DesignSupportStatus
reasons: list[str] = Field(default_factory=list)
warnings: list[str] = Field(default_factory=list)
OVERLAY_KEYWORDS: dict[str, list[str]] = {
"productionization_overlay": [
"production",
"observability",
"state",
"fallback",
"approval",
"trace",
"eval",
"resume",
"durable",
],
"scale_slo_overlay": [
"latency",
"throughput",
"scale",
"slo",
"cost",
"p95",
"error rate",
],
"freshness_overlay": [
"freshness",
"change stream",
"reindex",
"stale",
],
"adversarial_overlay": [
"prompt injection",
"adversarial",
"malicious",
"robustness",
],
}
DOMAIN_PACK_KEYWORDS: dict[str, list[str]] = {
"oncall_copilot": ["incident", "oncall", "runbook", "alert"],
"rfp_drafter": ["rfp", "proposal", "sales engineering"],
"analyst_sql": ["sql", "query", "analysis", "dashboard"],
"qbr_prep": ["qbr", "business review", "account review"],
"investment_memo": ["investment", "memo", "venture", "vc"],
"clinical_case_triage": ["clinical", "patient", "diagnosis", "medical"],
"customer_support_agent": [
"customer support",
"support bot",
"support ticket",
"ticket",
"refund",
"billing",
"outage",
"account access",
"suspicious login",
"customer message",
],
}
REVIEW_REQUIRED_KEYWORDS = {"clinical", "patient", "medical", "diagnosis"}
HIGH_STAKES_KEYWORDS = {"legal", "prescription", "financial advice"}
GROUNDED_RETRIEVAL_KEYWORDS = {
"rag",
"citation",
"citations",
"grounded",
"grounded answer",
"faithful",
"hallucination",
"knowledge base",
"answer from documents",
}
RANKED_RETRIEVAL_KEYWORDS = {
"semantic search",
"search",
"retrieval",
"vector",
"ranking",
"nearest neighbor",
"metadata filter",
}
CONTROL_PLANE_KEYWORDS = {
"feature flag",
"feature flags",
"gradual rollout",
"rollout",
"targeting",
"targeted",
"kill switch",
"environment override",
"audit log",
"audit trail",
"config update",
"configuration update",
"flag evaluation",
}
STATEFUL_KEYWORDS = {
"booking",
"reservation",
"inventory",
"wallet",
"payment",
"idempotent",
"idempotency",
"concurrency",
"mutable state",
}
TOOL_USE_KEYWORDS = {
"agent",
"tool",
"workflow",
"copilot",
"draft",
"trace",
"approval",
"handoff",
"sql",
}
FRAMEWORK_LANGUAGE_HINTS: dict[str, str] = {
# Python
"fastapi": "python",
"flask": "python",
"django": "python",
# TypeScript / Node
"express": "typescript",
"hono": "typescript",
"nestjs": "typescript",
# Go
"gin": "go",
"fiber": "go",
# Rust
"actix": "rust",
"actix-web": "rust",
"axum": "rust",
# Ruby — `framework` is the framework NAME ("rails"); the version
# is captured separately in `framework_version`, so don't add
# version-tagged keys like "rails 8" (they'd shadow "rails" on
# length-descending sort).
"ruby on rails": "ruby",
"rails": "ruby",
"sinatra": "ruby",
"hanami": "ruby",
# Java / Kotlin
"spring boot": "java",
"spring": "java",
"quarkus": "java",
"micronaut": "java",
"ktor": "kotlin",
# Elixir
"phoenix": "elixir",
# .NET
"aspnet": "csharp",
"asp.net": "csharp",
"asp.net core": "csharp",
# PHP
"laravel": "php",
"symfony": "php",
}
DEFAULT_FRAMEWORK_BY_LANGUAGE: dict[str, str] = {
"python": "fastapi",
"typescript": "express",
"javascript": "express",
"go": "gin",
"rust": "actix-web",
"ruby": "rails",
"java": "spring boot",
"kotlin": "ktor",
"elixir": "phoenix",
"csharp": "aspnet",
"php": "laravel",
}
LANGUAGE_KEYWORDS: dict[str, list[str]] = {
"python": ["python", "fastapi", "flask", "django"],
"typescript": ["typescript", "ts", "nestjs", "hono"],
"javascript": ["javascript", "node", "node.js", "express"],
"go": ["go", "golang", "gin", "fiber"],
"rust": ["rust", "actix", "axum"],
"ruby": ["ruby", "rails", "ruby on rails", "sinatra", "hanami", "gemfile"],
"java": ["java", "spring boot", "spring", "quarkus", "micronaut", "maven", "gradle"],
"kotlin": ["kotlin", "ktor"],
"elixir": ["elixir", "phoenix", "liveview"],
"csharp": ["c#", "csharp", "aspnet", "asp.net", "dotnet", ".net"],
"php": ["php", "laravel", "symfony"],
}
def _tokenize_phrase(value: str) -> tuple[str, ...]:
tokens: list[str] = []
current: list[str] = []
for character in value.lower():
if character.isalnum():
current.append(character)
continue
if current:
tokens.append("".join(current))
current = []
if current:
tokens.append("".join(current))
return tuple(tokens)
def _contains_keyword(text: str, keyword: str) -> bool:
keyword_tokens = _tokenize_phrase(keyword)
if not keyword_tokens:
return False
text_tokens = _tokenize_phrase(text)
span = len(keyword_tokens)
if span > len(text_tokens):
return False
for start in range(len(text_tokens) - span + 1):
if text_tokens[start : start + span] == keyword_tokens:
return True
return False
def _contains_any_keyword(text: str, keywords: list[str] | set[str]) -> bool:
return any(_contains_keyword(text, keyword) for keyword in keywords)
def _keyword_match_count(text: str, keywords: list[str]) -> int:
return sum(1 for keyword in keywords if _contains_keyword(text, keyword))
def build_project_contract(
*,
family: ProjectFamily,
title: str,
problem_statement: str,
implementation_language: str | None = None,
language_version: str | None = None,
application_framework: str | None = None,
framework_version: str | None = None,
package_manager: str | None = None,
primary_database: str | None = None,
primary_database_version: str | None = None,
cache_backend: str | None = None,
cache_backend_version: str | None = None,
tech_stack: list[str] | None = None,
data_sources: list[DataSourceSpec] | None = None,
) -> ProjectContractSpec:
text = " ".join([title, problem_statement]).lower()
source_specs = list(data_sources or [])
source_titles = [source.title for source in source_specs]
inferred_entities = extract_project_entities(title, problem_statement)
primary_entity = inferred_entities[0] if inferred_entities else None
runtime_binding = build_project_runtime_binding(
family=family,
implementation_language=implementation_language,
application_framework=application_framework,
primary_database=primary_database,
cache_backend=cache_backend,
tech_stack=tech_stack or [],
data_sources=source_specs,
)
runtime_plan = build_project_runtime_plan(
family=family,
implementation_language=implementation_language,
language_version=language_version,
application_framework=application_framework,
framework_version=framework_version,
package_manager=package_manager,
primary_database=primary_database,
primary_database_version=primary_database_version,
cache_backend=cache_backend,
cache_backend_version=cache_backend_version,
tech_stack=tech_stack or [],
data_sources=source_specs,
)
if family == ProjectFamily.grounded_retrieval_service:
return ProjectContractSpec(
family=family,
system_kind=(
f"{primary_entity.title()} retrieval service"
if primary_entity
else "Grounded retrieval and answer service"
),
core_entities=(
[*inferred_entities, *source_titles]
if inferred_entities
else ["retrieval corpus", *source_titles] if source_titles else ["retrieval corpus", "grounded response"]
),
primary_read_paths=[
"retrieve supporting passages for a query",
"compose a grounded answer with citations",
],
primary_write_paths=[],
invariants=[
"Answers stay grounded in the learner-visible corpus.",
"Unsupported questions abstain instead of guessing.",
"Citations reference the evidence that justified the answer.",
],
operational_concerns=["retrieval quality", "citation fidelity", "latency under repeated queries"],
runtime_binding=runtime_binding,
runtime_plan=runtime_plan,
)
if family == ProjectFamily.ranked_retrieval_service:
return ProjectContractSpec(
family=family,
system_kind=(
f"{primary_entity.title()} search service"
if primary_entity
else "Ranked retrieval service"
),
core_entities=(
[*inferred_entities, *source_titles]
if inferred_entities
else ["retrieval corpus", *source_titles] if source_titles else ["retrieval corpus", "search result"]
),
primary_read_paths=["retrieve and rank relevant results for a query"],
primary_write_paths=[],
invariants=[
"Results are ranked consistently for equivalent queries.",
"Filters do not leak results outside the requested scope.",
],
operational_concerns=["ranking quality", "metadata filtering", "read-path latency"],
runtime_binding=runtime_binding,
runtime_plan=runtime_plan,
)
if family == ProjectFamily.control_plane_service:
entity = primary_entity or "control definition"
entity_plural = pluralize_phrase(entity)
return ProjectContractSpec(
family=family,
system_kind=f"{entity.title()} control plane",
core_entities=inferred_entities or ["control definitions", "decision rules", "request context", "audit events"],
primary_read_paths=[
f"evaluate the active {entity} for a request context",
f"serve low-latency {entity_plural} decisions for live traffic",
],
primary_write_paths=[
f"create or update {entity_plural} safely",
f"publish {entity_plural} changes with traceable state transitions",
],
invariants=[
"Decisions are deterministic for the same context and active definition.",
"Live reads stay coherent when control definitions change.",
"Every mutation is auditable and attributable.",
],
operational_concerns=["read-path coherence", "safe control updates", "operator-visible audit trails"],
runtime_binding=runtime_binding,
runtime_plan=runtime_plan,
)
if family == ProjectFamily.transactional_stateful_service:
entity = primary_entity or "record"
entity_plural = pluralize_phrase(entity)
return ProjectContractSpec(
family=family,
system_kind=f"{entity.title()} service",
core_entities=inferred_entities or ["durable records", "mutable workflow state"],
primary_read_paths=[f"serve the current {entity} state safely under load"],
primary_write_paths=[f"create or update {entity_plural} without violating invariants"],
invariants=[
"Concurrent or repeated writes do not corrupt critical state.",
"State transitions preserve the service's core business invariants.",
],
operational_concerns=["concurrency safety", "idempotency", "failure recovery"],
runtime_binding=runtime_binding,
runtime_plan=runtime_plan,
)
if family == ProjectFamily.workflow_agent_service:
entity = primary_entity or "workflow request"
entity_plural = pluralize_phrase(entity)
return ProjectContractSpec(
family=family,
system_kind=f"{entity.title()} workflow service",
core_entities=inferred_entities or ["requests", "tool runs", "operator decisions", "run traces"],
primary_read_paths=[f"inspect {entity_plural} and route work through bounded workflows"],
primary_write_paths=[f"progress {entity_plural} without breaking the published contract"],
invariants=[
"The service preserves a stable response contract.",
"Operator-visible traces explain why the workflow took each step.",
],
operational_concerns=["tool routing", "fallbacks", "traceability"],
runtime_binding=runtime_binding,
runtime_plan=runtime_plan,
)
entity = primary_entity or "service request"
return ProjectContractSpec(
family=ProjectFamily.generic_backend_service,
system_kind=f"{entity.title()} service",
core_entities=inferred_entities or ["service request", "service response"],
primary_read_paths=[f"handle supported {entity} flows through a stable contract"],
primary_write_paths=[],
invariants=["The service preserves the published contract for supported requests."],
operational_concerns=["error handling", "observability"],
runtime_binding=runtime_binding,
runtime_plan=runtime_plan,
)
def build_project_runtime_binding(
*,
family: ProjectFamily,
implementation_language: str | None,
application_framework: str | None,
primary_database: str | None,
cache_backend: str | None,
tech_stack: list[str],
data_sources: list[DataSourceSpec],
) -> ProjectRuntimeBindingSpec:
backing_services: list[ProjectServiceBinding] = []
seed_artifacts: list[str] = []
integration_points: list[str] = []
if implementation_language:
language_note = f"Implement the learner-facing service in {implementation_language}."
if application_framework:
language_note = f"Implement the learner-facing service in {implementation_language} using {application_framework}."
integration_points.append(language_note)
if primary_database:
backing_services.append(
ProjectServiceBinding(
service_id=primary_database,
role="durable state",
technology=primary_database,
)
)
seed_artifacts.append(f"Initialize and seed {primary_database} for learner-visible scenarios.")
integration_points.append(f"Connect the application write path to {primary_database}.")
if cache_backend:
backing_services.append(
ProjectServiceBinding(
service_id=cache_backend,
role="cache or fast read path",
technology=cache_backend,
)
)
integration_points.append(f"Wire {cache_backend} into the read path without breaking freshness guarantees.")
for source in data_sources:
if source.kind == DataSourceKind.uploaded_file:
seed_artifacts.append(f"Materialize `{source.title}` into the learner workspace.")
elif source.kind == DataSourceKind.seed_database:
seed_artifacts.append(f"Load `{source.title}` into the backing state before review runs.")
elif source.kind == DataSourceKind.mock_api:
backing_services.append(
ProjectServiceBinding(
service_id=source.id,
role="mock dependency",
technology=source.format or "http",
)
)
integration_points.append(f"Bind the application to the mocked dependency `{source.title}`.")
elif source.kind == DataSourceKind.object_store:
backing_services.append(
ProjectServiceBinding(
service_id=source.id,
role="object storage",
technology=source.format or "blob storage",
)
)
if family == ProjectFamily.control_plane_service:
integration_points.extend(
[
"Keep live decisions deterministic for the same request context.",
"Publish mutations without leaving caches or derived read paths stale.",
]
)
elif family == ProjectFamily.transactional_stateful_service:
integration_points.extend(
[
"Preserve write correctness under repeated or concurrent requests.",
"Make state transitions observable enough to debug production failures.",
]
)
elif family == ProjectFamily.workflow_agent_service:
integration_points.extend(
[
"Keep tool routing bounded and explainable in traces.",
"Handle approval and fallback paths without breaking the response contract.",
]
)
elif family in {ProjectFamily.grounded_retrieval_service, ProjectFamily.ranked_retrieval_service}:
integration_points.extend(
[
"Make the retrieval layer query learner-visible data consistently.",
"Keep the answer or ranking path aligned with the visible corpus contract.",
]
)
else:
integration_points.append("Keep the application contract stable while integrating supporting runtime pieces.")
for tech in tech_stack:
if tech and tech not in {primary_database, cache_backend}:
integration_points.append(f"Use `{tech}` only where it materially supports the project contract.")
return ProjectRuntimeBindingSpec(
implementation_language=implementation_language,
application_framework=application_framework,
backing_services=backing_services,
seed_artifacts=list(dict.fromkeys(seed_artifacts)),
integration_points=list(dict.fromkeys(integration_points)),
)
def _version_hint_for(*, aliases: list[str], tech_stack: list[str]) -> str | None:
alias_set = {alias.lower() for alias in aliases if alias}
for item in tech_stack:
lowered = item.lower().strip()
if not lowered:
continue
if not any(alias in lowered for alias in alias_set):
continue
match = re.search(r"\b(?:v)?(\d+(?:\.\d+)*)\b", lowered)
if match:
return match.group(1)
return None
def default_language_version_for_stack(implementation_language: str | None) -> str | None:
normalized_language = (implementation_language or "").strip().lower() or None
defaults = {
"python": "3.12",
"typescript": "22",
"javascript": "22",
"go": "1.25",
"rust": "1.86",
}
return defaults.get(normalized_language)
def default_dependency_version(technology: str | None) -> str | None:
normalized = (technology or "").strip().lower() or None
defaults = {
"postgres": "16",
"postgresql": "16",
"mongodb": "7",
"mongo": "7",
"redis": "7",
"mysql": "8",
"mariadb": "8",
}
return defaults.get(normalized)
def infer_package_manager(
*,
implementation_language: str | None,
tech_stack: list[str],
) -> str | None:
lowered_stack = " ".join(tech_stack).lower()
if "pnpm" in lowered_stack:
return "pnpm"
if "yarn" in lowered_stack:
return "yarn"
if "bun" in lowered_stack:
return "bun"
if "npm" in lowered_stack:
return "npm"
if "uv" in lowered_stack:
return "uv"
if "poetry" in lowered_stack:
return "poetry"
if implementation_language == "python":
return "uv"
if implementation_language in {"typescript", "javascript"}:
return "pnpm"
if implementation_language == "go":
return "go"
if implementation_language == "rust":
return "cargo"
return None
def runtime_container_image_for_stack(
*,
implementation_language: str | None,
language_version: str | None,
allow_inference: bool = True,
) -> str | None:
normalized_language = (implementation_language or "").strip().lower() or None
version = (language_version or "").strip() or (
default_language_version_for_stack(normalized_language)
if allow_inference
else None
)
if normalized_language == "python":
return f"python:{version}-slim" if version else None
if normalized_language in {"typescript", "javascript"}:
return f"node:{version}-bookworm-slim" if version else None
if normalized_language == "go":
return f"golang:{version}-bookworm" if version else None
if normalized_language == "rust":
return f"rust:{version}-bookworm" if version else None
return None
def dependency_container_image(
*,
technology: str | None,
version_hint: str | None,
allow_inference: bool = True,
) -> str | None:
normalized = (technology or "").strip().lower()
version = (version_hint or "").strip() or (
default_dependency_version(normalized)
if allow_inference
else None
)
if normalized in {"postgres", "postgresql"}:
base_version = version or "16"
return base_version if ":" in base_version else f"postgres:{base_version}-alpine"
if normalized in {"mongodb", "mongo"}:
return f"mongo:{version or '7'}"
if normalized == "redis":
base_version = version or "7"
return base_version if ":" in base_version else f"redis:{base_version}-alpine"
if normalized in {"mysql", "mariadb"}:
return f"{normalized}:{version or '8'}"
return None
def build_project_runtime_plan(
*,
family: ProjectFamily,
implementation_language: str | None,
language_version: str | None,
application_framework: str | None,
framework_version: str | None,
package_manager: str | None,
primary_database: str | None,
primary_database_version: str | None,
cache_backend: str | None,
cache_backend_version: str | None,
tech_stack: list[str],
data_sources: list[DataSourceSpec],
allow_inference: bool = True,
) -> ProjectRuntimePlanSpec:
language_runtime = {
"typescript": "node",
"javascript": "node",
}.get(implementation_language or "", implementation_language)
resolved_language_version = (language_version or "").strip() or (
_version_hint_for(
aliases=[language_runtime or "", implementation_language or ""],
tech_stack=tech_stack,
)
if allow_inference
else None
) or (
default_language_version_for_stack(implementation_language)
if allow_inference
else None
)
resolved_framework_version = (framework_version or "").strip() or (
_version_hint_for(
aliases=[application_framework or ""],
tech_stack=tech_stack,
)
if allow_inference
else None
)
resolved_package_manager = (package_manager or "").strip().lower() or (
infer_package_manager(
implementation_language=implementation_language,
tech_stack=tech_stack,
)
if allow_inference
else None
)
resolved_primary_database_version = (primary_database_version or "").strip() or (
_version_hint_for(
aliases=[primary_database or ""],
tech_stack=tech_stack,
)
if allow_inference
else None
) or (
default_dependency_version(primary_database)
if allow_inference
else None
)
resolved_cache_backend_version = (cache_backend_version or "").strip() or (
_version_hint_for(
aliases=[cache_backend or ""],
tech_stack=tech_stack,
)
if allow_inference
else None
) or (
default_dependency_version(cache_backend)
if allow_inference
else None
)
services: list[ProjectRuntimeServiceSpec] = [
ProjectRuntimeServiceSpec(
service_id="app",
role="learner-facing application",
technology=application_framework or implementation_language,
version_hint=resolved_framework_version or resolved_language_version,
package_manager=resolved_package_manager,
entrypoint_path=None,
container_image=runtime_container_image_for_stack(
implementation_language=implementation_language,
language_version=resolved_language_version,
allow_inference=allow_inference,
),
learner_managed=True,
healthcheck_path="/health",
default_port=8000,
)
]
if primary_database:
services.append(
ProjectRuntimeServiceSpec(
service_id=primary_database,
role="durable state",
technology=primary_database,
version_hint=resolved_primary_database_version,
container_image=dependency_container_image(
technology=primary_database,
version_hint=resolved_primary_database_version,
allow_inference=allow_inference,
),
learner_managed=False,
)
)
if cache_backend:
services.append(
ProjectRuntimeServiceSpec(
service_id=cache_backend,
role="cache or fast read path",
technology=cache_backend,
version_hint=resolved_cache_backend_version,
container_image=dependency_container_image(
technology=cache_backend,
version_hint=resolved_cache_backend_version,
allow_inference=allow_inference,
),
learner_managed=False,
)
)
for source in data_sources:
if source.kind == DataSourceKind.mock_api:
services.append(
ProjectRuntimeServiceSpec(
service_id=source.id,
role="mock dependency",
technology=source.format or "http",
learner_managed=False,
)
)
elif source.kind == DataSourceKind.object_store:
services.append(
ProjectRuntimeServiceSpec(
service_id=source.id,
role="object storage",
technology=source.format or "blob storage",
learner_managed=False,
)
)
seed_steps: list[ProjectRuntimeCommandSpec] = []
if primary_database:
seed_steps.append(
ProjectRuntimeCommandSpec(
phase="seed",
command=f"Seed {primary_database} with learner-visible baseline data.",
target_service_id=primary_database,
)
)
for source in data_sources:
if source.kind == DataSourceKind.uploaded_file and source.workspace_path:
seed_steps.append(
ProjectRuntimeCommandSpec(
phase="seed",
command=f"Materialize `{source.title}` at `{source.workspace_path}`.",
target_service_id="app",
)
)
notes = [
f"Target the `{family.value}` runtime shape rather than falling back to a generic app contract.",
"Treat runtime plan commands as advisory metadata only; authored runtime protocol files are the execution source of truth.",
]
if tech_stack:
notes.append(
"Honor explicit runtime requirements such as "
+ ", ".join(f"`{item}`" for item in tech_stack[:5])
+ "."
)
return ProjectRuntimePlanSpec(
implementation_language=implementation_language,
language_version=resolved_language_version,
application_framework=application_framework,
framework_version=resolved_framework_version,
package_manager=resolved_package_manager,
services=services,
setup_steps=[],
seed_steps=seed_steps,
verify_steps=[],
run_steps=[],
check_steps=[],
notes=notes,
)
def infer_implementation_stack(
*,
title: str,
problem_statement: str,
implementation_language: str | None,
application_framework: str | None,
tech_stack: list[str] | None,
) -> tuple[str | None, str | None]:
normalized_language = (implementation_language or "").strip().lower() or None
normalized_framework = (application_framework or "").strip().lower() or None
text = " ".join([title, problem_statement, *(tech_stack or [])]).lower()
if normalized_framework is None:
for candidate_framework in sorted(FRAMEWORK_LANGUAGE_HINTS.keys(), key=len, reverse=True):
if _contains_keyword(text, candidate_framework):
normalized_framework = candidate_framework
break
if normalized_framework and not normalized_language:
normalized_language = FRAMEWORK_LANGUAGE_HINTS.get(normalized_framework)
if normalized_language is None:
for candidate_language, keywords in LANGUAGE_KEYWORDS.items():
if _contains_any_keyword(text, keywords):
normalized_language = candidate_language
break
if normalized_framework is None and normalized_language is not None:
normalized_framework = DEFAULT_FRAMEWORK_BY_LANGUAGE.get(normalized_language)
if normalized_language is None and normalized_framework is None:
normalized_language = "python"
normalized_framework = "fastapi"
return normalized_language, normalized_framework
def runtime_commands_for_stack(
*,
implementation_language: str | None,
application_framework: str | None,
) -> tuple[str, str, str]:
_ = (implementation_language, application_framework)
local_run_command = f"sh {RUNTIME_RUN_SCRIPT_PATH}"
visible_check_command = f"sh {RUNTIME_VISIBLE_CHECK_SCRIPT_PATH}"
preview_command = local_run_command
return local_run_command, visible_check_command, preview_command
def infer_package_type(*, text: str, package_type_hint: PackageType | None) -> PackageType:
if package_type_hint is not None:
return package_type_hint
if _contains_any_keyword(
text,
[
"demo to production",
"inherited demo",
"progressive",
"production ready",
"production-ready",
],
):
return PackageType.progressive_codebase_course
if _contains_any_keyword(text, ["course", "catalog", "survey", "multiple assignments"]):
return PackageType.survey_course
return PackageType.progressive_codebase_course
def infer_risk_class(text: str) -> RiskClass:
if _contains_any_keyword(text, HIGH_STAKES_KEYWORDS):
return RiskClass.high_stakes
if _contains_any_keyword(text, REVIEW_REQUIRED_KEYWORDS):
return RiskClass.review_required
return RiskClass.standard
def infer_overlays(text: str) -> list[str]:
overlays: list[str] = []
for overlay_id, keywords in OVERLAY_KEYWORDS.items():
if _contains_any_keyword(text, keywords):
overlays.append(overlay_id)
return overlays
def infer_domain_pack(text: str) -> str | None:
scores: list[tuple[int, str]] = []
for domain_pack_id, keywords in DOMAIN_PACK_KEYWORDS.items():
score = _keyword_match_count(text, keywords)
if score:
scores.append((score, domain_pack_id))
if not scores:
return None
scores.sort(reverse=True)
return scores[0][1]
def build_assignment_design(
*,
package_type: PackageType,
risk_class: RiskClass,
domain_pack: str | None,
overlays: list[str],
project_contract: ProjectContractSpec | None = None,
retrieval_mode: RetrievalMode = RetrievalMode.none,
answer_synthesis_required: bool = False,
citations_required: bool = False,
abstention_required: bool = False,
tool_use_required: bool = False,
traceability_required: bool = True,
durable_state_required: bool = False,
approval_flow_required: bool = False,
execution_surface: ExecutionSurface = ExecutionSurface.http_service,
starter_type: StarterType = StarterType.partial,
implementation_language: str | None = None,
language_version: str | None = None,
application_framework: str | None = None,
framework_version: str | None = None,
package_manager: str | None = None,
primary_database: str | None = None,
primary_database_version: str | None = None,
cache_backend: str | None = None,
cache_backend_version: str | None = None,
tech_stack: list[str] | None = None,
data_sources: list[DataSourceSpec] | None = None,
) -> AssignmentDesignSpec:
resolved_project_contract = project_contract or default_project_contract()