feat: add authenticated creator continuity command
CI / test (push) Successful in 11s

This commit is contained in:
m
2026-08-16 16:24:02 +03:00
parent 4a165b5238
commit 6a572ad777
7 changed files with 643 additions and 35 deletions
+57 -35
View File
@@ -14,6 +14,11 @@ import (
"git.cin.su/m/Assistant/modules/commit/ports"
)
var (
errActionAlreadyAttempted = errors.New("action already has an execution attempt")
errActionDoesNotNeedReconcile = errors.New("action does not require reconciliation")
)
type executionRepository interface {
LoadAuthorized(context.Context, commitcontract.AuthorizedActionID) (commitcontract.AuthorizedAction, error)
LoadLatestAttempt(context.Context, commitcontract.AuthorizedActionID) (commitcontract.ActionAttempt, error)
@@ -36,25 +41,17 @@ func registerExecutionRoutes(mux *http.ServeMux, repository executionRepository,
http.Error(w, "authorized action unavailable", http.StatusInternalServerError)
return
}
if _, err = repository.LoadLatestAttempt(r.Context(), action.ID); err == nil {
http.Error(w, "action already has an execution attempt", http.StatusConflict)
return
} else if !errors.Is(err, postgres.ErrActionAttemptNotFound) {
http.Error(w, "action attempt unavailable", http.StatusInternalServerError)
return
}
request, ok := decodeAttemptRequest(w, r)
if !ok {
return
}
begun := commitcontract.ActionAttempt{ID: request.AttemptID, AuthorizedID: action.ID, IntentID: action.Proposal.IntentID, State: commitcontract.ActionExecuting}
if err = repository.BeginAttempt(r.Context(), begun); err != nil {
http.Error(w, "begin execution attempt", http.StatusConflict)
attempt, executeErr := executeAuthorized(r.Context(), repository, executor, action, request.AttemptID)
if errors.Is(executeErr, errActionAlreadyAttempted) {
http.Error(w, "action already has an execution attempt", http.StatusConflict)
return
}
attempt, executeErr := commitdomain.Execute(action, request.AttemptID, executor)
if err = repository.FinishAttempt(r.Context(), attempt); err != nil {
http.Error(w, "finish execution attempt", http.StatusInternalServerError)
if executeErr != nil && !errors.Is(executeErr, commitdomain.ErrUnknownOutcome) {
http.Error(w, "execute authorized action", http.StatusInternalServerError)
return
}
writeAttempt(w, attempt, executeErr)
@@ -70,40 +67,65 @@ func registerExecutionRoutes(mux *http.ServeMux, repository executionRepository,
http.Error(w, "authorized action unavailable", http.StatusInternalServerError)
return
}
previous, err := repository.LoadLatestAttempt(r.Context(), action.ID)
if err != nil {
http.Error(w, "unknown execution attempt not found", http.StatusConflict)
return
}
if previous.State == commitcontract.ActionExecuting {
previous.State = commitcontract.ActionUnknownOutcome
if err = repository.FinishAttempt(r.Context(), previous); err != nil {
http.Error(w, "recover interrupted attempt", http.StatusInternalServerError)
return
}
}
if previous.State != commitcontract.ActionUnknownOutcome {
http.Error(w, "action does not require reconciliation", http.StatusConflict)
return
}
request, ok := decodeAttemptRequest(w, r)
if !ok {
return
}
begun := commitcontract.ActionAttempt{ID: request.AttemptID, AuthorizedID: action.ID, IntentID: action.Proposal.IntentID, State: commitcontract.ActionExecuting}
if err = repository.BeginAttempt(r.Context(), begun); err != nil {
http.Error(w, "begin reconciliation attempt", http.StatusConflict)
attempt, reconcileErr := reconcileAuthorized(r.Context(), repository, reconciler, action, request.AttemptID)
if errors.Is(reconcileErr, errActionDoesNotNeedReconcile) {
http.Error(w, "action does not require reconciliation", http.StatusConflict)
return
}
attempt, reconcileErr := commitdomain.Reconcile(action, previous, request.AttemptID, reconciler)
if err = repository.FinishAttempt(r.Context(), attempt); err != nil {
http.Error(w, "finish reconciliation attempt", http.StatusInternalServerError)
if reconcileErr != nil && !errors.Is(reconcileErr, commitdomain.ErrUnknownOutcome) {
http.Error(w, "reconcile authorized action", http.StatusInternalServerError)
return
}
writeAttempt(w, attempt, reconcileErr)
}))
}
func executeAuthorized(ctx context.Context, repository executionRepository, executor ports.Executor, action commitcontract.AuthorizedAction, attemptID commitcontract.ActionAttemptID) (commitcontract.ActionAttempt, error) {
if _, err := repository.LoadLatestAttempt(ctx, action.ID); err == nil {
return commitcontract.ActionAttempt{}, errActionAlreadyAttempted
} else if !errors.Is(err, postgres.ErrActionAttemptNotFound) {
return commitcontract.ActionAttempt{}, err
}
begun := commitcontract.ActionAttempt{ID: attemptID, AuthorizedID: action.ID, IntentID: action.Proposal.IntentID, State: commitcontract.ActionExecuting}
if err := repository.BeginAttempt(ctx, begun); err != nil {
return commitcontract.ActionAttempt{}, err
}
attempt, operationErr := commitdomain.Execute(action, attemptID, executor)
if err := repository.FinishAttempt(ctx, attempt); err != nil {
return commitcontract.ActionAttempt{}, err
}
return attempt, operationErr
}
func reconcileAuthorized(ctx context.Context, repository executionRepository, reconciler ports.Reconciler, action commitcontract.AuthorizedAction, attemptID commitcontract.ActionAttemptID) (commitcontract.ActionAttempt, error) {
previous, err := repository.LoadLatestAttempt(ctx, action.ID)
if err != nil {
return commitcontract.ActionAttempt{}, errActionDoesNotNeedReconcile
}
if previous.State == commitcontract.ActionExecuting {
previous.State = commitcontract.ActionUnknownOutcome
if err = repository.FinishAttempt(ctx, previous); err != nil {
return commitcontract.ActionAttempt{}, err
}
}
if previous.State != commitcontract.ActionUnknownOutcome {
return commitcontract.ActionAttempt{}, errActionDoesNotNeedReconcile
}
begun := commitcontract.ActionAttempt{ID: attemptID, AuthorizedID: action.ID, IntentID: action.Proposal.IntentID, State: commitcontract.ActionExecuting}
if err = repository.BeginAttempt(ctx, begun); err != nil {
return commitcontract.ActionAttempt{}, err
}
attempt, operationErr := commitdomain.Reconcile(action, previous, attemptID, reconciler)
if err = repository.FinishAttempt(ctx, attempt); err != nil {
return commitcontract.ActionAttempt{}, err
}
return attempt, operationErr
}
func decodeAttemptRequest(w http.ResponseWriter, r *http.Request) (attemptRequest, bool) {
var request attemptRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8<<10)).Decode(&request); err != nil || request.AttemptID == "" {