-
Notifications
You must be signed in to change notification settings - Fork 0
feat(learning): harden rebuild and recovery semantics #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(learning): harden rebuild recovery semantics
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
aceclaw-daemon/src/main/java/dev/aceclaw/daemon/LearningMaintenanceRecoveryStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package dev.aceclaw.daemon; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.time.Instant; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Persists per-project maintenance recovery state so interrupted runs can be retried safely. | ||
| */ | ||
| public final class LearningMaintenanceRecoveryStore { | ||
|
|
||
| private static final String FILE_NAME = ".aceclaw/metrics/learning-maintenance-state.json"; | ||
|
|
||
| private final ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); | ||
|
|
||
| public RecoveryState markStarted(Path projectRoot, String workspaceHash, String trigger) throws IOException { | ||
| Objects.requireNonNull(projectRoot, "projectRoot"); | ||
| Objects.requireNonNull(workspaceHash, "workspaceHash"); | ||
| Objects.requireNonNull(trigger, "trigger"); | ||
| if (workspaceHash.isBlank() || trigger.isBlank()) { | ||
| throw new IllegalArgumentException("workspaceHash and trigger must not be blank"); | ||
| } | ||
| var previous = load(projectRoot).orElse(null); | ||
| int attempt = previous == null ? 1 : previous.attempt() + 1; | ||
| Instant now = Instant.now(); | ||
| var next = new RecoveryState( | ||
| workspaceHash, | ||
| projectRoot.toAbsolutePath().normalize().toString(), | ||
| trigger, | ||
| RecoveryStatus.RUNNING, | ||
| attempt, | ||
| previous == null ? now : previous.firstStartedAt(), | ||
| now, | ||
| ""); | ||
| write(projectRoot, next); | ||
| return next; | ||
| } | ||
|
|
||
| public void markFailed(Path projectRoot, String workspaceHash, String trigger, Exception error) throws IOException { | ||
| Objects.requireNonNull(error, "error"); | ||
| var current = load(projectRoot).orElse(null); | ||
| Instant now = Instant.now(); | ||
| var next = new RecoveryState( | ||
| workspaceHash, | ||
| projectRoot.toAbsolutePath().normalize().toString(), | ||
| trigger, | ||
| RecoveryStatus.FAILED, | ||
| current == null ? 1 : current.attempt(), | ||
| current == null ? now : current.firstStartedAt(), | ||
| now, | ||
| truncate(error.getClass().getSimpleName() + ": " + String.valueOf(error.getMessage()), 300)); | ||
| write(projectRoot, next); | ||
| } | ||
|
|
||
| public void clear(Path projectRoot) throws IOException { | ||
| Objects.requireNonNull(projectRoot, "projectRoot"); | ||
| Files.deleteIfExists(stateFile(projectRoot)); | ||
| } | ||
|
|
||
| public Optional<RecoveryState> load(Path projectRoot) throws IOException { | ||
| Objects.requireNonNull(projectRoot, "projectRoot"); | ||
| Path file = stateFile(projectRoot); | ||
| if (!Files.isRegularFile(file)) { | ||
| return Optional.empty(); | ||
| } | ||
| try { | ||
| var json = Files.readString(file); | ||
| if (json == null || json.isBlank()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(mapper.readValue(json, RecoveryState.class)); | ||
| } catch (IOException | RuntimeException e) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| public boolean needsRecovery(Path projectRoot, String workspaceHash) { | ||
| Objects.requireNonNull(projectRoot, "projectRoot"); | ||
| Objects.requireNonNull(workspaceHash, "workspaceHash"); | ||
| if (workspaceHash.isBlank()) { | ||
| throw new IllegalArgumentException("workspaceHash must not be blank"); | ||
| } | ||
| try { | ||
| return load(projectRoot) | ||
| .filter(state -> workspaceHash.equals(state.workspaceHash())) | ||
| .filter(state -> state.status() == RecoveryStatus.RUNNING || state.status() == RecoveryStatus.FAILED) | ||
| .isPresent(); | ||
| } catch (IOException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| Path stateFile(Path projectRoot) { | ||
| return projectRoot.resolve(FILE_NAME); | ||
| } | ||
|
|
||
| private void write(Path projectRoot, RecoveryState state) throws IOException { | ||
| Path file = stateFile(projectRoot); | ||
| Files.createDirectories(file.getParent()); | ||
| Path tmp = file.resolveSibling(file.getFileName() + ".tmp"); | ||
| try { | ||
| Files.writeString(tmp, mapper.writeValueAsString(state), | ||
| StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); | ||
| Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); | ||
| } catch (IOException e) { | ||
| try { | ||
| Files.deleteIfExists(tmp); | ||
| } catch (IOException ignored) { | ||
| // best effort cleanup | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private static String truncate(String value, int max) { | ||
| if (value == null || value.length() <= max) { | ||
| return value == null ? "" : value; | ||
| } | ||
| return value.substring(0, max); | ||
| } | ||
|
|
||
| public record RecoveryState( | ||
| String workspaceHash, | ||
| String projectPath, | ||
| String trigger, | ||
| RecoveryStatus status, | ||
| int attempt, | ||
| Instant firstStartedAt, | ||
| Instant updatedAt, | ||
| String lastError | ||
| ) { | ||
| public RecoveryState { | ||
| workspaceHash = Objects.requireNonNull(workspaceHash, "workspaceHash"); | ||
| projectPath = projectPath == null ? "" : projectPath; | ||
| trigger = Objects.requireNonNull(trigger, "trigger"); | ||
| status = Objects.requireNonNull(status, "status"); | ||
| firstStartedAt = firstStartedAt == null ? Instant.now() : firstStartedAt; | ||
| updatedAt = updatedAt == null ? Instant.now() : updatedAt; | ||
| lastError = lastError == null ? "" : lastError; | ||
| if (workspaceHash.isBlank() || trigger.isBlank()) { | ||
| throw new IllegalArgumentException("workspaceHash and trigger must not be blank"); | ||
| } | ||
| if (attempt <= 0) { | ||
| throw new IllegalArgumentException("attempt must be positive"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public enum RecoveryStatus { | ||
| RUNNING, | ||
| FAILED | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.