codeplain 0.4.3.dev65__py3-none-any.whl → 0.4.3.dev66__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {codeplain-0.4.3.dev65.dist-info → codeplain-0.4.3.dev66.dist-info}/METADATA +1 -1
  2. {codeplain-0.4.3.dev65.dist-info → codeplain-0.4.3.dev66.dist-info}/RECORD +37 -33
  3. codeplain_REST_api.py +12 -0
  4. git_utils.py +66 -0
  5. module_renderer.py +19 -1
  6. partial_rendering.py +34 -10
  7. plain2code.py +18 -4
  8. plain2code_arguments.py +6 -0
  9. plain_modules.py +69 -0
  10. render_machine/actions/commit_conformance_tests_changes.py +7 -3
  11. render_machine/actions/exit_with_error.py +7 -6
  12. render_machine/actions/finish_functional_requirement.py +16 -4
  13. render_machine/actions/fix_unit_tests.py +19 -0
  14. render_machine/actions/prepare_repositories.py +68 -0
  15. render_machine/actions/render_conformance_tests.py +20 -2
  16. render_machine/actions/render_functional_requirement.py +3 -0
  17. render_machine/render_context.py +63 -17
  18. render_machine/render_types.py +4 -1
  19. render_machine/render_utils.py +6 -0
  20. render_machine/state_machine_config.py +2 -4
  21. tests/test_conformance_fix_conversation.py +1 -0
  22. tests/test_conformance_tests_json_order.py +37 -0
  23. tests/test_fix_unit_tests_action.py +35 -0
  24. tests/test_fix_unit_tests_conformance_context.py +1 -0
  25. tests/test_git_utils.py +26 -0
  26. tests/test_partial_rendering.py +3 -0
  27. tests/test_plain_modules.py +92 -0
  28. tests/test_prepare_repositories_layout.py +2 -1
  29. tests/test_regenerate_checks.py +173 -0
  30. tests/test_regenerate_reset.py +167 -0
  31. tests/test_render_context_conformance_regression.py +198 -0
  32. tests/test_render_stop_reason.py +27 -2
  33. tests/test_terminal_attachment.py +1 -0
  34. tui/plain_module_render_choice_tui.py +7 -0
  35. {codeplain-0.4.3.dev65.dist-info → codeplain-0.4.3.dev66.dist-info}/WHEEL +0 -0
  36. {codeplain-0.4.3.dev65.dist-info → codeplain-0.4.3.dev66.dist-info}/entry_points.txt +0 -0
  37. {codeplain-0.4.3.dev65.dist-info → codeplain-0.4.3.dev66.dist-info}/licenses/LICENSE +0 -0
@@ -1,16 +1,28 @@
1
1
  from typing import Any
2
2
 
3
- from render_machine.actions.commit_implementation_code_changes import CommitImplementationCodeChanges
3
+ import git_utils
4
+ from render_machine.actions.base_action import BaseAction
4
5
  from render_machine.render_context import RenderContext
5
6
 
6
7
 
7
- class FinishFunctionalRequirement(CommitImplementationCodeChanges):
8
+ class FinishFunctionalRequirement(BaseAction):
8
9
  SUCCESSFUL_OUTCOME = "functional_requirement_finished"
9
10
 
10
- def execute(self, render_context: RenderContext, previous_action_payload: Any | None):
11
+ def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
11
12
  render_context.plain_module.update_frid_in_module_metadata(render_context.frid_context.frid)
12
13
 
13
- super().execute(render_context, previous_action_payload)
14
+ commit_message = (
15
+ git_utils.FUNCTIONAL_REQUIREMENT_REGENERATED_COMMIT_MESSAGE
16
+ if render_context.is_regenerate
17
+ else git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE
18
+ )
19
+ git_utils.add_all_files_and_commit(
20
+ render_context.build_folder,
21
+ commit_message.format(render_context.frid_context.frid),
22
+ render_context.module_name,
23
+ render_context.frid_context.frid,
24
+ render_context.run_state.render_id,
25
+ )
14
26
 
15
27
  render_context.codeplain_api.finish_functional_requirement(
16
28
  render_context.frid_context.frid,
@@ -246,8 +246,27 @@ class FixUnitTests(BaseAction):
246
246
  )
247
247
  if current:
248
248
  sections.append(f"### Module: {render_context.module_name} (Currently Being Implemented)\n{current[-1]}")
249
+ later = FixUnitTests._functionalities_after_regenerated_one(render_context)
250
+ if later:
251
+ sections.append(
252
+ f"### Module: {render_context.module_name} (Already Implemented, for context)\n" + "\n".join(later)
253
+ )
249
254
  return "\n\n".join(sections)
250
255
 
256
+ @staticmethod
257
+ def _functionalities_after_regenerated_one(render_context: RenderContext) -> list[str]:
258
+ """The specs of the functionalities after the regenerated one, as their code implements them.
259
+
260
+ A regeneration renders one functionality again, and the functionalities after it keep their
261
+ code. Without their specs, the agent can change their code to agree with a wrong unit test.
262
+ """
263
+ if not render_context.is_regenerate:
264
+ return []
265
+ stored = render_context.plain_module.load_module_metadata()["functionalities"]
266
+ stored_frids = [functionality["id"] for functionality in stored]
267
+ position = stored_frids.index(render_context.frid_context.frid)
268
+ return [functionality["markdown"] for functionality in stored[position + 1 :]]
269
+
251
270
  @staticmethod
252
271
  def _read_script(script: str | None) -> str:
253
272
  if not script:
@@ -7,12 +7,17 @@ from memory_management import MemoryManager
7
7
  from plain2code_console import console
8
8
  from render_machine.actions.base_action import BaseAction
9
9
  from render_machine.render_context import RenderContext
10
+ from render_machine.render_types import RenderError
10
11
 
11
12
 
12
13
  class PrepareRepositories(BaseAction):
13
14
  SUCCESSFUL_OUTCOME = "repositories_prepared"
15
+ REGENERATE_NOT_POSSIBLE_OUTCOME = "regenerate_not_possible"
14
16
 
15
17
  def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
18
+ if render_context.is_regenerate:
19
+ return self._prepare_regenerate(render_context)
20
+
16
21
  if render_context.render_range is not None and render_context.render_range[0] != plain_spec.get_first_frid(
17
22
  render_context.plain_source_tree
18
23
  ):
@@ -87,3 +92,66 @@ class PrepareRepositories(BaseAction):
87
92
  )
88
93
 
89
94
  return self.SUCCESSFUL_OUTCOME, None
95
+
96
+ def _regenerate_not_possible(self, render_context: RenderContext, error_message: str):
97
+ # The error goes back as the outcome, so the state machine runs ExitWithError, which logs it.
98
+ # Dispatching the error from inside this action would end the render before that step runs,
99
+ # and the user would see no reason.
100
+ render_context.last_error_message = error_message
101
+ return self.REGENERATE_NOT_POSSIBLE_OUTCOME, RenderError.encode(message=error_message).to_payload()
102
+
103
+ def _prepare_regenerate(self, render_context: RenderContext):
104
+ frid = render_context.render_range[0]
105
+
106
+ if not git_utils.has_commit_for_frid(render_context.build_folder, frid, render_context.module_name):
107
+ return self._regenerate_not_possible(
108
+ render_context,
109
+ f"Cannot regenerate functionality {frid} because it has not been fully rendered yet. "
110
+ f"Please render all functionalities first by running: "
111
+ f"codeplain {render_context.module_name}.plain",
112
+ )
113
+
114
+ render_context.starting_frid = frid
115
+
116
+ module_metadata = render_context.plain_module.load_module_metadata()
117
+ if not module_metadata or "functionalities" not in module_metadata:
118
+ return self._regenerate_not_possible(
119
+ render_context,
120
+ "module_metadata.json is missing or incomplete. Please re-render the module from the beginning.",
121
+ )
122
+
123
+ stored_functionalities = module_metadata["functionalities"]
124
+ old_functionality = next((stored for stored in stored_functionalities if stored["id"] == frid), None)
125
+ if old_functionality is None:
126
+ return self._regenerate_not_possible(
127
+ render_context,
128
+ f"module_metadata.json holds no functionality {frid}. "
129
+ "Please re-render the module from the beginning.",
130
+ )
131
+
132
+ render_context.old_frid_spec = old_functionality["markdown"]
133
+ # The specs the code on disk currently implements (code-truth). Sent to the API so the
134
+ # regeneration prompt shows the old spec for any changed-but-not-yet-regenerated FR,
135
+ # keeping specs and code in sync.
136
+ render_context.code_functional_requirements = [stored["markdown"] for stored in stored_functionalities]
137
+
138
+ # An interrupted regeneration leaves its commits and changes on top of the last finished
139
+ # functionality. Remove them, so that this regeneration starts from finished code and tests.
140
+ git_utils.revert_to_last_finished_commit(render_context.build_folder, render_context.module_name)
141
+ if render_context.render_conformance_tests:
142
+ git_utils.revert_to_last_finished_commit(
143
+ render_context.conformance_tests.get_module_conformance_tests_folder(render_context.module_name),
144
+ render_context.module_name,
145
+ )
146
+ conformance_tests_json = render_context.conformance_tests.get_conformance_tests_json(
147
+ render_context.module_name
148
+ )
149
+ if frid in conformance_tests_json:
150
+ old_folder = conformance_tests_json[frid]["folder_name"]
151
+ file_utils.delete_folder(old_folder)
152
+ del conformance_tests_json[frid]
153
+ render_context.conformance_tests.dump_conformance_tests_json(
154
+ render_context.module_name, conformance_tests_json
155
+ )
156
+
157
+ return self.SUCCESSFUL_OUTCOME, None
@@ -75,14 +75,18 @@ class RenderConformanceTests(BaseAction):
75
75
 
76
76
  console.debug(f"Storing conformance test files in subfolder {conformance_tests_folder_name}/")
77
77
 
78
- render_context.conformance_tests_running_context.get_conformance_tests_json(
78
+ conformance_tests_json = render_context.conformance_tests_running_context.get_conformance_tests_json(
79
79
  render_context.conformance_tests_running_context.current_testing_module_name
80
- )[render_context.conformance_tests_running_context.current_testing_frid] = {
80
+ )
81
+ conformance_tests_json[render_context.conformance_tests_running_context.current_testing_frid] = {
81
82
  "folder_name": conformance_tests_folder_name,
82
83
  "functional_requirement": render_context.frid_context.specifications[
83
84
  plain_spec.FUNCTIONAL_REQUIREMENTS
84
85
  ][-1],
85
86
  }
87
+ RenderConformanceTests._keep_specification_order(
88
+ conformance_tests_json, list(plain_spec.get_frids(render_context.plain_source_tree))
89
+ )
86
90
  else:
87
91
  conformance_tests_folder_name = (
88
92
  render_context.conformance_tests_running_context.get_current_conformance_test_folder_name()
@@ -125,6 +129,7 @@ class RenderConformanceTests(BaseAction):
125
129
  render_context.conformance_tests_running_context.current_testing_module_name
126
130
  ),
127
131
  run_state=render_context.run_state,
132
+ is_regeneration=render_context.is_regenerate,
128
133
  )
129
134
 
130
135
  self._warn_about_untraced_scenarios(render_context, traceability)
@@ -140,6 +145,19 @@ class RenderConformanceTests(BaseAction):
140
145
 
141
146
  return self.SUCCESSFUL_OUTCOME, None
142
147
 
148
+ @staticmethod
149
+ def _keep_specification_order(conformance_tests_json: dict, frids: list[str]) -> None:
150
+ """Put the entries back in the order of the functionalities in the specification.
151
+
152
+ A regeneration, or conformance tests written again, removes the entry and adds it at the end.
153
+ A module that requires this one runs these tests in the order of the entries.
154
+ """
155
+ ordered = sorted(
156
+ conformance_tests_json.items(), key=lambda item: frids.index(item[0]) if item[0] in frids else len(frids)
157
+ )
158
+ conformance_tests_json.clear()
159
+ conformance_tests_json.update(ordered)
160
+
143
161
  @staticmethod
144
162
  def _warn_about_untraced_scenarios(render_context: RenderContext, traceability: dict) -> None:
145
163
  """Warn about a scenario with no test, a test that implements no scenario, and a scenario
@@ -78,6 +78,9 @@ class RenderFunctionalRequirement(BaseAction):
78
78
  render_context.get_required_modules_functionalities(),
79
79
  render_context.should_run_unit_tests(),
80
80
  render_context.run_state,
81
+ is_regeneration=render_context.is_regenerate,
82
+ old_functional_requirement_text=render_context.old_frid_spec,
83
+ code_functional_requirements=render_context.code_functional_requirements,
81
84
  )
82
85
  except FunctionalRequirementTooComplex as e:
83
86
  error_message = f"The functionality:\n{render_context.frid_context.functional_requirement_text}\n is too complex to be implemented. Please break down the functionality into smaller parts."
@@ -50,6 +50,7 @@ class RenderContext:
50
50
  test_script_timeout: Optional[int] = None,
51
51
  stop_event: Optional[threading.Event] = None,
52
52
  enter_pause_event: Optional[threading.Event] = None,
53
+ is_regenerate: bool = False,
53
54
  ):
54
55
  self.codeplain_api: CodeplainAPI = codeplain_api
55
56
  self.memory_manager = memory_manager
@@ -73,6 +74,9 @@ class RenderContext:
73
74
  self.event_bus = event_bus
74
75
  self.stop_event = stop_event
75
76
  self.enter_pause_event = enter_pause_event
77
+ self.is_regenerate = is_regenerate
78
+ self.old_frid_spec: str | None = None
79
+ self.code_functional_requirements: list[str] | None = None
76
80
  self.script_execution_history = ScriptExecutionHistory()
77
81
  # A preparation script run started while the conformance tests are generated (see PrepareTestingEnvironment).
78
82
  self.background_preparation: Any = None
@@ -89,7 +93,7 @@ class RenderContext:
89
93
  self.frid_context: Optional[FridContext] = None
90
94
  self.unit_tests_running_context: Optional[UnitTestsRunningContext] = None
91
95
  self.conformance_tests_running_context: Optional[ConformanceTestsRunningContext] = None
92
- # Constants that should remain for a single frid, but possible over multiple rerenderings of the same frid
96
+ # Constants that should remain for a single frid, but possible over multiple regenerations of the same frid
93
97
  self.functional_requirements_render_attempts_failed_unit_during_conformance_tests = 0
94
98
 
95
99
  # Initialize conformance tests utilities. The resolver lets a required module that ships
@@ -448,11 +452,13 @@ class RenderContext:
448
452
  def _has_reached_implementation_frid(self) -> bool:
449
453
  """Check if regression has reached the FRID being implemented."""
450
454
  ctx = self.conformance_tests_running_context
451
- return (
455
+ if not (
452
456
  ctx.execution_phase == TestExecutionPhase.RUNNING_REGRESSION
453
457
  and ctx.current_testing_module_name == self.module_name
454
- and (ctx.current_testing_frid is None or ctx.current_testing_frid == ctx.frid_being_implemented)
455
- )
458
+ ):
459
+ return False
460
+
461
+ return ctx.current_testing_frid is None or ctx.current_testing_frid == ctx.frid_being_implemented
456
462
 
457
463
  def _setup_test_specifications(self):
458
464
  """Load specifications for the current test."""
@@ -574,25 +580,65 @@ class RenderContext:
574
580
  # Get reference to the updated context
575
581
  ctx = self.conformance_tests_running_context
576
582
 
583
+ if (
584
+ self.is_regenerate
585
+ and ctx.current_testing_module_name == self.module_name
586
+ and ctx.current_testing_frid == ctx.frid_being_implemented
587
+ ):
588
+ # The regenerated functionality was already tested in the initial phase, so move past it.
589
+ # The functionalities after it keep their code, so the run continues to the end of the spec
590
+ # file. Past the last functionality the frid is None, and the run ends below.
591
+ self.conformance_tests_running_context = self._get_next_test_to_run()
592
+ ctx = self.conformance_tests_running_context
593
+
594
+ if ctx.current_testing_frid is None or not ctx.current_conformance_tests_exist():
595
+ self._end_regression_run()
596
+ return
597
+
577
598
  # Set up specs and run test
578
599
  self._setup_test_specifications()
579
600
 
580
- if ctx.current_conformance_tests_exist():
581
- # Check if this is the implementation FRID (last test to run)
582
- if self._has_reached_implementation_frid():
583
- # Reached implementation FRID - only re-run it if code changed during regression
584
- if ctx.code_changed_during_regression:
585
- # Code changed - run the implementation FRID again to verify no regression
586
- # After it passes, mark as completed on next iteration
587
- ctx.execution_phase = TestExecutionPhase.COMPLETED
588
- else:
589
- # No code changes - skip re-running implementation FRID, mark as completed immediately
590
- ctx.execution_phase = TestExecutionPhase.COMPLETED
591
- self.machine.dispatch(triggers.MARK_ALL_CONFORMANCE_TESTS_PASSED)
592
- return
601
+ # Check if this is the implementation FRID (last test to run)
602
+ if not self.is_regenerate and self._has_reached_implementation_frid():
603
+ # Reached implementation FRID - only re-run it if code changed during regression
604
+ if ctx.code_changed_during_regression:
605
+ # Code changed - run the implementation FRID again to verify no regression
606
+ # After it passes, mark as completed on next iteration
607
+ ctx.execution_phase = TestExecutionPhase.COMPLETED
608
+ else:
609
+ # No code changes - skip re-running implementation FRID, mark as completed immediately
610
+ ctx.execution_phase = TestExecutionPhase.COMPLETED
611
+ self.machine.dispatch(triggers.MARK_ALL_CONFORMANCE_TESTS_PASSED)
612
+ return
613
+
614
+ self.machine.dispatch(triggers.MARK_CONFORMANCE_TESTS_READY)
615
+
616
+ def _end_regression_run(self):
617
+ """End the regression run when the next functionality has no conformance tests, or when a
618
+ regeneration has walked past the last functionality.
619
+
620
+ The functionalities of this module are walked through the spec file, so the walk can reach a
621
+ functionality that has none. A project that was rendered only in part has such
622
+ functionalities. Nothing is left to run then, and the run must not end without a trigger:
623
+ the state machine would stay in the same state and generate conformance tests for that
624
+ functionality.
593
625
 
626
+ The running context goes back to the functionality being implemented, because the
627
+ postprocessing steps look their conformance test folder up by it.
628
+ """
629
+ ctx = self.conformance_tests_running_context
630
+ ctx.current_testing_module_name = self.module_name
631
+ ctx.current_testing_frid = ctx.frid_being_implemented
632
+ self._setup_test_specifications()
633
+ ctx.execution_phase = TestExecutionPhase.COMPLETED
634
+
635
+ if ctx.code_changed_during_regression:
636
+ # Code changed during the run, so test the functionality being implemented once more.
594
637
  self.machine.dispatch(triggers.MARK_CONFORMANCE_TESTS_READY)
638
+ else:
639
+ self.machine.dispatch(triggers.MARK_ALL_CONFORMANCE_TESTS_PASSED)
595
640
 
641
+ # TODO why is this in the context and not part of the state machine (in an action)
596
642
  # ========== Main Conformance Test Orchestration ==========
597
643
 
598
644
  def start_conformance_tests_for_frid(self):
@@ -236,7 +236,10 @@ class ConformanceTestsRunningContext:
236
236
  if plain_spec.ACCEPTANCE_TESTS not in self.current_testing_frid_specifications:
237
237
  return None
238
238
  acceptance_tests = self.current_testing_frid_specifications[plain_spec.ACCEPTANCE_TESTS]
239
- if not acceptance_tests or self.acceptance_tests_completed == 0:
239
+ # The counter belongs to the functionality being implemented. The specifications can belong
240
+ # to another functionality, which can have fewer acceptance tests, so the counter alone is
241
+ # not a valid position in this list.
242
+ if self.acceptance_tests_completed == 0 or self.acceptance_tests_completed > len(acceptance_tests):
240
243
  return None
241
244
  return acceptance_tests[self.acceptance_tests_completed - 1]
242
245
 
@@ -12,6 +12,7 @@ if sys.platform == "linux":
12
12
  import fcntl
13
13
 
14
14
  import file_utils
15
+ import git_utils
15
16
  import plain_spec
16
17
  from plain2code_console import MUTED_COLOR, RETRY_COLOR, SUCCESS_COLOR, console
17
18
  from plain2code_exceptions import RenderCancelledError
@@ -36,6 +37,11 @@ PIPE_SIZE_KB = 1024 # 1MB
36
37
 
37
38
 
38
39
  def revert_changes_for_frid(render_context):
40
+ if render_context.is_regenerate:
41
+ # The functionalities after the regenerated one stay implemented, so a reset to the previous
42
+ # functionality would remove their code. Only the changes of this regeneration are removed.
43
+ git_utils.revert_to_last_finished_commit(render_context.build_folder, render_context.module_name)
44
+ return
39
45
  if render_context.frid_context.frid is not None:
40
46
  previous_frid = plain_spec.get_previous_frid(render_context.plain_source_tree, render_context.frid_context.frid)
41
47
  render_context.plain_module.revert_code_to_frid(previous_frid)
@@ -57,12 +57,9 @@ class StateMachineConfig:
57
57
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.POSTPROCESSING_CONFORMANCE_TESTS.value}_{States.CONFORMANCE_TESTS_READY_FOR_SUMMARY.value}": SummarizeConformanceTests(),
58
58
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.POSTPROCESSING_CONFORMANCE_TESTS.value}_{States.CONFORMANCE_TESTS_READY_FOR_COMMIT.value}": CommitConformanceTestsChanges(
59
59
  git_utils.CONFORMANCE_TESTS_PASSED_COMMIT_MESSAGE,
60
- git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE,
61
60
  ),
62
61
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.POSTPROCESSING_CONFORMANCE_TESTS.value}_{States.CONFORMANCE_TESTS_READY_FOR_AMBIGUITY_ANALYSIS.value}": AnalyzeSpecificationAmbiguity(),
63
- f"{States.IMPLEMENTING_FRID.value}_{States.FRID_FULLY_IMPLEMENTED.value}": FinishFunctionalRequirement(
64
- git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE
65
- ),
62
+ f"{States.IMPLEMENTING_FRID.value}_{States.FRID_FULLY_IMPLEMENTED.value}": FinishFunctionalRequirement(),
66
63
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.PROCESSING_UNIT_TESTS.value}_{States.UNIT_TESTS_READY.value}": RunUnitTests(),
67
64
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.PROCESSING_UNIT_TESTS.value}_{States.UNIT_TESTS_FAILED.value}": FixUnitTests(),
68
65
  States.RENDER_COMPLETED.value: CreateDist(),
@@ -73,6 +70,7 @@ class StateMachineConfig:
73
70
  """Get the mapping of action outcomes to state machine triggers."""
74
71
  return {
75
72
  PrepareRepositories.SUCCESSFUL_OUTCOME: triggers.START_RENDER,
73
+ PrepareRepositories.REGENERATE_NOT_POSSIBLE_OUTCOME: triggers.HANDLE_ERROR,
76
74
  RenderFunctionalRequirement.SUCCESSFUL_OUTCOME: triggers.RENDER_FUNCTIONAL_REQUIREMENT,
77
75
  RenderFunctionalRequirement.FUNCTIONAL_REQUIREMENT_TOO_COMPLEX_OUTCOME: triggers.HANDLE_ERROR,
78
76
  RenderFunctionalRequirement.ITERATION_LIMIT_EXCEEDED_OUTCOME: triggers.HANDLE_ERROR,
@@ -137,6 +137,7 @@ def make_render_context(project, api, testing_frid="1", frid="1", testing_module
137
137
  conformance_tests_running_context=ctx,
138
138
  plain_source_tree={},
139
139
  module_name="mod",
140
+ is_regenerate=False,
140
141
  required_modules=[],
141
142
  frid_context=SimpleNamespace(frid=frid, linked_resources={}, changed_files={"app.py"}),
142
143
  get_required_modules_functionalities=lambda: {},
@@ -0,0 +1,37 @@
1
+ """The entries of conformance_tests.json follow the order of the functionalities in the specification.
2
+
3
+ A regeneration removes the entry of the functionality and writes it again, so without this the entry
4
+ moves to the end. A module that requires this one runs these tests in the order of the entries.
5
+ """
6
+
7
+ from render_machine.actions.render_conformance_tests import RenderConformanceTests
8
+
9
+ FRIDS = ["run-cli", "add-command", "list-command", "report-command"]
10
+
11
+
12
+ def test_a_rewritten_entry_goes_back_to_its_position():
13
+ conformance_tests_json = {"run-cli": 1, "list-command": 3, "report-command": 4, "add-command": 2}
14
+
15
+ RenderConformanceTests._keep_specification_order(conformance_tests_json, FRIDS)
16
+
17
+ assert list(conformance_tests_json) == FRIDS
18
+
19
+
20
+ def test_the_same_object_is_reordered():
21
+ """The running context holds this dict, so it must be reordered in place."""
22
+ conformance_tests_json = {"list-command": 3, "run-cli": 1}
23
+ same_object = conformance_tests_json
24
+
25
+ RenderConformanceTests._keep_specification_order(conformance_tests_json, FRIDS)
26
+
27
+ assert same_object is conformance_tests_json
28
+ assert list(same_object) == ["run-cli", "list-command"]
29
+ assert same_object["list-command"] == 3
30
+
31
+
32
+ def test_an_entry_the_specification_no_longer_has_goes_last():
33
+ conformance_tests_json = {"removed-command": 9, "add-command": 2, "run-cli": 1}
34
+
35
+ RenderConformanceTests._keep_specification_order(conformance_tests_json, FRIDS)
36
+
37
+ assert list(conformance_tests_json) == ["run-cli", "add-command", "removed-command"]
@@ -63,6 +63,7 @@ def render_context(tmp_path, monkeypatch):
63
63
  codeplain_api=None,
64
64
  build_folder=str(build),
65
65
  module_name="m",
66
+ is_regenerate=False,
66
67
  run_state=object(),
67
68
  plain_source_tree=plain_source_tree,
68
69
  unittests_script=None,
@@ -244,3 +245,37 @@ def test_other_http_errors_on_continue_are_not_swallowed(render_context):
244
245
  render_context.codeplain_api = FakeAPI([requests.exceptions.HTTPError(response=SimpleNamespace(status_code=500))])
245
246
  with pytest.raises(requests.exceptions.HTTPError):
246
247
  FixUnitTests().execute(render_context, {"previous_unittests_issue": "FAILED"})
248
+
249
+
250
+ def test_a_regeneration_shows_the_functionalities_after_the_regenerated_one(render_context, monkeypatch):
251
+ """The functionalities after the regenerated one keep their code. The agent must see their specs, as
252
+ their code implements them, or it can change that code to agree with a wrong unit test."""
253
+ render_context.is_regenerate = True
254
+ plain_module = SimpleNamespace(
255
+ load_module_metadata=lambda: {
256
+ "functionalities": [
257
+ {"id": "1", "markdown": "- Old feature."},
258
+ {"id": "2", "markdown": "- New feature, as it was before."},
259
+ {"id": "3", "markdown": "- Later feature."},
260
+ {"id": "4", "markdown": "- Last feature."},
261
+ ]
262
+ }
263
+ )
264
+ monkeypatch.setattr(FakeRenderContext, "plain_module", property(lambda self: plain_module))
265
+ specifications = plain_spec.get_specifications_for_frid(render_context.plain_source_tree, "2")[0]
266
+
267
+ section = FixUnitTests._functional_requirements_section(render_context, specifications)
268
+
269
+ assert section.endswith(
270
+ "### Module: m (Currently Being Implemented)\n- New feature.\n\n"
271
+ "### Module: m (Already Implemented, for context)\n- Later feature.\n- Last feature."
272
+ )
273
+ assert "as it was before" not in section
274
+
275
+
276
+ def test_a_normal_render_shows_no_functionality_after_the_current_one(render_context):
277
+ specifications = plain_spec.get_specifications_for_frid(render_context.plain_source_tree, "2")[0]
278
+
279
+ section = FixUnitTests._functional_requirements_section(render_context, specifications)
280
+
281
+ assert section.endswith("### Module: m (Currently Being Implemented)\n- New feature.")
@@ -112,6 +112,7 @@ def make_render_context(api, build_folder, memory_folder, conformance_tests_runn
112
112
  unit_tests_running_context=UnitTestsRunningContext(fix_attempts=1),
113
113
  plain_source_tree={},
114
114
  module_name="mod",
115
+ is_regenerate=False,
115
116
  required_modules=None,
116
117
  frid_context=SimpleNamespace(frid="1", linked_resources={}, changed_files=set()),
117
118
  get_required_modules_functionalities=lambda: {},
tests/test_git_utils.py CHANGED
@@ -11,6 +11,7 @@ from git_utils import (
11
11
  CONFORMANCE_TESTS_PASSED_COMMIT_MESSAGE,
12
12
  FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE,
13
13
  FUNCTIONAL_REQUIREMENT_IMPLEMENTED_COMMIT_MESSAGE,
14
+ FUNCTIONAL_REQUIREMENT_REGENERATED_COMMIT_MESSAGE,
14
15
  REFACTORED_CODE_COMMIT_MESSAGE,
15
16
  add_all_files_and_commit,
16
17
  diff,
@@ -498,6 +499,31 @@ def test_get_last_finished_frid_ignores_non_finished_commits(empty_repo):
498
499
  assert get_last_rendered_functionality(empty_repo) == ("module_a", "add-task")
499
500
 
500
501
 
502
+ def test_get_last_finished_frid_ignores_regenerated_commits(empty_repo):
503
+ """A regenerated commit sits on top of the frontier and must not become the last rendered frid."""
504
+ file_path = Path(empty_repo) / "a.txt"
505
+ for frid in ("1", "2", "3"):
506
+ file_path.write_text(f"v{frid}")
507
+ add_all_files_and_commit(
508
+ empty_repo,
509
+ FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE.format(frid),
510
+ module_name="module_a",
511
+ frid=frid,
512
+ )
513
+
514
+ # Functionality 1 is rendered again in place, so its commit is the most recent one.
515
+ file_path.write_text("v1 again")
516
+ add_all_files_and_commit(
517
+ empty_repo,
518
+ FUNCTIONAL_REQUIREMENT_REGENERATED_COMMIT_MESSAGE.format("1"),
519
+ module_name="module_a",
520
+ frid="1",
521
+ )
522
+
523
+ # The module is still rendered up to functionality 3.
524
+ assert get_last_rendered_functionality(empty_repo) == ("module_a", "3")
525
+
526
+
501
527
  def test_get_last_finished_frid_without_module_name(empty_repo):
502
528
  """Raise InvalidGitRepositoryError when the finished commit omits the module name line."""
503
529
  file_path = Path(empty_repo) / "a.txt"
@@ -59,6 +59,9 @@ class FakeModule:
59
59
  self._next_frid = next_frid
60
60
  self.plain_source = _tree(*frids)
61
61
 
62
+ def get_regenerated_frid_removed_by_render_from(self, start_frid):
63
+ return None
64
+
62
65
  @property
63
66
  def all_required_modules(self):
64
67
  result = []
@@ -60,6 +60,11 @@ def _stored(*frids: str) -> list[dict[str, str]]:
60
60
  return [{"id": frid, "markdown": f"{frid} markdown."} for frid in frids]
61
61
 
62
62
 
63
+ def _stale(*frids: str) -> list[dict[str, str]]:
64
+ """Metadata entries holding an older markdown than the one the spec file now carries."""
65
+ return [{"id": frid, "markdown": f"old {frid}."} for frid in frids]
66
+
67
+
63
68
  def _write_metadata(module: PlainModule, metadata: dict) -> None:
64
69
  folder = module.get_codeplain_folder()
65
70
  os.makedirs(folder, exist_ok=True)
@@ -494,6 +499,93 @@ def test_code_variable_frid_not_flagged_as_change(code_var_module):
494
499
  assert determine_partial_render_start(code_var_module) is None
495
500
 
496
501
 
502
+ # --------------------------------------------------------------------------
503
+ # update_frid_in_module_metadata — regenerate of a single functionality
504
+ # --------------------------------------------------------------------------
505
+
506
+
507
+ def test_save_module_metadata_restates_every_functionality(solo_module):
508
+ """save_module_metadata records the whole module against the current spec. That is why the
509
+ regenerate path must not call it: it would restate the functionalities that were not rendered."""
510
+ _write_metadata(solo_module, {"functionalities": ["old one", "old two", "old three"]})
511
+
512
+ solo_module.save_module_metadata()
513
+
514
+ metadata = solo_module.load_module_metadata()
515
+ assert metadata["functionalities"] == solo_module._get_module_functional_requirements()
516
+ assert metadata["source_hash"] == solo_module.get_module_source_hash()
517
+
518
+
519
+ def test_update_frid_leaves_other_functionalities_untouched(solo_module):
520
+ """Regenerating one functionality must not restate the spec of the others. Their code was not
521
+ regenerated, so their metadata entries have to keep the spec the code implements."""
522
+ current = solo_module._get_module_functional_requirements()
523
+ stale = _stale("solo-one", "solo-two", "solo-three")
524
+ _write_metadata(solo_module, {"functionalities": stale})
525
+
526
+ solo_module.update_frid_in_module_metadata("solo-two")
527
+
528
+ metadata = solo_module.load_module_metadata()
529
+ assert metadata["functionalities"] == [stale[0], current[1], stale[2]]
530
+
531
+
532
+ def test_update_frid_keeps_source_hash_while_another_frid_differs(solo_module):
533
+ """source_hash claims the code implements the whole spec, so it must stay stale while the other
534
+ functionalities still hold their old spec."""
535
+ _write_metadata(
536
+ solo_module,
537
+ {
538
+ "source_hash": "stale",
539
+ "non_functional_source_hash": solo_module.get_module_non_functional_source_hash(),
540
+ "functionalities": _stale("solo-one", "solo-two", "solo-three"),
541
+ },
542
+ )
543
+
544
+ solo_module.update_frid_in_module_metadata("solo-two")
545
+
546
+ assert solo_module.load_module_metadata()["source_hash"] == "stale"
547
+
548
+
549
+ def test_update_frid_refreshes_source_hash_when_module_matches_spec(solo_module):
550
+ """Once the last outstanding functionality is regenerated, the module matches the spec again and
551
+ source_hash is stamped, so no further change is reported."""
552
+ current = solo_module._get_module_functional_requirements()
553
+ _write_metadata(
554
+ solo_module,
555
+ {
556
+ "source_hash": "stale",
557
+ "non_functional_source_hash": solo_module.get_module_non_functional_source_hash(),
558
+ "functionalities": [current[0], _stale("solo-two")[0], current[2]],
559
+ },
560
+ )
561
+
562
+ solo_module.update_frid_in_module_metadata("solo-two")
563
+
564
+ metadata = solo_module.load_module_metadata()
565
+ assert metadata["functionalities"] == current
566
+ assert metadata["source_hash"] == solo_module.get_module_source_hash()
567
+
568
+
569
+ def test_update_frid_keeps_source_hash_when_non_functional_content_changed(solo_module):
570
+ """Every functionality matches, but a definition or requirement changed. The code of the other
571
+ functionalities was generated against the old text, so the module is not in sync."""
572
+ current = solo_module._get_module_functional_requirements()
573
+ _write_metadata(
574
+ solo_module,
575
+ {
576
+ "source_hash": "stale",
577
+ "non_functional_source_hash": "different",
578
+ "functionalities": [current[0], _stale("solo-two")[0], current[2]],
579
+ },
580
+ )
581
+
582
+ solo_module.update_frid_in_module_metadata("solo-two")
583
+
584
+ metadata = solo_module.load_module_metadata()
585
+ assert metadata["functionalities"] == current
586
+ assert metadata["source_hash"] == "stale"
587
+
588
+
497
589
  # --------------------------------------------------------------------------
498
590
  # module folder layout
499
591
  # --------------------------------------------------------------------------