syntax = "proto3";

package nexuscrm.runtime.v1;

option go_package = "github.com/pingcap-inc/nexuscrm-contract/wire-go/gen/runtime/v1;runtimev1";

// ApplicationResultService exposes only Runtime's immutable application-result
// authority. Command acceptance remains a separate, unchanged service because
// durable inbox admission is not application completion.
service ApplicationResultService {
  rpc GetApplicationResult(GetApplicationResultRequest) returns (GetApplicationResultResponse);
}

// ApplicationResultState deliberately hides Runtime queue, lease, retry, and
// dependency states. A nonterminal command is only pending.
enum ApplicationResultState {
  APPLICATION_RESULT_STATE_UNSPECIFIED = 0;
  APPLICATION_RESULT_STATE_PENDING = 1;
  APPLICATION_RESULT_STATE_TERMINAL = 2;
}

enum ApplicationResultOutcome {
  APPLICATION_RESULT_OUTCOME_UNSPECIFIED = 0;
  APPLICATION_RESULT_OUTCOME_APPLIED = 1;
  APPLICATION_RESULT_OUTCOME_NOOP = 2;
  APPLICATION_RESULT_OUTCOME_REJECTED = 3;
  APPLICATION_RESULT_OUTCOME_FAILED = 4;
}

// The unspecified failure code is valid only for APPLIED and NOOP results.
// Other outcomes require their one disclosure-safe failure code.
enum ApplicationResultFailureCode {
  APPLICATION_RESULT_FAILURE_CODE_UNSPECIFIED = 0;
  APPLICATION_RESULT_FAILURE_CODE_APPLICATION_REJECTED = 1;
  APPLICATION_RESULT_FAILURE_CODE_RUNTIME_EXECUTION_FAILED = 2;
}

// Unknown, expired, and digest-mismatched pairs share the same top-level
// missing failure; the wire response never discloses which case occurred.
message GetApplicationResultRequest {
  string command_id = 1;
  string execution_ref_digest = 2;
}

// ApplicationResult is the immutable terminal fact committed by Runtime with
// the application transaction. It contains only digests and a deterministic
// commit receipt, never Runtime-owned result payload or internal error text.
message ApplicationResult {
  string command_id = 1;
  string execution_ref_digest = 2;
  ApplicationResultOutcome outcome = 3;
  string result_digest = 4;
  string runtime_commit_receipt = 5;
  ApplicationResultFailureCode failure_code = 6;
}

message GetApplicationResultResponse {
  string command_id = 1;
  string execution_ref_digest = 2;
  ApplicationResultState state = 3;
  // Present exactly when state is APPLICATION_RESULT_STATE_TERMINAL.
  ApplicationResult result = 4;
}
