08_orchestrator_daemon

Orchestrator Daemon

The Orchestrator Daemon

Purpose

The Orchestrator Daemon is a background component that manages the execution of your programs. Once the compiler has written the final code (C++, Python, or TypeScript), the Daemon takes over. It handles the task of actually running that code, providing input, and capturing the results so they can be shown to you.

Why it exists

Running code in different languages requires different tools and commands. For example, C++ needs to be compiled into a binary file before it can run, while Python can run directly. The Daemon exists to hide this complexity. It provides a single, consistent way to execute any program built with ARES, regardless of the language. It handles the "dirty work" of managing temporary files and system processes so you can focus on your results.

How it works

The Daemon follows a strict sequence of steps to run your code safely.

  1. Creating a workspace. It creates a hidden temporary directory (called .ares_tmp) to store the source files it is about to run.
  2. Preparing the source. It writes the generated code into a file in that directory.
  3. Compiling (if needed). If the code is C++, the Daemon calls the g++ compiler to turn the source file into a runnable program.
  4. Spawning a process. It starts the program (or the Python interpreter) as a separate "child" process on your computer.
  5. Piping data. It sends any required input data directly into the program's standard input and listens for any results coming back.
  6. Cleanup. Once the program finishes, the Daemon captures the output, measures how long it took to run, and deletes the temporary files.

Intuition

Think of the Orchestrator Daemon like a stage manager in a theater. The actors (the generated code) have their scripts, but they need someone to tell them when to go on stage, ensure the lights are on, and clean up after the performance is over. You are the director; you provide the script, and the stage manager ensures the performance happens exactly as intended without you needing to step onto the stage yourself.

Implementation details

The core logic for execution is found in src/orchestrator/daemon.ts.

  • Temporary files: It uses the Node.js fs module to manage files and directories.
  • Process management: It uses the child_process module to start and stop programs on your operating system.

complexity

The Daemon adds almost no overhead to your program's execution. Most of its work happens in "constant time," meaning it doesn't get slower as your program gets bigger. The only parts that take time are the compilation of C++ code and the time your program itself takes to run.

Trace example

This is what happens when you run a C++ program with the Daemon:

  1. Write: The system saves your code to .ares_tmp/run.cpp.
  2. Compile: It runs g++ -O2 -o .ares_tmp/run.exe .ares_tmp/run.cpp.
  3. Start: It executes run.exe.
  4. Input: It sends your data into the program.
  5. Finish: It reads the output, deletes run.exe, and gives you the result.

Related entities

  • 6_compiler_and_runtime/01_pipeline.md: Phase 9 of the pipeline is where the Daemon is called.
  • src/orchestrator/daemon.ts: The actual code that manages these processes.
ARES