载入中...
搜索中...
未找到
SequenceRuntime.cpp
浏览该文件的文档.
61eve::Value captureFrame(const SequenceAsset* asset, const std::string& node, const eve::Value& bindings,
114eve::Result<void> SequenceRuntime::fail(eve::DiagnosticCode code, std::string message, std::string path) {
116 return eve::Result<void>::failure(eve::Diagnostic::error(code, message, std::move(path), {}, "dnut.runtime"));
119void SequenceRuntime::emit(EventKind kind, const SequenceNode* node, const std::string& detail) const {
145void SequenceRuntime::unregisterCommand(std::string_view name) { commandHandlers_.erase(std::string(name)); }
150 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: bindings must be an object", "bindings");
212 if (!asset_) return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: no active asset", "node");
241 eve::Diagnostic::error(eve::DiagnosticCode::Failed, outcome.error, "condition", {}, "dnut.runtime"));
258 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: execution budget exceeded", "budget");
260 if (!node) return fail(eve::DiagnosticCode::InvariantViolation, "sequence: invalid execution cursor", "cursor");
364 if (const eve::Value* arguments = node->payload.find("arguments"); arguments && arguments->isObject())
390 if (const eve::Value* arguments = node->payload.find("arguments"); arguments && arguments->isObject())
446 return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: runtime is not blocked", "advance");
495eve::Result<void> SequenceRuntime::select(std::string_view routeLabel) { return selectImpl(routeLabel); }
515 if (!outcome.error.empty()) return fail(eve::DiagnosticCode::Failed, outcome.error, "route.condition");
549 if (const eve::Value* key = node->payload.find("result"); key && key->isString() && !key->asString().empty())
606 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: loop missing mode", "loop.mode");
609 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: loop missing body", "loop.routes");
659 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: unknown loop mode '" + mode + "'", "loop.mode");
662eve::Result<void> SequenceRuntime::handleBreakOrContinue(const SequenceNode& node, bool isBreak) {
677 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel needs two lanes", "parallel");
680 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel missing join", "parallel.join");
698 return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: join without parallel", "join");
715 return fail(eve::DiagnosticCode::NotFound, "sequence: parallel lane missing node '" + lane.nodeId + "'",
729 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: jump has no target", "jump.target");
738 if (node->type == "choice" || node->type == "command" || node->type == "call" || node->type == "parallel" ||
739 node->type == "loop" || node->type == "break" || node->type == "continue" || node->type == "end" ||
764 else if (const SequenceNode* landed = asset_->findNode(lane.nodeId); landed && landed->type == "join")
771 return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: no parallel frame", "parallel");
775 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel budget exceeded", "budget");
819 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel made no progress", "parallel");
823eve::Result<void> SequenceRuntime::resumeCommand(std::string_view requestId, eve::StateValue result) {
827eve::Result<void> SequenceRuntime::resumeCommand(std::string_view requestId, eve::Value result) {
831eve::Result<void> SequenceRuntime::resumeCommandImpl(std::string_view requestId, eve::Value result) {
834 return fail(eve::DiagnosticCode::DialogueNotWaitingForCommand, "sequence: runtime is not waiting for a command",
837 return fail(eve::DiagnosticCode::Conflict, "sequence: command request id is stale or does not match",
861 return fail(eve::DiagnosticCode::Failed, error.empty() ? "sequence: command resume failed" : std::move(error),
934 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state must be an object", "state");
940 return fail(eve::DiagnosticCode::UnknownVersion, "sequence: unsupported runtime save schema or version",
945 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'active'", "state");
956 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'current'", "state");
960 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: saved frame is malformed", "state.current");
964 return fail(eve::DiagnosticCode::NotFound, "sequence: saved asset '" + assetId + "' is missing", "state.asset");
972 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: saved values are malformed", "state.current");
974 const auto readLoopStack = [&](const eve::Value* value, std::vector<LoopFrame>& outFrames) -> eve::Result<void> {
978 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: loopStack is malformed", "state.loopStack");
990 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: loop frame counters are malformed",
1010 !readString(item, "joinNext", frame.joinNext) || !readString(item, "joinNodeId", frame.joinNodeId))
1016 if (!activeLane || !activeLane->isInt64() || !hostBlocked || !hostBlocked->isBool() || !lanes ||
1018 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallel frame lanes are malformed",
1034 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallel lane flags are malformed",
1049 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'stack'", "state");
1065 savedVersion->asInt() != savedAsset->version || !frameBindings || !frameBindings->isObject() ||
1067 return fail(eve::DiagnosticCode::Conflict, "sequence: saved call frame cannot be restored", "state.stack");
1070 if (auto loops = readLoopStack(saved.find("loopStack"), frame.loopStack); !loops.ok()) return loops;
1071 if (auto parallels = readParallelStack(saved.find("parallelStack"), frame.parallelStack); !parallels.ok())
1079 if (auto loops = readLoopStack(in.find("loopStack"), restoredLoops); !loops.ok()) return loops;
1080 if (auto parallels = readParallelStack(in.find("parallelStack"), restoredParallels); !parallels.ok())
1085 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'blocked'", "state");
1088 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: waitingStep is malformed", "state");
1091 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: waitingCommand is malformed", "state");
1094 if (!pendingRequestId || !pendingRequestId->isString() || !commandSequence || !commandSequence->isInt64() ||
1096 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: pending command state is malformed", "state");
1102 return fail(eve::DiagnosticCode::NotFound, "sequence: saved node '" + nodeId + "' is missing", "state.node");
1105 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: pending command cursor is not a command node",
1128 if (const eve::Value* arguments = restoredNode->payload.find("arguments"); arguments && arguments->isObject())
std::vector< std::vector< CompiledStatement > > lanes
Parallel lane bodies (parallel { lane {…} lane {…} }).
Definition DnutCompiler.cpp:36
Cross-frame interpreter for compiled `.dnut` sequences.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:514
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
@ Float
@ String
@ Array
@ Object
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
std::vector< std::string > keys() const
Return object member names in deterministic order.
Definition Value.cpp:258
void set(const std::string &key, Value value)
Insert or replace an object member.
Definition Value.cpp:238
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
std::int64_t asInt() const
Return the Int64 payload; the caller must have checked the kind.
Definition Value.cpp:189
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:115
bool asBool() const
Return the boolean payload; the caller must have checked the kind.
Definition Value.cpp:201
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
eve::Result< void > runUntilBlocked()
Execute nodes until the sequence blocks, ends or fails.
Definition SequenceRuntime.cpp:247
const SequenceNode * currentNode() const
Borrowed current node, or nullptr. @lifetime Invalidated by the next transition.
Definition SequenceRuntime.cpp:74
static constexpr std::int64_t MinSaveVersion
Oldest restoreable cursor save schema version.
Definition SequenceRuntime.h:75
void stop()
Stop and clear the active sequence without emitting Ended.
Definition SequenceRuntime.cpp:124
static constexpr std::string_view SaveSchema
Stable cursor save schema retained across the L1 ownership migration.
Definition SequenceRuntime.h:71
eve::Result< void > captureState(eve::Value &out) const
Capture the complete cursor, bindings, locals and call stack.
Definition SequenceRuntime.cpp:865
static constexpr std::int64_t SaveVersion
Current cursor save schema version (v3 adds loop/parallel stacks).
Definition SequenceRuntime.h:73
static constexpr int kExecutionBudget
Upper bound on nodes executed by one runUntilBlocked call.
Definition SequenceRuntime.h:108
eve::Result< void > advance()
Acknowledge a host-presented wait or handler-less Await step.
Definition SequenceRuntime.cpp:443
@ NodeEntered
eve::Result< void > resumeCommand(std::string_view requestId, eve::StateValue result)
Resume the exact blocked command with a legacy state value.
Definition SequenceRuntime.cpp:823
eve::Result< void > start(const SequenceAsset *asset, eve::Value bindings=eve::Value::Object{})
Begin a sequence at the asset entry node.
Definition SequenceRuntime.cpp:147
void unregisterCommand(std::string_view name)
Remove a named generic command handler.
Definition SequenceRuntime.cpp:145
void registerCommand(std::string name, SequenceCommandHandler handler)
Register a named generic command handler.
Definition SequenceRuntime.cpp:141
eve::Result< void > resumeStep(eve::Value result)
Resume a step whose handler returned Blocked.
Definition SequenceRuntime.cpp:542
const SequenceAsset * asset() const
Borrowed active asset, or nullptr. @lifetime Owned by the caller of start.
Definition SequenceRuntime.h:221
const eve::Value & bindings() const
Definition SequenceRuntime.h:225
eve::Result< void > selectRouteForTransaction(std::string_view routeLabel)
Enter one already prepared choice route transactionally.
Definition SequenceRuntime.cpp:497
eve::Result< void > select(std::string_view routeLabel)
Answer a blocked choice node by route label.
Definition SequenceRuntime.cpp:495
eve::Result< void > restoreState(const eve::Value &in)
Restore a captured cursor through the configured asset resolver.
Definition SequenceRuntime.cpp:932
StepOutcome dispatch(const SequenceNode &node, const StepContext &context) const
Route one already validated node to its handler.
Definition StepKindRegistry.cpp:150
eve::Value toCanonicalValue(const eve::StateValue &value)
Convert the legacy reload value into the canonical common value.
Definition DialogueState.cpp:203
Definition ConversationDialect.cpp:13
@ Blocked
@ Failed
const char * sequenceParameterTypeName(SequenceParameterType type) noexcept
Return the stable lowercase spelling of a parameter type.
Definition SequenceAsset.cpp:19
SequenceParameterType
Declared value kind for one sequence invocation parameter.
Definition SequenceAsset.h:18
std::function< SequenceCommandResponse(const SequenceCommandRequest &)> SequenceCommandHandler
Synchronous owner-thread handler for one command request.
Definition SequenceRuntime.h:54
void keys(const Value &value, std::initializer_list< const char * > names)
Definition OceanCodecInternal.h:8
bool readString(const eve::Value::Object &object, const char *name, std::string &output)
Definition ArtifactProvider.cpp:21
@ InvalidArgument
@ NotFound
@ UnknownVersion
@ PreconditionViolation
@ DialogueNotWaitingForCommand
Dialogue runner is not positioned at an asynchronous command.
@ InvariantViolation
@ Failed
@ Conflict
@ Applied
Definition Container.h:602
Immutable, parameterized and versioned compiled sequence.
Definition SequenceAsset.h:78
eve::Result< void > validate() const
Validate stable identifiers, the entry point and every graph reference.
Definition SequenceAsset.cpp:37
const SequenceNode * findNode(const std::string &nodeId) const noexcept
Find one node by its stable identifier.
Definition SequenceAsset.cpp:30
std::vector< SequenceParameter > parameters
Definition SequenceAsset.h:83
Immutable request emitted by a generic command node.
Definition SequenceRuntime.h:33
Outcome of one authored condition, as seen by the language core.
Definition SequenceRuntime.h:24
std::string error
Evaluation failure; non-empty means no route may be selected.
Definition SequenceRuntime.h:29
std::string reason
Stable machine-readable reason; empty when passed is true.
Definition SequenceRuntime.h:27