diff --git a/orchestrator.py b/orchestrator.py index 433cb0b..4437ddc 100644 --- a/orchestrator.py +++ b/orchestrator.py @@ -320,12 +320,21 @@ def truncate_output(text, max_len=1000): error = truncate_output(result.stderr.strip()) # ----------------------------------------------- + # --- SHIFT-LEFT: TOKEN ECONOMICS (TRUNCATION) --- + combined_output = output + if error: + combined_output += f"\nSTDERR:\n{error}" + + if len(combined_output) > 8000: + combined_output = ( + combined_output[:8000] + + "\n\n...[SYSTEM WARNING: Output truncated at 8000 characters to save context.]..." + ) + if result.returncode == 0: - return output if output else f"[SUCCESS: {command}]" + return combined_output if combined_output else f"[SUCCESS: {command}]" else: - # If there is stdout alongside the error, include a snippet of it - err_msg = error if error else output - return f"[ERROR: Command execution failed - {err_msg}]" + return f"[ERROR: Command execution failed]\n{combined_output}" except subprocess.TimeoutExpired: return "[ERROR: Command timed out after 60 seconds.]" @@ -410,7 +419,7 @@ def read_directory_contents(dir_path): # Currently restricted to markdown if filename.endswith(".md"): filepath = os.path.join(dir_path, filename) - content += f"\n--- FILE: {filename} ---\n{read_file(filepath)}\n" + content += f'\n\n{read_file(filepath)}\n\n' except FileNotFoundError: pass return content @@ -440,9 +449,8 @@ def assemble_context(agent_name): # --- CONTEXT FUNNELING: Only load active artifacts --- context += "\n\n--- ACTIVE FEATURE ARTIFACTS ---\n" for artifact_path in get_active_artifacts(): - fname = os.path.basename(artifact_path) fcontent = read_file(os.path.join(BASE_DIR, artifact_path)) - context += f"\n--- FILE: {fname} ---\n{fcontent}\n" + context += f'\n\n{fcontent}\n\n' # List existing contracts instead of reading all their contents to save tokens contract_list = list_directory(contracts_dir) diff --git a/tests/evals/test_orchestrator.py b/tests/evals/test_orchestrator.py index 9b407cf..0fef97f 100644 --- a/tests/evals/test_orchestrator.py +++ b/tests/evals/test_orchestrator.py @@ -169,6 +169,7 @@ def test_actual_file_io(tmp_path, monkeypatch) -> None: # Test read_directory_contents dir_res = read_directory_contents(str(docs_dir)) + # SHIFT-LEFT: Updated test to check for the new XML caching tags assert "--- FILE: test.md ---" in dir_res