refactor(action): support recover schedulable executable actions together, and ActionExecutor will emit a context block to explain recovering result

This commit is contained in:
2026-04-08 16:52:21 +08:00
parent e04b2c4fe8
commit 2935daeffa
2 changed files with 67 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class ActionExecutor extends AbstractAgentModule.Standalone {
@@ -49,8 +50,14 @@ public class ActionExecutor extends AbstractAgentModule.Standalone {
runnerClient = actionCapability.runnerClient();
blockManager = new ExecutingActionBlockManager(cognitionCapability.contextWorkspace());
actionCapability.listActions(Action.Status.EXECUTING, null)
.forEach(this::execute);
Set<ExecutableAction> recoveredActions = new HashSet<>();
recoveredActions.addAll(actionCapability.listActions(Action.Status.EXECUTING, null));
recoveredActions.addAll(actionCapability.listActions(Action.Status.INTERRUPTED, null).stream().map(executableAction -> {
executableAction.setStatus(Action.Status.EXECUTING);
return executableAction;
}).collect(Collectors.toSet()));
recoveredActions.forEach(this::execute);
blockManager.emitActionRecoveredBlock(recoveredActions);
}
public void execute(Action action) {

View File

@@ -28,6 +28,64 @@ class ExecutingActionBlockManager {
this.contextWorkspace = contextWorkspace;
}
void emitActionRecoveredBlock(Set<ExecutableAction> recoveredActions) {
Set<ExecutableActionSnapshot> snapshots = recoveredActions.stream().map(ExecutableAction::snapshot).collect(Collectors.toSet());
String blockName = "actions_recovered";
String emittedAt = emittedAt();
String event = "actions_recovered";
contextWorkspace.register(new ContextBlock(
buildExecutingActionRecoveredFullBlock(snapshots, blockName, emittedAt, event),
buildExecutingActionRecoveredCompactBlock(snapshots, blockName, emittedAt, event),
buildExecutingActionRecoveredAbstractBlock(snapshots, blockName, event),
Set.of(ContextBlock.VisibleDomain.ACTION),
100,
12,
1
));
}
private @NotNull BlockContent buildExecutingActionRecoveredAbstractBlock(Set<ExecutableActionSnapshot> recoveredExecutingActions, String blockName, String event) {
return new ActionBlockContent(blockName, SOURCE) {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendEventElement(document, root, event);
appendTextElement(document, root, "abstract", recoveredExecutingActions.size() + " executing actions recovered.");
}
};
}
private @NotNull BlockContent buildExecutingActionRecoveredCompactBlock(Set<ExecutableActionSnapshot> recoveredExecutingActions, String blockName, String emittedAt, String event) {
return new ActionBlockContent(blockName, SOURCE) {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendEventElement(document, root, event);
appendTextElement(document, root, "emitted_at", emittedAt);
appendListElement(document, root, "recovered_actions", "action", recoveredExecutingActions, (actionElement, action) -> {
appendTextElement(document, actionElement, "description", action.getDescription());
return Unit.INSTANCE;
});
}
};
}
private @NotNull BlockContent buildExecutingActionRecoveredFullBlock(Set<ExecutableActionSnapshot> recoveredExecutingActions, String blockName, String emittedAt, String event) {
return new ActionBlockContent(blockName, SOURCE) {
@Override
protected void fillXml(@NotNull Document document, @NotNull Element root) {
appendEventElement(document, root, event);
appendTextElement(document, root, "emitted_at", emittedAt);
appendListElement(document, root, "recovered_actions", "action", recoveredExecutingActions, (actionElement, action) -> {
appendTextElement(document, actionElement, "description", action.getDescription());
appendTextElement(document, actionElement, "source", action.getSource());
appendTextElement(document, actionElement, "executing_stage", action.getExecutingStage());
return Unit.INSTANCE;
});
}
};
}
void emitStateActionTriggeredBlock(StateAction stateAction) {
StateActionSnapshot snapshot = stateAction.snapshot();
@@ -351,7 +409,6 @@ class ExecutingActionBlockManager {
return "executing_action-" + actionId;
}
private static abstract class ActionBlockContent extends BlockContent {
private ActionBlockContent(@NotNull String blockName, @NotNull String source, @NotNull Urgency urgency) {