package store import ( "crypto/sha256" "database/sql" "encoding/hex" "fmt" ) // LocalPendingOriginationForCommand resolves only a durably accepted, held // execution for the exact delivered command bytes. Missing rows include // rejected/terminal commands and already issued or refused decisions. func (s *Store) LocalPendingOriginationForCommand(body []byte) (string, error) { if len(body) == 0 { return "", sql.ErrNoRows } hash := sha256.Sum256(body) s.mu.Lock() defer s.mu.Unlock() var executionID string err := s.db.QueryRow(`SELECT t.execution_id FROM tasks t JOIN inbox i ON i.command_id=t.task_item_id AND i.tenant_id=t.tenant_id AND i.tenant_key=t.tenant_key JOIN reservations r ON r.execution_id=t.execution_id AND r.state='held' WHERE i.command_type='call.execute' AND i.status='persisted' AND i.body_hash=? AND i.body=? AND t.status='reserved' AND NOT EXISTS (SELECT 1 FROM local_v02_origination_decisions d WHERE d.execution_id=t.execution_id)`, hex.EncodeToString(hash[:]), body).Scan(&executionID) if err != nil { return "", err } return executionID, nil } func (s *Store) LocalOriginationDecision(executionID string) (string, error) { if executionID == "" { return "", ErrLocalOriginationNotAuthorized } s.mu.Lock() defer s.mu.Unlock() var decision string if err := s.db.QueryRow(`SELECT decision FROM local_v02_origination_decisions WHERE execution_id=?`, executionID).Scan(&decision); err != nil { return "", fmt.Errorf("read local origination decision: %w", err) } return decision, nil }