Recovery algorithm
Agentic Space Factory uses the Factory as an observer and guardrail layer, and Pi as the repair agent.
Roles
Factory
The Factory is responsible for deterministic observation and state management:
- poll the Hugging Face Job and generated Space;
- read
get_space_runtime()and Space logs; - detect terminal build/runtime failures instead of waiting blindly;
- collect raw evidence into artifacts;
- give Pi a complete repair context;
- enforce safe action budgets and cooldowns;
- re-upload/revalidate after Pi changes the workspace.
The Factory should not solve arbitrary dependency or application bugs by hardcoding fixes.
Pi
Pi is responsible for the actual agentic repair work:
- read the HF Spaces gist rules;
- read
INCIDENT_BRIEF.md; - read
DEPENDENCY_ERROR_BRIEF.mdwhen present; - inspect build/runtime logs and the current workspace;
- identify the first actionable error;
- patch the minimal file(s), usually
requirements.txtfor pip failures; - explain the patch in
REPAIR_SUMMARY.md; - let the Factory upload and validate the repaired Space.
Main recovery flow
live validation fails or Space enters BUILD_ERROR/RUNTIME_ERROR
β Factory collects Space runtime + logs
β Factory writes build_error_observation.json when applicable
β Factory writes INCIDENT_BRIEF.md
β Factory asks Pi for diagnosis
β Pi writes REPAIR_DECISION.json
β Factory normalizes unsafe decisions
β if patch_code:
Pi receives REPAIR_BRIEF.md + logs + gist + workspace
Pi patches the existing workspace
Pi writes REPAIR_SUMMARY.md
Factory uploads and validates
β if wait_for_logs / factory_rebuild_same_code:
Factory applies budget/cooldown guardrails
β if manual_hardware / technical_blocker:
UI shows explicit action/blocker
Dependency errors
Dependency errors are not handled by a giant table of hardcoded pins.
The Factory only extracts the first useful pip error and writes DEPENDENCY_ERROR_BRIEF.md, for example:
ResolutionImpossible
Cannot install ...
No matching distribution found ...
metadata-generation-failed
Then Pi must apply the gist method:
read first actionable error
patch once, surgically
rebuild
validate live
Safety guarantees
- Pi receives
HF_TOKEN/HUGGING_FACE_HUB_TOKENinside the Job as secrets. - Token context is recorded only as redacted metadata in
token_context.json. - The Factory blocks same-code rebuild loops with budgets and cooldowns.
- The Factory does not continue waiting when the Space is already in
BUILD_ERROR. - Repair phases are part of the same run and unified in trace artifacts.